Added permission request for shared memory into general settings
This commit is contained in:
parent
884caf3739
commit
4623eede17
2 changed files with 68 additions and 19 deletions
|
@ -6,6 +6,7 @@ import android.content.DialogInterface;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ActivityInfo;
|
import android.content.pm.ActivityInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
@ -19,6 +20,7 @@ import android.preference.Preference.OnPreferenceChangeListener;
|
||||||
import android.preference.Preference.OnPreferenceClickListener;
|
import android.preference.Preference.OnPreferenceClickListener;
|
||||||
import android.preference.PreferenceGroup;
|
import android.preference.PreferenceGroup;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
|
import android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
|
@ -50,15 +52,17 @@ 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 {
|
public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback {
|
||||||
|
|
||||||
public static final String MORE_VALUE = "MORE_VALUE";
|
public static final String MORE_VALUE = "MORE_VALUE";
|
||||||
private Preference applicationDir;
|
private Preference applicationDir;
|
||||||
private ListPreference applicationModePreference;
|
private ListPreference applicationModePreference;
|
||||||
private ListPreference drivingRegionPreference;
|
private ListPreference drivingRegionPreference;
|
||||||
|
private ChooseAppDirFragment chooseAppDirFragment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -335,15 +339,15 @@ public class SettingsGeneralActivity extends SettingsBaseActivity {
|
||||||
|
|
||||||
private void showAppDirDialogV19() {
|
private void showAppDirDialogV19() {
|
||||||
AlertDialog.Builder bld = new AlertDialog.Builder(this);
|
AlertDialog.Builder bld = new AlertDialog.Builder(this);
|
||||||
ChooseAppDirFragment frg = new DashChooseAppDirFragment.ChooseAppDirFragment(this, (Dialog) null) {
|
chooseAppDirFragment = new DashChooseAppDirFragment.ChooseAppDirFragment(this, (Dialog) null) {
|
||||||
@Override
|
@Override
|
||||||
protected void successCallback() {
|
protected void successCallback() {
|
||||||
updateApplicationDirTextAndSummary();
|
updateApplicationDirTextAndSummary();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
bld.setView(frg.initView(getLayoutInflater(), null, null));
|
bld.setView(chooseAppDirFragment.initView(getLayoutInflater(), null, null));
|
||||||
AlertDialog dlg = bld.show();
|
AlertDialog dlg = bld.show();
|
||||||
frg.setDialog(dlg);
|
chooseAppDirFragment.setDialog(dlg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -620,5 +624,24 @@ public class SettingsGeneralActivity extends SettingsBaseActivity {
|
||||||
return setFiles;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package net.osmand.plus.dashboard;
|
package net.osmand.plus.dashboard;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
|
@ -11,6 +12,7 @@ import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.StatFs;
|
import android.os.StatFs;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
|
@ -30,6 +32,7 @@ import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.OsmandSettings;
|
import net.osmand.plus.OsmandSettings;
|
||||||
import net.osmand.plus.ProgressImplementation;
|
import net.osmand.plus.ProgressImplementation;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
|
import net.osmand.plus.download.DownloadActivity;
|
||||||
import net.osmand.util.Algorithms;
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -66,6 +69,9 @@ public class DashChooseAppDirFragment {
|
||||||
private Fragment fragment;
|
private Fragment fragment;
|
||||||
private Dialog dlg;
|
private Dialog dlg;
|
||||||
|
|
||||||
|
private int typeTemp = -1;
|
||||||
|
private File selectedFileTemp;
|
||||||
|
|
||||||
public ChooseAppDirFragment(Activity activity, Fragment f) {
|
public ChooseAppDirFragment(Activity activity, Fragment f) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
this.fragment = f;
|
this.fragment = f;
|
||||||
|
@ -172,14 +178,12 @@ public class DashChooseAppDirFragment {
|
||||||
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED);
|
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED);
|
||||||
}
|
}
|
||||||
File df = settings.getDefaultInternalStorage();
|
File df = settings.getDefaultInternalStorage();
|
||||||
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT || OsmandSettings.isWritable(df)) {
|
|
||||||
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) {
|
if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) {
|
||||||
selected = items.size();
|
selected = items.size();
|
||||||
}
|
}
|
||||||
items.add(getString(R.string.storage_directory_shared));
|
items.add(getString(R.string.storage_directory_shared));
|
||||||
paths.add(df.getAbsolutePath());
|
paths.add(df.getAbsolutePath());
|
||||||
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT);
|
types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT);
|
||||||
}
|
|
||||||
|
|
||||||
File[] externals = getMyApplication().getExternalFilesDirs(null);
|
File[] externals = getMyApplication().getExternalFilesDirs(null);
|
||||||
if (externals != null) {
|
if (externals != null) {
|
||||||
|
@ -231,13 +235,26 @@ public class DashChooseAppDirFragment {
|
||||||
if (which == items.size() - 1) {
|
if (which == items.size() - 1) {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
showOtherDialog();
|
showOtherDialog();
|
||||||
|
} else {
|
||||||
|
|
||||||
|
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 {
|
} else {
|
||||||
mapsCopied = false;
|
mapsCopied = false;
|
||||||
type = types.get(which);
|
type = types.get(which);
|
||||||
selectedFile = new File(paths.get(which));
|
selectedFile = new File(paths.get(which));
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
updateView();
|
updateView();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -245,6 +262,15 @@ public class DashChooseAppDirFragment {
|
||||||
editalert.show();
|
editalert.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void processPermissionGranted() {
|
||||||
|
if (typeTemp != -1 && selectedFileTemp != null) {
|
||||||
|
mapsCopied = false;
|
||||||
|
type = typeTemp;
|
||||||
|
selectedFile = selectedFileTemp;
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void showOtherDialog() {
|
public void showOtherDialog() {
|
||||||
AlertDialog.Builder editalert = new AlertDialog.Builder(activity);
|
AlertDialog.Builder editalert = new AlertDialog.Builder(activity);
|
||||||
editalert.setTitle(R.string.application_dir);
|
editalert.setTitle(R.string.application_dir);
|
||||||
|
|
Loading…
Reference in a new issue