SslCertificateViewAdapter.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.adapter;
  22. import android.net.http.SslCertificate;
  23. import android.view.View;
  24. import com.owncloud.android.databinding.SslUntrustedCertLayoutBinding;
  25. import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
  26. import java.text.DateFormat;
  27. import java.util.Date;
  28. import androidx.annotation.NonNull;
  29. /**
  30. * TODO
  31. */
  32. public class SslCertificateViewAdapter implements SslUntrustedCertDialog.CertificateViewAdapter {
  33. //private final static String TAG = SslCertificateViewAdapter.class.getSimpleName();
  34. private SslCertificate mCertificate;
  35. /**
  36. * Constructor
  37. *
  38. * @param certificate the SSL certificate
  39. */
  40. public SslCertificateViewAdapter(SslCertificate certificate) {
  41. mCertificate = certificate;
  42. }
  43. @Override
  44. public void updateCertificateView(SslUntrustedCertLayoutBinding binding) {
  45. if (mCertificate != null) {
  46. binding.nullCert.setVisibility(View.GONE);
  47. showSubject(mCertificate.getIssuedTo(), binding);
  48. showIssuer(mCertificate.getIssuedBy(), binding);
  49. showValidity(mCertificate.getValidNotBeforeDate(), mCertificate.getValidNotAfterDate(), binding);
  50. } else {
  51. binding.nullCert.setVisibility(View.VISIBLE);
  52. }
  53. }
  54. private void showValidity(Date notBefore, Date notAfter, @NonNull SslUntrustedCertLayoutBinding binding) {
  55. DateFormat dateFormat = DateFormat.getDateInstance();
  56. binding.valueValidityFrom.setText(dateFormat.format(notBefore));
  57. binding.valueValidityTo.setText(dateFormat.format(notAfter));
  58. }
  59. private void showSubject(SslCertificate.DName subject, @NonNull SslUntrustedCertLayoutBinding binding) {
  60. binding.valueSubjectCN.setText(subject.getCName());
  61. binding.valueSubjectCN.setVisibility(View.VISIBLE);
  62. binding.valueSubjectO.setText(subject.getOName());
  63. binding.valueSubjectO.setVisibility(View.VISIBLE);
  64. binding.valueSubjectOU.setText(subject.getUName());
  65. binding.valueSubjectOU.setVisibility(View.VISIBLE);
  66. // SslCertificates don't offer this information
  67. binding.valueSubjectC.setVisibility(View.GONE);
  68. binding.valueSubjectST.setVisibility(View.GONE);
  69. binding.valueSubjectL.setVisibility(View.GONE);
  70. binding.labelSubjectC.setVisibility(View.GONE);
  71. binding.labelSubjectST.setVisibility(View.GONE);
  72. binding.labelSubjectL.setVisibility(View.GONE);
  73. }
  74. private void showIssuer(SslCertificate.DName issuer, @NonNull SslUntrustedCertLayoutBinding binding) {
  75. binding.valueIssuerCN.setText(issuer.getCName());
  76. binding.valueIssuerCN.setVisibility(View.VISIBLE);
  77. binding.valueIssuerO.setText(issuer.getOName());
  78. binding.valueIssuerO.setVisibility(View.VISIBLE);
  79. binding.valueIssuerOU.setText(issuer.getUName());
  80. binding.valueIssuerOU.setVisibility(View.VISIBLE);
  81. // SslCertificates don't offer this information
  82. binding.valueIssuerC.setVisibility(View.GONE);
  83. binding.valueIssuerST.setVisibility(View.GONE);
  84. binding.valueIssuerL.setVisibility(View.GONE);
  85. binding.labelIssuerC.setVisibility(View.GONE);
  86. binding.labelIssuerST.setVisibility(View.GONE);
  87. binding.labelIssuerL.setVisibility(View.GONE);
  88. }
  89. }