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