MainActivity.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.app.KeyguardManager;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.os.Build;
  26. import android.os.Bundle;
  27. import android.view.ViewGroup;
  28. import androidx.annotation.RequiresApi;
  29. import androidx.appcompat.widget.Toolbar;
  30. import autodagger.AutoInjector;
  31. import butterknife.BindView;
  32. import butterknife.ButterKnife;
  33. import com.bluelinelabs.conductor.Conductor;
  34. import com.bluelinelabs.conductor.Router;
  35. import com.bluelinelabs.conductor.RouterTransaction;
  36. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  37. import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
  38. import com.nextcloud.talk.R;
  39. import com.nextcloud.talk.application.NextcloudTalkApplication;
  40. import com.nextcloud.talk.controllers.*;
  41. import com.nextcloud.talk.controllers.base.providers.ActionBarProvider;
  42. import com.nextcloud.talk.utils.SecurityUtils;
  43. import com.nextcloud.talk.utils.bundle.BundleKeys;
  44. import com.nextcloud.talk.utils.database.user.UserUtils;
  45. import io.requery.Persistable;
  46. import io.requery.android.sqlcipher.SqlCipherDatabaseSource;
  47. import io.requery.reactivex.ReactiveEntityStore;
  48. import javax.inject.Inject;
  49. @AutoInjector(NextcloudTalkApplication.class)
  50. public final class MainActivity extends BaseActivity implements ActionBarProvider {
  51. private static final String TAG = "MainActivity";
  52. @BindView(R.id.toolbar)
  53. Toolbar toolbar;
  54. @BindView(R.id.controller_container)
  55. ViewGroup container;
  56. @Inject
  57. UserUtils userUtils;
  58. @Inject
  59. ReactiveEntityStore<Persistable> dataStore;
  60. @Inject
  61. SqlCipherDatabaseSource sqlCipherDatabaseSource;
  62. private Router router;
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.activity_main);
  67. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  68. ButterKnife.bind(this);
  69. setSupportActionBar(toolbar);
  70. router = Conductor.attachRouter(this, container, savedInstanceState);
  71. boolean hasDb = true;
  72. try {
  73. sqlCipherDatabaseSource.getWritableDatabase();
  74. } catch (Exception exception) {
  75. hasDb = false;
  76. }
  77. if (getIntent().hasExtra(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL)) {
  78. if (!router.hasRootController()) {
  79. router.setRoot(RouterTransaction.with(new ConversationsListController())
  80. .pushChangeHandler(new HorizontalChangeHandler())
  81. .popChangeHandler(new HorizontalChangeHandler()));
  82. }
  83. onNewIntent(getIntent());
  84. } else if (!router.hasRootController()) {
  85. if (hasDb) {
  86. if (userUtils.anyUserExists()) {
  87. router.setRoot(RouterTransaction.with(new ConversationsListController())
  88. .pushChangeHandler(new HorizontalChangeHandler())
  89. .popChangeHandler(new HorizontalChangeHandler()));
  90. } else {
  91. router.setRoot(RouterTransaction.with(new ServerSelectionController())
  92. .pushChangeHandler(new HorizontalChangeHandler())
  93. .popChangeHandler(new HorizontalChangeHandler()));
  94. }
  95. } else {
  96. router.setRoot(RouterTransaction.with(new ServerSelectionController())
  97. .pushChangeHandler(new HorizontalChangeHandler())
  98. .popChangeHandler(new HorizontalChangeHandler()));
  99. }
  100. }
  101. }
  102. @Override
  103. public void onResume() {
  104. super.onResume();
  105. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  106. checkIfWeAreSecure();
  107. }
  108. }
  109. @RequiresApi(api = Build.VERSION_CODES.M)
  110. public void checkIfWeAreSecure() {
  111. KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  112. if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) {
  113. if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
  114. if (router != null && router.getControllerWithTag(LockedController.TAG) == null) {
  115. router.pushController(RouterTransaction.with(new LockedController())
  116. .pushChangeHandler(new VerticalChangeHandler())
  117. .popChangeHandler(new VerticalChangeHandler())
  118. .tag(LockedController.TAG));
  119. }
  120. }
  121. }
  122. }
  123. @Override
  124. protected void onNewIntent(Intent intent) {
  125. super.onNewIntent(intent);
  126. if (intent.hasExtra(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL)) {
  127. if (intent.getBooleanExtra(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL, false)) {
  128. router.pushController(RouterTransaction.with(new CallNotificationController(intent.getExtras()))
  129. .pushChangeHandler(new HorizontalChangeHandler())
  130. .popChangeHandler(new HorizontalChangeHandler()));
  131. } else {
  132. router.pushController(RouterTransaction.with(new ChatController(intent.getExtras()))
  133. .pushChangeHandler(new HorizontalChangeHandler())
  134. .popChangeHandler(new HorizontalChangeHandler()));
  135. }
  136. }
  137. }
  138. @Override
  139. public void onBackPressed() {
  140. if (router.getControllerWithTag(LockedController.TAG) != null) {
  141. return;
  142. }
  143. if (!router.handleBack()) {
  144. super.onBackPressed();
  145. }
  146. }
  147. }