Remove settings tasks on private space deletion
Bug: 342074934 Test: atest PrivateSpaceMaintainerTest Test: Manual on device Change-Id: Ic0555583ae0d7ea5650d819f1fccbd022b1d64c2
This commit is contained in:
@@ -61,8 +61,9 @@ public class PrivateSpaceMaintainer {
|
||||
@GuardedBy("this")
|
||||
private UserHandle mUserHandle;
|
||||
private final KeyguardManager mKeyguardManager;
|
||||
/** This variable should be accessed via {@link #getBroadcastReceiver()} only. */
|
||||
@Nullable private ProfileAvailabilityBroadcastReceiver mProfileAvailabilityBroadcastReceiver;
|
||||
/** This variable should be accessed via {@link #getProfileBroadcastReceiver()} only. */
|
||||
@Nullable
|
||||
private ProfileBroadcastReceiver mProfileBroadcastReceiver;
|
||||
|
||||
/** 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;
|
||||
@@ -147,7 +148,6 @@ public class PrivateSpaceMaintainer {
|
||||
Log.i(TAG, "Deleting Private space with id: " + mUserHandle.getIdentifier());
|
||||
if (mUserManager.removeUser(mUserHandle)) {
|
||||
Log.i(TAG, "Private space deleted");
|
||||
unregisterBroadcastReceiver();
|
||||
mUserHandle = null;
|
||||
|
||||
return ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE;
|
||||
@@ -373,13 +373,15 @@ public class PrivateSpaceMaintainer {
|
||||
&& 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() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(Intent.ACTION_PROFILE_UNAVAILABLE);
|
||||
filter.addAction(Intent.ACTION_PROFILE_REMOVED);
|
||||
mContext.registerReceiver(/* receiver= */ this, filter, Context.RECEIVER_NOT_EXPORTED);
|
||||
}
|
||||
|
||||
@@ -391,6 +393,13 @@ public class PrivateSpaceMaintainer {
|
||||
@Override
|
||||
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
|
||||
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())) {
|
||||
Log.d(TAG, "Ignoring intent for non-private profile with user id "
|
||||
+ userHandle.getIdentifier());
|
||||
@@ -407,7 +416,7 @@ public class PrivateSpaceMaintainer {
|
||||
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
|
||||
return;
|
||||
}
|
||||
var broadcastReceiver = getBroadcastReceiver();
|
||||
var broadcastReceiver = getProfileBroadcastReceiver();
|
||||
if (broadcastReceiver == null) {
|
||||
return;
|
||||
}
|
||||
@@ -419,17 +428,18 @@ public class PrivateSpaceMaintainer {
|
||||
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
|
||||
return;
|
||||
}
|
||||
if (mProfileAvailabilityBroadcastReceiver == null) {
|
||||
if (mProfileBroadcastReceiver == null) {
|
||||
Log.w(TAG, "Requested to unregister when there is no receiver.");
|
||||
return;
|
||||
}
|
||||
mProfileAvailabilityBroadcastReceiver.unregister();
|
||||
mProfileAvailabilityBroadcastReceiver = null;
|
||||
mProfileBroadcastReceiver.unregister();
|
||||
mProfileBroadcastReceiver = null;
|
||||
}
|
||||
|
||||
/** Always use this getter to access {@link #mProfileAvailabilityBroadcastReceiver}. */
|
||||
/** Always use this getter to access {@link #mProfileBroadcastReceiver}. */
|
||||
@VisibleForTesting
|
||||
@Nullable synchronized ProfileAvailabilityBroadcastReceiver getBroadcastReceiver() {
|
||||
@Nullable
|
||||
synchronized ProfileBroadcastReceiver getProfileBroadcastReceiver() {
|
||||
if (!android.os.Flags.allowPrivateProfile()
|
||||
|| !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
|
||||
return null;
|
||||
@@ -438,16 +448,16 @@ public class PrivateSpaceMaintainer {
|
||||
Log.e(TAG, "Cannot return a broadcast receiver when private space doesn't exist");
|
||||
return null;
|
||||
}
|
||||
if (mProfileAvailabilityBroadcastReceiver == null) {
|
||||
mProfileAvailabilityBroadcastReceiver = new ProfileAvailabilityBroadcastReceiver();
|
||||
if (mProfileBroadcastReceiver == null) {
|
||||
mProfileBroadcastReceiver = new ProfileBroadcastReceiver();
|
||||
}
|
||||
return mProfileAvailabilityBroadcastReceiver;
|
||||
return mProfileBroadcastReceiver;
|
||||
}
|
||||
|
||||
/** This is purely for testing purpose only, and should not be used elsewhere. */
|
||||
@VisibleForTesting
|
||||
synchronized void resetBroadcastReceiver() {
|
||||
mProfileAvailabilityBroadcastReceiver = null;
|
||||
mProfileBroadcastReceiver = null;
|
||||
}
|
||||
|
||||
private void removeSettingsAllTasks() {
|
||||
|
@@ -213,7 +213,7 @@ public class PrivateSpaceMaintainerTest {
|
||||
privateSpaceMaintainer.deletePrivateSpace();
|
||||
privateSpaceMaintainer.createPrivateSpace();
|
||||
// test that no exception is thrown, which would indicate that the receiver was registered.
|
||||
mContext.unregisterReceiver(privateSpaceMaintainer.getBroadcastReceiver());
|
||||
mContext.unregisterReceiver(privateSpaceMaintainer.getProfileBroadcastReceiver());
|
||||
privateSpaceMaintainer.resetBroadcastReceiver();
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ public class PrivateSpaceMaintainerTest {
|
||||
PrivateSpaceMaintainer.getInstance(mContext);
|
||||
privateSpaceMaintainer.createPrivateSpace();
|
||||
privateSpaceMaintainer.deletePrivateSpace();
|
||||
assertThat(privateSpaceMaintainer.getBroadcastReceiver()).isNull();
|
||||
assertThat(privateSpaceMaintainer.getProfileBroadcastReceiver()).isNull();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user