1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary;
18 import java.util.List;
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;
48 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
49 import com.hughes.android.util.IntentLauncher;
50 import com.hughes.util.StringUtil;
52 public class DictionaryManagerActivity extends ListActivity {
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;
59 DictionaryApplication application;
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);
72 public void onCreate(Bundle savedInstanceState) {
73 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
75 super.onCreate(savedInstanceState);
76 Log.d(LOG, "onCreate:" + this);
78 application = (DictionaryApplication) getApplication();
81 setContentView(R.layout.list_activity);
83 getListView().setOnItemClickListener(new OnItemClickListener() {
85 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
91 getListView().setClickable(true);
94 registerForContextMenu(getListView());
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) {
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;
117 alert.getWindow().setAttributes(layoutParams);
118 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
123 protected void onStart() {
125 uiHandler = new Handler();
129 protected void onStop() {
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);
141 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
142 startActivity(intent);
147 protected void onResume() {
150 if (PreferenceActivity.prefsMightHaveChanged) {
151 PreferenceActivity.prefsMightHaveChanged = false;
153 startActivity(getIntent());
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) &&
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.
171 application.backgroundUpdateDictionaries(new Runnable() {
174 if (uiHandler == null) {
177 uiHandler.post(new Runnable() {
180 setListAdapter(adapter = new Adapter());
186 setListAdapter(adapter = new Adapter());
189 public boolean onCreateOptionsMenu(final Menu menu) {
190 application.onCreateGlobalOptionsMenu(this, menu);
196 public void onCreateContextMenu(final ContextMenu menu, final View view,
197 final ContextMenuInfo menuInfo) {
198 super.onCreateContextMenu(menu, view, menuInfo);
200 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
201 final int position = adapterContextMenuInfo.position;
202 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
204 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
205 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
206 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
208 public boolean onMenuItemClick(MenuItem item) {
209 application.moveDictionaryToTop(dictionaryInfo);
210 setListAdapter(adapter = new Adapter());
216 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
217 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
219 public boolean onMenuItemClick(MenuItem item) {
220 application.deleteDictionary(dictionaryInfo);
221 setListAdapter(adapter = new Adapter());
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() {
231 public boolean onMenuItemClick(MenuItem item) {
232 final Intent intent = getDownloadIntent(downloadable);
233 startActivity(intent);
234 setListAdapter(adapter = new Adapter());
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);
250 class Adapter extends BaseAdapter {
252 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
255 public int getCount() {
256 return dictionaryInfos.size();
260 public DictionaryInfo getItem(int position) {
261 return dictionaryInfos.get(position);
265 public long getItemId(int position) {
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();
277 result = new LinearLayout(parent.getContext());
280 final DictionaryInfo dictionaryInfo = getItem(position);
281 result.setOrientation(LinearLayout.VERTICAL);
283 final LinearLayout row = new LinearLayout(parent.getContext());
284 row.setOrientation(LinearLayout.HORIZONTAL);
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);
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);
311 final ImageView checkMark = new ImageView(parent.getContext());
312 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
313 row.addView(checkMark);
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(" | ");
327 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
329 final TextView indexView = new TextView(parent.getContext());
330 indexView.setText(builder.toString());
331 row2.addView(indexView);
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() {
343 public void onClick(View v) {
344 DictionaryManagerActivity.this.onClick(position);