]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/HtmlDisplayActivity.java
Prefer XML click handling setup.
[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.os.Bundle;
20 import android.support.v7.app.ActionBar;
21 import android.support.v7.app.ActionBarActivity;
22 import android.util.Base64;
23 import android.util.Log;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.widget.Button;
28
29 import com.hughes.util.StringUtil;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.UnsupportedEncodingException;
34
35 public final class HtmlDisplayActivity extends ActionBarActivity {
36
37     static final String LOG = "QuickDic";
38
39     static final String HTML_RES = "html_res";
40     static final String HTML = "html";
41     static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
42     static final String SHOW_OK_BUTTON = "showOKButton";
43
44     public static Intent getHelpLaunchIntent(Context c) {
45         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
46         intent.putExtra(HTML_RES, R.raw.help);
47         return intent;
48     }
49
50     public static Intent getWhatsNewLaunchIntent(Context c) {
51         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
52         intent.putExtra(HTML_RES, R.raw.whats_new);
53         return intent;
54     }
55
56     public static Intent getHtmlIntent(Context c, final String html, final String textToHighlight,
57                                        final boolean showOkButton) {
58         final Intent intent = new Intent(c, HtmlDisplayActivity.class);
59         intent.putExtra(HTML, html);
60         intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight);
61         intent.putExtra(SHOW_OK_BUTTON, showOkButton);
62         return intent;
63     }
64
65     public void onOkClick(View dummy) {
66         finish();
67     }
68
69     /** Called when the activity is first created. */
70     @Override
71     public void onCreate(final Bundle savedInstanceState) {
72         setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
73
74         super.onCreate(savedInstanceState);
75         setContentView(R.layout.html_display_activity);
76
77         ActionBar actionBar = getSupportActionBar();
78         actionBar.setDisplayHomeAsUpEnabled(true);
79
80         final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
81         String html;
82         if (htmlRes != -1) {
83             InputStream res = getResources().openRawResource(htmlRes);
84             html = StringUtil.readToString(res);
85             try {
86                 res.close();
87             } catch (IOException e) {
88             }
89         } else {
90             html = getIntent().getStringExtra(HTML);
91         }
92         final MyWebView webView = (MyWebView) findViewById(R.id.webView);
93         try {
94             // No way to get pure UTF-8 data into WebView
95             html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT);
96         } catch (UnsupportedEncodingException e) {
97             throw new RuntimeException("Missing UTF-8 support?!", e);
98         }
99         // Use loadURL to allow specifying a charset
100         webView.loadUrl("data:text/html;charset=utf-8;base64," + html);
101         webView.activity = this;
102
103         final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
104         if (textToHighlight != null && !"".equals(textToHighlight)) {
105             Log.d(LOG, "NOT Highlighting text: " + textToHighlight);
106             // This isn't working:
107             // webView.findAll(textToHighlight);
108             // webView.showFindDialog(textToHighlight, false);
109         }
110
111         final Button okButton = (Button) findViewById(R.id.okButton);
112         if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
113             okButton.setVisibility(Button.GONE);
114         }
115     }
116
117     @Override
118     public void onBackPressed() {
119         final MyWebView webView = (MyWebView)findViewById(R.id.webView);
120         if (webView.canGoBack()) webView.goBack();
121         else super.onBackPressed();
122     }
123
124     @Override
125     public boolean onOptionsItemSelected(MenuItem item) {
126         // Explicitly handle the up button press so
127         // we return to the dictionary.
128         if (item.getItemId() == android.R.id.home) {
129             finish();
130             return true;
131         }
132         return super.onOptionsItemSelected(item);
133     }
134 }