]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Removed font stuff, added notes. Added language names to what's new.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryManagerActivity.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 import java.util.ArrayList;
19 import java.util.List;
20
21 import android.app.AlertDialog;
22 import android.app.ListActivity;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.SharedPreferences.Editor;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.preference.PreferenceManager;
30 import android.text.Editable;
31 import android.text.TextWatcher;
32 import android.util.Log;
33 import android.util.TypedValue;
34 import android.view.ContextMenu;
35 import android.view.ContextMenu.ContextMenuInfo;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.MenuItem.OnMenuItemClickListener;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.view.WindowManager;
42 import android.webkit.WebView;
43 import android.widget.AdapterView;
44 import android.widget.AdapterView.AdapterContextMenuInfo;
45 import android.widget.AdapterView.OnItemClickListener;
46 import android.widget.BaseAdapter;
47 import android.widget.Button;
48 import android.widget.CheckBox;
49 import android.widget.CompoundButton;
50 import android.widget.CompoundButton.OnCheckedChangeListener;
51 import android.widget.EditText;
52 import android.widget.ImageView;
53 import android.widget.LinearLayout;
54 import android.widget.TextView;
55
56 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
57 import com.hughes.android.util.IntentLauncher;
58 import com.hughes.util.StringUtil;
59
60 public class DictionaryManagerActivity extends ListActivity {
61
62   static final String LOG = "QuickDic";
63   static boolean blockAutoLaunch = false;
64
65   DictionaryApplication application;
66   Adapter adapter;
67   
68   EditText filterText;
69   CheckBox showLocal;
70   
71   Handler uiHandler;
72   
73   public static Intent getLaunchIntent() {
74     final Intent intent = new Intent();
75     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
76         DictionaryManagerActivity.class.getName());
77     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
78     return intent;
79   }
80   
81   public void onCreate(Bundle savedInstanceState) {
82     setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
83
84     super.onCreate(savedInstanceState);
85     Log.d(LOG, "onCreate:" + this);
86     
87     application = (DictionaryApplication) getApplication();
88
89     // UI init.
90     setContentView(R.layout.dictionary_manager_activity);
91     
92     filterText = (EditText) findViewById(R.id.filterText);
93     showLocal = (CheckBox) findViewById(R.id.showLocal);
94     
95     filterText.addTextChangedListener(new TextWatcher() {
96       @Override
97       public void onTextChanged(CharSequence s, int start, int before, int count) {
98       }
99       
100       @Override
101       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
102       }
103       
104       @Override
105       public void afterTextChanged(Editable s) {
106         onFilterTextChanged();
107       }
108     });
109     
110     showLocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {
111       @Override
112       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
113         onShowLocalChanged();
114       }
115     });
116
117     getListView().setOnItemClickListener(new OnItemClickListener() {
118       @Override
119       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
120           long id) {
121         onClick(index);
122       }
123     });
124     
125     getListView().setClickable(true);
126
127     // ContextMenu.
128     registerForContextMenu(getListView());
129
130     blockAutoLaunch = false;
131     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
132     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
133     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
134       blockAutoLaunch = true;
135       startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
136       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
137     }
138   }
139   
140   @Override
141   protected void onStart() {
142     super.onStart();
143     uiHandler = new Handler();
144   }
145   
146   @Override
147   protected void onStop() {
148     super.onStop();
149     uiHandler = null;
150   }
151   
152   @Override
153   protected void onResume() {
154     super.onResume();
155     
156     if (PreferenceActivity.prefsMightHaveChanged) {
157       PreferenceActivity.prefsMightHaveChanged = false;
158       finish();
159       startActivity(getIntent());
160     }
161     
162     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
163     showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
164     
165     if (!blockAutoLaunch &&
166         getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
167         prefs.contains(C.DICT_FILE) && 
168         prefs.contains(C.INDEX_INDEX)) {
169       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
170       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
171       finish();
172       return;
173     }
174     
175     application.backgroundUpdateDictionaries(new Runnable() {
176       @Override
177       public void run() {
178         if (uiHandler == null) {
179           return;
180         }
181         uiHandler.post(new Runnable() {
182           @Override
183           public void run() {
184             setListAdapter(adapter = new Adapter());
185           }
186         });
187       }
188     });
189
190     setListAdapter(adapter = new Adapter());
191   }
192
193   public boolean onCreateOptionsMenu(final Menu menu) {
194     application.onCreateGlobalOptionsMenu(this, menu);
195     return true;
196   }
197   
198
199   @Override
200   public void onCreateContextMenu(final ContextMenu menu, final View view,
201       final ContextMenuInfo menuInfo) {
202     super.onCreateContextMenu(menu, view, menuInfo);
203     
204     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
205     final int position = adapterContextMenuInfo.position;
206     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
207     
208     if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
209       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
210       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
211         @Override
212         public boolean onMenuItemClick(MenuItem item) {
213           application.moveDictionaryToTop(dictionaryInfo);
214           setListAdapter(adapter = new Adapter());
215           return true;
216         }
217       });
218     }
219
220     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
221     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
222       @Override
223       public boolean onMenuItemClick(MenuItem item) {
224         application.deleteDictionary(dictionaryInfo);
225         setListAdapter(adapter = new Adapter());
226         return true;
227       }
228     });
229
230     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
231     if (downloadable != null) {
232       final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
233       downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
234         @Override
235         public boolean onMenuItemClick(MenuItem item) {
236           final Intent intent = getDownloadIntent(downloadable);
237           startActivity(intent);
238           setListAdapter(adapter = new Adapter());
239           return true;
240         }
241       });
242     }
243
244   }
245
246   private Intent getDownloadIntent(final DictionaryInfo downloadable) {
247     final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
248         application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
249         downloadable.dictInfo);
250     return intent;
251   }
252   
253   private void onFilterTextChanged() {
254     setListAdapter(adapter = new Adapter());
255
256   }
257
258   private void onShowLocalChanged() {
259     setListAdapter(adapter = new Adapter());
260     Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
261     prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
262     prefs.commit();
263   }
264   
265   private void onClick(int index) {
266     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
267     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
268     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
269       final Intent intent = getDownloadIntent(downloadable);
270       startActivity(intent);
271     } else {
272       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
273       startActivity(intent);
274     }
275   }
276   
277   class Adapter extends BaseAdapter {
278     
279     final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
280     
281     Adapter() {
282       final String filter = filterText.getText().toString().trim().toLowerCase();
283       for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
284         boolean canShow = true;
285         if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
286           canShow = false;
287         }
288         if (canShow && filter.length() > 0) {
289           if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
290             canShow = false;
291           }
292         }
293         if (canShow) {
294           dictionaryInfos.add(dictionaryInfo);
295           
296         }
297       }
298     }
299
300     @Override
301     public int getCount() {
302       return dictionaryInfos.size();
303     }
304
305     @Override
306     public DictionaryInfo getItem(int position) {
307       return dictionaryInfos.get(position);
308     }
309
310     @Override
311     public long getItemId(int position) {
312       return position;
313     }
314     
315     @Override
316     public View getView(final int position, final View convertView, final ViewGroup parent) {
317       final LinearLayout result;
318       // Android 4.0.3 leaks memory like crazy if we don't do this.
319       if (convertView instanceof LinearLayout) {
320         result = (LinearLayout) convertView;
321         result.removeAllViews();
322       } else {
323         result = new LinearLayout(parent.getContext());
324       }
325       
326       final DictionaryInfo dictionaryInfo = getItem(position);
327       result.setOrientation(LinearLayout.VERTICAL);
328
329       final LinearLayout row = new LinearLayout(parent.getContext());
330       row.setOrientation(LinearLayout.HORIZONTAL);
331       result.addView(row);
332
333       {
334       final TextView textView = new TextView(parent.getContext());
335       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
336       textView.setText(name);
337       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
338       row.addView(textView);
339       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
340       layoutParams.weight = 1.0f;
341       textView.setLayoutParams(layoutParams);
342       }
343       
344       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
345       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); 
346       if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
347         final Button downloadButton = new Button(parent.getContext());
348         downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
349         final Intent intent = getDownloadIntent(downloadable);
350         downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
351         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
352         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
353         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
354         downloadButton.setLayoutParams(layoutParams);
355         row.addView(downloadButton);
356       } else {
357         final ImageView checkMark = new ImageView(parent.getContext());
358         checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
359         row.addView(checkMark);
360       }
361
362       // Add the information about each index.
363       final LinearLayout row2 = new LinearLayout(parent.getContext());
364       row2.setOrientation(LinearLayout.HORIZONTAL);
365       final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
366       row2.setLayoutParams(layoutParams);
367       result.addView(row2);
368       final StringBuilder builder = new StringBuilder();
369       for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
370         if (builder.length() > 0) {
371           builder.append(" | ");
372         }
373         builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
374       }
375       final TextView indexView = new TextView(parent.getContext());
376       indexView.setText(builder.toString());
377       row2.addView(indexView);
378       
379       
380       // Because we have a Button inside a ListView row:
381       // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
382       result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
383       result.setClickable(true);
384       result.setFocusable(true);
385       result.setLongClickable(true);
386       result.setBackgroundResource(android.R.drawable.menuitem_background);
387       result.setOnClickListener(new TextView.OnClickListener() {
388         @Override
389         public void onClick(View v) {
390           DictionaryManagerActivity.this.onClick(position);
391         }
392       });
393       
394       return result;
395     }
396   }
397
398 }