blob: 18925278c56f6db8c711b77a0859b6c7f3c6490b [file] [log] [blame]
Keun-young Park4aeb4bf2015-12-08 18:31:33 -08001/*
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 Periathiruvadi38388302018-02-22 16:42:49 -080016
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080017package com.android.car.pm;
18
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080019import android.annotation.Nullable;
Keun-young Park4727da32016-05-31 10:00:51 -070020import android.app.ActivityManager.StackInfo;
Keun-young Parke54ac272016-02-16 19:02:18 -080021import android.car.Car;
22import android.car.content.pm.AppBlockingPackageInfo;
23import android.car.content.pm.CarAppBlockingPolicy;
24import android.car.content.pm.CarAppBlockingPolicyService;
25import android.car.content.pm.CarPackageManager;
26import android.car.content.pm.ICarPackageManager;
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080027import android.car.drivingstate.CarUxRestrictions;
28import android.car.drivingstate.ICarUxRestrictionsChangeListener;
Ram Periathiruvadi38388302018-02-22 16:42:49 -080029import android.content.BroadcastReceiver;
Keun-young Park4727da32016-05-31 10:00:51 -070030import android.content.ComponentName;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080031import android.content.Context;
32import android.content.Intent;
Ram Periathiruvadi38388302018-02-22 16:42:49 -080033import android.content.IntentFilter;
34import android.content.pm.ActivityInfo;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080035import android.content.pm.PackageInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080038import android.content.pm.ResolveInfo;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070039import android.content.pm.ServiceInfo;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080040import android.content.pm.Signature;
Keun-young Park4727da32016-05-31 10:00:51 -070041import android.content.res.Resources;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080042import android.os.Handler;
43import android.os.HandlerThread;
44import android.os.Looper;
45import android.os.Message;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080046import android.util.ArraySet;
47import android.util.Log;
48import android.util.Pair;
Ram Periathiruvadi8ef1a202018-03-16 17:49:21 -070049
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080050import com.android.car.CarLog;
51import com.android.car.CarServiceBase;
52import com.android.car.CarServiceUtils;
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080053import com.android.car.CarUxRestrictionsManagerService;
Keun-young Park4727da32016-05-31 10:00:51 -070054import com.android.car.R;
55import com.android.car.SystemActivityMonitoringService;
56import com.android.car.SystemActivityMonitoringService.TopTaskInfoContainer;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080057import com.android.internal.annotations.GuardedBy;
Ram Periathiruvadi8ef1a202018-03-16 17:49:21 -070058import com.android.internal.annotations.VisibleForTesting;
Ram Periathiruvadi38388302018-02-22 16:42:49 -080059
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080060import java.io.PrintWriter;
Ram Periathiruvadi38388302018-02-22 16:42:49 -080061import java.util.ArrayList;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080062import java.util.HashMap;
63import java.util.LinkedList;
64import java.util.List;
65import java.util.Map.Entry;
Keun-young Park4727da32016-05-31 10:00:51 -070066import java.util.Set;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080067
Keun-young Parkf9215202016-10-10 12:34:08 -070068//TODO monitor app installing and refresh policy, bug: 31970400
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080069
70public class CarPackageManagerService extends ICarPackageManager.Stub implements CarServiceBase {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080071 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 Park4aeb4bf2015-12-08 18:31:33 -080074
75 private final Context mContext;
Keun-young Park4727da32016-05-31 10:00:51 -070076 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080077 private final PackageManager mPackageManager;
78
79 private final HandlerThread mHandlerThread;
80 private final PackageHandler mHandler;
81
Ram Periathiruvadi38388302018-02-22 16:42:49 -080082 private String mDefaultActivityWhitelist;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080083 /**
84 * Hold policy set from policy service or client.
85 * Key: packageName of policy service
86 */
87 @GuardedBy("this")
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080088 private final HashMap<String, ClientPolicy> mClientPolicies = new HashMap<>();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080089 @GuardedBy("this")
Ram Periathiruvadi38388302018-02-22 16:42:49 -080090 private HashMap<String, AppBlockingPackageInfoWrapper> mActivityWhitelists = new HashMap<>();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080091 @GuardedBy("this")
92 private LinkedList<AppBlockingPolicyProxy> mProxies;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080093
94 @GuardedBy("this")
95 private final LinkedList<CarAppBlockingPolicy> mWaitingPolicies = new LinkedList<>();
96
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -080097 private final CarUxRestrictionsManagerService mCarUxRestrictionsService;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -080098 private boolean mEnableActivityBlocking;
Keun-young Park4727da32016-05-31 10:00:51 -070099 private final ComponentName mActivityBlockingActivity;
100
101 private final ActivityLaunchListener mActivityLaunchListener = new ActivityLaunchListener();
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800102 private final UxRestrictionsListener mUxRestrictionsListener;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800103 private final BootPhaseEventReceiver mBootPhaseEventReceiver = new BootPhaseEventReceiver();
Keun-young Park4727da32016-05-31 10:00:51 -0700104
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800105 public CarPackageManagerService(Context context,
106 CarUxRestrictionsManagerService uxRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700107 SystemActivityMonitoringService systemActivityMonitoringService) {
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800108 mContext = context;
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800109 mCarUxRestrictionsService = uxRestrictionsService;
Keun-young Park4727da32016-05-31 10:00:51 -0700110 mSystemActivityMonitoringService = systemActivityMonitoringService;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800111 mPackageManager = mContext.getPackageManager();
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800112 mUxRestrictionsListener = new UxRestrictionsListener(uxRestrictionsService);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800113 mHandlerThread = new HandlerThread(CarLog.TAG_PACKAGE);
114 mHandlerThread.start();
115 mHandler = new PackageHandler(mHandlerThread.getLooper());
Keun-young Park4727da32016-05-31 10:00:51 -0700116 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 Park4aeb4bf2015-12-08 18:31:33 -0800120 }
121
122 @Override
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800123 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 Granata3da3c612017-01-20 15:24:30 -0800137 CarServiceUtils.assertPackageName(mContext, packageName);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800138 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 Park4727da32016-05-31 10:00:51 -0700198 @Override
199 public boolean isActivityBackedBySafeActivity(ComponentName activityName) {
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800200 if (!mUxRestrictionsListener.isRestricted()) {
Keun-young Park4727da32016-05-31 10:00:51 -0700201 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 Park4aeb4bf2015-12-08 18:31:33 -0800217 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 Gampe985ca2f2018-02-09 12:57:54 -0800230 @GuardedBy("this")
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800231 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 Gampe985ca2f2018-02-09 12:57:54 -0800241 @GuardedBy("this")
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800242 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 Periathiruvadi38388302018-02-22 16:42:49 -0800249 AppBlockingPackageInfoWrapper wrapper = mActivityWhitelists.get(packageName);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800250 return (wrapper != null) ? wrapper.info : null;
251 }
252
Andreas Gampe985ca2f2018-02-09 12:57:54 -0800253 @GuardedBy("this")
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800254 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 Periathiruvadi38388302018-02-22 16:42:49 -0800260 return isActivityInMapAndMatching(mActivityWhitelists, packageName, className);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800261 }
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 Periathiruvadi38388302018-02-22 16:42:49 -0800267 if (DBG_POLICY_CHECK) {
268 Log.d(CarLog.TAG_PACKAGE, "Pkg not in whitelist:" + packageName);
269 }
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800270 return false;
271 }
272 return wrapper.info.isActivityCovered(className);
273 }
274
275 @Override
276 public void init() {
Keun-young Park98960812016-10-04 12:50:54 -0700277 synchronized (this) {
278 mHandler.requestInit();
Keun-young Park4727da32016-05-31 10:00:51 -0700279 }
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800280 }
281
282 @Override
283 public void release() {
284 synchronized (this) {
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800285 mHandler.requestRelease();
Keun-young Park6dcc50b2016-10-10 19:01:11 -0700286 // wait for release do be done. This guarantees that init is done.
287 try {
288 wait();
289 } catch (InterruptedException e) {
290 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800291 mActivityWhitelists.clear();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800292 mClientPolicies.clear();
293 if (mProxies != null) {
294 for (AppBlockingPolicyProxy proxy : mProxies) {
295 proxy.disconnect();
296 }
297 mProxies.clear();
298 }
299 wakeupClientsWaitingForPolicySetitngLocked();
300 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800301 mContext.unregisterReceiver(mBootPhaseEventReceiver);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800302 mCarUxRestrictionsService.unregisterUxRestrictionsChangeListener(mUxRestrictionsListener);
Keun-young Park98960812016-10-04 12:50:54 -0700303 mSystemActivityMonitoringService.registerActivityLaunchListener(null);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800304 }
305
306 // run from HandlerThread
307 private void doHandleInit() {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800308 IntentFilter bootIntent = new IntentFilter();
309 bootIntent.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED);
310 mContext.registerReceiver(mBootPhaseEventReceiver, bootIntent);
Keun-young Park6dcc50b2016-10-10 19:01:11 -0700311 try {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800312 mCarUxRestrictionsService.registerUxRestrictionsChangeListener(mUxRestrictionsListener);
Keun-young Park6dcc50b2016-10-10 19:01:11 -0700313 } 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 Park98960812016-10-04 12:50:54 -0700318 mSystemActivityMonitoringService.registerActivityLaunchListener(
319 mActivityLaunchListener);
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800320 }
321
322 private void doParseInstalledPackages() {
323 startAppBlockingPolicies();
324 generateActivityWhitelist();
325 mUxRestrictionsListener.checkIfTopActivityNeedsBlocking();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800326 }
327
Keun-young Park6dcc50b2016-10-10 19:01:11 -0700328 private synchronized void doHandleRelease() {
329 notifyAll();
330 }
331
Andreas Gampe985ca2f2018-02-09 12:57:54 -0800332 @GuardedBy("this")
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800333 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 Park4aeb4bf2015-12-08 18:31:33 -0800343 synchronized (this) {
344 wakeupClientsWaitingForPolicySetitngLocked();
345 }
Keun-young Park4727da32016-05-31 10:00:51 -0700346 blockTopActivitiesIfNecessary();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800347 }
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 Park4727da32016-05-31 10:00:51 -0700379 blockTopActivitiesIfNecessary();
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800380 }
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 Park4727da32016-05-31 10:00:51 -0700456 /**
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 Periathiruvadi38388302018-02-22 16:42:49 -0800469 mDefaultActivityWhitelist = whitelist;
Keun-young Park4727da32016-05-31 10:00:51 -0700470 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 Park98960812016-10-04 12:50:54 -0700482 } else if (packageActivityPair.length == 2) {
Keun-young Park4727da32016-05-31 10:00:51 -0700483 // 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 Periathiruvadi38388302018-02-22 16:42:49 -0800492 private void generateActivityWhitelist() {
493 HashMap<String, AppBlockingPackageInfoWrapper> activityWhitelist = new HashMap<>();
Keun-young Park4727da32016-05-31 10:00:51 -0700494 HashMap<String, Set<String>> configWhitelist = parseConfigWhitelist();
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800495
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 Park4aeb4bf2015-12-08 18:31:33 -0800500 for (PackageInfo info : packages) {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800501 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 Park4727da32016-05-31 10:00:51 -0700527 }
528 } else {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800529 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 Park4727da32016-05-31 10:00:51 -0700533 }
534 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800535 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 Park4aeb4bf2015-12-08 18:31:33 -0800557 }
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800558 }
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800559 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800560 // 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 Park4aeb4bf2015-12-08 18:31:33 -0800572 }
573 synchronized (this) {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800574 mActivityWhitelists.putAll(activityWhitelist);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800575 }
576 }
577
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800578 @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 Periathiruvadi8ef1a202018-03-16 17:49:21 -0700590 /**
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 Park4aeb4bf2015-12-08 18:31:33 -0800596 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 Park4727da32016-05-31 10:00:51 -0700669 writer.println("mEnableActivityBlocking:" + mEnableActivityBlocking);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800670 writer.println("ActivityRestricted:" + mUxRestrictionsListener.isRestricted());
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800671 writer.print(dumpPoliciesLocked(true));
672 }
673 }
674
Andreas Gampe985ca2f2018-02-09 12:57:54 -0800675 @GuardedBy("this")
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800676 private String dumpPoliciesLocked(boolean dumpAll) {
677 StringBuilder sb = new StringBuilder();
678 if (dumpAll) {
679 sb.append("**System white list**\n");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800680 for (AppBlockingPackageInfoWrapper wrapper : mActivityWhitelists.values()) {
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800681 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 Park98960812016-10-04 12:50:54 -0700702 sb.append("**Default Whitelist string**\n");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800703 sb.append(mDefaultActivityWhitelist + "\n");
Keun-young Park98960812016-10-04 12:50:54 -0700704
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800705 return sb.toString();
706 }
707
Keun-young Park4727da32016-05-31 10:00:51 -0700708 private void blockTopActivityIfNecessary(TopTaskInfoContainer topTask) {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800709 boolean restricted = mUxRestrictionsListener.isRestricted();
Keun-young Park4727da32016-05-31 10:00:51 -0700710 if (!restricted) {
711 return;
712 }
713 doBlockTopActivityIfNotAllowed(topTask);
714 }
715
716 private void doBlockTopActivityIfNotAllowed(TopTaskInfoContainer topTask) {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800717 if (topTask.topActivity == null) {
718 return;
719 }
Keun-young Park4727da32016-05-31 10:00:51 -0700720 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 Periathiruvadibf0eab72018-02-06 12:32:43 -0800726 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 Periathiruvadi38388302018-02-22 16:42:49 -0800737 if (DBG_POLICY_CHECK) {
Keun-young Park4727da32016-05-31 10:00:51 -0700738 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 Park4727da32016-05-31 10:00:51 -0700741 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800742 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 Park4727da32016-05-31 10:00:51 -0700748 }
749
750 private void blockTopActivitiesIfNecessary() {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800751 boolean restricted = mUxRestrictionsListener.isRestricted();
Keun-young Park4727da32016-05-31 10:00:51 -0700752 if (!restricted) {
753 return;
754 }
755 List<TopTaskInfoContainer> topTasks = mSystemActivityMonitoringService.getTopTasks();
756 for (TopTaskInfoContainer topTask : topTasks) {
757 doBlockTopActivityIfNotAllowed(topTask);
758 }
759 }
760
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800761 public synchronized void setEnableActivityBlocking(boolean enable) {
762 mEnableActivityBlocking = enable;
763 }
764
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800765 /**
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800766 * 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 Park4aeb4bf2015-12-08 18:31:33 -0800780 * 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 Periathiruvadi38388302018-02-22 16:42:49 -0800784 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 Park4aeb4bf2015-12-08 18:31:33 -0800788
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 Park6dcc50b2016-10-10 19:01:11 -0700802 Message msg = obtainMessage(MSG_RELEASE);
803 sendMessage(msg);
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800804 }
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 Periathiruvadi38388302018-02-22 16:42:49 -0800818 private void requestParsingInstalledPkgs() {
819 Message msg = obtainMessage(MSG_PARSE_PKG);
820 sendMessage(msg);
821 }
822
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800823 @Override
824 public void handleMessage(Message msg) {
825 switch (msg.what) {
826 case MSG_INIT:
827 doHandleInit();
828 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800829 case MSG_PARSE_PKG:
830 doParseInstalledPackages();
831 break;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800832 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 Park6dcc50b2016-10-10 19:01:11 -0700840 case MSG_RELEASE:
841 doHandleRelease();
842 break;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -0800843 }
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 Park4727da32016-05-31 10:00:51 -0700931
932 private class ActivityLaunchListener
933 implements SystemActivityMonitoringService.ActivityLaunchListener {
934 @Override
935 public void onActivityLaunch(TopTaskInfoContainer topTask) {
936 blockTopActivityIfNecessary(topTask);
937 }
938 }
939
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800940 /**
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 Park4727da32016-05-31 10:00:51 -0700949
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800950 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 Park4727da32016-05-31 10:00:51 -0700962 synchronized (this) {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800963 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 Park4727da32016-05-31 10:00:51 -0700974 }
975 }
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800976 if (DBG_POLICY_CHECK) {
977 Log.d(CarLog.TAG_PACKAGE, "block?: " + shouldCheck);
978 }
979 if (shouldCheck) {
Keun-young Park4727da32016-05-31 10:00:51 -0700980 blockTopActivitiesIfNecessary();
981 }
982 }
983
984 private synchronized boolean isRestricted() {
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800985 // 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 Park4727da32016-05-31 10:00:51 -0700996 }
997 }
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800998
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 Park4aeb4bf2015-12-08 18:31:33 -08001013}