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 android.app.ListActivity;
18 import android.content.Intent;
19 import android.content.SharedPreferences;
20 import android.content.SharedPreferences.Editor;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.preference.PreferenceManager;
24 import android.text.Editable;
25 import android.text.TextWatcher;
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.widget.AdapterView;
37 import android.widget.AdapterView.AdapterContextMenuInfo;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.BaseAdapter;
40 import android.widget.Button;
41 import android.widget.CheckBox;
42 import android.widget.CompoundButton;
43 import android.widget.CompoundButton.OnCheckedChangeListener;
44 import android.widget.EditText;
45 import android.widget.ImageView;
46 import android.widget.LinearLayout;
47 import android.widget.TextView;
49 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
50 import com.hughes.android.util.IntentLauncher;
53 import java.util.ArrayList;
54 import java.util.List;
56 public class DictionaryManagerActivity extends ListActivity {
58 static final String LOG = "QuickDic";
59 static boolean blockAutoLaunch = false;
61 DictionaryApplication application;
69 public static Intent getLaunchIntent() {
70 final Intent intent = new Intent();
71 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
72 DictionaryManagerActivity.class.getName());
73 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
77 public void onCreate(Bundle savedInstanceState) {
78 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
80 super.onCreate(savedInstanceState);
81 Log.d(LOG, "onCreate:" + this);
83 application = (DictionaryApplication) getApplication();
86 setContentView(R.layout.dictionary_manager_activity);
88 filterText = (EditText) findViewById(R.id.filterText);
89 showLocal = (CheckBox) findViewById(R.id.showLocal);
91 filterText.addTextChangedListener(new TextWatcher() {
93 public void onTextChanged(CharSequence s, int start, int before, int count) {
97 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
101 public void afterTextChanged(Editable s) {
102 onFilterTextChanged();
106 showLocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {
108 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
109 onShowLocalChanged();
113 getListView().setOnItemClickListener(new OnItemClickListener() {
115 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
121 getListView().setClickable(true);
124 registerForContextMenu(getListView());
126 blockAutoLaunch = false;
127 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
128 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
129 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
130 blockAutoLaunch = true;
131 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
132 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
137 protected void onStart() {
139 uiHandler = new Handler();
143 protected void onStop() {
149 protected void onResume() {
152 if (PreferenceActivity.prefsMightHaveChanged) {
153 PreferenceActivity.prefsMightHaveChanged = false;
155 startActivity(getIntent());
158 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
159 showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
161 if (!blockAutoLaunch &&
162 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
163 prefs.contains(C.DICT_FILE) &&
164 prefs.contains(C.INDEX_INDEX)) {
165 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
166 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
171 application.backgroundUpdateDictionaries(new Runnable() {
174 if (uiHandler == null) {
177 uiHandler.post(new Runnable() {
180 setListAdapter(adapter = new Adapter());
186 setListAdapter(adapter = new Adapter());
189 public boolean onCreateOptionsMenu(final Menu menu) {
190 application.onCreateGlobalOptionsMenu(this, menu);
196 public void onCreateContextMenu(final ContextMenu menu, final View view,
197 final ContextMenuInfo menuInfo) {
198 super.onCreateContextMenu(menu, view, menuInfo);
200 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
201 final int position = adapterContextMenuInfo.position;
202 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
204 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
205 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
206 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
208 public boolean onMenuItemClick(MenuItem item) {
209 application.moveDictionaryToTop(dictionaryInfo);
210 setListAdapter(adapter = new Adapter());
216 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
217 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
219 public boolean onMenuItemClick(MenuItem item) {
220 application.deleteDictionary(dictionaryInfo);
221 setListAdapter(adapter = new Adapter());
226 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
227 if (downloadable != null) {
228 final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
229 downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
231 public boolean onMenuItemClick(MenuItem item) {
232 final Intent intent = getDownloadIntent(downloadable);
233 startActivity(intent);
234 setListAdapter(adapter = new Adapter());
242 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
243 final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
244 application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
245 downloadable.dictInfo);
249 private void onFilterTextChanged() {
250 setListAdapter(adapter = new Adapter());
254 private void onShowLocalChanged() {
255 setListAdapter(adapter = new Adapter());
256 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
257 prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
261 private void onClick(int index) {
262 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
263 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
264 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
265 final Intent intent = getDownloadIntent(downloadable);
266 startActivity(intent);
268 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
269 startActivity(intent);
273 class Adapter extends BaseAdapter {
275 final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
278 final String filter = filterText.getText().toString().trim().toLowerCase();
279 for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
280 boolean canShow = true;
281 if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
284 if (canShow && filter.length() > 0) {
285 if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
290 dictionaryInfos.add(dictionaryInfo);
297 public int getCount() {
298 return dictionaryInfos.size();
302 public DictionaryInfo getItem(int position) {
303 return dictionaryInfos.get(position);
307 public long getItemId(int position) {
312 public View getView(final int position, final View convertView, final ViewGroup parent) {
313 final LinearLayout result;
314 // Android 4.0.3 leaks memory like crazy if we don't do this.
315 if (convertView instanceof LinearLayout) {
316 result = (LinearLayout) convertView;
317 result.removeAllViews();
319 result = new LinearLayout(parent.getContext());
322 final DictionaryInfo dictionaryInfo = getItem(position);
323 result.setOrientation(LinearLayout.VERTICAL);
325 final LinearLayout row = new LinearLayout(parent.getContext());
326 row.setOrientation(LinearLayout.HORIZONTAL);
330 final TextView textView = new TextView(parent.getContext());
331 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
332 textView.setText(name);
333 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
334 row.addView(textView);
335 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
336 layoutParams.weight = 1.0f;
337 textView.setLayoutParams(layoutParams);
340 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
341 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
342 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
343 final Button downloadButton = new Button(parent.getContext());
344 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
345 final Intent intent = getDownloadIntent(downloadable);
346 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
347 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
348 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
349 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
350 downloadButton.setLayoutParams(layoutParams);
351 row.addView(downloadButton);
353 final ImageView checkMark = new ImageView(parent.getContext());
354 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
355 row.addView(checkMark);
358 // Add the information about each index.
359 final LinearLayout row2 = new LinearLayout(parent.getContext());
360 row2.setOrientation(LinearLayout.HORIZONTAL);
361 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
362 row2.setLayoutParams(layoutParams);
363 result.addView(row2);
364 final StringBuilder builder = new StringBuilder();
365 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
366 if (builder.length() > 0) {
367 builder.append(" | ");
369 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
371 final TextView indexView = new TextView(parent.getContext());
372 indexView.setText(builder.toString());
373 row2.addView(indexView);
376 // Because we have a Button inside a ListView row:
377 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
378 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
379 result.setClickable(true);
380 result.setFocusable(true);
381 result.setLongClickable(true);
382 result.setBackgroundResource(android.R.drawable.menuitem_background);
383 result.setOnClickListener(new TextView.OnClickListener() {
385 public void onClick(View v) {
386 DictionaryManagerActivity.this.onClick(position);