// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.hughes.android.dictionary;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.hughes.util.StringUtil;

public final class HtmlDisplayActivity extends ActionBarActivity {

    static final String LOG = "QuickDic";

    static final String HTML_RES = "html_res";
    static final String HTML = "html";
    static final String TEXT_TO_HIGHLIGHT = "textToHighlight";
    static final String SHOW_OK_BUTTON = "showOKButton";

    public static Intent getHelpLaunchIntent(Context c) {
        final Intent intent = new Intent(c, HtmlDisplayActivity.class);
        intent.putExtra(HTML_RES, R.raw.help);
        return intent;
    }

    public static Intent getWhatsNewLaunchIntent(Context c) {
        final Intent intent = new Intent(c, HtmlDisplayActivity.class);
        intent.putExtra(HTML_RES, R.raw.whats_new);
        return intent;
    }

    public static Intent getHtmlIntent(Context c, final String html, final String textToHighlight,
            final boolean showOkButton) {
        final Intent intent = new Intent(c, HtmlDisplayActivity.class);
        intent.putExtra(HTML, html);
        intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight);
        intent.putExtra(SHOW_OK_BUTTON, showOkButton);
        return intent;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.html_display_activity);
        
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);
        final String html;
        if (htmlRes != -1) {
            html = StringUtil.readToString(getResources().openRawResource(htmlRes));
        } else {
            html = getIntent().getStringExtra(HTML);
        }
        final MyWebView webView = (MyWebView) findViewById(R.id.webView);
        webView.loadData(html, "text/html", "utf-8");
        webView.activity = this;

        final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);
        if (textToHighlight != null && !"".equals(textToHighlight)) {
            Log.d(LOG, "NOT Highlighting text: " + textToHighlight);
            // This isn't working:
            // webView.findAll(textToHighlight);
            // webView.showFindDialog(textToHighlight, false);
        }

        final Button okButton = (Button) findViewById(R.id.okButton);
        okButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {
            okButton.setVisibility(Button.GONE);
        }
    }

}

@@ -843,16 +843,45 @@
 ivity;%0D%0A
+import android.util.Base64;%0D%0A
 import a
@@ -1039,16 +1039,64 @@
 til;%0D%0A%0D%0A
+import java.io.UnsupportedEncodingException;%0D%0A%0D%0A
 public f
@@ -2769,30 +2769,24 @@
 );%0D%0A        
-final 
 String html;
@@ -2787,16 +2787,16 @@
  html;%0D%0A
+
         
@@ -3073,51 +3073,387 @@
     
-webView.loadData(html, %22text/html%22, %22utf-8%22
+try %7B%0D%0A            // No way to get pure UTF-8 data into WebView%0D%0A            html = Base64.encodeToString(html.getBytes(%22UTF-8%22), Base64.DEFAULT);%0D%0A        %7D catch (UnsupportedEncodingException e) %7B%0D%0A            throw new RuntimeException(e);%0D%0A        %7D%0D%0A        // Use loadURL to allow specifying a charset%0D%0A        webView.loadUrl(%22data:text/html;charset=utf-8;base64,%22 + html
 );%0D%0A

Make loading data into WebView more correct.