]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Long-press on lang button shows list.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryManagerActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary;
16
17 import java.io.File;
18 import java.util.List;
19
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.preference.PreferenceManager;
27 import android.util.Log;
28 import android.util.TypedValue;
29 import android.view.ContextMenu;
30 import android.view.ContextMenu.ContextMenuInfo;
31 import android.view.Menu;
32 import android.view.MenuItem;
33 import android.view.MenuItem.OnMenuItemClickListener;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.view.WindowManager;
37 import android.webkit.WebView;
38 import android.widget.AdapterView;
39 import android.widget.AdapterView.AdapterContextMenuInfo;
40 import android.widget.AdapterView.OnItemClickListener;
41 import android.widget.BaseAdapter;
42 import android.widget.Button;
43 import android.widget.LinearLayout;
44 import android.widget.TextView;
45
46 import com.hughes.android.util.IntentLauncher;
47 import com.hughes.util.StringUtil;
48
49 public class DictionaryManagerActivity extends ListActivity {
50
51   static final String LOG = "QuickDic";
52   static boolean canAutoLaunch = true;
53
54   DictionaryApplication application;
55   Adapter adapter;
56   
57   public void onCreate(Bundle savedInstanceState) {
58     super.onCreate(savedInstanceState);
59     Log.d(LOG, "onCreate:" + this);
60     
61     application = (DictionaryApplication) getApplication();
62
63     // UI init.
64     setContentView(R.layout.list_activity);
65
66     getListView().setOnItemClickListener(new OnItemClickListener() {
67       @Override
68       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
69           long id) {
70         onClick(index);
71       }
72     });
73     
74     getListView().setClickable(true);
75
76     // ContextMenu.
77     registerForContextMenu(getListView());
78
79     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
80     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
81     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
82       canAutoLaunch = false;
83       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
84       builder.setCancelable(false);
85       final WebView webView = new WebView(getApplicationContext());
86       webView.loadData(StringUtil.readToString(getResources().openRawResource(R.raw.whats_new)), "text/html", "utf-8");
87       builder.setView(webView);
88       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
89           public void onClick(DialogInterface dialog, int id) {
90                dialog.cancel();
91           }
92       });
93       final AlertDialog alert = builder.create();
94       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
95       layoutParams.copyFrom(alert.getWindow().getAttributes());
96       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
97       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
98       alert.show();
99       alert.getWindow().setAttributes(layoutParams);
100       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
101     }
102     
103     if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
104       canAutoLaunch = false;
105     }
106   }
107   
108   private void onClick(int index) {
109     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
110     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
111     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
112       final Intent intent = DownloadActivity
113           .getLaunchIntent(downloadable.downloadUrl,
114               application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
115               dictionaryInfo.dictInfo);
116       startActivity(intent);
117     } else {
118       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
119       startActivity(intent);
120     }
121   }
122   
123   @Override
124   protected void onResume() {
125     super.onResume();
126     
127     if (PreferenceActivity.prefsMightHaveChanged) {
128       PreferenceActivity.prefsMightHaveChanged = false;
129       finish();
130       startActivity(getIntent());
131     }
132     
133     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
134     if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
135       canAutoLaunch = false;  // Only autolaunch once per-process, on startup.
136       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
137       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
138       // Don't finish, so that user can hit back and get here.
139       //finish();
140       return;
141     }
142
143     setListAdapter(adapter = new Adapter());
144   }
145
146   public boolean onCreateOptionsMenu(final Menu menu) {
147     final MenuItem about = menu.add(getString(R.string.about));
148     about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
149       public boolean onMenuItemClick(final MenuItem menuItem) {
150         final Intent intent = new Intent().setClassName(AboutActivity.class
151             .getPackage().getName(), AboutActivity.class.getCanonicalName());
152         startActivity(intent);
153         return false;
154       }
155     });
156     
157     final MenuItem preferences = menu.add(getString(R.string.preferences));
158     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
159       public boolean onMenuItemClick(final MenuItem menuItem) {
160         PreferenceActivity.prefsMightHaveChanged = true;
161         startActivity(new Intent(DictionaryManagerActivity.this,
162             PreferenceActivity.class));
163         return false;
164       }
165     });
166     
167     return true;
168   }
169   
170
171   @Override
172   public void onCreateContextMenu(final ContextMenu menu, final View view,
173       final ContextMenuInfo menuInfo) {
174     super.onCreateContextMenu(menu, view, menuInfo);
175     
176     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
177     final int position = adapterContextMenuInfo.position;
178     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
179     
180     if (position > 0) {
181       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
182       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
183         @Override
184         public boolean onMenuItemClick(MenuItem item) {
185           application.moveDictionaryToTop(dictionaryInfo);
186           setListAdapter(adapter = new Adapter());
187           return true;
188         }
189       });
190     }
191
192     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
193     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
194       @Override
195       public boolean onMenuItemClick(MenuItem item) {
196         application.deleteDictionary(dictionaryInfo);
197         setListAdapter(adapter = new Adapter());
198         return true;
199       }
200     });
201
202   }
203
204   class Adapter extends BaseAdapter {
205     
206     final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
207
208     @Override
209     public int getCount() {
210       return dictionaryInfos.size();
211     }
212
213     @Override
214     public DictionaryInfo getItem(int position) {
215       return dictionaryInfos.get(position);
216     }
217
218     @Override
219     public long getItemId(int position) {
220       return position;
221     }
222     
223     @Override
224     public View getView(final int position, final View convertView, final ViewGroup parent) {
225       final DictionaryInfo dictionaryInfo = getItem(position);
226       final LinearLayout result = new LinearLayout(parent.getContext());
227       
228       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
229       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename); 
230       if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
231         final Button downloadButton = new Button(parent.getContext());
232         downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton));
233         downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
234             .getLaunchIntent(downloadable.downloadUrl,
235                 application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
236                 dictionaryInfo.dictInfo)) {
237           @Override
238           protected void onGo() {
239             application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
240           }
241         });
242         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
243         layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
244         layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
245         downloadButton.setLayoutParams(layoutParams);
246         result.addView(downloadButton);
247       }
248
249       final TextView textView = new TextView(parent.getContext());
250       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
251       textView.setText(name);
252       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
253       result.addView(textView);
254
255       // Because we have a Button inside a ListView row:
256       // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
257       result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
258       result.setClickable(true);
259       result.setFocusable(true);
260       result.setLongClickable(true);
261       result.setBackgroundResource(android.R.drawable.menuitem_background);
262       result.setOnClickListener(new TextView.OnClickListener() {
263         @Override
264         public void onClick(View v) {
265           DictionaryManagerActivity.this.onClick(position);
266         }
267       });
268
269       return result;
270     }
271   }
272
273   public static Intent getLaunchIntent() {
274     final Intent intent = new Intent();
275     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
276         DictionaryManagerActivity.class.getName());
277     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
278     return intent;
279   }
280
281 }