diff options
author | Danning Chen <danningc@google.com> | 2020-02-18 13:04:39 -0800 |
---|---|---|
committer | Danning Chen <danningc@google.com> | 2020-02-18 13:23:04 -0800 |
commit | 91f90e0fd66d3b78e38028a9af2a0d02116f9dd1 (patch) | |
tree | b3ca5fc1409343a97a5fb3c2a749b90565cc13bf /services/people | |
parent | 1cc7197864912c164320a5d80a4c22536ace4044 (diff) |
Add getShareTargets() to ShortcutServiceInternal and get caller's user ID from AppPredictionSessionId
Previously, the caller's user ID was got from Binder. But
AppPredictionManager clears caller's identity. The only way to get the
caller's user ID is to get it from session ID.
Bug: 146522621
Test: atest com.android.server.people.data.DataManagerTest
Test: atest com.android.server.people.data.ShareTargetPredictorTest
Change-Id: Ia0dc35879084110849e06e919ff3b3c7241c46fd
Diffstat (limited to 'services/people')
5 files changed, 26 insertions, 27 deletions
diff --git a/services/people/java/com/android/server/people/PeopleService.java b/services/people/java/com/android/server/people/PeopleService.java index 663bf4f30708..2499614a3738 100644 --- a/services/people/java/com/android/server/people/PeopleService.java +++ b/services/people/java/com/android/server/people/PeopleService.java @@ -88,7 +88,7 @@ public class PeopleService extends SystemService { @Override public void onCreatePredictionSession(AppPredictionContext context, AppPredictionSessionId sessionId) { - mSessions.put(sessionId, new SessionInfo(context, mDataManager)); + mSessions.put(sessionId, new SessionInfo(context, mDataManager, sessionId.getUserId())); } @Override diff --git a/services/people/java/com/android/server/people/SessionInfo.java b/services/people/java/com/android/server/people/SessionInfo.java index eaa0781f12ef..28612f1dd49b 100644 --- a/services/people/java/com/android/server/people/SessionInfo.java +++ b/services/people/java/com/android/server/people/SessionInfo.java @@ -16,6 +16,7 @@ package com.android.server.people; +import android.annotation.UserIdInt; import android.app.prediction.AppPredictionContext; import android.app.prediction.AppTarget; import android.app.prediction.IPredictionCallback; @@ -38,9 +39,10 @@ class SessionInfo { private final RemoteCallbackList<IPredictionCallback> mCallbacks = new RemoteCallbackList<>(); - SessionInfo(AppPredictionContext predictionContext, DataManager dataManager) { + SessionInfo(AppPredictionContext predictionContext, DataManager dataManager, + @UserIdInt int callingUserId) { mAppTargetPredictor = AppTargetPredictor.create(predictionContext, - this::updatePredictions, dataManager); + this::updatePredictions, dataManager, callingUserId); } void addCallback(IPredictionCallback callback) { diff --git a/services/people/java/com/android/server/people/data/DataManager.java b/services/people/java/com/android/server/people/data/DataManager.java index dd9cbd00f6a2..6b97c98b0029 100644 --- a/services/people/java/com/android/server/people/data/DataManager.java +++ b/services/people/java/com/android/server/people/data/DataManager.java @@ -34,13 +34,11 @@ import android.content.IntentFilter; import android.content.pm.LauncherApps.ShortcutQuery; import android.content.pm.PackageManagerInternal; import android.content.pm.ShortcutInfo; -import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager.ShareShortcutInfo; import android.content.pm.ShortcutServiceInternal; import android.content.pm.UserInfo; import android.database.ContentObserver; import android.net.Uri; -import android.os.Binder; import android.os.CancellationSignal; import android.os.Handler; import android.os.Process; @@ -83,7 +81,6 @@ import java.util.function.Function; */ public class DataManager { - private static final String PLATFORM_PACKAGE_NAME = "android"; private static final int MY_UID = Process.myUid(); private static final int MY_PID = Process.myPid(); private static final long QUERY_EVENTS_MAX_AGE_MS = DateUtils.DAY_IN_MILLIS; @@ -106,7 +103,6 @@ public class DataManager { private ShortcutServiceInternal mShortcutServiceInternal; private PackageManagerInternal mPackageManagerInternal; - private ShortcutManager mShortcutManager; private UserManager mUserManager; public DataManager(Context context) { @@ -125,7 +121,6 @@ public class DataManager { public void initialize() { mShortcutServiceInternal = LocalServices.getService(ShortcutServiceInternal.class); mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); - mShortcutManager = mContext.getSystemService(ShortcutManager.class); mUserManager = mContext.getSystemService(UserManager.class); mShortcutServiceInternal.addListener(new ShortcutServiceListener()); @@ -171,8 +166,7 @@ public class DataManager { mNotificationListeners.put(userId, notificationListener); try { notificationListener.registerAsSystemService(mContext, - new ComponentName(PLATFORM_PACKAGE_NAME, getClass().getCanonicalName()), - userId); + new ComponentName(mContext, getClass()), userId); } catch (RemoteException e) { // Should never occur for local calls. } @@ -242,8 +236,8 @@ public class DataManager { * Iterates through all the {@link PackageData}s owned by the unlocked users who are in the * same profile group as the calling user. */ - public void forAllPackages(Consumer<PackageData> consumer) { - List<UserInfo> users = mUserManager.getEnabledProfiles(mInjector.getCallingUserId()); + void forPackagesInProfile(@UserIdInt int callingUserId, Consumer<PackageData> consumer) { + List<UserInfo> users = mUserManager.getEnabledProfiles(callingUserId); for (UserInfo userInfo : users) { UserData userData = getUnlockedUserData(userInfo.id); if (userData != null) { @@ -275,8 +269,10 @@ public class DataManager { * Gets the {@link ShareShortcutInfo}s from all packages owned by the calling user that match * the specified {@link IntentFilter}. */ - public List<ShareShortcutInfo> getShareShortcuts(@NonNull IntentFilter intentFilter) { - return mShortcutManager.getShareTargets(intentFilter); + public List<ShareShortcutInfo> getShareShortcuts(@NonNull IntentFilter intentFilter, + @UserIdInt int callingUserId) { + return mShortcutServiceInternal.getShareTargets( + mContext.getPackageName(), intentFilter, callingUserId); } /** Reports the {@link AppTargetEvent} from App Prediction Manager. */ @@ -361,7 +357,7 @@ public class DataManager { @ShortcutQuery.QueryFlags int queryFlags = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_PINNED | ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER; return mShortcutServiceInternal.getShortcuts( - mInjector.getCallingUserId(), /*callingPackage=*/ PLATFORM_PACKAGE_NAME, + UserHandle.USER_SYSTEM, mContext.getPackageName(), /*changedSince=*/ 0, packageName, shortcutIds, /*locusIds=*/ null, /*componentName=*/ null, queryFlags, userId, MY_PID, MY_UID); } @@ -775,7 +771,7 @@ public class DataManager { @Override public void onReceive(Context context, Intent intent) { - forAllPackages(PackageData::saveToDisk); + forAllUnlockedUsers(userData -> userData.forAllPackages(PackageData::saveToDisk)); } } @@ -809,9 +805,5 @@ public class DataManager { Function<String, PackageData> packageDataGetter) { return new UsageStatsQueryHelper(userId, packageDataGetter); } - - int getCallingUserId() { - return Binder.getCallingUserHandle().getIdentifier(); - } } } diff --git a/services/people/java/com/android/server/people/prediction/AppTargetPredictor.java b/services/people/java/com/android/server/people/prediction/AppTargetPredictor.java index 44f3e35833d9..19cf8af5d66b 100644 --- a/services/people/java/com/android/server/people/prediction/AppTargetPredictor.java +++ b/services/people/java/com/android/server/people/prediction/AppTargetPredictor.java @@ -18,6 +18,7 @@ package com.android.server.people.prediction; import android.annotation.MainThread; import android.annotation.NonNull; +import android.annotation.UserIdInt; import android.annotation.WorkerThread; import android.app.prediction.AppPredictionContext; import android.app.prediction.AppTarget; @@ -42,25 +43,28 @@ public class AppTargetPredictor { /** Creates a {@link AppTargetPredictor} instance based on the prediction context. */ public static AppTargetPredictor create(@NonNull AppPredictionContext predictionContext, @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, - @NonNull DataManager dataManager) { + @NonNull DataManager dataManager, @UserIdInt int callingUserId) { if (UI_SURFACE_SHARE.equals(predictionContext.getUiSurface())) { return new ShareTargetPredictor( - predictionContext, updatePredictionsMethod, dataManager); + predictionContext, updatePredictionsMethod, dataManager, callingUserId); } - return new AppTargetPredictor(predictionContext, updatePredictionsMethod, dataManager); + return new AppTargetPredictor( + predictionContext, updatePredictionsMethod, dataManager, callingUserId); } private final AppPredictionContext mPredictionContext; private final Consumer<List<AppTarget>> mUpdatePredictionsMethod; private final DataManager mDataManager; + final int mCallingUserId; private final ExecutorService mCallbackExecutor; AppTargetPredictor(@NonNull AppPredictionContext predictionContext, @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, - @NonNull DataManager dataManager) { + @NonNull DataManager dataManager, @UserIdInt int callingUserId) { mPredictionContext = predictionContext; mUpdatePredictionsMethod = updatePredictionsMethod; mDataManager = dataManager; + mCallingUserId = callingUserId; mCallbackExecutor = Executors.newSingleThreadExecutor(); } diff --git a/services/people/java/com/android/server/people/prediction/ShareTargetPredictor.java b/services/people/java/com/android/server/people/prediction/ShareTargetPredictor.java index 280ced3a07c5..90d821641149 100644 --- a/services/people/java/com/android/server/people/prediction/ShareTargetPredictor.java +++ b/services/people/java/com/android/server/people/prediction/ShareTargetPredictor.java @@ -19,6 +19,7 @@ package com.android.server.people.prediction; import android.annotation.MainThread; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.UserIdInt; import android.annotation.WorkerThread; import android.app.prediction.AppPredictionContext; import android.app.prediction.AppTarget; @@ -45,8 +46,8 @@ class ShareTargetPredictor extends AppTargetPredictor { ShareTargetPredictor(@NonNull AppPredictionContext predictionContext, @NonNull Consumer<List<AppTarget>> updatePredictionsMethod, - @NonNull DataManager dataManager) { - super(predictionContext, updatePredictionsMethod, dataManager); + @NonNull DataManager dataManager, @UserIdInt int callingUserId) { + super(predictionContext, updatePredictionsMethod, dataManager, callingUserId); mIntentFilter = predictionContext.getExtras().getParcelable( ChooserActivity.APP_PREDICTION_INTENT_FILTER_KEY); } @@ -84,7 +85,7 @@ class ShareTargetPredictor extends AppTargetPredictor { List<ShareTarget> getShareTargets() { List<ShareTarget> shareTargets = new ArrayList<>(); List<ShareShortcutInfo> shareShortcuts = - getDataManager().getShareShortcuts(mIntentFilter); + getDataManager().getShareShortcuts(mIntentFilter, mCallingUserId); for (ShareShortcutInfo shareShortcut : shareShortcuts) { ShortcutInfo shortcutInfo = shareShortcut.getShortcutInfo(); |