]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/HtmlDisplayActivity.java
Some lint fixes.
[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 import java.nio.charset.StandardCharsets;
36
37 public final class HtmlDisplayActivity extends AppCompatActivity {
38
39     private static final String LOG = "QuickDic";
40
41     private static final String HTML_RES = "html_res";
42     private static final String HTML = "html";
43     private static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
44     private static final String SHOW_OK_BUTTON = "showOKButton";
45
46     public static Intent getHelpLaunchIntent(Context c) {
47         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
48         intent.putExtra(HTML_RES, R.raw.help);
49         return intent;
50     }
51
52     public static Intent getWhatsNewLaunchIntent(Context c) {
53         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
54         intent.putExtra(HTML_RES, R.raw.whats_new);
55         return intent;
56     }
57
58     public static Intent getHtmlIntent(Context c, final String html, final String textToHighlight,
59                                        final boolean showOkButton) {
60         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
61         intent.putExtra(HTML, html);
62         intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight == null ? "" : textToHighlight);
63         intent.putExtra(SHOW_OK_BUTTON, showOkButton);
64         return intent;
65     }
66
67     public void onOkClick(View dummy) {
68         finish();
69     }
70
71     /** Called when the activity is first created. */
72     @Override
73     public void onCreate(final Bundle savedInstanceState) {
74         DictionaryApplication.INSTANCE.init(getApplicationContext());
75         setTheme(DictionaryApplication.INSTANCE.getSelectedTheme().themeId);
76
77         super.onCreate(savedInstanceState);
78         setContentView(R.layout.html_display_activity);
79
80         ActionBar actionBar = getSupportActionBar();
81         actionBar.setDisplayHomeAsUpEnabled(true);
82
83         final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
84         String html;
85         if (htmlRes != -1) {
86             InputStream res = getResources().openRawResource(htmlRes);
87             html = StringUtil.readToString(res);
88             try {
89                 res.close();
90             } catch (IOException ignored) {
91             }
92         } else {
93             html = getIntent().getStringExtra(HTML);
94         }
95         final MyWebView webView = (MyWebView) findViewById(R.id.webView);
96         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
97         final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");
98         int fontSizeSp;
99         try {
100             fontSizeSp = Integer.parseInt(fontSize.trim());
101         } catch (NumberFormatException e) {
102             fontSizeSp = 14;
103         }
104         webView.getSettings().setDefaultFontSize(fontSizeSp);
105         // No way to get pure UTF-8 data into WebView
106         html = Base64.encodeToString(html.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
107         // Use loadURL to allow specifying a charset
108         webView.loadUrl("data:text/html;charset=utf-8;base64," + html);
109         webView.activity = this;
110
111         final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
112         if (textToHighlight != null && !"".equals(textToHighlight)) {
113             Log.d(LOG, "NOT Highlighting text: " + textToHighlight);
114             // This isn't working:
115             // webView.findAll(textToHighlight);
116             // webView.showFindDialog(textToHighlight, false);
117         }
118
119         final Button okButton = (Button) findViewById(R.id.okButton);
120         if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
121             okButton.setVisibility(Button.GONE);
122         }
123     }
124
125     @Override
126     public void onBackPressed() {
127         final MyWebView webView = (MyWebView)findViewById(R.id.webView);
128         if (webView.canGoBack()) webView.goBack();
129         else super.onBackPressed();
130     }
131
132     @Override
133     public boolean onOptionsItemSelected(MenuItem item) {
134         // Explicitly handle the up button press so
135         // we return to the dictionary.
136         if (item.getItemId() == android.R.id.home) {
137             finish();
138             return true;
139         }
140         return super.onOptionsItemSelected(item);
141     }
142 }