]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Try to fix font issue.
[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       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
136       builder.setCancelable(false);
137       final WebView webView = new WebView(getApplicationContext());
138       webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
139       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
140           public void onClick(DialogInterface dialog, int id) {
141                dialog.cancel();
142           }
143       });
144       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
145       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
146       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
147       webView.setLayoutParams(layoutParams);
148       builder.setView(webView);
149       final AlertDialog alert = builder.create();
150       alert.getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
151       alert.show();
152       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
153     }
154   }
155   
156   @Override
157   protected void onStart() {
158     super.onStart();
159     uiHandler = new Handler();
160   }
161   
162   @Override
163   protected void onStop() {
164     super.onStop();
165     uiHandler = null;
166   }
167   
168   @Override
169   protected void onResume() {
170     super.onResume();
171     
172     if (PreferenceActivity.prefsMightHaveChanged) {
173       PreferenceActivity.prefsMightHaveChanged = false;
174       finish();
175       startActivity(getIntent());
176     }
177     
178     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
179     showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
180     
181     if (!blockAutoLaunch &&
182         getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
183         prefs.contains(C.DICT_FILE) && 
184         prefs.contains(C.INDEX_INDEX)) {
185       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
186       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
187       finish();
188       return;
189     }
190     
191     application.backgroundUpdateDictionaries(new Runnable() {
192       @Override
193       public void run() {
194         if (uiHandler == null) {
195           return;
196         }
197         uiHandler.post(new Runnable() {
198           @Override
199           public void run() {
200             setListAdapter(adapter = new Adapter());
201           }
202         });
203       }
204     });
205
206     setListAdapter(adapter = new Adapter());
207   }
208
209   public boolean onCreateOptionsMenu(final Menu menu) {
210     application.onCreateGlobalOptionsMenu(this, menu);
211     return true;
212   }
213   
214
215   @Override
216   public void onCreateContextMenu(final ContextMenu menu, final View view,
217       final ContextMenuInfo menuInfo) {
218     super.onCreateContextMenu(menu, view, menuInfo);
219     
220     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
221     final int position = adapterContextMenuInfo.position;
222     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
223     
224     if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
225       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
226       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
227         @Override
228         public boolean onMenuItemClick(MenuItem item) {
229           application.moveDictionaryToTop(dictionaryInfo);
230           setListAdapter(adapter = new Adapter());
231           return true;
232         }
233       });
234     }
235
236     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
237     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
238       @Override
239       public boolean onMenuItemClick(MenuItem item) {
240         application.deleteDictionary(dictionaryInfo);
241         setListAdapter(adapter = new Adapter());
242         return true;
243       }
244     });
245
246     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
247     if (downloadable != null) {
248       final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
249       downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
250         @Override
251         public boolean onMenuItemClick(MenuItem item) {
252           final Intent intent = getDownloadIntent(downloadable);
253           startActivity(intent);
254           setListAdapter(adapter = new Adapter());
255           return true;
256         }
257       });
258     }
259
260   }
261
262   private Intent getDownloadIntent(final DictionaryInfo downloadable) {
263     final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
264         application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
265         downloadable.dictInfo);
266     return intent;
267   }
268   
269   private void onFilterTextChanged() {
270     setListAdapter(adapter = new Adapter());
271
272   }
273
274   private void onShowLocalChanged() {
275     setListAdapter(adapter = new Adapter());
276     Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
277     prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
278     prefs.commit();
279   }
280   
281   private void onClick(int index) {
282     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
283     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
284     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
285       final Intent intent = getDownloadIntent(downloadable);
286       startActivity(intent);
287     } else {
288       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
289       startActivity(intent);
290     }
291   }
292   
293   class Adapter extends BaseAdapter {
294     
295     final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
296     
297     Adapter() {
298       final String filter = filterText.getText().toString().trim().toLowerCase();
299       for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
300         boolean canShow = true;
301         if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
302           canShow = false;
303         }
304         if (canShow && filter.length() > 0) {
305           if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
306             canShow = false;
307           }
308         }
309         if (canShow) {
310           dictionaryInfos.add(dictionaryInfo);
311           
312         }
313       }
314     }
315
316     @Override
317     public int getCount() {
318       return dictionaryInfos.size();
319     }
320
321     @Override
322     public DictionaryInfo getItem(int position) {
323       return dictionaryInfos.get(position);
324     }
325
326     @Override
327     public long getItemId(int position) {
328       return position;
329     }
330     
331     @Override
332     public View getView(final int position, final View convertView, final ViewGroup parent) {
333       final LinearLayout result;
334       // Android 4.0.3 leaks memory like crazy if we don't do this.
335       if (convertView instanceof LinearLayout) {
336         result = (LinearLayout) convertView;
337         result.removeAllViews();
338       } else {
339         result = new LinearLayout(parent.getContext());
340       }
341       
342       final DictionaryInfo dictionaryInfo = getItem(position);
343       result.setOrientation(LinearLayout.VERTICAL);
344
345       final LinearLayout row = new LinearLayout(parent.getContext());
346       row.setOrientation(LinearLayout.HORIZONTAL);
347       result.addView(row);
348
349       {
350       final TextView textView = new TextView(parent.getContext());
351       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
352       textView.setText(name);
353       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
354       row.addView(textView);
355       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
356       layoutParams.weight = 1.0f;
357       textView.setLayoutParams(layoutParams);
358       }
359       
360       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
361       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); 
362       if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
363         final Button downloadButton = new Button(parent.getContext());
364         downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
365         final Intent intent = getDownloadIntent(downloadable);
366         downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
367         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
368         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
369         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
370         downloadButton.setLayoutParams(layoutParams);
371         row.addView(downloadButton);
372       } else {
373         final ImageView checkMark = new ImageView(parent.getContext());
374         checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
375         row.addView(checkMark);
376       }
377
378       // Add the information about each index.
379       final LinearLayout row2 = new LinearLayout(parent.getContext());
380       row2.setOrientation(LinearLayout.HORIZONTAL);
381       final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
382       row2.setLayoutParams(layoutParams);
383       result.addView(row2);
384       final StringBuilder builder = new StringBuilder();
385       for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
386         if (builder.length() > 0) {
387           builder.append(" | ");
388         }
389         builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
390       }
391       final TextView indexView = new TextView(parent.getContext());
392       indexView.setText(builder.toString());
393       row2.addView(indexView);
394       
395       
396       // Because we have a Button inside a ListView row:
397       // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
398       result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
399       result.setClickable(true);
400       result.setFocusable(true);
401       result.setLongClickable(true);
402       result.setBackgroundResource(android.R.drawable.menuitem_background);
403       result.setOnClickListener(new TextView.OnClickListener() {
404         @Override
405         public void onClick(View v) {
406           DictionaryManagerActivity.this.onClick(position);
407         }
408       });
409       
410       return result;
411     }
412   }
413
414 }