Merge "Add newlyUnlocked to onTrustChanged" into tm-qpr-dev

This commit is contained in:
Derek Jedral
2022-12-16 03:34:27 +00:00
committed by Android (Google) Code Review
7 changed files with 49 additions and 24 deletions

View File

@@ -24,7 +24,7 @@ import java.util.List;
* {@hide} * {@hide}
*/ */
oneway interface ITrustListener { oneway interface ITrustListener {
void onTrustChanged(boolean enabled, int userId, int flags, void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, int flags,
in List<String> trustGrantedMessages); in List<String> trustGrantedMessages);
void onTrustManagedChanged(boolean managed, int userId); void onTrustManagedChanged(boolean managed, int userId);
void onTrustError(in CharSequence message); void onTrustError(in CharSequence message);

View File

@@ -22,6 +22,7 @@ import android.annotation.SystemService;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context; import android.content.Context;
import android.hardware.biometrics.BiometricSourceType; import android.hardware.biometrics.BiometricSourceType;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
@@ -45,6 +46,7 @@ public class TrustManager {
private static final String TAG = "TrustManager"; private static final String TAG = "TrustManager";
private static final String DATA_FLAGS = "initiatedByUser"; private static final String DATA_FLAGS = "initiatedByUser";
private static final String DATA_NEWLY_UNLOCKED = "newlyUnlocked";
private static final String DATA_MESSAGE = "message"; private static final String DATA_MESSAGE = "message";
private static final String DATA_GRANTED_MESSAGES = "grantedMessages"; private static final String DATA_GRANTED_MESSAGES = "grantedMessages";
@@ -171,13 +173,14 @@ public class TrustManager {
try { try {
ITrustListener.Stub iTrustListener = new ITrustListener.Stub() { ITrustListener.Stub iTrustListener = new ITrustListener.Stub() {
@Override @Override
public void onTrustChanged(boolean enabled, int userId, int flags, public void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId,
List<String> trustGrantedMessages) { int flags, List<String> trustGrantedMessages) {
Message m = mHandler.obtainMessage(MSG_TRUST_CHANGED, (enabled ? 1 : 0), userId, Message m = mHandler.obtainMessage(MSG_TRUST_CHANGED, (enabled ? 1 : 0), userId,
trustListener); trustListener);
if (flags != 0) { if (flags != 0) {
m.getData().putInt(DATA_FLAGS, flags); m.getData().putInt(DATA_FLAGS, flags);
} }
m.getData().putInt(DATA_NEWLY_UNLOCKED, newlyUnlocked ? 1 : 0);
m.getData().putCharSequenceArrayList( m.getData().putCharSequenceArrayList(
DATA_GRANTED_MESSAGES, (ArrayList) trustGrantedMessages); DATA_GRANTED_MESSAGES, (ArrayList) trustGrantedMessages);
m.sendToTarget(); m.sendToTarget();
@@ -265,9 +268,14 @@ public class TrustManager {
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch(msg.what) { switch(msg.what) {
case MSG_TRUST_CHANGED: case MSG_TRUST_CHANGED:
int flags = msg.peekData() != null ? msg.peekData().getInt(DATA_FLAGS) : 0; Bundle data = msg.peekData();
((TrustListener) msg.obj).onTrustChanged(msg.arg1 != 0, msg.arg2, flags, int flags = data != null ? data.getInt(DATA_FLAGS) : 0;
msg.getData().getStringArrayList(DATA_GRANTED_MESSAGES)); boolean enabled = msg.arg1 != 0;
int newlyUnlockedInt =
data != null ? data.getInt(DATA_NEWLY_UNLOCKED) : 0;
boolean newlyUnlocked = newlyUnlockedInt != 0;
((TrustListener) msg.obj).onTrustChanged(enabled, newlyUnlocked, msg.arg2,
flags, msg.getData().getStringArrayList(DATA_GRANTED_MESSAGES));
break; break;
case MSG_TRUST_MANAGED_CHANGED: case MSG_TRUST_MANAGED_CHANGED:
((TrustListener)msg.obj).onTrustManagedChanged(msg.arg1 != 0, msg.arg2); ((TrustListener)msg.obj).onTrustManagedChanged(msg.arg1 != 0, msg.arg2);
@@ -284,6 +292,8 @@ public class TrustManager {
/** /**
* Reports that the trust state has changed. * Reports that the trust state has changed.
* @param enabled If true, the system believes the environment to be trusted. * @param enabled If true, the system believes the environment to be trusted.
* @param newlyUnlocked If true, the system believes the device is newly unlocked due
* to the trust changing.
* @param userId The user, for which the trust changed. * @param userId The user, for which the trust changed.
* @param flags Flags specified by the trust agent when granting trust. See * @param flags Flags specified by the trust agent when granting trust. See
* {@link android.service.trust.TrustAgentService#grantTrust(CharSequence, long, int) * {@link android.service.trust.TrustAgentService#grantTrust(CharSequence, long, int)
@@ -291,7 +301,7 @@ public class TrustManager {
* @param trustGrantedMessages Messages to display to the user when trust has been granted * @param trustGrantedMessages Messages to display to the user when trust has been granted
* by one or more trust agents. * by one or more trust agents.
*/ */
void onTrustChanged(boolean enabled, int userId, int flags, void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, int flags,
List<String> trustGrantedMessages); List<String> trustGrantedMessages);
/** /**

View File

@@ -480,7 +480,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
} }
@Override @Override
public void onTrustChanged(boolean enabled, int userId, int flags, public void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, int flags,
List<String> trustGrantedMessages) { List<String> trustGrantedMessages) {
Assert.isMainThread(); Assert.isMainThread();
boolean wasTrusted = mUserHasTrust.get(userId, false); boolean wasTrusted = mUserHasTrust.get(userId, false);

View File

@@ -823,7 +823,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
mKeyguardUpdateMonitor.dispatchStartedWakingUp(PowerManager.WAKE_REASON_POWER_BUTTON); mKeyguardUpdateMonitor.dispatchStartedWakingUp(PowerManager.WAKE_REASON_POWER_BUTTON);
mTestableLooper.processAllMessages(); mTestableLooper.processAllMessages();
lockscreenBypassIsAllowed(); lockscreenBypassIsAllowed();
mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, true /* newlyUnlocked */,
KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */,
new ArrayList<>()); new ArrayList<>());
keyguardIsVisible(); keyguardIsVisible();
@@ -834,7 +834,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() { public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
mKeyguardUpdateMonitor.dispatchStartedWakingUp(PowerManager.WAKE_REASON_POWER_BUTTON); mKeyguardUpdateMonitor.dispatchStartedWakingUp(PowerManager.WAKE_REASON_POWER_BUTTON);
mTestableLooper.processAllMessages(); mTestableLooper.processAllMessages();
mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, true /* newlyUnlocked */,
KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, new ArrayList<>()); KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, new ArrayList<>());
keyguardIsVisible(); keyguardIsVisible();
verify(mFaceManager, never()).authenticate(any(), any(), any(), any(), anyInt(), verify(mFaceManager, never()).authenticate(any(), any(), any(), any(), anyInt(),
@@ -1049,8 +1049,8 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Test @Test
public void testGetUserCanSkipBouncer_whenTrust() { public void testGetUserCanSkipBouncer_whenTrust() {
int user = KeyguardUpdateMonitor.getCurrentUser(); int user = KeyguardUpdateMonitor.getCurrentUser();
mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */, mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, true /* newlyUnlocked */,
new ArrayList<>()); user, 0 /* flags */, new ArrayList<>());
assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
} }
@@ -1314,7 +1314,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
when(mStrongAuthTracker.hasUserAuthenticatedSinceBoot()).thenReturn(true); when(mStrongAuthTracker.hasUserAuthenticatedSinceBoot()).thenReturn(true);
// WHEN trust is enabled (ie: via smartlock) // WHEN trust is enabled (ie: via smartlock)
mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, true /* newlyUnlocked */,
KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, new ArrayList<>()); KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, new ArrayList<>());
// THEN we shouldn't listen for udfps // THEN we shouldn't listen for udfps
@@ -1418,7 +1418,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Test @Test
public void testShowTrustGrantedMessage_onTrustGranted() { public void testShowTrustGrantedMessage_onTrustGranted() {
// WHEN trust is enabled (ie: via some trust agent) with a trustGranted string // WHEN trust is enabled (ie: via some trust agent) with a trustGranted string
mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, true /* newlyUnlocked */,
KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */, KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */,
Arrays.asList("Unlocked by wearable")); Arrays.asList("Unlocked by wearable"));
@@ -1870,6 +1870,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// WHEN onTrustChanged with TRUST_DISMISS_KEYGUARD flag // WHEN onTrustChanged with TRUST_DISMISS_KEYGUARD flag
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
getCurrentUser() /* userId */, getCurrentUser() /* userId */,
TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD /* flags */, TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD /* flags */,
null /* trustGrantedMessages */); null /* trustGrantedMessages */);
@@ -1893,6 +1894,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// WHEN onTrustChanged with TRUST_DISMISS_KEYGUARD flag // WHEN onTrustChanged with TRUST_DISMISS_KEYGUARD flag
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
getCurrentUser() /* userId */, getCurrentUser() /* userId */,
TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD /* flags */, TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD /* flags */,
null /* trustGrantedMessages */); null /* trustGrantedMessages */);
@@ -1917,6 +1919,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// WHEN onTrustChanged for a different user // WHEN onTrustChanged for a different user
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
546 /* userId, not the current userId */, 546 /* userId, not the current userId */,
0 /* flags */, 0 /* flags */,
null /* trustGrantedMessages */); null /* trustGrantedMessages */);
@@ -1941,6 +1944,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// flags (temporary & rewable is active unlock) // flags (temporary & rewable is active unlock)
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
getCurrentUser() /* userId */, getCurrentUser() /* userId */,
TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD
| TrustAgentService.FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE /* flags */, | TrustAgentService.FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE /* flags */,
@@ -1968,6 +1972,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// WHEN onTrustChanged with INITIATED_BY_USER flag // WHEN onTrustChanged with INITIATED_BY_USER flag
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
getCurrentUser() /* userId, not the current userId */, getCurrentUser() /* userId, not the current userId */,
TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER /* flags */, TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER /* flags */,
null /* trustGrantedMessages */); null /* trustGrantedMessages */);
@@ -1992,6 +1997,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// WHEN onTrustChanged with INITIATED_BY_USER flag // WHEN onTrustChanged with INITIATED_BY_USER flag
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
true /* enabled */, true /* enabled */,
true /* newlyUnlocked */,
getCurrentUser() /* userId, not the current userId */, getCurrentUser() /* userId, not the current userId */,
TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER
| TrustAgentService.FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE /* flags */, | TrustAgentService.FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE /* flags */,
@@ -2309,6 +2315,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
private void currentUserDoesNotHaveTrust() { private void currentUserDoesNotHaveTrust() {
mKeyguardUpdateMonitor.onTrustChanged( mKeyguardUpdateMonitor.onTrustChanged(
false,
false, false,
KeyguardUpdateMonitor.getCurrentUser(), KeyguardUpdateMonitor.getCurrentUser(),
-1, -1,

View File

@@ -417,7 +417,7 @@ public class Fingerprint21UdfpsMock extends Fingerprint21 implements TrustManage
} }
@Override @Override
public void onTrustChanged(boolean enabled, int userId, int flags, public void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, int flags,
List<String> trustGrantedMessages) { List<String> trustGrantedMessages) {
mUserHasTrust.put(userId, enabled); mUserHasTrust.put(userId, enabled);
} }

View File

@@ -563,7 +563,12 @@ public class TrustManagerService extends SystemService {
changed = mUserIsTrusted.get(userId) != trusted; changed = mUserIsTrusted.get(userId) != trusted;
mUserIsTrusted.put(userId, trusted); mUserIsTrusted.put(userId, trusted);
} }
dispatchOnTrustChanged(trusted, userId, flags, getTrustGrantedMessages(userId)); dispatchOnTrustChanged(
trusted,
false /* newlyUnlocked */,
userId,
flags,
getTrustGrantedMessages(userId));
if (changed) { if (changed) {
refreshDeviceLockedForUser(userId); refreshDeviceLockedForUser(userId);
if (!trusted) { if (!trusted) {
@@ -628,7 +633,9 @@ public class TrustManagerService extends SystemService {
if (DEBUG) Slog.d(TAG, "pendingTrustState: " + pendingTrustState); if (DEBUG) Slog.d(TAG, "pendingTrustState: " + pendingTrustState);
boolean isNowTrusted = pendingTrustState == TrustState.TRUSTED; boolean isNowTrusted = pendingTrustState == TrustState.TRUSTED;
dispatchOnTrustChanged(isNowTrusted, userId, flags, getTrustGrantedMessages(userId)); boolean newlyUnlocked = !alreadyUnlocked && isNowTrusted;
dispatchOnTrustChanged(
isNowTrusted, newlyUnlocked, userId, flags, getTrustGrantedMessages(userId));
if (isNowTrusted != wasTrusted) { if (isNowTrusted != wasTrusted) {
refreshDeviceLockedForUser(userId); refreshDeviceLockedForUser(userId);
if (!isNowTrusted) { if (!isNowTrusted) {
@@ -643,8 +650,7 @@ public class TrustManagerService extends SystemService {
} }
} }
boolean wasLocked = !alreadyUnlocked; boolean shouldSendCallback = newlyUnlocked;
boolean shouldSendCallback = wasLocked && pendingTrustState == TrustState.TRUSTED;
if (shouldSendCallback) { if (shouldSendCallback) {
if (resultCallback != null) { if (resultCallback != null) {
if (DEBUG) Slog.d(TAG, "calling back with UNLOCKED_BY_GRANT"); if (DEBUG) Slog.d(TAG, "calling back with UNLOCKED_BY_GRANT");
@@ -1387,16 +1393,17 @@ public class TrustManagerService extends SystemService {
} }
} }
private void dispatchOnTrustChanged(boolean enabled, int userId, int flags, private void dispatchOnTrustChanged(boolean enabled, boolean newlyUnlocked, int userId,
@NonNull List<String> trustGrantedMessages) { int flags, @NonNull List<String> trustGrantedMessages) {
if (DEBUG) { if (DEBUG) {
Log.i(TAG, "onTrustChanged(" + enabled + ", " + userId + ", 0x" Log.i(TAG, "onTrustChanged(" + enabled + ", " + newlyUnlocked + ", " + userId + ", 0x"
+ Integer.toHexString(flags) + ")"); + Integer.toHexString(flags) + ")");
} }
if (!enabled) flags = 0; if (!enabled) flags = 0;
for (int i = 0; i < mTrustListeners.size(); i++) { for (int i = 0; i < mTrustListeners.size(); i++) {
try { try {
mTrustListeners.get(i).onTrustChanged(enabled, userId, flags, trustGrantedMessages); mTrustListeners.get(i).onTrustChanged(
enabled, newlyUnlocked, userId, flags, trustGrantedMessages);
} catch (DeadObjectException e) { } catch (DeadObjectException e) {
Slog.d(TAG, "Removing dead TrustListener."); Slog.d(TAG, "Removing dead TrustListener.");
mTrustListeners.remove(i); mTrustListeners.remove(i);

View File

@@ -63,6 +63,7 @@ class LockStateTrackingRule : TestRule {
inner class Listener : TrustListener { inner class Listener : TrustListener {
override fun onTrustChanged( override fun onTrustChanged(
enabled: Boolean, enabled: Boolean,
newlyUnlocked: Boolean,
userId: Int, userId: Int,
flags: Int, flags: Int,
trustGrantedMessages: MutableList<String> trustGrantedMessages: MutableList<String>