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