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