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 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
136 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
141 protected void onStart() {
143 uiHandler = new Handler();
147 protected void onStop() {
153 protected void onResume() {
156 if (PreferenceActivity.prefsMightHaveChanged) {
157 PreferenceActivity.prefsMightHaveChanged = false;
159 startActivity(getIntent());
162 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
163 showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
165 if (!blockAutoLaunch &&
166 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
167 prefs.contains(C.DICT_FILE) &&
168 prefs.contains(C.INDEX_INDEX)) {
169 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
170 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
175 application.backgroundUpdateDictionaries(new Runnable() {
178 if (uiHandler == null) {
181 uiHandler.post(new Runnable() {
184 setListAdapter(adapter = new Adapter());
190 setListAdapter(adapter = new Adapter());
193 public boolean onCreateOptionsMenu(final Menu menu) {
194 application.onCreateGlobalOptionsMenu(this, menu);
200 public void onCreateContextMenu(final ContextMenu menu, final View view,
201 final ContextMenuInfo menuInfo) {
202 super.onCreateContextMenu(menu, view, menuInfo);
204 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
205 final int position = adapterContextMenuInfo.position;
206 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
208 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
209 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
210 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
212 public boolean onMenuItemClick(MenuItem item) {
213 application.moveDictionaryToTop(dictionaryInfo);
214 setListAdapter(adapter = new Adapter());
220 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
221 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
223 public boolean onMenuItemClick(MenuItem item) {
224 application.deleteDictionary(dictionaryInfo);
225 setListAdapter(adapter = new Adapter());
230 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
231 if (downloadable != null) {
232 final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
233 downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
235 public boolean onMenuItemClick(MenuItem item) {
236 final Intent intent = getDownloadIntent(downloadable);
237 startActivity(intent);
238 setListAdapter(adapter = new Adapter());
246 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
247 final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
248 application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
249 downloadable.dictInfo);
253 private void onFilterTextChanged() {
254 setListAdapter(adapter = new Adapter());
258 private void onShowLocalChanged() {
259 setListAdapter(adapter = new Adapter());
260 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
261 prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
265 private void onClick(int index) {
266 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
267 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
268 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
269 final Intent intent = getDownloadIntent(downloadable);
270 startActivity(intent);
272 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
273 startActivity(intent);
277 class Adapter extends BaseAdapter {
279 final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
282 final String filter = filterText.getText().toString().trim().toLowerCase();
283 for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
284 boolean canShow = true;
285 if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
288 if (canShow && filter.length() > 0) {
289 if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
294 dictionaryInfos.add(dictionaryInfo);
301 public int getCount() {
302 return dictionaryInfos.size();
306 public DictionaryInfo getItem(int position) {
307 return dictionaryInfos.get(position);
311 public long getItemId(int position) {
316 public View getView(final int position, final View convertView, final ViewGroup parent) {
317 final LinearLayout result;
318 // Android 4.0.3 leaks memory like crazy if we don't do this.
319 if (convertView instanceof LinearLayout) {
320 result = (LinearLayout) convertView;
321 result.removeAllViews();
323 result = new LinearLayout(parent.getContext());
326 final DictionaryInfo dictionaryInfo = getItem(position);
327 result.setOrientation(LinearLayout.VERTICAL);
329 final LinearLayout row = new LinearLayout(parent.getContext());
330 row.setOrientation(LinearLayout.HORIZONTAL);
334 final TextView textView = new TextView(parent.getContext());
335 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
336 textView.setText(name);
337 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
338 row.addView(textView);
339 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
340 layoutParams.weight = 1.0f;
341 textView.setLayoutParams(layoutParams);
344 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
345 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
346 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
347 final Button downloadButton = new Button(parent.getContext());
348 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
349 final Intent intent = getDownloadIntent(downloadable);
350 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
351 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
352 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
353 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
354 downloadButton.setLayoutParams(layoutParams);
355 row.addView(downloadButton);
357 final ImageView checkMark = new ImageView(parent.getContext());
358 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
359 row.addView(checkMark);
362 // Add the information about each index.
363 final LinearLayout row2 = new LinearLayout(parent.getContext());
364 row2.setOrientation(LinearLayout.HORIZONTAL);
365 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
366 row2.setLayoutParams(layoutParams);
367 result.addView(row2);
368 final StringBuilder builder = new StringBuilder();
369 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
370 if (builder.length() > 0) {
371 builder.append(" | ");
373 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
375 final TextView indexView = new TextView(parent.getContext());
376 indexView.setText(builder.toString());
377 row2.addView(indexView);
380 // Because we have a Button inside a ListView row:
381 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
382 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
383 result.setClickable(true);
384 result.setFocusable(true);
385 result.setLongClickable(true);
386 result.setBackgroundResource(android.R.drawable.menuitem_background);
387 result.setOnClickListener(new TextView.OnClickListener() {
389 public void onClick(View v) {
390 DictionaryManagerActivity.this.onClick(position);