[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [orbot/master] request permissions



commit d048cd6fb2292997cea2a974a2ae3923105bd371
Author: arrase <arrase@xxxxxxxxx>
Date:   Sun Nov 20 19:03:45 2016 +0100

    request permissions
---
 .../android/ui/hs/HiddenServicesActivity.java      | 65 ++++++++++++++++++++++
 app/src/main/res/values/strings.xml                |  3 +
 2 files changed, 68 insertions(+)

diff --git a/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java b/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java
index 26889d0..8a81a40 100644
--- a/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java
+++ b/app/src/main/java/org/torproject/android/ui/hs/HiddenServicesActivity.java
@@ -1,11 +1,17 @@
 package org.torproject.android.ui.hs;
 
 
+import android.Manifest;
+import android.annotation.SuppressLint;
 import android.content.ContentResolver;
+import android.content.pm.PackageManager;
 import android.database.ContentObserver;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.support.design.widget.FloatingActionButton;
+import android.support.design.widget.Snackbar;
+import android.support.v4.app.ActivityCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.AdapterView;
@@ -22,6 +28,8 @@ public class HiddenServicesActivity extends AppCompatActivity {
     private ContentResolver mCR;
     private OnionListAdapter mAdapter;
 
+    public final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
+
     private String[] mProjection = new String[]{
             HSContentProvider.HiddenService._ID,
             HSContentProvider.HiddenService.NAME,
@@ -46,6 +54,9 @@ public class HiddenServicesActivity extends AppCompatActivity {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.layout_hs_list_view);
 
+        if (usesRuntimePermissions())
+            checkPermissions();
+
         mCR = getContentResolver();
 
         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
@@ -89,4 +100,58 @@ public class HiddenServicesActivity extends AppCompatActivity {
             }
         });
     }
+
+    private boolean usesRuntimePermissions() {
+        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
+    }
+
+    @SuppressLint("NewApi")
+    private boolean hasPermission(String permission) {
+        return !usesRuntimePermissions() || (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
+    }
+
+    private void checkPermissions() {
+        if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+            if (ActivityCompat.shouldShowRequestPermissionRationale
+                    (HiddenServicesActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+                Snackbar.make(findViewById(android.R.id.content),
+                        R.string.please_grant_permissions_for_external_storage,
+                        Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
+                        new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                ActivityCompat.requestPermissions(HiddenServicesActivity.this,
+                                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+                                        PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
+                            }
+                        }).show();
+            } else {
+                ActivityCompat.requestPermissions(HiddenServicesActivity.this,
+                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+                        PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
+            }
+        }
+    }
+
+    @SuppressLint("NewApi")
+    @Override
+    public void onRequestPermissionsResult(int requestCode,
+                                           String permissions[], int[] grantResults) {
+        switch (requestCode) {
+            case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
+                // If request is cancelled, the result arrays are empty.
+                if (grantResults.length > 0
+                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+                    Snackbar.make(findViewById(android.R.id.content),
+                            R.string.permission_granted,
+                            Snackbar.LENGTH_LONG).show();
+                } else {
+                    Snackbar.make(findViewById(android.R.id.content),
+                            R.string.permission_denied,
+                            Snackbar.LENGTH_LONG).show();
+                }
+                break;
+            }
+        }
+    }
 }
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index cfa2393..05408dc 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -343,4 +343,7 @@
     <string name="delete_service">Delete Service</string>
     <string name="done">Done!</string>
     <string name="filemanager_not_available">Filemanager not available</string>
+    <string name="please_grant_permissions_for_external_storage">Please grant permissions for external storage</string>
+    <string name="permission_granted">Permission granted</string>
+    <string name="permission_denied">Permission denied</string>
 </resources>



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits