From ada229b40980e7e721b7be85e53d18af821b5f91 Mon Sep 17 00:00:00 2001 From: Milton Wu Date: Sat, 17 Jun 2023 18:30:13 +0800 Subject: [PATCH] [BiometricsV2] Refactor FindRfpsFragment Refactor FingerprintEnrollFindRfpsFragment to kotlin and add bindView() method for it Bug: 286197313 Test: atest FingerprintEnrollmentActivityTest Test: Manually test enrollment as Rear fingerpint device Change-Id: Ifa11888f3e848fb7127e14ddcc41d02277163a1a --- .../FingerprintEnrollFindRfpsFragment.java | 270 ----------------- .../view/FingerprintEnrollFindRfpsFragment.kt | 286 ++++++++++++++++++ 2 files changed, 286 insertions(+), 270 deletions(-) delete mode 100644 src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.java create mode 100644 src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.kt diff --git a/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.java b/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.java deleted file mode 100644 index 61f8e823fe0..00000000000 --- a/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.biometrics2.ui.view; - -import static android.hardware.fingerprint.FingerprintManager.ENROLL_FIND_SENSOR; -import static android.view.View.OnClickListener; - -import android.app.Activity; -import android.content.Context; -import android.hardware.fingerprint.FingerprintManager; -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.Surface; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.Observer; -import androidx.lifecycle.ViewModelProvider; - -import com.android.settings.R; -import com.android.settings.biometrics.fingerprint.FingerprintFindSensorAnimation; -import com.android.settings.biometrics2.ui.model.EnrollmentProgress; -import com.android.settings.biometrics2.ui.model.EnrollmentStatusMessage; -import com.android.settings.biometrics2.ui.viewmodel.DeviceRotationViewModel; -import com.android.settings.biometrics2.ui.viewmodel.FingerprintEnrollFindSensorViewModel; -import com.android.settings.biometrics2.ui.viewmodel.FingerprintEnrollProgressViewModel; - -import com.google.android.setupcompat.template.FooterBarMixin; -import com.google.android.setupcompat.template.FooterButton; -import com.google.android.setupdesign.GlifLayout; - -/** - * Fragment explaining the side fingerprint sensor location for fingerprint enrollment. - * It interacts with ProgressViewModel, and FingerprintFindSensorAnimation. - *
- | Has                 | UDFPS | SFPS | Other (Rear FPS) |
- |---------------------|-------|------|------------------|
- | Primary button      | Yes   | No   | No               |
- | Illustration Lottie | Yes   | Yes  | No               |
- | Animation           | No    | No   | Depend on layout |
- | Progress ViewModel  | No    | Yes  | Yes              |
- | Orientation detect  | No    | Yes  | No               |
- | Foldable detect     | No    | Yes  | No               |
- 
- */ -public class FingerprintEnrollFindRfpsFragment extends Fragment { - - private static final boolean DEBUG = false; - private static final String TAG = "FingerprintEnrollFindRfpsFragment"; - - private FingerprintEnrollFindSensorViewModel mViewModel; - private FingerprintEnrollProgressViewModel mProgressViewModel; - private DeviceRotationViewModel mRotationViewModel; - - private View mView; - private GlifLayout mGlifLayout; - private FooterBarMixin mFooterBarMixin; - private final OnClickListener mOnSkipClickListener = (v) -> mViewModel.onSkipButtonClick(); - @Nullable private FingerprintFindSensorAnimation mAnimation; - @Surface.Rotation private int mLastRotation = -1; - - private final Observer mRotationObserver = rotation -> { - if (DEBUG) { - Log.d(TAG, "rotationObserver " + rotation); - } - if (rotation != null) { - onRotationChanged(rotation); - } - }; - - private final Observer mProgressObserver = progress -> { - if (DEBUG) { - Log.d(TAG, "mProgressObserver(" + progress + ")"); - } - if (progress != null && !progress.isInitialStep()) { - stopLookingForFingerprint(true); - } - }; - - private final Observer mLastCancelMessageObserver = errorMessage -> { - if (DEBUG) { - Log.d(TAG, "mLastCancelMessageObserver(" + errorMessage + ")"); - } - if (errorMessage != null) { - onLastCancelMessage(errorMessage); - } - }; - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - final Context context = inflater.getContext(); - mView = inflater.inflate(R.layout.fingerprint_enroll_find_sensor, container, false); - mGlifLayout = mView.findViewById(R.id.setup_wizard_layout); - mFooterBarMixin = mGlifLayout.getMixin(FooterBarMixin.class); - mFooterBarMixin.setSecondaryButton( - new FooterButton.Builder(context) - .setText(R.string.security_settings_fingerprint_enroll_enrolling_skip) - .setButtonType(FooterButton.ButtonType.SKIP) - .setTheme(R.style.SudGlifButton_Secondary) - .build() - ); - View animationView = mView.findViewById(R.id.fingerprint_sensor_location_animation); - if (animationView instanceof FingerprintFindSensorAnimation) { - mAnimation = (FingerprintFindSensorAnimation) animationView; - } - return mView; - } - - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - final Activity activity = getActivity(); - final GlifLayoutHelper glifLayoutHelper = new GlifLayoutHelper(activity, mGlifLayout); - glifLayoutHelper.setHeaderText( - R.string.security_settings_fingerprint_enroll_find_sensor_title); - glifLayoutHelper.setDescriptionText( - getText(R.string.security_settings_fingerprint_enroll_find_sensor_message)); - mFooterBarMixin.getSecondaryButton().setOnClickListener(mOnSkipClickListener); - } - - @Override - public void onStart() { - super.onStart(); - - if (DEBUG) { - Log.d(TAG, "onStart(), start looking for fingerprint, animation exist:" - + (mAnimation != null)); - } - startLookingForFingerprint(); - } - - @Override - public void onResume() { - final LiveData rotationLiveData = mRotationViewModel.getLiveData(); - mLastRotation = rotationLiveData.getValue(); - rotationLiveData.observe(this, mRotationObserver); - - if (mAnimation != null) { - if (DEBUG) { - Log.d(TAG, "onResume(), start animation"); - } - mAnimation.startAnimation(); - } - super.onResume(); - } - - @Override - public void onPause() { - if (mAnimation != null) { - if (DEBUG) { - Log.d(TAG, "onPause(), pause animation"); - } - mAnimation.pauseAnimation(); - } - super.onPause(); - } - - @Override - public void onStop() { - super.onStop(); - final boolean isEnrolling = mProgressViewModel.isEnrolling(); - if (DEBUG) { - Log.d(TAG, "onStop(), current enrolling: " + isEnrolling + ", animation exist:" - + (mAnimation != null)); - } - if (isEnrolling) { - stopLookingForFingerprint(false); - } - } - - private void startLookingForFingerprint() { - if (mProgressViewModel.isEnrolling()) { - Log.d(TAG, "startLookingForFingerprint(), failed because isEnrolling is true before" - + " starting"); - return; - } - - final boolean startResult = mProgressViewModel.startEnrollment(ENROLL_FIND_SENSOR); - if (!startResult) { - Log.e(TAG, "startLookingForFingerprint(), failed to start enrollment"); - } - mProgressViewModel.getProgressLiveData().observe(this, mProgressObserver); - } - - private void stopLookingForFingerprint(boolean waitForLastCancelErrMsg) { - if (!mProgressViewModel.isEnrolling()) { - Log.d(TAG, "stopLookingForFingerprint(), failed because isEnrolling is false before" - + " stopping"); - return; - } - - if (waitForLastCancelErrMsg) { - mProgressViewModel.clearErrorMessageLiveData(); // Prevent got previous error message - mProgressViewModel.getErrorMessageLiveData().observe(this, - mLastCancelMessageObserver); - } - - mProgressViewModel.getProgressLiveData().removeObserver(mProgressObserver); - final boolean cancelResult = mProgressViewModel.cancelEnrollment(); - if (!cancelResult) { - Log.e(TAG, "stopLookingForFingerprint(), failed to cancel enrollment"); - } - } - - private void onRotationChanged(@Surface.Rotation int newRotation) { - if (DEBUG) { - Log.d(TAG, "onRotationChanged() from " + mLastRotation + " to " + newRotation); - } - if (newRotation % 2 != mLastRotation % 2) { - // Fragment is going to be recreated, just stopLookingForFingerprint() here. - stopLookingForFingerprint(true); - } - } - - private void onLastCancelMessage(@NonNull EnrollmentStatusMessage errorMessage) { - if (errorMessage.getMsgId() == FingerprintManager.FINGERPRINT_ERROR_CANCELED) { - final EnrollmentProgress progress = mProgressViewModel.getProgressLiveData().getValue(); - mProgressViewModel.clearProgressLiveData(); - mProgressViewModel.getErrorMessageLiveData().removeObserver(mLastCancelMessageObserver); - if (progress != null && !progress.isInitialStep()) { - mViewModel.onStartButtonClick(); - } - } else { - Log.e(TAG, "mErrorMessageObserver(" + errorMessage + ")"); - } - } - - @Override - public void onDestroy() { - if (mAnimation != null) { - if (DEBUG) { - Log.d(TAG, "onDestroy(), stop animation"); - } - mAnimation.stopAnimation(); - } - super.onDestroy(); - } - - @Override - public void onAttach(@NonNull Context context) { - final FragmentActivity activity = getActivity(); - final ViewModelProvider provider = new ViewModelProvider(activity); - mViewModel = provider.get(FingerprintEnrollFindSensorViewModel.class); - mProgressViewModel = provider.get(FingerprintEnrollProgressViewModel.class); - mRotationViewModel = provider.get(DeviceRotationViewModel.class); - super.onAttach(context); - } -} diff --git a/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.kt b/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.kt new file mode 100644 index 00000000000..8f47abc58cc --- /dev/null +++ b/src/com/android/settings/biometrics2/ui/view/FingerprintEnrollFindRfpsFragment.kt @@ -0,0 +1,286 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.biometrics2.ui.view + +import android.content.Context +import android.hardware.fingerprint.FingerprintManager +import android.hardware.fingerprint.FingerprintManager.ENROLL_FIND_SENSOR +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.Surface +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentActivity +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider +import com.android.settings.R +import com.android.settings.biometrics.fingerprint.FingerprintFindSensorAnimation +import com.android.settings.biometrics2.ui.model.EnrollmentProgress +import com.android.settings.biometrics2.ui.model.EnrollmentStatusMessage +import com.android.settings.biometrics2.ui.viewmodel.DeviceRotationViewModel +import com.android.settings.biometrics2.ui.viewmodel.FingerprintEnrollFindSensorViewModel +import com.android.settings.biometrics2.ui.viewmodel.FingerprintEnrollProgressViewModel +import com.google.android.setupcompat.template.FooterBarMixin +import com.google.android.setupcompat.template.FooterButton +import com.google.android.setupdesign.GlifLayout + +/** + * Fragment explaining the side fingerprint sensor location for fingerprint enrollment. + * It interacts with ProgressViewModel, and FingerprintFindSensorAnimation. + *
+ * | Has                 | UDFPS | SFPS | Other (Rear FPS) |
+ * |---------------------|-------|------|------------------|
+ * | Primary button      | Yes   | No   | No               |
+ * | Illustration Lottie | Yes   | Yes  | No               |
+ * | Animation           | No    | No   | Depend on layout |
+ * | Progress ViewModel  | No    | Yes  | Yes              |
+ * | Orientation detect  | No    | Yes  | No               |
+ * | Foldable detect     | No    | Yes  | No               |
+ * 
+ */ +class FingerprintEnrollFindRfpsFragment : Fragment() { + + private var _viewModel: FingerprintEnrollFindSensorViewModel? = null + private val viewModel: FingerprintEnrollFindSensorViewModel + get() = _viewModel!! + + private var _progressViewModel: FingerprintEnrollProgressViewModel? = null + private val progressViewModel: FingerprintEnrollProgressViewModel + get() = _progressViewModel!! + + private var _rotationViewModel: DeviceRotationViewModel? = null + private val rotationViewModel: DeviceRotationViewModel + get() = _rotationViewModel!! + + private var findRfpsView: GlifLayout? = null + + private val onSkipClickListener = + View.OnClickListener { _: View? -> viewModel.onSkipButtonClick() } + + private var animation: FingerprintFindSensorAnimation? = null + + @Surface.Rotation + private var lastRotation = -1 + + private val rotationObserver = Observer { rotation: Int? -> + if (DEBUG) { + Log.d(TAG, "rotationObserver $rotation") + } + rotation?.let { onRotationChanged(it) } + } + + private val progressObserver: Observer = + Observer { progress: EnrollmentProgress? -> + if (DEBUG) { + Log.d(TAG, "progressObserver($progress)") + } + if (progress != null && !progress.isInitialStep) { + stopLookingForFingerprint(true) + } + } + + private val lastCancelMessageObserver: Observer = + Observer { errorMessage: EnrollmentStatusMessage? -> + if (DEBUG) { + Log.d(TAG, "lastCancelMessageObserver($errorMessage)") + } + errorMessage?.let { onLastCancelMessage(it) } + } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + findRfpsView = inflater.inflate( + R.layout.fingerprint_enroll_find_sensor, + container, + false + ) as GlifLayout + + val animationView = findRfpsView!!.findViewById( + R.id.fingerprint_sensor_location_animation + ) + if (animationView is FingerprintFindSensorAnimation) { + animation = animationView + } + + return findRfpsView!! + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + requireActivity().bindFingerprintEnrollFindRfpsView( + view = findRfpsView!!, + onSkipClickListener = onSkipClickListener + ) + } + + override fun onStart() { + super.onStart() + if (DEBUG) { + Log.d( + TAG, + "onStart(), start looking for fingerprint, animation exist:${animation != null}" + ) + } + startLookingForFingerprint() + } + + override fun onResume() { + val rotationLiveData: LiveData = rotationViewModel.liveData + lastRotation = rotationLiveData.value!! + rotationLiveData.observe(this, rotationObserver) + animation?.let { + if (DEBUG) { + Log.d(TAG, "onResume(), start animation") + } + it.startAnimation() + } + super.onResume() + } + + override fun onPause() { + animation?.let { + if (DEBUG) { + Log.d(TAG, "onPause(), pause animation") + } + it.pauseAnimation() + } + super.onPause() + } + + override fun onStop() { + super.onStop() + val isEnrolling: Boolean = progressViewModel.isEnrolling + if (DEBUG) { + Log.d( + TAG, + "onStop(), current enrolling: ${isEnrolling}, animation exist:${animation != null}" + ) + } + if (isEnrolling) { + stopLookingForFingerprint(false) + } + } + + private fun startLookingForFingerprint() { + if (progressViewModel.isEnrolling) { + Log.d( + TAG, + "startLookingForFingerprint(), failed because isEnrolling is true before starting" + ) + return + } + val startResult: Boolean = progressViewModel.startEnrollment(ENROLL_FIND_SENSOR) + if (!startResult) { + Log.e(TAG, "startLookingForFingerprint(), failed to start enrollment") + } + progressViewModel.progressLiveData.observe(this, progressObserver) + } + + private fun stopLookingForFingerprint(waitForLastCancelErrMsg: Boolean) { + if (!progressViewModel.isEnrolling) { + Log.d( + TAG, + "stopLookingForFingerprint(), failed because isEnrolling is false before stopping" + ) + return + } + if (waitForLastCancelErrMsg) { + progressViewModel.clearErrorMessageLiveData() // Prevent got previous error message + progressViewModel.errorMessageLiveData.observe(this, lastCancelMessageObserver) + } + progressViewModel.progressLiveData.removeObserver(progressObserver) + val cancelResult: Boolean = progressViewModel.cancelEnrollment() + if (!cancelResult) { + Log.e(TAG, "stopLookingForFingerprint(), failed to cancel enrollment") + } + } + + private fun onRotationChanged(@Surface.Rotation newRotation: Int) { + if (DEBUG) { + Log.d(TAG, "onRotationChanged() from $lastRotation to $newRotation") + } + if (newRotation % 2 != lastRotation % 2) { + // Fragment is going to be recreated, just stopLookingForFingerprint() here. + stopLookingForFingerprint(true) + } + } + + private fun onLastCancelMessage(errorMessage: EnrollmentStatusMessage) { + if (errorMessage.msgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED) { + val progress: EnrollmentProgress? = progressViewModel.progressLiveData.value + progressViewModel.clearProgressLiveData() + progressViewModel.errorMessageLiveData.removeObserver(lastCancelMessageObserver) + if (progress != null && !progress.isInitialStep) { + viewModel.onStartButtonClick() + } + } else { + Log.e(TAG, "errorMessageObserver($errorMessage)") + } + } + + override fun onDestroy() { + animation?.let { + if (DEBUG) { + Log.d(TAG, "onDestroy(), stop animation") + } + it.stopAnimation() + } + super.onDestroy() + } + + override fun onAttach(context: Context) { + ViewModelProvider(requireActivity()).let { provider -> + _viewModel = provider[FingerprintEnrollFindSensorViewModel::class.java] + _progressViewModel = provider[FingerprintEnrollProgressViewModel::class.java] + _rotationViewModel = provider[DeviceRotationViewModel::class.java] + } + super.onAttach(context) + } + + companion object { + private const val DEBUG = false + private const val TAG = "FingerprintEnrollFindRfpsFragment" + } +} + +fun FragmentActivity.bindFingerprintEnrollFindRfpsView( + view: GlifLayout, + onSkipClickListener: View.OnClickListener, +) { + GlifLayoutHelper(this, view).let { + it.setHeaderText( + R.string.security_settings_fingerprint_enroll_find_sensor_title + ) + it.setDescriptionText( + getText(R.string.security_settings_fingerprint_enroll_find_sensor_message) + ) + } + + view.getMixin(FooterBarMixin::class.java).secondaryButton = + FooterButton.Builder(this) + .setText(R.string.security_settings_fingerprint_enroll_enrolling_skip) + .setButtonType(FooterButton.ButtonType.SKIP) + .setTheme(R.style.SudGlifButton_Secondary) + .build() + .also { + it.setOnClickListener(onSkipClickListener) + } +}