]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryApplication.java
Fixed crash after update.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryApplication.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.Application;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.SharedPreferences;
21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22 import android.net.Uri;
23 import android.os.Environment;
24 import android.preference.PreferenceManager;
25 import android.util.Log;
26 import android.util.TypedValue;
27 import android.view.View;
28 import android.widget.Button;
29 import android.widget.ImageButton;
30 import android.widget.ImageView.ScaleType;
31
32 import com.actionbarsherlock.view.Menu;
33 import com.actionbarsherlock.view.MenuItem;
34 import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
35 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
36 import com.hughes.android.dictionary.engine.Dictionary;
37 import com.hughes.android.dictionary.engine.Language;
38 import com.hughes.android.dictionary.engine.Language.LanguageResources;
39 import com.hughes.android.dictionary.engine.TransliteratorManager;
40 import com.hughes.android.util.PersistentObjectCache;
41 import com.hughes.util.ListUtil;
42 import com.ibm.icu.text.Collator;
43
44 import java.io.BufferedReader;
45 import java.io.File;
46 import java.io.IOException;
47 import java.io.InputStreamReader;
48 import java.io.Serializable;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Comparator;
52 import java.util.LinkedHashMap;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Map;
56
57 public class DictionaryApplication extends Application {
58
59     static final String LOG = "QuickDicApp";
60
61     // Static, determined by resources (and locale).
62     // Unordered.
63     static Map<String, DictionaryInfo> DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = null;
64
65     static final class DictionaryConfig implements Serializable {
66         private static final long serialVersionUID = -1444177164708201263L;
67         // User-ordered list, persisted, just the ones that are/have been
68         // present.
69         final List<String> dictionaryFilesOrdered = new ArrayList<String>();
70
71         final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
72         
73         /**
74          * Sometimes a deserialized version of this datastructure isn't valid.
75          * @return
76          */
77         boolean isValid() {
78             return uncompressedFilenameToDictionaryInfo != null && dictionaryFilesOrdered != null;
79         }
80     }
81
82     DictionaryConfig dictionaryConfig = null;
83
84     int languageButtonPixels = -1;
85
86     static synchronized void staticInit(final Context context) {
87         if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
88             return;
89         }
90         DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new LinkedHashMap<String, DictionaryInfo>();
91         final BufferedReader reader = new BufferedReader(
92                 new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
93         try {
94             String line;
95             while ((line = reader.readLine()) != null) {
96                 if (line.startsWith("#") || line.length() == 0) {
97                     continue;
98                 }
99                 final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
100                 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
101                         dictionaryInfo.uncompressedFilename, dictionaryInfo);
102             }
103             reader.close();
104         } catch (IOException e) {
105             Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
106         }
107     }
108
109     private File dictDir;
110
111     @Override
112     public void onCreate() {
113         super.onCreate();
114         Log.d("QuickDic", "Application: onCreate");
115         TransliteratorManager.init(null);
116         staticInit(getApplicationContext());
117
118         languageButtonPixels = (int) TypedValue.applyDimension(
119                 TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
120
121         // Load the dictionaries we know about.
122         dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
123                 C.DICTIONARY_CONFIGS, DictionaryConfig.class);
124         if (dictionaryConfig == null) {
125             dictionaryConfig = new DictionaryConfig();
126         }
127         if (!dictionaryConfig.isValid()) {
128             dictionaryConfig = new DictionaryConfig();
129         }
130
131         // Theme stuff.
132         setTheme(getSelectedTheme().themeId);
133         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
134         prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
135             @Override
136             public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
137                     String key) {
138                 Log.d("QuickDic", "prefs changed: " + key);
139                 if (key.equals(getString(R.string.themeKey))) {
140                     setTheme(getSelectedTheme().themeId);
141                 }
142             }
143         });
144     }
145
146     public void onCreateGlobalOptionsMenu(
147             final Context context, final Menu menu) {
148         final MenuItem about = menu.add(getString(R.string.about));
149         about.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
150         about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
151             public boolean onMenuItemClick(final MenuItem menuItem) {
152                 final Intent intent = new Intent().setClassName(AboutActivity.class
153                         .getPackage().getName(), AboutActivity.class.getCanonicalName());
154                 context.startActivity(intent);
155                 return false;
156             }
157         });
158
159         final MenuItem help = menu.add(getString(R.string.help));
160         help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
161         help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
162             public boolean onMenuItemClick(final MenuItem menuItem) {
163                 context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent());
164                 return false;
165             }
166         });
167
168         final MenuItem preferences = menu.add(getString(R.string.settings));
169         preferences.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
170         preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
171             public boolean onMenuItemClick(final MenuItem menuItem) {
172                 PreferenceActivity.prefsMightHaveChanged = true;
173                 final Intent intent = new Intent().setClassName(PreferenceActivity.class
174                         .getPackage().getName(), PreferenceActivity.class.getCanonicalName());
175                 context.startActivity(intent);
176                 return false;
177             }
178         });
179
180         final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
181         reportIssue.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
182         reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
183             public boolean onMenuItemClick(final MenuItem menuItem) {
184                 final Intent intent = new Intent(Intent.ACTION_VIEW);
185                 intent.setData(Uri
186                         .parse("http://code.google.com/p/quickdic-dictionary/issues/entry"));
187                 context.startActivity(intent);
188                 return false;
189             }
190         });
191     }
192
193     public synchronized File getDictDir() {
194         // This metaphor doesn't work, because we've already reset
195         // prefsMightHaveChanged.
196         // if (dictDir == null || PreferenceActivity.prefsMightHaveChanged) {
197         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
198         final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
199         String dir = prefs.getString(getString(R.string.quickdicDirectoryKey),
200                 defaultDictDir.getAbsolutePath());
201         if (dir.isEmpty()) {
202             dir = defaultDictDir.getAbsolutePath();
203         }
204         dictDir = new File(dir);
205         dictDir.mkdirs();
206         return dictDir;
207     }
208
209     public C.Theme getSelectedTheme() {
210         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
211         final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
212         if (theme.equals("themeLight")) {
213             return C.Theme.LIGHT;
214         } else {
215             return C.Theme.DEFAULT;
216         }
217     }
218
219     public File getPath(String uncompressedFilename) {
220         return new File(getDictDir(), uncompressedFilename);
221     }
222
223     String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
224     String defaultLangName = null;
225     final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
226
227     public String isoCodeToLocalizedLanguageName(final String isoCode) {
228         final Language.LanguageResources languageResources = Language.isoCodeToResources
229                 .get(isoCode);
230         final String lang = languageResources != null ? getApplicationContext().getString(
231                 languageResources.nameId) : isoCode;
232         return lang;
233     }
234
235     public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
236         // Hack to put the default locale first in the name.
237         if (indexInfos.size() > 1 &&
238                 indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
239             List<IndexInfo> result = new ArrayList<DictionaryInfo.IndexInfo>(indexInfos);
240             ListUtil.swap(result, 0, 1);
241             return result;
242         }
243         return indexInfos;
244     }
245
246     public synchronized String getDictionaryName(final String uncompressedFilename) {
247         final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
248         if (!currentLocale.equals(defaultLangISO2)) {
249             defaultLangISO2 = currentLocale;
250             fileToNameCache.clear();
251             defaultLangName = null;
252         }
253         if (defaultLangName == null) {
254             defaultLangName = isoCodeToLocalizedLanguageName(defaultLangISO2);
255         }
256
257         String name = fileToNameCache.get(uncompressedFilename);
258         if (name != null) {
259             return name;
260         }
261
262         final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
263                 .get(uncompressedFilename);
264         if (dictionaryInfo != null) {
265             final StringBuilder nameBuilder = new StringBuilder();
266
267             List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
268             for (int i = 0; i < sortedIndexInfos.size(); ++i) {
269                 if (i > 0) {
270                     nameBuilder.append("-");
271                 }
272                 nameBuilder
273                         .append(isoCodeToLocalizedLanguageName(sortedIndexInfos.get(i).shortName));
274             }
275             name = nameBuilder.toString();
276         } else {
277             name = uncompressedFilename.replace(".quickdic", "");
278         }
279         fileToNameCache.put(uncompressedFilename, name);
280         return name;
281     }
282
283     public View createButton(final Context context, final DictionaryInfo dictionaryInfo,
284             final IndexInfo indexInfo) {
285         LanguageResources languageResources = Language.isoCodeToResources.get(indexInfo.shortName);
286         View result;
287
288         if (languageResources == null || languageResources.flagId <= 0) {
289             Button button = new Button(context);
290             button.setText(indexInfo.shortName);
291             result = button;
292         } else {
293             ImageButton button = new ImageButton(context);
294             button.setImageResource(languageResources.flagId);
295             button.setScaleType(ScaleType.FIT_CENTER);
296             result = button;
297         }
298         result.setMinimumWidth(languageButtonPixels);
299         result.setMinimumHeight(languageButtonPixels * 2 / 3);
300         // result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
301         // LayoutParams.WRAP_CONTENT));
302         return result;
303     }
304
305     public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
306         dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
307         dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
308         PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
309     }
310
311     public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
312         while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {
313         }
314         ;
315         dictionaryConfig.uncompressedFilenameToDictionaryInfo
316                 .remove(dictionaryInfo.uncompressedFilename);
317         getPath(dictionaryInfo.uncompressedFilename).delete();
318         PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
319     }
320
321     final Collator collator = Collator.getInstance();
322     final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
323         @Override
324         public int compare(String uncompressedFilename1, String uncompressedFilename2) {
325             final String name1 = getDictionaryName(uncompressedFilename1);
326             final String name2 = getDictionaryName(uncompressedFilename2);
327             if (defaultLangName.length() > 0) {
328                 if (name1.startsWith(defaultLangName + "-")
329                         && !name2.startsWith(defaultLangName + "-")) {
330                     return -1;
331                 } else if (name2.startsWith(defaultLangName + "-")
332                         && !name1.startsWith(defaultLangName + "-")) {
333                     return 1;
334                 }
335             }
336             return collator.compare(name1, name2);
337         }
338     };
339     final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
340         @Override
341         public int compare(DictionaryInfo d1, DictionaryInfo d2) {
342             // Single-index dictionaries first.
343             if (d1.indexInfos.size() != d2.indexInfos.size()) {
344                 return d1.indexInfos.size() - d2.indexInfos.size();
345             }
346             return uncompressedFilenameComparator.compare(d1.uncompressedFilename,
347                     d2.uncompressedFilename);
348         }
349     };
350
351     public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
352         new Thread(new Runnable() {
353             @Override
354             public void run() {
355                 final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
356                 synchronized (this) {
357                     oldDictionaryConfig.dictionaryFilesOrdered
358                             .addAll(dictionaryConfig.dictionaryFilesOrdered);
359                 }
360                 final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
361                 for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
362                     final File dictFile = getPath(uncompressedFilename);
363                     final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
364                     if (dictionaryInfo != null) {
365                         newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
366                         newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
367                                 uncompressedFilename, dictionaryInfo);
368                     }
369                 }
370
371                 // Are there dictionaries on the device that we didn't know
372                 // about already?
373                 // Pick them up and put them at the end of the list.
374                 final List<String> toAddSorted = new ArrayList<String>();
375                 final File[] dictDirFiles = getDictDir().listFiles();
376                 if (dictDirFiles != null) {
377                     for (final File file : dictDirFiles) {
378                         if (file.getName().endsWith(".zip")) {
379                             if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
380                                     .containsKey(file.getName().replace(".zip", ""))) {
381                                 file.delete();
382                             }
383                         }
384                         if (!file.getName().endsWith(".quickdic")) {
385                             continue;
386                         }
387                         if (newDictionaryConfig.uncompressedFilenameToDictionaryInfo
388                                 .containsKey(file.getName())) {
389                             // We have it in our list already.
390                             continue;
391                         }
392                         final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
393                         if (dictionaryInfo == null) {
394                             Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
395                             continue;
396                         }
397
398                         toAddSorted.add(file.getName());
399                         newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
400                                 file.getName(), dictionaryInfo);
401                     }
402                 } else {
403                     Log.w(LOG, "dictDir is not a diretory: " + getDictDir().getPath());
404                 }
405                 if (!toAddSorted.isEmpty()) {
406                     Collections.sort(toAddSorted, uncompressedFilenameComparator);
407                     newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
408                 }
409
410                 PersistentObjectCache.getInstance()
411                         .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
412                 synchronized (this) {
413                     dictionaryConfig = newDictionaryConfig;
414                 }
415
416                 try {
417                     onUpdateFinished.run();
418                 } catch (Exception e) {
419                     Log.e(LOG, "Exception running callback.", e);
420                 }
421             }
422         }).start();
423     }
424
425     public boolean matchesFilters(final DictionaryInfo dictionaryInfo, final String[] filters) {
426         if (filters == null) {
427             return true;
428         }
429         for (final String filter : filters) {
430             if (!getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(
431                     filter)) {
432                 return false;
433             }
434         }
435         return true;
436     }
437
438     public synchronized List<DictionaryInfo> getDictionariesOnDevice(String[] filters) {
439         final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
440                 dictionaryConfig.dictionaryFilesOrdered.size());
441         for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
442             final DictionaryInfo dictionaryInfo = dictionaryConfig.uncompressedFilenameToDictionaryInfo
443                     .get(uncompressedFilename);
444             if (dictionaryInfo != null && matchesFilters(dictionaryInfo, filters)) {
445                 result.add(dictionaryInfo);
446             }
447         }
448         return result;
449     }
450
451     public List<DictionaryInfo> getDownloadableDictionaries(String[] filters) {
452         final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
453                 dictionaryConfig.dictionaryFilesOrdered.size());
454
455         final Map<String, DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(
456                 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO);
457         remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
458         for (final DictionaryInfo dictionaryInfo : remaining.values()) {
459             if (matchesFilters(dictionaryInfo, filters)) {
460                 result.add(dictionaryInfo);
461             }
462         }
463         Collections.sort(result, dictionaryInfoComparator);
464         return result;
465     }
466
467     public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
468         return dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename) != null;
469     }
470
471     public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
472         final DictionaryInfo downloadable =
473                 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(
474                         dictionaryInfo.uncompressedFilename);
475         return downloadable != null &&
476                 downloadable.creationMillis > dictionaryInfo.creationMillis;
477     }
478
479     public DictionaryInfo getDownloadable(final String uncompressedFilename) {
480         final DictionaryInfo downloadable = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
481                 .get(uncompressedFilename);
482         return downloadable;
483     }
484
485 }