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

Commit 00ace073 authored by Roberto Cosenza's avatar Roberto Cosenza Committed by Android (Google) Code Review
Browse files

Merge "Don't initialize the People Service at boot but on demand. The service...

Merge "Don't initialize the People Service at boot but on demand. The service is not used by any known apps and we don't used any of it's  data." into main
parents e8e927a2 e0b3ad2e
Loading
Loading
Loading
Loading
+40 −28
Original line number Diff line number Diff line
@@ -63,9 +63,8 @@ public class PeopleService extends SystemService {

    private static final String TAG = "PeopleService";

    private DataManager mDataManager;
    @VisibleForTesting
    ConversationListenerHelper mConversationListenerHelper;
    private DataManager mLazyDataManager;
    private ConversationListenerHelper mLazyConversationListenerHelper;

    private PackageManagerInternal mPackageManagerInternal;

@@ -76,17 +75,30 @@ public class PeopleService extends SystemService {
     */
    public PeopleService(Context context) {
        super(context);
    }

    @VisibleForTesting
    ConversationListenerHelper getConversationListenerHelper() {
        if (mLazyConversationListenerHelper == null) {
            initLazyStuff();
        }
        return mLazyConversationListenerHelper;
    }

        mDataManager = new DataManager(context);
        mConversationListenerHelper = new ConversationListenerHelper();
        mDataManager.addConversationsListener(mConversationListenerHelper);
    private synchronized void initLazyStuff() {
        if (mLazyDataManager == null) {
            mLazyDataManager = new DataManager(getContext());
            mLazyDataManager.initialize();
            mLazyConversationListenerHelper = new ConversationListenerHelper();
            mLazyDataManager.addConversationsListener(mLazyConversationListenerHelper);
        }
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == PHASE_SYSTEM_SERVICES_READY) {
            mDataManager.initialize();
    private DataManager getDataManager() {
        if (mLazyDataManager == null) {
            initLazyStuff();
        }
        return mLazyDataManager;
    }

    @Override
@@ -105,12 +117,12 @@ public class PeopleService extends SystemService {

    @Override
    public void onUserUnlocked(@NonNull TargetUser user) {
        mDataManager.onUserUnlocked(user.getUserIdentifier());
        getDataManager().onUserUnlocked(user.getUserIdentifier());
    }

    @Override
    public void onUserStopping(@NonNull TargetUser user) {
        mDataManager.onUserStopping(user.getUserIdentifier());
        getDataManager().onUserStopping(user.getUserIdentifier());
    }

    /**
@@ -171,28 +183,28 @@ public class PeopleService extends SystemService {
        public ConversationChannel getConversation(
                String packageName, int userId, String shortcutId) {
            enforceSystemRootOrSystemUI(getContext(), "get conversation");
            return mDataManager.getConversation(packageName, userId, shortcutId);
            return getDataManager().getConversation(packageName, userId, shortcutId);
        }

        @Override
        public ParceledListSlice<ConversationChannel> getRecentConversations() {
            enforceSystemRootOrSystemUI(getContext(), "get recent conversations");
            return new ParceledListSlice<>(
                    mDataManager.getRecentConversations(
                    getDataManager().getRecentConversations(
                            Binder.getCallingUserHandle().getIdentifier()));
        }

        @Override
        public void removeRecentConversation(String packageName, int userId, String shortcutId) {
            enforceSystemOrRoot("remove a recent conversation");
            mDataManager.removeRecentConversation(packageName, userId, shortcutId,
            getDataManager().removeRecentConversation(packageName, userId, shortcutId,
                    Binder.getCallingUserHandle().getIdentifier());
        }

        @Override
        public void removeAllRecentConversations() {
            enforceSystemOrRoot("remove all recent conversations");
            mDataManager.removeAllRecentConversations(
            getDataManager().removeAllRecentConversations(
                    Binder.getCallingUserHandle().getIdentifier());
        }

@@ -200,7 +212,7 @@ public class PeopleService extends SystemService {
        public boolean isConversation(String packageName, int userId, String shortcutId) {
            enforceHasReadPeopleDataPermission();
            handleIncomingUser(userId);
            return mDataManager.isConversation(packageName, userId, shortcutId);
            return getDataManager().isConversation(packageName, userId, shortcutId);
        }

        private void enforceHasReadPeopleDataPermission() throws SecurityException {
@@ -213,7 +225,7 @@ public class PeopleService extends SystemService {
        @Override
        public long getLastInteraction(String packageName, int userId, String shortcutId) {
            enforceSystemRootOrSystemUI(getContext(), "get last interaction");
            return mDataManager.getLastInteraction(packageName, userId, shortcutId);
            return getDataManager().getLastInteraction(packageName, userId, shortcutId);
        }

        @Override
@@ -224,7 +236,7 @@ public class PeopleService extends SystemService {
            if (status.getStartTimeMillis() > System.currentTimeMillis()) {
                throw new IllegalArgumentException("Start time must be in the past");
            }
            mDataManager.addOrUpdateStatus(packageName, userId, conversationId, status);
            getDataManager().addOrUpdateStatus(packageName, userId, conversationId, status);
        }

        @Override
@@ -232,14 +244,14 @@ public class PeopleService extends SystemService {
                String statusId) {
            handleIncomingUser(userId);
            checkCallerIsSameApp(packageName);
            mDataManager.clearStatus(packageName, userId, conversationId, statusId);
            getDataManager().clearStatus(packageName, userId, conversationId, statusId);
        }

        @Override
        public void clearStatuses(String packageName, int userId, String conversationId) {
            handleIncomingUser(userId);
            checkCallerIsSameApp(packageName);
            mDataManager.clearStatuses(packageName, userId, conversationId);
            getDataManager().clearStatuses(packageName, userId, conversationId);
        }

        @Override
@@ -250,21 +262,21 @@ public class PeopleService extends SystemService {
                checkCallerIsSameApp(packageName);
            }
            return new ParceledListSlice<>(
                    mDataManager.getStatuses(packageName, userId, conversationId));
                    getDataManager().getStatuses(packageName, userId, conversationId));
        }

        @Override
        public void registerConversationListener(
                String packageName, int userId, String shortcutId, IConversationListener listener) {
            enforceSystemRootOrSystemUI(getContext(), "register conversation listener");
            mConversationListenerHelper.addConversationListener(
            getConversationListenerHelper().addConversationListener(
                    new ListenerKey(packageName, userId, shortcutId), listener);
        }

        @Override
        public void unregisterConversationListener(IConversationListener listener) {
            enforceSystemRootOrSystemUI(getContext(), "unregister conversation listener");
            mConversationListenerHelper.removeConversationListener(listener);
            getConversationListenerHelper().removeConversationListener(listener);
        }
    };

@@ -393,7 +405,7 @@ public class PeopleService extends SystemService {
        public void onCreatePredictionSession(AppPredictionContext appPredictionContext,
                AppPredictionSessionId sessionId) {
            mSessions.put(sessionId,
                    new SessionInfo(appPredictionContext, mDataManager, sessionId.getUserId(),
                    new SessionInfo(appPredictionContext, getDataManager(), sessionId.getUserId(),
                            getContext()));
        }

@@ -448,18 +460,18 @@ public class PeopleService extends SystemService {

        @Override
        public void pruneDataForUser(@UserIdInt int userId, @NonNull CancellationSignal signal) {
            mDataManager.pruneDataForUser(userId, signal);
            getDataManager().pruneDataForUser(userId, signal);
        }

        @Nullable
        @Override
        public byte[] getBackupPayload(@UserIdInt int userId) {
            return mDataManager.getBackupPayload(userId);
            return getDataManager().getBackupPayload(userId);
        }

        @Override
        public void restore(@UserIdInt int userId, @NonNull byte[] payload) {
            mDataManager.restore(userId, payload);
            getDataManager().restore(userId, payload);
        }

        @VisibleForTesting
+36 −10
Original line number Diff line number Diff line
@@ -41,8 +41,10 @@ import android.app.prediction.AppPredictionSessionId;
import android.app.prediction.AppTarget;
import android.app.prediction.IPredictionCallback;
import android.content.Context;
import android.content.pm.PackageManagerInternal;
import android.content.pm.ParceledListSlice;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutServiceInternal;
import android.os.Binder;
import android.os.Bundle;
import android.os.RemoteException;
@@ -56,6 +58,7 @@ import androidx.test.InstrumentationRegistry;

import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
import com.android.server.LocalServices;
import com.android.server.notification.NotificationManagerInternal;

import org.junit.After;
import org.junit.Before;
@@ -86,6 +89,13 @@ public final class PeopleServiceTest {
    private AppPredictionSessionId mSessionId;
    private AppPredictionContext mPredictionContext;

    @Mock
    ShortcutServiceInternal mShortcutServiceInternal;
    @Mock
    PackageManagerInternal mPackageManagerInternal;
    @Mock
    NotificationManagerInternal mNotificationManagerInternal;

    @Mock
    private Context mMockContext;

@@ -110,6 +120,10 @@ public final class PeopleServiceTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        LocalServices.addService(ShortcutServiceInternal.class, mShortcutServiceInternal);
        LocalServices.addService(PackageManagerInternal.class, mPackageManagerInternal);
        LocalServices.addService(NotificationManagerInternal.class, mNotificationManagerInternal);

        mPeopleService = new TestablePeopleService(mContext);
        mTestableLooper = TestableLooper.get(this);
        mIPeopleManager = ((IPeopleManager) mPeopleService.mService);
@@ -137,6 +151,9 @@ public final class PeopleServiceTest {
    @After
    public void tearDown() {
        LocalServices.removeServiceForTest(PeopleServiceInternal.class);
        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
        LocalServices.removeServiceForTest(PackageManagerInternal.class);
        LocalServices.removeServiceForTest(NotificationManagerInternal.class);
    }

    @Test
@@ -167,25 +184,29 @@ public final class PeopleServiceTest {
    @Test
    public void testRegisterConversationListener() throws Exception {
        assertEquals(0,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());

        mIPeopleManager.registerConversationListener(TEST_PACKAGE_NAME, 0, CONVERSATION_ID_1,
                new TestableConversationListener());
        mTestableLooper.processAllMessages();
        assertEquals(1,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());

        mIPeopleManager.registerConversationListener(TEST_PACKAGE_NAME, 0, CONVERSATION_ID_1,
                new TestableConversationListener());
        mTestableLooper.processAllMessages();
        assertEquals(2,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());

        mIPeopleManager.registerConversationListener(TEST_PACKAGE_NAME, 0, CONVERSATION_ID_2,
                new TestableConversationListener());
        mTestableLooper.processAllMessages();
        assertEquals(3,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());
    }

    @Test
@@ -201,20 +222,24 @@ public final class PeopleServiceTest {
                listener3);
        mTestableLooper.processAllMessages();
        assertEquals(3,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());

        mIPeopleManager.unregisterConversationListener(
                listener2);
        assertEquals(2,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());
        mIPeopleManager.unregisterConversationListener(
                listener1);
        assertEquals(1,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());
        mIPeopleManager.unregisterConversationListener(
                listener3);
        assertEquals(0,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());
    }

    @Test
@@ -229,12 +254,13 @@ public final class PeopleServiceTest {
                PeopleManager.ConversationListener.class);
        registerListener(CONVERSATION_ID_2, listenerForConversation2);
        assertEquals(3,
                mPeopleService.mConversationListenerHelper.mListeners.getRegisteredCallbackCount());
                mPeopleService.getConversationListenerHelper()
                        .mListeners.getRegisteredCallbackCount());

        // Update conversation with two listeners.
        ConversationStatus status = new ConversationStatus.Builder(CONVERSATION_ID_1,
                ACTIVITY_GAME).build();
        mPeopleService.mConversationListenerHelper.onConversationsUpdate(
        mPeopleService.getConversationListenerHelper().onConversationsUpdate(
                Arrays.asList(getConversation(CONVERSATION_ID_1, status)));
        mTestLooper.dispatchAll();