]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Avoid inheriting from Application.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 6366812fc9c22368a875d05f94b829aa6a8c7761..008b57de6c1ef6869612267e22177b1e79829a63 100644 (file)
@@ -42,6 +42,7 @@ import android.text.Spannable;
 import android.text.method.LinkMovementMethod;
 import android.text.style.ClickableSpan;
 import android.text.style.StyleSpan;
+import android.util.DisplayMetrics;
 import android.util.Log;
 import android.util.TypedValue;
 import android.view.ContextMenu;
@@ -225,10 +226,12 @@ public class DictionaryActivity extends ActionBarActivity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        application = DictionaryApplication.INSTANCE;
         // This needs to be before super.onCreate, otherwise ActionbarSherlock
         // doesn't makes the background of the actionbar white when you're
         // in the dark theme.
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+        setTheme(application.getSelectedTheme().themeId);
 
         Log.d(LOG, "onCreate:" + this);
         super.onCreate(savedInstanceState);
@@ -240,7 +243,6 @@ public class DictionaryActivity extends ActionBarActivity {
 
         setContentView(R.layout.dictionary_activity);
 
-        application = (DictionaryApplication) getApplication();
         theme = application.getSelectedTheme();
         textColorFg = getResources().getColor(theme.tokenRowFgColor);
 
@@ -432,24 +434,25 @@ public class DictionaryActivity extends ActionBarActivity {
 
         setListAdapter(new IndexAdapter(index));
 
+        // Pre-load the Transliterator (will spawn its own thread)
+        TransliteratorManager.init(new TransliteratorManager.Callback() {
+            @Override
+            public void onTransliteratorReady() {
+                uiHandler.post(new Runnable() {
+                    @Override
+                    public void run() {
+                        onSearchTextChange(searchView.getQuery().toString());
+                    }
+                });
+            }
+        }, DictionaryApplication.threadBackground);
+
         // Pre-load the collators.
         new Thread(new Runnable() {
             public void run() {
-                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
+                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE);
                 final long startMillis = System.currentTimeMillis();
                 try {
-                    TransliteratorManager.init(new TransliteratorManager.Callback() {
-                        @Override
-                        public void onTransliteratorReady() {
-                            uiHandler.post(new Runnable() {
-                                @Override
-                                public void run() {
-                                    onSearchTextChange(searchView.getQuery().toString());
-                                }
-                            });
-                        }
-                    });
-
                     for (final Index index : dictionary.indices) {
                         final String searchToken = index.sortedIndexEntries.get(0).token;
                         final IndexEntry entry = index.findExact(searchToken);
@@ -717,10 +720,9 @@ public class DictionaryActivity extends ActionBarActivity {
     }
 
     void updateLangButton() {
-        final LanguageResources languageResources =
-            DictionaryApplication.isoCodeToResources.get(index.shortName);
-        if (languageResources != null && languageResources.flagId != 0) {
-            languageButton.setImageResource(languageResources.flagId);
+        final int flagId = IsoUtils.INSTANCE.getFlagIdForIsoCode(index.shortName);
+        if (flagId != 0) {
+            languageButton.setImageResource(flagId);
         } else {
             if (indexIndex % 2 == 0) {
                 languageButton.setImageResource(android.R.drawable.ic_media_next);
@@ -805,8 +807,8 @@ public class DictionaryActivity extends ActionBarActivity {
 
                 for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
                     final IndexInfo indexInfo = dictionaryInfo.indexInfos.get(i);
-                    final View button = application.createButton(parent.getContext(),
-                                        dictionaryInfo, indexInfo);
+                    final View button = IsoUtils.INSTANCE.createButton(parent.getContext(),
+                                        dictionaryInfo, indexInfo, application.languageButtonPixels);
                     final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(),
                             getLaunchIntent(getApplicationContext(),
                                             application.getPath(dictionaryInfo.uncompressedFilename),
@@ -1012,9 +1014,22 @@ public class DictionaryActivity extends ActionBarActivity {
 
     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
-        AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
+        final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
         final RowBase row = (RowBase) getListAdapter().getItem(adapterContextMenuInfo.position);
 
+        if (clickOpensContextMenu && (row instanceof HtmlEntry.Row ||
+            (row instanceof TokenRow && ((TokenRow)row).getIndexEntry().htmlEntries.size() > 0))) {
+            final List<HtmlEntry> html = row instanceof TokenRow ? ((TokenRow)row).getIndexEntry().htmlEntries : Collections.singletonList(((HtmlEntry.Row)row).getEntry());
+            final String highlight = row instanceof HtmlEntry.Row ? ((HtmlEntry.Row)row).getTokenRow(true).getToken() : null;
+            final MenuItem open = menu.add("Open");
+            open.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+                public boolean onMenuItemClick(MenuItem item) {
+                    showHtml(html, highlight);
+                    return false;
+                }
+            });
+        }
+
         final android.view.MenuItem addToWordlist = menu.add(getString(R.string.addToWordList,
                 wordList.getName()));
         addToWordlist
@@ -1159,10 +1174,15 @@ public class DictionaryActivity extends ActionBarActivity {
         // searchView.selectAll();
     }
 
-    protected void onListItemClick(ListView l, View v, int row, long id) {
+    protected void onListItemClick(ListView l, View v, int rowIdx, long id) {
         defocusSearchText();
         if (clickOpensContextMenu && dictRaf != null) {
             openContextMenu(v);
+        } else {
+            final RowBase row = (RowBase)getListAdapter().getItem(rowIdx);
+            if (!(row instanceof PairEntry.Row)) {
+                v.performClick();
+            }
         }
     }
 
@@ -1406,6 +1426,16 @@ public class DictionaryActivity extends ActionBarActivity {
     // IndexAdapter
     // --------------------------------------------------------------------------
 
+    private void showHtml(final List<HtmlEntry> htmlEntries, final String htmlTextToHighlight) {
+        String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+        // Log.d(LOG, "html=" + html);
+        startActivityForResult(
+            HtmlDisplayActivity.getHtmlIntent(getApplicationContext(), String.format(
+                    "<html><head><meta name=\"viewport\" content=\"width=device-width\"></head><body>%s</body></html>", html),
+                                              htmlTextToHighlight, false),
+            0);
+    }
+
     static ViewGroup.LayoutParams WEIGHT_1 = new LinearLayout.LayoutParams(
         ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
 
@@ -1444,7 +1474,12 @@ public class DictionaryActivity extends ActionBarActivity {
 
         private void getMetrics() {
             // Get the screen's density scale
-            final float scale = getResources().getDisplayMetrics().density;
+            // The previous method getResources().getDisplayMetrics()
+            // used to occasionally trigger a null pointer exception,
+            // so try this instead.
+            DisplayMetrics dm = new DisplayMetrics();
+            getWindowManager().getDefaultDisplay().getMetrics(dm);
+            final float scale = dm.density;
             // Convert the dps to pixels, based on density scale
             mPaddingDefault = (int) (PADDING_DEFAULT_DP * scale + 0.5f);
             mPaddingLarge = (int) (PADDING_LARGE_DP * scale + 0.5f);
@@ -1637,13 +1672,13 @@ public class DictionaryActivity extends ActionBarActivity {
                 textView.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
-                        String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
-                        // Log.d(LOG, "html=" + html);
-                        startActivityForResult(
-                            HtmlDisplayActivity.getHtmlIntent(getApplicationContext(), String.format(
-                                    "<html><head></head><body>%s</body></html>", html),
-                                                              htmlTextToHighlight, false),
-                            0);
+                        showHtml(htmlEntries, htmlTextToHighlight);
+                    }
+                });
+                result.setOnClickListener(new OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        textView.performClick();
                     }
                 });
             }