request storage permission when opening message

This commit is contained in:
Daniel Gultsch 2018-03-05 17:05:45 +01:00
parent 6aee638432
commit b5e1bbbdc1
3 changed files with 51 additions and 4 deletions

View File

@ -38,6 +38,7 @@ import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Build;
@ -83,6 +84,8 @@ public class ConversationActivity extends XmppActivity implements OnConversation
public static final String EXTRA_NICK = "nick";
public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm";
public static final int REQUEST_OPEN_MESSAGE = 0x9876;
//secondary fragment (when holding the conversation, must be initialized before refreshing the overview fragment
private static final @IdRes
@ -281,6 +284,16 @@ public class ConversationActivity extends XmppActivity implements OnConversation
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String permissions[], @NonNull int[] grantResults) {
UriHandlerActivity.onRequestPermissionResult(this, requestCode, grantResults);
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch(requestCode) {
case REQUEST_OPEN_MESSAGE:
refreshUiReal();
ConversationFragment.openPendingMessage(this);
break;
}
}
}
}
@Override

View File

@ -409,16 +409,42 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
private int lastCompletionCursor;
private boolean firstWord = false;
private Message mPendingDownloadableMessage;
private final PendingItem<Message> pendingMessage = new PendingItem<>();
public static void downloadFile(Activity activity, Message message) {
private static ConversationFragment findConversationFragment(Activity activity) {
Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
if (fragment != null && fragment instanceof ConversationFragment) {
((ConversationFragment) fragment).startDownloadable(message);
return;
return (ConversationFragment) fragment;
}
fragment = activity.getFragmentManager().findFragmentById(R.id.secondary_fragment);
if (fragment != null && fragment instanceof ConversationFragment) {
((ConversationFragment) fragment).startDownloadable(message);
return (ConversationFragment) fragment;
}
return null;
}
public static void downloadFile(Activity activity, Message message) {
ConversationFragment fragment = findConversationFragment(activity);
if (fragment != null) {
fragment.startDownloadable(message);
}
}
public static void registerPendingMessage(Activity activity, Message message) {
ConversationFragment fragment = findConversationFragment(activity);
if (fragment != null) {
fragment.pendingMessage.push(message);
}
}
public static void openPendingMessage(Activity activity) {
ConversationFragment fragment = findConversationFragment(activity);
if (fragment != null) {
Message message = fragment.pendingMessage.pop();
if (message != null) {
fragment.messageListAdapter.openDownloadable(message);
}
}
}

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.ui.adapter;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
@ -15,6 +16,7 @@ import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.annotation.ColorInt;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
@ -63,6 +65,7 @@ import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.NotificationService;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.service.AudioPlayer;
@ -901,6 +904,11 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
public void openDownloadable(Message message) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ConversationFragment.registerPendingMessage(activity, message);
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationActivity.REQUEST_OPEN_MESSAGE);
return;
}
DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
if (!file.exists()) {
Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();