]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Remove some margins that seem a bit overkill.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 008b57de6c1ef6869612267e22177b1e79829a63..7517441d49c034cce939e2f661c894a4140e44c2 100644 (file)
@@ -99,6 +99,7 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Collections;
@@ -124,7 +125,8 @@ public class DictionaryActivity extends ActionBarActivity {
     DictionaryApplication application;
 
     File dictFile = null;
-    RandomAccessFile dictRaf = null;
+    FileChannel dictRaf = null;
+    String dictFileTitleName = null;
 
     Dictionary dictionary = null;
 
@@ -224,6 +226,22 @@ public class DictionaryActivity extends ActionBarActivity {
         return search.length();
     }
 
+    private void dictionaryOpenFail(Exception e) {
+        Log.e(LOG, "Unable to load dictionary.", e);
+        if (dictRaf != null) {
+            try {
+                dictRaf.close();
+            } catch (IOException e1) {
+                Log.e(LOG, "Unable to close dictRaf.", e1);
+            }
+            dictRaf = null;
+        }
+        Toast.makeText(this, getString(R.string.invalidDictionary, "", e.getMessage()),
+                       Toast.LENGTH_LONG).show();
+        startActivity(DictionaryManagerActivity.getLaunchIntent(getApplicationContext()));
+        finish();
+    }
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         DictionaryApplication.INSTANCE.init(getApplicationContext());
@@ -239,13 +257,22 @@ public class DictionaryActivity extends ActionBarActivity {
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
 
         // Don't auto-launch if this fails.
-        prefs.edit().remove(C.DICT_FILE).commit();
+        prefs.edit().remove(C.DICT_FILE).remove(C.INDEX_SHORT_NAME).commit();
 
         setContentView(R.layout.dictionary_activity);
 
         theme = application.getSelectedTheme();
         textColorFg = getResources().getColor(theme.tokenRowFgColor);
 
+        if (dictRaf != null) {
+            try {
+                dictRaf.close();
+            } catch (IOException e) {
+                Log.e(LOG, "Failed to close dictionary", e);
+            }
+            dictRaf = null;
+        }
+
         final Intent intent = getIntent();
         String intentAction = intent.getAction();
         /**
@@ -322,6 +349,18 @@ public class DictionaryActivity extends ActionBarActivity {
                 getIntent().putExtra(C.SEARCH_TOKEN, query);
             }
         }
+        // Support opening dictionary file directly
+        if (intentAction != null && intentAction.equals(Intent.ACTION_VIEW)) {
+            Uri uri = intent.getData();
+            intent.putExtra(C.DICT_FILE, uri.toString());
+            dictFileTitleName = uri.getLastPathSegment();
+            try {
+                dictRaf = getContentResolver().openAssetFileDescriptor(uri, "r").createInputStream().getChannel();
+            } catch (Exception e) {
+                dictionaryOpenFail(e);
+                return;
+            }
+        }
         /**
          * @author Dominik Köppl If no dictionary is chosen, use the default
          *         dictionary specified in the preferences If this step does
@@ -345,7 +384,7 @@ public class DictionaryActivity extends ActionBarActivity {
                 try {
                     Log.d(LOG, "Checking dictionary " + dics.get(i).uncompressedFilename);
                     final File dictfile = application.getPath(dics.get(i).uncompressedFilename);
-                    Dictionary dic = new Dictionary(new RandomAccessFile(dictfile, "r"));
+                    Dictionary dic = new Dictionary(new RandomAccessFile(dictfile, "r").getChannel());
                     for (int j = 0; j < dic.indices.size(); ++j) {
                         Index idx = dic.indices.get(j);
                         Log.d(LOG, "Checking index " + idx.shortName);
@@ -377,7 +416,7 @@ public class DictionaryActivity extends ActionBarActivity {
             finish();
             return;
         }
-        if (dictFilename != null)
+        if (dictRaf == null && dictFilename != null)
             dictFile = new File(dictFilename);
 
         ttsReady = false;
@@ -390,24 +429,14 @@ public class DictionaryActivity extends ActionBarActivity {
         });
 
         try {
-            final String name = application.getDictionaryName(dictFile.getName());
-            this.setTitle("QuickDic: " + name);
-            dictRaf = new RandomAccessFile(dictFile, "r");
+            if (dictRaf == null) {
+                dictFileTitleName = application.getDictionaryName(dictFile.getName());
+                dictRaf = new RandomAccessFile(dictFile, "r").getChannel();
+            }
+            this.setTitle("QuickDic: " + dictFileTitleName);
             dictionary = new Dictionary(dictRaf);
         } catch (Exception e) {
-            Log.e(LOG, "Unable to load dictionary.", e);
-            if (dictRaf != null) {
-                try {
-                    dictRaf.close();
-                } catch (IOException e1) {
-                    Log.e(LOG, "Unable to close dictRaf.", e1);
-                }
-                dictRaf = null;
-            }
-            Toast.makeText(this, getString(R.string.invalidDictionary, "", e.getMessage()),
-                           Toast.LENGTH_LONG).show();
-            startActivity(DictionaryManagerActivity.getLaunchIntent(getApplicationContext()));
-            finish();
+            dictionaryOpenFail(e);
             return;
         }
         String targetIndex = intent.getStringExtra(C.INDEX_SHORT_NAME);
@@ -534,7 +563,7 @@ public class DictionaryActivity extends ActionBarActivity {
         setSearchText(text, true);
         Log.d(LOG, "Trying to restore searchText=" + text);
 
-        setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
+        setDictionaryPrefs(this, dictFile, index.shortName);
 
         updateLangButton();
         searchView.requestFocus();
@@ -649,12 +678,14 @@ public class DictionaryActivity extends ActionBarActivity {
     }
 
     private static void setDictionaryPrefs(final Context context, final File dictFile,
-                                           final String indexShortName, final String searchToken) {
+                                           final String indexShortName) {
         final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(
                 context).edit();
-        prefs.putString(C.DICT_FILE, dictFile.getPath());
-        prefs.putString(C.INDEX_SHORT_NAME, indexShortName);
-        prefs.putString(C.SEARCH_TOKEN, ""); // Don't need to save search token.
+        if (dictFile != null) {
+            prefs.putString(C.DICT_FILE, dictFile.getPath());
+            prefs.putString(C.INDEX_SHORT_NAME, indexShortName);
+        }
+        prefs.remove(C.SEARCH_TOKEN); // Don't need to save search token.
         prefs.commit();
     }
 
@@ -805,7 +836,7 @@ public class DictionaryActivity extends ActionBarActivity {
 
                 final LinearLayout result = new LinearLayout(parent.getContext());
 
-                for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
+                for (int i = 0; dictionaryInfo.indexInfos != null && i < dictionaryInfo.indexInfos.size(); ++i) {
                     final IndexInfo indexInfo = dictionaryInfo.indexInfos.get(i);
                     final View button = IsoUtils.INSTANCE.createButton(parent.getContext(),
                                         dictionaryInfo, indexInfo, application.languageButtonPixels);
@@ -961,16 +992,20 @@ public class DictionaryActivity extends ActionBarActivity {
                     dialog.setContentView(R.layout.about_dictionary_dialog);
                     final TextView textView = (TextView) dialog.findViewById(R.id.text);
 
-                    final String name = application.getDictionaryName(dictFile.getName());
-                    dialog.setTitle(name);
+                    dialog.setTitle(dictFileTitleName);
 
                     final StringBuilder builder = new StringBuilder();
                     final DictionaryInfo dictionaryInfo = dictionary.getDictionaryInfo();
-                    dictionaryInfo.uncompressedBytes = dictFile.length();
                     if (dictionaryInfo != null) {
+                        try {
+                            dictionaryInfo.uncompressedBytes = dictRaf.size();
+                        } catch (IOException e) {
+                        }
                         builder.append(dictionaryInfo.dictInfo).append("\n\n");
-                        builder.append(getString(R.string.dictionaryPath, dictFile.getPath()))
-                        .append("\n");
+                        if (dictFile != null) {
+                            builder.append(getString(R.string.dictionaryPath, dictFile.getPath()))
+                            .append("\n");
+                        }
                         builder.append(
                             getString(R.string.dictionarySize, dictionaryInfo.uncompressedBytes))
                         .append("\n");
@@ -1263,7 +1298,7 @@ public class DictionaryActivity extends ActionBarActivity {
             indexAdapter = new IndexAdapter(index);
             setListAdapter(indexAdapter);
             Log.d(LOG, "changingIndex, newLang=" + index.longName);
-            setDictionaryPrefs(this, dictFile, index.shortName, searchView.getQuery().toString());
+            setDictionaryPrefs(this, dictFile, index.shortName);
             updateLangButton();
         }
         setSearchText(newSearchText, true, hideKeyboard);
@@ -1473,13 +1508,18 @@ public class DictionaryActivity extends ActionBarActivity {
         }
 
         private void getMetrics() {
+            float scale = 1;
             // Get the screen's density scale
             // 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;
+            // As it still crashes, add a fallback
+            try {
+                DisplayMetrics dm = new DisplayMetrics();
+                getWindowManager().getDefaultDisplay().getMetrics(dm);
+                scale = dm.density;
+            } catch (NullPointerException e)
+            {}
             // 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);
@@ -1543,16 +1583,13 @@ public class DictionaryActivity extends ActionBarActivity {
                 // Set the columns in the table.
                 if (r > 0) {
                     final TextView bullet = new TextView(tableRow.getContext());
-                    bullet.setText(" • ");
+                    bullet.setText(" •");
                     tableRow.addView(bullet);
                 }
                 tableRow.addView(col1, layoutParams);
-                final TextView margin = new TextView(tableRow.getContext());
-                margin.setText(" ");
-                tableRow.addView(margin);
                 if (r > 0) {
                     final TextView bullet = new TextView(tableRow.getContext());
-                    bullet.setText(" • ");
+                    bullet.setText(" •");
                     tableRow.addView(bullet);
                 }
                 tableRow.addView(col2, layoutParams);