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 | |
| 40 | // TODO: need to have a policy to define the default context |
| 41 | public static int DEFAULT_CAR_AUDIO_CONTEXT = |
| 42 | VehicleAudioContextFlag.VEHICLE_AUDIO_CONTEXT_MUSIC_FLAG; |
| 43 | private final Context mContext; |
| 44 | private final AudioHalService mAudioHal; |
| 45 | private final CarAudioService mAudioService; |
| 46 | private final CarInputService mInputService; |
| 47 | |
| 48 | private CarVolumeController mCarVolumeController; |
| 49 | |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 50 | private IVolumeController mIVolumeController; |
| 51 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 52 | public static int androidStreamToCarUsage(int logicalAndroidStream) { |
| 53 | return CarAudioAttributesUtil.getCarUsageFromAudioAttributes( |
| 54 | new AudioAttributes.Builder() |
| 55 | .setLegacyStreamType(logicalAndroidStream).build()); |
| 56 | } |
| 57 | |
| 58 | public CarVolumeService(Context context, CarAudioService audioService, AudioHalService audioHal, |
| 59 | CarInputService inputService) { |
| 60 | mContext = context; |
| 61 | mAudioHal = audioHal; |
| 62 | mAudioService = audioService; |
| 63 | mInputService = inputService; |
| 64 | } |
| 65 | |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 66 | public synchronized void init() { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 67 | mCarVolumeController = CarVolumeControllerFactory.createCarVolumeController(mContext, |
| 68 | mAudioService, mAudioHal, mInputService); |
| 69 | |
| 70 | mCarVolumeController.init(); |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 71 | if (mIVolumeController != null) { |
| 72 | mCarVolumeController.setVolumeController(mIVolumeController); |
| 73 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 74 | mInputService.setVolumeKeyListener(mCarVolumeController); |
| 75 | } |
| 76 | |
| 77 | public void setStreamVolume(int streamType, int index, int flags) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 78 | getController().setStreamVolume(streamType, index, flags); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Keun-young Park | 49cd19c | 2016-08-04 15:54:44 -0700 | [diff] [blame] | 81 | public synchronized void setVolumeController(IVolumeController controller) { |
| 82 | mIVolumeController = controller; |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 83 | getController().setVolumeController(controller); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public int getStreamMaxVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 87 | return getController().getStreamMaxVolume(stream); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public int getStreamMinVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 91 | return getController().getStreamMinVolume(stream); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public int getStreamVolume(int stream) { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 95 | return getController().getStreamVolume(stream); |
| 96 | } |
| 97 | |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 98 | public void dump(PrintWriter writer) { |
| 99 | mCarVolumeController.dump(writer); |
| 100 | } |
| 101 | |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 102 | private synchronized CarVolumeController getController() { |
| 103 | return mCarVolumeController; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Abstraction layer for volume controls, so that we don't have if-else check for audio |
| 108 | * properties everywhere. |
| 109 | */ |
| 110 | public static abstract class CarVolumeController implements CarInputService.KeyEventListener { |
| 111 | abstract void init(); |
| 112 | abstract public void setStreamVolume(int stream, int index, int flags); |
| 113 | abstract public void setVolumeController(IVolumeController controller); |
| 114 | abstract public int getStreamMaxVolume(int stream); |
| 115 | abstract public int getStreamMinVolume(int stream); |
| 116 | abstract public int getStreamVolume(int stream); |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 117 | abstract public void dump(PrintWriter writer); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 118 | } |
| 119 | } |