Merge pull request #435 from owncloud/ssl_dialogs_refactored_in_just_one
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SslUntrustedCertDialog.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.io.IOException;
20 import java.security.GeneralSecurityException;
21 import java.security.cert.X509Certificate;
22
23 import android.app.Activity;
24 import android.app.Dialog;
25 import android.net.http.SslError;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.Window;
31 import android.view.View.OnClickListener;
32 import android.webkit.SslErrorHandler;
33 import android.widget.Button;
34
35 import com.actionbarsherlock.app.SherlockDialogFragment;
36 import com.owncloud.android.R;
37 import com.owncloud.android.lib.common.network.CertificateCombinedException;
38 import com.owncloud.android.lib.common.network.NetworkUtils;
39 import com.owncloud.android.ui.adapter.CertificateCombinedExceptionViewAdapter;
40 import com.owncloud.android.ui.adapter.SslCertificateViewAdapter;
41 import com.owncloud.android.ui.adapter.SslErrorViewAdapter;
42 import com.owncloud.android.ui.adapter.X509CertificateViewAdapter;
43 import com.owncloud.android.utils.Log_OC;
44
45 /**
46 * Dialog to show information about an untrusted certificate and allow the user
47 * to decide trust on it or not.
48 *
49 * Abstract implementation of common functionality for different dialogs that
50 * get the information about the error and the certificate from different classes.
51 *
52 * @author masensio
53 * @author David A. Velasco
54 */
55 public class SslUntrustedCertDialog extends SherlockDialogFragment {
56
57 private final static String TAG = SslUntrustedCertDialog.class.getSimpleName();
58
59 protected View mView = null;
60 protected SslErrorHandler mHandler = null;
61 protected X509Certificate m509Certificate = null;
62
63 private ErrorViewAdapter mErrorViewAdapter = null;
64 private CertificateViewAdapter mCertificateViewAdapter = null;
65
66 public static SslUntrustedCertDialog newInstanceForEmptySslError(SslError error, SslErrorHandler handler) {
67 if (error == null) {
68 throw new IllegalArgumentException("Trying to create instance with parameter error == null");
69 }
70 if (handler == null) {
71 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
72 }
73 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
74 dialog.mHandler = handler;
75 dialog.mErrorViewAdapter = new SslErrorViewAdapter(error);
76 dialog.mCertificateViewAdapter = new SslCertificateViewAdapter(error.getCertificate());
77 return dialog;
78 }
79
80 public static SslUntrustedCertDialog newInstanceForFullSslError(CertificateCombinedException sslException) {
81 if (sslException == null) {
82 throw new IllegalArgumentException("Trying to create instance with parameter sslException == null");
83 }
84 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
85 dialog.m509Certificate = sslException.getServerCertificate();
86 dialog.mErrorViewAdapter = new CertificateCombinedExceptionViewAdapter(sslException);
87 dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(sslException.getServerCertificate());
88 return dialog;
89 }
90
91 public static SslUntrustedCertDialog newInstanceForFullSslError(X509Certificate cert, SslError error, SslErrorHandler handler) {
92 if (cert == null) {
93 throw new IllegalArgumentException("Trying to create instance with parameter cert == null");
94 }
95 if (error == null) {
96 throw new IllegalArgumentException("Trying to create instance with parameter error == null");
97 }
98 if (handler == null) {
99 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
100 }
101 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
102 dialog.m509Certificate = cert;
103 dialog.mHandler = handler;
104 dialog.mErrorViewAdapter = new SslErrorViewAdapter(error);
105 dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(cert);
106 return dialog;
107 }
108
109
110 @Override
111 public void onAttach(Activity activity) {
112 super.onAttach(activity);
113 if (!(activity instanceof OnSslUntrustedCertListener)) {
114 throw new IllegalArgumentException("The host activity must implement " + OnSslUntrustedCertListener.class.getCanonicalName());
115 }
116 }
117
118
119 @Override
120 public void onCreate(Bundle savedInstanceState) {
121 super.onCreate(savedInstanceState);
122 setRetainInstance(true); // force to keep the state of the fragment on configuration changes (such as device rotations)
123 setCancelable(false);
124 mView = null;
125 }
126
127 @Override
128 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
129 // Create a view by inflating desired layout
130 if (mView == null) {
131 mView = inflater.inflate(R.layout.ssl_untrusted_cert_layout, container, false);
132 mView.findViewById(R.id.details_scroll).setVisibility(View.GONE);
133 mErrorViewAdapter.updateErrorView(mView);
134 } else {
135 ((ViewGroup)mView.getParent()).removeView(mView);
136 }
137
138 Button ok = (Button) mView.findViewById(R.id.ok);
139 ok.setOnClickListener(new OnCertificateTrusted());
140
141 Button cancel = (Button) mView.findViewById(R.id.cancel);
142 cancel.setOnClickListener(new OnCertificateNotTrusted());
143
144 Button details = (Button) mView.findViewById(R.id.details_btn);
145 details.setOnClickListener(new OnClickListener() {
146
147 @Override
148 public void onClick(View v) {
149 View detailsScroll = mView.findViewById(R.id.details_scroll);
150 if (detailsScroll.getVisibility() == View.VISIBLE) {
151 detailsScroll.setVisibility(View.GONE);
152 ((Button) v).setText(R.string.ssl_validator_btn_details_see);
153
154 } else {
155 detailsScroll.setVisibility(View.VISIBLE);
156 ((Button) v).setText(R.string.ssl_validator_btn_details_hide);
157 mCertificateViewAdapter.updateCertificateView(mView);
158 }
159 }
160
161 });
162
163 return mView;
164 }
165
166
167 @Override
168 public Dialog onCreateDialog(Bundle savedInstanceState) {
169 final Dialog dialog = super.onCreateDialog(savedInstanceState);
170 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
171 return dialog;
172 }
173
174 @Override
175 public void onDestroyView() {
176 if (getDialog() != null && getRetainInstance())
177 getDialog().setDismissMessage(null);
178 super.onDestroyView();
179 }
180
181 private class OnCertificateNotTrusted implements OnClickListener {
182
183 @Override
184 public void onClick(View v) {
185 getDialog().cancel();
186 if (mHandler != null) {
187 mHandler.cancel();
188 }
189 ((OnSslUntrustedCertListener)getSherlockActivity()).onCancelCertificate();
190 }
191 }
192
193
194 private class OnCertificateTrusted implements OnClickListener {
195
196 @Override
197 public void onClick(View v) {
198 dismiss();
199 if (mHandler != null) {
200 mHandler.proceed();
201 }
202 if (m509Certificate != null) {
203 Activity activity = getSherlockActivity();
204 try {
205 NetworkUtils.addCertToKnownServersStore(m509Certificate, activity); // TODO make this asynchronously, it can take some time
206 ((OnSslUntrustedCertListener)activity).onSavedCertificate();
207
208 } catch (GeneralSecurityException e) {
209 ((OnSslUntrustedCertListener)activity).onFailedSavingCertificate();
210 Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e);
211
212 } catch (IOException e) {
213 ((OnSslUntrustedCertListener)activity).onFailedSavingCertificate();
214 Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e);
215 }
216 }
217 }
218
219 }
220
221
222 public interface OnSslUntrustedCertListener {
223 public void onSavedCertificate();
224 public void onFailedSavingCertificate();
225 public void onCancelCertificate();
226 }
227
228 public interface ErrorViewAdapter {
229 void updateErrorView(View mView);
230 }
231
232 public interface CertificateViewAdapter {
233 void updateCertificateView(View mView);
234 }
235
236 }