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