The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 17 | package com.android.server.audio; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.REMOTE_AUDIO_PLAYBACK; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 20 | import static android.media.AudioManager.RINGER_MODE_NORMAL; |
| 21 | import static android.media.AudioManager.RINGER_MODE_SILENT; |
| 22 | import static android.media.AudioManager.RINGER_MODE_VIBRATE; |
| 23 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.app.ActivityManagerNative; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 27 | import android.app.KeyguardManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothA2dp; |
| 29 | import android.bluetooth.BluetoothAdapter; |
| 30 | import android.bluetooth.BluetoothClass; |
| 31 | import android.bluetooth.BluetoothDevice; |
| 32 | import android.bluetooth.BluetoothHeadset; |
| 33 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 34 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 35 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ContentResolver; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 39 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 41 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
| 43 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 44 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 45 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 46 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 47 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 48 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 49 | import android.media.AudioAttributes; |
| 50 | import android.media.AudioDevicePort; |
| 51 | import android.media.AudioSystem; |
| 52 | import android.media.AudioFormat; |
| 53 | import android.media.AudioManager; |
| 54 | import android.media.AudioManagerInternal; |
| 55 | import android.media.AudioPort; |
| 56 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 57 | import android.media.IAudioFocusDispatcher; |
| 58 | import android.media.IAudioRoutesObserver; |
| 59 | import android.media.IAudioService; |
| 60 | import android.media.IRemoteControlDisplay; |
| 61 | import android.media.IRingtonePlayer; |
| 62 | import android.media.IVolumeController; |
| 63 | import android.media.MediaPlayer; |
| 64 | import android.media.SoundPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.media.MediaPlayer.OnCompletionListener; |
| 66 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 67 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 68 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 69 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 71 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.os.Environment; |
| 73 | import android.os.Handler; |
| 74 | import android.os.IBinder; |
| 75 | import android.os.Looper; |
| 76 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 77 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 78 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 80 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 81 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 82 | import android.os.UserHandle; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 83 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.provider.Settings; |
| 85 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 86 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 87 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 89 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 90 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 91 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 92 | import android.view.KeyEvent; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 93 | import android.view.OrientationEventListener; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 94 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 95 | import android.view.WindowManager; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 96 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 98 | import com.android.internal.util.XmlUtils; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 99 | import com.android.server.LocalServices; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 100 | |
| 101 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 103 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 105 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 106 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 108 | import java.util.HashMap; |
| 109 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 110 | import java.util.List; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 111 | import java.util.Map; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 112 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 113 | import java.util.Objects; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 114 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * The implementation of the volume manager service. |
| 118 | * <p> |
| 119 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 120 | * asynchronous to external calls. For example, the task of setting a volume |
| 121 | * will update our internal state, but in a separate thread will set the system |
| 122 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 123 | * will update the state and broadcast a change and in a separate thread later |
| 124 | * persist the ringer mode. |
| 125 | * |
| 126 | * @hide |
| 127 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 128 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | private static final String TAG = "AudioService"; |
| 131 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 132 | /** Debug audio mode */ |
| 133 | protected static final boolean DEBUG_MODE = Log.isLoggable(TAG + ".MOD", Log.DEBUG); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 134 | |
| 135 | /** Debug audio policy feature */ |
| 136 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 137 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 138 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 139 | protected static final boolean DEBUG_VOL = Log.isLoggable(TAG + ".VOL", Log.DEBUG); |
Jean-Michel Trivi | 18e7bce | 2011-08-26 12:11:36 -0700 | [diff] [blame] | 140 | |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 141 | /** debug calls to media session apis */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 142 | private static final boolean DEBUG_SESSIONS = Log.isLoggable(TAG + ".SESSIONS", Log.DEBUG); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 143 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 144 | /** Allow volume changes to set ringer mode to silent? */ |
| 145 | private static final boolean VOLUME_SETS_RINGER_MODE_SILENT = false; |
| 146 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 147 | /** In silent mode, are volume adjustments (raises) prevented? */ |
| 148 | private static final boolean PREVENT_VOLUME_ADJUSTMENT_IF_SILENT = true; |
| 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 151 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 153 | /** How long to delay after a volume down event before unmuting a stream */ |
| 154 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 155 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 156 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 157 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 158 | */ |
| 159 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 160 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 161 | private final Context mContext; |
| 162 | private final ContentResolver mContentResolver; |
| 163 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 164 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 165 | // the platform type affects volume and silent mode behavior |
| 166 | private final int mPlatformType; |
| 167 | |
| 168 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 169 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 173 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 174 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 175 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 176 | /** The controller for the volume UI. */ |
| 177 | private final VolumeController mVolumeController = new VolumeController(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 178 | private final ControllerService mControllerService = new ControllerService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
| 180 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | /** If the msg is already queued, replace it with this one. */ |
| 182 | private static final int SENDMSG_REPLACE = 0; |
| 183 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 184 | private static final int SENDMSG_NOOP = 1; |
| 185 | /** If the msg is already queued, queue this one and leave the old. */ |
| 186 | private static final int SENDMSG_QUEUE = 2; |
| 187 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 188 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 189 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | private static final int MSG_PERSIST_VOLUME = 1; |
| 191 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 192 | private static final int MSG_MEDIA_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 193 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 194 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 195 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 196 | private static final int MSG_SET_FORCE_USE = 8; |
| 197 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 198 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 199 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 200 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 201 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 202 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 203 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 204 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 205 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 206 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 207 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 208 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 209 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 210 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 211 | private static final int MSG_PERSIST_MICROPHONE_MUTE = 23; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 212 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 213 | // start of messages handled under wakelock |
| 214 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 215 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 216 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 217 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 218 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 219 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 220 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 221 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 222 | // Timeout for connection to bluetooth headset service |
| 223 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | /** @see AudioSystemThread */ |
| 226 | private AudioSystemThread mAudioSystemThread; |
| 227 | /** @see AudioHandler */ |
| 228 | private AudioHandler mAudioHandler; |
| 229 | /** @see VolumeStreamState */ |
| 230 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 231 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 232 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 233 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 234 | // protects mRingerMode |
| 235 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 238 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 241 | // Maximum volume adjust steps allowed in a single batch call. |
| 242 | private static final int MAX_BATCH_VOLUME_ADJUST_STEPS = 4; |
| 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | /* Sound effect file names */ |
| 245 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 246 | private static final List<String> SOUND_EFFECT_FILES = new ArrayList<String>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
| 248 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 249 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 250 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 251 | private final int[][] SOUND_EFFECT_FILES_MAP = new int[AudioManager.NUM_SOUND_EFFECTS][2]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 253 | /** @hide Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 254 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 255 | 5, // STREAM_VOICE_CALL |
| 256 | 7, // STREAM_SYSTEM |
| 257 | 7, // STREAM_RING |
| 258 | 15, // STREAM_MUSIC |
| 259 | 7, // STREAM_ALARM |
| 260 | 7, // STREAM_NOTIFICATION |
| 261 | 15, // STREAM_BLUETOOTH_SCO |
| 262 | 7, // STREAM_SYSTEM_ENFORCED |
| 263 | 15, // STREAM_DTMF |
| 264 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 265 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 266 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 267 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 268 | * of another stream: This avoids multiplying the volume settings for hidden |
| 269 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 270 | * NOTE: do not create loops in aliases! |
| 271 | * Some streams alias to different streams according to device category (phone or tablet) or |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 272 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 273 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 274 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 275 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 276 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 277 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 278 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 279 | AudioSystem.STREAM_RING, // STREAM_RING |
| 280 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 281 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 282 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 283 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 284 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 285 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 286 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 287 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 288 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 289 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 290 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 291 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 292 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 293 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 294 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 295 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 296 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 297 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 298 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 299 | }; |
| 300 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 301 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 302 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 303 | AudioSystem.STREAM_RING, // STREAM_RING |
| 304 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 305 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 306 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 307 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 308 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 309 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 310 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 311 | }; |
| 312 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 313 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 316 | * after mapping through mStreamVolumeAlias. |
| 317 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 318 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 319 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 320 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 321 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 322 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 323 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 324 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 325 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 326 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 327 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 328 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 329 | }; |
| 330 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 331 | private final boolean mUseFixedVolume; |
| 332 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 333 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | public void onError(int error) { |
| 335 | switch (error) { |
| 336 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 337 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, |
| 338 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | break; |
| 340 | default: |
| 341 | break; |
| 342 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 343 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | /** |
| 347 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 348 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 349 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 350 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 351 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 352 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 353 | private int mRingerModeExternal = -1; // reported ringer mode to outside clients (AudioManager) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 355 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 356 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 357 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 358 | // Streams currently muted by ringer mode |
| 359 | private int mRingerModeMutedStreams; |
| 360 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 361 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 362 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | private int mMuteAffectedStreams; |
| 364 | |
| 365 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 366 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 367 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 368 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | */ |
| 370 | private int mVibrateSetting; |
| 371 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 372 | // Is there a vibrator |
| 373 | private final boolean mHasVibrator; |
| 374 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 375 | // Broadcast receiver for device connections intent broadcasts |
| 376 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 377 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 378 | // Devices currently connected |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 379 | private final HashMap <Integer, String> mConnectedDevices = new HashMap <Integer, String>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 380 | |
| 381 | // Forced device usage for communications |
| 382 | private int mForcedUseForComm; |
| 383 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 384 | // List of binder death handlers for setMode() client processes. |
| 385 | // The last process to have called setMode() is at the top of the list. |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 386 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 387 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 388 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 389 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 390 | |
| 391 | // BluetoothHeadset API to control SCO connection |
| 392 | private BluetoothHeadset mBluetoothHeadset; |
| 393 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 394 | // Bluetooth headset device |
| 395 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 396 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 397 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 398 | // audio service (internal) or bluetooth headset (external) |
| 399 | private int mScoAudioState; |
| 400 | // SCO audio state is not active |
| 401 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 402 | // SCO audio activation request waiting for headset service to connect |
| 403 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 404 | // SCO audio state is active or starting due to a request from AudioManager API |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 405 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 406 | // SCO audio deactivation request waiting for headset service to connect |
| 407 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 408 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 409 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 410 | // in call audio) |
| 411 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 412 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 413 | // waiting for headset service to connect |
| 414 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 415 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 416 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 417 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 418 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 419 | // SCO audio mode is undefined |
| 420 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 421 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 422 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 423 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 424 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 425 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 426 | private static final int SCO_MODE_VR = 2; |
| 427 | |
| 428 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 429 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 430 | // Current connection state indicated by bluetooth headset |
| 431 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 432 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 433 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 434 | private boolean mSystemReady; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 435 | // listener for SoundPool sample load completion indication |
| 436 | private SoundPoolCallback mSoundPoolCallBack; |
| 437 | // thread for SoundPool listener |
| 438 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 439 | // message looper for SoundPool listener |
| 440 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 441 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 442 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 443 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 444 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 445 | // Keyguard manager proxy |
| 446 | private KeyguardManager mKeyguardManager; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 447 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 448 | // is controlled by Vol keys. |
| 449 | private int mVolumeControlStream = -1; |
| 450 | private final Object mForceControlStreamLock = new Object(); |
| 451 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 452 | // server process so in theory it is not necessary to monitor the client death. |
| 453 | // However it is good to be ready for future evolutions. |
| 454 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 455 | // Used to play ringtones outside system_server |
| 456 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 457 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 458 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 459 | private int mDeviceRotation = Surface.ROTATION_0; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 460 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 461 | // Request to override default use of A2DP for media. |
| 462 | private boolean mBluetoothA2dpEnabled; |
| 463 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 464 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 465 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 466 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 467 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 468 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 469 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 470 | // Devices for which the volume is fixed and VolumePanel slider should be disabled |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 471 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 472 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 473 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 474 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 475 | AudioSystem.DEVICE_OUT_SPDIF | |
| 476 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 477 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 478 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 479 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 480 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 481 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 482 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 483 | private boolean mDockAudioMediaEnabled = true; |
| 484 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 485 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 486 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 487 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 488 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 489 | // and used later when/if disableSafeMediaVolume() is called. |
| 490 | private StreamVolumeCommand mPendingVolumeCommand; |
| 491 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 492 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 493 | |
| 494 | private final MediaFocusControl mMediaFocusControl; |
| 495 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 496 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 497 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 498 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 499 | private final Object mA2dpAvrcpLock = new Object(); |
| 500 | // If absolute volume is supported in AVRCP device |
| 501 | private boolean mAvrcpAbsVolSupported = false; |
| 502 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 503 | private AudioOrientationEventListener mOrientationListener; |
| 504 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 505 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 506 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 507 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
| 508 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 509 | // Intent "extra" data keys. |
| 510 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 511 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 512 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 513 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 514 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 515 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 516 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 517 | |
| 518 | // Defines the format for the connection "address" for ALSA devices |
| 519 | public static String makeAlsaAddressString(int card, int device) { |
| 520 | return "card=" + card + ";device=" + device + ";"; |
| 521 | } |
| 522 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | /////////////////////////////////////////////////////////////////////////// |
| 524 | // Construction |
| 525 | /////////////////////////////////////////////////////////////////////////// |
| 526 | |
| 527 | /** @hide */ |
| 528 | public AudioService(Context context) { |
| 529 | mContext = context; |
| 530 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 531 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 532 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 533 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 534 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 535 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 536 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 537 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 538 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 539 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 540 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 541 | // Intialized volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 542 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 543 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 544 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 545 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 546 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 547 | } |
| 548 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 549 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 550 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 551 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 552 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 553 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 554 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 555 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 556 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 557 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 558 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 561 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 562 | mMediaFocusControl = new MediaFocusControl(mAudioHandler.getLooper(), |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 563 | mContext, mVolumeController, this); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 564 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 565 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 566 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 567 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 568 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 569 | sendMsg(mAudioHandler, |
| 570 | MSG_SET_FORCE_USE, |
| 571 | SENDMSG_QUEUE, |
| 572 | AudioSystem.FOR_SYSTEM, |
| 573 | cameraSoundForced ? |
| 574 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 575 | null, |
| 576 | 0); |
| 577 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 578 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 579 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 580 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 581 | // The default safe volume index read here will be replaced by the actual value when |
| 582 | // the mcc is read by onConfigureSafeVolume() |
| 583 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 584 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 585 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 586 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 587 | com.android.internal.R.bool.config_useFixedVolume); |
| 588 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 589 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 590 | // array initialized by updateStreamVolumeAlias() |
| 591 | updateStreamVolumeAlias(false /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 593 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 594 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 595 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 596 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 597 | |
| 598 | // Call setRingerModeInt() to apply correct mute |
| 599 | // state on streams affected by ringer mode. |
| 600 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 601 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 602 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 603 | // Register for device connection intent broadcasts. |
| 604 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 605 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 606 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 607 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 608 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 609 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 610 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 611 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 612 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 613 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 614 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 615 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 616 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 617 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 618 | // initialize orientation in AudioSystem |
| 619 | setOrientationForAudioSystem(); |
| 620 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 621 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 622 | if (mMonitorRotation) { |
| 623 | mDeviceRotation = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 624 | .getDefaultDisplay().getRotation(); |
| 625 | Log.v(TAG, "monitoring device rotation, initial=" + mDeviceRotation); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 626 | |
| 627 | mOrientationListener = new AudioOrientationEventListener(mContext); |
| 628 | mOrientationListener.enable(); |
| 629 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 630 | // initialize rotation in AudioSystem |
| 631 | setRotationForAudioSystem(); |
| 632 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 633 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 634 | context.registerReceiver(mReceiver, intentFilter); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 635 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 636 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 639 | public void systemReady() { |
| 640 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 641 | 0, 0, null, 0); |
| 642 | } |
| 643 | |
| 644 | public void onSystemReady() { |
| 645 | mSystemReady = true; |
| 646 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 647 | 0, 0, null, 0); |
| 648 | |
| 649 | mKeyguardManager = |
| 650 | (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 651 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 652 | resetBluetoothSco(); |
| 653 | getBluetoothHeadset(); |
| 654 | //FIXME: this is to maintain compatibility with deprecated intent |
| 655 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 656 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 657 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 658 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 659 | sendStickyBroadcastToAll(newIntent); |
| 660 | |
| 661 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 662 | if (adapter != null) { |
| 663 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 664 | BluetoothProfile.A2DP); |
| 665 | } |
| 666 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 667 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 668 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 669 | if (mHdmiManager != null) { |
| 670 | synchronized (mHdmiManager) { |
| 671 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 672 | if (mHdmiTvClient != null) { |
| 673 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 674 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 675 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 676 | mHdmiCecSink = false; |
| 677 | } |
| 678 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 679 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 680 | sendMsg(mAudioHandler, |
| 681 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 682 | SENDMSG_REPLACE, |
| 683 | 0, |
| 684 | 0, |
| 685 | null, |
| 686 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 687 | |
| 688 | StreamOverride.init(mContext); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 689 | mControllerService.init(); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 690 | } |
| 691 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | private void createAudioSystemThread() { |
| 693 | mAudioSystemThread = new AudioSystemThread(); |
| 694 | mAudioSystemThread.start(); |
| 695 | waitForAudioHandlerCreation(); |
| 696 | } |
| 697 | |
| 698 | /** Waits for the volume handler to be created by the other thread. */ |
| 699 | private void waitForAudioHandlerCreation() { |
| 700 | synchronized(this) { |
| 701 | while (mAudioHandler == null) { |
| 702 | try { |
| 703 | // Wait for mAudioHandler to be set by the other thread |
| 704 | wait(); |
| 705 | } catch (InterruptedException e) { |
| 706 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 712 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 713 | synchronized (VolumeStreamState.class) { |
| 714 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 715 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 716 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 717 | mStreamStates[streamType]. |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 718 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]]); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 719 | } |
| 720 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 721 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 722 | mStreamStates[streamType].applyAllVolumes(); |
| 723 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 728 | private void checkAllFixedVolumeDevices() |
| 729 | { |
| 730 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 731 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 732 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 733 | } |
| 734 | } |
| 735 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 736 | private void checkAllFixedVolumeDevices(int streamType) { |
| 737 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 738 | } |
| 739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 742 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 743 | |
| 744 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 745 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 748 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 749 | checkAllAliasStreamVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 752 | private void dumpStreamStates(PrintWriter pw) { |
| 753 | pw.println("\nStream volumes (device: index)"); |
| 754 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 755 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 756 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 757 | mStreamStates[i].dump(pw); |
| 758 | pw.println(""); |
| 759 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 760 | pw.print("\n- mute affected streams = 0x"); |
| 761 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 764 | private void updateStreamVolumeAlias(boolean updateVolumes) { |
| 765 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 766 | |
| 767 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 768 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 769 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 770 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 771 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 772 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 773 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 774 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 775 | break; |
| 776 | default: |
| 777 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 778 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 779 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 780 | |
| 781 | if (isPlatformTelevision()) { |
| 782 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 783 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 784 | if (isInCommunication()) { |
| 785 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 786 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 787 | } else { |
| 788 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 789 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 790 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 791 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 792 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 793 | if (updateVolumes) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 794 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias]); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 795 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 796 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 797 | sendMsg(mAudioHandler, |
| 798 | MSG_SET_ALL_VOLUMES, |
| 799 | SENDMSG_QUEUE, |
| 800 | 0, |
| 801 | 0, |
| 802 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 803 | } |
| 804 | } |
| 805 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 806 | private void readDockAudioSettings(ContentResolver cr) |
| 807 | { |
| 808 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 809 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 810 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 811 | sendMsg(mAudioHandler, |
| 812 | MSG_SET_FORCE_USE, |
| 813 | SENDMSG_QUEUE, |
| 814 | AudioSystem.FOR_DOCK, |
| 815 | mDockAudioMediaEnabled ? |
| 816 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 817 | null, |
| 818 | 0); |
| 819 | } |
| 820 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | private void readPersistedSettings() { |
| 822 | final ContentResolver cr = mContentResolver; |
| 823 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 824 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 825 | Settings.Global.getInt( |
| 826 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 827 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 828 | // sanity check in case the settings are restored from a device with incompatible |
| 829 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 830 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 831 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 832 | } |
| 833 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 834 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 835 | } |
| 836 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 837 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 838 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 839 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 840 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 841 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 842 | synchronized(mSettingsLock) { |
| 843 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 844 | if (mRingerModeExternal == -1) { |
| 845 | mRingerModeExternal = mRingerMode; |
| 846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 848 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 849 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 850 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 851 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 852 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 853 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 854 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 855 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 856 | AudioManager.VIBRATE_TYPE_RINGER, |
| 857 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 858 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 860 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 861 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 862 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 863 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 864 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 865 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 866 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 868 | boolean masterMute = System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 869 | 0, UserHandle.USER_CURRENT) == 1; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 870 | if (mUseFixedVolume) { |
| 871 | masterMute = false; |
| 872 | AudioSystem.setMasterVolume(1.0f); |
| 873 | } |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 874 | AudioSystem.setMasterMute(masterMute); |
| 875 | broadcastMasterMuteStatus(masterMute); |
| 876 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 877 | boolean microphoneMute = |
| 878 | System.getIntForUser(cr, System.MICROPHONE_MUTE, 0, UserHandle.USER_CURRENT) == 1; |
| 879 | AudioSystem.muteMicrophone(microphoneMute); |
| 880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | // Each stream will read its own persisted settings |
| 882 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 883 | // Broadcast the sticky intents |
| 884 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 885 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | |
| 887 | // Broadcast vibrate settings |
| 888 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 889 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 890 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 891 | // Load settings for the volume controller |
| 892 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 895 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 896 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 897 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 899 | private class AudioOrientationEventListener |
| 900 | extends OrientationEventListener { |
| 901 | public AudioOrientationEventListener(Context context) { |
| 902 | super(context); |
| 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public void onOrientationChanged(int orientation) { |
| 907 | //Even though we're responding to phone orientation events, |
| 908 | //use display rotation so audio stays in sync with video/dialogs |
| 909 | int newRotation = ((WindowManager) mContext.getSystemService( |
| 910 | Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); |
| 911 | if (newRotation != mDeviceRotation) { |
| 912 | mDeviceRotation = newRotation; |
| 913 | setRotationForAudioSystem(); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | /////////////////////////////////////////////////////////////////////////// |
| 919 | // IPC methods |
| 920 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 922 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 923 | String callingPackage) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 924 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 925 | Binder.getCallingUid()); |
| 926 | } |
| 927 | |
| 928 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 929 | String callingPackage, int uid) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 930 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream="+suggestedStreamType |
| 931 | + ", flags=" + flags); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 932 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 933 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 934 | if (mVolumeControlStream != -1) { |
| 935 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 936 | } else { |
| 937 | streamType = getActiveStreamType(suggestedStreamType); |
| 938 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 939 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 941 | // Play sounds on STREAM_RING only. |
| 942 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 943 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 945 | } |
| 946 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 947 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 948 | // Don't suppress mute/unmute requests |
| 949 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 950 | direction = 0; |
| 951 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 952 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 953 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 954 | } |
| 955 | |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 956 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 960 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 961 | String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 962 | adjustStreamVolume(streamType, direction, flags, callingPackage, Binder.getCallingUid()); |
| 963 | } |
| 964 | |
| 965 | private void adjustStreamVolume(int streamType, int direction, int flags, |
| 966 | String callingPackage, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 967 | if (mUseFixedVolume) { |
| 968 | return; |
| 969 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 970 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream="+streamType+", dir="+direction |
| 971 | + ", flags="+flags); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | ensureValidDirection(direction); |
| 974 | ensureValidStreamType(streamType); |
| 975 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 976 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 977 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 978 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 979 | return; |
| 980 | } |
| 981 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 982 | // use stream type alias here so that streams with same alias have the same behavior, |
| 983 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 984 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 985 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 986 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 987 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 988 | |
| 989 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 990 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 991 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 993 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 994 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 995 | // skip a2dp absolute volume control request when the device |
| 996 | // is not an a2dp device |
| 997 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 998 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 999 | return; |
| 1000 | } |
| 1001 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1002 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1003 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1007 | // reset any pending volume command |
| 1008 | synchronized (mSafeMediaVolumeState) { |
| 1009 | mPendingVolumeCommand = null; |
| 1010 | } |
| 1011 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1012 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1013 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1014 | ((device & mFixedVolumeDevices) != 0)) { |
| 1015 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1016 | |
| 1017 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1018 | // volume is enforced, and max and 0 for the others. |
| 1019 | // This is simulated by stepping by the full allowed volume range |
| 1020 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1021 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1022 | step = mSafeMediaVolumeIndex; |
| 1023 | } else { |
| 1024 | step = streamState.getMaxIndex(); |
| 1025 | } |
| 1026 | if (aliasIndex != 0) { |
| 1027 | aliasIndex = step; |
| 1028 | } |
| 1029 | } else { |
| 1030 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1031 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1032 | } |
| 1033 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1034 | // If either the client forces allowing ringer modes for this adjustment, |
| 1035 | // or the stream type is one that is affected by ringer modes |
| 1036 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1037 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1038 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1039 | // do not vibrate if already in vibrate mode |
| 1040 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1041 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1042 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1043 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1044 | // need to adjust the volume further. |
| 1045 | final int result = checkForRingerModeChange(aliasIndex, direction, step, streamState.mIsMuted); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1046 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1047 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1048 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1049 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1050 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1051 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1052 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1053 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1054 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1055 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1056 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1057 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1058 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1059 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1060 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1061 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1062 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1063 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1064 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1065 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1066 | synchronized (mA2dpAvrcpLock) { |
| 1067 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1068 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1069 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1072 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1073 | if (isMuteAdjust) { |
| 1074 | boolean state; |
| 1075 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1076 | state = !streamState.mIsMuted; |
| 1077 | } else { |
| 1078 | state = direction == AudioManager.ADJUST_MUTE; |
| 1079 | } |
| 1080 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1081 | setSystemAudioMute(state); |
| 1082 | } |
| 1083 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1084 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
| 1085 | mStreamStates[stream].mute(state); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1089 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1090 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1091 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1092 | } else if (streamState.adjustIndex(direction * step, device) || streamState.mIsMuted) { |
| 1093 | // Post message to set system volume (it in turn will post a |
| 1094 | // message to persist). |
| 1095 | if (streamState.mIsMuted) { |
| 1096 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1097 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1098 | // unmute immediately for volume up |
| 1099 | streamState.mute(false); |
| 1100 | } else if (direction == AudioManager.ADJUST_LOWER) { |
| 1101 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1102 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1103 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1104 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1105 | sendMsg(mAudioHandler, |
| 1106 | MSG_SET_DEVICE_VOLUME, |
| 1107 | SENDMSG_QUEUE, |
| 1108 | device, |
| 1109 | 0, |
| 1110 | streamState, |
| 1111 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1112 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1113 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1114 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1115 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1116 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1117 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1118 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1119 | if (mHdmiManager != null) { |
| 1120 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1121 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1122 | if (mHdmiCecSink && |
| 1123 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1124 | oldIndex != newIndex) { |
| 1125 | synchronized (mHdmiPlaybackClient) { |
| 1126 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1127 | KeyEvent.KEYCODE_VOLUME_UP; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1128 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1129 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1130 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1134 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1135 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1136 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1139 | // Called after a delay when volume down is pressed while muted |
| 1140 | private void onUnmuteStream(int stream, int flags) { |
| 1141 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1142 | streamState.mute(false); |
| 1143 | |
| 1144 | final int device = getDeviceForStream(stream); |
| 1145 | final int index = mStreamStates[stream].getIndex(device); |
| 1146 | sendVolumeUpdate(stream, index, index, flags); |
| 1147 | } |
| 1148 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1149 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1150 | if (mHdmiManager == null |
| 1151 | || mHdmiTvClient == null |
| 1152 | || oldVolume == newVolume |
| 1153 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1154 | |
| 1155 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1156 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1157 | synchronized (mHdmiManager) { |
| 1158 | if (!mHdmiSystemAudioSupported) return; |
| 1159 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1160 | final long token = Binder.clearCallingIdentity(); |
| 1161 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1162 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1163 | } finally { |
| 1164 | Binder.restoreCallingIdentity(token); |
| 1165 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1170 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1171 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1172 | class StreamVolumeCommand { |
| 1173 | public final int mStreamType; |
| 1174 | public final int mIndex; |
| 1175 | public final int mFlags; |
| 1176 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1177 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1178 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1179 | mStreamType = streamType; |
| 1180 | mIndex = index; |
| 1181 | mFlags = flags; |
| 1182 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1183 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1184 | |
| 1185 | @Override |
| 1186 | public String toString() { |
| 1187 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1188 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1189 | .append(mDevice).append('}').toString(); |
| 1190 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1191 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1192 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1193 | private void onSetStreamVolume(int streamType, int index, int flags, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1194 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1195 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1196 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1197 | (mStreamVolumeAlias[streamType] == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1198 | int newRingerMode; |
| 1199 | if (index == 0) { |
| 1200 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1201 | : VOLUME_SETS_RINGER_MODE_SILENT ? AudioManager.RINGER_MODE_SILENT |
| 1202 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1203 | } else { |
| 1204 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1205 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1206 | setRingerMode(newRingerMode, TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1207 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1211 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1212 | setStreamVolume(streamType, index, flags, callingPackage, Binder.getCallingUid()); |
| 1213 | } |
| 1214 | |
| 1215 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
| 1216 | int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1217 | if (mUseFixedVolume) { |
| 1218 | return; |
| 1219 | } |
| 1220 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1221 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1222 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1223 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1224 | |
| 1225 | final int device = getDeviceForStream(streamType); |
| 1226 | int oldIndex; |
| 1227 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1228 | // skip a2dp absolute volume control request when the device |
| 1229 | // is not an a2dp device |
| 1230 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1231 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1232 | return; |
| 1233 | } |
| 1234 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1235 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1236 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1240 | synchronized (mSafeMediaVolumeState) { |
| 1241 | // reset any pending volume command |
| 1242 | mPendingVolumeCommand = null; |
| 1243 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1244 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1245 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1246 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1247 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1248 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1249 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1250 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1251 | synchronized (mA2dpAvrcpLock) { |
| 1252 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1253 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1254 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1258 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1259 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1260 | } |
| 1261 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1262 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1263 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1264 | ((device & mFixedVolumeDevices) != 0)) { |
| 1265 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1266 | |
| 1267 | // volume is either 0 or max allowed for fixed volume devices |
| 1268 | if (index != 0) { |
| 1269 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1270 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1271 | index = mSafeMediaVolumeIndex; |
| 1272 | } else { |
| 1273 | index = streamState.getMaxIndex(); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1278 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1279 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1280 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1281 | streamType, index, flags, device); |
| 1282 | } else { |
| 1283 | onSetStreamVolume(streamType, index, flags, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1284 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1287 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1290 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1291 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1292 | synchronized(mForceControlStreamLock) { |
| 1293 | mVolumeControlStream = streamType; |
| 1294 | if (mVolumeControlStream == -1) { |
| 1295 | if (mForceControlStreamClient != null) { |
| 1296 | mForceControlStreamClient.release(); |
| 1297 | mForceControlStreamClient = null; |
| 1298 | } |
| 1299 | } else { |
| 1300 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1306 | private IBinder mCb; // To be notified of client's death |
| 1307 | |
| 1308 | ForceControlStreamClient(IBinder cb) { |
| 1309 | if (cb != null) { |
| 1310 | try { |
| 1311 | cb.linkToDeath(this, 0); |
| 1312 | } catch (RemoteException e) { |
| 1313 | // Client has died! |
| 1314 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1315 | cb = null; |
| 1316 | } |
| 1317 | } |
| 1318 | mCb = cb; |
| 1319 | } |
| 1320 | |
| 1321 | public void binderDied() { |
| 1322 | synchronized(mForceControlStreamLock) { |
| 1323 | Log.w(TAG, "SCO client died"); |
| 1324 | if (mForceControlStreamClient != this) { |
| 1325 | Log.w(TAG, "unregistered control stream client died"); |
| 1326 | } else { |
| 1327 | mForceControlStreamClient = null; |
| 1328 | mVolumeControlStream = -1; |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | public void release() { |
| 1334 | if (mCb != null) { |
| 1335 | mCb.unlinkToDeath(this, 0); |
| 1336 | mCb = null; |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1341 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1342 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1343 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1344 | final long ident = Binder.clearCallingIdentity(); |
| 1345 | try { |
| 1346 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1347 | } finally { |
| 1348 | Binder.restoreCallingIdentity(ident); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1353 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1354 | final long ident = Binder.clearCallingIdentity(); |
| 1355 | try { |
| 1356 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1357 | } finally { |
| 1358 | Binder.restoreCallingIdentity(ident); |
| 1359 | } |
| 1360 | } |
| 1361 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1362 | // UI update and Broadcast Intent |
| 1363 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1364 | if (!isPlatformVoice() && (streamType == AudioSystem.STREAM_RING)) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1365 | streamType = AudioSystem.STREAM_NOTIFICATION; |
| 1366 | } |
| 1367 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1368 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1369 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1370 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1371 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1374 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1375 | // receives volume notification from Audio Receiver. |
| 1376 | private int updateFlagsForSystemAudio(int flags) { |
| 1377 | if (mHdmiTvClient != null) { |
| 1378 | synchronized (mHdmiTvClient) { |
| 1379 | if (mHdmiSystemAudioSupported && |
| 1380 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1381 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | return flags; |
| 1386 | } |
| 1387 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1388 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1389 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1390 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1391 | broadcastMasterMuteStatus(muted); |
| 1392 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1393 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1394 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1395 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1396 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1397 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1398 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1399 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | * Sets the stream state's index, and posts a message to set system volume. |
| 1404 | * This will not call out to the UI. Assumes a valid stream type. |
| 1405 | * |
| 1406 | * @param streamType Type of the stream |
| 1407 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1408 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | * @param force If true, set the volume even if the desired volume is same |
| 1410 | * as the current volume. |
| 1411 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1412 | private void setStreamVolumeInt(int streamType, |
| 1413 | int index, |
| 1414 | int device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1415 | boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1417 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1418 | if (streamState.setIndex(index, device) || force) { |
| 1419 | // Post message to set system volume (it in turn will post a message |
| 1420 | // to persist). |
| 1421 | sendMsg(mAudioHandler, |
| 1422 | MSG_SET_DEVICE_VOLUME, |
| 1423 | SENDMSG_QUEUE, |
| 1424 | device, |
| 1425 | 0, |
| 1426 | streamState, |
| 1427 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1431 | private void setSystemAudioMute(boolean state) { |
| 1432 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1433 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1434 | if (!mHdmiSystemAudioSupported) return; |
| 1435 | synchronized (mHdmiTvClient) { |
| 1436 | final long token = Binder.clearCallingIdentity(); |
| 1437 | try { |
| 1438 | mHdmiTvClient.setSystemAudioMute(state); |
| 1439 | } finally { |
| 1440 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1441 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1446 | /** get stream mute state. */ |
| 1447 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1448 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1449 | streamType = getActiveStreamType(streamType); |
| 1450 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1451 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1452 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1453 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1456 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1457 | private IBinder mICallback; // To be notified of client's death |
| 1458 | |
| 1459 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1460 | mICallback = cb; |
| 1461 | try { |
| 1462 | cb.linkToDeath(this, 0/*flags*/); |
| 1463 | } catch (RemoteException e) { |
| 1464 | Log.e(TAG, "can't link to death", e); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | boolean isHandlerFor(IBinder cb) { |
| 1469 | return mICallback.equals(cb); |
| 1470 | } |
| 1471 | |
| 1472 | void forget() { |
| 1473 | try { |
| 1474 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1475 | } catch (NoSuchElementException e) { |
| 1476 | Log.e(TAG, "error unlinking to death", e); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | public void binderDied() { |
| 1481 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1482 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1488 | * @return true if there is a registered death handler, false otherwise */ |
| 1489 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1490 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1491 | while (it.hasNext()) { |
| 1492 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1493 | if (handler.isHandlerFor(cb)) { |
| 1494 | handler.forget(); |
| 1495 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1496 | return true; |
| 1497 | } |
| 1498 | } |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
| 1502 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1503 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1504 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1505 | while (it.hasNext()) { |
| 1506 | if (it.next().isHandlerFor(cb)) { |
| 1507 | return true; |
| 1508 | } |
| 1509 | } |
| 1510 | return false; |
| 1511 | } |
| 1512 | |
| 1513 | private int mRmtSbmxFullVolRefCount = 0; |
| 1514 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1515 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1516 | |
| 1517 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1518 | if (cb == null) { |
| 1519 | return; |
| 1520 | } |
| 1521 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1522 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1523 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1524 | return; |
| 1525 | } |
| 1526 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1527 | boolean applyRequired = false; |
| 1528 | if (startForcing) { |
| 1529 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1530 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1531 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1532 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1533 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1534 | applyRequired = true; |
| 1535 | } |
| 1536 | mRmtSbmxFullVolRefCount++; |
| 1537 | } |
| 1538 | } else { |
| 1539 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1540 | mRmtSbmxFullVolRefCount--; |
| 1541 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1542 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1543 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1544 | applyRequired = true; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | if (applyRequired) { |
| 1549 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1550 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1551 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1556 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1557 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
| 1558 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1559 | return; |
| 1560 | } |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1561 | if (mute != AudioSystem.getMasterMute()) { |
| 1562 | setSystemAudioMute(mute); |
| 1563 | AudioSystem.setMasterMute(mute); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1564 | // Post a persist master volume msg |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1565 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1566 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1567 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1568 | |
| 1569 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1570 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1571 | sendBroadcastToAll(intent); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1572 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /** get master mute state. */ |
| 1576 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1577 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1580 | public void setMasterMute(boolean mute, int flags, String callingPackage) { |
| 1581 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid()); |
| 1582 | } |
| 1583 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 1584 | protected static int getMaxStreamVolume(int streamType) { |
| 1585 | return MAX_STREAM_VOLUME[streamType]; |
| 1586 | } |
| 1587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | /** @see AudioManager#getStreamVolume(int) */ |
| 1589 | public int getStreamVolume(int streamType) { |
| 1590 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1591 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1592 | synchronized (VolumeStreamState.class) { |
| 1593 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1594 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1595 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1596 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1597 | index = 0; |
| 1598 | } |
| 1599 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 1600 | (device & mFixedVolumeDevices) != 0) { |
| 1601 | index = mStreamStates[streamType].getMaxIndex(); |
| 1602 | } |
| 1603 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1604 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1608 | public int getStreamMaxVolume(int streamType) { |
| 1609 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1610 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1613 | /** Get last audible volume before stream was muted. */ |
| 1614 | public int getLastAudibleStreamVolume(int streamType) { |
| 1615 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1616 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1617 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1618 | } |
| 1619 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 1620 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 1621 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1622 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1625 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 1626 | public void setMicrophoneMute(boolean on, String callingPackage) { |
| 1627 | if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, Binder.getCallingUid(), |
| 1628 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1629 | return; |
| 1630 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 1631 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 1632 | return; |
| 1633 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1634 | |
| 1635 | AudioSystem.muteMicrophone(on); |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1636 | // Post a persist microphone msg. |
| 1637 | sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1 |
| 1638 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1641 | @Override |
| 1642 | public int getRingerModeExternal() { |
| 1643 | synchronized(mSettingsLock) { |
| 1644 | return mRingerModeExternal; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | @Override |
| 1649 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1650 | synchronized(mSettingsLock) { |
| 1651 | return mRingerMode; |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1656 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1657 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1658 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1661 | /** @see AudioManager#isValidRingerMode(int) */ |
| 1662 | public boolean isValidRingerMode(int ringerMode) { |
| 1663 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 1664 | } |
| 1665 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1666 | public void setRingerModeExternal(int ringerMode, String caller) { |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1667 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1671 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1672 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1676 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1677 | return; |
| 1678 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1679 | if (caller == null || caller.length() == 0) { |
| 1680 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 1681 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1682 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1683 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1684 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1685 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1686 | final long identity = Binder.clearCallingIdentity(); |
| 1687 | try { |
| 1688 | synchronized (mSettingsLock) { |
| 1689 | final int ringerModeInternal = getRingerModeInternal(); |
| 1690 | final int ringerModeExternal = getRingerModeExternal(); |
| 1691 | if (external) { |
| 1692 | setRingerModeExt(ringerMode); |
| 1693 | if (mRingerModeDelegate != null) { |
| 1694 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
| 1695 | ringerMode, caller, ringerModeInternal); |
| 1696 | } |
| 1697 | if (ringerMode != ringerModeInternal) { |
| 1698 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1699 | } |
| 1700 | } else /*internal*/ { |
| 1701 | if (ringerMode != ringerModeInternal) { |
| 1702 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1703 | } |
| 1704 | if (mRingerModeDelegate != null) { |
| 1705 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
| 1706 | ringerMode, caller, ringerModeExternal); |
| 1707 | } |
| 1708 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1709 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1710 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1711 | } finally { |
| 1712 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1716 | private void setRingerModeExt(int ringerMode) { |
| 1717 | synchronized(mSettingsLock) { |
| 1718 | if (ringerMode == mRingerModeExternal) return; |
| 1719 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1720 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1721 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1722 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1723 | } |
| 1724 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1725 | private void setRingerModeInt(int ringerMode, boolean persist) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1726 | final boolean change; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1727 | synchronized(mSettingsLock) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1728 | change = mRingerMode != ringerMode; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1729 | mRingerMode = ringerMode; |
| 1730 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1731 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1732 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 1733 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 1734 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 1735 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1736 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1737 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 1738 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1739 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1740 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 1741 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 1742 | if (isMuted == shouldMute) continue; |
| 1743 | if (!shouldMute) { |
| 1744 | // unmute |
| 1745 | // ring and notifications volume should never be 0 when not silenced |
| 1746 | // on voice capable devices or devices that support vibration |
| 1747 | if ((isPlatformVoice() || mHasVibrator) && |
| 1748 | mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
| 1749 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 1750 | SparseIntArray indexMap = mStreamStates[streamType].mIndexMap; |
| 1751 | for (int i = 0; i < indexMap.size(); i++) { |
| 1752 | int device = indexMap.keyAt(i); |
| 1753 | int value = indexMap.valueAt(i); |
| 1754 | if (value == 0) { |
| 1755 | indexMap.put(device, 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1756 | } |
| 1757 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 1758 | // Persist volume for stream ring when it is changed here |
| 1759 | final int device = getDeviceForStream(streamType); |
| 1760 | sendMsg(mAudioHandler, |
| 1761 | MSG_PERSIST_VOLUME, |
| 1762 | SENDMSG_QUEUE, |
| 1763 | device, |
| 1764 | 0, |
| 1765 | mStreamStates[streamType], |
| 1766 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1767 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 1768 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1769 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1770 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1771 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1772 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1773 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1774 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1777 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1778 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1779 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 1780 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1781 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 1782 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1783 | if (change) { |
| 1784 | // Send sticky broadcast |
| 1785 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 1786 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | /** @see AudioManager#shouldVibrate(int) */ |
| 1790 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1791 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | |
| 1793 | switch (getVibrateSetting(vibrateType)) { |
| 1794 | |
| 1795 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1796 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | |
| 1798 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1799 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | |
| 1801 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 1802 | // return false, even for incoming calls |
| 1803 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | |
| 1805 | default: |
| 1806 | return false; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | /** @see AudioManager#getVibrateSetting(int) */ |
| 1811 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1812 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 1814 | } |
| 1815 | |
| 1816 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 1817 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 1818 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1819 | if (!mHasVibrator) return; |
| 1820 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1821 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 1822 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | |
| 1824 | // Broadcast change |
| 1825 | broadcastVibrateSetting(vibrateType); |
| 1826 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1829 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 1830 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1831 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1832 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 1833 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1834 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1835 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1836 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1840 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1841 | synchronized(mSetModeDeathHandlers) { |
| 1842 | Log.w(TAG, "setMode() client died"); |
| 1843 | int index = mSetModeDeathHandlers.indexOf(this); |
| 1844 | if (index < 0) { |
| 1845 | Log.w(TAG, "unregistered setMode() client died"); |
| 1846 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1847 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1848 | } |
| 1849 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1850 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1851 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1852 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 1853 | final long ident = Binder.clearCallingIdentity(); |
| 1854 | disconnectBluetoothSco(newModeOwnerPid); |
| 1855 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1856 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1859 | public int getPid() { |
| 1860 | return mPid; |
| 1861 | } |
| 1862 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1863 | public void setMode(int mode) { |
| 1864 | mMode = mode; |
| 1865 | } |
| 1866 | |
| 1867 | public int getMode() { |
| 1868 | return mMode; |
| 1869 | } |
| 1870 | |
| 1871 | public IBinder getBinder() { |
| 1872 | return mCb; |
| 1873 | } |
| 1874 | } |
| 1875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | /** @see AudioManager#setMode(int) */ |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1877 | public void setMode(int mode, IBinder cb) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1878 | if (DEBUG_MODE) { Log.v(TAG, "setMode(mode=" + mode + ")"); } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | if (!checkAudioSettingsPermission("setMode()")) { |
| 1880 | return; |
| 1881 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1882 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 1883 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 1884 | (mContext.checkCallingOrSelfPermission( |
| 1885 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1886 | != PackageManager.PERMISSION_GRANTED)) { |
| 1887 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 1888 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1892 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1893 | return; |
| 1894 | } |
| 1895 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1896 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1897 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1898 | if (mode == AudioSystem.MODE_CURRENT) { |
| 1899 | mode = mMode; |
| 1900 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1901 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid()); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1902 | } |
| 1903 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1904 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1905 | if (newModeOwnerPid != 0) { |
| 1906 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1907 | } |
| 1908 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1909 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1910 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1911 | // setModeInt() returns a valid PID if the audio mode was successfully set to |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1912 | // any mode other than NORMAL. |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 1913 | private int setModeInt(int mode, IBinder cb, int pid) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1914 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1915 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1916 | if (cb == null) { |
| 1917 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1918 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1919 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1920 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1921 | SetModeDeathHandler hdlr = null; |
| 1922 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 1923 | while (iter.hasNext()) { |
| 1924 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 1925 | if (h.getPid() == pid) { |
| 1926 | hdlr = h; |
| 1927 | // Remove from client list so that it is re-inserted at top of list |
| 1928 | iter.remove(); |
| 1929 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 1930 | break; |
| 1931 | } |
| 1932 | } |
| 1933 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 1934 | do { |
| 1935 | if (mode == AudioSystem.MODE_NORMAL) { |
| 1936 | // get new mode from client at top the list if any |
| 1937 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 1938 | hdlr = mSetModeDeathHandlers.get(0); |
| 1939 | cb = hdlr.getBinder(); |
| 1940 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1941 | if (DEBUG_MODE) { |
| 1942 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 1943 | + hdlr.mPid); |
| 1944 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 1945 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1946 | } else { |
| 1947 | if (hdlr == null) { |
| 1948 | hdlr = new SetModeDeathHandler(cb, pid); |
| 1949 | } |
| 1950 | // Register for client death notification |
| 1951 | try { |
| 1952 | cb.linkToDeath(hdlr, 0); |
| 1953 | } catch (RemoteException e) { |
| 1954 | // Client has died! |
| 1955 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 1956 | } |
| 1957 | |
| 1958 | // Last client to call setMode() is always at top of client list |
| 1959 | // as required by SetModeDeathHandler.binderDied() |
| 1960 | mSetModeDeathHandlers.add(0, hdlr); |
| 1961 | hdlr.setMode(mode); |
| 1962 | } |
| 1963 | |
| 1964 | if (mode != mMode) { |
| 1965 | status = AudioSystem.setPhoneState(mode); |
| 1966 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1967 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1968 | mMode = mode; |
| 1969 | } else { |
| 1970 | if (hdlr != null) { |
| 1971 | mSetModeDeathHandlers.remove(hdlr); |
| 1972 | cb.unlinkToDeath(hdlr, 0); |
| 1973 | } |
| 1974 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1975 | if (DEBUG_MODE) { Log.w(TAG, " mode set to MODE_NORMAL after phoneState pb"); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1976 | mode = AudioSystem.MODE_NORMAL; |
| 1977 | } |
| 1978 | } else { |
| 1979 | status = AudioSystem.AUDIO_STATUS_OK; |
| 1980 | } |
| 1981 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 1982 | |
| 1983 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 1984 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1985 | if (mSetModeDeathHandlers.isEmpty()) { |
| 1986 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 1987 | } else { |
| 1988 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 1989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | } |
| 1991 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1992 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1993 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
| 1994 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1995 | |
| 1996 | updateStreamVolumeAlias(true /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1998 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /** @see AudioManager#getMode() */ |
| 2002 | public int getMode() { |
| 2003 | return mMode; |
| 2004 | } |
| 2005 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2006 | //========================================================================================== |
| 2007 | // Sound Effects |
| 2008 | //========================================================================================== |
| 2009 | |
| 2010 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2011 | private static final String ATTR_VERSION = "version"; |
| 2012 | private static final String TAG_GROUP = "group"; |
| 2013 | private static final String ATTR_GROUP_NAME = "name"; |
| 2014 | private static final String TAG_ASSET = "asset"; |
| 2015 | private static final String ATTR_ASSET_ID = "id"; |
| 2016 | private static final String ATTR_ASSET_FILE = "file"; |
| 2017 | |
| 2018 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2019 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2020 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2021 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2022 | |
| 2023 | class LoadSoundEffectReply { |
| 2024 | public int mStatus = 1; |
| 2025 | }; |
| 2026 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2027 | private void loadTouchSoundAssetDefaults() { |
| 2028 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2029 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2030 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2031 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | private void loadTouchSoundAssets() { |
| 2036 | XmlResourceParser parser = null; |
| 2037 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2038 | // only load assets once. |
| 2039 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2040 | return; |
| 2041 | } |
| 2042 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2043 | loadTouchSoundAssetDefaults(); |
| 2044 | |
| 2045 | try { |
| 2046 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2047 | |
| 2048 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2049 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2050 | boolean inTouchSoundsGroup = false; |
| 2051 | |
| 2052 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2053 | while (true) { |
| 2054 | XmlUtils.nextElement(parser); |
| 2055 | String element = parser.getName(); |
| 2056 | if (element == null) { |
| 2057 | break; |
| 2058 | } |
| 2059 | if (element.equals(TAG_GROUP)) { |
| 2060 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2061 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2062 | inTouchSoundsGroup = true; |
| 2063 | break; |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | while (inTouchSoundsGroup) { |
| 2068 | XmlUtils.nextElement(parser); |
| 2069 | String element = parser.getName(); |
| 2070 | if (element == null) { |
| 2071 | break; |
| 2072 | } |
| 2073 | if (element.equals(TAG_ASSET)) { |
| 2074 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2075 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2076 | int fx; |
| 2077 | |
| 2078 | try { |
| 2079 | Field field = AudioManager.class.getField(id); |
| 2080 | fx = field.getInt(null); |
| 2081 | } catch (Exception e) { |
| 2082 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2083 | continue; |
| 2084 | } |
| 2085 | |
| 2086 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2087 | if (i == -1) { |
| 2088 | i = SOUND_EFFECT_FILES.size(); |
| 2089 | SOUND_EFFECT_FILES.add(file); |
| 2090 | } |
| 2091 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2092 | } else { |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | } catch (Resources.NotFoundException e) { |
| 2098 | Log.w(TAG, "audio assets file not found", e); |
| 2099 | } catch (XmlPullParserException e) { |
| 2100 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2101 | } catch (IOException e) { |
| 2102 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2103 | } finally { |
| 2104 | if (parser != null) { |
| 2105 | parser.close(); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2110 | /** @see AudioManager#playSoundEffect(int) */ |
| 2111 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2112 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2117 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2118 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2119 | return; |
| 2120 | } |
| 2121 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2122 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2123 | effectType, (int) (volume * 1000), null, 0); |
| 2124 | } |
| 2125 | |
| 2126 | /** |
| 2127 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2128 | * This method must be called at first when sound effects are enabled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2129 | */ |
| 2130 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2131 | int attempts = 3; |
| 2132 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2133 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2134 | synchronized (reply) { |
| 2135 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2136 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2137 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2138 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2139 | } catch (InterruptedException e) { |
| 2140 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2141 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2142 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2143 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2144 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * Unloads samples from the sound pool. |
| 2149 | * This method can be called to free some memory when |
| 2150 | * sound effects are disabled. |
| 2151 | */ |
| 2152 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2153 | sendMsg(mAudioHandler, MSG_UNLOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2156 | class SoundPoolListenerThread extends Thread { |
| 2157 | public SoundPoolListenerThread() { |
| 2158 | super("SoundPoolListenerThread"); |
| 2159 | } |
| 2160 | |
| 2161 | @Override |
| 2162 | public void run() { |
| 2163 | |
| 2164 | Looper.prepare(); |
| 2165 | mSoundPoolLooper = Looper.myLooper(); |
| 2166 | |
| 2167 | synchronized (mSoundEffectsLock) { |
| 2168 | if (mSoundPool != null) { |
| 2169 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2170 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2171 | } |
| 2172 | mSoundEffectsLock.notify(); |
| 2173 | } |
| 2174 | Looper.loop(); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | private final class SoundPoolCallback implements |
| 2179 | android.media.SoundPool.OnLoadCompleteListener { |
| 2180 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2181 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2182 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2183 | |
| 2184 | public int status() { |
| 2185 | return mStatus; |
| 2186 | } |
| 2187 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2188 | public void setSamples(int[] samples) { |
| 2189 | for (int i = 0; i < samples.length; i++) { |
| 2190 | // do not wait ack for samples rejected upfront by SoundPool |
| 2191 | if (samples[i] > 0) { |
| 2192 | mSamples.add(samples[i]); |
| 2193 | } |
| 2194 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2198 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2199 | int i = mSamples.indexOf(sampleId); |
| 2200 | if (i >= 0) { |
| 2201 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2202 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2203 | if ((status != 0) || mSamples. isEmpty()) { |
| 2204 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2205 | mSoundEffectsLock.notify(); |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2211 | /** @see AudioManager#reloadAudioSettings() */ |
| 2212 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2213 | readAudioSettings(false /*userSwitch*/); |
| 2214 | } |
| 2215 | |
| 2216 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2217 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2218 | readPersistedSettings(); |
| 2219 | |
| 2220 | // restore volume settings |
| 2221 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2222 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2223 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2224 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2225 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2226 | continue; |
| 2227 | } |
| 2228 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2229 | streamState.readSettings(); |
| 2230 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2231 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2232 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2233 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2234 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2235 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2239 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2240 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2241 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2242 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2243 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2244 | checkAllAliasStreamVolumes(); |
| 2245 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2246 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2247 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2248 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2249 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2250 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2251 | enforceSafeMediaVolume(); |
| 2252 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2253 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2256 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2257 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2258 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2259 | return; |
| 2260 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2261 | |
| 2262 | if (on) { |
| 2263 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2264 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2265 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2266 | } |
| 2267 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2268 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2269 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2270 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2271 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2272 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2273 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2277 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2278 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2281 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2282 | public void setBluetoothScoOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2283 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2284 | return; |
| 2285 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2286 | |
| 2287 | if (on) { |
| 2288 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2289 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2290 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2291 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2292 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2293 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2294 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2295 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2296 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2300 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2301 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2304 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2305 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2306 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2307 | mBluetoothA2dpEnabled = on; |
| 2308 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2309 | AudioSystem.FOR_MEDIA, |
| 2310 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2311 | null, 0); |
| 2312 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2316 | public boolean isBluetoothA2dpOn() { |
| 2317 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2318 | return mBluetoothA2dpEnabled; |
| 2319 | } |
| 2320 | } |
| 2321 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2322 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2323 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2324 | int scoAudioMode = |
| 2325 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2326 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2327 | startBluetoothScoInt(cb, scoAudioMode); |
| 2328 | } |
| 2329 | |
| 2330 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2331 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2332 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2333 | } |
| 2334 | |
| 2335 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2336 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2337 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2338 | return; |
| 2339 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2340 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2341 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2342 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2343 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2344 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2345 | // client is created. |
| 2346 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2347 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2348 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /** @see AudioManager#stopBluetoothSco() */ |
| 2352 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2353 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2354 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2355 | return; |
| 2356 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2357 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2358 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2359 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2360 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2361 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2362 | if (client != null) { |
| 2363 | client.decCount(); |
| 2364 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2365 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2368 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2369 | private class ScoClient implements IBinder.DeathRecipient { |
| 2370 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2371 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2372 | private int mStartcount; // number of SCO connections started by this client |
| 2373 | |
| 2374 | ScoClient(IBinder cb) { |
| 2375 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2376 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2377 | mStartcount = 0; |
| 2378 | } |
| 2379 | |
| 2380 | public void binderDied() { |
| 2381 | synchronized(mScoClients) { |
| 2382 | Log.w(TAG, "SCO client died"); |
| 2383 | int index = mScoClients.indexOf(this); |
| 2384 | if (index < 0) { |
| 2385 | Log.w(TAG, "unregistered SCO client died"); |
| 2386 | } else { |
| 2387 | clearCount(true); |
| 2388 | mScoClients.remove(this); |
| 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2393 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2394 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2395 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2396 | if (mStartcount == 0) { |
| 2397 | try { |
| 2398 | mCb.linkToDeath(this, 0); |
| 2399 | } catch (RemoteException e) { |
| 2400 | // client has already died! |
| 2401 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2402 | } |
| 2403 | } |
| 2404 | mStartcount++; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | public void decCount() { |
| 2409 | synchronized(mScoClients) { |
| 2410 | if (mStartcount == 0) { |
| 2411 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2412 | } else { |
| 2413 | mStartcount--; |
| 2414 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2415 | try { |
| 2416 | mCb.unlinkToDeath(this, 0); |
| 2417 | } catch (NoSuchElementException e) { |
| 2418 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2419 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2420 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2421 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | public void clearCount(boolean stopSco) { |
| 2427 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2428 | if (mStartcount != 0) { |
| 2429 | try { |
| 2430 | mCb.unlinkToDeath(this, 0); |
| 2431 | } catch (NoSuchElementException e) { |
| 2432 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2433 | } |
| 2434 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2435 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2436 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2437 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | public int getCount() { |
| 2443 | return mStartcount; |
| 2444 | } |
| 2445 | |
| 2446 | public IBinder getBinder() { |
| 2447 | return mCb; |
| 2448 | } |
| 2449 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2450 | public int getPid() { |
| 2451 | return mCreatorPid; |
| 2452 | } |
| 2453 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2454 | public int totalCount() { |
| 2455 | synchronized(mScoClients) { |
| 2456 | int count = 0; |
| 2457 | int size = mScoClients.size(); |
| 2458 | for (int i = 0; i < size; i++) { |
| 2459 | count += mScoClients.get(i).getCount(); |
| 2460 | } |
| 2461 | return count; |
| 2462 | } |
| 2463 | } |
| 2464 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2465 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2466 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2467 | if (totalCount() == 0) { |
| 2468 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2469 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2470 | // the connection. |
| 2471 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2472 | // Accept SCO audio activation only in NORMAL audio mode or if the mode is |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2473 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2474 | synchronized(mSetModeDeathHandlers) { |
| 2475 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2476 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2477 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2478 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2479 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2480 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2481 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2482 | if (mBluetoothHeadsetDevice != null) { |
| 2483 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2484 | mContentResolver, |
| 2485 | "bluetooth_sco_channel_"+ |
| 2486 | mBluetoothHeadsetDevice.getAddress(), |
| 2487 | SCO_MODE_VIRTUAL_CALL)); |
| 2488 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2489 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2490 | } |
| 2491 | } else { |
| 2492 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2493 | } |
| 2494 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2495 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2496 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2497 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2498 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2499 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2500 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2501 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2502 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2503 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2504 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2505 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2506 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2507 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2508 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2509 | } else { |
| 2510 | broadcastScoConnectionState( |
| 2511 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2512 | } |
| 2513 | } else if (getBluetoothHeadset()) { |
| 2514 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2515 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2516 | } else { |
| 2517 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2518 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2519 | } |
| 2520 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2521 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2522 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2523 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2524 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2525 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2526 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2527 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2528 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2529 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2530 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2531 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2532 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2533 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2534 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2535 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2536 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2537 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2538 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2539 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2540 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2541 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2542 | broadcastScoConnectionState( |
| 2543 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2544 | } |
| 2545 | } else if (getBluetoothHeadset()) { |
| 2546 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2547 | } |
| 2548 | } else { |
| 2549 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2550 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2551 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2557 | private void checkScoAudioState() { |
| 2558 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2559 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2560 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2561 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2562 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2563 | } |
| 2564 | } |
| 2565 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2566 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2567 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2568 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2569 | int size = mScoClients.size(); |
| 2570 | for (int i = 0; i < size; i++) { |
| 2571 | client = mScoClients.get(i); |
| 2572 | if (client.getBinder() == cb) |
| 2573 | return client; |
| 2574 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2575 | if (create) { |
| 2576 | client = new ScoClient(cb); |
| 2577 | mScoClients.add(client); |
| 2578 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2579 | return client; |
| 2580 | } |
| 2581 | } |
| 2582 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2583 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2584 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2585 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2586 | int size = mScoClients.size(); |
| 2587 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2588 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2589 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2590 | cl.clearCount(stopSco); |
| 2591 | } else { |
| 2592 | savedClient = cl; |
| 2593 | } |
| 2594 | } |
| 2595 | mScoClients.clear(); |
| 2596 | if (savedClient != null) { |
| 2597 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2602 | private boolean getBluetoothHeadset() { |
| 2603 | boolean result = false; |
| 2604 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2605 | if (adapter != null) { |
| 2606 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2607 | BluetoothProfile.HEADSET); |
| 2608 | } |
| 2609 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2610 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2611 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2612 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2613 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2614 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2615 | return result; |
| 2616 | } |
| 2617 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2618 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2619 | synchronized(mScoClients) { |
| 2620 | checkScoAudioState(); |
| 2621 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2622 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2623 | if (mBluetoothHeadsetDevice != null) { |
| 2624 | if (mBluetoothHeadset != null) { |
| 2625 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2626 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2627 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2628 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2629 | } |
| 2630 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2631 | getBluetoothHeadset()) { |
| 2632 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2633 | } |
| 2634 | } |
| 2635 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2636 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | private void resetBluetoothSco() { |
| 2642 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2643 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2644 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2645 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2650 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 2651 | SENDMSG_QUEUE, state, 0, null, 0); |
| 2652 | } |
| 2653 | |
| 2654 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2655 | if (state != mScoConnectionState) { |
| 2656 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 2657 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 2658 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 2659 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2660 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2661 | mScoConnectionState = state; |
| 2662 | } |
| 2663 | } |
| 2664 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2665 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 2666 | new BluetoothProfile.ServiceListener() { |
| 2667 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2668 | BluetoothDevice btDevice; |
| 2669 | List<BluetoothDevice> deviceList; |
| 2670 | switch(profile) { |
| 2671 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2672 | synchronized (mConnectedDevices) { |
| 2673 | synchronized (mA2dpAvrcpLock) { |
| 2674 | mA2dp = (BluetoothA2dp) proxy; |
| 2675 | deviceList = mA2dp.getConnectedDevices(); |
| 2676 | if (deviceList.size() > 0) { |
| 2677 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2678 | int state = mA2dp.getConnectionState(btDevice); |
| 2679 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2680 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 2681 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2682 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2683 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2684 | state, |
| 2685 | 0, |
| 2686 | btDevice, |
| 2687 | delay); |
| 2688 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 2689 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2690 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2691 | break; |
| 2692 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2693 | case BluetoothProfile.A2DP_SINK: |
| 2694 | deviceList = proxy.getConnectedDevices(); |
| 2695 | if (deviceList.size() > 0) { |
| 2696 | btDevice = deviceList.get(0); |
| 2697 | synchronized (mConnectedDevices) { |
| 2698 | int state = proxy.getConnectionState(btDevice); |
| 2699 | queueMsgUnderWakeLock(mAudioHandler, |
| 2700 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 2701 | state, |
| 2702 | 0, |
| 2703 | btDevice, |
| 2704 | 0 /* delay */); |
| 2705 | } |
| 2706 | } |
| 2707 | break; |
| 2708 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2709 | case BluetoothProfile.HEADSET: |
| 2710 | synchronized (mScoClients) { |
| 2711 | // Discard timeout message |
| 2712 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 2713 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 2714 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 2715 | if (deviceList.size() > 0) { |
| 2716 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 2717 | } else { |
| 2718 | mBluetoothHeadsetDevice = null; |
| 2719 | } |
| 2720 | // Refresh SCO audio state |
| 2721 | checkScoAudioState(); |
| 2722 | // Continue pending action if any |
| 2723 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 2724 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 2725 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2726 | boolean status = false; |
| 2727 | if (mBluetoothHeadsetDevice != null) { |
| 2728 | switch (mScoAudioState) { |
| 2729 | case SCO_STATE_ACTIVATE_REQ: |
| 2730 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2731 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2732 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2733 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2734 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2735 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2736 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2737 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2738 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2739 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2740 | break; |
| 2741 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2742 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2743 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2744 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2745 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2746 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2747 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2748 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2749 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2750 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2751 | break; |
| 2752 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 2753 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2754 | mBluetoothHeadsetDevice); |
| 2755 | } |
| 2756 | } |
| 2757 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2758 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2759 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2760 | } |
| 2761 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2762 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2763 | break; |
| 2764 | |
| 2765 | default: |
| 2766 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2767 | } |
| 2768 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2769 | public void onServiceDisconnected(int profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2770 | switch(profile) { |
| 2771 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2772 | synchronized (mConnectedDevices) { |
| 2773 | synchronized (mA2dpAvrcpLock) { |
| 2774 | mA2dp = null; |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2775 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)) { |
| 2776 | makeA2dpDeviceUnavailableNow( |
| 2777 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)); |
| 2778 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2779 | } |
| 2780 | } |
| 2781 | break; |
| 2782 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2783 | case BluetoothProfile.A2DP_SINK: |
| 2784 | synchronized (mConnectedDevices) { |
| 2785 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)) { |
| 2786 | makeA2dpSrcUnavailable( |
| 2787 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)); |
| 2788 | } |
| 2789 | } |
| 2790 | break; |
| 2791 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2792 | case BluetoothProfile.HEADSET: |
| 2793 | synchronized (mScoClients) { |
| 2794 | mBluetoothHeadset = null; |
| 2795 | } |
| 2796 | break; |
| 2797 | |
| 2798 | default: |
| 2799 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 2803 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2804 | private void onCheckMusicActive() { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2805 | synchronized (mSafeMediaVolumeState) { |
| 2806 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2807 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 2808 | |
| 2809 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 2810 | sendMsg(mAudioHandler, |
| 2811 | MSG_CHECK_MUSIC_ACTIVE, |
| 2812 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2813 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2814 | 0, |
| 2815 | null, |
| 2816 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2817 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2818 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 2819 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2820 | // Approximate cumulative active music time |
| 2821 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 2822 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
| 2823 | setSafeMediaVolumeEnabled(true); |
| 2824 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2825 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2826 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2833 | private void saveMusicActiveMs() { |
| 2834 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 2835 | } |
| 2836 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2837 | private void onConfigureSafeVolume(boolean force) { |
| 2838 | synchronized (mSafeMediaVolumeState) { |
| 2839 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 2840 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 2841 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 2842 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 2843 | boolean safeMediaVolumeEnabled = |
| 2844 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 2845 | || mContext.getResources().getBoolean( |
| 2846 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2847 | |
| 2848 | // The persisted state is either "disabled" or "active": this is the state applied |
| 2849 | // next time we boot and cannot be "inactive" |
| 2850 | int persistedState; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2851 | if (safeMediaVolumeEnabled) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2852 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2853 | // The state can already be "inactive" here if the user has forced it before |
| 2854 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 2855 | // it to "active". |
| 2856 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2857 | if (mMusicActiveMs == 0) { |
| 2858 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2859 | enforceSafeMediaVolume(); |
| 2860 | } else { |
| 2861 | // We have existing playback time recorded, already confirmed. |
| 2862 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 2863 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2864 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2865 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2866 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2867 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 2868 | } |
| 2869 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2870 | sendMsg(mAudioHandler, |
| 2871 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 2872 | SENDMSG_QUEUE, |
| 2873 | persistedState, |
| 2874 | 0, |
| 2875 | null, |
| 2876 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2881 | /////////////////////////////////////////////////////////////////////////// |
| 2882 | // Internal methods |
| 2883 | /////////////////////////////////////////////////////////////////////////// |
| 2884 | |
| 2885 | /** |
| 2886 | * Checks if the adjustment should change ringer mode instead of just |
| 2887 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 2888 | * indices on the stream states. |
| 2889 | */ |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2890 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2891 | int result = FLAG_ADJUST_VOLUME; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2892 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2893 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2894 | switch (ringerMode) { |
| 2895 | case RINGER_MODE_NORMAL: |
| 2896 | if (direction == AudioManager.ADJUST_LOWER) { |
| 2897 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2898 | // "step" is the delta in internal index units corresponding to a |
| 2899 | // change of 1 in UI index units. |
| 2900 | // Because of rounding when rescaling from one stream index range to its alias |
| 2901 | // index range, we cannot simply test oldIndex == step: |
| 2902 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 2903 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2904 | ringerMode = RINGER_MODE_VIBRATE; |
| 2905 | } |
| 2906 | } else { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2907 | // (oldIndex < step) is equivalent to (old UI index == 0) |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 2908 | if ((oldIndex < step) |
| 2909 | && VOLUME_SETS_RINGER_MODE_SILENT |
| 2910 | && mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2911 | ringerMode = RINGER_MODE_SILENT; |
| 2912 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 2913 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2914 | } else if (direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2915 | || direction == AudioManager.ADJUST_MUTE) { |
| 2916 | if (mHasVibrator) { |
| 2917 | ringerMode = RINGER_MODE_VIBRATE; |
| 2918 | } else { |
| 2919 | ringerMode = RINGER_MODE_SILENT; |
| 2920 | } |
| 2921 | // Setting the ringer mode will toggle mute |
| 2922 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2923 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2924 | break; |
| 2925 | case RINGER_MODE_VIBRATE: |
| 2926 | if (!mHasVibrator) { |
| 2927 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 2928 | "but no vibrator is present"); |
| 2929 | break; |
| 2930 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2931 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2932 | // This is the case we were muted with the volume turned up |
| 2933 | if (oldIndex >= 2 * step && isMuted) { |
| 2934 | ringerMode = RINGER_MODE_NORMAL; |
| 2935 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 2936 | if (VOLUME_SETS_RINGER_MODE_SILENT) { |
| 2937 | ringerMode = RINGER_MODE_SILENT; |
| 2938 | } else { |
| 2939 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 2940 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2941 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2942 | } else if (direction == AudioManager.ADJUST_RAISE |
| 2943 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2944 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2945 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2946 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2947 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2948 | break; |
| 2949 | case RINGER_MODE_SILENT: |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2950 | if (direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
| 2951 | // This is the case we were muted with the volume turned up |
| 2952 | ringerMode = RINGER_MODE_NORMAL; |
| 2953 | } else if (direction == AudioManager.ADJUST_RAISE |
| 2954 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2955 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2956 | if (PREVENT_VOLUME_ADJUSTMENT_IF_SILENT) { |
| 2957 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2958 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2959 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2960 | ringerMode = RINGER_MODE_VIBRATE; |
| 2961 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2962 | // If we don't have a vibrator or they were toggling mute |
| 2963 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2964 | ringerMode = RINGER_MODE_NORMAL; |
| 2965 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2966 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 2967 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2968 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2969 | break; |
| 2970 | default: |
| 2971 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 2972 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2973 | } |
| 2974 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2975 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2976 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2977 | mPrevVolDirection = direction; |
| 2978 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2979 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2982 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2983 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2984 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | } |
| 2986 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2987 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 2988 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 2989 | } |
| 2990 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 2991 | boolean updateRingerModeAffectedStreams() { |
| 2992 | int ringerModeAffectedStreams; |
| 2993 | // make sure settings for ringer mode are consistent with device type: non voice capable |
| 2994 | // devices (tablets) include media stream in silent mode whereas phones don't. |
| 2995 | ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
| 2996 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 2997 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 2998 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 2999 | UserHandle.USER_CURRENT); |
| 3000 | |
| 3001 | // ringtone, notification and system streams are always affected by ringer mode |
| 3002 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_RING)| |
| 3003 | (1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3004 | (1 << AudioSystem.STREAM_SYSTEM); |
| 3005 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3006 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3007 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3008 | ringerModeAffectedStreams = 0; |
| 3009 | break; |
| 3010 | default: |
John Spurlock | 77e54d9 | 2014-08-11 12:16:24 -0400 | [diff] [blame] | 3011 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3012 | break; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3013 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3014 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3015 | synchronized (mCameraSoundForced) { |
| 3016 | if (mCameraSoundForced) { |
| 3017 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3018 | } else { |
| 3019 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3020 | } |
| 3021 | } |
| 3022 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3023 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3024 | } else { |
| 3025 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3026 | } |
| 3027 | |
| 3028 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3029 | Settings.System.putIntForUser(mContentResolver, |
| 3030 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3031 | ringerModeAffectedStreams, |
| 3032 | UserHandle.USER_CURRENT); |
| 3033 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3034 | return true; |
| 3035 | } |
| 3036 | return false; |
| 3037 | } |
| 3038 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3039 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3040 | public boolean isStreamAffectedByMute(int streamType) { |
| 3041 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3042 | } |
| 3043 | |
| 3044 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3045 | switch (direction) { |
| 3046 | case AudioManager.ADJUST_LOWER: |
| 3047 | case AudioManager.ADJUST_RAISE: |
| 3048 | case AudioManager.ADJUST_SAME: |
| 3049 | case AudioManager.ADJUST_MUTE: |
| 3050 | case AudioManager.ADJUST_UNMUTE: |
| 3051 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3052 | break; |
| 3053 | default: |
| 3054 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 3058 | private void ensureValidSteps(int steps) { |
| 3059 | if (Math.abs(steps) > MAX_BATCH_VOLUME_ADJUST_STEPS) { |
| 3060 | throw new IllegalArgumentException("Bad volume adjust steps " + steps); |
| 3061 | } |
| 3062 | } |
| 3063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | private void ensureValidStreamType(int streamType) { |
| 3065 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3066 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3067 | } |
| 3068 | } |
| 3069 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3070 | private boolean isMuteAdjust(int adjust) { |
| 3071 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3072 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3073 | } |
| 3074 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3075 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3076 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3077 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3078 | TelecomManager telecomManager = |
| 3079 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3080 | |
| 3081 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3082 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3083 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3084 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3085 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3086 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3087 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3088 | /** |
| 3089 | * For code clarity for getActiveStreamType(int) |
| 3090 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3091 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3092 | * in the last "delay_ms" ms. |
| 3093 | */ |
| 3094 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3095 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3096 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3097 | } |
| 3098 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3099 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3100 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3101 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3102 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3103 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3104 | == AudioSystem.FORCE_BT_SCO) { |
| 3105 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3106 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3107 | } else { |
| 3108 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3109 | return AudioSystem.STREAM_VOICE_CALL; |
| 3110 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3111 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3112 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3113 | if (DEBUG_VOL) |
| 3114 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3115 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3116 | } else { |
| 3117 | if (DEBUG_VOL) |
| 3118 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3119 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3120 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3121 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3122 | if (DEBUG_VOL) |
| 3123 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3124 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3125 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3126 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3127 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3128 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 3129 | // TV always defaults to STREAM_MUSIC |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3130 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3131 | } |
| 3132 | break; |
| 3133 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3134 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3135 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3136 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3137 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3138 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3139 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3140 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3141 | return AudioSystem.STREAM_VOICE_CALL; |
| 3142 | } |
| 3143 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3144 | StreamOverride.sDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3145 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3146 | StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3147 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3148 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3149 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3150 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3151 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3152 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3153 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3154 | if (DEBUG_VOL) Log.v(TAG, |
| 3155 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3156 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3157 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3158 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3159 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3160 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3161 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3162 | + suggestedStreamType); |
| 3163 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | } |
| 3165 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3166 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3167 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3168 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3169 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3170 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3171 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3172 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | private void broadcastVibrateSetting(int vibrateType) { |
| 3176 | // Send broadcast |
| 3177 | if (ActivityManagerNative.isSystemReady()) { |
| 3178 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3179 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3180 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3181 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3186 | /** |
| 3187 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3188 | * Note that the wake lock needs to be released after the message has been handled. |
| 3189 | */ |
| 3190 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3191 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3192 | final long ident = Binder.clearCallingIdentity(); |
| 3193 | // Always acquire the wake lock as AudioService because it is released by the |
| 3194 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3195 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3196 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3197 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3199 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3200 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3201 | int existingMsgPolicy, int arg1, int arg2, Object obj, int delay) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | |
| 3203 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3204 | handler.removeMessages(msg); |
| 3205 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3206 | return; |
| 3207 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3208 | synchronized (mLastDeviceConnectMsgTime) { |
| 3209 | long time = SystemClock.uptimeMillis() + delay; |
| 3210 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3211 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3212 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3213 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3214 | mLastDeviceConnectMsgTime = time; |
| 3215 | } |
| 3216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3220 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3221 | == PackageManager.PERMISSION_GRANTED) { |
| 3222 | return true; |
| 3223 | } |
| 3224 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3225 | + Binder.getCallingPid() |
| 3226 | + ", uid=" + Binder.getCallingUid(); |
| 3227 | Log.w(TAG, msg); |
| 3228 | return false; |
| 3229 | } |
| 3230 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3231 | private int getDeviceForStream(int stream) { |
| 3232 | int device = AudioSystem.getDevicesForStream(stream); |
| 3233 | if ((device & (device - 1)) != 0) { |
| 3234 | // Multiple device selection is either: |
| 3235 | // - speaker + one other device: give priority to speaker in this case. |
| 3236 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3237 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3238 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3239 | // - HDMI-CEC system audio mode only output: give priority to available item in order. |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3240 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3241 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3242 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3243 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3244 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3245 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3246 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3247 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3248 | } else { |
| 3249 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3250 | } |
| 3251 | } |
| 3252 | return device; |
| 3253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3255 | /* |
| 3256 | * A class just for packaging up a set of connection parameters. |
| 3257 | */ |
| 3258 | private class WiredDeviceConnectionState { |
| 3259 | public int mType; |
| 3260 | public int mState; |
| 3261 | public String mAddress; |
| 3262 | public String mName; |
| 3263 | |
| 3264 | public WiredDeviceConnectionState(int type, int state, String address, String name) { |
| 3265 | mType = type; |
| 3266 | mState = state; |
| 3267 | mAddress = address; |
| 3268 | mName = name; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | public void setWiredDeviceConnectionState(int type, int state, String address, |
| 3273 | String name) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3274 | synchronized (mConnectedDevices) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3275 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3276 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3277 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3278 | 0, |
| 3279 | 0, |
| 3280 | new WiredDeviceConnectionState(type, state, address, name), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3281 | delay); |
| 3282 | } |
| 3283 | } |
| 3284 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3285 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3286 | { |
| 3287 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3288 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3289 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3290 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3291 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3292 | if (profile == BluetoothProfile.A2DP) { |
| 3293 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3294 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3295 | } else { |
| 3296 | delay = 0; |
| 3297 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3298 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3299 | (profile == BluetoothProfile.A2DP ? |
| 3300 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3301 | state, |
| 3302 | 0, |
| 3303 | device, |
| 3304 | delay); |
| 3305 | } |
| 3306 | return delay; |
| 3307 | } |
| 3308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3309 | /////////////////////////////////////////////////////////////////////////// |
| 3310 | // Inner classes |
| 3311 | /////////////////////////////////////////////////////////////////////////// |
| 3312 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3313 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3314 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3315 | // 2 mSetModeDeathHandlers |
| 3316 | // 3 mSettingsLock |
| 3317 | // 4 VolumeStreamState.class |
| 3318 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3319 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3320 | private final int mStreamType; |
| 3321 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3322 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3323 | private String mVolumeIndexSettingName; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3324 | private int mIndexMax; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3325 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3326 | private final Intent mVolumeChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3327 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3328 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3329 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3330 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | |
| 3332 | mStreamType = streamType; |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 3333 | mIndexMax = MAX_STREAM_VOLUME[streamType]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3334 | AudioSystem.initStreamVolume(streamType, 0, mIndexMax); |
| 3335 | mIndexMax *= 10; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3336 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3337 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3338 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3339 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3342 | public String getSettingNameForDevice(int device) { |
| 3343 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3344 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3345 | if (suffix.isEmpty()) { |
| 3346 | return name; |
| 3347 | } |
| 3348 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3351 | public void readSettings() { |
| 3352 | synchronized (VolumeStreamState.class) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 3353 | // force maximum volume on all streams if fixed volume property is set |
| 3354 | if (mUseFixedVolume) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3355 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3356 | return; |
| 3357 | } |
| 3358 | // do not read system stream volume from settings: this stream is always aliased |
| 3359 | // to another stream type and its volume is never persisted. Values in settings can |
| 3360 | // only be stale values |
| 3361 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3362 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3363 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3364 | synchronized (mCameraSoundForced) { |
| 3365 | if (mCameraSoundForced) { |
| 3366 | index = mIndexMax; |
| 3367 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3368 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3369 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3370 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3371 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3372 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3373 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3374 | |
| 3375 | for (int i = 0; remainingDevices != 0; i++) { |
| 3376 | int device = (1 << i); |
| 3377 | if ((device & remainingDevices) == 0) { |
| 3378 | continue; |
| 3379 | } |
| 3380 | remainingDevices &= ~device; |
| 3381 | |
| 3382 | // retrieve current volume for device |
| 3383 | String name = getSettingNameForDevice(device); |
| 3384 | // if no volume stored for current stream and device, use default volume if default |
| 3385 | // device, continue otherwise |
| 3386 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3387 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3388 | int index = Settings.System.getIntForUser( |
| 3389 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3390 | if (index == -1) { |
| 3391 | continue; |
| 3392 | } |
| 3393 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3394 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3395 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | } |
| 3398 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3399 | // must be called while synchronized VolumeStreamState.class |
| 3400 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3401 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3402 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3403 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3404 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) |
| 3405 | || ((device & mFullVolumeDevices) != 0)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3406 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3407 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3408 | index = (getIndex(device) + 5)/10; |
| 3409 | } |
| 3410 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3411 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3412 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3413 | public void applyAllVolumes() { |
| 3414 | synchronized (VolumeStreamState.class) { |
| 3415 | // apply default volume first: by convention this will reset all |
| 3416 | // devices volumes in audio policy manager to the supplied value |
| 3417 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3418 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3419 | index = 0; |
| 3420 | } else { |
| 3421 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3422 | } |
| 3423 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3424 | // then apply device specific volumes |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3425 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3426 | int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3427 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3428 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3429 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3430 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3431 | mAvrcpAbsVolSupported) |
| 3432 | || ((device & mFullVolumeDevices) != 0)) |
| 3433 | { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3434 | index = (mIndexMax + 5)/10; |
| 3435 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3436 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3437 | } |
| 3438 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3439 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3440 | } |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | public boolean adjustIndex(int deltaIndex, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3445 | return setIndex(getIndex(device) + deltaIndex, |
| 3446 | device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3447 | } |
| 3448 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3449 | public boolean setIndex(int index, int device) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3450 | boolean changed = false; |
| 3451 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3452 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3453 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3454 | index = getValidIndex(index); |
| 3455 | synchronized (mCameraSoundForced) { |
| 3456 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3457 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3458 | } |
| 3459 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3460 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3461 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3462 | changed = oldIndex != index; |
| 3463 | if (changed) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3464 | // Apply change to all streams using this one as alias |
| 3465 | // if changing volume of current device, also change volume of current |
| 3466 | // device on aliased stream |
| 3467 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3468 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3469 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3470 | if (streamType != mStreamType && |
| 3471 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3472 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 3473 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3474 | device); |
| 3475 | if (currentDevice) { |
| 3476 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3477 | getDeviceForStream(streamType)); |
| 3478 | } |
| 3479 | } |
| 3480 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3481 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3482 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3483 | if (changed) { |
| 3484 | oldIndex = (oldIndex + 5) / 10; |
| 3485 | index = (index + 5) / 10; |
| 3486 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 3487 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
| 3488 | sendBroadcastToAll(mVolumeChanged); |
| 3489 | } |
| 3490 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3493 | public int getIndex(int device) { |
| 3494 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3495 | int index = mIndexMap.get(device, -1); |
| 3496 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3497 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3498 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3499 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3500 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3501 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3502 | } |
| 3503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3504 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3505 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3508 | public void setAllIndexes(VolumeStreamState srcStream) { |
| 3509 | synchronized (VolumeStreamState.class) { |
| 3510 | int srcStreamType = srcStream.getStreamType(); |
| 3511 | // apply default device volume from source stream to all devices first in case |
| 3512 | // some devices are present in this stream state but not in source stream state |
| 3513 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3514 | index = rescaleIndex(index, srcStreamType, mStreamType); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3515 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3516 | mIndexMap.put(mIndexMap.keyAt(i), index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3517 | } |
| 3518 | // Now apply actual volume for devices in source stream state |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3519 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 3520 | for (int i = 0; i < srcMap.size(); i++) { |
| 3521 | int device = srcMap.keyAt(i); |
| 3522 | index = srcMap.valueAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3523 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3524 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3525 | setIndex(index, device); |
| 3526 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3530 | public void setAllIndexesToMax() { |
| 3531 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3532 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3533 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3534 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3535 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3536 | } |
| 3537 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3538 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3539 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3540 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3541 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3542 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3543 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3544 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3545 | // Set the new mute volume. This propagates the values to |
| 3546 | // the audio system, otherwise the volume won't be changed |
| 3547 | // at the lower level. |
| 3548 | sendMsg(mAudioHandler, |
| 3549 | MSG_SET_ALL_VOLUMES, |
| 3550 | SENDMSG_QUEUE, |
| 3551 | 0, |
| 3552 | 0, |
| 3553 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3555 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3556 | if (changed) { |
| 3557 | // Stream mute changed, fire the intent. |
| 3558 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 3559 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3560 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 3561 | sendBroadcastToAll(intent); |
| 3562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | } |
| 3564 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3565 | public int getStreamType() { |
| 3566 | return mStreamType; |
| 3567 | } |
| 3568 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3569 | public void checkFixedVolumeDevices() { |
| 3570 | synchronized (VolumeStreamState.class) { |
| 3571 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 3572 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3573 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3574 | int device = mIndexMap.keyAt(i); |
| 3575 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3576 | if (((device & mFullVolumeDevices) != 0) |
| 3577 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3578 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3579 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3580 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3581 | } |
| 3582 | } |
| 3583 | } |
| 3584 | } |
| 3585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3586 | private int getValidIndex(int index) { |
| 3587 | if (index < 0) { |
| 3588 | return 0; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame^] | 3589 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3590 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | return index; |
| 3594 | } |
| 3595 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3596 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3597 | pw.print(" Muted: "); |
| 3598 | pw.println(mIsMuted); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3599 | pw.print(" Max: "); |
| 3600 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3601 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3602 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3603 | if (i > 0) { |
| 3604 | pw.print(", "); |
| 3605 | } |
| 3606 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3607 | pw.print(Integer.toHexString(device)); |
| 3608 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 3609 | : AudioSystem.getOutputDeviceName(device); |
| 3610 | if (!deviceName.isEmpty()) { |
| 3611 | pw.print(" ("); |
| 3612 | pw.print(deviceName); |
| 3613 | pw.print(")"); |
| 3614 | } |
| 3615 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3616 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3617 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3618 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | /** Thread that handles native AudioSystem control. */ |
| 3623 | private class AudioSystemThread extends Thread { |
| 3624 | AudioSystemThread() { |
| 3625 | super("AudioService"); |
| 3626 | } |
| 3627 | |
| 3628 | @Override |
| 3629 | public void run() { |
| 3630 | // Set this thread up so the handler will work on it |
| 3631 | Looper.prepare(); |
| 3632 | |
| 3633 | synchronized(AudioService.this) { |
| 3634 | mAudioHandler = new AudioHandler(); |
| 3635 | |
| 3636 | // Notify that the handler has been created |
| 3637 | AudioService.this.notify(); |
| 3638 | } |
| 3639 | |
| 3640 | // Listen for volume change requests that are set by VolumePanel |
| 3641 | Looper.loop(); |
| 3642 | } |
| 3643 | } |
| 3644 | |
| 3645 | /** Handles internal volume messages in separate volume thread. */ |
| 3646 | private class AudioHandler extends Handler { |
| 3647 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3648 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3649 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3650 | synchronized (VolumeStreamState.class) { |
| 3651 | // Apply volume |
| 3652 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3653 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3654 | // Apply change to all streams using this one as alias |
| 3655 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3656 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3657 | if (streamType != streamState.mStreamType && |
| 3658 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 3659 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 3660 | // that may have a different device selected |
| 3661 | int streamDevice = getDeviceForStream(streamType); |
| 3662 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 3663 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 3664 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 3665 | } |
| 3666 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3667 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3668 | } |
| 3669 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3670 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3671 | sendMsg(mAudioHandler, |
| 3672 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 3673 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3674 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3675 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3676 | streamState, |
| 3677 | PERSIST_DELAY); |
| 3678 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | } |
| 3680 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3681 | private void setAllVolumes(VolumeStreamState streamState) { |
| 3682 | |
| 3683 | // Apply volume |
| 3684 | streamState.applyAllVolumes(); |
| 3685 | |
| 3686 | // Apply change to all streams using this one as alias |
| 3687 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3688 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3689 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3690 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3691 | mStreamStates[streamType].applyAllVolumes(); |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3696 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3697 | if (mUseFixedVolume) { |
| 3698 | return; |
| 3699 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3700 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 3701 | return; |
| 3702 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3703 | System.putIntForUser(mContentResolver, |
| 3704 | streamState.getSettingNameForDevice(device), |
| 3705 | (streamState.getIndex(device) + 5)/ 10, |
| 3706 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3709 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3710 | if (mUseFixedVolume) { |
| 3711 | return; |
| 3712 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 3713 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | } |
| 3715 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3716 | private boolean onLoadSoundEffects() { |
| 3717 | int status; |
| 3718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3719 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3720 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3721 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 3722 | return false; |
| 3723 | } |
| 3724 | |
| 3725 | if (mSoundPool != null) { |
| 3726 | return true; |
| 3727 | } |
| 3728 | |
| 3729 | loadTouchSoundAssets(); |
| 3730 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 3731 | mSoundPool = new SoundPool.Builder() |
| 3732 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 3733 | .setAudioAttributes(new AudioAttributes.Builder() |
| 3734 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 3735 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 3736 | .build()) |
| 3737 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3738 | mSoundPoolCallBack = null; |
| 3739 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 3740 | mSoundPoolListenerThread.start(); |
| 3741 | int attempts = 3; |
| 3742 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 3743 | try { |
| 3744 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3745 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3746 | } catch (InterruptedException e) { |
| 3747 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | if (mSoundPoolCallBack == null) { |
| 3752 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 3753 | if (mSoundPoolLooper != null) { |
| 3754 | mSoundPoolLooper.quit(); |
| 3755 | mSoundPoolLooper = null; |
| 3756 | } |
| 3757 | mSoundPoolListenerThread = null; |
| 3758 | mSoundPool.release(); |
| 3759 | mSoundPool = null; |
| 3760 | return false; |
| 3761 | } |
| 3762 | /* |
| 3763 | * poolId table: The value -1 in this table indicates that corresponding |
| 3764 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 3765 | * Once loaded, the value in poolId is the sample ID and the same |
| 3766 | * sample can be reused for another effect using the same file. |
| 3767 | */ |
| 3768 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3769 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3770 | poolId[fileIdx] = -1; |
| 3771 | } |
| 3772 | /* |
| 3773 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 3774 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 3775 | * this indicates we have a valid sample loaded for this effect. |
| 3776 | */ |
| 3777 | |
| 3778 | int numSamples = 0; |
| 3779 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3780 | // Do not load sample if this effect uses the MediaPlayer |
| 3781 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 3782 | continue; |
| 3783 | } |
| 3784 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 3785 | String filePath = Environment.getRootDirectory() |
| 3786 | + SOUND_EFFECTS_PATH |
| 3787 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 3788 | int sampleId = mSoundPool.load(filePath, 0); |
| 3789 | if (sampleId <= 0) { |
| 3790 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 3791 | } else { |
| 3792 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 3793 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 3794 | numSamples++; |
| 3795 | } |
| 3796 | } else { |
| 3797 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 3798 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 3799 | } |
| 3800 | } |
| 3801 | // wait for all samples to be loaded |
| 3802 | if (numSamples > 0) { |
| 3803 | mSoundPoolCallBack.setSamples(poolId); |
| 3804 | |
| 3805 | attempts = 3; |
| 3806 | status = 1; |
| 3807 | while ((status == 1) && (attempts-- > 0)) { |
| 3808 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3809 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3810 | status = mSoundPoolCallBack.status(); |
| 3811 | } catch (InterruptedException e) { |
| 3812 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 3813 | } |
| 3814 | } |
| 3815 | } else { |
| 3816 | status = -1; |
| 3817 | } |
| 3818 | |
| 3819 | if (mSoundPoolLooper != null) { |
| 3820 | mSoundPoolLooper.quit(); |
| 3821 | mSoundPoolLooper = null; |
| 3822 | } |
| 3823 | mSoundPoolListenerThread = null; |
| 3824 | if (status != 0) { |
| 3825 | Log.w(TAG, |
| 3826 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 3827 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3828 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 3829 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | mSoundPool.release(); |
| 3834 | mSoundPool = null; |
| 3835 | } |
| 3836 | } |
| 3837 | return (status == 0); |
| 3838 | } |
| 3839 | |
| 3840 | /** |
| 3841 | * Unloads samples from the sound pool. |
| 3842 | * This method can be called to free some memory when |
| 3843 | * sound effects are disabled. |
| 3844 | */ |
| 3845 | private void onUnloadSoundEffects() { |
| 3846 | synchronized (mSoundEffectsLock) { |
| 3847 | if (mSoundPool == null) { |
| 3848 | return; |
| 3849 | } |
| 3850 | |
| 3851 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3852 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3853 | poolId[fileIdx] = 0; |
| 3854 | } |
| 3855 | |
| 3856 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3857 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 3858 | continue; |
| 3859 | } |
| 3860 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 3861 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 3862 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3863 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 3864 | } |
| 3865 | } |
| 3866 | mSoundPool.release(); |
| 3867 | mSoundPool = null; |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | private void onPlaySoundEffect(int effectType, int volume) { |
| 3872 | synchronized (mSoundEffectsLock) { |
| 3873 | |
| 3874 | onLoadSoundEffects(); |
| 3875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | if (mSoundPool == null) { |
| 3877 | return; |
| 3878 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3879 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3880 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3881 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 3882 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3883 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 3884 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3885 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3886 | |
| 3887 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3888 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 3889 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3890 | } else { |
| 3891 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3892 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 3893 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 3894 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3895 | mediaPlayer.setDataSource(filePath); |
| 3896 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 3897 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 3898 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3899 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 3900 | public void onCompletion(MediaPlayer mp) { |
| 3901 | cleanupPlayer(mp); |
| 3902 | } |
| 3903 | }); |
| 3904 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 3905 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 3906 | cleanupPlayer(mp); |
| 3907 | return true; |
| 3908 | } |
| 3909 | }); |
| 3910 | mediaPlayer.start(); |
| 3911 | } catch (IOException ex) { |
| 3912 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 3913 | } catch (IllegalArgumentException ex) { |
| 3914 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 3915 | } catch (IllegalStateException ex) { |
| 3916 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | private void cleanupPlayer(MediaPlayer mp) { |
| 3923 | if (mp != null) { |
| 3924 | try { |
| 3925 | mp.stop(); |
| 3926 | mp.release(); |
| 3927 | } catch (IllegalStateException ex) { |
| 3928 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3933 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 3934 | synchronized (mConnectedDevices) { |
| 3935 | setForceUseInt_SyncDevices(usage, config); |
| 3936 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3937 | } |
| 3938 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3939 | private void onPersistSafeVolumeState(int state) { |
| 3940 | Settings.Global.putInt(mContentResolver, |
| 3941 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 3942 | state); |
| 3943 | } |
| 3944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3945 | @Override |
| 3946 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3947 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3948 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3949 | case MSG_SET_DEVICE_VOLUME: |
| 3950 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 3951 | break; |
| 3952 | |
| 3953 | case MSG_SET_ALL_VOLUMES: |
| 3954 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3955 | break; |
| 3956 | |
| 3957 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3958 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3959 | break; |
| 3960 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 3961 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3962 | if (mUseFixedVolume) { |
| 3963 | return; |
| 3964 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3965 | Settings.System.putIntForUser(mContentResolver, |
| 3966 | Settings.System.VOLUME_MASTER_MUTE, |
| 3967 | msg.arg1, |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 3968 | msg.arg2); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 3969 | break; |
| 3970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3971 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3972 | // note that the value persisted is the current ringer mode, not the |
| 3973 | // value of ringer mode as of the time the request was made to persist |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3974 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3975 | break; |
| 3976 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3977 | case MSG_MEDIA_SERVER_DIED: |
Eric Laurent | eb4b8a2 | 2014-07-21 13:10:07 -0700 | [diff] [blame] | 3978 | if (!mSystemReady || |
| 3979 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3980 | Log.e(TAG, "Media server died."); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3981 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3982 | null, 500); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3983 | break; |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3984 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3985 | Log.e(TAG, "Media server started."); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3986 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3987 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 3988 | // server crash |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3989 | // Note that we only execute this when the media server |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3990 | // process restarts after a crash, not the first time it is started. |
| 3991 | AudioSystem.setParameters("restarting=true"); |
| 3992 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 3993 | readAndSetLowRamDevice(); |
| 3994 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3995 | // Restore device connection states |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3996 | synchronized (mConnectedDevices) { |
| 3997 | Set set = mConnectedDevices.entrySet(); |
| 3998 | Iterator i = set.iterator(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3999 | while (i.hasNext()) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4000 | Map.Entry device = (Map.Entry)i.next(); |
| 4001 | AudioSystem.setDeviceConnectionState( |
| 4002 | ((Integer)device.getKey()).intValue(), |
| 4003 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4004 | (String)device.getValue(), |
| 4005 | "unknown-device"); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4006 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4007 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4008 | // Restore call state |
| 4009 | AudioSystem.setPhoneState(mMode); |
| 4010 | |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4011 | // Restore forced usage for communcations and record |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4012 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4013 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4014 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 4015 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4016 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4017 | // Restore stream volumes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4019 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4020 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4021 | AudioSystem.initStreamVolume(streamType, 0, (streamState.mIndexMax + 5) / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4022 | |
| 4023 | streamState.applyAllVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4024 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4025 | |
| 4026 | // Restore ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4027 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4028 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4029 | // Reset device orientation (if monitored for this device) |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4030 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4031 | setOrientationForAudioSystem(); |
| 4032 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4033 | if (mMonitorRotation) { |
| 4034 | setRotationForAudioSystem(); |
| 4035 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4036 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4037 | synchronized (mBluetoothA2dpEnabledLock) { |
| 4038 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 4039 | mBluetoothA2dpEnabled ? |
| 4040 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 4041 | } |
Eric Laurent | bff5ca5 | 2012-11-02 16:48:26 -0700 | [diff] [blame] | 4042 | |
| 4043 | synchronized (mSettingsLock) { |
| 4044 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 4045 | mDockAudioMediaEnabled ? |
| 4046 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 4047 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4048 | if (mHdmiManager != null) { |
| 4049 | synchronized (mHdmiManager) { |
| 4050 | if (mHdmiTvClient != null) { |
| 4051 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 4052 | } |
| 4053 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 4054 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 4055 | |
| 4056 | synchronized (mAudioPolicies) { |
| 4057 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 4058 | policy.connectMixes(); |
| 4059 | } |
| 4060 | } |
| 4061 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4062 | // indicate the end of reconfiguration phase to audio HAL |
| 4063 | AudioSystem.setParameters("restarting=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4064 | break; |
| 4065 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4066 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4067 | onUnloadSoundEffects(); |
| 4068 | break; |
| 4069 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4070 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4071 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4072 | // can take several dozens of milliseconds to complete |
| 4073 | boolean loaded = onLoadSoundEffects(); |
| 4074 | if (msg.obj != null) { |
| 4075 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4076 | synchronized (reply) { |
| 4077 | reply.mStatus = loaded ? 0 : -1; |
| 4078 | reply.notify(); |
| 4079 | } |
| 4080 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4081 | break; |
| 4082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4083 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4084 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4085 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4086 | |
| 4087 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4088 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4089 | synchronized (mConnectedDevices) { |
| 4090 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4091 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4092 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4093 | |
| 4094 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4095 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4096 | setForceUse(msg.arg1, msg.arg2); |
| 4097 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4098 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4099 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4100 | resetBluetoothSco(); |
| 4101 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4102 | |
| 4103 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4104 | { WiredDeviceConnectionState connectState = |
| 4105 | (WiredDeviceConnectionState)msg.obj; |
| 4106 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
| 4107 | connectState.mAddress, connectState.mName); |
| 4108 | mAudioEventWakeLock.release(); |
| 4109 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4110 | break; |
| 4111 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4112 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4113 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4114 | mAudioEventWakeLock.release(); |
| 4115 | break; |
| 4116 | |
| 4117 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4118 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4119 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4120 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4121 | |
| 4122 | case MSG_REPORT_NEW_ROUTES: { |
| 4123 | int N = mRoutesObservers.beginBroadcast(); |
| 4124 | if (N > 0) { |
| 4125 | AudioRoutesInfo routes; |
| 4126 | synchronized (mCurAudioRoutes) { |
| 4127 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4128 | } |
| 4129 | while (N > 0) { |
| 4130 | N--; |
| 4131 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4132 | try { |
| 4133 | obs.dispatchAudioRoutesChanged(routes); |
| 4134 | } catch (RemoteException e) { |
| 4135 | } |
| 4136 | } |
| 4137 | } |
| 4138 | mRoutesObservers.finishBroadcast(); |
| 4139 | break; |
| 4140 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4141 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4142 | case MSG_CHECK_MUSIC_ACTIVE: |
| 4143 | onCheckMusicActive(); |
| 4144 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4145 | |
| 4146 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4147 | onSendBecomingNoisyIntent(); |
| 4148 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4149 | |
| 4150 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4151 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
| 4152 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED)); |
| 4153 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4154 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4155 | onPersistSafeVolumeState(msg.arg1); |
| 4156 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4157 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4158 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4159 | onBroadcastScoConnectionState(msg.arg1); |
| 4160 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4161 | |
| 4162 | case MSG_SYSTEM_READY: |
| 4163 | onSystemReady(); |
| 4164 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4165 | |
| 4166 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4167 | final int musicActiveMs = msg.arg1; |
| 4168 | Settings.Secure.putIntForUser(mContentResolver, |
| 4169 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4170 | UserHandle.USER_CURRENT); |
| 4171 | break; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4172 | case MSG_PERSIST_MICROPHONE_MUTE: |
| 4173 | Settings.System.putIntForUser(mContentResolver, |
| 4174 | Settings.System.MICROPHONE_MUTE, |
| 4175 | msg.arg1, |
| 4176 | msg.arg2); |
| 4177 | break; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4178 | case MSG_UNMUTE_STREAM: |
| 4179 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4180 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | } |
| 4184 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4185 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4186 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4187 | SettingsObserver() { |
| 4188 | super(new Handler()); |
| 4189 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4190 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4191 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4192 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | @Override |
| 4196 | public void onChange(boolean selfChange) { |
| 4197 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4198 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4199 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4200 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4201 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4202 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4203 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4204 | /* |
| 4205 | * Ensure all stream types that should be affected by ringer mode |
| 4206 | * are in the proper state. |
| 4207 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4208 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4209 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4210 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4211 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4212 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4213 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4214 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4215 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4216 | private void makeA2dpDeviceAvailable(String address) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4217 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4218 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4219 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4220 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4221 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4222 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4223 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4224 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4225 | address, |
| 4226 | "a2dp-device"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4227 | // Reset A2DP suspend state each time a new sink is connected |
| 4228 | AudioSystem.setParameters("A2dpSuspended=false"); |
| 4229 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP), |
| 4230 | address); |
| 4231 | } |
| 4232 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4233 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4234 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4237 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4238 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4239 | synchronized (mA2dpAvrcpLock) { |
| 4240 | mAvrcpAbsVolSupported = false; |
| 4241 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4242 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4243 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4244 | address, |
| 4245 | "a2dp-device"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4246 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4247 | synchronized (mCurAudioRoutes) { |
| 4248 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4249 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4250 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4251 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4252 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4253 | } |
| 4254 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4257 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4258 | private void makeA2dpDeviceUnavailableLater(String address) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4259 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4260 | // reconnection of the sink. |
| 4261 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4262 | // the device will be made unavailable later, so consider it disconnected right away |
| 4263 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
| 4264 | // send the delayed message to make the device unavailable later |
| 4265 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
| 4266 | mAudioHandler.sendMessageDelayed(msg, BTA2DP_DOCK_TIMEOUT_MILLIS); |
| 4267 | |
| 4268 | } |
| 4269 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4270 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4271 | private void makeA2dpSrcAvailable(String address) { |
| 4272 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4273 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4274 | address, |
| 4275 | "a2dp-device"); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4276 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP), |
| 4277 | address); |
| 4278 | } |
| 4279 | |
| 4280 | // must be called synchronized on mConnectedDevices |
| 4281 | private void makeA2dpSrcUnavailable(String address) { |
| 4282 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4283 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4284 | address, |
| 4285 | "a2dp-device"); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4286 | mConnectedDevices.remove(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP); |
| 4287 | } |
| 4288 | |
| 4289 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4290 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4291 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4292 | } |
| 4293 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4294 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4295 | private boolean hasScheduledA2dpDockTimeout() { |
| 4296 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4297 | } |
| 4298 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4299 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4300 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4301 | if (DEBUG_VOL) { |
| 4302 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4303 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4304 | if (btDevice == null) { |
| 4305 | return; |
| 4306 | } |
| 4307 | String address = btDevice.getAddress(); |
| 4308 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4309 | address = ""; |
| 4310 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4311 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4312 | synchronized (mConnectedDevices) { |
| 4313 | boolean isConnected = |
| 4314 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) && |
| 4315 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP).equals(address)); |
| 4316 | |
| 4317 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4318 | if (btDevice.isBluetoothDock()) { |
| 4319 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4320 | // introduction of a delay for transient disconnections of docks when |
| 4321 | // power is rapidly turned off/on, this message will be canceled if |
| 4322 | // we reconnect the dock under a preset delay |
| 4323 | makeA2dpDeviceUnavailableLater(address); |
| 4324 | // the next time isConnected is evaluated, it will be false for the dock |
| 4325 | } |
| 4326 | } else { |
| 4327 | makeA2dpDeviceUnavailableNow(address); |
| 4328 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4329 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4330 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4331 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4332 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4333 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4334 | } |
| 4335 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4336 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4337 | if (btDevice.isBluetoothDock()) { |
| 4338 | // this could be a reconnection after a transient disconnection |
| 4339 | cancelA2dpDeviceTimeout(); |
| 4340 | mDockAddress = address; |
| 4341 | } else { |
| 4342 | // this could be a connection of another A2DP device before the timeout of |
| 4343 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4344 | if(hasScheduledA2dpDockTimeout()) { |
| 4345 | cancelA2dpDeviceTimeout(); |
| 4346 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4347 | } |
| 4348 | } |
| 4349 | makeA2dpDeviceAvailable(address); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4350 | synchronized (mCurAudioRoutes) { |
| 4351 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4352 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4353 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4354 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4355 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4356 | } |
| 4357 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4362 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4363 | { |
| 4364 | if (DEBUG_VOL) { |
| 4365 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4366 | } |
| 4367 | if (btDevice == null) { |
| 4368 | return; |
| 4369 | } |
| 4370 | String address = btDevice.getAddress(); |
| 4371 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4372 | address = ""; |
| 4373 | } |
| 4374 | |
| 4375 | synchronized (mConnectedDevices) { |
| 4376 | boolean isConnected = |
| 4377 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) && |
| 4378 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP).equals(address)); |
| 4379 | |
| 4380 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4381 | makeA2dpSrcUnavailable(address); |
| 4382 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4383 | makeA2dpSrcAvailable(address); |
| 4384 | } |
| 4385 | } |
| 4386 | } |
| 4387 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4388 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4389 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4390 | synchronized (mA2dpAvrcpLock) { |
| 4391 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4392 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4393 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4394 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4395 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4396 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4397 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4401 | private boolean handleDeviceConnection(boolean connect, int device, String address, String deviceName) { |
| 4402 | Slog.i(TAG, "handleDeviceConnection(" + connect + |
| 4403 | " dev:" + Integer.toHexString(device) + |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4404 | " address:" + address + |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4405 | " name:" + deviceName + ")"); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4406 | synchronized (mConnectedDevices) { |
| 4407 | boolean isConnected = (mConnectedDevices.containsKey(device) && |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4408 | (address.isEmpty() || mConnectedDevices.get(device).equals(address))); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4409 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4410 | if (isConnected && !connect) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4411 | AudioSystem.setDeviceConnectionState(device, |
| 4412 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4413 | address, deviceName); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4414 | mConnectedDevices.remove(device); |
| 4415 | return true; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4416 | } else if (!isConnected && connect) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4417 | AudioSystem.setDeviceConnectionState(device, |
| 4418 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4419 | address, deviceName); |
| 4420 | mConnectedDevices.put(new Integer(device), address); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4421 | return true; |
| 4422 | } |
| 4423 | } |
| 4424 | return false; |
| 4425 | } |
| 4426 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4427 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4428 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4429 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4430 | int mBecomingNoisyIntentDevices = |
| 4431 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4432 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4433 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4434 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4435 | |
| 4436 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4437 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4438 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4439 | int delay = 0; |
| 4440 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4441 | int devices = 0; |
| 4442 | for (int dev : mConnectedDevices.keySet()) { |
Eric Laurent | 27c30e4 | 2014-08-27 12:36:33 -0700 | [diff] [blame] | 4443 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) && |
| 4444 | ((dev & mBecomingNoisyIntentDevices) != 0)) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4445 | devices |= dev; |
| 4446 | } |
| 4447 | } |
| 4448 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4449 | sendMsg(mAudioHandler, |
| 4450 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4451 | SENDMSG_REPLACE, |
| 4452 | 0, |
| 4453 | 0, |
| 4454 | null, |
| 4455 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4456 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4460 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4461 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4462 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4463 | synchronized (mLastDeviceConnectMsgTime) { |
| 4464 | long time = SystemClock.uptimeMillis(); |
| 4465 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 4466 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4467 | } |
| 4468 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4469 | } |
| 4470 | return delay; |
| 4471 | } |
| 4472 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4473 | private void sendDeviceConnectionIntent(int device, int state, String address, String deviceName) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4474 | { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4475 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 4476 | " state:0x" + Integer.toHexString(state) + |
| 4477 | " address:" + address + |
| 4478 | " name:" + deviceName + ");"); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4479 | Intent intent = new Intent(); |
| 4480 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4481 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 4482 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 4483 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 4484 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4485 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4486 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4487 | int connType = 0; |
| 4488 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4489 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4490 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4491 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4492 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4493 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 4494 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 4495 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4496 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4497 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4498 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4499 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 4500 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4501 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4502 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4503 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 4504 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4507 | synchronized (mCurAudioRoutes) { |
| 4508 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4509 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4510 | if (state != 0) { |
| 4511 | newConn |= connType; |
| 4512 | } else { |
| 4513 | newConn &= ~connType; |
| 4514 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4515 | if (newConn != mCurAudioRoutes.mainType) { |
| 4516 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4517 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4518 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4519 | } |
| 4520 | } |
| 4521 | } |
| 4522 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4523 | final long ident = Binder.clearCallingIdentity(); |
| 4524 | try { |
| 4525 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4526 | } finally { |
| 4527 | Binder.restoreCallingIdentity(ident); |
| 4528 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4531 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
| 4532 | String deviceName) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4533 | { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4534 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 4535 | + " state:" + Integer.toHexString(state) |
| 4536 | + " address:" + address |
| 4537 | + " deviceName:" + deviceName + ");"); |
| 4538 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4539 | synchronized (mConnectedDevices) { |
| 4540 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4541 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4542 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4543 | setBluetoothA2dpOnInt(true); |
| 4544 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4545 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4546 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4547 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4548 | handleDeviceConnection(state == 1, device, address, deviceName); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4549 | if (state != 0) { |
| 4550 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4551 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4552 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4553 | setBluetoothA2dpOnInt(false); |
| 4554 | } |
| 4555 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4556 | sendMsg(mAudioHandler, |
| 4557 | MSG_CHECK_MUSIC_ACTIVE, |
| 4558 | SENDMSG_REPLACE, |
| 4559 | 0, |
| 4560 | 0, |
| 4561 | null, |
| 4562 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4563 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4564 | // Television devices without CEC service apply software volume on HDMI output |
| 4565 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4566 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 4567 | checkAllFixedVolumeDevices(); |
| 4568 | if (mHdmiManager != null) { |
| 4569 | synchronized (mHdmiManager) { |
| 4570 | if (mHdmiPlaybackClient != null) { |
| 4571 | mHdmiCecSink = false; |
| 4572 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 4573 | } |
| 4574 | } |
| 4575 | } |
| 4576 | } |
| 4577 | } else { |
| 4578 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4579 | if (mHdmiManager != null) { |
| 4580 | synchronized (mHdmiManager) { |
| 4581 | mHdmiCecSink = false; |
| 4582 | } |
| 4583 | } |
| 4584 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4585 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4586 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 4587 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4588 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4592 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4593 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 4594 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4595 | if (state == 1) { |
| 4596 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 4597 | int[] portGeneration = new int[1]; |
| 4598 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 4599 | if (status == AudioManager.SUCCESS) { |
| 4600 | for (AudioPort port : ports) { |
| 4601 | if (port instanceof AudioDevicePort) { |
| 4602 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4603 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 4604 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4605 | // format the list of supported encodings |
| 4606 | int[] formats = devicePort.formats(); |
| 4607 | if (formats.length > 0) { |
| 4608 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 4609 | for (int format : formats) { |
| 4610 | // a format in the list can be 0, skip it |
| 4611 | if (format != AudioFormat.ENCODING_INVALID) { |
| 4612 | encodingList.add(format); |
| 4613 | } |
| 4614 | } |
| 4615 | int[] encodingArray = new int[encodingList.size()]; |
| 4616 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 4617 | encodingArray[i] = encodingList.get(i); |
| 4618 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4619 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4620 | } |
| 4621 | // find the maximum supported number of channels |
| 4622 | int maxChannels = 0; |
| 4623 | for (int mask : devicePort.channelMasks()) { |
| 4624 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 4625 | if (channelCount > maxChannels) { |
| 4626 | maxChannels = channelCount; |
| 4627 | } |
| 4628 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4629 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4630 | } |
| 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | } |
| 4635 | } |
| 4636 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4637 | /* cache of the address of the last dock the device was connected to */ |
| 4638 | private String mDockAddress; |
| 4639 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4640 | /** |
| 4641 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 4642 | */ |
| 4643 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 4644 | @Override |
| 4645 | public void onReceive(Context context, Intent intent) { |
| 4646 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4647 | int outDevice; |
| 4648 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4649 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4650 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4651 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 4652 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4653 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4654 | int config; |
| 4655 | switch (dockState) { |
| 4656 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 4657 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 4658 | break; |
| 4659 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 4660 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 4661 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4662 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4663 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4664 | break; |
| 4665 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 4666 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 4667 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4668 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 4669 | default: |
| 4670 | config = AudioSystem.FORCE_NONE; |
| 4671 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4672 | // Low end docks have a menu to enable or disable audio |
| 4673 | // (see mDockAudioMediaEnabled) |
| 4674 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 4675 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 4676 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 4677 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 4678 | } |
| 4679 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4680 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4681 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4682 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4683 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 4684 | inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Nick Pelly | a56d1c7 | 2009-08-19 14:49:29 -0700 | [diff] [blame] | 4685 | String address = null; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4686 | |
| 4687 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 4688 | if (btDevice == null) { |
| 4689 | return; |
| 4690 | } |
| 4691 | |
| 4692 | address = btDevice.getAddress(); |
| 4693 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 4694 | if (btClass != null) { |
| 4695 | switch (btClass.getDeviceClass()) { |
| 4696 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 4697 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4698 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4699 | break; |
| 4700 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4701 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4702 | break; |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4703 | } |
| 4704 | } |
| 4705 | |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4706 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4707 | address = ""; |
| 4708 | } |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4709 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4710 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4711 | boolean success = |
| 4712 | handleDeviceConnection(connected, outDevice, address, "Bluetooth Headset") && |
| 4713 | handleDeviceConnection(connected, inDevice, address, "Bluetooth Headset"); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4714 | if (success) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4715 | synchronized (mScoClients) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4716 | if (connected) { |
| 4717 | mBluetoothHeadsetDevice = btDevice; |
| 4718 | } else { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4719 | mBluetoothHeadsetDevice = null; |
| 4720 | resetBluetoothSco(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4721 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4722 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4723 | } |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4724 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4725 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4726 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4727 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4728 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4729 | // broadcast intent if the connection was initated by AudioService |
| 4730 | if (!mScoClients.isEmpty() && |
| 4731 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 4732 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 4733 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4734 | broadcast = true; |
| 4735 | } |
| 4736 | switch (btState) { |
| 4737 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4738 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4739 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4740 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4741 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4742 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4743 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4744 | break; |
| 4745 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4746 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4747 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 4748 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4749 | break; |
| 4750 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4751 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4752 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4753 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4754 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4755 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4756 | default: |
| 4757 | // do not broadcast CONNECTING or invalid state |
| 4758 | broadcast = false; |
| 4759 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4760 | } |
| 4761 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4762 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4763 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4764 | //FIXME: this is to maintain compatibility with deprecated intent |
| 4765 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4766 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4767 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4768 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4769 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4770 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4771 | if (mMonitorRotation) { |
| 4772 | mOrientationListener.onOrientationChanged(0); //argument is ignored anyway |
| 4773 | mOrientationListener.enable(); |
| 4774 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4775 | AudioSystem.setParameters("screen_state=on"); |
| 4776 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4777 | if (mMonitorRotation) { |
| 4778 | //reduce wakeups (save current) by only listening when display is on |
| 4779 | mOrientationListener.disable(); |
| 4780 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4781 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4782 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4783 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4784 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4785 | // attempt to stop music playback for background user |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4786 | sendMsg(mAudioHandler, |
| 4787 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4788 | SENDMSG_REPLACE, |
| 4789 | 0, |
| 4790 | 0, |
| 4791 | null, |
| 4792 | 0); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4793 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4794 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4795 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4796 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4797 | readAudioSettings(true /*userSwitch*/); |
| 4798 | // preserve STREAM_MUSIC volume from one user to the next. |
| 4799 | sendMsg(mAudioHandler, |
| 4800 | MSG_SET_ALL_VOLUMES, |
| 4801 | SENDMSG_QUEUE, |
| 4802 | 0, |
| 4803 | 0, |
| 4804 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4805 | } |
| 4806 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4807 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4808 | |
| 4809 | //========================================================================================== |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4810 | // RemoteControlDisplay / RemoteControlClient / Remote info |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4811 | //========================================================================================== |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4812 | public boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, |
| 4813 | ComponentName listenerComp) { |
| 4814 | return mMediaFocusControl.registerRemoteController(rcd, w, h, listenerComp); |
| 4815 | } |
| 4816 | |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4817 | public boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4818 | return mMediaFocusControl.registerRemoteControlDisplay(rcd, w, h); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4819 | } |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4820 | |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4821 | public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4822 | mMediaFocusControl.unregisterRemoteControlDisplay(rcd); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4826 | mMediaFocusControl.remoteControlDisplayUsesBitmapSize(rcd, w, h); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4827 | } |
| 4828 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 4829 | public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, |
| 4830 | boolean wantsSync) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4831 | mMediaFocusControl.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync); |
| 4832 | } |
| 4833 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4834 | @Override |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4835 | public void setRemoteStreamVolume(int index) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 4836 | enforceVolumeController("set the remote stream volume"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4837 | mMediaFocusControl.setRemoteStreamVolume(index); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4840 | //========================================================================================== |
| 4841 | // Audio Focus |
| 4842 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4843 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4844 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4845 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4846 | // permission checks |
| 4847 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4848 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4849 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 4850 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 4851 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 4852 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 4853 | } |
| 4854 | } else { |
| 4855 | // only a registered audio policy can be used to lock focus |
| 4856 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4857 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 4858 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4859 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 4860 | } |
| 4861 | } |
| 4862 | } |
| 4863 | } |
| 4864 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4865 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 4866 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4867 | } |
| 4868 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4869 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 4870 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | public void unregisterAudioFocusClient(String clientId) { |
| 4874 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4875 | } |
| 4876 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 4877 | public int getCurrentAudioFocus() { |
| 4878 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 4879 | } |
| 4880 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 4881 | private boolean readCameraSoundForced() { |
| 4882 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 4883 | mContext.getResources().getBoolean( |
| 4884 | com.android.internal.R.bool.config_camera_sound_forced); |
| 4885 | } |
| 4886 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4887 | //========================================================================================== |
| 4888 | // Device orientation |
| 4889 | //========================================================================================== |
| 4890 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4891 | * Handles device configuration changes that may map to a change in the orientation |
| 4892 | * or orientation. |
| 4893 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 4894 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4895 | */ |
| 4896 | private void handleConfigurationChanged(Context context) { |
| 4897 | try { |
| 4898 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 4899 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4900 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4901 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4902 | if (mMonitorOrientation) { |
| 4903 | int newOrientation = config.orientation; |
| 4904 | if (newOrientation != mDeviceOrientation) { |
| 4905 | mDeviceOrientation = newOrientation; |
| 4906 | setOrientationForAudioSystem(); |
| 4907 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4908 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4909 | sendMsg(mAudioHandler, |
| 4910 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 4911 | SENDMSG_REPLACE, |
| 4912 | 0, |
| 4913 | 0, |
| 4914 | null, |
| 4915 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4916 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 4917 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4918 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4919 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4920 | synchronized (mCameraSoundForced) { |
| 4921 | if (cameraSoundForced != mCameraSoundForced) { |
| 4922 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4923 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4924 | } |
| 4925 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4926 | if (cameraSoundForcedChanged) { |
| 4927 | if (!isPlatformTelevision()) { |
| 4928 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 4929 | if (cameraSoundForced) { |
| 4930 | s.setAllIndexesToMax(); |
| 4931 | mRingerModeAffectedStreams &= |
| 4932 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4933 | } else { |
| 4934 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM]); |
| 4935 | mRingerModeAffectedStreams |= |
| 4936 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4937 | } |
| 4938 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4939 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | sendMsg(mAudioHandler, |
| 4943 | MSG_SET_FORCE_USE, |
| 4944 | SENDMSG_QUEUE, |
| 4945 | AudioSystem.FOR_SYSTEM, |
| 4946 | cameraSoundForced ? |
| 4947 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 4948 | null, |
| 4949 | 0); |
| 4950 | |
| 4951 | sendMsg(mAudioHandler, |
| 4952 | MSG_SET_ALL_VOLUMES, |
| 4953 | SENDMSG_QUEUE, |
| 4954 | 0, |
| 4955 | 0, |
| 4956 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 4957 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4958 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4959 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4960 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4961 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | private void setOrientationForAudioSystem() { |
| 4966 | switch (mDeviceOrientation) { |
| 4967 | case Configuration.ORIENTATION_LANDSCAPE: |
| 4968 | //Log.i(TAG, "orientation is landscape"); |
| 4969 | AudioSystem.setParameters("orientation=landscape"); |
| 4970 | break; |
| 4971 | case Configuration.ORIENTATION_PORTRAIT: |
| 4972 | //Log.i(TAG, "orientation is portrait"); |
| 4973 | AudioSystem.setParameters("orientation=portrait"); |
| 4974 | break; |
| 4975 | case Configuration.ORIENTATION_SQUARE: |
| 4976 | //Log.i(TAG, "orientation is square"); |
| 4977 | AudioSystem.setParameters("orientation=square"); |
| 4978 | break; |
| 4979 | case Configuration.ORIENTATION_UNDEFINED: |
| 4980 | //Log.i(TAG, "orientation is undefined"); |
| 4981 | AudioSystem.setParameters("orientation=undefined"); |
| 4982 | break; |
| 4983 | default: |
| 4984 | Log.e(TAG, "Unknown orientation"); |
| 4985 | } |
| 4986 | } |
| 4987 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4988 | private void setRotationForAudioSystem() { |
| 4989 | switch (mDeviceRotation) { |
| 4990 | case Surface.ROTATION_0: |
| 4991 | AudioSystem.setParameters("rotation=0"); |
| 4992 | break; |
| 4993 | case Surface.ROTATION_90: |
| 4994 | AudioSystem.setParameters("rotation=90"); |
| 4995 | break; |
| 4996 | case Surface.ROTATION_180: |
| 4997 | AudioSystem.setParameters("rotation=180"); |
| 4998 | break; |
| 4999 | case Surface.ROTATION_270: |
| 5000 | AudioSystem.setParameters("rotation=270"); |
| 5001 | break; |
| 5002 | default: |
| 5003 | Log.e(TAG, "Unknown device rotation"); |
| 5004 | } |
| 5005 | } |
| 5006 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5007 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5008 | // Handles request to override default use of A2DP for media. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5009 | // Must be called synchronized on mConnectedDevices |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5010 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5011 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5012 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5013 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5014 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5015 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5019 | // Must be called synchronized on mConnectedDevices |
| 5020 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5021 | switch (usage) { |
| 5022 | case AudioSystem.FOR_MEDIA: |
| 5023 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5024 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5025 | } else { // config == AudioSystem.FORCE_NONE |
| 5026 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5027 | } |
| 5028 | break; |
| 5029 | case AudioSystem.FOR_DOCK: |
| 5030 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5031 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5032 | } else { // config == AudioSystem.FORCE_NONE |
| 5033 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5034 | } |
| 5035 | break; |
| 5036 | default: |
| 5037 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5038 | } |
| 5039 | AudioSystem.setForceUse(usage, config); |
| 5040 | } |
| 5041 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5042 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5043 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5044 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5045 | mRingtonePlayer = player; |
| 5046 | } |
| 5047 | |
| 5048 | @Override |
| 5049 | public IRingtonePlayer getRingtonePlayer() { |
| 5050 | return mRingtonePlayer; |
| 5051 | } |
| 5052 | |
| 5053 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5054 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5055 | synchronized (mCurAudioRoutes) { |
| 5056 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5057 | mRoutesObservers.register(observer); |
| 5058 | return routes; |
| 5059 | } |
| 5060 | } |
| 5061 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5062 | |
| 5063 | //========================================================================================== |
| 5064 | // Safe media volume management. |
| 5065 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5066 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5067 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5068 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5069 | // property. Platforms with a different limit must set this property accordingly in their |
| 5070 | // overlay. |
| 5071 | //========================================================================================== |
| 5072 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5073 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5074 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5075 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5076 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5077 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5078 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5079 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5080 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5081 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5082 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5083 | private Integer mSafeMediaVolumeState; |
| 5084 | |
| 5085 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5086 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5087 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5088 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5089 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5090 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5091 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5092 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5093 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5094 | private int mMusicActiveMs; |
| 5095 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5096 | private static final int MUSIC_ACTIVE_POLL_PERIOD_MS = 60000; // 1 minute polling interval |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5097 | private static final int SAFE_VOLUME_CONFIGURE_TIMEOUT_MS = 30000; // 30s after boot completed |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5098 | |
| 5099 | private void setSafeMediaVolumeEnabled(boolean on) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5100 | synchronized (mSafeMediaVolumeState) { |
| 5101 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5102 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5103 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5104 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 5105 | enforceSafeMediaVolume(); |
| 5106 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5107 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5108 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5109 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5110 | sendMsg(mAudioHandler, |
| 5111 | MSG_CHECK_MUSIC_ACTIVE, |
| 5112 | SENDMSG_REPLACE, |
| 5113 | 0, |
| 5114 | 0, |
| 5115 | null, |
| 5116 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5117 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5118 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | |
| 5122 | private void enforceSafeMediaVolume() { |
| 5123 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5124 | int devices = mSafeMediaVolumeDevices; |
| 5125 | int i = 0; |
| 5126 | |
| 5127 | while (devices != 0) { |
| 5128 | int device = 1 << i++; |
| 5129 | if ((device & devices) == 0) { |
| 5130 | continue; |
| 5131 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5132 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5133 | if (index > mSafeMediaVolumeIndex) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5134 | streamState.setIndex(mSafeMediaVolumeIndex, device); |
| 5135 | sendMsg(mAudioHandler, |
| 5136 | MSG_SET_DEVICE_VOLUME, |
| 5137 | SENDMSG_QUEUE, |
| 5138 | device, |
| 5139 | 0, |
| 5140 | streamState, |
| 5141 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5142 | } |
| 5143 | devices &= ~device; |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5148 | synchronized (mSafeMediaVolumeState) { |
| 5149 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5150 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5151 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5152 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5153 | return false; |
| 5154 | } |
| 5155 | return true; |
| 5156 | } |
| 5157 | } |
| 5158 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5159 | @Override |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5160 | public void disableSafeMediaVolume() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5161 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5162 | synchronized (mSafeMediaVolumeState) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5163 | setSafeMediaVolumeEnabled(false); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5164 | if (mPendingVolumeCommand != null) { |
| 5165 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5166 | mPendingVolumeCommand.mIndex, |
| 5167 | mPendingVolumeCommand.mFlags, |
| 5168 | mPendingVolumeCommand.mDevice); |
| 5169 | mPendingVolumeCommand = null; |
| 5170 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5171 | } |
| 5172 | } |
| 5173 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5174 | //========================================================================================== |
| 5175 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5176 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5177 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5178 | //========================================================================================== |
| 5179 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5180 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5181 | public void onComplete(int status) { |
| 5182 | if (mHdmiManager != null) { |
| 5183 | synchronized (mHdmiManager) { |
| 5184 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5185 | // Television devices without CEC service apply software volume on HDMI output |
| 5186 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5187 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5188 | } |
| 5189 | checkAllFixedVolumeDevices(); |
| 5190 | } |
| 5191 | } |
| 5192 | } |
| 5193 | }; |
| 5194 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5195 | // If HDMI-CEC system audio is supported |
| 5196 | private boolean mHdmiSystemAudioSupported = false; |
| 5197 | // Set only when device is tv. |
| 5198 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5199 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5200 | // cached HdmiControlManager interface |
| 5201 | private HdmiControlManager mHdmiManager; |
| 5202 | // Set only when device is a set-top box. |
| 5203 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5204 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5205 | private boolean mHdmiCecSink; |
| 5206 | |
| 5207 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5208 | |
| 5209 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5210 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5211 | int device = AudioSystem.DEVICE_NONE; |
| 5212 | if (mHdmiManager != null) { |
| 5213 | synchronized (mHdmiManager) { |
| 5214 | if (mHdmiTvClient == null) { |
| 5215 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5216 | return device; |
| 5217 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5218 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5219 | synchronized (mHdmiTvClient) { |
| 5220 | if (mHdmiSystemAudioSupported != on) { |
| 5221 | mHdmiSystemAudioSupported = on; |
| 5222 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5223 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5224 | AudioSystem.FORCE_NONE); |
| 5225 | } |
| 5226 | device = AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC); |
| 5227 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5228 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5229 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5230 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5231 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5232 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5233 | @Override |
| 5234 | public boolean isHdmiSystemAudioSupported() { |
| 5235 | return mHdmiSystemAudioSupported; |
| 5236 | } |
| 5237 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5238 | //========================================================================================== |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5239 | // Accessibility: taking touch exploration into account for selecting the default |
| 5240 | // stream override timeout when adjusting volume |
| 5241 | //========================================================================================== |
| 5242 | private static class StreamOverride |
| 5243 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 5244 | |
| 5245 | // AudioService.getActiveStreamType() will return: |
| 5246 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5247 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5248 | // stopped |
| 5249 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 5000; |
| 5250 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
| 5251 | |
| 5252 | static int sDelayMs; |
| 5253 | |
| 5254 | static void init(Context ctxt) { |
| 5255 | AccessibilityManager accessibilityManager = |
| 5256 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5257 | updateDefaultStreamOverrideDelay( |
| 5258 | accessibilityManager.isTouchExplorationEnabled()); |
| 5259 | accessibilityManager.addTouchExplorationStateChangeListener( |
| 5260 | new StreamOverride()); |
| 5261 | } |
| 5262 | |
| 5263 | @Override |
| 5264 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5265 | updateDefaultStreamOverrideDelay(enabled); |
| 5266 | } |
| 5267 | |
| 5268 | private static void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5269 | if (touchExploreEnabled) { |
| 5270 | sDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5271 | } else { |
| 5272 | sDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5273 | } |
| 5274 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5275 | + " stream override delay is now " + sDelayMs + " ms"); |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5280 | // Camera shutter sound policy. |
| 5281 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5282 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5283 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5284 | //========================================================================================== |
| 5285 | |
| 5286 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5287 | private Boolean mCameraSoundForced; |
| 5288 | |
| 5289 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5290 | public boolean isCameraSoundForced() { |
| 5291 | synchronized (mCameraSoundForced) { |
| 5292 | return mCameraSoundForced; |
| 5293 | } |
| 5294 | } |
| 5295 | |
| 5296 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5297 | "SILENT", |
| 5298 | "VIBRATE", |
| 5299 | "NORMAL" |
| 5300 | }; |
| 5301 | |
| 5302 | private void dumpRingerMode(PrintWriter pw) { |
| 5303 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5304 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5305 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5306 | pw.print("- ringer mode affected streams = 0x"); |
| 5307 | pw.println(Integer.toHexString(mRingerModeAffectedStreams)); |
| 5308 | pw.print("- ringer mode muted streams = 0x"); |
| 5309 | pw.println(Integer.toHexString(mRingerModeMutedStreams)); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5310 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5311 | } |
| 5312 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5313 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5314 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5315 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5316 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5317 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5318 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5319 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5320 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5321 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 5322 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5323 | |
| 5324 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5325 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5326 | pw.print(" mSafeMediaVolumeState="); |
| 5327 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5328 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5329 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5330 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5331 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5332 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5333 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5334 | pw.print(" mControllerService="); pw.println(mControllerService); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5335 | |
| 5336 | dumpAudioPolicies(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5340 | switch(state) { |
| 5341 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5342 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5343 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5344 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5345 | } |
| 5346 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5347 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5348 | |
| 5349 | // Inform AudioFlinger of our device's low RAM attribute |
| 5350 | private static void readAndSetLowRamDevice() |
| 5351 | { |
| 5352 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5353 | if (status != 0) { |
| 5354 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5355 | } |
| 5356 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5357 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5358 | private void enforceVolumeController(String action) { |
| 5359 | if (mControllerService.mUid != 0 && Binder.getCallingUid() == mControllerService.mUid) { |
| 5360 | return; |
| 5361 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5362 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5363 | "Only SystemUI can " + action); |
| 5364 | } |
| 5365 | |
| 5366 | @Override |
| 5367 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5368 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5369 | |
| 5370 | // return early if things are not actually changing |
| 5371 | if (mVolumeController.isSameBinder(controller)) { |
| 5372 | return; |
| 5373 | } |
| 5374 | |
| 5375 | // dismiss the old volume controller |
| 5376 | mVolumeController.postDismiss(); |
| 5377 | if (controller != null) { |
| 5378 | // we are about to register a new controller, listen for its death |
| 5379 | try { |
| 5380 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5381 | @Override |
| 5382 | public void binderDied() { |
| 5383 | if (mVolumeController.isSameBinder(controller)) { |
| 5384 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5385 | setVolumeController(null); |
| 5386 | } |
| 5387 | } |
| 5388 | }, 0); |
| 5389 | } catch (RemoteException e) { |
| 5390 | // noop |
| 5391 | } |
| 5392 | } |
| 5393 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5394 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5395 | } |
| 5396 | |
| 5397 | @Override |
| 5398 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5399 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5400 | |
| 5401 | // return early if the controller is not current |
| 5402 | if (!mVolumeController.isSameBinder(controller)) { |
| 5403 | return; |
| 5404 | } |
| 5405 | |
| 5406 | mVolumeController.setVisible(visible); |
| 5407 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5408 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5409 | |
| 5410 | public static class VolumeController { |
| 5411 | private static final String TAG = "VolumeController"; |
| 5412 | |
| 5413 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5414 | private boolean mVisible; |
| 5415 | private long mNextLongPress; |
| 5416 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5417 | |
| 5418 | public void setController(IVolumeController controller) { |
| 5419 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5420 | mVisible = false; |
| 5421 | } |
| 5422 | |
| 5423 | public void loadSettings(ContentResolver cr) { |
| 5424 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5425 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5426 | } |
| 5427 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5428 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5429 | if (isMute) { |
| 5430 | return false; |
| 5431 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5432 | boolean suppress = false; |
| 5433 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5434 | final long now = SystemClock.uptimeMillis(); |
| 5435 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5436 | // ui will become visible |
| 5437 | if (mNextLongPress < now) { |
| 5438 | mNextLongPress = now + mLongPressTimeout; |
| 5439 | } |
| 5440 | suppress = true; |
| 5441 | } else if (mNextLongPress > 0) { // in a long-press |
| 5442 | if (now > mNextLongPress) { |
| 5443 | // long press triggered, no more suppression |
| 5444 | mNextLongPress = 0; |
| 5445 | } else { |
| 5446 | // keep suppressing until the long press triggers |
| 5447 | suppress = true; |
| 5448 | } |
| 5449 | } |
| 5450 | } |
| 5451 | return suppress; |
| 5452 | } |
| 5453 | |
| 5454 | public void setVisible(boolean visible) { |
| 5455 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | public boolean isSameBinder(IVolumeController controller) { |
| 5459 | return Objects.equals(asBinder(), binder(controller)); |
| 5460 | } |
| 5461 | |
| 5462 | public IBinder asBinder() { |
| 5463 | return binder(mController); |
| 5464 | } |
| 5465 | |
| 5466 | private static IBinder binder(IVolumeController controller) { |
| 5467 | return controller == null ? null : controller.asBinder(); |
| 5468 | } |
| 5469 | |
| 5470 | @Override |
| 5471 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5472 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5476 | if (mController == null) |
| 5477 | return; |
| 5478 | try { |
| 5479 | mController.displaySafeVolumeWarning(flags); |
| 5480 | } catch (RemoteException e) { |
| 5481 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5482 | } |
| 5483 | } |
| 5484 | |
| 5485 | public void postVolumeChanged(int streamType, int flags) { |
| 5486 | if (mController == null) |
| 5487 | return; |
| 5488 | try { |
| 5489 | mController.volumeChanged(streamType, flags); |
| 5490 | } catch (RemoteException e) { |
| 5491 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5492 | } |
| 5493 | } |
| 5494 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5495 | public void postMasterMuteChanged(int flags) { |
| 5496 | if (mController == null) |
| 5497 | return; |
| 5498 | try { |
| 5499 | mController.masterMuteChanged(flags); |
| 5500 | } catch (RemoteException e) { |
| 5501 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5502 | } |
| 5503 | } |
| 5504 | |
| 5505 | public void setLayoutDirection(int layoutDirection) { |
| 5506 | if (mController == null) |
| 5507 | return; |
| 5508 | try { |
| 5509 | mController.setLayoutDirection(layoutDirection); |
| 5510 | } catch (RemoteException e) { |
| 5511 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5512 | } |
| 5513 | } |
| 5514 | |
| 5515 | public void postDismiss() { |
| 5516 | if (mController == null) |
| 5517 | return; |
| 5518 | try { |
| 5519 | mController.dismiss(); |
| 5520 | } catch (RemoteException e) { |
| 5521 | Log.w(TAG, "Error calling dismiss", e); |
| 5522 | } |
| 5523 | } |
| 5524 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5525 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5526 | /** |
| 5527 | * Interface for system components to get some extra functionality through |
| 5528 | * LocalServices. |
| 5529 | */ |
| 5530 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5531 | @Override |
| 5532 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 5533 | mRingerModeDelegate = delegate; |
| 5534 | if (mRingerModeDelegate != null) { |
| 5535 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 5536 | } |
| 5537 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 5538 | |
| 5539 | @Override |
| 5540 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 5541 | String callingPackage, int uid) { |
| 5542 | // direction and stream type swap here because the public |
| 5543 | // adjustSuggested has a different order than the other methods. |
| 5544 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, uid); |
| 5545 | } |
| 5546 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5547 | @Override |
| 5548 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 5549 | String callingPackage, int uid) { |
| 5550 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5551 | } |
| 5552 | |
| 5553 | @Override |
| 5554 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 5555 | String callingPackage, int uid) { |
| 5556 | setStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5557 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 5558 | |
| 5559 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5560 | public int getRingerModeInternal() { |
| 5561 | return AudioService.this.getRingerModeInternal(); |
| 5562 | } |
| 5563 | |
| 5564 | @Override |
| 5565 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 5566 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 5567 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5568 | |
| 5569 | @Override |
| 5570 | public int getVolumeControllerUid() { |
| 5571 | return mControllerService.mUid; |
| 5572 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5573 | } |
| 5574 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5575 | //========================================================================================== |
| 5576 | // Audio policy management |
| 5577 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5578 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 5579 | boolean hasFocusListener) { |
| 5580 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 5581 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5582 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5583 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5584 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5585 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5586 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5587 | if (!hasPermissionForPolicy) { |
| 5588 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 5589 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5590 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5591 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5592 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5593 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5594 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5595 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5596 | Slog.e(TAG, "Cannot re-register policy"); |
| 5597 | return null; |
| 5598 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5599 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 5600 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 5601 | regId = app.getRegistrationId(); |
| 5602 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5603 | } catch (RemoteException e) { |
| 5604 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5605 | Slog.w(TAG, "Audio policy registration failed, could not link to " + pcb + |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5606 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5607 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5608 | } |
| 5609 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5610 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5611 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5612 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5613 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 5614 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5615 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5616 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5617 | if (app == null) { |
| 5618 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 5619 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5620 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5621 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5622 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5623 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5624 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5625 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5626 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5627 | } |
| 5628 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5629 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 5630 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 5631 | + " policy " + pcb.asBinder()); |
| 5632 | // error handling |
| 5633 | boolean hasPermissionForPolicy = |
| 5634 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 5635 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5636 | if (!hasPermissionForPolicy) { |
| 5637 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 5638 | + Binder.getCallingPid() + " / uid " |
| 5639 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 5640 | return AudioManager.ERROR; |
| 5641 | } |
| 5642 | |
| 5643 | synchronized (mAudioPolicies) { |
| 5644 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5645 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 5646 | return AudioManager.ERROR; |
| 5647 | } |
| 5648 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 5649 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5650 | // is there already one policy managing ducking? |
| 5651 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5652 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5653 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 5654 | return AudioManager.ERROR; |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | app.mFocusDuckBehavior = duckingBehavior; |
| 5659 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 5660 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 5661 | } |
| 5662 | return AudioManager.SUCCESS; |
| 5663 | } |
| 5664 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5665 | private void dumpAudioPolicies(PrintWriter pw) { |
| 5666 | pw.println("\nAudio policies:"); |
| 5667 | synchronized (mAudioPolicies) { |
| 5668 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5669 | pw.println(policy.toLogFriendlyString()); |
| 5670 | } |
| 5671 | } |
| 5672 | } |
| 5673 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5674 | //====================== |
| 5675 | // Audio policy proxy |
| 5676 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5677 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5678 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 5679 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5680 | */ |
| 5681 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5682 | private static final String TAG = "AudioPolicyProxy"; |
| 5683 | AudioPolicyConfig mConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5684 | IAudioPolicyCallback mPolicyToken; |
| 5685 | boolean mHasFocusListener; |
| 5686 | /** |
| 5687 | * Audio focus ducking behavior for an audio policy. |
| 5688 | * This variable reflects the value that was successfully set in |
| 5689 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 5690 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 5691 | * is handling ducking for audio focus. |
| 5692 | */ |
| 5693 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 5694 | |
| 5695 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 5696 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5697 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5698 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5699 | mPolicyToken = token; |
| 5700 | mHasFocusListener = hasFocusListener; |
| 5701 | if (mHasFocusListener) { |
| 5702 | mMediaFocusControl.addFocusFollower(mPolicyToken); |
| 5703 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5704 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | public void binderDied() { |
| 5708 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5709 | Log.i(TAG, "audio policy " + mPolicyToken + " died"); |
| 5710 | release(); |
| 5711 | mAudioPolicies.remove(mPolicyToken.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5712 | } |
| 5713 | } |
| 5714 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5715 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5716 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5717 | } |
| 5718 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5719 | void release() { |
| 5720 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5721 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 5722 | } |
| 5723 | if (mHasFocusListener) { |
| 5724 | mMediaFocusControl.removeFocusFollower(mPolicyToken); |
| 5725 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5726 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5727 | } |
| 5728 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5729 | void connectMixes() { |
| 5730 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5731 | } |
| 5732 | }; |
| 5733 | |
| 5734 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 5735 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5736 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5737 | |
| 5738 | private class ControllerService extends ContentObserver { |
| 5739 | private int mUid; |
| 5740 | private ComponentName mComponent; |
| 5741 | |
| 5742 | public ControllerService() { |
| 5743 | super(null); |
| 5744 | } |
| 5745 | |
| 5746 | @Override |
| 5747 | public String toString() { |
| 5748 | return String.format("{mUid=%d,mComponent=%s}", mUid, mComponent); |
| 5749 | } |
| 5750 | |
| 5751 | public void init() { |
| 5752 | onChange(true); |
| 5753 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 5754 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT), false, this); |
| 5755 | } |
| 5756 | |
| 5757 | @Override |
| 5758 | public void onChange(boolean selfChange) { |
| 5759 | mUid = 0; |
| 5760 | mComponent = null; |
| 5761 | final String setting = Settings.Secure.getString(mContentResolver, |
| 5762 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT); |
| 5763 | if (setting == null) return; |
| 5764 | try { |
| 5765 | mComponent = ComponentName.unflattenFromString(setting); |
| 5766 | if (mComponent == null) return; |
| 5767 | mUid = mContext.getPackageManager() |
| 5768 | .getApplicationInfo(mComponent.getPackageName(), 0).uid; |
| 5769 | } catch (Exception e) { |
| 5770 | Log.w(TAG, "Error loading controller service", e); |
| 5771 | } |
| 5772 | if (DEBUG_VOL) Log.d(TAG, "Reloaded controller service: " + this); |
| 5773 | } |
| 5774 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5775 | } |