Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.car; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioAttributes; |
| 21 | import android.media.IVolumeController; |
| 22 | import android.view.KeyEvent; |
| 23 | |
| 24 | import com.android.car.hal.AudioHalService; |
| 25 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioContextFlag; |
| 26 | |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 27 | import java.io.PrintWriter; |
| 28 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Handles car volume controls. |
| 31 | * |
| 32 | * It delegates to a {@link com.android.car.CarVolumeService.CarVolumeController} to do proper |
| 33 | * volume controls based on different car properties. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | public class CarVolumeService { |
| 38 | private static final String TAG = "CarVolumeService"; |
| 39 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 40 | public static int DEFAULT_CAR_AUDIO_CONTEXT = |
| 41 | VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_MUSIC_FLAG; |
| 42 | private final Context mContext; |
| 43 | private final AudioHalService mAudioHal; |
| 44 | private final CarAudioService mAudioService; |
| 45 | private final CarInputService mInputService; |
| 46 | |
| 47 | private CarVolumeController mCarVolumeController; |
| 48 | |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 49 | private IVolumeController mIVolumeController; |
| 50 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 51 | public static int androidStreamToCarUsage(int logicalAndroidStream) { |
| 52 | return CarAudioAttributesUtil.getCarUsageFromAudioAttributes( |
| 53 | new AudioAttributes.Builder() |
| 54 | .setLegacyStreamType(logicalAndroidStream).build()); |
| 55 | } |
| 56 | |
| 57 | public CarVolumeService(Context context, CarAudioService audioService, AudioHalService audioHal, |
| 58 | CarInputService inputService) { |
| 59 | mContext = context; |
| 60 | mAudioHal = audioHal; |
| 61 | mAudioService = audioService; |
| 62 | mInputService = inputService; |
| 63 | } |
| 64 | |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 65 | public synchronized void init() { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 66 | mCarVolumeController = CarVolumeControllerFactory.createCarVolumeController(mContext, |
| 67 | mAudioService, mAudioHal, mInputService); |
| 68 | |
| 69 | mCarVolumeController.init(); |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 70 | if (mIVolumeController != null) { |
| 71 | mCarVolumeController.setVolumeController(mIVolumeController); |
| 72 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 73 | mInputService.setVolumeKeyListener(mCarVolumeController); |
| 74 | } |
| 75 | |
| 76 | public void setStreamVolume(int streamType, int index, int flags) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 77 | getController().setStreamVolume(streamType, index, flags); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 80 | public synchronized void setVolumeController(IVolumeController controller) { |
| 81 | mIVolumeController = controller; |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 82 | getController().setVolumeController(controller); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public int getStreamMaxVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 86 | return getController().getStreamMaxVolume(stream); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public int getStreamMinVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 90 | return getController().getStreamMinVolume(stream); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public int getStreamVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 94 | return getController().getStreamVolume(stream); |
| 95 | } |
| 96 | |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 97 | public void dump(PrintWriter writer) { |
| 98 | mCarVolumeController.dump(writer); |
| 99 | } |
| 100 | |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 101 | private synchronized CarVolumeController getController() { |
| 102 | return mCarVolumeController; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Abstraction layer for volume controls, so that we don't have if-else check for audio |
| 107 | * properties everywhere. |
| 108 | */ |
| 109 | public static abstract class CarVolumeController implements CarInputService.KeyEventListener { |
| 110 | abstract void init(); |
| 111 | abstract public void setStreamVolume(int stream, int index, int flags); |
| 112 | abstract public void setVolumeController(IVolumeController controller); |
| 113 | abstract public int getStreamMaxVolume(int stream); |
| 114 | abstract public int getStreamMinVolume(int stream); |
| 115 | abstract public int getStreamVolume(int stream); |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 116 | abstract public void dump(PrintWriter writer); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 117 | } |
| 118 | } |