]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Lots of bug fixes! Workaround for ICS OOM, background loading of which
[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.List;
19
20 import android.app.AlertDialog;
21 import android.app.ListActivity;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.preference.PreferenceManager;
28 import android.util.Log;
29 import android.util.TypedValue;
30 import android.view.ContextMenu;
31 import android.view.ContextMenu.ContextMenuInfo;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.MenuItem.OnMenuItemClickListener;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.view.WindowManager;
38 import android.webkit.WebView;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.AdapterContextMenuInfo;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.BaseAdapter;
43 import android.widget.Button;
44 import android.widget.ImageView;
45 import android.widget.LinearLayout;
46 import android.widget.TextView;
47
48 import com.hughes.android.dictionary.C.Theme;
49 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
50 import com.hughes.android.util.IntentLauncher;
51 import com.hughes.util.StringUtil;
52
53 public class DictionaryManagerActivity extends ListActivity {
54
55   static final String LOG = "QuickDic";
56   static boolean canAutoLaunch = true;
57
58   DictionaryApplication application;
59   Adapter adapter;
60   
61   Handler uiHandler;
62   
63   public static Intent getLaunchIntent() {
64     final Intent intent = new Intent();
65     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
66         DictionaryManagerActivity.class.getName());
67     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
68     return intent;
69   }
70   
71   public void onCreate(Bundle savedInstanceState) {
72     setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
73
74     super.onCreate(savedInstanceState);
75     Log.d(LOG, "onCreate:" + this);
76     
77     application = (DictionaryApplication) getApplication();
78
79     // UI init.
80     setContentView(R.layout.list_activity);
81
82     getListView().setOnItemClickListener(new OnItemClickListener() {
83       @Override
84       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
85           long id) {
86         onClick(index);
87       }
88     });
89     
90     getListView().setClickable(true);
91
92     // ContextMenu.
93     registerForContextMenu(getListView());
94
95     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
96     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
97     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
98       canAutoLaunch = false;
99       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
100       builder.setCancelable(false);
101       final WebView webView = new WebView(getApplicationContext());
102       webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
103       builder.setView(webView);
104       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
105           public void onClick(DialogInterface dialog, int id) {
106                dialog.cancel();
107           }
108       });
109       final AlertDialog alert = builder.create();
110       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
111       layoutParams.copyFrom(alert.getWindow().getAttributes());
112       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
113       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
114       alert.show();
115       alert.getWindow().setAttributes(layoutParams);
116       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
117     }
118     
119     if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
120       canAutoLaunch = false;
121     }
122   }
123   
124   @Override
125   protected void onStart() {
126     super.onStart();
127     uiHandler = new Handler();
128   }
129   
130   @Override
131   protected void onStop() {
132     super.onStop();
133     uiHandler = null;
134   }
135
136   private void onClick(int index) {
137     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
138     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
139     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
140       final Intent intent = getDownloadIntent(downloadable);
141       startActivity(intent);
142     } else {
143       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
144       startActivity(intent);
145     }
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     if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
160       canAutoLaunch = false;  // Only autolaunch once per-process, on startup.
161       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
162       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
163       // Don't finish, so that user can hit back and get here.
164       //finish();
165       return;
166     }
167     
168     application.backgroundUpdateDictionaries(new Runnable() {
169       @Override
170       public void run() {
171         if (uiHandler == null) {
172           return;
173         }
174         uiHandler.post(new Runnable() {
175           @Override
176           public void run() {
177             setListAdapter(adapter = new Adapter());
178           }
179         });
180       }
181     });
182
183     setListAdapter(adapter = new Adapter());
184   }
185
186   public boolean onCreateOptionsMenu(final Menu menu) {
187     application.onCreateGlobalOptionsMenu(this, menu);
188     return true;
189   }
190   
191
192   @Override
193   public void onCreateContextMenu(final ContextMenu menu, final View view,
194       final ContextMenuInfo menuInfo) {
195     super.onCreateContextMenu(menu, view, menuInfo);
196     
197     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
198     final int position = adapterContextMenuInfo.position;
199     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
200     
201     if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
202       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
203       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
204         @Override
205         public boolean onMenuItemClick(MenuItem item) {
206           application.moveDictionaryToTop(dictionaryInfo);
207           setListAdapter(adapter = new Adapter());
208           return true;
209         }
210       });
211     }
212
213     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
214     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
215       @Override
216       public boolean onMenuItemClick(MenuItem item) {
217         application.deleteDictionary(dictionaryInfo);
218         setListAdapter(adapter = new Adapter());
219         return true;
220       }
221     });
222
223     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
224     if (downloadable != null) {
225       final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
226       downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
227         @Override
228         public boolean onMenuItemClick(MenuItem item) {
229           final Intent intent = getDownloadIntent(downloadable);
230           startActivity(intent);
231           setListAdapter(adapter = new Adapter());
232           return true;
233         }
234
235       });
236     }
237
238   }
239
240   private Intent getDownloadIntent(final DictionaryInfo downloadable) {
241     final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
242         application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
243         downloadable.dictInfo);
244     return intent;
245   }
246
247   class Adapter extends BaseAdapter {
248     
249     final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
250
251     @Override
252     public int getCount() {
253       return dictionaryInfos.size();
254     }
255
256     @Override
257     public DictionaryInfo getItem(int position) {
258       return dictionaryInfos.get(position);
259     }
260
261     @Override
262     public long getItemId(int position) {
263       return position;
264     }
265     
266     @Override
267     public View getView(final int position, final View convertView, final ViewGroup parent) {
268       final LinearLayout result;
269       // Android 4.0.3 leaks memory like crazy if we don't do this.
270       if (convertView instanceof LinearLayout) {
271         result = (LinearLayout) convertView;
272         result.removeAllViews();
273       } else {
274         result = new LinearLayout(parent.getContext());
275       }
276       
277       final DictionaryInfo dictionaryInfo = getItem(position);
278       result.setOrientation(LinearLayout.VERTICAL);
279
280       final LinearLayout row = new LinearLayout(parent.getContext());
281       row.setOrientation(LinearLayout.HORIZONTAL);
282       result.addView(row);
283
284       {
285       final TextView textView = new TextView(parent.getContext());
286       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
287       textView.setText(name);
288       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
289       row.addView(textView);
290       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
291       layoutParams.weight = 1.0f;
292       textView.setLayoutParams(layoutParams);
293       }
294       
295       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
296       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); 
297       if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
298         final Button downloadButton = new Button(parent.getContext());
299         downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
300         final Intent intent = getDownloadIntent(downloadable);
301         downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
302         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
303         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
304         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
305         downloadButton.setLayoutParams(layoutParams);
306         row.addView(downloadButton);
307       } else {
308         final ImageView checkMark = new ImageView(parent.getContext());
309         checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
310         row.addView(checkMark);
311       }
312
313       // Add the information about each index.
314       final LinearLayout row2 = new LinearLayout(parent.getContext());
315       row2.setOrientation(LinearLayout.HORIZONTAL);
316       final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
317       row2.setLayoutParams(layoutParams);
318       result.addView(row2);
319       final StringBuilder builder = new StringBuilder();
320       for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
321         if (builder.length() > 0) {
322           builder.append(" | ");
323         }
324         builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
325       }
326       final TextView indexView = new TextView(parent.getContext());
327       indexView.setText(builder.toString());
328       row2.addView(indexView);
329       
330       
331       // Because we have a Button inside a ListView row:
332       // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
333       result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
334       result.setClickable(true);
335       result.setFocusable(true);
336       result.setLongClickable(true);
337       result.setBackgroundResource(android.R.drawable.menuitem_background);
338       result.setOnClickListener(new TextView.OnClickListener() {
339         @Override
340         public void onClick(View v) {
341           DictionaryManagerActivity.this.onClick(position);
342         }
343       });
344       
345       return result;
346     }
347   }
348
349 }