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