implement activity blocking

- monitor activity launch and shut it down if
  car is not parked and if the app is not allowed.
- Launch blocking activity for unsafe app.
- Blocking activity stays up to timeout (3 secs) and
  finish itself if there is safe app behind.
  If safe app is not there, launch home. If home is
  not safe either. stay.
- allow OEMs to give static whitelist as overlay.

bug: 25267050

Change-Id: Id0bdae757fa98345069f2d83711b46447506f888
diff --git a/car-lib/api/system-current.txt b/car-lib/api/system-current.txt
index b6f897b..53e1a00 100644
--- a/car-lib/api/system-current.txt
+++ b/car-lib/api/system-current.txt
@@ -366,6 +366,7 @@
 
   public class CarPackageManager {
     method public boolean isActivityAllowedWhileDriving(java.lang.String, java.lang.String) throws android.car.CarNotConnectedException;
+    method public boolean isActivityBackedBySafeActivity(android.content.ComponentName) throws android.car.CarNotConnectedException;
     method public boolean isServiceAllowedWhileDriving(java.lang.String, java.lang.String) throws android.car.CarNotConnectedException;
     method public void setAppBlockingPolicy(java.lang.String, android.car.content.pm.CarAppBlockingPolicy, int) throws android.car.CarNotConnectedException, java.lang.IllegalArgumentException, java.lang.SecurityException;
     field public static final int FLAG_SET_POLICY_ADD = 2; // 0x2
diff --git a/car-lib/src/android/car/content/pm/CarPackageManager.java b/car-lib/src/android/car/content/pm/CarPackageManager.java
index 1cdbfc7..ffef31e 100644
--- a/car-lib/src/android/car/content/pm/CarPackageManager.java
+++ b/car-lib/src/android/car/content/pm/CarPackageManager.java
@@ -21,6 +21,7 @@
 import android.car.CarApiUtil;
 import android.car.CarManagerBase;
 import android.car.CarNotConnectedException;
+import android.content.ComponentName;
 import android.content.Context;
 import android.os.IBinder;
 import android.os.Looper;
@@ -122,13 +123,40 @@
     }
 
     /**
+     * Check if finishing Activity will lead into safe Activity (=allowed Activity) to be shown.
+     * This can be used by unsafe activity blocking Activity to check if finishing itself can
+     * lead into being launched again due to unsafe activity shown. Note that checking this does not
+     * guarantee that blocking will not be done as driving state can change after this call is made.
+     *
+     * @param activityName
+     * @return true if there is a safe Activity (or car is stopped) in the back of task stack
+     *         so that finishing the Activity will not trigger another Activity blocking. If
+     *         the given Activity is not in foreground, then it will return true as well as
+     *         finishing the Activity will not make any difference.
+     *
+     * @hide
+     */
+    @SystemApi
+    public boolean isActivityBackedBySafeActivity(ComponentName activityName)
+            throws CarNotConnectedException {
+        try {
+            return mService.isActivityBackedBySafeActivity(activityName);
+        } catch (IllegalStateException e) {
+            CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
+        } catch (RemoteException e) {
+            //ignore as CarApi will handle disconnection anyway.
+        }
+        return true;
+    }
+
+    /**
      * Check if given activity is allowed while driving.
      * @param packageName
      * @param className
      * @return
      */
     public boolean isActivityAllowedWhileDriving(String packageName, String className)
-            throws CarNotConnectedException{
+            throws CarNotConnectedException {
         try {
             return mService.isActivityAllowedWhileDriving(packageName, className);
         } catch (IllegalStateException e) {
diff --git a/car-lib/src/android/car/content/pm/ICarPackageManager.aidl b/car-lib/src/android/car/content/pm/ICarPackageManager.aidl
index d451187..306db65 100644
--- a/car-lib/src/android/car/content/pm/ICarPackageManager.aidl
+++ b/car-lib/src/android/car/content/pm/ICarPackageManager.aidl
@@ -17,10 +17,12 @@
 package android.car.content.pm;
 
 import android.car.content.pm.CarAppBlockingPolicy;
+import android.content.ComponentName;
 
 /** @hide */
 interface ICarPackageManager {
     void setAppBlockingPolicy(in String packageName, in CarAppBlockingPolicy policy, int flags) = 0;
     boolean isActivityAllowedWhileDriving(in String packageName, in String className) = 1;
     boolean isServiceAllowedWhileDriving(in String packageName, in String className) = 2;
+    boolean isActivityBackedBySafeActivity(in ComponentName activityName) = 3;
 }