Merge branch 'develop' into check_server_certificates_in_SSO_webview
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / SslCertificateViewAdapter.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.adapter;
18
19 import java.text.DateFormat;
20 import java.util.Date;
21
22 import com.owncloud.android.R;
23 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
24 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.CertificateViewAdapter;
25
26 import android.net.http.SslCertificate;
27 import android.view.View;
28 import android.widget.TextView;
29
30 /**
31 * TODO
32 *
33 * @author masensio
34 * @author David A. Velasco
35 */
36 public class SslCertificateViewAdapter implements SslUntrustedCertDialog.CertificateViewAdapter {
37
38 //private final static String TAG = SslCertificateViewAdapter.class.getSimpleName();
39
40 private SslCertificate mCertificate;
41
42
43 /**
44 * Constructor
45 *
46 * @param
47 */
48 public SslCertificateViewAdapter(SslCertificate certificate) {
49 mCertificate = certificate;
50 }
51
52 @Override
53 public void updateCertificateView(View dialogView) {
54 TextView nullCerView = (TextView) dialogView.findViewById(R.id.null_cert);
55 if (mCertificate != null) {
56 nullCerView.setVisibility(View.GONE);
57 showSubject(mCertificate.getIssuedTo(), dialogView);
58 showIssuer(mCertificate.getIssuedBy(), dialogView);
59 showValidity(mCertificate.getValidNotBeforeDate(), mCertificate.getValidNotAfterDate(), dialogView);
60 hideSignature(dialogView);
61
62 } else {
63 nullCerView.setVisibility(View.VISIBLE);
64 }
65 }
66
67 private void showValidity(Date notBefore, Date notAfter, View dialogView) {
68 TextView fromView = ((TextView)dialogView.findViewById(R.id.value_validity_from));
69 TextView toView = ((TextView)dialogView.findViewById(R.id.value_validity_to));
70 DateFormat dateFormat = DateFormat.getDateInstance();
71 fromView.setText(dateFormat.format(notBefore));
72 toView.setText(dateFormat.format(notAfter));
73 }
74
75
76 private void showSubject(SslCertificate.DName subject, View dialogView) {
77 TextView cnView = ((TextView)dialogView.findViewById(R.id.value_subject_CN));
78 cnView.setText(subject.getCName());
79 cnView.setVisibility(View.VISIBLE);
80
81 TextView oView = ((TextView)dialogView.findViewById(R.id.value_subject_O));
82 oView.setText(subject.getOName());
83 oView.setVisibility(View.VISIBLE);
84
85 TextView ouView = ((TextView)dialogView.findViewById(R.id.value_subject_OU));
86 ouView.setText(subject.getUName());
87 ouView.setVisibility(View.VISIBLE);
88
89 // SslCertificates don't offer this information
90 ((TextView)dialogView.findViewById(R.id.value_subject_C)).setVisibility(View.GONE);
91 ((TextView)dialogView.findViewById(R.id.value_subject_ST)).setVisibility(View.GONE);
92 ((TextView)dialogView.findViewById(R.id.value_subject_L)).setVisibility(View.GONE);
93 ((TextView)dialogView.findViewById(R.id.label_subject_C)).setVisibility(View.GONE);
94 ((TextView)dialogView.findViewById(R.id.label_subject_ST)).setVisibility(View.GONE);
95 ((TextView)dialogView.findViewById(R.id.label_subject_L)).setVisibility(View.GONE);
96 }
97
98
99 private void showIssuer(SslCertificate.DName issuer, View dialogView) {
100 TextView cnView = ((TextView)dialogView.findViewById(R.id.value_issuer_CN));
101 cnView.setText(issuer.getCName());
102 cnView.setVisibility(View.VISIBLE);
103
104 TextView oView = ((TextView)dialogView.findViewById(R.id.value_issuer_O));
105 oView.setText(issuer.getOName());
106 oView.setVisibility(View.VISIBLE);
107
108 TextView ouView = ((TextView)dialogView.findViewById(R.id.value_issuer_OU));
109 ouView.setText(issuer.getUName());
110 ouView.setVisibility(View.VISIBLE);
111
112 // SslCertificates don't offer this information
113 ((TextView)dialogView.findViewById(R.id.value_issuer_C)).setVisibility(View.GONE);
114 ((TextView)dialogView.findViewById(R.id.value_issuer_ST)).setVisibility(View.GONE);
115 ((TextView)dialogView.findViewById(R.id.value_issuer_L)).setVisibility(View.GONE);
116 ((TextView)dialogView.findViewById(R.id.label_issuer_C)).setVisibility(View.GONE);
117 ((TextView)dialogView.findViewById(R.id.label_issuer_ST)).setVisibility(View.GONE);
118 ((TextView)dialogView.findViewById(R.id.label_issuer_L)).setVisibility(View.GONE);
119 }
120
121 private void hideSignature(View dialogView) {
122 ((TextView)dialogView.findViewById(R.id.label_signature)).setVisibility(View.GONE);
123 ((TextView)dialogView.findViewById(R.id.label_signature_algorithm)).setVisibility(View.GONE);
124 ((TextView)dialogView.findViewById(R.id.value_signature_algorithm)).setVisibility(View.GONE);
125 ((TextView)dialogView.findViewById(R.id.value_signature)).setVisibility(View.GONE);
126 }
127
128 }