added permission check for runtime permission required for storage

This commit is contained in:
Hans Cappelle
2017-10-09 18:01:39 +02:00
parent 886441d1a9
commit ff711182d7
2 changed files with 71 additions and 3 deletions

View File

@@ -9,8 +9,8 @@
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:label="@string/app_name"
android:icon="@drawable/launcher"

View File

@@ -19,16 +19,21 @@
*/
package com.keepassdroid.fileselect;
import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
@@ -74,6 +79,7 @@ import java.net.URLDecoder;
public class FileSelectActivity extends Activity {
private static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 111;
private ListView mList;
private ListAdapter mAdapter;
@@ -218,7 +224,12 @@ public class FileSelectActivity extends Activity {
startActivityForResult(i, OPEN_DOC);
}
else {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
Intent i;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
} else {
i = new Intent(Intent.ACTION_GET_CONTENT);
}
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
@@ -408,6 +419,9 @@ public class FileSelectActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
// check for storage permission
checkStoragePermission();
// Check to see if we need to change modes
if ( fileHistory.hasRecentFiles() != recentMode ) {
@@ -421,6 +435,60 @@ public class FileSelectActivity extends Activity {
fnv.updateExternalStorageWarning();
}
private void checkStoragePermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(FileSelectActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
//if (ActivityCompat.shouldShowRequestPermissionRationale(FileSelectActivity.this,
// Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(FileSelectActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
//}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);