1 /* 2 * Copyright (C) 2018 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 */ 16 17 package android.car.cluster; 18 19 import android.car.Car; 20 import android.car.CarOccupantZoneManager; 21 import android.content.Context; 22 import android.hardware.display.DisplayManager.DisplayListener; 23 import android.util.Log; 24 import android.view.Display; 25 26 import com.android.internal.util.Preconditions; 27 28 /** 29 * This class provides a display for instrument cluster renderer. 30 * <p> 31 * By default it will try to provide physical secondary display if it is connected, if secondary 32 * display is not connected during creation of this class then it will wait for the display will 33 * be added. 34 */ 35 public class ClusterDisplayProvider { 36 private static final String TAG = "Cluster.DisplayProvider"; 37 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 38 39 private final DisplayListener mListener; 40 private final Car mCar; 41 private CarOccupantZoneManager mOccupantZoneManager; 42 43 private int mClusterDisplayId = Display.INVALID_DISPLAY; 44 ClusterDisplayProvider(Context context, DisplayListener clusterDisplayListener)45 ClusterDisplayProvider(Context context, DisplayListener clusterDisplayListener) { 46 mListener = clusterDisplayListener; 47 mCar = Car.createCar(context, null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER, 48 (car, ready) -> { 49 if (!ready) return; 50 initClusterDisplayProvider(context, (CarOccupantZoneManager) car.getCarManager( 51 Car.CAR_OCCUPANT_ZONE_SERVICE)); 52 }); 53 } 54 release()55 void release() { 56 if (mCar != null && mCar.isConnected()) { 57 mCar.disconnect(); 58 } 59 } 60 initClusterDisplayProvider( Context context, CarOccupantZoneManager occupantZoneManager)61 private void initClusterDisplayProvider( 62 Context context, CarOccupantZoneManager occupantZoneManager) { 63 Preconditions.checkArgument( 64 occupantZoneManager != null,"Can't get CarOccupantZoneManager"); 65 mOccupantZoneManager = occupantZoneManager; 66 checkClusterDisplayChanged(); 67 mOccupantZoneManager.registerOccupantZoneConfigChangeListener( 68 new ClusterDisplayChangeListener()); 69 } 70 checkClusterDisplayChanged()71 private void checkClusterDisplayChanged() { 72 int clusterDisplayId = getClusterDisplayId(); 73 if (clusterDisplayId == mClusterDisplayId) { 74 return; 75 } 76 if (mClusterDisplayId != Display.INVALID_DISPLAY) { 77 Log.i(TAG, "Cluster display is removed"); 78 mListener.onDisplayRemoved(mClusterDisplayId); 79 } 80 mClusterDisplayId = clusterDisplayId; 81 if (clusterDisplayId != Display.INVALID_DISPLAY) { 82 Log.i(TAG, "Found cluster displayId=" + clusterDisplayId); 83 mListener.onDisplayAdded(clusterDisplayId); 84 } 85 } 86 getClusterDisplayId()87 private int getClusterDisplayId() { 88 return mOccupantZoneManager.getDisplayIdForDriver( 89 CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER); 90 } 91 92 private final class ClusterDisplayChangeListener implements 93 CarOccupantZoneManager.OccupantZoneConfigChangeListener { 94 @Override onOccupantZoneConfigChanged(int changeFlags)95 public void onOccupantZoneConfigChanged(int changeFlags) { 96 if (DEBUG) Log.d(TAG, "onOccupantZoneConfigChanged changeFlags=" + changeFlags); 97 if ((changeFlags & CarOccupantZoneManager.ZONE_CONFIG_CHANGE_FLAG_DISPLAY) == 0) { 98 return; 99 } 100 checkClusterDisplayChanged(); 101 } 102 } 103 104 @Override toString()105 public String toString() { 106 return getClass().getSimpleName() + "{" 107 + " clusterDisplayId = " + mClusterDisplayId 108 + "}"; 109 } 110 }