| 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.chrome.browser; |
| 6 | |
| 7 | import android.app.Dialog; |
| 8 | import android.content.Context; |
| 9 | import android.content.DialogInterface; |
| 10 | import android.content.Intent; |
| 11 | import android.graphics.Bitmap; |
| 12 | import android.graphics.Color; |
| 13 | import android.provider.Browser; |
| 14 | import android.text.Html; |
| 15 | import android.text.TextUtils; |
| 16 | import android.view.LayoutInflater; |
| 17 | import android.view.View; |
| 18 | import android.view.View.OnClickListener; |
| 19 | import android.view.ViewGroup.LayoutParams; |
| 20 | import android.view.Window; |
| 21 | import android.widget.ImageView; |
| 22 | import android.widget.LinearLayout; |
| 23 | import android.widget.ScrollView; |
| 24 | import android.widget.TextView; |
| 25 | |
| 26 | import org.chromium.base.CalledByNative; |
| 27 | import org.chromium.chrome.R; |
| 28 | import org.chromium.content.browser.ContentViewCore; |
| 29 | |
| 30 | import java.net.URISyntaxException; |
| 31 | |
| 32 | /** |
| 33 | * Java side of Android implementation of the website settings UI. |
| 34 | */ |
| 35 | class WebsiteSettingsPopup implements OnClickListener { |
| 36 | private static final String HELP_URL = |
| 37 | "http://www.google.com/support/chrome/bin/answer.py?answer=95617"; |
| 38 | private final Context mContext; |
| 39 | private final Dialog mDialog; |
| 40 | private final LinearLayout mContainer; |
| 41 | private final ContentViewCore mContentViewCore; |
| 42 | private final int mPadding; |
| 43 | private TextView mCertificateViewer, mMoreInfoLink; |
| 44 | private String mLinkUrl; |
| 45 | |
| 46 | private WebsiteSettingsPopup(Context context, ContentViewCore contentViewCore, |
| 47 | final int nativeWebsiteSettingsPopup) { |
| 48 | mContext = context; |
| 49 | mDialog = new Dialog(mContext); |
| 50 | mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 51 | mDialog.setCanceledOnTouchOutside(true); |
| 52 | mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 53 | @Override |
| 54 | public void onCancel(DialogInterface dialogInterface) { |
| 55 | assert nativeWebsiteSettingsPopup != 0; |
| 56 | nativeDestroy(nativeWebsiteSettingsPopup); |
| 57 | } |
| 58 | }); |
| 59 | mContainer = new LinearLayout(mContext); |
| 60 | mContainer.setOrientation(LinearLayout.VERTICAL); |
| 61 | mContentViewCore = contentViewCore; |
| 62 | mPadding = (int) context.getResources().getDimension(R.dimen.certificate_viewer_padding); |
| 63 | mContainer.setPadding(mPadding, 0, mPadding, 0); |
| 64 | } |
| 65 | |
| 66 | /** Adds a section, which contains an icon, a headline, and a description. */ |
| 67 | @CalledByNative |
| 68 | private void addSection(int enumeratedIconId, String headline, String description) { |
| 69 | View section = LayoutInflater.from(mContext).inflate(R.layout.website_settings, null); |
| 70 | ImageView i = (ImageView) section.findViewById(R.id.website_settings_icon); |
| 71 | int drawableId = ResourceId.mapToDrawableId(enumeratedIconId); |
| 72 | i.setImageResource(drawableId); |
| 73 | |
| 74 | TextView h = (TextView) section.findViewById(R.id.website_settings_headline); |
| 75 | h.setText(headline); |
| 76 | if (TextUtils.isEmpty(headline)) h.setVisibility(View.GONE); |
| 77 | |
| 78 | TextView d = (TextView) section.findViewById(R.id.website_settings_description); |
| 79 | d.setText(description); |
| 80 | if (TextUtils.isEmpty(description)) d.setVisibility(View.GONE); |
| 81 | |
| 82 | mContainer.addView(section); |
| 83 | } |
| 84 | |
| 85 | /** Adds a horizontal dividing line to separate sections. */ |
| 86 | @CalledByNative |
| 87 | private void addDivider() { |
| 88 | View divider = new View(mContext); |
| 89 | final int dividerHeight = (int) (2 * mContext.getResources().getDisplayMetrics().density); |
| 90 | divider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dividerHeight)); |
| 91 | divider.setBackgroundColor(Color.GRAY); |
| 92 | mContainer.addView(divider); |
| 93 | } |
| 94 | |
| 95 | @CalledByNative |
| 96 | private void setCertificateViewer(String label) { |
| 97 | assert mCertificateViewer == null; |
| 98 | mCertificateViewer = new TextView(mContext); |
| 99 | mCertificateViewer.setText(Html.fromHtml("<a href='#'>" + label + "</a>")); |
| 100 | mCertificateViewer.setOnClickListener(this); |
| 101 | mCertificateViewer.setPadding(0, 0, 0, mPadding); |
| 102 | mContainer.addView(mCertificateViewer); |
| 103 | } |
| 104 | |
| 105 | @CalledByNative |
| 106 | private void addMoreInfoLink(String linkText) { |
| 107 | addUrl(linkText, HELP_URL); |
| 108 | } |
| 109 | |
| 110 | /** Adds a section containing a description and a hyperlink. */ |
| 111 | private void addUrl(String label, String url) { |
| 112 | mMoreInfoLink = new TextView(mContext); |
| 113 | mLinkUrl = url; |
| 114 | mMoreInfoLink.setText(Html.fromHtml("<a href='#'>" + label + "</a>")); |
| 115 | mMoreInfoLink.setPadding(0, mPadding, 0, mPadding); |
| 116 | mMoreInfoLink.setOnClickListener(this); |
| 117 | mContainer.addView(mMoreInfoLink); |
| 118 | } |
| 119 | |
| 120 | /** Displays the WebsiteSettingsPopup. */ |
| 121 | @CalledByNative |
| 122 | private void show() { |
| 123 | ScrollView scrollView = new ScrollView(mContext); |
| 124 | scrollView.addView(mContainer); |
| 125 | mDialog.addContentView(scrollView, |
| 126 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, |
| 127 | LinearLayout.LayoutParams.MATCH_PARENT)); |
| 128 | mDialog.show(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onClick(View v) { |
| 133 | mDialog.dismiss(); |
| 134 | if (mCertificateViewer == v) { |
| 135 | byte[][] certChain = nativeGetCertificateChain(mContentViewCore); |
| 136 | CertificateViewer.showCertificateChain(mContext, certChain); |
| 137 | } else if (mMoreInfoLink == v) { |
| 138 | try { |
| 139 | Intent i = Intent.parseUri(mLinkUrl, Intent.URI_INTENT_SCHEME); |
| 140 | i.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true); |
| 141 | i.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName()); |
| 142 | mContext.startActivity(i); |
| 143 | } catch (URISyntaxException ex) {} |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @CalledByNative |
| 148 | private static WebsiteSettingsPopup create(Context context, ContentViewCore contentViewCore, |
| 149 | int nativeWebsiteSettingsPopup) { |
| 150 | return new WebsiteSettingsPopup(context, contentViewCore, nativeWebsiteSettingsPopup); |
| 151 | } |
| 152 | |
| 153 | private native void nativeDestroy(int nativeWebsiteSettingsPopupAndroid); |
| 154 | private native byte[][] nativeGetCertificateChain(ContentViewCore contentViewCore); |
| 155 | } |