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.ImageView;
41 import android.widget.LinearLayout;
42 import android.widget.TextView;
43 import android.widget.ToggleButton;
45 import com.actionbarsherlock.app.SherlockActivity;
46 import com.actionbarsherlock.app.SherlockListActivity;
47 import com.actionbarsherlock.view.Menu;
48 import com.actionbarsherlock.view.MenuInflater;
49 import com.actionbarsherlock.widget.SearchView;
50 import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
51 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
52 import com.hughes.android.dictionary.engine.Language;
53 import com.hughes.android.dictionary.engine.Language.LanguageResources;
54 import com.hughes.android.util.IntentLauncher;
57 import java.util.ArrayList;
58 import java.util.List;
60 public class DictionaryManagerActivity extends SherlockActivity {
62 static final String LOG = "QuickDic";
63 static boolean blockAutoLaunch = false;
65 DictionaryApplication application;
68 // EditText filterText;
69 SearchView filterSearchView;
70 ToggleButton hideDownloadable;
72 LinearLayout dictionariesOnDevice;
73 LinearLayout downloadableDictionaries;
77 public static Intent getLaunchIntent() {
78 final Intent intent = new Intent();
79 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
80 DictionaryManagerActivity.class.getName());
81 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
85 public void onCreate(Bundle savedInstanceState) {
86 setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
88 super.onCreate(savedInstanceState);
89 Log.d(LOG, "onCreate:" + this);
91 application = (DictionaryApplication) getApplication();
94 setContentView(R.layout.dictionary_manager_activity);
96 dictionariesOnDevice = (LinearLayout) findViewById(R.id.dictionariesOnDeviceGoHere);
97 downloadableDictionaries = (LinearLayout) findViewById(R.id.downloadableDictionariesGoHere);
99 // filterText = (EditText) findViewById(R.id.filterText);
101 // filterText.addTextChangedListener(new TextWatcher() {
103 // public void onTextChanged(CharSequence s, int start, int before, int
108 // public void beforeTextChanged(CharSequence s, int start, int count,
113 // public void afterTextChanged(Editable s) {
114 // onFilterTextChanged();
118 // final ImageButton clearSearchText = (ImageButton)
119 // findViewById(R.id.ClearSearchTextButton);
120 // clearSearchText.setOnClickListener(new View.OnClickListener() {
122 // public void onClick(View arg0) {
123 // filterText.setText("");
124 // filterText.requestFocus();
128 hideDownloadable = (ToggleButton) findViewById(R.id.hideDownloadable);
129 hideDownloadable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
131 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
132 onShowLocalChanged();
137 // registerForContextMenu(getListView());
139 blockAutoLaunch = false;
140 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
141 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
142 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(
143 thanksForUpdatingLatestVersion)) {
144 blockAutoLaunch = true;
145 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
146 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion)
152 protected void onStart() {
154 uiHandler = new Handler();
158 protected void onStop() {
164 protected void onResume() {
167 if (PreferenceActivity.prefsMightHaveChanged) {
168 PreferenceActivity.prefsMightHaveChanged = false;
170 startActivity(getIntent());
173 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
174 hideDownloadable.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
176 if (!blockAutoLaunch &&
177 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
178 prefs.contains(C.DICT_FILE) &&
179 prefs.contains(C.INDEX_SHORT_NAME)) {
180 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
181 startActivity(DictionaryActivity.getLaunchIntent(
182 new File(prefs.getString(C.DICT_FILE, "")), prefs.getString(C.INDEX_SHORT_NAME, ""),
183 prefs.getString(C.SEARCH_TOKEN, "")));
188 application.backgroundUpdateDictionaries(new Runnable() {
191 if (uiHandler == null) {
194 uiHandler.post(new Runnable() {
197 populateDictionaryLists("");
203 populateDictionaryLists("");
207 public boolean onCreateOptionsMenu(final Menu menu) {
208 MenuInflater inflater = getSupportMenuInflater();
209 inflater.inflate(R.menu.dictionary_manager_options_menu, menu);
211 filterSearchView = (SearchView) menu.findItem(R.id.filterText).getActionView();
212 filterSearchView.setOnQueryTextListener(new OnQueryTextListener() {
214 public boolean onQueryTextSubmit(String query) {
219 public boolean onQueryTextChange(String filterText) {
220 populateDictionaryLists(filterText);
225 application.onCreateGlobalOptionsMenu(this, menu);
230 // public void onCreateContextMenu(final ContextMenu menu, final View
232 // final ContextMenuInfo menuInfo) {
233 // super.onCreateContextMenu(menu, view, menuInfo);
235 // final AdapterContextMenuInfo adapterContextMenuInfo =
236 // (AdapterContextMenuInfo) menuInfo;
237 // final int position = adapterContextMenuInfo.position;
238 // final DictionaryInfo dictionaryInfo = adapter.getItem(position);
240 // if (position > 0 &&
241 // application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename))
243 // final android.view.MenuItem moveToTopMenuItem =
244 // menu.add(R.string.moveToTop);
245 // moveToTopMenuItem.setOnMenuItemClickListener(new
246 // android.view.MenuItem.OnMenuItemClickListener() {
248 // public boolean onMenuItemClick(android.view.MenuItem item) {
249 // application.moveDictionaryToTop(dictionaryInfo);
250 // setListAdapter(adapter = new Adapter());
256 // final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
258 // .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
260 // public boolean onMenuItemClick(android.view.MenuItem item) {
261 // application.deleteDictionary(dictionaryInfo);
262 // setListAdapter(adapter = new Adapter());
267 // final DictionaryInfo downloadable = application
268 // .getDownloadable(dictionaryInfo.uncompressedFilename);
269 // if (downloadable != null) {
270 // final android.view.MenuItem downloadMenuItem = menu.add(getString(
271 // R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
273 // .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
275 // public boolean onMenuItemClick(android.view.MenuItem item) {
276 // final Intent intent = getDownloadIntent(downloadable);
277 // startActivity(intent);
278 // setListAdapter(adapter = new Adapter());
286 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
287 // DownloadManager downloadManager = (DownloadManager)
288 // getSystemService(DOWNLOAD_SERVICE);
289 // DownloadManager.Request request = new
290 // DownloadManager.Request(Uri.parse(""));
291 // long id = downloadManager.enqueue(request);
292 // DownloadManager.Query query;
296 private void onShowLocalChanged() {
297 downloadableDictionaries.setVisibility(hideDownloadable.isChecked() ? View.GONE
299 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
300 prefs.putBoolean(C.SHOW_LOCAL, hideDownloadable.isChecked());
304 // private void onClick(int index) {
305 // final DictionaryInfo dictionaryInfo = adapter.getItem(index);
306 // final DictionaryInfo downloadable =
307 // application.getDownloadable(dictionaryInfo.uncompressedFilename);
309 // (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)
310 // && downloadable != null) {
311 // final Intent intent = getDownloadIntent(downloadable);
312 // startActivity(intent);
314 // final Intent intent =
315 // DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename),
317 // startActivity(intent);
321 private void populateDictionaryLists(String filterText) {
323 dictionariesOnDevice.removeAllViews();
324 final List<DictionaryInfo> dictionaryInfos = application.getDictionariesOnDevice();
325 for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
326 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
327 R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
328 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
329 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
331 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
332 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
333 for (IndexInfo indexInfo : sortedIndexInfos) {
334 final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
335 buttons.addView(button);
338 dictionariesOnDevice.addView(row);
345 // class Adapter extends BaseAdapter {
347 // final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
350 // final String filter = filterSearchView.getText().toString().trim().toLowerCase();
351 // for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
352 // boolean canShow = true;
353 // if (hideDownloadable.isChecked()
354 // && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
357 // if (canShow && filter.length() > 0) {
358 // if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename)
359 // .toLowerCase().contains(filter)) {
364 // dictionaryInfos.add(dictionaryInfo);
370 // public int getCount() {
371 // return dictionaryInfos.size();
375 // public DictionaryInfo getItem(int position) {
376 // return dictionaryInfos.get(position);
380 // public long getItemId(int position) {
385 // public View getView(final int position, View convertView, final ViewGroup parent) {
386 // if (convertView == null) {
387 // convertView = LayoutInflater.from(parent.getContext()).inflate(
388 // R.layout.dictionary_manager_row, parent, false);
391 // final DictionaryInfo dictionaryInfo = getItem(position);
393 // final TextView textView = (TextView) convertView.findViewById(R.id.dictionaryName);
394 // final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
395 // textView.setText(name);
397 // final Button downloadButton = (Button) convertView
398 // .findViewById(R.id.dictionaryDownloadButton);
399 // final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
400 // final DictionaryInfo downloadable = application
401 // .getDownloadable(dictionaryInfo.uncompressedFilename);
402 // if (updateAvailable) {
403 // downloadButton.setCompoundDrawablesWithIntrinsicBounds(
404 // android.R.drawable.ic_menu_add,
406 // downloadButton.setText(getString(R.string.downloadButton,
407 // downloadable.zipBytes / 1024.0 / 1024.0));
408 // } else if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
409 // downloadButton.setCompoundDrawablesWithIntrinsicBounds(
410 // android.R.drawable.ic_menu_add,
412 // downloadButton.setText(getString(R.string.downloadButton,
413 // downloadable.zipBytes / 1024.0 / 1024.0));
415 // downloadButton.setCompoundDrawablesWithIntrinsicBounds(
416 // android.R.drawable.checkbox_on_background,
418 // downloadButton.setText("");
420 // final Intent intent = getDownloadIntent(downloadable);
421 // downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
423 // // Add the information about each index.
424 // final TextView dictionaryDetails = (TextView) convertView
425 // .findViewById(R.id.dictionaryDetails);
426 // final StringBuilder builder = new StringBuilder();
427 // for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
428 // if (builder.length() > 0) {
429 // builder.append(" | ");
431 // builder.append(getString(R.string.indexInfo, indexInfo.shortName,
432 // indexInfo.mainTokenCount));
434 // dictionaryDetails.setText(builder.toString());
436 // // // Because we have a Button inside a ListView row:
438 // // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
439 // // convertView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
440 // convertView.setClickable(true);
441 // convertView.setFocusable(true);
442 // convertView.setLongClickable(true);
443 // // result.setBackgroundResource(android.R.drawable.menuitem_background);
444 // convertView.setOnClickListener(new TextView.OnClickListener() {
446 // public void onClick(View v) {
447 // DictionaryManagerActivity.this.onClick(position);
451 // return convertView;