Guest User

MainActivity

a guest
Sep 14th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 59.18 KB | None | 0 0
  1. package com.example.user.aitzik4;
  2.  
  3. import android.app.Activity;
  4.  
  5. import android.app.AlertDialog;
  6. import android.content.ComponentName;
  7. import android.content.ContentResolver;
  8. import android.content.Context;
  9. import android.content.DialogInterface;
  10. import android.content.SharedPreferences;
  11. import android.content.pm.PackageManager;
  12. import android.content.res.AssetManager;
  13. import android.content.res.Resources;
  14. import android.database.Cursor;
  15. import android.graphics.Bitmap;
  16. import android.graphics.BitmapFactory;
  17. import android.graphics.Canvas;
  18. import android.graphics.Matrix;
  19. import android.os.AsyncTask;
  20. import android.os.Bundle;
  21. import android.os.CancellationSignal;
  22. import android.os.Environment;
  23. import android.os.ParcelFileDescriptor;
  24. import android.os.Parcelable;
  25. import android.print.PageRange;
  26. import android.print.PrintAttributes;
  27. import android.print.PrintDocumentAdapter;
  28. import android.print.PrintDocumentInfo;
  29. import android.print.PrintManager;
  30. import android.provider.MediaStore;
  31. import android.support.v4.print.PrintHelper;
  32. import android.telephony.PhoneNumberUtils;
  33. import android.text.format.DateFormat;
  34. import android.util.Base64;
  35. import android.util.Log;
  36. import android.widget.Button;
  37. import android.view.View;
  38. import android.content.Intent;
  39. import android.net.Uri;
  40. import android.widget.EditText;
  41. import android.widget.ImageView;
  42. import android.widget.ProgressBar;
  43. import android.widget.Toast;
  44.  
  45. import java.io.BufferedReader;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileNotFoundException;
  50. import java.io.FileOutputStream;
  51. import java.io.FileWriter;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.io.InputStreamReader;
  55. import java.io.OutputStream;
  56. import java.net.HttpURLConnection;
  57. import java.net.MalformedURLException;
  58. import java.net.URL;
  59. import java.util.ArrayList;
  60. import java.util.Arrays;
  61. import java.util.Random;
  62. import java.util.Set;
  63. import java.util.concurrent.ExecutionException;
  64.  
  65.  
  66. import okhttp3.Call;
  67. import okhttp3.Callback;
  68. import okhttp3.FormBody;
  69. import okhttp3.OkHttpClient;
  70. import okhttp3.Request;
  71. import okhttp3.RequestBody;
  72. import okhttp3.Response;
  73.  
  74. import static android.provider.MediaStore.ACTION_IMAGE_CAPTURE;
  75.  
  76. public class MainActivity extends Activity {
  77.     ImageView tv1;
  78.     Button share;
  79.     String lastStamp = null;
  80.     String tempStamp = null;
  81.     String printTime = null;
  82.     String latestFrame;
  83.     String [] getterarray;
  84.     String [] whatsappArray;
  85.     Uri [] emailArray;
  86.     String filePath;
  87.     String [] filePathArray;
  88.     EditText mEdit;
  89.     final int FIRST_PICTURE_TIME=120*1000;
  90.     final int ADDITIONAL_PICTURE_TIME=50*1000;
  91.     EditText mEdit2;
  92.     String number;
  93.     Boolean posting=false;
  94.     ImageView imageView,logo,temp, imageview4, imageview3;
  95.     String emailx;
  96.     final String stampPath="/sdcard/Aitzik4",
  97.                 filesInQueue="/sdcard/Aitzik4",
  98.                 counterPath="/sdcard/Aitzik4";
  99.     Bitmap merge;
  100.     ProgressBar pb;
  101.     String dummy1="/sdcard/Pictures/Frames/frame5.png",
  102.             dummy2="/sdcard/DCIM/Camera/2018-05-21 23.26.54_0.jpg";
  103.     Uri imageUri;
  104.     String framepath=null;
  105.  
  106.     @Override
  107.     protected void onCreate(Bundle savedInstanceState) {
  108.         super.onCreate(savedInstanceState);
  109.         setContentView(R.layout.activity_main);
  110.         latestFrame=getNewSaveDefaultArray();
  111.  
  112.         //latestFrame="sdcard/Pictures/Frames/frame1.png";
  113.         temp = (ImageView) findViewById(R.id.imageView2);
  114.         temp.setImageURI(Uri.parse(latestFrame));
  115.         logo= (ImageView) findViewById(R.id.imageView);
  116.  
  117.  
  118.         check_for_updates();
  119.  
  120.  
  121.  
  122.         AssetManager assetManager = getAssets();
  123.         InputStream istr = null;
  124.         try {
  125.             istr = assetManager.open("logo_300x70.png");
  126.         } catch (IOException e) {
  127.             e.printStackTrace();
  128.         }
  129.         logo.setImageBitmap(BitmapFactory.decodeStream(istr));
  130.  
  131.         pb = (ProgressBar)findViewById(R.id.loading_progress_xml);
  132.         pb.setVisibility(View.INVISIBLE);
  133.  
  134.  
  135.         Button share=findViewById(R.id.share);
  136.         Button frames=findViewById(R.id.frame);
  137.         final Button email=findViewById(R.id.email);
  138.  
  139.         frames.setOnClickListener(new View.OnClickListener() {
  140.             @Override
  141.             public void onClick(View view) {
  142.                 Intent i = new Intent(
  143.                         Intent.ACTION_PICK,
  144.                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  145.  
  146.                 startActivityForResult(i, 1);
  147.             }
  148.         });
  149.  
  150.         email.setOnClickListener(new View.OnClickListener() {
  151.             public void onClick(View v) {
  152.                // pb.setVisibility(View.VISIBLE);
  153.                 EditText et=(EditText)findViewById(R.id.editText2);
  154.                 emailx=et.getText().toString();
  155.                 AssetManager assetManager = getAssets();
  156.                 InputStream important = null;
  157.                 try {
  158.                     important = assetManager.open("important.png");
  159.                 } catch (IOException e) {
  160.                     e.printStackTrace();
  161.                 }
  162.  
  163.                 String now = System.currentTimeMillis() + "".trim();
  164.                 String myFileName = now;
  165.                 String path = "/sdcard/Aitzik4/";
  166.  
  167.                 Bitmap allthree=null;
  168.                 if(filePath != null){//one image
  169.                      allthree = overlay(BitmapFactory.decodeStream(important),filepathtobitmap(filePath),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  170.                     emailArray=new Uri[1];
  171.                     emailArray[0]=bitmapToUriConverter(allthree);
  172.                 }else{//multipul
  173.                     emailArray=new Uri[filePathArray.length];
  174.                     for(int i = 0; i<filePathArray.length; i++){
  175.                         allthree = overlay(BitmapFactory.decodeStream(important),filepathtobitmap(filePathArray[i]),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  176.                         emailArray[i]=bitmapToUriConverter(allthree);
  177.                     }
  178.                 }
  179.  
  180.                 Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
  181.  
  182.                 intent.putExtra(Intent.EXTRA_EMAIL, emailx);
  183.                 intent.putExtra(Intent.EXTRA_SUBJECT, "התמונות המגניבות שלכם");
  184.                 intent.putExtra(Intent.EXTRA_TEXT, "מקווים שנהנתם!");
  185.                 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, new ArrayList<>(Arrays.asList(emailArray)));
  186.                 intent.setType("image/png");
  187.                 intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
  188.                 startActivity(intent);
  189.                 //startActivity(Intent.createChooser(intent, "Choice App to send email:"));
  190.                // Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",emailx, null));
  191.               //  emailIntent.putExtra(Intent.EXTRA_SUBJECT, "hello there");
  192.  
  193.                 //emailIntent.setType("application/image");
  194.  
  195.  
  196.                 //emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
  197.  
  198.                /* ArrayList<Uri> imageUriArray = new ArrayList<Uri>();
  199.                 for(int i = 0; i<emailArray.length; i++) {
  200.                     imageUriArray.add(Uri.parse(Uri.decode(emailArray[i].trim())));
  201.                     //temp[i] = Uri.parse(Uri.decode(picture[i].trim()));
  202.                     //Log.d("DEBUG LMAO",temp[i]+"");
  203.                     Log.d("DEBUG LMAO",emailArray[i]+"");
  204.                 }*/
  205.                 // sendIntent.putExtra(Intent.EXTRA_STREAM, temp);
  206.              //   emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUriArray);
  207.                // emailIntent.putExtra(Intent.EXTRA_STREAM, "file:///data/data/com.example.user.aitzik4/files/Image-79271305.jpeg");
  208.  
  209.                // startActivity(Intent.createChooser(emailIntent, "Send email..."));
  210.                 //merge=overlay(BitmapFactory.decodeStream(important),filepathtobitmap(filePath),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  211.                 //alert(toRealPath(latestFrame));
  212.                 // String disposablef=latestFrame;
  213.                 // String disposablei=filePath;
  214.                 //  merge=null;
  215.  
  216.                 // imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
  217.                 // final String base65=BitmapToString(merge);
  218.                 /*
  219.                 AsyncTask.execute(new Runnable() {
  220.                     @Override
  221.                     public void run() {
  222.                         if(!posting) {
  223.                             posting=true;
  224.                             postf(emailx, "email");
  225.  
  226.                         }
  227.  
  228.                     }
  229.                 });
  230.                 */
  231.             }
  232.         });
  233.  
  234.         share.setOnClickListener(new View.OnClickListener() {
  235.             public void onClick(View v) {
  236.  
  237.                 String now = System.currentTimeMillis() + "".trim();
  238.                 String myFileName = now;
  239.                 String path = "/sdcard/Aitzik4/";
  240.  
  241.                 AssetManager assetManager = getAssets();
  242.                 InputStream important = null;
  243.                 try {
  244.                     important = assetManager.open("important.png");
  245.                 } catch (IOException e) {
  246.                     e.printStackTrace();
  247.                 }
  248.  
  249.  
  250.                 if(filePath != null) {
  251.                     Bitmap allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePath), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  252.  
  253.                     //stamp wasnt found
  254.                     //alert("There is no stamp");
  255.  
  256.                     savePNG(allthree, path, myFileName);
  257.  
  258.                     createHTML(path, myFileName);// if no file is found
  259.                 }else{
  260.  
  261.                     Bitmap allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePathArray[0]), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  262.  
  263.                     //stamp wasnt found
  264.                     //alert("There is no stamp");
  265.  
  266.                     savePNG(allthree, path, myFileName);
  267.  
  268.                     createHTML(path, myFileName);// if no file is found
  269.  
  270.                     for(int i = 1; i<filePathArray.length; i++){
  271.  
  272.                         allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePathArray[i]), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  273.  
  274.                         savePNG(allthree, path, myFileName+i+"");
  275.  
  276.                         addPictureToTheEndOfAnHtmlFile(myFileName, myFileName+i+"");
  277.                     }
  278.  
  279.                 }
  280.  
  281.  
  282.  
  283.                 Intent shareIntent = new Intent();
  284.                 shareIntent.setAction(Intent.ACTION_SEND);
  285.                 shareIntent.putExtra(Intent.EXTRA_TEXT,"file:///"+path+myFileName+".html");
  286.                 shareIntent.setType("text/plain");
  287.                 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  288.                 shareIntent.setClassName("com.happy2print.premium", "com.dynamixsoftware.printhand.ui.phone.ActivityDetails");
  289.                 //startActivity(Intent.createChooser(shareIntent, "Share images..."));
  290.                 startActivity(shareIntent);
  291.  
  292.                 /*
  293.                 // Bitmap allthree=overlay(filepathtobitmap(filePath),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  294.  
  295.                 //readStampFileOrCreateNewIfNotExists();
  296.  
  297.                 // String myFileName = lastStamp;
  298.                 // String path = "/sdcard/Aitzik4/";
  299.                 // savePNG(allthree,path,myFileName);
  300.  
  301.  
  302.                 if(IsPrintTime()){
  303.                     //stamp is found
  304.  
  305.                     lastStamp = readStampFileOrCreateNewIfNotExists();
  306.                     tempStamp = readTempStampFileOrCreateNewIfNotExists();
  307.                     String myFileName = lastStamp;
  308.  
  309.                     String path = "/sdcard/Aitzik4/";
  310.                     //alert("#aa12eas: stampNowTimeDifference is "+stampNowTimeDifference());
  311.  
  312.                     if(stampNowTimeDifference()>calculateLastQueue()) {
  313.                         //enough time has passed and printer is not busy
  314.                         //alert("Printer should not be busy right now");
  315.  
  316.  
  317.                         //alert("1534013579: "+stampNowTimeDifference() + " difference "+ calculateLastQueue());
  318.                         deletePrintTime();
  319.                         printTime = readPrintTimeFileOrCreateNewIfNotExists();
  320.                         addOnetoCounter();
  321.                         deleteStamp();
  322.                         makeTempStampNotTemp();
  323.                         saveToLastQueue();
  324.                         deleteCounter();
  325.  
  326.                         myFileName = now;
  327.                         //saveDataToStampFile(myFileName);
  328.                         //lastStamp = myFileName;
  329.                         lastStamp = tempStamp;
  330.  
  331.                         savePNG(allthree,path,myFileName);
  332.                         addPictureToTheEndOfAnHtmlFile(lastStamp,now);
  333.  
  334.                         Intent shareIntent1 = new Intent();
  335.                         shareIntent1.setAction(Intent.ACTION_SEND);
  336.                         shareIntent1.putExtra(Intent.EXTRA_TEXT,"file:///"+"/sdcard/AItzik4/"+lastStamp+".html");
  337.                         shareIntent1.setType("text/plain");
  338.                         shareIntent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  339.                         startActivity(Intent.createChooser(shareIntent1, "Share images..."));
  340.  
  341.  
  342.                     }else{
  343.                         alert("המדפסת עסוקה, הקובץ נוסף לתור");
  344.  
  345.  
  346.  
  347.                         if(!waitingForPrint()){
  348.                             //no files in queue for printing
  349.  
  350.                             lastStamp = updateStamp();
  351.                             tempStamp = readTempStampFileOrCreateNewIfNotExists();
  352.                             //alert("This is the first file in queue");
  353.  
  354.                             savePNG(allthree,path,tempStamp);
  355.                             createHTML(path,tempStamp);
  356.  
  357.                             addOnetoCounter();
  358.                         }else{
  359.                             //alert("We have some files who are wating for be printed");
  360.  
  361.                             String stamp = tempStamp;
  362.                             savePNG(allthree,path,now);
  363.                             myFileName = now;
  364.  
  365.                             addPictureToTheEndOfAnHtmlFile(stamp,myFileName);
  366.  
  367.                             try{
  368.                                 addOnetoCounter();
  369.                             }catch (Exception  e){
  370.                                 alert("Error while trying to addOnetoCounter. error #1534033234");
  371.                                 alert(e.getMessage());
  372.                             }
  373.                         }
  374.  
  375.                     }
  376.  
  377.  
  378.                 }else{
  379.                     //stamp wasnt found
  380.                     //alert("There is no stamp");
  381.                     printTime = readPrintTimeFileOrCreateNewIfNotExists();
  382.                     lastStamp = readStampFileOrCreateNewIfNotExists();
  383.                     //alert("1534072710 lastStamp is:"+lastStamp);
  384.  
  385.                     String myFileName = lastStamp;
  386.                     String path = "/sdcard/Aitzik4/";
  387.                     savePNG(allthree,path,myFileName);
  388.  
  389.                     createHTML(path,myFileName);// if no file is found
  390.                     Intent shareIntent = new Intent();
  391.                     shareIntent.setAction(Intent.ACTION_SEND);
  392.                     shareIntent.putExtra(Intent.EXTRA_TEXT,"file:///"+path+myFileName+".html");
  393.                     shareIntent.setType("text/plain");
  394.                     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  395.                     startActivity(Intent.createChooser(shareIntent, "Share images..."));
  396.  
  397.                 }
  398.             }
  399.             */
  400.             }
  401.         });
  402.  
  403.         /*
  404.         share.setOnClickListener(new View.OnClickListener() {
  405.             public void onClick(View v) {
  406.                 String now = System.currentTimeMillis() + "".trim();
  407.                 ;
  408.  
  409.                 AssetManager assetManager = getAssets();
  410.                 InputStream important = null;
  411.                 try {
  412.                     important = assetManager.open("important.png");
  413.                 } catch (IOException e) {
  414.                     e.printStackTrace();
  415.                 }
  416.  
  417.                 Bitmap allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePath), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  418.  
  419.                 //stamp wasnt found
  420.                 //alert("There is no stamp");
  421.  
  422.                 String myFileName = now;
  423.                 String path = "/sdcard/Aitzik4/";
  424.                 savePNG(allthree,path,myFileName);
  425.  
  426.                 createHTML(path,myFileName);// if no file is found
  427.                 Intent shareIntent = new Intent();
  428.                 shareIntent.setAction(Intent.ACTION_SEND);
  429.                 shareIntent.putExtra(Intent.EXTRA_TEXT,"file:///"+path+myFileName+".html");
  430.                 shareIntent.setType("text/plain");
  431.                 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  432.                 startActivity(Intent.createChooser(shareIntent, "Share images..."));
  433.  
  434.                 /*
  435.                 // Bitmap allthree=overlay(filepathtobitmap(filePath),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  436.  
  437.                 //readStampFileOrCreateNewIfNotExists();
  438.  
  439.                 // String myFileName = lastStamp;
  440.                 // String path = "/sdcard/Aitzik4/";
  441.                 // savePNG(allthree,path,myFileName);
  442.  
  443.  
  444.                 if(IsPrintTime()){
  445.                     //stamp is found
  446.  
  447.                     lastStamp = readStampFileOrCreateNewIfNotExists();
  448.                     tempStamp = readTempStampFileOrCreateNewIfNotExists();
  449.                     String myFileName = lastStamp;
  450.  
  451.                     String path = "/sdcard/Aitzik4/";
  452.                     //alert("#aa12eas: stampNowTimeDifference is "+stampNowTimeDifference());
  453.  
  454.                     if(stampNowTimeDifference()>calculateLastQueue()) {
  455.                         //enough time has passed and printer is not busy
  456.                         //alert("Printer should not be busy right now");
  457.  
  458.  
  459.                         //alert("1534013579: "+stampNowTimeDifference() + " difference "+ calculateLastQueue());
  460.                         deletePrintTime();
  461.                         printTime = readPrintTimeFileOrCreateNewIfNotExists();
  462.                         addOnetoCounter();
  463.                         deleteStamp();
  464.                         makeTempStampNotTemp();
  465.                         saveToLastQueue();
  466.                         deleteCounter();
  467.  
  468.                         myFileName = now;
  469.                         //saveDataToStampFile(myFileName);
  470.                         //lastStamp = myFileName;
  471.                         lastStamp = tempStamp;
  472.  
  473.                         savePNG(allthree,path,myFileName);
  474.                         addPictureToTheEndOfAnHtmlFile(lastStamp,now);
  475.  
  476.                         Intent shareIntent1 = new Intent();
  477.                         shareIntent1.setAction(Intent.ACTION_SEND);
  478.                         shareIntent1.putExtra(Intent.EXTRA_TEXT,"file:///"+"/sdcard/AItzik4/"+lastStamp+".html");
  479.                         shareIntent1.setType("text/plain");
  480.                         shareIntent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  481.                         startActivity(Intent.createChooser(shareIntent1, "Share images..."));
  482.  
  483.  
  484.                     }else{
  485.                         alert("המדפסת עסוקה, הקובץ נוסף לתור");
  486.  
  487.  
  488.  
  489.                         if(!waitingForPrint()){
  490.                             //no files in queue for printing
  491.  
  492.                             lastStamp = updateStamp();
  493.                             tempStamp = readTempStampFileOrCreateNewIfNotExists();
  494.                             //alert("This is the first file in queue");
  495.  
  496.                             savePNG(allthree,path,tempStamp);
  497.                             createHTML(path,tempStamp);
  498.  
  499.                             addOnetoCounter();
  500.                         }else{
  501.                             //alert("We have some files who are wating for be printed");
  502.  
  503.                             String stamp = tempStamp;
  504.                             savePNG(allthree,path,now);
  505.                             myFileName = now;
  506.  
  507.                             addPictureToTheEndOfAnHtmlFile(stamp,myFileName);
  508.  
  509.                             try{
  510.                                 addOnetoCounter();
  511.                             }catch (Exception  e){
  512.                                 alert("Error while trying to addOnetoCounter. error #1534033234");
  513.                                 alert(e.getMessage());
  514.                             }
  515.                         }
  516.  
  517.                     }
  518.  
  519.  
  520.                 }else{
  521.                     //stamp wasnt found
  522.                     //alert("There is no stamp");
  523.                     printTime = readPrintTimeFileOrCreateNewIfNotExists();
  524.                     lastStamp = readStampFileOrCreateNewIfNotExists();
  525.                     //alert("1534072710 lastStamp is:"+lastStamp);
  526.  
  527.                     String myFileName = lastStamp;
  528.                     String path = "/sdcard/Aitzik4/";
  529.                     savePNG(allthree,path,myFileName);
  530.  
  531.                     createHTML(path,myFileName);// if no file is found
  532.                     Intent shareIntent = new Intent();
  533.                     shareIntent.setAction(Intent.ACTION_SEND);
  534.                     shareIntent.putExtra(Intent.EXTRA_TEXT,"file:///"+path+myFileName+".html");
  535.                     shareIntent.setType("text/plain");
  536.                     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  537.                     startActivity(Intent.createChooser(shareIntent, "Share images..."));
  538.  
  539.                 }
  540.             }
  541.             *//*
  542.             }
  543.         });
  544.         */
  545.  
  546.         final Button button = findViewById(R.id.whatsapp);
  547.         button.setOnClickListener(new View.OnClickListener() {
  548.             public void onClick(View v) {
  549.                // pb.setVisibility(View.VISIBLE);
  550.                 mEdit   = (EditText)findViewById(R.id.editText);
  551.                 number=mEdit.getText().toString();
  552.                 AssetManager assetManager = getAssets();
  553.                 InputStream important = null;
  554.                 try {
  555.                     important = assetManager.open("important.png");
  556.                 } catch (IOException e) {
  557.                     e.printStackTrace();
  558.                 }
  559.  
  560.                 String now = System.currentTimeMillis() + "".trim();
  561.                 String myFileName = now;
  562.                 String path = "/sdcard/Aitzik4/";
  563.                 if(filePath != null) {
  564.                     Bitmap allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePath), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  565.  
  566.                     //stamp wasnt found
  567.                     //alert("There is no stamp");
  568.                   //  convertFileToContentUri
  569.                     //savePNG(allthree, path, myFileName);
  570.                     //Log.d("DEBUG K", path+myFileName);
  571.                    //Uri myUri = Uri.fromFile(new File(path+myFileName+".png"));
  572.                   // Log.d("DEBUG K", myUri+"");
  573.                    whatsappArray=new String[1];
  574.                     whatsappArray[0]=bitmapToUriConverter(allthree)+"";
  575.                 }else{
  576.                     whatsappArray=new String[filePathArray.length];
  577.                     for(int i = 0; i<filePathArray.length; i++){
  578.  
  579.                         Bitmap allthree = overlay(BitmapFactory.decodeStream(important), filepathtobitmap(filePathArray[i]), filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  580.                         whatsappArray[i]=bitmapToUriConverter(allthree)+"";
  581.  
  582.  
  583.                     }
  584.                 }
  585.  
  586.                 //  merge=overlay(BitmapFactory.decodeStream(important),filepathtobitmap(filePath),filepathtobitmap(toRealPath(getNewSaveDefaultArray())));
  587.  
  588.                 AsyncTask.execute(new Runnable() {
  589.                     @Override
  590.                     public void run() {
  591.  
  592.                         if(!posting) {
  593.                             //posting=true;
  594.                             openWhatsApp(number,whatsappArray);
  595.                             //postf(number, "number");
  596.                         }
  597.  
  598.                     }
  599.                 });
  600.  
  601.  
  602.             }
  603.  
  604.  
  605.  
  606.  
  607.  
  608.         });
  609.  
  610.         Intent intent = getIntent();
  611.         String action = intent.getAction();
  612.         String type = intent.getType();
  613.         if ((Intent.ACTION_SEND.equals(action)||Intent.ACTION_SEND_MULTIPLE.equals(action)) && type != null) {
  614.  
  615.             if ("text/plain".equals(type)) {
  616.                 handleSendText(intent); // Handle text being sent
  617.             } else if (type.startsWith("image/")) {
  618.                 handleSendImage(intent);
  619.                 // Handle single image being sent
  620.             }
  621.         } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
  622.             if (type.startsWith("image/")) {
  623.                 //  handleSendMultipleImages(intent); // Handle multiple images being sent
  624.             }
  625.         } else {
  626.             // Handle other intents, such as being started from the home screen
  627.         }
  628.     }
  629.  
  630.     private void deleteCounter() {
  631.         //("delete counter");
  632.         File file=new File("/sdcard/Aitzik4/counter.txt");
  633.         file.delete();
  634.     }
  635.  
  636.     private void deleteStamp() {
  637.         lastStamp = null;
  638.         File file=new File("/sdcard/Aitzik4/stamp.txt");
  639.         file.delete();
  640.     }
  641.     private void deletePrintTime() {
  642.         printTime = null;
  643.         File file=new File("/sdcard/Aitzik4/printTime.txt");
  644.         file.delete();
  645.     }
  646.  
  647.     private void deleteTempStamp() {
  648.         lastStamp = null;
  649.         File file=new File("/sdcard/Aitzik4/tempStamp.txt");
  650.         file.delete();
  651.     }
  652.  
  653.     private boolean IsStamp() {
  654.         File dir = new File(stampPath+"/stamp.txt");
  655.         if(dir.exists()) {
  656.             return true;
  657.         }
  658.         return false;
  659.     }
  660.     private boolean IsPrintTime() {
  661.         File dir = new File(stampPath+"/printTime.txt");
  662.         if(dir.exists()) {
  663.             return true;
  664.         }
  665.         return false;
  666.     }
  667.  
  668.     void handleSendText(Intent intent) {
  669.         String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
  670.         if (sharedText != null) {
  671.             // Update UI to reflect text being shared
  672.         }
  673.     }
  674. /*
  675.     void handleSendImage(Intent intent) {
  676.         imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
  677.         if (imageUri != null) {
  678.             filePath = null;
  679.             if (imageUri != null && "content".equals(imageUri.getScheme())) {
  680.                 Cursor cursor = this.getContentResolver().query(imageUri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
  681.                 cursor.moveToFirst();
  682.                 filePath = cursor.getString(0);
  683.                 cursor.close();
  684.             } else {
  685.                 filePath = imageUri.getPath();
  686.             }
  687.             //buildPrintImage();
  688.             imageView = (ImageView) findViewById(R.id.imageView5);
  689.             imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
  690.  
  691.         }
  692.     }
  693. */
  694.     void handleSendImage(Intent intent) {
  695.         //printExtras(intent.getExtras());
  696.         //alert(getExtraArray(intent.getExtras()));
  697.  
  698.         if(getExtraArray(intent.getExtras()).toLowerCase().contains("[")){
  699.            /* Uri imageUriArray = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
  700.  
  701.                 alert(imageUriArray.getClass().getName());
  702.         */
  703.             //alert("array");
  704.  
  705.             String tempArrString = getExtraArray(intent.getExtras()).substring(1,getExtraArray(intent.getExtras()).length() - 1);
  706.             String[] pathArray = tempArrString.split(",");
  707.             for(int i=0; i<pathArray.length; i++){
  708.                 pathArray[i]=pathArray[i].trim();
  709.             }
  710.  
  711.             String msg = "";
  712.             filePathArray = new String [pathArray.length];
  713.             for(int i = 0; i<pathArray.length; i++){
  714.                 msg += pathArray[i]+"\n";
  715.                //imageUri = Uri.parse(Uri.decode(pathArray[i].substring(7)));
  716.  
  717.                 imageUri = Uri.parse(Uri.decode(pathArray[i]));
  718.                 Log.d("3",imageUri+"" +" to string");
  719.                 //alert(imageUri.getPath() +" get path");
  720.                // alert(imageUri.getClass().getName() +" get type");
  721.                 if (imageUri != null) {
  722.                     filePath = null;
  723.                     if (imageUri != null && "content".equals(imageUri.getScheme())) {
  724.                         Cursor cursor = this.getContentResolver().query(imageUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
  725.                         cursor.moveToFirst();
  726.                         filePath = cursor.getString(0);
  727.                         cursor.close();
  728.                     } else {
  729.                         filePath = imageUri.getPath();
  730.                     }
  731.                    // alert(imageUri.toString()+" ==>  "+filePath);
  732.                     //buildPrintImage();
  733.                     imageView = (ImageView) findViewById(R.id.imageView5);
  734.                     imageView.setImageURI(Uri.parse(filePath));
  735.  
  736.                 }
  737.                 filePathArray[i] = filePath;
  738.             }
  739.             filePath = null;
  740.             //alert(msg);
  741.             /*
  742.             ArrayList<Uri> imgList = new ArrayList<Uri>();
  743.             intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgList);
  744.             for (Uri object: imgList) {
  745.                String filePath = imgList.getEncodedPath();
  746.                alert(imgList.toString);
  747.  
  748.                 imageUri = object;
  749.                 if (imageUri != null) {
  750.                     filePath = null;
  751.                     if (imageUri != null && "content".equals(imageUri.getScheme())) {
  752.                         Cursor cursor = this.getContentResolver().query(imageUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
  753.                         cursor.moveToFirst();
  754.                         filePath = cursor.getString(0);
  755.                         cursor.close();
  756.                     } else {
  757.                         filePath = imageUri.getPath();
  758.                     }
  759.                     //buildPrintImage();
  760.                     imageView = (ImageView) findViewById(R.id.imageView5);
  761.                     imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
  762.  
  763.                 }
  764.  
  765.             }
  766.             */
  767.         }else{
  768.             //alert(Intent.EXTRA_STREAM);
  769.             imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
  770.            // alert(imageUri.getClass().getName());
  771.             if (imageUri != null) {
  772.                 filePath = null;
  773.                 if (imageUri != null && "content".equals(imageUri.getScheme())) {
  774.                     Cursor cursor = this.getContentResolver().query(imageUri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
  775.                     cursor.moveToFirst();
  776.                     filePath = cursor.getString(0);
  777.                     cursor.close();
  778.                 } else {
  779.                     filePath = imageUri.getPath();
  780.                 }
  781.                 //alert(imageUri.toString()+" ==>  "+filePath);
  782.                 //buildPrintImage();
  783.                 imageView = (ImageView) findViewById(R.id.imageView5);
  784.                 imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
  785.  
  786.             }
  787.         }
  788.  
  789.     }
  790.  
  791.     public void printExtras(Bundle extras) {
  792.         String msg="";
  793.         for(String extraKey : extras.keySet()) {
  794.             msg += "Extra: " + extraKey + ": " + extras.get(extraKey)+"\n";
  795.         }
  796.         alert(msg);
  797.     }
  798.  
  799.     public String getExtraArray(Bundle extras) {
  800.         String msg="";
  801.         int counter = 0;
  802.         for(String extraKey : extras.keySet()) {
  803.             if(counter==0)
  804.             msg = extras.get(extraKey)+"";
  805.             counter++;
  806.         }
  807.         return msg;
  808.     }
  809.  
  810.     public Bitmap overlay(Bitmap bmp1, Bitmap bmp2,Bitmap bm3) {// big picture frame
  811.         Bitmap bmOverlay=null;
  812.         //ssssssssssssss
  813.         try {
  814.             bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
  815.             Canvas canvas = new Canvas(bmOverlay);
  816.             canvas.drawBitmap(bmp1, new Matrix(), null);
  817.             canvas.drawBitmap(bmp2, 2, 21, null); // the image
  818.             canvas.drawBitmap(bm3, 0, 10, null); // the frame
  819.             //bmOverlay = rotateImage(bmOverlay,180);
  820.             return bmOverlay;
  821.         }catch (Exception e){
  822.             alert("לא בחרת מסגרת");
  823.         }
  824.         return bmOverlay;
  825.     }
  826.  
  827.     public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {// big picture frame
  828.         Bitmap bmOverlay=null;
  829.         try {
  830.             bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
  831.             Canvas canvas = new Canvas(bmOverlay);
  832.             canvas.drawBitmap(bmp1, new Matrix(), null);
  833.             canvas.drawBitmap(bmp2, 0, 0, null);
  834.             return bmOverlay;
  835.         }catch (Exception e){
  836.             reset();
  837.         }
  838.         return bmOverlay;
  839.     }
  840.  
  841.     public static String BitmapToString(Bitmap bitmap) {
  842.         try {
  843.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  844.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  845.             byte[] b = baos.toByteArray();
  846.             String temp = Base64.encodeToString(b, Base64.DEFAULT);
  847.             return temp;
  848.         } catch (NullPointerException e) {
  849.             return null;
  850.         } catch (OutOfMemoryError e) {
  851.             return null;
  852.         }
  853.     }
  854.  
  855.     public void alert(String message){
  856.         AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
  857.         alertDialog.setTitle("Alert");
  858.         alertDialog.setMessage(message+"");
  859.         alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  860.                 new DialogInterface.OnClickListener() {
  861.                     public void onClick(DialogInterface dialog, int which) {
  862.                         dialog.dismiss();
  863.                     }
  864.                 });
  865.         alertDialog.show();
  866.     }
  867.  
  868.     public Uri bitmapToUriConverter(Bitmap mBitmap) {
  869.         Uri uri = null;
  870.         try {
  871.             final BitmapFactory.Options options = new BitmapFactory.Options();
  872.             // Calculate inSampleSize
  873.             options.inSampleSize =1;
  874.  
  875.             // Decode bitmap with inSampleSize set
  876.             options.inJustDecodeBounds = false;
  877.             Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth(), mBitmap.getHeight(),
  878.                     true);
  879.             File file = new File(MainActivity.this
  880.                     .getFilesDir(), "Image"
  881.                     + new Random().nextInt() + ".jpeg");
  882.             FileOutputStream out = MainActivity.this
  883.                     .openFileOutput(file.getName(),
  884.                             Context.MODE_WORLD_READABLE);
  885.             newBitmap.compress(Bitmap.CompressFormat.JPEG, 91, out);
  886.             out.flush();
  887.             out.close();
  888.             //get absolute path
  889.             String realPath = file.getAbsolutePath();
  890.             File f = new File(realPath);
  891.             uri = Uri.fromFile(f);
  892.  
  893.         } catch (Exception e) {
  894.             alert("conversion fail");
  895.         }
  896.         return uri;
  897.     }
  898.  
  899.     public void postf(final String paravalue, final String paraname) {
  900.         AsyncTask.execute(new Runnable() {
  901.             @Override
  902.             public void run() {
  903.  
  904.                 // findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
  905.                 //   HideAllElements();
  906.  
  907.                 OkHttpClient client = new OkHttpClient();
  908.                 //String a=BitmapToString(merge);
  909.                 String url;
  910.                 if (paraname.equals("number")) {
  911.                     url = "https://cbl.co.il/nadinmedia/getdata.php?data=getwhatsapplink";
  912.                 } else {
  913.                     url = "https://cbl.co.il/nadinmedia/getdata.php?data=sendemail";
  914.                 }
  915.                 FormBody.Builder formBuilder = new FormBody.Builder()
  916.                         .add("image", BitmapToString(merge))
  917.                         .add(paraname, paravalue);
  918.                 RequestBody formBody = formBuilder.build();
  919.  
  920.                 Request request = new Request.Builder()
  921.                         .url(url)
  922.                         .post(formBody)
  923.                         .build();
  924.  
  925.                 client.newCall(request).enqueue(new Callback() {
  926.                     @Override
  927.                     public void onFailure(Call call, IOException e) {
  928.                         alert("לא נבחרה תמונה");
  929.                     }
  930.  
  931.                     @Override
  932.                     public void onResponse(Call call, Response response) throws IOException {
  933.                         if (response.isSuccessful()) {
  934.                             final String myResponse = response.body().string();
  935.                             MainActivity.this.runOnUiThread(new Runnable() {
  936.                                 @Override
  937.                                 public void run() {
  938.                                     if (paraname.equals("number")) {
  939.                                         Uri uri = Uri.parse(myResponse);
  940.                                         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  941.                                         startActivity(intent);
  942.                                     }
  943.                                     posting = false;
  944.                                     pb.setVisibility(View.INVISIBLE);
  945.  
  946.  
  947.                                 }
  948.                             });
  949.                         }
  950.                     }
  951.                 });
  952.  
  953.  
  954.             }
  955.         });
  956.     }
  957.     public void check_for_updates(){
  958.         //alert(System.currentTimeMillis()+"");
  959.         //1535759999 31/08/2018
  960.         if(System.currentTimeMillis()/1000 > 1541030399){
  961.             throw new RuntimeException("ERROR");
  962.         }
  963.     }
  964.  
  965.     public static Bitmap rotateImage(Bitmap src, float degree){
  966.         // create new matrix
  967.         Matrix matrix = new Matrix();
  968.         // setup rotation degree
  969.         matrix.postRotate(degree);
  970.         Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
  971.         return bmp;
  972.     }
  973.  
  974.     @Override
  975.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  976.         super.onActivityResult(requestCode, resultCode, data);
  977.  
  978.         if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
  979.             Uri selectedImage = data.getData();
  980.             String[] filePathColumn = { MediaStore.Images.Media.DATA };
  981.  
  982.             Cursor cursor = getContentResolver().query(selectedImage,
  983.                     filePathColumn, null, null, null);
  984.             cursor.moveToFirst();
  985.             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  986.             String picturePath = cursor.getString(columnIndex);
  987.             cursor.close();
  988.  
  989.             latestFrame=toRealPath(picturePath);
  990.             setNewSaveDefault(picturePath);
  991.             //buildPrintImage();
  992.             imageView = (ImageView) findViewById(R.id.imageView2);
  993.             imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  994.  
  995.  
  996.         }
  997.  
  998.  
  999.     }
  1000.  
  1001.     public void setNewSaveDefault( String joint){
  1002.         SharedPreferences sharedpreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
  1003.         SharedPreferences.Editor editor = sharedpreferences.edit();
  1004.         editor.putString("alldata",joint);
  1005.  
  1006.  
  1007.         //apply your changes
  1008.         editor.apply();
  1009.     }
  1010.  
  1011.     public String getNewSaveDefaultArray(){
  1012.         SharedPreferences sharedpreferences1 = getSharedPreferences("data", Context.MODE_PRIVATE);
  1013.  
  1014.         //get your string with default string in case referred key is not found
  1015.  
  1016.  
  1017.         return  sharedpreferences1.getString("alldata","/sdcard/Pictures/Frames/01.png");
  1018.     }
  1019.  
  1020.     public String toRealPath(String dumbpath){
  1021.         if(dumbpath.contains("emulated")) {
  1022.             dumbpath = dumbpath.substring(dumbpath.indexOf("0") + 1);
  1023.             dumbpath = "/sdcard" + dumbpath;
  1024.         }
  1025.  
  1026.         return dumbpath;
  1027.  
  1028.     }
  1029.  
  1030.     public void exit(){
  1031.         Intent intent = new Intent(Intent.ACTION_MAIN);
  1032.         intent.addCategory(Intent.CATEGORY_HOME);
  1033.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  1034.         startActivity(intent);
  1035.     }
  1036.  
  1037.     private Bitmap filepathtobitmap(String filePath){
  1038.  
  1039.         BitmapFactory.Options options = new BitmapFactory.Options();
  1040.         //options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  1041.         options.inJustDecodeBounds = false;
  1042.         options.inPreferredConfig = Bitmap.Config.RGB_565;
  1043.         options.inDither = true;
  1044.         options.inSampleSize = 2;
  1045.         Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
  1046.  
  1047.         return bitmap;
  1048.     }
  1049.  
  1050.     private void reset(){
  1051.         Intent i = getBaseContext().getPackageManager()
  1052.                 .getLaunchIntentForPackage( getBaseContext().getPackageName() );
  1053.         i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  1054.         startActivity(i);
  1055.     }
  1056.  
  1057.     public static int calculateInSampleSize(
  1058.             BitmapFactory.Options options, int reqWidth, int reqHeight) {
  1059.         // Raw height and width of image
  1060.         final int height = options.outHeight;
  1061.         final int width = options.outWidth;
  1062.         int inSampleSize = 1;
  1063.  
  1064.         if (height > reqHeight || width > reqWidth) {
  1065.  
  1066.             final int halfHeight = height / 2;
  1067.             final int halfWidth = width / 2;
  1068.  
  1069.             // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  1070.             // height and width larger than the requested height and width.
  1071.             while ((halfHeight / inSampleSize) >= reqHeight
  1072.                     && (halfWidth / inSampleSize) >= reqWidth) {
  1073.                 inSampleSize *= 2;
  1074.             }
  1075.         }
  1076.  
  1077.         return inSampleSize;
  1078.     }
  1079.  
  1080.     public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
  1081.                                                          int reqWidth, int reqHeight) {
  1082.  
  1083.         // First decode with inJustDecodeBounds=true to check dimensions
  1084.         final BitmapFactory.Options options = new BitmapFactory.Options();
  1085.         options.inJustDecodeBounds = true;
  1086.         BitmapFactory.decodeResource(res, resId, options);
  1087.  
  1088.         // Calculate inSampleSize
  1089.         options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  1090.  
  1091.         // Decode bitmap with inSampleSize set
  1092.         options.inJustDecodeBounds = false;
  1093.         return BitmapFactory.decodeResource(res, resId, options);
  1094.     }
  1095.  
  1096.     private void savePNG(Bitmap bmp, String path, String filename) {
  1097.         FileOutputStream out = null;
  1098.         try {
  1099.             String fullPathAndName = path+""+filename+".png";
  1100.             out = new FileOutputStream(fullPathAndName);
  1101.             bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
  1102.             // PNG is a lossless format, the compression factor (100) is ignored
  1103.         } catch (Exception e) {
  1104.             e.printStackTrace();
  1105.         } finally {
  1106.             try {
  1107.                 if (out != null) {
  1108.                     out.close();
  1109.                 }
  1110.             } catch (IOException e) {
  1111.                 e.printStackTrace();
  1112.             }
  1113.         }
  1114.     }
  1115.  
  1116.     private void saveStringIntoAfile(String path, String filename, String content) {
  1117.         FileOutputStream out = null;
  1118.         try {
  1119.             String fullPathAndName = path+""+filename+".html";
  1120.             out = new FileOutputStream(fullPathAndName);
  1121.             byte[] data = content.getBytes();
  1122.             out.write(data);
  1123.  
  1124.         } catch (Exception e) {
  1125.             e.printStackTrace();
  1126.         } finally {
  1127.             try {
  1128.                 if (out != null) {
  1129.                     out.flush();
  1130.                     out.close();
  1131.                 }
  1132.             } catch (IOException e) {
  1133.                 e.printStackTrace();
  1134.             }
  1135.         }
  1136.     }
  1137.  
  1138.     private void createHTML(String path, String filename) {
  1139.         FileOutputStream out = null;
  1140.         try {
  1141.             String fullPathAndName = path+""+filename+".html";
  1142.             out = new FileOutputStream(fullPathAndName);
  1143.             String html = "<!doctype html>\n" +
  1144.                     "<html>\n" +
  1145.                     "\t<head>\n" +
  1146.                     "\t\t<meta charset=\"UTF-8\" />\n" +
  1147.                     "\t\t<title></title>\n" +
  1148.                     "\t\t<style>\n" +
  1149.                     "\t\t\t*{\n" +
  1150.                     "\t\t\t\tmargin: 0;\n" +
  1151.                     "\t\t\t\tpadding: 0;\n" +
  1152.                     "\t\t\t}\n" +
  1153.                     "\t\t\t\n" +
  1154.                     "\t\t\tbody{\n" +
  1155.                     "\t\t\t\tdirection: RTL;\n" +
  1156.                     "\t\t\t\ttext-align: left;\n" +
  1157.                     "\t\t\t}\n" +
  1158.                     "\t\t\t\n" +
  1159.                     "\t\t\timg{\n" +
  1160.                     "\t\t\t\tmargin-top: 385px;\n" +
  1161.                     "\t\t\t}\n" +
  1162.                     "\t\t</style>\n" +
  1163.                     "\t</head>\n" +
  1164.                     "\t\n" +
  1165.                     "\t<body>\n" +
  1166.                     "\t\t<div class='page'>\n" +
  1167.                     "\t\t\t<img src='"+filename+".png' width=\"572PX\" />\n" +
  1168.                     "\t\t</div>\n" +
  1169.                     "\t\t<!-- PUTITHERE -->\n"+
  1170.                     "\t</body>\n" +
  1171.                     "</html>";
  1172.  
  1173.  
  1174.             byte[] data = html.getBytes();
  1175.             out.write(data);
  1176.  
  1177.         } catch (Exception e) {
  1178.             e.printStackTrace();
  1179.         } finally {
  1180.             try {
  1181.                 if (out != null) {
  1182.                     out.close();
  1183.                 }
  1184.             } catch (IOException e) {
  1185.                 e.printStackTrace();
  1186.             }
  1187.         }
  1188.     }
  1189.  
  1190.     public static String convertStreamToString(InputStream is) throws Exception {
  1191.         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  1192.         StringBuilder sb = new StringBuilder();
  1193.         String line = null;
  1194.         while ((line = reader.readLine()) != null) {
  1195.             sb.append(line).append("\n");
  1196.         }
  1197.         reader.close();
  1198.         return sb.toString();
  1199.     }
  1200.  
  1201.     public static String getStringFromFile (String filePath) throws Exception {
  1202.         File fl = new File(filePath);
  1203.         FileInputStream fin = new FileInputStream(fl);
  1204.         String ret = convertStreamToString(fin);
  1205.         //Make sure you close all streams.
  1206.         fin.close();
  1207.         return ret;
  1208.     }
  1209.  
  1210.     private long stampNowTimeDifference(){
  1211.         //return the amount of time difference between saved time stamp and now in miliseconds
  1212.         String stamptemp=null;
  1213.         try {
  1214.              stamptemp=getStringFromFile("/sdcard/Aitzik4/printTime.txt");
  1215.         } catch (Exception e) {
  1216.             e.printStackTrace();
  1217.         }
  1218.  
  1219.         long now= System.currentTimeMillis();
  1220.  
  1221.         //alert(now);
  1222.  
  1223.         long stamp=Long.valueOf(stamptemp.trim());
  1224.         //alert(stamp);
  1225.  
  1226.         long calculate = now-stamp;
  1227.         //alert(calculate);
  1228.  
  1229.         return calculate;
  1230.     }
  1231.  
  1232.     private int getAdditionalTime(){
  1233.         File dir = new File(filesInQueue+"/counter.txt");
  1234.         if(dir.exists()){
  1235.           try {
  1236.             return Integer.parseInt(getStringFromFile("/sdcard/Aitzik4/counter.txt").trim())*130*1000;
  1237.           }catch (Exception e) {
  1238.               alert("Error #1534033965");
  1239.               e.printStackTrace();
  1240.           }
  1241.  
  1242.         }
  1243.         return 0;
  1244.  
  1245.     }
  1246.  
  1247.     private void addOnetoCounter(){
  1248.         File dir = new File(filesInQueue+"/counter.txt");
  1249.         String tempcounter=null;
  1250.         if(dir.exists()){
  1251.             try {
  1252.                  tempcounter=getStringFromFile(filesInQueue+"/counter.txt");
  1253.             } catch (Exception e) {
  1254.                 alert("error #1534033906 :"+e.getMessage());
  1255.             }
  1256.             int tempcount=Integer.parseInt(tempcounter.trim());
  1257.             tempcount++;
  1258.             try {
  1259.                 FileWriter writer1 = new FileWriter(dir);
  1260.                 writer1.append(tempcount+"");
  1261.                 writer1.flush();
  1262.                 writer1.close();
  1263.             }catch (Exception e){
  1264.                 alert("error #1534033925  :"+e.getMessage());
  1265.             }
  1266.  
  1267.         }else{
  1268.             //create it and set it to 1
  1269.             File directory = new File(counterPath, "counter.txt");
  1270.             try {
  1271.                 FileWriter writer4 = new FileWriter(dir);
  1272.                 writer4.append("1");
  1273.                 writer4.flush();
  1274.                 writer4.close();
  1275.             }catch (Exception e){
  1276.                 alert("Error #1534033994 "+e.getMessage());
  1277.             }
  1278.  
  1279.         }
  1280.     }
  1281.  
  1282.     private boolean waitingForPrint(){
  1283.         File dir = new File(filesInQueue+"/counter.txt");
  1284.         if(dir.exists()) {
  1285.             return true;
  1286.         }
  1287.         return false;
  1288.     }
  1289.  
  1290.     private void saveToLastQueue(){
  1291.         File queues = new File(filesInQueue, "lastqueue.txt");
  1292.  
  1293.         try {
  1294.             FileWriter writer2 = new FileWriter(queues);
  1295.             writer2.append(getStringFromFile("/sdcard/Aitzik4/counter.txt"));
  1296.             writer2.flush();
  1297.             writer2.close();
  1298.         }catch (Exception e){
  1299.             alert(e.getMessage());
  1300.         }
  1301.  
  1302.     }
  1303.  
  1304.     private long calculateLastQueue(){
  1305.         //alert("claculate queue");
  1306.         long lastQueue= 0;
  1307.         try {
  1308.             lastQueue = Long.parseLong(getStringFromFile("/sdcard/Aitzik4/lastqueue.txt").trim());
  1309.         } catch (Exception e) {
  1310.             e.printStackTrace();
  1311.         }
  1312.         if(lastQueue<1){
  1313.             lastQueue=1;
  1314.         }
  1315.         return (lastQueue-1)*ADDITIONAL_PICTURE_TIME+FIRST_PICTURE_TIME;
  1316.  
  1317.     }
  1318.  
  1319.     public String updateStamp(){
  1320.         lastStamp = System.currentTimeMillis()+"".trim();
  1321.         return lastStamp;
  1322.     }
  1323.     public String updateTempStamp(){
  1324.         tempStamp = System.currentTimeMillis()+"".trim();
  1325.         return tempStamp;
  1326.     }
  1327.     public String updatePrintTime(){
  1328.         printTime = System.currentTimeMillis()+"".trim();
  1329.         return printTime;
  1330.     }
  1331.  
  1332.     public String readStampFileOrCreateNewIfNotExists(){
  1333.         String stampFilePath = "/sdcard/Aitzik4/stamp.txt";
  1334.         File stamp = new File(stampFilePath);
  1335.         if(!stamp.exists()) {
  1336.             lastStamp = updateStamp();
  1337.             try {
  1338.                 FileWriter writer1 = new FileWriter(stamp);
  1339.                 writer1.append(lastStamp);
  1340.                 writer1.flush();
  1341.                 writer1.close();
  1342.             }catch (Exception e){
  1343.                 alert("1534035627 exeption");
  1344.             }
  1345.  
  1346.         }else{
  1347.             try {
  1348.                 lastStamp =  getStringFromFile(stampFilePath).trim();
  1349.             } catch (Exception e) {
  1350.                 alert("Error while reading from stamp.txt file. error #1534035177");
  1351.                 e.printStackTrace();
  1352.             }
  1353.         }
  1354.  
  1355.         return lastStamp;
  1356.     }
  1357.     public String readPrintTimeFileOrCreateNewIfNotExists(){
  1358.         String stampFilePath = "/sdcard/Aitzik4/printTime.txt";
  1359.         File stamp = new File(stampFilePath);
  1360.         if(!stamp.exists()) {
  1361.             printTime = updateStamp();
  1362.             try {
  1363.                 FileWriter writer1 = new FileWriter(stamp);
  1364.                 writer1.append(printTime);
  1365.                 writer1.flush();
  1366.                 writer1.close();
  1367.             }catch (Exception e){
  1368.                 alert("1534035627 exeption");
  1369.             }
  1370.  
  1371.         }else{
  1372.             try {
  1373.                 printTime =  getStringFromFile(stampFilePath).trim();
  1374.             } catch (Exception e) {
  1375.                 alert("Error while reading from stamp.txt file. error #1534035177");
  1376.                 e.printStackTrace();
  1377.             }
  1378.         }
  1379.  
  1380.         return printTime;
  1381.     }
  1382.  
  1383.     public String readTempStampFileOrCreateNewIfNotExists(){
  1384.         String tempStampFilePath = "/sdcard/Aitzik4/tempStamp.txt";
  1385.         File stamp = new File(tempStampFilePath);
  1386.         if(!stamp.exists()) {
  1387.             tempStamp = updateTempStamp();
  1388.             try {
  1389.                 FileWriter writer1 = new FileWriter(stamp);
  1390.                 writer1.append(tempStamp);
  1391.                 writer1.flush();
  1392.                 writer1.close();
  1393.             }catch (Exception e){
  1394.                 alert("1534035627 exeption");
  1395.             }
  1396.  
  1397.         }else{
  1398.             try {
  1399.                 tempStamp =  getStringFromFile(tempStampFilePath).trim();
  1400.             } catch (Exception e) {
  1401.                 alert("Error while reading from stamp.txt file. error #1534035177");
  1402.                 e.printStackTrace();
  1403.             }
  1404.         }
  1405.  
  1406.         return tempStamp;
  1407.     }
  1408.  
  1409.     public void makeTempStampNotTemp(){
  1410.         String tempStampFilePath = "/sdcard/Aitzik4/tempStamp.txt";
  1411.         String stamp = null;
  1412.         try {
  1413.             stamp = getStringFromFile(tempStampFilePath).trim();
  1414.         } catch (Exception e) {
  1415.             alert("Error while reading from stamp.txt file. error #1534179579");
  1416.             e.printStackTrace();
  1417.         }
  1418.         deleteTempStamp();
  1419.         String stampFilePath = "/sdcard/Aitzik4/stamp.txt";
  1420.         File newStamp = new File(stampFilePath);
  1421.         try {
  1422.             FileWriter writer1 = new FileWriter(newStamp);
  1423.             writer1.append(stamp);
  1424.             writer1.flush();
  1425.             writer1.close();
  1426.         }catch (Exception e){
  1427.             alert("1534179446 exeption");
  1428.         }
  1429.     }
  1430.  
  1431.     public String saveDataToStampFile(String stamp){
  1432.         deleteStamp();
  1433.         String stampFilePath = "/sdcard/Aitzik4/stamp.txt";
  1434.         File stampFile = new File(stampFilePath);
  1435.         if(!stampFile.exists()) {
  1436.             try {
  1437.                 FileWriter writer1 = new FileWriter(stampFile);
  1438.                 writer1.append(stamp);
  1439.                 writer1.flush();
  1440.                 writer1.close();
  1441.             }catch (Exception e){
  1442.                 alert("15340351111627 exeption");
  1443.             }
  1444.  
  1445.         }
  1446.  
  1447.         return stamp;
  1448.     }
  1449.  
  1450.     public String saveDataToTempStampFile(String stamp){
  1451.         deleteStamp();
  1452.         String stampFilePath = "/sdcard/Aitzik4/tempStamp.txt";
  1453.         File stampFile = new File(stampFilePath);
  1454.         if(!stampFile.exists()) {
  1455.             try {
  1456.                 FileWriter writer1 = new FileWriter(stampFile);
  1457.                 writer1.append(stamp);
  1458.                 writer1.flush();
  1459.                 writer1.close();
  1460.             }catch (Exception e){
  1461.                 alert("15340351111627 exeption");
  1462.             }
  1463.  
  1464.         }
  1465.  
  1466.         return stamp;
  1467.     }
  1468.  
  1469.     private void addPictureToTheEndOfAnHtmlFile(String htmlFile, String imageName) {
  1470.         String htmlContent = "";
  1471.         htmlFile = htmlFile.trim();
  1472.         String htmlFileFullPath = "/sdcard/Aitzik4/" +htmlFile+".html";
  1473.         //alert("1534032278 htmlFileFullPath: "+htmlFileFullPath);
  1474.         try {
  1475.             htmlContent = getStringFromFile(htmlFileFullPath);
  1476.         } catch (Exception e) {
  1477.             e.printStackTrace();
  1478.         }
  1479.  
  1480.         //alert("1534032300 htmlContent:"+htmlContent);
  1481.         htmlContent = htmlContent.replace("\t</body>\n", "");
  1482.         htmlContent = htmlContent.replace("</html>", "");
  1483.         //int t=0;
  1484.         //t = htmlContent.indexOf("</body>") - 2;
  1485.         //htmlContent = htmlContent.substring(0, t);
  1486.         htmlContent +=  "\n\t\t<div class='page'>\n" +
  1487.                 "\t\t\t<img src='"+imageName+".png' width=\"572PX\" />\n" +
  1488.                 "\t\t</div>\n" +
  1489.                 "\t\t<!-- PUTITHERE -->\n"+
  1490.                 "\t</body>\n"+
  1491.                 "</html>";
  1492.         //alert("1534033763 htmlContent: "+htmlContent);
  1493.         saveStringIntoAfile("/sdcard/Aitzik4/", htmlFile, htmlContent);
  1494.     }
  1495.  
  1496.    /* public void onResume() {
  1497.         super.onResume();
  1498.         reset();
  1499.     }*/
  1500.    private void openWhatsApp(String smsNumber, String[]picture) {
  1501.        if(smsNumber.charAt(0) == '0'){
  1502.            smsNumber = "972"+smsNumber.substring(1);
  1503.        }else if(smsNumber.charAt(0) == '+'){
  1504.            smsNumber=smsNumber.substring(1);
  1505.        }
  1506.        boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
  1507.        if (isWhatsappInstalled) {
  1508.            /*Intent sendIntent = new Intent("android.intent.action.SEND");
  1509.            sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
  1510.            sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");//phone number without "+" prefix
  1511.            sendIntent.putExtra(Intent.EXTRA_TEXT, "banana");
  1512.            startActivity(sendIntent);*/
  1513.  
  1514.            Intent sendIntent = new Intent();
  1515.            sendIntent.setAction(Intent.ACTION_SEND);
  1516.            sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net");
  1517.            sendIntent.putExtra(Intent.EXTRA_TEXT, "ההודעה שלך");
  1518.            //Uri [] temp = new Uri [picture.length];
  1519.            ArrayList<Uri> imageUriArray = new ArrayList<Uri>();
  1520.            for(int i = 0; i<picture.length; i++) {
  1521.                imageUriArray.add(Uri.parse(Uri.decode(picture[i].trim())));
  1522.                //temp[i] = Uri.parse(Uri.decode(picture[i].trim()));
  1523.                //Log.d("DEBUG LMAO",temp[i]+"");
  1524.                Log.d("DEBUG LMAO",picture[i]+"");
  1525.            }
  1526.           // sendIntent.putExtra(Intent.EXTRA_STREAM, temp);
  1527.            sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUriArray);
  1528.            sendIntent.setType("text/plain");
  1529.            sendIntent.setType("image/*");
  1530.            sendIntent.setPackage("com.whatsapp");
  1531.            startActivity(sendIntent);
  1532.  
  1533.        } else {
  1534.            Uri uri = Uri.parse("market://details?id=com.whatsapp");
  1535.            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
  1536.            alert("וואטסאפ לא מותקן");
  1537.            // Toast.makeText(this, "WhatsApp not Installed",Toast.LENGTH_SHORT).show();
  1538.            startActivity(goToMarket);
  1539.        }
  1540.    }
  1541.  
  1542.     private boolean whatsappInstalledOrNot(String uri) {
  1543.         PackageManager pm = getPackageManager();
  1544.         boolean app_installed = false;
  1545.         try {
  1546.             pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
  1547.             app_installed = true;
  1548.         } catch (PackageManager.NameNotFoundException e) {
  1549.             app_installed = false;
  1550.         }
  1551.         return app_installed;
  1552.     }
  1553.     public static Uri convertFileToContentUri(Context context, File file) throws Exception {
  1554.  
  1555.         //Uri localImageUri = Uri.fromFile(localImageFile); // Not suitable as it's not a content Uri
  1556.  
  1557.         ContentResolver cr = context.getContentResolver();
  1558.         String imagePath = file.getAbsolutePath();
  1559.         String imageName = null;
  1560.         String imageDescription = null;
  1561.         String uriString = MediaStore.Images.Media.insertImage(cr, imagePath, imageName, imageDescription);
  1562.         return Uri.parse(uriString);
  1563.     }
  1564.  
  1565.  
  1566.  
  1567. }
Add Comment
Please, Sign In to add comment