887ea4a3047adc5958d18360f800856810c73741
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / WebsiteSettingsPopup.java
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.
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.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;
24
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;
29
30 import java.net.URISyntaxException;
31
32 /**
33  * Java side of Android implementation of the website settings UI.
34  */
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;
45
46     private WebsiteSettingsPopup(Context context, WebContents webContents) {
47         mContext = context;
48         mWebContents = webContents;
49
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);
54
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) {
62             @Override
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.
66                 mDialog.dismiss();
67             }
68         };
69         mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
70             @Override
71             public void onDismiss(DialogInterface dialog) {
72                 assert nativeWebsiteSettingsPopup != 0;
73                 webContentsObserver.detachFromWebContents();
74                 nativeDestroy(nativeWebsiteSettingsPopup);
75             }
76         });
77     }
78
79     /** Adds a section, which contains an icon, a headline, and a description. */
80     @CalledByNative
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);
86
87         TextView h = (TextView) section.findViewById(R.id.website_settings_headline);
88         h.setText(headline);
89         if (TextUtils.isEmpty(headline)) h.setVisibility(View.GONE);
90
91         TextView d = (TextView) section.findViewById(R.id.website_settings_description);
92         d.setText(description);
93         if (TextUtils.isEmpty(description)) d.setVisibility(View.GONE);
94
95         mContainer.addView(section);
96     }
97
98     /** Adds a horizontal dividing line to separate sections. */
99     @CalledByNative
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);
106     }
107
108     @CalledByNative
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);
116     }
117
118     @CalledByNative
119     private void addMoreInfoLink(String linkText) {
120         addUrl(linkText, HELP_URL);
121     }
122
123     /** Adds a section containing a description and a hyperlink. */
124     private void addUrl(String label, String url) {
125         mMoreInfoLink = new TextView(mContext);
126         mLinkUrl = url;
127         mMoreInfoLink.setText(Html.fromHtml("<a href='#'>" + label + "</a>"));
128         mMoreInfoLink.setPadding(0, mPadding, 0, mPadding);
129         mMoreInfoLink.setOnClickListener(this);
130         mContainer.addView(mMoreInfoLink);
131     }
132
133     /** Displays the WebsiteSettingsPopup. */
134     @CalledByNative
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));
141         mDialog.show();
142     }
143
144     @Override
145     public void onClick(View v) {
146         mDialog.dismiss();
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.
152                 return;
153             }
154             CertificateViewer.showCertificateChain(mContext, certChain);
155         } else if (mMoreInfoLink == v) {
156             try {
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) {}
162         }
163     }
164
165     /**
166      * Shows a WebsiteSettings dialog for the provided WebContents.
167      *
168      * The popup adds itself to the view hierarchy which owns the reference while it's
169      * visible.
170      *
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.
174      */
175     @SuppressWarnings("unused")
176     public static void show(Context context, WebContents webContents) {
177         new WebsiteSettingsPopup(context, webContents);
178     }
179
180     private static native long nativeInit(WebsiteSettingsPopup popup, WebContents webContents);
181     private native void nativeDestroy(long nativeWebsiteSettingsPopupAndroid);
182     private native byte[][] nativeGetCertificateChain(WebContents webContents);
183 }