fix permission handling

This commit is contained in:
Daniel Gultsch 2018-02-25 09:46:44 +01:00
parent b5fa9b77c9
commit 743543ad90
2 changed files with 48 additions and 37 deletions

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -268,7 +269,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
return false; return false;
} }
} }
if (activity.hasStoragePermission(REQUEST_ADD_EDITOR_CONTENT)) { if (hasStoragePermission(REQUEST_ADD_EDITOR_CONTENT)) {
attachImageToConversation(inputContentInfo.getContentUri()); attachImageToConversation(inputContentInfo.getContentUri());
} else { } else {
mPendingEditorContent = inputContentInfo.getContentUri(); mPendingEditorContent = inputContentInfo.getContentUri();
@ -991,7 +992,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
copyUrl(selectedMessage); copyUrl(selectedMessage);
return true; return true;
case R.id.download_file: case R.id.download_file:
downloadFile(selectedMessage); startDownloadable(selectedMessage);
return true; return true;
case R.id.cancel_transmission: case R.id.cancel_transmission:
cancelTransmission(selectedMessage); cancelTransmission(selectedMessage);
@ -1129,7 +1130,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
public void attachFile(final int attachmentChoice) { public void attachFile(final int attachmentChoice) {
if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) { if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) {
if (!Config.ONLY_INTERNAL_STORAGE && !activity.hasStoragePermission(attachmentChoice)) { if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(attachmentChoice)) {
return; return;
} }
} }
@ -1213,7 +1214,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
} }
public void startDownloadable(Message message) { 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; this.mPendingDownloadableMessage = message;
return; return;
} }
@ -1268,6 +1269,19 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
builder.create().show(); 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;
}
}
public void unmuteConversation(final Conversation conversation) { public void unmuteConversation(final Conversation conversation) {
conversation.setMutedTill(0); conversation.setMutedTill(0);
this.activity.xmppConnectionService.updateConversation(conversation); this.activity.xmppConnectionService.updateConversation(conversation);
@ -1462,19 +1476,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) { public static void downloadFile(Activity activity, Message message) {
Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
if (fragment != null && fragment instanceof ConversationFragment) { if (fragment != null && fragment instanceof ConversationFragment) {
((ConversationFragment) fragment).downloadFile(message); ((ConversationFragment) fragment).startDownloadable(message);
return; return;
} }
fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment); fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment);
if (fragment != null && fragment instanceof ConversationFragment) { if (fragment != null && fragment instanceof ConversationFragment) {
((ConversationFragment) fragment).downloadFile(message); ((ConversationFragment) fragment).startDownloadable(message);
} }
} }
@ -1599,10 +1609,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) { public void reInit(Conversation conversation, Bundle extras) {
this.saveMessageDraftStopAudioPlayer();
this.reInit(conversation); this.reInit(conversation);
if (extras != null) { 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); processExtras(extras);
} else { } else {
pendingExtras.push(extras); pendingExtras.push(extras);
@ -1618,32 +1644,18 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
if (conversation == null) { if (conversation == null) {
return; 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; return;
} }
Log.d(Config.LOGTAG, "reInit(restore="+Boolean.toString(restore)+")"); Log.d(Config.LOGTAG, "reInit(restore="+Boolean.toString(restore)+")");
setupIme(); setupIme();
if (this.conversation != null) { if (!restore) {
final String msg = this.binding.textinput.getText().toString(); this.conversation.trim();
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 (activity != null) { this.binding.textSendButton.setContentDescription(activity.getString(R.string.send_message_to_x, conversation.getName()));
this.binding.textSendButton.setContentDescription(activity.getString(R.string.send_message_to_x, conversation.getName()));
}
this.conversation = conversation;
this.binding.textinput.setKeyboardListener(null); this.binding.textinput.setKeyboardListener(null);
this.binding.textinput.setText(""); this.binding.textinput.setText("");
this.binding.textinput.append(this.conversation.getNextMessage()); this.binding.textinput.append(this.conversation.getNextMessage());
@ -1666,11 +1678,10 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
this.binding.messagesView.setSelection(pos); this.binding.messagesView.setSelection(pos);
isAtBottom = pos == bottom; isAtBottom = pos == bottom;
} }
if (activity != null) {
activity.onConversationRead(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 //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.xmppConnectionService.getNotificationService().setOpenConversation(this.conversation);
}
} }
private void processExtras(Bundle extras) { private void processExtras(Bundle extras) {

View File

@ -768,7 +768,7 @@ public abstract class XmppActivity extends AppCompatActivity {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener); 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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode); requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);