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.ImageView;
44 import android.widget.LinearLayout;
45 import android.widget.TextView;
47 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
48 import com.hughes.android.util.IntentLauncher;
49 import com.hughes.util.StringUtil;
51 public class DictionaryManagerActivity extends ListActivity {
53 static final String LOG = "QuickDic";
54 static boolean canAutoLaunch = true;
56 DictionaryApplication application;
59 public static Intent getLaunchIntent() {
60 final Intent intent = new Intent();
61 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
62 DictionaryManagerActivity.class.getName());
63 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
67 public void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 Log.d(LOG, "onCreate:" + this);
71 application = (DictionaryApplication) getApplication();
74 setContentView(R.layout.list_activity);
76 getListView().setOnItemClickListener(new OnItemClickListener() {
78 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
84 getListView().setClickable(true);
87 registerForContextMenu(getListView());
89 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
90 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
91 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
92 canAutoLaunch = false;
93 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
94 builder.setCancelable(false);
95 final WebView webView = new WebView(getApplicationContext());
96 webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
97 builder.setView(webView);
98 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
99 public void onClick(DialogInterface dialog, int id) {
103 final AlertDialog alert = builder.create();
104 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
105 layoutParams.copyFrom(alert.getWindow().getAttributes());
106 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
107 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
109 alert.getWindow().setAttributes(layoutParams);
110 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
113 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
114 canAutoLaunch = false;
118 private void onClick(int index) {
119 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
120 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
121 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
122 final Intent intent = DownloadActivity
123 .getLaunchIntent(downloadable.downloadUrl,
124 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
125 dictionaryInfo.dictInfo);
126 startActivity(intent);
128 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
129 startActivity(intent);
134 protected void onResume() {
137 if (PreferenceActivity.prefsMightHaveChanged) {
138 PreferenceActivity.prefsMightHaveChanged = false;
140 startActivity(getIntent());
143 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
144 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
145 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
146 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
147 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
148 // Don't finish, so that user can hit back and get here.
153 setListAdapter(adapter = new Adapter());
156 public boolean onCreateOptionsMenu(final Menu menu) {
157 application.onCreateGlobalOptionsMenu(this, menu);
163 public void onCreateContextMenu(final ContextMenu menu, final View view,
164 final ContextMenuInfo menuInfo) {
165 super.onCreateContextMenu(menu, view, menuInfo);
167 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
168 final int position = adapterContextMenuInfo.position;
169 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
172 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
173 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
175 public boolean onMenuItemClick(MenuItem item) {
176 application.moveDictionaryToTop(dictionaryInfo);
177 setListAdapter(adapter = new Adapter());
183 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
184 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
186 public boolean onMenuItemClick(MenuItem item) {
187 application.deleteDictionary(dictionaryInfo);
188 setListAdapter(adapter = new Adapter());
195 class Adapter extends BaseAdapter {
197 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
200 public int getCount() {
201 return dictionaryInfos.size();
205 public DictionaryInfo getItem(int position) {
206 return dictionaryInfos.get(position);
210 public long getItemId(int position) {
215 public View getView(final int position, final View convertView, final ViewGroup parent) {
216 final DictionaryInfo dictionaryInfo = getItem(position);
217 final LinearLayout result = new LinearLayout(parent.getContext());
218 result.setOrientation(LinearLayout.VERTICAL);
220 final LinearLayout row = new LinearLayout(parent.getContext());
221 row.setOrientation(LinearLayout.HORIZONTAL);
224 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
225 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
226 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
227 final Button downloadButton = new Button(parent.getContext());
228 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
229 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
230 .getLaunchIntent(downloadable.downloadUrl,
231 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
232 dictionaryInfo.dictInfo)) {
234 protected void onGo() {
235 application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
238 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
239 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
240 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
241 downloadButton.setLayoutParams(layoutParams);
242 row.addView(downloadButton);
244 final ImageView checkMark = new ImageView(parent.getContext());
245 checkMark.setImageResource(android.R.drawable.checkbox_on_background);
246 row.addView(checkMark);
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 row.addView(textView);
255 // Add the information about each index.
256 final LinearLayout row2 = new LinearLayout(parent.getContext());
257 row2.setOrientation(LinearLayout.HORIZONTAL);
258 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
259 row2.setLayoutParams(layoutParams);
260 result.addView(row2);
261 final StringBuilder builder = new StringBuilder();
262 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
263 if (builder.length() > 0) {
264 builder.append(" | ");
266 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
268 final TextView indexView = new TextView(parent.getContext());
269 indexView.setText(builder.toString());
270 row2.addView(indexView);
273 // Because we have a Button inside a ListView row:
274 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
275 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
276 result.setClickable(true);
277 result.setFocusable(true);
278 result.setLongClickable(true);
279 result.setBackgroundResource(android.R.drawable.menuitem_background);
280 result.setOnClickListener(new TextView.OnClickListener() {
282 public void onClick(View v) {
283 DictionaryManagerActivity.this.onClick(position);