]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Switching to WebView!
[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.Context;
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.Menu;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.ContextMenu.ContextMenuInfo;
35 import android.view.MenuItem.OnMenuItemClickListener;
36 import android.view.WindowManager;
37 import android.webkit.WebView;
38 import android.widget.AdapterView;
39 import android.widget.BaseAdapter;
40 import android.widget.TableLayout;
41 import android.widget.TextView;
42 import android.widget.AdapterView.AdapterContextMenuInfo;
43 import android.widget.AdapterView.OnItemClickListener;
44
45 import com.hughes.android.util.PersistentObjectCache;
46
47 public class DictionaryManagerActivity extends ListActivity {
48
49   static final String LOG = "QuickDic";
50   
51   QuickDicConfig quickDicConfig;
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       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
78       builder.setCancelable(false);
79       final WebView webView = new WebView(getApplicationContext());
80       webView.loadData(getString(R.string.thanksForUpdating), "text/html", "utf-8");
81       builder.setView(webView);
82       builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
83           public void onClick(DialogInterface dialog, int id) {
84                dialog.cancel();
85           }
86       });
87       final AlertDialog alert = builder.create();
88       WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
89       layoutParams.copyFrom(alert.getWindow().getAttributes());
90       layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
91       layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
92       alert.show();
93       alert.getWindow().setAttributes(layoutParams);
94       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
95     }
96   }
97   
98   private void onClick(int dictIndex) {
99     final Intent intent = DictionaryActivity.getIntent(this, dictIndex, 0, "");
100     startActivity(intent);
101   }
102   
103   @Override
104   protected void onResume() {
105     super.onResume();
106     
107     if (PreferenceActivity.prefsMightHaveChanged) {
108       PreferenceActivity.prefsMightHaveChanged = false;
109       finish();
110       startActivity(getIntent());
111     }
112     
113     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
114     if (prefs.contains(C.DICT_INDEX) && prefs.contains(C.INDEX_INDEX)) {
115       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
116       startActivity(DictionaryActivity.getIntent(this, prefs.getInt(C.DICT_INDEX, 0), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
117       //finish();
118       return;
119     }
120
121     quickDicConfig = PersistentObjectCache.init(this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);
122     if (quickDicConfig == null) {
123       quickDicConfig = new QuickDicConfig(this);
124     } else {
125       quickDicConfig.addDefaultDictionaries(this);
126     }
127     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
128
129     Log.d(LOG, "DictionaryList: " + quickDicConfig.dictionaryInfos);
130     setListAdapter(new Adapter());
131   }
132
133   public boolean onCreateOptionsMenu(final Menu menu) {
134     final MenuItem about = menu.add(getString(R.string.about));
135     about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
136       public boolean onMenuItemClick(final MenuItem menuItem) {
137         final Intent intent = new Intent().setClassName(AboutActivity.class
138             .getPackage().getName(), AboutActivity.class.getCanonicalName());
139         startActivity(intent);
140         return false;
141       }
142     });
143     
144     final MenuItem preferences = menu.add(getString(R.string.preferences));
145     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
146       public boolean onMenuItemClick(final MenuItem menuItem) {
147         PreferenceActivity.prefsMightHaveChanged = true;
148         startActivity(new Intent(DictionaryManagerActivity.this,
149             PreferenceActivity.class));
150         return false;
151       }
152     });
153     
154     return true;
155   }
156   
157
158   @Override
159   public void onCreateContextMenu(final ContextMenu menu, final View view,
160       final ContextMenuInfo menuInfo) {
161     super.onCreateContextMenu(menu, view, menuInfo);
162     
163     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
164     
165     if (adapterContextMenuInfo.position > 0) {
166       final MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
167       moveToTopMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
168         @Override
169         public boolean onMenuItemClick(MenuItem item) {
170           final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
171           quickDicConfig.dictionaryInfos.add(0, dictionaryConfig);
172           dictionaryConfigsChanged();
173           return true;
174         }
175       });
176     }
177
178     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
179     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
180       @Override
181       public boolean onMenuItemClick(MenuItem item) {
182         quickDicConfig.dictionaryInfos.remove(adapterContextMenuInfo.position);
183         dictionaryConfigsChanged();
184         return true;
185       }
186     });
187
188   }
189
190   private void dictionaryConfigsChanged() {
191     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, quickDicConfig);
192     setListAdapter(getListAdapter());
193   }
194
195   class Adapter extends BaseAdapter {
196
197     @Override
198     public int getCount() {
199       return quickDicConfig.dictionaryInfos.size();
200     }
201
202     @Override
203     public DictionaryInfo getItem(int position) {
204       return quickDicConfig.dictionaryInfos.get(position);
205     }
206
207     @Override
208     public long getItemId(int position) {
209       return position;
210     }
211     
212     @Override
213     public View getView(int position, View convertView, ViewGroup parent) {
214       final DictionaryInfo dictionaryConfig = getItem(position);
215       final TableLayout tableLayout = new TableLayout(parent.getContext());
216       final TextView view = new TextView(parent.getContext());
217       
218       String name = dictionaryConfig.name;
219       if (!new File(dictionaryConfig.localFile).canRead()) {
220         name = getString(R.string.notOnDevice, dictionaryConfig.name);
221       }
222
223       view.setText(name);
224       view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
225       tableLayout.addView(view);
226
227       return tableLayout;
228     }
229     
230   }
231
232   public static Intent getIntent(final Context context) {
233     DictionaryActivity.clearDictionaryPrefs(context);
234     final Intent intent = new Intent();
235     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
236         DictionaryManagerActivity.class.getName());
237     return intent;
238   }
239
240 }