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;
19 import android.app.AlertDialog;
20 import android.app.ListActivity;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.os.Bundle;
25 import android.preference.PreferenceManager;
26 import android.util.Log;
27 import android.util.TypedValue;
28 import android.view.ContextMenu;
29 import android.view.ContextMenu.ContextMenuInfo;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.view.MenuItem.OnMenuItemClickListener;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.WindowManager;
36 import android.webkit.WebView;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.AdapterContextMenuInfo;
39 import android.widget.AdapterView.OnItemClickListener;
40 import android.widget.BaseAdapter;
41 import android.widget.TableLayout;
42 import android.widget.TextView;
44 import com.hughes.android.util.PersistentObjectCache;
46 public class DictionaryManagerActivity extends ListActivity {
48 static final String LOG = "QuickDic";
49 QuickDicConfig quickDicConfig;
51 static boolean canAutoLaunch = true;
54 public void onCreate(Bundle savedInstanceState) {
55 //((DictionaryApplication)getApplication()).applyTheme(this);
57 super.onCreate(savedInstanceState);
58 Log.d(LOG, "onCreate:" + this);
61 setContentView(R.layout.list_activity);
63 getListView().setOnItemClickListener(new OnItemClickListener() {
65 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
72 registerForContextMenu(getListView());
74 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
75 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
76 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
77 canAutoLaunch = false;
78 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
79 builder.setCancelable(false);
80 final WebView webView = new WebView(getApplicationContext());
81 webView.loadData(getString(R.string.thanksForUpdating), "text/html", "utf-8");
82 builder.setView(webView);
83 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
84 public void onClick(DialogInterface dialog, int id) {
88 final AlertDialog alert = builder.create();
89 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
90 layoutParams.copyFrom(alert.getWindow().getAttributes());
91 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
92 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
94 alert.getWindow().setAttributes(layoutParams);
95 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
98 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
99 canAutoLaunch = false;
103 private void onClick(int dictIndex) {
104 final DictionaryInfo dictionaryInfo = quickDicConfig.dictionaryInfos.get(dictIndex);
105 final Intent intent = DictionaryActivity.getLaunchIntent(dictionaryInfo.localFile, 0, "");
106 startActivity(intent);
110 protected void onResume() {
113 if (PreferenceActivity.prefsMightHaveChanged) {
114 PreferenceActivity.prefsMightHaveChanged = false;
116 startActivity(getIntent());
119 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
120 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
121 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
122 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
123 startActivity(DictionaryActivity.getLaunchIntent(prefs.getString(C.DICT_FILE, ""), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
124 // Don't finish, so that user can hit back and get here.
129 quickDicConfig = PersistentObjectCache.init(this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);
130 if (quickDicConfig == null) {
131 quickDicConfig = new QuickDicConfig(this);
133 quickDicConfig.addDefaultDictionaries(this);
135 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
137 Log.d(LOG, "DictionaryList: " + quickDicConfig.dictionaryInfos);
138 setListAdapter(new Adapter());
141 public boolean onCreateOptionsMenu(final Menu menu) {
142 final MenuItem about = menu.add(getString(R.string.about));
143 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
144 public boolean onMenuItemClick(final MenuItem menuItem) {
145 final Intent intent = new Intent().setClassName(AboutActivity.class
146 .getPackage().getName(), AboutActivity.class.getCanonicalName());
147 startActivity(intent);
152 final MenuItem preferences = menu.add(getString(R.string.preferences));
153 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
154 public boolean onMenuItemClick(final MenuItem menuItem) {
155 PreferenceActivity.prefsMightHaveChanged = true;
156 startActivity(new Intent(DictionaryManagerActivity.this,
157 PreferenceActivity.class));
167 public void onCreateContextMenu(final ContextMenu menu, final View view,
168 final ContextMenuInfo menuInfo) {
169 super.onCreateContextMenu(menu, view, menuInfo);
171 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
173 if (adapterContextMenuInfo.position > 0) {
174 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
175 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
177 public boolean onMenuItemClick(MenuItem item) {
178 final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
179 quickDicConfig.dictionaryInfos.add(0, dictionaryConfig);
180 dictionaryConfigsChanged();
186 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
187 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
189 public boolean onMenuItemClick(MenuItem item) {
190 quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
191 dictionaryConfigsChanged();
198 private void dictionaryConfigsChanged() {
199 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
200 setListAdapter(getListAdapter());
203 class Adapter extends BaseAdapter {
206 public int getCount() {
207 return quickDicConfig.dictionaryInfos.size();
211 public DictionaryInfo getItem(int position) {
212 return quickDicConfig.dictionaryInfos.get(position);
216 public long getItemId(int position) {
221 public View getView(int position, View convertView, ViewGroup parent) {
222 final DictionaryInfo dictionaryConfig = getItem(position);
223 final TableLayout tableLayout = new TableLayout(parent.getContext());
224 final TextView view = new TextView(parent.getContext());
226 String name = dictionaryConfig.name;
227 if (!new File(dictionaryConfig.localFile).canRead()) {
228 name = getString(R.string.notOnDevice, dictionaryConfig.name);
232 view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
233 tableLayout.addView(view);
240 public static Intent getLaunchIntent() {
241 final Intent intent = new Intent();
242 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
243 DictionaryManagerActivity.class.getName());
244 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);