]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryActivity.java
go
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
1 package com.hughes.android.dictionary;\r
2 \r
3 import java.io.File;\r
4 import java.io.RandomAccessFile;\r
5 import java.util.concurrent.Executor;\r
6 import java.util.concurrent.Executors;\r
7 import java.util.concurrent.atomic.AtomicBoolean;\r
8 \r
9 import android.app.ListActivity;\r
10 import android.content.Context;\r
11 import android.content.Intent;\r
12 import android.content.SharedPreferences;\r
13 import android.graphics.Typeface;\r
14 import android.os.Bundle;\r
15 import android.os.Handler;\r
16 import android.os.Vibrator;\r
17 import android.preference.PreferenceManager;\r
18 import android.text.Editable;\r
19 import android.text.Spannable;\r
20 import android.text.TextWatcher;\r
21 import android.text.style.StyleSpan;\r
22 import android.util.Log;\r
23 import android.view.View;\r
24 import android.view.ViewGroup;\r
25 import android.view.View.OnClickListener;\r
26 import android.view.inputmethod.InputMethodManager;\r
27 import android.widget.BaseAdapter;\r
28 import android.widget.Button;\r
29 import android.widget.EditText;\r
30 import android.widget.ListAdapter;\r
31 import android.widget.TableLayout;\r
32 import android.widget.TableRow;\r
33 import android.widget.TextView;\r
34 \r
35 import com.hughes.android.dictionary.engine.Dictionary;\r
36 import com.hughes.android.dictionary.engine.Index;\r
37 import com.hughes.android.dictionary.engine.PairEntry;\r
38 import com.hughes.android.dictionary.engine.RowBase;\r
39 import com.hughes.android.dictionary.engine.TokenRow;\r
40 import com.hughes.android.util.PersistentObjectCache;\r
41 \r
42 public class DictionaryActivity extends ListActivity {\r
43 \r
44   static final String LOG = "QuickDic";\r
45   \r
46   static final int VIBRATE_MILLIS = 100;\r
47 \r
48   RandomAccessFile dictRaf = null;\r
49   Dictionary dictionary = null;\r
50   int indexIndex = 0;\r
51   Index index = null;\r
52   \r
53   // package for test.\r
54   final Handler uiHandler = new Handler();\r
55   private final Executor searchExecutor = Executors.newSingleThreadExecutor();\r
56   private SearchOperation currentSearchOperation = null;\r
57 \r
58   EditText searchText;\r
59   Button langButton;\r
60 \r
61   // Never null.\r
62   private File wordList = null;\r
63   private boolean saveOnlyFirstSubentry = false;\r
64 \r
65   // Visible for testing.\r
66   ListAdapter indexAdapter = null;\r
67 \r
68   private Vibrator vibrator = null;\r
69   \r
70   public DictionaryActivity() {\r
71   }\r
72   \r
73   public static Intent getIntent(final int dictIndex, final int indexIndex, final String searchToken) {\r
74     final Intent intent = new Intent();\r
75     intent.setClassName(DictionaryActivity.class.getPackage().getName(), DictionaryActivity.class.getName());\r
76     intent.putExtra(C.DICT_INDEX, dictIndex);\r
77     intent.putExtra(C.INDEX_INDEX, indexIndex);\r
78     intent.putExtra(C.SEARCH_TOKEN, searchToken);\r
79     return intent;\r
80   }\r
81 \r
82   @Override\r
83   public void onCreate(Bundle savedInstanceState) {\r
84     super.onCreate(savedInstanceState);\r
85     \r
86     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
87     \r
88     PersistentObjectCache.init(this);\r
89     QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
90         this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
91     \r
92     final Intent intent = getIntent();\r
93     \r
94     final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);\r
95     try {\r
96       final DictionaryConfig dictionaryConfig = quickDicConfig.dictionaryConfigs.get(dictIndex);\r
97       dictRaf = new RandomAccessFile(dictionaryConfig.localFile, "r");\r
98       dictionary = new Dictionary(dictRaf); \r
99     } catch (Exception e) {\r
100       Log.e(LOG, "Unable to load dictionary.", e);\r
101       DictionaryEditActivity.getIntent(dictIndex);\r
102       finish();\r
103       return;\r
104     }\r
105 \r
106     // Pre-load the collators.\r
107     searchExecutor.execute(new Runnable() {\r
108       public void run() {\r
109         final long startMillis = System.currentTimeMillis();\r
110         for (final Index index : dictionary.indices) {\r
111           index.sortLanguage.getFindCollator();\r
112           final com.ibm.icu.text.Collator c = index.sortLanguage\r
113               .getSortCollator();\r
114           if (c.compare("pre-print", "preppy") >= 0) {\r
115             Log.e(LOG, c.getClass()\r
116                 + " is buggy, lookups may not work properly.");\r
117           }\r
118         }\r
119         Log.d(LOG, "Loading collators took:"\r
120             + (System.currentTimeMillis() - startMillis));\r
121       }\r
122     });\r
123     \r
124     indexIndex = intent.getIntExtra(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
125     index = dictionary.indices.get(indexIndex);\r
126     setListAdapter(new IndexAdapter(index));\r
127     \r
128     setContentView(R.layout.dictionary_activity);\r
129     searchText = (EditText) findViewById(R.id.SearchText);\r
130     langButton = (Button) findViewById(R.id.LangButton);\r
131     \r
132     searchText.addTextChangedListener(new SearchTextWatcher());\r
133     \r
134     \r
135     final Button clearSearchTextButton = (Button) findViewById(R.id.ClearSearchTextButton);\r
136     clearSearchTextButton.setOnClickListener(new OnClickListener() {\r
137       public void onClick(View v) {\r
138         onClearSearchTextButton(clearSearchTextButton);\r
139       }\r
140     });\r
141     clearSearchTextButton.setVisibility(PreferenceManager.getDefaultSharedPreferences(this).getBoolean(\r
142         getString(R.string.showClearSearchTextButtonKey), true) ? View.VISIBLE\r
143         : View.GONE);\r
144     \r
145     final Button langButton = (Button) findViewById(R.id.LangButton);\r
146     langButton.setOnClickListener(new OnClickListener() {\r
147       public void onClick(View v) {\r
148         onLanguageButton();\r
149       }\r
150     });\r
151     \r
152     final Button upButton = (Button) findViewById(R.id.UpButton);\r
153     upButton.setOnClickListener(new OnClickListener() {\r
154       public void onClick(View v) {\r
155         onUpDownButton(true);\r
156       }\r
157     });\r
158     final Button downButton = (Button) findViewById(R.id.DownButton);\r
159     downButton.setOnClickListener(new OnClickListener() {\r
160       public void onClick(View v) {\r
161         onUpDownButton(false);\r
162       }\r
163     });\r
164 \r
165     // ContextMenu.\r
166     registerForContextMenu(getListView());\r
167     \r
168     if (prefs.getBoolean(getString(R.string.vibrateOnFailedSearchKey), true)) {\r
169       vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);\r
170     }\r
171 \r
172     updateLangButton();\r
173 \r
174   }\r
175 \r
176   // --------------------------------------------------------------------------\r
177   // Buttons\r
178   // --------------------------------------------------------------------------\r
179 \r
180   private void onClearSearchTextButton(final Button clearSearchTextButton) {\r
181     clearSearchTextButton.requestFocus();\r
182     searchText.setText("");\r
183     searchText.requestFocus();\r
184     Log.d(LOG, "Trying to show soft keyboard.");\r
185     final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
186     manager.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);\r
187   }\r
188   \r
189   void updateLangButton() {\r
190     langButton.setText(index.shortName.toUpperCase());\r
191   }\r
192 \r
193   void onLanguageButton() {\r
194     if (currentSearchOperation != null) {\r
195       currentSearchOperation.interrupted.set(true);\r
196       currentSearchOperation = null;\r
197     }\r
198     \r
199     indexIndex = (indexIndex + 1) % dictionary.indices.size();\r
200     index = dictionary.indices.get(indexIndex);\r
201     indexAdapter = new IndexAdapter(index);\r
202     Log.d(LOG, "onLanguageButton, newLang=" + index.longName);\r
203     setListAdapter(indexAdapter);\r
204     updateLangButton();\r
205     onSearchTextChange(searchText.getText().toString());\r
206   }\r
207   \r
208   void onUpDownButton(final boolean up) {\r
209     final int firstVisibleRow = getListView().getFirstVisiblePosition();\r
210     final RowBase row = index.rows.get(firstVisibleRow);\r
211     final TokenRow tokenRow = row.getTokenRow(true);\r
212     final int destIndexEntry;\r
213     if (up) {\r
214       if (row != tokenRow) {\r
215         destIndexEntry = tokenRow.referenceIndex;\r
216       } else {\r
217         destIndexEntry = Math.max(tokenRow.referenceIndex - 1, 0);\r
218       }\r
219     } else {\r
220       // Down\r
221       destIndexEntry = Math.min(tokenRow.referenceIndex + 1, index.sortedIndexEntries.size());\r
222     }\r
223     \r
224     Log.d(LOG, "onUpDownButton, destIndexEntry=" + destIndexEntry);\r
225     jumpToRow(index.sortedIndexEntries.get(destIndexEntry).startRow);\r
226   }\r
227 \r
228   // --------------------------------------------------------------------------\r
229   // Menu\r
230   // --------------------------------------------------------------------------\r
231 \r
232   // --------------------------------------------------------------------------\r
233   // SearchOperation\r
234   // --------------------------------------------------------------------------\r
235 \r
236   private void searchFinished(final SearchOperation searchOperation) {\r
237     if (searchOperation != this.currentSearchOperation) {\r
238       return;\r
239     }\r
240     \r
241     final Index.SearchResult searchResult = searchOperation.searchResult;\r
242     Log.d(LOG, "searchFinished, " + searchResult.longestPrefixString + ", success=" + searchResult.success);\r
243 \r
244     jumpToRow(searchResult.longestPrefix.startRow);\r
245     \r
246     if (!searchResult.success) {\r
247       if (vibrator != null) {\r
248         vibrator.vibrate(VIBRATE_MILLIS);\r
249       }\r
250       searchText.setText(searchResult.longestPrefixString);\r
251       searchText.setSelection(searchResult.longestPrefixString.length());\r
252       return;\r
253     }\r
254   }\r
255   \r
256   private final void jumpToRow(final int row) {\r
257     setSelection(row);\r
258     getListView().setSelected(true);\r
259   }\r
260 \r
261   final class SearchOperation implements Runnable {\r
262     \r
263     final AtomicBoolean interrupted = new AtomicBoolean(false);\r
264     final String searchText;\r
265     final Index index;\r
266     \r
267     long searchStartMillis;\r
268 \r
269     Index.SearchResult searchResult;\r
270     \r
271     SearchOperation(final String searchText, final Index index) {\r
272       this.searchText = searchText.trim();\r
273       this.index = index;\r
274     }\r
275 \r
276     @Override\r
277     public void run() {\r
278       searchStartMillis = System.currentTimeMillis();\r
279       searchResult = index.findLongestSubstring(searchText, interrupted);\r
280       Log.d(LOG, "searchText=" + searchText + ", searchDuration="\r
281           + (System.currentTimeMillis() - searchStartMillis) + ", interrupted="\r
282           + interrupted.get());\r
283       if (!interrupted.get()) {\r
284         uiHandler.post(new Runnable() {\r
285           @Override\r
286           public void run() {            \r
287             searchFinished(SearchOperation.this);\r
288           }\r
289         });\r
290       }\r
291     }\r
292   }\r
293 \r
294   \r
295   // --------------------------------------------------------------------------\r
296   // IndexAdapter\r
297   // --------------------------------------------------------------------------\r
298 \r
299   static final class IndexAdapter extends BaseAdapter {\r
300     \r
301     final Index index;\r
302 \r
303     IndexAdapter(final Index index) {\r
304       this.index = index;\r
305     }\r
306 \r
307     @Override\r
308     public int getCount() {\r
309       return index.rows.size();\r
310     }\r
311 \r
312     @Override\r
313     public Object getItem(int position) {\r
314       return index.rows.get(position);\r
315     }\r
316 \r
317     @Override\r
318     public long getItemId(int position) {\r
319       return position;\r
320     }\r
321 \r
322     @Override\r
323     public View getView(int position, View convertView, ViewGroup parent) {\r
324       final RowBase row = index.rows.get(position);\r
325       if (row instanceof PairEntry.Row) {\r
326         return getView((PairEntry.Row) row, parent);\r
327       } else if (row instanceof TokenRow) {\r
328         return getView((TokenRow) row, parent);\r
329       } else {\r
330         throw new IllegalArgumentException("Unsupported Row type: " + row.getClass());\r
331       }\r
332     }\r
333 \r
334     private View getView(PairEntry.Row row, ViewGroup parent) {\r
335       final TableLayout result = new TableLayout(parent.getContext());\r
336       final PairEntry entry = row.getEntry();\r
337       final int rowCount = entry.pairs.length;\r
338       for (int r = 0; r < rowCount; ++r) {\r
339         final TableRow tableRow = new TableRow(result.getContext());\r
340 \r
341         TextView column1 = new TextView(tableRow.getContext());\r
342         TextView column2 = new TextView(tableRow.getContext());\r
343         final TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();\r
344         layoutParams.weight = 0.5f;\r
345 \r
346         if (r > 0) {\r
347           final TextView spacer = new TextView(tableRow.getContext());\r
348           spacer.setText(" • ");\r
349           tableRow.addView(spacer);\r
350         }\r
351         tableRow.addView(column1, layoutParams);\r
352         if (r > 0) {\r
353           final TextView spacer = new TextView(tableRow.getContext());\r
354           spacer.setText(" • ");\r
355           tableRow.addView(spacer);\r
356         }\r
357         tableRow.addView(column2, layoutParams);\r
358 \r
359         column1.setWidth(1);\r
360         column2.setWidth(1);\r
361 \r
362         // TODO: color words by gender\r
363         final String col1Text = index.swapPairEntries ? entry.pairs[r].lang2 : entry.pairs[r].lang1;\r
364         column1.setText(col1Text, TextView.BufferType.SPANNABLE);\r
365         final Spannable col1Spannable = (Spannable) column1.getText();\r
366         \r
367         int startPos = 0;\r
368         final String token = row.getTokenRow(true).getToken();\r
369         while ((startPos = col1Text.indexOf(token, startPos)) != -1) {\r
370           col1Spannable.setSpan(new StyleSpan(Typeface.BOLD), startPos,\r
371               startPos + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
372           startPos += token.length();\r
373         }\r
374 \r
375         final String col2Text = index.swapPairEntries ? entry.pairs[r].lang1 : entry.pairs[r].lang2;\r
376         column2.setText(col2Text, TextView.BufferType.NORMAL);\r
377 \r
378         result.addView(tableRow);\r
379       }\r
380 \r
381       return result;\r
382     }\r
383 \r
384     private View getView(TokenRow row, ViewGroup parent) {\r
385       final TextView textView = new TextView(parent.getContext());\r
386       textView.setText(row.getToken());\r
387       textView.setTextSize(20);\r
388       return textView;\r
389     }\r
390     \r
391   }\r
392 \r
393   // --------------------------------------------------------------------------\r
394   // SearchText\r
395   // --------------------------------------------------------------------------\r
396 \r
397   void onSearchTextChange(final String searchText) {\r
398     Log.d(LOG, "onSearchTextChange: " + searchText);\r
399     if (currentSearchOperation != null) {\r
400       currentSearchOperation.interrupted.set(true);\r
401     }\r
402     currentSearchOperation = new SearchOperation(searchText, index);\r
403     searchExecutor.execute(currentSearchOperation);\r
404   }\r
405   \r
406   private class SearchTextWatcher implements TextWatcher {\r
407     public void afterTextChanged(final Editable searchTextEditable) {\r
408       Log.d(LOG, "Search text changed: " + searchText.getText());\r
409       if (searchText.hasFocus()) {\r
410         // If they were typing to cause the change, update the UI.\r
411         onSearchTextChange(searchText.getText().toString());\r
412       }\r
413     }\r
414 \r
415     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,\r
416         int arg3) {\r
417     }\r
418 \r
419     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {\r
420     }\r
421   }\r
422 \r
423 }\r