From 3849be68d90c42ef60fc2b0d0342205b98086914 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Tue, 26 Apr 2016 10:47:14 +0300 Subject: [PATCH] Fix permission for shared memory --- .../activities/SettingsGeneralActivity.java | 29 ++++++++++++------- .../dashboard/DashChooseAppDirFragment.java | 19 +++++++++--- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java index 704bbfefb3..4f196c1413 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java @@ -52,8 +52,6 @@ import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; -import java.util.Timer; -import java.util.TimerTask; public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback { @@ -63,6 +61,8 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR private ListPreference applicationModePreference; private ListPreference drivingRegionPreference; private ChooseAppDirFragment chooseAppDirFragment; + private boolean permissionRequested; + private boolean permissionGranted; @Override public void onCreate(Bundle savedInstanceState) { @@ -345,6 +345,9 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR updateApplicationDirTextAndSummary(); } }; + if (permissionRequested && !permissionGranted) { + chooseAppDirFragment.setPermissionDenied(); + } bld.setView(chooseAppDirFragment.initView(getLayoutInflater(), null, null)); AlertDialog dlg = bld.show(); chooseAppDirFragment.setDialog(dlg); @@ -624,21 +627,25 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR return setFiles; } + @Override + protected void onResume() { + super.onResume(); + if (permissionRequested) { + showAppDirDialogV19(); + permissionRequested = false; + } + } + @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { - if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE + permissionRequested = requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE; + if (permissionRequested && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - new Timer().schedule(new TimerTask() { - @Override - public void run() { - if (chooseAppDirFragment != null) { - chooseAppDirFragment.processPermissionGranted(); - } - } - }, 1); + permissionGranted = true; } else { + permissionGranted = false; Toast.makeText(this, R.string.missing_write_external_storage_permission, Toast.LENGTH_LONG).show(); diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java index f2e1dc7e96..e5bac5032f 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java @@ -69,8 +69,8 @@ public class DashChooseAppDirFragment { private Fragment fragment; private Dialog dlg; - private int typeTemp = -1; - private String selectePathTemp; + private static int typeTemp = -1; + private static String selectePathTemp; public ChooseAppDirFragment(Activity activity, Fragment f) { this.activity = activity; @@ -82,6 +82,11 @@ public class DashChooseAppDirFragment { this.dlg = dlg; } + public void setPermissionDenied() { + typeTemp = -1; + selectePathTemp = null; + } + private String getFreeSpace(File dir) { if (dir.canRead()) { StatFs fs = new StatFs(dir.getAbsolutePath()); @@ -156,6 +161,7 @@ public class DashChooseAppDirFragment { copyMapsBtn = view.findViewById(R.id.copy_maps); confirmBtn = view.findViewById(R.id.confirm); addListeners(); + processPermissionGranted(); updateView(); return view; } @@ -243,6 +249,9 @@ public class DashChooseAppDirFragment { typeTemp = types.get(which); selectePathTemp = paths.get(which); dialog.dismiss(); + if (dlg != null) { + dlg.dismiss(); + } ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, @@ -262,12 +271,14 @@ public class DashChooseAppDirFragment { editalert.show(); } - public void processPermissionGranted() { + private void processPermissionGranted() { if (typeTemp != -1 && selectePathTemp != null) { mapsCopied = false; type = typeTemp; selectedFile = new File(selectePathTemp); - updateView(); + + typeTemp = -1; + selectePathTemp = null; } }