BaseActivity.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  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. package com.nextcloud.talk.activities;
  21. import android.annotation.SuppressLint;
  22. import android.content.Context;
  23. import android.os.Build;
  24. import android.os.Bundle;
  25. import android.util.Log;
  26. import android.view.WindowManager;
  27. import android.webkit.SslErrorHandler;
  28. import androidx.annotation.Nullable;
  29. import androidx.appcompat.app.AppCompatActivity;
  30. import autodagger.AutoInjector;
  31. import com.nextcloud.talk.R;
  32. import com.nextcloud.talk.application.NextcloudTalkApplication;
  33. import com.nextcloud.talk.events.CertificateEvent;
  34. import com.nextcloud.talk.utils.SecurityUtils;
  35. import com.nextcloud.talk.utils.preferences.AppPreferences;
  36. import com.nextcloud.talk.utils.ssl.MagicTrustManager;
  37. import com.yarolegovich.lovelydialog.LovelyStandardDialog;
  38. import org.greenrobot.eventbus.EventBus;
  39. import org.greenrobot.eventbus.Subscribe;
  40. import org.greenrobot.eventbus.ThreadMode;
  41. import javax.inject.Inject;
  42. import java.security.cert.CertificateParsingException;
  43. import java.security.cert.X509Certificate;
  44. import java.text.DateFormat;
  45. import java.util.List;
  46. @AutoInjector(NextcloudTalkApplication.class)
  47. public class BaseActivity extends AppCompatActivity {
  48. private static final String TAG = "BaseActivity";
  49. @Inject
  50. EventBus eventBus;
  51. @Inject
  52. AppPreferences appPreferences;
  53. @Inject
  54. Context context;
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  58. super.onCreate(savedInstanceState);
  59. }
  60. @Override
  61. public void onResume() {
  62. super.onResume();
  63. if (appPreferences.getIsScreenSecured()) {
  64. getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
  65. } else {
  66. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
  67. }
  68. if (appPreferences.getIsScreenLocked()) {
  69. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  70. SecurityUtils.createKey(appPreferences.getScreenLockTimeout());
  71. }
  72. }
  73. }
  74. public void showCertificateDialog(X509Certificate cert, MagicTrustManager magicTrustManager,
  75. @Nullable SslErrorHandler sslErrorHandler) {
  76. DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG);
  77. String validFrom = formatter.format(cert.getNotBefore());
  78. String validUntil = formatter.format(cert.getNotAfter());
  79. String issuedBy = cert.getIssuerDN().toString();
  80. String issuedFor;
  81. try {
  82. if (cert.getSubjectAlternativeNames() != null) {
  83. StringBuilder stringBuilder = new StringBuilder();
  84. for (Object o : cert.getSubjectAlternativeNames()) {
  85. List list = (List) o;
  86. int type = (Integer) list.get(0);
  87. if (type == 2) {
  88. String name = (String) list.get(1);
  89. stringBuilder.append("[").append(type).append("]").append(name).append(" ");
  90. }
  91. }
  92. issuedFor = stringBuilder.toString();
  93. } else {
  94. issuedFor = cert.getSubjectDN().getName();
  95. }
  96. @SuppressLint("StringFormatMatches") String dialogText = String.format(getResources()
  97. .getString(R.string.nc_certificate_dialog_text),
  98. issuedBy, issuedFor, validFrom, validUntil);
  99. new LovelyStandardDialog(this)
  100. .setTopColorRes(R.color.nc_darkRed)
  101. .setNegativeButtonColorRes(R.color.nc_darkRed)
  102. .setPositiveButtonColorRes(R.color.colorPrimaryDark)
  103. .setIcon(R.drawable.ic_security_white_24dp)
  104. .setTitle(R.string.nc_certificate_dialog_title)
  105. .setMessage(dialogText)
  106. .setPositiveButton(R.string.nc_yes, v -> {
  107. magicTrustManager.addCertInTrustStore(cert);
  108. if (sslErrorHandler != null) {
  109. sslErrorHandler.proceed();
  110. }
  111. })
  112. .setNegativeButton(R.string.nc_no, view1 -> {
  113. if (sslErrorHandler != null) {
  114. sslErrorHandler.cancel();
  115. }
  116. })
  117. .show();
  118. } catch (CertificateParsingException e) {
  119. Log.d(TAG, "Failed to parse the certificate");
  120. }
  121. }
  122. @Subscribe(threadMode = ThreadMode.MAIN)
  123. public void onMessageEvent(CertificateEvent event) {
  124. showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
  125. }
  126. @Override
  127. public void onStart() {
  128. super.onStart();
  129. eventBus.register(this);
  130. }
  131. @Override
  132. public void onStop() {
  133. super.onStop();
  134. eventBus.unregister(this);
  135. }
  136. }