Merge branch 'develop' into check_server_certificates_in_SSO_webview
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SslUntrustedCertDialogForEmptySslError.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17 package com.owncloud.android.ui.dialog;
18
19 import java.text.DateFormat;
20 import java.util.Date;
21
22 import com.owncloud.android.R;
23 import com.owncloud.android.authentication.AuthenticatorActivity;
24
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;
39
40 /**
41 * Dialog to show an Untrusted Certificate
42 *
43 * @author masensio
44 * @author David A. Velasco
45 */
46 public class SslUntrustedCertDialogForEmptySslError extends SslUntrustedCertDialogABSTRACT {
47
48 //private final static String TAG = SslUntrustedCertDialogForEmptySslError.class.getSimpleName();
49
50 private SslError mError;
51 private SslErrorHandler mHandler;
52 private View mView;
53
54
55 /**
56 * Factory method.
57 *
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.
60 * @return New dialog.
61 */
62 public static SslUntrustedCertDialogForEmptySslError newInstance(SslError error, SslErrorHandler handler) {
63 return new SslUntrustedCertDialogForEmptySslError(error, handler);
64 }
65
66
67 /**
68 * Empty constructor.
69 *
70 * Required by Android framework. Never used, since the state is retained; see {@link #onCreate(Bundle)}
71 */
72 public SslUntrustedCertDialogForEmptySslError() {}
73
74
75 /**
76 * Private constructor.
77 *
78 * Used by the factory method {@link #newInstance(SslError, SslErrorHandler)}.
79 *
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.
82 */
83 private SslUntrustedCertDialogForEmptySslError(SslError error, SslErrorHandler handler) {
84 mError = error;
85 mHandler = handler;
86 }
87
88
89 @Override
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());
94 }*/
95 }
96
97
98 // TODO try to move to the parent class ?
99 @Override
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);
104 mView = null;
105 }
106
107 // try to move to the parent class ?
108 @Override
109 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
110 // Create a view by inflating desired layout
111 if (mView == null) {
112 mView = inflater.inflate(R.layout.ssl_untrusted_cert_layout, container, false);
113 } else {
114 ((ViewGroup)mView.getParent()).removeView(mView);
115 }
116
117 showNoMessageError();
118
119 Button ok = (Button) mView.findViewById(R.id.ok);
120 ok.setOnClickListener(new OnClickListener() {
121
122 @Override
123 public void onClick(View v) {
124 //AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
125 mHandler.proceed();
126 dismiss();
127 }
128 });
129
130 Button cancel = (Button) mView.findViewById(R.id.cancel);
131 cancel.setOnClickListener(new OnClickListener() {
132
133 @Override
134 public void onClick(View v) {
135 AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
136 getDialog().cancel();
137 mHandler.cancel();
138 act.cancelWebView();
139 }
140 });
141
142 Button details = (Button) mView.findViewById(R.id.details_btn);
143 details.setOnClickListener(new OnClickListener() {
144 @Override
145 public void onClick(View v) {
146 View detailsScroll = mView.findViewById(R.id.details_scroll);
147 if (detailsScroll.getVisibility() == View.VISIBLE) {
148 detailsScroll.setVisibility(View.GONE);
149 ((Button) v).setText(R.string.ssl_validator_btn_details_see);
150
151 } else {
152 detailsScroll.setVisibility(View.VISIBLE);
153 ((Button) v).setText(R.string.ssl_validator_btn_details_hide);
154 showCertificateData();
155 }
156 }
157 });
158
159 return mView;
160 }
161
162 @Override
163 public Dialog onCreateDialog(Bundle savedInstanceState) {
164 final Dialog dialog = super.onCreateDialog(savedInstanceState);
165 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
166 return dialog;
167 }
168
169 @Override
170 public void onDestroyView() {
171 if (getDialog() != null && getRetainInstance())
172 getDialog().setDismissMessage(null);
173 super.onDestroyView();
174 }
175
176 private void showCertificateData() {
177 TextView nullCerView = (TextView) mView.findViewById(R.id.null_cert);
178 SslCertificate cert = mError.getCertificate();
179 if (cert != null) {
180 nullCerView.setVisibility(View.GONE);
181 showSubject(cert.getIssuedTo());
182 showIssuer(cert.getIssuedBy());
183 showValidity(cert.getValidNotBeforeDate(), cert.getValidNotAfterDate());
184 hideSignature();
185
186 } else {
187 nullCerView.setVisibility(View.VISIBLE);
188 }
189 }
190
191 private void showValidity(Date notBefore, Date notAfter) {
192 TextView fromView = ((TextView)mView.findViewById(R.id.value_validity_from));
193 TextView toView = ((TextView)mView.findViewById(R.id.value_validity_to));
194 DateFormat dateFormat = DateFormat.getDateInstance();
195 fromView.setText(dateFormat.format(notBefore));
196 toView.setText(dateFormat.format(notAfter));
197 }
198
199
200 private void showSubject(SslCertificate.DName subject) {
201 TextView cnView = ((TextView)mView.findViewById(R.id.value_subject_CN));
202 cnView.setText(subject.getCName());
203 cnView.setVisibility(View.VISIBLE);
204
205 TextView oView = ((TextView)mView.findViewById(R.id.value_subject_O));
206 oView.setText(subject.getOName());
207 oView.setVisibility(View.VISIBLE);
208
209 TextView ouView = ((TextView)mView.findViewById(R.id.value_subject_OU));
210 ouView.setText(subject.getUName());
211 ouView.setVisibility(View.VISIBLE);
212
213 // SslCertificates don't offer this information
214 ((TextView)mView.findViewById(R.id.value_subject_C)).setVisibility(View.GONE);
215 ((TextView)mView.findViewById(R.id.value_subject_ST)).setVisibility(View.GONE);
216 ((TextView)mView.findViewById(R.id.value_subject_L)).setVisibility(View.GONE);
217 ((TextView)mView.findViewById(R.id.label_subject_C)).setVisibility(View.GONE);
218 ((TextView)mView.findViewById(R.id.label_subject_ST)).setVisibility(View.GONE);
219 ((TextView)mView.findViewById(R.id.label_subject_L)).setVisibility(View.GONE);
220 }
221
222
223 private void showIssuer(SslCertificate.DName issuer) {
224 TextView cnView = ((TextView)mView.findViewById(R.id.value_issuer_CN));
225 cnView.setText(issuer.getCName());
226 cnView.setVisibility(View.VISIBLE);
227
228 TextView oView = ((TextView)mView.findViewById(R.id.value_issuer_O));
229 oView.setText(issuer.getOName());
230 oView.setVisibility(View.VISIBLE);
231
232 TextView ouView = ((TextView)mView.findViewById(R.id.value_issuer_OU));
233 ouView.setText(issuer.getUName());
234 ouView.setVisibility(View.VISIBLE);
235
236 // SslCertificates don't offer this information
237 ((TextView)mView.findViewById(R.id.value_issuer_C)).setVisibility(View.GONE);
238 ((TextView)mView.findViewById(R.id.value_issuer_ST)).setVisibility(View.GONE);
239 ((TextView)mView.findViewById(R.id.value_issuer_L)).setVisibility(View.GONE);
240 ((TextView)mView.findViewById(R.id.label_issuer_C)).setVisibility(View.GONE);
241 ((TextView)mView.findViewById(R.id.label_issuer_ST)).setVisibility(View.GONE);
242 ((TextView)mView.findViewById(R.id.label_issuer_L)).setVisibility(View.GONE);
243 }
244
245 private void hideSignature() {
246 ((TextView)mView.findViewById(R.id.label_signature)).setVisibility(View.GONE);
247 ((TextView)mView.findViewById(R.id.label_signature_algorithm)).setVisibility(View.GONE);
248 ((TextView)mView.findViewById(R.id.value_signature_algorithm)).setVisibility(View.GONE);
249 ((TextView)mView.findViewById(R.id.value_signature)).setVisibility(View.GONE);
250 }
251
252 private void showNoMessageError() {
253 /// clean
254 mView.findViewById(R.id.reason_cert_not_trusted).setVisibility(View.GONE);
255 mView.findViewById(R.id.reason_cert_expired).setVisibility(View.GONE);
256 mView.findViewById(R.id.reason_cert_not_yet_valid).setVisibility(View.GONE);
257 mView.findViewById(R.id.reason_hostname_not_verified).setVisibility(View.GONE);
258 mView.findViewById(R.id.details_scroll).setVisibility(View.GONE);
259
260 mView.findViewById(R.id.reason_no_info_about_error).setVisibility(View.VISIBLE);
261
262 }
263 }