Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / WebsiteSettingsPopupLegacy.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.TextUtils;
14 import android.view.LayoutInflater;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.view.ViewGroup;
18 import android.view.Window;
19 import android.widget.Button;
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.ApiCompatibilityUtils;
26 import org.chromium.base.CalledByNative;
27 import org.chromium.chrome.R;
28 import org.chromium.content.browser.WebContentsObserver;
29 import org.chromium.content_public.browser.WebContents;
30
31 import java.net.URISyntaxException;
32
33 /**
34  * Java side of Android implementation of the website settings UI.
35  */
36 public class WebsiteSettingsPopupLegacy implements OnClickListener {
37     private static final String HELP_URL =
38             "http://www.google.com/support/chrome/bin/answer.py?answer=95617";
39     private static final int DESCRIPTION_TEXT_SIZE_SP = 12;
40     private final Context mContext;
41     private final Dialog mDialog;
42     private final LinearLayout mContainer;
43     private final WebContents mWebContents;
44     private final int mPaddingWide, mPaddingThin;
45     private final long mNativeWebsiteSettingsPopupLegacy;
46     private TextView mCertificateViewer, mMoreInfoLink;
47     private ViewGroup mCertificateLayout, mDescriptionLayout;
48     private Button mResetCertDecisionsButton;
49     private String mLinkUrl;
50
51     private WebsiteSettingsPopupLegacy(Context context, WebContents webContents) {
52         mContext = context;
53         mWebContents = webContents;
54
55         mContainer = new LinearLayout(mContext);
56         mContainer.setOrientation(LinearLayout.VERTICAL);
57         mContainer.setBackgroundColor(Color.WHITE);
58         mPaddingWide = (int) context.getResources().getDimension(
59                 R.dimen.certificate_viewer_padding_wide);
60         mPaddingThin = (int) context.getResources().getDimension(
61                 R.dimen.certificate_viewer_padding_thin);
62         mContainer.setPadding(mPaddingWide, mPaddingWide + mPaddingThin, mPaddingWide,
63                 mPaddingWide);
64
65         mDialog = new Dialog(mContext);
66         mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
67         mDialog.setCanceledOnTouchOutside(true);
68         // This needs to come after other member initialization.
69         mNativeWebsiteSettingsPopupLegacy = nativeInit(this, webContents);
70         final WebContentsObserver webContentsObserver =
71                 new WebContentsObserver(mWebContents) {
72             @Override
73             public void navigationEntryCommitted() {
74                 // If a navigation is committed (e.g. from in-page redirect), the data we're
75                 // showing is stale so dismiss the dialog.
76                 mDialog.dismiss();
77             }
78         };
79         mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
80             @Override
81             public void onDismiss(DialogInterface dialog) {
82                 assert mNativeWebsiteSettingsPopupLegacy != 0;
83                 webContentsObserver.detachFromWebContents();
84                 nativeDestroy(mNativeWebsiteSettingsPopupLegacy);
85             }
86         });
87     }
88
89     /**
90      * Adds certificate section, which contains an icon, a headline, a
91      * description and a label for certificate info link.
92      */
93     @CalledByNative
94     private void addCertificateSection(int enumeratedIconId, String headline, String description,
95             String label) {
96         View section = addSection(enumeratedIconId, headline, description);
97         assert mCertificateLayout == null;
98         mCertificateLayout = (ViewGroup) section.findViewById(R.id.website_settings_text_layout);
99         if (label != null && !label.isEmpty()) {
100             setCertificateViewer(label);
101         }
102     }
103
104     /**
105      * Adds Description section, which contains an icon, a headline, and a
106      * description. Most likely headline for description is empty
107      */
108     @CalledByNative
109     private void addDescriptionSection(int enumeratedIconId, String headline, String description) {
110         View section = addSection(enumeratedIconId, headline, description);
111         assert mDescriptionLayout == null;
112         mDescriptionLayout = (ViewGroup) section.findViewById(R.id.website_settings_text_layout);
113     }
114
115     private View addSection(int enumeratedIconId, String headline, String description) {
116         View section = LayoutInflater.from(mContext).inflate(R.layout.website_settings_legacy,
117                 null);
118         ImageView i = (ImageView) section.findViewById(R.id.website_settings_icon);
119         int drawableId = ResourceId.mapToDrawableId(enumeratedIconId);
120         i.setImageResource(drawableId);
121
122         TextView h = (TextView) section.findViewById(R.id.website_settings_headline);
123         h.setText(headline);
124         if (TextUtils.isEmpty(headline)) h.setVisibility(View.GONE);
125
126         TextView d = (TextView) section.findViewById(R.id.website_settings_description);
127         d.setText(description);
128         d.setTextSize(DESCRIPTION_TEXT_SIZE_SP);
129         if (TextUtils.isEmpty(description)) d.setVisibility(View.GONE);
130
131         mContainer.addView(section);
132         return section;
133     }
134
135     private void setCertificateViewer(String label) {
136         assert mCertificateViewer == null;
137         mCertificateViewer = new TextView(mContext);
138         mCertificateViewer.setText(label);
139         mCertificateViewer.setTextColor(
140                 mContext.getResources().getColor(R.color.website_settings_popup_text_link));
141         mCertificateViewer.setTextSize(DESCRIPTION_TEXT_SIZE_SP);
142         mCertificateViewer.setOnClickListener(this);
143         mCertificateViewer.setPadding(0, mPaddingWide, 0, mPaddingWide);
144         mCertificateLayout.addView(mCertificateViewer);
145     }
146
147     @CalledByNative
148     private void addResetCertDecisionsButton(String label) {
149         assert mNativeWebsiteSettingsPopupLegacy != 0;
150         assert mResetCertDecisionsButton == null;
151
152         mResetCertDecisionsButton = new Button(mContext);
153         mResetCertDecisionsButton.setText(label);
154         ApiCompatibilityUtils.setBackgroundForView(mResetCertDecisionsButton,
155                 mContext.getResources().getDrawable(
156                         R.drawable.website_settings_reset_cert_decisions));
157         mResetCertDecisionsButton.setTextColor(
158                 mContext.getResources().getColor(
159                 R.color.website_settings_popup_reset_cert_decisions_button));
160         mResetCertDecisionsButton.setTextSize(DESCRIPTION_TEXT_SIZE_SP);
161         mResetCertDecisionsButton.setOnClickListener(this);
162
163         LinearLayout container = new LinearLayout(mContext);
164         container.setOrientation(LinearLayout.VERTICAL);
165         container.addView(mResetCertDecisionsButton);
166         container.setPadding(0, 0, 0, mPaddingWide);
167         mContainer.addView(container);
168     }
169
170     @CalledByNative
171     private void addMoreInfoLink(String linkText) {
172         addUrl(linkText, HELP_URL);
173     }
174
175     /** Adds a section containing a description and a hyperlink. */
176     private void addUrl(String label, String url) {
177         mMoreInfoLink = new TextView(mContext);
178         mLinkUrl = url;
179         mMoreInfoLink.setText(label);
180         mMoreInfoLink.setTextColor(
181                 mContext.getResources().getColor(R.color.website_settings_popup_text_link));
182         mMoreInfoLink.setTextSize(DESCRIPTION_TEXT_SIZE_SP);
183         mMoreInfoLink.setPadding(0, mPaddingWide + mPaddingThin, 0, mPaddingWide);
184         mMoreInfoLink.setOnClickListener(this);
185         mDescriptionLayout.addView(mMoreInfoLink);
186     }
187
188     /** Displays the WebsiteSettingsPopupLegacy. */
189     @CalledByNative
190     private void showDialog() {
191         ScrollView scrollView = new ScrollView(mContext);
192         scrollView.addView(mContainer);
193         mDialog.addContentView(scrollView,
194                 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
195                         LinearLayout.LayoutParams.MATCH_PARENT));
196         mDialog.show();
197     }
198
199     @Override
200     public void onClick(View v) {
201         mDialog.dismiss();
202         if (mResetCertDecisionsButton == v) {
203             nativeResetCertDecisions(mNativeWebsiteSettingsPopupLegacy, mWebContents);
204         } else if (mCertificateViewer == v) {
205             byte[][] certChain = nativeGetCertificateChain(mWebContents);
206             if (certChain == null) {
207                 // The WebContents may have been destroyed/invalidated. If so,
208                 // ignore this request.
209                 return;
210             }
211             CertificateViewer.showCertificateChain(mContext, certChain);
212         } else if (mMoreInfoLink == v) {
213             try {
214                 Intent i = Intent.parseUri(mLinkUrl, Intent.URI_INTENT_SCHEME);
215                 i.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true);
216                 i.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName());
217                 mContext.startActivity(i);
218             } catch (URISyntaxException ex) {
219                 // Do nothing intentionally.
220             }
221         }
222     }
223
224     /**
225      * Shows a WebsiteSettings dialog for the provided WebContents.
226      *
227      * The popup adds itself to the view hierarchy which owns the reference while it's
228      * visible.
229      *
230      * @param context Context which is used for launching a dialog.
231      * @param webContents The WebContents for which to show Website information. This
232      *         information is retrieved for the visible entry.
233      */
234     @SuppressWarnings("unused")
235     public static void show(Context context, WebContents webContents) {
236         new WebsiteSettingsPopupLegacy(context, webContents);
237     }
238
239     private static native long nativeInit(WebsiteSettingsPopupLegacy popup,
240             WebContents webContents);
241     private native void nativeDestroy(long nativeWebsiteSettingsPopupLegacyAndroid);
242     private native void nativeResetCertDecisions(
243             long nativeWebsiteSettingsPopupLegacyAndroid, WebContents webContents);
244     private native byte[][] nativeGetCertificateChain(WebContents webContents);
245 }