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.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();

View file

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