From 362eddae27568674e78aa4968b1a4165cd67f517 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 31 Mar 2018 00:35:16 +0200 Subject: fixed take photo + do not reint twice --- .../pixart/messenger/ui/ConversationFragment.java | 89 ++++++++++++++++------ 1 file changed, 64 insertions(+), 25 deletions(-) (limited to 'src/main/java/de/pixart/messenger') diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 1f6540a79..e310ed089 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -139,6 +139,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action"; public static final String STATE_CONVERSATION_UUID = ConversationFragment.class.getName() + ".uuid"; public static final String STATE_SCROLL_POSITION = ConversationFragment.class.getName() + ".scroll_position"; + public static final String STATE_PHOTO_URI = ConversationFragment.class.getName() + ".take_photo_uri"; final protected List messageList = new ArrayList<>(); final private List mPendingImageUris = new ArrayList<>(); @@ -146,6 +147,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private final PendingItem postponedActivityResult = new PendingItem<>(); private final PendingItem pendingConversationsUuid = new PendingItem<>(); private final PendingItem pendingExtras = new PendingItem<>(); + private final PendingItem pendingTakePhotoUri = new PendingItem<>(); protected MessageAdapter messageListAdapter; private Conversation conversation; public FragmentConversationBinding binding; @@ -153,6 +155,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd. MMM yyyy", Locale.getDefault()); private Toast messageLoaderToast; private ConversationActivity activity; + + private boolean reInitRequiredOnStart = true; + protected OnClickListener clickToVerify = new OnClickListener() { @Override public void onClick(View v) { @@ -750,6 +755,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private void sendImage(Conversation conversation, Uri uri) { final Toast prepareFileToast = Toast.makeText(getActivity(), getText(R.string.preparing_image), Toast.LENGTH_LONG); prepareFileToast.show(); + activity.delegateUriPermissionsToService(uri); activity.xmppConnectionService.attachImageToConversation(conversation, uri, new UiCallback() { @@ -767,14 +773,14 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke @Override public void error(final int error, Message message) { hidePrepareFileToast(prepareFileToast); - getActivity().runOnUiThread(() -> activity.replaceToast(getString(error))); + activity.runOnUiThread(() -> activity.replaceToast(getString(error))); } }); } private void hidePrepareFileToast(final Toast prepareFileToast) { if (prepareFileToast != null) { - getActivity().runOnUiThread(prepareFileToast::cancel); + activity.runOnUiThread(prepareFileToast::cancel); } } @@ -907,18 +913,28 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke break; case ATTACHMENT_CHOICE_CHOOSE_IMAGE: List imageUris = AttachmentTool.extractUriFromIntent(data); - for (Iterator i = imageUris.iterator(); i.hasNext(); i.remove()) { - Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching image to conversations. CHOOSE_IMAGE"); - attachImageToConversation(conversation, i.next(), false); + int ImageUrisCount = imageUris.size(); + if (activity.xmppConnectionServiceBound) { + if (ImageUrisCount == 1) { + Uri uri = imageUris.get(0); + Log.d(Config.LOGTAG, "ConversationActivity.onActivityResult() - attaching image to conversations. CHOOSE_IMAGE"); + attachImageToConversation(conversation, uri, false); + } else { + for (Iterator i = imageUris.iterator(); i.hasNext(); i.remove()) { + Log.d(Config.LOGTAG, "ConversationActivity.onActivityResult() - attaching images to conversations. CHOOSE_IMAGES"); + attachImagesToConversation(conversation, i.next()); + } + } } break; case ATTACHMENT_CHOICE_TAKE_FROM_CAMERA: - //TODO save photo uri - final List PhotoUris = AttachmentTool.extractUriFromIntent(data); - for (Iterator i = PhotoUris.iterator(); i.hasNext(); i.remove()) { - Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching image to conversations. TAKE_FROM_CAMERA"); - attachPhotoToConversation(conversation, i.next()); + Uri takePhotoUri = pendingTakePhotoUri.pop(); + if (takePhotoUri != null) { + attachImageToConversation(conversation, takePhotoUri, false); + } else { + Log.d(Config.LOGTAG, "lost take photo uri. unable to to attach"); } + break; case ATTACHMENT_CHOICE_CHOOSE_FILE: case ATTACHMENT_CHOICE_RECORD_VOICE: final List fileUris = AttachmentTool.extractUriFromIntent(data); @@ -1525,6 +1541,14 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke boolean chooser = false; String fallbackPackageId = null; switch (attachmentChoice) { + case ATTACHMENT_CHOICE_CHOOSE_IMAGE: + intent.setAction(Intent.ACTION_GET_CONTENT); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + } + intent.setType("image/*"); + chooser = true; + break; case ATTACHMENT_CHOICE_CHOOSE_VIDEO: chooser = true; intent.setType("video/*"); @@ -1536,10 +1560,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke builder.setTitle(getString(R.string.attach_take_from_camera)); builder.setNegativeButton(getString(R.string.action_take_photo), (dialog, which) -> { - Uri uri = activity.xmppConnectionService.getFileBackend().getTakePhotoUri(); + final Uri uri = activity.xmppConnectionService.getFileBackend().getTakePhotoUri(); + pendingTakePhotoUri.push(uri); + intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); - intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, attachmentChoice); }); builder.setPositiveButton(getString(R.string.action_take_video), @@ -1794,6 +1820,10 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke super.onSaveInstanceState(outState); if (conversation != null) { outState.putString(STATE_CONVERSATION_UUID, conversation.getUuid()); + Uri uri = pendingTakePhotoUri.pop(); + if (uri != null) { + outState.putString(STATE_PHOTO_URI, uri.toString()); + } } } @@ -1806,16 +1836,24 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke String uuid = savedInstanceState.getString(STATE_CONVERSATION_UUID); if (uuid != null) { this.pendingConversationsUuid.push(uuid); + String takePhotoUri = savedInstanceState.getString(STATE_PHOTO_URI); + if (takePhotoUri != null) { + pendingTakePhotoUri.push(Uri.parse(takePhotoUri)); + } } } @Override public void onStart() { super.onStart(); - reInit(conversation); - final Bundle extras = pendingExtras.pop(); - if (extras != null) { - processExtras(extras); + if (this.reInitRequiredOnStart) { + reInit(conversation); + final Bundle extras = pendingExtras.pop(); + if (extras != null) { + processExtras(extras); + } + } else { + Log.d(Config.LOGTAG, "skipped reinit on start"); } } @@ -1860,29 +1898,29 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke public void reInit(Conversation conversation, Bundle extras) { this.saveMessageDraftStopAudioPlayer(); - this.reInit(conversation); - if (extras != null) { - //if binding or activity does not exist we will retry in onStart() - if (this.activity != null && this.binding != null) { + if (this.reInit(conversation)) { + if (extras != null) { processExtras(extras); + this.reInitRequiredOnStart = false; } else { + this.reInitRequiredOnStart = true; pendingExtras.push(extras); } } } - private void reInit(Conversation conversation) { - reInit(conversation, false); + private boolean reInit(Conversation conversation) { + return reInit(conversation, false); } - private void reInit(Conversation conversation, boolean restore) { + private boolean reInit(Conversation conversation, boolean restore) { if (conversation == null) { - return; + return false; } this.conversation = conversation; //once we set the conversation all is good and it will automatically do the right thing in onStart() if (this.activity == null || this.binding == null) { - return; + return false; } Log.d(Config.LOGTAG, "reInit(restore=" + Boolean.toString(restore) + ")"); setupIme(); @@ -1925,6 +1963,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke activity.onConversationRead(this.conversation); //TODO if we only do this when this fragment is running on main it won't *bing* in tablet layout which might be unnecessary since we can *see* it activity.xmppConnectionService.getNotificationService().setOpenConversation(this.conversation); + return true; } private void processExtras(Bundle extras) { -- cgit v1.2.3