1 // Copyright 2011 Google Inc. All Rights Reserved.
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
15 package com.hughes.android.dictionary;
\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
29 import com.hughes.util.StringUtil;
\r
31 import java.io.UnsupportedEncodingException;
\r
33 public final class HtmlDisplayActivity extends ActionBarActivity {
\r
35 static final String LOG = "QuickDic";
\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
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
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
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
63 /** Called when the activity is first created. */
\r
65 public void onCreate(final Bundle savedInstanceState) {
\r
66 setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
\r
68 super.onCreate(savedInstanceState);
\r
69 setContentView(R.layout.html_display_activity);
\r
71 ActionBar actionBar = getSupportActionBar();
\r
72 actionBar.setDisplayHomeAsUpEnabled(true);
\r
74 final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
\r
76 if (htmlRes != -1) {
\r
77 html = StringUtil.readToString(getResources().openRawResource(htmlRes));
\r
79 html = getIntent().getStringExtra(HTML);
\r
81 final MyWebView webView = (MyWebView) findViewById(R.id.webView);
\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
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
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
100 final Button okButton = (Button) findViewById(R.id.okButton);
\r
101 okButton.setOnClickListener(new OnClickListener() {
\r
103 public void onClick(View v) {
\r
107 if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
\r
108 okButton.setVisibility(Button.GONE);
\r
113 public boolean onOptionsItemSelected(MenuItem item) {
\r
114 // Explicitly handle the up button press so
\r
115 // we return to the dictionary.
\r
116 if (item.getItemId() == android.R.id.home)
\r
121 return super.onOptionsItemSelected(item);
\r