MainActivity.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. *
  3. * Nextcloud Talk application
  4. *
  5. * @author Mario Danic
  6. * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com)
  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 as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.talk.activities;
  22. import android.annotation.SuppressLint;
  23. import android.os.Bundle;
  24. import android.support.annotation.Nullable;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.support.v7.widget.Toolbar;
  27. import android.util.Log;
  28. import android.view.ViewGroup;
  29. import android.webkit.SslErrorHandler;
  30. import com.bluelinelabs.conductor.Conductor;
  31. import com.bluelinelabs.conductor.Router;
  32. import com.bluelinelabs.conductor.RouterTransaction;
  33. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  34. import com.nextcloud.talk.R;
  35. import com.nextcloud.talk.application.NextcloudTalkApplication;
  36. import com.nextcloud.talk.controllers.MagicBottomNavigationController;
  37. import com.nextcloud.talk.controllers.ServerSelectionController;
  38. import com.nextcloud.talk.controllers.base.providers.ActionBarProvider;
  39. import com.nextcloud.talk.events.CertificateEvent;
  40. import com.nextcloud.talk.utils.database.user.UserUtils;
  41. import com.nextcloud.talk.utils.ssl.MagicTrustManager;
  42. import com.yarolegovich.lovelydialog.LovelyStandardDialog;
  43. import org.greenrobot.eventbus.EventBus;
  44. import org.greenrobot.eventbus.Subscribe;
  45. import org.greenrobot.eventbus.ThreadMode;
  46. import java.security.cert.CertificateParsingException;
  47. import java.security.cert.X509Certificate;
  48. import java.text.DateFormat;
  49. import java.util.List;
  50. import javax.inject.Inject;
  51. import autodagger.AutoInjector;
  52. import butterknife.BindView;
  53. import butterknife.ButterKnife;
  54. import io.requery.Persistable;
  55. import io.requery.android.sqlcipher.SqlCipherDatabaseSource;
  56. import io.requery.reactivex.ReactiveEntityStore;
  57. @AutoInjector(NextcloudTalkApplication.class)
  58. public final class MainActivity extends AppCompatActivity implements ActionBarProvider {
  59. private static final String TAG = "MainActivity";
  60. @BindView(R.id.toolbar)
  61. Toolbar toolbar;
  62. @BindView(R.id.controller_container)
  63. ViewGroup container;
  64. @Inject
  65. UserUtils userUtils;
  66. @Inject
  67. ReactiveEntityStore<Persistable> dataStore;
  68. @Inject
  69. SqlCipherDatabaseSource sqlCipherDatabaseSource;
  70. @Inject
  71. EventBus eventBus;
  72. private Router router;
  73. @Override
  74. protected void onCreate(Bundle savedInstanceState) {
  75. super.onCreate(savedInstanceState);
  76. setContentView(R.layout.activity_main);
  77. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  78. ButterKnife.bind(this);
  79. setSupportActionBar(toolbar);
  80. router = Conductor.attachRouter(this, container, savedInstanceState);
  81. boolean hasDb = true;
  82. try {
  83. sqlCipherDatabaseSource.getWritableDatabase();
  84. } catch (Exception exception) {
  85. hasDb = false;
  86. }
  87. if (!router.hasRootController() && hasDb && userUtils.anyUserExists()) {
  88. sqlCipherDatabaseSource.close();
  89. router.setRoot(RouterTransaction.with(new MagicBottomNavigationController())
  90. .pushChangeHandler(new HorizontalChangeHandler())
  91. .popChangeHandler(new HorizontalChangeHandler()));
  92. } else if (!router.hasRootController()) {
  93. router.setRoot(RouterTransaction.with(new ServerSelectionController())
  94. .pushChangeHandler(new HorizontalChangeHandler())
  95. .popChangeHandler(new HorizontalChangeHandler()));
  96. }
  97. }
  98. @Override
  99. public void onBackPressed() {
  100. if (!router.handleBack()) {
  101. super.onBackPressed();
  102. }
  103. }
  104. public void showCertificateDialog(X509Certificate cert, MagicTrustManager magicTrustManager,
  105. @Nullable SslErrorHandler sslErrorHandler) {
  106. DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG);
  107. String validFrom = formatter.format(cert.getNotBefore());
  108. String validUntil = formatter.format(cert.getNotAfter());
  109. String issuedBy = cert.getIssuerDN().toString();
  110. String issuedFor;
  111. try {
  112. if (cert.getSubjectAlternativeNames() != null) {
  113. StringBuilder stringBuilder = new StringBuilder();
  114. for (Object o : cert.getSubjectAlternativeNames()) {
  115. List list = (List) o;
  116. int type = (Integer) list.get(0);
  117. if (type == 2) {
  118. String name = (String) list.get(1);
  119. stringBuilder.append("[").append(type).append("]").append(name).append(" ");
  120. }
  121. }
  122. issuedFor = stringBuilder.toString();
  123. } else {
  124. issuedFor = cert.getSubjectDN().getName();
  125. }
  126. @SuppressLint("StringFormatMatches") String dialogText = String.format(getResources()
  127. .getString(R.string.nc_certificate_dialog_text),
  128. issuedBy, issuedFor, validFrom, validUntil);
  129. new LovelyStandardDialog(this)
  130. .setTopColorRes(R.color.nc_darkRed)
  131. .setNegativeButtonColorRes(R.color.nc_darkRed)
  132. .setPositiveButtonColorRes(R.color.colorPrimaryDark)
  133. .setIcon(R.drawable.ic_security_white_24dp)
  134. .setTitle(R.string.nc_certificate_dialog_title)
  135. .setMessage(dialogText)
  136. .setPositiveButton(R.string.nc_yes, v -> {
  137. magicTrustManager.addCertInTrustStore(cert);
  138. if (sslErrorHandler != null) {
  139. sslErrorHandler.proceed();
  140. }
  141. })
  142. .setNegativeButton(R.string.nc_no, view1 -> {
  143. if (sslErrorHandler != null) {
  144. sslErrorHandler.cancel();
  145. }
  146. })
  147. .show();
  148. } catch (CertificateParsingException e) {
  149. Log.d(TAG, "Failed to parse the certificate");
  150. }
  151. }
  152. @Subscribe(threadMode = ThreadMode.MAIN)
  153. public void onMessageEvent(CertificateEvent event) {
  154. showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
  155. }
  156. @Override
  157. public void onStart() {
  158. super.onStart();
  159. eventBus.register(this);
  160. }
  161. @Override
  162. public void onStop() {
  163. super.onStop();
  164. eventBus.unregister(this);
  165. }
  166. }