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.

By making the service lazy, we may gain some ms at boot and still preserve its functionality.

Note that the associated PeopleServiceTest.java had to be modified slightly because of a bug with the test itself. The test was not emulating the change to the boot phase and therefore it was not running the full initialization of the service. Making it lazy, forces the test to comply.

Test: Run the unchanged CTS test with `atest android.app.people.cts.PeopleManagerTest` and `atest com.android.server.people.PeopleServiceTest`x

Skip-CP-To-Master: We need to look at a different strategy for master https://b.corp.google.com/issues/286193387

Bug: 285828603
Bug: 286193387
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:062eda268fa33bee3364931b9b6eb293af5f718b)
Merged-In: I1db9446dbcabd7f9e8b1fdc0390b021f1e0185fa
Change-Id: I1db9446dbcabd7f9e8b1fdc0390b021f1e0185fa
This commit is contained in:
Roberto Cosenza
2023-06-06 11:27:38 +01:00
parent 0073d0927d
commit e0b3ad2e65
2 changed files with 77 additions and 39 deletions

View File

@@ -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);
mDataManager = new DataManager(context);
mConversationListenerHelper = new ConversationListenerHelper();
mDataManager.addConversationsListener(mConversationListenerHelper);
}
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_SYSTEM_SERVICES_READY) {
mDataManager.initialize();
@VisibleForTesting
ConversationListenerHelper getConversationListenerHelper() {
if (mLazyConversationListenerHelper == null) {
initLazyStuff();
}
return mLazyConversationListenerHelper;
}
private synchronized void initLazyStuff() {
if (mLazyDataManager == null) {
mLazyDataManager = new DataManager(getContext());
mLazyDataManager.initialize();
mLazyConversationListenerHelper = new ConversationListenerHelper();
mLazyDataManager.addConversationsListener(mLazyConversationListenerHelper);
}
}
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

View File

@@ -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();