package de.thedevstack.conversationsplus.ui.listeners; import android.app.PendingIntent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.widget.Toast; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; import de.thedevstack.conversationsplus.enums.UserDecision; import de.thedevstack.conversationsplus.exceptions.UiException; import de.thedevstack.conversationsplus.utils.FileUtils; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.DownloadableFile; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.persistance.FileBackend; import de.thedevstack.conversationsplus.services.XmppConnectionService; import de.thedevstack.conversationsplus.ui.UiCallback; import de.thedevstack.conversationsplus.ui.XmppActivity; import de.thedevstack.conversationsplus.utils.StreamUtil; /** * Created by tzur on 31.10.2015. */ public class ResizePictureUserDecisionListener implements UserDecisionListener { protected Uri uri; protected final Conversation conversation; protected final UiCallback callback; protected final XmppConnectionService xmppConnectionService; protected final Toast prepareFileToast; protected final XmppActivity activity; public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, XmppConnectionService xmppConnectionService) { this.xmppConnectionService = xmppConnectionService; this.conversation = conversation; this.activity = activity; this.prepareFileToast = Toast.makeText(ConversationsPlusApplication.getAppContext(), ConversationsPlusApplication.getInstance().getText(R.string.preparing_image), Toast.LENGTH_LONG); this.callback = new UiCallback() { @Override public void userInputRequried(PendingIntent pi, Message object) { hidePrepareFileToast(); } @Override public void success(Message message) { ResizePictureUserDecisionListener.this.xmppConnectionService.sendMessage(message); } @Override public void error(int error, Message message) { hidePrepareFileToast(); //TODO Find another way to display an error dialog ResizePictureUserDecisionListener.this.activity.displayErrorDialog(error); } protected void hidePrepareFileToast() { ResizePictureUserDecisionListener.this.activity.runOnUiThread(new Runnable() { @Override public void run() { ResizePictureUserDecisionListener.this.prepareFileToast.cancel(); } }); } }; } public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, Uri uri, XmppConnectionService xmppConnectionService) { this(activity, conversation, xmppConnectionService); this.uri = uri; } public void setUri(Uri uri) { this.uri = uri; } protected void showPrepareFileToast() { activity.runOnUiThread(new Runnable() { @Override public void run() { prepareFileToast.show(); } }); } @Override public void onYes() { this.showPrepareFileToast(); final Message message; if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED); } else { message = new Message(conversation, "", conversation.getNextEncryption()); } message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_IMAGE); ConversationsPlusApplication.executeFileAdding(new Runnable() { @Override public void run() { try { Bitmap resizedAndRotatedImage = ImageUtil.resizeAndRotateImage(uri); DownloadableFile file = FileBackend.compressImageAndCopyToPrivateStorage(message, resizedAndRotatedImage); String filePath = file.getAbsolutePath(); long imageSize = file.getSize(); int imageWidth = resizedAndRotatedImage.getWidth(); int imageHeight = resizedAndRotatedImage.getHeight(); MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight); if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { xmppConnectionService.getPgpEngine().encrypt(message, callback); } else { callback.success(message); } } catch (final UiException e) { Logging.e("pictureresizesending", "Error while sending resized picture. " + e.getMessage()); callback.error(e.getResId(), message); } } }); } @Override public void onNo() { this.showPrepareFileToast(); final Message message; if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED); } else { message = new Message(conversation, "", conversation.getNextEncryption()); } message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_IMAGE); ConversationsPlusApplication.executeFileAdding(new Runnable() { @Override public void run() { InputStream is = null; try { is = StreamUtil.openInputStreamFromContentResolver(uri); long imageSize = is.available(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String filePath = FileUtils.getPath(uri); MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight); if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { xmppConnectionService.getPgpEngine().encrypt(message, callback); } else { callback.success(message); } } catch (FileNotFoundException e) { Logging.e("picturesending", "File not found to send not resized. " + e.getMessage()); callback.error(R.string.error_file_not_found, message); } catch (IOException e) { Logging.e("picturesending", "Error while sending not resized picture. " + e.getMessage()); callback.error(R.string.error_io_exception, message); } finally { if (null != is) { try { is.close(); } catch (IOException e) { Logging.w("picturesending", "Error while closing stream for sending not resized picture. " + e.getMessage()); } } } } }); } @Override public void onRemember(UserDecision decision) { ConversationsPlusPreferences.applyResizePicture(decision); } }