1 /* 2 * Copyright (C) 2016 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 com.android.systemui.volume.car; 18 19 import android.car.Car; 20 import android.car.CarNotConnectedException; 21 import android.car.media.CarAudioManager; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.ServiceConnection; 25 import android.os.IBinder; 26 import android.util.Log; 27 28 import com.android.systemui.volume.VolumeDialogControllerImpl; 29 30 /** 31 * A volume dialog controller for the automotive use case. 32 * 33 * {@link android.car.media.CarAudioManager} is the source of truth to get the stream volumes. 34 * And volume changes should be sent to the car's audio module instead of the android's audio mixer. 35 */ 36 public class CarVolumeDialogController extends VolumeDialogControllerImpl { 37 private static final String TAG = "CarVolumeDialogController"; 38 39 private final Car mCar; 40 private CarAudioManager mCarAudioManager; 41 42 private final ServiceConnection mConnection = new ServiceConnection() { 43 @Override 44 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 45 try { 46 mCarAudioManager = (CarAudioManager) mCar.getCarManager(Car.AUDIO_SERVICE); 47 setVolumeController(); 48 CarVolumeDialogController.this.getState(); 49 } catch (CarNotConnectedException e) { 50 Log.e(TAG, "Car is not connected!", e); 51 } 52 } 53 54 @Override 55 public void onServiceDisconnected(ComponentName componentName) { 56 Log.e(TAG, "Car service is disconnected"); 57 } 58 }; 59 60 public CarVolumeDialogController(Context context) { 61 super(context); 62 mCar = Car.createCar(context, mConnection); 63 mCar.connect(); 64 } 65 66 @Override 67 protected void setAudioManagerStreamVolume(int stream, int level, int flag) { 68 if (mCarAudioManager == null) { 69 Log.d(TAG, "Car audio manager is not initialized yet"); 70 return; 71 } 72 try { 73 mCarAudioManager.setStreamVolume(stream, level, flag); 74 } catch (CarNotConnectedException e) { 75 Log.e(TAG, "Car is not connected", e); 76 } 77 } 78 79 @Override 80 protected int getAudioManagerStreamVolume(int stream) { 81 if(mCarAudioManager == null) { 82 Log.d(TAG, "Car audio manager is not initialized yet"); 83 return 0; 84 } 85 86 try { 87 return mCarAudioManager.getStreamVolume(stream); 88 } catch (CarNotConnectedException e) { 89 Log.e(TAG, "Car is not connected", e); 90 return 0; 91 } 92 } 93 94 @Override 95 protected int getAudioManagerStreamMaxVolume(int stream) { 96 if(mCarAudioManager == null) { 97 Log.d(TAG, "Car audio manager is not initialized yet"); 98 return 0; 99 } 100 101 try { 102 return mCarAudioManager.getStreamMaxVolume(stream); 103 } catch (CarNotConnectedException e) { 104 Log.e(TAG, "Car is not connected", e); 105 return 0; 106 } 107 } 108 109 @Override 110 protected int getAudioManagerStreamMinVolume(int stream) { 111 if(mCarAudioManager == null) { 112 Log.d(TAG, "Car audio manager is not initialized yet"); 113 return 0; 114 } 115 116 try { 117 return mCarAudioManager.getStreamMinVolume(stream); 118 } catch (CarNotConnectedException e) { 119 Log.e(TAG, "Car is not connected", e); 120 return 0; 121 } 122 } 123 124 @Override 125 public void setVolumeController() { 126 if (mCarAudioManager == null) { 127 Log.d(TAG, "Car audio manager is not initialized yet"); 128 return; 129 } 130 try { 131 mCarAudioManager.setVolumeController(mVolumeController); 132 } catch (CarNotConnectedException e) { 133 Log.e(TAG, "Car is not connected", e); 134 } 135 } 136 } 137