]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
DictionaryManager can launch dictionaries again.
[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.ImageView;
41 import android.widget.LinearLayout;
42 import android.widget.TextView;
43 import android.widget.ToggleButton;
44
45 import com.actionbarsherlock.app.SherlockActivity;
46 import com.actionbarsherlock.app.SherlockListActivity;
47 import com.actionbarsherlock.view.Menu;
48 import com.actionbarsherlock.view.MenuInflater;
49 import com.actionbarsherlock.widget.SearchView;
50 import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
51 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
52 import com.hughes.android.dictionary.engine.Language;
53 import com.hughes.android.dictionary.engine.Language.LanguageResources;
54 import com.hughes.android.util.IntentLauncher;
55
56 import java.io.File;
57 import java.util.ArrayList;
58 import java.util.List;
59
60 public class DictionaryManagerActivity extends SherlockActivity {
61
62     static final String LOG = "QuickDic";
63     static boolean blockAutoLaunch = false;
64
65     DictionaryApplication application;
66 //    Adapter adapter;
67
68     // EditText filterText;
69     SearchView filterSearchView;
70     ToggleButton hideDownloadable;
71
72     LinearLayout dictionariesOnDevice;
73     LinearLayout downloadableDictionaries;
74
75     Handler uiHandler;
76
77     public static Intent getLaunchIntent() {
78         final Intent intent = new Intent();
79         intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
80                 DictionaryManagerActivity.class.getName());
81         intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
82         return intent;
83     }
84
85     public void onCreate(Bundle savedInstanceState) {
86         setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
87
88         super.onCreate(savedInstanceState);
89         Log.d(LOG, "onCreate:" + this);
90
91         application = (DictionaryApplication) getApplication();
92
93         // UI init.
94         setContentView(R.layout.dictionary_manager_activity);
95
96         dictionariesOnDevice = (LinearLayout) findViewById(R.id.dictionariesOnDeviceGoHere);
97         downloadableDictionaries = (LinearLayout) findViewById(R.id.downloadableDictionariesGoHere);
98
99         // filterText = (EditText) findViewById(R.id.filterText);
100         //
101         // filterText.addTextChangedListener(new TextWatcher() {
102         // @Override
103         // public void onTextChanged(CharSequence s, int start, int before, int
104         // count) {
105         // }
106         //
107         // @Override
108         // public void beforeTextChanged(CharSequence s, int start, int count,
109         // int after) {
110         // }
111         //
112         // @Override
113         // public void afterTextChanged(Editable s) {
114         // onFilterTextChanged();
115         // }
116         // });
117
118         // final ImageButton clearSearchText = (ImageButton)
119         // findViewById(R.id.ClearSearchTextButton);
120         // clearSearchText.setOnClickListener(new View.OnClickListener() {
121         // @Override
122         // public void onClick(View arg0) {
123         // filterText.setText("");
124         // filterText.requestFocus();
125         // }
126         // });
127
128         hideDownloadable = (ToggleButton) findViewById(R.id.hideDownloadable);
129         hideDownloadable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
130             @Override
131             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
132                 onShowLocalChanged();
133             }
134         });
135
136         // ContextMenu.
137         // registerForContextMenu(getListView());
138
139         blockAutoLaunch = false;
140         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
141         final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
142         if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(
143                 thanksForUpdatingLatestVersion)) {
144             blockAutoLaunch = true;
145             startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent());
146             prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion)
147                     .commit();
148         }
149     }
150
151     @Override
152     protected void onStart() {
153         super.onStart();
154         uiHandler = new Handler();
155     }
156
157     @Override
158     protected void onStop() {
159         super.onStop();
160         uiHandler = null;
161     }
162
163     @Override
164     protected void onResume() {
165         super.onResume();
166
167         if (PreferenceActivity.prefsMightHaveChanged) {
168             PreferenceActivity.prefsMightHaveChanged = false;
169             finish();
170             startActivity(getIntent());
171         }
172
173         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
174         hideDownloadable.setChecked(prefs.getBoolean(C.SHOW_LOCAL, false));
175
176         if (!blockAutoLaunch &&
177                 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
178                 prefs.contains(C.DICT_FILE) &&
179                 prefs.contains(C.INDEX_SHORT_NAME)) {
180             Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
181             startActivity(DictionaryActivity.getLaunchIntent(
182                     new File(prefs.getString(C.DICT_FILE, "")), prefs.getString(C.INDEX_SHORT_NAME, ""),
183                     prefs.getString(C.SEARCH_TOKEN, "")));
184             finish();
185             return;
186         }
187
188         application.backgroundUpdateDictionaries(new Runnable() {
189             @Override
190             public void run() {
191                 if (uiHandler == null) {
192                     return;
193                 }
194                 uiHandler.post(new Runnable() {
195                     @Override
196                     public void run() {
197                         populateDictionaryLists("");
198                     }
199                 });
200             }
201         });
202
203         populateDictionaryLists("");
204     }
205
206     @Override
207     public boolean onCreateOptionsMenu(final Menu menu) {
208         MenuInflater inflater = getSupportMenuInflater();
209         inflater.inflate(R.menu.dictionary_manager_options_menu, menu);
210         
211         filterSearchView = (SearchView) menu.findItem(R.id.filterText).getActionView();
212         filterSearchView.setOnQueryTextListener(new OnQueryTextListener() {
213             @Override
214             public boolean onQueryTextSubmit(String query) {
215                 return true;
216             }
217             
218             @Override
219             public boolean onQueryTextChange(String filterText) {
220                 populateDictionaryLists(filterText);
221                 return true;
222             }
223         });
224
225         application.onCreateGlobalOptionsMenu(this, menu);
226         return true;
227     }
228
229         // @Override
230         // public void onCreateContextMenu(final ContextMenu menu, final View
231         // view,
232         // final ContextMenuInfo menuInfo) {
233         // super.onCreateContextMenu(menu, view, menuInfo);
234         //
235         // final AdapterContextMenuInfo adapterContextMenuInfo =
236         // (AdapterContextMenuInfo) menuInfo;
237         // final int position = adapterContextMenuInfo.position;
238         // final DictionaryInfo dictionaryInfo = adapter.getItem(position);
239         //
240         // if (position > 0 &&
241         // application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename))
242         // {
243         // final android.view.MenuItem moveToTopMenuItem =
244         // menu.add(R.string.moveToTop);
245         // moveToTopMenuItem.setOnMenuItemClickListener(new
246         // android.view.MenuItem.OnMenuItemClickListener() {
247         // @Override
248         // public boolean onMenuItemClick(android.view.MenuItem item) {
249         // application.moveDictionaryToTop(dictionaryInfo);
250         // setListAdapter(adapter = new Adapter());
251         // return true;
252         // }
253         // });
254         // }
255 //
256 //        final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
257 //        deleteMenuItem
258 //                .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
259 //                    @Override
260 //                    public boolean onMenuItemClick(android.view.MenuItem item) {
261 //                        application.deleteDictionary(dictionaryInfo);
262 //                        setListAdapter(adapter = new Adapter());
263 //                        return true;
264 //                    }
265 //                });
266 //
267 //        final DictionaryInfo downloadable = application
268 //                .getDownloadable(dictionaryInfo.uncompressedFilename);
269 //        if (downloadable != null) {
270 //            final android.view.MenuItem downloadMenuItem = menu.add(getString(
271 //                    R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
272 //            downloadMenuItem
273 //                    .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
274 //                        @Override
275 //                        public boolean onMenuItemClick(android.view.MenuItem item) {
276 //                            final Intent intent = getDownloadIntent(downloadable);
277 //                            startActivity(intent);
278 //                            setListAdapter(adapter = new Adapter());
279 //                            return true;
280 //                        }
281 //                    });
282 //        }
283 //
284 //    }
285
286     private Intent getDownloadIntent(final DictionaryInfo downloadable) {
287         // DownloadManager downloadManager = (DownloadManager)
288         // getSystemService(DOWNLOAD_SERVICE);
289         // DownloadManager.Request request = new
290         // DownloadManager.Request(Uri.parse(""));
291         // long id = downloadManager.enqueue(request);
292         // DownloadManager.Query query;
293         return null;
294     }
295
296     private void onShowLocalChanged() {
297         downloadableDictionaries.setVisibility(hideDownloadable.isChecked() ? View.GONE
298                 : View.VISIBLE);
299         Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
300         prefs.putBoolean(C.SHOW_LOCAL, hideDownloadable.isChecked());
301         prefs.commit();
302     }
303
304     // private void onClick(int index) {
305     // final DictionaryInfo dictionaryInfo = adapter.getItem(index);
306     // final DictionaryInfo downloadable =
307     // application.getDownloadable(dictionaryInfo.uncompressedFilename);
308     // if
309     // (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)
310     // && downloadable != null) {
311     // final Intent intent = getDownloadIntent(downloadable);
312     // startActivity(intent);
313     // } else {
314     // final Intent intent =
315     // DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename),
316     // 0, "");
317     // startActivity(intent);
318     // }
319     // }
320
321     private void populateDictionaryLists(String filterText) {
322         // On device.
323         dictionariesOnDevice.removeAllViews();
324         final List<DictionaryInfo> dictionaryInfos = application.getDictionariesOnDevice();
325         for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
326             View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
327                     R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
328             final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
329             name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
330             
331             LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
332             final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
333             for (IndexInfo indexInfo : sortedIndexInfos) {
334                 final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
335                 buttons.addView(button);
336             }
337             
338             dictionariesOnDevice.addView(row);
339         }
340
341         // Downloadable.
342
343     }
344
345 //    class Adapter extends BaseAdapter {
346 //
347 //        final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();
348 //
349 //        Adapter() {
350 //            final String filter = filterSearchView.getText().toString().trim().toLowerCase();
351 //            for (final DictionaryInfo dictionaryInfo : application.getAllDictionaries()) {
352 //                boolean canShow = true;
353 //                if (hideDownloadable.isChecked()
354 //                        && !application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
355 //                    canShow = false;
356 //                }
357 //                if (canShow && filter.length() > 0) {
358 //                    if (!application.getDictionaryName(dictionaryInfo.uncompressedFilename)
359 //                            .toLowerCase().contains(filter)) {
360 //                        canShow = false;
361 //                    }
362 //                }
363 //                if (canShow) {
364 //                    dictionaryInfos.add(dictionaryInfo);
365 //                }
366 //            }
367 //        }
368 //
369 //        @Override
370 //        public int getCount() {
371 //            return dictionaryInfos.size();
372 //        }
373 //
374 //        @Override
375 //        public DictionaryInfo getItem(int position) {
376 //            return dictionaryInfos.get(position);
377 //        }
378 //
379 //        @Override
380 //        public long getItemId(int position) {
381 //            return position;
382 //        }
383 //
384 //        @Override
385 //        public View getView(final int position, View convertView, final ViewGroup parent) {
386 //            if (convertView == null) {
387 //                convertView = LayoutInflater.from(parent.getContext()).inflate(
388 //                        R.layout.dictionary_manager_row, parent, false);
389 //            }
390 //
391 //            final DictionaryInfo dictionaryInfo = getItem(position);
392 //
393 //            final TextView textView = (TextView) convertView.findViewById(R.id.dictionaryName);
394 //            final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
395 //            textView.setText(name);
396 //
397 //            final Button downloadButton = (Button) convertView
398 //                    .findViewById(R.id.dictionaryDownloadButton);
399 //            final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
400 //            final DictionaryInfo downloadable = application
401 //                    .getDownloadable(dictionaryInfo.uncompressedFilename);
402 //            if (updateAvailable) {
403 //                downloadButton.setCompoundDrawablesWithIntrinsicBounds(
404 //                        android.R.drawable.ic_menu_add,
405 //                        0, 0, 0);
406 //                downloadButton.setText(getString(R.string.downloadButton,
407 //                        downloadable.zipBytes / 1024.0 / 1024.0));
408 //            } else if (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)) {
409 //                downloadButton.setCompoundDrawablesWithIntrinsicBounds(
410 //                        android.R.drawable.ic_menu_add,
411 //                        0, 0, 0);
412 //                downloadButton.setText(getString(R.string.downloadButton,
413 //                        downloadable.zipBytes / 1024.0 / 1024.0));
414 //            } else {
415 //                downloadButton.setCompoundDrawablesWithIntrinsicBounds(
416 //                        android.R.drawable.checkbox_on_background,
417 //                        0, 0, 0);
418 //                downloadButton.setText("");
419 //            }
420 //            final Intent intent = getDownloadIntent(downloadable);
421 //            downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), intent));
422 //
423 //            // Add the information about each index.
424 //            final TextView dictionaryDetails = (TextView) convertView
425 //                    .findViewById(R.id.dictionaryDetails);
426 //            final StringBuilder builder = new StringBuilder();
427 //            for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
428 //                if (builder.length() > 0) {
429 //                    builder.append(" | ");
430 //                }
431 //                builder.append(getString(R.string.indexInfo, indexInfo.shortName,
432 //                        indexInfo.mainTokenCount));
433 //            }
434 //            dictionaryDetails.setText(builder.toString());
435 //
436 //            // // Because we have a Button inside a ListView row:
437 //            // //
438 //            // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
439 //            // convertView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
440 //            convertView.setClickable(true);
441 //            convertView.setFocusable(true);
442 //            convertView.setLongClickable(true);
443 //            // result.setBackgroundResource(android.R.drawable.menuitem_background);
444 //            convertView.setOnClickListener(new TextView.OnClickListener() {
445 //                @Override
446 //                public void onClick(View v) {
447 //                    DictionaryManagerActivity.this.onClick(position);
448 //                }
449 //            });
450 //
451 //            return convertView;
452 //        }
453 //    }
454
455 }