Support the private display in DirectRenderingCluster.

Bug: 237787920
Test: Check if DirectRenderingCluster works with the private display
Change-Id: I00a2c78dbf942f6ba9bc82eb4c2dad6ec2f649aa
(cherry picked from commit b73d96c6d917b578a83d4b170c991d70d9a2388d)
Merged-In: I00a2c78dbf942f6ba9bc82eb4c2dad6ec2f649aa
diff --git a/DirectRenderingCluster/AndroidManifest.xml b/DirectRenderingCluster/AndroidManifest.xml
index 809fa3f..f972e97 100644
--- a/DirectRenderingCluster/AndroidManifest.xml
+++ b/DirectRenderingCluster/AndroidManifest.xml
@@ -42,8 +42,11 @@
     <uses-permission android:name="android.permission.MANAGE_USERS" />
     <!-- Required to launch navigation apps -->
     <uses-permission android:name="android.car.permission.CAR_INSTRUMENT_CLUSTER_CONTROL"/>
+    <!-- Required to use CarOccupantZoneManager.getDisplayIdForDriver() -->
+    <uses-permission android:name="android.car.permission.ACCESS_PRIVATE_DISPLAY_ID"/>
     <!-- Required to watch activities running on the cluster -->
     <uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER"/>
+
     <!-- Required to show car sensor data -->
     <uses-permission android:name="android.car.permission.CAR_ENERGY"/>
     <uses-permission android:name="android.car.permission.CAR_POWERTRAIN"/>
diff --git a/DirectRenderingCluster/src/android/car/cluster/ClusterDisplayProvider.java b/DirectRenderingCluster/src/android/car/cluster/ClusterDisplayProvider.java
index 4050099..5949a50 100644
--- a/DirectRenderingCluster/src/android/car/cluster/ClusterDisplayProvider.java
+++ b/DirectRenderingCluster/src/android/car/cluster/ClusterDisplayProvider.java
@@ -18,7 +18,6 @@
 
 import android.car.Car;
 import android.car.CarOccupantZoneManager;
-import android.car.CarOccupantZoneManager.OccupantZoneInfo;
 import android.content.Context;
 import android.hardware.display.DisplayManager.DisplayListener;
 import android.util.Log;
@@ -26,8 +25,6 @@
 
 import com.android.internal.util.Preconditions;
 
-import java.util.List;
-
 /**
  * This class provides a display for instrument cluster renderer.
  * <p>
@@ -37,7 +34,7 @@
  */
 public class ClusterDisplayProvider {
     private static final String TAG = "Cluster.DisplayProvider";
-    private static final boolean DEBUG = false;
+    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
 
     private final DisplayListener mListener;
     private final Car mCar;
@@ -66,35 +63,30 @@
         Preconditions.checkArgument(
                 occupantZoneManager != null,"Can't get CarOccupantZoneManager");
         mOccupantZoneManager = occupantZoneManager;
-        checkClusterDisplayAdded();
+        checkClusterDisplayChanged();
         mOccupantZoneManager.registerOccupantZoneConfigChangeListener(
                 new ClusterDisplayChangeListener());
     }
 
-    private void checkClusterDisplayAdded() {
-        Display clusterDisplay = getClusterDisplay();
-        if (clusterDisplay != null) {
-            Log.i(TAG, String.format("Found display: %s (id: %d, owner: %s)",
-                    clusterDisplay.getName(), clusterDisplay.getDisplayId(),
-                    clusterDisplay.getOwnerPackageName()));
-            mClusterDisplayId = clusterDisplay.getDisplayId();
-            mListener.onDisplayAdded(clusterDisplay.getDisplayId());
+    private void checkClusterDisplayChanged() {
+        int clusterDisplayId = getClusterDisplayId();
+        if (clusterDisplayId == mClusterDisplayId) {
+            return;
+        }
+        if (mClusterDisplayId != Display.INVALID_DISPLAY) {
+            Log.i(TAG, "Cluster display is removed");
+            mListener.onDisplayRemoved(mClusterDisplayId);
+        }
+        mClusterDisplayId = clusterDisplayId;
+        if (clusterDisplayId != Display.INVALID_DISPLAY) {
+            Log.i(TAG, "Found cluster displayId=" + clusterDisplayId);
+            mListener.onDisplayAdded(clusterDisplayId);
         }
     }
 
-    private Display getClusterDisplay() {
-        List<OccupantZoneInfo> zones = mOccupantZoneManager.getAllOccupantZones();
-        int zones_size = zones.size();
-        for (int i = 0; i < zones_size; ++i) {
-            OccupantZoneInfo zone = zones.get(i);
-            // Assumes that a Car has only one driver.
-            if (zone.occupantType == CarOccupantZoneManager.OCCUPANT_TYPE_DRIVER) {
-                return mOccupantZoneManager.getDisplayForOccupant(
-                        zone, CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER);
-            }
-        }
-        Log.e(TAG, "Can't find the OccupantZoneInfo for driver");
-        return null;
+    private int getClusterDisplayId() {
+        return mOccupantZoneManager.getDisplayIdForDriver(
+                CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER);
     }
 
     private final class ClusterDisplayChangeListener implements
@@ -105,15 +97,7 @@
             if ((changeFlags & CarOccupantZoneManager.ZONE_CONFIG_CHANGE_FLAG_DISPLAY) == 0) {
                 return;
             }
-            if (mClusterDisplayId == Display.INVALID_DISPLAY) {
-                checkClusterDisplayAdded();
-            } else {
-                Display clusterDisplay = getClusterDisplay();
-                if (clusterDisplay == null) {
-                    mListener.onDisplayRemoved(mClusterDisplayId);
-                    mClusterDisplayId = Display.INVALID_DISPLAY;
-                }
-            }
+            checkClusterDisplayChanged();
         }
     }