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