Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c56a9256 authored by Shawn Lin's avatar Shawn Lin Committed by Michael Bestas
Browse files

Fixed "Unlock your phone" unexpectedlly turned ON after OTA

If the new settings key is not set, we should use the values of the old
keys as default value.

Bug: 444673089
Test: atest FaceSettingsAppsPreferenceControllerTest
        FaceSettingsKeyguardUnlockPreferenceControllerTest
        FingerprintSettingsAppsPreferenceControllerTest
        FingerprintSettingsKeyguardUnlockPreferenceControllerTest
Flag: EXEMPT BUGFIX
(cherry picked from commit 05f0884146a093e6311d7a30232d6850a28368ef)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:feb931006ee2d56c146156d5bb1491117841ccf3
Merged-In: I345defc78500c244e29e8595f5fbc705b95f4ba6
Change-Id: I345defc78500c244e29e8595f5fbc705b95f4ba6
parent 67c32d04
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.biometrics.face;

import static android.provider.Settings.Secure.BIOMETRIC_APP_ENABLED;
import static android.provider.Settings.Secure.FACE_APP_ENABLED;

import android.app.settings.SettingsEnums;
@@ -33,6 +34,7 @@ import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;

public class FaceSettingsAppsPreferenceController extends
        FaceSettingsPreferenceController {
    private static final int NOT_SET = -1;
    private static final int ON = 1;
    private static final int OFF = 0;
    private static final int DEFAULT = ON;
@@ -53,12 +55,23 @@ public class FaceSettingsAppsPreferenceController extends
                }
            }
        }

        // For OTA case: if FACE_APP_ENABLED is not set and BIOMETRIC_APP_ENABLED is set, set the
        // default value of the former to that of the latter.
        final int defValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FACE_APP_ENABLED, NOT_SET, getUserId());
        final int oldDefValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                BIOMETRIC_APP_ENABLED, NOT_SET, getUserId());
        if (defValue == NOT_SET && oldDefValue != NOT_SET) {
            Settings.Secure.putIntForUser(mContext.getContentResolver(),
                    FACE_APP_ENABLED, oldDefValue, getUserId());
        }
    }

    @Override
    public boolean isChecked() {
        return Settings.Secure.getIntForUser(mContext.getContentResolver(), FACE_APP_ENABLED,
                DEFAULT, getUserId()) == ON;
        return Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FACE_APP_ENABLED, DEFAULT, getUserId()) == ON;
    }

    @Override
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.biometrics.face;

import static android.provider.Settings.Secure.BIOMETRIC_KEYGUARD_ENABLED;
import static android.provider.Settings.Secure.FACE_KEYGUARD_ENABLED;

import android.app.settings.SettingsEnums;
@@ -32,6 +33,7 @@ import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;

public class FaceSettingsKeyguardUnlockPreferenceController extends
        FaceSettingsPreferenceController {
    private static final int NOT_SET = -1;
    private static final int ON = 1;
    private static final int OFF = 0;
    private static final int DEFAULT = ON;
@@ -44,6 +46,17 @@ public class FaceSettingsKeyguardUnlockPreferenceController extends
        super(context, key);
        mFaceManager = Utils.getFaceManagerOrNull(context);
        mUserManager = context.getSystemService(UserManager.class);

        // For OTA case: if FACE_KEYGUARD_ENABLED is not set and BIOMETRIC_KEYGUARD_ENABLED is set,
        // set the default value of the former to that of the latter.
        final int defValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FACE_KEYGUARD_ENABLED, NOT_SET, getUserId());
        final int oldDefValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                BIOMETRIC_KEYGUARD_ENABLED, NOT_SET, getUserId());
        if (defValue == NOT_SET && oldDefValue != NOT_SET) {
            Settings.Secure.putIntForUser(mContext.getContentResolver(),
                    FACE_KEYGUARD_ENABLED, oldDefValue, getUserId());
        }
    }

    @Override
+15 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.biometrics.fingerprint;

import static android.provider.Settings.Secure.BIOMETRIC_APP_ENABLED;
import static android.provider.Settings.Secure.FINGERPRINT_APP_ENABLED;

import android.app.settings.SettingsEnums;
@@ -31,6 +32,7 @@ import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;

public class FingerprintSettingsAppsPreferenceController
        extends FingerprintSettingsPreferenceController {
    private static final int NOT_SET = -1;
    private static final int ON = 1;
    private static final int OFF = 0;
    private static final int DEFAULT = ON;
@@ -41,12 +43,23 @@ public class FingerprintSettingsAppsPreferenceController
            @NonNull Context context, @NonNull String key) {
        super(context, key);
        mFingerprintManager = Utils.getFingerprintManagerOrNull(context);

        // For OTA case: if FINGERPRINT_APP_ENABLED is not set and BIOMETRIC_APP_ENABLED is set,
        // set the default value of the former to that of the latter.
        final int defValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FINGERPRINT_APP_ENABLED, NOT_SET, getUserId());
        final int oldDefValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                BIOMETRIC_APP_ENABLED, NOT_SET, getUserId());
        if (defValue == NOT_SET && oldDefValue != NOT_SET) {
            Settings.Secure.putIntForUser(mContext.getContentResolver(),
                    FINGERPRINT_APP_ENABLED, oldDefValue, getUserId());
        }
    }

    @Override
    public boolean isChecked() {
        return Settings.Secure.getIntForUser(mContext.getContentResolver(), FINGERPRINT_APP_ENABLED,
                DEFAULT, getUserId()) == ON;
        return Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FINGERPRINT_APP_ENABLED, DEFAULT, getUserId()) == ON;
    }

    @Override
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.biometrics.fingerprint;

import static android.provider.Settings.Secure.BIOMETRIC_KEYGUARD_ENABLED;
import static android.provider.Settings.Secure.FINGERPRINT_KEYGUARD_ENABLED;

import android.app.settings.SettingsEnums;
@@ -33,6 +34,7 @@ import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;
public class FingerprintSettingsKeyguardUnlockPreferenceController
        extends FingerprintSettingsPreferenceController {

    private static final int NOT_SET = -1;
    private static final int ON = 1;
    private static final int OFF = 0;
    private static final int DEFAULT = ON;
@@ -45,6 +47,17 @@ public class FingerprintSettingsKeyguardUnlockPreferenceController
        super(context, key);
        mFingerprintManager = Utils.getFingerprintManagerOrNull(context);
        mUserManager = context.getSystemService(UserManager.class);

        // For OTA case: if FINGERPRINT_KEYGUARD_ENABLED is not set and BIOMETRIC_KEYGUARD_ENABLED
        // is set, set the default value of the former to that of the latter.
        final int defValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                FINGERPRINT_KEYGUARD_ENABLED, NOT_SET, getUserId());
        final int oldDefValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                BIOMETRIC_KEYGUARD_ENABLED, NOT_SET, getUserId());
        if (defValue == NOT_SET && oldDefValue != NOT_SET) {
            Settings.Secure.putIntForUser(mContext.getContentResolver(),
                    FINGERPRINT_KEYGUARD_ENABLED, oldDefValue, getUserId());
        }
    }

    @Override
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.biometrics.face;

import static android.provider.Settings.Secure.BIOMETRIC_APP_ENABLED;
import static android.provider.Settings.Secure.FACE_APP_ENABLED;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.when;

import android.content.Context;
import android.hardware.biometrics.ComponentInfoInternal;
import android.hardware.biometrics.SensorProperties;
import android.hardware.face.FaceManager;
import android.hardware.face.FaceSensorProperties;
import android.hardware.face.FaceSensorPropertiesInternal;
import android.provider.Settings;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowSecureSettings;
import com.android.settings.testutils.shadow.ShadowUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowSecureSettings.class})
public class FaceSettingsAppsPreferenceControllerTest {
    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Spy
    private Context mContext = ApplicationProvider.getApplicationContext();
    @Mock
    private FaceManager mFaceManager;
    private FaceSettingsAppsPreferenceController mController;

    private FaceSensorPropertiesInternal mConvenienceSensorProperty =
            new FaceSensorPropertiesInternal(
                    0 /* sensorId */,
                    SensorProperties.STRENGTH_CONVENIENCE,
                    1 /* maxEnrollmentsPerUser */,
                    new ArrayList<ComponentInfoInternal>(),
                    FaceSensorProperties.TYPE_UNKNOWN,
                    true /* supportsFaceDetection */,
                    true /* supportsSelfIllumination */,
                    true /* resetLockoutRequiresChallenge */);
    private FakeFeatureFactory mFeatureFactory;

    @Before
    public void setUp() {
        when(mContext.getSystemService(Context.FACE_SERVICE)).thenReturn(mFaceManager);
        final ArrayList<FaceSensorPropertiesInternal> list = new ArrayList<>();
        list.add(mConvenienceSensorProperty);
        when(mFaceManager.getSensorPropertiesInternal()).thenReturn(list);
        mFeatureFactory = FakeFeatureFactory.setupForTest();
    }

    @After
    public void tearDown() {
        ShadowUtils.reset();
    }

    @Test
    public void isChecked_BiometricAppEnableOff_FaceAppEnabledNotSet_returnFalse() {
        Settings.Secure.putIntForUser(mContext.getContentResolver(),
                BIOMETRIC_APP_ENABLED, 0, mContext.getUserId());
        Settings.Secure.putIntForUser(mContext.getContentResolver(),
                FACE_APP_ENABLED, -1, mContext.getUserId());

        mController = new FaceSettingsAppsPreferenceController(
                mContext, "biometric_settings_face_app");

        assertThat(mController.isChecked()).isFalse();
    }

    @Test
    public void isChecked_BiometricAppEnableOff_FaceAppEnabledOn_returnTrue() {
        Settings.Secure.putIntForUser(mContext.getContentResolver(),
                BIOMETRIC_APP_ENABLED, 0, mContext.getUserId());
        Settings.Secure.putIntForUser(mContext.getContentResolver(),
                FACE_APP_ENABLED, 1, mContext.getUserId());

        mController = new FaceSettingsAppsPreferenceController(
                mContext, "biometric_settings_face_app");

        assertThat(mController.isChecked()).isTrue();
    }
}
Loading