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 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
70 super.onCreate(savedInstanceState);
71 Log.d(LOG, "onCreate:" + this);
73 application = (DictionaryApplication) getApplication();
76 setContentView(R.layout.list_activity);
78 getListView().setOnItemClickListener(new OnItemClickListener() {
80 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
86 getListView().setClickable(true);
89 registerForContextMenu(getListView());
91 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
92 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
93 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
94 canAutoLaunch = false;
95 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
96 builder.setCancelable(false);
97 final WebView webView = new WebView(getApplicationContext());
98 webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
99 builder.setView(webView);
100 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
101 public void onClick(DialogInterface dialog, int id) {
105 final AlertDialog alert = builder.create();
106 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
107 layoutParams.copyFrom(alert.getWindow().getAttributes());
108 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
109 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
111 alert.getWindow().setAttributes(layoutParams);
112 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
115 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
116 canAutoLaunch = false;
120 private void onClick(int index) {
121 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
122 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
123 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
124 final Intent intent = DownloadActivity
125 .getLaunchIntent(downloadable.downloadUrl,
126 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
127 dictionaryInfo.dictInfo);
128 startActivity(intent);
130 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
131 startActivity(intent);
136 protected void onResume() {
139 if (PreferenceActivity.prefsMightHaveChanged) {
140 PreferenceActivity.prefsMightHaveChanged = false;
142 startActivity(getIntent());
145 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
146 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
147 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
148 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
149 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
150 // Don't finish, so that user can hit back and get here.
155 setListAdapter(adapter = new Adapter());
158 public boolean onCreateOptionsMenu(final Menu menu) {
159 application.onCreateGlobalOptionsMenu(this, menu);
165 public void onCreateContextMenu(final ContextMenu menu, final View view,
166 final ContextMenuInfo menuInfo) {
167 super.onCreateContextMenu(menu, view, menuInfo);
169 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
170 final int position = adapterContextMenuInfo.position;
171 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
174 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
175 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
177 public boolean onMenuItemClick(MenuItem item) {
178 application.moveDictionaryToTop(dictionaryInfo);
179 setListAdapter(adapter = new Adapter());
185 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
186 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
188 public boolean onMenuItemClick(MenuItem item) {
189 application.deleteDictionary(dictionaryInfo);
190 setListAdapter(adapter = new Adapter());
197 class Adapter extends BaseAdapter {
199 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
202 public int getCount() {
203 return dictionaryInfos.size();
207 public DictionaryInfo getItem(int position) {
208 return dictionaryInfos.get(position);
212 public long getItemId(int position) {
217 public View getView(final int position, final View convertView, final ViewGroup parent) {
218 final DictionaryInfo dictionaryInfo = getItem(position);
219 final LinearLayout result = new LinearLayout(parent.getContext());
220 result.setOrientation(LinearLayout.VERTICAL);
222 final LinearLayout row = new LinearLayout(parent.getContext());
223 row.setOrientation(LinearLayout.HORIZONTAL);
227 final TextView textView = new TextView(parent.getContext());
228 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
229 textView.setText(name);
230 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
231 row.addView(textView);
232 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
233 layoutParams.weight = 1.0f;
234 textView.setLayoutParams(layoutParams);
237 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
238 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
239 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
240 final Button downloadButton = new Button(parent.getContext());
241 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
242 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
243 .getLaunchIntent(downloadable.downloadUrl,
244 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
245 dictionaryInfo.dictInfo)) {
247 protected void onGo() {
248 application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
251 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
252 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
253 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
254 downloadButton.setLayoutParams(layoutParams);
255 row.addView(downloadButton);
257 final ImageView checkMark = new ImageView(parent.getContext());
258 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
259 row.addView(checkMark);
262 // Add the information about each index.
263 final LinearLayout row2 = new LinearLayout(parent.getContext());
264 row2.setOrientation(LinearLayout.HORIZONTAL);
265 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
266 row2.setLayoutParams(layoutParams);
267 result.addView(row2);
268 final StringBuilder builder = new StringBuilder();
269 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
270 if (builder.length() > 0) {
271 builder.append(" | ");
273 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
275 final TextView indexView = new TextView(parent.getContext());
276 indexView.setText(builder.toString());
277 row2.addView(indexView);
280 // Because we have a Button inside a ListView row:
281 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
282 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
283 result.setClickable(true);
284 result.setFocusable(true);
285 result.setLongClickable(true);
286 result.setBackgroundResource(android.R.drawable.menuitem_background);
287 result.setOnClickListener(new TextView.OnClickListener() {
289 public void onClick(View v) {
290 DictionaryManagerActivity.this.onClick(position);