Merge "Fix driving state for manual transmission." into pi-dev
diff --git a/car-lib/api/current.txt b/car-lib/api/current.txt
index 8adacdf..9cd2e4c 100644
--- a/car-lib/api/current.txt
+++ b/car-lib/api/current.txt
@@ -230,7 +230,6 @@
   public final class CarUxRestrictionsManager {
     method public android.car.drivingstate.CarUxRestrictions getCurrentCarUxRestrictions() throws android.car.CarNotConnectedException;
     method public synchronized void registerListener(android.car.drivingstate.CarUxRestrictionsManager.OnUxRestrictionsChangedListener) throws android.car.CarNotConnectedException, java.lang.IllegalArgumentException;
-    method public synchronized void registerListener(android.car.drivingstate.CarUxRestrictionsManager.onUxRestrictionsChangedListener) throws android.car.CarNotConnectedException, java.lang.IllegalArgumentException;
     method public synchronized void unregisterListener() throws android.car.CarNotConnectedException;
   }
 
@@ -238,11 +237,6 @@
     method public abstract void onUxRestrictionsChanged(android.car.drivingstate.CarUxRestrictions);
   }
 
-  public static abstract interface CarUxRestrictionsManager.onUxRestrictionsChangedListener {
-    method public abstract void dummy();
-    method public abstract void onUxRestrictionsChanged(android.car.drivingstate.CarUxRestrictions);
-  }
-
 }
 
 package android.car.hardware {
@@ -293,6 +287,7 @@
 
   public final class CarSensorManager {
     method public android.car.hardware.CarSensorEvent getLatestSensorEvent(int) throws android.car.CarNotConnectedException;
+    method public java.util.List<android.car.hardware.CarPropertyConfig> getPropertyList() throws android.car.CarNotConnectedException;
     method public int[] getSupportedSensors() throws android.car.CarNotConnectedException;
     method public boolean isSensorSupported(int) throws android.car.CarNotConnectedException;
     method public static boolean isSensorSupported(int[], int);
diff --git a/car-lib/src/android/car/drivingstate/CarUxRestrictionsManager.java b/car-lib/src/android/car/drivingstate/CarUxRestrictionsManager.java
index 9ef3c86..57e7d60 100644
--- a/car-lib/src/android/car/drivingstate/CarUxRestrictionsManager.java
+++ b/car-lib/src/android/car/drivingstate/CarUxRestrictionsManager.java
@@ -226,35 +226,4 @@
             listener.onUxRestrictionsChanged(restrictionInfo);
         }
     }
-
-    /**
-     * To be removed after updating the support library with the new car stubs lib.
-     * b/80506092 has more details.
-     *
-     */
-    public interface onUxRestrictionsChangedListener {
-        /**
-         * To be removed see b/80506092 for details.
-         * @param restrictionInfo
-         */
-        void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo);
-        /**
-         * Temp workaround.  To be removed.
-         * To differentiate from the new OnUxRestrictionsChangedListener for clients calling
-         * registerListener with an anonymous class or lambda functions.
-         */
-        void dummy();
-    }
-
-    /**
-     * To be removed after updating the support library with the new car stubs lib.
-     * b/80506092 has more details.
-     *
-     */
-    public synchronized void registerListener(@NonNull onUxRestrictionsChangedListener listener)
-            throws CarNotConnectedException, IllegalArgumentException {
-        // Intentionally left NOP.
-    }
-
-
 }
diff --git a/car-lib/src/android/car/hardware/CarSensorManager.java b/car-lib/src/android/car/hardware/CarSensorManager.java
index fc97c9d..f6cc8df 100644
--- a/car-lib/src/android/car/hardware/CarSensorManager.java
+++ b/car-lib/src/android/car/hardware/CarSensorManager.java
@@ -268,11 +268,9 @@
 
         @Override
         public void onChangeEvent(CarPropertyValue value) {
-            synchronized (this) {
-                CarSensorManager manager = mManager.get();
-                if (manager != null) {
-                    manager.handleOnChangeEvent(value, mListener);
-                }
+            CarSensorManager manager = mManager.get();
+            if (manager != null) {
+                manager.handleOnChangeEvent(value, mListener);
             }
         }
 
@@ -325,9 +323,15 @@
         return new int[0];
     }
 
-    private List<CarPropertyConfig> getPropertyList() throws CarNotConnectedException {
+    /**
+     * Get list of properties represented by CarSensorManager for this car.
+     * @return List of CarPropertyConfig objects available via Car Cabin Manager.
+     * @throws CarNotConnectedException if the connection to the car service has been lost.
+     */
+    public List<CarPropertyConfig> getPropertyList() throws CarNotConnectedException {
         return mCarPropertyMgr.getPropertyList(mSensorConfigIds);
     }
+
     /**
      * Tells if given sensor is supported or not.
      * @param sensorType
@@ -463,22 +467,27 @@
     }
 
     private CarSensorEvent createCarSensorEvent(CarPropertyValue propertyValue) {
-        Class<?> actualClass = propertyValue.getValue().getClass();
         CarSensorEvent event = null;
-        if (actualClass == Float.class) {
+        Object o = propertyValue.getValue();
+        if (o instanceof Float) {
             event = new CarSensorEvent(propertyValue.getPropertyId(),
                     propertyValue.getTimestamp(), 1, 0, 0);
-            event.floatValues[0] = (float) propertyValue.getValue();
-        } else if (actualClass == Integer.class) {
+            event.floatValues[0] = (float) o;
+        } else if (o instanceof Integer) {
             event = new CarSensorEvent(propertyValue.getPropertyId(),
                     propertyValue.getTimestamp(), 0, 1, 0);
-            event.intValues[0] = (int) propertyValue.getValue();
-        } else if (actualClass == Boolean.class) {
+            event.intValues[0] = (int) o;
+        } else if (o instanceof Boolean) {
             event = new CarSensorEvent(propertyValue.getPropertyId(),
                     propertyValue.getTimestamp(), 0, 1, 0);
-            event.intValues[0] = (boolean) propertyValue.getValue() ? 1 : 0;
-        } else {
-            // TODO: handle int64_vec and mixed type
+            event.intValues[0] = (boolean) o ? 1 : 0;
+        } else if (o instanceof Long[]) {
+            Long[] value = (Long[]) o;
+            event = new CarSensorEvent(propertyValue.getPropertyId(),
+                    propertyValue.getTimestamp(), 0, 0, value.length);
+            for (int i = 0; i < value.length; i++) {
+                event.longValues[i] = value[i];
+            }
         }
         return event;
     }
@@ -503,6 +512,7 @@
                 for (CarPropertyConfig p : propertyConfigs) {
                     if (p.getPropertyId() == type) {
                         b = createWheelDistanceTickBundle(p.getConfigArray());
+                        break;
                     }
                 }
                 break;
diff --git a/car-lib/src/android/car/hardware/CarVendorExtensionManager.java b/car-lib/src/android/car/hardware/CarVendorExtensionManager.java
index ce2831c..9f06764 100644
--- a/car-lib/src/android/car/hardware/CarVendorExtensionManager.java
+++ b/car-lib/src/android/car/hardware/CarVendorExtensionManager.java
@@ -28,6 +28,7 @@
 import com.android.internal.annotations.GuardedBy;
 
 import java.lang.ref.WeakReference;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
@@ -44,41 +45,18 @@
     private final static boolean DBG = false;
     private final static String TAG = CarVendorExtensionManager.class.getSimpleName();
     private final CarPropertyManager mPropertyManager;
-    private CarPropertyEventListenerToBase mListenerToBase = null;
 
     @GuardedBy("mLock")
     private final ArraySet<CarVendorExtensionCallback> mCallbacks = new ArraySet<>();
     private final Object mLock = new Object();
 
-    private static class CarPropertyEventListenerToBase implements
-            CarPropertyManager.CarPropertyEventListener{
-        private final WeakReference<CarVendorExtensionManager> mManager;
-
-        CarPropertyEventListenerToBase(CarVendorExtensionManager manager) {
-            mManager = new WeakReference<>(manager);
-        }
-
-        @Override
-        public void onChangeEvent(CarPropertyValue value) {
-            CarVendorExtensionManager manager = mManager.get();
-            if (manager != null) {
-                manager.handleOnChangeEvent(value);
-            }
-        }
-
-        @Override
-        public void onErrorEvent(int propertyId, int zone) {
-            CarVendorExtensionManager manager = mManager.get();
-            if (manager != null) {
-                manager.handleOnErrorEvent(propertyId, zone);
-            }
-        }
-    }
+    @GuardedBy("mLock")
+    private CarPropertyEventListenerToBase mListenerToBase = null;
 
     private void handleOnChangeEvent(CarPropertyValue value) {
         Collection<CarVendorExtensionCallback> callbacks;
-        synchronized (this) {
-            callbacks = new ArraySet<>(mCallbacks);
+        synchronized (mLock) {
+            callbacks = new ArrayList<>(mCallbacks);
         }
         for (CarVendorExtensionCallback l: callbacks) {
             l.onChangeEvent(value);
@@ -87,14 +65,13 @@
 
     private void handleOnErrorEvent(int propertyId, int zone) {
         Collection<CarVendorExtensionCallback> listeners;
-        synchronized (this) {
-            listeners = new ArraySet<>(mCallbacks);
+        synchronized (mLock) {
+            listeners = new ArrayList<>(mCallbacks);
         }
-        if (!listeners.isEmpty()) {
-            for (CarVendorExtensionCallback l: listeners) {
-                l.onErrorEvent(propertyId, zone);
-            }
+        for (CarVendorExtensionCallback l: listeners) {
+            l.onErrorEvent(propertyId, zone);
         }
+
     }
 
     /**
@@ -242,4 +219,28 @@
     public void onCarDisconnected() {
         mPropertyManager.onCarDisconnected();
     }
+    private static class CarPropertyEventListenerToBase implements
+            CarPropertyManager.CarPropertyEventListener {
+        private final WeakReference<CarVendorExtensionManager> mManager;
+
+        CarPropertyEventListenerToBase(CarVendorExtensionManager manager) {
+            mManager = new WeakReference<>(manager);
+        }
+
+        @Override
+        public void onChangeEvent(CarPropertyValue value) {
+            CarVendorExtensionManager manager = mManager.get();
+            if (manager != null) {
+                manager.handleOnChangeEvent(value);
+            }
+        }
+
+        @Override
+        public void onErrorEvent(int propertyId, int zone) {
+            CarVendorExtensionManager manager = mManager.get();
+            if (manager != null) {
+                manager.handleOnErrorEvent(propertyId, zone);
+            }
+        }
+    }
 }
diff --git a/car-lib/src/android/car/hardware/property/CarPropertyManager.java b/car-lib/src/android/car/hardware/property/CarPropertyManager.java
index d59ce58..5c094e7 100644
--- a/car-lib/src/android/car/hardware/property/CarPropertyManager.java
+++ b/car-lib/src/android/car/hardware/property/CarPropertyManager.java
@@ -30,7 +30,7 @@
 import android.util.Log;
 import android.util.SparseArray;
 
-import com.android.car.internal.CarRatedListeners2;
+import com.android.car.internal.CarRatedFloatListeners;
 import com.android.car.internal.SingleMessageHandler;
 
 import java.lang.ref.WeakReference;
@@ -370,7 +370,7 @@
     }
 
 
-    private class CarPropertyListeners extends CarRatedListeners2<CarPropertyEventListener> {
+    private class CarPropertyListeners extends CarRatedFloatListeners<CarPropertyEventListener> {
         CarPropertyListeners(float rate) {
             super(rate);
         }
diff --git a/car-lib/src/android/car/user/CarUserManagerHelper.java b/car-lib/src/android/car/user/CarUserManagerHelper.java
index 7585ec7..4a69f0b 100644
--- a/car-lib/src/android/car/user/CarUserManagerHelper.java
+++ b/car-lib/src/android/car/user/CarUserManagerHelper.java
@@ -17,6 +17,7 @@
 
 import android.annotation.Nullable;
 import android.app.ActivityManager;
+import android.car.settings.CarSettings;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -227,6 +228,16 @@
     }
 
     /**
+     * Checks whether the user is default user.
+     *
+     * @param userInfo User to check against system user.
+     * @return {@code true} if is default user, {@code false} otherwise.
+     */
+    public boolean isDefaultUser(UserInfo userInfo) {
+        return userInfo.id == CarSettings.DEFAULT_USER_ID_TO_BOOT_INTO;
+    }
+
+    /**
      * Checks whether passed in user is the foreground user.
      *
      * @param userInfo User to check.
@@ -409,6 +420,13 @@
             return false;
         }
 
+        // Not allow to delete the default user for now. Since default user is the one to
+        // boot into.
+        if (isHeadlessSystemUser() && isDefaultUser(userInfo)) {
+            Log.w(TAG, "User " + userInfo.id + " is the default user, could not be removed.");
+            return false;
+        }
+
         if (userInfo.id == getCurrentForegroundUserId()) {
             startNewGuestSession(guestUserName);
         }
diff --git a/car-lib/src/com/android/car/internal/CarRatedListeners2.java b/car-lib/src/com/android/car/internal/CarRatedFloatListeners.java
similarity index 82%
rename from car-lib/src/com/android/car/internal/CarRatedListeners2.java
rename to car-lib/src/com/android/car/internal/CarRatedFloatListeners.java
index 8708cd4..c59a2fa 100644
--- a/car-lib/src/com/android/car/internal/CarRatedListeners2.java
+++ b/car-lib/src/com/android/car/internal/CarRatedFloatListeners.java
@@ -23,22 +23,24 @@
 
 /**
  * Represent listeners for a property grouped by their rate.
- * @param <EventListenerType>
+ * T is a type of EventListener such as CarPropertyEventListener
+ * in {@link android.car.hardware.property.CarPropertyManager}
+ * @param <T>
  * @hide
  */
-public class CarRatedListeners2<EventListenerType> {
-    private final Map<EventListenerType, Float> mListenersToRate = new HashMap<>(4);
+public class CarRatedFloatListeners<T> {
+    private final Map<T, Float> mListenersToRate = new HashMap<>(4);
 
     private float mUpdateRate;
 
     protected long mLastUpdateTime = -1;
 
-    protected CarRatedListeners2(float rate) {
+    protected CarRatedFloatListeners(float rate) {
         mUpdateRate = rate;
     }
 
     /** Check listener */
-    public boolean contains(EventListenerType listener) {
+    public boolean contains(T listener) {
         return mListenersToRate.containsKey(listener);
     }
     /** Return current rate after updating */
@@ -52,7 +54,7 @@
      * @param listener
      * @return true if rate was updated. Otherwise, returns false.
      */
-    public boolean remove(EventListenerType listener) {
+    public boolean remove(T listener) {
         mListenersToRate.remove(listener);
         if (mListenersToRate.isEmpty()) {
             return false;
@@ -76,7 +78,7 @@
      * @param updateRate
      * @return true if rate was updated. Otherwise, returns false.
      */
-    public boolean addAndUpdateRate(EventListenerType listener, float updateRate) {
+    public boolean addAndUpdateRate(T listener, float updateRate) {
         Float oldUpdateRate = mListenersToRate.put(listener, updateRate);
         if (mUpdateRate < updateRate) {
             mUpdateRate = updateRate;
@@ -87,7 +89,7 @@
         return false;
     }
 
-    public Collection<EventListenerType> getListeners() {
+    public Collection<T> getListeners() {
         return mListenersToRate.keySet();
     }
 }