Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 16 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 17 | package com.android.car.pm; |
| 18 | |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 19 | import static android.Manifest.permission.QUERY_ALL_PACKAGES; |
| 20 | |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 23 | import android.annotation.UserIdInt; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 25 | import android.app.ActivityTaskManager.RootTaskInfo; |
Arnaud Berry | d1f88ee | 2020-02-19 13:49:31 -0800 | [diff] [blame] | 26 | import android.app.PendingIntent; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 27 | import android.car.Car; |
| 28 | import android.car.content.pm.AppBlockingPackageInfo; |
| 29 | import android.car.content.pm.CarAppBlockingPolicy; |
| 30 | import android.car.content.pm.CarAppBlockingPolicyService; |
| 31 | import android.car.content.pm.CarPackageManager; |
| 32 | import android.car.content.pm.ICarPackageManager; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 33 | import android.car.drivingstate.CarUxRestrictions; |
| 34 | import android.car.drivingstate.ICarUxRestrictionsChangeListener; |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 35 | import android.car.hardware.power.CarPowerPolicy; |
| 36 | import android.car.hardware.power.CarPowerPolicyFilter; |
| 37 | import android.car.hardware.power.ICarPowerPolicyListener; |
| 38 | import android.car.hardware.power.PowerComponent; |
| 39 | import android.car.user.CarUserManager; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 41 | import android.content.ComponentName; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 42 | import android.content.Context; |
| 43 | import android.content.Intent; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 44 | import android.content.IntentFilter; |
| 45 | import android.content.pm.ActivityInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 46 | import android.content.pm.PackageInfo; |
| 47 | import android.content.pm.PackageManager; |
| 48 | import android.content.pm.PackageManager.NameNotFoundException; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 49 | import android.content.pm.ResolveInfo; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 50 | import android.content.pm.ServiceInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 51 | import android.content.pm.Signature; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 52 | import android.content.res.Resources; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 53 | import android.hardware.display.DisplayManager; |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 54 | import android.os.Binder; |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 55 | import android.os.Build; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 56 | import android.os.Handler; |
| 57 | import android.os.HandlerThread; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 58 | import android.os.IBinder; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 59 | import android.os.Looper; |
| 60 | import android.os.Message; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 61 | import android.os.ParcelFileDescriptor; |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 62 | import android.os.Process; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 63 | import android.os.RemoteException; |
| 64 | import android.os.ServiceManager; |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 65 | import android.os.ServiceSpecificException; |
| 66 | import android.os.SystemProperties; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
Gaurav Bhola | 38569c7 | 2021-06-30 15:36:20 +0000 | [diff] [blame] | 68 | import android.text.TextUtils; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 69 | import android.util.ArraySet; |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 70 | import android.util.IndentingPrintWriter; |
Bin Tan | 2536d40 | 2021-02-10 15:28:35 -0800 | [diff] [blame] | 71 | import android.util.LocalLog; |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 72 | import android.util.Log; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 73 | import android.util.Pair; |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame] | 74 | import android.util.Slog; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 75 | import android.util.SparseArray; |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 76 | import android.view.Display; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 77 | import android.view.DisplayAddress; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 78 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 79 | import com.android.car.CarLocalServices; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 80 | import com.android.car.CarLog; |
| 81 | import com.android.car.CarServiceBase; |
| 82 | import com.android.car.CarServiceUtils; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 83 | import com.android.car.CarUxRestrictionsManagerService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 84 | import com.android.car.R; |
| 85 | import com.android.car.SystemActivityMonitoringService; |
| 86 | import com.android.car.SystemActivityMonitoringService.TopTaskInfoContainer; |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 87 | import com.android.car.power.CarPowerManagementService; |
| 88 | import com.android.car.user.CarUserService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 89 | import com.android.internal.annotations.GuardedBy; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 91 | import com.android.server.utils.Slogf; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 92 | |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 93 | import com.google.android.collect.Sets; |
| 94 | |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 95 | import java.io.BufferedReader; |
| 96 | import java.io.FileReader; |
| 97 | import java.io.IOException; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 98 | import java.lang.ref.WeakReference; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 99 | import java.util.ArrayList; |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 100 | import java.util.Arrays; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 101 | import java.util.HashMap; |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 102 | import java.util.HashSet; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 103 | import java.util.LinkedList; |
| 104 | import java.util.List; |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 105 | import java.util.Map; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 106 | import java.util.Map.Entry; |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 107 | import java.util.Objects; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 108 | import java.util.Set; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 109 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 110 | public class CarPackageManagerService extends ICarPackageManager.Stub implements CarServiceBase { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 111 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 112 | static final boolean DBG = false; |
| 113 | |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 114 | private static final String TAG = CarLog.tagFor(CarPackageManagerService.class); |
| 115 | |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 116 | // Delimiters to parse packages and activities in the configuration XML resource. |
| 117 | private static final String PACKAGE_DELIMITER = ","; |
| 118 | private static final String PACKAGE_ACTIVITY_DELIMITER = "/"; |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 119 | private static final int LOG_SIZE = 20; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 120 | private static final String[] WINDOW_DUMP_ARGUMENTS = new String[]{"windows"}; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 121 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 122 | private static final String PROPERTY_RO_DRIVING_SAFETY_REGION = |
| 123 | "ro.android.car.drivingsafetyregion"; |
| 124 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 125 | private final Context mContext; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 126 | private final SystemActivityMonitoringService mSystemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 127 | private final PackageManager mPackageManager; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 128 | private final ActivityManager mActivityManager; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 129 | private final DisplayManager mDisplayManager; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 130 | private final IBinder mWindowManagerBinder; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 131 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 132 | private final HandlerThread mHandlerThread = CarServiceUtils.getHandlerThread( |
| 133 | getClass().getSimpleName()); |
| 134 | private final PackageHandler mHandler = new PackageHandler(mHandlerThread.getLooper(), this); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 135 | private final Object mLock = new Object(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 136 | |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 137 | // For dumpsys logging. |
Bin Tan | 2536d40 | 2021-02-10 15:28:35 -0800 | [diff] [blame] | 138 | private final LocalLog mBlockedActivityLogs = new LocalLog(LOG_SIZE); |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 139 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 140 | // Store the allowlist and blocklist strings from the resource file. |
| 141 | private String mConfiguredAllowlist; |
| 142 | private String mConfiguredSystemAllowlist; |
| 143 | private String mConfiguredBlocklist; |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 144 | @GuardedBy("mLock") |
| 145 | private Map<String, Set<String>> mConfiguredAllowlistMap; |
| 146 | @GuardedBy("mLock") |
| 147 | private Map<String, Set<String>> mConfiguredBlocklistMap; |
| 148 | |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 149 | private final List<String> mAllowedAppInstallSources; |
| 150 | |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 151 | @GuardedBy("mLock") |
| 152 | private final SparseArray<ComponentName> mTopActivityWithDialogPerDisplay = new SparseArray<>(); |
| 153 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Hold policy set from policy service or client. |
| 156 | * Key: packageName of policy service |
| 157 | */ |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 158 | @GuardedBy("mLock") |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 159 | private final HashMap<String, ClientPolicy> mClientPolicies = new HashMap<>(); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 160 | @GuardedBy("mLock") |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 161 | private HashMap<String, AppBlockingPackageInfoWrapper> mActivityAllowlistMap = new HashMap<>(); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 162 | @GuardedBy("mLock") |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 163 | private HashSet<String> mActivityDenylistPackages = new HashSet<String>(); |
| 164 | |
| 165 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 166 | private LinkedList<AppBlockingPolicyProxy> mProxies; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 167 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 168 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 169 | private final LinkedList<CarAppBlockingPolicy> mWaitingPolicies = new LinkedList<>(); |
| 170 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 171 | @GuardedBy("mLock") |
| 172 | private String mCurrentDrivingSafetyRegion = CarPackageManager.DRIVING_SAFETY_REGION_ALL; |
| 173 | // Package name + '/' + className format |
| 174 | @GuardedBy("mLock") |
| 175 | private final ArraySet<String> mTempAllowedActivities = new ArraySet<>(); |
| 176 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 177 | private final CarUxRestrictionsManagerService mCarUxRestrictionsService; |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 178 | private final boolean mEnableActivityBlocking; |
| 179 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 180 | private final ComponentName mActivityBlockingActivity; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 181 | private final boolean mPreventTemplatedAppsFromShowingDialog; |
| 182 | private final String mTemplateActivityClassName; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 183 | |
| 184 | private final ActivityLaunchListener mActivityLaunchListener = new ActivityLaunchListener(); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 185 | |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 186 | // K: (logical) display id of a physical display, V: UXR change listener of this display. |
| 187 | // For multi-display, monitor UXR change on each display. |
| 188 | private final SparseArray<UxRestrictionsListener> mUxRestrictionsListeners = |
| 189 | new SparseArray<>(); |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 190 | private final VendorServiceController mVendorServiceController; |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 191 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 192 | // Information related to when the installed packages should be parsed for building a allow and |
| 193 | // block list |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 194 | private final Set<String> mPackageManagerActions = Sets.newArraySet( |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 195 | Intent.ACTION_PACKAGE_ADDED, |
| 196 | Intent.ACTION_PACKAGE_CHANGED, |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 197 | Intent.ACTION_PACKAGE_REMOVED, |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 198 | Intent.ACTION_PACKAGE_REPLACED); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 199 | |
| 200 | private final PackageParsingEventReceiver mPackageParsingEventReceiver = |
| 201 | new PackageParsingEventReceiver(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 202 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Name of blocked activity. |
| 205 | * |
| 206 | * @hide |
| 207 | */ |
| 208 | public static final String BLOCKING_INTENT_EXTRA_BLOCKED_ACTIVITY_NAME = "blocked_activity"; |
| 209 | /** |
| 210 | * int task id of the blocked task. |
| 211 | * |
| 212 | * @hide |
| 213 | */ |
| 214 | public static final String BLOCKING_INTENT_EXTRA_BLOCKED_TASK_ID = "blocked_task_id"; |
| 215 | /** |
| 216 | * Name of root activity of blocked task. |
| 217 | * |
| 218 | * @hide |
| 219 | */ |
| 220 | public static final String BLOCKING_INTENT_EXTRA_ROOT_ACTIVITY_NAME = "root_activity_name"; |
| 221 | /** |
| 222 | * Boolean indicating whether the root activity is distraction-optimized (DO). |
| 223 | * Blocking screen should show a button to restart the task if {@code true}. |
| 224 | * |
| 225 | * @hide |
| 226 | */ |
| 227 | public static final String BLOCKING_INTENT_EXTRA_IS_ROOT_ACTIVITY_DO = "is_root_activity_do"; |
| 228 | |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 229 | /** |
| 230 | * int display id of the blocked task. |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 231 | * |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 232 | * @hide |
| 233 | */ |
| 234 | public static final String BLOCKING_INTENT_EXTRA_DISPLAY_ID = "display_id"; |
| 235 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 236 | private final CarUserManager.UserLifecycleListener mUserLifecycleListener = |
| 237 | new CarUserManager.UserLifecycleListener() { |
| 238 | @Override |
| 239 | public void onEvent(CarUserManager.UserLifecycleEvent event) { |
| 240 | if (event.getEventType() |
| 241 | == CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING) { |
| 242 | synchronized (mLock) { |
| 243 | resetTempAllowedActivitiesLocked(); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | private final ICarPowerPolicyListener mDisplayPowerPolicyListener = |
| 250 | new ICarPowerPolicyListener.Stub() { |
| 251 | @Override |
| 252 | public void onPolicyChanged(CarPowerPolicy policy, |
| 253 | CarPowerPolicy accumulatedPolicy) { |
| 254 | if (!policy.isComponentEnabled(PowerComponent.DISPLAY)) { |
| 255 | synchronized (mLock) { |
| 256 | resetTempAllowedActivitiesLocked(); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 262 | public CarPackageManagerService(Context context, |
| 263 | CarUxRestrictionsManagerService uxRestrictionsService, |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 264 | SystemActivityMonitoringService systemActivityMonitoringService) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 265 | mContext = context; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 266 | mCarUxRestrictionsService = uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 267 | mSystemActivityMonitoringService = systemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 268 | mPackageManager = mContext.getPackageManager(); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 269 | mActivityManager = mContext.getSystemService(ActivityManager.class); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 270 | mDisplayManager = mContext.getSystemService(DisplayManager.class); |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 271 | mWindowManagerBinder = ServiceManager.getService(Context.WINDOW_SERVICE); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 272 | Resources res = context.getResources(); |
| 273 | mEnableActivityBlocking = res.getBoolean(R.bool.enableActivityBlockingForSafety); |
| 274 | String blockingActivity = res.getString(R.string.activityBlockingActivity); |
| 275 | mActivityBlockingActivity = ComponentName.unflattenFromString(blockingActivity); |
Gaurav Bhola | cb9439b | 2021-10-06 00:31:20 +0000 | [diff] [blame^] | 276 | if (mEnableActivityBlocking && mActivityBlockingActivity == null) { |
| 277 | Slogf.wtf(TAG, "mActivityBlockingActivity can't be null when enabled"); |
| 278 | } |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 279 | mAllowedAppInstallSources = Arrays.asList( |
| 280 | res.getStringArray(R.array.allowedAppInstallSources)); |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 281 | mVendorServiceController = new VendorServiceController( |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 282 | mContext, mHandler.getLooper()); |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 283 | mPreventTemplatedAppsFromShowingDialog = |
| 284 | res.getBoolean(R.bool.config_preventTemplatedAppsFromShowingDialog); |
| 285 | mTemplateActivityClassName = res.getString(R.string.config_template_activity_class_name); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 288 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 289 | @Override |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 290 | public void setAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 291 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 292 | Slog.d(TAG, "policy setting from binder call, client:" + packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 293 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 294 | doSetAppBlockingPolicy(packageName, policy, flags); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Restarts the requested task. If task with {@code taskId} does not exist, do nothing. |
| 299 | */ |
| 300 | @Override |
| 301 | public void restartTask(int taskId) { |
Keun young Park | 572023a | 2020-06-03 17:29:36 -0700 | [diff] [blame] | 302 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.REAL_GET_TASKS) |
| 303 | != PackageManager.PERMISSION_GRANTED) { |
| 304 | throw new SecurityException( |
| 305 | "requires permission " + android.Manifest.permission.REAL_GET_TASKS); |
| 306 | } |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 307 | mSystemActivityMonitoringService.restartTask(taskId); |
| 308 | } |
| 309 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public String getCurrentDrivingSafetyRegion() { |
| 312 | assertAppBlockingOrDrivingStatePermission(); |
| 313 | synchronized (mLock) { |
| 314 | return mCurrentDrivingSafetyRegion; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | private String getComponentNameString(String packageName, String className) { |
| 319 | return packageName + '/' + className; |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public void controlOneTimeActivityBlockingBypassingAsUser(String packageName, |
| 324 | String activityClassName, boolean bypass, @UserIdInt int userId) { |
| 325 | assertAppBlockingPermission(); |
| 326 | if (!callerCanQueryPackage(packageName)) { |
| 327 | throw new SecurityException("cannot query other package"); |
| 328 | } |
| 329 | try { |
| 330 | // Read this to check the validity of pkg / activity name. Not checking this can allow |
| 331 | // bad apps to be allowed later. |
| 332 | CarAppMetadataReader.getSupportedDrivingSafetyRegionsForActivityAsUser(mContext, |
| 333 | packageName, activityClassName, userId); |
| 334 | } catch (NameNotFoundException e) { |
| 335 | throw new ServiceSpecificException(CarPackageManager.ERROR_CODE_NO_PACKAGE, |
| 336 | e.getMessage()); |
| 337 | } |
| 338 | String componentName = getComponentNameString(packageName, activityClassName); |
| 339 | synchronized (mLock) { |
| 340 | if (bypass) { |
| 341 | mTempAllowedActivities.add(componentName); |
| 342 | } else { |
| 343 | mTempAllowedActivities.remove(componentName); |
| 344 | } |
| 345 | } |
| 346 | if (!bypass) { // block top activities if bypassing is disabled. |
| 347 | blockTopActivitiesIfNecessary(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @GuardedBy("mLock") |
| 352 | private void resetTempAllowedActivitiesLocked() { |
| 353 | mTempAllowedActivities.clear(); |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public List<String> getSupportedDrivingSafetyRegionsForActivityAsUser(String packageName, |
| 358 | String activityClassName, @UserIdInt int userId) { |
| 359 | assertAppBlockingOrDrivingStatePermission(); |
| 360 | if (!callerCanQueryPackage(packageName)) { |
| 361 | throw new SecurityException("cannot query other package"); |
| 362 | } |
| 363 | long token = Binder.clearCallingIdentity(); |
| 364 | try { |
| 365 | return CarAppMetadataReader.getSupportedDrivingSafetyRegionsForActivityAsUser(mContext, |
| 366 | packageName, activityClassName, userId); |
| 367 | } catch (NameNotFoundException e) { |
| 368 | throw new ServiceSpecificException(CarPackageManager.ERROR_CODE_NO_PACKAGE, |
| 369 | e.getMessage()); |
| 370 | } finally { |
| 371 | Binder.restoreCallingIdentity(token); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private void assertAppBlockingOrDrivingStatePermission() { |
| 376 | if (mContext.checkCallingOrSelfPermission(Car.PERMISSION_CONTROL_APP_BLOCKING) |
| 377 | != PackageManager.PERMISSION_GRANTED && mContext.checkCallingOrSelfPermission( |
| 378 | Car.PERMISSION_CAR_DRIVING_STATE) != PackageManager.PERMISSION_GRANTED) { |
| 379 | throw new SecurityException( |
| 380 | "requires permission " + Car.PERMISSION_CONTROL_APP_BLOCKING + " or " |
| 381 | + Car.PERMISSION_CAR_DRIVING_STATE); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | private void assertAppBlockingPermission() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 386 | if (mContext.checkCallingOrSelfPermission(Car.PERMISSION_CONTROL_APP_BLOCKING) |
| 387 | != PackageManager.PERMISSION_GRANTED) { |
| 388 | throw new SecurityException( |
| 389 | "requires permission " + Car.PERMISSION_CONTROL_APP_BLOCKING); |
| 390 | } |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | private void doSetAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, |
| 394 | int flags) { |
| 395 | assertAppBlockingPermission(); |
Enrico Granata | 3da3c61 | 2017-01-20 15:24:30 -0800 | [diff] [blame] | 396 | CarServiceUtils.assertPackageName(mContext, packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 397 | if (policy == null) { |
| 398 | throw new IllegalArgumentException("policy cannot be null"); |
| 399 | } |
| 400 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0 && |
| 401 | (flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
| 402 | throw new IllegalArgumentException( |
| 403 | "Cannot set both FLAG_SET_POLICY_ADD and FLAG_SET_POLICY_REMOVE flag"); |
| 404 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 405 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 406 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 407 | mWaitingPolicies.add(policy); |
| 408 | } |
| 409 | } |
| 410 | mHandler.requestUpdatingPolicy(packageName, policy, flags); |
| 411 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
| 412 | synchronized (mLock) { |
| 413 | try { |
| 414 | while (mWaitingPolicies.contains(policy)) { |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 415 | mLock.wait(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 416 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 417 | } catch (InterruptedException e) { |
| 418 | // Pass it over binder call |
| 419 | throw new IllegalStateException( |
| 420 | "Interrupted while waiting for policy completion", e); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | @Override |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 427 | public boolean isActivityDistractionOptimized(String packageName, String className) { |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 428 | if (!callerCanQueryPackage(packageName)) return false; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 429 | assertPackageAndClassName(packageName, className); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 430 | synchronized (mLock) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 431 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 432 | Slog.d(TAG, "isActivityDistractionOptimized" + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 433 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 434 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 435 | if (mTempAllowedActivities.contains(getComponentNameString(packageName, |
| 436 | className))) { |
| 437 | return true; |
| 438 | } |
| 439 | |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 440 | for (int i = mTopActivityWithDialogPerDisplay.size() - 1; i >= 0; i--) { |
| 441 | ComponentName activityWithDialog = mTopActivityWithDialogPerDisplay.get( |
| 442 | mTopActivityWithDialogPerDisplay.keyAt(i)); |
| 443 | if (activityWithDialog.getClassName().equals(className) |
| 444 | && activityWithDialog.getPackageName().equals(packageName)) { |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 449 | if (searchFromClientPolicyBlocklistsLocked(packageName)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 450 | return false; |
| 451 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 452 | |
| 453 | if (isActivityInClientPolicyAllowlistsLocked(packageName, className)) { |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | // Check deny and allow list |
| 458 | boolean packageBlocked = mActivityDenylistPackages.contains(packageName); |
| 459 | AppBlockingPackageInfoWrapper infoWrapper = mActivityAllowlistMap.get(packageName); |
| 460 | if (!packageBlocked && infoWrapper == null) { |
| 461 | // Update cache |
| 462 | updateActivityAllowlistAndDenylistMap(packageName); |
| 463 | packageBlocked = mActivityDenylistPackages.contains(packageName); |
| 464 | infoWrapper = mActivityAllowlistMap.get(packageName); |
| 465 | } |
| 466 | |
| 467 | if (packageBlocked |
| 468 | || !isActivityInMapAndMatching(infoWrapper, packageName, className)) { |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | return true; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 476 | @VisibleForTesting |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 477 | boolean callerCanQueryPackage(String packageName) { |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 478 | int callingUid = Binder.getCallingUid(); |
| 479 | if (hasPermissionGranted(QUERY_ALL_PACKAGES, callingUid)) { |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 480 | return true; |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 481 | } |
| 482 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
| 483 | if (packages != null && packages.length > 0) { |
| 484 | for (int i = 0; i < packages.length; i++) { |
| 485 | if (Objects.equals(packageName, packages[i])) { |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 486 | return true; |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 491 | Slog.w(TAG, QUERY_ALL_PACKAGES + " permission is needed to query other packages."); |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 492 | |
| 493 | return false; |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | private static boolean hasPermissionGranted(String permission, int uid) { |
| 497 | return ActivityManager.checkComponentPermission(permission, uid, |
| 498 | /* owningUid= */ Process.INVALID_UID, |
| 499 | /* exported= */ true) == PackageManager.PERMISSION_GRANTED; |
| 500 | } |
| 501 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 502 | @Override |
Arnaud Berry | d1f88ee | 2020-02-19 13:49:31 -0800 | [diff] [blame] | 503 | public boolean isPendingIntentDistractionOptimized(PendingIntent pendingIntent) { |
| 504 | ResolveInfo info = mPackageManager.resolveActivity( |
| 505 | pendingIntent.getIntent(), PackageManager.MATCH_DEFAULT_ONLY); |
| 506 | if (info == null) return false; |
| 507 | ActivityInfo activityInfo = info.activityInfo; |
| 508 | return isActivityDistractionOptimized(activityInfo.packageName, activityInfo.name); |
| 509 | } |
| 510 | |
| 511 | @Override |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 512 | public boolean isServiceDistractionOptimized(String packageName, String className) { |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 513 | if (!callerCanQueryPackage(packageName)) return false; |
| 514 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 515 | if (packageName == null) { |
| 516 | throw new IllegalArgumentException("Package name null"); |
| 517 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 518 | synchronized (mLock) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 519 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 520 | Slog.d(TAG, "isServiceDistractionOptimized" + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 521 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 522 | |
| 523 | if (searchFromClientPolicyBlocklistsLocked(packageName)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 524 | return false; |
| 525 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 526 | |
| 527 | if (searchFromClientPolicyAllowlistsLocked(packageName)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 528 | return true; |
| 529 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 530 | |
| 531 | // Check deny and allow list |
| 532 | boolean packageBlocked = mActivityDenylistPackages.contains(packageName); |
| 533 | AppBlockingPackageInfoWrapper infoWrapper = mActivityAllowlistMap.get(packageName); |
| 534 | if (!packageBlocked && infoWrapper == null) { |
| 535 | // Update cache |
| 536 | updateActivityAllowlistAndDenylistMap(packageName); |
| 537 | packageBlocked = mActivityDenylistPackages.contains(packageName); |
| 538 | infoWrapper = mActivityAllowlistMap.get(packageName); |
| 539 | } |
| 540 | |
| 541 | if (packageBlocked || infoWrapper == null || infoWrapper.info == null) { |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | return true; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 546 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 549 | @Override |
| 550 | public boolean isActivityBackedBySafeActivity(ComponentName activityName) { |
Mayank Garg | 771c745 | 2021-03-09 13:33:26 -0800 | [diff] [blame] | 551 | if (activityName == null) return false; |
Mayank Garg | 892160e | 2021-03-22 22:43:23 -0700 | [diff] [blame] | 552 | if (!callerCanQueryPackage(activityName.getPackageName())) return false; |
| 553 | |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 554 | RootTaskInfo info = mSystemActivityMonitoringService.getFocusedStackForTopActivity( |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 555 | activityName); |
| 556 | if (info == null) { // not top in focused stack |
| 557 | return true; |
| 558 | } |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 559 | if (!isUxRestrictedOnDisplay(info.displayId)) { |
| 560 | return true; |
| 561 | } |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 562 | if (info.childTaskNames.length <= 1) { // nothing below this. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | ComponentName activityBehind = ComponentName.unflattenFromString( |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 566 | info.childTaskNames[info.childTaskNames.length - 2]); |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 567 | return isActivityDistractionOptimized(activityBehind.getPackageName(), |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 568 | activityBehind.getClassName()); |
| 569 | } |
| 570 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 571 | public Looper getLooper() { |
| 572 | return mHandlerThread.getLooper(); |
| 573 | } |
| 574 | |
| 575 | private void assertPackageAndClassName(String packageName, String className) { |
| 576 | if (packageName == null) { |
| 577 | throw new IllegalArgumentException("Package name null"); |
| 578 | } |
| 579 | if (className == null) { |
| 580 | throw new IllegalArgumentException("Class name null"); |
| 581 | } |
| 582 | } |
| 583 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 584 | @GuardedBy("mLock") |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 585 | private boolean searchFromClientPolicyBlocklistsLocked(String packageName) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 586 | for (ClientPolicy policy : mClientPolicies.values()) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 587 | AppBlockingPackageInfoWrapper wrapper = policy.mBlocklistsMap.get(packageName); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 588 | if (wrapper != null && wrapper.isMatching && wrapper.info != null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 592 | |
| 593 | return false; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 596 | @GuardedBy("mLock") |
| 597 | private boolean searchFromClientPolicyAllowlistsLocked(String packageName) { |
| 598 | for (ClientPolicy policy : mClientPolicies.values()) { |
| 599 | AppBlockingPackageInfoWrapper wrapper = policy.mAllowlistsMap.get(packageName); |
| 600 | if (wrapper != null && wrapper.isMatching && wrapper.info != null) { |
| 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | @GuardedBy("mLock") |
| 608 | private boolean isActivityInClientPolicyAllowlistsLocked(String packageName, String className) { |
| 609 | for (ClientPolicy policy : mClientPolicies.values()) { |
| 610 | if (isActivityInMapAndMatching(policy.mAllowlistsMap.get(packageName), packageName, |
| 611 | className)) { |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | private boolean isActivityInMapAndMatching(AppBlockingPackageInfoWrapper wrapper, |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 619 | String packageName, String className) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 620 | if (wrapper == null || !wrapper.isMatching) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 621 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 622 | Slog.d(TAG, "Pkg not in allowlist:" + packageName); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 623 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | return wrapper.info.isActivityCovered(className); |
| 627 | } |
| 628 | |
| 629 | @Override |
| 630 | public void init() { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 631 | String safetyRegion = SystemProperties.get(PROPERTY_RO_DRIVING_SAFETY_REGION, ""); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 632 | synchronized (mLock) { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 633 | setDrivingSafetyRegionWithCheckLocked(safetyRegion); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 634 | mHandler.requestInit(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 635 | } |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 636 | CarLocalServices.getService(CarUserService.class).addUserLifecycleListener( |
| 637 | mUserLifecycleListener); |
| 638 | CarLocalServices.getService(CarPowerManagementService.class).addPowerPolicyListener( |
| 639 | new CarPowerPolicyFilter.Builder().setComponents(PowerComponent.DISPLAY).build(), |
| 640 | mDisplayPowerPolicyListener); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public void release() { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 645 | CarLocalServices.getService(CarPowerManagementService.class).removePowerPolicyListener( |
| 646 | mDisplayPowerPolicyListener); |
| 647 | CarLocalServices.getService(CarUserService.class).removeUserLifecycleListener( |
| 648 | mUserLifecycleListener); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 649 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 650 | mHandler.requestRelease(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 651 | // wait for release do be done. This guarantees that init is done. |
| 652 | try { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 653 | mLock.wait(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 654 | } catch (InterruptedException e) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 655 | Slog.e(TAG, "Interrupted wait during release"); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 656 | Thread.currentThread().interrupt(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 657 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 658 | mActivityAllowlistMap.clear(); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 659 | mActivityDenylistPackages.clear(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 660 | mClientPolicies.clear(); |
| 661 | if (mProxies != null) { |
| 662 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 663 | proxy.disconnect(); |
| 664 | } |
| 665 | mProxies.clear(); |
| 666 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 667 | mWaitingPolicies.clear(); |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 668 | resetTempAllowedActivitiesLocked(); |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 669 | mLock.notifyAll(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 670 | } |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 671 | mContext.unregisterReceiver(mPackageParsingEventReceiver); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 672 | mSystemActivityMonitoringService.registerActivityLaunchListener(null); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 673 | for (int i = 0; i < mUxRestrictionsListeners.size(); i++) { |
| 674 | UxRestrictionsListener listener = mUxRestrictionsListeners.valueAt(i); |
| 675 | mCarUxRestrictionsService.unregisterUxRestrictionsChangeListener(listener); |
| 676 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 679 | @GuardedBy("mLock") |
| 680 | private void setDrivingSafetyRegionWithCheckLocked(String region) { |
| 681 | if (region.isEmpty()) { |
| 682 | mCurrentDrivingSafetyRegion = CarPackageManager.DRIVING_SAFETY_REGION_ALL; |
| 683 | } else { |
| 684 | mCurrentDrivingSafetyRegion = region; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Reset driving stat and all dynamically added allow list so that region information for |
| 690 | * all packages are reset. This also resets one time allow list. |
| 691 | */ |
| 692 | public void resetDrivingSafetyRegion(@NonNull String region) { |
| 693 | synchronized (mLock) { |
| 694 | setDrivingSafetyRegionWithCheckLocked(region); |
| 695 | resetTempAllowedActivitiesLocked(); |
| 696 | mActivityAllowlistMap.clear(); |
| 697 | mActivityDenylistPackages.clear(); |
| 698 | } |
| 699 | } |
| 700 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 701 | // run from HandlerThread |
| 702 | private void doHandleInit() { |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 703 | startAppBlockingPolicies(); |
| 704 | IntentFilter pkgParseIntent = new IntentFilter(); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 705 | for (String action : mPackageManagerActions) { |
| 706 | pkgParseIntent.addAction(action); |
| 707 | } |
| 708 | pkgParseIntent.addDataScheme("package"); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 709 | mContext.registerReceiverAsUser(mPackageParsingEventReceiver, UserHandle.ALL, |
| 710 | pkgParseIntent, null, null); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 711 | |
| 712 | List<Display> physicalDisplays = getPhysicalDisplays(); |
| 713 | |
| 714 | // Assume default display (display 0) is always a physical display. |
| 715 | Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); |
| 716 | if (!physicalDisplays.contains(defaultDisplay)) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 717 | if (Log.isLoggable(TAG, Log.INFO)) { |
| 718 | Slog.i(TAG, "Adding default display to physical displays."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 719 | } |
| 720 | physicalDisplays.add(defaultDisplay); |
| 721 | } |
| 722 | for (Display physicalDisplay : physicalDisplays) { |
| 723 | int displayId = physicalDisplay.getDisplayId(); |
| 724 | UxRestrictionsListener listener = new UxRestrictionsListener(mCarUxRestrictionsService); |
| 725 | mUxRestrictionsListeners.put(displayId, listener); |
| 726 | mCarUxRestrictionsService.registerUxRestrictionsChangeListener(listener, displayId); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 727 | } |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 728 | mVendorServiceController.init(); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 729 | mSystemActivityMonitoringService.registerActivityLaunchListener(mActivityLaunchListener); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 732 | private void doParseInstalledPackage(String packageName) { |
| 733 | // Delete the package from allowlist and denylist mapping |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 734 | synchronized (mLock) { |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 735 | mActivityDenylistPackages.remove(packageName); |
| 736 | mActivityAllowlistMap.remove(packageName); |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 737 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 738 | |
| 739 | // Generate allowlist and denylist mapping for the package |
| 740 | updateActivityAllowlistAndDenylistMap(packageName); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 741 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 744 | private void doHandleRelease() { |
| 745 | synchronized (mLock) { |
| 746 | mVendorServiceController.release(); |
| 747 | mLock.notifyAll(); |
| 748 | } |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 751 | private void doUpdatePolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 752 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 753 | Slog.d(TAG, "setting policy from:" + packageName + ",policy:" + policy + ",flags:0x" |
| 754 | + Integer.toHexString(flags)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 755 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 756 | AppBlockingPackageInfoWrapper[] blocklistWrapper = verifyList(policy.blacklists); |
| 757 | AppBlockingPackageInfoWrapper[] allowlistWrapper = verifyList(policy.whitelists); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 758 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 759 | ClientPolicy clientPolicy = mClientPolicies.get(packageName); |
| 760 | if (clientPolicy == null) { |
| 761 | clientPolicy = new ClientPolicy(); |
| 762 | mClientPolicies.put(packageName, clientPolicy); |
| 763 | } |
| 764 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 765 | clientPolicy.addToBlocklists(blocklistWrapper); |
| 766 | clientPolicy.addToAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 767 | } else if ((flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 768 | clientPolicy.removeBlocklists(blocklistWrapper); |
| 769 | clientPolicy.removeAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 770 | } else { //replace. |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 771 | clientPolicy.replaceBlocklists(blocklistWrapper); |
| 772 | clientPolicy.replaceAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 773 | } |
| 774 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 775 | mWaitingPolicies.remove(policy); |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 776 | mLock.notifyAll(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 777 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 778 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 779 | Slog.d(TAG, "policy set:" + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 780 | } |
| 781 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 782 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | private AppBlockingPackageInfoWrapper[] verifyList(AppBlockingPackageInfo[] list) { |
| 786 | if (list == null) { |
| 787 | return null; |
| 788 | } |
| 789 | LinkedList<AppBlockingPackageInfoWrapper> wrappers = new LinkedList<>(); |
| 790 | for (int i = 0; i < list.length; i++) { |
| 791 | AppBlockingPackageInfo info = list[i]; |
| 792 | if (info == null) { |
| 793 | continue; |
| 794 | } |
| 795 | boolean isMatching = isInstalledPackageMatching(info); |
| 796 | wrappers.add(new AppBlockingPackageInfoWrapper(info, isMatching)); |
| 797 | } |
| 798 | return wrappers.toArray(new AppBlockingPackageInfoWrapper[wrappers.size()]); |
| 799 | } |
| 800 | |
| 801 | boolean isInstalledPackageMatching(AppBlockingPackageInfo info) { |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 802 | PackageInfo packageInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 803 | try { |
| 804 | packageInfo = mPackageManager.getPackageInfo(info.packageName, |
| 805 | PackageManager.GET_SIGNATURES); |
| 806 | } catch (NameNotFoundException e) { |
| 807 | return false; |
| 808 | } |
| 809 | if (packageInfo == null) { |
| 810 | return false; |
| 811 | } |
| 812 | // if it is system app and client specified the flag, do not check signature |
| 813 | if ((info.flags & AppBlockingPackageInfo.FLAG_SYSTEM_APP) == 0 || |
| 814 | (!packageInfo.applicationInfo.isSystemApp() && |
| 815 | !packageInfo.applicationInfo.isUpdatedSystemApp())) { |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 816 | Signature[] signatures = packageInfo.signatures; |
| 817 | if (!isAnySignatureMatching(signatures, info.signatures)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 818 | return false; |
| 819 | } |
| 820 | } |
| 821 | int version = packageInfo.versionCode; |
| 822 | if (info.minRevisionCode == 0) { |
| 823 | if (info.maxRevisionCode == 0) { // all versions |
| 824 | return true; |
| 825 | } else { // only max version matters |
| 826 | return info.maxRevisionCode > version; |
| 827 | } |
| 828 | } else { // min version matters |
| 829 | if (info.maxRevisionCode == 0) { |
| 830 | return info.minRevisionCode < version; |
| 831 | } else { |
| 832 | return (info.minRevisionCode < version) && (info.maxRevisionCode > version); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Any signature from policy matching with package's signatures is treated as matching. |
| 839 | */ |
| 840 | boolean isAnySignatureMatching(Signature[] fromPackage, Signature[] fromPolicy) { |
| 841 | if (fromPackage == null) { |
| 842 | return false; |
| 843 | } |
| 844 | if (fromPolicy == null) { |
| 845 | return false; |
| 846 | } |
| 847 | ArraySet<Signature> setFromPackage = new ArraySet<Signature>(); |
| 848 | for (Signature sig : fromPackage) { |
| 849 | setFromPackage.add(sig); |
| 850 | } |
| 851 | for (Signature sig : fromPolicy) { |
| 852 | if (setFromPackage.contains(sig)) { |
| 853 | return true; |
| 854 | } |
| 855 | } |
| 856 | return false; |
| 857 | } |
| 858 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 859 | private AppBlockingPackageInfoWrapper getPackageInfoWrapperForUser(String packageName, |
| 860 | @UserIdInt int userId, Map<String, Set<String>> configAllowlist, |
| 861 | Map<String, Set<String>> configBlocklist) { |
| 862 | PackageInfo info; |
| 863 | try { |
| 864 | info = mPackageManager.getPackageInfoAsUser(packageName, |
| 865 | PackageManager.GET_SIGNATURES | PackageManager.GET_ACTIVITIES |
| 866 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 867 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 868 | | PackageManager.MATCH_DISABLED_COMPONENTS, |
| 869 | userId); |
| 870 | } catch (NameNotFoundException e) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 871 | Slog.w(TAG, packageName + " not installed! User Id: " + userId); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 872 | return null; |
| 873 | } |
| 874 | |
| 875 | |
| 876 | if (info == null || info.applicationInfo == null) { |
| 877 | return null; |
| 878 | } |
| 879 | |
| 880 | int flags = 0; |
| 881 | Set<String> activities = new ArraySet<>(); |
| 882 | |
| 883 | if (info.applicationInfo.isSystemApp() |
| 884 | || info.applicationInfo.isUpdatedSystemApp()) { |
| 885 | flags = AppBlockingPackageInfo.FLAG_SYSTEM_APP; |
| 886 | } |
| 887 | |
| 888 | /* 1. Check if all or some of this app is in the <activityAllowlist> or |
| 889 | <systemActivityAllowlist> in config.xml */ |
| 890 | Set<String> configActivitiesForPackage = configAllowlist.get(info.packageName); |
| 891 | if (configActivitiesForPackage != null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 892 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 893 | Slog.d(TAG, info.packageName + " allowlisted"); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 894 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 895 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 896 | if (configActivitiesForPackage.isEmpty()) { |
| 897 | // Whole Pkg has been allowlisted |
| 898 | flags |= AppBlockingPackageInfo.FLAG_WHOLE_ACTIVITY; |
| 899 | // Add all activities to the allowlist |
| 900 | List<String> activitiesForPackage = getActivitiesInPackage(info); |
| 901 | if (activitiesForPackage != null) { |
| 902 | activities.addAll(activitiesForPackage); |
| 903 | } else { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 904 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 905 | Slog.d(TAG, info.packageName + ": Activities null"); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } else { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 909 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 910 | Slog.d(TAG, "Partially Allowlisted. WL Activities: " |
| 911 | + configActivitiesForPackage); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 912 | } |
| 913 | activities.addAll(configActivitiesForPackage); |
| 914 | } |
| 915 | } |
| 916 | /* 2. If app is not listed in the config.xml check their Manifest meta-data to |
| 917 | see if they have any Distraction Optimized(DO) activities. |
| 918 | For non system apps, we check if the app install source was a permittable |
| 919 | source. This prevents side-loaded apps to fake DO. Bypass the check |
| 920 | for debug builds for development convenience. */ |
| 921 | if (!isDebugBuild() |
| 922 | && !info.applicationInfo.isSystemApp() |
| 923 | && !info.applicationInfo.isUpdatedSystemApp()) { |
| 924 | try { |
| 925 | if (mAllowedAppInstallSources != null) { |
| 926 | String installerName = mPackageManager.getInstallerPackageName( |
| 927 | info.packageName); |
| 928 | if (installerName == null || (installerName != null |
| 929 | && !mAllowedAppInstallSources.contains(installerName))) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 930 | Slog.w(TAG, |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 931 | info.packageName + " not installed from permitted sources " |
| 932 | + (installerName == null ? "NULL" : installerName)); |
| 933 | return null; |
| 934 | } |
| 935 | } |
| 936 | } catch (IllegalArgumentException e) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 937 | Slog.w(TAG, info.packageName + " not installed!"); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 938 | return null; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | try { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 943 | String[] doActivities = findDistractionOptimizedActivitiesAsUser(info.packageName, |
| 944 | userId); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 945 | if (doActivities != null) { |
| 946 | // Some of the activities in this app are Distraction Optimized. |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 947 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 948 | for (String activity : doActivities) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 949 | Slog.d(TAG, "adding " + activity + " from " + info.packageName |
| 950 | + " to allowlist"); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 951 | } |
| 952 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 953 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 954 | activities.addAll(Arrays.asList(doActivities)); |
| 955 | } |
| 956 | } catch (NameNotFoundException e) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 957 | Slog.w(TAG, "Error reading metadata: " + info.packageName); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 958 | return null; |
| 959 | } |
| 960 | |
| 961 | // Nothing to add to allowlist |
| 962 | if (activities.isEmpty()) { |
| 963 | return null; |
| 964 | } |
| 965 | |
| 966 | /* 3. Check if parsed activity is in <activityBlocklist> in config.xml. Anything |
| 967 | in blocklist should not be allowlisted, either as D.O. or by config. */ |
| 968 | if (configBlocklist.containsKey(info.packageName)) { |
| 969 | Set<String> configBlocklistActivities = configBlocklist.get(info.packageName); |
| 970 | if (configBlocklistActivities.isEmpty()) { |
| 971 | // Whole package should be blocklisted. |
| 972 | return null; |
| 973 | } |
| 974 | activities.removeAll(configBlocklistActivities); |
| 975 | } |
| 976 | |
| 977 | Signature[] signatures; |
| 978 | signatures = info.signatures; |
| 979 | AppBlockingPackageInfo appBlockingInfo = new AppBlockingPackageInfo(info.packageName, |
| 980 | /* minRevisionCode = */ 0, /* maxRevisionCode = */ 0, flags, signatures, |
| 981 | activities.toArray(new String[activities.size()])); |
| 982 | AppBlockingPackageInfoWrapper wrapper = new AppBlockingPackageInfoWrapper( |
| 983 | appBlockingInfo, true); |
| 984 | return wrapper; |
| 985 | } |
| 986 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 987 | /** |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 988 | * Update map of allowlisted packages and activities of the form {pkgName, Allowlisted |
| 989 | * activities} and set of denylisted packages. The information can come from a configuration XML |
| 990 | * resource or from the apps marking their activities as distraction optimized. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 991 | */ |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 992 | private void updateActivityAllowlistAndDenylistMap(String packageName) { |
| 993 | int userId = mActivityManager.getCurrentUser(); |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 994 | Slog.d(TAG, "Updating allowlist and denylist mapping for package: " + packageName |
| 995 | + " for UserId: " + userId); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 996 | // Get the apps/activities that are allowlisted in the configuration XML resources. |
| 997 | Map<String, Set<String>> configAllowlist = generateConfigAllowlist(); |
| 998 | Map<String, Set<String>> configBlocklist = generateConfigBlocklist(); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 999 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1000 | AppBlockingPackageInfoWrapper wrapper = |
| 1001 | getPackageInfoWrapperForUser(packageName, userId, configAllowlist, configBlocklist); |
| 1002 | |
| 1003 | if (wrapper == null && userId != UserHandle.USER_SYSTEM) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1004 | Slog.d(TAG, "Updating allowlist and denylist mapping for package: " + packageName |
| 1005 | + " for UserId: " + UserHandle.USER_SYSTEM); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1006 | // check package for system user, in case package is disabled for current user |
| 1007 | wrapper = getPackageInfoWrapperForUser(packageName, UserHandle.USER_SYSTEM, |
| 1008 | configAllowlist, configBlocklist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1009 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1010 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1011 | synchronized (mLock) { |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1012 | if (wrapper != null) { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1013 | if (DBG) { |
| 1014 | Slog.d(TAG, "Package: " + packageName + " added in allowlist."); |
| 1015 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1016 | mActivityAllowlistMap.put(packageName, wrapper); |
| 1017 | } else { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1018 | if (DBG) { |
| 1019 | Slog.d(TAG, "Package: " + packageName + " added in denylist."); |
| 1020 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1021 | mActivityDenylistPackages.add(packageName); |
| 1022 | } |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1026 | private Map<String, Set<String>> generateConfigAllowlist() { |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1027 | synchronized (mLock) { |
| 1028 | if (mConfiguredAllowlistMap != null) return mConfiguredAllowlistMap; |
| 1029 | |
| 1030 | Map<String, Set<String>> configAllowlist = new HashMap<>(); |
| 1031 | mConfiguredAllowlist = mContext.getString(R.string.activityAllowlist); |
| 1032 | if (mConfiguredAllowlist == null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1033 | Slog.w(TAG, "Allowlist is null."); |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1034 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1035 | parseConfigList(mConfiguredAllowlist, configAllowlist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1036 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1037 | mConfiguredSystemAllowlist = mContext.getString(R.string.systemActivityAllowlist); |
| 1038 | if (mConfiguredSystemAllowlist == null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1039 | Slog.w(TAG, "System allowlist is null."); |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1040 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1041 | parseConfigList(mConfiguredSystemAllowlist, configAllowlist); |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1042 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1043 | // Add the blocking overlay activity to the allowlist, since that needs to run in a |
| 1044 | // restricted state to communicate the reason an app was blocked. |
| 1045 | Set<String> defaultActivity = new ArraySet<>(); |
| 1046 | if (mActivityBlockingActivity != null) { |
| 1047 | defaultActivity.add(mActivityBlockingActivity.getClassName()); |
| 1048 | configAllowlist.put(mActivityBlockingActivity.getPackageName(), defaultActivity); |
| 1049 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1050 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1051 | mConfiguredAllowlistMap = configAllowlist; |
| 1052 | return configAllowlist; |
| 1053 | } |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1056 | private Map<String, Set<String>> generateConfigBlocklist() { |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1057 | synchronized (mLock) { |
| 1058 | if (mConfiguredBlocklistMap != null) return mConfiguredBlocklistMap; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1059 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1060 | Map<String, Set<String>> configBlocklist = new HashMap<>(); |
| 1061 | mConfiguredBlocklist = mContext.getString(R.string.activityDenylist); |
| 1062 | if (mConfiguredBlocklist == null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1063 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1064 | Slog.d(TAG, "Null blocklist in config"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1065 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 1066 | } |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1067 | parseConfigList(mConfiguredBlocklist, configBlocklist); |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 1068 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1069 | mConfiguredBlocklistMap = configBlocklist; |
| 1070 | return configBlocklist; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1071 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 1074 | private boolean isDebugBuild() { |
| 1075 | return Build.IS_USERDEBUG || Build.IS_ENG; |
| 1076 | } |
| 1077 | |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1078 | /** |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1079 | * Parses the given resource and updates the input map of packages and activities. |
| 1080 | * |
| 1081 | * Key is package name and value is list of activities. Empty set implies whole package is |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1082 | * included. |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1083 | * |
| 1084 | * When there are multiple entries regarding one package, the entry with |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1085 | * greater scope wins. Namely if there were 2 entries such that one allowlists |
| 1086 | * an activity, and the other allowlists the entire package of the activity, |
| 1087 | * the package is allowlisted, regardless of input order. |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1088 | */ |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1089 | @VisibleForTesting |
| 1090 | /* package */ void parseConfigList(String configList, |
| 1091 | @NonNull Map<String, Set<String>> packageToActivityMap) { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1092 | if (configList == null) { |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 1093 | return; |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1094 | } |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1095 | String[] entries = configList.split(PACKAGE_DELIMITER); |
| 1096 | for (String entry : entries) { |
| 1097 | String[] packageActivityPair = entry.split(PACKAGE_ACTIVITY_DELIMITER); |
| 1098 | Set<String> activities = packageToActivityMap.get(packageActivityPair[0]); |
| 1099 | boolean newPackage = false; |
| 1100 | if (activities == null) { |
| 1101 | activities = new ArraySet<>(); |
| 1102 | newPackage = true; |
| 1103 | packageToActivityMap.put(packageActivityPair[0], activities); |
| 1104 | } |
| 1105 | if (packageActivityPair.length == 1) { // whole package |
| 1106 | activities.clear(); |
| 1107 | } else if (packageActivityPair.length == 2) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1108 | // add class name only when the whole package is not allowlisted. |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1109 | if (newPackage || (activities.size() > 0)) { |
| 1110 | activities.add(packageActivityPair[1]); |
| 1111 | } |
| 1112 | } |
| 1113 | } |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1116 | @Nullable |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 1117 | private List<String> getActivitiesInPackage(PackageInfo info) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1118 | if (info == null || info.activities == null) { |
| 1119 | return null; |
| 1120 | } |
| 1121 | List<String> activityList = new ArrayList<>(); |
| 1122 | for (ActivityInfo aInfo : info.activities) { |
| 1123 | activityList.add(aInfo.name); |
| 1124 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 1125 | return activityList; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 1128 | /** |
| 1129 | * Checks if there are any {@link CarAppBlockingPolicyService} and creates a proxy to |
| 1130 | * bind to them and retrieve the {@link CarAppBlockingPolicy} |
| 1131 | */ |
| 1132 | @VisibleForTesting |
| 1133 | public void startAppBlockingPolicies() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1134 | Intent policyIntent = new Intent(); |
| 1135 | policyIntent.setAction(CarAppBlockingPolicyService.SERVICE_INTERFACE); |
| 1136 | List<ResolveInfo> policyInfos = mPackageManager.queryIntentServices(policyIntent, 0); |
| 1137 | if (policyInfos == null) { //no need to wait for service binding and retrieval. |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | LinkedList<AppBlockingPolicyProxy> proxies = new LinkedList<>(); |
| 1141 | for (ResolveInfo resolveInfo : policyInfos) { |
| 1142 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 1143 | if (serviceInfo == null) { |
| 1144 | continue; |
| 1145 | } |
| 1146 | if (serviceInfo.isEnabled()) { |
| 1147 | if (mPackageManager.checkPermission(Car.PERMISSION_CONTROL_APP_BLOCKING, |
| 1148 | serviceInfo.packageName) != PackageManager.PERMISSION_GRANTED) { |
| 1149 | continue; |
| 1150 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1151 | Slog.i(TAG, "found policy holding service:" + serviceInfo); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1152 | AppBlockingPolicyProxy proxy = new AppBlockingPolicyProxy(this, mContext, |
| 1153 | serviceInfo); |
| 1154 | proxy.connect(); |
| 1155 | proxies.add(proxy); |
| 1156 | } |
| 1157 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1158 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1159 | mProxies = proxies; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | public void onPolicyConnectionAndSet(AppBlockingPolicyProxy proxy, |
| 1164 | CarAppBlockingPolicy policy) { |
| 1165 | doHandlePolicyConnection(proxy, policy); |
| 1166 | } |
| 1167 | |
| 1168 | public void onPolicyConnectionFailure(AppBlockingPolicyProxy proxy) { |
| 1169 | doHandlePolicyConnection(proxy, null); |
| 1170 | } |
| 1171 | |
| 1172 | private void doHandlePolicyConnection(AppBlockingPolicyProxy proxy, |
| 1173 | CarAppBlockingPolicy policy) { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1174 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1175 | if (mProxies == null) { |
| 1176 | proxy.disconnect(); |
| 1177 | return; |
| 1178 | } |
| 1179 | mProxies.remove(proxy); |
| 1180 | if (mProxies.size() == 0) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1181 | mProxies = null; |
| 1182 | } |
| 1183 | } |
| 1184 | try { |
| 1185 | if (policy != null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1186 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1187 | Slog.d(TAG, "policy setting from policy service:" + proxy.getPackageName()); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1188 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 1189 | doSetAppBlockingPolicy(proxy.getPackageName(), policy, 0); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1190 | } |
| 1191 | } finally { |
| 1192 | proxy.disconnect(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | @Override |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 1197 | public void dump(IndentingPrintWriter writer) { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1198 | synchronized (mLock) { |
Jordan Jozwiak | 0aa0598 | 2019-06-17 15:14:39 -0700 | [diff] [blame] | 1199 | writer.println("*CarPackageManagerService*"); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1200 | writer.println("mEnableActivityBlocking:" + mEnableActivityBlocking); |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1201 | writer.println("mPreventTemplatedAppsFromShowingDialog:" |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1202 | + mPreventTemplatedAppsFromShowingDialog); |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1203 | writer.println("mTemplateActivityClassName:" + mTemplateActivityClassName); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1204 | List<String> restrictions = new ArrayList<>(mUxRestrictionsListeners.size()); |
| 1205 | for (int i = 0; i < mUxRestrictionsListeners.size(); i++) { |
| 1206 | int displayId = mUxRestrictionsListeners.keyAt(i); |
| 1207 | UxRestrictionsListener listener = mUxRestrictionsListeners.valueAt(i); |
| 1208 | restrictions.add(String.format("Display %d is %s", |
| 1209 | displayId, (listener.isRestricted() ? "restricted" : "unrestricted"))); |
| 1210 | } |
| 1211 | writer.println("Display Restrictions:\n" + String.join("\n", restrictions)); |
Jordan Jozwiak | 0aa0598 | 2019-06-17 15:14:39 -0700 | [diff] [blame] | 1212 | writer.println(" Blocked activity log:"); |
Bin Tan | 2536d40 | 2021-02-10 15:28:35 -0800 | [diff] [blame] | 1213 | mBlockedActivityLogs.dump(writer); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1214 | writer.print(dumpPoliciesLocked(true)); |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1215 | writer.print("mCurrentDrivingSafetyRegion:"); |
| 1216 | writer.println(mCurrentDrivingSafetyRegion); |
| 1217 | writer.print("mTempAllowedActivities:"); |
| 1218 | writer.println(mTempAllowedActivities); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1222 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1223 | private String dumpPoliciesLocked(boolean dumpAll) { |
| 1224 | StringBuilder sb = new StringBuilder(); |
| 1225 | if (dumpAll) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1226 | sb.append("**System allowlist**\n"); |
| 1227 | for (AppBlockingPackageInfoWrapper wrapper : mActivityAllowlistMap.values()) { |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1228 | sb.append(wrapper.toString() + "\n"); |
| 1229 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1230 | } |
| 1231 | sb.append("**Client Policies**\n"); |
| 1232 | for (Entry<String, ClientPolicy> entry : mClientPolicies.entrySet()) { |
| 1233 | sb.append("Client:" + entry.getKey() + "\n"); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1234 | sb.append(" allowlists:\n"); |
| 1235 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().mAllowlistsMap.values()) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1236 | sb.append(wrapper.toString() + "\n"); |
| 1237 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1238 | sb.append(" blocklists:\n"); |
| 1239 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().mBlocklistsMap.values()) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1240 | sb.append(wrapper.toString() + "\n"); |
| 1241 | } |
| 1242 | } |
| 1243 | sb.append("**Unprocessed policy services**\n"); |
| 1244 | if (mProxies != null) { |
| 1245 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 1246 | sb.append(proxy.toString() + "\n"); |
| 1247 | } |
| 1248 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1249 | sb.append("**Allowlist string in resource**\n"); |
| 1250 | sb.append(mConfiguredAllowlist + "\n"); |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 1251 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1252 | sb.append("**System allowlist string in resource**\n"); |
| 1253 | sb.append(mConfiguredSystemAllowlist + "\n"); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1254 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1255 | sb.append("**Blocklist string in resource**\n"); |
| 1256 | sb.append(mConfiguredBlocklist + "\n"); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 1257 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1258 | sb.append("**Allowlist map from resource**\n"); |
| 1259 | sb.append(mConfiguredAllowlistMap + "\n"); |
| 1260 | |
| 1261 | sb.append("**Blocklist from resource**\n"); |
| 1262 | sb.append(mConfiguredBlocklist + "\n"); |
| 1263 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1264 | return sb.toString(); |
| 1265 | } |
| 1266 | |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1267 | /** |
| 1268 | * Returns display with physical address. |
| 1269 | */ |
| 1270 | private List<Display> getPhysicalDisplays() { |
| 1271 | List<Display> displays = new ArrayList<>(); |
| 1272 | for (Display display : mDisplayManager.getDisplays()) { |
| 1273 | if (display.getAddress() instanceof DisplayAddress.Physical) { |
| 1274 | displays.add(display); |
| 1275 | } |
| 1276 | } |
| 1277 | return displays; |
| 1278 | } |
| 1279 | |
| 1280 | /** |
| 1281 | * Returns whether UX restrictions is required for display. |
| 1282 | * |
| 1283 | * Non-physical display will use restrictions for {@link Display#DEFAULT_DISPLAY}. |
| 1284 | */ |
| 1285 | private boolean isUxRestrictedOnDisplay(int displayId) { |
| 1286 | UxRestrictionsListener listenerForTopTaskDisplay; |
| 1287 | if (mUxRestrictionsListeners.indexOfKey(displayId) < 0) { |
| 1288 | listenerForTopTaskDisplay = mUxRestrictionsListeners.get(Display.DEFAULT_DISPLAY); |
| 1289 | if (listenerForTopTaskDisplay == null) { |
| 1290 | // This should never happen. |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1291 | Slog.e(TAG, "Missing listener for default display."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1292 | return true; |
| 1293 | } |
| 1294 | } else { |
| 1295 | listenerForTopTaskDisplay = mUxRestrictionsListeners.get(displayId); |
| 1296 | } |
| 1297 | |
| 1298 | return listenerForTopTaskDisplay.isRestricted(); |
| 1299 | } |
| 1300 | |
| 1301 | private void blockTopActivitiesIfNecessary() { |
| 1302 | List<TopTaskInfoContainer> topTasks = mSystemActivityMonitoringService.getTopTasks(); |
| 1303 | for (TopTaskInfoContainer topTask : topTasks) { |
| 1304 | if (topTask == null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1305 | Slog.e(TAG, "Top tasks contains null."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1306 | continue; |
| 1307 | } |
| 1308 | blockTopActivityIfNecessary(topTask); |
| 1309 | } |
| 1310 | } |
| 1311 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1312 | private void blockTopActivityIfNecessary(TopTaskInfoContainer topTask) { |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1313 | synchronized (mLock) { |
Gaurav Bhola | cb9439b | 2021-10-06 00:31:20 +0000 | [diff] [blame^] | 1314 | if (!Objects.equals(mActivityBlockingActivity, topTask.topActivity) |
Gaurav Bhola | 3a91fef | 2021-10-02 00:32:33 +0000 | [diff] [blame] | 1315 | && mTopActivityWithDialogPerDisplay.contains(topTask.displayId) |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1316 | && !topTask.topActivity.equals( |
| 1317 | mTopActivityWithDialogPerDisplay.get(topTask.displayId))) { |
| 1318 | // Clear top activity-with-dialog if the activity has changed on this display. |
| 1319 | mTopActivityWithDialogPerDisplay.remove(topTask.displayId); |
| 1320 | } |
| 1321 | } |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1322 | if (isUxRestrictedOnDisplay(topTask.displayId)) { |
| 1323 | doBlockTopActivityIfNotAllowed(topTask); |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 1324 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | private void doBlockTopActivityIfNotAllowed(TopTaskInfoContainer topTask) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1328 | if (topTask.topActivity == null) { |
| 1329 | return; |
| 1330 | } |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1331 | boolean allowed = isActivityAllowed(topTask); |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1332 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1333 | Slog.d(TAG, "new activity:" + topTask.toString() + " allowed:" + allowed); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1334 | } |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1335 | if (allowed) { |
| 1336 | return; |
| 1337 | } |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1338 | if (!mEnableActivityBlocking) { |
| 1339 | Slog.d(TAG, "Current activity " + topTask.topActivity |
| 1340 | + " not allowed, blocking disabled. Number of tasks in stack:" |
| 1341 | + topTask.taskInfo.childTaskIds.length); |
| 1342 | return; |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1343 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1344 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1345 | Slog.d(TAG, "Current activity " + topTask.topActivity |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame] | 1346 | + " not allowed, will block, number of tasks in stack:" |
| 1347 | + topTask.taskInfo.childTaskIds.length); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1348 | } |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1349 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1350 | // Figure out the root activity of blocked task. |
| 1351 | String taskRootActivity = null; |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1352 | for (int i = 0; i < topTask.taskInfo.childTaskIds.length; i++) { |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1353 | // topTask.taskId is the task that should be blocked. |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1354 | if (topTask.taskInfo.childTaskIds[i] == topTask.taskId) { |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1355 | // stackInfo represents an ActivityStack. Its fields taskIds and taskNames |
| 1356 | // are 1:1 mapped, where taskNames is the name of root activity in this task. |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1357 | taskRootActivity = topTask.taskInfo.childTaskNames[i]; |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1358 | break; |
| 1359 | } |
| 1360 | } |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1361 | |
| 1362 | boolean isRootDO = false; |
| 1363 | if (taskRootActivity != null) { |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1364 | ComponentName taskRootComponentName = |
| 1365 | ComponentName.unflattenFromString(taskRootActivity); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1366 | isRootDO = isActivityDistractionOptimized( |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1367 | taskRootComponentName.getPackageName(), taskRootComponentName.getClassName()); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | Intent newActivityIntent = createBlockingActivityIntent( |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1371 | mActivityBlockingActivity, topTask.displayId, |
| 1372 | topTask.topActivity.flattenToShortString(), topTask.taskId, taskRootActivity, |
| 1373 | isRootDO); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1374 | |
| 1375 | // Intent contains all info to debug what is blocked - log into both logcat and dumpsys. |
| 1376 | String log = "Starting blocking activity with intent: " + newActivityIntent.toUri(0); |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1377 | if (Log.isLoggable(TAG, Log.INFO)) { |
| 1378 | Slog.i(TAG, log); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1379 | } |
Bin Tan | 2536d40 | 2021-02-10 15:28:35 -0800 | [diff] [blame] | 1380 | mBlockedActivityLogs.log(log); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1381 | mSystemActivityMonitoringService.blockActivity(topTask, newActivityIntent); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1384 | private boolean isActivityAllowed(TopTaskInfoContainer topTaskInfoContainer) { |
| 1385 | ComponentName activityName = topTaskInfoContainer.topActivity; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1386 | boolean isDistractionOptimized = isActivityDistractionOptimized( |
| 1387 | activityName.getPackageName(), |
| 1388 | activityName.getClassName()); |
| 1389 | if (!isDistractionOptimized) { |
| 1390 | return false; |
| 1391 | } |
| 1392 | return !(mPreventTemplatedAppsFromShowingDialog |
| 1393 | && isTemplateActivity(activityName) |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1394 | && isActivityShowingADialogOnDisplay(activityName, topTaskInfoContainer.displayId)); |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | private boolean isTemplateActivity(ComponentName activityName) { |
| 1398 | // TODO(b/191263486): Finalise on how to detect the templated activities. |
| 1399 | return activityName.getClassName().equals(mTemplateActivityClassName); |
| 1400 | } |
| 1401 | |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1402 | private boolean isActivityShowingADialogOnDisplay(ComponentName activityName, int displayId) { |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1403 | String output = dumpWindows(); |
| 1404 | List<WindowDumpParser.Window> appWindows = |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1405 | WindowDumpParser.getParsedAppWindows(output, activityName.getPackageName()); |
Gaurav Bhola | 38569c7 | 2021-06-30 15:36:20 +0000 | [diff] [blame] | 1406 | // TODO(b/192354699): Handle case where an activity can have multiple instances on the same |
| 1407 | // display. |
| 1408 | int totalAppWindows = appWindows.size(); |
| 1409 | String firstActivityRecord = null; |
| 1410 | int numTopActivityAppWindowsOnDisplay = 0; |
| 1411 | for (int i = 0; i < totalAppWindows; i++) { |
| 1412 | WindowDumpParser.Window appWindow = appWindows.get(i); |
| 1413 | if (appWindow.getDisplayId() != displayId) { |
| 1414 | continue; |
| 1415 | } |
| 1416 | if (TextUtils.isEmpty(appWindow.getActivityRecord())) { |
| 1417 | continue; |
| 1418 | } |
| 1419 | if (firstActivityRecord == null) { |
| 1420 | firstActivityRecord = appWindow.getActivityRecord(); |
| 1421 | } |
| 1422 | if (firstActivityRecord.equals(appWindow.getActivityRecord())) { |
| 1423 | numTopActivityAppWindowsOnDisplay++; |
| 1424 | } |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1425 | } |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1426 | Slogf.d(TAG, "Top activity = " + activityName); |
Gaurav Bhola | 38569c7 | 2021-06-30 15:36:20 +0000 | [diff] [blame] | 1427 | Slogf.d(TAG, "Number of app widows of top activity = " + numTopActivityAppWindowsOnDisplay); |
| 1428 | boolean isShowingADialog = numTopActivityAppWindowsOnDisplay > 1; |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1429 | synchronized (mLock) { |
| 1430 | if (isShowingADialog) { |
| 1431 | mTopActivityWithDialogPerDisplay.put(displayId, activityName); |
| 1432 | } else { |
| 1433 | mTopActivityWithDialogPerDisplay.remove(displayId); |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1436 | return isShowingADialog; |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | private String dumpWindows() { |
| 1440 | try { |
| 1441 | ParcelFileDescriptor[] fileDescriptors = ParcelFileDescriptor.createSocketPair(); |
| 1442 | mWindowManagerBinder.dump( |
| 1443 | fileDescriptors[0].getFileDescriptor(), WINDOW_DUMP_ARGUMENTS); |
| 1444 | fileDescriptors[0].close(); |
| 1445 | StringBuilder outputBuilder = new StringBuilder(); |
| 1446 | BufferedReader reader = new BufferedReader( |
| 1447 | new FileReader(fileDescriptors[1].getFileDescriptor())); |
| 1448 | String line; |
| 1449 | while ((line = reader.readLine()) != null) { |
| 1450 | outputBuilder.append(line).append("\n"); |
| 1451 | } |
| 1452 | reader.close(); |
| 1453 | fileDescriptors[1].close(); |
| 1454 | return outputBuilder.toString(); |
| 1455 | } catch (IOException | RemoteException e) { |
| 1456 | throw new RuntimeException(e); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1460 | /** |
| 1461 | * Creates an intent to start blocking activity. |
| 1462 | * |
| 1463 | * @param blockingActivity the activity to launch |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 1464 | * @param blockedActivity the activity being blocked |
| 1465 | * @param blockedTaskId the blocked task id, which contains the blocked activity |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1466 | * @param taskRootActivity root activity of the blocked task |
Gaurav Bhola | 8317304 | 2021-06-21 09:36:20 +0000 | [diff] [blame] | 1467 | * @param isRootDo denotes if the root activity is distraction optimised |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1468 | * @return an intent to launch the blocking activity. |
| 1469 | */ |
| 1470 | private static Intent createBlockingActivityIntent(ComponentName blockingActivity, |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1471 | int displayId, String blockedActivity, int blockedTaskId, String taskRootActivity, |
| 1472 | boolean isRootDo) { |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1473 | Intent newActivityIntent = new Intent(); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1474 | newActivityIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1475 | newActivityIntent.setComponent(blockingActivity); |
| 1476 | newActivityIntent.putExtra( |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1477 | BLOCKING_INTENT_EXTRA_DISPLAY_ID, displayId); |
| 1478 | newActivityIntent.putExtra( |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1479 | BLOCKING_INTENT_EXTRA_BLOCKED_ACTIVITY_NAME, blockedActivity); |
| 1480 | newActivityIntent.putExtra( |
| 1481 | BLOCKING_INTENT_EXTRA_BLOCKED_TASK_ID, blockedTaskId); |
| 1482 | newActivityIntent.putExtra( |
| 1483 | BLOCKING_INTENT_EXTRA_ROOT_ACTIVITY_NAME, taskRootActivity); |
| 1484 | newActivityIntent.putExtra( |
| 1485 | BLOCKING_INTENT_EXTRA_IS_ROOT_ACTIVITY_DO, isRootDo); |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 1486 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1487 | return newActivityIntent; |
| 1488 | } |
| 1489 | |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1490 | /** |
| 1491 | * Enable/Disable activity blocking by correspondingly enabling/disabling broadcasting UXR |
| 1492 | * changes in {@link CarUxRestrictionsManagerService}. This is only available in |
| 1493 | * engineering builds for development convenience. |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1494 | */ |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1495 | @Override |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1496 | public void setEnableActivityBlocking(boolean enable) { |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1497 | if (!isDebugBuild()) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1498 | Slog.e(TAG, "Cannot enable/disable activity blocking"); |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1499 | return; |
| 1500 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1501 | |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1502 | // Check if the caller has the same signature as that of the car service. |
| 1503 | if (mPackageManager.checkSignatures(Process.myUid(), Binder.getCallingUid()) |
| 1504 | != PackageManager.SIGNATURE_MATCH) { |
| 1505 | throw new SecurityException( |
| 1506 | "Caller " + mPackageManager.getNameForUid(Binder.getCallingUid()) |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 1507 | + " does not have the right signature"); |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1508 | } |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1509 | mCarUxRestrictionsService.setUxRChangeBroadcastEnabled(enable); |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1512 | /** |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1513 | * Get the distraction optimized activities for the given package. |
| 1514 | * |
| 1515 | * @param pkgName Name of the package |
| 1516 | * @return Array of the distraction optimized activities in the package |
| 1517 | */ |
| 1518 | @Nullable |
| 1519 | public String[] getDistractionOptimizedActivities(String pkgName) { |
| 1520 | try { |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1521 | return findDistractionOptimizedActivitiesAsUser(pkgName, |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1522 | mActivityManager.getCurrentUser()); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1523 | } catch (NameNotFoundException e) { |
| 1524 | return null; |
| 1525 | } |
| 1526 | } |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1527 | |
Keun young Park | 40fcb0c | 2021-09-14 18:47:28 -0700 | [diff] [blame] | 1528 | private String[] findDistractionOptimizedActivitiesAsUser(String pkgName, int userId) |
| 1529 | throws NameNotFoundException { |
| 1530 | String regionString; |
| 1531 | synchronized (mLock) { |
| 1532 | regionString = mCurrentDrivingSafetyRegion; |
| 1533 | } |
| 1534 | return CarAppMetadataReader.findDistractionOptimizedActivitiesAsUser(mContext, pkgName, |
| 1535 | userId, regionString); |
| 1536 | } |
| 1537 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1538 | /** |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1539 | * Reading policy and setting policy can take time. Run it in a separate handler thread. |
| 1540 | */ |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1541 | private static final class PackageHandler extends Handler { |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 1542 | private static final String TAG = CarLog.tagFor(CarPackageManagerService.class); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1543 | |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 1544 | private static final int MSG_INIT = 0; |
| 1545 | private static final int MSG_PARSE_PKG = 1; |
| 1546 | private static final int MSG_UPDATE_POLICY = 2; |
| 1547 | private static final int MSG_RELEASE = 3; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1548 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1549 | private final WeakReference<CarPackageManagerService> mService; |
| 1550 | |
| 1551 | private PackageHandler(Looper looper, CarPackageManagerService service) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1552 | super(looper); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1553 | mService = new WeakReference<CarPackageManagerService>(service); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | private void requestInit() { |
| 1557 | Message msg = obtainMessage(MSG_INIT); |
| 1558 | sendMessage(msg); |
| 1559 | } |
| 1560 | |
| 1561 | private void requestRelease() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1562 | removeMessages(MSG_UPDATE_POLICY); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1563 | Message msg = obtainMessage(MSG_RELEASE); |
| 1564 | sendMessage(msg); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1567 | private void requestUpdatingPolicy(String packageName, CarAppBlockingPolicy policy, |
| 1568 | int flags) { |
| 1569 | Pair<String, CarAppBlockingPolicy> pair = new Pair<>(packageName, policy); |
| 1570 | Message msg = obtainMessage(MSG_UPDATE_POLICY, flags, 0, pair); |
| 1571 | sendMessage(msg); |
| 1572 | } |
| 1573 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1574 | private void requestParsingInstalledPkg(String packageName) { |
| 1575 | Message msg = obtainMessage(MSG_PARSE_PKG, packageName); |
| 1576 | sendMessage(msg); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1579 | @Override |
| 1580 | public void handleMessage(Message msg) { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1581 | CarPackageManagerService service = mService.get(); |
| 1582 | if (service == null) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame] | 1583 | Slog.i(TAG, "handleMessage null service"); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1584 | return; |
| 1585 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1586 | switch (msg.what) { |
| 1587 | case MSG_INIT: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1588 | service.doHandleInit(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1589 | break; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1590 | case MSG_PARSE_PKG: |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1591 | service.doParseInstalledPackage((String) msg.obj); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1592 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1593 | case MSG_UPDATE_POLICY: |
| 1594 | Pair<String, CarAppBlockingPolicy> pair = |
| 1595 | (Pair<String, CarAppBlockingPolicy>) msg.obj; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1596 | service.doUpdatePolicy(pair.first, pair.second, msg.arg1); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1597 | break; |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1598 | case MSG_RELEASE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1599 | service.doHandleRelease(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1600 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | private static class AppBlockingPackageInfoWrapper { |
| 1606 | private final AppBlockingPackageInfo info; |
| 1607 | /** |
| 1608 | * Whether the current info is matching with the target package in system. Mismatch can |
| 1609 | * happen for version out of range or signature mismatch. |
| 1610 | */ |
| 1611 | private boolean isMatching; |
| 1612 | |
| 1613 | private AppBlockingPackageInfoWrapper(AppBlockingPackageInfo info, boolean isMatching) { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1614 | this.info = info; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1615 | this.isMatching = isMatching; |
| 1616 | } |
| 1617 | |
| 1618 | @Override |
| 1619 | public String toString() { |
| 1620 | return "AppBlockingPackageInfoWrapper [info=" + info + ", isMatching=" + isMatching + |
| 1621 | "]"; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * Client policy holder per each client. Should be accessed with CarpackageManagerService.this |
| 1627 | * held. |
| 1628 | */ |
| 1629 | private static class ClientPolicy { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1630 | private final HashMap<String, AppBlockingPackageInfoWrapper> mAllowlistsMap = |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1631 | new HashMap<>(); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1632 | private final HashMap<String, AppBlockingPackageInfoWrapper> mBlocklistsMap = |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1633 | new HashMap<>(); |
| 1634 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1635 | private void replaceAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1636 | mAllowlistsMap.clear(); |
| 1637 | addToAllowlists(allowlists); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1640 | private void addToAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1641 | if (allowlists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1642 | return; |
| 1643 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1644 | for (AppBlockingPackageInfoWrapper wrapper : allowlists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1645 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1646 | mAllowlistsMap.put(wrapper.info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1651 | private void removeAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1652 | if (allowlists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1653 | return; |
| 1654 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1655 | for (AppBlockingPackageInfoWrapper wrapper : allowlists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1656 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1657 | mAllowlistsMap.remove(wrapper.info.packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1662 | private void replaceBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1663 | mBlocklistsMap.clear(); |
| 1664 | addToBlocklists(blocklists); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1667 | private void addToBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1668 | if (blocklists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1669 | return; |
| 1670 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1671 | for (AppBlockingPackageInfoWrapper wrapper : blocklists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1672 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1673 | mBlocklistsMap.put(wrapper.info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1678 | private void removeBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1679 | if (blocklists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1680 | return; |
| 1681 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1682 | for (AppBlockingPackageInfoWrapper wrapper : blocklists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1683 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1684 | mBlocklistsMap.remove(wrapper.info.packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1689 | |
| 1690 | private class ActivityLaunchListener |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1691 | implements SystemActivityMonitoringService.ActivityLaunchListener { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1692 | @Override |
| 1693 | public void onActivityLaunch(TopTaskInfoContainer topTask) { |
Yao, Yuxing | 2646cff | 2019-04-23 17:01:26 -0700 | [diff] [blame] | 1694 | if (topTask == null) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1695 | Slog.e(TAG, "Received callback with null top task."); |
Yao, Yuxing | 2646cff | 2019-04-23 17:01:26 -0700 | [diff] [blame] | 1696 | return; |
| 1697 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1698 | blockTopActivityIfNecessary(topTask); |
| 1699 | } |
| 1700 | } |
| 1701 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1702 | /** |
| 1703 | * Listens to the UX restrictions from {@link CarUxRestrictionsManagerService} and initiates |
| 1704 | * checking if the foreground Activity should be blocked. |
| 1705 | */ |
| 1706 | private class UxRestrictionsListener extends ICarUxRestrictionsChangeListener.Stub { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1707 | @GuardedBy("mLock") |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1708 | @Nullable |
| 1709 | private CarUxRestrictions mCurrentUxRestrictions; |
| 1710 | private final CarUxRestrictionsManagerService uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1711 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1712 | public UxRestrictionsListener(CarUxRestrictionsManagerService service) { |
| 1713 | uxRestrictionsService = service; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | @Override |
| 1717 | public void onUxRestrictionsChanged(CarUxRestrictions restrictions) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1718 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1719 | Slog.d(TAG, "Received uxr restrictions: " |
| 1720 | + restrictions.isRequiresDistractionOptimization() + " : " |
| 1721 | + restrictions.getActiveRestrictions()); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1722 | } |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 1723 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1724 | synchronized (mLock) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1725 | mCurrentUxRestrictions = new CarUxRestrictions(restrictions); |
| 1726 | } |
| 1727 | checkIfTopActivityNeedsBlocking(); |
| 1728 | } |
| 1729 | |
| 1730 | private void checkIfTopActivityNeedsBlocking() { |
| 1731 | boolean shouldCheck = false; |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1732 | synchronized (mLock) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1733 | if (mCurrentUxRestrictions != null |
| 1734 | && mCurrentUxRestrictions.isRequiresDistractionOptimization()) { |
| 1735 | shouldCheck = true; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1738 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1739 | Slog.d(TAG, "Should check top tasks?: " + shouldCheck); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1740 | } |
| 1741 | if (shouldCheck) { |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1742 | // Loop over all top tasks to ensure tasks on virtual display can also be blocked. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1743 | blockTopActivitiesIfNecessary(); |
| 1744 | } |
| 1745 | } |
| 1746 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1747 | private boolean isRestricted() { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1748 | // if current restrictions is null, try querying the service, once. |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1749 | synchronized (mLock) { |
| 1750 | if (mCurrentUxRestrictions == null) { |
| 1751 | mCurrentUxRestrictions = uxRestrictionsService.getCurrentUxRestrictions(); |
| 1752 | } |
| 1753 | if (mCurrentUxRestrictions != null) { |
| 1754 | return mCurrentUxRestrictions.isRequiresDistractionOptimization(); |
| 1755 | } |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1756 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1757 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1758 | // If restriction information is still not available (could happen during bootup), |
| 1759 | // return not restricted. This maintains parity with previous implementation but needs |
| 1760 | // a revisit as we test more. |
| 1761 | return false; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1764 | |
| 1765 | /** |
Gaurav Bhola | afd79ef | 2021-06-16 13:22:03 +0000 | [diff] [blame] | 1766 | * Called when a window change event is received by the {@link CarSafetyAccessibilityService}. |
| 1767 | */ |
| 1768 | @VisibleForTesting |
| 1769 | void onWindowChangeEvent() { |
| 1770 | Slogf.d(TAG, "onWindowChange event received"); |
| 1771 | mHandlerThread.getThreadHandler().post(() -> blockTopActivitiesIfNecessary()); |
| 1772 | } |
| 1773 | |
| 1774 | /** |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1775 | * Listens to the package install/uninstall events to know when to initiate parsing |
| 1776 | * installed packages. |
| 1777 | */ |
| 1778 | private class PackageParsingEventReceiver extends BroadcastReceiver { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1779 | @Override |
| 1780 | public void onReceive(Context context, Intent intent) { |
| 1781 | if (intent == null || intent.getAction() == null) { |
| 1782 | return; |
| 1783 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1784 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1785 | Slog.d(TAG, "PackageParsingEventReceiver Received " + intent.getAction()); |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1786 | } |
| 1787 | String action = intent.getAction(); |
| 1788 | if (isPackageManagerAction(action)) { |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1789 | // send a delayed message so if we received multiple related intents, we parse |
| 1790 | // only once. |
| 1791 | logEventChange(intent); |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1792 | String packageName = getPackageName(intent); |
| 1793 | mHandler.requestParsingInstalledPkg(packageName); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
Mayank Garg | fde5e66 | 2021-01-19 22:21:10 -0800 | [diff] [blame] | 1797 | private String getPackageName(Intent intent) { |
| 1798 | // For mPackageManagerActions, data should contain package name. |
| 1799 | String dataString = intent.getDataString(); |
| 1800 | if (dataString == null) return null; |
| 1801 | |
| 1802 | String scheme = intent.getScheme(); |
| 1803 | if (!scheme.equals("package")) return null; |
| 1804 | |
| 1805 | String[] splitData = intent.getDataString().split(":"); |
| 1806 | if (splitData.length < 2) return null; |
| 1807 | |
| 1808 | return splitData[1]; |
| 1809 | } |
| 1810 | |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1811 | private boolean isPackageManagerAction(String action) { |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 1812 | return mPackageManagerActions.contains(action); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * Convenience log function to log what changed. Logs only when more debug logs |
| 1817 | * are needed - DBG_POLICY_CHECK needs to be true |
| 1818 | */ |
| 1819 | private void logEventChange(Intent intent) { |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1820 | if (intent == null) { |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1823 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1824 | String packageName = intent.getData().getSchemeSpecificPart(); |
| 1825 | Slog.d(TAG, "Pkg Changed:" + packageName); |
| 1826 | String action = intent.getAction(); |
| 1827 | if (action == null) { |
| 1828 | return; |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1829 | } |
Mayank Garg | bfa6613 | 2021-05-05 19:56:20 -0700 | [diff] [blame] | 1830 | if (action.equals(Intent.ACTION_PACKAGE_CHANGED)) { |
| 1831 | Slog.d(TAG, "Changed components"); |
| 1832 | String[] cc = intent |
| 1833 | .getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST); |
| 1834 | if (cc != null) { |
| 1835 | for (String c : cc) { |
| 1836 | Slog.d(TAG, c); |
| 1837 | } |
| 1838 | } |
| 1839 | } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| 1840 | || action.equals(Intent.ACTION_PACKAGE_ADDED)) { |
| 1841 | Slog.d(TAG, action + " Replacing?: " |
| 1842 | + intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
| 1843 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1847 | } |