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.ArrayList;
19 import java.util.List;
21 import android.app.AlertDialog;
22 import android.app.ListActivity;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.SharedPreferences.Editor;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.preference.PreferenceManager;
30 import android.text.Editable;
31 import android.text.TextWatcher;
32 import android.util.Log;
33 import android.util.TypedValue;
34 import android.view.ContextMenu;
35 import android.view.ContextMenu.ContextMenuInfo;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.MenuItem.OnMenuItemClickListener;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.view.WindowManager;
42 import android.webkit.WebView;
43 import android.widget.AdapterView;
44 import android.widget.AdapterView.AdapterContextMenuInfo;
45 import android.widget.AdapterView.OnItemClickListener;
46 import android.widget.BaseAdapter;
47 import android.widget.Button;
48 import android.widget.CheckBox;
49 import android.widget.CompoundButton;
50 import android.widget.CompoundButton.OnCheckedChangeListener;
51 import android.widget.EditText;
52 import android.widget.ImageView;
53 import android.widget.LinearLayout;
54 import android.widget.TextView;
56 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
57 import com.hughes.android.util.IntentLauncher;
58 import com.hughes.util.StringUtil;
60 public class DictionaryManagerActivity extends ListActivity {
62 static final String LOG = "QuickDic";
63 static boolean blockAutoLaunch = false;
65 DictionaryApplication application;
73 public static Intent getLaunchIntent() {
74 final Intent intent = new Intent();
75 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
76 DictionaryManagerActivity.class.getName());
77 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
81 public void onCreate(Bundle savedInstanceState) {
82 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
84 super.onCreate(savedInstanceState);
85 Log.d(LOG, "onCreate:" + this);
87 application = (DictionaryApplication) getApplication();
90 setContentView(R.layout.dictionary_manager_activity);
92 filterText = (EditText) findViewById(R.id.filterText);
93 showLocal = (CheckBox) findViewById(R.id.showLocal);
95 filterText.addTextChangedListener(new TextWatcher() {
97 public void onTextChanged(CharSequence s, int start, int before, int count) {
101 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
105 public void afterTextChanged(Editable s) {
106 onFilterTextChanged();
110 showLocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {
112 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
113 onShowLocalChanged();
117 getListView().setOnItemClickListener(new OnItemClickListener() {
119 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
125 getListView().setClickable(true);
128 registerForContextMenu(getListView());
130 blockAutoLaunch = false;
131 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
132 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
133 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
134 blockAutoLaunch = true;
135 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
136 builder.setCancelable(false);
137 final WebView webView = new WebView(getApplicationContext());
138 webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
139 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
140 public void onClick(DialogInterface dialog, int id) {
144 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
145 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
146 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
147 webView.setLayoutParams(layoutParams);
148 builder.setView(webView);
149 final AlertDialog alert = builder.create();
150 alert.getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
152 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
157 protected void onStart() {
159 uiHandler = new Handler();
163 protected void onStop() {
169 protected void onResume() {
172 if (PreferenceActivity.prefsMightHaveChanged) {
173 PreferenceActivity.prefsMightHaveChanged = false;
175 startActivity(getIntent());
178 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
179 showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
181 if (!blockAutoLaunch &&
182 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
183 prefs.contains(C.DICT_FILE) &&
184 prefs.contains(C.INDEX_INDEX)) {
185 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
186 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
191 application.backgroundUpdateDictionaries(new Runnable() {
194 if (uiHandler == null) {
197 uiHandler.post(new Runnable() {
200 setListAdapter(adapter = new Adapter());
206 setListAdapter(adapter = new Adapter());
209 public boolean onCreateOptionsMenu(final Menu menu) {
210 application.onCreateGlobalOptionsMenu(this, menu);
216 public void onCreateContextMenu(final ContextMenu menu, final View view,
217 final ContextMenuInfo menuInfo) {
218 super.onCreateContextMenu(menu, view, menuInfo);
220 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
221 final int position = adapterContextMenuInfo.position;
222 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
224 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
225 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
226 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
228 public boolean onMenuItemClick(MenuItem item) {
229 application.moveDictionaryToTop(dictionaryInfo);
230 setListAdapter(adapter = new Adapter());
236 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
237 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
239 public boolean onMenuItemClick(MenuItem item) {
240 application.deleteDictionary(dictionaryInfo);
241 setListAdapter(adapter = new Adapter());
246 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
247 if (downloadable != null) {
248 final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
249 downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
251 public boolean onMenuItemClick(MenuItem item) {
252 final Intent intent = getDownloadIntent(downloadable);
253 startActivity(intent);
254 setListAdapter(adapter = new Adapter());
262 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
263 final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
264 application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
265 downloadable.dictInfo);
269 private void onFilterTextChanged() {
270 setListAdapter(adapter = new Adapter());
274 private void onShowLocalChanged() {
275 setListAdapter(adapter = new Adapter());
276 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
277 prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
281 private void onClick(int index) {
282 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
283 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
284 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
285 final Intent intent = getDownloadIntent(downloadable);
286 startActivity(intent);
288 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
289 startActivity(intent);
293 class Adapter extends BaseAdapter {
295 final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
298 final String filter = filterText.getText().toString().trim().toLowerCase();
299 for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
300 boolean canShow = true;
301 if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
304 if (canShow && filter.length() > 0) {
305 if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
310 dictionaryInfos.add(dictionaryInfo);
317 public int getCount() {
318 return dictionaryInfos.size();
322 public DictionaryInfo getItem(int position) {
323 return dictionaryInfos.get(position);
327 public long getItemId(int position) {
332 public View getView(final int position, final View convertView, final ViewGroup parent) {
333 final LinearLayout result;
334 // Android 4.0.3 leaks memory like crazy if we don't do this.
335 if (convertView instanceof LinearLayout) {
336 result = (LinearLayout) convertView;
337 result.removeAllViews();
339 result = new LinearLayout(parent.getContext());
342 final DictionaryInfo dictionaryInfo = getItem(position);
343 result.setOrientation(LinearLayout.VERTICAL);
345 final LinearLayout row = new LinearLayout(parent.getContext());
346 row.setOrientation(LinearLayout.HORIZONTAL);
350 final TextView textView = new TextView(parent.getContext());
351 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
352 textView.setText(name);
353 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
354 row.addView(textView);
355 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
356 layoutParams.weight = 1.0f;
357 textView.setLayoutParams(layoutParams);
360 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
361 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
362 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
363 final Button downloadButton = new Button(parent.getContext());
364 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
365 final Intent intent = getDownloadIntent(downloadable);
366 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
367 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
368 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
369 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
370 downloadButton.setLayoutParams(layoutParams);
371 row.addView(downloadButton);
373 final ImageView checkMark = new ImageView(parent.getContext());
374 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
375 row.addView(checkMark);
378 // Add the information about each index.
379 final LinearLayout row2 = new LinearLayout(parent.getContext());
380 row2.setOrientation(LinearLayout.HORIZONTAL);
381 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
382 row2.setLayoutParams(layoutParams);
383 result.addView(row2);
384 final StringBuilder builder = new StringBuilder();
385 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
386 if (builder.length() > 0) {
387 builder.append(" | ");
389 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
391 final TextView indexView = new TextView(parent.getContext());
392 indexView.setText(builder.toString());
393 row2.addView(indexView);
396 // Because we have a Button inside a ListView row:
397 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
398 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
399 result.setClickable(true);
400 result.setFocusable(true);
401 result.setLongClickable(true);
402 result.setBackgroundResource(android.R.drawable.menuitem_background);
403 result.setOnClickListener(new TextView.OnClickListener() {
405 public void onClick(View v) {
406 DictionaryManagerActivity.this.onClick(position);