1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary;
17 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import android.app.Application;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.SharedPreferences;
33 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
34 import android.net.Uri;
35 import android.preference.PreferenceManager;
36 import android.util.Log;
37 import android.view.Menu;
38 import android.view.MenuItem;
39 import android.view.MenuItem.OnMenuItemClickListener;
41 import com.hughes.android.dictionary.engine.Dictionary;
42 import com.hughes.android.dictionary.engine.Language;
43 import com.hughes.android.dictionary.engine.TransliteratorManager;
44 import com.hughes.android.util.PersistentObjectCache;
45 import com.ibm.icu.text.Collator;
47 public class DictionaryApplication extends Application {
49 static final String LOG = "QuickDicApp";
51 // Static, determined by resources (and locale).
53 static Map<String,DictionaryInfo> DOWNLOADABLE_NAME_TO_INFO = null;
55 static final class DictionaryConfig implements Serializable {
56 private static final long serialVersionUID = -1444177164708201263L;
57 // User-ordered list, persisted, just the ones that are/have been present.
58 final List<String> dictionaryFilesOrdered = new ArrayList<String>();
59 final Map<String, DictionaryInfo> dictionaryInfoCache = new LinkedHashMap<String, DictionaryInfo>();
61 DictionaryConfig dictionaryConfig = null;
63 static final class DictionaryHistory implements Serializable {
64 private static final long serialVersionUID = -4842995032541390284L;
65 // User-ordered list, persisted, just the ones that are/have been present.
66 final List<DictionaryLink> dictionaryLinks = new ArrayList<DictionaryLink>();
68 DictionaryHistory dictionaryHistory = null;
72 static synchronized void staticInit(final Context context) {
73 if (DOWNLOADABLE_NAME_TO_INFO != null) {
76 DOWNLOADABLE_NAME_TO_INFO = new LinkedHashMap<String,DictionaryInfo>();
77 final BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
80 while ((line = reader.readLine()) != null) {
81 if (line.startsWith("#") || line.length() == 0) {
84 final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
85 DOWNLOADABLE_NAME_TO_INFO.put(dictionaryInfo.uncompressedFilename, dictionaryInfo);
88 } catch (IOException e) {
89 Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
95 public void onCreate() {
97 Log.d("QuickDic", "Application: onCreate");
98 TransliteratorManager.init(null);
99 staticInit(getApplicationContext());
101 // Load the dictionaries we know about.
102 dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(C.DICTIONARY_CONFIGS, DictionaryConfig.class);
103 if (dictionaryConfig == null) {
104 dictionaryConfig = new DictionaryConfig();
108 setTheme(getSelectedTheme().themeId);
109 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
110 prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
112 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
114 Log.d("THAD", "prefs changed: " + key);
115 if (key.equals(getString(R.string.themeKey))) {
116 setTheme(getSelectedTheme().themeId);
122 public void onCreateGlobalOptionsMenu(
123 final Context context, final Menu menu) {
124 final MenuItem about = menu.add(getString(R.string.about));
125 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
126 public boolean onMenuItemClick(final MenuItem menuItem) {
127 final Intent intent = new Intent().setClassName(AboutActivity.class
128 .getPackage().getName(), AboutActivity.class.getCanonicalName());
129 context.startActivity(intent);
134 final MenuItem help = menu.add(getString(R.string.help));
135 help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
136 public boolean onMenuItemClick(final MenuItem menuItem) {
137 context.startActivity(HelpActivity.getLaunchIntent());
142 final MenuItem preferences = menu.add(getString(R.string.preferences));
143 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
144 public boolean onMenuItemClick(final MenuItem menuItem) {
145 PreferenceActivity.prefsMightHaveChanged = true;
146 final Intent intent = new Intent().setClassName(PreferenceActivity.class
147 .getPackage().getName(), PreferenceActivity.class.getCanonicalName());
148 context.startActivity(intent);
154 final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
155 reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
156 public boolean onMenuItemClick(final MenuItem menuItem) {
157 final Intent intent = new Intent(Intent.ACTION_VIEW);
158 intent.setData(Uri.parse("http://code.google.com/p/quickdic-dictionary/issues/entry"));
159 context.startActivity(intent);
165 public synchronized File getDictDir() {
166 // This metaphore doesn't work, because we've already reset prefsMightHaveChanged.
167 // if (dictDir == null || PreferenceActivity.prefsMightHaveChanged) {
168 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
169 final String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), getString(R.string.quickdicDirectoryDefault));
170 dictDir = new File(dir);
176 public C.Theme getSelectedTheme() {
177 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
178 final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
179 if (theme.equals("themeLight")) {
180 return C.Theme.LIGHT;
182 return C.Theme.DEFAULT;
186 public File getPath(String uncompressedFilename) {
187 return new File(getDictDir(), uncompressedFilename);
191 final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
193 public String getLanguageName(final String isoCode) {
194 final Language.LanguageResources languageResources = Language.isoCodeToResources.get(isoCode);
195 final String lang = languageResources != null ? getApplicationContext().getString(languageResources.nameId) : isoCode;
199 public synchronized String getDictionaryName(final String uncompressedFilename) {
200 String name = fileToNameCache.get(uncompressedFilename);
205 final DictionaryInfo dictionaryInfo = DOWNLOADABLE_NAME_TO_INFO.get(uncompressedFilename);
206 if (dictionaryInfo != null) {
207 final StringBuilder nameBuilder = new StringBuilder();
208 for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
210 nameBuilder.append("-");
212 nameBuilder.append(getLanguageName(dictionaryInfo.indexInfos.get(i).shortName));
214 name = nameBuilder.toString();
216 name = uncompressedFilename.replace(".quickdic", "");
218 fileToNameCache.put(uncompressedFilename, name);
222 public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
223 dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
224 dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
225 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
228 public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
229 while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {};
230 dictionaryConfig.dictionaryInfoCache.remove(dictionaryInfo.uncompressedFilename);
231 getPath(dictionaryInfo.uncompressedFilename).delete();
232 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
235 final Collator collator = Collator.getInstance();
236 final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
238 public int compare(String uncompressedFilename1, String uncompressedFilename2) {
239 return collator.compare(getDictionaryName(uncompressedFilename1), getDictionaryName(uncompressedFilename2));
242 final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
244 public int compare(DictionaryInfo d1, DictionaryInfo d2) {
245 return uncompressedFilenameComparator.compare(d1.uncompressedFilename, d2.uncompressedFilename);
249 public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
250 new Thread(new Runnable() {
253 final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
255 oldDictionaryConfig.dictionaryFilesOrdered.addAll(dictionaryConfig.dictionaryFilesOrdered);
257 final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
258 for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
259 final File dictFile = getPath(uncompressedFilename);
260 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
261 if (dictionaryInfo != null) {
262 newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
263 newDictionaryConfig.dictionaryInfoCache.put(uncompressedFilename, dictionaryInfo);
267 // Are there dictionaries on the device that we didn't know about already?
268 // Pick them up and put them at the end of the list.
269 final List<String> toAddSorted = new ArrayList<String>();
270 final File[] dictDirFiles = getDictDir().listFiles();
271 if (dictDirFiles != null) {
272 for (final File file : dictDirFiles) {
273 if (file.getName().endsWith(".zip")) {
274 if (DOWNLOADABLE_NAME_TO_INFO.containsKey(file.getName().replace(".zip", ""))) {
278 if (!file.getName().endsWith(".quickdic")) {
281 if (newDictionaryConfig.dictionaryInfoCache.containsKey(file.getName())) {
282 // We have it in our list already.
285 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
286 if (dictionaryInfo == null) {
287 Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
291 toAddSorted.add(file.getName());
292 newDictionaryConfig.dictionaryInfoCache.put(file.getName(), dictionaryInfo);
295 Log.w(LOG, "dictDir is not a diretory: " + getDictDir().getPath());
297 if (!toAddSorted.isEmpty()) {
298 Collections.sort(toAddSorted, uncompressedFilenameComparator);
299 newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
302 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
303 synchronized (this) {
304 dictionaryConfig = newDictionaryConfig;
308 onUpdateFinished.run();
309 } catch (Exception e) {
310 Log.e(LOG, "Exception running callback.", e);
315 public synchronized List<DictionaryInfo> getUsableDicts() {
316 final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(dictionaryConfig.dictionaryFilesOrdered.size());
317 for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
318 final DictionaryInfo dictionaryInfo = dictionaryConfig.dictionaryInfoCache.get(uncompressedFilename);
319 if (dictionaryInfo != null) {
320 result.add(dictionaryInfo);
326 public synchronized List<DictionaryInfo> getAllDictionaries() {
327 final List<DictionaryInfo> result = getUsableDicts();
329 // The downloadable ones.
330 final Map<String,DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(DOWNLOADABLE_NAME_TO_INFO);
331 remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
332 final List<DictionaryInfo> toAddSorted = new ArrayList<DictionaryInfo>(remaining.values());
333 Collections.sort(toAddSorted, dictionaryInfoComparator);
334 result.addAll(toAddSorted);
339 public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
340 return dictionaryConfig.dictionaryInfoCache.get(uncompressedFilename) != null;
343 public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
344 final DictionaryInfo downloadable = DOWNLOADABLE_NAME_TO_INFO.get(dictionaryInfo.uncompressedFilename);
345 return downloadable != null && downloadable.creationMillis > dictionaryInfo.creationMillis;
348 public DictionaryInfo getDownloadable(final String uncompressedFilename) {
349 final DictionaryInfo downloadable = DOWNLOADABLE_NAME_TO_INFO.get(uncompressedFilename);