From Capacious Cat, 4 Years ago, written in Java.
Embed
  1. package de.thedevstack.conversationsplus.ui.listeners;
  2.  
  3. import android.app.PendingIntent;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.net.Uri;
  7. import android.widget.Toast;
  8.  
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.net.URI;
  13.  
  14. import de.thedevstack.android.logcat.Logging;
  15. import de.thedevstack.conversationsplus.ConversationsPlusApplication;
  16. import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
  17. import de.thedevstack.conversationsplus.enums.UserDecision;
  18. import de.thedevstack.conversationsplus.exceptions.UiException;
  19. import de.thedevstack.conversationsplus.utils.FileUtils;
  20. import de.thedevstack.conversationsplus.utils.ImageUtil;
  21. import de.thedevstack.conversationsplus.utils.MessageUtil;
  22. import de.thedevstack.conversationsplus.R;
  23. import de.thedevstack.conversationsplus.entities.Conversation;
  24. import de.thedevstack.conversationsplus.entities.DownloadableFile;
  25. import de.thedevstack.conversationsplus.entities.Message;
  26. import de.thedevstack.conversationsplus.persistance.FileBackend;
  27. import de.thedevstack.conversationsplus.services.XmppConnectionService;
  28. import de.thedevstack.conversationsplus.ui.UiCallback;
  29. import de.thedevstack.conversationsplus.ui.XmppActivity;
  30. import de.thedevstack.conversationsplus.utils.StreamUtil;
  31.  
  32. /**
  33.  * Created by tzur on 31.10.2015.
  34.  */
  35. public class ResizePictureUserDecisionListener implements UserDecisionListener {
  36.     protected Uri uri;
  37.     protected final Conversation conversation;
  38.     protected final UiCallback<Message> callback;
  39.     protected final XmppConnectionService xmppConnectionService;
  40.     protected final Toast prepareFileToast;
  41.     protected final XmppActivity activity;
  42.  
  43.     public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, XmppConnectionService xmppConnectionService) {
  44.         this.xmppConnectionService = xmppConnectionService;
  45.         this.conversation = conversation;
  46.         this.activity = activity;
  47.         this.prepareFileToast = Toast.makeText(ConversationsPlusApplication.getAppContext(), ConversationsPlusApplication.getInstance().getText(R.string.preparing_image), Toast.LENGTH_LONG);
  48.         this.callback = new UiCallback<Message>() {
  49.  
  50.             @Override
  51.             public void userInputRequried(PendingIntent pi, Message object) {
  52.                 hidePrepareFileToast();
  53.             }
  54.  
  55.             @Override
  56.             public void success(Message message) {
  57.                 ResizePictureUserDecisionListener.this.xmppConnectionService.sendMessage(message);
  58.             }
  59.  
  60.             @Override
  61.             public void error(int error, Message message) {
  62.                 hidePrepareFileToast();
  63.                 //TODO Find another way to display an error dialog
  64.                 ResizePictureUserDecisionListener.this.activity.displayErrorDialog(error);
  65.             }
  66.  
  67.             protected void hidePrepareFileToast() {
  68.                 ResizePictureUserDecisionListener.this.activity.runOnUiThread(new Runnable() {
  69.                     @Override
  70.                     public void run() {
  71.                         ResizePictureUserDecisionListener.this.prepareFileToast.cancel();
  72.                     }
  73.                 });
  74.             }
  75.         };
  76.     }
  77.  
  78.     public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, Uri uri, XmppConnectionService xmppConnectionService) {
  79.         this(activity, conversation, xmppConnectionService);
  80.         this.uri = uri;
  81.     }
  82.  
  83.     public void setUri(Uri uri) {
  84.         this.uri = uri;
  85.     }
  86.  
  87.     protected void showPrepareFileToast() {
  88.         activity.runOnUiThread(new Runnable() {
  89.             @Override
  90.             public void run() {
  91.                 prepareFileToast.show();
  92.             }
  93.         });
  94.     }
  95.  
  96.     @Override
  97.     public void onYes() {
  98.         this.showPrepareFileToast();
  99.         final Message message;
  100.         if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
  101.             message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
  102.         } else {
  103.             message = new Message(conversation, "", conversation.getNextEncryption());
  104.         }
  105.         message.setCounterpart(conversation.getNextCounterpart());
  106.         message.setType(Message.TYPE_IMAGE);
  107.         ConversationsPlusApplication.executeFileAdding(new OnYesRunnable(message, uri));
  108.     }
  109.  
  110.     @Override
  111.     public void onNo() {
  112.         this.showPrepareFileToast();
  113.         final Message message;
  114.         if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
  115.             message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
  116.         } else {
  117.             message = new Message(conversation, "", conversation.getNextEncryption());
  118.         }
  119.         message.setCounterpart(conversation.getNextCounterpart());
  120.         message.setType(Message.TYPE_IMAGE);
  121.         ConversationsPlusApplication.executeFileAdding(new OnNoRunnable(message, uri));
  122.     }
  123.  
  124.     @Override
  125.     public void onRemember(UserDecision decision) {
  126.         ConversationsPlusPreferences.applyResizePicture(decision);
  127.     }
  128.  
  129.     private abstract class OnClickRunnable implements Runnable {
  130.  
  131.         protected final Message message;
  132.         protected final Uri uri;
  133.  
  134.         public OnClickRunnable(Message message, Uri uri) {
  135.             this.message = message;
  136.             this.uri = uri;
  137.         }
  138.     }
  139.  
  140.     private class OnNoRunnable extends OnClickRunnable {
  141.  
  142.         public OnNoRunnable(Message message, Uri uri) {
  143.             super(message, uri);
  144.         }
  145.  
  146.         @Override
  147.         public void run() {
  148.             InputStream is = null;
  149.             try {
  150.                 is = StreamUtil.openInputStreamFromContentResolver(uri);
  151.                 long imageSize = is.available();
  152.                 BitmapFactory.Options options = new BitmapFactory.Options();
  153.                 options.inJustDecodeBounds = true;
  154.                 BitmapFactory.decodeStream(is, null, options);
  155.                 int imageHeight = options.outHeight;
  156.                 int imageWidth = options.outWidth;
  157.                 String filePath = FileUtils.getPath(uri);
  158.                 MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight);
  159.                 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
  160.                     xmppConnectionService.getPgpEngine().encrypt(message, callback);
  161.                 } else {
  162.                     callback.success(message);
  163.                 }
  164.             } catch (FileNotFoundException e) {
  165.                 Logging.e("picturesending", "File not found to send not resized. " + e.getMessage());
  166.                 callback.error(R.string.error_file_not_found, message);
  167.             } catch (IOException e) {
  168.                 Logging.e("picturesending", "Error while sending not resized picture. " + e.getMessage());
  169.                 callback.error(R.string.error_io_exception, message);
  170.             } finally {
  171.                 if (null != is) {
  172.                     try {
  173.                         is.close();
  174.                     } catch (IOException e) {
  175.                         Logging.w("picturesending", "Error while closing stream for sending not resized picture. " + e.getMessage());
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.     }
  181.  
  182.     private class OnYesRunnable extends OnClickRunnable {
  183.  
  184.         public OnYesRunnable(Message message, Uri uri) {
  185.             super(message, uri);
  186.         }
  187.  
  188.         @Override
  189.         public void run() {
  190.             try {
  191.                 Bitmap resizedAndRotatedImage = ImageUtil.resizeAndRotateImage(uri);
  192.                 DownloadableFile file = FileBackend.compressImageAndCopyToPrivateStorage(message, resizedAndRotatedImage);
  193.                 String filePath = file.getAbsolutePath();
  194.                 long imageSize = file.getSize();
  195.                 int imageWidth = resizedAndRotatedImage.getWidth();
  196.                 int imageHeight = resizedAndRotatedImage.getHeight();
  197.                 MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight);
  198.                 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
  199.                     xmppConnectionService.getPgpEngine().encrypt(message, callback);
  200.                 } else {
  201.                     callback.success(message);
  202.                 }
  203.             } catch (final UiException e) {
  204.                 Logging.e("pictureresizesending", "Error while sending resized picture. " + e.getMessage());
  205.                 callback.error(e.getResId(), message);
  206.             }
  207.         }
  208.     }
  209. }
  210.