]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
UI fixes, launch with intent not prefs,
[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
19 import android.app.AlertDialog;
20 import android.app.ListActivity;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.os.Bundle;
25 import android.preference.PreferenceManager;
26 import android.util.Log;
27 import android.util.TypedValue;
28 import android.view.ContextMenu;
29 import android.view.ContextMenu.ContextMenuInfo;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.view.MenuItem.OnMenuItemClickListener;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.WindowManager;
36 import android.webkit.WebView;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.AdapterContextMenuInfo;
39 import android.widget.AdapterView.OnItemClickListener;
40 import android.widget.BaseAdapter;
41 import android.widget.TableLayout;
42 import android.widget.TextView;
43
44 import com.hughes.android.util.PersistentObjectCache;
45
46 public class DictionaryManagerActivity extends ListActivity {
47
48   static final String LOG = "QuickDic";
49   QuickDicConfig quickDicConfig;
50   
51   static boolean canAutoLaunch = true;
52   
53   
54   public void onCreate(Bundle savedInstanceState) {
55     //((DictionaryApplication)getApplication()).applyTheme(this);
56
57     super.onCreate(savedInstanceState);
58     Log.d(LOG, "onCreate:" + this);
59
60     // UI init.
61     setContentView(R.layout.list_activity);
62
63     getListView().setOnItemClickListener(new OnItemClickListener() {
64       @Override
65       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
66           long id) {
67         onClick(index);
68       }
69     });
70
71     // ContextMenu.
72     registerForContextMenu(getListView());
73
74     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
75     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
76     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
77       canAutoLaunch = false;
78       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
79       builder.setCancelable(false);
80       final WebView webView = new WebView(getApplicationContext());
81       webView.loadData(getString(R.string.thanksForUpdating), "text/html", "utf-8");
82       builder.setView(webView);
83       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
84           public void onClick(DialogInterface dialog, int id) {
85                dialog.cancel();
86           }
87       });
88       final AlertDialog alert = builder.create();
89       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
90       layoutParams.copyFrom(alert.getWindow().getAttributes());
91       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
92       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
93       alert.show();
94       alert.getWindow().setAttributes(layoutParams);
95       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
96     }
97     
98     if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
99       canAutoLaunch = false;
100     }
101   }
102   
103   private void onClick(int dictIndex) {
104     final DictionaryInfo dictionaryInfo = quickDicConfig.dictionaryInfos.get(dictIndex);
105     final Intent intent = DictionaryActivity.getLaunchIntent(dictionaryInfo.localFile, 0, "");
106     startActivity(intent);
107   }
108   
109   @Override
110   protected void onResume() {
111     super.onResume();
112     
113     if (PreferenceActivity.prefsMightHaveChanged) {
114       PreferenceActivity.prefsMightHaveChanged = false;
115       finish();
116       startActivity(getIntent());
117     }
118     
119     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
120     if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
121       canAutoLaunch = false;  // Only autolaunch once per-process, on startup.
122       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
123       startActivity(DictionaryActivity.getLaunchIntent(prefs.getString(C.DICT_FILE, ""), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
124       // Don't finish, so that user can hit back and get here.
125       //finish();
126       return;
127     }
128
129     quickDicConfig = PersistentObjectCache.init(this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);
130     if (quickDicConfig == null) {
131       quickDicConfig = new QuickDicConfig(this);
132     } else {
133       quickDicConfig.addDefaultDictionaries(this);
134     }
135     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
136
137     Log.d(LOG, "DictionaryList: " + quickDicConfig.dictionaryInfos);
138     setListAdapter(new Adapter());
139   }
140
141   public boolean onCreateOptionsMenu(final Menu menu) {
142     final MenuItem about = menu.add(getString(R.string.about));
143     about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
144       public boolean onMenuItemClick(final MenuItem menuItem) {
145         final Intent intent = new Intent().setClassName(AboutActivity.class
146             .getPackage().getName(), AboutActivity.class.getCanonicalName());
147         startActivity(intent);
148         return false;
149       }
150     });
151     
152     final MenuItem preferences = menu.add(getString(R.string.preferences));
153     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
154       public boolean onMenuItemClick(final MenuItem menuItem) {
155         PreferenceActivity.prefsMightHaveChanged = true;
156         startActivity(new Intent(DictionaryManagerActivity.this,
157             PreferenceActivity.class));
158         return false;
159       }
160     });
161     
162     return true;
163   }
164   
165
166   @Override
167   public void onCreateContextMenu(final ContextMenu menu, final View view,
168       final ContextMenuInfo menuInfo) {
169     super.onCreateContextMenu(menu, view, menuInfo);
170     
171     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
172     
173     if (adapterContextMenuInfo.position > 0) {
174       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
175       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
176         @Override
177         public boolean onMenuItemClick(MenuItem item) {
178           final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
179           quickDicConfig.dictionaryInfos.add(0, dictionaryConfig);
180           dictionaryConfigsChanged();
181           return true;
182         }
183       });
184     }
185
186     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
187     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
188       @Override
189       public boolean onMenuItemClick(MenuItem item) {
190         quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
191         dictionaryConfigsChanged();
192         return true;
193       }
194     });
195
196   }
197
198   private void dictionaryConfigsChanged() {
199     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
200     setListAdapter(getListAdapter());
201   }
202
203   class Adapter extends BaseAdapter {
204
205     @Override
206     public int getCount() {
207       return quickDicConfig.dictionaryInfos.size();
208     }
209
210     @Override
211     public DictionaryInfo getItem(int position) {
212       return quickDicConfig.dictionaryInfos.get(position);
213     }
214
215     @Override
216     public long getItemId(int position) {
217       return position;
218     }
219     
220     @Override
221     public View getView(int position, View convertView, ViewGroup parent) {
222       final DictionaryInfo dictionaryConfig = getItem(position);
223       final TableLayout tableLayout = new TableLayout(parent.getContext());
224       final TextView view = new TextView(parent.getContext());
225       
226       String name = dictionaryConfig.name;
227       if (!new File(dictionaryConfig.localFile).canRead()) {
228         name = getString(R.string.notOnDevice, dictionaryConfig.name);
229       }
230
231       view.setText(name);
232       view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
233       tableLayout.addView(view);
234
235       return tableLayout;
236     }
237     
238   }
239
240   public static Intent getLaunchIntent() {
241     final Intent intent = new Intent();
242     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
243         DictionaryManagerActivity.class.getName());
244     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
245     return intent;
246   }
247
248 }