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; |
| 57 | import android.media.AudioSystem; |
| 58 | import android.media.IAudioFocusDispatcher; |
| 59 | import android.media.IAudioRoutesObserver; |
| 60 | import android.media.IAudioService; |
| 61 | import android.media.IRemoteControlDisplay; |
| 62 | import android.media.IRingtonePlayer; |
| 63 | import android.media.IVolumeController; |
| 64 | import android.media.MediaPlayer; |
| 65 | import android.media.SoundPool; |
| 66 | import android.media.AudioAttributes.Builder; |
| 67 | import android.media.AudioManagerInternal.RingerModeDelegate; |
| 68 | import android.media.AudioSystem.ErrorCallback; |
| 69 | import android.media.IAudioService.Stub; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.media.MediaPlayer.OnCompletionListener; |
| 71 | import android.media.MediaPlayer.OnErrorListener; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 72 | import android.media.SoundPool.OnLoadCompleteListener; |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 73 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 74 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 75 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 76 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 78 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.os.Environment; |
| 80 | import android.os.Handler; |
| 81 | import android.os.IBinder; |
| 82 | import android.os.Looper; |
| 83 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 84 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 85 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 87 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 88 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 89 | import android.os.UserHandle; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 90 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.provider.Settings; |
| 92 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 93 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 94 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 96 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 97 | import android.util.Slog; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 98 | import android.view.KeyEvent; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 99 | import android.view.OrientationEventListener; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 100 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 101 | import android.view.WindowManager; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 102 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 104 | import com.android.internal.util.XmlUtils; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 105 | import com.android.server.LocalServices; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 106 | |
| 107 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 109 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 111 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 112 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 114 | import java.util.HashMap; |
| 115 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 116 | import java.util.List; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 117 | import java.util.Map; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 118 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 119 | import java.util.Objects; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 120 | import java.util.Set; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 121 | import java.util.concurrent.ConcurrentHashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * The implementation of the volume manager service. |
| 125 | * <p> |
| 126 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 127 | * asynchronous to external calls. For example, the task of setting a volume |
| 128 | * will update our internal state, but in a separate thread will set the system |
| 129 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 130 | * will update the state and broadcast a change and in a separate thread later |
| 131 | * persist the ringer mode. |
| 132 | * |
| 133 | * @hide |
| 134 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 135 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
| 137 | private static final String TAG = "AudioService"; |
| 138 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 139 | /** Debug audio mode */ |
| 140 | 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] | 141 | |
| 142 | /** Debug audio policy feature */ |
| 143 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 144 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 145 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 146 | 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] | 147 | |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 148 | /** debug calls to media session apis */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 149 | private static final boolean DEBUG_SESSIONS = Log.isLoggable(TAG + ".SESSIONS", Log.DEBUG); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 150 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 151 | /** Allow volume changes to set ringer mode to silent? */ |
| 152 | private static final boolean VOLUME_SETS_RINGER_MODE_SILENT = false; |
| 153 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 154 | /** In silent mode, are volume adjustments (raises) prevented? */ |
| 155 | private static final boolean PREVENT_VOLUME_ADJUSTMENT_IF_SILENT = true; |
| 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 158 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 160 | /** How long to delay after a volume down event before unmuting a stream */ |
| 161 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 162 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 163 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 164 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 165 | */ |
| 166 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 167 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 168 | private final Context mContext; |
| 169 | private final ContentResolver mContentResolver; |
| 170 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 171 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 172 | // the platform type affects volume and silent mode behavior |
| 173 | private final int mPlatformType; |
| 174 | |
| 175 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 176 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 180 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 181 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 182 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 183 | /** The controller for the volume UI. */ |
| 184 | private final VolumeController mVolumeController = new VolumeController(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 185 | private final ControllerService mControllerService = new ControllerService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
| 187 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | /** If the msg is already queued, replace it with this one. */ |
| 189 | private static final int SENDMSG_REPLACE = 0; |
| 190 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 191 | private static final int SENDMSG_NOOP = 1; |
| 192 | /** If the msg is already queued, queue this one and leave the old. */ |
| 193 | private static final int SENDMSG_QUEUE = 2; |
| 194 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 195 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 196 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | private static final int MSG_PERSIST_VOLUME = 1; |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 198 | private static final int MSG_PERSIST_MASTER_VOLUME = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 200 | private static final int MSG_MEDIA_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 201 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 202 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 203 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 204 | private static final int MSG_SET_FORCE_USE = 8; |
| 205 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 206 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 207 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 208 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 209 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 210 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 211 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 212 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 213 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 214 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 215 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 216 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 217 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 218 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 219 | private static final int MSG_PERSIST_MICROPHONE_MUTE = 23; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 220 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 221 | // start of messages handled under wakelock |
| 222 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 223 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 224 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 225 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 226 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 227 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 228 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 229 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 230 | // Timeout for connection to bluetooth headset service |
| 231 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | /** @see AudioSystemThread */ |
| 234 | private AudioSystemThread mAudioSystemThread; |
| 235 | /** @see AudioHandler */ |
| 236 | private AudioHandler mAudioHandler; |
| 237 | /** @see VolumeStreamState */ |
| 238 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 239 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 240 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 241 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 242 | // protects mRingerMode |
| 243 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 246 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 249 | // Internally master volume is a float in the 0.0 - 1.0 range, |
| 250 | // but to support integer based AudioManager API we translate it to 0 - 100 |
| 251 | private static final int MAX_MASTER_VOLUME = 100; |
| 252 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 253 | // Maximum volume adjust steps allowed in a single batch call. |
| 254 | private static final int MAX_BATCH_VOLUME_ADJUST_STEPS = 4; |
| 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /* Sound effect file names */ |
| 257 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 258 | 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] | 259 | |
| 260 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 261 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 262 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 263 | 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] | 264 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 265 | /** @hide Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 266 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 267 | 5, // STREAM_VOICE_CALL |
| 268 | 7, // STREAM_SYSTEM |
| 269 | 7, // STREAM_RING |
| 270 | 15, // STREAM_MUSIC |
| 271 | 7, // STREAM_ALARM |
| 272 | 7, // STREAM_NOTIFICATION |
| 273 | 15, // STREAM_BLUETOOTH_SCO |
| 274 | 7, // STREAM_SYSTEM_ENFORCED |
| 275 | 15, // STREAM_DTMF |
| 276 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 277 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 278 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 279 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 280 | * of another stream: This avoids multiplying the volume settings for hidden |
| 281 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 282 | * NOTE: do not create loops in aliases! |
| 283 | * 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] | 284 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 285 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 286 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 287 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 288 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 289 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 290 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 291 | AudioSystem.STREAM_RING, // STREAM_RING |
| 292 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 293 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 294 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 295 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 296 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 297 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 298 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 299 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 300 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 301 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 302 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 303 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 304 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 305 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 306 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 307 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 308 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 309 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 310 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 311 | }; |
| 312 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 313 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 314 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 315 | AudioSystem.STREAM_RING, // STREAM_RING |
| 316 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 317 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 318 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 319 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 320 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 321 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 322 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 323 | }; |
| 324 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 328 | * after mapping through mStreamVolumeAlias. |
| 329 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame^] | 330 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 331 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 332 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 333 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 334 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 335 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 336 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 337 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 338 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 339 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 340 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 341 | }; |
| 342 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 343 | private final boolean mUseFixedVolume; |
| 344 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 345 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | public void onError(int error) { |
| 347 | switch (error) { |
| 348 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 349 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, |
| 350 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | break; |
| 352 | default: |
| 353 | break; |
| 354 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | /** |
| 359 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 360 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 361 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 362 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 363 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 364 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 365 | 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] | 366 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 367 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 368 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 369 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 370 | // Streams currently muted by ringer mode |
| 371 | private int mRingerModeMutedStreams; |
| 372 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 373 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 374 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | private int mMuteAffectedStreams; |
| 376 | |
| 377 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 378 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 379 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 380 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | */ |
| 382 | private int mVibrateSetting; |
| 383 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 384 | // Is there a vibrator |
| 385 | private final boolean mHasVibrator; |
| 386 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 387 | // Broadcast receiver for device connections intent broadcasts |
| 388 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 389 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 390 | // Devices currently connected |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 391 | private final HashMap <Integer, String> mConnectedDevices = new HashMap <Integer, String>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 392 | |
| 393 | // Forced device usage for communications |
| 394 | private int mForcedUseForComm; |
| 395 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 396 | // True if we have master volume support |
| 397 | private final boolean mUseMasterVolume; |
| 398 | |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 399 | private final int[] mMasterVolumeRamp; |
| 400 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 401 | // List of binder death handlers for setMode() client processes. |
| 402 | // 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] | 403 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 404 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 405 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 406 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 407 | |
| 408 | // BluetoothHeadset API to control SCO connection |
| 409 | private BluetoothHeadset mBluetoothHeadset; |
| 410 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 411 | // Bluetooth headset device |
| 412 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 413 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 414 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 415 | // audio service (internal) or bluetooth headset (external) |
| 416 | private int mScoAudioState; |
| 417 | // SCO audio state is not active |
| 418 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 419 | // SCO audio activation request waiting for headset service to connect |
| 420 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 421 | // 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] | 422 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 423 | // SCO audio deactivation request waiting for headset service to connect |
| 424 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 425 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 426 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 427 | // in call audio) |
| 428 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 429 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 430 | // waiting for headset service to connect |
| 431 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 432 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 433 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 434 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 435 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 436 | // SCO audio mode is undefined |
| 437 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 438 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 439 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 440 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 441 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 442 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 443 | private static final int SCO_MODE_VR = 2; |
| 444 | |
| 445 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 446 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 447 | // Current connection state indicated by bluetooth headset |
| 448 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 449 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 450 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 451 | private boolean mSystemReady; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 452 | // listener for SoundPool sample load completion indication |
| 453 | private SoundPoolCallback mSoundPoolCallBack; |
| 454 | // thread for SoundPool listener |
| 455 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 456 | // message looper for SoundPool listener |
| 457 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 458 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 459 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 460 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 461 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 462 | // Keyguard manager proxy |
| 463 | private KeyguardManager mKeyguardManager; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 464 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 465 | // is controlled by Vol keys. |
| 466 | private int mVolumeControlStream = -1; |
| 467 | private final Object mForceControlStreamLock = new Object(); |
| 468 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 469 | // server process so in theory it is not necessary to monitor the client death. |
| 470 | // However it is good to be ready for future evolutions. |
| 471 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 472 | // Used to play ringtones outside system_server |
| 473 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 474 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 475 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 476 | private int mDeviceRotation = Surface.ROTATION_0; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 477 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 478 | // Request to override default use of A2DP for media. |
| 479 | private boolean mBluetoothA2dpEnabled; |
| 480 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 481 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 482 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 483 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 484 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 485 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 486 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 487 | // 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] | 488 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 489 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 490 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 491 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 492 | AudioSystem.DEVICE_OUT_SPDIF | |
| 493 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 494 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 495 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 496 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 497 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 498 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 499 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 500 | private boolean mDockAudioMediaEnabled = true; |
| 501 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 502 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 503 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 504 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 505 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 506 | // and used later when/if disableSafeMediaVolume() is called. |
| 507 | private StreamVolumeCommand mPendingVolumeCommand; |
| 508 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 509 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 510 | |
| 511 | private final MediaFocusControl mMediaFocusControl; |
| 512 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 513 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 514 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 515 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 516 | private final Object mA2dpAvrcpLock = new Object(); |
| 517 | // If absolute volume is supported in AVRCP device |
| 518 | private boolean mAvrcpAbsVolSupported = false; |
| 519 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 520 | private AudioOrientationEventListener mOrientationListener; |
| 521 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 522 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 523 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 524 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
| 525 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 526 | // Intent "extra" data keys. |
| 527 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 528 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 529 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 530 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 531 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 532 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 533 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 534 | |
| 535 | // Defines the format for the connection "address" for ALSA devices |
| 536 | public static String makeAlsaAddressString(int card, int device) { |
| 537 | return "card=" + card + ";device=" + device + ";"; |
| 538 | } |
| 539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | /////////////////////////////////////////////////////////////////////////// |
| 541 | // Construction |
| 542 | /////////////////////////////////////////////////////////////////////////// |
| 543 | |
| 544 | /** @hide */ |
| 545 | public AudioService(Context context) { |
| 546 | mContext = context; |
| 547 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 548 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 549 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 550 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 551 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 552 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 553 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 554 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 555 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 556 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 557 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 558 | // Intialized volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 559 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 560 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 561 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 562 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 563 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 564 | } |
| 565 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 566 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 567 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 568 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 569 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 570 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 571 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 572 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 573 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 574 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 575 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 578 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 579 | mMediaFocusControl = new MediaFocusControl(mAudioHandler.getLooper(), |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 580 | mContext, mVolumeController, this); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 581 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 582 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 583 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 584 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 585 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 586 | sendMsg(mAudioHandler, |
| 587 | MSG_SET_FORCE_USE, |
| 588 | SENDMSG_QUEUE, |
| 589 | AudioSystem.FOR_SYSTEM, |
| 590 | cameraSoundForced ? |
| 591 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 592 | null, |
| 593 | 0); |
| 594 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 595 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 596 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 597 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 598 | // The default safe volume index read here will be replaced by the actual value when |
| 599 | // the mcc is read by onConfigureSafeVolume() |
| 600 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 601 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 602 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 603 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 604 | com.android.internal.R.bool.config_useFixedVolume); |
Wally Yau | da39290 | 2014-11-28 12:40:30 -0800 | [diff] [blame] | 605 | mUseMasterVolume = context.getResources().getBoolean( |
| 606 | com.android.internal.R.bool.config_useMasterVolume); |
| 607 | mMasterVolumeRamp = context.getResources().getIntArray( |
| 608 | com.android.internal.R.array.config_masterVolumeRamp); |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 609 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 610 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 611 | // array initialized by updateStreamVolumeAlias() |
| 612 | updateStreamVolumeAlias(false /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 614 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 615 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 616 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 617 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 618 | |
| 619 | // Call setRingerModeInt() to apply correct mute |
| 620 | // state on streams affected by ringer mode. |
| 621 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 622 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 623 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 624 | // Register for device connection intent broadcasts. |
| 625 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 626 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 627 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 628 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 629 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 630 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 631 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 632 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 633 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 634 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 635 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 636 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 637 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 638 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 639 | // initialize orientation in AudioSystem |
| 640 | setOrientationForAudioSystem(); |
| 641 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 642 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 643 | if (mMonitorRotation) { |
| 644 | mDeviceRotation = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 645 | .getDefaultDisplay().getRotation(); |
| 646 | Log.v(TAG, "monitoring device rotation, initial=" + mDeviceRotation); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 647 | |
| 648 | mOrientationListener = new AudioOrientationEventListener(mContext); |
| 649 | mOrientationListener.enable(); |
| 650 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 651 | // initialize rotation in AudioSystem |
| 652 | setRotationForAudioSystem(); |
| 653 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 654 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 655 | context.registerReceiver(mReceiver, intentFilter); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 656 | |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 657 | restoreMasterVolume(); |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 658 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 659 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 662 | public void systemReady() { |
| 663 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 664 | 0, 0, null, 0); |
| 665 | } |
| 666 | |
| 667 | public void onSystemReady() { |
| 668 | mSystemReady = true; |
| 669 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 670 | 0, 0, null, 0); |
| 671 | |
| 672 | mKeyguardManager = |
| 673 | (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 674 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 675 | resetBluetoothSco(); |
| 676 | getBluetoothHeadset(); |
| 677 | //FIXME: this is to maintain compatibility with deprecated intent |
| 678 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 679 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 680 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 681 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 682 | sendStickyBroadcastToAll(newIntent); |
| 683 | |
| 684 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 685 | if (adapter != null) { |
| 686 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 687 | BluetoothProfile.A2DP); |
| 688 | } |
| 689 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 690 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 691 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 692 | if (mHdmiManager != null) { |
| 693 | synchronized (mHdmiManager) { |
| 694 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 695 | if (mHdmiTvClient != null) { |
| 696 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 697 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 698 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 699 | mHdmiCecSink = false; |
| 700 | } |
| 701 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 702 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 703 | sendMsg(mAudioHandler, |
| 704 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 705 | SENDMSG_REPLACE, |
| 706 | 0, |
| 707 | 0, |
| 708 | null, |
| 709 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 710 | |
| 711 | StreamOverride.init(mContext); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 712 | mControllerService.init(); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 713 | } |
| 714 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | private void createAudioSystemThread() { |
| 716 | mAudioSystemThread = new AudioSystemThread(); |
| 717 | mAudioSystemThread.start(); |
| 718 | waitForAudioHandlerCreation(); |
| 719 | } |
| 720 | |
| 721 | /** Waits for the volume handler to be created by the other thread. */ |
| 722 | private void waitForAudioHandlerCreation() { |
| 723 | synchronized(this) { |
| 724 | while (mAudioHandler == null) { |
| 725 | try { |
| 726 | // Wait for mAudioHandler to be set by the other thread |
| 727 | wait(); |
| 728 | } catch (InterruptedException e) { |
| 729 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 735 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 736 | synchronized (VolumeStreamState.class) { |
| 737 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 738 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 739 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 740 | mStreamStates[streamType]. |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 741 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]]); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 742 | } |
| 743 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 744 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 745 | mStreamStates[streamType].applyAllVolumes(); |
| 746 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 751 | private void checkAllFixedVolumeDevices() |
| 752 | { |
| 753 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 754 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 755 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 756 | } |
| 757 | } |
| 758 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 759 | private void checkAllFixedVolumeDevices(int streamType) { |
| 760 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 761 | } |
| 762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 765 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 766 | |
| 767 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 768 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 771 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 772 | checkAllAliasStreamVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 775 | private void dumpStreamStates(PrintWriter pw) { |
| 776 | pw.println("\nStream volumes (device: index)"); |
| 777 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 778 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 779 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 780 | mStreamStates[i].dump(pw); |
| 781 | pw.println(""); |
| 782 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 783 | pw.print("\n- mute affected streams = 0x"); |
| 784 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 787 | private void updateStreamVolumeAlias(boolean updateVolumes) { |
| 788 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 789 | |
| 790 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 791 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 792 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 793 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 794 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 795 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 796 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 797 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 798 | break; |
| 799 | default: |
| 800 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 801 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 802 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 803 | |
| 804 | if (isPlatformTelevision()) { |
| 805 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 806 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 807 | if (isInCommunication()) { |
| 808 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 809 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 810 | } else { |
| 811 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 812 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 813 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 814 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 815 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 816 | if (updateVolumes) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 817 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias]); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 818 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 819 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 820 | sendMsg(mAudioHandler, |
| 821 | MSG_SET_ALL_VOLUMES, |
| 822 | SENDMSG_QUEUE, |
| 823 | 0, |
| 824 | 0, |
| 825 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 826 | } |
| 827 | } |
| 828 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 829 | private void readDockAudioSettings(ContentResolver cr) |
| 830 | { |
| 831 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 832 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 833 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 834 | sendMsg(mAudioHandler, |
| 835 | MSG_SET_FORCE_USE, |
| 836 | SENDMSG_QUEUE, |
| 837 | AudioSystem.FOR_DOCK, |
| 838 | mDockAudioMediaEnabled ? |
| 839 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 840 | null, |
| 841 | 0); |
| 842 | } |
| 843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | private void readPersistedSettings() { |
| 845 | final ContentResolver cr = mContentResolver; |
| 846 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 847 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 848 | Settings.Global.getInt( |
| 849 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 850 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 851 | // sanity check in case the settings are restored from a device with incompatible |
| 852 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 853 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 854 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 855 | } |
| 856 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 857 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 858 | } |
| 859 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 860 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 861 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 862 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 863 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 864 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 865 | synchronized(mSettingsLock) { |
| 866 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 867 | if (mRingerModeExternal == -1) { |
| 868 | mRingerModeExternal = mRingerMode; |
| 869 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 871 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 872 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 873 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 874 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 875 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 876 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 877 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 878 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 879 | AudioManager.VIBRATE_TYPE_RINGER, |
| 880 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 881 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 883 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 884 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 885 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 886 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 887 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 888 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 889 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 891 | boolean masterMute = System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
| 892 | 0, UserHandle.USER_CURRENT) == 1; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 893 | if (mUseFixedVolume) { |
| 894 | masterMute = false; |
| 895 | AudioSystem.setMasterVolume(1.0f); |
| 896 | } |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 897 | AudioSystem.setMasterMute(masterMute); |
| 898 | broadcastMasterMuteStatus(masterMute); |
| 899 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 900 | boolean microphoneMute = |
| 901 | System.getIntForUser(cr, System.MICROPHONE_MUTE, 0, UserHandle.USER_CURRENT) == 1; |
| 902 | AudioSystem.muteMicrophone(microphoneMute); |
| 903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | // Each stream will read its own persisted settings |
| 905 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 906 | // Broadcast the sticky intents |
| 907 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 908 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | |
| 910 | // Broadcast vibrate settings |
| 911 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 912 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 913 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 914 | // Load settings for the volume controller |
| 915 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | } |
| 917 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 918 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 919 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 922 | private class AudioOrientationEventListener |
| 923 | extends OrientationEventListener { |
| 924 | public AudioOrientationEventListener(Context context) { |
| 925 | super(context); |
| 926 | } |
| 927 | |
| 928 | @Override |
| 929 | public void onOrientationChanged(int orientation) { |
| 930 | //Even though we're responding to phone orientation events, |
| 931 | //use display rotation so audio stays in sync with video/dialogs |
| 932 | int newRotation = ((WindowManager) mContext.getSystemService( |
| 933 | Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); |
| 934 | if (newRotation != mDeviceRotation) { |
| 935 | mDeviceRotation = newRotation; |
| 936 | setRotationForAudioSystem(); |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | /////////////////////////////////////////////////////////////////////////// |
| 942 | // IPC methods |
| 943 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 945 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 946 | String callingPackage) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 947 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 948 | Binder.getCallingUid()); |
| 949 | } |
| 950 | |
| 951 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 952 | String callingPackage, int uid) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 953 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream="+suggestedStreamType |
| 954 | + ", flags=" + flags); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 955 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 956 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 957 | if (mVolumeControlStream != -1) { |
| 958 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 959 | } else { |
| 960 | streamType = getActiveStreamType(suggestedStreamType); |
| 961 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 962 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 964 | // Play sounds on STREAM_RING only. |
| 965 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 966 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 968 | } |
| 969 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 970 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 971 | // Don't suppress mute/unmute requests |
| 972 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 973 | direction = 0; |
| 974 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 975 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 976 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 977 | } |
| 978 | |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 979 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 983 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 984 | String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 985 | adjustStreamVolume(streamType, direction, flags, callingPackage, Binder.getCallingUid()); |
| 986 | } |
| 987 | |
| 988 | private void adjustStreamVolume(int streamType, int direction, int flags, |
| 989 | String callingPackage, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 990 | if (mUseFixedVolume) { |
| 991 | return; |
| 992 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 993 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream="+streamType+", dir="+direction |
| 994 | + ", flags="+flags); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 995 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | ensureValidDirection(direction); |
| 997 | ensureValidStreamType(streamType); |
| 998 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 999 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1000 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1001 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1002 | return; |
| 1003 | } |
| 1004 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1005 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1006 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1007 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1008 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1009 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1010 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1011 | |
| 1012 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1013 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1014 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1016 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1017 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1018 | // skip a2dp absolute volume control request when the device |
| 1019 | // is not an a2dp device |
| 1020 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1021 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1022 | return; |
| 1023 | } |
| 1024 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame^] | 1025 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1026 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1027 | return; |
| 1028 | } |
| 1029 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1030 | // reset any pending volume command |
| 1031 | synchronized (mSafeMediaVolumeState) { |
| 1032 | mPendingVolumeCommand = null; |
| 1033 | } |
| 1034 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1035 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1036 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1037 | ((device & mFixedVolumeDevices) != 0)) { |
| 1038 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1039 | |
| 1040 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1041 | // volume is enforced, and max and 0 for the others. |
| 1042 | // This is simulated by stepping by the full allowed volume range |
| 1043 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1044 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1045 | step = mSafeMediaVolumeIndex; |
| 1046 | } else { |
| 1047 | step = streamState.getMaxIndex(); |
| 1048 | } |
| 1049 | if (aliasIndex != 0) { |
| 1050 | aliasIndex = step; |
| 1051 | } |
| 1052 | } else { |
| 1053 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1054 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1055 | } |
| 1056 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1057 | // If either the client forces allowing ringer modes for this adjustment, |
| 1058 | // or the stream type is one that is affected by ringer modes |
| 1059 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1060 | (streamTypeAlias == getMasterStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1061 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1062 | // do not vibrate if already in vibrate mode |
| 1063 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1064 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1065 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1066 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1067 | // need to adjust the volume further. |
| 1068 | final int result = checkForRingerModeChange(aliasIndex, direction, step, streamState.mIsMuted); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1069 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1070 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1071 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1072 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1073 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1074 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1075 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1076 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1077 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1078 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1079 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1080 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1081 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1082 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1083 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1084 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1085 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1086 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1087 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1088 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1089 | synchronized (mA2dpAvrcpLock) { |
| 1090 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1091 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1092 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1095 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1096 | if (isMuteAdjust) { |
| 1097 | boolean state; |
| 1098 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1099 | state = !streamState.mIsMuted; |
| 1100 | } else { |
| 1101 | state = direction == AudioManager.ADJUST_MUTE; |
| 1102 | } |
| 1103 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1104 | setSystemAudioMute(state); |
| 1105 | } |
| 1106 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1107 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
| 1108 | mStreamStates[stream].mute(state); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1112 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1113 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1114 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1115 | } else if (streamState.adjustIndex(direction * step, device) || streamState.mIsMuted) { |
| 1116 | // Post message to set system volume (it in turn will post a |
| 1117 | // message to persist). |
| 1118 | if (streamState.mIsMuted) { |
| 1119 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1120 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1121 | // unmute immediately for volume up |
| 1122 | streamState.mute(false); |
| 1123 | } else if (direction == AudioManager.ADJUST_LOWER) { |
| 1124 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1125 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1126 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1127 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1128 | sendMsg(mAudioHandler, |
| 1129 | MSG_SET_DEVICE_VOLUME, |
| 1130 | SENDMSG_QUEUE, |
| 1131 | device, |
| 1132 | 0, |
| 1133 | streamState, |
| 1134 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1135 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1136 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1137 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1138 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1139 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1140 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1141 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1142 | if (mHdmiManager != null) { |
| 1143 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1144 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1145 | if (mHdmiCecSink && |
| 1146 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1147 | oldIndex != newIndex) { |
| 1148 | synchronized (mHdmiPlaybackClient) { |
| 1149 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1150 | KeyEvent.KEYCODE_VOLUME_UP; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1151 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1152 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1153 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1157 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1158 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1159 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1162 | // Called after a delay when volume down is pressed while muted |
| 1163 | private void onUnmuteStream(int stream, int flags) { |
| 1164 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1165 | streamState.mute(false); |
| 1166 | |
| 1167 | final int device = getDeviceForStream(stream); |
| 1168 | final int index = mStreamStates[stream].getIndex(device); |
| 1169 | sendVolumeUpdate(stream, index, index, flags); |
| 1170 | } |
| 1171 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1172 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1173 | if (mHdmiManager == null |
| 1174 | || mHdmiTvClient == null |
| 1175 | || oldVolume == newVolume |
| 1176 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1177 | |
| 1178 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1179 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1180 | synchronized (mHdmiManager) { |
| 1181 | if (!mHdmiSystemAudioSupported) return; |
| 1182 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1183 | final long token = Binder.clearCallingIdentity(); |
| 1184 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1185 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1186 | } finally { |
| 1187 | Binder.restoreCallingIdentity(token); |
| 1188 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 1193 | /** @see AudioManager#adjustMasterVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1194 | public void adjustMasterVolume(int steps, int flags, String callingPackage) { |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1195 | adjustMasterVolume(steps, flags, callingPackage, Binder.getCallingUid()); |
| 1196 | } |
| 1197 | |
| 1198 | public void adjustMasterVolume(int steps, int flags, String callingPackage, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1199 | if (mUseFixedVolume) { |
| 1200 | return; |
| 1201 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1202 | if (isMuteAdjust(steps)) { |
| 1203 | setMasterMuteInternal(steps, flags, callingPackage, uid); |
| 1204 | return; |
| 1205 | } |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1206 | ensureValidSteps(steps); |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 1207 | int volume = Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME); |
| 1208 | int delta = 0; |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1209 | int numSteps = Math.abs(steps); |
| 1210 | int direction = steps > 0 ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER; |
| 1211 | for (int i = 0; i < numSteps; ++i) { |
| 1212 | delta = findVolumeDelta(direction, volume); |
| 1213 | volume += delta; |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1214 | } |
RoboErik | 24b082f | 2012-02-24 14:21:16 -0800 | [diff] [blame] | 1215 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1216 | //Log.d(TAG, "adjustMasterVolume volume: " + volume + " steps: " + steps); |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1217 | setMasterVolume(volume, flags, callingPackage, uid); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1218 | } |
| 1219 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1220 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1221 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1222 | class StreamVolumeCommand { |
| 1223 | public final int mStreamType; |
| 1224 | public final int mIndex; |
| 1225 | public final int mFlags; |
| 1226 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1227 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1228 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1229 | mStreamType = streamType; |
| 1230 | mIndex = index; |
| 1231 | mFlags = flags; |
| 1232 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1233 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1234 | |
| 1235 | @Override |
| 1236 | public String toString() { |
| 1237 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1238 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1239 | .append(mDevice).append('}').toString(); |
| 1240 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1241 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1242 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1243 | private void onSetStreamVolume(int streamType, int index, int flags, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1244 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1245 | // setting volume on master stream type also controls silent mode |
| 1246 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1247 | (mStreamVolumeAlias[streamType] == getMasterStreamType())) { |
| 1248 | int newRingerMode; |
| 1249 | if (index == 0) { |
| 1250 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1251 | : VOLUME_SETS_RINGER_MODE_SILENT ? AudioManager.RINGER_MODE_SILENT |
| 1252 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1253 | } else { |
| 1254 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1255 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1256 | setRingerMode(newRingerMode, TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1257 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1261 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1262 | setStreamVolume(streamType, index, flags, callingPackage, Binder.getCallingUid()); |
| 1263 | } |
| 1264 | |
| 1265 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
| 1266 | int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1267 | if (mUseFixedVolume) { |
| 1268 | return; |
| 1269 | } |
| 1270 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1271 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1272 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1273 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1274 | |
| 1275 | final int device = getDeviceForStream(streamType); |
| 1276 | int oldIndex; |
| 1277 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1278 | // skip a2dp absolute volume control request when the device |
| 1279 | // is not an a2dp device |
| 1280 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1281 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1282 | return; |
| 1283 | } |
| 1284 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame^] | 1285 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1286 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1287 | return; |
| 1288 | } |
| 1289 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1290 | synchronized (mSafeMediaVolumeState) { |
| 1291 | // reset any pending volume command |
| 1292 | mPendingVolumeCommand = null; |
| 1293 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1294 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1295 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1296 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1297 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1298 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1299 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1300 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1301 | synchronized (mA2dpAvrcpLock) { |
| 1302 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1303 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1304 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1308 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1309 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1310 | } |
| 1311 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1312 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1313 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1314 | ((device & mFixedVolumeDevices) != 0)) { |
| 1315 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1316 | |
| 1317 | // volume is either 0 or max allowed for fixed volume devices |
| 1318 | if (index != 0) { |
| 1319 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1320 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1321 | index = mSafeMediaVolumeIndex; |
| 1322 | } else { |
| 1323 | index = streamState.getMaxIndex(); |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1328 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1329 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1330 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1331 | streamType, index, flags, device); |
| 1332 | } else { |
| 1333 | onSetStreamVolume(streamType, index, flags, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1334 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1335 | } |
| 1336 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1337 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1340 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1341 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1342 | synchronized(mForceControlStreamLock) { |
| 1343 | mVolumeControlStream = streamType; |
| 1344 | if (mVolumeControlStream == -1) { |
| 1345 | if (mForceControlStreamClient != null) { |
| 1346 | mForceControlStreamClient.release(); |
| 1347 | mForceControlStreamClient = null; |
| 1348 | } |
| 1349 | } else { |
| 1350 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1356 | private IBinder mCb; // To be notified of client's death |
| 1357 | |
| 1358 | ForceControlStreamClient(IBinder cb) { |
| 1359 | if (cb != null) { |
| 1360 | try { |
| 1361 | cb.linkToDeath(this, 0); |
| 1362 | } catch (RemoteException e) { |
| 1363 | // Client has died! |
| 1364 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1365 | cb = null; |
| 1366 | } |
| 1367 | } |
| 1368 | mCb = cb; |
| 1369 | } |
| 1370 | |
| 1371 | public void binderDied() { |
| 1372 | synchronized(mForceControlStreamLock) { |
| 1373 | Log.w(TAG, "SCO client died"); |
| 1374 | if (mForceControlStreamClient != this) { |
| 1375 | Log.w(TAG, "unregistered control stream client died"); |
| 1376 | } else { |
| 1377 | mForceControlStreamClient = null; |
| 1378 | mVolumeControlStream = -1; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | public void release() { |
| 1384 | if (mCb != null) { |
| 1385 | mCb.unlinkToDeath(this, 0); |
| 1386 | mCb = null; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1391 | private int findVolumeDelta(int direction, int volume) { |
| 1392 | int delta = 0; |
| 1393 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1394 | if (volume == MAX_MASTER_VOLUME) { |
| 1395 | return 0; |
| 1396 | } |
| 1397 | // This is the default value if we make it to the end |
| 1398 | delta = mMasterVolumeRamp[1]; |
| 1399 | // If we're raising the volume move down the ramp array until we |
| 1400 | // find the volume we're above and use that groups delta. |
| 1401 | for (int i = mMasterVolumeRamp.length - 1; i > 1; i -= 2) { |
| 1402 | if (volume >= mMasterVolumeRamp[i - 1]) { |
| 1403 | delta = mMasterVolumeRamp[i]; |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
| 1407 | } else if (direction == AudioManager.ADJUST_LOWER){ |
| 1408 | if (volume == 0) { |
| 1409 | return 0; |
| 1410 | } |
| 1411 | int length = mMasterVolumeRamp.length; |
| 1412 | // This is the default value if we make it to the end |
| 1413 | delta = -mMasterVolumeRamp[length - 1]; |
| 1414 | // If we're lowering the volume move up the ramp array until we |
| 1415 | // find the volume we're below and use the group below it's delta |
| 1416 | for (int i = 2; i < length; i += 2) { |
| 1417 | if (volume <= mMasterVolumeRamp[i]) { |
| 1418 | delta = -mMasterVolumeRamp[i - 1]; |
| 1419 | break; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | return delta; |
| 1424 | } |
| 1425 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1426 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1427 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1428 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1429 | final long ident = Binder.clearCallingIdentity(); |
| 1430 | try { |
| 1431 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1432 | } finally { |
| 1433 | Binder.restoreCallingIdentity(ident); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1438 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1439 | final long ident = Binder.clearCallingIdentity(); |
| 1440 | try { |
| 1441 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1442 | } finally { |
| 1443 | Binder.restoreCallingIdentity(ident); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1447 | // UI update and Broadcast Intent |
| 1448 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1449 | if (!isPlatformVoice() && (streamType == AudioSystem.STREAM_RING)) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1450 | streamType = AudioSystem.STREAM_NOTIFICATION; |
| 1451 | } |
| 1452 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1453 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1454 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1455 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1456 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1459 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1460 | // receives volume notification from Audio Receiver. |
| 1461 | private int updateFlagsForSystemAudio(int flags) { |
| 1462 | if (mHdmiTvClient != null) { |
| 1463 | synchronized (mHdmiTvClient) { |
| 1464 | if (mHdmiSystemAudioSupported && |
| 1465 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1466 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | return flags; |
| 1471 | } |
| 1472 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1473 | // UI update and Broadcast Intent |
| 1474 | private void sendMasterVolumeUpdate(int flags, int oldVolume, int newVolume) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1475 | mVolumeController.postMasterVolumeChanged(updateFlagsForSystemAudio(flags)); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1476 | |
| 1477 | Intent intent = new Intent(AudioManager.MASTER_VOLUME_CHANGED_ACTION); |
| 1478 | intent.putExtra(AudioManager.EXTRA_PREV_MASTER_VOLUME_VALUE, oldVolume); |
| 1479 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_VALUE, newVolume); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1480 | sendBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | // UI update and Broadcast Intent |
| 1484 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1485 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1486 | broadcastMasterMuteStatus(muted); |
| 1487 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1488 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1489 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1490 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1491 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1492 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1493 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1494 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1495 | } |
| 1496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | * Sets the stream state's index, and posts a message to set system volume. |
| 1499 | * This will not call out to the UI. Assumes a valid stream type. |
| 1500 | * |
| 1501 | * @param streamType Type of the stream |
| 1502 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1503 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | * @param force If true, set the volume even if the desired volume is same |
| 1505 | * as the current volume. |
| 1506 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1507 | private void setStreamVolumeInt(int streamType, |
| 1508 | int index, |
| 1509 | int device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1510 | boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1512 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1513 | if (streamState.setIndex(index, device) || force) { |
| 1514 | // Post message to set system volume (it in turn will post a message |
| 1515 | // to persist). |
| 1516 | sendMsg(mAudioHandler, |
| 1517 | MSG_SET_DEVICE_VOLUME, |
| 1518 | SENDMSG_QUEUE, |
| 1519 | device, |
| 1520 | 0, |
| 1521 | streamState, |
| 1522 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | } |
| 1524 | } |
| 1525 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1526 | private void setSystemAudioMute(boolean state) { |
| 1527 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1528 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1529 | if (!mHdmiSystemAudioSupported) return; |
| 1530 | synchronized (mHdmiTvClient) { |
| 1531 | final long token = Binder.clearCallingIdentity(); |
| 1532 | try { |
| 1533 | mHdmiTvClient.setSystemAudioMute(state); |
| 1534 | } finally { |
| 1535 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1536 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1541 | /** get stream mute state. */ |
| 1542 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1543 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1544 | streamType = getActiveStreamType(streamType); |
| 1545 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1546 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1547 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1548 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1551 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1552 | private IBinder mICallback; // To be notified of client's death |
| 1553 | |
| 1554 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1555 | mICallback = cb; |
| 1556 | try { |
| 1557 | cb.linkToDeath(this, 0/*flags*/); |
| 1558 | } catch (RemoteException e) { |
| 1559 | Log.e(TAG, "can't link to death", e); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | boolean isHandlerFor(IBinder cb) { |
| 1564 | return mICallback.equals(cb); |
| 1565 | } |
| 1566 | |
| 1567 | void forget() { |
| 1568 | try { |
| 1569 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1570 | } catch (NoSuchElementException e) { |
| 1571 | Log.e(TAG, "error unlinking to death", e); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | public void binderDied() { |
| 1576 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1577 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1583 | * @return true if there is a registered death handler, false otherwise */ |
| 1584 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1585 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1586 | while (it.hasNext()) { |
| 1587 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1588 | if (handler.isHandlerFor(cb)) { |
| 1589 | handler.forget(); |
| 1590 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1591 | return true; |
| 1592 | } |
| 1593 | } |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
| 1597 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1598 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1599 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1600 | while (it.hasNext()) { |
| 1601 | if (it.next().isHandlerFor(cb)) { |
| 1602 | return true; |
| 1603 | } |
| 1604 | } |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
| 1608 | private int mRmtSbmxFullVolRefCount = 0; |
| 1609 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1610 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1611 | |
| 1612 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1613 | if (cb == null) { |
| 1614 | return; |
| 1615 | } |
| 1616 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1617 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1618 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1619 | return; |
| 1620 | } |
| 1621 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1622 | boolean applyRequired = false; |
| 1623 | if (startForcing) { |
| 1624 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1625 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1626 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1627 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1628 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1629 | applyRequired = true; |
| 1630 | } |
| 1631 | mRmtSbmxFullVolRefCount++; |
| 1632 | } |
| 1633 | } else { |
| 1634 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1635 | mRmtSbmxFullVolRefCount--; |
| 1636 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1637 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1638 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1639 | applyRequired = true; |
| 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | if (applyRequired) { |
| 1644 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1645 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1646 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1651 | private void setMasterMuteInternal(int adjust, int flags, String callingPackage, int uid) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1652 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
| 1653 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1654 | return; |
| 1655 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1656 | boolean state; |
| 1657 | if (adjust == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1658 | state = !AudioSystem.getMasterMute(); |
| 1659 | } else { |
| 1660 | state = adjust == AudioManager.ADJUST_MUTE; |
| 1661 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1662 | if (state != AudioSystem.getMasterMute()) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1663 | setSystemAudioMute(state); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1664 | AudioSystem.setMasterMute(state); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1665 | // Post a persist master volume msg |
Justin Koh | 75cf9e1 | 2012-04-04 15:27:37 -0700 | [diff] [blame] | 1666 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, state ? 1 |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1667 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
Justin Koh | 0273af5 | 2012-04-04 18:29:50 -0700 | [diff] [blame] | 1668 | sendMasterMuteUpdate(state, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1669 | |
| 1670 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1671 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, state); |
| 1672 | sendBroadcastToAll(intent); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1673 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /** get master mute state. */ |
| 1677 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1678 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 1681 | protected static int getMaxStreamVolume(int streamType) { |
| 1682 | return MAX_STREAM_VOLUME[streamType]; |
| 1683 | } |
| 1684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1685 | /** @see AudioManager#getStreamVolume(int) */ |
| 1686 | public int getStreamVolume(int streamType) { |
| 1687 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1688 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1689 | synchronized (VolumeStreamState.class) { |
| 1690 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1691 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1692 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1693 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1694 | index = 0; |
| 1695 | } |
| 1696 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 1697 | (device & mFixedVolumeDevices) != 0) { |
| 1698 | index = mStreamStates[streamType].getMaxIndex(); |
| 1699 | } |
| 1700 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1701 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1704 | @Override |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 1705 | public int getMasterVolume() { |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1706 | if (isMasterMute()) return 0; |
| 1707 | return getLastAudibleMasterVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1710 | @Override |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1711 | public void setMasterVolume(int volume, int flags, String callingPackage) { |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1712 | setMasterVolume(volume, flags, callingPackage, Binder.getCallingUid()); |
| 1713 | } |
| 1714 | |
| 1715 | public void setMasterVolume(int volume, int flags, String callingPackage, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1716 | if (mUseFixedVolume) { |
| 1717 | return; |
| 1718 | } |
| 1719 | |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 1720 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
| 1721 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1722 | return; |
| 1723 | } |
| 1724 | |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 1725 | if (volume < 0) { |
| 1726 | volume = 0; |
| 1727 | } else if (volume > MAX_MASTER_VOLUME) { |
| 1728 | volume = MAX_MASTER_VOLUME; |
| 1729 | } |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1730 | doSetMasterVolume((float)volume / MAX_MASTER_VOLUME, flags); |
| 1731 | } |
| 1732 | |
| 1733 | private void doSetMasterVolume(float volume, int flags) { |
| 1734 | // don't allow changing master volume when muted |
| 1735 | if (!AudioSystem.getMasterMute()) { |
| 1736 | int oldVolume = getMasterVolume(); |
| 1737 | AudioSystem.setMasterVolume(volume); |
| 1738 | |
| 1739 | int newVolume = getMasterVolume(); |
| 1740 | if (newVolume != oldVolume) { |
| 1741 | // Post a persist master volume msg |
| 1742 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME, SENDMSG_REPLACE, |
| 1743 | Math.round(volume * (float)1000.0), 0, null, PERSIST_DELAY); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1744 | setSystemAudioVolume(oldVolume, newVolume, getMasterMaxVolume(), flags); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1745 | } |
Justin Koh | 3caba51 | 2012-04-02 15:32:18 -0700 | [diff] [blame] | 1746 | // Send the volume update regardless whether there was a change. |
| 1747 | sendMasterVolumeUpdate(flags, oldVolume, newVolume); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1748 | } |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 1749 | } |
| 1750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1752 | public int getStreamMaxVolume(int streamType) { |
| 1753 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1754 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 1757 | public int getMasterMaxVolume() { |
| 1758 | return MAX_MASTER_VOLUME; |
| 1759 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1760 | |
| 1761 | /** Get last audible volume before stream was muted. */ |
| 1762 | public int getLastAudibleStreamVolume(int streamType) { |
| 1763 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1764 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1765 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1768 | /** Get last audible master volume before it was muted. */ |
| 1769 | public int getLastAudibleMasterVolume() { |
| 1770 | return Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME); |
| 1771 | } |
| 1772 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 1773 | /** @see AudioManager#getMasterStreamType() */ |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1774 | public int getMasterStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1775 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1778 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 1779 | public void setMicrophoneMute(boolean on, String callingPackage) { |
| 1780 | if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, Binder.getCallingUid(), |
| 1781 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1782 | return; |
| 1783 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 1784 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 1785 | return; |
| 1786 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1787 | |
| 1788 | AudioSystem.muteMicrophone(on); |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1789 | // Post a persist microphone msg. |
| 1790 | sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1 |
| 1791 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1792 | } |
| 1793 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1794 | @Override |
| 1795 | public int getRingerModeExternal() { |
| 1796 | synchronized(mSettingsLock) { |
| 1797 | return mRingerModeExternal; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | @Override |
| 1802 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1803 | synchronized(mSettingsLock) { |
| 1804 | return mRingerMode; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1809 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1810 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1811 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1814 | /** @see AudioManager#isValidRingerMode(int) */ |
| 1815 | public boolean isValidRingerMode(int ringerMode) { |
| 1816 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 1817 | } |
| 1818 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1819 | public void setRingerModeExternal(int ringerMode, String caller) { |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1820 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1824 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1825 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1829 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1830 | return; |
| 1831 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1832 | if (caller == null || caller.length() == 0) { |
| 1833 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 1834 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1835 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1836 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1837 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1838 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1839 | final long identity = Binder.clearCallingIdentity(); |
| 1840 | try { |
| 1841 | synchronized (mSettingsLock) { |
| 1842 | final int ringerModeInternal = getRingerModeInternal(); |
| 1843 | final int ringerModeExternal = getRingerModeExternal(); |
| 1844 | if (external) { |
| 1845 | setRingerModeExt(ringerMode); |
| 1846 | if (mRingerModeDelegate != null) { |
| 1847 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
| 1848 | ringerMode, caller, ringerModeInternal); |
| 1849 | } |
| 1850 | if (ringerMode != ringerModeInternal) { |
| 1851 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1852 | } |
| 1853 | } else /*internal*/ { |
| 1854 | if (ringerMode != ringerModeInternal) { |
| 1855 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1856 | } |
| 1857 | if (mRingerModeDelegate != null) { |
| 1858 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
| 1859 | ringerMode, caller, ringerModeExternal); |
| 1860 | } |
| 1861 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1862 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1863 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1864 | } finally { |
| 1865 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1869 | private void setRingerModeExt(int ringerMode) { |
| 1870 | synchronized(mSettingsLock) { |
| 1871 | if (ringerMode == mRingerModeExternal) return; |
| 1872 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1873 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1874 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1875 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1876 | } |
| 1877 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1878 | private void setRingerModeInt(int ringerMode, boolean persist) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1879 | final boolean change; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1880 | synchronized(mSettingsLock) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1881 | change = mRingerMode != ringerMode; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1882 | mRingerMode = ringerMode; |
| 1883 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1884 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1885 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 1886 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 1887 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 1888 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1889 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1890 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 1891 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1892 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1893 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 1894 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 1895 | if (isMuted == shouldMute) continue; |
| 1896 | if (!shouldMute) { |
| 1897 | // unmute |
| 1898 | // ring and notifications volume should never be 0 when not silenced |
| 1899 | // on voice capable devices or devices that support vibration |
| 1900 | if ((isPlatformVoice() || mHasVibrator) && |
| 1901 | mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
| 1902 | synchronized (VolumeStreamState.class) { |
| 1903 | Set set = mStreamStates[streamType].mIndex.entrySet(); |
| 1904 | Iterator i = set.iterator(); |
| 1905 | while (i.hasNext()) { |
| 1906 | Map.Entry entry = (Map.Entry)i.next(); |
| 1907 | if ((Integer)entry.getValue() == 0) { |
| 1908 | entry.setValue(10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1909 | } |
| 1910 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 1911 | // Persist volume for stream ring when it is changed here |
| 1912 | final int device = getDeviceForStream(streamType); |
| 1913 | sendMsg(mAudioHandler, |
| 1914 | MSG_PERSIST_VOLUME, |
| 1915 | SENDMSG_QUEUE, |
| 1916 | device, |
| 1917 | 0, |
| 1918 | mStreamStates[streamType], |
| 1919 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1920 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 1921 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1922 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1923 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1924 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1925 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1926 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1927 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1930 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1931 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1932 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 1933 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1934 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 1935 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1936 | if (change) { |
| 1937 | // Send sticky broadcast |
| 1938 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 1939 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1942 | private void restoreMasterVolume() { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1943 | if (mUseFixedVolume) { |
| 1944 | AudioSystem.setMasterVolume(1.0f); |
| 1945 | return; |
| 1946 | } |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1947 | if (mUseMasterVolume) { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1948 | float volume = Settings.System.getFloatForUser(mContentResolver, |
| 1949 | Settings.System.VOLUME_MASTER, -1.0f, UserHandle.USER_CURRENT); |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1950 | if (volume >= 0.0f) { |
| 1951 | AudioSystem.setMasterVolume(volume); |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1956 | /** @see AudioManager#shouldVibrate(int) */ |
| 1957 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1958 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | |
| 1960 | switch (getVibrateSetting(vibrateType)) { |
| 1961 | |
| 1962 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1963 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | |
| 1965 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1966 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | |
| 1968 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 1969 | // return false, even for incoming calls |
| 1970 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | |
| 1972 | default: |
| 1973 | return false; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /** @see AudioManager#getVibrateSetting(int) */ |
| 1978 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1979 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 1981 | } |
| 1982 | |
| 1983 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 1984 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 1985 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1986 | if (!mHasVibrator) return; |
| 1987 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1988 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 1989 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | |
| 1991 | // Broadcast change |
| 1992 | broadcastVibrateSetting(vibrateType); |
| 1993 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1996 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 1997 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1998 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1999 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2000 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2001 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2002 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2003 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2007 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2008 | synchronized(mSetModeDeathHandlers) { |
| 2009 | Log.w(TAG, "setMode() client died"); |
| 2010 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2011 | if (index < 0) { |
| 2012 | Log.w(TAG, "unregistered setMode() client died"); |
| 2013 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2014 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2015 | } |
| 2016 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2017 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2018 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2019 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2020 | final long ident = Binder.clearCallingIdentity(); |
| 2021 | disconnectBluetoothSco(newModeOwnerPid); |
| 2022 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2023 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2026 | public int getPid() { |
| 2027 | return mPid; |
| 2028 | } |
| 2029 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2030 | public void setMode(int mode) { |
| 2031 | mMode = mode; |
| 2032 | } |
| 2033 | |
| 2034 | public int getMode() { |
| 2035 | return mMode; |
| 2036 | } |
| 2037 | |
| 2038 | public IBinder getBinder() { |
| 2039 | return mCb; |
| 2040 | } |
| 2041 | } |
| 2042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | /** @see AudioManager#setMode(int) */ |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2044 | public void setMode(int mode, IBinder cb) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2045 | if (DEBUG_MODE) { Log.v(TAG, "setMode(mode=" + mode + ")"); } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2046 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2047 | return; |
| 2048 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2049 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2050 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2051 | (mContext.checkCallingOrSelfPermission( |
| 2052 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2053 | != PackageManager.PERMISSION_GRANTED)) { |
| 2054 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2055 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2056 | return; |
| 2057 | } |
| 2058 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2059 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2060 | return; |
| 2061 | } |
| 2062 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2063 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2064 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2065 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2066 | mode = mMode; |
| 2067 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2068 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid()); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2069 | } |
| 2070 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2071 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2072 | if (newModeOwnerPid != 0) { |
| 2073 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2076 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2077 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2078 | // 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] | 2079 | // any mode other than NORMAL. |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2080 | private int setModeInt(int mode, IBinder cb, int pid) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2081 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2082 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2083 | if (cb == null) { |
| 2084 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2085 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2086 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2087 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2088 | SetModeDeathHandler hdlr = null; |
| 2089 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2090 | while (iter.hasNext()) { |
| 2091 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2092 | if (h.getPid() == pid) { |
| 2093 | hdlr = h; |
| 2094 | // Remove from client list so that it is re-inserted at top of list |
| 2095 | iter.remove(); |
| 2096 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 2101 | do { |
| 2102 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2103 | // get new mode from client at top the list if any |
| 2104 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2105 | hdlr = mSetModeDeathHandlers.get(0); |
| 2106 | cb = hdlr.getBinder(); |
| 2107 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2108 | if (DEBUG_MODE) { |
| 2109 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2110 | + hdlr.mPid); |
| 2111 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2112 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2113 | } else { |
| 2114 | if (hdlr == null) { |
| 2115 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2116 | } |
| 2117 | // Register for client death notification |
| 2118 | try { |
| 2119 | cb.linkToDeath(hdlr, 0); |
| 2120 | } catch (RemoteException e) { |
| 2121 | // Client has died! |
| 2122 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2123 | } |
| 2124 | |
| 2125 | // Last client to call setMode() is always at top of client list |
| 2126 | // as required by SetModeDeathHandler.binderDied() |
| 2127 | mSetModeDeathHandlers.add(0, hdlr); |
| 2128 | hdlr.setMode(mode); |
| 2129 | } |
| 2130 | |
| 2131 | if (mode != mMode) { |
| 2132 | status = AudioSystem.setPhoneState(mode); |
| 2133 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2134 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2135 | mMode = mode; |
| 2136 | } else { |
| 2137 | if (hdlr != null) { |
| 2138 | mSetModeDeathHandlers.remove(hdlr); |
| 2139 | cb.unlinkToDeath(hdlr, 0); |
| 2140 | } |
| 2141 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2142 | 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] | 2143 | mode = AudioSystem.MODE_NORMAL; |
| 2144 | } |
| 2145 | } else { |
| 2146 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2147 | } |
| 2148 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2149 | |
| 2150 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 2151 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2152 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2153 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2154 | } else { |
| 2155 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2156 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2157 | } |
| 2158 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2159 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2160 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
| 2161 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2162 | |
| 2163 | updateStreamVolumeAlias(true /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2164 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2165 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /** @see AudioManager#getMode() */ |
| 2169 | public int getMode() { |
| 2170 | return mMode; |
| 2171 | } |
| 2172 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2173 | //========================================================================================== |
| 2174 | // Sound Effects |
| 2175 | //========================================================================================== |
| 2176 | |
| 2177 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2178 | private static final String ATTR_VERSION = "version"; |
| 2179 | private static final String TAG_GROUP = "group"; |
| 2180 | private static final String ATTR_GROUP_NAME = "name"; |
| 2181 | private static final String TAG_ASSET = "asset"; |
| 2182 | private static final String ATTR_ASSET_ID = "id"; |
| 2183 | private static final String ATTR_ASSET_FILE = "file"; |
| 2184 | |
| 2185 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2186 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2187 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2188 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2189 | |
| 2190 | class LoadSoundEffectReply { |
| 2191 | public int mStatus = 1; |
| 2192 | }; |
| 2193 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2194 | private void loadTouchSoundAssetDefaults() { |
| 2195 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2196 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2197 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2198 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | private void loadTouchSoundAssets() { |
| 2203 | XmlResourceParser parser = null; |
| 2204 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2205 | // only load assets once. |
| 2206 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2207 | return; |
| 2208 | } |
| 2209 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2210 | loadTouchSoundAssetDefaults(); |
| 2211 | |
| 2212 | try { |
| 2213 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2214 | |
| 2215 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2216 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2217 | boolean inTouchSoundsGroup = false; |
| 2218 | |
| 2219 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2220 | while (true) { |
| 2221 | XmlUtils.nextElement(parser); |
| 2222 | String element = parser.getName(); |
| 2223 | if (element == null) { |
| 2224 | break; |
| 2225 | } |
| 2226 | if (element.equals(TAG_GROUP)) { |
| 2227 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2228 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2229 | inTouchSoundsGroup = true; |
| 2230 | break; |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | while (inTouchSoundsGroup) { |
| 2235 | XmlUtils.nextElement(parser); |
| 2236 | String element = parser.getName(); |
| 2237 | if (element == null) { |
| 2238 | break; |
| 2239 | } |
| 2240 | if (element.equals(TAG_ASSET)) { |
| 2241 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2242 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2243 | int fx; |
| 2244 | |
| 2245 | try { |
| 2246 | Field field = AudioManager.class.getField(id); |
| 2247 | fx = field.getInt(null); |
| 2248 | } catch (Exception e) { |
| 2249 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2250 | continue; |
| 2251 | } |
| 2252 | |
| 2253 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2254 | if (i == -1) { |
| 2255 | i = SOUND_EFFECT_FILES.size(); |
| 2256 | SOUND_EFFECT_FILES.add(file); |
| 2257 | } |
| 2258 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2259 | } else { |
| 2260 | break; |
| 2261 | } |
| 2262 | } |
| 2263 | } |
| 2264 | } catch (Resources.NotFoundException e) { |
| 2265 | Log.w(TAG, "audio assets file not found", e); |
| 2266 | } catch (XmlPullParserException e) { |
| 2267 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2268 | } catch (IOException e) { |
| 2269 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2270 | } finally { |
| 2271 | if (parser != null) { |
| 2272 | parser.close(); |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2277 | /** @see AudioManager#playSoundEffect(int) */ |
| 2278 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2279 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2283 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2284 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2285 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2286 | return; |
| 2287 | } |
| 2288 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2289 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2290 | effectType, (int) (volume * 1000), null, 0); |
| 2291 | } |
| 2292 | |
| 2293 | /** |
| 2294 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2295 | * 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] | 2296 | */ |
| 2297 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2298 | int attempts = 3; |
| 2299 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2300 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2301 | synchronized (reply) { |
| 2302 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2303 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2304 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2305 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2306 | } catch (InterruptedException e) { |
| 2307 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2308 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2311 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | /** |
| 2315 | * Unloads samples from the sound pool. |
| 2316 | * This method can be called to free some memory when |
| 2317 | * sound effects are disabled. |
| 2318 | */ |
| 2319 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2320 | 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] | 2321 | } |
| 2322 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2323 | class SoundPoolListenerThread extends Thread { |
| 2324 | public SoundPoolListenerThread() { |
| 2325 | super("SoundPoolListenerThread"); |
| 2326 | } |
| 2327 | |
| 2328 | @Override |
| 2329 | public void run() { |
| 2330 | |
| 2331 | Looper.prepare(); |
| 2332 | mSoundPoolLooper = Looper.myLooper(); |
| 2333 | |
| 2334 | synchronized (mSoundEffectsLock) { |
| 2335 | if (mSoundPool != null) { |
| 2336 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2337 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2338 | } |
| 2339 | mSoundEffectsLock.notify(); |
| 2340 | } |
| 2341 | Looper.loop(); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | private final class SoundPoolCallback implements |
| 2346 | android.media.SoundPool.OnLoadCompleteListener { |
| 2347 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2348 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2349 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2350 | |
| 2351 | public int status() { |
| 2352 | return mStatus; |
| 2353 | } |
| 2354 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2355 | public void setSamples(int[] samples) { |
| 2356 | for (int i = 0; i < samples.length; i++) { |
| 2357 | // do not wait ack for samples rejected upfront by SoundPool |
| 2358 | if (samples[i] > 0) { |
| 2359 | mSamples.add(samples[i]); |
| 2360 | } |
| 2361 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2365 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2366 | int i = mSamples.indexOf(sampleId); |
| 2367 | if (i >= 0) { |
| 2368 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2369 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2370 | if ((status != 0) || mSamples. isEmpty()) { |
| 2371 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2372 | mSoundEffectsLock.notify(); |
| 2373 | } |
| 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2378 | /** @see AudioManager#reloadAudioSettings() */ |
| 2379 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2380 | readAudioSettings(false /*userSwitch*/); |
| 2381 | } |
| 2382 | |
| 2383 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2384 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2385 | readPersistedSettings(); |
| 2386 | |
| 2387 | // restore volume settings |
| 2388 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2389 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2390 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2391 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2392 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2393 | continue; |
| 2394 | } |
| 2395 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2396 | streamState.readSettings(); |
| 2397 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2398 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2399 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2400 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2401 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2402 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2406 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2407 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2408 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2409 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2410 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2411 | checkAllAliasStreamVolumes(); |
| 2412 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2413 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2414 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2415 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2416 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2417 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2418 | enforceSafeMediaVolume(); |
| 2419 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2420 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2423 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2424 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2425 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2426 | return; |
| 2427 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2428 | |
| 2429 | if (on) { |
| 2430 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2431 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2432 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2433 | } |
| 2434 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2435 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2436 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2437 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2438 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2439 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2440 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2444 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2445 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2448 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2449 | public void setBluetoothScoOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2450 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2451 | return; |
| 2452 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2453 | |
| 2454 | if (on) { |
| 2455 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2456 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2457 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2458 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2459 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2460 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2461 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2462 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2463 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2467 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2468 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2471 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2472 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2473 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2474 | mBluetoothA2dpEnabled = on; |
| 2475 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2476 | AudioSystem.FOR_MEDIA, |
| 2477 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2478 | null, 0); |
| 2479 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2483 | public boolean isBluetoothA2dpOn() { |
| 2484 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2485 | return mBluetoothA2dpEnabled; |
| 2486 | } |
| 2487 | } |
| 2488 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2489 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2490 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2491 | int scoAudioMode = |
| 2492 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2493 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2494 | startBluetoothScoInt(cb, scoAudioMode); |
| 2495 | } |
| 2496 | |
| 2497 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2498 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2499 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2500 | } |
| 2501 | |
| 2502 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2503 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2504 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2505 | return; |
| 2506 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2507 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2508 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2509 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2510 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2511 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2512 | // client is created. |
| 2513 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2514 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2515 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /** @see AudioManager#stopBluetoothSco() */ |
| 2519 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2520 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2521 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2522 | return; |
| 2523 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2524 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2525 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2526 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2527 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2528 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2529 | if (client != null) { |
| 2530 | client.decCount(); |
| 2531 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2532 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2535 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2536 | private class ScoClient implements IBinder.DeathRecipient { |
| 2537 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2538 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2539 | private int mStartcount; // number of SCO connections started by this client |
| 2540 | |
| 2541 | ScoClient(IBinder cb) { |
| 2542 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2543 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2544 | mStartcount = 0; |
| 2545 | } |
| 2546 | |
| 2547 | public void binderDied() { |
| 2548 | synchronized(mScoClients) { |
| 2549 | Log.w(TAG, "SCO client died"); |
| 2550 | int index = mScoClients.indexOf(this); |
| 2551 | if (index < 0) { |
| 2552 | Log.w(TAG, "unregistered SCO client died"); |
| 2553 | } else { |
| 2554 | clearCount(true); |
| 2555 | mScoClients.remove(this); |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2560 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2561 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2562 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2563 | if (mStartcount == 0) { |
| 2564 | try { |
| 2565 | mCb.linkToDeath(this, 0); |
| 2566 | } catch (RemoteException e) { |
| 2567 | // client has already died! |
| 2568 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2569 | } |
| 2570 | } |
| 2571 | mStartcount++; |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | public void decCount() { |
| 2576 | synchronized(mScoClients) { |
| 2577 | if (mStartcount == 0) { |
| 2578 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2579 | } else { |
| 2580 | mStartcount--; |
| 2581 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2582 | try { |
| 2583 | mCb.unlinkToDeath(this, 0); |
| 2584 | } catch (NoSuchElementException e) { |
| 2585 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2586 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2587 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2588 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | public void clearCount(boolean stopSco) { |
| 2594 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2595 | if (mStartcount != 0) { |
| 2596 | try { |
| 2597 | mCb.unlinkToDeath(this, 0); |
| 2598 | } catch (NoSuchElementException e) { |
| 2599 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2600 | } |
| 2601 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2602 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2603 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2604 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | public int getCount() { |
| 2610 | return mStartcount; |
| 2611 | } |
| 2612 | |
| 2613 | public IBinder getBinder() { |
| 2614 | return mCb; |
| 2615 | } |
| 2616 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2617 | public int getPid() { |
| 2618 | return mCreatorPid; |
| 2619 | } |
| 2620 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2621 | public int totalCount() { |
| 2622 | synchronized(mScoClients) { |
| 2623 | int count = 0; |
| 2624 | int size = mScoClients.size(); |
| 2625 | for (int i = 0; i < size; i++) { |
| 2626 | count += mScoClients.get(i).getCount(); |
| 2627 | } |
| 2628 | return count; |
| 2629 | } |
| 2630 | } |
| 2631 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2632 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2633 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2634 | if (totalCount() == 0) { |
| 2635 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2636 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2637 | // the connection. |
| 2638 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2639 | // 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] | 2640 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2641 | synchronized(mSetModeDeathHandlers) { |
| 2642 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2643 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2644 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2645 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2646 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2647 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2648 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2649 | if (mBluetoothHeadsetDevice != null) { |
| 2650 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2651 | mContentResolver, |
| 2652 | "bluetooth_sco_channel_"+ |
| 2653 | mBluetoothHeadsetDevice.getAddress(), |
| 2654 | SCO_MODE_VIRTUAL_CALL)); |
| 2655 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2656 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2657 | } |
| 2658 | } else { |
| 2659 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2660 | } |
| 2661 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2662 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2663 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2664 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2665 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2666 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2667 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2668 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2669 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2670 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2671 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2672 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2673 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2674 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2675 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2676 | } else { |
| 2677 | broadcastScoConnectionState( |
| 2678 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2679 | } |
| 2680 | } else if (getBluetoothHeadset()) { |
| 2681 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2682 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2683 | } else { |
| 2684 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2685 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2686 | } |
| 2687 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2688 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2689 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2690 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2691 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2692 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2693 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2694 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2695 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2696 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2697 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2698 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2699 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2700 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2701 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2702 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2703 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2704 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2705 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2706 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2707 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2708 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2709 | broadcastScoConnectionState( |
| 2710 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2711 | } |
| 2712 | } else if (getBluetoothHeadset()) { |
| 2713 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2714 | } |
| 2715 | } else { |
| 2716 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2717 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2718 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2724 | private void checkScoAudioState() { |
| 2725 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2726 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2727 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2728 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2729 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2730 | } |
| 2731 | } |
| 2732 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2733 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2734 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2735 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2736 | int size = mScoClients.size(); |
| 2737 | for (int i = 0; i < size; i++) { |
| 2738 | client = mScoClients.get(i); |
| 2739 | if (client.getBinder() == cb) |
| 2740 | return client; |
| 2741 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2742 | if (create) { |
| 2743 | client = new ScoClient(cb); |
| 2744 | mScoClients.add(client); |
| 2745 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2746 | return client; |
| 2747 | } |
| 2748 | } |
| 2749 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2750 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2751 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2752 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2753 | int size = mScoClients.size(); |
| 2754 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2755 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2756 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2757 | cl.clearCount(stopSco); |
| 2758 | } else { |
| 2759 | savedClient = cl; |
| 2760 | } |
| 2761 | } |
| 2762 | mScoClients.clear(); |
| 2763 | if (savedClient != null) { |
| 2764 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | } |
| 2768 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2769 | private boolean getBluetoothHeadset() { |
| 2770 | boolean result = false; |
| 2771 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2772 | if (adapter != null) { |
| 2773 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2774 | BluetoothProfile.HEADSET); |
| 2775 | } |
| 2776 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2777 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2778 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2779 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2780 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2781 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2782 | return result; |
| 2783 | } |
| 2784 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2785 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2786 | synchronized(mScoClients) { |
| 2787 | checkScoAudioState(); |
| 2788 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2789 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2790 | if (mBluetoothHeadsetDevice != null) { |
| 2791 | if (mBluetoothHeadset != null) { |
| 2792 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2793 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2794 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2795 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2796 | } |
| 2797 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2798 | getBluetoothHeadset()) { |
| 2799 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2800 | } |
| 2801 | } |
| 2802 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2803 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | private void resetBluetoothSco() { |
| 2809 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2810 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2811 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2812 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2817 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 2818 | SENDMSG_QUEUE, state, 0, null, 0); |
| 2819 | } |
| 2820 | |
| 2821 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2822 | if (state != mScoConnectionState) { |
| 2823 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 2824 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 2825 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 2826 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2827 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2828 | mScoConnectionState = state; |
| 2829 | } |
| 2830 | } |
| 2831 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2832 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 2833 | new BluetoothProfile.ServiceListener() { |
| 2834 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2835 | BluetoothDevice btDevice; |
| 2836 | List<BluetoothDevice> deviceList; |
| 2837 | switch(profile) { |
| 2838 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2839 | synchronized (mConnectedDevices) { |
| 2840 | synchronized (mA2dpAvrcpLock) { |
| 2841 | mA2dp = (BluetoothA2dp) proxy; |
| 2842 | deviceList = mA2dp.getConnectedDevices(); |
| 2843 | if (deviceList.size() > 0) { |
| 2844 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2845 | int state = mA2dp.getConnectionState(btDevice); |
| 2846 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2847 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 2848 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2849 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2850 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2851 | state, |
| 2852 | 0, |
| 2853 | btDevice, |
| 2854 | delay); |
| 2855 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 2856 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2857 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2858 | break; |
| 2859 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2860 | case BluetoothProfile.A2DP_SINK: |
| 2861 | deviceList = proxy.getConnectedDevices(); |
| 2862 | if (deviceList.size() > 0) { |
| 2863 | btDevice = deviceList.get(0); |
| 2864 | synchronized (mConnectedDevices) { |
| 2865 | int state = proxy.getConnectionState(btDevice); |
| 2866 | queueMsgUnderWakeLock(mAudioHandler, |
| 2867 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 2868 | state, |
| 2869 | 0, |
| 2870 | btDevice, |
| 2871 | 0 /* delay */); |
| 2872 | } |
| 2873 | } |
| 2874 | break; |
| 2875 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2876 | case BluetoothProfile.HEADSET: |
| 2877 | synchronized (mScoClients) { |
| 2878 | // Discard timeout message |
| 2879 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 2880 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 2881 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 2882 | if (deviceList.size() > 0) { |
| 2883 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 2884 | } else { |
| 2885 | mBluetoothHeadsetDevice = null; |
| 2886 | } |
| 2887 | // Refresh SCO audio state |
| 2888 | checkScoAudioState(); |
| 2889 | // Continue pending action if any |
| 2890 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 2891 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 2892 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2893 | boolean status = false; |
| 2894 | if (mBluetoothHeadsetDevice != null) { |
| 2895 | switch (mScoAudioState) { |
| 2896 | case SCO_STATE_ACTIVATE_REQ: |
| 2897 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2898 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2899 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2900 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2901 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2902 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2903 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2904 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2905 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2906 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2907 | break; |
| 2908 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2909 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2910 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2911 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2912 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2913 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2914 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2915 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2916 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2917 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2918 | break; |
| 2919 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 2920 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2921 | mBluetoothHeadsetDevice); |
| 2922 | } |
| 2923 | } |
| 2924 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2925 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2926 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2927 | } |
| 2928 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2929 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2930 | break; |
| 2931 | |
| 2932 | default: |
| 2933 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2934 | } |
| 2935 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2936 | public void onServiceDisconnected(int profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2937 | switch(profile) { |
| 2938 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2939 | synchronized (mConnectedDevices) { |
| 2940 | synchronized (mA2dpAvrcpLock) { |
| 2941 | mA2dp = null; |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2942 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)) { |
| 2943 | makeA2dpDeviceUnavailableNow( |
| 2944 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)); |
| 2945 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | break; |
| 2949 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2950 | case BluetoothProfile.A2DP_SINK: |
| 2951 | synchronized (mConnectedDevices) { |
| 2952 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)) { |
| 2953 | makeA2dpSrcUnavailable( |
| 2954 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)); |
| 2955 | } |
| 2956 | } |
| 2957 | break; |
| 2958 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2959 | case BluetoothProfile.HEADSET: |
| 2960 | synchronized (mScoClients) { |
| 2961 | mBluetoothHeadset = null; |
| 2962 | } |
| 2963 | break; |
| 2964 | |
| 2965 | default: |
| 2966 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 2970 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2971 | private void onCheckMusicActive() { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2972 | synchronized (mSafeMediaVolumeState) { |
| 2973 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2974 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 2975 | |
| 2976 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 2977 | sendMsg(mAudioHandler, |
| 2978 | MSG_CHECK_MUSIC_ACTIVE, |
| 2979 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2980 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2981 | 0, |
| 2982 | null, |
| 2983 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2984 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2985 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 2986 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2987 | // Approximate cumulative active music time |
| 2988 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 2989 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
| 2990 | setSafeMediaVolumeEnabled(true); |
| 2991 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2992 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2993 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | } |
| 2998 | } |
| 2999 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3000 | private void saveMusicActiveMs() { |
| 3001 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3002 | } |
| 3003 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3004 | private void onConfigureSafeVolume(boolean force) { |
| 3005 | synchronized (mSafeMediaVolumeState) { |
| 3006 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3007 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3008 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3009 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3010 | boolean safeMediaVolumeEnabled = |
| 3011 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3012 | || mContext.getResources().getBoolean( |
| 3013 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3014 | |
| 3015 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3016 | // next time we boot and cannot be "inactive" |
| 3017 | int persistedState; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3018 | if (safeMediaVolumeEnabled) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3019 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3020 | // The state can already be "inactive" here if the user has forced it before |
| 3021 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3022 | // it to "active". |
| 3023 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3024 | if (mMusicActiveMs == 0) { |
| 3025 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3026 | enforceSafeMediaVolume(); |
| 3027 | } else { |
| 3028 | // We have existing playback time recorded, already confirmed. |
| 3029 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3030 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3031 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3032 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3033 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3034 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3035 | } |
| 3036 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3037 | sendMsg(mAudioHandler, |
| 3038 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3039 | SENDMSG_QUEUE, |
| 3040 | persistedState, |
| 3041 | 0, |
| 3042 | null, |
| 3043 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | } |
| 3047 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3048 | /////////////////////////////////////////////////////////////////////////// |
| 3049 | // Internal methods |
| 3050 | /////////////////////////////////////////////////////////////////////////// |
| 3051 | |
| 3052 | /** |
| 3053 | * Checks if the adjustment should change ringer mode instead of just |
| 3054 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3055 | * indices on the stream states. |
| 3056 | */ |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3057 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3058 | int result = FLAG_ADJUST_VOLUME; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3059 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3061 | switch (ringerMode) { |
| 3062 | case RINGER_MODE_NORMAL: |
| 3063 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3064 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3065 | // "step" is the delta in internal index units corresponding to a |
| 3066 | // change of 1 in UI index units. |
| 3067 | // Because of rounding when rescaling from one stream index range to its alias |
| 3068 | // index range, we cannot simply test oldIndex == step: |
| 3069 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3070 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3071 | ringerMode = RINGER_MODE_VIBRATE; |
| 3072 | } |
| 3073 | } else { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3074 | // (oldIndex < step) is equivalent to (old UI index == 0) |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 3075 | if ((oldIndex < step) |
| 3076 | && VOLUME_SETS_RINGER_MODE_SILENT |
| 3077 | && mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3078 | ringerMode = RINGER_MODE_SILENT; |
| 3079 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3080 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3081 | } else if (direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3082 | || direction == AudioManager.ADJUST_MUTE) { |
| 3083 | if (mHasVibrator) { |
| 3084 | ringerMode = RINGER_MODE_VIBRATE; |
| 3085 | } else { |
| 3086 | ringerMode = RINGER_MODE_SILENT; |
| 3087 | } |
| 3088 | // Setting the ringer mode will toggle mute |
| 3089 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3090 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3091 | break; |
| 3092 | case RINGER_MODE_VIBRATE: |
| 3093 | if (!mHasVibrator) { |
| 3094 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3095 | "but no vibrator is present"); |
| 3096 | break; |
| 3097 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3098 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3099 | // This is the case we were muted with the volume turned up |
| 3100 | if (oldIndex >= 2 * step && isMuted) { |
| 3101 | ringerMode = RINGER_MODE_NORMAL; |
| 3102 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3103 | if (VOLUME_SETS_RINGER_MODE_SILENT) { |
| 3104 | ringerMode = RINGER_MODE_SILENT; |
| 3105 | } else { |
| 3106 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3107 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3108 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3109 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3110 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3111 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3112 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3113 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3114 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3115 | break; |
| 3116 | case RINGER_MODE_SILENT: |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3117 | if (direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
| 3118 | // This is the case we were muted with the volume turned up |
| 3119 | ringerMode = RINGER_MODE_NORMAL; |
| 3120 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3121 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3122 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3123 | if (PREVENT_VOLUME_ADJUSTMENT_IF_SILENT) { |
| 3124 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3125 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3126 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3127 | ringerMode = RINGER_MODE_VIBRATE; |
| 3128 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3129 | // If we don't have a vibrator or they were toggling mute |
| 3130 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3131 | ringerMode = RINGER_MODE_NORMAL; |
| 3132 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3133 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3134 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3135 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3136 | break; |
| 3137 | default: |
| 3138 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3139 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3140 | } |
| 3141 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3142 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3143 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3144 | mPrevVolDirection = direction; |
| 3145 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3146 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3147 | } |
| 3148 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3149 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3150 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3151 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3154 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3155 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3156 | } |
| 3157 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3158 | boolean updateRingerModeAffectedStreams() { |
| 3159 | int ringerModeAffectedStreams; |
| 3160 | // make sure settings for ringer mode are consistent with device type: non voice capable |
| 3161 | // devices (tablets) include media stream in silent mode whereas phones don't. |
| 3162 | ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
| 3163 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3164 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3165 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3166 | UserHandle.USER_CURRENT); |
| 3167 | |
| 3168 | // ringtone, notification and system streams are always affected by ringer mode |
| 3169 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_RING)| |
| 3170 | (1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3171 | (1 << AudioSystem.STREAM_SYSTEM); |
| 3172 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3173 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3174 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3175 | ringerModeAffectedStreams = 0; |
| 3176 | break; |
| 3177 | default: |
John Spurlock | 77e54d9 | 2014-08-11 12:16:24 -0400 | [diff] [blame] | 3178 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3179 | break; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3180 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3181 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3182 | synchronized (mCameraSoundForced) { |
| 3183 | if (mCameraSoundForced) { |
| 3184 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3185 | } else { |
| 3186 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3187 | } |
| 3188 | } |
| 3189 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3190 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3191 | } else { |
| 3192 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3193 | } |
| 3194 | |
| 3195 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3196 | Settings.System.putIntForUser(mContentResolver, |
| 3197 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3198 | ringerModeAffectedStreams, |
| 3199 | UserHandle.USER_CURRENT); |
| 3200 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3201 | return true; |
| 3202 | } |
| 3203 | return false; |
| 3204 | } |
| 3205 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3206 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3207 | public boolean isStreamAffectedByMute(int streamType) { |
| 3208 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3209 | } |
| 3210 | |
| 3211 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3212 | switch (direction) { |
| 3213 | case AudioManager.ADJUST_LOWER: |
| 3214 | case AudioManager.ADJUST_RAISE: |
| 3215 | case AudioManager.ADJUST_SAME: |
| 3216 | case AudioManager.ADJUST_MUTE: |
| 3217 | case AudioManager.ADJUST_UNMUTE: |
| 3218 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3219 | break; |
| 3220 | default: |
| 3221 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3222 | } |
| 3223 | } |
| 3224 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 3225 | private void ensureValidSteps(int steps) { |
| 3226 | if (Math.abs(steps) > MAX_BATCH_VOLUME_ADJUST_STEPS) { |
| 3227 | throw new IllegalArgumentException("Bad volume adjust steps " + steps); |
| 3228 | } |
| 3229 | } |
| 3230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3231 | private void ensureValidStreamType(int streamType) { |
| 3232 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3233 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3234 | } |
| 3235 | } |
| 3236 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3237 | private boolean isMuteAdjust(int adjust) { |
| 3238 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3239 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3240 | } |
| 3241 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3242 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3243 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3244 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3245 | TelecomManager telecomManager = |
| 3246 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3247 | |
| 3248 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3249 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3250 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3251 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3252 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3253 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3254 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3255 | /** |
| 3256 | * For code clarity for getActiveStreamType(int) |
| 3257 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3258 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3259 | * in the last "delay_ms" ms. |
| 3260 | */ |
| 3261 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3262 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3263 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3264 | } |
| 3265 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3266 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3267 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3268 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3269 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3270 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3271 | == AudioSystem.FORCE_BT_SCO) { |
| 3272 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3273 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3274 | } else { |
| 3275 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3276 | return AudioSystem.STREAM_VOICE_CALL; |
| 3277 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3278 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3279 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3280 | if (DEBUG_VOL) |
| 3281 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3282 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3283 | } else { |
| 3284 | if (DEBUG_VOL) |
| 3285 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3286 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3287 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3288 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3289 | if (DEBUG_VOL) |
| 3290 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3291 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3292 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3293 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3294 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3295 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 3296 | // TV always defaults to STREAM_MUSIC |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3297 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3298 | } |
| 3299 | break; |
| 3300 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3301 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3302 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3303 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3304 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3305 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3306 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3307 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3308 | return AudioSystem.STREAM_VOICE_CALL; |
| 3309 | } |
| 3310 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3311 | StreamOverride.sDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3312 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3313 | StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3314 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3315 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3316 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3317 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3318 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3319 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3320 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3321 | if (DEBUG_VOL) Log.v(TAG, |
| 3322 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3323 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3324 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3325 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3326 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3327 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3328 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3329 | + suggestedStreamType); |
| 3330 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | } |
| 3332 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3333 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3334 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3335 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3336 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3337 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3338 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3339 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | private void broadcastVibrateSetting(int vibrateType) { |
| 3343 | // Send broadcast |
| 3344 | if (ActivityManagerNative.isSystemReady()) { |
| 3345 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3346 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3347 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3348 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3353 | /** |
| 3354 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3355 | * Note that the wake lock needs to be released after the message has been handled. |
| 3356 | */ |
| 3357 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3358 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3359 | final long ident = Binder.clearCallingIdentity(); |
| 3360 | // Always acquire the wake lock as AudioService because it is released by the |
| 3361 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3362 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3363 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3364 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3366 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3367 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3368 | 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] | 3369 | |
| 3370 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3371 | handler.removeMessages(msg); |
| 3372 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3373 | return; |
| 3374 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3375 | synchronized (mLastDeviceConnectMsgTime) { |
| 3376 | long time = SystemClock.uptimeMillis() + delay; |
| 3377 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3378 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3379 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3380 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3381 | mLastDeviceConnectMsgTime = time; |
| 3382 | } |
| 3383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3387 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3388 | == PackageManager.PERMISSION_GRANTED) { |
| 3389 | return true; |
| 3390 | } |
| 3391 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3392 | + Binder.getCallingPid() |
| 3393 | + ", uid=" + Binder.getCallingUid(); |
| 3394 | Log.w(TAG, msg); |
| 3395 | return false; |
| 3396 | } |
| 3397 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3398 | private int getDeviceForStream(int stream) { |
| 3399 | int device = AudioSystem.getDevicesForStream(stream); |
| 3400 | if ((device & (device - 1)) != 0) { |
| 3401 | // Multiple device selection is either: |
| 3402 | // - speaker + one other device: give priority to speaker in this case. |
| 3403 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3404 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3405 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3406 | // - 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] | 3407 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3408 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3409 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3410 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3411 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3412 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3413 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3414 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3415 | } else { |
| 3416 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3417 | } |
| 3418 | } |
| 3419 | return device; |
| 3420 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3421 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3422 | /* |
| 3423 | * A class just for packaging up a set of connection parameters. |
| 3424 | */ |
| 3425 | private class WiredDeviceConnectionState { |
| 3426 | public int mType; |
| 3427 | public int mState; |
| 3428 | public String mAddress; |
| 3429 | public String mName; |
| 3430 | |
| 3431 | public WiredDeviceConnectionState(int type, int state, String address, String name) { |
| 3432 | mType = type; |
| 3433 | mState = state; |
| 3434 | mAddress = address; |
| 3435 | mName = name; |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | public void setWiredDeviceConnectionState(int type, int state, String address, |
| 3440 | String name) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3441 | synchronized (mConnectedDevices) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3442 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3443 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3444 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3445 | 0, |
| 3446 | 0, |
| 3447 | new WiredDeviceConnectionState(type, state, address, name), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3448 | delay); |
| 3449 | } |
| 3450 | } |
| 3451 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3452 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3453 | { |
| 3454 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3455 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3456 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3457 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3458 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3459 | if (profile == BluetoothProfile.A2DP) { |
| 3460 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3461 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3462 | } else { |
| 3463 | delay = 0; |
| 3464 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3465 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3466 | (profile == BluetoothProfile.A2DP ? |
| 3467 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3468 | state, |
| 3469 | 0, |
| 3470 | device, |
| 3471 | delay); |
| 3472 | } |
| 3473 | return delay; |
| 3474 | } |
| 3475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | /////////////////////////////////////////////////////////////////////////// |
| 3477 | // Inner classes |
| 3478 | /////////////////////////////////////////////////////////////////////////// |
| 3479 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3480 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3481 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3482 | // 2 mSetModeDeathHandlers |
| 3483 | // 3 mSettingsLock |
| 3484 | // 4 VolumeStreamState.class |
| 3485 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3487 | private final int mStreamType; |
| 3488 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3489 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3490 | private String mVolumeIndexSettingName; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3491 | private int mIndexMax; |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3492 | private final ConcurrentHashMap<Integer, Integer> mIndex = |
| 3493 | new ConcurrentHashMap<Integer, Integer>(8, 0.75f, 4); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3494 | private final Intent mVolumeChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3495 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3496 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3497 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3498 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | |
| 3500 | mStreamType = streamType; |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 3501 | mIndexMax = MAX_STREAM_VOLUME[streamType]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3502 | AudioSystem.initStreamVolume(streamType, 0, mIndexMax); |
| 3503 | mIndexMax *= 10; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3504 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3505 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3506 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3507 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3508 | } |
| 3509 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3510 | public String getSettingNameForDevice(int device) { |
| 3511 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3512 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3513 | if (suffix.isEmpty()) { |
| 3514 | return name; |
| 3515 | } |
| 3516 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3519 | public void readSettings() { |
| 3520 | synchronized (VolumeStreamState.class) { |
Wally Yau | da39290 | 2014-11-28 12:40:30 -0800 | [diff] [blame] | 3521 | // force maximum volume on all streams if fixed volume property |
| 3522 | // or master volume property is set |
| 3523 | if (mUseFixedVolume || mUseMasterVolume) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3524 | mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 3525 | return; |
| 3526 | } |
| 3527 | // do not read system stream volume from settings: this stream is always aliased |
| 3528 | // to another stream type and its volume is never persisted. Values in settings can |
| 3529 | // only be stale values |
| 3530 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3531 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3532 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3533 | synchronized (mCameraSoundForced) { |
| 3534 | if (mCameraSoundForced) { |
| 3535 | index = mIndexMax; |
| 3536 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3537 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3538 | mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 3539 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3540 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3541 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3542 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3543 | |
| 3544 | for (int i = 0; remainingDevices != 0; i++) { |
| 3545 | int device = (1 << i); |
| 3546 | if ((device & remainingDevices) == 0) { |
| 3547 | continue; |
| 3548 | } |
| 3549 | remainingDevices &= ~device; |
| 3550 | |
| 3551 | // retrieve current volume for device |
| 3552 | String name = getSettingNameForDevice(device); |
| 3553 | // if no volume stored for current stream and device, use default volume if default |
| 3554 | // device, continue otherwise |
| 3555 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3556 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3557 | int index = Settings.System.getIntForUser( |
| 3558 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3559 | if (index == -1) { |
| 3560 | continue; |
| 3561 | } |
| 3562 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3563 | mIndex.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3564 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3568 | // must be called while synchronized VolumeStreamState.class |
| 3569 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3570 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3571 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3572 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3573 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) |
| 3574 | || ((device & mFullVolumeDevices) != 0)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3575 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3576 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3577 | index = (getIndex(device) + 5)/10; |
| 3578 | } |
| 3579 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3581 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3582 | public void applyAllVolumes() { |
| 3583 | synchronized (VolumeStreamState.class) { |
| 3584 | // apply default volume first: by convention this will reset all |
| 3585 | // devices volumes in audio policy manager to the supplied value |
| 3586 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3587 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3588 | index = 0; |
| 3589 | } else { |
| 3590 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3591 | } |
| 3592 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3593 | // then apply device specific volumes |
| 3594 | Set set = mIndex.entrySet(); |
| 3595 | Iterator i = set.iterator(); |
| 3596 | while (i.hasNext()) { |
| 3597 | Map.Entry entry = (Map.Entry)i.next(); |
| 3598 | int device = ((Integer)entry.getKey()).intValue(); |
| 3599 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3600 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3601 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3602 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3603 | mAvrcpAbsVolSupported) |
| 3604 | || ((device & mFullVolumeDevices) != 0)) |
| 3605 | { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3606 | index = (mIndexMax + 5)/10; |
| 3607 | } else { |
| 3608 | index = ((Integer)entry.getValue() + 5)/10; |
| 3609 | } |
| 3610 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3611 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | public boolean adjustIndex(int deltaIndex, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3617 | return setIndex(getIndex(device) + deltaIndex, |
| 3618 | device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3619 | } |
| 3620 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3621 | public boolean setIndex(int index, int device) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3622 | boolean changed = false; |
| 3623 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3624 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3625 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3626 | index = getValidIndex(index); |
| 3627 | synchronized (mCameraSoundForced) { |
| 3628 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3629 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3630 | } |
| 3631 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3632 | mIndex.put(device, index); |
| 3633 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3634 | changed = oldIndex != index; |
| 3635 | if (changed) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3636 | // Apply change to all streams using this one as alias |
| 3637 | // if changing volume of current device, also change volume of current |
| 3638 | // device on aliased stream |
| 3639 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3640 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3641 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3642 | if (streamType != mStreamType && |
| 3643 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3644 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 3645 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3646 | device); |
| 3647 | if (currentDevice) { |
| 3648 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3649 | getDeviceForStream(streamType)); |
| 3650 | } |
| 3651 | } |
| 3652 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3654 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3655 | if (changed) { |
| 3656 | oldIndex = (oldIndex + 5) / 10; |
| 3657 | index = (index + 5) / 10; |
| 3658 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 3659 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
| 3660 | sendBroadcastToAll(mVolumeChanged); |
| 3661 | } |
| 3662 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3663 | } |
| 3664 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3665 | public int getIndex(int device) { |
| 3666 | synchronized (VolumeStreamState.class) { |
| 3667 | Integer index = mIndex.get(device); |
| 3668 | if (index == null) { |
| 3669 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
| 3670 | index = mIndex.get(AudioSystem.DEVICE_OUT_DEFAULT); |
| 3671 | } |
| 3672 | return index.intValue(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3673 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3676 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3677 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3678 | } |
| 3679 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3680 | public void setAllIndexes(VolumeStreamState srcStream) { |
| 3681 | synchronized (VolumeStreamState.class) { |
| 3682 | int srcStreamType = srcStream.getStreamType(); |
| 3683 | // apply default device volume from source stream to all devices first in case |
| 3684 | // some devices are present in this stream state but not in source stream state |
| 3685 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3686 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3687 | Set set = mIndex.entrySet(); |
| 3688 | Iterator i = set.iterator(); |
| 3689 | while (i.hasNext()) { |
| 3690 | Map.Entry entry = (Map.Entry)i.next(); |
| 3691 | entry.setValue(index); |
| 3692 | } |
| 3693 | // Now apply actual volume for devices in source stream state |
| 3694 | set = srcStream.mIndex.entrySet(); |
| 3695 | i = set.iterator(); |
| 3696 | while (i.hasNext()) { |
| 3697 | Map.Entry entry = (Map.Entry)i.next(); |
| 3698 | int device = ((Integer)entry.getKey()).intValue(); |
| 3699 | index = ((Integer)entry.getValue()).intValue(); |
| 3700 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3701 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3702 | setIndex(index, device); |
| 3703 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3704 | } |
| 3705 | } |
| 3706 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3707 | public void setAllIndexesToMax() { |
| 3708 | synchronized (VolumeStreamState.class) { |
| 3709 | Set set = mIndex.entrySet(); |
| 3710 | Iterator i = set.iterator(); |
| 3711 | while (i.hasNext()) { |
| 3712 | Map.Entry entry = (Map.Entry)i.next(); |
| 3713 | entry.setValue(mIndexMax); |
| 3714 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3715 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3718 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3719 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3720 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3721 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3722 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3723 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3724 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3725 | // Set the new mute volume. This propagates the values to |
| 3726 | // the audio system, otherwise the volume won't be changed |
| 3727 | // at the lower level. |
| 3728 | sendMsg(mAudioHandler, |
| 3729 | MSG_SET_ALL_VOLUMES, |
| 3730 | SENDMSG_QUEUE, |
| 3731 | 0, |
| 3732 | 0, |
| 3733 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3734 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3735 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3736 | if (changed) { |
| 3737 | // Stream mute changed, fire the intent. |
| 3738 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 3739 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3740 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 3741 | sendBroadcastToAll(intent); |
| 3742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3743 | } |
| 3744 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3745 | public int getStreamType() { |
| 3746 | return mStreamType; |
| 3747 | } |
| 3748 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3749 | public void checkFixedVolumeDevices() { |
| 3750 | synchronized (VolumeStreamState.class) { |
| 3751 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 3752 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
| 3753 | Set set = mIndex.entrySet(); |
| 3754 | Iterator i = set.iterator(); |
| 3755 | while (i.hasNext()) { |
| 3756 | Map.Entry entry = (Map.Entry)i.next(); |
| 3757 | int device = ((Integer)entry.getKey()).intValue(); |
| 3758 | int index = ((Integer)entry.getValue()).intValue(); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3759 | if (((device & mFullVolumeDevices) != 0) |
| 3760 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3761 | entry.setValue(mIndexMax); |
| 3762 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3763 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | } |
| 3768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | private int getValidIndex(int index) { |
| 3770 | if (index < 0) { |
| 3771 | return 0; |
Wally Yau | da39290 | 2014-11-28 12:40:30 -0800 | [diff] [blame] | 3772 | } else if (mUseFixedVolume || mUseMasterVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3773 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | return index; |
| 3777 | } |
| 3778 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3779 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3780 | pw.print(" Muted: "); |
| 3781 | pw.println(mIsMuted); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3782 | pw.print(" Max: "); |
| 3783 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3784 | pw.print(" Current: "); |
| 3785 | Set set = mIndex.entrySet(); |
| 3786 | Iterator i = set.iterator(); |
| 3787 | while (i.hasNext()) { |
| 3788 | Map.Entry entry = (Map.Entry)i.next(); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3789 | final int device = (Integer) entry.getKey(); |
| 3790 | pw.print(Integer.toHexString(device)); |
| 3791 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 3792 | : AudioSystem.getOutputDeviceName(device); |
| 3793 | if (!deviceName.isEmpty()) { |
| 3794 | pw.print(" ("); |
| 3795 | pw.print(deviceName); |
| 3796 | pw.print(")"); |
| 3797 | } |
| 3798 | pw.print(": "); |
| 3799 | final int index = (((Integer) entry.getValue()) + 5) / 10; |
| 3800 | pw.print(index); |
| 3801 | if (i.hasNext()) { |
| 3802 | pw.print(", "); |
| 3803 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3804 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | /** Thread that handles native AudioSystem control. */ |
| 3809 | private class AudioSystemThread extends Thread { |
| 3810 | AudioSystemThread() { |
| 3811 | super("AudioService"); |
| 3812 | } |
| 3813 | |
| 3814 | @Override |
| 3815 | public void run() { |
| 3816 | // Set this thread up so the handler will work on it |
| 3817 | Looper.prepare(); |
| 3818 | |
| 3819 | synchronized(AudioService.this) { |
| 3820 | mAudioHandler = new AudioHandler(); |
| 3821 | |
| 3822 | // Notify that the handler has been created |
| 3823 | AudioService.this.notify(); |
| 3824 | } |
| 3825 | |
| 3826 | // Listen for volume change requests that are set by VolumePanel |
| 3827 | Looper.loop(); |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | /** Handles internal volume messages in separate volume thread. */ |
| 3832 | private class AudioHandler extends Handler { |
| 3833 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3834 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3835 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3836 | synchronized (VolumeStreamState.class) { |
| 3837 | // Apply volume |
| 3838 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3839 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3840 | // Apply change to all streams using this one as alias |
| 3841 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3842 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3843 | if (streamType != streamState.mStreamType && |
| 3844 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 3845 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 3846 | // that may have a different device selected |
| 3847 | int streamDevice = getDeviceForStream(streamType); |
| 3848 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 3849 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 3850 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 3851 | } |
| 3852 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3853 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3854 | } |
| 3855 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3856 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3857 | sendMsg(mAudioHandler, |
| 3858 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 3859 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3860 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3861 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3862 | streamState, |
| 3863 | PERSIST_DELAY); |
| 3864 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3865 | } |
| 3866 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3867 | private void setAllVolumes(VolumeStreamState streamState) { |
| 3868 | |
| 3869 | // Apply volume |
| 3870 | streamState.applyAllVolumes(); |
| 3871 | |
| 3872 | // Apply change to all streams using this one as alias |
| 3873 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3874 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3875 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3876 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3877 | mStreamStates[streamType].applyAllVolumes(); |
| 3878 | } |
| 3879 | } |
| 3880 | } |
| 3881 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3882 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3883 | if (mUseFixedVolume) { |
| 3884 | return; |
| 3885 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3886 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 3887 | return; |
| 3888 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3889 | System.putIntForUser(mContentResolver, |
| 3890 | streamState.getSettingNameForDevice(device), |
| 3891 | (streamState.getIndex(device) + 5)/ 10, |
| 3892 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3893 | } |
| 3894 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3895 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3896 | if (mUseFixedVolume) { |
| 3897 | return; |
| 3898 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 3899 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3900 | } |
| 3901 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3902 | private boolean onLoadSoundEffects() { |
| 3903 | int status; |
| 3904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3906 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3907 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 3908 | return false; |
| 3909 | } |
| 3910 | |
| 3911 | if (mSoundPool != null) { |
| 3912 | return true; |
| 3913 | } |
| 3914 | |
| 3915 | loadTouchSoundAssets(); |
| 3916 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 3917 | mSoundPool = new SoundPool.Builder() |
| 3918 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 3919 | .setAudioAttributes(new AudioAttributes.Builder() |
| 3920 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 3921 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 3922 | .build()) |
| 3923 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3924 | mSoundPoolCallBack = null; |
| 3925 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 3926 | mSoundPoolListenerThread.start(); |
| 3927 | int attempts = 3; |
| 3928 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 3929 | try { |
| 3930 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3931 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3932 | } catch (InterruptedException e) { |
| 3933 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | if (mSoundPoolCallBack == null) { |
| 3938 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 3939 | if (mSoundPoolLooper != null) { |
| 3940 | mSoundPoolLooper.quit(); |
| 3941 | mSoundPoolLooper = null; |
| 3942 | } |
| 3943 | mSoundPoolListenerThread = null; |
| 3944 | mSoundPool.release(); |
| 3945 | mSoundPool = null; |
| 3946 | return false; |
| 3947 | } |
| 3948 | /* |
| 3949 | * poolId table: The value -1 in this table indicates that corresponding |
| 3950 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 3951 | * Once loaded, the value in poolId is the sample ID and the same |
| 3952 | * sample can be reused for another effect using the same file. |
| 3953 | */ |
| 3954 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3955 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3956 | poolId[fileIdx] = -1; |
| 3957 | } |
| 3958 | /* |
| 3959 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 3960 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 3961 | * this indicates we have a valid sample loaded for this effect. |
| 3962 | */ |
| 3963 | |
| 3964 | int numSamples = 0; |
| 3965 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3966 | // Do not load sample if this effect uses the MediaPlayer |
| 3967 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 3968 | continue; |
| 3969 | } |
| 3970 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 3971 | String filePath = Environment.getRootDirectory() |
| 3972 | + SOUND_EFFECTS_PATH |
| 3973 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 3974 | int sampleId = mSoundPool.load(filePath, 0); |
| 3975 | if (sampleId <= 0) { |
| 3976 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 3977 | } else { |
| 3978 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 3979 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 3980 | numSamples++; |
| 3981 | } |
| 3982 | } else { |
| 3983 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 3984 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 3985 | } |
| 3986 | } |
| 3987 | // wait for all samples to be loaded |
| 3988 | if (numSamples > 0) { |
| 3989 | mSoundPoolCallBack.setSamples(poolId); |
| 3990 | |
| 3991 | attempts = 3; |
| 3992 | status = 1; |
| 3993 | while ((status == 1) && (attempts-- > 0)) { |
| 3994 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3995 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3996 | status = mSoundPoolCallBack.status(); |
| 3997 | } catch (InterruptedException e) { |
| 3998 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 3999 | } |
| 4000 | } |
| 4001 | } else { |
| 4002 | status = -1; |
| 4003 | } |
| 4004 | |
| 4005 | if (mSoundPoolLooper != null) { |
| 4006 | mSoundPoolLooper.quit(); |
| 4007 | mSoundPoolLooper = null; |
| 4008 | } |
| 4009 | mSoundPoolListenerThread = null; |
| 4010 | if (status != 0) { |
| 4011 | Log.w(TAG, |
| 4012 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4013 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4014 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4015 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | mSoundPool.release(); |
| 4020 | mSoundPool = null; |
| 4021 | } |
| 4022 | } |
| 4023 | return (status == 0); |
| 4024 | } |
| 4025 | |
| 4026 | /** |
| 4027 | * Unloads samples from the sound pool. |
| 4028 | * This method can be called to free some memory when |
| 4029 | * sound effects are disabled. |
| 4030 | */ |
| 4031 | private void onUnloadSoundEffects() { |
| 4032 | synchronized (mSoundEffectsLock) { |
| 4033 | if (mSoundPool == null) { |
| 4034 | return; |
| 4035 | } |
| 4036 | |
| 4037 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4038 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4039 | poolId[fileIdx] = 0; |
| 4040 | } |
| 4041 | |
| 4042 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4043 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4044 | continue; |
| 4045 | } |
| 4046 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4047 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4048 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4049 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4050 | } |
| 4051 | } |
| 4052 | mSoundPool.release(); |
| 4053 | mSoundPool = null; |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4058 | synchronized (mSoundEffectsLock) { |
| 4059 | |
| 4060 | onLoadSoundEffects(); |
| 4061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4062 | if (mSoundPool == null) { |
| 4063 | return; |
| 4064 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4065 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4066 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4067 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4068 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4069 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4070 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4071 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4072 | |
| 4073 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4074 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4075 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4076 | } else { |
| 4077 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4078 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 4079 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 4080 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4081 | mediaPlayer.setDataSource(filePath); |
| 4082 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4083 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4084 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4085 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4086 | public void onCompletion(MediaPlayer mp) { |
| 4087 | cleanupPlayer(mp); |
| 4088 | } |
| 4089 | }); |
| 4090 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4091 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4092 | cleanupPlayer(mp); |
| 4093 | return true; |
| 4094 | } |
| 4095 | }); |
| 4096 | mediaPlayer.start(); |
| 4097 | } catch (IOException ex) { |
| 4098 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4099 | } catch (IllegalArgumentException ex) { |
| 4100 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4101 | } catch (IllegalStateException ex) { |
| 4102 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | private void cleanupPlayer(MediaPlayer mp) { |
| 4109 | if (mp != null) { |
| 4110 | try { |
| 4111 | mp.stop(); |
| 4112 | mp.release(); |
| 4113 | } catch (IllegalStateException ex) { |
| 4114 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4115 | } |
| 4116 | } |
| 4117 | } |
| 4118 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4119 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4120 | synchronized (mConnectedDevices) { |
| 4121 | setForceUseInt_SyncDevices(usage, config); |
| 4122 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4125 | private void onPersistSafeVolumeState(int state) { |
| 4126 | Settings.Global.putInt(mContentResolver, |
| 4127 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4128 | state); |
| 4129 | } |
| 4130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4131 | @Override |
| 4132 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4133 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4134 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4135 | case MSG_SET_DEVICE_VOLUME: |
| 4136 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4137 | break; |
| 4138 | |
| 4139 | case MSG_SET_ALL_VOLUMES: |
| 4140 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | break; |
| 4142 | |
| 4143 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4144 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4145 | break; |
| 4146 | |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 4147 | case MSG_PERSIST_MASTER_VOLUME: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4148 | if (mUseFixedVolume) { |
| 4149 | return; |
| 4150 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4151 | Settings.System.putFloatForUser(mContentResolver, |
| 4152 | Settings.System.VOLUME_MASTER, |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4153 | msg.arg1 / (float)1000.0, |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4154 | UserHandle.USER_CURRENT); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 4155 | break; |
| 4156 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4157 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4158 | if (mUseFixedVolume) { |
| 4159 | return; |
| 4160 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4161 | Settings.System.putIntForUser(mContentResolver, |
| 4162 | Settings.System.VOLUME_MASTER_MUTE, |
| 4163 | msg.arg1, |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4164 | msg.arg2); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4165 | break; |
| 4166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4167 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4168 | // note that the value persisted is the current ringer mode, not the |
| 4169 | // 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] | 4170 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4171 | break; |
| 4172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4173 | case MSG_MEDIA_SERVER_DIED: |
Eric Laurent | eb4b8a2 | 2014-07-21 13:10:07 -0700 | [diff] [blame] | 4174 | if (!mSystemReady || |
| 4175 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4176 | Log.e(TAG, "Media server died."); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4177 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4178 | null, 500); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4179 | break; |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4180 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4181 | Log.e(TAG, "Media server started."); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4182 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4183 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 4184 | // server crash |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4185 | // Note that we only execute this when the media server |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4186 | // process restarts after a crash, not the first time it is started. |
| 4187 | AudioSystem.setParameters("restarting=true"); |
| 4188 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 4189 | readAndSetLowRamDevice(); |
| 4190 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4191 | // Restore device connection states |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4192 | synchronized (mConnectedDevices) { |
| 4193 | Set set = mConnectedDevices.entrySet(); |
| 4194 | Iterator i = set.iterator(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4195 | while (i.hasNext()) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4196 | Map.Entry device = (Map.Entry)i.next(); |
| 4197 | AudioSystem.setDeviceConnectionState( |
| 4198 | ((Integer)device.getKey()).intValue(), |
| 4199 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4200 | (String)device.getValue(), |
| 4201 | "unknown-device"); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4202 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4203 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4204 | // Restore call state |
| 4205 | AudioSystem.setPhoneState(mMode); |
| 4206 | |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4207 | // Restore forced usage for communcations and record |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4208 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4209 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4210 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 4211 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4212 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4213 | // Restore stream volumes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4215 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4216 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4217 | AudioSystem.initStreamVolume(streamType, 0, (streamState.mIndexMax + 5) / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4218 | |
| 4219 | streamState.applyAllVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4220 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4221 | |
| 4222 | // Restore ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4223 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4224 | |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 4225 | // Restore master volume |
| 4226 | restoreMasterVolume(); |
| 4227 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4228 | // Reset device orientation (if monitored for this device) |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4229 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4230 | setOrientationForAudioSystem(); |
| 4231 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4232 | if (mMonitorRotation) { |
| 4233 | setRotationForAudioSystem(); |
| 4234 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4235 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4236 | synchronized (mBluetoothA2dpEnabledLock) { |
| 4237 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 4238 | mBluetoothA2dpEnabled ? |
| 4239 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 4240 | } |
Eric Laurent | bff5ca5 | 2012-11-02 16:48:26 -0700 | [diff] [blame] | 4241 | |
| 4242 | synchronized (mSettingsLock) { |
| 4243 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 4244 | mDockAudioMediaEnabled ? |
| 4245 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 4246 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4247 | if (mHdmiManager != null) { |
| 4248 | synchronized (mHdmiManager) { |
| 4249 | if (mHdmiTvClient != null) { |
| 4250 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 4251 | } |
| 4252 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 4253 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 4254 | |
| 4255 | synchronized (mAudioPolicies) { |
| 4256 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 4257 | policy.connectMixes(); |
| 4258 | } |
| 4259 | } |
| 4260 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4261 | // indicate the end of reconfiguration phase to audio HAL |
| 4262 | AudioSystem.setParameters("restarting=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4263 | break; |
| 4264 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4265 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4266 | onUnloadSoundEffects(); |
| 4267 | break; |
| 4268 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4269 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4270 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4271 | // can take several dozens of milliseconds to complete |
| 4272 | boolean loaded = onLoadSoundEffects(); |
| 4273 | if (msg.obj != null) { |
| 4274 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4275 | synchronized (reply) { |
| 4276 | reply.mStatus = loaded ? 0 : -1; |
| 4277 | reply.notify(); |
| 4278 | } |
| 4279 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4280 | break; |
| 4281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4282 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4283 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4284 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4285 | |
| 4286 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4287 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4288 | synchronized (mConnectedDevices) { |
| 4289 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4290 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4291 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4292 | |
| 4293 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4294 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4295 | setForceUse(msg.arg1, msg.arg2); |
| 4296 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4297 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4298 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4299 | resetBluetoothSco(); |
| 4300 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4301 | |
| 4302 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4303 | { WiredDeviceConnectionState connectState = |
| 4304 | (WiredDeviceConnectionState)msg.obj; |
| 4305 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
| 4306 | connectState.mAddress, connectState.mName); |
| 4307 | mAudioEventWakeLock.release(); |
| 4308 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4309 | break; |
| 4310 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4311 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4312 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4313 | mAudioEventWakeLock.release(); |
| 4314 | break; |
| 4315 | |
| 4316 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4317 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4318 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4319 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4320 | |
| 4321 | case MSG_REPORT_NEW_ROUTES: { |
| 4322 | int N = mRoutesObservers.beginBroadcast(); |
| 4323 | if (N > 0) { |
| 4324 | AudioRoutesInfo routes; |
| 4325 | synchronized (mCurAudioRoutes) { |
| 4326 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4327 | } |
| 4328 | while (N > 0) { |
| 4329 | N--; |
| 4330 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4331 | try { |
| 4332 | obs.dispatchAudioRoutesChanged(routes); |
| 4333 | } catch (RemoteException e) { |
| 4334 | } |
| 4335 | } |
| 4336 | } |
| 4337 | mRoutesObservers.finishBroadcast(); |
| 4338 | break; |
| 4339 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4340 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4341 | case MSG_CHECK_MUSIC_ACTIVE: |
| 4342 | onCheckMusicActive(); |
| 4343 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4344 | |
| 4345 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4346 | onSendBecomingNoisyIntent(); |
| 4347 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4348 | |
| 4349 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4350 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
| 4351 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED)); |
| 4352 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4353 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4354 | onPersistSafeVolumeState(msg.arg1); |
| 4355 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4356 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4357 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4358 | onBroadcastScoConnectionState(msg.arg1); |
| 4359 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4360 | |
| 4361 | case MSG_SYSTEM_READY: |
| 4362 | onSystemReady(); |
| 4363 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4364 | |
| 4365 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4366 | final int musicActiveMs = msg.arg1; |
| 4367 | Settings.Secure.putIntForUser(mContentResolver, |
| 4368 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4369 | UserHandle.USER_CURRENT); |
| 4370 | break; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4371 | case MSG_PERSIST_MICROPHONE_MUTE: |
| 4372 | Settings.System.putIntForUser(mContentResolver, |
| 4373 | Settings.System.MICROPHONE_MUTE, |
| 4374 | msg.arg1, |
| 4375 | msg.arg2); |
| 4376 | break; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4377 | case MSG_UNMUTE_STREAM: |
| 4378 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4379 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4384 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4385 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4386 | SettingsObserver() { |
| 4387 | super(new Handler()); |
| 4388 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4389 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4390 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4391 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | @Override |
| 4395 | public void onChange(boolean selfChange) { |
| 4396 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4397 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4398 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4399 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4400 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4401 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4402 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4403 | /* |
| 4404 | * Ensure all stream types that should be affected by ringer mode |
| 4405 | * are in the proper state. |
| 4406 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4407 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4408 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4409 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4410 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4411 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4412 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4413 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4414 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4415 | private void makeA2dpDeviceAvailable(String address) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4416 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4417 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4418 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4419 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4420 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4421 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4422 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4423 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4424 | address, |
| 4425 | "a2dp-device"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4426 | // Reset A2DP suspend state each time a new sink is connected |
| 4427 | AudioSystem.setParameters("A2dpSuspended=false"); |
| 4428 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP), |
| 4429 | address); |
| 4430 | } |
| 4431 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4432 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4433 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4434 | } |
| 4435 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4436 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4437 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4438 | synchronized (mA2dpAvrcpLock) { |
| 4439 | mAvrcpAbsVolSupported = false; |
| 4440 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4441 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4442 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4443 | address, |
| 4444 | "a2dp-device"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4445 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4446 | synchronized (mCurAudioRoutes) { |
| 4447 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4448 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4449 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4450 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4451 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4452 | } |
| 4453 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4456 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4457 | private void makeA2dpDeviceUnavailableLater(String address) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4458 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4459 | // reconnection of the sink. |
| 4460 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4461 | // the device will be made unavailable later, so consider it disconnected right away |
| 4462 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
| 4463 | // send the delayed message to make the device unavailable later |
| 4464 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
| 4465 | mAudioHandler.sendMessageDelayed(msg, BTA2DP_DOCK_TIMEOUT_MILLIS); |
| 4466 | |
| 4467 | } |
| 4468 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4469 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4470 | private void makeA2dpSrcAvailable(String address) { |
| 4471 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4472 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4473 | address, |
| 4474 | "a2dp-device"); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4475 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP), |
| 4476 | address); |
| 4477 | } |
| 4478 | |
| 4479 | // must be called synchronized on mConnectedDevices |
| 4480 | private void makeA2dpSrcUnavailable(String address) { |
| 4481 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4482 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4483 | address, |
| 4484 | "a2dp-device"); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4485 | mConnectedDevices.remove(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP); |
| 4486 | } |
| 4487 | |
| 4488 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4489 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4490 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4491 | } |
| 4492 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4493 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4494 | private boolean hasScheduledA2dpDockTimeout() { |
| 4495 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4496 | } |
| 4497 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4498 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4499 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4500 | if (DEBUG_VOL) { |
| 4501 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4502 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4503 | if (btDevice == null) { |
| 4504 | return; |
| 4505 | } |
| 4506 | String address = btDevice.getAddress(); |
| 4507 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4508 | address = ""; |
| 4509 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4510 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4511 | synchronized (mConnectedDevices) { |
| 4512 | boolean isConnected = |
| 4513 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) && |
| 4514 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP).equals(address)); |
| 4515 | |
| 4516 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4517 | if (btDevice.isBluetoothDock()) { |
| 4518 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4519 | // introduction of a delay for transient disconnections of docks when |
| 4520 | // power is rapidly turned off/on, this message will be canceled if |
| 4521 | // we reconnect the dock under a preset delay |
| 4522 | makeA2dpDeviceUnavailableLater(address); |
| 4523 | // the next time isConnected is evaluated, it will be false for the dock |
| 4524 | } |
| 4525 | } else { |
| 4526 | makeA2dpDeviceUnavailableNow(address); |
| 4527 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4528 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4529 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4530 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4531 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4532 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4533 | } |
| 4534 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4535 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4536 | if (btDevice.isBluetoothDock()) { |
| 4537 | // this could be a reconnection after a transient disconnection |
| 4538 | cancelA2dpDeviceTimeout(); |
| 4539 | mDockAddress = address; |
| 4540 | } else { |
| 4541 | // this could be a connection of another A2DP device before the timeout of |
| 4542 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4543 | if(hasScheduledA2dpDockTimeout()) { |
| 4544 | cancelA2dpDeviceTimeout(); |
| 4545 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4546 | } |
| 4547 | } |
| 4548 | makeA2dpDeviceAvailable(address); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4549 | synchronized (mCurAudioRoutes) { |
| 4550 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4551 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4552 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4553 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4554 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4555 | } |
| 4556 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | } |
| 4560 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4561 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4562 | { |
| 4563 | if (DEBUG_VOL) { |
| 4564 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4565 | } |
| 4566 | if (btDevice == null) { |
| 4567 | return; |
| 4568 | } |
| 4569 | String address = btDevice.getAddress(); |
| 4570 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4571 | address = ""; |
| 4572 | } |
| 4573 | |
| 4574 | synchronized (mConnectedDevices) { |
| 4575 | boolean isConnected = |
| 4576 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) && |
| 4577 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP).equals(address)); |
| 4578 | |
| 4579 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4580 | makeA2dpSrcUnavailable(address); |
| 4581 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4582 | makeA2dpSrcAvailable(address); |
| 4583 | } |
| 4584 | } |
| 4585 | } |
| 4586 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4587 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4588 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4589 | synchronized (mA2dpAvrcpLock) { |
| 4590 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4591 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4592 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4593 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4594 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4595 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4596 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4597 | } |
| 4598 | } |
| 4599 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4600 | private boolean handleDeviceConnection(boolean connect, int device, String address, String deviceName) { |
| 4601 | Slog.i(TAG, "handleDeviceConnection(" + connect + |
| 4602 | " dev:" + Integer.toHexString(device) + |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4603 | " address:" + address + |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4604 | " name:" + deviceName + ")"); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4605 | synchronized (mConnectedDevices) { |
| 4606 | boolean isConnected = (mConnectedDevices.containsKey(device) && |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4607 | (address.isEmpty() || mConnectedDevices.get(device).equals(address))); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4608 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4609 | if (isConnected && !connect) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4610 | AudioSystem.setDeviceConnectionState(device, |
| 4611 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4612 | address, deviceName); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4613 | mConnectedDevices.remove(device); |
| 4614 | return true; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4615 | } else if (!isConnected && connect) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4616 | AudioSystem.setDeviceConnectionState(device, |
| 4617 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4618 | address, deviceName); |
| 4619 | mConnectedDevices.put(new Integer(device), address); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4620 | return true; |
| 4621 | } |
| 4622 | } |
| 4623 | return false; |
| 4624 | } |
| 4625 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4626 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4627 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4628 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4629 | int mBecomingNoisyIntentDevices = |
| 4630 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4631 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4632 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4633 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4634 | |
| 4635 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4636 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4637 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4638 | int delay = 0; |
| 4639 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4640 | int devices = 0; |
| 4641 | for (int dev : mConnectedDevices.keySet()) { |
Eric Laurent | 27c30e4 | 2014-08-27 12:36:33 -0700 | [diff] [blame] | 4642 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) && |
| 4643 | ((dev & mBecomingNoisyIntentDevices) != 0)) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4644 | devices |= dev; |
| 4645 | } |
| 4646 | } |
| 4647 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4648 | sendMsg(mAudioHandler, |
| 4649 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4650 | SENDMSG_REPLACE, |
| 4651 | 0, |
| 4652 | 0, |
| 4653 | null, |
| 4654 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4655 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4656 | } |
| 4657 | } |
| 4658 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4659 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4660 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4661 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4662 | synchronized (mLastDeviceConnectMsgTime) { |
| 4663 | long time = SystemClock.uptimeMillis(); |
| 4664 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 4665 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4666 | } |
| 4667 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4668 | } |
| 4669 | return delay; |
| 4670 | } |
| 4671 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4672 | private void sendDeviceConnectionIntent(int device, int state, String address, String deviceName) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4673 | { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4674 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 4675 | " state:0x" + Integer.toHexString(state) + |
| 4676 | " address:" + address + |
| 4677 | " name:" + deviceName + ");"); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4678 | Intent intent = new Intent(); |
| 4679 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4680 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 4681 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 4682 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 4683 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4684 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4685 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4686 | int connType = 0; |
| 4687 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4688 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4689 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4690 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4691 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4692 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 4693 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 4694 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4695 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4696 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4697 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4698 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 4699 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4700 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4701 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4702 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 4703 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4706 | synchronized (mCurAudioRoutes) { |
| 4707 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4708 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4709 | if (state != 0) { |
| 4710 | newConn |= connType; |
| 4711 | } else { |
| 4712 | newConn &= ~connType; |
| 4713 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4714 | if (newConn != mCurAudioRoutes.mainType) { |
| 4715 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4716 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4717 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4722 | final long ident = Binder.clearCallingIdentity(); |
| 4723 | try { |
| 4724 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4725 | } finally { |
| 4726 | Binder.restoreCallingIdentity(ident); |
| 4727 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4730 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
| 4731 | String deviceName) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4732 | { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4733 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 4734 | + " state:" + Integer.toHexString(state) |
| 4735 | + " address:" + address |
| 4736 | + " deviceName:" + deviceName + ");"); |
| 4737 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4738 | synchronized (mConnectedDevices) { |
| 4739 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4740 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4741 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4742 | setBluetoothA2dpOnInt(true); |
| 4743 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4744 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4745 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4746 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4747 | handleDeviceConnection(state == 1, device, address, deviceName); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4748 | if (state != 0) { |
| 4749 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4750 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4751 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4752 | setBluetoothA2dpOnInt(false); |
| 4753 | } |
| 4754 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4755 | sendMsg(mAudioHandler, |
| 4756 | MSG_CHECK_MUSIC_ACTIVE, |
| 4757 | SENDMSG_REPLACE, |
| 4758 | 0, |
| 4759 | 0, |
| 4760 | null, |
| 4761 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4762 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4763 | // Television devices without CEC service apply software volume on HDMI output |
| 4764 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4765 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 4766 | checkAllFixedVolumeDevices(); |
| 4767 | if (mHdmiManager != null) { |
| 4768 | synchronized (mHdmiManager) { |
| 4769 | if (mHdmiPlaybackClient != null) { |
| 4770 | mHdmiCecSink = false; |
| 4771 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | } else { |
| 4777 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4778 | if (mHdmiManager != null) { |
| 4779 | synchronized (mHdmiManager) { |
| 4780 | mHdmiCecSink = false; |
| 4781 | } |
| 4782 | } |
| 4783 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4784 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4785 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 4786 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4787 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4791 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4792 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 4793 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4794 | if (state == 1) { |
| 4795 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 4796 | int[] portGeneration = new int[1]; |
| 4797 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 4798 | if (status == AudioManager.SUCCESS) { |
| 4799 | for (AudioPort port : ports) { |
| 4800 | if (port instanceof AudioDevicePort) { |
| 4801 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4802 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 4803 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4804 | // format the list of supported encodings |
| 4805 | int[] formats = devicePort.formats(); |
| 4806 | if (formats.length > 0) { |
| 4807 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 4808 | for (int format : formats) { |
| 4809 | // a format in the list can be 0, skip it |
| 4810 | if (format != AudioFormat.ENCODING_INVALID) { |
| 4811 | encodingList.add(format); |
| 4812 | } |
| 4813 | } |
| 4814 | int[] encodingArray = new int[encodingList.size()]; |
| 4815 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 4816 | encodingArray[i] = encodingList.get(i); |
| 4817 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4818 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4819 | } |
| 4820 | // find the maximum supported number of channels |
| 4821 | int maxChannels = 0; |
| 4822 | for (int mask : devicePort.channelMasks()) { |
| 4823 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 4824 | if (channelCount > maxChannels) { |
| 4825 | maxChannels = channelCount; |
| 4826 | } |
| 4827 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4828 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4836 | /* cache of the address of the last dock the device was connected to */ |
| 4837 | private String mDockAddress; |
| 4838 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4839 | /** |
| 4840 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 4841 | */ |
| 4842 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 4843 | @Override |
| 4844 | public void onReceive(Context context, Intent intent) { |
| 4845 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4846 | int outDevice; |
| 4847 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4848 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4849 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4850 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 4851 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4852 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4853 | int config; |
| 4854 | switch (dockState) { |
| 4855 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 4856 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 4857 | break; |
| 4858 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 4859 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 4860 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4861 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4862 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4863 | break; |
| 4864 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 4865 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 4866 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4867 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 4868 | default: |
| 4869 | config = AudioSystem.FORCE_NONE; |
| 4870 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4871 | // Low end docks have a menu to enable or disable audio |
| 4872 | // (see mDockAudioMediaEnabled) |
| 4873 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 4874 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 4875 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 4876 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 4877 | } |
| 4878 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4879 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4880 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4881 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4882 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 4883 | inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Nick Pelly | a56d1c7 | 2009-08-19 14:49:29 -0700 | [diff] [blame] | 4884 | String address = null; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4885 | |
| 4886 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 4887 | if (btDevice == null) { |
| 4888 | return; |
| 4889 | } |
| 4890 | |
| 4891 | address = btDevice.getAddress(); |
| 4892 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 4893 | if (btClass != null) { |
| 4894 | switch (btClass.getDeviceClass()) { |
| 4895 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 4896 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4897 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4898 | break; |
| 4899 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4900 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4901 | break; |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4905 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4906 | address = ""; |
| 4907 | } |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4908 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4909 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4910 | boolean success = |
| 4911 | handleDeviceConnection(connected, outDevice, address, "Bluetooth Headset") && |
| 4912 | handleDeviceConnection(connected, inDevice, address, "Bluetooth Headset"); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4913 | if (success) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4914 | synchronized (mScoClients) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4915 | if (connected) { |
| 4916 | mBluetoothHeadsetDevice = btDevice; |
| 4917 | } else { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4918 | mBluetoothHeadsetDevice = null; |
| 4919 | resetBluetoothSco(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4920 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4921 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4922 | } |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4923 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4924 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4925 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4926 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4927 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4928 | // broadcast intent if the connection was initated by AudioService |
| 4929 | if (!mScoClients.isEmpty() && |
| 4930 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 4931 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 4932 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4933 | broadcast = true; |
| 4934 | } |
| 4935 | switch (btState) { |
| 4936 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4937 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4938 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4939 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4940 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4941 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4942 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4943 | break; |
| 4944 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4945 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4946 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 4947 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4948 | break; |
| 4949 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4950 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4951 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4952 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4953 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4954 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4955 | default: |
| 4956 | // do not broadcast CONNECTING or invalid state |
| 4957 | broadcast = false; |
| 4958 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4959 | } |
| 4960 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4961 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4962 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4963 | //FIXME: this is to maintain compatibility with deprecated intent |
| 4964 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4965 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4966 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4967 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4968 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4969 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4970 | if (mMonitorRotation) { |
| 4971 | mOrientationListener.onOrientationChanged(0); //argument is ignored anyway |
| 4972 | mOrientationListener.enable(); |
| 4973 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4974 | AudioSystem.setParameters("screen_state=on"); |
| 4975 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4976 | if (mMonitorRotation) { |
| 4977 | //reduce wakeups (save current) by only listening when display is on |
| 4978 | mOrientationListener.disable(); |
| 4979 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4980 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4981 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4982 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4983 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4984 | // attempt to stop music playback for background user |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4985 | sendMsg(mAudioHandler, |
| 4986 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4987 | SENDMSG_REPLACE, |
| 4988 | 0, |
| 4989 | 0, |
| 4990 | null, |
| 4991 | 0); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4992 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4993 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4994 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4995 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4996 | readAudioSettings(true /*userSwitch*/); |
| 4997 | // preserve STREAM_MUSIC volume from one user to the next. |
| 4998 | sendMsg(mAudioHandler, |
| 4999 | MSG_SET_ALL_VOLUMES, |
| 5000 | SENDMSG_QUEUE, |
| 5001 | 0, |
| 5002 | 0, |
| 5003 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5004 | } |
| 5005 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5006 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5007 | |
| 5008 | //========================================================================================== |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5009 | // RemoteControlDisplay / RemoteControlClient / Remote info |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5010 | //========================================================================================== |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 5011 | public boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, |
| 5012 | ComponentName listenerComp) { |
| 5013 | return mMediaFocusControl.registerRemoteController(rcd, w, h, listenerComp); |
| 5014 | } |
| 5015 | |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 5016 | public boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 5017 | return mMediaFocusControl.registerRemoteControlDisplay(rcd, w, h); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 5018 | } |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 5019 | |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5020 | public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5021 | mMediaFocusControl.unregisterRemoteControlDisplay(rcd); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5025 | mMediaFocusControl.remoteControlDisplayUsesBitmapSize(rcd, w, h); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5026 | } |
| 5027 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 5028 | public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, |
| 5029 | boolean wantsSync) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5030 | mMediaFocusControl.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync); |
| 5031 | } |
| 5032 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5033 | @Override |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5034 | public void setRemoteStreamVolume(int index) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5035 | enforceVolumeController("set the remote stream volume"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5036 | mMediaFocusControl.setRemoteStreamVolume(index); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5037 | } |
| 5038 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5039 | //========================================================================================== |
| 5040 | // Audio Focus |
| 5041 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5042 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5043 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5044 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5045 | // permission checks |
| 5046 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5047 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5048 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 5049 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 5050 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 5051 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5052 | } |
| 5053 | } else { |
| 5054 | // only a registered audio policy can be used to lock focus |
| 5055 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5056 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5057 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5058 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | } |
| 5063 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5064 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 5065 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5068 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 5069 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | public void unregisterAudioFocusClient(String clientId) { |
| 5073 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5074 | } |
| 5075 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 5076 | public int getCurrentAudioFocus() { |
| 5077 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 5078 | } |
| 5079 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5080 | private boolean readCameraSoundForced() { |
| 5081 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 5082 | mContext.getResources().getBoolean( |
| 5083 | com.android.internal.R.bool.config_camera_sound_forced); |
| 5084 | } |
| 5085 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5086 | //========================================================================================== |
| 5087 | // Device orientation |
| 5088 | //========================================================================================== |
| 5089 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5090 | * Handles device configuration changes that may map to a change in the orientation |
| 5091 | * or orientation. |
| 5092 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 5093 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5094 | */ |
| 5095 | private void handleConfigurationChanged(Context context) { |
| 5096 | try { |
| 5097 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 5098 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5099 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5100 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5101 | if (mMonitorOrientation) { |
| 5102 | int newOrientation = config.orientation; |
| 5103 | if (newOrientation != mDeviceOrientation) { |
| 5104 | mDeviceOrientation = newOrientation; |
| 5105 | setOrientationForAudioSystem(); |
| 5106 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5107 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5108 | sendMsg(mAudioHandler, |
| 5109 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 5110 | SENDMSG_REPLACE, |
| 5111 | 0, |
| 5112 | 0, |
| 5113 | null, |
| 5114 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5115 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5116 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5117 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5118 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5119 | synchronized (mCameraSoundForced) { |
| 5120 | if (cameraSoundForced != mCameraSoundForced) { |
| 5121 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5122 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5125 | if (cameraSoundForcedChanged) { |
| 5126 | if (!isPlatformTelevision()) { |
| 5127 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 5128 | if (cameraSoundForced) { |
| 5129 | s.setAllIndexesToMax(); |
| 5130 | mRingerModeAffectedStreams &= |
| 5131 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5132 | } else { |
| 5133 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM]); |
| 5134 | mRingerModeAffectedStreams |= |
| 5135 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5136 | } |
| 5137 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5138 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | sendMsg(mAudioHandler, |
| 5142 | MSG_SET_FORCE_USE, |
| 5143 | SENDMSG_QUEUE, |
| 5144 | AudioSystem.FOR_SYSTEM, |
| 5145 | cameraSoundForced ? |
| 5146 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 5147 | null, |
| 5148 | 0); |
| 5149 | |
| 5150 | sendMsg(mAudioHandler, |
| 5151 | MSG_SET_ALL_VOLUMES, |
| 5152 | SENDMSG_QUEUE, |
| 5153 | 0, |
| 5154 | 0, |
| 5155 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 5156 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5157 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5158 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5159 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5160 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | private void setOrientationForAudioSystem() { |
| 5165 | switch (mDeviceOrientation) { |
| 5166 | case Configuration.ORIENTATION_LANDSCAPE: |
| 5167 | //Log.i(TAG, "orientation is landscape"); |
| 5168 | AudioSystem.setParameters("orientation=landscape"); |
| 5169 | break; |
| 5170 | case Configuration.ORIENTATION_PORTRAIT: |
| 5171 | //Log.i(TAG, "orientation is portrait"); |
| 5172 | AudioSystem.setParameters("orientation=portrait"); |
| 5173 | break; |
| 5174 | case Configuration.ORIENTATION_SQUARE: |
| 5175 | //Log.i(TAG, "orientation is square"); |
| 5176 | AudioSystem.setParameters("orientation=square"); |
| 5177 | break; |
| 5178 | case Configuration.ORIENTATION_UNDEFINED: |
| 5179 | //Log.i(TAG, "orientation is undefined"); |
| 5180 | AudioSystem.setParameters("orientation=undefined"); |
| 5181 | break; |
| 5182 | default: |
| 5183 | Log.e(TAG, "Unknown orientation"); |
| 5184 | } |
| 5185 | } |
| 5186 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5187 | private void setRotationForAudioSystem() { |
| 5188 | switch (mDeviceRotation) { |
| 5189 | case Surface.ROTATION_0: |
| 5190 | AudioSystem.setParameters("rotation=0"); |
| 5191 | break; |
| 5192 | case Surface.ROTATION_90: |
| 5193 | AudioSystem.setParameters("rotation=90"); |
| 5194 | break; |
| 5195 | case Surface.ROTATION_180: |
| 5196 | AudioSystem.setParameters("rotation=180"); |
| 5197 | break; |
| 5198 | case Surface.ROTATION_270: |
| 5199 | AudioSystem.setParameters("rotation=270"); |
| 5200 | break; |
| 5201 | default: |
| 5202 | Log.e(TAG, "Unknown device rotation"); |
| 5203 | } |
| 5204 | } |
| 5205 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5206 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5207 | // Handles request to override default use of A2DP for media. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5208 | // Must be called synchronized on mConnectedDevices |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5209 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5210 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5211 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5212 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5213 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5214 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5215 | } |
| 5216 | } |
| 5217 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5218 | // Must be called synchronized on mConnectedDevices |
| 5219 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5220 | switch (usage) { |
| 5221 | case AudioSystem.FOR_MEDIA: |
| 5222 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5223 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5224 | } else { // config == AudioSystem.FORCE_NONE |
| 5225 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5226 | } |
| 5227 | break; |
| 5228 | case AudioSystem.FOR_DOCK: |
| 5229 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5230 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5231 | } else { // config == AudioSystem.FORCE_NONE |
| 5232 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5233 | } |
| 5234 | break; |
| 5235 | default: |
| 5236 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5237 | } |
| 5238 | AudioSystem.setForceUse(usage, config); |
| 5239 | } |
| 5240 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5241 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5242 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5243 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5244 | mRingtonePlayer = player; |
| 5245 | } |
| 5246 | |
| 5247 | @Override |
| 5248 | public IRingtonePlayer getRingtonePlayer() { |
| 5249 | return mRingtonePlayer; |
| 5250 | } |
| 5251 | |
| 5252 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5253 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5254 | synchronized (mCurAudioRoutes) { |
| 5255 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5256 | mRoutesObservers.register(observer); |
| 5257 | return routes; |
| 5258 | } |
| 5259 | } |
| 5260 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5261 | |
| 5262 | //========================================================================================== |
| 5263 | // Safe media volume management. |
| 5264 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5265 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5266 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5267 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5268 | // property. Platforms with a different limit must set this property accordingly in their |
| 5269 | // overlay. |
| 5270 | //========================================================================================== |
| 5271 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5272 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5273 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5274 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5275 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5276 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5277 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5278 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5279 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5280 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5281 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5282 | private Integer mSafeMediaVolumeState; |
| 5283 | |
| 5284 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5285 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5286 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5287 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5288 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5289 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5290 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5291 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5292 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5293 | private int mMusicActiveMs; |
| 5294 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5295 | 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] | 5296 | 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] | 5297 | |
| 5298 | private void setSafeMediaVolumeEnabled(boolean on) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5299 | synchronized (mSafeMediaVolumeState) { |
| 5300 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5301 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5302 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5303 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 5304 | enforceSafeMediaVolume(); |
| 5305 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5306 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5307 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5308 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5309 | sendMsg(mAudioHandler, |
| 5310 | MSG_CHECK_MUSIC_ACTIVE, |
| 5311 | SENDMSG_REPLACE, |
| 5312 | 0, |
| 5313 | 0, |
| 5314 | null, |
| 5315 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5316 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5317 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5318 | } |
| 5319 | } |
| 5320 | |
| 5321 | private void enforceSafeMediaVolume() { |
| 5322 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5323 | int devices = mSafeMediaVolumeDevices; |
| 5324 | int i = 0; |
| 5325 | |
| 5326 | while (devices != 0) { |
| 5327 | int device = 1 << i++; |
| 5328 | if ((device & devices) == 0) { |
| 5329 | continue; |
| 5330 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5331 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5332 | if (index > mSafeMediaVolumeIndex) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5333 | streamState.setIndex(mSafeMediaVolumeIndex, device); |
| 5334 | sendMsg(mAudioHandler, |
| 5335 | MSG_SET_DEVICE_VOLUME, |
| 5336 | SENDMSG_QUEUE, |
| 5337 | device, |
| 5338 | 0, |
| 5339 | streamState, |
| 5340 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5341 | } |
| 5342 | devices &= ~device; |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5347 | synchronized (mSafeMediaVolumeState) { |
| 5348 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5349 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5350 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5351 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5352 | return false; |
| 5353 | } |
| 5354 | return true; |
| 5355 | } |
| 5356 | } |
| 5357 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5358 | @Override |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5359 | public void disableSafeMediaVolume() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5360 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5361 | synchronized (mSafeMediaVolumeState) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5362 | setSafeMediaVolumeEnabled(false); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5363 | if (mPendingVolumeCommand != null) { |
| 5364 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5365 | mPendingVolumeCommand.mIndex, |
| 5366 | mPendingVolumeCommand.mFlags, |
| 5367 | mPendingVolumeCommand.mDevice); |
| 5368 | mPendingVolumeCommand = null; |
| 5369 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5370 | } |
| 5371 | } |
| 5372 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5373 | //========================================================================================== |
| 5374 | // Hdmi Cec system audio mode. |
| 5375 | // If Hdmi Cec's system audio mode is on, audio service should notify volume change |
| 5376 | // to HdmiControlService so that audio recevier can handle volume change. |
| 5377 | //========================================================================================== |
| 5378 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5379 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5380 | public void onComplete(int status) { |
| 5381 | if (mHdmiManager != null) { |
| 5382 | synchronized (mHdmiManager) { |
| 5383 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5384 | // Television devices without CEC service apply software volume on HDMI output |
| 5385 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5386 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5387 | } |
| 5388 | checkAllFixedVolumeDevices(); |
| 5389 | } |
| 5390 | } |
| 5391 | } |
| 5392 | }; |
| 5393 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5394 | // If HDMI-CEC system audio is supported |
| 5395 | private boolean mHdmiSystemAudioSupported = false; |
| 5396 | // Set only when device is tv. |
| 5397 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5398 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5399 | // cached HdmiControlManager interface |
| 5400 | private HdmiControlManager mHdmiManager; |
| 5401 | // Set only when device is a set-top box. |
| 5402 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5403 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5404 | private boolean mHdmiCecSink; |
| 5405 | |
| 5406 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5407 | |
| 5408 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5409 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5410 | int device = AudioSystem.DEVICE_NONE; |
| 5411 | if (mHdmiManager != null) { |
| 5412 | synchronized (mHdmiManager) { |
| 5413 | if (mHdmiTvClient == null) { |
| 5414 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5415 | return device; |
| 5416 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5417 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5418 | synchronized (mHdmiTvClient) { |
| 5419 | if (mHdmiSystemAudioSupported != on) { |
| 5420 | mHdmiSystemAudioSupported = on; |
| 5421 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5422 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5423 | AudioSystem.FORCE_NONE); |
| 5424 | } |
| 5425 | device = AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC); |
| 5426 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5427 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5428 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5429 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5430 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5431 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5432 | @Override |
| 5433 | public boolean isHdmiSystemAudioSupported() { |
| 5434 | return mHdmiSystemAudioSupported; |
| 5435 | } |
| 5436 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5437 | //========================================================================================== |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5438 | // Accessibility: taking touch exploration into account for selecting the default |
| 5439 | // stream override timeout when adjusting volume |
| 5440 | //========================================================================================== |
| 5441 | private static class StreamOverride |
| 5442 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 5443 | |
| 5444 | // AudioService.getActiveStreamType() will return: |
| 5445 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5446 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5447 | // stopped |
| 5448 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 5000; |
| 5449 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
| 5450 | |
| 5451 | static int sDelayMs; |
| 5452 | |
| 5453 | static void init(Context ctxt) { |
| 5454 | AccessibilityManager accessibilityManager = |
| 5455 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5456 | updateDefaultStreamOverrideDelay( |
| 5457 | accessibilityManager.isTouchExplorationEnabled()); |
| 5458 | accessibilityManager.addTouchExplorationStateChangeListener( |
| 5459 | new StreamOverride()); |
| 5460 | } |
| 5461 | |
| 5462 | @Override |
| 5463 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5464 | updateDefaultStreamOverrideDelay(enabled); |
| 5465 | } |
| 5466 | |
| 5467 | private static void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5468 | if (touchExploreEnabled) { |
| 5469 | sDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5470 | } else { |
| 5471 | sDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5472 | } |
| 5473 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5474 | + " stream override delay is now " + sDelayMs + " ms"); |
| 5475 | } |
| 5476 | } |
| 5477 | |
| 5478 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5479 | // Camera shutter sound policy. |
| 5480 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5481 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5482 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5483 | //========================================================================================== |
| 5484 | |
| 5485 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5486 | private Boolean mCameraSoundForced; |
| 5487 | |
| 5488 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5489 | public boolean isCameraSoundForced() { |
| 5490 | synchronized (mCameraSoundForced) { |
| 5491 | return mCameraSoundForced; |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5496 | "SILENT", |
| 5497 | "VIBRATE", |
| 5498 | "NORMAL" |
| 5499 | }; |
| 5500 | |
| 5501 | private void dumpRingerMode(PrintWriter pw) { |
| 5502 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5503 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5504 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5505 | pw.print("- ringer mode affected streams = 0x"); |
| 5506 | pw.println(Integer.toHexString(mRingerModeAffectedStreams)); |
| 5507 | pw.print("- ringer mode muted streams = 0x"); |
| 5508 | pw.println(Integer.toHexString(mRingerModeMutedStreams)); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5509 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5512 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5513 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5514 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5515 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5516 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5517 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5518 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5519 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5520 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 5521 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5522 | |
| 5523 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5524 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5525 | pw.print(" mSafeMediaVolumeState="); |
| 5526 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5527 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5528 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5529 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5530 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5531 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5532 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5533 | pw.print(" mControllerService="); pw.println(mControllerService); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5534 | |
| 5535 | dumpAudioPolicies(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5539 | switch(state) { |
| 5540 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5541 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5542 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5543 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5544 | } |
| 5545 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5546 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5547 | |
| 5548 | // Inform AudioFlinger of our device's low RAM attribute |
| 5549 | private static void readAndSetLowRamDevice() |
| 5550 | { |
| 5551 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5552 | if (status != 0) { |
| 5553 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5554 | } |
| 5555 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5556 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5557 | private void enforceVolumeController(String action) { |
| 5558 | if (mControllerService.mUid != 0 && Binder.getCallingUid() == mControllerService.mUid) { |
| 5559 | return; |
| 5560 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5561 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5562 | "Only SystemUI can " + action); |
| 5563 | } |
| 5564 | |
| 5565 | @Override |
| 5566 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5567 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5568 | |
| 5569 | // return early if things are not actually changing |
| 5570 | if (mVolumeController.isSameBinder(controller)) { |
| 5571 | return; |
| 5572 | } |
| 5573 | |
| 5574 | // dismiss the old volume controller |
| 5575 | mVolumeController.postDismiss(); |
| 5576 | if (controller != null) { |
| 5577 | // we are about to register a new controller, listen for its death |
| 5578 | try { |
| 5579 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5580 | @Override |
| 5581 | public void binderDied() { |
| 5582 | if (mVolumeController.isSameBinder(controller)) { |
| 5583 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5584 | setVolumeController(null); |
| 5585 | } |
| 5586 | } |
| 5587 | }, 0); |
| 5588 | } catch (RemoteException e) { |
| 5589 | // noop |
| 5590 | } |
| 5591 | } |
| 5592 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5593 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5594 | } |
| 5595 | |
| 5596 | @Override |
| 5597 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5598 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5599 | |
| 5600 | // return early if the controller is not current |
| 5601 | if (!mVolumeController.isSameBinder(controller)) { |
| 5602 | return; |
| 5603 | } |
| 5604 | |
| 5605 | mVolumeController.setVisible(visible); |
| 5606 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5607 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5608 | |
| 5609 | public static class VolumeController { |
| 5610 | private static final String TAG = "VolumeController"; |
| 5611 | |
| 5612 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5613 | private boolean mVisible; |
| 5614 | private long mNextLongPress; |
| 5615 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5616 | |
| 5617 | public void setController(IVolumeController controller) { |
| 5618 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5619 | mVisible = false; |
| 5620 | } |
| 5621 | |
| 5622 | public void loadSettings(ContentResolver cr) { |
| 5623 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5624 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5625 | } |
| 5626 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5627 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5628 | if (isMute) { |
| 5629 | return false; |
| 5630 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5631 | boolean suppress = false; |
| 5632 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5633 | final long now = SystemClock.uptimeMillis(); |
| 5634 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5635 | // ui will become visible |
| 5636 | if (mNextLongPress < now) { |
| 5637 | mNextLongPress = now + mLongPressTimeout; |
| 5638 | } |
| 5639 | suppress = true; |
| 5640 | } else if (mNextLongPress > 0) { // in a long-press |
| 5641 | if (now > mNextLongPress) { |
| 5642 | // long press triggered, no more suppression |
| 5643 | mNextLongPress = 0; |
| 5644 | } else { |
| 5645 | // keep suppressing until the long press triggers |
| 5646 | suppress = true; |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | return suppress; |
| 5651 | } |
| 5652 | |
| 5653 | public void setVisible(boolean visible) { |
| 5654 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5655 | } |
| 5656 | |
| 5657 | public boolean isSameBinder(IVolumeController controller) { |
| 5658 | return Objects.equals(asBinder(), binder(controller)); |
| 5659 | } |
| 5660 | |
| 5661 | public IBinder asBinder() { |
| 5662 | return binder(mController); |
| 5663 | } |
| 5664 | |
| 5665 | private static IBinder binder(IVolumeController controller) { |
| 5666 | return controller == null ? null : controller.asBinder(); |
| 5667 | } |
| 5668 | |
| 5669 | @Override |
| 5670 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5671 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5675 | if (mController == null) |
| 5676 | return; |
| 5677 | try { |
| 5678 | mController.displaySafeVolumeWarning(flags); |
| 5679 | } catch (RemoteException e) { |
| 5680 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5681 | } |
| 5682 | } |
| 5683 | |
| 5684 | public void postVolumeChanged(int streamType, int flags) { |
| 5685 | if (mController == null) |
| 5686 | return; |
| 5687 | try { |
| 5688 | mController.volumeChanged(streamType, flags); |
| 5689 | } catch (RemoteException e) { |
| 5690 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | public void postMasterVolumeChanged(int flags) { |
| 5695 | if (mController == null) |
| 5696 | return; |
| 5697 | try { |
| 5698 | mController.masterVolumeChanged(flags); |
| 5699 | } catch (RemoteException e) { |
| 5700 | Log.w(TAG, "Error calling masterVolumeChanged", e); |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | public void postMasterMuteChanged(int flags) { |
| 5705 | if (mController == null) |
| 5706 | return; |
| 5707 | try { |
| 5708 | mController.masterMuteChanged(flags); |
| 5709 | } catch (RemoteException e) { |
| 5710 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | public void setLayoutDirection(int layoutDirection) { |
| 5715 | if (mController == null) |
| 5716 | return; |
| 5717 | try { |
| 5718 | mController.setLayoutDirection(layoutDirection); |
| 5719 | } catch (RemoteException e) { |
| 5720 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | public void postDismiss() { |
| 5725 | if (mController == null) |
| 5726 | return; |
| 5727 | try { |
| 5728 | mController.dismiss(); |
| 5729 | } catch (RemoteException e) { |
| 5730 | Log.w(TAG, "Error calling dismiss", e); |
| 5731 | } |
| 5732 | } |
| 5733 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5734 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5735 | /** |
| 5736 | * Interface for system components to get some extra functionality through |
| 5737 | * LocalServices. |
| 5738 | */ |
| 5739 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5740 | @Override |
| 5741 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 5742 | mRingerModeDelegate = delegate; |
| 5743 | if (mRingerModeDelegate != null) { |
| 5744 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 5745 | } |
| 5746 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 5747 | |
| 5748 | @Override |
| 5749 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 5750 | String callingPackage, int uid) { |
| 5751 | // direction and stream type swap here because the public |
| 5752 | // adjustSuggested has a different order than the other methods. |
| 5753 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, uid); |
| 5754 | } |
| 5755 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5756 | @Override |
| 5757 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 5758 | String callingPackage, int uid) { |
| 5759 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5760 | } |
| 5761 | |
| 5762 | @Override |
| 5763 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 5764 | String callingPackage, int uid) { |
| 5765 | setStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5766 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 5767 | |
| 5768 | @Override |
| 5769 | public void adjustMasterVolumeForUid(int steps, int flags, String callingPackage, |
| 5770 | int uid) { |
| 5771 | adjustMasterVolume(steps, flags, callingPackage, uid); |
| 5772 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5773 | |
| 5774 | @Override |
| 5775 | public int getRingerModeInternal() { |
| 5776 | return AudioService.this.getRingerModeInternal(); |
| 5777 | } |
| 5778 | |
| 5779 | @Override |
| 5780 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 5781 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 5782 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5783 | |
| 5784 | @Override |
| 5785 | public int getVolumeControllerUid() { |
| 5786 | return mControllerService.mUid; |
| 5787 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5790 | //========================================================================================== |
| 5791 | // Audio policy management |
| 5792 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5793 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 5794 | boolean hasFocusListener) { |
| 5795 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 5796 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5797 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5798 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5799 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5800 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5801 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5802 | if (!hasPermissionForPolicy) { |
| 5803 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 5804 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5805 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5806 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5807 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5808 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5809 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5810 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5811 | Slog.e(TAG, "Cannot re-register policy"); |
| 5812 | return null; |
| 5813 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5814 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 5815 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 5816 | regId = app.getRegistrationId(); |
| 5817 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5818 | } catch (RemoteException e) { |
| 5819 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5820 | 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] | 5821 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5822 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5823 | } |
| 5824 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5825 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5826 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5827 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5828 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 5829 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5830 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5831 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5832 | if (app == null) { |
| 5833 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 5834 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5835 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5836 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5837 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5838 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5839 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5840 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5841 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5842 | } |
| 5843 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5844 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 5845 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 5846 | + " policy " + pcb.asBinder()); |
| 5847 | // error handling |
| 5848 | boolean hasPermissionForPolicy = |
| 5849 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 5850 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5851 | if (!hasPermissionForPolicy) { |
| 5852 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 5853 | + Binder.getCallingPid() + " / uid " |
| 5854 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 5855 | return AudioManager.ERROR; |
| 5856 | } |
| 5857 | |
| 5858 | synchronized (mAudioPolicies) { |
| 5859 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5860 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 5861 | return AudioManager.ERROR; |
| 5862 | } |
| 5863 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 5864 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5865 | // is there already one policy managing ducking? |
| 5866 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5867 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5868 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 5869 | return AudioManager.ERROR; |
| 5870 | } |
| 5871 | } |
| 5872 | } |
| 5873 | app.mFocusDuckBehavior = duckingBehavior; |
| 5874 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 5875 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 5876 | } |
| 5877 | return AudioManager.SUCCESS; |
| 5878 | } |
| 5879 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5880 | private void dumpAudioPolicies(PrintWriter pw) { |
| 5881 | pw.println("\nAudio policies:"); |
| 5882 | synchronized (mAudioPolicies) { |
| 5883 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5884 | pw.println(policy.toLogFriendlyString()); |
| 5885 | } |
| 5886 | } |
| 5887 | } |
| 5888 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5889 | //====================== |
| 5890 | // Audio policy proxy |
| 5891 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5892 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5893 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 5894 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5895 | */ |
| 5896 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5897 | private static final String TAG = "AudioPolicyProxy"; |
| 5898 | AudioPolicyConfig mConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5899 | IAudioPolicyCallback mPolicyToken; |
| 5900 | boolean mHasFocusListener; |
| 5901 | /** |
| 5902 | * Audio focus ducking behavior for an audio policy. |
| 5903 | * This variable reflects the value that was successfully set in |
| 5904 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 5905 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 5906 | * is handling ducking for audio focus. |
| 5907 | */ |
| 5908 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 5909 | |
| 5910 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 5911 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5912 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5913 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5914 | mPolicyToken = token; |
| 5915 | mHasFocusListener = hasFocusListener; |
| 5916 | if (mHasFocusListener) { |
| 5917 | mMediaFocusControl.addFocusFollower(mPolicyToken); |
| 5918 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5919 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | public void binderDied() { |
| 5923 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5924 | Log.i(TAG, "audio policy " + mPolicyToken + " died"); |
| 5925 | release(); |
| 5926 | mAudioPolicies.remove(mPolicyToken.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5927 | } |
| 5928 | } |
| 5929 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5930 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5931 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5934 | void release() { |
| 5935 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5936 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 5937 | } |
| 5938 | if (mHasFocusListener) { |
| 5939 | mMediaFocusControl.removeFocusFollower(mPolicyToken); |
| 5940 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5941 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5944 | void connectMixes() { |
| 5945 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5946 | } |
| 5947 | }; |
| 5948 | |
| 5949 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 5950 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5951 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5952 | |
| 5953 | private class ControllerService extends ContentObserver { |
| 5954 | private int mUid; |
| 5955 | private ComponentName mComponent; |
| 5956 | |
| 5957 | public ControllerService() { |
| 5958 | super(null); |
| 5959 | } |
| 5960 | |
| 5961 | @Override |
| 5962 | public String toString() { |
| 5963 | return String.format("{mUid=%d,mComponent=%s}", mUid, mComponent); |
| 5964 | } |
| 5965 | |
| 5966 | public void init() { |
| 5967 | onChange(true); |
| 5968 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 5969 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT), false, this); |
| 5970 | } |
| 5971 | |
| 5972 | @Override |
| 5973 | public void onChange(boolean selfChange) { |
| 5974 | mUid = 0; |
| 5975 | mComponent = null; |
| 5976 | final String setting = Settings.Secure.getString(mContentResolver, |
| 5977 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT); |
| 5978 | if (setting == null) return; |
| 5979 | try { |
| 5980 | mComponent = ComponentName.unflattenFromString(setting); |
| 5981 | if (mComponent == null) return; |
| 5982 | mUid = mContext.getPackageManager() |
| 5983 | .getApplicationInfo(mComponent.getPackageName(), 0).uid; |
| 5984 | } catch (Exception e) { |
| 5985 | Log.w(TAG, "Error loading controller service", e); |
| 5986 | } |
| 5987 | if (DEBUG_VOL) Log.d(TAG, "Reloaded controller service: " + this); |
| 5988 | } |
| 5989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5990 | } |