]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/HtmlDisplayActivity.java
Switch to app compat preferences.
[Dictionary.git] / src / com / hughes / android / dictionary / HtmlDisplayActivity.java
index de3c138373728e8b764e0a8b8ffc1f4e5f633989..f6a37e93e5cbdd4ec1063a3153da1872bd84de33 100644 (file)
@@ -16,14 +16,15 @@ package com.hughes.android.dictionary;
 
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.support.v7.preference.PreferenceManager;
 import android.support.v7.app.ActionBar;
-import android.support.v7.app.ActionBarActivity;
+import android.support.v7.app.AppCompatActivity;
 import android.util.Base64;
 import android.util.Log;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.View.OnClickListener;
 import android.widget.Button;
 
 import com.hughes.util.StringUtil;
@@ -32,14 +33,14 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 
-public final class HtmlDisplayActivity extends ActionBarActivity {
+public final class HtmlDisplayActivity extends AppCompatActivity {
 
-    static final String LOG = "QuickDic";
+    private static final String LOG = "QuickDic";
 
-    static final String HTML_RES = "html_res";
-    static final String HTML = "html";
-    static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
-    static final String SHOW_OK_BUTTON = "showOKButton";
+    private static final String HTML_RES = "html_res";
+    private static final String HTML = "html";
+    private static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
+    private static final String SHOW_OK_BUTTON = "showOKButton";
 
     public static Intent getHelpLaunchIntent(Context c) {
         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
@@ -54,18 +55,23 @@ public final class HtmlDisplayActivity extends ActionBarActivity {
     }
 
     public static Intent getHtmlIntent(Context c, final String html, final String textToHighlight,
-            final boolean showOkButton) {
+                                       final boolean showOkButton) {
         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
         intent.putExtra(HTML, html);
-        intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight);
+        intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight == null ? "" : textToHighlight);
         intent.putExtra(SHOW_OK_BUTTON, showOkButton);
         return intent;
     }
 
+    public void onOkClick(View dummy) {
+        finish();
+    }
+
     /** Called when the activity is first created. */
     @Override
     public void onCreate(final Bundle savedInstanceState) {
-        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+        DictionaryApplication.INSTANCE.init(getApplicationContext());
+        setTheme(DictionaryApplication.INSTANCE.getSelectedTheme().themeId);
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.html_display_activity);
@@ -80,14 +86,24 @@ public final class HtmlDisplayActivity extends ActionBarActivity {
             html = StringUtil.readToString(res);
             try {
                 res.close();
-            } catch (IOException e) {
+            } catch (IOException ignored) {
             }
         } else {
             html = getIntent().getStringExtra(HTML);
         }
         final MyWebView webView = (MyWebView) findViewById(R.id.webView);
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");
+        int fontSizeSp;
+        try {
+            fontSizeSp = Integer.parseInt(fontSize.trim());
+        } catch (NumberFormatException e) {
+            fontSizeSp = 14;
+        }
+        webView.getSettings().setDefaultFontSize(fontSizeSp);
         try {
             // No way to get pure UTF-8 data into WebView
+            // Cannot use StandardCharsets due to older Android.
             html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT);
         } catch (UnsupportedEncodingException e) {
             throw new RuntimeException("Missing UTF-8 support?!", e);
@@ -105,12 +121,6 @@ public final class HtmlDisplayActivity extends ActionBarActivity {
         }
 
         final Button okButton = (Button) findViewById(R.id.okButton);
-        okButton.setOnClickListener(new OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                finish();
-            }
-        });
         if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
             okButton.setVisibility(Button.GONE);
         }
@@ -127,8 +137,7 @@ public final class HtmlDisplayActivity extends ActionBarActivity {
     public boolean onOptionsItemSelected(MenuItem item) {
         // Explicitly handle the up button press so
         // we return to the dictionary.
-        if (item.getItemId() == android.R.id.home)
-        {
+        if (item.getItemId() == android.R.id.home) {
             finish();
             return true;
         }