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

Commit 9afa37ac authored by Ajay Kumar Nadathur Sreenivasan's avatar Ajay Kumar Nadathur Sreenivasan Committed by Android (Google) Code Review
Browse files

Merge "Screen lock type metrics are not reported if user goes back" into oc-dr1-dev

parents 6d8f0b57 3d9fffed
Loading
Loading
Loading
Loading
+55 −11
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.settings.fingerprint;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.widget.Button;
import android.widget.TextView;
@@ -32,6 +34,25 @@ import com.android.settings.password.SetupChooseLockGeneric;
import com.android.settings.password.SetupSkipDialog;

public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntroduction {
    private static final String KEY_LOCK_SCREEN_PRESENT = "wasLockScreenPresent";
    private boolean mAlreadyHadLockScreenSetup = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            mAlreadyHadLockScreenSetup = isKeyguardSecure();
        } else {
            mAlreadyHadLockScreenSetup = savedInstanceState.getBoolean(
                    KEY_LOCK_SCREEN_PRESENT, false);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY_LOCK_SCREEN_PRESENT, mAlreadyHadLockScreenSetup);
    }

    @Override
    protected Intent getChooseLockIntent() {
@@ -70,7 +91,16 @@ public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntrodu

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FINGERPRINT_FIND_SENSOR_REQUEST) {
        // if lock was already present, do not return intent data since it must have been
        // reported in previous attempts
        if (requestCode == FINGERPRINT_FIND_SENSOR_REQUEST && isKeyguardSecure()
                && !mAlreadyHadLockScreenSetup) {
            data = getMetricIntent(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Intent getMetricIntent(Intent data) {
        if (data == null) {
            data = new Intent();
        }
@@ -78,17 +108,15 @@ public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntrodu
        data.putExtra(SetupChooseLockGeneric.
                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY,
                lockPatternUtils.getKeyguardStoredPasswordQuality(UserHandle.myUserId()));
        }
        super.onActivityResult(requestCode, resultCode, data);
        return data;
    }

    @Override
    protected void onCancelButtonClick() {
        KeyguardManager keyguardManager = getSystemService(KeyguardManager.class);
        if (keyguardManager.isKeyguardSecure()) {
        if (isKeyguardSecure()) {
            // If the keyguard is already set up securely (maybe the user added a backup screen
            // lock and skipped fingerprint), return RESULT_SKIP directly.
            setResult(RESULT_SKIP);
            setResult(RESULT_SKIP, mAlreadyHadLockScreenSetup ? null : getMetricIntent(null));
            finish();
        } else {
            setResult(SetupSkipDialog.RESULT_SKIP);
@@ -96,6 +124,22 @@ public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntrodu
        }
    }

    /**
     * Propagate lock screen metrics if the user goes back from the fingerprint setup screen
     * after having added lock screen to his device.
     */
    @Override
    public void onBackPressed() {
        if (!mAlreadyHadLockScreenSetup && isKeyguardSecure()) {
            setResult(Activity.RESULT_CANCELED, getMetricIntent(null));
        }
        super.onBackPressed();
    }

    private boolean isKeyguardSecure() {
        return getSystemService(KeyguardManager.class).isKeyguardSecure();
    }

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.FINGERPRINT_ENROLL_INTRO_SETUP;
+84 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.widget.Button;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.password.SetupChooseLockGeneric.SetupChooseLockGenericFragment;
import com.android.settings.password.SetupSkipDialog;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowEventLogWriter;
@@ -105,6 +106,89 @@ public class SetupFingerprintEnrollIntroductionTest {
                .isEqualTo(FingerprintEnrollBase.RESULT_SKIP);
    }

    @Test
    public void testBackKeyPress_shouldSetIntentDataIfLockScreenAdded() {
        getShadowKeyguardManager().setIsKeyguardSecure(false);

        mController.create().resume();
        getShadowKeyguardManager().setIsKeyguardSecure(true);
        SetupFingerprintEnrollIntroduction activity = mController.get();
        activity.onBackPressed();

        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        assertThat(shadowActivity.getResultIntent()).isNotNull();
        assertThat(shadowActivity.getResultIntent().hasExtra(
                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue();
    }

    @Test
    public void testBackKeyPress_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() {
        getShadowKeyguardManager().setIsKeyguardSecure(true);

        mController.create().resume();
        SetupFingerprintEnrollIntroduction activity = mController.get();
        activity.onBackPressed();

        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        assertThat(shadowActivity.getResultIntent()).isNull();
    }

    @Test
    public void testCancelClicked_shouldSetIntentDataIfLockScreenAdded() {
        getShadowKeyguardManager().setIsKeyguardSecure(false);

        SetupFingerprintEnrollIntroduction activity = mController.create().resume().get();
        final Button skipButton = activity.findViewById(R.id.fingerprint_cancel_button);
        getShadowKeyguardManager().setIsKeyguardSecure(true);
        skipButton.performClick();

        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        assertThat(shadowActivity.getResultIntent()).isNotNull();
        assertThat(shadowActivity.getResultIntent().hasExtra(
                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue();
    }

    @Test
    public void testCancelClicked_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() {
        getShadowKeyguardManager().setIsKeyguardSecure(true);

        SetupFingerprintEnrollIntroduction activity = mController.create().resume().get();
        final Button skipButton = activity.findViewById(R.id.fingerprint_cancel_button);
        skipButton.performClick();

        ShadowActivity shadowActivity = Shadows.shadowOf(activity);
        assertThat(shadowActivity.getResultIntent()).isNull();
    }

    @Test
    public void testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() {
        getShadowKeyguardManager().setIsKeyguardSecure(true);
        SetupFingerprintEnrollIntroduction activity = mController.create().resume().get();
        activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST,
                FingerprintEnrollBase.RESULT_FINISHED, null);
        assertThat(Shadows.shadowOf(activity).getResultIntent()).isNull();
    }

    @Test
    public void testOnResultFromFindSensor_shouldSetIntentDataIfLockScreenAdded() {
        getShadowKeyguardManager().setIsKeyguardSecure(false);
        SetupFingerprintEnrollIntroduction activity = mController.create().resume().get();
        getShadowKeyguardManager().setIsKeyguardSecure(true);
        activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST,
                FingerprintEnrollBase.RESULT_FINISHED, null);
        assertThat(Shadows.shadowOf(activity).getResultIntent()).isNotNull();
    }

    @Test
    public void testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenNotAdded() {
        getShadowKeyguardManager().setIsKeyguardSecure(false);
        SetupFingerprintEnrollIntroduction activity = mController.create().resume().get();
        activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST,
                FingerprintEnrollBase.RESULT_FINISHED, null);
        assertThat(Shadows.shadowOf(activity).getResultIntent()).isNull();
    }


    private ShadowKeyguardManager getShadowKeyguardManager() {
        return Shadows.shadowOf(application.getSystemService(KeyguardManager.class));
    }
+17 −0
Original line number Diff line number Diff line
@@ -19,12 +19,14 @@ package com.android.settings.testutils.shadow;
import android.app.admin.DevicePolicyManager;

import com.android.internal.widget.LockPatternUtils;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(LockPatternUtils.class)
public class ShadowLockPatternUtils {

    private int mPasswordQuality = 1;
    @Implementation
    public boolean isSecure(int id) {
        return true;
@@ -34,4 +36,19 @@ public class ShadowLockPatternUtils {
    public int getActivePasswordQuality(int userId) {
        return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
    }

    @Implementation
    public int getKeyguardStoredPasswordQuality(int userHandle) {
        return mPasswordQuality;
    }

    // Non-Android accessor.
    public int getPasswordQuality() {
        return mPasswordQuality;
    }

    // Non-Android accessor.
    public void setPasswordQuality(int passwordQuality) {
        mPasswordQuality = passwordQuality;
    }
}