Merge "Fixing permissions for sensors."
diff --git a/tests/DirectRenderingClusterSample/AndroidManifest.xml b/tests/DirectRenderingClusterSample/AndroidManifest.xml
index 660f460..2dcdc58 100644
--- a/tests/DirectRenderingClusterSample/AndroidManifest.xml
+++ b/tests/DirectRenderingClusterSample/AndroidManifest.xml
@@ -46,6 +46,7 @@
     <uses-permission android:name="android.car.permission.CAR_POWERTRAIN"/>
     <uses-permission android:name="android.car.permission.CAR_INFO"/>
     <uses-permission android:name="android.car.permission.CAR_SPEED"/>
+    <uses-permission android:name="android.car.permission.CAR_ENGINE_DETAILED"/>
 
     <application android:label="@string/app_name"
                  android:icon="@mipmap/ic_launcher"
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ClusterViewModel.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ClusterViewModel.java
index 8a67856..2efe765 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ClusterViewModel.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/ClusterViewModel.java
@@ -74,23 +74,8 @@
             try {
                 Log.i(TAG, "onServiceConnected, name: " + name + ", service: " + service);
 
-                // Listen navigation focus state
-                mCarAppFocusManager = (CarAppFocusManager) mCar.getCarManager(
-                        Car.APP_FOCUS_SERVICE);
-                if (mCarAppFocusManager == null) {
-                    Log.e(TAG, "onServiceConnected: unable to obtain CarAppFocusManager");
-                    return;
-                }
-                mCarAppFocusManager.addFocusListener(
-                        (appType, active) -> setNavigationFocus(active),
-                        CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
-
-                // Listen property value changes
-                mCarPropertyManager = (CarPropertyManager) mCar.getCarManager(Car.PROPERTY_SERVICE);
-                for (Integer propertyId : Sensors.getInstance().getPropertyIds()) {
-                    mCarPropertyManager.registerListener(mCarPropertyEventListener,
-                            propertyId, PROPERTIES_REFRESH_RATE_UI);
-                }
+                registerAppFocusListener();
+                registerCarPropertiesListener();
             } catch (CarNotConnectedException e) {
                 Log.e(TAG, "onServiceConnected: error obtaining manager", e);
             }
@@ -104,6 +89,33 @@
         }
     };
 
+    private void registerAppFocusListener() throws CarNotConnectedException {
+        mCarAppFocusManager = (CarAppFocusManager) mCar.getCarManager(
+                Car.APP_FOCUS_SERVICE);
+        if (mCarAppFocusManager != null) {
+            mCarAppFocusManager.addFocusListener(
+                    (appType, active) -> setNavigationFocus(active),
+                    CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
+        } else {
+            Log.e(TAG, "onServiceConnected: unable to obtain CarAppFocusManager");
+        }
+    }
+
+    private void registerCarPropertiesListener() throws CarNotConnectedException {
+        Sensors sensors = Sensors.getInstance();
+        mCarPropertyManager = (CarPropertyManager) mCar.getCarManager(Car.PROPERTY_SERVICE);
+        for (Integer propertyId : sensors.getPropertyIds()) {
+            try {
+                mCarPropertyManager.registerListener(mCarPropertyEventListener,
+                        propertyId, PROPERTIES_REFRESH_RATE_UI);
+            } catch (SecurityException ex) {
+                Log.e(TAG, "onServiceConnected: Unable to listen to car property: " + propertyId
+                        + " sensors: " + sensors.getSensorsForPropertyId(propertyId), ex);
+            }
+        }
+    }
+
+
     private CarPropertyManager.CarPropertyEventListener mCarPropertyEventListener =
             new CarPropertyManager.CarPropertyEventListener() {
         @Override
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
index e138a6b..cf38e1f 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/MainClusterActivity.java
@@ -429,11 +429,9 @@
      * have a default navigation activity selected yet.
      */
     private void tryLaunchNavigationActivity() {
-        int userHandle = ActivityManager.getCurrentUser();
-        if (userHandle == UserHandle.USER_SYSTEM || mNavigationDisplayId == NO_DISPLAY) {
+        if (mNavigationDisplayId == NO_DISPLAY) {
             if (Log.isLoggable(TAG, Log.DEBUG)) {
-                Log.d(TAG, String.format("Launch activity ignored (user: %d, display: %d)",
-                        userHandle, mNavigationDisplayId));
+                Log.d(TAG, String.format("Launch activity ignored (no display yet)"));
             }
             // Not ready to launch yet.
             return;
diff --git a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/sensors/Sensor.java b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/sensors/Sensor.java
index 54cc7f8..8f01cd7 100644
--- a/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/sensors/Sensor.java
+++ b/tests/DirectRenderingClusterSample/src/android/car/cluster/sample/sensors/Sensor.java
@@ -57,4 +57,9 @@
         mExpectedPropertyType = expectedPropertyType;
         mAdapter = adapter;
     }
+
+    @Override
+    public String toString() {
+        return mName;
+    }
 }