X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2FDictionaryActivity.java;h=b5451a79d1d561c85b3dac2a404b420997761540;hb=49583e178d3673fd1cade2f306d97303ffbd4a0a;hp=c4d2ddae81a926ede0ede7237abe1c3a953823e1;hpb=20608310f9c77588105f5731935b6a9c9a4749bd;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index c4d2dda..b5451a7 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -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()); @@ -246,6 +264,15 @@ public class DictionaryActivity extends ActionBarActivity { 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); @@ -652,8 +681,10 @@ public class DictionaryActivity extends ActionBarActivity { final String indexShortName, final String searchToken) { final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences( context).edit(); - prefs.putString(C.DICT_FILE, dictFile.getPath()); - prefs.putString(C.INDEX_SHORT_NAME, indexShortName); + if (dictFile != null) { + 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. prefs.commit(); } @@ -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");