Skip to content

Commit

Permalink
#169: fix dynamic ask for WRITE_EXTERNAL_STORAGE for ImageDetailActiv…
Browse files Browse the repository at this point in the history
…ityViewPager.java
  • Loading branch information
k3b committed Mar 23, 2020
1 parent 2dd9c90 commit e16f142
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,18 @@ public static void showActivity(String debugContext, Activity context, Uri image
}

@Override
protected void onCreateEx(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
this.mWaitingForMediaScannerResult = false;

// #17: let actionbar overlap image so there is no need to resize main view item
// http://stackoverflow.com/questions/6749261/custom-translucent-android-actionbar
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

super.onCreate(savedInstanceState);
}

@Override
protected void onCreateEx(Bundle savedInstanceState) {
SettingsActivity.prefs2Global(this);
Intent intent = getIntent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package de.k3b.android.widget;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
Expand All @@ -37,8 +39,10 @@
* * write to sdcard/usbstick,....
*/
public abstract class FilePermissionActivity extends ActivityWithAutoCloseDialogs {
private static final int REQUEST_ID_SD_ROOT_DIR = 2001;

private static final int REQUEST_ID_WRITE_EXTERNAL_STORAGE = 2000;
private static Uri fileRootUri = null;
private static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

@Override
Expand All @@ -49,10 +53,25 @@ protected void onCreate(Bundle savedInstanceState) {
!= PackageManager.PERMISSION_GRANTED) {
requestPermission(PERMISSION_WRITE_EXTERNAL_STORAGE, REQUEST_ID_WRITE_EXTERNAL_STORAGE);
} else {
onCreateEx(savedInstanceState);
askForDirectoryRoot();
}
}

protected void askForDirectoryRoot() {
// Not enough to access sd-card via file :-(
if (false && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& fileRootUri == null) {

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_ID_SD_ROOT_DIR);
} else {
onCreateEx(null);
}

}
protected abstract void onCreateEx(Bundle savedInstanceState);

/**
Expand All @@ -66,7 +85,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
&& (grantResults.length > 0)
&& (grantResults[0] == PackageManager.PERMISSION_GRANTED);
if (success) {
onCreateEx(null);
askForDirectoryRoot();
} else {
Log.i(Global.LOG_CONTEXT, this.getClass().getSimpleName()
+ ": " + getText(R.string.permission_error));
Expand All @@ -80,6 +99,15 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ID_SD_ROOT_DIR && resultCode == RESULT_OK) {
fileRootUri = data.getData();
onCreateEx(null);
}
}


protected void requestPermission(final String permission, final int requestCode) {
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
}
Expand Down

0 comments on commit e16f142

Please sign in to comment.