1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary;
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;
29 import com.hughes.util.StringUtil;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.UnsupportedEncodingException;
35 public final class HtmlDisplayActivity extends ActionBarActivity {
37 static final String LOG = "QuickDic";
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";
44 public static Intent getHelpLaunchIntent(Context c) {
45 final Intent intent = new Intent(c, HtmlDisplayActivity.class);
46 intent.putExtra(HTML_RES, R.raw.help);
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);
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);
65 /** Called when the activity is first created. */
67 public void onCreate(final Bundle savedInstanceState) {
68 setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
70 super.onCreate(savedInstanceState);
71 setContentView(R.layout.html_display_activity);
73 ActionBar actionBar = getSupportActionBar();
74 actionBar.setDisplayHomeAsUpEnabled(true);
76 final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
79 InputStream res = getResources().openRawResource(htmlRes);
80 html = StringUtil.readToString(res);
83 } catch (IOException e) {
86 html = getIntent().getStringExtra(HTML);
88 final MyWebView webView = (MyWebView) findViewById(R.id.webView);
90 // No way to get pure UTF-8 data into WebView
91 html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT);
92 } catch (UnsupportedEncodingException e) {
93 throw new RuntimeException("Missing UTF-8 support?!", e);
95 // Use loadURL to allow specifying a charset
96 webView.loadUrl("data:text/html;charset=utf-8;base64," + html);
97 webView.activity = this;
99 final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
100 if (textToHighlight != null && !"".equals(textToHighlight)) {
101 Log.d(LOG, "NOT Highlighting text: " + textToHighlight);
102 // This isn't working:
103 // webView.findAll(textToHighlight);
104 // webView.showFindDialog(textToHighlight, false);
107 final Button okButton = (Button) findViewById(R.id.okButton);
108 okButton.setOnClickListener(new OnClickListener() {
110 public void onClick(View v) {
114 if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
115 okButton.setVisibility(Button.GONE);
120 public void onBackPressed() {
121 final MyWebView webView = (MyWebView)findViewById(R.id.webView);
122 if (webView.canGoBack()) webView.goBack();
123 else super.onBackPressed();
127 public boolean onOptionsItemSelected(MenuItem item) {
128 // Explicitly handle the up button press so
129 // we return to the dictionary.
130 if (item.getItemId() == android.R.id.home)
135 return super.onOptionsItemSelected(item);