1 // Copyright 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.
5 package org.chromium.chrome.browser;
7 import android.app.Dialog;
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.graphics.Color;
12 import android.provider.Browser;
13 import android.text.Html;
14 import android.text.TextUtils;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.View.OnClickListener;
18 import android.view.ViewGroup.LayoutParams;
19 import android.view.Window;
20 import android.widget.ImageView;
21 import android.widget.LinearLayout;
22 import android.widget.ScrollView;
23 import android.widget.TextView;
25 import org.chromium.base.CalledByNative;
26 import org.chromium.chrome.R;
27 import org.chromium.content.browser.WebContentsObserverAndroid;
28 import org.chromium.content_public.browser.WebContents;
30 import java.net.URISyntaxException;
33 * Java side of Android implementation of the website settings UI.
35 public 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 WebContents mWebContents;
42 private final int mPadding;
43 private TextView mCertificateViewer, mMoreInfoLink;
44 private String mLinkUrl;
46 private WebsiteSettingsPopup(Context context, WebContents webContents) {
48 mWebContents = webContents;
50 mContainer = new LinearLayout(mContext);
51 mContainer.setOrientation(LinearLayout.VERTICAL);
52 mPadding = (int) context.getResources().getDimension(R.dimen.certificate_viewer_padding);
53 mContainer.setPadding(mPadding, 0, mPadding, 0);
55 mDialog = new Dialog(mContext);
56 mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
57 mDialog.setCanceledOnTouchOutside(true);
58 // This needs to come after other member initialization.
59 final long nativeWebsiteSettingsPopup = nativeInit(this, webContents);
60 final WebContentsObserverAndroid webContentsObserver =
61 new WebContentsObserverAndroid(mWebContents) {
63 public void navigationEntryCommitted() {
64 // If a navigation is committed (e.g. from in-page redirect), the data we're
65 // showing is stale so dismiss the dialog.
69 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
71 public void onDismiss(DialogInterface dialog) {
72 assert nativeWebsiteSettingsPopup != 0;
73 webContentsObserver.detachFromWebContents();
74 nativeDestroy(nativeWebsiteSettingsPopup);
79 /** Adds a section, which contains an icon, a headline, and a description. */
81 private void addSection(int enumeratedIconId, String headline, String description) {
82 View section = LayoutInflater.from(mContext).inflate(R.layout.website_settings, null);
83 ImageView i = (ImageView) section.findViewById(R.id.website_settings_icon);
84 int drawableId = ResourceId.mapToDrawableId(enumeratedIconId);
85 i.setImageResource(drawableId);
87 TextView h = (TextView) section.findViewById(R.id.website_settings_headline);
89 if (TextUtils.isEmpty(headline)) h.setVisibility(View.GONE);
91 TextView d = (TextView) section.findViewById(R.id.website_settings_description);
92 d.setText(description);
93 if (TextUtils.isEmpty(description)) d.setVisibility(View.GONE);
95 mContainer.addView(section);
98 /** Adds a horizontal dividing line to separate sections. */
100 private void addDivider() {
101 View divider = new View(mContext);
102 final int dividerHeight = (int) (2 * mContext.getResources().getDisplayMetrics().density);
103 divider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dividerHeight));
104 divider.setBackgroundColor(Color.GRAY);
105 mContainer.addView(divider);
109 private void setCertificateViewer(String label) {
110 assert mCertificateViewer == null;
111 mCertificateViewer = new TextView(mContext);
112 mCertificateViewer.setText(Html.fromHtml("<a href='#'>" + label + "</a>"));
113 mCertificateViewer.setOnClickListener(this);
114 mCertificateViewer.setPadding(0, 0, 0, mPadding);
115 mContainer.addView(mCertificateViewer);
119 private void addMoreInfoLink(String linkText) {
120 addUrl(linkText, HELP_URL);
123 /** Adds a section containing a description and a hyperlink. */
124 private void addUrl(String label, String url) {
125 mMoreInfoLink = new TextView(mContext);
127 mMoreInfoLink.setText(Html.fromHtml("<a href='#'>" + label + "</a>"));
128 mMoreInfoLink.setPadding(0, mPadding, 0, mPadding);
129 mMoreInfoLink.setOnClickListener(this);
130 mContainer.addView(mMoreInfoLink);
133 /** Displays the WebsiteSettingsPopup. */
135 private void showDialog() {
136 ScrollView scrollView = new ScrollView(mContext);
137 scrollView.addView(mContainer);
138 mDialog.addContentView(scrollView,
139 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
140 LinearLayout.LayoutParams.MATCH_PARENT));
145 public void onClick(View v) {
147 if (mCertificateViewer == v) {
148 byte[][] certChain = nativeGetCertificateChain(mWebContents);
149 if (certChain == null) {
150 // The WebContents may have been destroyed/invalidated. If so,
151 // ignore this request.
154 CertificateViewer.showCertificateChain(mContext, certChain);
155 } else if (mMoreInfoLink == v) {
157 Intent i = Intent.parseUri(mLinkUrl, Intent.URI_INTENT_SCHEME);
158 i.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true);
159 i.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName());
160 mContext.startActivity(i);
161 } catch (URISyntaxException ex) {}
166 * Shows a WebsiteSettings dialog for the provided WebContents.
168 * The popup adds itself to the view hierarchy which owns the reference while it's
171 * @param context Context which is used for launching a dialog.
172 * @param webContents The WebContents for which to show Website information. This
173 * information is retrieved for the visible entry.
175 @SuppressWarnings("unused")
176 public static void show(Context context, WebContents webContents) {
177 new WebsiteSettingsPopup(context, webContents);
180 private static native long nativeInit(WebsiteSettingsPopup popup, WebContents webContents);
181 private native void nativeDestroy(long nativeWebsiteSettingsPopupAndroid);
182 private native byte[][] nativeGetCertificateChain(WebContents webContents);