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