commit b04d0eb17aa6b804c5f99b9fbca3ced63375580a Author: arrase arrase@gmail.com Date: Thu Nov 24 22:22:04 2016 +0100
actionbar permission request flow --- .../ui/hiddenservices/HiddenServicesActivity.java | 28 +++++++++++++++++++--- .../ui/hiddenservices/dialogs/HSActionsDialog.java | 9 ++++--- .../hiddenservices/dialogs/SelectBackupDialog.java | 2 +- .../hiddenservices/storage/PermissionManager.java | 8 +++---- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java index 119b13a..dc233bb 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java @@ -2,6 +2,7 @@ package org.torproject.android.ui.hiddenservices;
import android.content.ContentResolver; +import android.content.pm.PackageManager; import android.database.ContentObserver; import android.os.Bundle; import android.os.Handler; @@ -16,18 +17,18 @@ import android.widget.ListView; import android.widget.TextView;
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.dialogs.HSActionsDialog; import org.torproject.android.ui.hiddenservices.dialogs.HSDataDialog; import org.torproject.android.ui.hiddenservices.dialogs.SelectBackupDialog; import org.torproject.android.ui.hiddenservices.providers.HSContentProvider; +import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
public class HiddenServicesActivity extends AppCompatActivity { + public final int WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR = 1; private ContentResolver mCR; private OnionListAdapter mAdapter; private Toolbar toolbar; - private String[] mProjection = new String[]{ HSContentProvider.HiddenService._ID, HSContentProvider.HiddenService.NAME, @@ -102,7 +103,7 @@ public class HiddenServicesActivity extends AppCompatActivity { if (id == R.id.menu_restore_backup) { if (PermissionManager.usesRuntimePermissions() && !PermissionManager.hasExternalWritePermission(this)) { - PermissionManager.requestPermissions(this); + PermissionManager.requestPermissions(this, WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR); return true; }
@@ -114,6 +115,27 @@ public class HiddenServicesActivity extends AppCompatActivity { 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 { HSObserver(Handler handler) { super(handler); diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java index 103f87b..7d79fb2 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSActionsDialog.java @@ -17,11 +17,11 @@ import android.widget.Toast;
import org.torproject.android.R; 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.storage.PermissionManager;
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 @Override @@ -41,7 +41,10 @@ public class HSActionsDialog extends DialogFragment {
if (PermissionManager.usesRuntimePermissions() && !PermissionManager.hasExternalWritePermission(mContext)) { - PermissionManager.requestPermissions(getActivity()); + + PermissionManager.requestPermissions( + getActivity(), WRITE_EXTERNAL_STORAGE_FROM_ACTION_DIALOG); + return; }
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java index ce2c717..5ca4158 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java @@ -11,8 +11,8 @@ import android.widget.AdapterView; import android.widget.ListView;
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.storage.ExternalStorage;
import java.io.File; import java.io.FilenameFilter; diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/storage/PermissionManager.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/storage/PermissionManager.java index 80706c5..3a3cf09 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/storage/PermissionManager.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/storage/PermissionManager.java @@ -14,7 +14,6 @@ import android.view.View; import org.torproject.android.R;
public class PermissionManager { - private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
public static boolean usesRuntimePermissions() { 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); }
- public static void requestPermissions(FragmentActivity activity) { + public static void requestPermissions(FragmentActivity activity, int action) { + final int mAction = action; final FragmentActivity mActivity = activity;
if (ActivityCompat.shouldShowRequestPermissionRationale @@ -38,13 +38,13 @@ public class PermissionManager { public void onClick(View v) { ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); + mAction); } }).show(); } else { ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); + mAction); } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 97f2798..d2057ff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -349,5 +349,5 @@ <string name="permission_denied">Permission denied</string> <string name="restore_backup">Restore Backup</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>