Fix permission for shared memory

This commit is contained in:
Alexey Kulish 2016-04-26 10:47:14 +03:00
parent 76223ff8a7
commit 3849be68d9
2 changed files with 33 additions and 15 deletions

View file

@ -52,8 +52,6 @@ import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback { public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback {
@ -63,6 +61,8 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR
private ListPreference applicationModePreference; private ListPreference applicationModePreference;
private ListPreference drivingRegionPreference; private ListPreference drivingRegionPreference;
private ChooseAppDirFragment chooseAppDirFragment; private ChooseAppDirFragment chooseAppDirFragment;
private boolean permissionRequested;
private boolean permissionGranted;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -345,6 +345,9 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR
updateApplicationDirTextAndSummary(); updateApplicationDirTextAndSummary();
} }
}; };
if (permissionRequested && !permissionGranted) {
chooseAppDirFragment.setPermissionDenied();
}
bld.setView(chooseAppDirFragment.initView(getLayoutInflater(), null, null)); bld.setView(chooseAppDirFragment.initView(getLayoutInflater(), null, null));
AlertDialog dlg = bld.show(); AlertDialog dlg = bld.show();
chooseAppDirFragment.setDialog(dlg); chooseAppDirFragment.setDialog(dlg);
@ -624,21 +627,25 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR
return setFiles; return setFiles;
} }
@Override
protected void onResume() {
super.onResume();
if (permissionRequested) {
showAppDirDialogV19();
permissionRequested = false;
}
}
@Override @Override
public void onRequestPermissionsResult(int requestCode, public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) { 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.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Timer().schedule(new TimerTask() { permissionGranted = true;
@Override
public void run() {
if (chooseAppDirFragment != null) {
chooseAppDirFragment.processPermissionGranted();
}
}
}, 1);
} else { } else {
permissionGranted = false;
Toast.makeText(this, Toast.makeText(this,
R.string.missing_write_external_storage_permission, R.string.missing_write_external_storage_permission,
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();

View file

@ -69,8 +69,8 @@ public class DashChooseAppDirFragment {
private Fragment fragment; private Fragment fragment;
private Dialog dlg; private Dialog dlg;
private int typeTemp = -1; private static int typeTemp = -1;
private String selectePathTemp; private static String selectePathTemp;
public ChooseAppDirFragment(Activity activity, Fragment f) { public ChooseAppDirFragment(Activity activity, Fragment f) {
this.activity = activity; this.activity = activity;
@ -82,6 +82,11 @@ public class DashChooseAppDirFragment {
this.dlg = dlg; this.dlg = dlg;
} }
public void setPermissionDenied() {
typeTemp = -1;
selectePathTemp = null;
}
private String getFreeSpace(File dir) { private String getFreeSpace(File dir) {
if (dir.canRead()) { if (dir.canRead()) {
StatFs fs = new StatFs(dir.getAbsolutePath()); StatFs fs = new StatFs(dir.getAbsolutePath());
@ -156,6 +161,7 @@ public class DashChooseAppDirFragment {
copyMapsBtn = view.findViewById(R.id.copy_maps); copyMapsBtn = view.findViewById(R.id.copy_maps);
confirmBtn = view.findViewById(R.id.confirm); confirmBtn = view.findViewById(R.id.confirm);
addListeners(); addListeners();
processPermissionGranted();
updateView(); updateView();
return view; return view;
} }
@ -243,6 +249,9 @@ public class DashChooseAppDirFragment {
typeTemp = types.get(which); typeTemp = types.get(which);
selectePathTemp = paths.get(which); selectePathTemp = paths.get(which);
dialog.dismiss(); dialog.dismiss();
if (dlg != null) {
dlg.dismiss();
}
ActivityCompat.requestPermissions(activity, ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
@ -262,12 +271,14 @@ public class DashChooseAppDirFragment {
editalert.show(); editalert.show();
} }
public void processPermissionGranted() { private void processPermissionGranted() {
if (typeTemp != -1 && selectePathTemp != null) { if (typeTemp != -1 && selectePathTemp != null) {
mapsCopied = false; mapsCopied = false;
type = typeTemp; type = typeTemp;
selectedFile = new File(selectePathTemp); selectedFile = new File(selectePathTemp);
updateView();
typeTemp = -1;
selectePathTemp = null;
} }
} }