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; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 23 | import android.app.AppGlobals; |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 24 | import android.app.IApplicationThread; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 25 | import android.app.PendingIntent; |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 26 | import android.app.admin.DevicePolicyManager; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 27 | import android.app.usage.UsageStatsManagerInternal; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 28 | import android.content.ComponentName; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 31 | import android.content.IntentSender; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 32 | import android.content.pm.ActivityInfo; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 33 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 34 | import android.content.pm.ILauncherApps; |
| 35 | import android.content.pm.IOnAppsChangedListener; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 36 | import android.content.pm.IPackageInstallerCallback; |
Makoto Onuki | 1b29616 | 2020-01-28 15:56:49 -0800 | [diff] [blame] | 37 | import android.content.pm.IPackageManager; |
Ricky Wai | e346a49 | 2019-01-21 14:54:32 +0000 | [diff] [blame] | 38 | import android.content.pm.LauncherApps; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 39 | import android.content.pm.LauncherApps.ShortcutQuery; |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 40 | import android.content.pm.PackageInfo; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageInstaller.SessionInfo; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageManagerInternal; |
Sunny Goyal | 6cbc2fe | 2015-11-24 09:34:20 -0800 | [diff] [blame] | 44 | import android.content.pm.ParceledListSlice; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 45 | import android.content.pm.ResolveInfo; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 46 | import android.content.pm.ShortcutInfo; |
| 47 | import android.content.pm.ShortcutServiceInternal; |
| 48 | import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 49 | import android.content.pm.UserInfo; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 50 | import android.content.pm.parsing.AndroidPackage; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 51 | import android.graphics.Rect; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 52 | import android.net.Uri; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 53 | import android.os.Binder; |
| 54 | import android.os.Bundle; |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 55 | import android.os.Handler; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 56 | import android.os.IInterface; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 57 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 58 | import android.os.RemoteCallbackList; |
| 59 | import android.os.RemoteException; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 60 | import android.os.ServiceManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 61 | import android.os.UserHandle; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 62 | import android.os.UserManager; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 63 | import android.os.UserManagerInternal; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 64 | import android.provider.Settings; |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 65 | import android.util.Log; |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 66 | import android.util.Pair; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 67 | import android.util.Slog; |
| 68 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 69 | import com.android.internal.annotations.VisibleForTesting; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 70 | import com.android.internal.content.PackageMonitor; |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 71 | import com.android.internal.os.BackgroundThread; |
Ricky Wai | 68e0722 | 2019-03-21 18:07:12 +0000 | [diff] [blame] | 72 | import com.android.internal.util.ArrayUtils; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 73 | import com.android.server.LocalServices; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 74 | import com.android.server.SystemService; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 75 | import com.android.server.wm.ActivityTaskManagerInternal; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 76 | |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 77 | import java.util.ArrayList; |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 78 | import java.util.Collections; |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 79 | import java.util.HashSet; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 80 | import java.util.List; |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 81 | import java.util.Objects; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Service that manages requests and callbacks for launchers that support |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 85 | * managed profiles. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 86 | */ |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 87 | public class LauncherAppsService extends SystemService { |
| 88 | |
| 89 | private final LauncherAppsImpl mLauncherAppsImpl; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 90 | |
| 91 | public LauncherAppsService(Context context) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 92 | super(context); |
| 93 | mLauncherAppsImpl = new LauncherAppsImpl(context); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 96 | @Override |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 97 | public void onStart() { |
| 98 | publishBinderService(Context.LAUNCHER_APPS_SERVICE, mLauncherAppsImpl); |
| 99 | } |
| 100 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 101 | static class BroadcastCookie { |
| 102 | public final UserHandle user; |
| 103 | public final String packageName; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 104 | public final int callingUid; |
| 105 | public final int callingPid; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 106 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 107 | BroadcastCookie(UserHandle userHandle, String packageName, int callingPid, int callingUid) { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 108 | this.user = userHandle; |
| 109 | this.packageName = packageName; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 110 | this.callingUid = callingUid; |
| 111 | this.callingPid = callingPid; |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 115 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 116 | static class LauncherAppsImpl extends ILauncherApps.Stub { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 117 | private static final boolean DEBUG = false; |
| 118 | private static final String TAG = "LauncherAppsService"; |
Ricky Wai | e346a49 | 2019-01-21 14:54:32 +0000 | [diff] [blame] | 119 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 120 | private final Context mContext; |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 121 | private final UserManager mUm; |
Makoto Onuki | 1b29616 | 2020-01-28 15:56:49 -0800 | [diff] [blame] | 122 | private final IPackageManager mIPM; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 123 | private final UserManagerInternal mUserManagerInternal; |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 124 | private final UsageStatsManagerInternal mUsageStatsManagerInternal; |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 125 | private final ActivityManagerInternal mActivityManagerInternal; |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 126 | private final ActivityTaskManagerInternal mActivityTaskManagerInternal; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 127 | private final ShortcutServiceInternal mShortcutServiceInternal; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 128 | private final PackageCallbackList<IOnAppsChangedListener> mListeners |
| 129 | = new PackageCallbackList<IOnAppsChangedListener>(); |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 130 | private final DevicePolicyManager mDpm; |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 131 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 132 | private final MyPackageMonitor mPackageMonitor = new MyPackageMonitor(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 133 | |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 134 | private final Handler mCallbackHandler; |
| 135 | |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 136 | private PackageInstallerService mPackageInstallerService; |
| 137 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 138 | public LauncherAppsImpl(Context context) { |
| 139 | mContext = context; |
Makoto Onuki | 1b29616 | 2020-01-28 15:56:49 -0800 | [diff] [blame] | 140 | mIPM = AppGlobals.getPackageManager(); |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 141 | mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 142 | mUserManagerInternal = Objects.requireNonNull( |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 143 | LocalServices.getService(UserManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 144 | mUsageStatsManagerInternal = Objects.requireNonNull( |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 145 | LocalServices.getService(UsageStatsManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 146 | mActivityManagerInternal = Objects.requireNonNull( |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 147 | LocalServices.getService(ActivityManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 148 | mActivityTaskManagerInternal = Objects.requireNonNull( |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 149 | LocalServices.getService(ActivityTaskManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 150 | mShortcutServiceInternal = Objects.requireNonNull( |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 151 | LocalServices.getService(ShortcutServiceInternal.class)); |
| 152 | mShortcutServiceInternal.addListener(mPackageMonitor); |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 153 | mCallbackHandler = BackgroundThread.getHandler(); |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 154 | mDpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 157 | @VisibleForTesting |
| 158 | int injectBinderCallingUid() { |
| 159 | return getCallingUid(); |
| 160 | } |
| 161 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 162 | @VisibleForTesting |
| 163 | int injectBinderCallingPid() { |
| 164 | return getCallingPid(); |
| 165 | } |
| 166 | |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 167 | final int injectCallingUserId() { |
| 168 | return UserHandle.getUserId(injectBinderCallingUid()); |
| 169 | } |
| 170 | |
| 171 | @VisibleForTesting |
| 172 | long injectClearCallingIdentity() { |
| 173 | return Binder.clearCallingIdentity(); |
| 174 | } |
| 175 | |
| 176 | // Injection point. |
| 177 | @VisibleForTesting |
| 178 | void injectRestoreCallingIdentity(long token) { |
| 179 | Binder.restoreCallingIdentity(token); |
| 180 | } |
| 181 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 182 | private int getCallingUserId() { |
| 183 | return UserHandle.getUserId(injectBinderCallingUid()); |
| 184 | } |
| 185 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 186 | /* |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 187 | * @see android.content.pm.ILauncherApps#addOnAppsChangedListener |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 188 | */ |
| 189 | @Override |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 190 | public void addOnAppsChangedListener(String callingPackage, IOnAppsChangedListener listener) |
| 191 | throws RemoteException { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 192 | verifyCallingPackage(callingPackage); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 193 | synchronized (mListeners) { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 194 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 195 | Log.d(TAG, "Adding listener from " + Binder.getCallingUserHandle()); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 196 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 197 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 198 | if (DEBUG) { |
| 199 | Log.d(TAG, "Starting package monitoring"); |
| 200 | } |
| 201 | startWatchingPackageBroadcasts(); |
| 202 | } |
| 203 | mListeners.unregister(listener); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 204 | mListeners.register(listener, new BroadcastCookie(UserHandle.of(getCallingUserId()), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 205 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid())); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 206 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 207 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 208 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 209 | /* |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 210 | * @see android.content.pm.ILauncherApps#removeOnAppsChangedListener |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 211 | */ |
| 212 | @Override |
| 213 | public void removeOnAppsChangedListener(IOnAppsChangedListener listener) |
| 214 | throws RemoteException { |
| 215 | synchronized (mListeners) { |
| 216 | if (DEBUG) { |
| 217 | Log.d(TAG, "Removing listener from " + Binder.getCallingUserHandle()); |
| 218 | } |
| 219 | mListeners.unregister(listener); |
| 220 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 221 | stopWatchingPackageBroadcasts(); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 227 | * @see android.content.pm.ILauncherApps#registerPackageInstallerCallback |
| 228 | */ |
| 229 | @Override |
| 230 | public void registerPackageInstallerCallback(String callingPackage, |
| 231 | IPackageInstallerCallback callback) { |
| 232 | verifyCallingPackage(callingPackage); |
| 233 | UserHandle callingIdUserHandle = new UserHandle(getCallingUserId()); |
| 234 | getPackageInstallerService().registerCallback(callback, eventUserId -> |
| 235 | isEnabledProfileOf(callingIdUserHandle, |
| 236 | new UserHandle(eventUserId), "shouldReceiveEvent")); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public ParceledListSlice<SessionInfo> getAllSessions(String callingPackage) { |
| 241 | verifyCallingPackage(callingPackage); |
| 242 | List<SessionInfo> sessionInfos = new ArrayList<>(); |
| 243 | int[] userIds = mUm.getEnabledProfileIds(getCallingUserId()); |
| 244 | long token = Binder.clearCallingIdentity(); |
| 245 | try { |
| 246 | for (int userId : userIds) { |
| 247 | sessionInfos.addAll(getPackageInstallerService().getAllSessions(userId) |
| 248 | .getList()); |
| 249 | } |
| 250 | } finally { |
| 251 | Binder.restoreCallingIdentity(token); |
| 252 | } |
| 253 | return new ParceledListSlice<>(sessionInfos); |
| 254 | } |
| 255 | |
| 256 | private PackageInstallerService getPackageInstallerService() { |
| 257 | if (mPackageInstallerService == null) { |
| 258 | mPackageInstallerService = ((PackageInstallerService) ((PackageManagerService) |
| 259 | ServiceManager.getService("package")).getPackageInstaller()); |
| 260 | } |
| 261 | return mPackageInstallerService; |
| 262 | } |
| 263 | |
| 264 | /** |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 265 | * Register a receiver to watch for package broadcasts |
| 266 | */ |
| 267 | private void startWatchingPackageBroadcasts() { |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 268 | mPackageMonitor.register(mContext, UserHandle.ALL, true, mCallbackHandler); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Unregister package broadcast receiver |
| 273 | */ |
| 274 | private void stopWatchingPackageBroadcasts() { |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 275 | if (DEBUG) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 276 | Log.d(TAG, "Stopped watching for packages"); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 277 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 278 | mPackageMonitor.unregister(); |
| 279 | } |
| 280 | |
| 281 | void checkCallbackCount() { |
| 282 | synchronized (mListeners) { |
| 283 | if (DEBUG) { |
| 284 | Log.d(TAG, "Callback count = " + mListeners.getRegisteredCallbackCount()); |
| 285 | } |
| 286 | if (mListeners.getRegisteredCallbackCount() == 0) { |
| 287 | stopWatchingPackageBroadcasts(); |
| 288 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 291 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 292 | /** |
| 293 | * Checks if the calling user is in the same group as {@code targetUser}, and allowed |
| 294 | * to access it. |
| 295 | * |
| 296 | * @return TRUE if the calling user can access {@code targetUserId}. FALSE if not *but |
| 297 | * they're still in the same profile group*. |
| 298 | * |
| 299 | * @throws SecurityException if the calling user and {@code targetUser} are not in the same |
| 300 | * group. |
| 301 | */ |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 302 | private boolean canAccessProfile(int targetUserId, String message) { |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 303 | final int callingUserId = injectCallingUserId(); |
| 304 | |
| 305 | if (targetUserId == callingUserId) return true; |
| 306 | |
| 307 | long ident = injectClearCallingIdentity(); |
| 308 | try { |
| 309 | final UserInfo callingUserInfo = mUm.getUserInfo(callingUserId); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 310 | if (callingUserInfo != null && callingUserInfo.isProfile()) { |
Makoto Onuki | e70b29e | 2018-04-03 16:44:39 -0700 | [diff] [blame] | 311 | Slog.w(TAG, message + " for another profile " |
| 312 | + targetUserId + " from " + callingUserId + " not allowed"); |
| 313 | return false; |
| 314 | } |
| 315 | } finally { |
| 316 | injectRestoreCallingIdentity(ident); |
| 317 | } |
| 318 | |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 319 | return mUserManagerInternal.isProfileAccessible(injectCallingUserId(), targetUserId, |
| 320 | message, true); |
Amith Yamasani | 932249d | 2014-05-01 14:39:35 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Makoto Onuki | 7a6a05f | 2016-03-10 17:01:08 -0800 | [diff] [blame] | 323 | @VisibleForTesting // We override it in unit tests |
| 324 | void verifyCallingPackage(String callingPackage) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 325 | int packageUid = -1; |
| 326 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 327 | packageUid = AppGlobals.getPackageManager().getPackageUid(callingPackage, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 328 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 329 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
Makoto Onuki | 3ee7f71 | 2016-03-10 17:28:55 -0800 | [diff] [blame] | 330 | | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 331 | UserHandle.getUserId(getCallingUid())); |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 332 | } catch (RemoteException ignore) { |
| 333 | } |
| 334 | if (packageUid < 0) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 335 | Log.e(TAG, "Package not found: " + callingPackage); |
| 336 | } |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 337 | if (packageUid != injectBinderCallingUid()) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 338 | throw new SecurityException("Calling package name mismatch"); |
| 339 | } |
| 340 | } |
| 341 | |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 342 | private ResolveInfo getHiddenAppActivityInfo(String packageName, int callingUid, |
| 343 | UserHandle user) { |
| 344 | Intent intent = new Intent(); |
Ricky Wai | 92cd006 | 2019-02-14 20:23:22 +0000 | [diff] [blame] | 345 | intent.setComponent(new ComponentName(packageName, |
| 346 | PackageManager.APP_DETAILS_ACTIVITY_CLASS_NAME)); |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 347 | final PackageManagerInternal pmInt = |
| 348 | LocalServices.getService(PackageManagerInternal.class); |
| 349 | List<ResolveInfo> apps = pmInt.queryIntentActivities(intent, |
Patrick Baumann | 182ea1c | 2019-11-18 13:43:11 -0800 | [diff] [blame] | 350 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 351 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 352 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 353 | callingUid, user.getIdentifier()); |
| 354 | if (apps.size() > 0) { |
| 355 | return apps.get(0); |
| 356 | } |
| 357 | return null; |
| 358 | } |
| 359 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 360 | @Override |
Suprabh Shukla | 7900049 | 2018-12-24 17:03:02 -0800 | [diff] [blame] | 361 | public boolean shouldHideFromSuggestions(String packageName, UserHandle user) { |
| 362 | if (!canAccessProfile(user.getIdentifier(), "cannot get shouldHideFromSuggestions")) { |
| 363 | return false; |
| 364 | } |
| 365 | final PackageManagerInternal pmi = LocalServices.getService( |
| 366 | PackageManagerInternal.class); |
| 367 | int flags = pmi.getDistractingPackageRestrictions(packageName, user.getIdentifier()); |
| 368 | return (flags & PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS) != 0; |
| 369 | } |
| 370 | |
| 371 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 372 | public ParceledListSlice<ResolveInfo> getLauncherActivities(String callingPackage, |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 373 | String packageName, UserHandle user) throws RemoteException { |
| 374 | ParceledListSlice<ResolveInfo> launcherActivities = queryActivitiesForUser( |
| 375 | callingPackage, |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 376 | new Intent(Intent.ACTION_MAIN) |
| 377 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 378 | .setPackage(packageName), |
| 379 | user); |
Ricky Wai | 13b0abe | 2018-10-23 11:03:02 +0100 | [diff] [blame] | 380 | if (Settings.Global.getInt(mContext.getContentResolver(), |
Michal Karpinski | 217d597 | 2018-11-07 15:18:21 +0000 | [diff] [blame] | 381 | Settings.Global.SHOW_HIDDEN_LAUNCHER_ICON_APPS_ENABLED, 1) == 0) { |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 382 | return launcherActivities; |
| 383 | } |
Ricky Wai | 10ceaf9 | 2019-01-14 10:39:40 +0000 | [diff] [blame] | 384 | if (launcherActivities == null) { |
| 385 | // Cannot access profile, so we don't even return any hidden apps. |
| 386 | return null; |
| 387 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 388 | |
| 389 | final int callingUid = injectBinderCallingUid(); |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 390 | final long ident = injectClearCallingIdentity(); |
| 391 | try { |
| 392 | if (mUm.getUserInfo(user.getIdentifier()).isManagedProfile()) { |
| 393 | // Managed profile should not show hidden apps |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 394 | return launcherActivities; |
| 395 | } |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 396 | if (mDpm.getDeviceOwnerComponentOnAnyUser() != null) { |
| 397 | // Device owner devices should not show hidden apps |
| 398 | return launcherActivities; |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 399 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 400 | |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 401 | final ArrayList<ResolveInfo> result = new ArrayList<>(launcherActivities.getList()); |
| 402 | final PackageManagerInternal pmInt = |
| 403 | LocalServices.getService(PackageManagerInternal.class); |
| 404 | if (packageName != null) { |
| 405 | // If this hidden app should not be shown, return the original list. |
| 406 | // Otherwise, inject hidden activity that forwards user to app details page. |
| 407 | if (result.size() > 0) { |
| 408 | return launcherActivities; |
| 409 | } |
| 410 | ApplicationInfo appInfo = pmInt.getApplicationInfo(packageName, /*flags*/ 0, |
| 411 | callingUid, user.getIdentifier()); |
Ricky Wai | a52e426 | 2019-03-13 17:39:16 +0000 | [diff] [blame] | 412 | if (shouldShowSyntheticActivity(user, appInfo)) { |
Ricky Wai | e6a9038 | 2019-01-14 11:34:10 +0000 | [diff] [blame] | 413 | ResolveInfo info = getHiddenAppActivityInfo(packageName, callingUid, user); |
| 414 | if (info != null) { |
| 415 | result.add(info); |
| 416 | } |
| 417 | } |
| 418 | return new ParceledListSlice<>(result); |
| 419 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 420 | final HashSet<String> visiblePackages = new HashSet<>(); |
| 421 | for (ResolveInfo info : result) { |
| 422 | visiblePackages.add(info.activityInfo.packageName); |
| 423 | } |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 424 | List<ApplicationInfo> installedPackages = pmInt.getInstalledApplications(0, |
| 425 | user.getIdentifier(), callingUid); |
| 426 | for (ApplicationInfo applicationInfo : installedPackages) { |
| 427 | if (!visiblePackages.contains(applicationInfo.packageName)) { |
Ricky Wai | a52e426 | 2019-03-13 17:39:16 +0000 | [diff] [blame] | 428 | if (!shouldShowSyntheticActivity(user, applicationInfo)) { |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 429 | continue; |
| 430 | } |
| 431 | ResolveInfo info = getHiddenAppActivityInfo(applicationInfo.packageName, |
| 432 | callingUid, user); |
| 433 | if (info != null) { |
| 434 | result.add(info); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | return new ParceledListSlice<>(result); |
| 439 | } finally { |
| 440 | injectRestoreCallingIdentity(ident); |
| 441 | } |
| 442 | } |
| 443 | |
Ricky Wai | a52e426 | 2019-03-13 17:39:16 +0000 | [diff] [blame] | 444 | private boolean shouldShowSyntheticActivity(UserHandle user, ApplicationInfo appInfo) { |
Michal Karpinski | 08404a95 | 2018-11-15 17:46:00 +0000 | [diff] [blame] | 445 | if (appInfo == null || appInfo.isSystemApp() || appInfo.isUpdatedSystemApp()) { |
Ricky Wai | cf134eb | 2018-10-10 09:26:32 +0100 | [diff] [blame] | 446 | return false; |
| 447 | } |
Ricky Wai | 8757727 | 2019-01-24 19:18:25 +0000 | [diff] [blame] | 448 | if (isManagedProfileAdmin(user, appInfo.packageName)) { |
| 449 | return false; |
| 450 | } |
Ricky Wai | 68e0722 | 2019-03-21 18:07:12 +0000 | [diff] [blame] | 451 | final PackageManagerInternal pmInt = |
| 452 | LocalServices.getService(PackageManagerInternal.class); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 453 | final AndroidPackage pkg = pmInt.getPackage(appInfo.packageName); |
Ricky Wai | 68e0722 | 2019-03-21 18:07:12 +0000 | [diff] [blame] | 454 | if (pkg == null) { |
| 455 | // Should not happen, but we shouldn't be failing if it does |
| 456 | return false; |
| 457 | } |
Ricky Wai | 3eb1cc6 | 2019-09-05 18:21:29 +0100 | [diff] [blame] | 458 | // If app does not have any default enabled launcher activity or any permissions, |
| 459 | // the app can legitimately have no icon so we do not show the synthetic activity. |
| 460 | return requestsPermissions(pkg) && hasDefaultEnableLauncherActivity( |
| 461 | appInfo.packageName); |
Ricky Wai | 68e0722 | 2019-03-21 18:07:12 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 464 | private boolean requestsPermissions(@NonNull AndroidPackage pkg) { |
| 465 | return !ArrayUtils.isEmpty(pkg.getRequestedPermissions()); |
Ricky Wai | 3eb1cc6 | 2019-09-05 18:21:29 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | private boolean hasDefaultEnableLauncherActivity(@NonNull String packageName) { |
| 469 | final PackageManagerInternal pmInt = |
| 470 | LocalServices.getService(PackageManagerInternal.class); |
| 471 | final Intent matchIntent = new Intent(Intent.ACTION_MAIN); |
| 472 | matchIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 473 | matchIntent.setPackage(packageName); |
| 474 | final List<ResolveInfo> infoList = pmInt.queryIntentActivities(matchIntent, |
Patrick Baumann | 182ea1c | 2019-11-18 13:43:11 -0800 | [diff] [blame] | 475 | matchIntent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Ricky Wai | 3eb1cc6 | 2019-09-05 18:21:29 +0100 | [diff] [blame] | 476 | PackageManager.MATCH_DISABLED_COMPONENTS, Binder.getCallingUid(), |
| 477 | getCallingUserId()); |
| 478 | final int size = infoList.size(); |
| 479 | for (int i = 0; i < size; i++) { |
| 480 | if (infoList.get(i).activityInfo.enabled) { |
| 481 | return true; |
| 482 | } |
Ricky Wai | 68e0722 | 2019-03-21 18:07:12 +0000 | [diff] [blame] | 483 | } |
Ricky Wai | 3eb1cc6 | 2019-09-05 18:21:29 +0100 | [diff] [blame] | 484 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Ricky Wai | 8757727 | 2019-01-24 19:18:25 +0000 | [diff] [blame] | 487 | private boolean isManagedProfileAdmin(UserHandle user, String packageName) { |
| 488 | final List<UserInfo> userInfoList = mUm.getProfiles(user.getIdentifier()); |
| 489 | for (int i = 0; i < userInfoList.size(); i++) { |
| 490 | UserInfo userInfo = userInfoList.get(i); |
| 491 | if (!userInfo.isManagedProfile()) { |
| 492 | continue; |
| 493 | } |
| 494 | ComponentName componentName = mDpm.getProfileOwnerAsUser(userInfo.getUserHandle()); |
| 495 | if (componentName == null) { |
| 496 | continue; |
| 497 | } |
| 498 | if (componentName.getPackageName().equals(packageName)) { |
| 499 | return true; |
| 500 | } |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 505 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 506 | public ActivityInfo resolveActivity( |
| 507 | String callingPackage, ComponentName component, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 508 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 509 | if (!canAccessProfile(user.getIdentifier(), "Cannot resolve activity")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 510 | return null; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 511 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 512 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 513 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 514 | long ident = Binder.clearCallingIdentity(); |
| 515 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 516 | final PackageManagerInternal pmInt = |
| 517 | LocalServices.getService(PackageManagerInternal.class); |
| 518 | return pmInt.getActivityInfo(component, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 519 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 520 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 521 | callingUid, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 522 | } finally { |
| 523 | Binder.restoreCallingIdentity(ident); |
| 524 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 528 | public ParceledListSlice getShortcutConfigActivities( |
| 529 | String callingPackage, String packageName, UserHandle user) |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 530 | throws RemoteException { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 531 | return queryActivitiesForUser(callingPackage, |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 532 | new Intent(Intent.ACTION_CREATE_SHORTCUT).setPackage(packageName), user); |
| 533 | } |
| 534 | |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 535 | private ParceledListSlice<ResolveInfo> queryActivitiesForUser(String callingPackage, |
| 536 | Intent intent, UserHandle user) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 537 | if (!canAccessProfile(user.getIdentifier(), "Cannot retrieve activities")) { |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 538 | return null; |
| 539 | } |
| 540 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 541 | final int callingUid = injectBinderCallingUid(); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 542 | long ident = injectClearCallingIdentity(); |
| 543 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 544 | final PackageManagerInternal pmInt = |
| 545 | LocalServices.getService(PackageManagerInternal.class); |
| 546 | List<ResolveInfo> apps = pmInt.queryIntentActivities(intent, |
Patrick Baumann | 182ea1c | 2019-11-18 13:43:11 -0800 | [diff] [blame] | 547 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 548 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 549 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 550 | callingUid, user.getIdentifier()); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 551 | return new ParceledListSlice<>(apps); |
| 552 | } finally { |
| 553 | injectRestoreCallingIdentity(ident); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public IntentSender getShortcutConfigActivityIntent(String callingPackage, |
| 559 | ComponentName component, UserHandle user) throws RemoteException { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 560 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 561 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 562 | return null; |
| 563 | } |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 564 | Objects.requireNonNull(component); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 565 | |
| 566 | // All right, create the sender. |
| 567 | Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(component); |
| 568 | final long identity = Binder.clearCallingIdentity(); |
| 569 | try { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 570 | final PendingIntent pi = PendingIntent.getActivityAsUser( |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 571 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 572 | | PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 573 | null, user); |
| 574 | return pi == null ? null : pi.getIntentSender(); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 575 | } finally { |
| 576 | Binder.restoreCallingIdentity(identity); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 581 | public boolean isPackageEnabled(String callingPackage, String packageName, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 582 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 583 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 584 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 585 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 586 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 587 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 588 | long ident = Binder.clearCallingIdentity(); |
| 589 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 590 | final PackageManagerInternal pmInt = |
| 591 | LocalServices.getService(PackageManagerInternal.class); |
| 592 | PackageInfo info = pmInt.getPackageInfo(packageName, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 593 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 594 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 595 | callingUid, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 596 | return info != null && info.applicationInfo.enabled; |
| 597 | } finally { |
| 598 | Binder.restoreCallingIdentity(ident); |
| 599 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | @Override |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 603 | public Bundle getSuspendedPackageLauncherExtras(String packageName, |
| 604 | UserHandle user) { |
| 605 | if (!canAccessProfile(user.getIdentifier(), "Cannot get launcher extras")) { |
| 606 | return null; |
| 607 | } |
| 608 | final PackageManagerInternal pmi = |
| 609 | LocalServices.getService(PackageManagerInternal.class); |
| 610 | return pmi.getSuspendedPackageLauncherExtras(packageName, user.getIdentifier()); |
| 611 | } |
| 612 | |
| 613 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 614 | public ApplicationInfo getApplicationInfo( |
| 615 | String callingPackage, String packageName, int flags, UserHandle user) |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 616 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 617 | if (!canAccessProfile(user.getIdentifier(), "Cannot check package")) { |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 618 | return null; |
| 619 | } |
| 620 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 621 | final int callingUid = injectBinderCallingUid(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 622 | long ident = Binder.clearCallingIdentity(); |
| 623 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 624 | final PackageManagerInternal pmInt = |
| 625 | LocalServices.getService(PackageManagerInternal.class); |
| 626 | ApplicationInfo info = pmInt.getApplicationInfo(packageName, flags, |
| 627 | callingUid, user.getIdentifier()); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 628 | return info; |
| 629 | } finally { |
| 630 | Binder.restoreCallingIdentity(ident); |
| 631 | } |
| 632 | } |
| 633 | |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 634 | @Override |
| 635 | public LauncherApps.AppUsageLimit getAppUsageLimit(String callingPackage, |
| 636 | String packageName, UserHandle user) { |
| 637 | verifyCallingPackage(callingPackage); |
| 638 | if (!canAccessProfile(user.getIdentifier(), "Cannot access usage limit")) { |
| 639 | return null; |
| 640 | } |
Varun Shah | e9abb75 | 2019-02-11 11:25:06 -0800 | [diff] [blame] | 641 | if (!mActivityTaskManagerInternal.isCallerRecents(Binder.getCallingUid())) { |
| 642 | throw new SecurityException("Caller is not the recents app"); |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | final UsageStatsManagerInternal.AppUsageLimitData data = |
| 646 | mUsageStatsManagerInternal.getAppUsageLimit(packageName, user); |
| 647 | if (data == null) { |
| 648 | return null; |
| 649 | } |
| 650 | return new LauncherApps.AppUsageLimit( |
Varun Shah | 9c6f72b | 2019-01-25 21:13:56 -0800 | [diff] [blame] | 651 | data.getTotalUsageLimit(), data.getUsageRemaining()); |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 654 | private void ensureShortcutPermission(@NonNull String callingPackage) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 655 | verifyCallingPackage(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 656 | if (!mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 657 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid())) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 658 | throw new SecurityException("Caller can't access shortcut information"); |
| 659 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public ParceledListSlice getShortcuts(String callingPackage, long changedSince, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 664 | String packageName, List shortcutIds, ComponentName componentName, int flags, |
| 665 | UserHandle targetUser) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 666 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 667 | if (!canAccessProfile(targetUser.getIdentifier(), "Cannot get shortcuts")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 668 | return new ParceledListSlice<>(Collections.EMPTY_LIST); |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 669 | } |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 670 | if (shortcutIds != null && packageName == null) { |
| 671 | throw new IllegalArgumentException( |
| 672 | "To query by shortcut ID, package name must also be set"); |
| 673 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 674 | |
Felipe Leme | f93ce17 | 2016-06-13 14:19:40 -0700 | [diff] [blame] | 675 | // TODO(b/29399275): Eclipse compiler requires explicit List<ShortcutInfo> cast below. |
| 676 | return new ParceledListSlice<>((List<ShortcutInfo>) |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 677 | mShortcutServiceInternal.getShortcuts(getCallingUserId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 678 | callingPackage, changedSince, packageName, shortcutIds, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 679 | componentName, flags, targetUser.getIdentifier(), |
| 680 | injectBinderCallingPid(), injectBinderCallingUid())); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | @Override |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 684 | public void pinShortcuts(String callingPackage, String packageName, List<String> ids, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 685 | UserHandle targetUser) { |
| 686 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 687 | if (!canAccessProfile(targetUser.getIdentifier(), "Cannot pin shortcuts")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 688 | return; |
| 689 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 690 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 691 | mShortcutServiceInternal.pinShortcuts(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 692 | callingPackage, packageName, ids, targetUser.getIdentifier()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | @Override |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 696 | public int getShortcutIconResId(String callingPackage, String packageName, String id, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 697 | int targetUserId) { |
| 698 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 699 | if (!canAccessProfile(targetUserId, "Cannot access shortcuts")) { |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 700 | return 0; |
| 701 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 702 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 703 | return mShortcutServiceInternal.getShortcutIconResId(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 704 | callingPackage, packageName, id, targetUserId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | @Override |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 708 | public ParcelFileDescriptor getShortcutIconFd(String callingPackage, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 709 | String packageName, String id, int targetUserId) { |
| 710 | ensureShortcutPermission(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 711 | if (!canAccessProfile(targetUserId, "Cannot access shortcuts")) { |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 712 | return null; |
| 713 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 714 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 715 | return mShortcutServiceInternal.getShortcutIconFd(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 716 | callingPackage, packageName, id, targetUserId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 720 | public boolean hasShortcutHostPermission(String callingPackage) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 721 | verifyCallingPackage(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 722 | return mShortcutServiceInternal.hasShortcutHostPermission(getCallingUserId(), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 723 | callingPackage, injectBinderCallingPid(), injectBinderCallingUid()); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | @Override |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 727 | public boolean startShortcut(String callingPackage, String packageName, String shortcutId, |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 728 | Rect sourceBounds, Bundle startActivityOptions, int targetUserId) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 729 | verifyCallingPackage(callingPackage); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 730 | if (!canAccessProfile(targetUserId, "Cannot start activity")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 731 | return false; |
| 732 | } |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 733 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 734 | // Even without the permission, pinned shortcuts are always launchable. |
| 735 | if (!mShortcutServiceInternal.isPinnedByCaller(getCallingUserId(), |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 736 | callingPackage, packageName, shortcutId, targetUserId)) { |
| 737 | ensureShortcutPermission(callingPackage); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 740 | final Intent[] intents = mShortcutServiceInternal.createShortcutIntents( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 741 | getCallingUserId(), callingPackage, packageName, shortcutId, targetUserId, |
| 742 | injectBinderCallingPid(), injectBinderCallingUid()); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 743 | if (intents == null || intents.length == 0) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 744 | return false; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 745 | } |
| 746 | // Note the target activity doesn't have to be exported. |
| 747 | |
Makoto Onuki | 7e5c3e8 | 2016-07-22 11:57:05 -0700 | [diff] [blame] | 748 | intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 749 | intents[0].setSourceBounds(sourceBounds); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 750 | |
| 751 | return startShortcutIntentsAsPublisher( |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 752 | intents, packageName, startActivityOptions, targetUserId); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 755 | private boolean startShortcutIntentsAsPublisher(@NonNull Intent[] intents, |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 756 | @NonNull String publisherPackage, Bundle startActivityOptions, int userId) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 757 | final int code; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 758 | try { |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 759 | code = mActivityTaskManagerInternal.startActivitiesAsPackage(publisherPackage, |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 760 | userId, intents, startActivityOptions); |
Bryce Lee | 7f93686 | 2017-05-09 15:33:18 -0700 | [diff] [blame] | 761 | if (ActivityManager.isStartResultSuccessful(code)) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 762 | return true; // Success |
| 763 | } else { |
| 764 | Log.e(TAG, "Couldn't start activity, code=" + code); |
Makoto Onuki | ea11db1 | 2016-06-24 15:17:44 -0700 | [diff] [blame] | 765 | } |
Bryce Lee | 7f93686 | 2017-05-09 15:33:18 -0700 | [diff] [blame] | 766 | return false; |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 767 | } catch (SecurityException e) { |
| 768 | if (DEBUG) { |
| 769 | Slog.d(TAG, "SecurityException while launching intent", e); |
| 770 | } |
| 771 | return false; |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 772 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 775 | @Override |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 776 | public boolean isActivityEnabled( |
| 777 | String callingPackage, ComponentName component, UserHandle user) |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 778 | throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 779 | if (!canAccessProfile(user.getIdentifier(), "Cannot check component")) { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 780 | return false; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 781 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 782 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 783 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 784 | long ident = Binder.clearCallingIdentity(); |
| 785 | try { |
Makoto Onuki | 1b29616 | 2020-01-28 15:56:49 -0800 | [diff] [blame] | 786 | final int state = mIPM.getComponentEnabledSetting(component, user.getIdentifier()); |
| 787 | switch (state) { |
| 788 | case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT: |
| 789 | break; // Need to check the manifest's enabled state. |
| 790 | case PackageManager.COMPONENT_ENABLED_STATE_ENABLED: |
| 791 | return true; |
| 792 | case PackageManager.COMPONENT_ENABLED_STATE_DISABLED: |
| 793 | case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER: |
| 794 | case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED: |
| 795 | return false; |
| 796 | } |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 797 | final PackageManagerInternal pmInt = |
| 798 | LocalServices.getService(PackageManagerInternal.class); |
| 799 | ActivityInfo info = pmInt.getActivityInfo(component, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 800 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 801 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 802 | callingUid, user.getIdentifier()); |
Makoto Onuki | 516020a | 2019-01-28 14:16:42 -0800 | [diff] [blame] | 803 | // Note we don't check "exported" because if the caller has the same UID as the |
| 804 | // callee's UID, it can still be launched. |
| 805 | // (If an app doesn't export a front door activity and causes issues with the |
| 806 | // launcher, that's just the app's bug.) |
| 807 | return info != null && info.isEnabled(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 808 | } finally { |
| 809 | Binder.restoreCallingIdentity(ident); |
| 810 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 813 | @Override |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 814 | public void startSessionDetailsActivityAsUser(IApplicationThread caller, |
| 815 | String callingPackage, SessionInfo sessionInfo, Rect sourceBounds, |
| 816 | Bundle opts, UserHandle userHandle) throws RemoteException { |
| 817 | int userId = userHandle.getIdentifier(); |
| 818 | if (!canAccessProfile(userId, "Cannot start details activity")) { |
| 819 | return; |
| 820 | } |
| 821 | |
| 822 | Intent i = new Intent(Intent.ACTION_VIEW) |
| 823 | .setData(new Uri.Builder() |
| 824 | .scheme("market") |
| 825 | .authority("details") |
| 826 | .appendQueryParameter("id", sessionInfo.appPackageName) |
| 827 | .build()) |
| 828 | .putExtra(Intent.EXTRA_REFERRER, new Uri.Builder().scheme("android-app") |
| 829 | .authority(callingPackage).build()); |
| 830 | i.setSourceBounds(sourceBounds); |
| 831 | |
| 832 | mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, i, opts, |
| 833 | userId); |
| 834 | } |
| 835 | |
| 836 | @Override |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 837 | public void startActivityAsUser(IApplicationThread caller, String callingPackage, |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 838 | ComponentName component, Rect sourceBounds, |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 839 | Bundle opts, UserHandle user) throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 840 | if (!canAccessProfile(user.getIdentifier(), "Cannot start activity")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 841 | return; |
| 842 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 843 | |
| 844 | Intent launchIntent = new Intent(Intent.ACTION_MAIN); |
| 845 | launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
Makoto Onuki | 347a6bd | 2016-07-19 11:13:08 -0700 | [diff] [blame] | 846 | launchIntent.setSourceBounds(sourceBounds); |
| 847 | launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 848 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 849 | launchIntent.setPackage(component.getPackageName()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 850 | |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 851 | boolean canLaunch = false; |
| 852 | |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 853 | final int callingUid = injectBinderCallingUid(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 854 | long ident = Binder.clearCallingIdentity(); |
| 855 | try { |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 856 | final PackageManagerInternal pmInt = |
| 857 | LocalServices.getService(PackageManagerInternal.class); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 858 | // Check that the component actually has Intent.CATEGORY_LAUCNCHER |
| 859 | // as calling startActivityAsUser ignores the category and just |
| 860 | // resolves based on the component if present. |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 861 | List<ResolveInfo> apps = pmInt.queryIntentActivities(launchIntent, |
Patrick Baumann | 182ea1c | 2019-11-18 13:43:11 -0800 | [diff] [blame] | 862 | launchIntent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 863 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 864 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Todd Kennedy | 18211fd | 2017-06-06 09:15:46 -0700 | [diff] [blame] | 865 | callingUid, user.getIdentifier()); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 866 | final int size = apps.size(); |
| 867 | for (int i = 0; i < size; ++i) { |
| 868 | ActivityInfo activityInfo = apps.get(i).activityInfo; |
| 869 | if (activityInfo.packageName.equals(component.getPackageName()) && |
| 870 | activityInfo.name.equals(component.getClassName())) { |
Sunny Goyal | 06ef5af | 2018-10-16 15:32:41 -0700 | [diff] [blame] | 871 | if (!activityInfo.exported) { |
| 872 | throw new SecurityException("Cannot launch non-exported components " |
| 873 | + component); |
| 874 | } |
| 875 | |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 876 | // Found an activity with category launcher that matches |
| 877 | // this component so ok to launch. |
Tony Mak | 12917c6 | 2018-01-15 15:17:13 +0000 | [diff] [blame] | 878 | launchIntent.setPackage(null); |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 879 | launchIntent.setComponent(component); |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 880 | canLaunch = true; |
| 881 | break; |
Kenny Guy | 2df1892 | 2014-07-17 13:01:25 +0100 | [diff] [blame] | 882 | } |
| 883 | } |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 884 | if (!canLaunch) { |
| 885 | throw new SecurityException("Attempt to launch activity without " |
| 886 | + " category Intent.CATEGORY_LAUNCHER " + component); |
| 887 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 888 | } finally { |
| 889 | Binder.restoreCallingIdentity(ident); |
| 890 | } |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 891 | mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 892 | launchIntent, opts, user.getIdentifier()); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 895 | @Override |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 896 | public void showAppDetailsAsUser(IApplicationThread caller, |
| 897 | String callingPackage, ComponentName component, |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 898 | Rect sourceBounds, Bundle opts, UserHandle user) throws RemoteException { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 899 | if (!canAccessProfile(user.getIdentifier(), "Cannot show app details")) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 900 | return; |
| 901 | } |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 902 | |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 903 | final Intent intent; |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 904 | long ident = Binder.clearCallingIdentity(); |
| 905 | try { |
| 906 | String packageName = component.getPackageName(); |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 907 | intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 908 | Uri.fromParts("package", packageName, null)); |
Winson | 369aad0 | 2016-02-17 10:35:47 -0800 | [diff] [blame] | 909 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 910 | intent.setSourceBounds(sourceBounds); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 911 | } finally { |
| 912 | Binder.restoreCallingIdentity(ident); |
| 913 | } |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 914 | mActivityTaskManagerInternal.startActivityAsUser(caller, callingPackage, |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 915 | intent, opts, user.getIdentifier()); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 918 | /** Checks if user is a profile of or same as listeningUser. |
| 919 | * and the user is enabled. */ |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 920 | private boolean isEnabledProfileOf(UserHandle listeningUser, UserHandle user, |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 921 | String debugMsg) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 922 | return mUserManagerInternal.isProfileAccessible(listeningUser.getIdentifier(), |
| 923 | user.getIdentifier(), debugMsg, false); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 926 | @VisibleForTesting |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 927 | void postToPackageMonitorHandler(Runnable r) { |
Makoto Onuki | c5475d4 | 2016-03-25 10:54:07 -0700 | [diff] [blame] | 928 | mCallbackHandler.post(r); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | private class MyPackageMonitor extends PackageMonitor implements ShortcutChangeListener { |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 932 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 933 | // TODO Simplify with lambdas. |
| 934 | |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 935 | @Override |
| 936 | public void onPackageAdded(String packageName, int uid) { |
| 937 | UserHandle user = new UserHandle(getChangingUserId()); |
| 938 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 939 | try { |
| 940 | for (int i = 0; i < n; i++) { |
| 941 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 942 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 943 | if (!isEnabledProfileOf(cookie.user, user, "onPackageAdded")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 944 | try { |
| 945 | listener.onPackageAdded(user, packageName); |
| 946 | } catch (RemoteException re) { |
| 947 | Slog.d(TAG, "Callback failed ", re); |
| 948 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 949 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 950 | } finally { |
| 951 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 952 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 953 | |
| 954 | super.onPackageAdded(packageName, uid); |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void onPackageRemoved(String packageName, int uid) { |
| 959 | UserHandle user = new UserHandle(getChangingUserId()); |
| 960 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 961 | try { |
| 962 | for (int i = 0; i < n; i++) { |
| 963 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 964 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 965 | if (!isEnabledProfileOf(cookie.user, user, "onPackageRemoved")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 966 | try { |
| 967 | listener.onPackageRemoved(user, packageName); |
| 968 | } catch (RemoteException re) { |
| 969 | Slog.d(TAG, "Callback failed ", re); |
| 970 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 971 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 972 | } finally { |
| 973 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 974 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 975 | |
| 976 | super.onPackageRemoved(packageName, uid); |
| 977 | } |
| 978 | |
| 979 | @Override |
| 980 | public void onPackageModified(String packageName) { |
| 981 | UserHandle user = new UserHandle(getChangingUserId()); |
| 982 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 983 | try { |
| 984 | for (int i = 0; i < n; i++) { |
| 985 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 986 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 987 | if (!isEnabledProfileOf(cookie.user, user, "onPackageModified")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 988 | try { |
| 989 | listener.onPackageChanged(user, packageName); |
| 990 | } catch (RemoteException re) { |
| 991 | Slog.d(TAG, "Callback failed ", re); |
| 992 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 993 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 994 | } finally { |
| 995 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 996 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 997 | |
| 998 | super.onPackageModified(packageName); |
| 999 | } |
| 1000 | |
| 1001 | @Override |
| 1002 | public void onPackagesAvailable(String[] packages) { |
| 1003 | UserHandle user = new UserHandle(getChangingUserId()); |
| 1004 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1005 | try { |
| 1006 | for (int i = 0; i < n; i++) { |
| 1007 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 1008 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1009 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesAvailable")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1010 | try { |
| 1011 | listener.onPackagesAvailable(user, packages, isReplacing()); |
| 1012 | } catch (RemoteException re) { |
| 1013 | Slog.d(TAG, "Callback failed ", re); |
| 1014 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1015 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1016 | } finally { |
| 1017 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1018 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1019 | |
| 1020 | super.onPackagesAvailable(packages); |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | public void onPackagesUnavailable(String[] packages) { |
| 1025 | UserHandle user = new UserHandle(getChangingUserId()); |
| 1026 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1027 | try { |
| 1028 | for (int i = 0; i < n; i++) { |
| 1029 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 1030 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1031 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesUnavailable")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1032 | try { |
| 1033 | listener.onPackagesUnavailable(user, packages, isReplacing()); |
| 1034 | } catch (RemoteException re) { |
| 1035 | Slog.d(TAG, "Callback failed ", re); |
| 1036 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1037 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1038 | } finally { |
| 1039 | mListeners.finishBroadcast(); |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1040 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1041 | |
| 1042 | super.onPackagesUnavailable(packages); |
| 1043 | } |
| 1044 | |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1045 | @Override |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 1046 | public void onPackagesSuspended(String[] packages) { |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1047 | UserHandle user = new UserHandle(getChangingUserId()); |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 1048 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
| 1049 | final ArrayList<Pair<String, Bundle>> packagesWithExtras = new ArrayList<>(); |
| 1050 | final ArrayList<String> packagesWithoutExtras = new ArrayList<>(); |
| 1051 | for (String pkg : packages) { |
| 1052 | final Bundle launcherExtras = pmi.getSuspendedPackageLauncherExtras(pkg, |
| 1053 | user.getIdentifier()); |
| 1054 | if (launcherExtras != null) { |
| 1055 | packagesWithExtras.add(new Pair<>(pkg, launcherExtras)); |
| 1056 | } else { |
| 1057 | packagesWithoutExtras.add(pkg); |
| 1058 | } |
| 1059 | } |
| 1060 | final String[] packagesNullExtras = packagesWithoutExtras.toArray( |
| 1061 | new String[packagesWithoutExtras.size()]); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1062 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1063 | try { |
| 1064 | for (int i = 0; i < n; i++) { |
| 1065 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 1066 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1067 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesSuspended")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1068 | try { |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 1069 | listener.onPackagesSuspended(user, packagesNullExtras, null); |
| 1070 | for (int idx = 0; idx < packagesWithExtras.size(); idx++) { |
| 1071 | Pair<String, Bundle> packageExtraPair = packagesWithExtras.get(idx); |
| 1072 | listener.onPackagesSuspended(user, |
| 1073 | new String[]{packageExtraPair.first}, |
| 1074 | packageExtraPair.second); |
| 1075 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1076 | } catch (RemoteException re) { |
| 1077 | Slog.d(TAG, "Callback failed ", re); |
| 1078 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1079 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1080 | } finally { |
| 1081 | mListeners.finishBroadcast(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1082 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | @Override |
| 1086 | public void onPackagesUnsuspended(String[] packages) { |
| 1087 | UserHandle user = new UserHandle(getChangingUserId()); |
| 1088 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1089 | try { |
| 1090 | for (int i = 0; i < n; i++) { |
| 1091 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 1092 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1093 | if (!isEnabledProfileOf(cookie.user, user, "onPackagesUnsuspended")) continue; |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1094 | try { |
| 1095 | listener.onPackagesUnsuspended(user, packages); |
| 1096 | } catch (RemoteException re) { |
| 1097 | Slog.d(TAG, "Callback failed ", re); |
| 1098 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1099 | } |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1100 | } finally { |
| 1101 | mListeners.finishBroadcast(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1102 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1103 | |
| 1104 | super.onPackagesUnsuspended(packages); |
| 1105 | } |
| 1106 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1107 | @Override |
| 1108 | public void onShortcutChanged(@NonNull String packageName, |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1109 | @UserIdInt int userId) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1110 | postToPackageMonitorHandler(() -> onShortcutChangedInner(packageName, userId)); |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | private void onShortcutChangedInner(@NonNull String packageName, |
| 1114 | @UserIdInt int userId) { |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1115 | final int n = mListeners.beginBroadcast(); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1116 | try { |
| 1117 | final UserHandle user = UserHandle.of(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1118 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1119 | for (int i = 0; i < n; i++) { |
| 1120 | IOnAppsChangedListener listener = mListeners.getBroadcastItem(i); |
| 1121 | BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i); |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1122 | if (!isEnabledProfileOf(cookie.user, user, "onShortcutChanged")) continue; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 1123 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1124 | final int launcherUserId = cookie.user.getIdentifier(); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 1125 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1126 | // Make sure the caller has the permission. |
| 1127 | if (!mShortcutServiceInternal.hasShortcutHostPermission( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 1128 | launcherUserId, cookie.packageName, |
| 1129 | cookie.callingPid, cookie.callingUid)) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1130 | continue; |
| 1131 | } |
| 1132 | // Each launcher has a different set of pinned shortcuts, so we need to do a |
| 1133 | // query in here. |
| 1134 | // (As of now, only one launcher has the permission at a time, so it's bit |
| 1135 | // moot, but we may change the permission model eventually.) |
| 1136 | final List<ShortcutInfo> list = |
| 1137 | mShortcutServiceInternal.getShortcuts(launcherUserId, |
| 1138 | cookie.packageName, |
| 1139 | /* changedSince= */ 0, packageName, /* shortcutIds=*/ null, |
| 1140 | /* component= */ null, |
| 1141 | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 1142 | | ShortcutQuery.FLAG_MATCH_ALL_KINDS_WITH_ALL_PINNED |
| 1143 | , userId, cookie.callingPid, cookie.callingUid); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1144 | try { |
| 1145 | listener.onShortcutChanged(user, packageName, |
| 1146 | new ParceledListSlice<>(list)); |
| 1147 | } catch (RemoteException re) { |
| 1148 | Slog.d(TAG, "Callback failed ", re); |
| 1149 | } |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1150 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1151 | } catch (RuntimeException e) { |
| 1152 | // When the user is locked we get IllegalState, so just catch all. |
| 1153 | Log.w(TAG, e.getMessage(), e); |
Makoto Onuki | 7bdb9ce | 2016-09-15 12:52:57 -0700 | [diff] [blame] | 1154 | } finally { |
| 1155 | mListeners.finishBroadcast(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1156 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1157 | } |
Amith Yamasani | df7db07 | 2014-06-01 10:41:13 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | class PackageCallbackList<T extends IInterface> extends RemoteCallbackList<T> { |
| 1161 | @Override |
| 1162 | public void onCallbackDied(T callback, Object cookie) { |
| 1163 | checkCallbackCount(); |
| 1164 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1165 | } |
| 1166 | } |
Nicolas Prevot | b683019 | 2015-06-26 13:45:12 -0700 | [diff] [blame] | 1167 | } |