]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Tried to fix launch last used bug.
[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.DictionaryInfo.IndexInfo;
49 import com.hughes.android.util.IntentLauncher;
50 import com.hughes.util.StringUtil;
51
52 public class DictionaryManagerActivity extends ListActivity {
53
54   static final String LOG = "QuickDic";
55   static final long AUTO_LAUNCH_WAIT_MILLIS = 20 * 1000;
56   static long lastAutoLaunchMillis = -1;
57   static boolean blockAutoLaunch = false;
58
59   DictionaryApplication application;
60   Adapter adapter;
61   
62   Handler uiHandler;
63   
64   public static Intent getLaunchIntent() {
65     final Intent intent = new Intent();
66     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
67         DictionaryManagerActivity.class.getName());
68     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
69     return intent;
70   }
71   
72   public void onCreate(Bundle savedInstanceState) {
73     setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
74
75     super.onCreate(savedInstanceState);
76     Log.d(LOG, "onCreate:" + this);
77     
78     application = (DictionaryApplication) getApplication();
79
80     // UI init.
81     setContentView(R.layout.list_activity);
82
83     getListView().setOnItemClickListener(new OnItemClickListener() {
84       @Override
85       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
86           long id) {
87         onClick(index);
88       }
89     });
90     
91     getListView().setClickable(true);
92
93     // ContextMenu.
94     registerForContextMenu(getListView());
95
96     blockAutoLaunch = false;
97     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
98     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
99     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
100       blockAutoLaunch = true;
101       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
102       builder.setCancelable(false);
103       final WebView webView = new WebView(getApplicationContext());
104       webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
105       builder.setView(webView);
106       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
107           public void onClick(DialogInterface dialog, int id) {
108                dialog.cancel();
109           }
110       });
111       final AlertDialog alert = builder.create();
112       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
113       layoutParams.copyFrom(alert.getWindow().getAttributes());
114       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
115       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
116       alert.show();
117       alert.getWindow().setAttributes(layoutParams);
118       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
119     }
120   }
121   
122   @Override
123   protected void onStart() {
124     super.onStart();
125     uiHandler = new Handler();
126   }
127   
128   @Override
129   protected void onStop() {
130     super.onStop();
131     uiHandler = null;
132   }
133
134   private void onClick(int index) {
135     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
136     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
137     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
138       final Intent intent = getDownloadIntent(downloadable);
139       startActivity(intent);
140     } else {
141       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
142       startActivity(intent);
143     }
144   }
145   
146   @Override
147   protected void onResume() {
148     super.onResume();
149     
150     if (PreferenceActivity.prefsMightHaveChanged) {
151       PreferenceActivity.prefsMightHaveChanged = false;
152       finish();
153       startActivity(getIntent());
154     }
155     
156     final long now = System.currentTimeMillis();
157     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
158     if (now - lastAutoLaunchMillis > AUTO_LAUNCH_WAIT_MILLIS &&
159         getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
160         prefs.contains(C.DICT_FILE) && 
161         prefs.contains(C.INDEX_INDEX) &&
162         !blockAutoLaunch) {
163       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
164       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
165       lastAutoLaunchMillis = now;
166       // Don't finish, so that user can hit back and get here.
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
243   private Intent getDownloadIntent(final DictionaryInfo downloadable) {
244     final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
245         application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
246         downloadable.dictInfo);
247     return intent;
248   }
249
250   class Adapter extends BaseAdapter {
251     
252     final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
253
254     @Override
255     public int getCount() {
256       return dictionaryInfos.size();
257     }
258
259     @Override
260     public DictionaryInfo getItem(int position) {
261       return dictionaryInfos.get(position);
262     }
263
264     @Override
265     public long getItemId(int position) {
266       return position;
267     }
268     
269     @Override
270     public View getView(final int position, final View convertView, final ViewGroup parent) {
271       final LinearLayout result;
272       // Android 4.0.3 leaks memory like crazy if we don't do this.
273       if (convertView instanceof LinearLayout) {
274         result = (LinearLayout) convertView;
275         result.removeAllViews();
276       } else {
277         result = new LinearLayout(parent.getContext());
278       }
279       
280       final DictionaryInfo dictionaryInfo = getItem(position);
281       result.setOrientation(LinearLayout.VERTICAL);
282
283       final LinearLayout row = new LinearLayout(parent.getContext());
284       row.setOrientation(LinearLayout.HORIZONTAL);
285       result.addView(row);
286
287       {
288       final TextView textView = new TextView(parent.getContext());
289       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
290       textView.setText(name);
291       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
292       row.addView(textView);
293       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
294       layoutParams.weight = 1.0f;
295       textView.setLayoutParams(layoutParams);
296       }
297       
298       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
299       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); 
300       if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
301         final Button downloadButton = new Button(parent.getContext());
302         downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
303         final Intent intent = getDownloadIntent(downloadable);
304         downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
305         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
306         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
307         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
308         downloadButton.setLayoutParams(layoutParams);
309         row.addView(downloadButton);
310       } else {
311         final ImageView checkMark = new ImageView(parent.getContext());
312         checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
313         row.addView(checkMark);
314       }
315
316       // Add the information about each index.
317       final LinearLayout row2 = new LinearLayout(parent.getContext());
318       row2.setOrientation(LinearLayout.HORIZONTAL);
319       final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
320       row2.setLayoutParams(layoutParams);
321       result.addView(row2);
322       final StringBuilder builder = new StringBuilder();
323       for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
324         if (builder.length() > 0) {
325           builder.append(" | ");
326         }
327         builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
328       }
329       final TextView indexView = new TextView(parent.getContext());
330       indexView.setText(builder.toString());
331       row2.addView(indexView);
332       
333       
334       // Because we have a Button inside a ListView row:
335       // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
336       result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
337       result.setClickable(true);
338       result.setFocusable(true);
339       result.setLongClickable(true);
340       result.setBackgroundResource(android.R.drawable.menuitem_background);
341       result.setOnClickListener(new TextView.OnClickListener() {
342         @Override
343         public void onClick(View v) {
344           DictionaryManagerActivity.this.onClick(position);
345         }
346       });
347       
348       return result;
349     }
350   }
351
352 }