]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryManagerActivity.java
Got the flags set to a reasonable size.
[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
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(showDownloadable.isChecked() ? View.GONE
298 //                : View.VISIBLE);
299         if (filterSearchView != null) {
300             populateDictionaryLists(filterSearchView.getQuery().toString());
301         } else {
302             populateDictionaryLists("");
303         }
304         Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
305         prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
306         prefs.commit();
307     }
308
309     // private void onClick(int index) {
310     // final DictionaryInfo dictionaryInfo = adapter.getItem(index);
311     // final DictionaryInfo downloadable =
312     // application.getDownloadable(dictionaryInfo.uncompressedFilename);
313     // if
314     // (!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename)
315     // && downloadable != null) {
316     // final Intent intent = getDownloadIntent(downloadable);
317     // startActivity(intent);
318     // } else {
319     // final Intent intent =
320     // DictionaryActivity.getLaunchIntent(application.getPath(dictionaryInfo.uncompressedFilename),
321     // 0, "");
322     // startActivity(intent);
323     // }
324     // }
325
326     private void populateDictionaryLists(String filterText) {
327         // On device.
328         dictionariesOnDevice.removeAllViews();
329         {
330             final List<DictionaryInfo> dictionaryInfos = application.getDictionariesOnDevice();
331             for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
332                 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
333                         R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
334                 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
335                 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
336                 
337                 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
338                 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
339                 for (IndexInfo indexInfo : sortedIndexInfos) {
340                     final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
341                     buttons.addView(button);
342                 }
343                 
344                 dictionariesOnDevice.addView(row);
345             }
346         }
347
348         // Downloadable.
349         downloadableDictionaries.removeAllViews();
350         if (showDownloadable.isChecked()) {
351             final List<DictionaryInfo> dictionaryInfos = application.getDownloadableDictionaries();
352             for (final DictionaryInfo dictionaryInfo : dictionaryInfos) {
353                 View row = LayoutInflater.from(dictionariesOnDevice.getContext()).inflate(
354                         R.layout.dictionary_on_device_row, dictionariesOnDevice, false);
355                 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
356                 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
357                 
358                 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
359                 final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
360                 for (IndexInfo indexInfo : sortedIndexInfos) {
361                     final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
362                     buttons.addView(button);
363                 }
364                 downloadableDictionaries.addView(row);
365             }
366         }
367     }
368
369 }