android:key="@string/fontSizeKey"\r
android:title="@string/fontSizeTitle" \r
android:summary="@string/fontSizeSummary"\r
- android:defaultValue="12"\r
+ android:defaultValue="14"\r
android:persistent="true"\r
/>\r
\r
static final String DICTIONARY_CONFIGS = "dictionaryConfigs";
- static final String DICT_INDEX = "dictIndex";
+ static final String DICT_FILE = "dictFile";
static final String INDEX_INDEX = "indexIndex";
static final String SEARCH_TOKEN = "searchToken";
+ static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch";
public static final String THANKS_FOR_UPDATING_VERSION = "thanksForUpdatingVersion";
public DictionaryActivity() {\r
}\r
\r
- public static Intent getIntent(final Context context, final int dictIndex, final int indexIndex, final String searchToken) {\r
- setDictionaryPrefs(context, dictIndex, indexIndex, searchToken);\r
+ public static Intent getLaunchIntent(final String dictFile, final int indexIndex, final String searchToken) {\r
final Intent intent = new Intent();\r
intent.setClassName(DictionaryActivity.class.getPackage().getName(), DictionaryActivity.class.getName());\r
+ intent.putExtra(C.DICT_FILE, dictFile);\r
+ intent.putExtra(C.INDEX_INDEX, indexIndex);\r
+ intent.putExtra(C.SEARCH_TOKEN, searchToken);\r
return intent;\r
}\r
\r
- // TODO: Can we trigger an App restart when the preferences activity gets opened?\r
// TODO: fix these...\r
\r
@Override\r
protected void onSaveInstanceState(final Bundle outState) {\r
super.onSaveInstanceState(outState);\r
- setDictionaryPrefs(this, dictIndex, indexIndex, searchText.getText().toString());\r
- }\r
-\r
- public static void setDictionaryPrefs(final Context context,\r
- final int dictIndex, final int indexIndex, final String searchToken) {\r
- final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
- prefs.putInt(C.DICT_INDEX, dictIndex);\r
- prefs.putInt(C.INDEX_INDEX, indexIndex);\r
- prefs.putString(C.SEARCH_TOKEN, searchToken);\r
- prefs.commit();\r
+ outState.putString(C.SEARCH_TOKEN, searchText.getText().toString());\r
}\r
\r
- public static void clearDictionaryPrefs(final Context context) {\r
- final SharedPreferences.Editor prefs = PreferenceManager\r
- .getDefaultSharedPreferences(context).edit();\r
- prefs.remove(C.DICT_INDEX);\r
- prefs.remove(C.INDEX_INDEX);\r
- prefs.remove(C.SEARCH_TOKEN);\r
- prefs.commit();\r
- Log.d(LOG, "Removed default dictionary prefs.");\r
- }\r
-\r
@Override\r
public void onCreate(Bundle savedInstanceState) {\r
+ clearDictionaryPrefs(this);\r
+ \r
Log.d(LOG, "onCreate:" + this);\r
theme = ((DictionaryApplication)getApplication()).getSelectedTheme();\r
super.onCreate(savedInstanceState);\r
\r
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
- \r
+ final Intent intent = getIntent();\r
+ dictFile = intent.getStringExtra(C.DICT_FILE);\r
+\r
try {\r
PersistentObjectCache.init(this);\r
QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
- dictIndex = prefs.getInt(C.DICT_INDEX, 0) ;\r
- final DictionaryInfo dictionaryConfig = quickDicConfig.dictionaryInfos.get(dictIndex);\r
+ final DictionaryInfo dictionaryConfig = quickDicConfig.getDictionaryInfoByFile(dictFile);\r
this.setTitle("QuickDic: " + dictionaryConfig.name);\r
dictRaf = new RandomAccessFile(dictionaryConfig.localFile, "r");\r
dictionary = new Dictionary(dictRaf); \r
dictRaf = null;\r
}\r
Toast.makeText(this, getString(R.string.invalidDictionary, "", e.getMessage()), Toast.LENGTH_LONG);\r
- startActivity(DictionaryManagerActivity.getIntent(this));\r
+ startActivity(DictionaryEditActivity.getLaunchIntent(dictFile));\r
finish();\r
return;\r
}\r
\r
- indexIndex = prefs.getInt(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
Log.d(LOG, "Loading index.");\r
+ indexIndex = intent.getIntExtra(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
index = dictionary.indices.get(indexIndex);\r
setListAdapter(new IndexAdapter(index));\r
-\r
+ \r
// Pre-load the collators.\r
searchExecutor.execute(new Runnable() {\r
public void run() {\r
}\r
});\r
\r
- final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "12");\r
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
+ \r
+ final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");\r
try {\r
- fontSizeSp = Integer.parseInt(fontSize);\r
+ fontSizeSp = Integer.parseInt(fontSize.trim());\r
} catch (NumberFormatException e) {\r
fontSizeSp = 12;\r
}\r
// vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);\r
//}\r
Log.d(LOG, "wordList=" + wordList + ", saveOnlyFirstSubentry=" + saveOnlyFirstSubentry);\r
+ \r
+ setDictionaryPrefs(this, dictFile, indexIndex, searchText.getText().toString());\r
}\r
\r
@Override\r
protected void onPause() {\r
super.onPause();\r
}\r
+ \r
+ private static void setDictionaryPrefs(final Context context,\r
+ final String dictFile, final int indexIndex, final String searchToken) {\r
+ final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+ prefs.putString(C.DICT_FILE, dictFile);\r
+ prefs.putInt(C.INDEX_INDEX, indexIndex);\r
+ prefs.putString(C.SEARCH_TOKEN, searchToken);\r
+ prefs.commit();\r
+ }\r
+\r
+ private static void clearDictionaryPrefs(final Context context) {\r
+ final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+ prefs.remove(C.DICT_FILE);\r
+ prefs.remove(C.INDEX_INDEX);\r
+ prefs.remove(C.SEARCH_TOKEN);\r
+ prefs.commit();\r
+ }\r
+\r
\r
@Override\r
protected void onDestroy() {\r
if (dictRaf == null) {\r
return;\r
}\r
- setDictionaryPrefs(this, dictIndex, indexIndex, searchText.getText().toString());\r
\r
// Before we close the RAF, we have to wind the current search down.\r
if (currentSearchOperation != null) {\r
currentSearchOperation.interrupted.set(true);\r
currentSearchOperation = null;\r
}\r
- \r
changeIndex((indexIndex + 1)% dictionary.indices.size());\r
- onSearchTextChange(searchText.getText().toString());\r
}\r
\r
static class OpenIndexButton extends Button implements OnClickListener {\r
\r
final Activity activity;\r
- final int dictionaryIndex;\r
+ final String dictFile;\r
final int indexIndex;\r
\r
- public OpenIndexButton(final Context context, final Activity activity, final String text, final int dictionaryIndex, final int indexIndex) {\r
+ public OpenIndexButton(final Context context, final Activity activity, final String text, final String dictFile, final int indexIndex) {\r
super(context);\r
this.activity = activity;\r
- this.dictionaryIndex = dictionaryIndex;\r
+ this.dictFile = dictFile;\r
this.indexIndex = indexIndex;\r
setOnClickListener(this);\r
setText(text, BufferType.NORMAL);\r
@Override\r
public void onClick(View v) {\r
activity.finish();\r
- getContext().startActivity(DictionaryActivity.getIntent(getContext(), dictionaryIndex, indexIndex, ""));\r
+ getContext().startActivity(DictionaryActivity.getLaunchIntent(dictFile, indexIndex, ""));\r
}\r
\r
}\r
@Override\r
public View getView(int position, View convertView, ViewGroup parent) {\r
final LinearLayout result = new LinearLayout(parent.getContext());\r
- result.addView(new Butt)\r
+ //result.addView(new Butt)\r
+ return result;\r
}\r
\r
@Override\r
Log.d(LOG, "changingIndex, newLang=" + index.longName);\r
setListAdapter(indexAdapter);\r
updateLangButton();\r
+ searchText.requestFocus(); // Otherwise, nothing may happen.\r
+ onSearchTextChange(searchText.getText().toString());\r
}\r
\r
void onUpDownButton(final boolean up) {\r
final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));\r
dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
public boolean onMenuItemClick(final MenuItem menuItem) {\r
- startActivity(DictionaryManagerActivity.getIntent(DictionaryActivity.this));\r
+ startActivity(DictionaryManagerActivity.getLaunchIntent());\r
finish();\r
return false;\r
}\r
if (indexIndex != selectedSpannableIndex) {\r
changeIndex(selectedSpannableIndex);\r
}\r
- searchText.setText(selectedText);\r
+ setSearchText(selectedText);\r
return false;\r
}\r
});\r
public boolean onKeyDown(final int keyCode, final KeyEvent event) {\r
if (event.getUnicodeChar() != 0) {\r
if (!searchText.hasFocus()) {\r
- searchText.setText("" + (char) event.getUnicodeChar());\r
- onSearchTextChange(searchText.getText().toString());\r
- searchText.requestFocus();\r
- Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+ setSearchText("" + (char) event.getUnicodeChar());\r
}\r
return true;\r
}\r
return super.onKeyDown(keyCode, event);\r
}\r
\r
+ private void setSearchText(final String text) {\r
+ searchText.setText(text);\r
+ searchText.requestFocus();\r
+ onSearchTextChange(searchText.getText().toString());\r
+ Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+ }\r
+\r
\r
// --------------------------------------------------------------------------\r
// SearchOperation\r
textView.setMovementMethod(LinkMovementMethod.getInstance());\r
final Matcher matcher = CHAR_DASH.matcher(text);\r
while (matcher.find()) {\r
- spannable.setSpan(new MyClickableSpan(), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
+ spannable.setSpan(new NonLinkClickableSpan(), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
}\r
}\r
\r
static final String LOG = "QuickDic";
QuickDicConfig quickDicConfig;
- private DictionaryInfo dictionaryConfig;
+ private DictionaryInfo dictionaryInfo;
final Handler uiHandler = new Handler();
- public static Intent getIntent(final int dictIndex) {
+ public static Intent getLaunchIntent(final String dictFile) {
final Intent intent = new Intent();
intent.setClassName(DictionaryEditActivity.class.getPackage().getName(),
DictionaryEditActivity.class.getName());
- intent.putExtra(C.DICT_INDEX, dictIndex);
+ intent.putExtra(C.DICT_FILE, dictFile);
return intent;
}
setContentView(R.layout.edit_activity);
final Intent intent = getIntent();
-
- final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);
+ final String dictFile = intent.getStringExtra(C.DICT_FILE);
PersistentObjectCache.init(this);
try {
quickDicConfig = PersistentObjectCache.init(this).read(
C.DICTIONARY_CONFIGS, QuickDicConfig.class);
- dictionaryConfig = quickDicConfig.dictionaryInfos.get(dictIndex);
+ dictionaryInfo = quickDicConfig.getDictionaryInfoByFile(dictFile);
} catch (Exception e) {
Log.e(LOG, "Failed to read QuickDicConfig.", e);
- quickDicConfig = new QuickDicConfig(this);
- dictionaryConfig = quickDicConfig.dictionaryInfos.get(0);
+ finish();
+ startActivity(DictionaryManagerActivity.getLaunchIntent());
+ return;
}
// Write stuff from object into fields.
((EditText) findViewById(R.id.dictionaryName))
- .setText(dictionaryConfig.name);
+ .setText(dictionaryInfo.name);
((EditText) findViewById(R.id.localFile))
- .setText(dictionaryConfig.localFile);
+ .setText(dictionaryInfo.localFile);
final TextWatcher textWatcher = new TextWatcher() {
@Override
((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
- downloadUrl.setText(dictionaryConfig.downloadUrl);
+ downloadUrl.setText(dictionaryInfo.downloadUrl);
downloadUrl.addTextChangedListener(textWatcher);
final Button downloadButton = (Button) findViewById(R.id.downloadButton);
@Override
public void onClick(View v) {
startDownloadDictActivity(DictionaryEditActivity.this,
- dictionaryConfig);
+ dictionaryInfo);
}
});
openButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
- final Intent intent = DictionaryActivity.getIntent(DictionaryEditActivity.this, dictIndex, 0, "");
+ final Intent intent = DictionaryActivity.getLaunchIntent(dictFile, 0, "");
startActivity(intent);
}
});
super.onPause();
// Read stuff from fields into object.
- dictionaryConfig.name = ((EditText) findViewById(R.id.dictionaryName))
+ dictionaryInfo.name = ((EditText) findViewById(R.id.dictionaryName))
.getText().toString();
- dictionaryConfig.localFile = ((EditText) findViewById(R.id.localFile))
+ dictionaryInfo.localFile = ((EditText) findViewById(R.id.localFile))
.getText().toString();
- dictionaryConfig.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
+ dictionaryInfo.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
.getText().toString();
PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem menuItem) {
startDownloadDictActivity(DictionaryEditActivity.this,
- dictionaryConfig);
+ dictionaryInfo);
return false;
}
});
final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));
dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem menuItem) {
- startActivity(DictionaryManagerActivity.getIntent(DictionaryEditActivity.this));
+ startActivity(DictionaryManagerActivity.getLaunchIntent());
return false;
}
});
private static final long serialVersionUID = -6850863377577700388L;
- public static final class IndexInfo {
+ public static final class IndexInfo implements Serializable {
+ private static final long serialVersionUID = 6524751236198309438L;
+
public IndexInfo(String langIso, int allTokenCount, int mainTokenCount) {
this.langIso = langIso;
this.allTokenCount = allTokenCount;
import android.app.AlertDialog;
import android.app.ListActivity;
-import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
+import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
-import android.view.ContextMenu.ContextMenuInfo;
-import android.view.MenuItem.OnMenuItemClickListener;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.AdapterView;
+import android.widget.AdapterView.AdapterContextMenuInfo;
+import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
-import android.widget.AdapterView.AdapterContextMenuInfo;
-import android.widget.AdapterView.OnItemClickListener;
import com.hughes.android.util.PersistentObjectCache;
public class DictionaryManagerActivity extends ListActivity {
static final String LOG = "QuickDic";
-
QuickDicConfig quickDicConfig;
+ static boolean canAutoLaunch = true;
+
public void onCreate(Bundle savedInstanceState) {
//((DictionaryApplication)getApplication()).applyTheme(this);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(thanksForUpdatingLatestVersion)) {
+ canAutoLaunch = false;
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
final WebView webView = new WebView(getApplicationContext());
alert.getWindow().setAttributes(layoutParams);
prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion).commit();
}
+
+ if (!getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true)) {
+ canAutoLaunch = false;
+ }
}
private void onClick(int dictIndex) {
- final Intent intent = DictionaryActivity.getIntent(this, dictIndex, 0, "");
+ final DictionaryInfo dictionaryInfo = quickDicConfig.dictionaryInfos.get(dictIndex);
+ final Intent intent = DictionaryActivity.getLaunchIntent(dictionaryInfo.localFile, 0, "");
startActivity(intent);
}
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- if (prefs.contains(C.DICT_INDEX) && prefs.contains(C.INDEX_INDEX)) {
+ if (canAutoLaunch && prefs.contains(C.DICT_FILE) && prefs.contains(C.INDEX_INDEX)) {
+ canAutoLaunch = false; // Only autolaunch once per-process, on startup.
Log.d(LOG, "Skipping Dictionary List, going straight to dictionary.");
- startActivity(DictionaryActivity.getIntent(this, prefs.getInt(C.DICT_INDEX, 0), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
+ startActivity(DictionaryActivity.getLaunchIntent(prefs.getString(C.DICT_FILE, ""), prefs.getInt(C.INDEX_INDEX, 0), prefs.getString(C.SEARCH_TOKEN, "")));
+ // Don't finish, so that user can hit back and get here.
//finish();
return;
}
}
- public static Intent getIntent(final Context context) {
- DictionaryActivity.clearDictionaryPrefs(context);
+ public static Intent getLaunchIntent() {
final Intent intent = new Intent();
intent.setClassName(DictionaryManagerActivity.class.getPackage().getName(),
DictionaryManagerActivity.class.getName());
+ intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
return intent;
}
+++ /dev/null
-package com.hughes.android.dictionary;
-
-import android.text.TextPaint;
-import android.text.style.ClickableSpan;
-import android.view.View;
-
-public class MyClickableSpan extends ClickableSpan {
-
- static MyClickableSpan instance = new MyClickableSpan();
-
- // Won't see these on a long-click.
- @Override
- public void onClick(View widget) {
- }
-
- @Override
- public void updateDrawState(TextPaint ds) {
- super.updateDrawState(ds);
- ds.setUnderlineText(false);
- //ds.setColor(color);
- }
-
-}
--- /dev/null
+// Copyright 2012 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.hughes.android.dictionary;
+
+import android.text.TextPaint;
+import android.text.style.ClickableSpan;
+import android.view.View;
+
+public class NonLinkClickableSpan extends ClickableSpan {
+
+ static NonLinkClickableSpan instance = new NonLinkClickableSpan();
+
+ // Won't see these on a long-click.
+ @Override
+ public void onClick(View widget) {
+ }
+
+ @Override
+ public void updateDrawState(TextPaint ds) {
+ super.updateDrawState(ds);
+ ds.setUnderlineText(false);
+ //ds.setColor(color);
+ }
+
+}
}
dictionaryInfos.add(dictionaryConfig);
}
+
+ DictionaryInfo getDictionaryInfoByFile(final String dictFile) throws Exception {
+ for (int i = 0; i < dictionaryInfos.size(); ++i) {
+ if (dictionaryInfos.get(i).localFile.equals(dictFile)) {
+ return dictionaryInfos.get(i);
+ }
+ }
+ throw new Exception("Not found: " + dictFile);
+ }
}