fix broadcast receivers overflow for system_server
Every time when user is switched the `UsbSettingsManager` creates a new
instance of settings object for the new user. It leads to creating
several receivers and subscribing to some broadcast messages. Then
system is switched on other user, settings for the old user are removed
from the internal container, but receivers are not unsubscribed. As a
result, the number of receivers for the `system_server` process is
continuously increased and may exceed the allowed limit.
It is proposed to explicitly unscribe receivers before remove settings.
Test: flash a DUT with user build and then run:
> run cts -m CtsDevicePolicyManagerTestCases
at least two times, check that DUT is not in the recovery mode;
or switch user ~1000 times:
$ adb shell am switch-user 0
$ adb shell am switch-user 10
and check that logcat doesn't contain a line:
E SystemServiceManager: java.lang.IllegalStateException: \
Too many receivers, total of 1000, registered
Change-Id: I4bb9feb408ce7c321a56d0e573c45c8794ed6860
Signed-off-by: Sergii Piatakov <sergii.piatakov@globallogic.com>
This commit is contained in:
@@ -64,12 +64,13 @@ class MtpNotificationManager {
|
||||
|
||||
private final Context mContext;
|
||||
private final OnOpenInAppListener mListener;
|
||||
private final Receiver mReceiver;
|
||||
|
||||
MtpNotificationManager(Context context, OnOpenInAppListener listener) {
|
||||
mContext = context;
|
||||
mListener = listener;
|
||||
final Receiver receiver = new Receiver();
|
||||
context.registerReceiver(receiver, new IntentFilter(ACTION_OPEN_IN_APPS));
|
||||
mReceiver = new Receiver();
|
||||
context.registerReceiver(mReceiver, new IntentFilter(ACTION_OPEN_IN_APPS));
|
||||
}
|
||||
|
||||
void showNotification(UsbDevice device) {
|
||||
@@ -154,4 +155,8 @@ class MtpNotificationManager {
|
||||
static interface OnOpenInAppListener {
|
||||
void onOpenInApp(UsbDevice device);
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,6 +260,15 @@ class UsbProfileGroupSettingsManager {
|
||||
mUsbHandlerManager = usbResolveActivityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister all broadcast receivers. Must be called explicitly before
|
||||
* object deletion.
|
||||
*/
|
||||
public void unregisterReceivers() {
|
||||
mPackageMonitor.unregister();
|
||||
mMtpNotificationManager.unregister();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all defaults and denied packages for a user.
|
||||
*
|
||||
|
||||
@@ -124,6 +124,7 @@ class UsbSettingsManager {
|
||||
if (mSettingsByProfileGroup.indexOfKey(userToRemove.getIdentifier()) >= 0) {
|
||||
// The user to remove is the parent user of the group. The parent is the last user
|
||||
// that gets removed. All state will be removed with the user
|
||||
mSettingsByProfileGroup.get(userToRemove.getIdentifier()).unregisterReceivers();
|
||||
mSettingsByProfileGroup.remove(userToRemove.getIdentifier());
|
||||
} else {
|
||||
// We cannot find the parent user of the user that is removed, hence try to remove
|
||||
|
||||
Reference in New Issue
Block a user