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;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
26 import android.content.Context;
27 import android.os.Environment;
29 import com.hughes.android.dictionary.engine.Language;
31 public final class QuickDicConfig implements Serializable {
33 private static final long serialVersionUID = 6711617368780900979L;
35 final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
37 public QuickDicConfig(final Context context) {
38 addDefaultDictionaries(context);
41 public void addDefaultDictionaries(final Context context) {
42 for (final DictionaryInfo dictionaryInfo : getDefaultDictionaries(context).values()) {
43 addOrReplace(dictionaryInfo);
47 private static Map<String,DictionaryInfo> defaultDictionaries = null;
48 public synchronized static Map<String,DictionaryInfo> getDefaultDictionaries(final Context context) {
49 if (defaultDictionaries != null) {
50 return defaultDictionaries;
53 defaultDictionaries = new LinkedHashMap<String, DictionaryInfo>();
55 final BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
58 while ((line = reader.readLine()) != null) {
59 if (line.startsWith("#") || line.length() == 0) {
62 final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
64 for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
65 final Integer langCode = Language.isoCodeToResourceId.get(dictionaryInfo.indexInfos.get(i).langIso);
66 final String lang = langCode != null ? context.getString(langCode) : dictionaryInfo.indexInfos.get(i).langIso;
72 dictionaryInfo.name = name;
73 dictionaryInfo.localFile = Environment.getExternalStorageDirectory().getName() + "/quickdic/" + dictionaryInfo.uncompressedFilename;
74 defaultDictionaries.put(dictionaryInfo.localFile, dictionaryInfo);
76 } catch (IOException e) {
77 defaultDictionaries = null;
78 return new LinkedHashMap<String, DictionaryInfo>();
81 return defaultDictionaries;
84 private void addOrReplace(final DictionaryInfo dictionaryConfig) {
85 for (int i = 0; i < dictionaryInfos.size(); ++i) {
86 if (dictionaryInfos.get(i).uncompressedFilename.equals(dictionaryConfig.uncompressedFilename)) {
87 dictionaryInfos.set(i, dictionaryConfig);
91 dictionaryInfos.add(dictionaryConfig);
94 DictionaryInfo getDictionaryInfoByFile(final String dictFile) throws Exception {
95 for (int i = 0; i < dictionaryInfos.size(); ++i) {
96 if (dictionaryInfos.get(i).localFile.equals(dictFile)) {
97 return dictionaryInfos.get(i);
100 throw new Exception("Not found: " + dictFile);