]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryListActivity.java
Add Slovak, increment version, show what's new.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryListActivity.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 java.io.File;
18
19 import android.app.AlertDialog;
20 import android.app.ListActivity;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Bundle;
26 import android.preference.PreferenceManager;
27 import android.util.Log;
28 import android.util.TypedValue;
29 import android.view.ContextMenu;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.ContextMenu.ContextMenuInfo;
35 import android.view.MenuItem.OnMenuItemClickListener;
36 import android.webkit.WebView;
37 import android.widget.AdapterView;
38 import android.widget.BaseAdapter;
39 import android.widget.TableLayout;
40 import android.widget.TextView;
41 import android.widget.AdapterView.AdapterContextMenuInfo;
42 import android.widget.AdapterView.OnItemClickListener;
43
44 import com.hughes.android.util.PersistentObjectCache;
45
46 public class DictionaryListActivity extends ListActivity {
47
48   static final String LOG = "QuickDic";
49   
50   QuickDicConfig quickDicConfig = new QuickDicConfig();
51   
52   
53   public void onCreate(Bundle savedInstanceState) {
54     ((DictionaryApplication)getApplication()).applyTheme(this);
55
56     super.onCreate(savedInstanceState);
57     Log.d(LOG, "onCreate:" + this);
58
59     // UI init.
60     setContentView(R.layout.list_activity);
61
62     getListView().setOnItemClickListener(new OnItemClickListener() {
63       @Override
64       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
65           long id) {
66         onClick(index);
67       }
68     });
69
70     // ContextMenu.
71     registerForContextMenu(getListView());
72
73     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
74     final int introMessageId = 100;
75     if (prefs.getInt(C.INTRO_MESSAGE_SHOWN, 0) < introMessageId) {
76       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
77       builder.setCancelable(false);
78       final WebView webView = new WebView(getApplicationContext());
79       webView.loadData(getString(R.string.thanksForUpdating), "text/html", "utf-8");
80       builder.setView(webView);
81       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
82           public void onClick(DialogInterface dialog, int id) {
83                dialog.cancel();
84           }
85       });
86       final AlertDialog alert = builder.create();
87       alert.show();
88       prefs.edit().putInt(C.INTRO_MESSAGE_SHOWN, introMessageId).commit();
89     }
90   }
91   
92   private void onClick(int dictIndex) {
93     final Intent intent = DictionaryActivity.getIntent(this, dictIndex, 0, "");
94     startActivity(intent);
95   }
96   
97   @Override
98   protected void onResume() {
99     super.onResume();
100     
101     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
102     if (prefs.contains(C.DICT_INDEX) && prefs.contains(C.INDEX_INDEX)) {
103       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
104       startActivity(DictionaryActivity.getIntent(this, prefs.getInt(C.DICT_INDEX, 0), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
105       //finish();
106       return;
107     }
108
109     quickDicConfig = PersistentObjectCache.init(this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);
110     if (quickDicConfig == null) {
111       quickDicConfig = new QuickDicConfig();
112       PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
113     }
114     if (quickDicConfig.currentVersion < QuickDicConfig.LATEST_VERSION) {
115       Log.d(LOG, "Dictionary list is old, updating it.");
116       
117       // Replace <-> with -
118       if (quickDicConfig.currentVersion < 5) {
119         for (final DictionaryConfig config : quickDicConfig.dictionaryConfigs) {
120           config.name = config.name.replace("<->", "-");
121         }
122       }
123       quickDicConfig.addDefaultDictionaries();
124       quickDicConfig.currentVersion = QuickDicConfig.LATEST_VERSION;
125       PersistentObjectCache.init(this).write(C.DICTIONARY_CONFIGS, quickDicConfig);
126     }
127
128     setListAdapter(new Adapter());
129     
130   }
131
132   public boolean onCreateOptionsMenu(final Menu menu) {
133     final MenuItem newDictionaryMenuItem = menu.add(R.string.addDictionary);
134     newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
135           public boolean onMenuItemClick(final MenuItem menuItem) {
136             final DictionaryConfig dictionaryConfig = new DictionaryConfig();
137             dictionaryConfig.name = getString(R.string.newDictionary);
138             quickDicConfig.dictionaryConfigs.add(0, dictionaryConfig);
139             dictionaryConfigsChanged();
140             return false;
141           }
142         });
143
144     final MenuItem addDefaultDictionariesMenuItem = menu.add(R.string.addDefaultDictionaries);
145     addDefaultDictionariesMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
146           public boolean onMenuItemClick(final MenuItem menuItem) {
147             quickDicConfig.addDefaultDictionaries();
148             dictionaryConfigsChanged();
149             return false;
150           }
151         });
152
153     final MenuItem removeAllDictionariesMenuItem = menu.add(R.string.removeAllDictionaries);
154     removeAllDictionariesMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
155           public boolean onMenuItemClick(final MenuItem menuItem) {
156             quickDicConfig.dictionaryConfigs.clear();
157             dictionaryConfigsChanged();
158             return false;
159           }
160         });
161
162     final MenuItem about = menu.add(getString(R.string.about));
163     about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
164       public boolean onMenuItemClick(final MenuItem menuItem) {
165         final Intent intent = new Intent().setClassName(AboutActivity.class
166             .getPackage().getName(), AboutActivity.class.getCanonicalName());
167         startActivity(intent);
168         return false;
169       }
170     });
171     
172     final MenuItem preferences = menu.add(getString(R.string.preferences));
173     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
174       public boolean onMenuItemClick(final MenuItem menuItem) {
175         startActivity(new Intent(DictionaryListActivity.this,
176             PreferenceActivity.class));
177         return false;
178       }
179     });
180     
181     return true;
182   }
183   
184
185   @Override
186   public void onCreateContextMenu(final ContextMenu menu, final View view,
187       final ContextMenuInfo menuInfo) {
188     super.onCreateContextMenu(menu, view, menuInfo);
189     
190     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
191     
192     final MenuItem editMenuItem = menu.add(R.string.editDictionary);
193     editMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
194       @Override
195       public boolean onMenuItemClick(MenuItem item) {
196         startActivity(DictionaryEditActivity.getIntent(adapterContextMenuInfo.position));
197         return true;
198       }
199     });
200
201     if (adapterContextMenuInfo.position > 0) {
202       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
203       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
204         @Override
205         public boolean onMenuItemClick(MenuItem item) {
206           final DictionaryConfig dictionaryConfig = quickDicConfig.dictionaryConfigs.remove(adapterContextMenuInfo.position);
207           quickDicConfig.dictionaryConfigs.add(0, dictionaryConfig);
208           dictionaryConfigsChanged();
209           return true;
210         }
211       });
212     }
213
214     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
215     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
216       @Override
217       public boolean onMenuItemClick(MenuItem item) {
218         quickDicConfig.dictionaryConfigs.remove(adapterContextMenuInfo.position);
219         dictionaryConfigsChanged();
220         return true;
221       }
222     });
223
224   }
225
226   private void dictionaryConfigsChanged() {
227     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
228     setListAdapter(getListAdapter());
229   }
230
231   class Adapter extends BaseAdapter {
232
233     @Override
234     public int getCount() {
235       return quickDicConfig.dictionaryConfigs.size();
236     }
237
238     @Override
239     public DictionaryConfig getItem(int position) {
240       return quickDicConfig.dictionaryConfigs.get(position);
241     }
242
243     @Override
244     public long getItemId(int position) {
245       return position;
246     }
247     
248     @Override
249     public View getView(int position, View convertView, ViewGroup parent) {
250       final DictionaryConfig dictionaryConfig = getItem(position);
251       final TableLayout tableLayout = new TableLayout(parent.getContext());
252       final TextView view = new TextView(parent.getContext());
253       
254       String name = dictionaryConfig.name;
255       if (!new File(dictionaryConfig.localFile).canRead()) {
256         name = getString(R.string.notOnDevice, dictionaryConfig.name);
257       }
258
259       view.setText(name);
260       view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
261       tableLayout.addView(view);
262
263       return tableLayout;
264     }
265     
266   }
267
268   public static Intent getIntent(final Context context) {
269     DictionaryActivity.clearDictionaryPrefs(context);
270     final Intent intent = new Intent();
271     intent.setClassName(DictionaryListActivity.class.getPackage().getName(),
272         DictionaryListActivity.class.getName());
273     return intent;
274   }
275
276 }