LockedController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
  7. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.controllers;
  23. import android.app.Activity;
  24. import android.app.KeyguardManager;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.os.Build;
  28. import android.os.Handler;
  29. import android.os.Looper;
  30. import android.util.Log;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import androidx.annotation.NonNull;
  35. import androidx.annotation.RequiresApi;
  36. import androidx.biometric.BiometricPrompt;
  37. import androidx.core.content.res.ResourcesCompat;
  38. import androidx.fragment.app.FragmentActivity;
  39. import autodagger.AutoInjector;
  40. import butterknife.OnClick;
  41. import com.nextcloud.talk.R;
  42. import com.nextcloud.talk.application.NextcloudTalkApplication;
  43. import com.nextcloud.talk.controllers.base.BaseController;
  44. import com.nextcloud.talk.utils.DisplayUtils;
  45. import com.nextcloud.talk.utils.SecurityUtils;
  46. import com.nextcloud.talk.utils.preferences.AppPreferences;
  47. import org.jetbrains.annotations.NotNull;
  48. import javax.inject.Inject;
  49. import java.util.concurrent.Executor;
  50. import java.util.concurrent.Executors;
  51. @AutoInjector(NextcloudTalkApplication.class)
  52. public class LockedController extends BaseController {
  53. public static final String TAG = "LockedController";
  54. private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 112;
  55. @Inject
  56. AppPreferences appPreferences;
  57. @NotNull
  58. @Override
  59. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  60. return inflater.inflate(R.layout.controller_locked, container, false);
  61. }
  62. @Override
  63. protected void onViewBound(@NonNull View view) {
  64. super.onViewBound(view);
  65. NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
  66. }
  67. @RequiresApi(api = Build.VERSION_CODES.M)
  68. @Override
  69. protected void onAttach(@NonNull View view) {
  70. super.onAttach(view);
  71. if (getActivity() != null && getResources() != null) {
  72. DisplayUtils.applyColorToStatusBar(getActivity(), ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null));
  73. DisplayUtils.applyColorToNavigationBar(getActivity().getWindow(), ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null));
  74. }
  75. checkIfWeAreSecure();
  76. }
  77. @RequiresApi(api = Build.VERSION_CODES.M)
  78. @OnClick(R.id.unlockContainer)
  79. void unlock() {
  80. checkIfWeAreSecure();
  81. }
  82. @RequiresApi(api = Build.VERSION_CODES.M)
  83. private void showBiometricDialog() {
  84. Context context = getActivity();
  85. if (context != null) {
  86. final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
  87. .setTitle(String.format(context.getString(R.string.nc_biometric_unlock), context.getString(R.string.nc_app_name)))
  88. .setNegativeButtonText(context.getString(R.string.nc_cancel))
  89. .build();
  90. Executor executor = Executors.newSingleThreadExecutor();
  91. final BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor,
  92. new BiometricPrompt.AuthenticationCallback() {
  93. @Override
  94. public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
  95. super.onAuthenticationSucceeded(result);
  96. Log.d(TAG, "Fingerprint recognised successfully");
  97. new Handler(Looper.getMainLooper()).post(() -> getRouter().popCurrentController());
  98. }
  99. @Override
  100. public void onAuthenticationFailed() {
  101. super.onAuthenticationFailed();
  102. Log.d(TAG, "Fingerprint not recognised");
  103. }
  104. @Override
  105. public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
  106. super.onAuthenticationError(errorCode, errString);
  107. showAuthenticationScreen();
  108. }
  109. }
  110. );
  111. BiometricPrompt.CryptoObject cryptoObject = SecurityUtils.getCryptoObject();
  112. if (cryptoObject != null) {
  113. biometricPrompt.authenticate(promptInfo, cryptoObject);
  114. } else {
  115. biometricPrompt.authenticate(promptInfo);
  116. }
  117. }
  118. }
  119. @RequiresApi(api = Build.VERSION_CODES.M)
  120. private void checkIfWeAreSecure() {
  121. if (getActivity() != null) {
  122. KeyguardManager keyguardManager = (KeyguardManager) getActivity().getSystemService(Context.KEYGUARD_SERVICE);
  123. if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) {
  124. if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
  125. showBiometricDialog();
  126. } else {
  127. getRouter().popCurrentController();
  128. }
  129. }
  130. }
  131. }
  132. private void showAuthenticationScreen() {
  133. if (getActivity() != null) {
  134. KeyguardManager keyguardManager = (KeyguardManager) getActivity().getSystemService(Context.KEYGUARD_SERVICE);
  135. Intent intent = keyguardManager.createConfirmDeviceCredentialIntent(null, null);
  136. if (intent != null) {
  137. startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
  138. }
  139. }
  140. }
  141. @Override
  142. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  143. super.onActivityResult(requestCode, resultCode, data);
  144. if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
  145. if (resultCode == Activity.RESULT_OK) {
  146. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  147. if (SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
  148. Log.d(TAG, "All went well, dismiss locked controller");
  149. getRouter().popCurrentController();
  150. }
  151. }
  152. } else {
  153. Log.d(TAG, "Authorization failed");
  154. }
  155. }
  156. }
  157. public AppBarLayoutType getAppBarLayoutType() {
  158. return AppBarLayoutType.EMPTY;
  159. }
  160. }