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 showDownloadable;
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 showDownloadable = (ToggleButton) findViewById(R.id.hideDownloadable);
129 showDownloadable.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 showDownloadable.setChecked(prefs.getBoolean(C.SHOW_DOWNLOADABLE, 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);
224 filterSearchView.setIconifiedByDefault(false);
226 application.onCreateGlobalOptionsMenu(this, menu);
231 // public void onCreateContextMenu(final ContextMenu menu, final View
233 // final ContextMenuInfo menuInfo) {
234 // super.onCreateContextMenu(menu, view, menuInfo);
236 // final AdapterContextMenuInfo adapterContextMenuInfo =
237 // (AdapterContextMenuInfo) menuInfo;
238 // final int position = adapterContextMenuInfo.position;
239 // final DictionaryInfo dictionaryInfo = adapter.getItem(position);
241 // if (position > 0 &&
242 // application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename))
244 // final android.view.MenuItem moveToTopMenuItem =
245 // menu.add(R.string.moveToTop);
246 // moveToTopMenuItem.setOnMenuItemClickListener(new
247 // android.view.MenuItem.OnMenuItemClickListener() {
249 // public boolean onMenuItemClick(android.view.MenuItem item) {
250 // application.moveDictionaryToTop(dictionaryInfo);
251 // setListAdapter(adapter = new Adapter());
257 // final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
259 // .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
261 // public boolean onMenuItemClick(android.view.MenuItem item) {
262 // application.deleteDictionary(dictionaryInfo);
263 // setListAdapter(adapter = new Adapter());
268 // final DictionaryInfo downloadable = application
269 // .getDownloadable(dictionaryInfo.uncompressedFilename);
270 // if (downloadable != null) {
271 // final android.view.MenuItem downloadMenuItem = menu.add(getString(
272 // R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
274 // .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
276 // public boolean onMenuItemClick(android.view.MenuItem item) {
277 // final Intent intent = getDownloadIntent(downloadable);
278 // startActivity(intent);
279 // setListAdapter(adapter = new Adapter());
287 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
288 // DownloadManager downloadManager = (DownloadManager)
289 // getSystemService(DOWNLOAD_SERVICE);
290 // DownloadManager.Request request = new
291 // DownloadManager.Request(Uri.parse(""));
292 // long id = downloadManager.enqueue(request);
293 // DownloadManager.Query query;
297 private void onShowLocalChanged() {
298 // downloadableDictionaries.setVisibility(showDownloadable.isChecked() ? View.GONE
300 if (filterSearchView != null) {
301 populateDictionaryLists(filterSearchView.getQuery().toString());
303 populateDictionaryLists("");
305 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
306 prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
310 // private void onClick(int index) {
311 // final DictionaryInfo dictionaryInfo = adapter.getItem(index);
312 // final DictionaryInfo downloadable =
313 // application.getDownloadable(dictionaryInfo.uncompressedFilename);
315 // (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)
316 // && downloadable != null) {
317 // final Intent intent = getDownloadIntent(downloadable);
318 // startActivity(intent);
320 // final Intent intent =
321 // DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename),
323 // startActivity(intent);
327 private void populateDictionaryLists(String filterText) {
329 dictionariesOnDevice.removeAllViews();
331 final List<DictionaryInfo> dictionaryInfos = application.getDictionariesOnDevice();
332 for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
333 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
334 R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
335 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
336 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
338 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
339 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
340 for (IndexInfo indexInfo : sortedIndexInfos) {
341 final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
342 button.setOnClickListener(
343 new IntentLauncher(buttons.getContext(),
344 DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), indexInfo.shortName, "")));
345 buttons.addView(button);
348 dictionariesOnDevice.addView(row);
353 downloadableDictionaries.removeAllViews();
354 if (showDownloadable.isChecked()) {
355 final List<DictionaryInfo> dictionaryInfos = application.getDownloadableDictionaries();
356 for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
357 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
358 R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
359 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
360 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
362 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
363 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
364 for (IndexInfo indexInfo : sortedIndexInfos) {
365 final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
366 buttons.addView(button);
368 downloadableDictionaries.addView(row);