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.List;
20 import android.app.AlertDialog;
21 import android.app.ListActivity;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.preference.PreferenceManager;
28 import android.util.Log;
29 import android.util.TypedValue;
30 import android.view.ContextMenu;
31 import android.view.ContextMenu.ContextMenuInfo;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.MenuItem.OnMenuItemClickListener;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.view.WindowManager;
38 import android.webkit.WebView;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.AdapterContextMenuInfo;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.BaseAdapter;
43 import android.widget.Button;
44 import android.widget.ImageView;
45 import android.widget.LinearLayout;
46 import android.widget.TextView;
48 import com.hughes.android.dictionary.C.Theme;
49 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
50 import com.hughes.android.util.IntentLauncher;
51 import com.hughes.util.StringUtil;
53 public class DictionaryManagerActivity extends ListActivity {
55 static final String LOG = "QuickDic";
56 static boolean canAutoLaunch = true;
58 DictionaryApplication application;
63 public static Intent getLaunchIntent() {
64 final Intent intent = new Intent();
65 intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
66 DictionaryManagerActivity.class.getName());
67 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
71 public void onCreate(Bundle savedInstanceState) {
72 setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
74 super.onCreate(savedInstanceState);
75 Log.d(LOG, "onCreate:" + this);
77 application = (DictionaryApplication) getApplication();
80 setContentView(R.layout.list_activity);
82 getListView().setOnItemClickListener(new OnItemClickListener() {
84 public void onItemClick(AdapterView<?> arg0, View arg1, int index,
90 getListView().setClickable(true);
93 registerForContextMenu(getListView());
95 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
96 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
97 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
98 canAutoLaunch = false;
99 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
100 builder.setCancelable(false);
101 final WebView webView = new WebView(getApplicationContext());
102 webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
103 builder.setView(webView);
104 builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
105 public void onClick(DialogInterface dialog, int id) {
109 final AlertDialog alert = builder.create();
110 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
111 layoutParams.copyFrom(alert.getWindow().getAttributes());
112 layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
113 layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
115 alert.getWindow().setAttributes(layoutParams);
116 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
119 if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
120 canAutoLaunch = false;
125 protected void onStart() {
127 uiHandler = new Handler();
131 protected void onStop() {
136 private void onClick(int index) {
137 final DictionaryInfo dictionaryInfo = adapter.getItem(index);
138 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
139 if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
140 final Intent intent = getDownloadIntent(downloadable);
141 startActivity(intent);
143 final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
144 startActivity(intent);
149 protected void onResume() {
152 if (PreferenceActivity.prefsMightHaveChanged) {
153 PreferenceActivity.prefsMightHaveChanged = false;
155 startActivity(getIntent());
158 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
159 if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
160 canAutoLaunch = false; // Only autolaunch once per-process, on startup.
161 Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
162 startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
163 // Don't finish, so that user can hit back and get here.
168 application.backgroundUpdateDictionaries(new Runnable() {
171 if (uiHandler == null) {
174 uiHandler.post(new Runnable() {
177 setListAdapter(adapter = new Adapter());
183 setListAdapter(adapter = new Adapter());
186 public boolean onCreateOptionsMenu(final Menu menu) {
187 application.onCreateGlobalOptionsMenu(this, menu);
193 public void onCreateContextMenu(final ContextMenu menu, final View view,
194 final ContextMenuInfo menuInfo) {
195 super.onCreateContextMenu(menu, view, menuInfo);
197 final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
198 final int position = adapterContextMenuInfo.position;
199 final DictionaryInfo dictionaryInfo = adapter.getItem(position);
201 if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
202 final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
203 moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
205 public boolean onMenuItemClick(MenuItem item) {
206 application.moveDictionaryToTop(dictionaryInfo);
207 setListAdapter(adapter = new Adapter());
213 final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
214 deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
216 public boolean onMenuItemClick(MenuItem item) {
217 application.deleteDictionary(dictionaryInfo);
218 setListAdapter(adapter = new Adapter());
223 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
224 if (downloadable != null) {
225 final MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
226 downloadMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
228 public boolean onMenuItemClick(MenuItem item) {
229 final Intent intent = getDownloadIntent(downloadable);
230 startActivity(intent);
231 setListAdapter(adapter = new Adapter());
240 private Intent getDownloadIntent(final DictionaryInfo downloadable) {
241 final Intent intent = DownloadActivity.getLaunchIntent(downloadable.downloadUrl,
242 application.getPath(downloadable.uncompressedFilename).getPath() + ".zip",
243 downloadable.dictInfo);
247 class Adapter extends BaseAdapter {
249 final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
252 public int getCount() {
253 return dictionaryInfos.size();
257 public DictionaryInfo getItem(int position) {
258 return dictionaryInfos.get(position);
262 public long getItemId(int position) {
267 public View getView(final int position, final View convertView, final ViewGroup parent) {
268 final LinearLayout result;
269 // Android 4.0.3 leaks memory like crazy if we don't do this.
270 if (convertView instanceof LinearLayout) {
271 result = (LinearLayout) convertView;
272 result.removeAllViews();
274 result = new LinearLayout(parent.getContext());
277 final DictionaryInfo dictionaryInfo = getItem(position);
278 result.setOrientation(LinearLayout.VERTICAL);
280 final LinearLayout row = new LinearLayout(parent.getContext());
281 row.setOrientation(LinearLayout.HORIZONTAL);
285 final TextView textView = new TextView(parent.getContext());
286 final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
287 textView.setText(name);
288 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
289 row.addView(textView);
290 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
291 layoutParams.weight = 1.0f;
292 textView.setLayoutParams(layoutParams);
295 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
296 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
297 if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
298 final Button downloadButton = new Button(parent.getContext());
299 downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
300 final Intent intent = getDownloadIntent(downloadable);
301 downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
302 WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
303 layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
304 layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
305 downloadButton.setLayoutParams(layoutParams);
306 row.addView(downloadButton);
308 final ImageView checkMark = new ImageView(parent.getContext());
309 checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
310 row.addView(checkMark);
313 // Add the information about each index.
314 final LinearLayout row2 = new LinearLayout(parent.getContext());
315 row2.setOrientation(LinearLayout.HORIZONTAL);
316 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
317 row2.setLayoutParams(layoutParams);
318 result.addView(row2);
319 final StringBuilder builder = new StringBuilder();
320 for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
321 if (builder.length() > 0) {
322 builder.append(" | ");
324 builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
326 final TextView indexView = new TextView(parent.getContext());
327 indexView.setText(builder.toString());
328 row2.addView(indexView);
331 // Because we have a Button inside a ListView row:
332 // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
333 result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
334 result.setClickable(true);
335 result.setFocusable(true);
336 result.setLongClickable(true);
337 result.setBackgroundResource(android.R.drawable.menuitem_background);
338 result.setOnClickListener(new TextView.OnClickListener() {
340 public void onClick(View v) {
341 DictionaryManagerActivity.this.onClick(position);