Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8f35d505 authored by Jeongsik Mun's avatar Jeongsik Mun Committed by Todd Kennedy
Browse files

Avoid running RegisteredServicesCache tasks on main thread

Since RegisteredServicesCache tasks from SyncManager and AccountManager
run on the main thread of SystemServer, It could cause draining
the main thread and end up with ANR when receiving a lot of broadcasts
or notifying changes through a listener.

This CL changes a few things to avoid running it on main thread.
1. RegisteredServicesCache uses BackgroundThread by default for
receiving broadcasts and a listener, instead of main thread.
2. AccountManager uses its own handler for a listener.

Bug: 171907687
Test: atest SyncManagerTest CtsSyncManagerTest AccountManagerServiceTest
Change-Id: I838c15b797b46fd58bb3c6ce9ee444a4737775d8
parent 9995d6ca
Loading
Loading
Loading
Loading
+7 −4
Original line number Original line Diff line number Diff line
@@ -42,6 +42,7 @@ import android.util.Xml;


import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.ArrayUtils;


import libcore.io.IoUtils;
import libcore.io.IoUtils;
@@ -161,18 +162,20 @@ public abstract class RegisteredServicesCache<V> {
        intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        intentFilter.addDataScheme("package");
        mContext.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, intentFilter, null, null);
        Handler handler = BackgroundThread.getHandler();
        mContext.registerReceiverAsUser(
                mPackageReceiver, UserHandle.ALL, intentFilter, null, handler);


        // Register for events related to sdcard installation.
        // Register for events related to sdcard installation.
        IntentFilter sdFilter = new IntentFilter();
        IntentFilter sdFilter = new IntentFilter();
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        mContext.registerReceiver(mExternalReceiver, sdFilter);
        mContext.registerReceiver(mExternalReceiver, sdFilter, null, handler);


        // Register for user-related events
        // Register for user-related events
        IntentFilter userFilter = new IntentFilter();
        IntentFilter userFilter = new IntentFilter();
        sdFilter.addAction(Intent.ACTION_USER_REMOVED);
        sdFilter.addAction(Intent.ACTION_USER_REMOVED);
        mContext.registerReceiver(mUserRemovedReceiver, userFilter);
        mContext.registerReceiver(mUserRemovedReceiver, userFilter, null, handler);
    }
    }


    private void handlePackageEvent(Intent intent, int userId) {
    private void handlePackageEvent(Intent intent, int userId) {
@@ -265,7 +268,7 @@ public abstract class RegisteredServicesCache<V> {


    public void setListener(RegisteredServicesCacheListener<V> listener, Handler handler) {
    public void setListener(RegisteredServicesCacheListener<V> listener, Handler handler) {
        if (handler == null) {
        if (handler == null) {
            handler = new Handler(mContext.getMainLooper());
            handler = BackgroundThread.getHandler();
        }
        }
        synchronized (this) {
        synchronized (this) {
            mHandler = handler;
            mHandler = handler;
+1 −1
Original line number Original line Diff line number Diff line
@@ -279,7 +279,7 @@ public class AccountManagerService
        mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
        mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
        mHandler = new MessageHandler(injector.getMessageHandlerLooper());
        mHandler = new MessageHandler(injector.getMessageHandlerLooper());
        mAuthenticatorCache = mInjector.getAccountAuthenticatorCache();
        mAuthenticatorCache = mInjector.getAccountAuthenticatorCache();
        mAuthenticatorCache.setListener(this, null /* Handler */);
        mAuthenticatorCache.setListener(this, mHandler);


        sThis.set(this);
        sThis.set(this);