Added permission request for shared memory into general settings

This commit is contained in:
Alexey Kulish 2016-04-25 19:26:34 +03:00
parent ddecd6209b
commit 567f06c57e
2 changed files with 68 additions and 19 deletions

View file

@ -6,6 +6,7 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.AsyncTask;
@ -19,6 +20,7 @@ import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback;
import android.support.v7.app.AlertDialog;
import android.widget.EditText;
import android.widget.LinearLayout;
@ -50,15 +52,17 @@ 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 {
public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback {
public static final String MORE_VALUE = "MORE_VALUE";
private Preference applicationDir;
private ListPreference applicationModePreference;
private ListPreference drivingRegionPreference;
private ChooseAppDirFragment chooseAppDirFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -335,15 +339,15 @@ public class SettingsGeneralActivity extends SettingsBaseActivity {
private void showAppDirDialogV19() {
AlertDialog.Builder bld = new AlertDialog.Builder(this);
ChooseAppDirFragment frg = new DashChooseAppDirFragment.ChooseAppDirFragment(this, (Dialog) null) {
chooseAppDirFragment = new DashChooseAppDirFragment.ChooseAppDirFragment(this, (Dialog) null) {
@Override
protected void successCallback() {
updateApplicationDirTextAndSummary();
}
};
bld.setView(frg.initView(getLayoutInflater(), null, null));
bld.setView(chooseAppDirFragment.initView(getLayoutInflater(), null, null));
AlertDialog dlg = bld.show();
frg.setDialog(dlg);
chooseAppDirFragment.setDialog(dlg);
}
@ -620,5 +624,24 @@ public class SettingsGeneralActivity extends SettingsBaseActivity {
return setFiles;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (chooseAppDirFragment != null) {
chooseAppDirFragment.processPermissionGranted();
}
}
}, 1);
} else {
Toast.makeText(this,
R.string.missing_write_external_storage_permission,
Toast.LENGTH_LONG).show();
}
}
}

View file

@ -1,5 +1,6 @@
package net.osmand.plus.dashboard;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
@ -11,6 +12,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
@ -30,6 +32,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.ProgressImplementation;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.util.Algorithms;
import java.io.File;
@ -65,7 +68,10 @@ public class DashChooseAppDirFragment {
private Activity activity;
private Fragment fragment;
private Dialog dlg;
private int typeTemp = -1;
private File selectedFileTemp;
public ChooseAppDirFragment(Activity activity, Fragment f) {
this.activity = activity;
this.fragment = f;
@ -172,14 +178,12 @@ public class DashChooseAppDirFragment {
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED);
}
File df = settings.getDefaultInternalStorage();
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT || OsmandSettings.isWritable(df)) {
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) {
selected = items.size();
}
items.add(getString(R.string.storage_directory_shared));
paths.add(df.getAbsolutePath());
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT);
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) {
selected = items.size();
}
items.add(getString(R.string.storage_directory_shared));
paths.add(df.getAbsolutePath());
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT);
File[] externals = getMyApplication().getExternalFilesDirs(null);
if (externals != null) {
@ -232,12 +236,25 @@ public class DashChooseAppDirFragment {
dialog.dismiss();
showOtherDialog();
} else {
mapsCopied = false;
type = types.get(which);
selectedFile = new File(paths.get(which));
dialog.dismiss();
updateView();
if (types.get(which) == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT
&& !DownloadActivity.hasPermissionToWriteExternalStorage(activity)) {
typeTemp = types.get(which);
selectedFileTemp = new File(paths.get(which));
dialog.dismiss();
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
DownloadActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
mapsCopied = false;
type = types.get(which);
selectedFile = new File(paths.get(which));
dialog.dismiss();
updateView();
}
}
}
});
@ -245,6 +262,15 @@ public class DashChooseAppDirFragment {
editalert.show();
}
public void processPermissionGranted() {
if (typeTemp != -1 && selectedFileTemp != null) {
mapsCopied = false;
type = typeTemp;
selectedFile = selectedFileTemp;
updateView();
}
}
public void showOtherDialog() {
AlertDialog.Builder editalert = new AlertDialog.Builder(activity);
editalert.setTitle(R.string.application_dir);