]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Major refactor of down dictionary list is stored by app.
[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.util.List;
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 public class DictionaryManagerActivity extends ListActivity {
45
46   static final String LOG = "QuickDic";
47   QuickDicConfig quickDicConfig;
48   
49   static boolean canAutoLaunch = true;
50   
51   final DictionaryApplication application = (DictionaryApplication) getApplication();
52   
53   public void onCreate(Bundle savedInstanceState) {
54     //((DictionaryApplication)getApplication()).applyTheme(this);
55
56     super.onCreate(savedInstanceState);
57     Log.d(LOG, "onCreate:" + this);
58
59     // UI init.
60     setContentView(R.layout.list_activity);
61
62     getListView().setOnItemClickListener(new OnItemClickListener() {
63       @Override
64       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
65           long id) {
66         onClick(index);
67       }
68     });
69
70     // ContextMenu.
71     registerForContextMenu(getListView());
72
73     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
74     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
75     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
76       canAutoLaunch = false;
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     if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
98       canAutoLaunch = false;
99     }
100   }
101   
102   private void onClick(int index) {
103     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
104     final Intent intent = DictionaryActivity.getLaunchIntent(dictionaryInfo.uncompressedFilename, 0, "");
105     startActivity(intent);
106   }
107   
108   @Override
109   protected void onResume() {
110     super.onResume();
111     
112     if (PreferenceActivity.prefsMightHaveChanged) {
113       PreferenceActivity.prefsMightHaveChanged = false;
114       finish();
115       startActivity(getIntent());
116     }
117     
118     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
119     if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
120       canAutoLaunch = false;  // Only autolaunch once per-process, on startup.
121       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
122       startActivity(DictionaryActivity.getLaunchIntent(prefs.getString(C.DICT_FILE, ""), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
123       // Don't finish, so that user can hit back and get here.
124       //finish();
125       return;
126     }
127
128     setListAdapter(adapter);
129   }
130
131   public boolean onCreateOptionsMenu(final Menu menu) {
132     final MenuItem about = menu.add(getString(R.string.about));
133     about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
134       public boolean onMenuItemClick(final MenuItem menuItem) {
135         final Intent intent = new Intent().setClassName(AboutActivity.class
136             .getPackage().getName(), AboutActivity.class.getCanonicalName());
137         startActivity(intent);
138         return false;
139       }
140     });
141     
142     final MenuItem preferences = menu.add(getString(R.string.preferences));
143     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
144       public boolean onMenuItemClick(final MenuItem menuItem) {
145         PreferenceActivity.prefsMightHaveChanged = true;
146         startActivity(new Intent(DictionaryManagerActivity.this,
147             PreferenceActivity.class));
148         return false;
149       }
150     });
151     
152     return true;
153   }
154   
155
156   @Override
157   public void onCreateContextMenu(final ContextMenu menu, final View view,
158       final ContextMenuInfo menuInfo) {
159     super.onCreateContextMenu(menu, view, menuInfo);
160     
161     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
162     final int position = adapterContextMenuInfo.position;
163     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
164     
165     if (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           application.moveDictionaryToTop(dictionaryInfo.uncompressedFilename);
171           setListAdapter(adapter = new Adapter());
172           return true;
173         }
174       });
175     }
176
177     final MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
178     deleteMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
179       @Override
180       public boolean onMenuItemClick(MenuItem item) {
181         application.deleteDictionary(dictionaryInfo.uncompressedFilename);
182         setListAdapter(adapter = new Adapter());
183         return true;
184       }
185     });
186
187   }
188
189   class Adapter extends BaseAdapter {
190     
191     final List<DictionaryInfo> dictionaryInfos = application.getAllDictionaries();
192
193     @Override
194     public int getCount() {
195       return dictionaryInfos.size();
196     }
197
198     @Override
199     public DictionaryInfo getItem(int position) {
200       return dictionaryInfos.get(position);
201     }
202
203     @Override
204     public long getItemId(int position) {
205       return position;
206     }
207     
208     @Override
209     public View getView(int position, View convertView, ViewGroup parent) {
210       final DictionaryInfo dictionaryInfo = getItem(position);
211       final TableLayout tableLayout = new TableLayout(parent.getContext());
212       final TextView view = new TextView(parent.getContext());
213       
214       String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
215       if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
216         name = getString(R.string.notOnDevice, name);
217       }
218
219       view.setText(name);
220       view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
221       tableLayout.addView(view);
222
223       return tableLayout;
224     }
225   }
226   Adapter adapter = new Adapter();
227
228   public static Intent getLaunchIntent() {
229     final Intent intent = new Intent();
230     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
231         DictionaryManagerActivity.class.getName());
232     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
233     return intent;
234   }
235
236 }