From 9c501f32b5275b07f2104d65d696c0b4735b021c Mon Sep 17 00:00:00 2001 From: Thad Hughes Date: Sun, 23 Dec 2012 09:42:44 -0800 Subject: [PATCH] Fixed text color of normal text. --- AndroidManifest.xml | 2 +- res/values/themes.xml | 3 +- src/com/hughes/android/dictionary/C.java | 80 ++++++++++--------- .../dictionary/DictionaryActivity.java | 11 ++- .../android/dictionary/engine/HtmlEntry.java | 78 ++++++++++++++---- .../android/dictionary/engine/TokenRow.java | 2 +- .../android/util/NonLinkClickableSpan.java | 37 +++++---- 7 files changed, 140 insertions(+), 73 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 74fbac2..99eea88 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -16,7 +16,7 @@ - + #FFFFFF + #FFFFFF #111111 #111111 #000000 @@ -26,7 +27,7 @@ #000000 - #FFFFFF + #000000 #EEEEEE #EEEEEE #FFFFFF diff --git a/src/com/hughes/android/dictionary/C.java b/src/com/hughes/android/dictionary/C.java index e2f03e0..5d57514 100644 --- a/src/com/hughes/android/dictionary/C.java +++ b/src/com/hughes/android/dictionary/C.java @@ -16,43 +16,49 @@ package com.hughes.android.dictionary; public class C { - static final String DICTIONARY_CONFIGS = "dictionaryConfigs2"; - - public static final String DICT_FILE = "dictFile"; - public static final String INDEX_INDEX = "indexIndex"; - public static final String SEARCH_TOKEN = "searchToken"; - public static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch"; - public static final String SHOW_LOCAL = "showLocal"; - - public static final String THANKS_FOR_UPDATING_VERSION = "thanksForUpdatingVersion"; - - enum Theme { - DEFAULT(R.style.Theme_Default, R.style.Theme_Default_TokenRow_Fg, - R.drawable.theme_default_token_row_main_bg, - R.drawable.theme_default_token_row_other_bg, - R.drawable.theme_default_other_lang_bg), - - LIGHT(R.style.Theme_Light, - R.style.Theme_Light_TokenRow_Fg, - R.drawable.theme_light_token_row_main_bg, - R.drawable.theme_light_token_row_other_bg, - R.drawable.theme_light_other_lang_bg); - - private Theme(final int themeId, final int tokenRowFg, - final int tokenRowMainBg, final int tokenRowOtherBg, - final int otherLangBg) { - this.themeId = themeId; - this.tokenRowFg = tokenRowFg; - this.tokenRowMainBg = tokenRowMainBg; - this.tokenRowOtherBg = tokenRowOtherBg; - this.otherLangBg = otherLangBg; - } + static final String DICTIONARY_CONFIGS = "dictionaryConfigs2"; + + public static final String DICT_FILE = "dictFile"; + public static final String INDEX_INDEX = "indexIndex"; + public static final String SEARCH_TOKEN = "searchToken"; + public static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch"; + public static final String SHOW_LOCAL = "showLocal"; + + public static final String THANKS_FOR_UPDATING_VERSION = "thanksForUpdatingVersion"; + + enum Theme { + DEFAULT(R.style.Theme_Default, + R.style.Theme_Default_TokenRow_Fg, + R.color.theme_default_token_row_fg, + R.drawable.theme_default_token_row_main_bg, + R.drawable.theme_default_token_row_other_bg, + R.drawable.theme_default_other_lang_bg), - final int themeId; - final int tokenRowFg; - final int tokenRowMainBg; - final int tokenRowOtherBg; - final int otherLangBg; - } + LIGHT(R.style.Theme_Light, + R.style.Theme_Light_TokenRow_Fg, + R.color.theme_light_token_row_fg, + R.drawable.theme_light_token_row_main_bg, + R.drawable.theme_light_token_row_other_bg, + R.drawable.theme_light_other_lang_bg); + + private Theme(final int themeId, final int tokenRowFg, + final int tokenRowFgColor, + final int tokenRowMainBg, final int tokenRowOtherBg, + final int otherLangBg) { + this.themeId = themeId; + this.tokenRowFg = tokenRowFg; + this.tokenRowFgColor = tokenRowFgColor; + this.tokenRowMainBg = tokenRowMainBg; + this.tokenRowOtherBg = tokenRowOtherBg; + this.otherLangBg = otherLangBg; + } + + final int themeId; + final int tokenRowFg; + final int tokenRowFgColor; + final int tokenRowMainBg; + final int tokenRowOtherBg; + final int otherLangBg; + } } diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 96f385e..75751c7 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -19,6 +19,7 @@ import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.graphics.Typeface; import android.net.Uri; import android.os.Bundle; @@ -50,7 +51,6 @@ import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.BaseAdapter; import android.widget.Button; @@ -125,6 +125,8 @@ public class DictionaryActivity extends ListActivity { TextToSpeech textToSpeech; volatile boolean ttsReady; + + int textColorFg = Color.BLACK; private final Executor searchExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override @@ -206,6 +208,7 @@ public class DictionaryActivity extends ListActivity { application = (DictionaryApplication) getApplication(); theme = application.getSelectedTheme(); + textColorFg = getResources().getColor(theme.tokenRowFgColor); final Intent intent = getIntent(); dictFile = new File(intent.getStringExtra(C.DICT_FILE)); @@ -218,7 +221,7 @@ public class DictionaryActivity extends ListActivity { updateTTSLanuage(); } }); - + try { final String name = application.getDictionaryName(dictFile.getName()); this.setTitle("QuickDic: " + name); @@ -1322,7 +1325,7 @@ public class DictionaryActivity extends ListActivity { } static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}\\p{M}\\p{N}]+"); - + private void createTokenLinkSpans(final TextView textView, final Spannable spannable, final String text) { // Saw from the source code that LinkMovementMethod sets the selection! @@ -1330,7 +1333,7 @@ public class DictionaryActivity extends ListActivity { textView.setMovementMethod(LinkMovementMethod.getInstance()); final Matcher matcher = CHAR_DASH.matcher(text); while (matcher.find()) { - spannable.setSpan(new NonLinkClickableSpan(), matcher.start(), matcher.end(), + spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } } diff --git a/src/com/hughes/android/dictionary/engine/HtmlEntry.java b/src/com/hughes/android/dictionary/engine/HtmlEntry.java index 4143fd3..c7e0628 100644 --- a/src/com/hughes/android/dictionary/engine/HtmlEntry.java +++ b/src/com/hughes/android/dictionary/engine/HtmlEntry.java @@ -2,6 +2,7 @@ package com.hughes.android.dictionary.engine; import android.content.Intent; import android.net.Uri; +import android.util.Log; import com.hughes.android.dictionary.C; import com.hughes.util.StringUtil; @@ -13,8 +14,7 @@ import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URLDecoder; +import java.lang.ref.SoftReference; import java.net.URLEncoder; import java.util.List; import java.util.regex.Pattern; @@ -23,34 +23,37 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable { final Dictionary dictionary; @@ -85,7 +87,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable%s\n(%s)\n

%s\n", - formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name, - htmlEntry.html)); + result.append(String.format("

%s

\n

%s\n", + formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, + htmlEntry.getHtml())); } return result.toString(); } @@ -188,5 +190,51 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable htmlRef = new SoftReference(null); + + private LazyHtmlLoader(final RandomAccessFile raf) throws IOException { + this.raf = raf; + numBytes = raf.readInt(); + numZipBytes = raf.readInt(); + offset = raf.getFilePointer(); + raf.skipBytes(numZipBytes); + } + + public String getHtml() { + String html = htmlRef.get(); + if (html != null) { + return html; + } + System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + numZipBytes); + final byte[] bytes = new byte[numBytes]; + final byte[] zipBytes = new byte[numZipBytes]; + synchronized (raf) { + try { + raf.seek(offset); + raf.read(zipBytes); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + try { + StringUtil.unzipFully(zipBytes, bytes); + html = new String(bytes, "UTF-8"); + } catch (IOException e) { + throw new RuntimeException(e); + } + htmlRef = new SoftReference(html); + return html; + } + } } diff --git a/src/com/hughes/android/dictionary/engine/TokenRow.java b/src/com/hughes/android/dictionary/engine/TokenRow.java index 96842e9..86d4f7e 100644 --- a/src/com/hughes/android/dictionary/engine/TokenRow.java +++ b/src/com/hughes/android/dictionary/engine/TokenRow.java @@ -64,7 +64,7 @@ public class TokenRow extends RowBase { final String surrounder = hasMainEntry ? "***" : "==="; out.println(surrounder + getToken() + surrounder); for (final HtmlEntry htmlEntry : index.sortedIndexEntries.get(referenceIndex).htmlEntries) { - out.println("HtmlEntry: " + htmlEntry.title + " <<<" + htmlEntry.html + ">>>"); + out.println("HtmlEntry: " + htmlEntry.title + " <<<" + htmlEntry.getHtml() + ">>>"); } } diff --git a/src/com/hughes/android/util/NonLinkClickableSpan.java b/src/com/hughes/android/util/NonLinkClickableSpan.java index 075cc59..a590d38 100644 --- a/src/com/hughes/android/util/NonLinkClickableSpan.java +++ b/src/com/hughes/android/util/NonLinkClickableSpan.java @@ -19,19 +19,28 @@ import android.text.style.ClickableSpan; import android.view.View; public class NonLinkClickableSpan extends ClickableSpan { - - public static final 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); - } + + // The singleton pattern doesn't work here--we need a separate instance for + // each span. + + final int color; + + public NonLinkClickableSpan(int color) { + this.color = color; + } + + // Won't see these on a long-click. + @Override + public void onClick(View widget) { + // Don't need to do anything. These spans are just used to see where + // the user long-pressed. + } + + @Override + public void updateDrawState(TextPaint ds) { + super.updateDrawState(ds); + ds.setUnderlineText(false); + ds.setColor(color); + } } -- 2.43.0