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;
17 import java.util.List;
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 public class DictionaryManagerActivity extends ListActivity {
46 static final String LOG = "QuickDic";
47 QuickDicConfig quickDicConfig;
49 static boolean canAutoLaunch = true;
51 final DictionaryApplication application = (DictionaryApplication) getApplication();
53 public void onCreate(Bundle savedInstanceState) {
54 //((DictionaryApplication)getApplication()).applyTheme(this);
56 super.onCreate(savedInstanceState);
57 Log.d(LOG, "onCreate:" + this);
60 setContentView(R.layout.list_activity);
62 getListView().setOnItemClickListener(new OnItemClickListener() {
64 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
71 registerForContextMenu(getListView());
73 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
74 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
75 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
76 canAutoLaunch = false;
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();
97 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
98 canAutoLaunch = false;
102 private void onClick(int index) {
103 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
104 final Intent intent = DictionaryActivity.getLaunchIntent(dictionaryInfo.uncompressedFilename, 0, "");
105 startActivity(intent);
109 protected void onResume() {
112 if (PreferenceActivity.prefsMightHaveChanged) {
113 PreferenceActivity.prefsMightHaveChanged = false;
115 startActivity(getIntent());
118 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
119 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
120 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
121 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
122 startActivity(DictionaryActivity.getLaunchIntent(prefs.getString(C.DICT_FILE, ""), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
123 // Don't finish, so that user can hit back and get here.
128 setListAdapter(adapter);
131 public boolean onCreateOptionsMenu(final Menu menu) {
132 final MenuItem about = menu.add(getString(R.string.about));
133 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
134 public boolean onMenuItemClick(final MenuItem menuItem) {
135 final Intent intent = new Intent().setClassName(AboutActivity.class
136 .getPackage().getName(), AboutActivity.class.getCanonicalName());
137 startActivity(intent);
142 final MenuItem preferences = menu.add(getString(R.string.preferences));
143 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
144 public boolean onMenuItemClick(final MenuItem menuItem) {
145 PreferenceActivity.prefsMightHaveChanged = true;
146 startActivity(new Intent(DictionaryManagerActivity.this,
147 PreferenceActivity.class));
157 public void onCreateContextMenu(final ContextMenu menu, final View view,
158 final ContextMenuInfo menuInfo) {
159 super.onCreateContextMenu(menu, view, menuInfo);
161 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
162 final int position = adapterContextMenuInfo.position;
163 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
166 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
167 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
169 public boolean onMenuItemClick(MenuItem item) {
170 application.moveDictionaryToTop(dictionaryInfo.uncompressedFilename);
171 setListAdapter(adapter = new Adapter());
177 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
178 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
180 public boolean onMenuItemClick(MenuItem item) {
181 application.deleteDictionary(dictionaryInfo.uncompressedFilename);
182 setListAdapter(adapter = new Adapter());
189 class Adapter extends BaseAdapter {
191 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
194 public int getCount() {
195 return dictionaryInfos.size();
199 public DictionaryInfo getItem(int position) {
200 return dictionaryInfos.get(position);
204 public long getItemId(int position) {
209 public View getView(int position, View convertView, ViewGroup parent) {
210 final DictionaryInfo dictionaryInfo = getItem(position);
211 final TableLayout tableLayout = new TableLayout(parent.getContext());
212 final TextView view = new TextView(parent.getContext());
214 String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
215 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
216 name = getString(R.string.notOnDevice, name);
220 view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
221 tableLayout.addView(view);
226 Adapter adapter = new Adapter();
228 public static Intent getLaunchIntent() {
229 final Intent intent = new Intent();
230 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
231 DictionaryManagerActivity.class.getName());
232 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);