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