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 boolean canAutoLaunch = true;
57 DictionaryApplication application;
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);
70 public void onCreate(Bundle savedInstanceState) {
71 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
73 super.onCreate(savedInstanceState);
74 Log.d(LOG, "onCreate:" + this);
76 application = (DictionaryApplication) getApplication();
79 setContentView(R.layout.list_activity);
81 getListView().setOnItemClickListener(new OnItemClickListener() {
83 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
89 getListView().setClickable(true);
92 registerForContextMenu(getListView());
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) {
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;
114 alert.getWindow().setAttributes(layoutParams);
115 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
118 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
119 canAutoLaunch = false;
124 protected void onStart() {
126 uiHandler = new Handler();
130 protected void onStop() {
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);
142 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
143 startActivity(intent);
148 protected void onResume() {
151 if (PreferenceActivity.prefsMightHaveChanged) {
152 PreferenceActivity.prefsMightHaveChanged = false;
154 startActivity(getIntent());
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.
167 application.backgroundUpdateDictionaries(new Runnable() {
170 if (uiHandler == null) {
173 uiHandler.post(new Runnable() {
176 setListAdapter(adapter = new Adapter());
182 setListAdapter(adapter = new Adapter());
185 public boolean onCreateOptionsMenu(final Menu menu) {
186 application.onCreateGlobalOptionsMenu(this, menu);
192 public void onCreateContextMenu(final ContextMenu menu, final View view,
193 final ContextMenuInfo menuInfo) {
194 super.onCreateContextMenu(menu, view, menuInfo);
196 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
197 final int position = adapterContextMenuInfo.position;
198 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
200 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
201 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
202 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
204 public boolean onMenuItemClick(MenuItem item) {
205 application.moveDictionaryToTop(dictionaryInfo);
206 setListAdapter(adapter = new Adapter());
212 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
213 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
215 public boolean onMenuItemClick(MenuItem item) {
216 application.deleteDictionary(dictionaryInfo);
217 setListAdapter(adapter = new Adapter());
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() {
227 public boolean onMenuItemClick(MenuItem item) {
228 final Intent intent = getDownloadIntent(downloadable);
229 startActivity(intent);
230 setListAdapter(adapter = new Adapter());
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);
246 class Adapter extends BaseAdapter {
248 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
251 public int getCount() {
252 return dictionaryInfos.size();
256 public DictionaryInfo getItem(int position) {
257 return dictionaryInfos.get(position);
261 public long getItemId(int position) {
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();
273 result = new LinearLayout(parent.getContext());
276 final DictionaryInfo dictionaryInfo = getItem(position);
277 result.setOrientation(LinearLayout.VERTICAL);
279 final LinearLayout row = new LinearLayout(parent.getContext());
280 row.setOrientation(LinearLayout.HORIZONTAL);
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);
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);
307 final ImageView checkMark = new ImageView(parent.getContext());
308 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
309 row.addView(checkMark);
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(" | ");
323 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
325 final TextView indexView = new TextView(parent.getContext());
326 indexView.setText(builder.toString());
327 row2.addView(indexView);
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() {
339 public void onClick(View v) {
340 DictionaryManagerActivity.this.onClick(position);