]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Picking back up in the middle of a major refactoring of the UI, tyring
[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 android.content.Intent;
18 import android.content.SharedPreferences;
19 import android.content.SharedPreferences.Editor;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.preference.PreferenceManager;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.util.Log;
26 import android.view.ContextMenu;
27 import android.view.ContextMenu.ContextMenuInfo;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import android.widget.AdapterView.AdapterContextMenuInfo;
33 import android.widget.AdapterView.OnItemClickListener;
34 import android.widget.BaseAdapter;
35 import android.widget.Button;
36 import android.widget.CompoundButton;
37 import android.widget.CompoundButton.OnCheckedChangeListener;
38 import android.widget.EditText;
39 import android.widget.ImageButton;
40 import android.widget.TextView;
41 import android.widget.ToggleButton;
42
43 import com.actionbarsherlock.app.SherlockListActivity;
44 import com.actionbarsherlock.view.Menu;
45 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
46 import com.hughes.android.util.IntentLauncher;
47
48 import java.io.File;
49 import java.util.ArrayList;
50 import java.util.List;
51
52 public class DictionaryManagerActivity extends SherlockListActivity {
53
54   static final String LOG = "QuickDic";
55   static boolean blockAutoLaunch = false;
56
57   DictionaryApplication application;
58   Adapter adapter;
59   
60   EditText filterText;
61   ToggleButton showLocal;
62   
63   Handler uiHandler;
64   
65   public static Intent getLaunchIntent() {
66     final Intent intent = new Intent();
67     intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
68         DictionaryManagerActivity.class.getName());
69     intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
70     return intent;
71   }
72   
73   public void onCreate(Bundle savedInstanceState) {
74     setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
75
76     super.onCreate(savedInstanceState);
77     Log.d(LOG, "onCreate:" + this);
78     
79     application = (DictionaryApplication) getApplication();
80
81     // UI init.
82     setContentView(R.layout.dictionary_manager_activity);
83     
84     filterText = (EditText) findViewById(R.id.filterText);
85     showLocal = (ToggleButton) findViewById(R.id.showLocal);
86     
87     filterText.addTextChangedListener(new TextWatcher() {
88       @Override
89       public void onTextChanged(CharSequence s, int start, int before, int count) {
90       }
91       
92       @Override
93       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
94       }
95       
96       @Override
97       public void afterTextChanged(Editable s) {
98         onFilterTextChanged();
99       }
100     });
101     
102     final ImageButton clearSearchText = (ImageButton) findViewById(R.id.ClearSearchTextButton);
103     clearSearchText.setOnClickListener(new View.OnClickListener() {
104         @Override
105         public void onClick(View arg0) {
106             filterText.setText("");
107             filterText.requestFocus();
108         }
109     });
110     
111     showLocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {
112       @Override
113       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
114         onShowLocalChanged();
115       }
116     });
117
118     getListView().setOnItemClickListener(new OnItemClickListener() {
119       @Override
120       public void onItemClick(AdapterView<?> arg0, View arg1, int index,
121           long id) {
122         onClick(index);
123       }
124     });
125     
126     getListView().setClickable(true);
127
128     // ContextMenu.
129     registerForContextMenu(getListView());
130
131     blockAutoLaunch = false;
132     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
133     final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
134     if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
135       blockAutoLaunch = true;
136       startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
137       prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
138     }
139   }
140   
141   @Override
142   protected void onStart() {
143     super.onStart();
144     uiHandler = new Handler();
145   }
146   
147   @Override
148   protected void onStop() {
149     super.onStop();
150     uiHandler = null;
151   }
152   
153   @Override
154   protected void onResume() {
155     super.onResume();
156     
157     if (SettingsActivity.settingsMightHaveChanged) {
158       SettingsActivity.settingsMightHaveChanged = false;
159       finish();
160       startActivity(getIntent());
161     }
162     
163     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
164     showLocal.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
165     
166     if (!blockAutoLaunch &&
167         getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
168         prefs.contains(C.DICT_FILE) && 
169         prefs.contains(C.INDEX_INDEX)) {
170       Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
171       startActivity(DictionaryActivity.getLaunchIntent(new File(prefs.getString(C.DICT_FILE, "")), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
172       finish();
173       return;
174     }
175     
176     application.backgroundUpdateDictionaries(new Runnable() {
177       @Override
178       public void run() {
179         if (uiHandler == null) {
180           return;
181         }
182         uiHandler.post(new Runnable() {
183           @Override
184           public void run() {
185             setListAdapter(adapter = new Adapter());
186           }
187         });
188       }
189     });
190
191     setListAdapter(adapter = new Adapter());
192   }
193
194   public boolean onCreateOptionsMenu(final Menu menu) {
195     application.onCreateGlobalOptionsMenu(this, menu);
196     return true;
197   }
198   
199
200   @Override
201   public void onCreateContextMenu(final ContextMenu menu, final View view,
202       final ContextMenuInfo menuInfo) {
203     super.onCreateContextMenu(menu, view, menuInfo);
204     
205     final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
206     final int position = adapterContextMenuInfo.position;
207     final DictionaryInfo dictionaryInfo = adapter.getItem(position);
208     
209     if (position > 0 && application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
210       final android.view.MenuItem moveToTopMenuItem = menu.add(R.string.moveToTop);
211       moveToTopMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
212         @Override
213         public boolean onMenuItemClick(android.view.MenuItem item) {
214           application.moveDictionaryToTop(dictionaryInfo);
215           setListAdapter(adapter = new Adapter());
216           return true;
217         }
218       });
219     }
220
221     final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
222     deleteMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
223       @Override
224       public boolean onMenuItemClick(android.view.MenuItem item) {
225         application.deleteDictionary(dictionaryInfo);
226         setListAdapter(adapter = new Adapter());
227         return true;
228       }
229     });
230
231     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
232     if (downloadable != null) {
233       final android.view.MenuItem downloadMenuItem = menu.add(getString(R.string.downloadButton, downloadable.zipBytes/1024.0/1024.0));
234       downloadMenuItem.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
235         @Override
236         public boolean onMenuItemClick(android.view.MenuItem item) {
237           final Intent intent = getDownloadIntent(downloadable);
238           startActivity(intent);
239           setListAdapter(adapter = new Adapter());
240           return true;
241         }
242       });
243     }
244
245   }
246
247   private Intent getDownloadIntent(final DictionaryInfo downloadable) {
248 //      DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
249 //      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(""));
250 //      long id = downloadManager.enqueue(request);
251 //      DownloadManager.Query query;
252       return null;
253   }
254   
255   private void onFilterTextChanged() {
256     setListAdapter(adapter = new Adapter());
257
258   }
259
260   private void onShowLocalChanged() {
261     setListAdapter(adapter = new Adapter());
262     Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
263     prefs.putBoolean(C.SHOW_LOCAL, showLocal.isChecked());
264     prefs.commit();
265   }
266   
267   private void onClick(int index) {
268     final DictionaryInfo dictionaryInfo = adapter.getItem(index);
269     final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
270     if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) && downloadable != null) {
271       final Intent intent = getDownloadIntent(downloadable);
272       startActivity(intent);
273     } else {
274       final Intent intent = DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), 0, "");
275       startActivity(intent);
276     }
277   }
278   
279   class Adapter extends BaseAdapter {
280     
281     final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
282     
283     Adapter() {
284       final String filter = filterText.getText().toString().trim().toLowerCase();
285       for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
286         boolean canShow = true;
287         if (showLocal.isChecked() && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
288           canShow = false;
289         }
290         if (canShow && filter.length() > 0) {
291           if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
292             canShow = false;
293           }
294         }
295         if (canShow) {
296           dictionaryInfos.add(dictionaryInfo);
297         }
298       }
299     }
300
301     @Override
302     public int getCount() {
303       return dictionaryInfos.size();
304     }
305
306     @Override
307     public DictionaryInfo getItem(int position) {
308       return dictionaryInfos.get(position);
309     }
310
311     @Override
312     public long getItemId(int position) {
313       return position;
314     }
315     
316     @Override
317     public View getView(final int position, View convertView, final ViewGroup parent) {
318        if (convertView == null) {
319            convertView = LayoutInflater.from(parent.getContext()).inflate(
320                    R.layout.dictionary_manager_row, parent, false);
321        }
322         
323       final DictionaryInfo dictionaryInfo = getItem(position);
324
325       final TextView textView = (TextView) convertView.findViewById(R.id.dictionaryName);
326       final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
327       textView.setText(name);
328       
329       final Button downloadButton = (Button) convertView.findViewById(R.id.dictionaryDownloadButton);
330       final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
331       final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
332       if (updateAvailable) {
333           downloadButton.setCompoundDrawablesWithIntrinsicBounds(
334                               android.R.drawable.ic_menu_add, 
335                           0, 0, 0);
336           downloadButton.setText(getString(R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
337       } else if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
338           downloadButton.setCompoundDrawablesWithIntrinsicBounds(
339                   android.R.drawable.ic_menu_add, 
340               0, 0, 0);
341           downloadButton.setText(getString(R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
342       } else {
343           downloadButton.setCompoundDrawablesWithIntrinsicBounds(
344                   android.R.drawable.checkbox_on_background, 
345               0, 0, 0);
346           downloadButton.setText("");
347       }
348       final Intent intent = getDownloadIntent(downloadable);
349       downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
350
351       // Add the information about each index.
352       final TextView dictionaryDetails = (TextView) convertView.findViewById(R.id.dictionaryDetails);
353       final StringBuilder builder = new StringBuilder();
354       for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
355         if (builder.length() > 0) {
356           builder.append(" | ");
357         }
358         builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
359       }
360       dictionaryDetails.setText(builder.toString());
361       
362 //      // Because we have a Button inside a ListView row:
363 //      // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
364       //convertView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
365       convertView.setClickable(true);
366       convertView.setFocusable(true);
367       convertView.setLongClickable(true);
368 //      result.setBackgroundResource(android.R.drawable.menuitem_background);
369       convertView.setOnClickListener(new TextView.OnClickListener() {
370         @Override
371         public void onClick(View v) {
372           DictionaryManagerActivity.this.onClick(position);
373         }
374       });
375       
376       return convertView;
377     }
378   }
379
380 }