1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.dialog
;
19 import java
.text
.DateFormat
;
20 import java
.util
.Date
;
22 import com
.owncloud
.android
.R
;
23 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
25 import android
.app
.Activity
;
26 import android
.app
.Dialog
;
27 import android
.net
.http
.SslCertificate
;
28 import android
.net
.http
.SslError
;
29 import android
.os
.Bundle
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.View
.OnClickListener
;
33 import android
.view
.ViewGroup
;
34 import android
.view
.Window
;
35 import android
.webkit
.SslErrorHandler
;
36 import android
.webkit
.WebView
;
37 import android
.widget
.Button
;
38 import android
.widget
.TextView
;
41 * Dialog to show an Untrusted Certificate
44 * @author David A. Velasco
46 public class SslUntrustedCertDialogForEmptySslError
extends SslUntrustedCertDialogABSTRACT
{
48 //private final static String TAG = SslUntrustedCertDialogForEmptySslError.class.getSimpleName();
50 private SslError mError
;
51 private SslErrorHandler mHandler
;
58 * @param error Error occurred; details about it will be shown in the dialog.
59 * @param handler Handler to indicate to the {@link WebView} where the error was found what to do next.
62 public static SslUntrustedCertDialogForEmptySslError
newInstance(SslError error
, SslErrorHandler handler
) {
63 return new SslUntrustedCertDialogForEmptySslError(error
, handler
);
70 * Required by Android framework. Never used, since the state is retained; see {@link #onCreate(Bundle)}
72 public SslUntrustedCertDialogForEmptySslError() {}
76 * Private constructor.
78 * Used by the factory method {@link #newInstance(SslError, SslErrorHandler)}.
80 * @param error Error occurred; details about it will be shown in the dialog.
81 * @param handler Handler to indicate to the {@link WebView} where the error was found what to do next.
83 private SslUntrustedCertDialogForEmptySslError(SslError error
, SslErrorHandler handler
) {
90 public void onAttach(Activity activity
) {
91 super.onAttach(activity
);
92 /*if (!(activity instanceof OnSslUntrustedCertListener)) {
93 throw new IllegalArgumentException("Trying to attach to an Activity not implementing " + OnSslUntrustedCertListener.class.getCanonicalName());
98 // TODO try to move to the parent class ?
100 public void onCreate(Bundle savedInstanceState
) {
101 super.onCreate(savedInstanceState
);
102 setRetainInstance(true
); // force to keep the state of the fragment on configuration changes (such as device rotations)
103 setCancelable(false
);
107 // try to move to the parent class ?
109 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
110 // Create a view by inflating desired layout
112 mView
= inflater
.inflate(R
.layout
.ssl_untrusted_cert_layout
, container
, false
);
114 ((ViewGroup
)mView
.getParent()).removeView(mView
);
117 Button ok
= (Button
) mView
.findViewById(R
.id
.untrusted_ok
);
118 ok
.setOnClickListener(new OnClickListener() {
121 public void onClick(View v
) {
122 //AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
128 Button cancel
= (Button
) mView
.findViewById(R
.id
.untrusted_cancel
);
129 cancel
.setOnClickListener(new OnClickListener() {
132 public void onClick(View v
) {
133 AuthenticatorActivity act
= ((AuthenticatorActivity
)getSherlockActivity());
134 getDialog().cancel();
140 Button details
= (Button
) mView
.findViewById(R
.id
.untrusted_details_btn
);
141 details
.setOnClickListener(new OnClickListener() {
143 public void onClick(View v
) {
144 View detailsScroll
= mView
.findViewById(R
.id
.untrusted_details_scroll
);
145 if (detailsScroll
.getVisibility() == View
.VISIBLE
) {
146 detailsScroll
.setVisibility(View
.GONE
);
147 ((Button
) v
).setText(R
.string
.ssl_validator_btn_details_see
);
150 detailsScroll
.setVisibility(View
.VISIBLE
);
151 ((Button
) v
).setText(R
.string
.ssl_validator_btn_details_hide
);
152 showCertificateData();
161 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
162 final Dialog dialog
= super.onCreateDialog(savedInstanceState
);
163 dialog
.requestWindowFeature(Window
.FEATURE_NO_TITLE
);
168 public void onDestroyView() {
169 if (getDialog() != null
&& getRetainInstance())
170 getDialog().setDismissMessage(null
);
171 super.onDestroyView();
174 private void showCertificateData() {
175 TextView nullCerView
= (TextView
) mView
.findViewById(R
.id
.untrusted_null_cert
);
176 SslCertificate cert
= mError
.getCertificate();
178 nullCerView
.setVisibility(View
.GONE
);
179 showSubject(cert
.getIssuedTo());
180 showIssuer(cert
.getIssuedBy());
181 showValidity(cert
.getValidNotBeforeDate(), cert
.getValidNotAfterDate());
185 nullCerView
.setVisibility(View
.VISIBLE
);
189 private void showValidity(Date notBefore
, Date notAfter
) {
190 TextView fromView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_validity_from
));
191 TextView toView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_validity_to
));
192 DateFormat dateFormat
= DateFormat
.getDateInstance();
193 fromView
.setText(dateFormat
.format(notBefore
));
194 toView
.setText(dateFormat
.format(notAfter
));
198 private void showSubject(SslCertificate
.DName subject
) {
199 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_CN
));
200 cnView
.setText(subject
.getCName());
201 cnView
.setVisibility(View
.VISIBLE
);
203 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_O
));
204 oView
.setText(subject
.getOName());
205 oView
.setVisibility(View
.VISIBLE
);
207 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_OU
));
208 ouView
.setText(subject
.getUName());
209 ouView
.setVisibility(View
.VISIBLE
);
211 // SslCertificates don't offer this information
212 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_C
)).setVisibility(View
.GONE
);
213 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_ST
)).setVisibility(View
.GONE
);
214 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_subject_L
)).setVisibility(View
.GONE
);
215 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_subject_C
)).setVisibility(View
.GONE
);
216 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_subject_ST
)).setVisibility(View
.GONE
);
217 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_subject_L
)).setVisibility(View
.GONE
);
221 private void showIssuer(SslCertificate
.DName issuer
) {
222 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_CN
));
223 cnView
.setText(issuer
.getCName());
224 cnView
.setVisibility(View
.VISIBLE
);
226 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_O
));
227 oView
.setText(issuer
.getOName());
228 oView
.setVisibility(View
.VISIBLE
);
230 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_OU
));
231 ouView
.setText(issuer
.getUName());
232 ouView
.setVisibility(View
.VISIBLE
);
234 // SslCertificates don't offer this information
235 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_C
)).setVisibility(View
.GONE
);
236 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_ST
)).setVisibility(View
.GONE
);
237 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_issuer_L
)).setVisibility(View
.GONE
);
238 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_issuer_C
)).setVisibility(View
.GONE
);
239 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_issuer_ST
)).setVisibility(View
.GONE
);
240 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_issuer_L
)).setVisibility(View
.GONE
);
243 private void hideSignature() {
244 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_signature
)).setVisibility(View
.GONE
);
245 ((TextView
)mView
.findViewById(R
.id
.untrusted_label_signature_algorithm
)).setVisibility(View
.GONE
);
246 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_signature_algorithm
)).setVisibility(View
.GONE
);
247 ((TextView
)mView
.findViewById(R
.id
.untrusted_value_signature
)).setVisibility(View
.GONE
);