Remove settings tasks on private space deletion

Bug: 342074934
Test: atest PrivateSpaceMaintainerTest
Test: Manual on device

Change-Id: Ic0555583ae0d7ea5650d819f1fccbd022b1d64c2
This commit is contained in:
Olivier Nshimiye
2024-06-12 13:34:01 +00:00
parent 7030928506
commit 38a08fe98d
2 changed files with 28 additions and 18 deletions

View File

@@ -61,8 +61,9 @@ public class PrivateSpaceMaintainer {
@GuardedBy("this") @GuardedBy("this")
private UserHandle mUserHandle; private UserHandle mUserHandle;
private final KeyguardManager mKeyguardManager; private final KeyguardManager mKeyguardManager;
/** This variable should be accessed via {@link #getBroadcastReceiver()} only. */ /** This variable should be accessed via {@link #getProfileBroadcastReceiver()} only. */
@Nullable private ProfileAvailabilityBroadcastReceiver mProfileAvailabilityBroadcastReceiver; @Nullable
private ProfileBroadcastReceiver mProfileBroadcastReceiver;
/** This is the default value for the hide private space entry point settings. */ /** This is the default value for the hide private space entry point settings. */
public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL = 0; public static final int HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL = 0;
@@ -147,7 +148,6 @@ public class PrivateSpaceMaintainer {
Log.i(TAG, "Deleting Private space with id: " + mUserHandle.getIdentifier()); Log.i(TAG, "Deleting Private space with id: " + mUserHandle.getIdentifier());
if (mUserManager.removeUser(mUserHandle)) { if (mUserManager.removeUser(mUserHandle)) {
Log.i(TAG, "Private space deleted"); Log.i(TAG, "Private space deleted");
unregisterBroadcastReceiver();
mUserHandle = null; mUserHandle = null;
return ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE; return ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE;
@@ -373,13 +373,15 @@ public class PrivateSpaceMaintainer {
&& android.multiuser.Flags.enablePrivateSpaceFeatures(); && android.multiuser.Flags.enablePrivateSpaceFeatures();
} }
/** {@link BroadcastReceiver} which handles the private profile's availability related /**
* broadcasts. * {@link BroadcastReceiver} which handles the private profile's availability and deletion
* related broadcasts.
*/ */
private final class ProfileAvailabilityBroadcastReceiver extends BroadcastReceiver { private final class ProfileBroadcastReceiver extends BroadcastReceiver {
void register() { void register() {
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PROFILE_UNAVAILABLE); filter.addAction(Intent.ACTION_PROFILE_UNAVAILABLE);
filter.addAction(Intent.ACTION_PROFILE_REMOVED);
mContext.registerReceiver(/* receiver= */ this, filter, Context.RECEIVER_NOT_EXPORTED); mContext.registerReceiver(/* receiver= */ this, filter, Context.RECEIVER_NOT_EXPORTED);
} }
@@ -391,6 +393,13 @@ public class PrivateSpaceMaintainer {
@Override @Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) { public void onReceive(@NonNull Context context, @NonNull Intent intent) {
UserHandle userHandle = intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class); UserHandle userHandle = intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class);
if (intent.getAction().equals(Intent.ACTION_PROFILE_REMOVED)) {
// This applies to all profiles getting removed, since there is no way to tell if
// it is a private profile that got removed.
removeSettingsAllTasks();
unregisterBroadcastReceiver();
return;
}
if (!userHandle.equals(getPrivateProfileHandle())) { if (!userHandle.equals(getPrivateProfileHandle())) {
Log.d(TAG, "Ignoring intent for non-private profile with user id " Log.d(TAG, "Ignoring intent for non-private profile with user id "
+ userHandle.getIdentifier()); + userHandle.getIdentifier());
@@ -407,7 +416,7 @@ public class PrivateSpaceMaintainer {
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) { || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
return; return;
} }
var broadcastReceiver = getBroadcastReceiver(); var broadcastReceiver = getProfileBroadcastReceiver();
if (broadcastReceiver == null) { if (broadcastReceiver == null) {
return; return;
} }
@@ -419,17 +428,18 @@ public class PrivateSpaceMaintainer {
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) { || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
return; return;
} }
if (mProfileAvailabilityBroadcastReceiver == null) { if (mProfileBroadcastReceiver == null) {
Log.w(TAG, "Requested to unregister when there is no receiver."); Log.w(TAG, "Requested to unregister when there is no receiver.");
return; return;
} }
mProfileAvailabilityBroadcastReceiver.unregister(); mProfileBroadcastReceiver.unregister();
mProfileAvailabilityBroadcastReceiver = null; mProfileBroadcastReceiver = null;
} }
/** Always use this getter to access {@link #mProfileAvailabilityBroadcastReceiver}. */ /** Always use this getter to access {@link #mProfileBroadcastReceiver}. */
@VisibleForTesting @VisibleForTesting
@Nullable synchronized ProfileAvailabilityBroadcastReceiver getBroadcastReceiver() { @Nullable
synchronized ProfileBroadcastReceiver getProfileBroadcastReceiver() {
if (!android.os.Flags.allowPrivateProfile() if (!android.os.Flags.allowPrivateProfile()
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) { || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
return null; return null;
@@ -438,16 +448,16 @@ public class PrivateSpaceMaintainer {
Log.e(TAG, "Cannot return a broadcast receiver when private space doesn't exist"); Log.e(TAG, "Cannot return a broadcast receiver when private space doesn't exist");
return null; return null;
} }
if (mProfileAvailabilityBroadcastReceiver == null) { if (mProfileBroadcastReceiver == null) {
mProfileAvailabilityBroadcastReceiver = new ProfileAvailabilityBroadcastReceiver(); mProfileBroadcastReceiver = new ProfileBroadcastReceiver();
} }
return mProfileAvailabilityBroadcastReceiver; return mProfileBroadcastReceiver;
} }
/** This is purely for testing purpose only, and should not be used elsewhere. */ /** This is purely for testing purpose only, and should not be used elsewhere. */
@VisibleForTesting @VisibleForTesting
synchronized void resetBroadcastReceiver() { synchronized void resetBroadcastReceiver() {
mProfileAvailabilityBroadcastReceiver = null; mProfileBroadcastReceiver = null;
} }
private void removeSettingsAllTasks() { private void removeSettingsAllTasks() {

View File

@@ -213,7 +213,7 @@ public class PrivateSpaceMaintainerTest {
privateSpaceMaintainer.deletePrivateSpace(); privateSpaceMaintainer.deletePrivateSpace();
privateSpaceMaintainer.createPrivateSpace(); privateSpaceMaintainer.createPrivateSpace();
// test that no exception is thrown, which would indicate that the receiver was registered. // test that no exception is thrown, which would indicate that the receiver was registered.
mContext.unregisterReceiver(privateSpaceMaintainer.getBroadcastReceiver()); mContext.unregisterReceiver(privateSpaceMaintainer.getProfileBroadcastReceiver());
privateSpaceMaintainer.resetBroadcastReceiver(); privateSpaceMaintainer.resetBroadcastReceiver();
} }
@@ -225,7 +225,7 @@ public class PrivateSpaceMaintainerTest {
PrivateSpaceMaintainer.getInstance(mContext); PrivateSpaceMaintainer.getInstance(mContext);
privateSpaceMaintainer.createPrivateSpace(); privateSpaceMaintainer.createPrivateSpace();
privateSpaceMaintainer.deletePrivateSpace(); privateSpaceMaintainer.deletePrivateSpace();
assertThat(privateSpaceMaintainer.getBroadcastReceiver()).isNull(); assertThat(privateSpaceMaintainer.getProfileBroadcastReceiver()).isNull();
} }
/** /**