SslUntrustedCertDialogForEmptySslError.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import java.text.DateFormat;
  19. import java.util.Date;
  20. import com.owncloud.android.R;
  21. import com.owncloud.android.authentication.AuthenticatorActivity;
  22. import android.app.Activity;
  23. import android.app.Dialog;
  24. import android.net.http.SslCertificate;
  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.View.OnClickListener;
  30. import android.view.ViewGroup;
  31. import android.view.Window;
  32. import android.webkit.SslErrorHandler;
  33. import android.webkit.WebView;
  34. import android.widget.Button;
  35. import android.widget.TextView;
  36. /**
  37. * Dialog to show an Untrusted Certificate
  38. *
  39. * @author masensio
  40. * @author David A. Velasco
  41. */
  42. public class SslUntrustedCertDialogForEmptySslError extends SslUntrustedCertDialogABSTRACT {
  43. //private final static String TAG = SslUntrustedCertDialogForEmptySslError.class.getSimpleName();
  44. private SslError mError;
  45. private SslErrorHandler mHandler;
  46. private View mView;
  47. /**
  48. * Factory method.
  49. *
  50. * @param error Error occurred; details about it will be shown in the dialog.
  51. * @param handler Handler to indicate to the {@link WebView} where the error was found what to do next.
  52. * @return New dialog.
  53. */
  54. public static SslUntrustedCertDialogForEmptySslError newInstance(SslError error, SslErrorHandler handler) {
  55. return new SslUntrustedCertDialogForEmptySslError(error, handler);
  56. }
  57. /**
  58. * Empty constructor.
  59. *
  60. * Required by Android framework. Never used, since the state is retained; see {@link #onCreate(Bundle)}
  61. */
  62. public SslUntrustedCertDialogForEmptySslError() {}
  63. /**
  64. * Private constructor.
  65. *
  66. * Used by the factory method {@link #newInstance(SslError, SslErrorHandler)}.
  67. *
  68. * @param error Error occurred; details about it will be shown in the dialog.
  69. * @param handler Handler to indicate to the {@link WebView} where the error was found what to do next.
  70. */
  71. private SslUntrustedCertDialogForEmptySslError(SslError error, SslErrorHandler handler) {
  72. mError = error;
  73. mHandler = handler;
  74. }
  75. @Override
  76. public void onAttach(Activity activity) {
  77. super.onAttach(activity);
  78. /*if (!(activity instanceof OnSslUntrustedCertListener)) {
  79. throw new IllegalArgumentException("Trying to attach to an Activity not implementing " + OnSslUntrustedCertListener.class.getCanonicalName());
  80. }*/
  81. }
  82. // TODO try to move to the parent class ?
  83. @Override
  84. public void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. setRetainInstance(true); // force to keep the state of the fragment on configuration changes (such as device rotations)
  87. setCancelable(false);
  88. mView = null;
  89. }
  90. // try to move to the parent class ?
  91. @Override
  92. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  93. // Create a view by inflating desired layout
  94. if (mView == null) {
  95. mView = inflater.inflate(R.layout.ssl_untrusted_cert_layout, container, false);
  96. } else {
  97. ((ViewGroup)mView.getParent()).removeView(mView);
  98. }
  99. showNoMessageError();
  100. Button ok = (Button) mView.findViewById(R.id.ok);
  101. ok.setOnClickListener(new OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. //AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
  105. mHandler.proceed();
  106. dismiss();
  107. }
  108. });
  109. Button cancel = (Button) mView.findViewById(R.id.cancel);
  110. cancel.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. AuthenticatorActivity act = ((AuthenticatorActivity)getSherlockActivity());
  114. getDialog().cancel();
  115. mHandler.cancel();
  116. act.cancelWebView();
  117. }
  118. });
  119. Button details = (Button) mView.findViewById(R.id.details_btn);
  120. details.setOnClickListener(new OnClickListener() {
  121. @Override
  122. public void onClick(View v) {
  123. View detailsScroll = mView.findViewById(R.id.details_scroll);
  124. if (detailsScroll.getVisibility() == View.VISIBLE) {
  125. detailsScroll.setVisibility(View.GONE);
  126. ((Button) v).setText(R.string.ssl_validator_btn_details_see);
  127. } else {
  128. detailsScroll.setVisibility(View.VISIBLE);
  129. ((Button) v).setText(R.string.ssl_validator_btn_details_hide);
  130. showCertificateData();
  131. }
  132. }
  133. });
  134. return mView;
  135. }
  136. @Override
  137. public Dialog onCreateDialog(Bundle savedInstanceState) {
  138. final Dialog dialog = super.onCreateDialog(savedInstanceState);
  139. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  140. return dialog;
  141. }
  142. @Override
  143. public void onDestroyView() {
  144. if (getDialog() != null && getRetainInstance())
  145. getDialog().setDismissMessage(null);
  146. super.onDestroyView();
  147. }
  148. private void showCertificateData() {
  149. TextView nullCerView = (TextView) mView.findViewById(R.id.null_cert);
  150. SslCertificate cert = mError.getCertificate();
  151. if (cert != null) {
  152. nullCerView.setVisibility(View.GONE);
  153. showSubject(cert.getIssuedTo());
  154. showIssuer(cert.getIssuedBy());
  155. showValidity(cert.getValidNotBeforeDate(), cert.getValidNotAfterDate());
  156. hideSignature();
  157. } else {
  158. nullCerView.setVisibility(View.VISIBLE);
  159. }
  160. }
  161. private void showValidity(Date notBefore, Date notAfter) {
  162. TextView fromView = ((TextView)mView.findViewById(R.id.value_validity_from));
  163. TextView toView = ((TextView)mView.findViewById(R.id.value_validity_to));
  164. DateFormat dateFormat = DateFormat.getDateInstance();
  165. fromView.setText(dateFormat.format(notBefore));
  166. toView.setText(dateFormat.format(notAfter));
  167. }
  168. private void showSubject(SslCertificate.DName subject) {
  169. TextView cnView = ((TextView)mView.findViewById(R.id.value_subject_CN));
  170. cnView.setText(subject.getCName());
  171. cnView.setVisibility(View.VISIBLE);
  172. TextView oView = ((TextView)mView.findViewById(R.id.value_subject_O));
  173. oView.setText(subject.getOName());
  174. oView.setVisibility(View.VISIBLE);
  175. TextView ouView = ((TextView)mView.findViewById(R.id.value_subject_OU));
  176. ouView.setText(subject.getUName());
  177. ouView.setVisibility(View.VISIBLE);
  178. // SslCertificates don't offer this information
  179. ((TextView)mView.findViewById(R.id.value_subject_C)).setVisibility(View.GONE);
  180. ((TextView)mView.findViewById(R.id.value_subject_ST)).setVisibility(View.GONE);
  181. ((TextView)mView.findViewById(R.id.value_subject_L)).setVisibility(View.GONE);
  182. ((TextView)mView.findViewById(R.id.label_subject_C)).setVisibility(View.GONE);
  183. ((TextView)mView.findViewById(R.id.label_subject_ST)).setVisibility(View.GONE);
  184. ((TextView)mView.findViewById(R.id.label_subject_L)).setVisibility(View.GONE);
  185. }
  186. private void showIssuer(SslCertificate.DName issuer) {
  187. TextView cnView = ((TextView)mView.findViewById(R.id.value_issuer_CN));
  188. cnView.setText(issuer.getCName());
  189. cnView.setVisibility(View.VISIBLE);
  190. TextView oView = ((TextView)mView.findViewById(R.id.value_issuer_O));
  191. oView.setText(issuer.getOName());
  192. oView.setVisibility(View.VISIBLE);
  193. TextView ouView = ((TextView)mView.findViewById(R.id.value_issuer_OU));
  194. ouView.setText(issuer.getUName());
  195. ouView.setVisibility(View.VISIBLE);
  196. // SslCertificates don't offer this information
  197. ((TextView)mView.findViewById(R.id.value_issuer_C)).setVisibility(View.GONE);
  198. ((TextView)mView.findViewById(R.id.value_issuer_ST)).setVisibility(View.GONE);
  199. ((TextView)mView.findViewById(R.id.value_issuer_L)).setVisibility(View.GONE);
  200. ((TextView)mView.findViewById(R.id.label_issuer_C)).setVisibility(View.GONE);
  201. ((TextView)mView.findViewById(R.id.label_issuer_ST)).setVisibility(View.GONE);
  202. ((TextView)mView.findViewById(R.id.label_issuer_L)).setVisibility(View.GONE);
  203. }
  204. private void hideSignature() {
  205. ((TextView)mView.findViewById(R.id.label_signature)).setVisibility(View.GONE);
  206. ((TextView)mView.findViewById(R.id.label_signature_algorithm)).setVisibility(View.GONE);
  207. ((TextView)mView.findViewById(R.id.value_signature_algorithm)).setVisibility(View.GONE);
  208. ((TextView)mView.findViewById(R.id.value_signature)).setVisibility(View.GONE);
  209. }
  210. private void showNoMessageError() {
  211. /// clean
  212. mView.findViewById(R.id.reason_cert_not_trusted).setVisibility(View.GONE);
  213. mView.findViewById(R.id.reason_cert_expired).setVisibility(View.GONE);
  214. mView.findViewById(R.id.reason_cert_not_yet_valid).setVisibility(View.GONE);
  215. mView.findViewById(R.id.reason_hostname_not_verified).setVisibility(View.GONE);
  216. mView.findViewById(R.id.details_scroll).setVisibility(View.GONE);
  217. mView.findViewById(R.id.reason_no_info_about_error).setVisibility(View.VISIBLE);
  218. }
  219. }