From: Reimar Döffinger Date: Wed, 18 Nov 2015 20:25:42 +0000 (+0100) Subject: Check that specified path is writeable. X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=53e8b04c0b122ced8b54fef0bd5499a654be1455 Check that specified path is writeable. Also show a list of alternatives. --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e5137e8..b92e9fb 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,7 +2,7 @@ = Build.VERSION_CODES.KITKAT) { getApplicationContext().getExternalFilesDirs(null); } - if (!dictDir.isDirectory() || !dictDir.canWrite()) { - String dirs = " " + Environment.getExternalStorageDirectory(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - File[] files = getApplicationContext().getExternalFilesDirs(null); - for (File f : files) { - dirs += " " + f.getAbsolutePath(); - } - } else { - dirs += " " + getApplicationContext().getExternalFilesDir(null).getAbsolutePath(); - } - Toast.makeText(getApplicationContext(), "Chosen directory not writeable, try one of" + dirs, Toast.LENGTH_LONG).show(); - } return dictDir; } diff --git a/src/com/hughes/android/dictionary/PreferenceActivity.java b/src/com/hughes/android/dictionary/PreferenceActivity.java index c7132e6..e141259 100644 --- a/src/com/hughes/android/dictionary/PreferenceActivity.java +++ b/src/com/hughes/android/dictionary/PreferenceActivity.java @@ -14,14 +14,19 @@ package com.hughes.android.dictionary; +import android.app.AlertDialog; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Build; +import android.os.Environment; import android.preference.ListPreference; import android.preference.PreferenceManager; +import java.io.File; import java.util.List; -public class PreferenceActivity extends android.preference.PreferenceActivity { +public class PreferenceActivity extends android.preference.PreferenceActivity + implements SharedPreferences.OnSharedPreferenceChangeListener { static boolean prefsMightHaveChanged = false; @@ -61,10 +66,38 @@ public class PreferenceActivity extends android.preference.PreferenceActivity { defaultDic.setEntries(entries); defaultDic.setEntryValues(entryvalues); + + prefs.registerOnSharedPreferenceChangeListener(this); } @Override public void onContentChanged() { super.onContentChanged(); } + + @Override + public void onSharedPreferenceChanged(SharedPreferences p, String v) { + final DictionaryApplication application = (DictionaryApplication)getApplication(); + File dictDir = application.getDictDir(); + if (!dictDir.isDirectory() || !dictDir.canWrite()) { + String dirs = ""; + String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath(); + if (new File(externalDir).canWrite()) + dirs += "\n" + externalDir + "/quickDic"; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + File[] files = getApplicationContext().getExternalFilesDirs(null); + for (File f : files) { + if (f.canWrite()) + dirs += "\n" + f.getAbsolutePath(); + } + } else { + String externalFilesDir = getApplicationContext().getExternalFilesDir(null).getAbsolutePath(); + if (new File(externalFilesDir).canWrite()) + dirs += "\n" + externalFilesDir; + } + new AlertDialog.Builder(this).setTitle(getString(R.string.error)) + .setMessage("Chosen directory not writeable, try one of" + dirs) + .setNeutralButton("Close", null).show(); + } + } }