From 4ebea503479848c318f606e7a398b33f41ef17e8 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 30 Mar 2018 22:23:15 +0200 Subject: fix permission handling --- .../pixart/messenger/ui/ConversationFragment.java | 82 +++++++++++++--------- .../java/de/pixart/messenger/ui/XmppActivity.java | 2 +- 2 files changed, 48 insertions(+), 36 deletions(-) (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 93b8146dc..1f6540a79 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -1,5 +1,6 @@ package de.pixart.messenger.ui; +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; @@ -288,7 +289,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return false; } } - if (activity.hasStoragePermission(REQUEST_ADD_EDITOR_CONTENT)) { + if (hasStoragePermission(REQUEST_ADD_EDITOR_CONTENT)) { attachImageToConversation(inputContentInfo.getContentUri()); } else { mPendingEditorContent = inputContentInfo.getContentUri(); @@ -1226,7 +1227,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke copyUrl(selectedMessage); return true; case R.id.download_file: - downloadFile(selectedMessage); + startDownloadable(selectedMessage); return true; case R.id.cancel_transmission: cancelTransmission(selectedMessage); @@ -1367,7 +1368,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke public void attachFile(final int attachmentChoice) { if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) { - if (!Config.ONLY_INTERNAL_STORAGE && !activity.hasStoragePermission(attachmentChoice)) { + if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(attachmentChoice)) { return; } } @@ -1464,7 +1465,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } public void startDownloadable(Message message) { - if (!Config.ONLY_INTERNAL_STORAGE && !activity.hasStoragePermission(REQUEST_START_DOWNLOAD)) { + if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(REQUEST_START_DOWNLOAD)) { this.mPendingDownloadableMessage = message; return; } @@ -1503,6 +1504,19 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke builder.create().show(); } + private boolean hasStoragePermission(int requestCode) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode); + return false; + } else { + return true; + } + } else { + return true; + } + } + protected void selectPresenceToAttachFile(final int attachmentChoice) { final int encryption = conversation.getNextEncryption(); final Account account = conversation.getAccount(); @@ -1698,19 +1712,15 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - private void downloadFile(Message message) { - activity.xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true); - } - public static void downloadFile(Activity activity, Message message) { Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); if (fragment != null && fragment instanceof ConversationFragment) { - ((ConversationFragment) fragment).downloadFile(message); + ((ConversationFragment) fragment).startDownloadable(message); return; } fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment); if (fragment != null && fragment instanceof ConversationFragment) { - ((ConversationFragment) fragment).downloadFile(message); + ((ConversationFragment) fragment).startDownloadable(message); } } @@ -1834,10 +1844,26 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } + private void saveMessageDraftStopAudioPlayer() { + final Conversation previousConversation = this.conversation; + if (this.activity == null || this.binding == null || previousConversation == null) { + return; + } + Log.d(Config.LOGTAG, "ConversationFragment.saveMessageDraftStopAudioPlayer()"); + final String msg = this.binding.textinput.getText().toString(); + if (previousConversation.setNextMessage(msg)) { + activity.xmppConnectionService.updateConversation(previousConversation); + } + updateChatState(this.conversation, msg); + messageListAdapter.stopAudioPlayer(); + } + public void reInit(Conversation conversation, Bundle extras) { + this.saveMessageDraftStopAudioPlayer(); this.reInit(conversation); if (extras != null) { - if (activity != null) { + //if binding or activity does not exist we will retry in onStart() + if (this.activity != null && this.binding != null) { processExtras(extras); } else { pendingExtras.push(extras); @@ -1853,31 +1879,18 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke if (conversation == null) { return; } - if (this.activity == null) { - this.conversation = conversation; + 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; } Log.d(Config.LOGTAG, "reInit(restore=" + Boolean.toString(restore) + ")"); setupIme(); - if (this.conversation != null) { - final String msg = this.binding.textinput.getText().toString(); - if (this.conversation.setNextMessage(msg)) { - activity.xmppConnectionService.updateConversation(conversation); - } - if (this.conversation != conversation && !restore) { - updateChatState(this.conversation, msg); - messageListAdapter.stopAudioPlayer(); - } - if (!restore) { - this.conversation.trim(); - } + if (!restore) { + this.conversation.trim(); } - if (activity != null) { - this.binding.textSendButton.setContentDescription(activity.getString(R.string.send_message_to_x, conversation.getName())); - } - - this.conversation = conversation; + this.binding.textSendButton.setContentDescription(activity.getString(R.string.send_message_to_x, conversation.getName())); this.binding.textinput.setKeyboardListener(null); this.binding.textinput.setText(""); this.binding.textinput.append(this.conversation.getNextMessage()); @@ -1908,11 +1921,10 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke this.binding.messagesView.setSelection(pos); isAtBottom = pos == bottom; } - if (activity != null) { - 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); - } + + 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); } private void processExtras(Bundle extras) { diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index 5d4d1e49f..a2a568b9d 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -791,7 +791,7 @@ public abstract class XmppActivity extends AppCompatActivity { dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener); } - public boolean hasStoragePermission(int requestCode) { + protected boolean hasStoragePermission(int requestCode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode); -- cgit v1.2.3