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.content.Intent;
18 import android.content.SharedPreferences;
19 import android.content.SharedPreferences.Editor;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.preference.PreferenceManager;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.util.Log;
26 import android.view.ContextMenu;
27 import android.view.ContextMenu.ContextMenuInfo;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import android.widget.AdapterView.AdapterContextMenuInfo;
33 import android.widget.AdapterView.OnItemClickListener;
34 import android.widget.BaseAdapter;
35 import android.widget.Button;
36 import android.widget.CompoundButton;
37 import android.widget.CompoundButton.OnCheckedChangeListener;
38 import android.widget.EditText;
39 import android.widget.ImageButton;
40 import android.widget.TextView;
41 import android.widget.ToggleButton;
43 import com.actionbarsherlock.app.SherlockListActivity;
44 import com.actionbarsherlock.view.Menu;
45 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
46 import com.hughes.android.util.IntentLauncher;
49 import java.util.ArrayList;
50 import java.util.List;
52 public class DictionaryManagerActivity extends SherlockListActivity {
54 static final String LOG = "QuickDic";
55 static boolean blockAutoLaunch = false;
57 DictionaryApplication application;
61 ToggleButton showLocal;
65 public static Intent getLaunchIntent() {
66 final Intent intent = new Intent();
67 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
68 DictionaryManagerActivity.class.getName());
69 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
73 public void onCreate(Bundle savedInstanceState) {
74 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
76 super.onCreate(savedInstanceState);
77 Log.d(LOG, "onCreate:" + this);
79 application = (DictionaryApplication) getApplication();
82 setContentView(R.layout.dictionary_manager_activity);
84 filterText = (EditText) findViewById(R.id.filterText);
85 showLocal = (ToggleButton) findViewById(R.id.showLocal);
87 filterText.addTextChangedListener(new TextWatcher() {
89 public void onTextChanged(CharSequence s, int start, int before, int count) {
93 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
97 public void afterTextChanged(Editable s) {
98 onFilterTextChanged();
102 final ImageButton clearSearchText = (ImageButton) findViewById(R.id.ClearSearchTextButton);
103 clearSearchText.setOnClickListener(new View.OnClickListener() {
105 public void onClick(View arg0) {
106 filterText.setText("");
107 filterText.requestFocus();
111 showLocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {
113 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
114 onShowLocalChanged();
118 getListView().setOnItemClickListener(new OnItemClickListener() {
120 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
126 getListView().setClickable(true);
129 registerForContextMenu(getListView());
131 blockAutoLaunch = false;
132 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
133 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
134 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
135 blockAutoLaunch = true;
136 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
137 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
142 protected void onStart() {
144 uiHandler = new Handler();
148 protected void onStop() {
154 protected void onResume() {
157 if (PreferenceActivity.prefsMightHaveChanged) {
158 PreferenceActivity.prefsMightHaveChanged = false;
160 startActivity(getIntent());
163 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
164 showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
166 if (!blockAutoLaunch &&
167 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
168 prefs.contains(C.DICT_FILE) &&
169 prefs.contains(C.INDEX_INDEX)) {
170 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
171 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
176 application.backgroundUpdateDictionaries(new Runnable() {
179 if (uiHandler == null) {
182 uiHandler.post(new Runnable() {
185 setListAdapter(adapter = new Adapter());
191 setListAdapter(adapter = new Adapter());
194 public boolean onCreateOptionsMenu(final Menu menu) {
195 application.onCreateGlobalOptionsMenu(this, menu);
201 public void onCreateContextMenu(final ContextMenu menu, final View view,
202 final ContextMenuInfo menuInfo) {
203 super.onCreateContextMenu(menu, view, menuInfo);
205 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
206 final int position = adapterContextMenuInfo.position;
207 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
209 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
210 final android.view.MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
211 moveToTopMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
213 public boolean onMenuItemClick(android.view.MenuItem item) {
214 application.moveDictionaryToTop(dictionaryInfo);
215 setListAdapter(adapter = new Adapter());
221 final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
222 deleteMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
224 public boolean onMenuItemClick(android.view.MenuItem item) {
225 application.deleteDictionary(dictionaryInfo);
226 setListAdapter(adapter = new Adapter());
231 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
232 if (downloadable != null) {
233 final android.view.MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
234 downloadMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
236 public boolean onMenuItemClick(android.view.MenuItem item) {
237 final Intent intent = getDownloadIntent(downloadable);
238 startActivity(intent);
239 setListAdapter(adapter = new Adapter());
247 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
248 // DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
249 // DownloadManager.Request request = new DownloadManager.Request(Uri.parse(""));
250 // long id = downloadManager.enqueue(request);
251 // DownloadManager.Query query;
255 private void onFilterTextChanged() {
256 setListAdapter(adapter = new Adapter());
260 private void onShowLocalChanged() {
261 setListAdapter(adapter = new Adapter());
262 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
263 prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
267 private void onClick(int index) {
268 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
269 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
270 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
271 final Intent intent = getDownloadIntent(downloadable);
272 startActivity(intent);
274 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
275 startActivity(intent);
279 class Adapter extends BaseAdapter {
281 final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
284 final String filter = filterText.getText().toString().trim().toLowerCase();
285 for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
286 boolean canShow = true;
287 if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
290 if (canShow && filter.length() > 0) {
291 if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
296 dictionaryInfos.add(dictionaryInfo);
302 public int getCount() {
303 return dictionaryInfos.size();
307 public DictionaryInfo getItem(int position) {
308 return dictionaryInfos.get(position);
312 public long getItemId(int position) {
317 public View getView(final int position, View convertView, final ViewGroup parent) {
318 if (convertView == null) {
319 convertView = LayoutInflater.from(parent.getContext()).inflate(
320 R.layout.dictionary_manager_row, parent, false);
323 final DictionaryInfo dictionaryInfo = getItem(position);
325 final TextView textView = (TextView) convertView.findViewById(R.id.dictionaryName);
326 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
327 textView.setText(name);
329 final Button downloadButton = (Button) convertView.findViewById(R.id.dictionaryDownloadButton);
330 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
331 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
332 if (updateAvailable) {
333 downloadButton.setCompoundDrawablesWithIntrinsicBounds(
334 android.R.drawable.ic_menu_add,
336 downloadButton.setText(getString(R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
337 } else if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
338 downloadButton.setCompoundDrawablesWithIntrinsicBounds(
339 android.R.drawable.ic_menu_add,
341 downloadButton.setText(getString(R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
343 downloadButton.setCompoundDrawablesWithIntrinsicBounds(
344 android.R.drawable.checkbox_on_background,
346 downloadButton.setText("");
348 final Intent intent = getDownloadIntent(downloadable);
349 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
351 // Add the information about each index.
352 final TextView dictionaryDetails = (TextView) convertView.findViewById(R.id.dictionaryDetails);
353 final StringBuilder builder = new StringBuilder();
354 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
355 if (builder.length() > 0) {
356 builder.append(" | ");
358 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
360 dictionaryDetails.setText(builder.toString());
362 // // Because we have a Button inside a ListView row:
363 // // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
364 //convertView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
365 convertView.setClickable(true);
366 convertView.setFocusable(true);
367 convertView.setLongClickable(true);
368 // result.setBackgroundResource(android.R.drawable.menuitem_background);
369 convertView.setOnClickListener(new TextView.OnClickListener() {
371 public void onClick(View v) {
372 DictionaryManagerActivity.this.onClick(position);