]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Using icons in QuickSwitch.
[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 showDownloadable;
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         showDownloadable = (ToggleButton) findViewById(R.id.hideDownloadable);
129         showDownloadable.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         showDownloadable.setChecked(prefs.getBoolean(C.SHOW_DOWNLOADABLE, 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         filterSearchView.setIconifiedByDefault(false);
225
226         application.onCreateGlobalOptionsMenu(this, menu);
227         return true;
228     }
229
230         // @Override
231         // public void onCreateContextMenu(final ContextMenu menu, final View
232         // view,
233         // final ContextMenuInfo menuInfo) {
234         // super.onCreateContextMenu(menu, view, menuInfo);
235         //
236         // final AdapterContextMenuInfo adapterContextMenuInfo =
237         // (AdapterContextMenuInfo) menuInfo;
238         // final int position = adapterContextMenuInfo.position;
239         // final DictionaryInfo dictionaryInfo = adapter.getItem(position);
240         //
241         // if (position > 0 &&
242         // application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename))
243         // {
244         // final android.view.MenuItem moveToTopMenuItem =
245         // menu.add(R.string.moveToTop);
246         // moveToTopMenuItem.setOnMenuItemClickListener(new
247         // android.view.MenuItem.OnMenuItemClickListener() {
248         // @Override
249         // public boolean onMenuItemClick(android.view.MenuItem item) {
250         // application.moveDictionaryToTop(dictionaryInfo);
251         // setListAdapter(adapter = new Adapter());
252         // return true;
253         // }
254         // });
255         // }
256 //
257 //        final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
258 //        deleteMenuItem
259 //                .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
260 //                    @Override
261 //                    public boolean onMenuItemClick(android.view.MenuItem item) {
262 //                        application.deleteDictionary(dictionaryInfo);
263 //                        setListAdapter(adapter = new Adapter());
264 //                        return true;
265 //                    }
266 //                });
267 //
268 //        final DictionaryInfo downloadable = application
269 //                .getDownloadable(dictionaryInfo.uncompressedFilename);
270 //        if (downloadable != null) {
271 //            final android.view.MenuItem downloadMenuItem = menu.add(getString(
272 //                    R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
273 //            downloadMenuItem
274 //                    .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
275 //                        @Override
276 //                        public boolean onMenuItemClick(android.view.MenuItem item) {
277 //                            final Intent intent = getDownloadIntent(downloadable);
278 //                            startActivity(intent);
279 //                            setListAdapter(adapter = new Adapter());
280 //                            return true;
281 //                        }
282 //                    });
283 //        }
284 //
285 //    }
286
287     private Intent getDownloadIntent(final DictionaryInfo downloadable) {
288         // DownloadManager downloadManager = (DownloadManager)
289         // getSystemService(DOWNLOAD_SERVICE);
290         // DownloadManager.Request request = new
291         // DownloadManager.Request(Uri.parse(""));
292         // long id = downloadManager.enqueue(request);
293         // DownloadManager.Query query;
294         return null;
295     }
296
297     private void onShowLocalChanged() {
298 //        downloadableDictionaries.setVisibility(showDownloadable.isChecked() ? View.GONE
299 //                : View.VISIBLE);
300         if (filterSearchView != null) {
301             populateDictionaryLists(filterSearchView.getQuery().toString());
302         } else {
303             populateDictionaryLists("");
304         }
305         Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
306         prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
307         prefs.commit();
308     }
309
310     // private void onClick(int index) {
311     // final DictionaryInfo dictionaryInfo = adapter.getItem(index);
312     // final DictionaryInfo downloadable =
313     // application.getDownloadable(dictionaryInfo.uncompressedFilename);
314     // if
315     // (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)
316     // && downloadable != null) {
317     // final Intent intent = getDownloadIntent(downloadable);
318     // startActivity(intent);
319     // } else {
320     // final Intent intent =
321     // DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename),
322     // 0, "");
323     // startActivity(intent);
324     // }
325     // }
326
327     private void populateDictionaryLists(String filterText) {
328         // On device.
329         dictionariesOnDevice.removeAllViews();
330         {
331             final List<DictionaryInfo> dictionaryInfos = application.getDictionariesOnDevice();
332             for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
333                 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
334                         R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
335                 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
336                 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
337                 
338                 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
339                 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
340                 for (IndexInfo indexInfo : sortedIndexInfos) {
341                     final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
342                     button.setOnClickListener(
343                             new IntentLauncher(buttons.getContext(), 
344                             DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename), indexInfo.shortName, "")));
345                     buttons.addView(button);
346                 }
347                 
348                 dictionariesOnDevice.addView(row);
349             }
350         }
351
352         // Downloadable.
353         downloadableDictionaries.removeAllViews();
354         if (showDownloadable.isChecked()) {
355             final List<DictionaryInfo> dictionaryInfos = application.getDownloadableDictionaries();
356             for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
357                 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
358                         R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
359                 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
360                 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
361                 
362                 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
363                 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
364                 for (IndexInfo indexInfo : sortedIndexInfos) {
365                     final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
366                     buttons.addView(button);
367                 }
368                 downloadableDictionaries.addView(row);
369             }
370         }
371     }
372
373 }