Fix buttons out of screen in SSL warning dialogs for small screens in landspace
[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 Button ok = (Button) mView.findViewById(R.id.untrusted_ok);
118 ok.setOnClickListener(new OnClickListener() {
119
120 @Override
121 public void onClick(View v) {
122 //AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
123 mHandler.proceed();
124 dismiss();
125 }
126 });
127
128 Button cancel = (Button) mView.findViewById(R.id.untrusted_cancel);
129 cancel.setOnClickListener(new OnClickListener() {
130
131 @Override
132 public void onClick(View v) {
133 AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
134 getDialog().cancel();
135 mHandler.cancel();
136 act.cancelWebView();
137 }
138 });
139
140 Button details = (Button) mView.findViewById(R.id.untrusted_details_btn);
141 details.setOnClickListener(new OnClickListener() {
142 @Override
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);
148
149 } else {
150 detailsScroll.setVisibility(View.VISIBLE);
151 ((Button) v).setText(R.string.ssl_validator_btn_details_hide);
152 showCertificateData();
153 }
154 }
155 });
156
157 return mView;
158 }
159
160 @Override
161 public Dialog onCreateDialog(Bundle savedInstanceState) {
162 final Dialog dialog = super.onCreateDialog(savedInstanceState);
163 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
164 return dialog;
165 }
166
167 @Override
168 public void onDestroyView() {
169 if (getDialog() != null && getRetainInstance())
170 getDialog().setDismissMessage(null);
171 super.onDestroyView();
172 }
173
174 private void showCertificateData() {
175 TextView nullCerView = (TextView) mView.findViewById(R.id.untrusted_null_cert);
176 SslCertificate cert = mError.getCertificate();
177 if (cert != null) {
178 nullCerView.setVisibility(View.GONE);
179 showSubject(cert.getIssuedTo());
180 showIssuer(cert.getIssuedBy());
181 showValidity(cert.getValidNotBeforeDate(), cert.getValidNotAfterDate());
182 hideSignature();
183
184 } else {
185 nullCerView.setVisibility(View.VISIBLE);
186 }
187 }
188
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));
195 }
196
197
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);
202
203 TextView oView = ((TextView)mView.findViewById(R.id.untrusted_value_subject_O));
204 oView.setText(subject.getOName());
205 oView.setVisibility(View.VISIBLE);
206
207 TextView ouView = ((TextView)mView.findViewById(R.id.untrusted_value_subject_OU));
208 ouView.setText(subject.getUName());
209 ouView.setVisibility(View.VISIBLE);
210
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);
218 }
219
220
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);
225
226 TextView oView = ((TextView)mView.findViewById(R.id.untrusted_value_issuer_O));
227 oView.setText(issuer.getOName());
228 oView.setVisibility(View.VISIBLE);
229
230 TextView ouView = ((TextView)mView.findViewById(R.id.untrusted_value_issuer_OU));
231 ouView.setText(issuer.getUName());
232 ouView.setVisibility(View.VISIBLE);
233
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);
241 }
242
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);
248 }
249
250 }