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; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 21 | import android.annotation.UserIdInt; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 26 | import android.content.pm.ActivityInfo; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 28 | import android.content.pm.ILauncherApps; |
| 29 | import android.content.pm.IOnAppsChangedListener; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 30 | import android.content.pm.IPackageManager; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 33 | import android.content.pm.PackageManager.NameNotFoundException; |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 34 | import android.content.pm.ParceledListSlice; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 35 | import android.content.pm.ResolveInfo; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 36 | import android.content.pm.ShortcutInfo; |
| 37 | import android.content.pm.ShortcutServiceInternal; |
| 38 | import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 39 | import android.content.pm.UserInfo; |
| 40 | import android.graphics.Rect; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 41 | import android.net.Uri; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 42 | import android.os.Binder; |
| 43 | import android.os.Bundle; |
| 44 | import android.os.IInterface; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 45 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 46 | import android.os.RemoteCallbackList; |
| 47 | import android.os.RemoteException; |
| 48 | import android.os.UserHandle; |
| 49 | import android.os.UserManager; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 50 | import android.provider.Settings; |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 51 | import android.util.Log; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 52 | import android.util.Slog; |
| 53 | |
| 54 | import com.android.internal.content.PackageMonitor; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 55 | import com.android.internal.util.Preconditions; |
| 56 | import com.android.server.LocalServices; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 57 | import com.android.server.SystemService; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 58 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | |
| 61 | /** |
| 62 | * Service that manages requests and callbacks for launchers that support |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 63 | * managed profiles. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 64 | */ |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 65 | public class LauncherAppsService extends SystemService { |
| 66 | |
| 67 | private final LauncherAppsImpl mLauncherAppsImpl; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 68 | |
| 69 | public LauncherAppsService(Context context) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 70 | super(context); |
| 71 | mLauncherAppsImpl = new LauncherAppsImpl(context); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 74 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 75 | public void onStart() { |
| 76 | publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl); |
| 77 | } |
| 78 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 79 | static class LauncherAppsImpl extends ILauncherApps.Stub { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 80 | private static final boolean DEBUG = false; |
| 81 | private static final String TAG = "LauncherAppsService"; |
| 82 | private final Context mContext; |
| 83 | private final PackageManager mPm; |
| 84 | private final UserManager mUm; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 85 | private final ShortcutServiceInternal mShortcutServiceInternal; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 86 | private final PackageCallbackList<IOnAppsChangedListener> mListeners |
| 87 | = new PackageCallbackList<IOnAppsChangedListener>(); |
| 88 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 89 | private final MyPackageMonitor mPackageMonitor = new MyPackageMonitor(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 90 | |
| 91 | public LauncherAppsImpl(Context context) { |
| 92 | mContext = context; |
| 93 | mPm = mContext.getPackageManager(); |
| 94 | mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 95 | mShortcutServiceInternal = Preconditions.checkNotNull( |
| 96 | LocalServices.getService(ShortcutServiceInternal.class)); |
| 97 | mShortcutServiceInternal.addListener(mPackageMonitor); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * @see android.content.pm.ILauncherApps#addOnAppsChangedListener( |
| 102 | * android.content.pm.IOnAppsChangedListener) |
| 103 | */ |
| 104 | @Override |
| 105 | public void addOnAppsChangedListener(IOnAppsChangedListener listener) throws RemoteException { |
| 106 | synchronized (mListeners) { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 107 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 108 | Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle()); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 109 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 110 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 111 | if (DEBUG) { |
| 112 | Log.d(TAG, "Starting package monitoring"); |
| 113 | } |
| 114 | startWatchingPackageBroadcasts(); |
| 115 | } |
| 116 | mListeners.unregister(listener); |
| 117 | mListeners.register(listener, Binder.getCallingUserHandle()); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 118 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 119 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 120 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 121 | /* |
| 122 | * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener( |
| 123 | * android.content.pm.IOnAppsChangedListener) |
| 124 | */ |
| 125 | @Override |
| 126 | public void removeOnAppsChangedListener(IOnAppsChangedListener listener) |
| 127 | throws RemoteException { |
| 128 | synchronized (mListeners) { |
| 129 | if (DEBUG) { |
| 130 | Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle()); |
| 131 | } |
| 132 | mListeners.unregister(listener); |
| 133 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 134 | stopWatchingPackageBroadcasts(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Register a receiver to watch for package broadcasts |
| 141 | */ |
| 142 | private void startWatchingPackageBroadcasts() { |
| 143 | mPackageMonitor.register(mContext, null, UserHandle.ALL, true); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Unregister package broadcast receiver |
| 148 | */ |
| 149 | private void stopWatchingPackageBroadcasts() { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 150 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 151 | Log.d(TAG, "Stopped watching for packages"); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 152 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 153 | mPackageMonitor.unregister(); |
| 154 | } |
| 155 | |
| 156 | void checkCallbackCount() { |
| 157 | synchronized (mListeners) { |
| 158 | if (DEBUG) { |
| 159 | Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount()); |
| 160 | } |
| 161 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 162 | stopWatchingPackageBroadcasts(); |
| 163 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 166 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Checks if the caller is in the same group as the userToCheck. |
| 169 | */ |
| 170 | private void ensureInUserProfiles(UserHandle userToCheck, String message) { |
| 171 | final int callingUserId = UserHandle.getCallingUserId(); |
| 172 | final int targetUserId = userToCheck.getIdentifier(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 173 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 174 | if (targetUserId == callingUserId) return; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 175 | |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 176 | long ident = Binder.clearCallingIdentity(); |
| 177 | try { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 178 | UserInfo callingUserInfo = mUm.getUserInfo(callingUserId); |
| 179 | UserInfo targetUserInfo = mUm.getUserInfo(targetUserId); |
| 180 | if (targetUserInfo == null |
| 181 | || targetUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 182 | || targetUserInfo.profileGroupId != callingUserInfo.profileGroupId) { |
| 183 | throw new SecurityException(message); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 184 | } |
| 185 | } finally { |
| 186 | Binder.restoreCallingIdentity(ident); |
| 187 | } |
| 188 | } |
| 189 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 190 | private void verifyCallingPackage(String callingPackage) { |
| 191 | int packageUid = -1; |
| 192 | try { |
| 193 | packageUid = mPm.getPackageUid(callingPackage, |
| 194 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE |
| 195 | | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
| 196 | } catch (NameNotFoundException e) { |
| 197 | Log.e(TAG, "Package not found: " + callingPackage); |
| 198 | } |
| 199 | if (packageUid != Binder.getCallingUid()) { |
| 200 | throw new SecurityException("Calling package name mismatch"); |
| 201 | } |
| 202 | } |
| 203 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Checks if the user is enabled. |
| 206 | */ |
| 207 | private boolean isUserEnabled(UserHandle user) { |
| 208 | long ident = Binder.clearCallingIdentity(); |
| 209 | try { |
| 210 | UserInfo targetUserInfo = mUm.getUserInfo(user.getIdentifier()); |
| 211 | return targetUserInfo != null && targetUserInfo.isEnabled(); |
| 212 | } finally { |
| 213 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 214 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 218 | public ParceledListSlice<ResolveInfo> getLauncherActivities(String packageName, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 219 | throws RemoteException { |
| 220 | ensureInUserProfiles(user, "Cannot retrieve activities for unrelated profile " + user); |
| 221 | if (!isUserEnabled(user)) { |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 222 | return null; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 223 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 224 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 225 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 226 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 227 | mainIntent.setPackage(packageName); |
| 228 | long ident = Binder.clearCallingIdentity(); |
| 229 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 230 | List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(mainIntent, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 231 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 232 | return new ParceledListSlice<>(apps); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 233 | } finally { |
| 234 | Binder.restoreCallingIdentity(ident); |
| 235 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 239 | public ResolveInfo resolveActivity(Intent intent, UserHandle user) |
| 240 | throws RemoteException { |
| 241 | ensureInUserProfiles(user, "Cannot resolve activity for unrelated profile " + user); |
| 242 | if (!isUserEnabled(user)) { |
| 243 | return null; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 244 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 245 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 246 | long ident = Binder.clearCallingIdentity(); |
| 247 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 248 | ResolveInfo app = mPm.resolveActivityAsUser(intent, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 249 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 250 | return app; |
| 251 | } finally { |
| 252 | Binder.restoreCallingIdentity(ident); |
| 253 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 257 | public boolean isPackageEnabled(String packageName, UserHandle user) |
| 258 | throws RemoteException { |
| 259 | ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user); |
| 260 | if (!isUserEnabled(user)) { |
| 261 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 262 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 263 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 264 | long ident = Binder.clearCallingIdentity(); |
| 265 | try { |
| 266 | IPackageManager pm = AppGlobals.getPackageManager(); |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 267 | PackageInfo info = pm.getPackageInfo(packageName, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 268 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 269 | return info != null && info.applicationInfo.enabled; |
| 270 | } finally { |
| 271 | Binder.restoreCallingIdentity(ident); |
| 272 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Override |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 276 | public ApplicationInfo getApplicationInfo(String packageName, int flags, UserHandle user) |
| 277 | throws RemoteException { |
| 278 | ensureInUserProfiles(user, "Cannot check package for unrelated profile " + user); |
| 279 | if (!isUserEnabled(user)) { |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | long ident = Binder.clearCallingIdentity(); |
| 284 | try { |
| 285 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 286 | ApplicationInfo info = pm.getApplicationInfo(packageName, flags, |
| 287 | user.getIdentifier()); |
| 288 | return info; |
| 289 | } finally { |
| 290 | Binder.restoreCallingIdentity(ident); |
| 291 | } |
| 292 | } |
| 293 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 294 | private void enforceShortcutPermission(UserHandle user) { |
| 295 | ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user); |
| 296 | // STOPSHIP Implement it |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public ParceledListSlice getShortcuts(String callingPackage, long changedSince, |
| 301 | String packageName, ComponentName componentName, int flags, UserHandle user) |
| 302 | throws RemoteException { |
| 303 | enforceShortcutPermission(user); |
| 304 | verifyCallingPackage(callingPackage); |
| 305 | |
| 306 | return new ParceledListSlice<>( |
| 307 | mShortcutServiceInternal.getShortcuts(callingPackage, changedSince, packageName, |
| 308 | componentName, flags, user.getIdentifier())); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public ParceledListSlice getShortcutInfo(String callingPackage, String packageName, |
| 313 | List<String> ids, UserHandle user) throws RemoteException { |
| 314 | enforceShortcutPermission(user); |
| 315 | verifyCallingPackage(callingPackage); |
| 316 | |
| 317 | return new ParceledListSlice<>( |
| 318 | mShortcutServiceInternal.getShortcutInfo(callingPackage, packageName, |
| 319 | ids, user.getIdentifier())); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public void pinShortcuts(String callingPackage, String packageName, List<String> ids, |
| 324 | UserHandle user) throws RemoteException { |
| 325 | enforceShortcutPermission(user); |
| 326 | verifyCallingPackage(callingPackage); |
| 327 | |
| 328 | mShortcutServiceInternal.pinShortcuts(callingPackage, packageName, |
| 329 | ids, user.getIdentifier()); |
| 330 | } |
| 331 | |
| 332 | @Override |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 333 | public int getShortcutIconResId(String callingPackage, ShortcutInfo shortcut, |
| 334 | UserHandle user) { |
| 335 | enforceShortcutPermission(user); |
| 336 | verifyCallingPackage(callingPackage); |
| 337 | |
| 338 | return mShortcutServiceInternal.getShortcutIconResId(callingPackage, shortcut, |
| 339 | user.getIdentifier()); |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public ParcelFileDescriptor getShortcutIconFd(String callingPackage, ShortcutInfo shortcut, |
| 344 | UserHandle user) { |
| 345 | enforceShortcutPermission(user); |
| 346 | verifyCallingPackage(callingPackage); |
| 347 | |
| 348 | return mShortcutServiceInternal.getShortcutIconFd(callingPackage, shortcut, |
| 349 | user.getIdentifier()); |
| 350 | } |
| 351 | |
| 352 | @Override |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 353 | public boolean startShortcut(String callingPackage, String packageName, String shortcutId, |
| 354 | Rect sourceBounds, Bundle startActivityOptions, UserHandle user) |
| 355 | throws RemoteException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 356 | enforceShortcutPermission(user); |
| 357 | verifyCallingPackage(callingPackage); |
| 358 | |
| 359 | final Intent intent = mShortcutServiceInternal.createShortcutIntent(callingPackage, |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 360 | packageName, shortcutId, user.getIdentifier()); |
| 361 | if (intent == null) { |
| 362 | return false; |
| 363 | } |
| 364 | // Note the target activity doesn't have to be exported. |
| 365 | |
| 366 | intent.setSourceBounds(sourceBounds); |
| 367 | prepareIntentForLaunch(intent, sourceBounds); |
| 368 | |
| 369 | final long ident = Binder.clearCallingIdentity(); |
| 370 | try { |
| 371 | mContext.startActivityAsUser(intent, startActivityOptions, user); |
| 372 | } finally { |
| 373 | Binder.restoreCallingIdentity(ident); |
| 374 | } |
| 375 | return true; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 378 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 379 | public boolean isActivityEnabled(ComponentName component, UserHandle user) |
| 380 | throws RemoteException { |
| 381 | ensureInUserProfiles(user, "Cannot check component for unrelated profile " + user); |
| 382 | if (!isUserEnabled(user)) { |
| 383 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 384 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 385 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 386 | long ident = Binder.clearCallingIdentity(); |
| 387 | try { |
| 388 | IPackageManager pm = AppGlobals.getPackageManager(); |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 389 | ActivityInfo info = pm.getActivityInfo(component, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 390 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Winson Chung | dcf21d15 | 2014-10-01 14:30:05 -0700 | [diff] [blame] | 391 | return info != null; |
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 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 397 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 398 | public void startActivityAsUser(ComponentName component, Rect sourceBounds, |
| 399 | Bundle opts, UserHandle user) throws RemoteException { |
| 400 | ensureInUserProfiles(user, "Cannot start activity for unrelated profile " + user); |
| 401 | if (!isUserEnabled(user)) { |
| 402 | throw new IllegalStateException("Cannot start activity for disabled profile " + user); |
| 403 | } |
| 404 | |
| 405 | Intent launchIntent = new Intent(Intent.ACTION_MAIN); |
| 406 | launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 407 | prepareIntentForLaunch(launchIntent, sourceBounds); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 408 | launchIntent.setPackage(component.getPackageName()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 409 | |
| 410 | long ident = Binder.clearCallingIdentity(); |
| 411 | try { |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 412 | IPackageManager pm = AppGlobals.getPackageManager(); |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 413 | ActivityInfo info = pm.getActivityInfo(component, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 414 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 415 | if (!info.exported) { |
| 416 | throw new SecurityException("Cannot launch non-exported components " |
| 417 | + component); |
| 418 | } |
| 419 | |
| 420 | // Check that the component actually has Intent.CATEGORY_LAUCNCHER |
| 421 | // as calling startActivityAsUser ignores the category and just |
| 422 | // resolves based on the component if present. |
| 423 | List<ResolveInfo> apps = mPm.queryIntentActivitiesAsUser(launchIntent, |
Kenny Guy | b1b3026 | 2016-02-09 16:02:35 +0000 | [diff] [blame] | 424 | PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE, user.getIdentifier()); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 425 | final int size = apps.size(); |
| 426 | for (int i = 0; i < size; ++i) { |
| 427 | ActivityInfo activityInfo = apps.get(i).activityInfo; |
| 428 | if (activityInfo.packageName.equals(component.getPackageName()) && |
| 429 | activityInfo.name.equals(component.getClassName())) { |
| 430 | // Found an activity with category launcher that matches |
| 431 | // this component so ok to launch. |
| 432 | launchIntent.setComponent(component); |
| 433 | mContext.startActivityAsUser(launchIntent, opts, user); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | throw new SecurityException("Attempt to launch activity without " |
| 438 | + " category Intent.CATEGORY_LAUNCHER " + component); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 439 | } finally { |
| 440 | Binder.restoreCallingIdentity(ident); |
| 441 | } |
| 442 | } |
| 443 | |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 444 | private void prepareIntentForLaunch(@NonNull Intent launchIntent, |
| 445 | @Nullable Rect sourceBounds) { |
| 446 | launchIntent.setSourceBounds(sourceBounds); |
| 447 | launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 448 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 449 | } |
| 450 | |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 451 | @Override |
| 452 | public void showAppDetailsAsUser(ComponentName component, Rect sourceBounds, |
| 453 | Bundle opts, UserHandle user) throws RemoteException { |
| 454 | ensureInUserProfiles(user, "Cannot show app details for unrelated profile " + user); |
| 455 | if (!isUserEnabled(user)) { |
| 456 | throw new IllegalStateException("Cannot show app details for disabled profile " |
| 457 | + user); |
| 458 | } |
| 459 | |
| 460 | long ident = Binder.clearCallingIdentity(); |
| 461 | try { |
| 462 | String packageName = component.getPackageName(); |
| 463 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, |
| 464 | Uri.fromParts("package", packageName, null)); |
Winson | 369aad0 | 2016-02-17 10:35:47 -0800 | [diff] [blame] | 465 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 466 | intent.setSourceBounds(sourceBounds); |
| 467 | mContext.startActivityAsUser(intent, opts, user); |
| 468 | } finally { |
| 469 | Binder.restoreCallingIdentity(ident); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 474 | private class MyPackageMonitor extends PackageMonitor implements ShortcutChangeListener { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 475 | |
| 476 | /** Checks if user is a profile of or same as listeningUser. |
| 477 | * and the user is enabled. */ |
| 478 | private boolean isEnabledProfileOf(UserHandle user, UserHandle listeningUser, |
| 479 | String debugMsg) { |
| 480 | if (user.getIdentifier() == listeningUser.getIdentifier()) { |
| 481 | if (DEBUG) Log.d(TAG, "Delivering msg to same user " + debugMsg); |
| 482 | return true; |
| 483 | } |
| 484 | long ident = Binder.clearCallingIdentity(); |
| 485 | try { |
| 486 | UserInfo userInfo = mUm.getUserInfo(user.getIdentifier()); |
| 487 | UserInfo listeningUserInfo = mUm.getUserInfo(listeningUser.getIdentifier()); |
| 488 | if (userInfo == null || listeningUserInfo == null |
| 489 | || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 490 | || userInfo.profileGroupId != listeningUserInfo.profileGroupId |
| 491 | || !userInfo.isEnabled()) { |
| 492 | if (DEBUG) { |
| 493 | Log.d(TAG, "Not delivering msg from " + user + " to " + listeningUser + ":" |
| 494 | + debugMsg); |
| 495 | } |
| 496 | return false; |
| 497 | } else { |
| 498 | if (DEBUG) { |
| 499 | Log.d(TAG, "Delivering msg from " + user + " to " + listeningUser + ":" |
| 500 | + debugMsg); |
| 501 | } |
| 502 | return true; |
| 503 | } |
| 504 | } finally { |
| 505 | Binder.restoreCallingIdentity(ident); |
| 506 | } |
| 507 | } |
| 508 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 509 | // TODO Simplify with lambdas. |
| 510 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 511 | @Override |
| 512 | public void onPackageAdded(String packageName, int uid) { |
| 513 | UserHandle user = new UserHandle(getChangingUserId()); |
| 514 | final int n = mListeners.beginBroadcast(); |
| 515 | for (int i = 0; i < n; i++) { |
| 516 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 517 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 518 | if (!isEnabledProfileOf(user, listeningUser, "onPackageAdded")) continue; |
| 519 | try { |
| 520 | listener.onPackageAdded(user, packageName); |
| 521 | } catch (RemoteException re) { |
| 522 | Slog.d(TAG, "Callback failed ", re); |
| 523 | } |
| 524 | } |
| 525 | mListeners.finishBroadcast(); |
| 526 | |
| 527 | super.onPackageAdded(packageName, uid); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public void onPackageRemoved(String packageName, int uid) { |
| 532 | UserHandle user = new UserHandle(getChangingUserId()); |
| 533 | final int n = mListeners.beginBroadcast(); |
| 534 | for (int i = 0; i < n; i++) { |
| 535 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 536 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 537 | if (!isEnabledProfileOf(user, listeningUser, "onPackageRemoved")) continue; |
| 538 | try { |
| 539 | listener.onPackageRemoved(user, packageName); |
| 540 | } catch (RemoteException re) { |
| 541 | Slog.d(TAG, "Callback failed ", re); |
| 542 | } |
| 543 | } |
| 544 | mListeners.finishBroadcast(); |
| 545 | |
| 546 | super.onPackageRemoved(packageName, uid); |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public void onPackageModified(String packageName) { |
| 551 | UserHandle user = new UserHandle(getChangingUserId()); |
| 552 | final int n = mListeners.beginBroadcast(); |
| 553 | for (int i = 0; i < n; i++) { |
| 554 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 555 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 556 | if (!isEnabledProfileOf(user, listeningUser, "onPackageModified")) continue; |
| 557 | try { |
| 558 | listener.onPackageChanged(user, packageName); |
| 559 | } catch (RemoteException re) { |
| 560 | Slog.d(TAG, "Callback failed ", re); |
| 561 | } |
| 562 | } |
| 563 | mListeners.finishBroadcast(); |
| 564 | |
| 565 | super.onPackageModified(packageName); |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void onPackagesAvailable(String[] packages) { |
| 570 | UserHandle user = new UserHandle(getChangingUserId()); |
| 571 | final int n = mListeners.beginBroadcast(); |
| 572 | for (int i = 0; i < n; i++) { |
| 573 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 574 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 575 | if (!isEnabledProfileOf(user, listeningUser, "onPackagesAvailable")) continue; |
| 576 | try { |
| 577 | listener.onPackagesAvailable(user, packages, isReplacing()); |
| 578 | } catch (RemoteException re) { |
| 579 | Slog.d(TAG, "Callback failed ", re); |
| 580 | } |
| 581 | } |
| 582 | mListeners.finishBroadcast(); |
| 583 | |
| 584 | super.onPackagesAvailable(packages); |
| 585 | } |
| 586 | |
| 587 | @Override |
| 588 | public void onPackagesUnavailable(String[] packages) { |
| 589 | UserHandle user = new UserHandle(getChangingUserId()); |
| 590 | final int n = mListeners.beginBroadcast(); |
| 591 | for (int i = 0; i < n; i++) { |
| 592 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 593 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 594 | if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnavailable")) continue; |
| 595 | try { |
| 596 | listener.onPackagesUnavailable(user, packages, isReplacing()); |
| 597 | } catch (RemoteException re) { |
| 598 | Slog.d(TAG, "Callback failed ", re); |
| 599 | } |
| 600 | } |
| 601 | mListeners.finishBroadcast(); |
| 602 | |
| 603 | super.onPackagesUnavailable(packages); |
| 604 | } |
| 605 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 606 | @Override |
| 607 | public void onPackagesSuspended(String[] packages) { |
| 608 | UserHandle user = new UserHandle(getChangingUserId()); |
| 609 | final int n = mListeners.beginBroadcast(); |
| 610 | for (int i = 0; i < n; i++) { |
| 611 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 612 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 613 | if (!isEnabledProfileOf(user, listeningUser, "onPackagesSuspended")) continue; |
| 614 | try { |
| 615 | listener.onPackagesSuspended(user, packages); |
| 616 | } catch (RemoteException re) { |
| 617 | Slog.d(TAG, "Callback failed ", re); |
| 618 | } |
| 619 | } |
| 620 | mListeners.finishBroadcast(); |
| 621 | |
| 622 | super.onPackagesSuspended(packages); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void onPackagesUnsuspended(String[] packages) { |
| 627 | UserHandle user = new UserHandle(getChangingUserId()); |
| 628 | final int n = mListeners.beginBroadcast(); |
| 629 | for (int i = 0; i < n; i++) { |
| 630 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 631 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 632 | if (!isEnabledProfileOf(user, listeningUser, "onPackagesUnsuspended")) continue; |
| 633 | try { |
| 634 | listener.onPackagesUnsuspended(user, packages); |
| 635 | } catch (RemoteException re) { |
| 636 | Slog.d(TAG, "Callback failed ", re); |
| 637 | } |
| 638 | } |
| 639 | mListeners.finishBroadcast(); |
| 640 | |
| 641 | super.onPackagesUnsuspended(packages); |
| 642 | } |
| 643 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 644 | @Override |
| 645 | public void onShortcutChanged(@NonNull String packageName, |
| 646 | @NonNull List<ShortcutInfo> shortcuts, @UserIdInt int userId) { |
| 647 | final UserHandle user = UserHandle.of(userId); |
| 648 | |
| 649 | final int n = mListeners.beginBroadcast(); |
| 650 | for (int i = 0; i < n; i++) { |
| 651 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 652 | UserHandle listeningUser = (UserHandle) mListeners.getBroadcastCookie(i); |
| 653 | if (!isEnabledProfileOf(user, listeningUser, "onShortcutChanged")) continue; |
| 654 | try { |
| 655 | listener.onShortcutChanged(user, packageName, |
| 656 | new ParceledListSlice<>(shortcuts)); |
| 657 | } catch (RemoteException re) { |
| 658 | Slog.d(TAG, "Callback failed ", re); |
| 659 | } |
| 660 | } |
| 661 | mListeners.finishBroadcast(); |
| 662 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { |
| 666 | @Override |
| 667 | public void onCallbackDied(T callback, Object cookie) { |
| 668 | checkCallbackCount(); |
| 669 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
Nicolas Prevot | b683019 | 2015-06-26 13:45:12 -0700 | [diff] [blame] | 672 | } |