]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/PreferenceActivity.java
Switch to app compat preferences.
[Dictionary.git] / src / com / hughes / android / dictionary / PreferenceActivity.java
index 966f21c7ad7ffe5548f50b343e470ff1b566059d..c4197e2734113cc3ad115085a733a47eb7fd37b0 100644 (file)
@@ -19,13 +19,14 @@ 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 android.support.v7.app.AppCompatActivity;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.ListPreference;
 
 import java.io.File;
 import java.util.List;
 
-public class PreferenceActivity extends android.preference.PreferenceActivity
+public class PreferenceActivity extends AppCompatActivity
     implements SharedPreferences.OnSharedPreferenceChangeListener {
 
     static boolean prefsMightHaveChanged = false;
@@ -45,27 +46,13 @@ public class PreferenceActivity extends android.preference.PreferenceActivity
             prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
         }
 
-        /**
-         * @author Dominik Köppl Preference: select default dictionary As this
+        /*
+          @author Dominik Köppl Preference: select default dictionary As this
          *         list is dynamically generated, we have to do it in this
          *         fashion
          */
         super.onCreate(savedInstanceState);
-        addPreferencesFromResource(R.xml.preferences);
-        ListPreference defaultDic = (ListPreference) findPreference(getResources().getString(
-                                        R.string.defaultDicKey));
-        List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
-
-        final CharSequence[] entries = new CharSequence[dicts.size()];
-        final CharSequence[] entryvalues = new CharSequence[dicts.size()];
-
-        for (int i = 0; i < entries.length; ++i) {
-            entries[i] = dicts.get(i).dictInfo;
-            entryvalues[i] = dicts.get(i).uncompressedFilename;
-        }
-
-        defaultDic.setEntries(entries);
-        defaultDic.setEntryValues(entryvalues);
+        setContentView(R.layout.preference_activity);
     }
 
     @Override
@@ -82,9 +69,33 @@ public class PreferenceActivity extends android.preference.PreferenceActivity
         prefs.registerOnSharedPreferenceChangeListener(this);
     }
 
-    @Override
-    public void onContentChanged() {
-        super.onContentChanged();
+    private String suggestedPaths(String suffix) {
+        String dirs = "";
+        String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
+        if (new File(externalDir).canWrite())
+            dirs += "\n" + externalDir + "/quickDic" + suffix;
+        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() + suffix;
+            }
+        } else {
+            File efd = null;
+            try {
+                efd = getApplicationContext().getExternalFilesDir(null);
+            } catch (Exception ignored) {
+            }
+            if (efd != null) {
+                String externalFilesDir = efd.getAbsolutePath();
+                if (new File(externalFilesDir).canWrite())
+                    dirs += "\n" + externalFilesDir + suffix;
+            }
+        }
+        File fd = getApplicationContext().getFilesDir();
+        if (fd.canWrite())
+            dirs += "\n" + fd.getAbsolutePath() + suffix;
+        return dirs;
     }
 
     @Override
@@ -93,32 +104,20 @@ public class PreferenceActivity extends android.preference.PreferenceActivity
         final DictionaryApplication application = DictionaryApplication.INSTANCE;
         File dictDir = application.getDictDir();
         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
-                !application.checkFileCreate(dictDir)) {
-            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 {
-                File efd = null;
-                try {
-                    efd = getApplicationContext().getExternalFilesDir(null);
-                } catch (Exception e) {
-                }
-                if (efd != null) {
-                    String externalFilesDir = efd.getAbsolutePath();
-                    if (new File(externalFilesDir).canWrite())
-                        dirs += "\n" + externalFilesDir;
-                }
-            }
-            File fd = getApplicationContext().getFilesDir();
-            if (fd.canWrite())
-                dirs += "\n" + fd.getAbsolutePath();
+                !DictionaryApplication.checkFileCreate(dictDir)) {
+            String dirs = suggestedPaths("");
+            new AlertDialog.Builder(this).setTitle(getString(R.string.error))
+            .setMessage(getString(R.string.chosenNotWritable) + dirs)
+            .setNeutralButton("Close", null).show();
+        }
+        File wordlist = application.getWordListFile();
+        boolean ok = false;
+        try {
+            ok = wordlist.canWrite() || wordlist.createNewFile();
+        } catch (Exception ignored) {
+        }
+        if (!ok) {
+            String dirs = suggestedPaths("/wordList.txt");
             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
             .setMessage(getString(R.string.chosenNotWritable) + dirs)
             .setNeutralButton("Close", null).show();