From a6a152e7de2b6db73474620c1eccda1ebe2eee9b Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Mon, 7 Mar 2016 13:41:15 -0800 Subject: [PATCH 1/2] Switch to SystemService lifecycle in TSMS. Like we did this for InputMethodManagerService [1], TextServicesManagerService (TSMS) needs to be recognized by SystemServiceManager with SystemService lifecycle mechanism so that we can receive SystemService#onUnlockUser() event, which is necessary to make TSMS encryption-aware. As a preparation, with this CL we only does mechcanical migration to SystemService lifecycle mechanism in TSMS. Hence no user-visible behavior change should occur. [1]: Ic17667df60b30e5355b61a3601ad27a000cab3a3 1e33dc8fdf3f722ecd32cc586b2a9515de24a242 Bug: 27456430 Change-Id: Ib3cc799d384f259b4fa3f5295f2da198df015eb8 --- .../server/TextServicesManagerService.java | 31 +++++++++++++++++-- .../java/com/android/server/SystemServer.java | 17 +--------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/services/core/java/com/android/server/TextServicesManagerService.java b/services/core/java/com/android/server/TextServicesManagerService.java index c4b4cbeda3db8..6ca021c708ccc 100644 --- a/services/core/java/com/android/server/TextServicesManagerService.java +++ b/services/core/java/com/android/server/TextServicesManagerService.java @@ -83,9 +83,34 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { new HashMap(); private final TextServicesSettings mSettings; - public void systemRunning() { - if (!mSystemReady) { - mSystemReady = true; + public static final class Lifecycle extends SystemService { + private TextServicesManagerService mService; + + public Lifecycle(Context context) { + super(context); + mService = new TextServicesManagerService(context); + } + + @Override + public void onStart() { + publishBinderService(Context.TEXT_SERVICES_MANAGER_SERVICE, mService); + } + + @Override + public void onBootPhase(int phase) { + // Called on the system server's main looper thread. + // TODO: Dispatch this to a worker thread as needed. + if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { + mService.systemRunning(); + } + } + } + + void systemRunning() { + synchronized (mSpellCheckerMap) { + if (!mSystemReady) { + mSystemReady = true; + } } } diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index b8c31e3f063ef..a2d5259b27586 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -635,7 +635,6 @@ public final class SystemServer { WallpaperManagerService wallpaper = null; LocationManagerService location = null; CountryDetectorService countryDetector = null; - TextServicesManagerService tsms = null; ILockSettings lockSettings = null; AssetAtlasService atlas = null; MediaRouterService mediaRouter = null; @@ -762,14 +761,7 @@ public final class SystemServer { } if (!disableNonCoreServices) { - traceBeginAndSlog("StartTextServicesManagerService"); - try { - tsms = new TextServicesManagerService(context); - ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms); - } catch (Throwable e) { - reportWtf("starting Text Service Manager Service", e); - } - Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); + mSystemServiceManager.startService(TextServicesManagerService.Lifecycle.class); } if (!disableNetwork) { @@ -1256,7 +1248,6 @@ public final class SystemServer { final CountryDetectorService countryDetectorF = countryDetector; final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater; final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService; - final TextServicesManagerService textServiceManagerServiceF = tsms; final StatusBarManagerService statusBarF = statusBar; final AssetAtlasService atlasF = atlas; final InputManagerService inputManagerF = inputManager; @@ -1371,12 +1362,6 @@ public final class SystemServer { } catch (Throwable e) { reportWtf("Notifying CommonTimeManagementService running", e); } - try { - if (textServiceManagerServiceF != null) - textServiceManagerServiceF.systemRunning(); - } catch (Throwable e) { - reportWtf("Notifying TextServicesManagerService running", e); - } try { if (atlasF != null) atlasF.systemRunning(); } catch (Throwable e) { From 9faa2aeb8a0d40bd834b7a4bf6343fbae3a302e3 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Mon, 7 Mar 2016 13:42:07 -0800 Subject: [PATCH 2/2] Rely on SystemService#onSwitchUser() in TSMS. SystemService class has already provided SystemService#onSwitchUser() callback event. We do not need to set up SynchronousUserSwitchObserver separately in TextServicesManagerService. Bug: 25816558 Bug: 27456430 Change-Id: I2d6f9932f30b72cf4ae4bc0c41810f75e2667478 --- .../server/TextServicesManagerService.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/services/core/java/com/android/server/TextServicesManagerService.java b/services/core/java/com/android/server/TextServicesManagerService.java index 6ca021c708ccc..e29cd109135b9 100644 --- a/services/core/java/com/android/server/TextServicesManagerService.java +++ b/services/core/java/com/android/server/TextServicesManagerService.java @@ -27,9 +27,9 @@ import com.android.internal.textservice.ITextServicesSessionListener; import org.xmlpull.v1.XmlPullParserException; +import android.annotation.UserIdInt; import android.app.ActivityManagerNative; import android.app.AppGlobals; -import android.app.SynchronousUserSwitchObserver; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; @@ -96,6 +96,13 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { publishBinderService(Context.TEXT_SERVICES_MANAGER_SERVICE, mService); } + @Override + public void onSwitchUser(@UserIdInt int userHandle) { + // Called on the system server's main looper thread. + // TODO: Dispatch this to a worker thread as needed. + mService.onSwitchUser(userHandle); + } + @Override public void onBootPhase(int phase) { // Called on the system server's main looper thread. @@ -114,6 +121,12 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { } } + void onSwitchUser(@UserIdInt int userId) { + synchronized (mSpellCheckerMap) { + switchUserLocked(userId); + } + } + public TextServicesManagerService(Context context) { mSystemReady = false; mContext = context; @@ -125,24 +138,6 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { int userId = UserHandle.USER_SYSTEM; try { - ActivityManagerNative.getDefault().registerUserSwitchObserver( - new SynchronousUserSwitchObserver() { - @Override - public void onUserSwitching(int newUserId) throws RemoteException { - synchronized(mSpellCheckerMap) { - switchUserLocked(newUserId); - } - } - - @Override - public void onUserSwitchComplete(int newUserId) throws RemoteException { - } - - @Override - public void onForegroundProfileSwitch(int newProfileId) { - // Ignore. - } - }); userId = ActivityManagerNative.getDefault().getCurrentUser().id; } catch (RemoteException e) { Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); @@ -155,7 +150,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { switchUserLocked(userId); } - private void switchUserLocked(int userId) { + private void switchUserLocked(@UserIdInt int userId) { mSettings.setCurrentUserId(userId); updateCurrentProfileIds(); unbindServiceLocked(); @@ -1040,17 +1035,18 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { private static class TextServicesSettings { private final ContentResolver mResolver; + @UserIdInt private int mCurrentUserId; @GuardedBy("mLock") private int[] mCurrentProfileIds = new int[0]; private Object mLock = new Object(); - public TextServicesSettings(ContentResolver resolver, int userId) { + public TextServicesSettings(ContentResolver resolver, @UserIdInt int userId) { mResolver = resolver; mCurrentUserId = userId; } - public void setCurrentUserId(int userId) { + public void setCurrentUserId(@UserIdInt int userId) { if (DBG) { Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to " + userId + ", new ime = " + getSelectedSpellChecker()); @@ -1065,7 +1061,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { } } - public boolean isCurrentProfile(int userId) { + public boolean isCurrentProfile(@UserIdInt int userId) { synchronized (mLock) { if (userId == mCurrentUserId) return true; for (int i = 0; i < mCurrentProfileIds.length; i++) { @@ -1075,6 +1071,7 @@ public class TextServicesManagerService extends ITextServicesManager.Stub { } } + @UserIdInt public int getCurrentUserId() { return mCurrentUserId; }