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.util.IntentLauncher;
47 import com.hughes.util.StringUtil;
49 public class DictionaryManagerActivity extends ListActivity {
51 static final String LOG = "QuickDic";
52 static boolean canAutoLaunch = true;
54 DictionaryApplication application;
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59 Log.d(LOG, "onCreate:" + this);
61 application = (DictionaryApplication) getApplication();
64 setContentView(R.layout.list_activity);
66 getListView().setOnItemClickListener(new OnItemClickListener() {
68 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
74 getListView().setClickable(true);
77 registerForContextMenu(getListView());
79 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
80 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
81 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
82 canAutoLaunch = false;
83 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
84 builder.setCancelable(false);
85 final WebView webView = new WebView(getApplicationContext());
86 webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
87 builder.setView(webView);
88 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
89 public void onClick(DialogInterface dialog, int id) {
93 final AlertDialog alert = builder.create();
94 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
95 layoutParams.copyFrom(alert.getWindow().getAttributes());
96 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
97 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
99 alert.getWindow().setAttributes(layoutParams);
100 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
103 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
104 canAutoLaunch = false;
108 private void onClick(int index) {
109 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
110 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
111 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
112 final Intent intent = DownloadActivity
113 .getLaunchIntent(downloadable.downloadUrl,
114 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
115 dictionaryInfo.dictInfo);
116 startActivity(intent);
118 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
119 startActivity(intent);
124 protected void onResume() {
127 if (PreferenceActivity.prefsMightHaveChanged) {
128 PreferenceActivity.prefsMightHaveChanged = false;
130 startActivity(getIntent());
133 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
134 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
135 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
136 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
137 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
138 // Don't finish, so that user can hit back and get here.
143 setListAdapter(adapter = new Adapter());
146 public boolean onCreateOptionsMenu(final Menu menu) {
147 final MenuItem about = menu.add(getString(R.string.about));
148 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
149 public boolean onMenuItemClick(final MenuItem menuItem) {
150 final Intent intent = new Intent().setClassName(AboutActivity.class
151 .getPackage().getName(), AboutActivity.class.getCanonicalName());
152 startActivity(intent);
157 final MenuItem preferences = menu.add(getString(R.string.preferences));
158 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
159 public boolean onMenuItemClick(final MenuItem menuItem) {
160 PreferenceActivity.prefsMightHaveChanged = true;
161 startActivity(new Intent(DictionaryManagerActivity.this,
162 PreferenceActivity.class));
172 public void onCreateContextMenu(final ContextMenu menu, final View view,
173 final ContextMenuInfo menuInfo) {
174 super.onCreateContextMenu(menu, view, menuInfo);
176 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
177 final int position = adapterContextMenuInfo.position;
178 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
181 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
182 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
184 public boolean onMenuItemClick(MenuItem item) {
185 application.moveDictionaryToTop(dictionaryInfo);
186 setListAdapter(adapter = new Adapter());
192 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
193 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
195 public boolean onMenuItemClick(MenuItem item) {
196 application.deleteDictionary(dictionaryInfo);
197 setListAdapter(adapter = new Adapter());
204 class Adapter extends BaseAdapter {
206 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
209 public int getCount() {
210 return dictionaryInfos.size();
214 public DictionaryInfo getItem(int position) {
215 return dictionaryInfos.get(position);
219 public long getItemId(int position) {
224 public View getView(final int position, final View convertView, final ViewGroup parent) {
225 final DictionaryInfo dictionaryInfo = getItem(position);
226 final LinearLayout result = new LinearLayout(parent.getContext());
228 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
229 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
230 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
231 final Button downloadButton = new Button(parent.getContext());
232 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton));
233 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
234 .getLaunchIntent(downloadable.downloadUrl,
235 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
236 dictionaryInfo.dictInfo)) {
238 protected void onGo() {
239 application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
242 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
243 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
244 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
245 downloadButton.setLayoutParams(layoutParams);
246 result.addView(downloadButton);
249 final TextView textView = new TextView(parent.getContext());
250 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
251 textView.setText(name);
252 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
253 result.addView(textView);
255 // Because we have a Button inside a ListView row:
256 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
257 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
258 result.setClickable(true);
259 result.setFocusable(true);
260 result.setLongClickable(true);
261 result.setBackgroundResource(android.R.drawable.menuitem_background);
262 result.setOnClickListener(new TextView.OnClickListener() {
264 public void onClick(View v) {
265 DictionaryManagerActivity.this.onClick(position);
273 public static Intent getLaunchIntent() {
274 final Intent intent = new Intent();
275 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
276 DictionaryManagerActivity.class.getName());
277 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);