Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 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.car; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
| 21 | import android.media.IAudioService; |
| 22 | import android.media.IVolumeController; |
| 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
| 25 | import android.os.Message; |
| 26 | import android.os.RemoteCallbackList; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 29 | import android.provider.Settings; |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 30 | import android.telecom.TelecomManager; |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 31 | import android.util.ArrayMap; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
| 34 | import android.view.KeyEvent; |
| 35 | |
| 36 | import com.android.car.CarVolumeService.CarVolumeController; |
| 37 | import com.android.car.hal.AudioHalService; |
| 38 | import com.android.internal.annotations.GuardedBy; |
| 39 | |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 40 | import java.io.PrintWriter; |
Keun-young Park | 7ea2107 | 2016-04-27 17:39:50 -0700 | [diff] [blame] | 41 | import java.util.Map; |
| 42 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 43 | /** |
| 44 | * A factory class to create {@link com.android.car.CarVolumeService.CarVolumeController} based |
| 45 | * on car properties. |
| 46 | */ |
| 47 | public class CarVolumeControllerFactory { |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 48 | // STOPSHIP if true. |
Vitalii Tomkiv | 1b1247b | 2016-09-30 11:27:19 -0700 | [diff] [blame] | 49 | private static final boolean DBG = false; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 50 | |
| 51 | public static CarVolumeController createCarVolumeController(Context context, |
| 52 | CarAudioService audioService, AudioHalService audioHal, CarInputService inputService) { |
| 53 | final boolean volumeSupported = audioHal.isAudioVolumeSupported(); |
| 54 | |
| 55 | // Case 1: Car Audio Module does not support volume controls |
| 56 | if (!volumeSupported) { |
| 57 | return new SimpleCarVolumeController(context); |
| 58 | } |
| 59 | return new CarExternalVolumeController(context, audioService, audioHal, inputService); |
| 60 | } |
| 61 | |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 62 | public static boolean interceptVolKeyBeforeDispatching(Context context) { |
| 63 | Log.d(CarLog.TAG_AUDIO, "interceptVolKeyBeforeDispatching"); |
| 64 | |
| 65 | TelecomManager telecomManager = (TelecomManager) |
| 66 | context.getSystemService(Context.TELECOM_SERVICE); |
| 67 | if (telecomManager != null && telecomManager.isRinging()) { |
| 68 | // If an incoming call is ringing, either VOLUME key means |
| 69 | // "silence ringer". This is consistent with current android phone's behavior |
| 70 | Log.i(CarLog.TAG_AUDIO, "interceptKeyBeforeQueueing:" |
| 71 | + " VOLUME key-down while ringing: Silence ringer!"); |
| 72 | |
| 73 | // Silence the ringer. (It's safe to call this |
| 74 | // even if the ringer has already been silenced.) |
| 75 | telecomManager.silenceRinger(); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | public static boolean isVolumeKey(KeyEvent event) { |
| 82 | return event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN |
| 83 | || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP; |
| 84 | } |
| 85 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 86 | /** |
| 87 | * To control volumes through {@link android.media.AudioManager} when car audio module does not |
| 88 | * support volume controls. |
| 89 | */ |
| 90 | public static final class SimpleCarVolumeController extends CarVolumeController { |
| 91 | private final AudioManager mAudioManager; |
| 92 | private final Context mContext; |
| 93 | |
| 94 | public SimpleCarVolumeController(Context context) { |
| 95 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 96 | mContext = context; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | void init() { |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setStreamVolume(int stream, int index, int flags) { |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 105 | if (DBG) { |
| 106 | Log.d(CarLog.TAG_AUDIO, "setStreamVolume " + stream + " " + index + " " + flags); |
| 107 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 108 | mAudioManager.setStreamVolume(stream, index, flags); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int getStreamVolume(int stream) { |
| 113 | return mAudioManager.getStreamVolume(stream); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void setVolumeController(IVolumeController controller) { |
| 118 | mAudioManager.setVolumeController(controller); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int getStreamMaxVolume(int stream) { |
| 123 | return mAudioManager.getStreamMaxVolume(stream); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int getStreamMinVolume(int stream) { |
| 128 | return mAudioManager.getStreamMinVolume(stream); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public boolean onKeyEvent(KeyEvent event) { |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 133 | if (!isVolumeKey(event)) { |
| 134 | return false; |
| 135 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 136 | handleVolumeKeyDefault(event); |
| 137 | return true; |
| 138 | } |
| 139 | |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public void dump(PrintWriter writer) { |
| 142 | writer.println("Volume controller:" + SimpleCarVolumeController.class.getSimpleName()); |
| 143 | // nothing else to dump |
| 144 | } |
| 145 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 146 | private void handleVolumeKeyDefault(KeyEvent event) { |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 147 | if (event.getAction() != KeyEvent.ACTION_DOWN |
| 148 | || interceptVolKeyBeforeDispatching(mContext)) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | boolean volUp = event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP; |
| 153 | int flags = AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND |
| 154 | | AudioManager.FLAG_FROM_KEY; |
| 155 | IAudioService audioService = getAudioService(); |
| 156 | String pkgName = mContext.getOpPackageName(); |
| 157 | try { |
| 158 | if (audioService != null) { |
| 159 | audioService.adjustSuggestedStreamVolume( |
| 160 | volUp ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER, |
| 161 | AudioManager.USE_DEFAULT_STREAM_TYPE, flags, pkgName, CarLog.TAG_INPUT); |
| 162 | } |
| 163 | } catch (RemoteException e) { |
| 164 | Log.e(CarLog.TAG_INPUT, "Error calling android audio service.", e); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private static IAudioService getAudioService() { |
| 169 | IAudioService audioService = IAudioService.Stub.asInterface( |
| 170 | ServiceManager.checkService(Context.AUDIO_SERVICE)); |
| 171 | if (audioService == null) { |
| 172 | Log.w(CarLog.TAG_INPUT, "Unable to find IAudioService interface."); |
| 173 | } |
| 174 | return audioService; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * The car volume controller to use when the car audio modules supports volume controls. |
| 180 | * |
| 181 | * Depending on whether the car support audio context and has persistent memory, we need to |
| 182 | * handle per context volume change properly. |
| 183 | * |
| 184 | * Regardless whether car supports audio context or not, we need to keep per audio context |
| 185 | * volume internally. If we only support single channel, then we only send the volume change |
| 186 | * event when that stream is in focus; Otherwise, we need to adjust the stream volume either on |
| 187 | * software mixer level or send it the car audio module if the car support audio context |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 188 | * and multi channel. TODO: Add support for multi channel. bug: 32095376 |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 189 | * |
| 190 | * Per context volume should be persisted, so the volumes can stay the same across boots. |
| 191 | * Depending on the hardware property, this can be persisted on car side (or/and android side). |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 192 | * TODO: we need to define one single source of truth if the car has memory. bug: 32091839 |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 193 | */ |
| 194 | public static class CarExternalVolumeController extends CarVolumeController |
| 195 | implements CarInputService.KeyEventListener, AudioHalService.AudioHalVolumeListener, |
| 196 | CarAudioService.AudioContextChangeListener { |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 197 | private static final String TAG = CarLog.TAG_AUDIO + ".VolCtrl"; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 198 | private static final int MSG_UPDATE_VOLUME = 0; |
| 199 | private static final int MSG_UPDATE_HAL = 1; |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 200 | private static final int MSG_SUPPRESS_UI_FOR_VOLUME = 2; |
| 201 | private static final int MSG_VOLUME_UI_RESTORE = 3; |
| 202 | |
| 203 | // within 5 seconds after a UI invisible volume change (e.g., due to audio context change, |
| 204 | // or explicitly flag), we will not show UI in respond to that particular volume changes |
| 205 | // events from HAL (context and volume index must match). |
| 206 | private static final int HIDE_VOLUME_UI_MILLISECONDS = 5 * 1000; // 5 seconds |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 207 | |
| 208 | private final Context mContext; |
| 209 | private final AudioRoutingPolicy mPolicy; |
| 210 | private final AudioHalService mHal; |
| 211 | private final CarInputService mInputService; |
| 212 | private final CarAudioService mAudioService; |
| 213 | |
| 214 | private int mSupportedAudioContext; |
| 215 | |
| 216 | private boolean mHasExternalMemory; |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 217 | private boolean mMasterVolumeOnly; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 218 | |
| 219 | @GuardedBy("this") |
| 220 | private int mCurrentContext = CarVolumeService.DEFAULT_CAR_AUDIO_CONTEXT; |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 221 | // current logical volume, the key is car audio context |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 222 | @GuardedBy("this") |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 223 | private final SparseArray<Integer> mCurrentCarContextVolume = |
| 224 | new SparseArray<>(VolumeUtils.CAR_AUDIO_CONTEXT.length); |
| 225 | // stream volume limit, the key is car audio context type |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 226 | @GuardedBy("this") |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 227 | private final SparseArray<Integer> mCarContextVolumeMax = |
| 228 | new SparseArray<>(VolumeUtils.CAR_AUDIO_CONTEXT.length); |
| 229 | // stream volume limit, the key is car audio context type |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 230 | @GuardedBy("this") |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 231 | private final RemoteCallbackList<IVolumeController> mVolumeControllers = |
| 232 | new RemoteCallbackList<>(); |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 233 | @GuardedBy("this") |
| 234 | private int[] mSuppressUiForVolume = new int[2]; |
| 235 | @GuardedBy("this") |
| 236 | private boolean mShouldSuppress = false; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 237 | |
| 238 | private final Handler mHandler = new VolumeHandler(); |
| 239 | |
| 240 | /** |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 241 | * Convert an car context to the car stream. |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 242 | * |
| 243 | * @return If car supports audio context, then it returns the car audio context. Otherwise, |
| 244 | * it returns the physical stream that maps to this logical stream. |
| 245 | */ |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 246 | private int carContextToCarStream(int carContext) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 247 | if (mSupportedAudioContext == 0) { |
| 248 | int physicalStream = mPolicy.getPhysicalStreamForLogicalStream( |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 249 | AudioHalService.carContextToCarUsage(carContext)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 250 | return physicalStream; |
| 251 | } else { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 252 | return carContext; |
| 253 | } |
| 254 | } |
| 255 | |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 256 | private void writeVolumeToSettings(int carContext, int volume) { |
| 257 | String key = VolumeUtils.CAR_AUDIO_CONTEXT_SETTINGS.get(carContext); |
| 258 | if (key != null) { |
| 259 | Settings.Global.putInt(mContext.getContentResolver(), key, volume); |
| 260 | } |
| 261 | } |
| 262 | |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 263 | /** |
| 264 | * All updates to external components should be posted to this handler to avoid holding |
| 265 | * the internal lock while sending updates. |
| 266 | */ |
| 267 | private final class VolumeHandler extends Handler { |
| 268 | @Override |
| 269 | public void handleMessage(Message msg) { |
| 270 | int stream; |
| 271 | int volume; |
| 272 | switch (msg.what) { |
| 273 | case MSG_UPDATE_VOLUME: |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 274 | // arg1 is car context |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 275 | stream = msg.arg1; |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 276 | volume = (int) msg.obj; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 277 | int flag = msg.arg2; |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 278 | synchronized (CarExternalVolumeController.this) { |
| 279 | // the suppressed stream is sending us update.... |
| 280 | if (mShouldSuppress && stream == mSuppressUiForVolume[0]) { |
| 281 | // the volume matches, we want to suppress it |
| 282 | if (volume == mSuppressUiForVolume[1]) { |
| 283 | if (DBG) { |
| 284 | Log.d(TAG, "Suppress Volume UI for stream " |
| 285 | + stream + " volume: " + volume); |
| 286 | } |
| 287 | flag &= ~AudioManager.FLAG_SHOW_UI; |
| 288 | } |
| 289 | // No matter if the volume matches or not, we will stop suppressing |
| 290 | // UI for this stream now. After an audio context switch, user may |
| 291 | // quickly turn the nob, -1 and +1, it ends the same volume, |
| 292 | // but we should show the UI for both. |
| 293 | removeMessages(MSG_VOLUME_UI_RESTORE); |
| 294 | mShouldSuppress = false; |
| 295 | } |
| 296 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 297 | final int size = mVolumeControllers.beginBroadcast(); |
| 298 | try { |
| 299 | for (int i = 0; i < size; i++) { |
| 300 | try { |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 301 | mVolumeControllers.getBroadcastItem(i).volumeChanged( |
| 302 | VolumeUtils.carContextToAndroidStream(stream), flag); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 303 | } catch (RemoteException ignored) { |
| 304 | } |
| 305 | } |
| 306 | } finally { |
| 307 | mVolumeControllers.finishBroadcast(); |
| 308 | } |
| 309 | break; |
| 310 | case MSG_UPDATE_HAL: |
| 311 | stream = msg.arg1; |
| 312 | volume = msg.arg2; |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 313 | synchronized (CarExternalVolumeController.this) { |
| 314 | if (mMasterVolumeOnly) { |
| 315 | stream = 0; |
| 316 | } |
| 317 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 318 | mHal.setStreamVolume(stream, volume); |
| 319 | break; |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 320 | case MSG_SUPPRESS_UI_FOR_VOLUME: |
| 321 | if (DBG) { |
| 322 | Log.d(TAG, "Suppress stream volume " + msg.arg1 + " " + msg.arg2); |
| 323 | } |
| 324 | synchronized (CarExternalVolumeController.this) { |
| 325 | mShouldSuppress = true; |
| 326 | mSuppressUiForVolume[0] = msg.arg1; |
| 327 | mSuppressUiForVolume[1] = msg.arg2; |
| 328 | } |
| 329 | removeMessages(MSG_VOLUME_UI_RESTORE); |
| 330 | sendMessageDelayed(obtainMessage(MSG_VOLUME_UI_RESTORE), |
| 331 | HIDE_VOLUME_UI_MILLISECONDS); |
| 332 | break; |
| 333 | case MSG_VOLUME_UI_RESTORE: |
| 334 | if (DBG) { |
| 335 | Log.d(TAG, "Volume Ui suppress expired"); |
| 336 | } |
| 337 | synchronized (CarExternalVolumeController.this) { |
| 338 | mShouldSuppress = false; |
| 339 | } |
| 340 | break; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public CarExternalVolumeController(Context context, CarAudioService audioService, |
| 348 | AudioHalService hal, CarInputService inputService) { |
| 349 | mContext = context; |
| 350 | mAudioService = audioService; |
| 351 | mPolicy = audioService.getAudioRoutingPolicy(); |
| 352 | mHal = hal; |
| 353 | mInputService = inputService; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | void init() { |
| 358 | mSupportedAudioContext = mHal.getSupportedAudioVolumeContexts(); |
| 359 | mHasExternalMemory = mHal.isExternalAudioVolumePersistent(); |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 360 | mMasterVolumeOnly = mHal.isAudioVolumeMasterOnly(); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 361 | synchronized (this) { |
| 362 | initVolumeLimitLocked(); |
| 363 | initCurrentVolumeLocked(); |
| 364 | } |
| 365 | mInputService.setVolumeKeyListener(this); |
| 366 | mHal.setVolumeListener(this); |
| 367 | mAudioService.setAudioContextChangeListener(Looper.getMainLooper(), this); |
| 368 | } |
| 369 | |
| 370 | private void initVolumeLimitLocked() { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 371 | for (int i : VolumeUtils.CAR_AUDIO_CONTEXT) { |
| 372 | int carStream = carContextToCarStream(i); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 373 | Integer volumeMax = mHal.getStreamMaxVolume(carStream); |
| 374 | int max = volumeMax == null ? 0 : volumeMax; |
| 375 | if (max < 0) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 376 | max = 0; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 377 | } |
| 378 | // get default stream volume limit first. |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 379 | mCarContextVolumeMax.put(i, max); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | private void initCurrentVolumeLocked() { |
| 384 | if (mHasExternalMemory) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 385 | // TODO: read per context volume from audio hal. bug: 32091839 |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 386 | } else { |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 387 | // when vhal does not work, get call can take long. For that case, |
| 388 | // for the same physical streams, cache initial get results |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 389 | Map<Integer, Integer> volumesPerCarStream = |
| 390 | new ArrayMap<>(VolumeUtils.CAR_AUDIO_CONTEXT.length); |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 391 | for (int i : VolumeUtils.CAR_AUDIO_CONTEXT) { |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 392 | String key = VolumeUtils.CAR_AUDIO_CONTEXT_SETTINGS.get(i); |
| 393 | if (key != null) { |
| 394 | int vol = Settings.Global.getInt(mContext.getContentResolver(), key, -1); |
| 395 | if (vol >= 0) { |
| 396 | // Read valid volume for this car context from settings and continue; |
| 397 | mCurrentCarContextVolume.put(i, vol); |
| 398 | if (DBG) { |
| 399 | Log.d(TAG, "init volume from settings, car audio context: " |
| 400 | + i + " volume: " + vol); |
| 401 | } |
| 402 | continue; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // There is no settings for this car context. Use the current physical car |
| 407 | // stream volume as initial value instead, and put the volume into settings. |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 408 | int carStream = carContextToCarStream(i); |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 409 | Integer volume = volumesPerCarStream.get(carStream); |
| 410 | if (volume == null) { |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 411 | volume = Integer.valueOf(mHal.getStreamVolume(mMasterVolumeOnly ? 0 : |
| 412 | carStream)); |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 413 | volumesPerCarStream.put(carStream, volume); |
| 414 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 415 | mCurrentCarContextVolume.put(i, volume); |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 416 | writeVolumeToSettings(i, volume); |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 417 | if (DBG) { |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 418 | Log.d(TAG, "init volume from physical stream," + |
| 419 | " car audio context: " + i + " volume: " + volume); |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 420 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void setStreamVolume(int stream, int index, int flags) { |
| 427 | synchronized (this) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 428 | int carContext; |
| 429 | // Currently car context and android logical stream are not |
| 430 | // one-to-one mapping. In this API, Android side asks us to change a logical stream |
| 431 | // volume. If the current car audio context maps to this logical stream, then we |
| 432 | // change the volume for the current car audio context. Otherwise, we change the |
| 433 | // volume for the primary mapped car audio context. |
| 434 | if (VolumeUtils.carContextToAndroidStream(mCurrentContext) == stream) { |
| 435 | carContext = mCurrentContext; |
| 436 | } else { |
| 437 | carContext = VolumeUtils.androidStreamToCarContext(stream); |
| 438 | } |
| 439 | if (DBG) { |
| 440 | Log.d(TAG, "Receive setStreamVolume logical stream: " + stream + " index: " |
| 441 | + index + " flags: " + flags + " maps to car context: " + carContext); |
| 442 | } |
| 443 | setStreamVolumeInternalLocked(carContext, index, flags); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 447 | private void setStreamVolumeInternalLocked(int carContext, int index, int flags) { |
| 448 | if (mCarContextVolumeMax.get(carContext) == null) { |
| 449 | Log.e(TAG, "Stream type not supported " + carContext); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 450 | return; |
| 451 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 452 | int limit = mCarContextVolumeMax.get(carContext); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 453 | if (index > limit) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 454 | Log.w(TAG, "Volume exceeds volume limit. context: " + carContext |
| 455 | + " index: " + index + " limit: " + limit); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 456 | index = limit; |
| 457 | } |
| 458 | |
| 459 | if (index < 0) { |
| 460 | index = 0; |
| 461 | } |
| 462 | |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 463 | if (mCurrentCarContextVolume.get(carContext) == index) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 467 | int carStream = carContextToCarStream(carContext); |
| 468 | if (DBG) { |
| 469 | Log.d(TAG, "Change car stream volume, stream: " + carStream + " volume:" + index); |
| 470 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 471 | // For single channel, only adjust the volume when the audio context is the current one. |
| 472 | if (mCurrentContext == carContext) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 473 | if (DBG) { |
| 474 | Log.d(TAG, "Sending volume change to HAL"); |
| 475 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 476 | mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE_HAL, carStream, index)); |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 477 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { |
| 478 | if (mShouldSuppress && mSuppressUiForVolume[0] == carContext) { |
| 479 | // In this case, the caller explicitly says "Show_UI" for the same context. |
| 480 | // We will respect the flag, and let the UI show. |
| 481 | mShouldSuppress = false; |
| 482 | mHandler.removeMessages(MSG_VOLUME_UI_RESTORE); |
| 483 | } |
| 484 | } else { |
| 485 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SUPPRESS_UI_FOR_VOLUME, |
| 486 | carContext, index)); |
| 487 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 488 | } |
| 489 | // Record the current volume internally. |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 490 | mCurrentCarContextVolume.put(carContext, index); |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 491 | writeVolumeToSettings(mCurrentContext, index); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public int getStreamVolume(int stream) { |
| 496 | synchronized (this) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 497 | if (VolumeUtils.carContextToAndroidStream(mCurrentContext) == stream) { |
| 498 | return mCurrentCarContextVolume.get(mCurrentContext); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 499 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 500 | return mCurrentCarContextVolume.get(VolumeUtils.androidStreamToCarContext(stream)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void setVolumeController(IVolumeController controller) { |
| 506 | synchronized (this) { |
| 507 | mVolumeControllers.register(controller); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void onVolumeChange(int carStream, int volume, int volumeState) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 513 | synchronized (this) { |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 514 | int flag = getVolumeUpdateFlag(true); |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 515 | if (DBG) { |
| 516 | Log.d(TAG, "onVolumeChange carStream:" + carStream + " volume: " + volume |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 517 | + " volumeState: " + volumeState |
| 518 | + " suppressUI? " + mShouldSuppress |
| 519 | + " stream: " + mSuppressUiForVolume[0] |
| 520 | + " volume: " + mSuppressUiForVolume[1]); |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 521 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 522 | int currentCarStream = carContextToCarStream(mCurrentContext); |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 523 | if (mMasterVolumeOnly) { //for master volume only H/W, always assume current stream |
| 524 | carStream = currentCarStream; |
| 525 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 526 | if (currentCarStream == carStream) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 527 | mCurrentCarContextVolume.put(mCurrentContext, volume); |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 528 | writeVolumeToSettings(mCurrentContext, volume); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 529 | mHandler.sendMessage( |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 530 | mHandler.obtainMessage(MSG_UPDATE_VOLUME, mCurrentContext, flag, |
| 531 | new Integer(volume))); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 532 | } else { |
| 533 | // Hal is telling us a car stream volume has changed, but it is not the current |
| 534 | // stream. |
| 535 | Log.w(TAG, "Car stream" + carStream |
| 536 | + " volume changed, but it is not current stream, ignored."); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 541 | private int getVolumeUpdateFlag(boolean showUi) { |
| 542 | return showUi? AudioManager.FLAG_SHOW_UI : 0; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void onVolumeLimitChange(int streamNumber, int volume) { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 547 | // TODO: How should this update be sent to SystemUI? bug: 32095237 |
| 548 | // maybe send a volume update without showing UI. |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 549 | synchronized (this) { |
| 550 | initVolumeLimitLocked(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public int getStreamMaxVolume(int stream) { |
| 556 | synchronized (this) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 557 | if (VolumeUtils.carContextToAndroidStream(mCurrentContext) == stream) { |
| 558 | return mCarContextVolumeMax.get(mCurrentContext); |
| 559 | } else { |
| 560 | return mCarContextVolumeMax.get(VolumeUtils.androidStreamToCarContext(stream)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 561 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public int getStreamMinVolume(int stream) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 567 | return 0; // Min value is always zero. |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public boolean onKeyEvent(KeyEvent event) { |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 572 | if (!isVolumeKey(event)) { |
| 573 | return false; |
| 574 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 575 | final boolean down = event.getAction() == KeyEvent.ACTION_DOWN; |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 576 | if (DBG) { |
| 577 | Log.d(TAG, "Receive volume keyevent " + event.toString()); |
| 578 | } |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 579 | // TODO: properly handle long press on volume key, bug: 32095989 |
Yao Chen | f162811 | 2016-04-29 10:36:08 -0700 | [diff] [blame] | 580 | if (!down || interceptVolKeyBeforeDispatching(mContext)) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | |
| 584 | synchronized (this) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 585 | int currentVolume = mCurrentCarContextVolume.get(mCurrentContext); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 586 | switch (event.getKeyCode()) { |
| 587 | case KeyEvent.KEYCODE_VOLUME_UP: |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 588 | setStreamVolumeInternalLocked(mCurrentContext, currentVolume + 1, |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 589 | getVolumeUpdateFlag(true)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 590 | break; |
| 591 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 592 | setStreamVolumeInternalLocked(mCurrentContext, currentVolume - 1, |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 593 | getVolumeUpdateFlag(true)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public void onContextChange(int primaryFocusContext, int primaryFocusPhysicalStream) { |
| 602 | synchronized (this) { |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 603 | if(DBG) { |
| 604 | Log.d(TAG, "Audio context changed from " + mCurrentContext + " to: " |
| 605 | + primaryFocusContext + " physical: " + primaryFocusPhysicalStream); |
| 606 | } |
Yao Chen | 740c270 | 2016-05-11 15:30:25 -0700 | [diff] [blame] | 607 | // if primaryFocusContext is 0, it means nothing is playing or holding focus, |
| 608 | // we will keep the last focus context and if the user changes the volume |
| 609 | // it will go to the last audio context. |
| 610 | if (primaryFocusContext == mCurrentContext || primaryFocusContext == 0) { |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 611 | return; |
| 612 | } |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 613 | int oldContext = mCurrentContext; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 614 | mCurrentContext = primaryFocusContext; |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 615 | // if car supports audio context and has external memory, then we don't need to do |
| 616 | // anything. |
| 617 | if(mSupportedAudioContext != 0 && mHasExternalMemory) { |
| 618 | if (DBG) { |
| 619 | Log.d(TAG, "Car support audio context and has external memory," + |
| 620 | " no volume change needed from car service"); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 621 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 622 | return; |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 623 | } |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 624 | |
| 625 | // Otherwise, we need to tell Hal what the correct volume is for the new context. |
| 626 | int currentVolume = mCurrentCarContextVolume.get(primaryFocusContext); |
| 627 | |
Keun-young Park | d36a995 | 2016-05-24 10:03:59 -0700 | [diff] [blame] | 628 | int carStreamNumber = (mSupportedAudioContext == 0) ? primaryFocusPhysicalStream : |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 629 | primaryFocusContext; |
| 630 | if (DBG) { |
| 631 | Log.d(TAG, "Change volume from: " |
Yao Chen | 2612bb4 | 2016-08-19 16:19:17 -0700 | [diff] [blame] | 632 | + mCurrentCarContextVolume.get(oldContext) |
Yao Chen | 75279c9 | 2016-05-06 14:41:37 -0700 | [diff] [blame] | 633 | + " to: "+ currentVolume); |
| 634 | } |
Yao Chen | 4df5a28 | 2016-08-24 13:08:26 -0700 | [diff] [blame] | 635 | mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE_HAL, carStreamNumber, |
| 636 | currentVolume)); |
| 637 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SUPPRESS_UI_FOR_VOLUME, |
| 638 | mCurrentContext, currentVolume)); |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 641 | |
| 642 | @Override |
| 643 | public void dump(PrintWriter writer) { |
| 644 | writer.println("Volume controller:" + |
| 645 | CarExternalVolumeController.class.getSimpleName()); |
| 646 | synchronized (this) { |
| 647 | writer.println("mSupportedAudioContext:0x" + |
| 648 | Integer.toHexString(mSupportedAudioContext) + |
| 649 | ",mHasExternalMemory:" + mHasExternalMemory + |
| 650 | ",mMasterVolumeOnly:" + mMasterVolumeOnly); |
| 651 | writer.println("mCurrentContext:0x" + Integer.toHexString(mCurrentContext)); |
| 652 | writer.println("mCurrentCarContextVolume:"); |
| 653 | dumpVolumes(writer, mCurrentCarContextVolume); |
| 654 | writer.println("mCarContextVolumeMax:"); |
| 655 | dumpVolumes(writer, mCarContextVolumeMax); |
Keun-young Park | c837829 | 2016-07-08 16:02:26 -0700 | [diff] [blame] | 656 | writer.println("Number of volume controllers:" + |
| 657 | mVolumeControllers.getRegisteredCallbackCount()); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | private void dumpVolumes(PrintWriter writer, SparseArray<Integer> array) { |
| 662 | for (int i = 0; i < array.size(); i++) { |
| 663 | writer.println("0x" + Integer.toHexString(array.keyAt(i)) + ":" + array.valueAt(i)); |
| 664 | } |
| 665 | } |
Yao Chen | c4d442f | 2016-04-08 11:33:47 -0700 | [diff] [blame] | 666 | } |
| 667 | } |