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 | |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 22 | import android.app.ActivityTaskManager.RootTaskInfo; |
Arnaud Berry | d1f88ee | 2020-02-19 13:49:31 -0800 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 24 | import android.car.Car; |
| 25 | import android.car.content.pm.AppBlockingPackageInfo; |
| 26 | import android.car.content.pm.CarAppBlockingPolicy; |
| 27 | import android.car.content.pm.CarAppBlockingPolicyService; |
| 28 | import android.car.content.pm.CarPackageManager; |
| 29 | import android.car.content.pm.ICarPackageManager; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 30 | import android.car.drivingstate.CarUxRestrictions; |
| 31 | import android.car.drivingstate.ICarUxRestrictionsChangeListener; |
Antonio Kantek | f700753 | 2020-03-17 10:37:58 -0700 | [diff] [blame] | 32 | import android.car.user.CarUserManager; |
| 33 | import android.car.user.CarUserManager.UserLifecycleListener; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 34 | import android.content.BroadcastReceiver; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 35 | import android.content.ComponentName; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 38 | import android.content.IntentFilter; |
| 39 | import android.content.pm.ActivityInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageInfo; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.PackageManager.NameNotFoundException; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 43 | import android.content.pm.ResolveInfo; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 44 | import android.content.pm.ServiceInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 45 | import android.content.pm.Signature; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 46 | import android.content.res.Resources; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 47 | import android.hardware.display.DisplayManager; |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 48 | import android.os.Binder; |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 49 | import android.os.Build; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 50 | import android.os.Handler; |
| 51 | import android.os.HandlerThread; |
| 52 | import android.os.Looper; |
| 53 | import android.os.Message; |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 54 | import android.os.Process; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 55 | import android.os.UserHandle; |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 56 | import android.text.format.DateFormat; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 57 | import android.util.ArraySet; |
| 58 | import android.util.Log; |
| 59 | import android.util.Pair; |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 60 | import android.util.Slog; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 61 | import android.util.SparseArray; |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 62 | import android.view.Display; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 63 | import android.view.DisplayAddress; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 64 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 65 | import com.android.car.CarLog; |
| 66 | import com.android.car.CarServiceBase; |
| 67 | import com.android.car.CarServiceUtils; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 68 | import com.android.car.CarUxRestrictionsManagerService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 69 | import com.android.car.R; |
| 70 | import com.android.car.SystemActivityMonitoringService; |
| 71 | import com.android.car.SystemActivityMonitoringService.TopTaskInfoContainer; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 72 | import com.android.car.user.CarUserService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 73 | import com.android.internal.annotations.GuardedBy; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 74 | import com.android.internal.annotations.VisibleForTesting; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 75 | |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 76 | import com.google.android.collect.Sets; |
| 77 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 78 | import java.io.PrintWriter; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 79 | import java.lang.ref.WeakReference; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 80 | import java.util.ArrayList; |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 81 | import java.util.Arrays; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 82 | import java.util.HashMap; |
| 83 | import java.util.LinkedList; |
| 84 | import java.util.List; |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 85 | import java.util.Map; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 86 | import java.util.Map.Entry; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 87 | import java.util.Set; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 88 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 89 | public class CarPackageManagerService extends ICarPackageManager.Stub implements CarServiceBase { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 90 | private static final boolean DBG_POLICY_SET = false; |
| 91 | private static final boolean DBG_POLICY_CHECK = false; |
| 92 | private static final boolean DBG_POLICY_ENFORCEMENT = false; |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 93 | // Delimiters to parse packages and activities in the configuration XML resource. |
| 94 | private static final String PACKAGE_DELIMITER = ","; |
| 95 | private static final String PACKAGE_ACTIVITY_DELIMITER = "/"; |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 96 | private static final int LOG_SIZE = 20; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 97 | |
| 98 | private final Context mContext; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 99 | private final CarUserService mUserService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 100 | private final SystemActivityMonitoringService mSystemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 101 | private final PackageManager mPackageManager; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 102 | private final ActivityManager mActivityManager; |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 103 | private final DisplayManager mDisplayManager; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 104 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 105 | private final HandlerThread mHandlerThread = CarServiceUtils.getHandlerThread( |
| 106 | getClass().getSimpleName()); |
| 107 | private final PackageHandler mHandler = new PackageHandler(mHandlerThread.getLooper(), this); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 108 | private final Object mLock = new Object(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 109 | |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 110 | // For dumpsys logging. |
| 111 | private final LinkedList<String> mBlockedActivityLogs = new LinkedList<>(); |
| 112 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 113 | // Store the allowlist and blocklist strings from the resource file. |
| 114 | private String mConfiguredAllowlist; |
| 115 | private String mConfiguredSystemAllowlist; |
| 116 | private String mConfiguredBlocklist; |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 117 | private final List<String> mAllowedAppInstallSources; |
| 118 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Hold policy set from policy service or client. |
| 121 | * Key: packageName of policy service |
| 122 | */ |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 123 | @GuardedBy("mLock") |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 124 | private final HashMap<String, ClientPolicy> mClientPolicies = new HashMap<>(); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 125 | @GuardedBy("mLock") |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 126 | private HashMap<String, AppBlockingPackageInfoWrapper> mActivityAllowlistMap = new HashMap<>(); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 127 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 128 | private LinkedList<AppBlockingPolicyProxy> mProxies; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 129 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 130 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 131 | private final LinkedList<CarAppBlockingPolicy> mWaitingPolicies = new LinkedList<>(); |
| 132 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 133 | private final CarUxRestrictionsManagerService mCarUxRestrictionsService; |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 134 | private boolean mEnableActivityBlocking; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 135 | private final ComponentName mActivityBlockingActivity; |
| 136 | |
| 137 | private final ActivityLaunchListener mActivityLaunchListener = new ActivityLaunchListener(); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 138 | // K: (logical) display id of a physical display, V: UXR change listener of this display. |
| 139 | // For multi-display, monitor UXR change on each display. |
| 140 | private final SparseArray<UxRestrictionsListener> mUxRestrictionsListeners = |
| 141 | new SparseArray<>(); |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 142 | private final VendorServiceController mVendorServiceController; |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 143 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 144 | // Information related to when the installed packages should be parsed for building a allow and |
| 145 | // block list |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 146 | private final Set<String> mPackageManagerActions = Sets.newArraySet( |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 147 | Intent.ACTION_PACKAGE_ADDED, |
| 148 | Intent.ACTION_PACKAGE_CHANGED, |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 149 | Intent.ACTION_PACKAGE_REMOVED, |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 150 | Intent.ACTION_PACKAGE_REPLACED); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 151 | |
| 152 | private final PackageParsingEventReceiver mPackageParsingEventReceiver = |
| 153 | new PackageParsingEventReceiver(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 154 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 155 | // To track if the packages have been parsed for building allow/blocklists. If we haven't had |
| 156 | // received any intents (boot complete or package changed), then the allowlist is null leading |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 157 | // to blocking everything. So, no blocking until we have had a chance to parse the packages. |
| 158 | private boolean mHasParsedPackages; |
| 159 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Name of blocked activity. |
| 162 | * |
| 163 | * @hide |
| 164 | */ |
| 165 | public static final String BLOCKING_INTENT_EXTRA_BLOCKED_ACTIVITY_NAME = "blocked_activity"; |
| 166 | /** |
| 167 | * int task id of the blocked task. |
| 168 | * |
| 169 | * @hide |
| 170 | */ |
| 171 | public static final String BLOCKING_INTENT_EXTRA_BLOCKED_TASK_ID = "blocked_task_id"; |
| 172 | /** |
| 173 | * Name of root activity of blocked task. |
| 174 | * |
| 175 | * @hide |
| 176 | */ |
| 177 | public static final String BLOCKING_INTENT_EXTRA_ROOT_ACTIVITY_NAME = "root_activity_name"; |
| 178 | /** |
| 179 | * Boolean indicating whether the root activity is distraction-optimized (DO). |
| 180 | * Blocking screen should show a button to restart the task if {@code true}. |
| 181 | * |
| 182 | * @hide |
| 183 | */ |
| 184 | public static final String BLOCKING_INTENT_EXTRA_IS_ROOT_ACTIVITY_DO = "is_root_activity_do"; |
| 185 | |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 186 | /** |
| 187 | * int display id of the blocked task. |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 188 | * |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 189 | * @hide |
| 190 | */ |
| 191 | public static final String BLOCKING_INTENT_EXTRA_DISPLAY_ID = "display_id"; |
| 192 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 193 | public CarPackageManagerService(Context context, |
| 194 | CarUxRestrictionsManagerService uxRestrictionsService, |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 195 | SystemActivityMonitoringService systemActivityMonitoringService, |
| 196 | CarUserService userService) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 197 | mContext = context; |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 198 | mUserService = userService; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 199 | mCarUxRestrictionsService = uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 200 | mSystemActivityMonitoringService = systemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 201 | mPackageManager = mContext.getPackageManager(); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 202 | mActivityManager = mContext.getSystemService(ActivityManager.class); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 203 | mDisplayManager = mContext.getSystemService(DisplayManager.class); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 204 | Resources res = context.getResources(); |
| 205 | mEnableActivityBlocking = res.getBoolean(R.bool.enableActivityBlockingForSafety); |
| 206 | String blockingActivity = res.getString(R.string.activityBlockingActivity); |
| 207 | mActivityBlockingActivity = ComponentName.unflattenFromString(blockingActivity); |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 208 | mAllowedAppInstallSources = Arrays.asList( |
| 209 | res.getStringArray(R.array.allowedAppInstallSources)); |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 210 | mVendorServiceController = new VendorServiceController( |
Anthony Hugh | fbb6776 | 2019-10-15 12:54:54 -0700 | [diff] [blame] | 211 | mContext, mHandler.getLooper()); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 214 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 215 | @Override |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 216 | public void setAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
| 217 | if (DBG_POLICY_SET) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 218 | Slog.i(CarLog.TAG_PACKAGE, "policy setting from binder call, client:" + packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 219 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 220 | doSetAppBlockingPolicy(packageName, policy, flags); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Restarts the requested task. If task with {@code taskId} does not exist, do nothing. |
| 225 | */ |
| 226 | @Override |
| 227 | public void restartTask(int taskId) { |
Keun young Park | 572023a | 2020-06-03 17:29:36 -0700 | [diff] [blame] | 228 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.REAL_GET_TASKS) |
| 229 | != PackageManager.PERMISSION_GRANTED) { |
| 230 | throw new SecurityException( |
| 231 | "requires permission " + android.Manifest.permission.REAL_GET_TASKS); |
| 232 | } |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 233 | mSystemActivityMonitoringService.restartTask(taskId); |
| 234 | } |
| 235 | |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 236 | private void doSetAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, |
| 237 | int flags) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 238 | if (mContext.checkCallingOrSelfPermission(Car.PERMISSION_CONTROL_APP_BLOCKING) |
| 239 | != PackageManager.PERMISSION_GRANTED) { |
| 240 | throw new SecurityException( |
| 241 | "requires permission " + Car.PERMISSION_CONTROL_APP_BLOCKING); |
| 242 | } |
Enrico Granata | 3da3c61 | 2017-01-20 15:24:30 -0800 | [diff] [blame] | 243 | CarServiceUtils.assertPackageName(mContext, packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 244 | if (policy == null) { |
| 245 | throw new IllegalArgumentException("policy cannot be null"); |
| 246 | } |
| 247 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0 && |
| 248 | (flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
| 249 | throw new IllegalArgumentException( |
| 250 | "Cannot set both FLAG_SET_POLICY_ADD and FLAG_SET_POLICY_REMOVE flag"); |
| 251 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 252 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 253 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 254 | mWaitingPolicies.add(policy); |
| 255 | } |
| 256 | } |
| 257 | mHandler.requestUpdatingPolicy(packageName, policy, flags); |
| 258 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
| 259 | synchronized (mLock) { |
| 260 | try { |
| 261 | while (mWaitingPolicies.contains(policy)) { |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 262 | mLock.wait(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 263 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 264 | } catch (InterruptedException e) { |
| 265 | // Pass it over binder call |
| 266 | throw new IllegalStateException( |
| 267 | "Interrupted while waiting for policy completion", e); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Override |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 274 | public boolean isActivityDistractionOptimized(String packageName, String className) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 275 | assertPackageAndClassName(packageName, className); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 276 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 277 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 278 | Slog.i(CarLog.TAG_PACKAGE, "isActivityDistractionOptimized" |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 279 | + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 280 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 281 | AppBlockingPackageInfo info = searchFromBlocklistsLocked(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 282 | if (info != null) { |
| 283 | return false; |
| 284 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 285 | return isActivityInAllowlistsLocked(packageName, className); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | @Override |
Arnaud Berry | d1f88ee | 2020-02-19 13:49:31 -0800 | [diff] [blame] | 290 | public boolean isPendingIntentDistractionOptimized(PendingIntent pendingIntent) { |
| 291 | ResolveInfo info = mPackageManager.resolveActivity( |
| 292 | pendingIntent.getIntent(), PackageManager.MATCH_DEFAULT_ONLY); |
| 293 | if (info == null) return false; |
| 294 | ActivityInfo activityInfo = info.activityInfo; |
| 295 | return isActivityDistractionOptimized(activityInfo.packageName, activityInfo.name); |
| 296 | } |
| 297 | |
| 298 | @Override |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 299 | public boolean isServiceDistractionOptimized(String packageName, String className) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 300 | if (packageName == null) { |
| 301 | throw new IllegalArgumentException("Package name null"); |
| 302 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 303 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 304 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 305 | Slog.i(CarLog.TAG_PACKAGE, "isServiceDistractionOptimized" |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 306 | + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 307 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 308 | AppBlockingPackageInfo info = searchFromBlocklistsLocked(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 309 | if (info != null) { |
| 310 | return false; |
| 311 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 312 | info = searchFromAllowlistsLocked(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 313 | if (info != null) { |
| 314 | return true; |
| 315 | } |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 320 | @Override |
| 321 | public boolean isActivityBackedBySafeActivity(ComponentName activityName) { |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 322 | RootTaskInfo info = mSystemActivityMonitoringService.getFocusedStackForTopActivity( |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 323 | activityName); |
| 324 | if (info == null) { // not top in focused stack |
| 325 | return true; |
| 326 | } |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 327 | if (!isUxRestrictedOnDisplay(info.displayId)) { |
| 328 | return true; |
| 329 | } |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 330 | if (info.childTaskNames.length <= 1) { // nothing below this. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | ComponentName activityBehind = ComponentName.unflattenFromString( |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 334 | info.childTaskNames[info.childTaskNames.length - 2]); |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 335 | return isActivityDistractionOptimized(activityBehind.getPackageName(), |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 336 | activityBehind.getClassName()); |
| 337 | } |
| 338 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 339 | public Looper getLooper() { |
| 340 | return mHandlerThread.getLooper(); |
| 341 | } |
| 342 | |
| 343 | private void assertPackageAndClassName(String packageName, String className) { |
| 344 | if (packageName == null) { |
| 345 | throw new IllegalArgumentException("Package name null"); |
| 346 | } |
| 347 | if (className == null) { |
| 348 | throw new IllegalArgumentException("Class name null"); |
| 349 | } |
| 350 | } |
| 351 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 352 | @GuardedBy("mLock") |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 353 | private AppBlockingPackageInfo searchFromBlocklistsLocked(String packageName) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 354 | for (ClientPolicy policy : mClientPolicies.values()) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 355 | AppBlockingPackageInfoWrapper wrapper = policy.mBlocklistsMap.get(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 356 | if (wrapper != null && wrapper.isMatching) { |
| 357 | return wrapper.info; |
| 358 | } |
| 359 | } |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 360 | return null; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 363 | @GuardedBy("mLock") |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 364 | private AppBlockingPackageInfo searchFromAllowlistsLocked(String packageName) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 365 | for (ClientPolicy policy : mClientPolicies.values()) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 366 | AppBlockingPackageInfoWrapper wrapper = policy.mAllowlistsMap.get(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 367 | if (wrapper != null && wrapper.isMatching) { |
| 368 | return wrapper.info; |
| 369 | } |
| 370 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 371 | AppBlockingPackageInfoWrapper wrapper = mActivityAllowlistMap.get(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 372 | return (wrapper != null) ? wrapper.info : null; |
| 373 | } |
| 374 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 375 | @GuardedBy("mLock") |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 376 | private boolean isActivityInAllowlistsLocked(String packageName, String className) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 377 | for (ClientPolicy policy : mClientPolicies.values()) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 378 | if (isActivityInMapAndMatching(policy.mAllowlistsMap, packageName, className)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 382 | return isActivityInMapAndMatching(mActivityAllowlistMap, packageName, className); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | private boolean isActivityInMapAndMatching(HashMap<String, AppBlockingPackageInfoWrapper> map, |
| 386 | String packageName, String className) { |
| 387 | AppBlockingPackageInfoWrapper wrapper = map.get(packageName); |
| 388 | if (wrapper == null || !wrapper.isMatching) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 389 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 390 | Slog.d(CarLog.TAG_PACKAGE, "Pkg not in allowlist:" + packageName); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 391 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | return wrapper.info.isActivityCovered(className); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void init() { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 399 | synchronized (mLock) { |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 400 | mHandler.requestInit(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 401 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void release() { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 406 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 407 | mHandler.requestRelease(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 408 | // wait for release do be done. This guarantees that init is done. |
| 409 | try { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 410 | mLock.wait(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 411 | } catch (InterruptedException e) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 412 | Slog.e(CarLog.TAG_PACKAGE, |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 413 | "Interrupted wait during release"); |
| 414 | Thread.currentThread().interrupt(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 415 | } |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 416 | mHasParsedPackages = false; |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 417 | mActivityAllowlistMap.clear(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 418 | mClientPolicies.clear(); |
| 419 | if (mProxies != null) { |
| 420 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 421 | proxy.disconnect(); |
| 422 | } |
| 423 | mProxies.clear(); |
| 424 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 425 | mWaitingPolicies.clear(); |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 426 | mLock.notifyAll(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 427 | } |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 428 | mContext.unregisterReceiver(mPackageParsingEventReceiver); |
Antonio Kantek | f700753 | 2020-03-17 10:37:58 -0700 | [diff] [blame] | 429 | mUserService.removeUserLifecycleListener(mUserLifecycleListener); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 430 | mSystemActivityMonitoringService.registerActivityLaunchListener(null); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 431 | for (int i = 0; i < mUxRestrictionsListeners.size(); i++) { |
| 432 | UxRestrictionsListener listener = mUxRestrictionsListeners.valueAt(i); |
| 433 | mCarUxRestrictionsService.unregisterUxRestrictionsChangeListener(listener); |
| 434 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Antonio Kantek | f700753 | 2020-03-17 10:37:58 -0700 | [diff] [blame] | 437 | private final UserLifecycleListener mUserLifecycleListener = event -> { |
| 438 | if (Log.isLoggable(CarLog.TAG_PACKAGE, Log.DEBUG)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 439 | Slog.d(CarLog.TAG_PACKAGE, "CarPackageManagerService.onEvent(" + event + ")"); |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 440 | } |
Antonio Kantek | f700753 | 2020-03-17 10:37:58 -0700 | [diff] [blame] | 441 | if (CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING == event.getEventType()) { |
| 442 | CarPackageManagerService.this.mHandler.requestParsingInstalledPkgs(0); |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 443 | } |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 444 | }; |
| 445 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 446 | // run from HandlerThread |
| 447 | private void doHandleInit() { |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 448 | startAppBlockingPolicies(); |
Antonio Kantek | f700753 | 2020-03-17 10:37:58 -0700 | [diff] [blame] | 449 | mUserService.addUserLifecycleListener(mUserLifecycleListener); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 450 | IntentFilter pkgParseIntent = new IntentFilter(); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 451 | for (String action : mPackageManagerActions) { |
| 452 | pkgParseIntent.addAction(action); |
| 453 | } |
| 454 | pkgParseIntent.addDataScheme("package"); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 455 | mContext.registerReceiverAsUser(mPackageParsingEventReceiver, UserHandle.ALL, |
| 456 | pkgParseIntent, null, null); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 457 | |
| 458 | List<Display> physicalDisplays = getPhysicalDisplays(); |
| 459 | |
| 460 | // Assume default display (display 0) is always a physical display. |
| 461 | Display defaultDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); |
| 462 | if (!physicalDisplays.contains(defaultDisplay)) { |
| 463 | if (Log.isLoggable(CarLog.TAG_PACKAGE, Log.INFO)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 464 | Slog.i(CarLog.TAG_PACKAGE, "Adding default display to physical displays."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 465 | } |
| 466 | physicalDisplays.add(defaultDisplay); |
| 467 | } |
| 468 | for (Display physicalDisplay : physicalDisplays) { |
| 469 | int displayId = physicalDisplay.getDisplayId(); |
| 470 | UxRestrictionsListener listener = new UxRestrictionsListener(mCarUxRestrictionsService); |
| 471 | mUxRestrictionsListeners.put(displayId, listener); |
| 472 | mCarUxRestrictionsService.registerUxRestrictionsChangeListener(listener, displayId); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 473 | } |
Pavel Maltsev | 38da431 | 2019-04-08 10:38:38 -0700 | [diff] [blame] | 474 | mVendorServiceController.init(); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | private void doParseInstalledPackages() { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 478 | int userId = mActivityManager.getCurrentUser(); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 479 | generateActivityAllowlistMap(userId); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 480 | synchronized (mLock) { |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 481 | mHasParsedPackages = true; |
| 482 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 483 | // Once the activity launch listener is registered we attempt to block any non-allowlisted |
| 484 | // activities that are launched. For this reason, we need to wait until after the allowlist |
Jordan Jozwiak | a5f1091 | 2019-08-22 15:29:01 -0700 | [diff] [blame] | 485 | // has been created. |
| 486 | mSystemActivityMonitoringService.registerActivityLaunchListener(mActivityLaunchListener); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 487 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 490 | private void doHandleRelease() { |
| 491 | synchronized (mLock) { |
| 492 | mVendorServiceController.release(); |
| 493 | mLock.notifyAll(); |
| 494 | } |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 497 | private void doUpdatePolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
| 498 | if (DBG_POLICY_SET) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 499 | Slog.i(CarLog.TAG_PACKAGE, "setting policy from:" + packageName + ",policy:" + policy |
| 500 | + ",flags:0x" + Integer.toHexString(flags)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 501 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 502 | AppBlockingPackageInfoWrapper[] blocklistWrapper = verifyList(policy.blacklists); |
| 503 | AppBlockingPackageInfoWrapper[] allowlistWrapper = verifyList(policy.whitelists); |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 504 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 505 | ClientPolicy clientPolicy = mClientPolicies.get(packageName); |
| 506 | if (clientPolicy == null) { |
| 507 | clientPolicy = new ClientPolicy(); |
| 508 | mClientPolicies.put(packageName, clientPolicy); |
| 509 | } |
| 510 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 511 | clientPolicy.addToBlocklists(blocklistWrapper); |
| 512 | clientPolicy.addToAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 513 | } else if ((flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 514 | clientPolicy.removeBlocklists(blocklistWrapper); |
| 515 | clientPolicy.removeAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 516 | } else { //replace. |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 517 | clientPolicy.replaceBlocklists(blocklistWrapper); |
| 518 | clientPolicy.replaceAllowlists(allowlistWrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 519 | } |
| 520 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 521 | mWaitingPolicies.remove(policy); |
Keun young Park | ff3196b | 2020-01-07 12:44:10 -0800 | [diff] [blame] | 522 | mLock.notifyAll(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 523 | } |
| 524 | if (DBG_POLICY_SET) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 525 | Slog.i(CarLog.TAG_PACKAGE, "policy set:" + dumpPoliciesLocked(false)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 528 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | private AppBlockingPackageInfoWrapper[] verifyList(AppBlockingPackageInfo[] list) { |
| 532 | if (list == null) { |
| 533 | return null; |
| 534 | } |
| 535 | LinkedList<AppBlockingPackageInfoWrapper> wrappers = new LinkedList<>(); |
| 536 | for (int i = 0; i < list.length; i++) { |
| 537 | AppBlockingPackageInfo info = list[i]; |
| 538 | if (info == null) { |
| 539 | continue; |
| 540 | } |
| 541 | boolean isMatching = isInstalledPackageMatching(info); |
| 542 | wrappers.add(new AppBlockingPackageInfoWrapper(info, isMatching)); |
| 543 | } |
| 544 | return wrappers.toArray(new AppBlockingPackageInfoWrapper[wrappers.size()]); |
| 545 | } |
| 546 | |
| 547 | boolean isInstalledPackageMatching(AppBlockingPackageInfo info) { |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 548 | PackageInfo packageInfo; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 549 | try { |
| 550 | packageInfo = mPackageManager.getPackageInfo(info.packageName, |
| 551 | PackageManager.GET_SIGNATURES); |
| 552 | } catch (NameNotFoundException e) { |
| 553 | return false; |
| 554 | } |
| 555 | if (packageInfo == null) { |
| 556 | return false; |
| 557 | } |
| 558 | // if it is system app and client specified the flag, do not check signature |
| 559 | if ((info.flags & AppBlockingPackageInfo.FLAG_SYSTEM_APP) == 0 || |
| 560 | (!packageInfo.applicationInfo.isSystemApp() && |
| 561 | !packageInfo.applicationInfo.isUpdatedSystemApp())) { |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 562 | Signature[] signatures = packageInfo.signatures; |
| 563 | if (!isAnySignatureMatching(signatures, info.signatures)) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | } |
| 567 | int version = packageInfo.versionCode; |
| 568 | if (info.minRevisionCode == 0) { |
| 569 | if (info.maxRevisionCode == 0) { // all versions |
| 570 | return true; |
| 571 | } else { // only max version matters |
| 572 | return info.maxRevisionCode > version; |
| 573 | } |
| 574 | } else { // min version matters |
| 575 | if (info.maxRevisionCode == 0) { |
| 576 | return info.minRevisionCode < version; |
| 577 | } else { |
| 578 | return (info.minRevisionCode < version) && (info.maxRevisionCode > version); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Any signature from policy matching with package's signatures is treated as matching. |
| 585 | */ |
| 586 | boolean isAnySignatureMatching(Signature[] fromPackage, Signature[] fromPolicy) { |
| 587 | if (fromPackage == null) { |
| 588 | return false; |
| 589 | } |
| 590 | if (fromPolicy == null) { |
| 591 | return false; |
| 592 | } |
| 593 | ArraySet<Signature> setFromPackage = new ArraySet<Signature>(); |
| 594 | for (Signature sig : fromPackage) { |
| 595 | setFromPackage.add(sig); |
| 596 | } |
| 597 | for (Signature sig : fromPolicy) { |
| 598 | if (setFromPackage.contains(sig)) { |
| 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 605 | /** |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 606 | * Generate a map of allowlisted packages and activities of the form {pkgName, Allowlisted |
| 607 | * activities}. The allowlist information can come from a configuration XML resource or from |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 608 | * the apps marking their activities as distraction optimized. |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 609 | * |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 610 | * @param userId Generate allowlist based on packages installed for this user. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 611 | */ |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 612 | private void generateActivityAllowlistMap(int userId) { |
| 613 | // Get the apps/activities that are allowlisted in the configuration XML resources. |
| 614 | Map<String, Set<String>> configAllowlist = generateConfigAllowlist(); |
| 615 | Map<String, Set<String>> configBlocklist = generateConfigBlocklist(); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 616 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 617 | Map<String, AppBlockingPackageInfoWrapper> activityAllowlist = |
| 618 | generateActivityAllowlistAsUser(UserHandle.USER_SYSTEM, |
| 619 | configAllowlist, configBlocklist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 620 | // Also parse packages for current user. |
| 621 | if (userId != UserHandle.USER_SYSTEM) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 622 | Map<String, AppBlockingPackageInfoWrapper> userAllowlistedPackages = |
| 623 | generateActivityAllowlistAsUser(userId, configAllowlist, configBlocklist); |
| 624 | for (String packageName : userAllowlistedPackages.keySet()) { |
| 625 | if (activityAllowlist.containsKey(packageName)) { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 626 | continue; |
| 627 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 628 | activityAllowlist.put(packageName, userAllowlistedPackages.get(packageName)); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 631 | synchronized (mLock) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 632 | mActivityAllowlistMap.clear(); |
| 633 | mActivityAllowlistMap.putAll(activityAllowlist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 637 | private Map<String, Set<String>> generateConfigAllowlist() { |
| 638 | Map<String, Set<String>> configAllowlist = new HashMap<>(); |
Mayank Garg | f253437 | 2020-08-05 17:08:17 -0700 | [diff] [blame] | 639 | mConfiguredAllowlist = mContext.getString(R.string.activityAllowlist); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 640 | if (mConfiguredAllowlist == null) { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 641 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 642 | Slog.w(CarLog.TAG_PACKAGE, "Allowlist is null."); |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 643 | } |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 644 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 645 | parseConfigList(mConfiguredAllowlist, configAllowlist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 646 | |
Mayank Garg | f253437 | 2020-08-05 17:08:17 -0700 | [diff] [blame] | 647 | mConfiguredSystemAllowlist = mContext.getString(R.string.systemActivityAllowlist); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 648 | if (mConfiguredSystemAllowlist == null) { |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 649 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 650 | Slog.w(CarLog.TAG_PACKAGE, "System allowlist is null."); |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 653 | parseConfigList(mConfiguredSystemAllowlist, configAllowlist); |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 654 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 655 | // Add the blocking overlay activity to the allowlist, since that needs to run in a |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 656 | // restricted state to communicate the reason an app was blocked. |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 657 | Set<String> defaultActivity = new ArraySet<>(); |
Ram Periathiruvadi | 402d12e | 2018-07-11 18:31:13 -0700 | [diff] [blame] | 658 | if (mActivityBlockingActivity != null) { |
| 659 | defaultActivity.add(mActivityBlockingActivity.getClassName()); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 660 | configAllowlist.put(mActivityBlockingActivity.getPackageName(), defaultActivity); |
Ram Periathiruvadi | 402d12e | 2018-07-11 18:31:13 -0700 | [diff] [blame] | 661 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 662 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 663 | return configAllowlist; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 666 | private Map<String, Set<String>> generateConfigBlocklist() { |
| 667 | Map<String, Set<String>> configBlocklist = new HashMap<>(); |
Mayank Garg | f253437 | 2020-08-05 17:08:17 -0700 | [diff] [blame] | 668 | mConfiguredBlocklist = mContext.getString(R.string.activityDenylist); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 669 | if (mConfiguredBlocklist == null) { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 670 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 671 | Slog.d(CarLog.TAG_PACKAGE, "Null blocklist in config"); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 674 | parseConfigList(mConfiguredBlocklist, configBlocklist); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 675 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 676 | return configBlocklist; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 680 | * Generates allowlisted activities based on packages installed for system user and current |
| 681 | * user (if different). Factors affecting allowlist: |
| 682 | * - allowlist from resource config; |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 683 | * - activity declared as Distraction Optimized (D.O.) in manifest; |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 684 | * - blocklist from resource config - package/activity blocklisted will not exist |
| 685 | * in returned allowlist. |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 686 | * |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 687 | * @param userId Parse packages installed for user. |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 688 | * @param configAllowlist Allowlist from config. |
| 689 | * @param configBlocklist Blocklist from config. |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 690 | */ |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 691 | private Map<String, AppBlockingPackageInfoWrapper> generateActivityAllowlistAsUser(int userId, |
| 692 | Map<String, Set<String>> configAllowlist, Map<String, Set<String>> configBlocklist) { |
| 693 | HashMap<String, AppBlockingPackageInfoWrapper> activityAllowlist = new HashMap<>(); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 694 | |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 695 | List<PackageInfo> packages = mPackageManager |
| 696 | .getInstalledPackagesAsUser(PackageManager.GET_SIGNATURES |
| 697 | | PackageManager.GET_ACTIVITIES | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 698 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 699 | for (PackageInfo info : packages) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 700 | if (info.applicationInfo == null) { |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | int flags = 0; |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 705 | Set<String> activities = new ArraySet<>(); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 706 | |
| 707 | if (info.applicationInfo.isSystemApp() |
| 708 | || info.applicationInfo.isUpdatedSystemApp()) { |
| 709 | flags = AppBlockingPackageInfo.FLAG_SYSTEM_APP; |
| 710 | } |
| 711 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 712 | /* 1. Check if all or some of this app is in the <activityAllowlist> or |
| 713 | <systemActivityAllowlist> in config.xml */ |
| 714 | Set<String> configActivitiesForPackage = configAllowlist.get(info.packageName); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 715 | if (configActivitiesForPackage != null) { |
| 716 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 717 | Slog.d(CarLog.TAG_PACKAGE, info.packageName + " allowlisted"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 718 | } |
| 719 | if (configActivitiesForPackage.size() == 0) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 720 | // Whole Pkg has been allowlisted |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 721 | flags |= AppBlockingPackageInfo.FLAG_WHOLE_ACTIVITY; |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 722 | // Add all activities to the allowlist |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 723 | List<String> activitiesForPackage = getActivitiesInPackage(info); |
| 724 | if (activitiesForPackage != null) { |
| 725 | activities.addAll(activitiesForPackage); |
| 726 | } else { |
| 727 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 728 | Slog.d(CarLog.TAG_PACKAGE, info.packageName + ": Activities null"); |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 729 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 730 | } |
| 731 | } else { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 732 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 733 | Slog.d(CarLog.TAG_PACKAGE, "Partially Allowlisted. WL Activities:"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 734 | for (String a : configActivitiesForPackage) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 735 | Slog.d(CarLog.TAG_PACKAGE, a); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 738 | activities.addAll(configActivitiesForPackage); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 739 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 740 | } |
| 741 | /* 2. If app is not listed in the config.xml check their Manifest meta-data to |
| 742 | see if they have any Distraction Optimized(DO) activities. |
| 743 | For non system apps, we check if the app install source was a permittable |
| 744 | source. This prevents side-loaded apps to fake DO. Bypass the check |
| 745 | for debug builds for development convenience. */ |
| 746 | if (!isDebugBuild() |
| 747 | && !info.applicationInfo.isSystemApp() |
| 748 | && !info.applicationInfo.isUpdatedSystemApp()) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 749 | try { |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 750 | if (mAllowedAppInstallSources != null) { |
| 751 | String installerName = mPackageManager.getInstallerPackageName( |
| 752 | info.packageName); |
| 753 | if (installerName == null || (installerName != null |
| 754 | && !mAllowedAppInstallSources.contains(installerName))) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 755 | Slog.w(CarLog.TAG_PACKAGE, |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 756 | info.packageName + " not installed from permitted sources " |
Oleh Cherpak | 01b0933 | 2019-02-12 15:43:33 +0200 | [diff] [blame] | 757 | + (installerName == null ? "NULL" : installerName)); |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 758 | continue; |
| 759 | } |
| 760 | } |
| 761 | } catch (IllegalArgumentException e) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 762 | Slog.w(CarLog.TAG_PACKAGE, info.packageName + " not installed!"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 763 | continue; |
| 764 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | try { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 768 | String[] doActivities = |
| 769 | CarAppMetadataReader.findDistractionOptimizedActivitiesAsUser( |
| 770 | mContext, info.packageName, userId); |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 771 | if (doActivities != null) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 772 | // Some of the activities in this app are Distraction Optimized. |
| 773 | if (DBG_POLICY_CHECK) { |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 774 | for (String activity : doActivities) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 775 | Slog.d(CarLog.TAG_PACKAGE, "adding " + activity + " from " |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 776 | + info.packageName + " to allowlist"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 777 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 778 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 779 | activities.addAll(Arrays.asList(doActivities)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 780 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 781 | } catch (NameNotFoundException e) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 782 | Slog.w(CarLog.TAG_PACKAGE, "Error reading metadata: " + info.packageName); |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 783 | continue; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 784 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 785 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 786 | // Nothing to add to allowlist |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 787 | if (activities.isEmpty()) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 791 | /* 3. Check if parsed activity is in <activityBlocklist> in config.xml. Anything |
| 792 | in blocklist should not be allowlisted, either as D.O. or by config. */ |
| 793 | if (configBlocklist.containsKey(info.packageName)) { |
| 794 | Set<String> configBlocklistActivities = configBlocklist.get(info.packageName); |
| 795 | if (configBlocklistActivities.isEmpty()) { |
| 796 | // Whole package should be blocklisted. |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 797 | continue; |
| 798 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 799 | activities.removeAll(configBlocklistActivities); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 802 | Signature[] signatures; |
| 803 | signatures = info.signatures; |
| 804 | AppBlockingPackageInfo appBlockingInfo = new AppBlockingPackageInfo( |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 805 | info.packageName, 0, 0, flags, signatures, |
| 806 | activities.toArray(new String[activities.size()])); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 807 | AppBlockingPackageInfoWrapper wrapper = new AppBlockingPackageInfoWrapper( |
| 808 | appBlockingInfo, true); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 809 | activityAllowlist.put(info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 810 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 811 | return activityAllowlist; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Ram Periathiruvadi | d937c32 | 2018-06-13 12:28:12 -0700 | [diff] [blame] | 814 | private boolean isDebugBuild() { |
| 815 | return Build.IS_USERDEBUG || Build.IS_ENG; |
| 816 | } |
| 817 | |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 818 | /** |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 819 | * Parses the given resource and updates the input map of packages and activities. |
| 820 | * |
| 821 | * 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] | 822 | * included. |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 823 | * |
| 824 | * When there are multiple entries regarding one package, the entry with |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 825 | * greater scope wins. Namely if there were 2 entries such that one allowlists |
| 826 | * an activity, and the other allowlists the entire package of the activity, |
| 827 | * the package is allowlisted, regardless of input order. |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 828 | */ |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 829 | @VisibleForTesting |
| 830 | /* package */ void parseConfigList(String configList, |
| 831 | @NonNull Map<String, Set<String>> packageToActivityMap) { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 832 | if (configList == null) { |
Yao, Yuxing | 4c3b93c | 2019-02-20 09:00:01 -0800 | [diff] [blame] | 833 | return; |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 834 | } |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 835 | String[] entries = configList.split(PACKAGE_DELIMITER); |
| 836 | for (String entry : entries) { |
| 837 | String[] packageActivityPair = entry.split(PACKAGE_ACTIVITY_DELIMITER); |
| 838 | Set<String> activities = packageToActivityMap.get(packageActivityPair[0]); |
| 839 | boolean newPackage = false; |
| 840 | if (activities == null) { |
| 841 | activities = new ArraySet<>(); |
| 842 | newPackage = true; |
| 843 | packageToActivityMap.put(packageActivityPair[0], activities); |
| 844 | } |
| 845 | if (packageActivityPair.length == 1) { // whole package |
| 846 | activities.clear(); |
| 847 | } else if (packageActivityPair.length == 2) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 848 | // add class name only when the whole package is not allowlisted. |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 849 | if (newPackage || (activities.size() > 0)) { |
| 850 | activities.add(packageActivityPair[1]); |
| 851 | } |
| 852 | } |
| 853 | } |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 856 | @Nullable |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 857 | private List<String> getActivitiesInPackage(PackageInfo info) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 858 | if (info == null || info.activities == null) { |
| 859 | return null; |
| 860 | } |
| 861 | List<String> activityList = new ArrayList<>(); |
| 862 | for (ActivityInfo aInfo : info.activities) { |
| 863 | activityList.add(aInfo.name); |
| 864 | } |
Yao, Yuxing | 2a1e1d9 | 2019-01-08 11:58:52 -0800 | [diff] [blame] | 865 | return activityList; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame] | 868 | /** |
| 869 | * Checks if there are any {@link CarAppBlockingPolicyService} and creates a proxy to |
| 870 | * bind to them and retrieve the {@link CarAppBlockingPolicy} |
| 871 | */ |
| 872 | @VisibleForTesting |
| 873 | public void startAppBlockingPolicies() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 874 | Intent policyIntent = new Intent(); |
| 875 | policyIntent.setAction(CarAppBlockingPolicyService.SERVICE_INTERFACE); |
| 876 | List<ResolveInfo> policyInfos = mPackageManager.queryIntentServices(policyIntent, 0); |
| 877 | 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] | 878 | return; |
| 879 | } |
| 880 | LinkedList<AppBlockingPolicyProxy> proxies = new LinkedList<>(); |
| 881 | for (ResolveInfo resolveInfo : policyInfos) { |
| 882 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 883 | if (serviceInfo == null) { |
| 884 | continue; |
| 885 | } |
| 886 | if (serviceInfo.isEnabled()) { |
| 887 | if (mPackageManager.checkPermission(Car.PERMISSION_CONTROL_APP_BLOCKING, |
| 888 | serviceInfo.packageName) != PackageManager.PERMISSION_GRANTED) { |
| 889 | continue; |
| 890 | } |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 891 | Slog.i(CarLog.TAG_PACKAGE, "found policy holding service:" + serviceInfo); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 892 | AppBlockingPolicyProxy proxy = new AppBlockingPolicyProxy(this, mContext, |
| 893 | serviceInfo); |
| 894 | proxy.connect(); |
| 895 | proxies.add(proxy); |
| 896 | } |
| 897 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 898 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 899 | mProxies = proxies; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | public void onPolicyConnectionAndSet(AppBlockingPolicyProxy proxy, |
| 904 | CarAppBlockingPolicy policy) { |
| 905 | doHandlePolicyConnection(proxy, policy); |
| 906 | } |
| 907 | |
| 908 | public void onPolicyConnectionFailure(AppBlockingPolicyProxy proxy) { |
| 909 | doHandlePolicyConnection(proxy, null); |
| 910 | } |
| 911 | |
| 912 | private void doHandlePolicyConnection(AppBlockingPolicyProxy proxy, |
| 913 | CarAppBlockingPolicy policy) { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 914 | synchronized (mLock) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 915 | if (mProxies == null) { |
| 916 | proxy.disconnect(); |
| 917 | return; |
| 918 | } |
| 919 | mProxies.remove(proxy); |
| 920 | if (mProxies.size() == 0) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 921 | mProxies = null; |
| 922 | } |
| 923 | } |
| 924 | try { |
| 925 | if (policy != null) { |
| 926 | if (DBG_POLICY_SET) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 927 | Slog.i(CarLog.TAG_PACKAGE, "policy setting from policy service:" |
| 928 | + proxy.getPackageName()); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 929 | } |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 930 | doSetAppBlockingPolicy(proxy.getPackageName(), policy, 0); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 931 | } |
| 932 | } finally { |
| 933 | proxy.disconnect(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public void dump(PrintWriter writer) { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 939 | synchronized (mLock) { |
Jordan Jozwiak | 0aa0598 | 2019-06-17 15:14:39 -0700 | [diff] [blame] | 940 | writer.println("*CarPackageManagerService*"); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 941 | writer.println("mEnableActivityBlocking:" + mEnableActivityBlocking); |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 942 | writer.println("mHasParsedPackages:" + mHasParsedPackages); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 943 | List<String> restrictions = new ArrayList<>(mUxRestrictionsListeners.size()); |
| 944 | for (int i = 0; i < mUxRestrictionsListeners.size(); i++) { |
| 945 | int displayId = mUxRestrictionsListeners.keyAt(i); |
| 946 | UxRestrictionsListener listener = mUxRestrictionsListeners.valueAt(i); |
| 947 | restrictions.add(String.format("Display %d is %s", |
| 948 | displayId, (listener.isRestricted() ? "restricted" : "unrestricted"))); |
| 949 | } |
| 950 | writer.println("Display Restrictions:\n" + String.join("\n", restrictions)); |
Jordan Jozwiak | 0aa0598 | 2019-06-17 15:14:39 -0700 | [diff] [blame] | 951 | writer.println(" Blocked activity log:"); |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 952 | writer.println(String.join("\n", mBlockedActivityLogs)); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 953 | writer.print(dumpPoliciesLocked(true)); |
| 954 | } |
| 955 | } |
| 956 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 957 | @GuardedBy("mLock") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 958 | private String dumpPoliciesLocked(boolean dumpAll) { |
| 959 | StringBuilder sb = new StringBuilder(); |
| 960 | if (dumpAll) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 961 | sb.append("**System allowlist**\n"); |
| 962 | for (AppBlockingPackageInfoWrapper wrapper : mActivityAllowlistMap.values()) { |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 963 | sb.append(wrapper.toString() + "\n"); |
| 964 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 965 | } |
| 966 | sb.append("**Client Policies**\n"); |
| 967 | for (Entry<String, ClientPolicy> entry : mClientPolicies.entrySet()) { |
| 968 | sb.append("Client:" + entry.getKey() + "\n"); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 969 | sb.append(" allowlists:\n"); |
| 970 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().mAllowlistsMap.values()) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 971 | sb.append(wrapper.toString() + "\n"); |
| 972 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 973 | sb.append(" blocklists:\n"); |
| 974 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().mBlocklistsMap.values()) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 975 | sb.append(wrapper.toString() + "\n"); |
| 976 | } |
| 977 | } |
| 978 | sb.append("**Unprocessed policy services**\n"); |
| 979 | if (mProxies != null) { |
| 980 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 981 | sb.append(proxy.toString() + "\n"); |
| 982 | } |
| 983 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 984 | sb.append("**Allowlist string in resource**\n"); |
| 985 | sb.append(mConfiguredAllowlist + "\n"); |
Ram Periathiruvadi | 46a3a3d | 2018-03-23 16:58:32 -0700 | [diff] [blame] | 986 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 987 | sb.append("**System allowlist string in resource**\n"); |
| 988 | sb.append(mConfiguredSystemAllowlist + "\n"); |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 989 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 990 | sb.append("**Blocklist string in resource**\n"); |
| 991 | sb.append(mConfiguredBlocklist + "\n"); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 992 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 993 | return sb.toString(); |
| 994 | } |
| 995 | |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 996 | /** |
| 997 | * Returns display with physical address. |
| 998 | */ |
| 999 | private List<Display> getPhysicalDisplays() { |
| 1000 | List<Display> displays = new ArrayList<>(); |
| 1001 | for (Display display : mDisplayManager.getDisplays()) { |
| 1002 | if (display.getAddress() instanceof DisplayAddress.Physical) { |
| 1003 | displays.add(display); |
| 1004 | } |
| 1005 | } |
| 1006 | return displays; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Returns whether UX restrictions is required for display. |
| 1011 | * |
| 1012 | * Non-physical display will use restrictions for {@link Display#DEFAULT_DISPLAY}. |
| 1013 | */ |
| 1014 | private boolean isUxRestrictedOnDisplay(int displayId) { |
| 1015 | UxRestrictionsListener listenerForTopTaskDisplay; |
| 1016 | if (mUxRestrictionsListeners.indexOfKey(displayId) < 0) { |
| 1017 | listenerForTopTaskDisplay = mUxRestrictionsListeners.get(Display.DEFAULT_DISPLAY); |
| 1018 | if (listenerForTopTaskDisplay == null) { |
| 1019 | // This should never happen. |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1020 | Slog.e(CarLog.TAG_PACKAGE, "Missing listener for default display."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1021 | return true; |
| 1022 | } |
| 1023 | } else { |
| 1024 | listenerForTopTaskDisplay = mUxRestrictionsListeners.get(displayId); |
| 1025 | } |
| 1026 | |
| 1027 | return listenerForTopTaskDisplay.isRestricted(); |
| 1028 | } |
| 1029 | |
| 1030 | private void blockTopActivitiesIfNecessary() { |
| 1031 | List<TopTaskInfoContainer> topTasks = mSystemActivityMonitoringService.getTopTasks(); |
| 1032 | for (TopTaskInfoContainer topTask : topTasks) { |
| 1033 | if (topTask == null) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1034 | Slog.e(CarLog.TAG_PACKAGE, "Top tasks contains null."); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1035 | continue; |
| 1036 | } |
| 1037 | blockTopActivityIfNecessary(topTask); |
| 1038 | } |
| 1039 | } |
| 1040 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1041 | private void blockTopActivityIfNecessary(TopTaskInfoContainer topTask) { |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1042 | if (isUxRestrictedOnDisplay(topTask.displayId)) { |
| 1043 | doBlockTopActivityIfNotAllowed(topTask); |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 1044 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | private void doBlockTopActivityIfNotAllowed(TopTaskInfoContainer topTask) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1048 | if (topTask.topActivity == null) { |
| 1049 | return; |
| 1050 | } |
Jordan Jozwiak | a5f1091 | 2019-08-22 15:29:01 -0700 | [diff] [blame] | 1051 | |
| 1052 | // We are not handling the UI blocking until we know what is allowed and what is not. |
| 1053 | if (!mHasParsedPackages) { |
| 1054 | if (Log.isLoggable(CarLog.TAG_PACKAGE, Log.INFO)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1055 | Slog.i(CarLog.TAG_PACKAGE, "Packages not parsed, so ignoring block for " + topTask); |
Jordan Jozwiak | a5f1091 | 2019-08-22 15:29:01 -0700 | [diff] [blame] | 1056 | } |
| 1057 | return; |
| 1058 | } |
| 1059 | |
Ram Periathiruvadi | e154f58 | 2018-03-21 21:53:27 -0700 | [diff] [blame] | 1060 | boolean allowed = isActivityDistractionOptimized( |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1061 | topTask.topActivity.getPackageName(), |
| 1062 | topTask.topActivity.getClassName()); |
| 1063 | if (DBG_POLICY_ENFORCEMENT) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1064 | Slog.i(CarLog.TAG_PACKAGE, "new activity:" + topTask.toString() + " allowed:" |
| 1065 | + allowed); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1066 | } |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1067 | if (allowed) { |
| 1068 | return; |
| 1069 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1070 | synchronized (mLock) { |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1071 | if (!mEnableActivityBlocking) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1072 | Slog.d(CarLog.TAG_PACKAGE, "Current activity " + topTask.topActivity |
| 1073 | + " not allowed, blocking disabled. Number of tasks in stack:" |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1074 | + topTask.taskInfo.childTaskIds.length); |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1075 | return; |
| 1076 | } |
| 1077 | } |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 1078 | if (DBG_POLICY_ENFORCEMENT) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1079 | Slog.i(CarLog.TAG_PACKAGE, "Current activity " + topTask.topActivity |
| 1080 | + " not allowed, will block, number of tasks in stack:" |
| 1081 | + topTask.taskInfo.childTaskIds.length); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1082 | } |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1083 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1084 | // Figure out the root activity of blocked task. |
| 1085 | String taskRootActivity = null; |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1086 | for (int i = 0; i < topTask.taskInfo.childTaskIds.length; i++) { |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1087 | // topTask.taskId is the task that should be blocked. |
Louis Chang | 3bf2f20 | 2020-08-18 13:04:28 +0800 | [diff] [blame] | 1088 | if (topTask.taskInfo.childTaskIds[i] == topTask.taskId) { |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1089 | // stackInfo represents an ActivityStack. Its fields taskIds and taskNames |
| 1090 | // 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] | 1091 | taskRootActivity = topTask.taskInfo.childTaskNames[i]; |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | } |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1095 | |
| 1096 | boolean isRootDO = false; |
| 1097 | if (taskRootActivity != null) { |
| 1098 | ComponentName componentName = ComponentName.unflattenFromString(taskRootActivity); |
| 1099 | isRootDO = isActivityDistractionOptimized( |
| 1100 | componentName.getPackageName(), componentName.getClassName()); |
| 1101 | } |
| 1102 | |
| 1103 | Intent newActivityIntent = createBlockingActivityIntent( |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1104 | mActivityBlockingActivity, topTask.displayId, |
| 1105 | topTask.topActivity.flattenToShortString(), topTask.taskId, taskRootActivity, |
| 1106 | isRootDO); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1107 | |
| 1108 | // Intent contains all info to debug what is blocked - log into both logcat and dumpsys. |
| 1109 | String log = "Starting blocking activity with intent: " + newActivityIntent.toUri(0); |
| 1110 | if (Log.isLoggable(CarLog.TAG_PACKAGE, Log.INFO)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1111 | Slog.i(CarLog.TAG_PACKAGE, log); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1112 | } |
| 1113 | addLog(log); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1114 | mSystemActivityMonitoringService.blockActivity(topTask, newActivityIntent); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1117 | /** |
| 1118 | * Creates an intent to start blocking activity. |
| 1119 | * |
| 1120 | * @param blockingActivity the activity to launch |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 1121 | * @param blockedActivity the activity being blocked |
| 1122 | * @param blockedTaskId the blocked task id, which contains the blocked activity |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1123 | * @param taskRootActivity root activity of the blocked task |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1124 | * @return an intent to launch the blocking activity. |
| 1125 | */ |
| 1126 | private static Intent createBlockingActivityIntent(ComponentName blockingActivity, |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1127 | int displayId, String blockedActivity, int blockedTaskId, String taskRootActivity, |
| 1128 | boolean isRootDo) { |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1129 | Intent newActivityIntent = new Intent(); |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1130 | newActivityIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1131 | newActivityIntent.setComponent(blockingActivity); |
| 1132 | newActivityIntent.putExtra( |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1133 | BLOCKING_INTENT_EXTRA_DISPLAY_ID, displayId); |
| 1134 | newActivityIntent.putExtra( |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1135 | BLOCKING_INTENT_EXTRA_BLOCKED_ACTIVITY_NAME, blockedActivity); |
| 1136 | newActivityIntent.putExtra( |
| 1137 | BLOCKING_INTENT_EXTRA_BLOCKED_TASK_ID, blockedTaskId); |
| 1138 | newActivityIntent.putExtra( |
| 1139 | BLOCKING_INTENT_EXTRA_ROOT_ACTIVITY_NAME, taskRootActivity); |
| 1140 | newActivityIntent.putExtra( |
| 1141 | BLOCKING_INTENT_EXTRA_IS_ROOT_ACTIVITY_DO, isRootDo); |
Yao, Yuxing | cfcc59f | 2019-03-05 16:35:47 -0800 | [diff] [blame] | 1142 | |
Yao, Yuxing | 14e83b4 | 2018-10-29 18:29:56 -0700 | [diff] [blame] | 1143 | return newActivityIntent; |
| 1144 | } |
| 1145 | |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1146 | /** |
| 1147 | * Enable/Disable activity blocking by correspondingly enabling/disabling broadcasting UXR |
| 1148 | * changes in {@link CarUxRestrictionsManagerService}. This is only available in |
| 1149 | * engineering builds for development convenience. |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1150 | */ |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1151 | @Override |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1152 | public void setEnableActivityBlocking(boolean enable) { |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1153 | if (!isDebugBuild()) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1154 | Slog.e(CarLog.TAG_PACKAGE, "Cannot enable/disable activity blocking"); |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1155 | return; |
| 1156 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1157 | |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1158 | // Check if the caller has the same signature as that of the car service. |
| 1159 | if (mPackageManager.checkSignatures(Process.myUid(), Binder.getCallingUid()) |
| 1160 | != PackageManager.SIGNATURE_MATCH) { |
| 1161 | throw new SecurityException( |
| 1162 | "Caller " + mPackageManager.getNameForUid(Binder.getCallingUid()) |
Mayank Garg | 31e7304 | 2020-01-23 00:10:38 -0800 | [diff] [blame] | 1163 | + " does not have the right signature"); |
Ram Periathiruvadi | a6e0d9b | 2018-04-25 20:19:22 -0700 | [diff] [blame] | 1164 | } |
Ram Periathiruvadi | cdee120 | 2018-07-17 18:01:31 -0700 | [diff] [blame] | 1165 | mCarUxRestrictionsService.setUxRChangeBroadcastEnabled(enable); |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1168 | /** |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1169 | * Get the distraction optimized activities for the given package. |
| 1170 | * |
| 1171 | * @param pkgName Name of the package |
| 1172 | * @return Array of the distraction optimized activities in the package |
| 1173 | */ |
| 1174 | @Nullable |
| 1175 | public String[] getDistractionOptimizedActivities(String pkgName) { |
| 1176 | try { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1177 | return CarAppMetadataReader.findDistractionOptimizedActivitiesAsUser(mContext, pkgName, |
| 1178 | mActivityManager.getCurrentUser()); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1179 | } catch (NameNotFoundException e) { |
| 1180 | return null; |
| 1181 | } |
| 1182 | } |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1183 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1184 | /** |
Yao, Yuxing | d1d6a37 | 2018-05-08 10:37:43 -0700 | [diff] [blame] | 1185 | * Append one line of log for dumpsys. |
| 1186 | * |
| 1187 | * <p>Maintains the size of log by {@link #LOG_SIZE} and appends tag and timestamp to the line. |
| 1188 | */ |
| 1189 | private void addLog(String log) { |
| 1190 | while (mBlockedActivityLogs.size() >= LOG_SIZE) { |
| 1191 | mBlockedActivityLogs.remove(); |
| 1192 | } |
| 1193 | StringBuffer sb = new StringBuffer() |
| 1194 | .append(CarLog.TAG_PACKAGE).append(':') |
| 1195 | .append(DateFormat.format( |
| 1196 | "MM-dd HH:mm:ss", System.currentTimeMillis())).append(": ") |
| 1197 | .append(log); |
| 1198 | mBlockedActivityLogs.add(sb.toString()); |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1202 | * Reading policy and setting policy can take time. Run it in a separate handler thread. |
| 1203 | */ |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1204 | private static final class PackageHandler extends Handler { |
| 1205 | private static final String TAG = PackageHandler.class.getSimpleName(); |
| 1206 | |
Keun young Park | 6c4fd34 | 2019-12-13 12:46:04 -0800 | [diff] [blame] | 1207 | private static final int MSG_INIT = 0; |
| 1208 | private static final int MSG_PARSE_PKG = 1; |
| 1209 | private static final int MSG_UPDATE_POLICY = 2; |
| 1210 | private static final int MSG_RELEASE = 3; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1211 | |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1212 | private final WeakReference<CarPackageManagerService> mService; |
| 1213 | |
| 1214 | private PackageHandler(Looper looper, CarPackageManagerService service) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1215 | super(looper); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1216 | mService = new WeakReference<CarPackageManagerService>(service); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | private void requestInit() { |
| 1220 | Message msg = obtainMessage(MSG_INIT); |
| 1221 | sendMessage(msg); |
| 1222 | } |
| 1223 | |
| 1224 | private void requestRelease() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1225 | removeMessages(MSG_UPDATE_POLICY); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1226 | Message msg = obtainMessage(MSG_RELEASE); |
| 1227 | sendMessage(msg); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1230 | private void requestUpdatingPolicy(String packageName, CarAppBlockingPolicy policy, |
| 1231 | int flags) { |
| 1232 | Pair<String, CarAppBlockingPolicy> pair = new Pair<>(packageName, policy); |
| 1233 | Message msg = obtainMessage(MSG_UPDATE_POLICY, flags, 0, pair); |
| 1234 | sendMessage(msg); |
| 1235 | } |
| 1236 | |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1237 | private void requestParsingInstalledPkgs(long delayMs) { |
Yao, Yuxing | 0c6fc73 | 2019-05-10 14:58:49 -0700 | [diff] [blame] | 1238 | // Parse packages for current user. |
| 1239 | removeMessages(MSG_PARSE_PKG); |
| 1240 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1241 | Message msg = obtainMessage(MSG_PARSE_PKG); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1242 | if (delayMs == 0) { |
| 1243 | sendMessage(msg); |
| 1244 | } else { |
| 1245 | sendMessageDelayed(msg, delayMs); |
| 1246 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1249 | @Override |
| 1250 | public void handleMessage(Message msg) { |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1251 | CarPackageManagerService service = mService.get(); |
| 1252 | if (service == null) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1253 | Slog.i(TAG, "handleMessage null service"); |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1254 | return; |
| 1255 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1256 | switch (msg.what) { |
| 1257 | case MSG_INIT: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1258 | service.doHandleInit(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1259 | break; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1260 | case MSG_PARSE_PKG: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1261 | service.doParseInstalledPackages(); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1262 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1263 | case MSG_UPDATE_POLICY: |
| 1264 | Pair<String, CarAppBlockingPolicy> pair = |
| 1265 | (Pair<String, CarAppBlockingPolicy>) msg.obj; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1266 | service.doUpdatePolicy(pair.first, pair.second, msg.arg1); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1267 | break; |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1268 | case MSG_RELEASE: |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 1269 | service.doHandleRelease(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 1270 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | private static class AppBlockingPackageInfoWrapper { |
| 1276 | private final AppBlockingPackageInfo info; |
| 1277 | /** |
| 1278 | * Whether the current info is matching with the target package in system. Mismatch can |
| 1279 | * happen for version out of range or signature mismatch. |
| 1280 | */ |
| 1281 | private boolean isMatching; |
| 1282 | |
| 1283 | private AppBlockingPackageInfoWrapper(AppBlockingPackageInfo info, boolean isMatching) { |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1284 | this.info = info; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1285 | this.isMatching = isMatching; |
| 1286 | } |
| 1287 | |
| 1288 | @Override |
| 1289 | public String toString() { |
| 1290 | return "AppBlockingPackageInfoWrapper [info=" + info + ", isMatching=" + isMatching + |
| 1291 | "]"; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Client policy holder per each client. Should be accessed with CarpackageManagerService.this |
| 1297 | * held. |
| 1298 | */ |
| 1299 | private static class ClientPolicy { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1300 | private final HashMap<String, AppBlockingPackageInfoWrapper> mAllowlistsMap = |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1301 | new HashMap<>(); |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1302 | private final HashMap<String, AppBlockingPackageInfoWrapper> mBlocklistsMap = |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1303 | new HashMap<>(); |
| 1304 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1305 | private void replaceAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1306 | mAllowlistsMap.clear(); |
| 1307 | addToAllowlists(allowlists); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1310 | private void addToAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1311 | if (allowlists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1312 | return; |
| 1313 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1314 | for (AppBlockingPackageInfoWrapper wrapper : allowlists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1315 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1316 | mAllowlistsMap.put(wrapper.info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1321 | private void removeAllowlists(AppBlockingPackageInfoWrapper[] allowlists) { |
| 1322 | if (allowlists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1325 | for (AppBlockingPackageInfoWrapper wrapper : allowlists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1326 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1327 | mAllowlistsMap.remove(wrapper.info.packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1332 | private void replaceBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1333 | mBlocklistsMap.clear(); |
| 1334 | addToBlocklists(blocklists); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1337 | private void addToBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1338 | if (blocklists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1339 | return; |
| 1340 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1341 | for (AppBlockingPackageInfoWrapper wrapper : blocklists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1342 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1343 | mBlocklistsMap.put(wrapper.info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1348 | private void removeBlocklists(AppBlockingPackageInfoWrapper[] blocklists) { |
| 1349 | if (blocklists == null) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1350 | return; |
| 1351 | } |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1352 | for (AppBlockingPackageInfoWrapper wrapper : blocklists) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1353 | if (wrapper != null) { |
Jordan Jozwiak | 794abcb | 2020-07-27 08:08:11 -0700 | [diff] [blame] | 1354 | mBlocklistsMap.remove(wrapper.info.packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1359 | |
| 1360 | private class ActivityLaunchListener |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1361 | implements SystemActivityMonitoringService.ActivityLaunchListener { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1362 | @Override |
| 1363 | public void onActivityLaunch(TopTaskInfoContainer topTask) { |
Yao, Yuxing | 2646cff | 2019-04-23 17:01:26 -0700 | [diff] [blame] | 1364 | if (topTask == null) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1365 | Slog.e(CarLog.TAG_PACKAGE, "Received callback with null top task."); |
Yao, Yuxing | 2646cff | 2019-04-23 17:01:26 -0700 | [diff] [blame] | 1366 | return; |
| 1367 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1368 | blockTopActivityIfNecessary(topTask); |
| 1369 | } |
| 1370 | } |
| 1371 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1372 | /** |
| 1373 | * Listens to the UX restrictions from {@link CarUxRestrictionsManagerService} and initiates |
| 1374 | * checking if the foreground Activity should be blocked. |
| 1375 | */ |
| 1376 | private class UxRestrictionsListener extends ICarUxRestrictionsChangeListener.Stub { |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1377 | @GuardedBy("mLock") |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1378 | @Nullable |
| 1379 | private CarUxRestrictions mCurrentUxRestrictions; |
| 1380 | private final CarUxRestrictionsManagerService uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1381 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1382 | public UxRestrictionsListener(CarUxRestrictionsManagerService service) { |
| 1383 | uxRestrictionsService = service; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | @Override |
| 1387 | public void onUxRestrictionsChanged(CarUxRestrictions restrictions) { |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 1388 | if (DBG_POLICY_ENFORCEMENT) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1389 | Slog.d(CarLog.TAG_PACKAGE, "Received uxr restrictions: " |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1390 | + restrictions.isRequiresDistractionOptimization() |
| 1391 | + " : " + restrictions.getActiveRestrictions()); |
| 1392 | } |
Ram Periathiruvadi | c57678d | 2018-04-23 09:18:33 -0700 | [diff] [blame] | 1393 | // We are not handling the restrictions until we know what is allowed and what is not. |
| 1394 | // This is to handle some situations, where car service is ready and getting sensor |
| 1395 | // data but we haven't received the boot complete intents. |
| 1396 | if (!mHasParsedPackages) { |
| 1397 | return; |
| 1398 | } |
| 1399 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1400 | synchronized (mLock) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1401 | mCurrentUxRestrictions = new CarUxRestrictions(restrictions); |
| 1402 | } |
| 1403 | checkIfTopActivityNeedsBlocking(); |
| 1404 | } |
| 1405 | |
| 1406 | private void checkIfTopActivityNeedsBlocking() { |
| 1407 | boolean shouldCheck = false; |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1408 | synchronized (mLock) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1409 | if (mCurrentUxRestrictions != null |
| 1410 | && mCurrentUxRestrictions.isRequiresDistractionOptimization()) { |
| 1411 | shouldCheck = true; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1412 | } |
| 1413 | } |
Yao, Yuxing | 4ddb2c6 | 2019-02-04 11:26:46 -0800 | [diff] [blame] | 1414 | if (DBG_POLICY_ENFORCEMENT) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1415 | Slog.d(CarLog.TAG_PACKAGE, "Should check top tasks?: " + shouldCheck); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1416 | } |
| 1417 | if (shouldCheck) { |
Yao, Yuxing | d525de1 | 2019-05-06 15:11:48 -0700 | [diff] [blame] | 1418 | // 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] | 1419 | blockTopActivitiesIfNecessary(); |
| 1420 | } |
| 1421 | } |
| 1422 | |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1423 | private boolean isRestricted() { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1424 | // if current restrictions is null, try querying the service, once. |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1425 | synchronized (mLock) { |
| 1426 | if (mCurrentUxRestrictions == null) { |
| 1427 | mCurrentUxRestrictions = uxRestrictionsService.getCurrentUxRestrictions(); |
| 1428 | } |
| 1429 | if (mCurrentUxRestrictions != null) { |
| 1430 | return mCurrentUxRestrictions.isRequiresDistractionOptimization(); |
| 1431 | } |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1432 | } |
Yan Zhu | 211b1fe | 2019-11-06 15:54:19 -0800 | [diff] [blame] | 1433 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 1434 | // If restriction information is still not available (could happen during bootup), |
| 1435 | // return not restricted. This maintains parity with previous implementation but needs |
| 1436 | // a revisit as we test more. |
| 1437 | return false; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 1438 | } |
| 1439 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1440 | |
| 1441 | /** |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1442 | * Listens to the package install/uninstall events to know when to initiate parsing |
| 1443 | * installed packages. |
| 1444 | */ |
| 1445 | private class PackageParsingEventReceiver extends BroadcastReceiver { |
| 1446 | private static final long PACKAGE_PARSING_DELAY_MS = 500; |
| 1447 | |
| 1448 | @Override |
| 1449 | public void onReceive(Context context, Intent intent) { |
| 1450 | if (intent == null || intent.getAction() == null) { |
| 1451 | return; |
| 1452 | } |
| 1453 | if (DBG_POLICY_CHECK) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1454 | Slog.d(CarLog.TAG_PACKAGE, |
Ram Periathiruvadi | e6e2ac3 | 2018-05-02 14:57:46 -0700 | [diff] [blame] | 1455 | "PackageParsingEventReceiver Received " + intent.getAction()); |
| 1456 | } |
| 1457 | String action = intent.getAction(); |
| 1458 | if (isPackageManagerAction(action)) { |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1459 | // send a delayed message so if we received multiple related intents, we parse |
| 1460 | // only once. |
| 1461 | logEventChange(intent); |
| 1462 | mHandler.requestParsingInstalledPkgs(PACKAGE_PARSING_DELAY_MS); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | private boolean isPackageManagerAction(String action) { |
Jordan Jozwiak | 12d59e0 | 2019-06-20 18:10:21 -0700 | [diff] [blame] | 1467 | return mPackageManagerActions.contains(action); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * Convenience log function to log what changed. Logs only when more debug logs |
| 1472 | * are needed - DBG_POLICY_CHECK needs to be true |
| 1473 | */ |
| 1474 | private void logEventChange(Intent intent) { |
| 1475 | if (!DBG_POLICY_CHECK || intent == null) { |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | String packageName = intent.getData().getSchemeSpecificPart(); |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1480 | Slog.d(CarLog.TAG_PACKAGE, "Pkg Changed:" + packageName); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1481 | String action = intent.getAction(); |
| 1482 | if (action == null) { |
| 1483 | return; |
| 1484 | } |
| 1485 | if (action.equals(Intent.ACTION_PACKAGE_CHANGED)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1486 | Slog.d(CarLog.TAG_PACKAGE, "Changed components"); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1487 | String[] cc = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST); |
| 1488 | if (cc != null) { |
| 1489 | for (String c : cc) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1490 | Slog.d(CarLog.TAG_PACKAGE, c); |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| 1494 | || action.equals(Intent.ACTION_PACKAGE_ADDED)) { |
Eric Jeong | 47b1e24 | 2020-12-21 13:41:36 -0800 | [diff] [blame^] | 1495 | Slog.d(CarLog.TAG_PACKAGE, action + " Replacing?: " + intent.getBooleanExtra( |
Ram Periathiruvadi | 295954d | 2018-04-10 18:27:28 -0700 | [diff] [blame] | 1496 | Intent.EXTRA_REPLACING, false)); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1500 | } |