@@ -580,6 +580,7 @@ private static final class PrivateProfileAutoLockSettingsListenersMap {
580580
581581 private final UserManagerService mUms ;
582582 private final Object mListenersMapLock = new Object ();
583+ private volatile boolean mIsMainUserListenerAdded = false ;
583584 @ GuardedBy ("mListenersMapLock" )
584585 private final SparseArray <PrivateProfileAutoLockSettingsListeners > mListenersMap ;
585586
@@ -588,76 +589,72 @@ private PrivateProfileAutoLockSettingsListenersMap(UserManagerService ums) {
588589 this .mListenersMap = new SparseArray <>();
589590 }
590591
591- private void addListeners (@ UserIdInt int parentUserId , boolean initialize ) {
592+ private void addListeners (@ UserIdInt int userId , boolean initialize ) {
592593 UserData userData ;
593594 synchronized (mUms .mUsersLock ) {
594- userData = mUms .getUserDataLU (parentUserId );
595+ userData = mUms .getUserDataLU (userId );
595596 }
597+ addListeners (userData , initialize );
598+ }
596599
597- if (!isUserFullAndCanHavePrivateProfilesInner (userData )) {
600+ private void addListeners (@ Nullable UserData userData , boolean initialize ) {
601+ if (!canAddOrRemoveListeners (userData , /* add = */ true )) {
598602 return ;
599603 }
600604
605+ addListenersUnchecked (userData , initialize );
606+ }
607+
608+ private void addListenersUnchecked (final UserData userData , boolean initialize ) {
609+ final int userId = userData .info .id ;
601610 synchronized (mListenersMapLock ) {
602- var listeners = new PrivateProfileAutoLockSettingsListeners (mUms , parentUserId );
603- mListenersMap .append (parentUserId , listeners );
611+ var listeners = new PrivateProfileAutoLockSettingsListeners (mUms , userId );
612+ mListenersMap .append (userId , listeners );
613+ if (userData .info .isMain ()) {
614+ mIsMainUserListenerAdded = true ;
615+ }
604616 if (initialize ) {
605- listeners .regiserObserver ();
606- listeners .autoLockPrivateSpace ();
617+ listeners .initialize ();
607618 }
608619 }
609620 }
610621
611- private void removeListeners (@ UserIdInt int parentUserId ) {
622+ private void removeListeners (@ UserIdInt int userId ) {
612623 UserData userData ;
613624 synchronized (mUms .mUsersLock ) {
614- userData = mUms .getUserDataLU (parentUserId );
615- }
616-
617- if (!isUserFullAndCanHavePrivateProfilesInner (userData )) {
618- return ;
625+ userData = mUms .getUserDataLU (userId );
619626 }
627+ removeListeners (userData );
628+ }
620629
621- if (userData .info .isMain ()) {
630+ private void removeListeners (final @ Nullable UserData userData ) {
631+ if (!canAddOrRemoveListeners (userData , /* add = */ false )) {
622632 return ;
623633 }
624634
625- removeListenersUnchecked (parentUserId );
635+ removeListenersUnchecked (userData );
626636 }
627637
628- private void removeListenersUnchecked (@ UserIdInt int parentUserId ) {
638+ private void removeListenersUnchecked (final UserData userData ) {
639+ final int userId = userData .info .id ;
629640 synchronized (mListenersMapLock ) {
630- var listeners = new PrivateProfileAutoLockSettingsListeners (mUms , parentUserId );
641+ var listeners = new PrivateProfileAutoLockSettingsListeners (mUms , userId );
631642 listeners .unregiserObserver ();
632643 listeners .cancelPendingAutoLockAlarms ();
633- mListenersMap .remove (parentUserId );
644+ mListenersMap .remove (userId );
634645 }
635646 }
636647
637648 @ Nullable
638649 private PrivateProfileAutoLockSettingsListeners getListeners (
639- @ UserIdInt int parentUserId ) {
640- UserData userData ;
641- synchronized (mUms .mUsersLock ) {
642- userData = mUms .getUserDataLU (parentUserId );
643- }
644-
645- if (!isUserFullAndCanHavePrivateProfilesInner (userData )) {
646- return null ;
647- }
648-
649- return getListenersUnchecked (parentUserId );
650- }
651-
652- @ Nullable
653- private PrivateProfileAutoLockSettingsListeners getListenersUnchecked (
654- @ UserIdInt int parentUserId ) {
650+ @ UserIdInt int userId ) {
655651 synchronized (mListenersMapLock ) {
656- return mListenersMap .get (parentUserId );
652+ return mListenersMap .get (userId );
657653 }
658654 }
659655
660- private boolean isUserFullAndCanHavePrivateProfilesInner (@ Nullable UserData userData ) {
656+ // add = false implies removal
657+ private boolean canAddOrRemoveListeners (@ Nullable UserData userData , boolean add ) {
661658 if (userData == null ) {
662659 return false ;
663660 }
@@ -666,6 +663,19 @@ private boolean isUserFullAndCanHavePrivateProfilesInner(@Nullable UserData user
666663 return false ;
667664 }
668665
666+ // Disallow adding again or removal of listeners on main user.
667+ if (add ) {
668+ if (userData .info .isMain ()) {
669+ if (mIsMainUserListenerAdded ) {
670+ return false ;
671+ }
672+ }
673+ } else {
674+ if (userData .info .isMain ()) {
675+ return false ;
676+ }
677+ }
678+
669679 return true ;
670680 }
671681 }
@@ -700,6 +710,18 @@ private Handler getHandler() {
700710 return mUms .mHandler ;
701711 }
702712
713+ private void initialize () {
714+ regiserObserver ();
715+ setOrUpdateAutoLockPreferenceForPrivateProfile (
716+ Settings .Secure .getIntForUser (mUms .mContext .getContentResolver (),
717+ Settings .Secure .PRIVATE_SPACE_AUTO_LOCK ,
718+ Settings .Secure .PRIVATE_SPACE_AUTO_LOCK_AFTER_DEVICE_RESTART ,
719+ mParentUserId ));
720+ if (mUms .isAutoLockingPrivateSpaceOnRestartsEnabled ()){
721+ autoLockPrivateSpace ();
722+ }
723+ }
724+
703725 private void regiserObserver () {
704726 getContext ().getContentResolver ().registerContentObserverAsUser (
705727 Settings .Secure .getUriFor (Settings .Secure .PRIVATE_SPACE_AUTO_LOCK ),
@@ -994,7 +1016,7 @@ void setOrUpdateAutoLockPreferenceForPrivateProfileOfMainUser(
9941016 int mainUserId = getMainUserIdUnchecked ();
9951017 if (mainUserId != UserHandle .USER_NULL ) {
9961018 PrivateProfileAutoLockSettingsListeners listeners =
997- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1019+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
9981020 if (listeners != null ) {
9991021 listeners .setOrUpdateAutoLockPreferenceForPrivateProfile (autoLockPreference );
10001022 }
@@ -1006,7 +1028,7 @@ void maybeScheduleAlarmToAutoLockPrivateSpaceOfMainUser() {
10061028 int mainUserId = getMainUserIdUnchecked ();
10071029 if (mainUserId != UserHandle .USER_NULL ) {
10081030 PrivateProfileAutoLockSettingsListeners listeners =
1009- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1031+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
10101032 if (listeners != null ) {
10111033 listeners .maybeScheduleAlarmToAutoLockPrivateSpace ();
10121034 }
@@ -1018,7 +1040,7 @@ void scheduleAlarmToAutoLockPrivateSpaceOfMainUser(int userId, long delayInMilli
10181040 int mainUserId = getMainUserIdUnchecked ();
10191041 if (mainUserId != UserHandle .USER_NULL ) {
10201042 PrivateProfileAutoLockSettingsListeners listeners =
1021- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1043+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
10221044 if (listeners != null ) {
10231045 listeners .scheduleAlarmToAutoLockPrivateSpace (userId , delayInMillis );
10241046 }
@@ -1030,7 +1052,7 @@ void tryAutoLockingPrivateSpaceOnKeyguardChangedOnMainUser(boolean isKeyguardLoc
10301052 int mainUserId = getMainUserIdUnchecked ();
10311053 if (mainUserId != UserHandle .USER_NULL ) {
10321054 PrivateProfileAutoLockSettingsListeners listeners =
1033- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1055+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
10341056 if (listeners != null ) {
10351057 listeners .tryAutoLockingPrivateSpaceOnKeyguardChanged (isKeyguardLocked );
10361058 }
@@ -1042,7 +1064,7 @@ void autoLockPrivateSpaceOfMainUser() {
10421064 int mainUserId = getMainUserIdUnchecked ();
10431065 if (mainUserId != UserHandle .USER_NULL ) {
10441066 PrivateProfileAutoLockSettingsListeners listeners =
1045- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1067+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
10461068 if (listeners != null ) {
10471069 listeners .autoLockPrivateSpace ();
10481070 }
@@ -1228,10 +1250,7 @@ public void onUserStarting(@NonNull TargetUser targetUser) {
12281250 && targetUser .isFull ()) {
12291251 mUms .setLastEnteredForegroundTimeToNow (user );
12301252 }
1231- if (targetUser .isFull () && !user .info .isMain ()) {
1232- mUms .mPrivateProfileAutoLockListeners .addListeners (
1233- targetUser .getUserIdentifier (), true );
1234- }
1253+ mUms .mPrivateProfileAutoLockListeners .addListeners (user , /* initialize = */ true );
12351254 }
12361255 }
12371256 }
@@ -1268,9 +1287,7 @@ public void onUserStopping(@NonNull TargetUser targetUser) {
12681287 user .startRealtime = 0 ;
12691288 user .unlockRealtime = 0 ;
12701289 }
1271- if (targetUser .isFull () && !user .info .isMain ()) {
1272- mUms .mPrivateProfileAutoLockListeners .removeListeners (targetUser .getUserIdentifier ());
1273- }
1290+ mUms .mPrivateProfileAutoLockListeners .removeListeners (user );
12741291 }
12751292 }
12761293 }
@@ -1370,22 +1387,13 @@ void systemReady() {
13701387 int mainUserId = getMainUserIdUnchecked ();
13711388 if (mainUserId != UserHandle .USER_NULL ) {
13721389 PrivateProfileAutoLockSettingsListeners listeners =
1373- mPrivateProfileAutoLockListeners .getListenersUnchecked (mainUserId );
1390+ mPrivateProfileAutoLockListeners .getListeners (mainUserId );
13741391 if (listeners != null ) {
1375- listeners .regiserObserver ();
1376- listeners .setOrUpdateAutoLockPreferenceForPrivateProfile (
1377- Settings .Secure .getIntForUser (mContext .getContentResolver (),
1378- Settings .Secure .PRIVATE_SPACE_AUTO_LOCK ,
1379- Settings .Secure .PRIVATE_SPACE_AUTO_LOCK_AFTER_DEVICE_RESTART ,
1380- mainUserId ));
1392+ listeners .initialize ();
13811393 }
13821394 }
13831395 }
13841396
1385- if (isAutoLockingPrivateSpaceOnRestartsEnabled ()) {
1386- autoLockPrivateSpaceOfMainUser ();
1387- }
1388-
13891397 showHsumNotificationIfNeeded ();
13901398
13911399 if (Flags .addUiForSoundsFromBackgroundUsers ()) {
0 commit comments