]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/HtmlDisplayActivity.java
7a8a7e89c4a286561b0753e2ac4a0f302b766318
[Dictionary.git] / src / com / hughes / android / dictionary / HtmlDisplayActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary;
16
17 import android.content.Context;
18 import android.content.Intent;
19 import android.content.SharedPreferences;
20 import android.os.Bundle;
21 import android.preference.PreferenceManager;
22 import android.support.v7.app.ActionBar;
23 import android.support.v7.app.AppCompatActivity;
24 import android.util.Base64;
25 import android.util.Log;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.widget.Button;
29
30 import com.hughes.util.StringUtil;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.UnsupportedEncodingException;
35
36 public final class HtmlDisplayActivity extends AppCompatActivity {
37
38     private static final String LOG = "QuickDic";
39
40     private static final String HTML_RES = "html_res";
41     private static final String HTML = "html";
42     private static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
43     private static final String SHOW_OK_BUTTON = "showOKButton";
44
45     public static Intent getHelpLaunchIntent(Context c) {
46         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
47         intent.putExtra(HTML_RES, R.raw.help);
48         return intent;
49     }
50
51     public static Intent getWhatsNewLaunchIntent(Context c) {
52         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
53         intent.putExtra(HTML_RES, R.raw.whats_new);
54         return intent;
55     }
56
57     public static Intent getHtmlIntent(Context c, final String html, final String textToHighlight,
58                                        final boolean showOkButton) {
59         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
60         intent.putExtra(HTML, html);
61         intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight == null ? "" : textToHighlight);
62         intent.putExtra(SHOW_OK_BUTTON, showOkButton);
63         return intent;
64     }
65
66     public void onOkClick(View dummy) {
67         finish();
68     }
69
70     /** Called when the activity is first created. */
71     @Override
72     public void onCreate(final Bundle savedInstanceState) {
73         DictionaryApplication.INSTANCE.init(getApplicationContext());
74         setTheme(DictionaryApplication.INSTANCE.getSelectedTheme().themeId);
75
76         super.onCreate(savedInstanceState);
77         setContentView(R.layout.html_display_activity);
78
79         ActionBar actionBar = getSupportActionBar();
80         actionBar.setDisplayHomeAsUpEnabled(true);
81
82         final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
83         String html;
84         if (htmlRes != -1) {
85             InputStream res = getResources().openRawResource(htmlRes);
86             html = StringUtil.readToString(res);
87             try {
88                 res.close();
89             } catch (IOException ignored) {
90             }
91         } else {
92             html = getIntent().getStringExtra(HTML);
93         }
94         final MyWebView webView = (MyWebView) findViewById(R.id.webView);
95         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
96         final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");
97         int fontSizeSp;
98         try {
99             fontSizeSp = Integer.parseInt(fontSize.trim());
100         } catch (NumberFormatException e) {
101             fontSizeSp = 14;
102         }
103         webView.getSettings().setDefaultFontSize(fontSizeSp);
104         try {
105             // No way to get pure UTF-8 data into WebView
106             html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT);
107         } catch (UnsupportedEncodingException e) {
108             throw new RuntimeException("Missing UTF-8 support?!", e);
109         }
110         // Use loadURL to allow specifying a charset
111         webView.loadUrl("data:text/html;charset=utf-8;base64," + html);
112         webView.activity = this;
113
114         final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
115         if (textToHighlight != null && !"".equals(textToHighlight)) {
116             Log.d(LOG, "NOT Highlighting text: " + textToHighlight);
117             // This isn't working:
118             // webView.findAll(textToHighlight);
119             // webView.showFindDialog(textToHighlight, false);
120         }
121
122         final Button okButton = (Button) findViewById(R.id.okButton);
123         if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
124             okButton.setVisibility(Button.GONE);
125         }
126     }
127
128     @Override
129     public void onBackPressed() {
130         final MyWebView webView = (MyWebView)findViewById(R.id.webView);
131         if (webView.canGoBack()) webView.goBack();
132         else super.onBackPressed();
133     }
134
135     @Override
136     public boolean onOptionsItemSelected(MenuItem item) {
137         // Explicitly handle the up button press so
138         // we return to the dictionary.
139         if (item.getItemId() == android.R.id.home) {
140             finish();
141             return true;
142         }
143         return super.onOptionsItemSelected(item);
144     }
145 }