request permissions

This commit is contained in:
arrase 2016-11-20 19:03:45 +01:00
parent 8e9bc2f840
commit d048cd6fb2
2 changed files with 68 additions and 0 deletions

View File

@ -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;
}
}
}
}

View File

@ -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>