]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/PreferenceActivity.java
Detect and warn about non-writeable wordlist file.
[Dictionary.git] / src / com / hughes / android / dictionary / PreferenceActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary;
16
17 import android.app.AlertDialog;
18 import android.content.SharedPreferences;
19 import android.os.Bundle;
20 import android.os.Build;
21 import android.os.Environment;
22 import android.preference.ListPreference;
23 import android.preference.PreferenceManager;
24
25 import java.io.File;
26 import java.util.List;
27
28 public class PreferenceActivity extends android.preference.PreferenceActivity
29     implements SharedPreferences.OnSharedPreferenceChangeListener {
30
31     static boolean prefsMightHaveChanged = false;
32
33     @SuppressWarnings("deprecation")
34     @Override
35     public void onCreate(Bundle savedInstanceState) {
36         DictionaryApplication.INSTANCE.init(getApplicationContext());
37         final DictionaryApplication application = DictionaryApplication.INSTANCE;
38         setTheme(application.getSelectedTheme().themeId);
39
40         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
41         if (prefs.getString(getString(R.string.quickdicDirectoryKey), "").equals("")) {
42             prefs.edit().putString(getString(R.string.quickdicDirectoryKey), application.getDictDir().getAbsolutePath()).commit();
43         }
44         if (prefs.getString(getString(R.string.wordListFileKey), "").equals("")) {
45             prefs.edit().putString(getString(R.string.wordListFileKey), application.getWordListFile().getAbsolutePath()).commit();
46         }
47
48         /*
49           @author Dominik Köppl Preference: select default dictionary As this
50          *         list is dynamically generated, we have to do it in this
51          *         fashion
52          */
53         super.onCreate(savedInstanceState);
54         addPreferencesFromResource(R.xml.preferences);
55         ListPreference defaultDic = (ListPreference) findPreference(getResources().getString(
56                                         R.string.defaultDicKey));
57         List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
58
59         final CharSequence[] entries = new CharSequence[dicts.size()];
60         final CharSequence[] entryvalues = new CharSequence[dicts.size()];
61
62         for (int i = 0; i < entries.length; ++i) {
63             entries[i] = dicts.get(i).dictInfo;
64             entryvalues[i] = dicts.get(i).uncompressedFilename;
65         }
66
67         defaultDic.setEntries(entries);
68         defaultDic.setEntryValues(entryvalues);
69     }
70
71     @Override
72     protected void onPause() {
73         super.onPause();
74         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
75         prefs.unregisterOnSharedPreferenceChangeListener(this);
76     }
77
78     @Override
79     protected void onResume() {
80         super.onResume();
81         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
82         prefs.registerOnSharedPreferenceChangeListener(this);
83     }
84
85     private String suggestedPaths(String suffix) {
86         String dirs = "";
87         String externalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
88         if (new File(externalDir).canWrite())
89             dirs += "\n" + externalDir + "/quickDic" + suffix;
90         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
91             File[] files = getApplicationContext().getExternalFilesDirs(null);
92             for (File f : files) {
93                 if (f.canWrite())
94                     dirs += "\n" + f.getAbsolutePath() + suffix;
95             }
96         } else {
97             File efd = null;
98             try {
99                 efd = getApplicationContext().getExternalFilesDir(null);
100             } catch (Exception ignored) {
101             }
102             if (efd != null) {
103                 String externalFilesDir = efd.getAbsolutePath();
104                 if (new File(externalFilesDir).canWrite())
105                     dirs += "\n" + externalFilesDir + suffix;
106             }
107         }
108         File fd = getApplicationContext().getFilesDir();
109         if (fd.canWrite())
110             dirs += "\n" + fd.getAbsolutePath() + suffix;
111         return dirs;
112     }
113
114     @Override
115     public void onSharedPreferenceChanged(SharedPreferences p, String v) {
116         DictionaryApplication.INSTANCE.init(getApplicationContext());
117         final DictionaryApplication application = DictionaryApplication.INSTANCE;
118         File dictDir = application.getDictDir();
119         if (!dictDir.isDirectory() || !dictDir.canWrite() ||
120                 !DictionaryApplication.checkFileCreate(dictDir)) {
121             String dirs = suggestedPaths("");
122             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
123             .setMessage(getString(R.string.chosenNotWritable) + dirs)
124             .setNeutralButton("Close", null).show();
125         }
126         File wordlist = application.getWordListFile();
127         boolean ok = false;
128         try {
129             ok = wordlist.canWrite() || wordlist.createNewFile();
130         } catch (Exception ignored) {
131         }
132         if (!ok) {
133             String dirs = suggestedPaths("/wordList.txt");
134             new AlertDialog.Builder(this).setTitle(getString(R.string.error))
135             .setMessage(getString(R.string.chosenNotWritable) + dirs)
136             .setNeutralButton("Close", null).show();
137         }
138     }
139 }