From b04d0eb17aa6b804c5f99b9fbca3ced63375580a Mon Sep 17 00:00:00 2001 From: arrase Date: Thu, 24 Nov 2016 22:22:04 +0100 Subject: [PATCH] actionbar permission request flow --- .../HiddenServicesActivity.java | 28 +++++++++++++++++-- .../dialogs/HSActionsDialog.java | 9 ++++-- .../dialogs/SelectBackupDialog.java | 2 +- .../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 119b13ad..dc233bb3 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 103f87ba..7d79fb29 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 ce2c7172..5ca41580 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 80706c5d..3a3cf097 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 97f27983..d2057ff3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -349,5 +349,5 @@ Permission denied Restore Backup Create a backup first - Name can't be empty + Name can\'t be empty