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.Context;
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.Menu;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.ContextMenu.ContextMenuInfo;
35 import android.view.MenuItem.OnMenuItemClickListener;
36 import android.view.WindowManager;
37 import android.webkit.WebView;
38 import android.widget.AdapterView;
39 import android.widget.BaseAdapter;
40 import android.widget.TableLayout;
41 import android.widget.TextView;
42 import android.widget.AdapterView.AdapterContextMenuInfo;
43 import android.widget.AdapterView.OnItemClickListener;
45 import com.hughes.android.util.PersistentObjectCache;
47 public class DictionaryManagerActivity extends ListActivity {
49 static final String LOG = "QuickDic";
51 QuickDicConfig quickDicConfig;
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 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
78 builder.setCancelable(false);
79 final WebView webView = new WebView(getApplicationContext());
80 webView.loadData(getString(R.string.thanksForUpdating), "text/html", "utf-8");
81 builder.setView(webView);
82 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
83 public void onClick(DialogInterface dialog, int id) {
87 final AlertDialog alert = builder.create();
88 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
89 layoutParams.copyFrom(alert.getWindow().getAttributes());
90 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
91 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
93 alert.getWindow().setAttributes(layoutParams);
94 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
98 private void onClick(int dictIndex) {
99 final Intent intent = DictionaryActivity.getIntent(this, dictIndex, 0, "");
100 startActivity(intent);
104 protected void onResume() {
107 if (PreferenceActivity.prefsMightHaveChanged) {
108 PreferenceActivity.prefsMightHaveChanged = false;
110 startActivity(getIntent());
113 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
114 if (prefs.contains(C.DICT_INDEX) && prefs.contains(C.INDEX_INDEX)) {
115 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
116 startActivity(DictionaryActivity.getIntent(this, prefs.getInt(C.DICT_INDEX, 0), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
121 quickDicConfig = PersistentObjectCache.init(this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);
122 if (quickDicConfig == null) {
123 quickDicConfig = new QuickDicConfig(this);
125 quickDicConfig.addDefaultDictionaries(this);
127 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
129 Log.d(LOG, "DictionaryList: " + quickDicConfig.dictionaryInfos);
130 setListAdapter(new Adapter());
133 public boolean onCreateOptionsMenu(final Menu menu) {
134 final MenuItem about = menu.add(getString(R.string.about));
135 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
136 public boolean onMenuItemClick(final MenuItem menuItem) {
137 final Intent intent = new Intent().setClassName(AboutActivity.class
138 .getPackage().getName(), AboutActivity.class.getCanonicalName());
139 startActivity(intent);
144 final MenuItem preferences = menu.add(getString(R.string.preferences));
145 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
146 public boolean onMenuItemClick(final MenuItem menuItem) {
147 PreferenceActivity.prefsMightHaveChanged = true;
148 startActivity(new Intent(DictionaryManagerActivity.this,
149 PreferenceActivity.class));
159 public void onCreateContextMenu(final ContextMenu menu, final View view,
160 final ContextMenuInfo menuInfo) {
161 super.onCreateContextMenu(menu, view, menuInfo);
163 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
165 if (adapterContextMenuInfo.position > 0) {
166 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
167 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
169 public boolean onMenuItemClick(MenuItem item) {
170 final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
171 quickDicConfig.dictionaryInfos.add(0, dictionaryConfig);
172 dictionaryConfigsChanged();
178 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
179 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
181 public boolean onMenuItemClick(MenuItem item) {
182 quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
183 dictionaryConfigsChanged();
190 private void dictionaryConfigsChanged() {
191 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
192 setListAdapter(getListAdapter());
195 class Adapter extends BaseAdapter {
198 public int getCount() {
199 return quickDicConfig.dictionaryInfos.size();
203 public DictionaryInfo getItem(int position) {
204 return quickDicConfig.dictionaryInfos.get(position);
208 public long getItemId(int position) {
213 public View getView(int position, View convertView, ViewGroup parent) {
214 final DictionaryInfo dictionaryConfig = getItem(position);
215 final TableLayout tableLayout = new TableLayout(parent.getContext());
216 final TextView view = new TextView(parent.getContext());
218 String name = dictionaryConfig.name;
219 if (!new File(dictionaryConfig.localFile).canRead()) {
220 name = getString(R.string.notOnDevice, dictionaryConfig.name);
224 view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
225 tableLayout.addView(view);
232 public static Intent getIntent(final Context context) {
233 DictionaryActivity.clearDictionaryPrefs(context);
234 final Intent intent = new Intent();
235 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
236 DictionaryManagerActivity.class.getName());