Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.UserIdInt; |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 22 | import android.app.ActivityManagerInternal; |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 23 | import android.app.AppDetailsActivity; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 25 | import android.app.IApplicationThread; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 26 | import android.app.PendingIntent; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 30 | import android.content.IntentSender; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 31 | import android.content.pm.ActivityInfo; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 32 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 33 | import android.content.pm.ILauncherApps; |
| 34 | import android.content.pm.IOnAppsChangedListener; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 35 | import android.content.pm.LauncherApps.ShortcutQuery; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 36 | import android.content.pm.PackageInfo; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageManagerInternal; |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 39 | import android.content.pm.ParceledListSlice; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 40 | import android.content.pm.ResolveInfo; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 41 | import android.content.pm.ShortcutInfo; |
| 42 | import android.content.pm.ShortcutServiceInternal; |
| 43 | import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 44 | import android.content.pm.UserInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 45 | import android.graphics.Rect; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 46 | import android.net.Uri; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
| 48 | import android.os.Bundle; |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 49 | import android.os.Handler; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 50 | import android.os.IInterface; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 51 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 52 | import android.os.RemoteCallbackList; |
| 53 | import android.os.RemoteException; |
| 54 | import android.os.UserHandle; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 55 | import android.os.UserManager; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 56 | import android.os.UserManagerInternal; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 57 | import android.provider.Settings; |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 58 | import android.util.Log; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 59 | import android.util.Slog; |
| 60 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 61 | import com.android.internal.annotations.VisibleForTesting; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 62 | import com.android.internal.content.PackageMonitor; |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 63 | import com.android.internal.os.BackgroundThread; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 64 | import com.android.internal.util.Preconditions; |
| 65 | import com.android.server.LocalServices; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 66 | import com.android.server.SystemService; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 67 | import com.android.server.wm.ActivityTaskManagerInternal; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 68 | |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 69 | import java.util.ArrayList; |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 70 | import java.util.Collections; |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 71 | import java.util.HashSet; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 72 | import java.util.List; |
| 73 | |
| 74 | /** |
| 75 | * Service that manages requests and callbacks for launchers that support |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 76 | * managed profiles. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 77 | */ |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 78 | public class LauncherAppsService extends SystemService { |
| 79 | |
| 80 | private final LauncherAppsImpl mLauncherAppsImpl; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 81 | |
| 82 | public LauncherAppsService(Context context) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 83 | super(context); |
| 84 | mLauncherAppsImpl = new LauncherAppsImpl(context); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 87 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 88 | public void onStart() { |
| 89 | publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl); |
| 90 | } |
| 91 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 92 | static class BroadcastCookie { |
| 93 | public final UserHandle user; |
| 94 | public final String packageName; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 95 | public final int callingUid; |
| 96 | public final int callingPid; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 97 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 98 | BroadcastCookie(UserHandle userHandle, String packageName, int callingPid, int callingUid) { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 99 | this.user = userHandle; |
| 100 | this.packageName = packageName; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 101 | this.callingUid = callingUid; |
| 102 | this.callingPid = callingPid; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 106 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 107 | static class LauncherAppsImpl extends ILauncherApps.Stub { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 108 | private static final boolean DEBUG = false; |
| 109 | private static final String TAG = "LauncherAppsService"; |
| 110 | private final Context mContext; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 111 | private final UserManager mUm; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 112 | private final UserManagerInternal mUserManagerInternal; |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 113 | private final ActivityManagerInternal mActivityManagerInternal; |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 114 | private final ActivityTaskManagerInternal mActivityTaskManagerInternal; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 115 | private final ShortcutServiceInternal mShortcutServiceInternal; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 116 | private final PackageCallbackList<IOnAppsChangedListener> mListeners |
| 117 | = new PackageCallbackList<IOnAppsChangedListener>(); |
| 118 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 119 | private final MyPackageMonitor mPackageMonitor = new MyPackageMonitor(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 120 | |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 121 | private final Handler mCallbackHandler; |
| 122 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 123 | public LauncherAppsImpl(Context context) { |
| 124 | mContext = context; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 125 | mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 126 | mUserManagerInternal = Preconditions.checkNotNull( |
| 127 | LocalServices.getService(UserManagerInternal.class)); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 128 | mActivityManagerInternal = Preconditions.checkNotNull( |
| 129 | LocalServices.getService(ActivityManagerInternal.class)); |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 130 | mActivityTaskManagerInternal = Preconditions.checkNotNull( |
| 131 | LocalServices.getService(ActivityTaskManagerInternal.class)); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 132 | mShortcutServiceInternal = Preconditions.checkNotNull( |
| 133 | LocalServices.getService(ShortcutServiceInternal.class)); |
| 134 | mShortcutServiceInternal.addListener(mPackageMonitor); |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 135 | mCallbackHandler = BackgroundThread.getHandler(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 138 | @VisibleForTesting |
| 139 | int injectBinderCallingUid() { |
| 140 | return getCallingUid(); |
| 141 | } |
| 142 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 143 | @VisibleForTesting |
| 144 | int injectBinderCallingPid() { |
| 145 | return getCallingPid(); |
| 146 | } |
| 147 | |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 148 | final int injectCallingUserId() { |
| 149 | return UserHandle.getUserId(injectBinderCallingUid()); |
| 150 | } |
| 151 | |
| 152 | @VisibleForTesting |
| 153 | long injectClearCallingIdentity() { |
| 154 | return Binder.clearCallingIdentity(); |
| 155 | } |
| 156 | |
| 157 | // Injection point. |
| 158 | @VisibleForTesting |
| 159 | void injectRestoreCallingIdentity(long token) { |
| 160 | Binder.restoreCallingIdentity(token); |
| 161 | } |
| 162 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 163 | private int getCallingUserId() { |
| 164 | return UserHandle.getUserId(injectBinderCallingUid()); |
| 165 | } |
| 166 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 167 | /* |
| 168 | * @see android.content.pm.ILauncherApps#addOnAppsChangedListener( |
| 169 | * android.content.pm.IOnAppsChangedListener) |
| 170 | */ |
| 171 | @Override |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 172 | public void addOnAppsChangedListener(String callingPackage, IOnAppsChangedListener listener) |
| 173 | throws RemoteException { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 174 | verifyCallingPackage(callingPackage); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 175 | synchronized (mListeners) { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 176 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 177 | Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle()); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 178 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 179 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 180 | if (DEBUG) { |
| 181 | Log.d(TAG, "Starting package monitoring"); |
| 182 | } |
| 183 | startWatchingPackageBroadcasts(); |
| 184 | } |
| 185 | mListeners.unregister(listener); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 186 | mListeners.register(listener, new BroadcastCookie(UserHandle.of(getCallingUserId()), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 187 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid())); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 188 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 189 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 190 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 191 | /* |
| 192 | * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener( |
| 193 | * android.content.pm.IOnAppsChangedListener) |
| 194 | */ |
| 195 | @Override |
| 196 | public void removeOnAppsChangedListener(IOnAppsChangedListener listener) |
| 197 | throws RemoteException { |
| 198 | synchronized (mListeners) { |
| 199 | if (DEBUG) { |
| 200 | Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle()); |
| 201 | } |
| 202 | mListeners.unregister(listener); |
| 203 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 204 | stopWatchingPackageBroadcasts(); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Register a receiver to watch for package broadcasts |
| 211 | */ |
| 212 | private void startWatchingPackageBroadcasts() { |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 213 | mPackageMonitor.register(mContext, UserHandle.ALL, true, mCallbackHandler); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Unregister package broadcast receiver |
| 218 | */ |
| 219 | private void stopWatchingPackageBroadcasts() { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 220 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 221 | Log.d(TAG, "Stopped watching for packages"); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 222 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 223 | mPackageMonitor.unregister(); |
| 224 | } |
| 225 | |
| 226 | void checkCallbackCount() { |
| 227 | synchronized (mListeners) { |
| 228 | if (DEBUG) { |
| 229 | Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount()); |
| 230 | } |
| 231 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 232 | stopWatchingPackageBroadcasts(); |
| 233 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 236 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Checks if the calling user is in the same group as {@code targetUser}, and allowed |
| 239 | * to access it. |
| 240 | * |
| 241 | * @return TRUE if the calling user can access {@code targetUserId}. FALSE if not *but |
| 242 | * they're still in the same profile group*. |
| 243 | * |
| 244 | * @throws SecurityException if the calling user and {@code targetUser} are not in the same |
| 245 | * group. |
| 246 | */ |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 247 | private boolean canAccessProfile(int targetUserId, String message) { |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 248 | final int callingUserId = injectCallingUserId(); |
| 249 | |
| 250 | if (targetUserId == callingUserId) return true; |
| 251 | |
| 252 | long ident = injectClearCallingIdentity(); |
| 253 | try { |
| 254 | final UserInfo callingUserInfo = mUm.getUserInfo(callingUserId); |
| 255 | if (callingUserInfo != null && callingUserInfo.isManagedProfile()) { |
| 256 | Slog.w(TAG, message + " for another profile " |
| 257 | + targetUserId + " from " + callingUserId + " not allowed"); |
| 258 | return false; |
| 259 | } |
| 260 | } finally { |
| 261 | injectRestoreCallingIdentity(ident); |
| 262 | } |
| 263 | |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 264 | return mUserManagerInternal.isProfileAccessible(injectCallingUserId(), targetUserId, |
| 265 | message, true); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 268 | @VisibleForTesting // We override it in unit tests |
| 269 | void verifyCallingPackage(String callingPackage) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 270 | int packageUid = -1; |
| 271 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 272 | packageUid = AppGlobals.getPackageManager().getPackageUid(callingPackage, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 273 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 274 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
Makoto Onuki | 3ee7f71 | 2016-03-10 17:28:55 -0800 | [diff] [blame] | 275 | | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 276 | UserHandle.getUserId(getCallingUid())); |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 277 | } catch (RemoteException ignore) { |
| 278 | } |
| 279 | if (packageUid < 0) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 280 | Log.e(TAG, "Package not found: " + callingPackage); |
| 281 | } |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 282 | if (packageUid != injectBinderCallingUid()) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 283 | throw new SecurityException("Calling package name mismatch"); |
| 284 | } |
| 285 | } |
| 286 | |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 287 | private ResolveInfo getHiddenAppActivityInfo(String packageName, int callingUid, |
| 288 | UserHandle user) { |
| 289 | Intent intent = new Intent(); |
| 290 | intent.setComponent(new ComponentName(packageName, AppDetailsActivity.class.getName())); |
| 291 | final PackageManagerInternal pmInt = |
| 292 | LocalServices.getService(PackageManagerInternal.class); |
| 293 | List<ResolveInfo> apps = pmInt.queryIntentActivities(intent, |
| 294 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 295 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 296 | callingUid, user.getIdentifier()); |
| 297 | if (apps.size() > 0) { |
| 298 | return apps.get(0); |
| 299 | } |
| 300 | return null; |
| 301 | } |
| 302 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 303 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 304 | public ParceledListSlice<ResolveInfo> getLauncherActivities(String callingPackage, |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 305 | String packageName, UserHandle user) throws RemoteException { |
| 306 | ParceledListSlice<ResolveInfo> launcherActivities = queryActivitiesForUser( |
| 307 | callingPackage, |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 308 | new Intent(Intent.ACTION_MAIN) |
| 309 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 310 | .setPackage(packageName), |
| 311 | user); |
Ricky Wai | 13b0abe | 2018-10-23 11:03:02 +0100 | [diff] [blame] | 312 | if (Settings.Global.getInt(mContext.getContentResolver(), |
Michal Karpinski | 217d597 | 2018-11-07 15:18:21 +0000 | [diff] [blame] | 313 | Settings.Global.SHOW_HIDDEN_LAUNCHER_ICON_APPS_ENABLED, 1) == 0) { |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 314 | return launcherActivities; |
| 315 | } |
Ricky Wai | 10ceaf9 | 2019-01-14 10:39:40 +0000 | [diff] [blame^] | 316 | if (launcherActivities == null) { |
| 317 | // Cannot access profile, so we don't even return any hidden apps. |
| 318 | return null; |
| 319 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 320 | |
| 321 | final int callingUid = injectBinderCallingUid(); |
| 322 | final ArrayList<ResolveInfo> result = new ArrayList<>(launcherActivities.getList()); |
Michal Karpinski | 6d5795f | 2018-11-06 15:18:30 +0000 | [diff] [blame] | 323 | final PackageManagerInternal pmInt = |
| 324 | LocalServices.getService(PackageManagerInternal.class); |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 325 | if (packageName != null) { |
Michal Karpinski | 6d5795f | 2018-11-06 15:18:30 +0000 | [diff] [blame] | 326 | // If this hidden app should not be shown, return the original list. |
| 327 | // Otherwise, inject hidden activity that forwards user to app details page. |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 328 | if (result.size() > 0) { |
| 329 | return launcherActivities; |
| 330 | } |
Michal Karpinski | 6d5795f | 2018-11-06 15:18:30 +0000 | [diff] [blame] | 331 | ApplicationInfo appInfo = pmInt.getApplicationInfo(packageName, /*flags*/ 0, |
| 332 | callingUid, user.getIdentifier()); |
| 333 | if (shouldShowHiddenApp(appInfo)) { |
| 334 | ResolveInfo info = getHiddenAppActivityInfo(packageName, callingUid, user); |
| 335 | if (info != null) { |
| 336 | result.add(info); |
| 337 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 338 | } |
| 339 | return new ParceledListSlice<>(result); |
| 340 | } |
| 341 | |
| 342 | long ident = injectClearCallingIdentity(); |
| 343 | try { |
| 344 | final HashSet<String> visiblePackages = new HashSet<>(); |
| 345 | for (ResolveInfo info : result) { |
| 346 | visiblePackages.add(info.activityInfo.packageName); |
| 347 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 348 | List<ApplicationInfo> installedPackages = pmInt.getInstalledApplications(0, |
| 349 | user.getIdentifier(), callingUid); |
| 350 | for (ApplicationInfo applicationInfo : installedPackages) { |
| 351 | if (!visiblePackages.contains(applicationInfo.packageName)) { |
| 352 | if (!shouldShowHiddenApp(applicationInfo)) { |
| 353 | continue; |
| 354 | } |
| 355 | ResolveInfo info = getHiddenAppActivityInfo(applicationInfo.packageName, |
| 356 | callingUid, user); |
| 357 | if (info != null) { |
| 358 | result.add(info); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | return new ParceledListSlice<>(result); |
| 363 | } finally { |
| 364 | injectRestoreCallingIdentity(ident); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | private static boolean shouldShowHiddenApp(ApplicationInfo appInfo) { |
Michal Karpinski | 08404a95 | 2018-11-15 17:46:00 +0000 | [diff] [blame] | 369 | if (appInfo == null || appInfo.isSystemApp() || appInfo.isUpdatedSystemApp()) { |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | return true; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 376 | public ActivityInfo resolveActivity( |
| 377 | String callingPackage, ComponentName component, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 378 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 379 | if (!canAccessProfile(user.getIdentifier(), "Cannot resolve activity")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 380 | return null; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 381 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 382 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 383 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 384 | long ident = Binder.clearCallingIdentity(); |
| 385 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 386 | final PackageManagerInternal pmInt = |
| 387 | LocalServices.getService(PackageManagerInternal.class); |
| 388 | return pmInt.getActivityInfo(component, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 389 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 390 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 391 | callingUid, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 392 | } finally { |
| 393 | Binder.restoreCallingIdentity(ident); |
| 394 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 398 | public ParceledListSlice getShortcutConfigActivities( |
| 399 | String callingPackage, String packageName, UserHandle user) |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 400 | throws RemoteException { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 401 | return queryActivitiesForUser(callingPackage, |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 402 | new Intent(Intent.ACTION_CREATE_SHORTCUT).setPackage(packageName), user); |
| 403 | } |
| 404 | |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 405 | private ParceledListSlice<ResolveInfo> queryActivitiesForUser(String callingPackage, |
| 406 | Intent intent, UserHandle user) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 407 | if (!canAccessProfile(user.getIdentifier(), "Cannot retrieve activities")) { |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 408 | return null; |
| 409 | } |
| 410 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 411 | final int callingUid = injectBinderCallingUid(); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 412 | long ident = injectClearCallingIdentity(); |
| 413 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 414 | final PackageManagerInternal pmInt = |
| 415 | LocalServices.getService(PackageManagerInternal.class); |
| 416 | List<ResolveInfo> apps = pmInt.queryIntentActivities(intent, |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 417 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 418 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 419 | callingUid, user.getIdentifier()); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 420 | return new ParceledListSlice<>(apps); |
| 421 | } finally { |
| 422 | injectRestoreCallingIdentity(ident); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public IntentSender getShortcutConfigActivityIntent(String callingPackage, |
| 428 | ComponentName component, UserHandle user) throws RemoteException { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 429 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 430 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 431 | return null; |
| 432 | } |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 433 | Preconditions.checkNotNull(component); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 434 | |
| 435 | // All right, create the sender. |
| 436 | Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(component); |
| 437 | final long identity = Binder.clearCallingIdentity(); |
| 438 | try { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 439 | final PendingIntent pi = PendingIntent.getActivityAsUser( |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 440 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 441 | | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 442 | null, user); |
| 443 | return pi == null ? null : pi.getIntentSender(); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 444 | } finally { |
| 445 | Binder.restoreCallingIdentity(identity); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 450 | public boolean isPackageEnabled(String callingPackage, String packageName, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 451 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 452 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 453 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 454 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 455 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 456 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 457 | long ident = Binder.clearCallingIdentity(); |
| 458 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 459 | final PackageManagerInternal pmInt = |
| 460 | LocalServices.getService(PackageManagerInternal.class); |
| 461 | PackageInfo info = pmInt.getPackageInfo(packageName, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 462 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 463 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 464 | callingUid, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 465 | return info != null && info.applicationInfo.enabled; |
| 466 | } finally { |
| 467 | Binder.restoreCallingIdentity(ident); |
| 468 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 472 | public Bundle getSuspendedPackageLauncherExtras(String packageName, |
| 473 | UserHandle user) { |
| 474 | if (!canAccessProfile(user.getIdentifier(), "Cannot get launcher extras")) { |
| 475 | return null; |
| 476 | } |
| 477 | final PackageManagerInternal pmi = |
| 478 | LocalServices.getService(PackageManagerInternal.class); |
| 479 | return pmi.getSuspendedPackageLauncherExtras(packageName, user.getIdentifier()); |
| 480 | } |
| 481 | |
| 482 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 483 | public ApplicationInfo getApplicationInfo( |
| 484 | String callingPackage, String packageName, int flags, UserHandle user) |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 485 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 486 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 487 | return null; |
| 488 | } |
| 489 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 490 | final int callingUid = injectBinderCallingUid(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 491 | long ident = Binder.clearCallingIdentity(); |
| 492 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 493 | final PackageManagerInternal pmInt = |
| 494 | LocalServices.getService(PackageManagerInternal.class); |
| 495 | ApplicationInfo info = pmInt.getApplicationInfo(packageName, flags, |
| 496 | callingUid, user.getIdentifier()); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 497 | return info; |
| 498 | } finally { |
| 499 | Binder.restoreCallingIdentity(ident); |
| 500 | } |
| 501 | } |
| 502 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 503 | private void ensureShortcutPermission(@NonNull String callingPackage) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 504 | verifyCallingPackage(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 505 | if (!mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 506 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid())) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 507 | throw new SecurityException("Caller can't access shortcut information"); |
| 508 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public ParceledListSlice getShortcuts(String callingPackage, long changedSince, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 513 | String packageName, List shortcutIds, ComponentName componentName, int flags, |
| 514 | UserHandle targetUser) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 515 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 516 | if (!canAccessProfile(targetUser.getIdentifier(), "Cannot get shortcuts")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 517 | return new ParceledListSlice<>(Collections.EMPTY_LIST); |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 518 | } |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 519 | if (shortcutIds != null && packageName == null) { |
| 520 | throw new IllegalArgumentException( |
| 521 | "To query by shortcut ID, package name must also be set"); |
| 522 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 523 | |
Felipe Leme | f93ce17 | 2016-06-13 14:19:40 -0700 | [diff] [blame] | 524 | // TODO(b/29399275): Eclipse compiler requires explicit List<ShortcutInfo> cast below. |
| 525 | return new ParceledListSlice<>((List<ShortcutInfo>) |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 526 | mShortcutServiceInternal.getShortcuts(getCallingUserId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 527 | callingPackage, changedSince, packageName, shortcutIds, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 528 | componentName, flags, targetUser.getIdentifier(), |
| 529 | injectBinderCallingPid(), injectBinderCallingUid())); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @Override |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 533 | public void pinShortcuts(String callingPackage, String packageName, List<String> ids, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 534 | UserHandle targetUser) { |
| 535 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 536 | if (!canAccessProfile(targetUser.getIdentifier(), "Cannot pin shortcuts")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 537 | return; |
| 538 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 539 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 540 | mShortcutServiceInternal.pinShortcuts(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 541 | callingPackage, packageName, ids, targetUser.getIdentifier()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | @Override |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 545 | public int getShortcutIconResId(String callingPackage, String packageName, String id, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 546 | int targetUserId) { |
| 547 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 548 | if (!canAccessProfile(targetUserId, "Cannot access shortcuts")) { |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 549 | return 0; |
| 550 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 551 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 552 | return mShortcutServiceInternal.getShortcutIconResId(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 553 | callingPackage, packageName, id, targetUserId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | @Override |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 557 | public ParcelFileDescriptor getShortcutIconFd(String callingPackage, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 558 | String packageName, String id, int targetUserId) { |
| 559 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 560 | if (!canAccessProfile(targetUserId, "Cannot access shortcuts")) { |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 561 | return null; |
| 562 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 563 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 564 | return mShortcutServiceInternal.getShortcutIconFd(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 565 | callingPackage, packageName, id, targetUserId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 569 | public boolean hasShortcutHostPermission(String callingPackage) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 570 | verifyCallingPackage(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 571 | return mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 572 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid()); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | @Override |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 576 | public boolean startShortcut(String callingPackage, String packageName, String shortcutId, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 577 | Rect sourceBounds, Bundle startActivityOptions, int targetUserId) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 578 | verifyCallingPackage(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 579 | if (!canAccessProfile(targetUserId, "Cannot start activity")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 580 | return false; |
| 581 | } |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 582 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 583 | // Even without the permission, pinned shortcuts are always launchable. |
| 584 | if (!mShortcutServiceInternal.isPinnedByCaller(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 585 | callingPackage, packageName, shortcutId, targetUserId)) { |
| 586 | ensureShortcutPermission(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 589 | final Intent[] intents = mShortcutServiceInternal.createShortcutIntents( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 590 | getCallingUserId(), callingPackage, packageName, shortcutId, targetUserId, |
| 591 | injectBinderCallingPid(), injectBinderCallingUid()); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 592 | if (intents == null || intents.length == 0) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 593 | return false; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 594 | } |
| 595 | // Note the target activity doesn't have to be exported. |
| 596 | |
Makoto Onuki | 7e5c3e8 | 2016-07-22 11:57:05 -0700 | [diff] [blame] | 597 | intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 598 | intents[0].setSourceBounds(sourceBounds); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 599 | |
| 600 | return startShortcutIntentsAsPublisher( |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 601 | intents, packageName, startActivityOptions, targetUserId); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 604 | private boolean startShortcutIntentsAsPublisher(@NonNull Intent[] intents, |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 605 | @NonNull String publisherPackage, Bundle startActivityOptions, int userId) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 606 | final int code; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 607 | try { |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 608 | code = mActivityTaskManagerInternal.startActivitiesAsPackage(publisherPackage, |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 609 | userId, intents, startActivityOptions); |
Bryce Lee | 7f93686 | 2017-05-09 15:33:18 -0700 | [diff] [blame] | 610 | if (ActivityManager.isStartResultSuccessful(code)) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 611 | return true; // Success |
| 612 | } else { |
| 613 | Log.e(TAG, "Couldn't start activity, code=" + code); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 614 | } |
Bryce Lee | 7f93686 | 2017-05-09 15:33:18 -0700 | [diff] [blame] | 615 | return false; |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 616 | } catch (SecurityException e) { |
| 617 | if (DEBUG) { |
| 618 | Slog.d(TAG, "SecurityException while launching intent", e); |
| 619 | } |
| 620 | return false; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 621 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 624 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 625 | public boolean isActivityEnabled( |
| 626 | String callingPackage, ComponentName component, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 627 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 628 | if (!canAccessProfile(user.getIdentifier(), "Cannot check component")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 629 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 630 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 631 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 632 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 633 | long ident = Binder.clearCallingIdentity(); |
| 634 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 635 | final PackageManagerInternal pmInt = |
| 636 | LocalServices.getService(PackageManagerInternal.class); |
| 637 | ActivityInfo info = pmInt.getActivityInfo(component, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 638 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 639 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 640 | callingUid, user.getIdentifier()); |
Winson Chung | dcf21d15 | 2014-10-01 14:30:05 -0700 | [diff] [blame] | 641 | return info != null; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 642 | } finally { |
| 643 | Binder.restoreCallingIdentity(ident); |
| 644 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 647 | @Override |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 648 | public void startActivityAsUser(IApplicationThread caller, String callingPackage, |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 649 | ComponentName component, Rect sourceBounds, |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 650 | Bundle opts, UserHandle user) throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 651 | if (!canAccessProfile(user.getIdentifier(), "Cannot start activity")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 652 | return; |
| 653 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 654 | |
| 655 | Intent launchIntent = new Intent(Intent.ACTION_MAIN); |
| 656 | launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
Makoto Onuki | 347a6bd | 2016-07-19 11:13:08 -0700 | [diff] [blame] | 657 | launchIntent.setSourceBounds(sourceBounds); |
| 658 | launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 659 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 660 | launchIntent.setPackage(component.getPackageName()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 661 | |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 662 | boolean canLaunch = false; |
| 663 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 664 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 665 | long ident = Binder.clearCallingIdentity(); |
| 666 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 667 | final PackageManagerInternal pmInt = |
| 668 | LocalServices.getService(PackageManagerInternal.class); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 669 | // Check that the component actually has Intent.CATEGORY_LAUCNCHER |
| 670 | // as calling startActivityAsUser ignores the category and just |
| 671 | // resolves based on the component if present. |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 672 | List<ResolveInfo> apps = pmInt.queryIntentActivities(launchIntent, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 673 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 674 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 675 | callingUid, user.getIdentifier()); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 676 | final int size = apps.size(); |
| 677 | for (int i = 0; i < size; ++i) { |
| 678 | ActivityInfo activityInfo = apps.get(i).activityInfo; |
| 679 | if (activityInfo.packageName.equals(component.getPackageName()) && |
| 680 | activityInfo.name.equals(component.getClassName())) { |
Sunny Goyal | 06ef5af | 2018-10-16 15:32:41 -0700 | [diff] [blame] | 681 | if (!activityInfo.exported) { |
| 682 | throw new SecurityException("Cannot launch non-exported components " |
| 683 | + component); |
| 684 | } |
| 685 | |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 686 | // Found an activity with category launcher that matches |
| 687 | // this component so ok to launch. |
Tony Mak | 12917c6 | 2018-01-15 15:17:13 +0000 | [diff] [blame] | 688 | launchIntent.setPackage(null); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 689 | launchIntent.setComponent(component); |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 690 | canLaunch = true; |
| 691 | break; |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 692 | } |
| 693 | } |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 694 | if (!canLaunch) { |
| 695 | throw new SecurityException("Attempt to launch activity without " |
| 696 | + " category Intent.CATEGORY_LAUNCHER " + component); |
| 697 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 698 | } finally { |
| 699 | Binder.restoreCallingIdentity(ident); |
| 700 | } |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 701 | mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 702 | launchIntent, opts, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 705 | @Override |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 706 | public void showAppDetailsAsUser(IApplicationThread caller, |
| 707 | String callingPackage, ComponentName component, |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 708 | Rect sourceBounds, Bundle opts, UserHandle user) throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 709 | if (!canAccessProfile(user.getIdentifier(), "Cannot show app details")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 710 | return; |
| 711 | } |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 712 | |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 713 | final Intent intent; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 714 | long ident = Binder.clearCallingIdentity(); |
| 715 | try { |
| 716 | String packageName = component.getPackageName(); |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 717 | intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 718 | Uri.fromParts("package", packageName, null)); |
Winson | 369aad0 | 2016-02-17 10:35:47 -0800 | [diff] [blame] | 719 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 720 | intent.setSourceBounds(sourceBounds); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 721 | } finally { |
| 722 | Binder.restoreCallingIdentity(ident); |
| 723 | } |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 724 | mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 725 | intent, opts, user.getIdentifier()); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 728 | /** Checks if user is a profile of or same as listeningUser. |
| 729 | * and the user is enabled. */ |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 730 | private boolean isEnabledProfileOf(UserHandle listeningUser, UserHandle user, |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 731 | String debugMsg) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 732 | return mUserManagerInternal.isProfileAccessible(listeningUser.getIdentifier(), |
| 733 | user.getIdentifier(), debugMsg, false); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 736 | @VisibleForTesting |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 737 | void postToPackageMonitorHandler(Runnable r) { |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 738 | mCallbackHandler.post(r); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | private class MyPackageMonitor extends PackageMonitor implements ShortcutChangeListener { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 742 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 743 | // TODO Simplify with lambdas. |
| 744 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 745 | @Override |
| 746 | public void onPackageAdded(String packageName, int uid) { |
| 747 | UserHandle user = new UserHandle(getChangingUserId()); |
| 748 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 749 | try { |
| 750 | for (int i = 0; i < n; i++) { |
| 751 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 752 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 753 | if (!isEnabledProfileOf(cookie.user, user, "onPackageAdded")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 754 | try { |
| 755 | listener.onPackageAdded(user, packageName); |
| 756 | } catch (RemoteException re) { |
| 757 | Slog.d(TAG, "Callback failed ", re); |
| 758 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 759 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 760 | } finally { |
| 761 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 762 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 763 | |
| 764 | super.onPackageAdded(packageName, uid); |
| 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public void onPackageRemoved(String packageName, int uid) { |
| 769 | UserHandle user = new UserHandle(getChangingUserId()); |
| 770 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 771 | try { |
| 772 | for (int i = 0; i < n; i++) { |
| 773 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 774 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 775 | if (!isEnabledProfileOf(cookie.user, user, "onPackageRemoved")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 776 | try { |
| 777 | listener.onPackageRemoved(user, packageName); |
| 778 | } catch (RemoteException re) { |
| 779 | Slog.d(TAG, "Callback failed ", re); |
| 780 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 781 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 782 | } finally { |
| 783 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 784 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 785 | |
| 786 | super.onPackageRemoved(packageName, uid); |
| 787 | } |
| 788 | |
| 789 | @Override |
| 790 | public void onPackageModified(String packageName) { |
| 791 | UserHandle user = new UserHandle(getChangingUserId()); |
| 792 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 793 | try { |
| 794 | for (int i = 0; i < n; i++) { |
| 795 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 796 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 797 | if (!isEnabledProfileOf(cookie.user, user, "onPackageModified")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 798 | try { |
| 799 | listener.onPackageChanged(user, packageName); |
| 800 | } catch (RemoteException re) { |
| 801 | Slog.d(TAG, "Callback failed ", re); |
| 802 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 803 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 804 | } finally { |
| 805 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 806 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 807 | |
| 808 | super.onPackageModified(packageName); |
| 809 | } |
| 810 | |
| 811 | @Override |
| 812 | public void onPackagesAvailable(String[] packages) { |
| 813 | UserHandle user = new UserHandle(getChangingUserId()); |
| 814 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 815 | try { |
| 816 | for (int i = 0; i < n; i++) { |
| 817 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 818 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 819 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesAvailable")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 820 | try { |
| 821 | listener.onPackagesAvailable(user, packages, isReplacing()); |
| 822 | } catch (RemoteException re) { |
| 823 | Slog.d(TAG, "Callback failed ", re); |
| 824 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 825 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 826 | } finally { |
| 827 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 828 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 829 | |
| 830 | super.onPackagesAvailable(packages); |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | public void onPackagesUnavailable(String[] packages) { |
| 835 | UserHandle user = new UserHandle(getChangingUserId()); |
| 836 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 837 | try { |
| 838 | for (int i = 0; i < n; i++) { |
| 839 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 840 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 841 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesUnavailable")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 842 | try { |
| 843 | listener.onPackagesUnavailable(user, packages, isReplacing()); |
| 844 | } catch (RemoteException re) { |
| 845 | Slog.d(TAG, "Callback failed ", re); |
| 846 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 847 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 848 | } finally { |
| 849 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 850 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 851 | |
| 852 | super.onPackagesUnavailable(packages); |
| 853 | } |
| 854 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 855 | @Override |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 856 | public void onPackagesSuspended(String[] packages, Bundle launcherExtras) { |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 857 | UserHandle user = new UserHandle(getChangingUserId()); |
| 858 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 859 | try { |
| 860 | for (int i = 0; i < n; i++) { |
| 861 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 862 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 863 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesSuspended")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 864 | try { |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 865 | listener.onPackagesSuspended(user, packages, launcherExtras); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 866 | } catch (RemoteException re) { |
| 867 | Slog.d(TAG, "Callback failed ", re); |
| 868 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 869 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 870 | } finally { |
| 871 | mListeners.finishBroadcast(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 872 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 873 | |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 874 | super.onPackagesSuspended(packages, launcherExtras); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | @Override |
| 878 | public void onPackagesUnsuspended(String[] packages) { |
| 879 | UserHandle user = new UserHandle(getChangingUserId()); |
| 880 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 881 | try { |
| 882 | for (int i = 0; i < n; i++) { |
| 883 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 884 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 885 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesUnsuspended")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 886 | try { |
| 887 | listener.onPackagesUnsuspended(user, packages); |
| 888 | } catch (RemoteException re) { |
| 889 | Slog.d(TAG, "Callback failed ", re); |
| 890 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 891 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 892 | } finally { |
| 893 | mListeners.finishBroadcast(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 894 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 895 | |
| 896 | super.onPackagesUnsuspended(packages); |
| 897 | } |
| 898 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 899 | @Override |
| 900 | public void onShortcutChanged(@NonNull String packageName, |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 901 | @UserIdInt int userId) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 902 | postToPackageMonitorHandler(() -> onShortcutChangedInner(packageName, userId)); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | private void onShortcutChangedInner(@NonNull String packageName, |
| 906 | @UserIdInt int userId) { |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 907 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 908 | try { |
| 909 | final UserHandle user = UserHandle.of(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 910 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 911 | for (int i = 0; i < n; i++) { |
| 912 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 913 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 914 | if (!isEnabledProfileOf(cookie.user, user, "onShortcutChanged")) continue; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 915 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 916 | final int launcherUserId = cookie.user.getIdentifier(); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 917 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 918 | // Make sure the caller has the permission. |
| 919 | if (!mShortcutServiceInternal.hasShortcutHostPermission( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 920 | launcherUserId, cookie.packageName, |
| 921 | cookie.callingPid, cookie.callingUid)) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 922 | continue; |
| 923 | } |
| 924 | // Each launcher has a different set of pinned shortcuts, so we need to do a |
| 925 | // query in here. |
| 926 | // (As of now, only one launcher has the permission at a time, so it's bit |
| 927 | // moot, but we may change the permission model eventually.) |
| 928 | final List<ShortcutInfo> list = |
| 929 | mShortcutServiceInternal.getShortcuts(launcherUserId, |
| 930 | cookie.packageName, |
| 931 | /* changedSince= */ 0, packageName, /* shortcutIds=*/ null, |
| 932 | /* component= */ null, |
| 933 | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 934 | | ShortcutQuery.FLAG_MATCH_ALL_KINDS_WITH_ALL_PINNED |
| 935 | , userId, cookie.callingPid, cookie.callingUid); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 936 | try { |
| 937 | listener.onShortcutChanged(user, packageName, |
| 938 | new ParceledListSlice<>(list)); |
| 939 | } catch (RemoteException re) { |
| 940 | Slog.d(TAG, "Callback failed ", re); |
| 941 | } |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 942 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 943 | } catch (RuntimeException e) { |
| 944 | // When the user is locked we get IllegalState, so just catch all. |
| 945 | Log.w(TAG, e.getMessage(), e); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 946 | } finally { |
| 947 | mListeners.finishBroadcast(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 948 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 949 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { |
| 953 | @Override |
| 954 | public void onCallbackDied(T callback, Object cookie) { |
| 955 | checkCallbackCount(); |
| 956 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 957 | } |
| 958 | } |
Nicolas Prevot | b683019 | 2015-06-26 13:45:12 -0700 | [diff] [blame] | 959 | } |