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; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.HandlerThread; |
| 44 | import android.os.Looper; |
| 45 | import android.os.Message; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 46 | import android.util.ArraySet; |
| 47 | import android.util.Log; |
| 48 | import android.util.Pair; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame^] | 49 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 50 | import com.android.car.CarLog; |
| 51 | import com.android.car.CarServiceBase; |
| 52 | import com.android.car.CarServiceUtils; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 53 | import com.android.car.CarUxRestrictionsManagerService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 54 | import com.android.car.R; |
| 55 | import com.android.car.SystemActivityMonitoringService; |
| 56 | import com.android.car.SystemActivityMonitoringService.TopTaskInfoContainer; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 57 | import com.android.internal.annotations.GuardedBy; |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame^] | 58 | import com.android.internal.annotations.VisibleForTesting; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 59 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 60 | import java.io.PrintWriter; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 61 | import java.util.ArrayList; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 62 | import java.util.HashMap; |
| 63 | import java.util.LinkedList; |
| 64 | import java.util.List; |
| 65 | import java.util.Map.Entry; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 66 | import java.util.Set; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 67 | |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 68 | //TODO monitor app installing and refresh policy, bug: 31970400 |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 69 | |
| 70 | public class CarPackageManagerService extends ICarPackageManager.Stub implements CarServiceBase { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 71 | private static final boolean DBG_POLICY_SET = false; |
| 72 | private static final boolean DBG_POLICY_CHECK = false; |
| 73 | private static final boolean DBG_POLICY_ENFORCEMENT = false; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 74 | |
| 75 | private final Context mContext; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 76 | private final SystemActivityMonitoringService mSystemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 77 | private final PackageManager mPackageManager; |
| 78 | |
| 79 | private final HandlerThread mHandlerThread; |
| 80 | private final PackageHandler mHandler; |
| 81 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 82 | private String mDefaultActivityWhitelist; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Hold policy set from policy service or client. |
| 85 | * Key: packageName of policy service |
| 86 | */ |
| 87 | @GuardedBy("this") |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 88 | private final HashMap<String, ClientPolicy> mClientPolicies = new HashMap<>(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 89 | @GuardedBy("this") |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 90 | private HashMap<String, AppBlockingPackageInfoWrapper> mActivityWhitelists = new HashMap<>(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 91 | @GuardedBy("this") |
| 92 | private LinkedList<AppBlockingPolicyProxy> mProxies; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 93 | |
| 94 | @GuardedBy("this") |
| 95 | private final LinkedList<CarAppBlockingPolicy> mWaitingPolicies = new LinkedList<>(); |
| 96 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 97 | private final CarUxRestrictionsManagerService mCarUxRestrictionsService; |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 98 | private boolean mEnableActivityBlocking; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 99 | private final ComponentName mActivityBlockingActivity; |
| 100 | |
| 101 | private final ActivityLaunchListener mActivityLaunchListener = new ActivityLaunchListener(); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 102 | private final UxRestrictionsListener mUxRestrictionsListener; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 103 | private final BootPhaseEventReceiver mBootPhaseEventReceiver = new BootPhaseEventReceiver(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 104 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 105 | public CarPackageManagerService(Context context, |
| 106 | CarUxRestrictionsManagerService uxRestrictionsService, |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 107 | SystemActivityMonitoringService systemActivityMonitoringService) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 108 | mContext = context; |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 109 | mCarUxRestrictionsService = uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 110 | mSystemActivityMonitoringService = systemActivityMonitoringService; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 111 | mPackageManager = mContext.getPackageManager(); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 112 | mUxRestrictionsListener = new UxRestrictionsListener(uxRestrictionsService); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 113 | mHandlerThread = new HandlerThread(CarLog.TAG_PACKAGE); |
| 114 | mHandlerThread.start(); |
| 115 | mHandler = new PackageHandler(mHandlerThread.getLooper()); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 116 | Resources res = context.getResources(); |
| 117 | mEnableActivityBlocking = res.getBoolean(R.bool.enableActivityBlockingForSafety); |
| 118 | String blockingActivity = res.getString(R.string.activityBlockingActivity); |
| 119 | mActivityBlockingActivity = ComponentName.unflattenFromString(blockingActivity); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 123 | public void setAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
| 124 | if (DBG_POLICY_SET) { |
| 125 | Log.i(CarLog.TAG_PACKAGE, "policy setting from binder call, client:" + packageName); |
| 126 | } |
| 127 | doSetAppBlockingPolicy(packageName, policy, flags, true /*setNow*/); |
| 128 | } |
| 129 | |
| 130 | private void doSetAppBlockingPolicy(String packageName, CarAppBlockingPolicy policy, int flags, |
| 131 | boolean setNow) { |
| 132 | if (mContext.checkCallingOrSelfPermission(Car.PERMISSION_CONTROL_APP_BLOCKING) |
| 133 | != PackageManager.PERMISSION_GRANTED) { |
| 134 | throw new SecurityException( |
| 135 | "requires permission " + Car.PERMISSION_CONTROL_APP_BLOCKING); |
| 136 | } |
Enrico Granata | 3da3c61 | 2017-01-20 15:24:30 -0800 | [diff] [blame] | 137 | CarServiceUtils.assertPackageName(mContext, packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 138 | if (policy == null) { |
| 139 | throw new IllegalArgumentException("policy cannot be null"); |
| 140 | } |
| 141 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0 && |
| 142 | (flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
| 143 | throw new IllegalArgumentException( |
| 144 | "Cannot set both FLAG_SET_POLICY_ADD and FLAG_SET_POLICY_REMOVE flag"); |
| 145 | } |
| 146 | mHandler.requestUpdatingPolicy(packageName, policy, flags); |
| 147 | if (setNow) { |
| 148 | mHandler.requestPolicySetting(); |
| 149 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
| 150 | synchronized (policy) { |
| 151 | try { |
| 152 | policy.wait(); |
| 153 | } catch (InterruptedException e) { |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean isActivityAllowedWhileDriving(String packageName, String className) { |
| 162 | assertPackageAndClassName(packageName, className); |
| 163 | synchronized (this) { |
| 164 | if (DBG_POLICY_CHECK) { |
| 165 | Log.i(CarLog.TAG_PACKAGE, "isActivityAllowedWhileDriving" + |
| 166 | dumpPoliciesLocked(false)); |
| 167 | } |
| 168 | AppBlockingPackageInfo info = searchFromBlacklistsLocked(packageName); |
| 169 | if (info != null) { |
| 170 | return false; |
| 171 | } |
| 172 | return isActivityInWhitelistsLocked(packageName, className); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean isServiceAllowedWhileDriving(String packageName, String className) { |
| 178 | if (packageName == null) { |
| 179 | throw new IllegalArgumentException("Package name null"); |
| 180 | } |
| 181 | synchronized (this) { |
| 182 | if (DBG_POLICY_CHECK) { |
| 183 | Log.i(CarLog.TAG_PACKAGE, "isServiceAllowedWhileDriving" + |
| 184 | dumpPoliciesLocked(false)); |
| 185 | } |
| 186 | AppBlockingPackageInfo info = searchFromBlacklistsLocked(packageName); |
| 187 | if (info != null) { |
| 188 | return false; |
| 189 | } |
| 190 | info = searchFromWhitelistsLocked(packageName); |
| 191 | if (info != null) { |
| 192 | return true; |
| 193 | } |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 198 | @Override |
| 199 | public boolean isActivityBackedBySafeActivity(ComponentName activityName) { |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 200 | if (!mUxRestrictionsListener.isRestricted()) { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | StackInfo info = mSystemActivityMonitoringService.getFocusedStackForTopActivity( |
| 204 | activityName); |
| 205 | if (info == null) { // not top in focused stack |
| 206 | return true; |
| 207 | } |
| 208 | if (info.taskNames.length <= 1) { // nothing below this. |
| 209 | return false; |
| 210 | } |
| 211 | ComponentName activityBehind = ComponentName.unflattenFromString( |
| 212 | info.taskNames[info.taskNames.length - 2]); |
| 213 | return isActivityAllowedWhileDriving(activityBehind.getPackageName(), |
| 214 | activityBehind.getClassName()); |
| 215 | } |
| 216 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 217 | public Looper getLooper() { |
| 218 | return mHandlerThread.getLooper(); |
| 219 | } |
| 220 | |
| 221 | private void assertPackageAndClassName(String packageName, String className) { |
| 222 | if (packageName == null) { |
| 223 | throw new IllegalArgumentException("Package name null"); |
| 224 | } |
| 225 | if (className == null) { |
| 226 | throw new IllegalArgumentException("Class name null"); |
| 227 | } |
| 228 | } |
| 229 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 230 | @GuardedBy("this") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 231 | private AppBlockingPackageInfo searchFromBlacklistsLocked(String packageName) { |
| 232 | for (ClientPolicy policy : mClientPolicies.values()) { |
| 233 | AppBlockingPackageInfoWrapper wrapper = policy.blacklistsMap.get(packageName); |
| 234 | if (wrapper != null && wrapper.isMatching) { |
| 235 | return wrapper.info; |
| 236 | } |
| 237 | } |
| 238 | return null; |
| 239 | } |
| 240 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 241 | @GuardedBy("this") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 242 | private AppBlockingPackageInfo searchFromWhitelistsLocked(String packageName) { |
| 243 | for (ClientPolicy policy : mClientPolicies.values()) { |
| 244 | AppBlockingPackageInfoWrapper wrapper = policy.whitelistsMap.get(packageName); |
| 245 | if (wrapper != null && wrapper.isMatching) { |
| 246 | return wrapper.info; |
| 247 | } |
| 248 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 249 | AppBlockingPackageInfoWrapper wrapper = mActivityWhitelists.get(packageName); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 250 | return (wrapper != null) ? wrapper.info : null; |
| 251 | } |
| 252 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 253 | @GuardedBy("this") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 254 | private boolean isActivityInWhitelistsLocked(String packageName, String className) { |
| 255 | for (ClientPolicy policy : mClientPolicies.values()) { |
| 256 | if (isActivityInMapAndMatching(policy.whitelistsMap, packageName, className)) { |
| 257 | return true; |
| 258 | } |
| 259 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 260 | return isActivityInMapAndMatching(mActivityWhitelists, packageName, className); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | private boolean isActivityInMapAndMatching(HashMap<String, AppBlockingPackageInfoWrapper> map, |
| 264 | String packageName, String className) { |
| 265 | AppBlockingPackageInfoWrapper wrapper = map.get(packageName); |
| 266 | if (wrapper == null || !wrapper.isMatching) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 267 | if (DBG_POLICY_CHECK) { |
| 268 | Log.d(CarLog.TAG_PACKAGE, "Pkg not in whitelist:" + packageName); |
| 269 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | return wrapper.info.isActivityCovered(className); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void init() { |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 277 | synchronized (this) { |
| 278 | mHandler.requestInit(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 279 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void release() { |
| 284 | synchronized (this) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 285 | mHandler.requestRelease(); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 286 | // wait for release do be done. This guarantees that init is done. |
| 287 | try { |
| 288 | wait(); |
| 289 | } catch (InterruptedException e) { |
| 290 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 291 | mActivityWhitelists.clear(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 292 | mClientPolicies.clear(); |
| 293 | if (mProxies != null) { |
| 294 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 295 | proxy.disconnect(); |
| 296 | } |
| 297 | mProxies.clear(); |
| 298 | } |
| 299 | wakeupClientsWaitingForPolicySetitngLocked(); |
| 300 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 301 | mContext.unregisterReceiver(mBootPhaseEventReceiver); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 302 | mCarUxRestrictionsService.unregisterUxRestrictionsChangeListener(mUxRestrictionsListener); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 303 | mSystemActivityMonitoringService.registerActivityLaunchListener(null); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // run from HandlerThread |
| 307 | private void doHandleInit() { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 308 | IntentFilter bootIntent = new IntentFilter(); |
| 309 | bootIntent.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED); |
| 310 | mContext.registerReceiver(mBootPhaseEventReceiver, bootIntent); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 311 | try { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 312 | mCarUxRestrictionsService.registerUxRestrictionsChangeListener(mUxRestrictionsListener); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 313 | } catch (IllegalArgumentException e) { |
| 314 | // can happen while mocking is going on while init is still done. |
| 315 | Log.w(CarLog.TAG_PACKAGE, "sensor subscription failed", e); |
| 316 | return; |
| 317 | } |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 318 | mSystemActivityMonitoringService.registerActivityLaunchListener( |
| 319 | mActivityLaunchListener); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | private void doParseInstalledPackages() { |
| 323 | startAppBlockingPolicies(); |
| 324 | generateActivityWhitelist(); |
| 325 | mUxRestrictionsListener.checkIfTopActivityNeedsBlocking(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 328 | private synchronized void doHandleRelease() { |
| 329 | notifyAll(); |
| 330 | } |
| 331 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 332 | @GuardedBy("this") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 333 | private void wakeupClientsWaitingForPolicySetitngLocked() { |
| 334 | for (CarAppBlockingPolicy waitingPolicy : mWaitingPolicies) { |
| 335 | synchronized (waitingPolicy) { |
| 336 | waitingPolicy.notifyAll(); |
| 337 | } |
| 338 | } |
| 339 | mWaitingPolicies.clear(); |
| 340 | } |
| 341 | |
| 342 | private void doSetPolicy() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 343 | synchronized (this) { |
| 344 | wakeupClientsWaitingForPolicySetitngLocked(); |
| 345 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 346 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private void doUpdatePolicy(String packageName, CarAppBlockingPolicy policy, int flags) { |
| 350 | if (DBG_POLICY_SET) { |
| 351 | Log.i(CarLog.TAG_PACKAGE, "setting policy from:" + packageName + ",policy:" + policy + |
| 352 | ",flags:0x" + Integer.toHexString(flags)); |
| 353 | } |
| 354 | AppBlockingPackageInfoWrapper[] blacklistWrapper = verifyList(policy.blacklists); |
| 355 | AppBlockingPackageInfoWrapper[] whitelistWrapper = verifyList(policy.whitelists); |
| 356 | synchronized (this) { |
| 357 | ClientPolicy clientPolicy = mClientPolicies.get(packageName); |
| 358 | if (clientPolicy == null) { |
| 359 | clientPolicy = new ClientPolicy(); |
| 360 | mClientPolicies.put(packageName, clientPolicy); |
| 361 | } |
| 362 | if ((flags & CarPackageManager.FLAG_SET_POLICY_ADD) != 0) { |
| 363 | clientPolicy.addToBlacklists(blacklistWrapper); |
| 364 | clientPolicy.addToWhitelists(whitelistWrapper); |
| 365 | } else if ((flags & CarPackageManager.FLAG_SET_POLICY_REMOVE) != 0) { |
| 366 | clientPolicy.removeBlacklists(blacklistWrapper); |
| 367 | clientPolicy.removeWhitelists(whitelistWrapper); |
| 368 | } else { //replace. |
| 369 | clientPolicy.replaceBlacklists(blacklistWrapper); |
| 370 | clientPolicy.replaceWhitelists(whitelistWrapper); |
| 371 | } |
| 372 | if ((flags & CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE) != 0) { |
| 373 | mWaitingPolicies.add(policy); |
| 374 | } |
| 375 | if (DBG_POLICY_SET) { |
| 376 | Log.i(CarLog.TAG_PACKAGE, "policy set:" + dumpPoliciesLocked(false)); |
| 377 | } |
| 378 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 379 | blockTopActivitiesIfNecessary(); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | private AppBlockingPackageInfoWrapper[] verifyList(AppBlockingPackageInfo[] list) { |
| 383 | if (list == null) { |
| 384 | return null; |
| 385 | } |
| 386 | LinkedList<AppBlockingPackageInfoWrapper> wrappers = new LinkedList<>(); |
| 387 | for (int i = 0; i < list.length; i++) { |
| 388 | AppBlockingPackageInfo info = list[i]; |
| 389 | if (info == null) { |
| 390 | continue; |
| 391 | } |
| 392 | boolean isMatching = isInstalledPackageMatching(info); |
| 393 | wrappers.add(new AppBlockingPackageInfoWrapper(info, isMatching)); |
| 394 | } |
| 395 | return wrappers.toArray(new AppBlockingPackageInfoWrapper[wrappers.size()]); |
| 396 | } |
| 397 | |
| 398 | boolean isInstalledPackageMatching(AppBlockingPackageInfo info) { |
| 399 | PackageInfo packageInfo = null; |
| 400 | try { |
| 401 | packageInfo = mPackageManager.getPackageInfo(info.packageName, |
| 402 | PackageManager.GET_SIGNATURES); |
| 403 | } catch (NameNotFoundException e) { |
| 404 | return false; |
| 405 | } |
| 406 | if (packageInfo == null) { |
| 407 | return false; |
| 408 | } |
| 409 | // if it is system app and client specified the flag, do not check signature |
| 410 | if ((info.flags & AppBlockingPackageInfo.FLAG_SYSTEM_APP) == 0 || |
| 411 | (!packageInfo.applicationInfo.isSystemApp() && |
| 412 | !packageInfo.applicationInfo.isUpdatedSystemApp())) { |
| 413 | Signature[] signatires = packageInfo.signatures; |
| 414 | if (!isAnySignatureMatching(signatires, info.signatures)) { |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | int version = packageInfo.versionCode; |
| 419 | if (info.minRevisionCode == 0) { |
| 420 | if (info.maxRevisionCode == 0) { // all versions |
| 421 | return true; |
| 422 | } else { // only max version matters |
| 423 | return info.maxRevisionCode > version; |
| 424 | } |
| 425 | } else { // min version matters |
| 426 | if (info.maxRevisionCode == 0) { |
| 427 | return info.minRevisionCode < version; |
| 428 | } else { |
| 429 | return (info.minRevisionCode < version) && (info.maxRevisionCode > version); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Any signature from policy matching with package's signatures is treated as matching. |
| 436 | */ |
| 437 | boolean isAnySignatureMatching(Signature[] fromPackage, Signature[] fromPolicy) { |
| 438 | if (fromPackage == null) { |
| 439 | return false; |
| 440 | } |
| 441 | if (fromPolicy == null) { |
| 442 | return false; |
| 443 | } |
| 444 | ArraySet<Signature> setFromPackage = new ArraySet<Signature>(); |
| 445 | for (Signature sig : fromPackage) { |
| 446 | setFromPackage.add(sig); |
| 447 | } |
| 448 | for (Signature sig : fromPolicy) { |
| 449 | if (setFromPackage.contains(sig)) { |
| 450 | return true; |
| 451 | } |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 456 | /** |
| 457 | * Return list of whitelist including default activity. Key is package name while |
| 458 | * value is list of activities. If list is empty, whole activities in the package |
| 459 | * are whitelisted. |
| 460 | * @return |
| 461 | */ |
| 462 | private HashMap<String, Set<String>> parseConfigWhitelist() { |
| 463 | HashMap<String, Set<String>> packageToActivityMap = new HashMap<>(); |
| 464 | Set<String> defaultActivity = new ArraySet<>(); |
| 465 | defaultActivity.add(mActivityBlockingActivity.getClassName()); |
| 466 | packageToActivityMap.put(mActivityBlockingActivity.getPackageName(), defaultActivity); |
| 467 | Resources res = mContext.getResources(); |
| 468 | String whitelist = res.getString(R.string.defauiltActivityWhitelist); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 469 | mDefaultActivityWhitelist = whitelist; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 470 | String[] entries = whitelist.split(","); |
| 471 | for (String entry : entries) { |
| 472 | String[] packageActivityPair = entry.split("/"); |
| 473 | Set<String> activities = packageToActivityMap.get(packageActivityPair[0]); |
| 474 | boolean newPackage = false; |
| 475 | if (activities == null) { |
| 476 | activities = new ArraySet<>(); |
| 477 | newPackage = true; |
| 478 | packageToActivityMap.put(packageActivityPair[0], activities); |
| 479 | } |
| 480 | if (packageActivityPair.length == 1) { // whole package |
| 481 | activities.clear(); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 482 | } else if (packageActivityPair.length == 2) { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 483 | // add class name only when the whole package is not whitelisted. |
| 484 | if (newPackage || (activities.size() > 0)) { |
| 485 | activities.add(packageActivityPair[1]); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | return packageToActivityMap; |
| 490 | } |
| 491 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 492 | private void generateActivityWhitelist() { |
| 493 | HashMap<String, AppBlockingPackageInfoWrapper> activityWhitelist = new HashMap<>(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 494 | HashMap<String, Set<String>> configWhitelist = parseConfigWhitelist(); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 495 | |
| 496 | List<PackageInfo> packages = mPackageManager.getInstalledPackages( |
| 497 | PackageManager.GET_SIGNATURES | PackageManager.GET_ACTIVITIES |
| 498 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 499 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 500 | for (PackageInfo info : packages) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 501 | if (info.applicationInfo == null) { |
| 502 | continue; |
| 503 | } |
| 504 | |
| 505 | int flags = 0; |
| 506 | String[] activities = null; |
| 507 | |
| 508 | if (info.applicationInfo.isSystemApp() |
| 509 | || info.applicationInfo.isUpdatedSystemApp()) { |
| 510 | flags = AppBlockingPackageInfo.FLAG_SYSTEM_APP; |
| 511 | } |
| 512 | |
| 513 | /* 1. Check if all or some of this app is in the <defauiltActivityWhitelist> |
| 514 | in config.xml */ |
| 515 | Set<String> configActivitiesForPackage = configWhitelist.get(info.packageName); |
| 516 | if (configActivitiesForPackage != null) { |
| 517 | if (DBG_POLICY_CHECK) { |
| 518 | Log.d(CarLog.TAG_PACKAGE, info.packageName + " whitelisted"); |
| 519 | } |
| 520 | if (configActivitiesForPackage.size() == 0) { |
| 521 | // Whole Pkg has been whitelisted |
| 522 | flags |= AppBlockingPackageInfo.FLAG_WHOLE_ACTIVITY; |
| 523 | // Add all activities to the whitelist |
| 524 | activities = getActivitiesInPackage(info); |
| 525 | if (activities == null && DBG_POLICY_CHECK) { |
| 526 | Log.d(CarLog.TAG_PACKAGE, info.packageName + ": Activities null"); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 527 | } |
| 528 | } else { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 529 | if (DBG_POLICY_CHECK) { |
| 530 | Log.d(CarLog.TAG_PACKAGE, "Partially Whitelisted. WL Activities:"); |
| 531 | for (String a : configActivitiesForPackage) { |
| 532 | Log.d(CarLog.TAG_PACKAGE, a); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 535 | activities = configActivitiesForPackage.toArray( |
| 536 | new String[configActivitiesForPackage.size()]); |
| 537 | } |
| 538 | } else { |
| 539 | /* 2. If app is not listed in the config.xml check their Manifest meta-data to |
| 540 | see if they have any Distraction Optimized(DO) activities */ |
| 541 | try { |
| 542 | activities = CarAppMetadataReader.findDistractionOptimizedActivities( |
| 543 | mContext, |
| 544 | info.packageName); |
| 545 | } catch (NameNotFoundException e) { |
| 546 | Log.w(CarLog.TAG_PACKAGE, "Error reading metadata: " + info.packageName); |
| 547 | continue; |
| 548 | } |
| 549 | if (activities != null) { |
| 550 | // Some of the activities in this app are Distraction Optimized. |
| 551 | if (DBG_POLICY_CHECK) { |
| 552 | for (String activity : activities) { |
| 553 | Log.d(CarLog.TAG_PACKAGE, |
| 554 | "adding " + activity + " from " + info.packageName |
| 555 | + " to whitelist"); |
| 556 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 557 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 558 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 559 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 560 | // Nothing to add to whitelist |
| 561 | if (activities == null) { |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | Signature[] signatures; |
| 566 | signatures = info.signatures; |
| 567 | AppBlockingPackageInfo appBlockingInfo = new AppBlockingPackageInfo( |
| 568 | info.packageName, 0, 0, flags, signatures, activities); |
| 569 | AppBlockingPackageInfoWrapper wrapper = new AppBlockingPackageInfoWrapper( |
| 570 | appBlockingInfo, true); |
| 571 | activityWhitelist.put(info.packageName, wrapper); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 572 | } |
| 573 | synchronized (this) { |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 574 | mActivityWhitelists.putAll(activityWhitelist); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 578 | @Nullable |
| 579 | private String[] getActivitiesInPackage(PackageInfo info) { |
| 580 | if (info == null || info.activities == null) { |
| 581 | return null; |
| 582 | } |
| 583 | List<String> activityList = new ArrayList<>(); |
| 584 | for (ActivityInfo aInfo : info.activities) { |
| 585 | activityList.add(aInfo.name); |
| 586 | } |
| 587 | return activityList.toArray(new String[activityList.size()]); |
| 588 | } |
| 589 | |
Ram Periathiruvadi | 8ef1a20 | 2018-03-16 17:49:21 -0700 | [diff] [blame^] | 590 | /** |
| 591 | * Checks if there are any {@link CarAppBlockingPolicyService} and creates a proxy to |
| 592 | * bind to them and retrieve the {@link CarAppBlockingPolicy} |
| 593 | */ |
| 594 | @VisibleForTesting |
| 595 | public void startAppBlockingPolicies() { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 596 | Intent policyIntent = new Intent(); |
| 597 | policyIntent.setAction(CarAppBlockingPolicyService.SERVICE_INTERFACE); |
| 598 | List<ResolveInfo> policyInfos = mPackageManager.queryIntentServices(policyIntent, 0); |
| 599 | if (policyInfos == null) { //no need to wait for service binding and retrieval. |
| 600 | mHandler.requestPolicySetting(); |
| 601 | return; |
| 602 | } |
| 603 | LinkedList<AppBlockingPolicyProxy> proxies = new LinkedList<>(); |
| 604 | for (ResolveInfo resolveInfo : policyInfos) { |
| 605 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 606 | if (serviceInfo == null) { |
| 607 | continue; |
| 608 | } |
| 609 | if (serviceInfo.isEnabled()) { |
| 610 | if (mPackageManager.checkPermission(Car.PERMISSION_CONTROL_APP_BLOCKING, |
| 611 | serviceInfo.packageName) != PackageManager.PERMISSION_GRANTED) { |
| 612 | continue; |
| 613 | } |
| 614 | Log.i(CarLog.TAG_PACKAGE, "found policy holding service:" + serviceInfo); |
| 615 | AppBlockingPolicyProxy proxy = new AppBlockingPolicyProxy(this, mContext, |
| 616 | serviceInfo); |
| 617 | proxy.connect(); |
| 618 | proxies.add(proxy); |
| 619 | } |
| 620 | } |
| 621 | synchronized (this) { |
| 622 | mProxies = proxies; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | public void onPolicyConnectionAndSet(AppBlockingPolicyProxy proxy, |
| 627 | CarAppBlockingPolicy policy) { |
| 628 | doHandlePolicyConnection(proxy, policy); |
| 629 | } |
| 630 | |
| 631 | public void onPolicyConnectionFailure(AppBlockingPolicyProxy proxy) { |
| 632 | doHandlePolicyConnection(proxy, null); |
| 633 | } |
| 634 | |
| 635 | private void doHandlePolicyConnection(AppBlockingPolicyProxy proxy, |
| 636 | CarAppBlockingPolicy policy) { |
| 637 | boolean shouldSetPolicy = false; |
| 638 | synchronized (this) { |
| 639 | if (mProxies == null) { |
| 640 | proxy.disconnect(); |
| 641 | return; |
| 642 | } |
| 643 | mProxies.remove(proxy); |
| 644 | if (mProxies.size() == 0) { |
| 645 | shouldSetPolicy = true; |
| 646 | mProxies = null; |
| 647 | } |
| 648 | } |
| 649 | try { |
| 650 | if (policy != null) { |
| 651 | if (DBG_POLICY_SET) { |
| 652 | Log.i(CarLog.TAG_PACKAGE, "policy setting from policy service:" + |
| 653 | proxy.getPackageName()); |
| 654 | } |
| 655 | doSetAppBlockingPolicy(proxy.getPackageName(), policy, 0, false /*setNow*/); |
| 656 | } |
| 657 | } finally { |
| 658 | proxy.disconnect(); |
| 659 | if (shouldSetPolicy) { |
| 660 | mHandler.requestPolicySetting(); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | @Override |
| 666 | public void dump(PrintWriter writer) { |
| 667 | synchronized (this) { |
| 668 | writer.println("*PackageManagementService*"); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 669 | writer.println("mEnableActivityBlocking:" + mEnableActivityBlocking); |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 670 | writer.println("ActivityRestricted:" + mUxRestrictionsListener.isRestricted()); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 671 | writer.print(dumpPoliciesLocked(true)); |
| 672 | } |
| 673 | } |
| 674 | |
Andreas Gampe | 985ca2f | 2018-02-09 12:57:54 -0800 | [diff] [blame] | 675 | @GuardedBy("this") |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 676 | private String dumpPoliciesLocked(boolean dumpAll) { |
| 677 | StringBuilder sb = new StringBuilder(); |
| 678 | if (dumpAll) { |
| 679 | sb.append("**System white list**\n"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 680 | for (AppBlockingPackageInfoWrapper wrapper : mActivityWhitelists.values()) { |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 681 | sb.append(wrapper.toString() + "\n"); |
| 682 | } |
| 683 | } |
| 684 | sb.append("**Client Policies**\n"); |
| 685 | for (Entry<String, ClientPolicy> entry : mClientPolicies.entrySet()) { |
| 686 | sb.append("Client:" + entry.getKey() + "\n"); |
| 687 | sb.append(" whitelists:\n"); |
| 688 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().whitelistsMap.values()) { |
| 689 | sb.append(wrapper.toString() + "\n"); |
| 690 | } |
| 691 | sb.append(" blacklists:\n"); |
| 692 | for (AppBlockingPackageInfoWrapper wrapper : entry.getValue().blacklistsMap.values()) { |
| 693 | sb.append(wrapper.toString() + "\n"); |
| 694 | } |
| 695 | } |
| 696 | sb.append("**Unprocessed policy services**\n"); |
| 697 | if (mProxies != null) { |
| 698 | for (AppBlockingPolicyProxy proxy : mProxies) { |
| 699 | sb.append(proxy.toString() + "\n"); |
| 700 | } |
| 701 | } |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 702 | sb.append("**Default Whitelist string**\n"); |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 703 | sb.append(mDefaultActivityWhitelist + "\n"); |
Keun-young Park | 9896081 | 2016-10-04 12:50:54 -0700 | [diff] [blame] | 704 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 705 | return sb.toString(); |
| 706 | } |
| 707 | |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 708 | private void blockTopActivityIfNecessary(TopTaskInfoContainer topTask) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 709 | boolean restricted = mUxRestrictionsListener.isRestricted(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 710 | if (!restricted) { |
| 711 | return; |
| 712 | } |
| 713 | doBlockTopActivityIfNotAllowed(topTask); |
| 714 | } |
| 715 | |
| 716 | private void doBlockTopActivityIfNotAllowed(TopTaskInfoContainer topTask) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 717 | if (topTask.topActivity == null) { |
| 718 | return; |
| 719 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 720 | boolean allowed = isActivityAllowedWhileDriving( |
| 721 | topTask.topActivity.getPackageName(), |
| 722 | topTask.topActivity.getClassName()); |
| 723 | if (DBG_POLICY_ENFORCEMENT) { |
| 724 | Log.i(CarLog.TAG_PACKAGE, "new activity:" + topTask.toString() + " allowed:" + allowed); |
| 725 | } |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 726 | if (allowed) { |
| 727 | return; |
| 728 | } |
| 729 | synchronized(this) { |
| 730 | if (!mEnableActivityBlocking) { |
| 731 | Log.d(CarLog.TAG_PACKAGE, "Current activity " + topTask.topActivity + |
| 732 | " not allowed, blocking disabled. Number of tasks in stack:" |
| 733 | + topTask.stackInfo.taskIds.length); |
| 734 | return; |
| 735 | } |
| 736 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 737 | if (DBG_POLICY_CHECK) { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 738 | Log.i(CarLog.TAG_PACKAGE, "Current activity " + topTask.topActivity + |
| 739 | " not allowed, will block, number of tasks in stack:" + |
| 740 | topTask.stackInfo.taskIds.length); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 741 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 742 | Intent newActivityIntent = new Intent(); |
| 743 | newActivityIntent.setComponent(mActivityBlockingActivity); |
| 744 | newActivityIntent.putExtra( |
| 745 | ActivityBlockingActivity.INTENT_KEY_BLOCKED_ACTIVITY, |
| 746 | topTask.topActivity.flattenToString()); |
| 747 | mSystemActivityMonitoringService.blockActivity(topTask, newActivityIntent); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | private void blockTopActivitiesIfNecessary() { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 751 | boolean restricted = mUxRestrictionsListener.isRestricted(); |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 752 | if (!restricted) { |
| 753 | return; |
| 754 | } |
| 755 | List<TopTaskInfoContainer> topTasks = mSystemActivityMonitoringService.getTopTasks(); |
| 756 | for (TopTaskInfoContainer topTask : topTasks) { |
| 757 | doBlockTopActivityIfNotAllowed(topTask); |
| 758 | } |
| 759 | } |
| 760 | |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 761 | public synchronized void setEnableActivityBlocking(boolean enable) { |
| 762 | mEnableActivityBlocking = enable; |
| 763 | } |
| 764 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 765 | /** |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 766 | * Get the distraction optimized activities for the given package. |
| 767 | * |
| 768 | * @param pkgName Name of the package |
| 769 | * @return Array of the distraction optimized activities in the package |
| 770 | */ |
| 771 | @Nullable |
| 772 | public String[] getDistractionOptimizedActivities(String pkgName) { |
| 773 | try { |
| 774 | return CarAppMetadataReader.findDistractionOptimizedActivities(mContext, pkgName); |
| 775 | } catch (NameNotFoundException e) { |
| 776 | return null; |
| 777 | } |
| 778 | } |
| 779 | /** |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 780 | * Reading policy and setting policy can take time. Run it in a separate handler thread. |
| 781 | */ |
| 782 | private class PackageHandler extends Handler { |
| 783 | private final int MSG_INIT = 0; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 784 | private final int MSG_PARSE_PKG = 1; |
| 785 | private final int MSG_SET_POLICY = 2; |
| 786 | private final int MSG_UPDATE_POLICY = 3; |
| 787 | private final int MSG_RELEASE = 4; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 788 | |
| 789 | private PackageHandler(Looper looper) { |
| 790 | super(looper); |
| 791 | } |
| 792 | |
| 793 | private void requestInit() { |
| 794 | Message msg = obtainMessage(MSG_INIT); |
| 795 | sendMessage(msg); |
| 796 | } |
| 797 | |
| 798 | private void requestRelease() { |
| 799 | removeMessages(MSG_INIT); |
| 800 | removeMessages(MSG_SET_POLICY); |
| 801 | removeMessages(MSG_UPDATE_POLICY); |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 802 | Message msg = obtainMessage(MSG_RELEASE); |
| 803 | sendMessage(msg); |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | private void requestPolicySetting() { |
| 807 | Message msg = obtainMessage(MSG_SET_POLICY); |
| 808 | sendMessage(msg); |
| 809 | } |
| 810 | |
| 811 | private void requestUpdatingPolicy(String packageName, CarAppBlockingPolicy policy, |
| 812 | int flags) { |
| 813 | Pair<String, CarAppBlockingPolicy> pair = new Pair<>(packageName, policy); |
| 814 | Message msg = obtainMessage(MSG_UPDATE_POLICY, flags, 0, pair); |
| 815 | sendMessage(msg); |
| 816 | } |
| 817 | |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 818 | private void requestParsingInstalledPkgs() { |
| 819 | Message msg = obtainMessage(MSG_PARSE_PKG); |
| 820 | sendMessage(msg); |
| 821 | } |
| 822 | |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 823 | @Override |
| 824 | public void handleMessage(Message msg) { |
| 825 | switch (msg.what) { |
| 826 | case MSG_INIT: |
| 827 | doHandleInit(); |
| 828 | break; |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 829 | case MSG_PARSE_PKG: |
| 830 | doParseInstalledPackages(); |
| 831 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 832 | case MSG_SET_POLICY: |
| 833 | doSetPolicy(); |
| 834 | break; |
| 835 | case MSG_UPDATE_POLICY: |
| 836 | Pair<String, CarAppBlockingPolicy> pair = |
| 837 | (Pair<String, CarAppBlockingPolicy>) msg.obj; |
| 838 | doUpdatePolicy(pair.first, pair.second, msg.arg1); |
| 839 | break; |
Keun-young Park | 6dcc50b | 2016-10-10 19:01:11 -0700 | [diff] [blame] | 840 | case MSG_RELEASE: |
| 841 | doHandleRelease(); |
| 842 | break; |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | private static class AppBlockingPackageInfoWrapper { |
| 848 | private final AppBlockingPackageInfo info; |
| 849 | /** |
| 850 | * Whether the current info is matching with the target package in system. Mismatch can |
| 851 | * happen for version out of range or signature mismatch. |
| 852 | */ |
| 853 | private boolean isMatching; |
| 854 | |
| 855 | private AppBlockingPackageInfoWrapper(AppBlockingPackageInfo info, boolean isMatching) { |
| 856 | this.info =info; |
| 857 | this.isMatching = isMatching; |
| 858 | } |
| 859 | |
| 860 | @Override |
| 861 | public String toString() { |
| 862 | return "AppBlockingPackageInfoWrapper [info=" + info + ", isMatching=" + isMatching + |
| 863 | "]"; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Client policy holder per each client. Should be accessed with CarpackageManagerService.this |
| 869 | * held. |
| 870 | */ |
| 871 | private static class ClientPolicy { |
| 872 | private final HashMap<String, AppBlockingPackageInfoWrapper> whitelistsMap = |
| 873 | new HashMap<>(); |
| 874 | private final HashMap<String, AppBlockingPackageInfoWrapper> blacklistsMap = |
| 875 | new HashMap<>(); |
| 876 | |
| 877 | private void replaceWhitelists(AppBlockingPackageInfoWrapper[] whitelists) { |
| 878 | whitelistsMap.clear(); |
| 879 | addToWhitelists(whitelists); |
| 880 | } |
| 881 | |
| 882 | private void addToWhitelists(AppBlockingPackageInfoWrapper[] whitelists) { |
| 883 | if (whitelists == null) { |
| 884 | return; |
| 885 | } |
| 886 | for (AppBlockingPackageInfoWrapper wrapper : whitelists) { |
| 887 | if (wrapper != null) { |
| 888 | whitelistsMap.put(wrapper.info.packageName, wrapper); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | private void removeWhitelists(AppBlockingPackageInfoWrapper[] whitelists) { |
| 894 | if (whitelists == null) { |
| 895 | return; |
| 896 | } |
| 897 | for (AppBlockingPackageInfoWrapper wrapper : whitelists) { |
| 898 | if (wrapper != null) { |
| 899 | whitelistsMap.remove(wrapper.info.packageName); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | private void replaceBlacklists(AppBlockingPackageInfoWrapper[] blacklists) { |
| 905 | blacklistsMap.clear(); |
| 906 | addToBlacklists(blacklists); |
| 907 | } |
| 908 | |
| 909 | private void addToBlacklists(AppBlockingPackageInfoWrapper[] blacklists) { |
| 910 | if (blacklists == null) { |
| 911 | return; |
| 912 | } |
| 913 | for (AppBlockingPackageInfoWrapper wrapper : blacklists) { |
| 914 | if (wrapper != null) { |
| 915 | blacklistsMap.put(wrapper.info.packageName, wrapper); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | private void removeBlacklists(AppBlockingPackageInfoWrapper[] blacklists) { |
| 921 | if (blacklists == null) { |
| 922 | return; |
| 923 | } |
| 924 | for (AppBlockingPackageInfoWrapper wrapper : blacklists) { |
| 925 | if (wrapper != null) { |
| 926 | blacklistsMap.remove(wrapper.info.packageName); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 931 | |
| 932 | private class ActivityLaunchListener |
| 933 | implements SystemActivityMonitoringService.ActivityLaunchListener { |
| 934 | @Override |
| 935 | public void onActivityLaunch(TopTaskInfoContainer topTask) { |
| 936 | blockTopActivityIfNecessary(topTask); |
| 937 | } |
| 938 | } |
| 939 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 940 | /** |
| 941 | * Listens to the UX restrictions from {@link CarUxRestrictionsManagerService} and initiates |
| 942 | * checking if the foreground Activity should be blocked. |
| 943 | */ |
| 944 | private class UxRestrictionsListener extends ICarUxRestrictionsChangeListener.Stub { |
| 945 | @GuardedBy("this") |
| 946 | @Nullable |
| 947 | private CarUxRestrictions mCurrentUxRestrictions; |
| 948 | private final CarUxRestrictionsManagerService uxRestrictionsService; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 949 | |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 950 | public UxRestrictionsListener(CarUxRestrictionsManagerService service) { |
| 951 | uxRestrictionsService = service; |
| 952 | mCurrentUxRestrictions = uxRestrictionsService.getCurrentUxRestrictions(); |
| 953 | } |
| 954 | |
| 955 | @Override |
| 956 | public void onUxRestrictionsChanged(CarUxRestrictions restrictions) { |
| 957 | if (DBG_POLICY_CHECK) { |
| 958 | Log.d(CarLog.TAG_PACKAGE, "Received uxr restrictions: " |
| 959 | + restrictions.isRequiresDistractionOptimization() |
| 960 | + " : " + restrictions.getActiveRestrictions()); |
| 961 | } |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 962 | synchronized (this) { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 963 | mCurrentUxRestrictions = new CarUxRestrictions(restrictions); |
| 964 | } |
| 965 | checkIfTopActivityNeedsBlocking(); |
| 966 | } |
| 967 | |
| 968 | private void checkIfTopActivityNeedsBlocking() { |
| 969 | boolean shouldCheck = false; |
| 970 | synchronized (this) { |
| 971 | if (mCurrentUxRestrictions != null |
| 972 | && mCurrentUxRestrictions.isRequiresDistractionOptimization()) { |
| 973 | shouldCheck = true; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 976 | if (DBG_POLICY_CHECK) { |
| 977 | Log.d(CarLog.TAG_PACKAGE, "block?: " + shouldCheck); |
| 978 | } |
| 979 | if (shouldCheck) { |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 980 | blockTopActivitiesIfNecessary(); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | private synchronized boolean isRestricted() { |
Ram Periathiruvadi | 2da6d0e | 2018-01-26 18:02:10 -0800 | [diff] [blame] | 985 | // if current restrictions is null, try querying the service, once. |
| 986 | if (mCurrentUxRestrictions == null) { |
| 987 | mCurrentUxRestrictions = uxRestrictionsService.getCurrentUxRestrictions(); |
| 988 | } |
| 989 | if (mCurrentUxRestrictions != null) { |
| 990 | return mCurrentUxRestrictions.isRequiresDistractionOptimization(); |
| 991 | } |
| 992 | // If restriction information is still not available (could happen during bootup), |
| 993 | // return not restricted. This maintains parity with previous implementation but needs |
| 994 | // a revisit as we test more. |
| 995 | return false; |
Keun-young Park | 4727da3 | 2016-05-31 10:00:51 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
Ram Periathiruvadi | 3838830 | 2018-02-22 16:42:49 -0800 | [diff] [blame] | 998 | |
| 999 | /** |
| 1000 | * Listens to the boot events to know when to initiate parsing installed packages. |
| 1001 | */ |
| 1002 | private class BootPhaseEventReceiver extends BroadcastReceiver { |
| 1003 | @Override |
| 1004 | public void onReceive(Context context, Intent intent) { |
| 1005 | if (DBG_POLICY_CHECK) { |
| 1006 | Log.d(CarLog.TAG_PACKAGE, "Received " + intent.getAction()); |
| 1007 | } |
| 1008 | if (intent != null && intent.getAction().equals(Intent.ACTION_LOCKED_BOOT_COMPLETED)) { |
| 1009 | mHandler.requestParsingInstalledPkgs(); |
| 1010 | } |
| 1011 | } |
| 1012 | } |
Keun-young Park | 4aeb4bf | 2015-12-08 18:31:33 -0800 | [diff] [blame] | 1013 | } |