From d49f3fa8062257d22537c31def79d29879b6e7cb Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Wed, 25 Jan 2017 14:09:48 -0800 Subject: [PATCH] Cache isManagedProfile(). LauncherApps wants to use it in the client side. Let's cache the result. Bug: 34340531 Test: manual tests with work profile Change-Id: I594a9ca688867e7cb0b35ab389ccef66135ef9cd --- core/java/android/os/UserManager.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index c5c380c1074b1..388054d35ba8c 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -66,6 +66,8 @@ public class UserManager { private final IUserManager mService; private final Context mContext; + private Boolean mIsManagedProfileCached; + /** * @hide * No user restriction. @@ -970,8 +972,14 @@ public class UserManager { */ @SystemApi public boolean isManagedProfile() { + // No need for synchronization. Once it becomes non-null, it'll be non-null forever. + // Worst case we might end up calling the AIDL method multiple times but that's fine. + if (mIsManagedProfileCached != null) { + return mIsManagedProfileCached; + } try { - return mService.isManagedProfile(UserHandle.myUserId()); + mIsManagedProfileCached = mService.isManagedProfile(UserHandle.myUserId()); + return mIsManagedProfileCached; } catch (RemoteException re) { throw re.rethrowFromSystemServer(); } @@ -987,6 +995,9 @@ public class UserManager { */ @SystemApi public boolean isManagedProfile(@UserIdInt int userId) { + if (userId == UserHandle.myUserId()) { + return isManagedProfile(); + } try { return mService.isManagedProfile(userId); } catch (RemoteException re) {