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.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;
46 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
47 import com.hughes.android.util.IntentLauncher;
48 import com.hughes.util.StringUtil;
50 public class DictionaryManagerActivity extends ListActivity {
52 static final String LOG = "QuickDic";
53 static boolean canAutoLaunch = true;
55 DictionaryApplication application;
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);
66 public void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 Log.d(LOG, "onCreate:" + this);
70 application = (DictionaryApplication) getApplication();
73 setContentView(R.layout.list_activity);
75 getListView().setOnItemClickListener(new OnItemClickListener() {
77 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
83 getListView().setClickable(true);
86 registerForContextMenu(getListView());
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) {
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;
108 alert.getWindow().setAttributes(layoutParams);
109 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
112 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
113 canAutoLaunch = false;
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);
127 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
128 startActivity(intent);
133 protected void onResume() {
136 if (PreferenceActivity.prefsMightHaveChanged) {
137 PreferenceActivity.prefsMightHaveChanged = false;
139 startActivity(getIntent());
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.
152 setListAdapter(adapter = new Adapter());
155 public boolean onCreateOptionsMenu(final Menu menu) {
156 application.onCreateGlobalOptionsMenu(this, menu);
162 public void onCreateContextMenu(final ContextMenu menu, final View view,
163 final ContextMenuInfo menuInfo) {
164 super.onCreateContextMenu(menu, view, menuInfo);
166 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
167 final int position = adapterContextMenuInfo.position;
168 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
171 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
172 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
174 public boolean onMenuItemClick(MenuItem item) {
175 application.moveDictionaryToTop(dictionaryInfo);
176 setListAdapter(adapter = new Adapter());
182 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
183 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
185 public boolean onMenuItemClick(MenuItem item) {
186 application.deleteDictionary(dictionaryInfo);
187 setListAdapter(adapter = new Adapter());
194 class Adapter extends BaseAdapter {
196 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
199 public int getCount() {
200 return dictionaryInfos.size();
204 public DictionaryInfo getItem(int position) {
205 return dictionaryInfos.get(position);
209 public long getItemId(int position) {
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());
218 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
219 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
220 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
221 final Button downloadButton = new Button(parent.getContext());
222 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
223 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
224 .getLaunchIntent(downloadable.downloadUrl,
225 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
226 dictionaryInfo.dictInfo)) {
228 protected void onGo() {
229 application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
232 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
233 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
234 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
235 downloadButton.setLayoutParams(layoutParams);
236 result.addView(downloadButton);
239 final TextView textView = new TextView(parent.getContext());
240 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
241 textView.setText(name);
242 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
243 result.addView(textView);
245 // Add the information about each index.
246 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
247 final TextView indexView = new TextView(parent.getContext());
248 indexView.setText(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
249 result.addView(indexView);
252 // Because we have a Button inside a ListView row:
253 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
254 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
255 result.setClickable(true);
256 result.setFocusable(true);
257 result.setLongClickable(true);
258 result.setBackgroundResource(android.R.drawable.menuitem_background);
259 result.setOnClickListener(new TextView.OnClickListener() {
261 public void onClick(View v) {
262 DictionaryManagerActivity.this.onClick(position);