actionbar permission request flow

This commit is contained in:
arrase 2016-11-24 22:22:04 +01:00
parent 17efdcafa2
commit b04d0eb17a
5 changed files with 37 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package org.torproject.android.ui.hiddenservices;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -16,18 +17,18 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
import org.torproject.android.ui.hiddenservices.adapters.OnionListAdapter; import org.torproject.android.ui.hiddenservices.adapters.OnionListAdapter;
import org.torproject.android.ui.hiddenservices.dialogs.HSActionsDialog; import org.torproject.android.ui.hiddenservices.dialogs.HSActionsDialog;
import org.torproject.android.ui.hiddenservices.dialogs.HSDataDialog; import org.torproject.android.ui.hiddenservices.dialogs.HSDataDialog;
import org.torproject.android.ui.hiddenservices.dialogs.SelectBackupDialog; import org.torproject.android.ui.hiddenservices.dialogs.SelectBackupDialog;
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider; import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
public class HiddenServicesActivity extends AppCompatActivity { public class HiddenServicesActivity extends AppCompatActivity {
public final int WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR = 1;
private ContentResolver mCR; private ContentResolver mCR;
private OnionListAdapter mAdapter; private OnionListAdapter mAdapter;
private Toolbar toolbar; private Toolbar toolbar;
private String[] mProjection = new String[]{ private String[] mProjection = new String[]{
HSContentProvider.HiddenService._ID, HSContentProvider.HiddenService._ID,
HSContentProvider.HiddenService.NAME, HSContentProvider.HiddenService.NAME,
@ -102,7 +103,7 @@ public class HiddenServicesActivity extends AppCompatActivity {
if (id == R.id.menu_restore_backup) { if (id == R.id.menu_restore_backup) {
if (PermissionManager.usesRuntimePermissions() if (PermissionManager.usesRuntimePermissions()
&& !PermissionManager.hasExternalWritePermission(this)) { && !PermissionManager.hasExternalWritePermission(this)) {
PermissionManager.requestPermissions(this); PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR);
return true; return true;
} }
@ -114,6 +115,27 @@ public class HiddenServicesActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (grantResults.length < 1
|| grantResults[0] != PackageManager.PERMISSION_GRANTED) {
return;
}
switch (requestCode) {
case WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR: {
SelectBackupDialog dialog = new SelectBackupDialog();
dialog.show(getSupportFragmentManager(), "SelectBackupDialog");
break;
}
case HSActionsDialog.WRITE_EXTERNAL_STORAGE_FROM_ACTION_DIALOG: {
// TODO
break;
}
}
}
class HSObserver extends ContentObserver { class HSObserver extends ContentObserver {
HSObserver(Handler handler) { HSObserver(Handler handler) {
super(handler); super(handler);

View File

@ -17,11 +17,11 @@ import android.widget.Toast;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.ui.hiddenservices.backup.BackupUtils; import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider; import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
public class HSActionsDialog extends DialogFragment { public class HSActionsDialog extends DialogFragment {
public final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1; public static final int WRITE_EXTERNAL_STORAGE_FROM_ACTION_DIALOG = 2;
@NonNull @NonNull
@Override @Override
@ -41,7 +41,10 @@ public class HSActionsDialog extends DialogFragment {
if (PermissionManager.usesRuntimePermissions() if (PermissionManager.usesRuntimePermissions()
&& !PermissionManager.hasExternalWritePermission(mContext)) { && !PermissionManager.hasExternalWritePermission(mContext)) {
PermissionManager.requestPermissions(getActivity());
PermissionManager.requestPermissions(
getActivity(), WRITE_EXTERNAL_STORAGE_FROM_ACTION_DIALOG);
return; return;
} }

View File

@ -11,8 +11,8 @@ import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
import org.torproject.android.ui.hiddenservices.adapters.BackupAdapter; import org.torproject.android.ui.hiddenservices.adapters.BackupAdapter;
import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;

View File

@ -14,7 +14,6 @@ import android.view.View;
import org.torproject.android.R; import org.torproject.android.R;
public class PermissionManager { public class PermissionManager {
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
public static boolean usesRuntimePermissions() { public static boolean usesRuntimePermissions() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
@ -25,7 +24,8 @@ public class PermissionManager {
return (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED); return (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
} }
public static void requestPermissions(FragmentActivity activity) { public static void requestPermissions(FragmentActivity activity, int action) {
final int mAction = action;
final FragmentActivity mActivity = activity; final FragmentActivity mActivity = activity;
if (ActivityCompat.shouldShowRequestPermissionRationale if (ActivityCompat.shouldShowRequestPermissionRationale
@ -38,13 +38,13 @@ public class PermissionManager {
public void onClick(View v) { public void onClick(View v) {
ActivityCompat.requestPermissions(mActivity, ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); mAction);
} }
}).show(); }).show();
} else { } else {
ActivityCompat.requestPermissions(mActivity, ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); mAction);
} }
} }
} }

View File

@ -349,5 +349,5 @@
<string name="permission_denied">Permission denied</string> <string name="permission_denied">Permission denied</string>
<string name="restore_backup">Restore Backup</string> <string name="restore_backup">Restore Backup</string>
<string name="create_a_backup_first">Create a backup first</string> <string name="create_a_backup_first">Create a backup first</string>
<string name="name_can_t_be_empty">Name can't be empty</string> <string name="name_can_t_be_empty">Name can\'t be empty</string>
</resources> </resources>