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; |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 23 | import static android.os.Process.FIRST_APPLICATION_UID; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 24 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 25 | import android.Manifest; |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 27 | import android.app.ActivityManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.ActivityManagerNative; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 29 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 30 | import android.app.AppOpsManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothA2dp; |
| 32 | import android.bluetooth.BluetoothAdapter; |
| 33 | import android.bluetooth.BluetoothClass; |
| 34 | import android.bluetooth.BluetoothDevice; |
| 35 | import android.bluetooth.BluetoothHeadset; |
| 36 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 38 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.ContentResolver; |
| 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 45 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 46 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 47 | import android.content.res.Resources; |
| 48 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 49 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 50 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 51 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 52 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 53 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 54 | import android.media.AudioAttributes; |
| 55 | import android.media.AudioDevicePort; |
| 56 | import android.media.AudioSystem; |
| 57 | import android.media.AudioFormat; |
| 58 | import android.media.AudioManager; |
| 59 | import android.media.AudioManagerInternal; |
| 60 | import android.media.AudioPort; |
| 61 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 62 | import android.media.IAudioFocusDispatcher; |
| 63 | import android.media.IAudioRoutesObserver; |
| 64 | import android.media.IAudioService; |
| 65 | import android.media.IRemoteControlDisplay; |
| 66 | import android.media.IRingtonePlayer; |
| 67 | import android.media.IVolumeController; |
| 68 | import android.media.MediaPlayer; |
| 69 | import android.media.SoundPool; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 70 | import android.media.VolumePolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.media.MediaPlayer.OnCompletionListener; |
| 72 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -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; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 90 | import android.os.UserManager; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 91 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.provider.Settings; |
| 93 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 94 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 95 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 96 | import android.util.AndroidRuntimeException; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 97 | import android.util.ArrayMap; |
| 98 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 100 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 101 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 102 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 103 | import android.view.KeyEvent; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 104 | import android.view.OrientationEventListener; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 105 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 106 | import android.view.WindowManager; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 107 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 109 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 110 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 111 | import com.android.server.LocalServices; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 112 | import com.android.server.pm.UserManagerService; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 113 | |
| 114 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 116 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 118 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 119 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 121 | import java.util.HashMap; |
| 122 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 123 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 124 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 125 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * The implementation of the volume manager service. |
| 129 | * <p> |
| 130 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 131 | * asynchronous to external calls. For example, the task of setting a volume |
| 132 | * will update our internal state, but in a separate thread will set the system |
| 133 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 134 | * will update the state and broadcast a change and in a separate thread later |
| 135 | * persist the ringer mode. |
| 136 | * |
| 137 | * @hide |
| 138 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 139 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | |
| 141 | private static final String TAG = "AudioService"; |
| 142 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 143 | /** Debug audio mode */ |
| 144 | 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] | 145 | |
| 146 | /** Debug audio policy feature */ |
| 147 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 148 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 149 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 150 | 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] | 151 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 152 | /** debug calls to devices APIs */ |
| 153 | protected static final boolean DEBUG_DEVICES = Log.isLoggable(TAG + ".DEVICES", Log.DEBUG); |
| 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 156 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 158 | /** How long to delay after a volume down event before unmuting a stream */ |
| 159 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 160 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 161 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 162 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 163 | */ |
| 164 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 165 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 166 | private final Context mContext; |
| 167 | private final ContentResolver mContentResolver; |
| 168 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 169 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 170 | // the platform type affects volume and silent mode behavior |
| 171 | private final int mPlatformType; |
| 172 | |
| 173 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 174 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 178 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 179 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 180 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 181 | /** The controller for the volume UI. */ |
| 182 | private final VolumeController mVolumeController = new VolumeController(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 183 | private final ControllerService mControllerService = new ControllerService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| 185 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | /** If the msg is already queued, replace it with this one. */ |
| 187 | private static final int SENDMSG_REPLACE = 0; |
| 188 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 189 | private static final int SENDMSG_NOOP = 1; |
| 190 | /** If the msg is already queued, queue this one and leave the old. */ |
| 191 | private static final int SENDMSG_QUEUE = 2; |
| 192 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 193 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 194 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | private static final int MSG_PERSIST_VOLUME = 1; |
| 196 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 197 | private static final int MSG_MEDIA_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 198 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 199 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 200 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 201 | private static final int MSG_SET_FORCE_USE = 8; |
| 202 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 203 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 204 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 205 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 206 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 207 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 208 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 209 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 210 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 211 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 212 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 213 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 214 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 215 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 216 | private static final int MSG_PERSIST_MICROPHONE_MUTE = 23; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 217 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 218 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 219 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 220 | // start of messages handled under wakelock |
| 221 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 222 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 223 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 224 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 225 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 226 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 227 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 228 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 229 | // Timeout for connection to bluetooth headset service |
| 230 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 231 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 232 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 233 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | /** @see AudioSystemThread */ |
| 236 | private AudioSystemThread mAudioSystemThread; |
| 237 | /** @see AudioHandler */ |
| 238 | private AudioHandler mAudioHandler; |
| 239 | /** @see VolumeStreamState */ |
| 240 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 241 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 242 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 243 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 244 | // protects mRingerMode |
| 245 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 248 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 251 | /* Sound effect file names */ |
| 252 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 253 | 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] | 254 | |
| 255 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 256 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 257 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 258 | 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] | 259 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 260 | /** Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 261 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 262 | 5, // STREAM_VOICE_CALL |
| 263 | 7, // STREAM_SYSTEM |
| 264 | 7, // STREAM_RING |
| 265 | 15, // STREAM_MUSIC |
| 266 | 7, // STREAM_ALARM |
| 267 | 7, // STREAM_NOTIFICATION |
| 268 | 15, // STREAM_BLUETOOTH_SCO |
| 269 | 7, // STREAM_SYSTEM_ENFORCED |
| 270 | 15, // STREAM_DTMF |
| 271 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 272 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 273 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 274 | /** Minimum volume index values for audio streams */ |
| 275 | private static int[] MIN_STREAM_VOLUME = new int[] { |
| 276 | 1, // STREAM_VOICE_CALL |
| 277 | 0, // STREAM_SYSTEM |
| 278 | 0, // STREAM_RING |
| 279 | 0, // STREAM_MUSIC |
| 280 | 0, // STREAM_ALARM |
| 281 | 0, // STREAM_NOTIFICATION |
| 282 | 1, // STREAM_BLUETOOTH_SCO |
| 283 | 0, // STREAM_SYSTEM_ENFORCED |
| 284 | 0, // STREAM_DTMF |
| 285 | 0 // STREAM_TTS |
| 286 | }; |
| 287 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 288 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 289 | * of another stream: This avoids multiplying the volume settings for hidden |
| 290 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 291 | * NOTE: do not create loops in aliases! |
| 292 | * 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] | 293 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 294 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 295 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 296 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 297 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 298 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 299 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 300 | AudioSystem.STREAM_RING, // STREAM_RING |
| 301 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 302 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 303 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 304 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 305 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 306 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 307 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 308 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 309 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 310 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 311 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 312 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 313 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 314 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 315 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 316 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 317 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 318 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 319 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 320 | }; |
| 321 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 322 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 323 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 324 | AudioSystem.STREAM_RING, // STREAM_RING |
| 325 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 326 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 327 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 328 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 329 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 330 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 331 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 332 | }; |
| 333 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 334 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 335 | /** |
| 336 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 337 | * after mapping through mStreamVolumeAlias. |
| 338 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 339 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 340 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 341 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 342 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 343 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 344 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 345 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 346 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 347 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 348 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 349 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 350 | }; |
| 351 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 352 | private final boolean mUseFixedVolume; |
| 353 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 354 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | public void onError(int error) { |
| 356 | switch (error) { |
| 357 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 358 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, |
| 359 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | break; |
| 361 | default: |
| 362 | break; |
| 363 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 364 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | /** |
| 368 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 369 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 370 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 371 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 372 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 373 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 374 | 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] | 375 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 376 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 377 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 378 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 379 | // Streams currently muted by ringer mode |
| 380 | private int mRingerModeMutedStreams; |
| 381 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 382 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 383 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | private int mMuteAffectedStreams; |
| 385 | |
| 386 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 387 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 388 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 389 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | */ |
| 391 | private int mVibrateSetting; |
| 392 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 393 | // Is there a vibrator |
| 394 | private final boolean mHasVibrator; |
| 395 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 396 | // Broadcast receiver for device connections intent broadcasts |
| 397 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 398 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 399 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 400 | // Use makeDeviceListKey() to make a unique key for this list. |
| 401 | private class DeviceListSpec { |
| 402 | int mDeviceType; |
| 403 | String mDeviceName; |
| 404 | String mDeviceAddress; |
| 405 | |
| 406 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 407 | mDeviceType = deviceType; |
| 408 | mDeviceName = deviceName; |
| 409 | mDeviceAddress = deviceAddress; |
| 410 | } |
| 411 | |
| 412 | public String toString() { |
| 413 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 414 | + " address:" + mDeviceAddress + "]"; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 419 | // and the "address" associated with a specific instance of that device type |
| 420 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 421 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 422 | } |
| 423 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 424 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 425 | |
| 426 | // Forced device usage for communications |
| 427 | private int mForcedUseForComm; |
| 428 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 429 | // List of binder death handlers for setMode() client processes. |
| 430 | // 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] | 431 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 432 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 433 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 434 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 435 | |
| 436 | // BluetoothHeadset API to control SCO connection |
| 437 | private BluetoothHeadset mBluetoothHeadset; |
| 438 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 439 | // Bluetooth headset device |
| 440 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 441 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 442 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 443 | // audio service (internal) or bluetooth headset (external) |
| 444 | private int mScoAudioState; |
| 445 | // SCO audio state is not active |
| 446 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 447 | // SCO audio activation request waiting for headset service to connect |
| 448 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 449 | // 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] | 450 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 451 | // SCO audio deactivation request waiting for headset service to connect |
| 452 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 453 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 454 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 455 | // in call audio) |
| 456 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 457 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 458 | // waiting for headset service to connect |
| 459 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 460 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 461 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 462 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 463 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 464 | // SCO audio mode is undefined |
| 465 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 466 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 467 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 468 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 469 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 470 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 471 | private static final int SCO_MODE_VR = 2; |
| 472 | |
| 473 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 474 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 475 | // Current connection state indicated by bluetooth headset |
| 476 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 477 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 478 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 479 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 480 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 481 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 482 | // listener for SoundPool sample load completion indication |
| 483 | private SoundPoolCallback mSoundPoolCallBack; |
| 484 | // thread for SoundPool listener |
| 485 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 486 | // message looper for SoundPool listener |
| 487 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 488 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 489 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 490 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 491 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 492 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 493 | // is controlled by Vol keys. |
| 494 | private int mVolumeControlStream = -1; |
| 495 | private final Object mForceControlStreamLock = new Object(); |
| 496 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 497 | // server process so in theory it is not necessary to monitor the client death. |
| 498 | // However it is good to be ready for future evolutions. |
| 499 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 500 | // Used to play ringtones outside system_server |
| 501 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 502 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 503 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 504 | private int mDeviceRotation = Surface.ROTATION_0; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 505 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 506 | // Request to override default use of A2DP for media. |
| 507 | private boolean mBluetoothA2dpEnabled; |
| 508 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 509 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 510 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 511 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 512 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 513 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 514 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 515 | // 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] | 516 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 517 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 518 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 519 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 520 | AudioSystem.DEVICE_OUT_SPDIF | |
| 521 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 522 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 523 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 524 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 525 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 526 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 527 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 528 | private boolean mDockAudioMediaEnabled = true; |
| 529 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 530 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 531 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 532 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 533 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 534 | // and used later when/if disableSafeMediaVolume() is called. |
| 535 | private StreamVolumeCommand mPendingVolumeCommand; |
| 536 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 537 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 538 | |
| 539 | private final MediaFocusControl mMediaFocusControl; |
| 540 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 541 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 542 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 543 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 544 | private final Object mA2dpAvrcpLock = new Object(); |
| 545 | // If absolute volume is supported in AVRCP device |
| 546 | private boolean mAvrcpAbsVolSupported = false; |
| 547 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 548 | private AudioOrientationEventListener mOrientationListener; |
| 549 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 550 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 551 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 552 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 553 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 554 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 555 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 556 | // Intent "extra" data keys. |
| 557 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 558 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 559 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 560 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 561 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 562 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 563 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 564 | |
| 565 | // Defines the format for the connection "address" for ALSA devices |
| 566 | public static String makeAlsaAddressString(int card, int device) { |
| 567 | return "card=" + card + ";device=" + device + ";"; |
| 568 | } |
| 569 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | /////////////////////////////////////////////////////////////////////////// |
| 571 | // Construction |
| 572 | /////////////////////////////////////////////////////////////////////////// |
| 573 | |
| 574 | /** @hide */ |
| 575 | public AudioService(Context context) { |
| 576 | mContext = context; |
| 577 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 578 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 579 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 580 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 581 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 582 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 583 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 584 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 585 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 586 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 587 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 588 | // Initialize volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 589 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 590 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 591 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 592 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 593 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 594 | } |
| 595 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 596 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 597 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 598 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 599 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 600 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 601 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 602 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 603 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 604 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 605 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 608 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 609 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 610 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 611 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 612 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 613 | sendMsg(mAudioHandler, |
| 614 | MSG_SET_FORCE_USE, |
| 615 | SENDMSG_QUEUE, |
| 616 | AudioSystem.FOR_SYSTEM, |
| 617 | cameraSoundForced ? |
| 618 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 619 | null, |
| 620 | 0); |
| 621 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 622 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 623 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 624 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 625 | // The default safe volume index read here will be replaced by the actual value when |
| 626 | // the mcc is read by onConfigureSafeVolume() |
| 627 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 628 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 629 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 630 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 631 | com.android.internal.R.bool.config_useFixedVolume); |
| 632 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 633 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 634 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 635 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 637 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 638 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 639 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 640 | mMediaFocusControl = new MediaFocusControl(mAudioHandler.getLooper(), |
| 641 | mContext, mVolumeController, this); |
| 642 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 643 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 644 | |
| 645 | // Call setRingerModeInt() to apply correct mute |
| 646 | // state on streams affected by ringer mode. |
| 647 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 648 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 649 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 650 | // Register for device connection intent broadcasts. |
| 651 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 652 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 653 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 654 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 655 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 656 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 657 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 658 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 659 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 660 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 661 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 662 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 663 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 664 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 665 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 666 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 667 | // initialize orientation in AudioSystem |
| 668 | setOrientationForAudioSystem(); |
| 669 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 670 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 671 | if (mMonitorRotation) { |
| 672 | mDeviceRotation = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 673 | .getDefaultDisplay().getRotation(); |
| 674 | Log.v(TAG, "monitoring device rotation, initial=" + mDeviceRotation); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 675 | |
| 676 | mOrientationListener = new AudioOrientationEventListener(mContext); |
| 677 | mOrientationListener.enable(); |
| 678 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 679 | // initialize rotation in AudioSystem |
| 680 | setRotationForAudioSystem(); |
| 681 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 682 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 683 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 684 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 685 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 688 | public void systemReady() { |
| 689 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 690 | 0, 0, null, 0); |
| 691 | } |
| 692 | |
| 693 | public void onSystemReady() { |
| 694 | mSystemReady = true; |
| 695 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 696 | 0, 0, null, 0); |
| 697 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 698 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 699 | resetBluetoothSco(); |
| 700 | getBluetoothHeadset(); |
| 701 | //FIXME: this is to maintain compatibility with deprecated intent |
| 702 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 703 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 704 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 705 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 706 | sendStickyBroadcastToAll(newIntent); |
| 707 | |
| 708 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 709 | if (adapter != null) { |
| 710 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 711 | BluetoothProfile.A2DP); |
| 712 | } |
| 713 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 714 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 715 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 716 | if (mHdmiManager != null) { |
| 717 | synchronized (mHdmiManager) { |
| 718 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 719 | if (mHdmiTvClient != null) { |
| 720 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 721 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 722 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 723 | mHdmiCecSink = false; |
| 724 | } |
| 725 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 726 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 727 | sendMsg(mAudioHandler, |
| 728 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 729 | SENDMSG_REPLACE, |
| 730 | 0, |
| 731 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 732 | TAG, |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 733 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 734 | |
| 735 | StreamOverride.init(mContext); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 736 | mControllerService.init(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 737 | onIndicateSystemReady(); |
| 738 | } |
| 739 | |
| 740 | void onIndicateSystemReady() { |
| 741 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 742 | return; |
| 743 | } |
| 744 | sendMsg(mAudioHandler, |
| 745 | MSG_INDICATE_SYSTEM_READY, |
| 746 | SENDMSG_REPLACE, |
| 747 | 0, |
| 748 | 0, |
| 749 | null, |
| 750 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 751 | } |
| 752 | |
| 753 | public void onMediaServerDied() { |
| 754 | if (!mSystemReady || |
| 755 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
| 756 | Log.e(TAG, "Media server died."); |
| 757 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
| 758 | null, 500); |
| 759 | return; |
| 760 | } |
| 761 | Log.e(TAG, "Media server started."); |
| 762 | |
| 763 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 764 | // server crash |
| 765 | // Note that we only execute this when the media server |
| 766 | // process restarts after a crash, not the first time it is started. |
| 767 | AudioSystem.setParameters("restarting=true"); |
| 768 | |
| 769 | readAndSetLowRamDevice(); |
| 770 | |
| 771 | // Restore device connection states |
| 772 | synchronized (mConnectedDevices) { |
| 773 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 774 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 775 | AudioSystem.setDeviceConnectionState( |
| 776 | spec.mDeviceType, |
| 777 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 778 | spec.mDeviceAddress, |
| 779 | spec.mDeviceName); |
| 780 | } |
| 781 | } |
| 782 | // Restore call state |
| 783 | AudioSystem.setPhoneState(mMode); |
| 784 | |
| 785 | // Restore forced usage for communcations and record |
| 786 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
| 787 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
| 788 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 789 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
| 790 | |
| 791 | // Restore stream volumes |
| 792 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 793 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 794 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 795 | AudioSystem.initStreamVolume(streamType, 0, (streamState.mIndexMax + 5) / 10); |
| 796 | |
| 797 | streamState.applyAllVolumes(); |
| 798 | } |
| 799 | |
| 800 | // Restore ringer mode |
| 801 | setRingerModeInt(getRingerModeInternal(), false); |
| 802 | |
| 803 | // Reset device orientation (if monitored for this device) |
| 804 | if (mMonitorOrientation) { |
| 805 | setOrientationForAudioSystem(); |
| 806 | } |
| 807 | if (mMonitorRotation) { |
| 808 | setRotationForAudioSystem(); |
| 809 | } |
| 810 | |
| 811 | synchronized (mBluetoothA2dpEnabledLock) { |
| 812 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 813 | mBluetoothA2dpEnabled ? |
| 814 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 815 | } |
| 816 | |
| 817 | synchronized (mSettingsLock) { |
| 818 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 819 | mDockAudioMediaEnabled ? |
| 820 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 821 | } |
| 822 | if (mHdmiManager != null) { |
| 823 | synchronized (mHdmiManager) { |
| 824 | if (mHdmiTvClient != null) { |
| 825 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | synchronized (mAudioPolicies) { |
| 831 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 832 | policy.connectMixes(); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | onIndicateSystemReady(); |
| 837 | // indicate the end of reconfiguration phase to audio HAL |
| 838 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 839 | } |
| 840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | private void createAudioSystemThread() { |
| 842 | mAudioSystemThread = new AudioSystemThread(); |
| 843 | mAudioSystemThread.start(); |
| 844 | waitForAudioHandlerCreation(); |
| 845 | } |
| 846 | |
| 847 | /** Waits for the volume handler to be created by the other thread. */ |
| 848 | private void waitForAudioHandlerCreation() { |
| 849 | synchronized(this) { |
| 850 | while (mAudioHandler == null) { |
| 851 | try { |
| 852 | // Wait for mAudioHandler to be set by the other thread |
| 853 | wait(); |
| 854 | } catch (InterruptedException e) { |
| 855 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 861 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 862 | synchronized (VolumeStreamState.class) { |
| 863 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 864 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 865 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 866 | mStreamStates[streamType]. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 867 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], |
| 868 | TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 869 | } |
| 870 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 871 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 872 | mStreamStates[streamType].applyAllVolumes(); |
| 873 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 878 | private void checkAllFixedVolumeDevices() |
| 879 | { |
| 880 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 881 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 882 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 883 | } |
| 884 | } |
| 885 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 886 | private void checkAllFixedVolumeDevices(int streamType) { |
| 887 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 888 | } |
| 889 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 890 | private void checkMuteAffectedStreams() { |
| 891 | // any stream with a min level > 0 is not muteable by definition |
| 892 | for (int i = 0; i < mStreamStates.length; i++) { |
| 893 | final VolumeStreamState vss = mStreamStates[i]; |
| 894 | if (vss.mIndexMin > 0) { |
| 895 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 902 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 903 | |
| 904 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 905 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 906 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 908 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 909 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 910 | checkMuteAffectedStreams(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | } |
| 912 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 913 | private void dumpStreamStates(PrintWriter pw) { |
| 914 | pw.println("\nStream volumes (device: index)"); |
| 915 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 916 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 917 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 918 | mStreamStates[i].dump(pw); |
| 919 | pw.println(""); |
| 920 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 921 | pw.print("\n- mute affected streams = 0x"); |
| 922 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 923 | } |
| 924 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 925 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 926 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 927 | |
| 928 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 929 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 930 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 931 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 932 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 933 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 934 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 935 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 936 | break; |
| 937 | default: |
| 938 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 939 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 940 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 941 | |
| 942 | if (isPlatformTelevision()) { |
| 943 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 944 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 945 | if (isInCommunication()) { |
| 946 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 947 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 948 | } else { |
| 949 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 950 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 951 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 952 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 953 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 954 | if (updateVolumes) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 955 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], |
| 956 | caller); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 957 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 958 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 959 | sendMsg(mAudioHandler, |
| 960 | MSG_SET_ALL_VOLUMES, |
| 961 | SENDMSG_QUEUE, |
| 962 | 0, |
| 963 | 0, |
| 964 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 965 | } |
| 966 | } |
| 967 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 968 | private void readDockAudioSettings(ContentResolver cr) |
| 969 | { |
| 970 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 971 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 972 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 973 | sendMsg(mAudioHandler, |
| 974 | MSG_SET_FORCE_USE, |
| 975 | SENDMSG_QUEUE, |
| 976 | AudioSystem.FOR_DOCK, |
| 977 | mDockAudioMediaEnabled ? |
| 978 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 979 | null, |
| 980 | 0); |
| 981 | } |
| 982 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | private void readPersistedSettings() { |
| 984 | final ContentResolver cr = mContentResolver; |
| 985 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 986 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 987 | Settings.Global.getInt( |
| 988 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 989 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 990 | // sanity check in case the settings are restored from a device with incompatible |
| 991 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 992 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 993 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 994 | } |
| 995 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 996 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 997 | } |
| 998 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 999 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1000 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1001 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1002 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1003 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1004 | synchronized(mSettingsLock) { |
| 1005 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1006 | if (mRingerModeExternal == -1) { |
| 1007 | mRingerModeExternal = mRingerMode; |
| 1008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1010 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1011 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1012 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1013 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1014 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1015 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1016 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1017 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1018 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1019 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1020 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1022 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1023 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1024 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1025 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1026 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1027 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1028 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1030 | boolean masterMute = System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1031 | 0, UserHandle.USER_CURRENT) == 1; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1032 | if (mUseFixedVolume) { |
| 1033 | masterMute = false; |
| 1034 | AudioSystem.setMasterVolume(1.0f); |
| 1035 | } |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1036 | AudioSystem.setMasterMute(masterMute); |
| 1037 | broadcastMasterMuteStatus(masterMute); |
| 1038 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1039 | boolean microphoneMute = |
| 1040 | System.getIntForUser(cr, System.MICROPHONE_MUTE, 0, UserHandle.USER_CURRENT) == 1; |
| 1041 | AudioSystem.muteMicrophone(microphoneMute); |
| 1042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | // Each stream will read its own persisted settings |
| 1044 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1045 | // Broadcast the sticky intents |
| 1046 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1047 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | |
| 1049 | // Broadcast vibrate settings |
| 1050 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1051 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1052 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1053 | // Load settings for the volume controller |
| 1054 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1057 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 1058 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 1059 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 1061 | private class AudioOrientationEventListener |
| 1062 | extends OrientationEventListener { |
| 1063 | public AudioOrientationEventListener(Context context) { |
| 1064 | super(context); |
| 1065 | } |
| 1066 | |
| 1067 | @Override |
| 1068 | public void onOrientationChanged(int orientation) { |
| 1069 | //Even though we're responding to phone orientation events, |
| 1070 | //use display rotation so audio stays in sync with video/dialogs |
| 1071 | int newRotation = ((WindowManager) mContext.getSystemService( |
| 1072 | Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); |
| 1073 | if (newRotation != mDeviceRotation) { |
| 1074 | mDeviceRotation = newRotation; |
| 1075 | setRotationForAudioSystem(); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | /////////////////////////////////////////////////////////////////////////// |
| 1081 | // IPC methods |
| 1082 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1084 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1085 | String callingPackage, String caller) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1086 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1087 | caller, Binder.getCallingUid()); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1091 | String callingPackage, String caller, int uid) { |
| 1092 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
| 1093 | + ", flags=" + flags + ", caller=" + caller); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1094 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1095 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1096 | if (mVolumeControlStream != -1) { |
| 1097 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1098 | } else { |
| 1099 | streamType = getActiveStreamType(suggestedStreamType); |
| 1100 | } |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1101 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1102 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1104 | // Play sounds on STREAM_RING only. |
| 1105 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1106 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1108 | } |
| 1109 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1110 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1111 | // Don't suppress mute/unmute requests |
| 1112 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1113 | direction = 0; |
| 1114 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1115 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1116 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1117 | } |
| 1118 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1119 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1123 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1124 | String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1125 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1126 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | private void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1130 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1131 | if (mUseFixedVolume) { |
| 1132 | return; |
| 1133 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1134 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1135 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | ensureValidDirection(direction); |
| 1138 | ensureValidStreamType(streamType); |
| 1139 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1140 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1141 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1142 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1143 | return; |
| 1144 | } |
| 1145 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1146 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1147 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1148 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1149 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1150 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1151 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1152 | |
| 1153 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1154 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1155 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1157 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1158 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1159 | // skip a2dp absolute volume control request when the device |
| 1160 | // is not an a2dp device |
| 1161 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1162 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1163 | return; |
| 1164 | } |
| 1165 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1166 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1167 | // so we handle user restrictions correctly. |
| 1168 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1169 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1170 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1171 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1172 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1173 | return; |
| 1174 | } |
| 1175 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1176 | // reset any pending volume command |
| 1177 | synchronized (mSafeMediaVolumeState) { |
| 1178 | mPendingVolumeCommand = null; |
| 1179 | } |
| 1180 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1181 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1182 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1183 | ((device & mFixedVolumeDevices) != 0)) { |
| 1184 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1185 | |
| 1186 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1187 | // volume is enforced, and max and 0 for the others. |
| 1188 | // This is simulated by stepping by the full allowed volume range |
| 1189 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1190 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1191 | step = mSafeMediaVolumeIndex; |
| 1192 | } else { |
| 1193 | step = streamState.getMaxIndex(); |
| 1194 | } |
| 1195 | if (aliasIndex != 0) { |
| 1196 | aliasIndex = step; |
| 1197 | } |
| 1198 | } else { |
| 1199 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1200 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1201 | } |
| 1202 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1203 | // If either the client forces allowing ringer modes for this adjustment, |
| 1204 | // or the stream type is one that is affected by ringer modes |
| 1205 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1206 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1207 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1208 | // do not vibrate if already in vibrate mode |
| 1209 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1210 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1211 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1212 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1213 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1214 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
| 1215 | streamState.mIsMuted); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1216 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1217 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1218 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1219 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1220 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1221 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1222 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1223 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1224 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1225 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1226 | // If the ringermode is suppressing media, prevent changes |
| 1227 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC |
| 1228 | && (mRingerModeMutedStreams & (1 << AudioSystem.STREAM_MUSIC)) != 0) { |
| 1229 | adjustVolume = false; |
| 1230 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1231 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1232 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1233 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1234 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1235 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1236 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1237 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1238 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1239 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1240 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1241 | synchronized (mA2dpAvrcpLock) { |
| 1242 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1243 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1244 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1247 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1248 | if (isMuteAdjust) { |
| 1249 | boolean state; |
| 1250 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1251 | state = !streamState.mIsMuted; |
| 1252 | } else { |
| 1253 | state = direction == AudioManager.ADJUST_MUTE; |
| 1254 | } |
| 1255 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1256 | setSystemAudioMute(state); |
| 1257 | } |
| 1258 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1259 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
| 1260 | mStreamStates[stream].mute(state); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1264 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1265 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1266 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1267 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1268 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1269 | // Post message to set system volume (it in turn will post a |
| 1270 | // message to persist). |
| 1271 | if (streamState.mIsMuted) { |
| 1272 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1273 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1274 | // unmute immediately for volume up |
| 1275 | streamState.mute(false); |
| 1276 | } else if (direction == AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1277 | if (mPlatformType == AudioSystem.PLATFORM_TELEVISION) { |
| 1278 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1279 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1280 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1281 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1282 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1283 | sendMsg(mAudioHandler, |
| 1284 | MSG_SET_DEVICE_VOLUME, |
| 1285 | SENDMSG_QUEUE, |
| 1286 | device, |
| 1287 | 0, |
| 1288 | streamState, |
| 1289 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1290 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1291 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1292 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1293 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1294 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1295 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1296 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1297 | if (mHdmiManager != null) { |
| 1298 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1299 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1300 | if (mHdmiCecSink && |
| 1301 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1302 | oldIndex != newIndex) { |
| 1303 | synchronized (mHdmiPlaybackClient) { |
| 1304 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1305 | KeyEvent.KEYCODE_VOLUME_UP; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1306 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1307 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1308 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1312 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1313 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1314 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1317 | // Called after a delay when volume down is pressed while muted |
| 1318 | private void onUnmuteStream(int stream, int flags) { |
| 1319 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1320 | streamState.mute(false); |
| 1321 | |
| 1322 | final int device = getDeviceForStream(stream); |
| 1323 | final int index = mStreamStates[stream].getIndex(device); |
| 1324 | sendVolumeUpdate(stream, index, index, flags); |
| 1325 | } |
| 1326 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1327 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1328 | if (mHdmiManager == null |
| 1329 | || mHdmiTvClient == null |
| 1330 | || oldVolume == newVolume |
| 1331 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1332 | |
| 1333 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1334 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1335 | synchronized (mHdmiManager) { |
| 1336 | if (!mHdmiSystemAudioSupported) return; |
| 1337 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1338 | final long token = Binder.clearCallingIdentity(); |
| 1339 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1340 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1341 | } finally { |
| 1342 | Binder.restoreCallingIdentity(token); |
| 1343 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1348 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1349 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1350 | class StreamVolumeCommand { |
| 1351 | public final int mStreamType; |
| 1352 | public final int mIndex; |
| 1353 | public final int mFlags; |
| 1354 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1355 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1356 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1357 | mStreamType = streamType; |
| 1358 | mIndex = index; |
| 1359 | mFlags = flags; |
| 1360 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1361 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1362 | |
| 1363 | @Override |
| 1364 | public String toString() { |
| 1365 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1366 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1367 | .append(mDevice).append('}').toString(); |
| 1368 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1369 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1370 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1371 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1372 | String caller) { |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1373 | final int stream = mStreamVolumeAlias[streamType]; |
| 1374 | setStreamVolumeInt(stream, index, device, false, caller); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1375 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1376 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1377 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1378 | int newRingerMode; |
| 1379 | if (index == 0) { |
| 1380 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1381 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1382 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1383 | } else { |
| 1384 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1385 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1386 | setRingerMode(newRingerMode, TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1387 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1388 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1389 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1393 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1394 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1395 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1399 | String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1400 | if (mUseFixedVolume) { |
| 1401 | return; |
| 1402 | } |
| 1403 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1404 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1405 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1406 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1407 | |
| 1408 | final int device = getDeviceForStream(streamType); |
| 1409 | int oldIndex; |
| 1410 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1411 | // skip a2dp absolute volume control request when the device |
| 1412 | // is not an a2dp device |
| 1413 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1414 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1415 | return; |
| 1416 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1417 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1418 | // so we handle user restrictions correctly. |
| 1419 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1420 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1421 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1422 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1423 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1424 | return; |
| 1425 | } |
| 1426 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1427 | synchronized (mSafeMediaVolumeState) { |
| 1428 | // reset any pending volume command |
| 1429 | mPendingVolumeCommand = null; |
| 1430 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1431 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1432 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1433 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1434 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1435 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1436 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1437 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1438 | synchronized (mA2dpAvrcpLock) { |
| 1439 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1440 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1441 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1445 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1446 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1447 | } |
| 1448 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1449 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1450 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1451 | ((device & mFixedVolumeDevices) != 0)) { |
| 1452 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1453 | |
| 1454 | // volume is either 0 or max allowed for fixed volume devices |
| 1455 | if (index != 0) { |
| 1456 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1457 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1458 | index = mSafeMediaVolumeIndex; |
| 1459 | } else { |
| 1460 | index = streamState.getMaxIndex(); |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1465 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1466 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1467 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1468 | streamType, index, flags, device); |
| 1469 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1470 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1471 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1472 | } |
| 1473 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1474 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1477 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1478 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1479 | synchronized(mForceControlStreamLock) { |
| 1480 | mVolumeControlStream = streamType; |
| 1481 | if (mVolumeControlStream == -1) { |
| 1482 | if (mForceControlStreamClient != null) { |
| 1483 | mForceControlStreamClient.release(); |
| 1484 | mForceControlStreamClient = null; |
| 1485 | } |
| 1486 | } else { |
| 1487 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1493 | private IBinder mCb; // To be notified of client's death |
| 1494 | |
| 1495 | ForceControlStreamClient(IBinder cb) { |
| 1496 | if (cb != null) { |
| 1497 | try { |
| 1498 | cb.linkToDeath(this, 0); |
| 1499 | } catch (RemoteException e) { |
| 1500 | // Client has died! |
| 1501 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1502 | cb = null; |
| 1503 | } |
| 1504 | } |
| 1505 | mCb = cb; |
| 1506 | } |
| 1507 | |
| 1508 | public void binderDied() { |
| 1509 | synchronized(mForceControlStreamLock) { |
| 1510 | Log.w(TAG, "SCO client died"); |
| 1511 | if (mForceControlStreamClient != this) { |
| 1512 | Log.w(TAG, "unregistered control stream client died"); |
| 1513 | } else { |
| 1514 | mForceControlStreamClient = null; |
| 1515 | mVolumeControlStream = -1; |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | public void release() { |
| 1521 | if (mCb != null) { |
| 1522 | mCb.unlinkToDeath(this, 0); |
| 1523 | mCb = null; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1528 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1529 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1530 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1531 | final long ident = Binder.clearCallingIdentity(); |
| 1532 | try { |
| 1533 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1534 | } finally { |
| 1535 | Binder.restoreCallingIdentity(ident); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1540 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1541 | final long ident = Binder.clearCallingIdentity(); |
| 1542 | try { |
| 1543 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1544 | } finally { |
| 1545 | Binder.restoreCallingIdentity(ident); |
| 1546 | } |
| 1547 | } |
| 1548 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1549 | private int getCurrentUserId() { |
| 1550 | final long ident = Binder.clearCallingIdentity(); |
| 1551 | try { |
| 1552 | UserInfo currentUser = ActivityManagerNative.getDefault().getCurrentUser(); |
| 1553 | return currentUser.id; |
| 1554 | } catch (RemoteException e) { |
| 1555 | // Activity manager not running, nothing we can do assume user 0. |
| 1556 | } finally { |
| 1557 | Binder.restoreCallingIdentity(ident); |
| 1558 | } |
| 1559 | return UserHandle.USER_OWNER; |
| 1560 | } |
| 1561 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1562 | // UI update and Broadcast Intent |
| 1563 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 1564 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1565 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1566 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1567 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1568 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1569 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1572 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1573 | // receives volume notification from Audio Receiver. |
| 1574 | private int updateFlagsForSystemAudio(int flags) { |
| 1575 | if (mHdmiTvClient != null) { |
| 1576 | synchronized (mHdmiTvClient) { |
| 1577 | if (mHdmiSystemAudioSupported && |
| 1578 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1579 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | return flags; |
| 1584 | } |
| 1585 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1586 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1587 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1588 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1589 | broadcastMasterMuteStatus(muted); |
| 1590 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1591 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1592 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1593 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1594 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1595 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1596 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1597 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1598 | } |
| 1599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | * Sets the stream state's index, and posts a message to set system volume. |
| 1602 | * This will not call out to the UI. Assumes a valid stream type. |
| 1603 | * |
| 1604 | * @param streamType Type of the stream |
| 1605 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1606 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | * @param force If true, set the volume even if the desired volume is same |
| 1608 | * as the current volume. |
| 1609 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1610 | private void setStreamVolumeInt(int streamType, |
| 1611 | int index, |
| 1612 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1613 | boolean force, |
| 1614 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1616 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1617 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1618 | // Post message to set system volume (it in turn will post a message |
| 1619 | // to persist). |
| 1620 | sendMsg(mAudioHandler, |
| 1621 | MSG_SET_DEVICE_VOLUME, |
| 1622 | SENDMSG_QUEUE, |
| 1623 | device, |
| 1624 | 0, |
| 1625 | streamState, |
| 1626 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1630 | private void setSystemAudioMute(boolean state) { |
| 1631 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1632 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1633 | if (!mHdmiSystemAudioSupported) return; |
| 1634 | synchronized (mHdmiTvClient) { |
| 1635 | final long token = Binder.clearCallingIdentity(); |
| 1636 | try { |
| 1637 | mHdmiTvClient.setSystemAudioMute(state); |
| 1638 | } finally { |
| 1639 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1640 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1645 | /** get stream mute state. */ |
| 1646 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1647 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1648 | streamType = getActiveStreamType(streamType); |
| 1649 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1650 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1651 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1652 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1655 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1656 | private IBinder mICallback; // To be notified of client's death |
| 1657 | |
| 1658 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1659 | mICallback = cb; |
| 1660 | try { |
| 1661 | cb.linkToDeath(this, 0/*flags*/); |
| 1662 | } catch (RemoteException e) { |
| 1663 | Log.e(TAG, "can't link to death", e); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | boolean isHandlerFor(IBinder cb) { |
| 1668 | return mICallback.equals(cb); |
| 1669 | } |
| 1670 | |
| 1671 | void forget() { |
| 1672 | try { |
| 1673 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1674 | } catch (NoSuchElementException e) { |
| 1675 | Log.e(TAG, "error unlinking to death", e); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | public void binderDied() { |
| 1680 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1681 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1687 | * @return true if there is a registered death handler, false otherwise */ |
| 1688 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1689 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1690 | while (it.hasNext()) { |
| 1691 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1692 | if (handler.isHandlerFor(cb)) { |
| 1693 | handler.forget(); |
| 1694 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1695 | return true; |
| 1696 | } |
| 1697 | } |
| 1698 | return false; |
| 1699 | } |
| 1700 | |
| 1701 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1702 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1703 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1704 | while (it.hasNext()) { |
| 1705 | if (it.next().isHandlerFor(cb)) { |
| 1706 | return true; |
| 1707 | } |
| 1708 | } |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | private int mRmtSbmxFullVolRefCount = 0; |
| 1713 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1714 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1715 | |
| 1716 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1717 | if (cb == null) { |
| 1718 | return; |
| 1719 | } |
| 1720 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1721 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1722 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1723 | return; |
| 1724 | } |
| 1725 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1726 | boolean applyRequired = false; |
| 1727 | if (startForcing) { |
| 1728 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1729 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1730 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1731 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1732 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1733 | applyRequired = true; |
| 1734 | } |
| 1735 | mRmtSbmxFullVolRefCount++; |
| 1736 | } |
| 1737 | } else { |
| 1738 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1739 | mRmtSbmxFullVolRefCount--; |
| 1740 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1741 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1742 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1743 | applyRequired = true; |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | if (applyRequired) { |
| 1748 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1749 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1750 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1755 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 1756 | int userId) { |
| 1757 | // If we are being called by the system check for user we are going to change |
| 1758 | // so we handle user restrictions correctly. |
| 1759 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1760 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 1761 | } |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1762 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
| 1763 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1764 | return; |
| 1765 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1766 | if (userId != UserHandle.getCallingUserId() && |
| 1767 | mContext.checkCallingOrSelfPermission( |
| 1768 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1769 | != PackageManager.PERMISSION_GRANTED) { |
| 1770 | return; |
| 1771 | } |
| 1772 | if (getCurrentUserId() == userId) { |
| 1773 | if (mute != AudioSystem.getMasterMute()) { |
| 1774 | setSystemAudioMute(mute); |
| 1775 | AudioSystem.setMasterMute(mute); |
| 1776 | // Post a persist master volume msg |
| 1777 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
| 1778 | : 0, userId, null, PERSIST_DELAY); |
| 1779 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1780 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1781 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1782 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 1783 | sendBroadcastToAll(intent); |
| 1784 | } |
| 1785 | } else { |
| 1786 | // If not the current user just persist the setting which will be loaded |
| 1787 | // on user switch. |
| 1788 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
| 1789 | : 0, userId, null, PERSIST_DELAY); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1790 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /** get master mute state. */ |
| 1794 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1795 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1798 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 1799 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 1800 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1801 | } |
| 1802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1803 | /** @see AudioManager#getStreamVolume(int) */ |
| 1804 | public int getStreamVolume(int streamType) { |
| 1805 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1806 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1807 | synchronized (VolumeStreamState.class) { |
| 1808 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1809 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1810 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1811 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1812 | index = 0; |
| 1813 | } |
| 1814 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 1815 | (device & mFixedVolumeDevices) != 0) { |
| 1816 | index = mStreamStates[streamType].getMaxIndex(); |
| 1817 | } |
| 1818 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1819 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1823 | public int getStreamMaxVolume(int streamType) { |
| 1824 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1825 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1828 | /** @see AudioManager#getStreamMinVolume(int) */ |
| 1829 | public int getStreamMinVolume(int streamType) { |
| 1830 | ensureValidStreamType(streamType); |
| 1831 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 1832 | } |
| 1833 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1834 | /** Get last audible volume before stream was muted. */ |
| 1835 | public int getLastAudibleStreamVolume(int streamType) { |
| 1836 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1837 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1838 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1841 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 1842 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1843 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1846 | /** @see AudioManager#setMicrophoneMute(boolean, int) */ |
| 1847 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 1848 | // If we are being called by the system check for user we are going to change |
| 1849 | // so we handle user restrictions correctly. |
| 1850 | int uid = Binder.getCallingUid(); |
| 1851 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1852 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 1853 | } |
| 1854 | if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
| 1855 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1856 | return; |
| 1857 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 1858 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 1859 | return; |
| 1860 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1861 | if (userId != UserHandle.getCallingUserId() && |
| 1862 | mContext.checkCallingOrSelfPermission( |
| 1863 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1864 | != PackageManager.PERMISSION_GRANTED) { |
| 1865 | return; |
| 1866 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1867 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1868 | // If mute is for current user actually mute, else just persist the setting |
| 1869 | // which will be loaded on user switch. |
| 1870 | if (getCurrentUserId() == userId) { |
| 1871 | AudioSystem.muteMicrophone(on); |
| 1872 | } |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1873 | // Post a persist microphone msg. |
| 1874 | sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1 |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1875 | : 0, userId, null, PERSIST_DELAY); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1876 | } |
| 1877 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1878 | @Override |
| 1879 | public int getRingerModeExternal() { |
| 1880 | synchronized(mSettingsLock) { |
| 1881 | return mRingerModeExternal; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | @Override |
| 1886 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1887 | synchronized(mSettingsLock) { |
| 1888 | return mRingerMode; |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1893 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1894 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1895 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | } |
| 1897 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1898 | /** @see AudioManager#isValidRingerMode(int) */ |
| 1899 | public boolean isValidRingerMode(int ringerMode) { |
| 1900 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 1901 | } |
| 1902 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1903 | public void setRingerModeExternal(int ringerMode, String caller) { |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1904 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1908 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1909 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1913 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1914 | return; |
| 1915 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1916 | if (caller == null || caller.length() == 0) { |
| 1917 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 1918 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1919 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1920 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1921 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1922 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1923 | final long identity = Binder.clearCallingIdentity(); |
| 1924 | try { |
| 1925 | synchronized (mSettingsLock) { |
| 1926 | final int ringerModeInternal = getRingerModeInternal(); |
| 1927 | final int ringerModeExternal = getRingerModeExternal(); |
| 1928 | if (external) { |
| 1929 | setRingerModeExt(ringerMode); |
| 1930 | if (mRingerModeDelegate != null) { |
| 1931 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1932 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1933 | } |
| 1934 | if (ringerMode != ringerModeInternal) { |
| 1935 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1936 | } |
| 1937 | } else /*internal*/ { |
| 1938 | if (ringerMode != ringerModeInternal) { |
| 1939 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1940 | } |
| 1941 | if (mRingerModeDelegate != null) { |
| 1942 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1943 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1944 | } |
| 1945 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1946 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1947 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1948 | } finally { |
| 1949 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1953 | private void setRingerModeExt(int ringerMode) { |
| 1954 | synchronized(mSettingsLock) { |
| 1955 | if (ringerMode == mRingerModeExternal) return; |
| 1956 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1957 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1958 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1959 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1962 | private void muteRingerModeStreams() { |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1963 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 1964 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 1965 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 1966 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1967 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1968 | final boolean ringerModeMute = mRingerMode == AudioManager.RINGER_MODE_VIBRATE |
| 1969 | || mRingerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1970 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1971 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 1972 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 1973 | if (isMuted == shouldMute) continue; |
| 1974 | if (!shouldMute) { |
| 1975 | // unmute |
| 1976 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1977 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1978 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1979 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 1980 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 1981 | int device = vss.mIndexMap.keyAt(i); |
| 1982 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 1983 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1984 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1985 | } |
| 1986 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 1987 | // Persist volume for stream ring when it is changed here |
| 1988 | final int device = getDeviceForStream(streamType); |
| 1989 | sendMsg(mAudioHandler, |
| 1990 | MSG_PERSIST_VOLUME, |
| 1991 | SENDMSG_QUEUE, |
| 1992 | device, |
| 1993 | 0, |
| 1994 | mStreamStates[streamType], |
| 1995 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1996 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 1997 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1998 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1999 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2000 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2001 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2002 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2003 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2004 | } |
| 2005 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2009 | final boolean change; |
| 2010 | synchronized(mSettingsLock) { |
| 2011 | change = mRingerMode != ringerMode; |
| 2012 | mRingerMode = ringerMode; |
| 2013 | } |
| 2014 | |
| 2015 | muteRingerModeStreams(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2016 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2017 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2018 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2019 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2020 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2021 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2022 | if (change) { |
| 2023 | // Send sticky broadcast |
| 2024 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2025 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2028 | /** @see AudioManager#shouldVibrate(int) */ |
| 2029 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2030 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | |
| 2032 | switch (getVibrateSetting(vibrateType)) { |
| 2033 | |
| 2034 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2035 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | |
| 2037 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2038 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | |
| 2040 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2041 | // return false, even for incoming calls |
| 2042 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | |
| 2044 | default: |
| 2045 | return false; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2050 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2051 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2053 | } |
| 2054 | |
| 2055 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2056 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2057 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2058 | if (!mHasVibrator) return; |
| 2059 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2060 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2061 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2062 | |
| 2063 | // Broadcast change |
| 2064 | broadcastVibrateSetting(vibrateType); |
| 2065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2068 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2069 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2070 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2071 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2072 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2073 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2074 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2075 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2079 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2080 | synchronized(mSetModeDeathHandlers) { |
| 2081 | Log.w(TAG, "setMode() client died"); |
| 2082 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2083 | if (index < 0) { |
| 2084 | Log.w(TAG, "unregistered setMode() client died"); |
| 2085 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2086 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2087 | } |
| 2088 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2089 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2090 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2091 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2092 | final long ident = Binder.clearCallingIdentity(); |
| 2093 | disconnectBluetoothSco(newModeOwnerPid); |
| 2094 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2095 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2096 | } |
| 2097 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2098 | public int getPid() { |
| 2099 | return mPid; |
| 2100 | } |
| 2101 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2102 | public void setMode(int mode) { |
| 2103 | mMode = mode; |
| 2104 | } |
| 2105 | |
| 2106 | public int getMode() { |
| 2107 | return mMode; |
| 2108 | } |
| 2109 | |
| 2110 | public IBinder getBinder() { |
| 2111 | return mCb; |
| 2112 | } |
| 2113 | } |
| 2114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2116 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2117 | if (DEBUG_MODE) { Log.v(TAG, "setMode(mode=" + mode + ", callingPackage=" + callingPackage + ")"); } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2119 | return; |
| 2120 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2121 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2122 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2123 | (mContext.checkCallingOrSelfPermission( |
| 2124 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2125 | != PackageManager.PERMISSION_GRANTED)) { |
| 2126 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2127 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2128 | return; |
| 2129 | } |
| 2130 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2131 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2132 | return; |
| 2133 | } |
| 2134 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2135 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2136 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2137 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2138 | mode = mMode; |
| 2139 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2140 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2141 | } |
| 2142 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2143 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2144 | if (newModeOwnerPid != 0) { |
| 2145 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2146 | } |
| 2147 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2148 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2149 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2150 | // 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] | 2151 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2152 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2153 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2154 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2155 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2156 | if (cb == null) { |
| 2157 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2158 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2159 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2160 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2161 | SetModeDeathHandler hdlr = null; |
| 2162 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2163 | while (iter.hasNext()) { |
| 2164 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2165 | if (h.getPid() == pid) { |
| 2166 | hdlr = h; |
| 2167 | // Remove from client list so that it is re-inserted at top of list |
| 2168 | iter.remove(); |
| 2169 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2170 | break; |
| 2171 | } |
| 2172 | } |
| 2173 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 2174 | do { |
| 2175 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2176 | // get new mode from client at top the list if any |
| 2177 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2178 | hdlr = mSetModeDeathHandlers.get(0); |
| 2179 | cb = hdlr.getBinder(); |
| 2180 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2181 | if (DEBUG_MODE) { |
| 2182 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2183 | + hdlr.mPid); |
| 2184 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2185 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2186 | } else { |
| 2187 | if (hdlr == null) { |
| 2188 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2189 | } |
| 2190 | // Register for client death notification |
| 2191 | try { |
| 2192 | cb.linkToDeath(hdlr, 0); |
| 2193 | } catch (RemoteException e) { |
| 2194 | // Client has died! |
| 2195 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2196 | } |
| 2197 | |
| 2198 | // Last client to call setMode() is always at top of client list |
| 2199 | // as required by SetModeDeathHandler.binderDied() |
| 2200 | mSetModeDeathHandlers.add(0, hdlr); |
| 2201 | hdlr.setMode(mode); |
| 2202 | } |
| 2203 | |
| 2204 | if (mode != mMode) { |
| 2205 | status = AudioSystem.setPhoneState(mode); |
| 2206 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2207 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2208 | mMode = mode; |
| 2209 | } else { |
| 2210 | if (hdlr != null) { |
| 2211 | mSetModeDeathHandlers.remove(hdlr); |
| 2212 | cb.unlinkToDeath(hdlr, 0); |
| 2213 | } |
| 2214 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2215 | 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] | 2216 | mode = AudioSystem.MODE_NORMAL; |
| 2217 | } |
| 2218 | } else { |
| 2219 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2220 | } |
| 2221 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2222 | |
| 2223 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 2224 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2225 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2226 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2227 | } else { |
| 2228 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2230 | } |
| 2231 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2232 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2233 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2234 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2235 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2236 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2238 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | /** @see AudioManager#getMode() */ |
| 2242 | public int getMode() { |
| 2243 | return mMode; |
| 2244 | } |
| 2245 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2246 | //========================================================================================== |
| 2247 | // Sound Effects |
| 2248 | //========================================================================================== |
| 2249 | |
| 2250 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2251 | private static final String ATTR_VERSION = "version"; |
| 2252 | private static final String TAG_GROUP = "group"; |
| 2253 | private static final String ATTR_GROUP_NAME = "name"; |
| 2254 | private static final String TAG_ASSET = "asset"; |
| 2255 | private static final String ATTR_ASSET_ID = "id"; |
| 2256 | private static final String ATTR_ASSET_FILE = "file"; |
| 2257 | |
| 2258 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2259 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2260 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2261 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2262 | |
| 2263 | class LoadSoundEffectReply { |
| 2264 | public int mStatus = 1; |
| 2265 | }; |
| 2266 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2267 | private void loadTouchSoundAssetDefaults() { |
| 2268 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2269 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2270 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2271 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | private void loadTouchSoundAssets() { |
| 2276 | XmlResourceParser parser = null; |
| 2277 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2278 | // only load assets once. |
| 2279 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2280 | return; |
| 2281 | } |
| 2282 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2283 | loadTouchSoundAssetDefaults(); |
| 2284 | |
| 2285 | try { |
| 2286 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2287 | |
| 2288 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2289 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2290 | boolean inTouchSoundsGroup = false; |
| 2291 | |
| 2292 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2293 | while (true) { |
| 2294 | XmlUtils.nextElement(parser); |
| 2295 | String element = parser.getName(); |
| 2296 | if (element == null) { |
| 2297 | break; |
| 2298 | } |
| 2299 | if (element.equals(TAG_GROUP)) { |
| 2300 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2301 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2302 | inTouchSoundsGroup = true; |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | while (inTouchSoundsGroup) { |
| 2308 | XmlUtils.nextElement(parser); |
| 2309 | String element = parser.getName(); |
| 2310 | if (element == null) { |
| 2311 | break; |
| 2312 | } |
| 2313 | if (element.equals(TAG_ASSET)) { |
| 2314 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2315 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2316 | int fx; |
| 2317 | |
| 2318 | try { |
| 2319 | Field field = AudioManager.class.getField(id); |
| 2320 | fx = field.getInt(null); |
| 2321 | } catch (Exception e) { |
| 2322 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2323 | continue; |
| 2324 | } |
| 2325 | |
| 2326 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2327 | if (i == -1) { |
| 2328 | i = SOUND_EFFECT_FILES.size(); |
| 2329 | SOUND_EFFECT_FILES.add(file); |
| 2330 | } |
| 2331 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2332 | } else { |
| 2333 | break; |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | } catch (Resources.NotFoundException e) { |
| 2338 | Log.w(TAG, "audio assets file not found", e); |
| 2339 | } catch (XmlPullParserException e) { |
| 2340 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2341 | } catch (IOException e) { |
| 2342 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2343 | } finally { |
| 2344 | if (parser != null) { |
| 2345 | parser.close(); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2350 | /** @see AudioManager#playSoundEffect(int) */ |
| 2351 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2352 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2356 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2357 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2358 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2359 | return; |
| 2360 | } |
| 2361 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2362 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2363 | effectType, (int) (volume * 1000), null, 0); |
| 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2368 | * 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] | 2369 | */ |
| 2370 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2371 | int attempts = 3; |
| 2372 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2373 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2374 | synchronized (reply) { |
| 2375 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2376 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2377 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2378 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2379 | } catch (InterruptedException e) { |
| 2380 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2381 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2383 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2384 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * Unloads samples from the sound pool. |
| 2389 | * This method can be called to free some memory when |
| 2390 | * sound effects are disabled. |
| 2391 | */ |
| 2392 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2393 | 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] | 2394 | } |
| 2395 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2396 | class SoundPoolListenerThread extends Thread { |
| 2397 | public SoundPoolListenerThread() { |
| 2398 | super("SoundPoolListenerThread"); |
| 2399 | } |
| 2400 | |
| 2401 | @Override |
| 2402 | public void run() { |
| 2403 | |
| 2404 | Looper.prepare(); |
| 2405 | mSoundPoolLooper = Looper.myLooper(); |
| 2406 | |
| 2407 | synchronized (mSoundEffectsLock) { |
| 2408 | if (mSoundPool != null) { |
| 2409 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2410 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2411 | } |
| 2412 | mSoundEffectsLock.notify(); |
| 2413 | } |
| 2414 | Looper.loop(); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | private final class SoundPoolCallback implements |
| 2419 | android.media.SoundPool.OnLoadCompleteListener { |
| 2420 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2421 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2422 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2423 | |
| 2424 | public int status() { |
| 2425 | return mStatus; |
| 2426 | } |
| 2427 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2428 | public void setSamples(int[] samples) { |
| 2429 | for (int i = 0; i < samples.length; i++) { |
| 2430 | // do not wait ack for samples rejected upfront by SoundPool |
| 2431 | if (samples[i] > 0) { |
| 2432 | mSamples.add(samples[i]); |
| 2433 | } |
| 2434 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2438 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2439 | int i = mSamples.indexOf(sampleId); |
| 2440 | if (i >= 0) { |
| 2441 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2442 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2443 | if ((status != 0) || mSamples. isEmpty()) { |
| 2444 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2445 | mSoundEffectsLock.notify(); |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2451 | /** @see AudioManager#reloadAudioSettings() */ |
| 2452 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2453 | readAudioSettings(false /*userSwitch*/); |
| 2454 | } |
| 2455 | |
| 2456 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2457 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2458 | readPersistedSettings(); |
| 2459 | |
| 2460 | // restore volume settings |
| 2461 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2462 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2463 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2464 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2465 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2466 | continue; |
| 2467 | } |
| 2468 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2469 | streamState.readSettings(); |
| 2470 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2471 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2472 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2473 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2474 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2475 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2479 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2480 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2481 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2482 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2483 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2484 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2485 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2486 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2487 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2488 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2489 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2490 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2491 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2492 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2493 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2494 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2497 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2498 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2499 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2500 | return; |
| 2501 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2502 | |
| 2503 | if (on) { |
| 2504 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2505 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2506 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2507 | } |
| 2508 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2509 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2510 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2511 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2512 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2513 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2514 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2518 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2519 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2522 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2523 | public void setBluetoothScoOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2524 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2525 | return; |
| 2526 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2527 | |
| 2528 | if (on) { |
| 2529 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2530 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2531 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2532 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2533 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2534 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2535 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2536 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2537 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2541 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2542 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2545 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2546 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2547 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2548 | mBluetoothA2dpEnabled = on; |
| 2549 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2550 | AudioSystem.FOR_MEDIA, |
| 2551 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2552 | null, 0); |
| 2553 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2557 | public boolean isBluetoothA2dpOn() { |
| 2558 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2559 | return mBluetoothA2dpEnabled; |
| 2560 | } |
| 2561 | } |
| 2562 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2563 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2564 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2565 | int scoAudioMode = |
| 2566 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2567 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2568 | startBluetoothScoInt(cb, scoAudioMode); |
| 2569 | } |
| 2570 | |
| 2571 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2572 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2573 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2574 | } |
| 2575 | |
| 2576 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2577 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2578 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2579 | return; |
| 2580 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2581 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2582 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2583 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2584 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2585 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2586 | // client is created. |
| 2587 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2588 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2589 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | /** @see AudioManager#stopBluetoothSco() */ |
| 2593 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2594 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2595 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2596 | return; |
| 2597 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2598 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2599 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2600 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2601 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2602 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2603 | if (client != null) { |
| 2604 | client.decCount(); |
| 2605 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2606 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2609 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2610 | private class ScoClient implements IBinder.DeathRecipient { |
| 2611 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2612 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2613 | private int mStartcount; // number of SCO connections started by this client |
| 2614 | |
| 2615 | ScoClient(IBinder cb) { |
| 2616 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2617 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2618 | mStartcount = 0; |
| 2619 | } |
| 2620 | |
| 2621 | public void binderDied() { |
| 2622 | synchronized(mScoClients) { |
| 2623 | Log.w(TAG, "SCO client died"); |
| 2624 | int index = mScoClients.indexOf(this); |
| 2625 | if (index < 0) { |
| 2626 | Log.w(TAG, "unregistered SCO client died"); |
| 2627 | } else { |
| 2628 | clearCount(true); |
| 2629 | mScoClients.remove(this); |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2634 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2635 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2636 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2637 | if (mStartcount == 0) { |
| 2638 | try { |
| 2639 | mCb.linkToDeath(this, 0); |
| 2640 | } catch (RemoteException e) { |
| 2641 | // client has already died! |
| 2642 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2643 | } |
| 2644 | } |
| 2645 | mStartcount++; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | public void decCount() { |
| 2650 | synchronized(mScoClients) { |
| 2651 | if (mStartcount == 0) { |
| 2652 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2653 | } else { |
| 2654 | mStartcount--; |
| 2655 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2656 | try { |
| 2657 | mCb.unlinkToDeath(this, 0); |
| 2658 | } catch (NoSuchElementException e) { |
| 2659 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2660 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2661 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2662 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | public void clearCount(boolean stopSco) { |
| 2668 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2669 | if (mStartcount != 0) { |
| 2670 | try { |
| 2671 | mCb.unlinkToDeath(this, 0); |
| 2672 | } catch (NoSuchElementException e) { |
| 2673 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2674 | } |
| 2675 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2676 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2677 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2678 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | public int getCount() { |
| 2684 | return mStartcount; |
| 2685 | } |
| 2686 | |
| 2687 | public IBinder getBinder() { |
| 2688 | return mCb; |
| 2689 | } |
| 2690 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2691 | public int getPid() { |
| 2692 | return mCreatorPid; |
| 2693 | } |
| 2694 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2695 | public int totalCount() { |
| 2696 | synchronized(mScoClients) { |
| 2697 | int count = 0; |
| 2698 | int size = mScoClients.size(); |
| 2699 | for (int i = 0; i < size; i++) { |
| 2700 | count += mScoClients.get(i).getCount(); |
| 2701 | } |
| 2702 | return count; |
| 2703 | } |
| 2704 | } |
| 2705 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2706 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2707 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2708 | if (totalCount() == 0) { |
| 2709 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2710 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2711 | // the connection. |
| 2712 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2713 | // 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] | 2714 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2715 | synchronized(mSetModeDeathHandlers) { |
| 2716 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2717 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2718 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2719 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2720 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2721 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2722 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2723 | if (mBluetoothHeadsetDevice != null) { |
| 2724 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2725 | mContentResolver, |
| 2726 | "bluetooth_sco_channel_"+ |
| 2727 | mBluetoothHeadsetDevice.getAddress(), |
| 2728 | SCO_MODE_VIRTUAL_CALL)); |
| 2729 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2730 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2731 | } |
| 2732 | } else { |
| 2733 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2734 | } |
| 2735 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2736 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2737 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2738 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2739 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2740 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2741 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2742 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2743 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2744 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2745 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2746 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2747 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2748 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2749 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2750 | } else { |
| 2751 | broadcastScoConnectionState( |
| 2752 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2753 | } |
| 2754 | } else if (getBluetoothHeadset()) { |
| 2755 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2756 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2757 | } else { |
| 2758 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2759 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2760 | } |
| 2761 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2762 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2763 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2764 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2765 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2766 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2767 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2768 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2769 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2770 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2771 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2772 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2773 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2774 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2775 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2776 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2777 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2778 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2779 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2780 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2781 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2782 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2783 | broadcastScoConnectionState( |
| 2784 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2785 | } |
| 2786 | } else if (getBluetoothHeadset()) { |
| 2787 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2788 | } |
| 2789 | } else { |
| 2790 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2791 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2792 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2793 | } |
| 2794 | } |
| 2795 | } |
| 2796 | } |
| 2797 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2798 | private void checkScoAudioState() { |
| 2799 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2800 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2801 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2802 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2803 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2804 | } |
| 2805 | } |
| 2806 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2807 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2808 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2809 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2810 | int size = mScoClients.size(); |
| 2811 | for (int i = 0; i < size; i++) { |
| 2812 | client = mScoClients.get(i); |
| 2813 | if (client.getBinder() == cb) |
| 2814 | return client; |
| 2815 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2816 | if (create) { |
| 2817 | client = new ScoClient(cb); |
| 2818 | mScoClients.add(client); |
| 2819 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2820 | return client; |
| 2821 | } |
| 2822 | } |
| 2823 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2824 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2825 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2826 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2827 | int size = mScoClients.size(); |
| 2828 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2829 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2830 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2831 | cl.clearCount(stopSco); |
| 2832 | } else { |
| 2833 | savedClient = cl; |
| 2834 | } |
| 2835 | } |
| 2836 | mScoClients.clear(); |
| 2837 | if (savedClient != null) { |
| 2838 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | } |
| 2842 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2843 | private boolean getBluetoothHeadset() { |
| 2844 | boolean result = false; |
| 2845 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2846 | if (adapter != null) { |
| 2847 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2848 | BluetoothProfile.HEADSET); |
| 2849 | } |
| 2850 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2851 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2852 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2853 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2854 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2855 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2856 | return result; |
| 2857 | } |
| 2858 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2859 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2860 | synchronized(mScoClients) { |
| 2861 | checkScoAudioState(); |
| 2862 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2863 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2864 | if (mBluetoothHeadsetDevice != null) { |
| 2865 | if (mBluetoothHeadset != null) { |
| 2866 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2867 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2868 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2869 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2870 | } |
| 2871 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2872 | getBluetoothHeadset()) { |
| 2873 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2874 | } |
| 2875 | } |
| 2876 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2877 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | private void resetBluetoothSco() { |
| 2883 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2884 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2885 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2886 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2891 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 2892 | SENDMSG_QUEUE, state, 0, null, 0); |
| 2893 | } |
| 2894 | |
| 2895 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2896 | if (state != mScoConnectionState) { |
| 2897 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 2898 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 2899 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 2900 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2901 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2902 | mScoConnectionState = state; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 2906 | void setBtScoDeviceConnectionState(BluetoothDevice btDevice, int state) { |
| 2907 | if (btDevice == null) { |
| 2908 | return; |
| 2909 | } |
| 2910 | |
| 2911 | String address = btDevice.getAddress(); |
| 2912 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 2913 | int outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 2914 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 2915 | if (btClass != null) { |
| 2916 | switch (btClass.getDeviceClass()) { |
| 2917 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 2918 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 2919 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 2920 | break; |
| 2921 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 2922 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 2923 | break; |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 2928 | address = ""; |
| 2929 | } |
| 2930 | |
| 2931 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
| 2932 | |
| 2933 | String btDeviceName = btDevice.getName(); |
| 2934 | boolean success = |
| 2935 | handleDeviceConnection(connected, outDevice, address, btDeviceName) && |
| 2936 | handleDeviceConnection(connected, inDevice, address, btDeviceName); |
| 2937 | if (success) { |
| 2938 | synchronized (mScoClients) { |
| 2939 | if (connected) { |
| 2940 | mBluetoothHeadsetDevice = btDevice; |
| 2941 | } else { |
| 2942 | mBluetoothHeadsetDevice = null; |
| 2943 | resetBluetoothSco(); |
| 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2949 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 2950 | new BluetoothProfile.ServiceListener() { |
| 2951 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2952 | BluetoothDevice btDevice; |
| 2953 | List<BluetoothDevice> deviceList; |
| 2954 | switch(profile) { |
| 2955 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2956 | synchronized (mConnectedDevices) { |
| 2957 | synchronized (mA2dpAvrcpLock) { |
| 2958 | mA2dp = (BluetoothA2dp) proxy; |
| 2959 | deviceList = mA2dp.getConnectedDevices(); |
| 2960 | if (deviceList.size() > 0) { |
| 2961 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2962 | int state = mA2dp.getConnectionState(btDevice); |
| 2963 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2964 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 2965 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2966 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2967 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2968 | state, |
| 2969 | 0, |
| 2970 | btDevice, |
| 2971 | delay); |
| 2972 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 2973 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2974 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2975 | break; |
| 2976 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2977 | case BluetoothProfile.A2DP_SINK: |
| 2978 | deviceList = proxy.getConnectedDevices(); |
| 2979 | if (deviceList.size() > 0) { |
| 2980 | btDevice = deviceList.get(0); |
| 2981 | synchronized (mConnectedDevices) { |
| 2982 | int state = proxy.getConnectionState(btDevice); |
| 2983 | queueMsgUnderWakeLock(mAudioHandler, |
| 2984 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 2985 | state, |
| 2986 | 0, |
| 2987 | btDevice, |
| 2988 | 0 /* delay */); |
| 2989 | } |
| 2990 | } |
| 2991 | break; |
| 2992 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2993 | case BluetoothProfile.HEADSET: |
| 2994 | synchronized (mScoClients) { |
| 2995 | // Discard timeout message |
| 2996 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 2997 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 2998 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 2999 | if (deviceList.size() > 0) { |
| 3000 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 3001 | } else { |
| 3002 | mBluetoothHeadsetDevice = null; |
| 3003 | } |
| 3004 | // Refresh SCO audio state |
| 3005 | checkScoAudioState(); |
| 3006 | // Continue pending action if any |
| 3007 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 3008 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 3009 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3010 | boolean status = false; |
| 3011 | if (mBluetoothHeadsetDevice != null) { |
| 3012 | switch (mScoAudioState) { |
| 3013 | case SCO_STATE_ACTIVATE_REQ: |
| 3014 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3015 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3016 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3017 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3018 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3019 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3020 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3021 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3022 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3023 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3024 | break; |
| 3025 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3026 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3027 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3028 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3029 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3030 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3031 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3032 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3033 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3034 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3035 | break; |
| 3036 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 3037 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3038 | mBluetoothHeadsetDevice); |
| 3039 | } |
| 3040 | } |
| 3041 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3042 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3043 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3044 | } |
| 3045 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3046 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3047 | break; |
| 3048 | |
| 3049 | default: |
| 3050 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3051 | } |
| 3052 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3053 | public void onServiceDisconnected(int profile) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3054 | ArraySet<String> toRemove = null; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3055 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3056 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3057 | synchronized (mConnectedDevices) { |
| 3058 | synchronized (mA2dpAvrcpLock) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3059 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3060 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3061 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3062 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3063 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3064 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3065 | } |
| 3066 | } |
| 3067 | if (toRemove != null) { |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 3068 | int delay = checkSendBecomingNoisyIntent( |
| 3069 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3070 | 0); |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3071 | for (int i = 0; i < toRemove.size(); i++) { |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 3072 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3073 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3074 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | break; |
| 3078 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3079 | case BluetoothProfile.A2DP_SINK: |
| 3080 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3081 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3082 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3083 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3084 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3085 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3086 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3087 | } |
| 3088 | } |
| 3089 | if (toRemove != null) { |
| 3090 | for (int i = 0; i < toRemove.size(); i++) { |
| 3091 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3092 | } |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | break; |
| 3096 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3097 | case BluetoothProfile.HEADSET: |
| 3098 | synchronized (mScoClients) { |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3099 | if (mBluetoothHeadsetDevice != null) { |
| 3100 | setBtScoDeviceConnectionState(mBluetoothHeadsetDevice, |
| 3101 | BluetoothProfile.STATE_DISCONNECTED); |
| 3102 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3103 | mBluetoothHeadset = null; |
| 3104 | } |
| 3105 | break; |
| 3106 | |
| 3107 | default: |
| 3108 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3112 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3113 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3114 | synchronized (mSafeMediaVolumeState) { |
| 3115 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3116 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3117 | |
| 3118 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3119 | sendMsg(mAudioHandler, |
| 3120 | MSG_CHECK_MUSIC_ACTIVE, |
| 3121 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3122 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3123 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3124 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3125 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3126 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3127 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 3128 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3129 | // Approximate cumulative active music time |
| 3130 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3131 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3132 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3133 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3134 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3135 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3142 | private void saveMusicActiveMs() { |
| 3143 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3144 | } |
| 3145 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3146 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3147 | synchronized (mSafeMediaVolumeState) { |
| 3148 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3149 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3150 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3151 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3152 | boolean safeMediaVolumeEnabled = |
| 3153 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3154 | || mContext.getResources().getBoolean( |
| 3155 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3156 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3157 | boolean safeMediaVolumeBypass = |
| 3158 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3159 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3160 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3161 | // next time we boot and cannot be "inactive" |
| 3162 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3163 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3164 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3165 | // The state can already be "inactive" here if the user has forced it before |
| 3166 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3167 | // it to "active". |
| 3168 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3169 | if (mMusicActiveMs == 0) { |
| 3170 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3171 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3172 | } else { |
| 3173 | // We have existing playback time recorded, already confirmed. |
| 3174 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3175 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3176 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3177 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3178 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3179 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3180 | } |
| 3181 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3182 | sendMsg(mAudioHandler, |
| 3183 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3184 | SENDMSG_QUEUE, |
| 3185 | persistedState, |
| 3186 | 0, |
| 3187 | null, |
| 3188 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3193 | /////////////////////////////////////////////////////////////////////////// |
| 3194 | // Internal methods |
| 3195 | /////////////////////////////////////////////////////////////////////////// |
| 3196 | |
| 3197 | /** |
| 3198 | * Checks if the adjustment should change ringer mode instead of just |
| 3199 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3200 | * indices on the stream states. |
| 3201 | */ |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3202 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted) { |
| 3203 | final boolean isTv = mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3204 | int result = FLAG_ADJUST_VOLUME; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3205 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3206 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3207 | switch (ringerMode) { |
| 3208 | case RINGER_MODE_NORMAL: |
| 3209 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3210 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3211 | // "step" is the delta in internal index units corresponding to a |
| 3212 | // change of 1 in UI index units. |
| 3213 | // Because of rounding when rescaling from one stream index range to its alias |
| 3214 | // index range, we cannot simply test oldIndex == step: |
| 3215 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3216 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3217 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3218 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3219 | } |
| 3220 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3221 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3222 | ringerMode = RINGER_MODE_SILENT; |
| 3223 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3224 | } |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3225 | } else if (isTv && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3226 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3227 | if (mHasVibrator) { |
| 3228 | ringerMode = RINGER_MODE_VIBRATE; |
| 3229 | } else { |
| 3230 | ringerMode = RINGER_MODE_SILENT; |
| 3231 | } |
| 3232 | // Setting the ringer mode will toggle mute |
| 3233 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3234 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3235 | break; |
| 3236 | case RINGER_MODE_VIBRATE: |
| 3237 | if (!mHasVibrator) { |
| 3238 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3239 | "but no vibrator is present"); |
| 3240 | break; |
| 3241 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3242 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3243 | // This is the case we were muted with the volume turned up |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3244 | if (isTv && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3245 | ringerMode = RINGER_MODE_NORMAL; |
| 3246 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3247 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3248 | final long diff = SystemClock.uptimeMillis() |
| 3249 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3250 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 3251 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3252 | ringerMode = RINGER_MODE_SILENT; |
| 3253 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3254 | } else { |
| 3255 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3256 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3257 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3258 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3259 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3260 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3261 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3262 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3263 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3264 | break; |
| 3265 | case RINGER_MODE_SILENT: |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3266 | if (isTv && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3267 | // This is the case we were muted with the volume turned up |
| 3268 | ringerMode = RINGER_MODE_NORMAL; |
| 3269 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3270 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3271 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3272 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3273 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3274 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3275 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3276 | ringerMode = RINGER_MODE_VIBRATE; |
| 3277 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3278 | // If we don't have a vibrator or they were toggling mute |
| 3279 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3280 | ringerMode = RINGER_MODE_NORMAL; |
| 3281 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3282 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3283 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3284 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3285 | break; |
| 3286 | default: |
| 3287 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3288 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3289 | } |
| 3290 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3291 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3292 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3293 | mPrevVolDirection = direction; |
| 3294 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3295 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3296 | } |
| 3297 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3298 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3300 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3301 | } |
| 3302 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3303 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3304 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3305 | } |
| 3306 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3307 | private boolean updateRingerModeAffectedStreams() { |
| 3308 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3309 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3310 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3311 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3312 | UserHandle.USER_CURRENT); |
| 3313 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3314 | if (mPlatformType == AudioSystem.PLATFORM_TELEVISION) { |
| 3315 | ringerModeAffectedStreams = 0; |
| 3316 | } else if (mRingerModeDelegate != null) { |
| 3317 | ringerModeAffectedStreams = mRingerModeDelegate |
| 3318 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3319 | } |
| 3320 | synchronized (mCameraSoundForced) { |
| 3321 | if (mCameraSoundForced) { |
| 3322 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3323 | } else { |
| 3324 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3325 | } |
| 3326 | } |
| 3327 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3328 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3329 | } else { |
| 3330 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3331 | } |
| 3332 | |
| 3333 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3334 | Settings.System.putIntForUser(mContentResolver, |
| 3335 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3336 | ringerModeAffectedStreams, |
| 3337 | UserHandle.USER_CURRENT); |
| 3338 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3339 | return true; |
| 3340 | } |
| 3341 | return false; |
| 3342 | } |
| 3343 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3344 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3345 | public boolean isStreamAffectedByMute(int streamType) { |
| 3346 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3347 | } |
| 3348 | |
| 3349 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3350 | switch (direction) { |
| 3351 | case AudioManager.ADJUST_LOWER: |
| 3352 | case AudioManager.ADJUST_RAISE: |
| 3353 | case AudioManager.ADJUST_SAME: |
| 3354 | case AudioManager.ADJUST_MUTE: |
| 3355 | case AudioManager.ADJUST_UNMUTE: |
| 3356 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3357 | break; |
| 3358 | default: |
| 3359 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | private void ensureValidStreamType(int streamType) { |
| 3364 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3365 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3366 | } |
| 3367 | } |
| 3368 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3369 | private boolean isMuteAdjust(int adjust) { |
| 3370 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3371 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3372 | } |
| 3373 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3374 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3375 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3376 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3377 | TelecomManager telecomManager = |
| 3378 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3379 | |
| 3380 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3381 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3382 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3383 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3384 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3385 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3386 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3387 | /** |
| 3388 | * For code clarity for getActiveStreamType(int) |
| 3389 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3390 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3391 | * in the last "delay_ms" ms. |
| 3392 | */ |
| 3393 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3394 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3395 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3396 | } |
| 3397 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3398 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3399 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3400 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3401 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3402 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3403 | == AudioSystem.FORCE_BT_SCO) { |
| 3404 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3405 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3406 | } else { |
| 3407 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3408 | return AudioSystem.STREAM_VOICE_CALL; |
| 3409 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3410 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3411 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3412 | if (DEBUG_VOL) |
| 3413 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3414 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3415 | } else { |
| 3416 | if (DEBUG_VOL) |
| 3417 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3418 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3419 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3420 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3421 | if (DEBUG_VOL) |
| 3422 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3423 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3424 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3425 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3426 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3427 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 3428 | // TV always defaults to STREAM_MUSIC |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3429 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3430 | } |
| 3431 | break; |
| 3432 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3433 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3434 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3435 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3436 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3437 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3438 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3439 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3440 | return AudioSystem.STREAM_VOICE_CALL; |
| 3441 | } |
| 3442 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3443 | StreamOverride.sDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3444 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3445 | StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3446 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3447 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3448 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3449 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3450 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3451 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3452 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3453 | if (DEBUG_VOL) Log.v(TAG, |
| 3454 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3455 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3456 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3457 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3458 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3459 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3460 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3461 | + suggestedStreamType); |
| 3462 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3463 | } |
| 3464 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3465 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3466 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3467 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3468 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3469 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3470 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3471 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | private void broadcastVibrateSetting(int vibrateType) { |
| 3475 | // Send broadcast |
| 3476 | if (ActivityManagerNative.isSystemReady()) { |
| 3477 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3478 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3479 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3480 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3485 | /** |
| 3486 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3487 | * Note that the wake lock needs to be released after the message has been handled. |
| 3488 | */ |
| 3489 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3490 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3491 | final long ident = Binder.clearCallingIdentity(); |
| 3492 | // Always acquire the wake lock as AudioService because it is released by the |
| 3493 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3494 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3495 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3496 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3498 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3499 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3500 | 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] | 3501 | |
| 3502 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3503 | handler.removeMessages(msg); |
| 3504 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3505 | return; |
| 3506 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3507 | synchronized (mLastDeviceConnectMsgTime) { |
| 3508 | long time = SystemClock.uptimeMillis() + delay; |
| 3509 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3510 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3511 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3512 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3513 | mLastDeviceConnectMsgTime = time; |
| 3514 | } |
| 3515 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3519 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3520 | == PackageManager.PERMISSION_GRANTED) { |
| 3521 | return true; |
| 3522 | } |
| 3523 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3524 | + Binder.getCallingPid() |
| 3525 | + ", uid=" + Binder.getCallingUid(); |
| 3526 | Log.w(TAG, msg); |
| 3527 | return false; |
| 3528 | } |
| 3529 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3530 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3531 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3532 | if ((device & (device - 1)) != 0) { |
| 3533 | // Multiple device selection is either: |
| 3534 | // - speaker + one other device: give priority to speaker in this case. |
| 3535 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3536 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3537 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3538 | // - 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] | 3539 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3540 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3541 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3542 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3543 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3544 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3545 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3546 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3547 | } else { |
| 3548 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3549 | } |
| 3550 | } |
| 3551 | return device; |
| 3552 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3553 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3554 | private int getDevicesForStream(int stream) { |
| 3555 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 3556 | } |
| 3557 | |
| 3558 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 3559 | ensureValidStreamType(stream); |
| 3560 | synchronized (VolumeStreamState.class) { |
| 3561 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | private void observeDevicesForStreams(int skipStream) { |
| 3566 | synchronized (VolumeStreamState.class) { |
| 3567 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 3568 | if (stream != skipStream) { |
| 3569 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 3570 | } |
| 3571 | } |
| 3572 | } |
| 3573 | } |
| 3574 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3575 | /* |
| 3576 | * A class just for packaging up a set of connection parameters. |
| 3577 | */ |
| 3578 | private class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3579 | public final int mType; |
| 3580 | public final int mState; |
| 3581 | public final String mAddress; |
| 3582 | public final String mName; |
| 3583 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3584 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3585 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 3586 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3587 | mType = type; |
| 3588 | mState = state; |
| 3589 | mAddress = address; |
| 3590 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3591 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3592 | } |
| 3593 | } |
| 3594 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3595 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 3596 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3597 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3598 | if (DEBUG_DEVICES) { |
| 3599 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 3600 | + address + ")"); |
| 3601 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3602 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3603 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3604 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3605 | 0, |
| 3606 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3607 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3608 | delay); |
| 3609 | } |
| 3610 | } |
| 3611 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3612 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3613 | { |
| 3614 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3615 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3616 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3617 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3618 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3619 | if (profile == BluetoothProfile.A2DP) { |
| 3620 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3621 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3622 | } else { |
| 3623 | delay = 0; |
| 3624 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3625 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3626 | (profile == BluetoothProfile.A2DP ? |
| 3627 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3628 | state, |
| 3629 | 0, |
| 3630 | device, |
| 3631 | delay); |
| 3632 | } |
| 3633 | return delay; |
| 3634 | } |
| 3635 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3636 | /////////////////////////////////////////////////////////////////////////// |
| 3637 | // Inner classes |
| 3638 | /////////////////////////////////////////////////////////////////////////// |
| 3639 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3640 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3641 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3642 | // 2 mSetModeDeathHandlers |
| 3643 | // 3 mSettingsLock |
| 3644 | // 4 VolumeStreamState.class |
| 3645 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3646 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3647 | private final int mStreamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3648 | private final int mIndexMin; |
| 3649 | private final int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3650 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3651 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3652 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3653 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3654 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3655 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3656 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3657 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3658 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3659 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3660 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3661 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3662 | |
| 3663 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3664 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 3665 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 3666 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3667 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3668 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3669 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3670 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3671 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 3672 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3673 | } |
| 3674 | |
| 3675 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 3676 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 3677 | if (devices == mObservedDevices) { |
| 3678 | return devices; |
| 3679 | } |
| 3680 | final int prevDevices = mObservedDevices; |
| 3681 | mObservedDevices = devices; |
| 3682 | if (checkOthers) { |
| 3683 | // one stream's devices have changed, check the others |
| 3684 | observeDevicesForStreams(mStreamType); |
| 3685 | } |
| 3686 | // log base stream changes to the event log |
| 3687 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 3688 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 3689 | } |
| 3690 | sendBroadcastToAll(mStreamDevicesChanged |
| 3691 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 3692 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 3693 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3694 | } |
| 3695 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3696 | public String getSettingNameForDevice(int device) { |
| 3697 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3698 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3699 | if (suffix.isEmpty()) { |
| 3700 | return name; |
| 3701 | } |
| 3702 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3705 | public void readSettings() { |
| 3706 | synchronized (VolumeStreamState.class) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3707 | // force maximum volume on all streams if fixed volume property is set |
| 3708 | if (mUseFixedVolume) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3709 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3710 | return; |
| 3711 | } |
| 3712 | // do not read system stream volume from settings: this stream is always aliased |
| 3713 | // to another stream type and its volume is never persisted. Values in settings can |
| 3714 | // only be stale values |
| 3715 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3716 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3717 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3718 | synchronized (mCameraSoundForced) { |
| 3719 | if (mCameraSoundForced) { |
| 3720 | index = mIndexMax; |
| 3721 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3722 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3723 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3724 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3725 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3726 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3727 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3728 | |
| 3729 | for (int i = 0; remainingDevices != 0; i++) { |
| 3730 | int device = (1 << i); |
| 3731 | if ((device & remainingDevices) == 0) { |
| 3732 | continue; |
| 3733 | } |
| 3734 | remainingDevices &= ~device; |
| 3735 | |
| 3736 | // retrieve current volume for device |
| 3737 | String name = getSettingNameForDevice(device); |
| 3738 | // if no volume stored for current stream and device, use default volume if default |
| 3739 | // device, continue otherwise |
| 3740 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3741 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3742 | int index = Settings.System.getIntForUser( |
| 3743 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3744 | if (index == -1) { |
| 3745 | continue; |
| 3746 | } |
| 3747 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3748 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3749 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3750 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | } |
| 3752 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3753 | // must be called while synchronized VolumeStreamState.class |
| 3754 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3755 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3756 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3757 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3758 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) |
| 3759 | || ((device & mFullVolumeDevices) != 0)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3760 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3761 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3762 | index = (getIndex(device) + 5)/10; |
| 3763 | } |
| 3764 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3767 | public void applyAllVolumes() { |
| 3768 | synchronized (VolumeStreamState.class) { |
| 3769 | // apply default volume first: by convention this will reset all |
| 3770 | // devices volumes in audio policy manager to the supplied value |
| 3771 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3772 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3773 | index = 0; |
| 3774 | } else { |
| 3775 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3776 | } |
| 3777 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3778 | // then apply device specific volumes |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3779 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3780 | int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3781 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3782 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3783 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3784 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3785 | mAvrcpAbsVolSupported) |
| 3786 | || ((device & mFullVolumeDevices) != 0)) |
| 3787 | { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3788 | index = (mIndexMax + 5)/10; |
| 3789 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3790 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3791 | } |
| 3792 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3793 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3794 | } |
| 3795 | } |
| 3796 | } |
| 3797 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3798 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 3799 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3800 | } |
| 3801 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3802 | public boolean setIndex(int index, int device, String caller) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3803 | boolean changed = false; |
| 3804 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3805 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3806 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3807 | index = getValidIndex(index); |
| 3808 | synchronized (mCameraSoundForced) { |
| 3809 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3810 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3811 | } |
| 3812 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3813 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3814 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3815 | changed = oldIndex != index; |
| 3816 | if (changed) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3817 | // Apply change to all streams using this one as alias |
| 3818 | // if changing volume of current device, also change volume of current |
| 3819 | // device on aliased stream |
| 3820 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3821 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3822 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3823 | if (streamType != mStreamType && |
| 3824 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3825 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3826 | mStreamStates[streamType].setIndex(scaledIndex, device, caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3827 | if (currentDevice) { |
| 3828 | mStreamStates[streamType].setIndex(scaledIndex, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3829 | getDeviceForStream(streamType), caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3833 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3834 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3835 | if (changed) { |
| 3836 | oldIndex = (oldIndex + 5) / 10; |
| 3837 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3838 | // log base stream changes to the event log |
| 3839 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 3840 | if (caller == null) { |
| 3841 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 3842 | } |
| 3843 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 3844 | caller); |
| 3845 | } |
| 3846 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3847 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 3848 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 3849 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 3850 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3851 | sendBroadcastToAll(mVolumeChanged); |
| 3852 | } |
| 3853 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3856 | public int getIndex(int device) { |
| 3857 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3858 | int index = mIndexMap.get(device, -1); |
| 3859 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3860 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3861 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3862 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3863 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3864 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3868 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3869 | } |
| 3870 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3871 | public int getMinIndex() { |
| 3872 | return mIndexMin; |
| 3873 | } |
| 3874 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3875 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3876 | synchronized (VolumeStreamState.class) { |
| 3877 | int srcStreamType = srcStream.getStreamType(); |
| 3878 | // apply default device volume from source stream to all devices first in case |
| 3879 | // some devices are present in this stream state but not in source stream state |
| 3880 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3881 | index = rescaleIndex(index, srcStreamType, mStreamType); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3882 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3883 | mIndexMap.put(mIndexMap.keyAt(i), index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3884 | } |
| 3885 | // Now apply actual volume for devices in source stream state |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3886 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 3887 | for (int i = 0; i < srcMap.size(); i++) { |
| 3888 | int device = srcMap.keyAt(i); |
| 3889 | index = srcMap.valueAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3890 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3891 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3892 | setIndex(index, device, caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3893 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3897 | public void setAllIndexesToMax() { |
| 3898 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3899 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3900 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3901 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3902 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3905 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3906 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3907 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3908 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3909 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3910 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3911 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3912 | // Set the new mute volume. This propagates the values to |
| 3913 | // the audio system, otherwise the volume won't be changed |
| 3914 | // at the lower level. |
| 3915 | sendMsg(mAudioHandler, |
| 3916 | MSG_SET_ALL_VOLUMES, |
| 3917 | SENDMSG_QUEUE, |
| 3918 | 0, |
| 3919 | 0, |
| 3920 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3921 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3922 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3923 | if (changed) { |
| 3924 | // Stream mute changed, fire the intent. |
| 3925 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 3926 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3927 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 3928 | sendBroadcastToAll(intent); |
| 3929 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3930 | } |
| 3931 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3932 | public int getStreamType() { |
| 3933 | return mStreamType; |
| 3934 | } |
| 3935 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3936 | public void checkFixedVolumeDevices() { |
| 3937 | synchronized (VolumeStreamState.class) { |
| 3938 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 3939 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3940 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3941 | int device = mIndexMap.keyAt(i); |
| 3942 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3943 | if (((device & mFullVolumeDevices) != 0) |
| 3944 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3945 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3946 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3947 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3954 | if (index < mIndexMin) { |
| 3955 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3956 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3957 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | return index; |
| 3961 | } |
| 3962 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3963 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3964 | pw.print(" Muted: "); |
| 3965 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3966 | pw.print(" Min: "); |
| 3967 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3968 | pw.print(" Max: "); |
| 3969 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3970 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3971 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3972 | if (i > 0) { |
| 3973 | pw.print(", "); |
| 3974 | } |
| 3975 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3976 | pw.print(Integer.toHexString(device)); |
| 3977 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 3978 | : AudioSystem.getOutputDeviceName(device); |
| 3979 | if (!deviceName.isEmpty()) { |
| 3980 | pw.print(" ("); |
| 3981 | pw.print(deviceName); |
| 3982 | pw.print(")"); |
| 3983 | } |
| 3984 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3985 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3986 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3987 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 3988 | pw.println(); |
| 3989 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3990 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 3991 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 3992 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 3993 | // (the default device is not returned by getDevicesForStream) |
| 3994 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 3995 | if ((devices & device) != 0) { |
| 3996 | if (n++ > 0) { |
| 3997 | pw.print(", "); |
| 3998 | } |
| 3999 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 4000 | } |
| 4001 | i++; |
| 4002 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | /** Thread that handles native AudioSystem control. */ |
| 4007 | private class AudioSystemThread extends Thread { |
| 4008 | AudioSystemThread() { |
| 4009 | super("AudioService"); |
| 4010 | } |
| 4011 | |
| 4012 | @Override |
| 4013 | public void run() { |
| 4014 | // Set this thread up so the handler will work on it |
| 4015 | Looper.prepare(); |
| 4016 | |
| 4017 | synchronized(AudioService.this) { |
| 4018 | mAudioHandler = new AudioHandler(); |
| 4019 | |
| 4020 | // Notify that the handler has been created |
| 4021 | AudioService.this.notify(); |
| 4022 | } |
| 4023 | |
| 4024 | // Listen for volume change requests that are set by VolumePanel |
| 4025 | Looper.loop(); |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | /** Handles internal volume messages in separate volume thread. */ |
| 4030 | private class AudioHandler extends Handler { |
| 4031 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4032 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4034 | synchronized (VolumeStreamState.class) { |
| 4035 | // Apply volume |
| 4036 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4037 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4038 | // Apply change to all streams using this one as alias |
| 4039 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4040 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4041 | if (streamType != streamState.mStreamType && |
| 4042 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 4043 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 4044 | // that may have a different device selected |
| 4045 | int streamDevice = getDeviceForStream(streamType); |
| 4046 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 4047 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 4048 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 4049 | } |
| 4050 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4051 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4052 | } |
| 4053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4054 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4055 | sendMsg(mAudioHandler, |
| 4056 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 4057 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4058 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4059 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4060 | streamState, |
| 4061 | PERSIST_DELAY); |
| 4062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4063 | } |
| 4064 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4065 | private void setAllVolumes(VolumeStreamState streamState) { |
| 4066 | |
| 4067 | // Apply volume |
| 4068 | streamState.applyAllVolumes(); |
| 4069 | |
| 4070 | // Apply change to all streams using this one as alias |
| 4071 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4072 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4073 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4074 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4075 | mStreamStates[streamType].applyAllVolumes(); |
| 4076 | } |
| 4077 | } |
| 4078 | } |
| 4079 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4080 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4081 | if (mUseFixedVolume) { |
| 4082 | return; |
| 4083 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4084 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 4085 | return; |
| 4086 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4087 | System.putIntForUser(mContentResolver, |
| 4088 | streamState.getSettingNameForDevice(device), |
| 4089 | (streamState.getIndex(device) + 5)/ 10, |
| 4090 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4093 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4094 | if (mUseFixedVolume) { |
| 4095 | return; |
| 4096 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 4097 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4098 | } |
| 4099 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4100 | private boolean onLoadSoundEffects() { |
| 4101 | int status; |
| 4102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4103 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4104 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4105 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 4106 | return false; |
| 4107 | } |
| 4108 | |
| 4109 | if (mSoundPool != null) { |
| 4110 | return true; |
| 4111 | } |
| 4112 | |
| 4113 | loadTouchSoundAssets(); |
| 4114 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 4115 | mSoundPool = new SoundPool.Builder() |
| 4116 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 4117 | .setAudioAttributes(new AudioAttributes.Builder() |
| 4118 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 4119 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 4120 | .build()) |
| 4121 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4122 | mSoundPoolCallBack = null; |
| 4123 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 4124 | mSoundPoolListenerThread.start(); |
| 4125 | int attempts = 3; |
| 4126 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 4127 | try { |
| 4128 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4129 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4130 | } catch (InterruptedException e) { |
| 4131 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | if (mSoundPoolCallBack == null) { |
| 4136 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 4137 | if (mSoundPoolLooper != null) { |
| 4138 | mSoundPoolLooper.quit(); |
| 4139 | mSoundPoolLooper = null; |
| 4140 | } |
| 4141 | mSoundPoolListenerThread = null; |
| 4142 | mSoundPool.release(); |
| 4143 | mSoundPool = null; |
| 4144 | return false; |
| 4145 | } |
| 4146 | /* |
| 4147 | * poolId table: The value -1 in this table indicates that corresponding |
| 4148 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 4149 | * Once loaded, the value in poolId is the sample ID and the same |
| 4150 | * sample can be reused for another effect using the same file. |
| 4151 | */ |
| 4152 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4153 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4154 | poolId[fileIdx] = -1; |
| 4155 | } |
| 4156 | /* |
| 4157 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 4158 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 4159 | * this indicates we have a valid sample loaded for this effect. |
| 4160 | */ |
| 4161 | |
| 4162 | int numSamples = 0; |
| 4163 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4164 | // Do not load sample if this effect uses the MediaPlayer |
| 4165 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 4166 | continue; |
| 4167 | } |
| 4168 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 4169 | String filePath = Environment.getRootDirectory() |
| 4170 | + SOUND_EFFECTS_PATH |
| 4171 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 4172 | int sampleId = mSoundPool.load(filePath, 0); |
| 4173 | if (sampleId <= 0) { |
| 4174 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 4175 | } else { |
| 4176 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 4177 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 4178 | numSamples++; |
| 4179 | } |
| 4180 | } else { |
| 4181 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 4182 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 4183 | } |
| 4184 | } |
| 4185 | // wait for all samples to be loaded |
| 4186 | if (numSamples > 0) { |
| 4187 | mSoundPoolCallBack.setSamples(poolId); |
| 4188 | |
| 4189 | attempts = 3; |
| 4190 | status = 1; |
| 4191 | while ((status == 1) && (attempts-- > 0)) { |
| 4192 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4193 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4194 | status = mSoundPoolCallBack.status(); |
| 4195 | } catch (InterruptedException e) { |
| 4196 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 4197 | } |
| 4198 | } |
| 4199 | } else { |
| 4200 | status = -1; |
| 4201 | } |
| 4202 | |
| 4203 | if (mSoundPoolLooper != null) { |
| 4204 | mSoundPoolLooper.quit(); |
| 4205 | mSoundPoolLooper = null; |
| 4206 | } |
| 4207 | mSoundPoolListenerThread = null; |
| 4208 | if (status != 0) { |
| 4209 | Log.w(TAG, |
| 4210 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4211 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4212 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4213 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | mSoundPool.release(); |
| 4218 | mSoundPool = null; |
| 4219 | } |
| 4220 | } |
| 4221 | return (status == 0); |
| 4222 | } |
| 4223 | |
| 4224 | /** |
| 4225 | * Unloads samples from the sound pool. |
| 4226 | * This method can be called to free some memory when |
| 4227 | * sound effects are disabled. |
| 4228 | */ |
| 4229 | private void onUnloadSoundEffects() { |
| 4230 | synchronized (mSoundEffectsLock) { |
| 4231 | if (mSoundPool == null) { |
| 4232 | return; |
| 4233 | } |
| 4234 | |
| 4235 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4236 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4237 | poolId[fileIdx] = 0; |
| 4238 | } |
| 4239 | |
| 4240 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4241 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4242 | continue; |
| 4243 | } |
| 4244 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4245 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4246 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4247 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4248 | } |
| 4249 | } |
| 4250 | mSoundPool.release(); |
| 4251 | mSoundPool = null; |
| 4252 | } |
| 4253 | } |
| 4254 | |
| 4255 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4256 | synchronized (mSoundEffectsLock) { |
| 4257 | |
| 4258 | onLoadSoundEffects(); |
| 4259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4260 | if (mSoundPool == null) { |
| 4261 | return; |
| 4262 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4263 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4264 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4265 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4266 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4267 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4268 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4270 | |
| 4271 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4272 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4273 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4274 | } else { |
| 4275 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4276 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 4277 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 4278 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4279 | mediaPlayer.setDataSource(filePath); |
| 4280 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4281 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4282 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4283 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4284 | public void onCompletion(MediaPlayer mp) { |
| 4285 | cleanupPlayer(mp); |
| 4286 | } |
| 4287 | }); |
| 4288 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4289 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4290 | cleanupPlayer(mp); |
| 4291 | return true; |
| 4292 | } |
| 4293 | }); |
| 4294 | mediaPlayer.start(); |
| 4295 | } catch (IOException ex) { |
| 4296 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4297 | } catch (IllegalArgumentException ex) { |
| 4298 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4299 | } catch (IllegalStateException ex) { |
| 4300 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4301 | } |
| 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | private void cleanupPlayer(MediaPlayer mp) { |
| 4307 | if (mp != null) { |
| 4308 | try { |
| 4309 | mp.stop(); |
| 4310 | mp.release(); |
| 4311 | } catch (IllegalStateException ex) { |
| 4312 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4313 | } |
| 4314 | } |
| 4315 | } |
| 4316 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4317 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4318 | synchronized (mConnectedDevices) { |
| 4319 | setForceUseInt_SyncDevices(usage, config); |
| 4320 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4321 | } |
| 4322 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4323 | private void onPersistSafeVolumeState(int state) { |
| 4324 | Settings.Global.putInt(mContentResolver, |
| 4325 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4326 | state); |
| 4327 | } |
| 4328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4329 | @Override |
| 4330 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4331 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4332 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4333 | case MSG_SET_DEVICE_VOLUME: |
| 4334 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4335 | break; |
| 4336 | |
| 4337 | case MSG_SET_ALL_VOLUMES: |
| 4338 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4339 | break; |
| 4340 | |
| 4341 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4342 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4343 | break; |
| 4344 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4345 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4346 | if (mUseFixedVolume) { |
| 4347 | return; |
| 4348 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4349 | Settings.System.putIntForUser(mContentResolver, |
| 4350 | Settings.System.VOLUME_MASTER_MUTE, |
| 4351 | msg.arg1, |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4352 | msg.arg2); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4353 | break; |
| 4354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4355 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4356 | // note that the value persisted is the current ringer mode, not the |
| 4357 | // 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] | 4358 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | break; |
| 4360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4361 | case MSG_MEDIA_SERVER_DIED: |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 4362 | onMediaServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4363 | break; |
| 4364 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4365 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4366 | onUnloadSoundEffects(); |
| 4367 | break; |
| 4368 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4369 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4370 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4371 | // can take several dozens of milliseconds to complete |
| 4372 | boolean loaded = onLoadSoundEffects(); |
| 4373 | if (msg.obj != null) { |
| 4374 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4375 | synchronized (reply) { |
| 4376 | reply.mStatus = loaded ? 0 : -1; |
| 4377 | reply.notify(); |
| 4378 | } |
| 4379 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4380 | break; |
| 4381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4382 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4383 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4384 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4385 | |
| 4386 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4387 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4388 | synchronized (mConnectedDevices) { |
| 4389 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4390 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4391 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4392 | |
| 4393 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4394 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4395 | setForceUse(msg.arg1, msg.arg2); |
| 4396 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4397 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4398 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4399 | resetBluetoothSco(); |
| 4400 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4401 | |
| 4402 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4403 | { WiredDeviceConnectionState connectState = |
| 4404 | (WiredDeviceConnectionState)msg.obj; |
| 4405 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4406 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4407 | mAudioEventWakeLock.release(); |
| 4408 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4409 | break; |
| 4410 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4411 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4412 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4413 | mAudioEventWakeLock.release(); |
| 4414 | break; |
| 4415 | |
| 4416 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4417 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4418 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4419 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4420 | |
| 4421 | case MSG_REPORT_NEW_ROUTES: { |
| 4422 | int N = mRoutesObservers.beginBroadcast(); |
| 4423 | if (N > 0) { |
| 4424 | AudioRoutesInfo routes; |
| 4425 | synchronized (mCurAudioRoutes) { |
| 4426 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4427 | } |
| 4428 | while (N > 0) { |
| 4429 | N--; |
| 4430 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4431 | try { |
| 4432 | obs.dispatchAudioRoutesChanged(routes); |
| 4433 | } catch (RemoteException e) { |
| 4434 | } |
| 4435 | } |
| 4436 | } |
| 4437 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4438 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4439 | break; |
| 4440 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4441 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4442 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4443 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4444 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4445 | |
| 4446 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4447 | onSendBecomingNoisyIntent(); |
| 4448 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4449 | |
| 4450 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4451 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4452 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 4453 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4454 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4455 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4456 | onPersistSafeVolumeState(msg.arg1); |
| 4457 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4458 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4459 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4460 | onBroadcastScoConnectionState(msg.arg1); |
| 4461 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4462 | |
| 4463 | case MSG_SYSTEM_READY: |
| 4464 | onSystemReady(); |
| 4465 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4466 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 4467 | case MSG_INDICATE_SYSTEM_READY: |
| 4468 | onIndicateSystemReady(); |
| 4469 | break; |
| 4470 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4471 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4472 | final int musicActiveMs = msg.arg1; |
| 4473 | Settings.Secure.putIntForUser(mContentResolver, |
| 4474 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4475 | UserHandle.USER_CURRENT); |
| 4476 | break; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4477 | case MSG_PERSIST_MICROPHONE_MUTE: |
| 4478 | Settings.System.putIntForUser(mContentResolver, |
| 4479 | Settings.System.MICROPHONE_MUTE, |
| 4480 | msg.arg1, |
| 4481 | msg.arg2); |
| 4482 | break; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4483 | case MSG_UNMUTE_STREAM: |
| 4484 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4485 | break; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 4486 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 4487 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 4488 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | } |
| 4492 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4493 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4494 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4495 | SettingsObserver() { |
| 4496 | super(new Handler()); |
| 4497 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4498 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4499 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4500 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | @Override |
| 4504 | public void onChange(boolean selfChange) { |
| 4505 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4506 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4507 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4508 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4509 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4510 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4511 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4512 | /* |
| 4513 | * Ensure all stream types that should be affected by ringer mode |
| 4514 | * are in the proper state. |
| 4515 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4516 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4517 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4518 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4519 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4520 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4521 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4522 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4523 | // must be called synchronized on mConnectedDevices |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4524 | private void makeA2dpDeviceAvailable(String address, String name) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4525 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4526 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4527 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4528 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4529 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4530 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4531 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4532 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4533 | // Reset A2DP suspend state each time a new sink is connected |
| 4534 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4535 | mConnectedDevices.put( |
| 4536 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4537 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4538 | address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4539 | } |
| 4540 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4541 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4542 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4545 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4546 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4547 | synchronized (mA2dpAvrcpLock) { |
| 4548 | mAvrcpAbsVolSupported = false; |
| 4549 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4550 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4551 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4552 | mConnectedDevices.remove( |
| 4553 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4554 | synchronized (mCurAudioRoutes) { |
| 4555 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4556 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4557 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4558 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4559 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4560 | } |
| 4561 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4564 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4565 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4566 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4567 | // reconnection of the sink. |
| 4568 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4569 | // the device will be made unavailable later, so consider it disconnected right away |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4570 | mConnectedDevices.remove( |
| 4571 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4572 | // send the delayed message to make the device unavailable later |
| 4573 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4574 | mAudioHandler.sendMessageDelayed(msg, delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4575 | |
| 4576 | } |
| 4577 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4578 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4579 | private void makeA2dpSrcAvailable(String address) { |
| 4580 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4581 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4582 | mConnectedDevices.put( |
| 4583 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4584 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4585 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | // must be called synchronized on mConnectedDevices |
| 4589 | private void makeA2dpSrcUnavailable(String address) { |
| 4590 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4591 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4592 | mConnectedDevices.remove( |
| 4593 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4597 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4598 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4599 | } |
| 4600 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4601 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4602 | private boolean hasScheduledA2dpDockTimeout() { |
| 4603 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4604 | } |
| 4605 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4606 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4607 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4608 | if (DEBUG_VOL) { |
| 4609 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4610 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4611 | if (btDevice == null) { |
| 4612 | return; |
| 4613 | } |
| 4614 | String address = btDevice.getAddress(); |
| 4615 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4616 | address = ""; |
| 4617 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4618 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4619 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4620 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4621 | btDevice.getAddress()); |
| 4622 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4623 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4624 | |
| 4625 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4626 | if (btDevice.isBluetoothDock()) { |
| 4627 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4628 | // introduction of a delay for transient disconnections of docks when |
| 4629 | // power is rapidly turned off/on, this message will be canceled if |
| 4630 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4631 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4632 | // the next time isConnected is evaluated, it will be false for the dock |
| 4633 | } |
| 4634 | } else { |
| 4635 | makeA2dpDeviceUnavailableNow(address); |
| 4636 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4637 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4638 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4639 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4640 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4641 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4642 | } |
| 4643 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4644 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4645 | if (btDevice.isBluetoothDock()) { |
| 4646 | // this could be a reconnection after a transient disconnection |
| 4647 | cancelA2dpDeviceTimeout(); |
| 4648 | mDockAddress = address; |
| 4649 | } else { |
| 4650 | // this could be a connection of another A2DP device before the timeout of |
| 4651 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4652 | if(hasScheduledA2dpDockTimeout()) { |
| 4653 | cancelA2dpDeviceTimeout(); |
| 4654 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4655 | } |
| 4656 | } |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4657 | makeA2dpDeviceAvailable(address, btDevice.getName()); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4658 | synchronized (mCurAudioRoutes) { |
| 4659 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4660 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4661 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4662 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4663 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4664 | } |
| 4665 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4670 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4671 | { |
| 4672 | if (DEBUG_VOL) { |
| 4673 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4674 | } |
| 4675 | if (btDevice == null) { |
| 4676 | return; |
| 4677 | } |
| 4678 | String address = btDevice.getAddress(); |
| 4679 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4680 | address = ""; |
| 4681 | } |
| 4682 | |
| 4683 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4684 | String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 4685 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4686 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4687 | |
| 4688 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4689 | makeA2dpSrcUnavailable(address); |
| 4690 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4691 | makeA2dpSrcAvailable(address); |
| 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4696 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4697 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4698 | synchronized (mA2dpAvrcpLock) { |
| 4699 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4700 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4701 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4702 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4703 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4704 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4705 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4709 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 4710 | String deviceName) { |
| 4711 | if (DEBUG_DEVICES) { |
| 4712 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 4713 | + " address:" + address + " name:" + deviceName + ")"); |
| 4714 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4715 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4716 | String deviceKey = makeDeviceListKey(device, address); |
| 4717 | if (DEBUG_DEVICES) { |
| 4718 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 4719 | } |
| 4720 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 4721 | boolean isConnected = deviceSpec != null; |
| 4722 | if (DEBUG_DEVICES) { |
| 4723 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 4724 | } |
| 4725 | if (connect && !isConnected) { |
| 4726 | AudioSystem.setDeviceConnectionState(device, AudioSystem.DEVICE_STATE_AVAILABLE, |
| 4727 | address, deviceName); |
| 4728 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
| 4729 | return true; |
| 4730 | } else if (!connect && isConnected) { |
| 4731 | AudioSystem.setDeviceConnectionState(device, AudioSystem.DEVICE_STATE_UNAVAILABLE, |
| 4732 | address, deviceName); |
| 4733 | mConnectedDevices.remove(deviceKey); |
| 4734 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | return false; |
| 4738 | } |
| 4739 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4740 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4741 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4742 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4743 | int mBecomingNoisyIntentDevices = |
| 4744 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4745 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4746 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4747 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4748 | |
| 4749 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4750 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4751 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4752 | int delay = 0; |
| 4753 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4754 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 4755 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 4756 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4757 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 4758 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 4759 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4760 | } |
| 4761 | } |
| 4762 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4763 | sendMsg(mAudioHandler, |
| 4764 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4765 | SENDMSG_REPLACE, |
| 4766 | 0, |
| 4767 | 0, |
| 4768 | null, |
| 4769 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4770 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4771 | } |
| 4772 | } |
| 4773 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4774 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4775 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4776 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4777 | synchronized (mLastDeviceConnectMsgTime) { |
| 4778 | long time = SystemClock.uptimeMillis(); |
| 4779 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 4780 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4781 | } |
| 4782 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4783 | } |
| 4784 | return delay; |
| 4785 | } |
| 4786 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4787 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 4788 | String deviceName) { |
| 4789 | if (DEBUG_DEVICES) { |
| 4790 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 4791 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 4792 | " name:" + deviceName + ");"); |
| 4793 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4794 | Intent intent = new Intent(); |
| 4795 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4796 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 4797 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 4798 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 4799 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4800 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4801 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4802 | int connType = 0; |
| 4803 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4804 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4805 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4806 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4807 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4808 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 4809 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 4810 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4811 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4812 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4813 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4814 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 4815 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4816 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4817 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4818 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 4819 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4822 | synchronized (mCurAudioRoutes) { |
| 4823 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4824 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4825 | if (state != 0) { |
| 4826 | newConn |= connType; |
| 4827 | } else { |
| 4828 | newConn &= ~connType; |
| 4829 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4830 | if (newConn != mCurAudioRoutes.mainType) { |
| 4831 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4832 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4833 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4834 | } |
| 4835 | } |
| 4836 | } |
| 4837 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4838 | final long ident = Binder.clearCallingIdentity(); |
| 4839 | try { |
| 4840 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4841 | } finally { |
| 4842 | Binder.restoreCallingIdentity(ident); |
| 4843 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4844 | } |
| 4845 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4846 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4847 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4848 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4849 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 4850 | + " state:" + Integer.toHexString(state) |
| 4851 | + " address:" + address |
| 4852 | + " deviceName:" + deviceName |
| 4853 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4854 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4855 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4856 | synchronized (mConnectedDevices) { |
| 4857 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4858 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4859 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4860 | setBluetoothA2dpOnInt(true); |
| 4861 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4862 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4863 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4864 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4865 | handleDeviceConnection(state == 1, device, address, deviceName); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4866 | if (state != 0) { |
| 4867 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4868 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4869 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4870 | setBluetoothA2dpOnInt(false); |
| 4871 | } |
| 4872 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4873 | sendMsg(mAudioHandler, |
| 4874 | MSG_CHECK_MUSIC_ACTIVE, |
| 4875 | SENDMSG_REPLACE, |
| 4876 | 0, |
| 4877 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4878 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4879 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4880 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4881 | // Television devices without CEC service apply software volume on HDMI output |
| 4882 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4883 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 4884 | checkAllFixedVolumeDevices(); |
| 4885 | if (mHdmiManager != null) { |
| 4886 | synchronized (mHdmiManager) { |
| 4887 | if (mHdmiPlaybackClient != null) { |
| 4888 | mHdmiCecSink = false; |
| 4889 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 4890 | } |
| 4891 | } |
| 4892 | } |
| 4893 | } |
| 4894 | } else { |
| 4895 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4896 | if (mHdmiManager != null) { |
| 4897 | synchronized (mHdmiManager) { |
| 4898 | mHdmiCecSink = false; |
| 4899 | } |
| 4900 | } |
| 4901 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4902 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4903 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 4904 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4905 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4906 | } |
| 4907 | } |
| 4908 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4909 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4910 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 4911 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4912 | if (state == 1) { |
| 4913 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 4914 | int[] portGeneration = new int[1]; |
| 4915 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 4916 | if (status == AudioManager.SUCCESS) { |
| 4917 | for (AudioPort port : ports) { |
| 4918 | if (port instanceof AudioDevicePort) { |
| 4919 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4920 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 4921 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4922 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 4923 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4924 | if (formats.length > 0) { |
| 4925 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 4926 | for (int format : formats) { |
| 4927 | // a format in the list can be 0, skip it |
| 4928 | if (format != AudioFormat.ENCODING_INVALID) { |
| 4929 | encodingList.add(format); |
| 4930 | } |
| 4931 | } |
| 4932 | int[] encodingArray = new int[encodingList.size()]; |
| 4933 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 4934 | encodingArray[i] = encodingList.get(i); |
| 4935 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4936 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4937 | } |
| 4938 | // find the maximum supported number of channels |
| 4939 | int maxChannels = 0; |
| 4940 | for (int mask : devicePort.channelMasks()) { |
| 4941 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 4942 | if (channelCount > maxChannels) { |
| 4943 | maxChannels = channelCount; |
| 4944 | } |
| 4945 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4946 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4954 | /* cache of the address of the last dock the device was connected to */ |
| 4955 | private String mDockAddress; |
| 4956 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4957 | /** |
| 4958 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 4959 | */ |
| 4960 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 4961 | @Override |
| 4962 | public void onReceive(Context context, Intent intent) { |
| 4963 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4964 | int outDevice; |
| 4965 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4966 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4967 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4968 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 4969 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4970 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4971 | int config; |
| 4972 | switch (dockState) { |
| 4973 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 4974 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 4975 | break; |
| 4976 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 4977 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 4978 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4979 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4980 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4981 | break; |
| 4982 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 4983 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 4984 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4985 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 4986 | default: |
| 4987 | config = AudioSystem.FORCE_NONE; |
| 4988 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4989 | // Low end docks have a menu to enable or disable audio |
| 4990 | // (see mDockAudioMediaEnabled) |
| 4991 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 4992 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 4993 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 4994 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 4995 | } |
| 4996 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4997 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4998 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4999 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 5000 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 5001 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 5002 | setBtScoDeviceConnectionState(btDevice, state); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 5003 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5004 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5005 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5006 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5007 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5008 | // broadcast intent if the connection was initated by AudioService |
| 5009 | if (!mScoClients.isEmpty() && |
| 5010 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 5011 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 5012 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5013 | broadcast = true; |
| 5014 | } |
| 5015 | switch (btState) { |
| 5016 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5017 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5018 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5019 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5020 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5021 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5022 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5023 | break; |
| 5024 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5025 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5026 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 5027 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5028 | break; |
| 5029 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5030 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5031 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5032 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5033 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5034 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5035 | default: |
| 5036 | // do not broadcast CONNECTING or invalid state |
| 5037 | broadcast = false; |
| 5038 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5039 | } |
| 5040 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5041 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5042 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5043 | //FIXME: this is to maintain compatibility with deprecated intent |
| 5044 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5045 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5046 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5047 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5048 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5049 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5050 | if (mMonitorRotation) { |
| 5051 | mOrientationListener.onOrientationChanged(0); //argument is ignored anyway |
| 5052 | mOrientationListener.enable(); |
| 5053 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5054 | AudioSystem.setParameters("screen_state=on"); |
| 5055 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5056 | if (mMonitorRotation) { |
| 5057 | //reduce wakeups (save current) by only listening when display is on |
| 5058 | mOrientationListener.disable(); |
| 5059 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5060 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 5061 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5062 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5063 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5064 | if (mUserSwitchedReceived) { |
| 5065 | // attempt to stop music playback for background user except on first user |
| 5066 | // switch (i.e. first boot) |
| 5067 | sendMsg(mAudioHandler, |
| 5068 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5069 | SENDMSG_REPLACE, |
| 5070 | 0, |
| 5071 | 0, |
| 5072 | null, |
| 5073 | 0); |
| 5074 | } |
| 5075 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5076 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5077 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5078 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5079 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5080 | readAudioSettings(true /*userSwitch*/); |
| 5081 | // preserve STREAM_MUSIC volume from one user to the next. |
| 5082 | sendMsg(mAudioHandler, |
| 5083 | MSG_SET_ALL_VOLUMES, |
| 5084 | SENDMSG_QUEUE, |
| 5085 | 0, |
| 5086 | 0, |
| 5087 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5088 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 5089 | // Disable audio recording for the background user/profile |
| 5090 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5091 | if (userId >= 0) { |
| 5092 | // TODO Kill recording streams instead of killing processes holding permission |
| 5093 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 5094 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 5095 | } |
| 5096 | UserManagerService.getInstance().setSystemControlledUserRestriction( |
| 5097 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 5098 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 5099 | // Enable audio recording for foreground user/profile |
| 5100 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5101 | UserManagerService.getInstance().setSystemControlledUserRestriction( |
| 5102 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5103 | } |
| 5104 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5105 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5106 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5107 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 5108 | PackageManager pm = mContext.getPackageManager(); |
| 5109 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 5110 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 5111 | ComponentName homeActivityName = null; |
| 5112 | if (!oldUser.isManagedProfile()) { |
| 5113 | homeActivityName = LocalServices.getService(ActivityManagerInternal.class) |
| 5114 | .getHomeActivityForUser(oldUser.id); |
| 5115 | } |
| 5116 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 5117 | List<PackageInfo> packages; |
| 5118 | try { |
| 5119 | packages = AppGlobals.getPackageManager() |
| 5120 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 5121 | } catch (RemoteException e) { |
| 5122 | throw new AndroidRuntimeException(e); |
| 5123 | } |
| 5124 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 5125 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 5126 | // Skip system processes |
| 5127 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 5128 | continue; |
| 5129 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 5130 | // Skip packages that have permission to interact across users |
| 5131 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 5132 | == PackageManager.PERMISSION_GRANTED) { |
| 5133 | continue; |
| 5134 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5135 | if (homeActivityName != null |
| 5136 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 5137 | && pkg.applicationInfo.isSystemApp()) { |
| 5138 | continue; |
| 5139 | } |
| 5140 | try { |
| 5141 | ActivityManagerNative.getDefault().killUid(pkg.applicationInfo.uid, |
| 5142 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 5143 | } catch (RemoteException e) { |
| 5144 | Log.w(TAG, "Error calling killUid", e); |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5149 | //========================================================================================== |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5150 | // RemoteControlDisplay / RemoteControlClient / Remote info |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5151 | //========================================================================================== |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 5152 | public boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, |
| 5153 | ComponentName listenerComp) { |
| 5154 | return mMediaFocusControl.registerRemoteController(rcd, w, h, listenerComp); |
| 5155 | } |
| 5156 | |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 5157 | public boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 5158 | return mMediaFocusControl.registerRemoteControlDisplay(rcd, w, h); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 5159 | } |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 5160 | |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5161 | public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5162 | mMediaFocusControl.unregisterRemoteControlDisplay(rcd); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5166 | mMediaFocusControl.remoteControlDisplayUsesBitmapSize(rcd, w, h); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 5167 | } |
| 5168 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 5169 | public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, |
| 5170 | boolean wantsSync) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5171 | mMediaFocusControl.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync); |
| 5172 | } |
| 5173 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5174 | @Override |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5175 | public void setRemoteStreamVolume(int index) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5176 | enforceVolumeController("set the remote stream volume"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5177 | mMediaFocusControl.setRemoteStreamVolume(index); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5180 | //========================================================================================== |
| 5181 | // Audio Focus |
| 5182 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5183 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5184 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5185 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5186 | // permission checks |
| 5187 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5188 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5189 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 5190 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 5191 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 5192 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5193 | } |
| 5194 | } else { |
| 5195 | // only a registered audio policy can be used to lock focus |
| 5196 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5197 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5198 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5199 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5200 | } |
| 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5205 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 5206 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5207 | } |
| 5208 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5209 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 5210 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | public void unregisterAudioFocusClient(String clientId) { |
| 5214 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 5217 | public int getCurrentAudioFocus() { |
| 5218 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 5219 | } |
| 5220 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5221 | private boolean readCameraSoundForced() { |
| 5222 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 5223 | mContext.getResources().getBoolean( |
| 5224 | com.android.internal.R.bool.config_camera_sound_forced); |
| 5225 | } |
| 5226 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5227 | //========================================================================================== |
| 5228 | // Device orientation |
| 5229 | //========================================================================================== |
| 5230 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5231 | * Handles device configuration changes that may map to a change in the orientation |
| 5232 | * or orientation. |
| 5233 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 5234 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5235 | */ |
| 5236 | private void handleConfigurationChanged(Context context) { |
| 5237 | try { |
| 5238 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 5239 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5240 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5241 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5242 | if (mMonitorOrientation) { |
| 5243 | int newOrientation = config.orientation; |
| 5244 | if (newOrientation != mDeviceOrientation) { |
| 5245 | mDeviceOrientation = newOrientation; |
| 5246 | setOrientationForAudioSystem(); |
| 5247 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5248 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5249 | sendMsg(mAudioHandler, |
| 5250 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 5251 | SENDMSG_REPLACE, |
| 5252 | 0, |
| 5253 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5254 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5255 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5256 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5257 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5258 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5259 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5260 | synchronized (mCameraSoundForced) { |
| 5261 | if (cameraSoundForced != mCameraSoundForced) { |
| 5262 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5263 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5264 | } |
| 5265 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5266 | if (cameraSoundForcedChanged) { |
| 5267 | if (!isPlatformTelevision()) { |
| 5268 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 5269 | if (cameraSoundForced) { |
| 5270 | s.setAllIndexesToMax(); |
| 5271 | mRingerModeAffectedStreams &= |
| 5272 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5273 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5274 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5275 | mRingerModeAffectedStreams |= |
| 5276 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5277 | } |
| 5278 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5279 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | sendMsg(mAudioHandler, |
| 5283 | MSG_SET_FORCE_USE, |
| 5284 | SENDMSG_QUEUE, |
| 5285 | AudioSystem.FOR_SYSTEM, |
| 5286 | cameraSoundForced ? |
| 5287 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 5288 | null, |
| 5289 | 0); |
| 5290 | |
| 5291 | sendMsg(mAudioHandler, |
| 5292 | MSG_SET_ALL_VOLUMES, |
| 5293 | SENDMSG_QUEUE, |
| 5294 | 0, |
| 5295 | 0, |
| 5296 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 5297 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5298 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5299 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5300 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5301 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | private void setOrientationForAudioSystem() { |
| 5306 | switch (mDeviceOrientation) { |
| 5307 | case Configuration.ORIENTATION_LANDSCAPE: |
| 5308 | //Log.i(TAG, "orientation is landscape"); |
| 5309 | AudioSystem.setParameters("orientation=landscape"); |
| 5310 | break; |
| 5311 | case Configuration.ORIENTATION_PORTRAIT: |
| 5312 | //Log.i(TAG, "orientation is portrait"); |
| 5313 | AudioSystem.setParameters("orientation=portrait"); |
| 5314 | break; |
| 5315 | case Configuration.ORIENTATION_SQUARE: |
| 5316 | //Log.i(TAG, "orientation is square"); |
| 5317 | AudioSystem.setParameters("orientation=square"); |
| 5318 | break; |
| 5319 | case Configuration.ORIENTATION_UNDEFINED: |
| 5320 | //Log.i(TAG, "orientation is undefined"); |
| 5321 | AudioSystem.setParameters("orientation=undefined"); |
| 5322 | break; |
| 5323 | default: |
| 5324 | Log.e(TAG, "Unknown orientation"); |
| 5325 | } |
| 5326 | } |
| 5327 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5328 | private void setRotationForAudioSystem() { |
| 5329 | switch (mDeviceRotation) { |
| 5330 | case Surface.ROTATION_0: |
| 5331 | AudioSystem.setParameters("rotation=0"); |
| 5332 | break; |
| 5333 | case Surface.ROTATION_90: |
| 5334 | AudioSystem.setParameters("rotation=90"); |
| 5335 | break; |
| 5336 | case Surface.ROTATION_180: |
| 5337 | AudioSystem.setParameters("rotation=180"); |
| 5338 | break; |
| 5339 | case Surface.ROTATION_270: |
| 5340 | AudioSystem.setParameters("rotation=270"); |
| 5341 | break; |
| 5342 | default: |
| 5343 | Log.e(TAG, "Unknown device rotation"); |
| 5344 | } |
| 5345 | } |
| 5346 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5347 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5348 | // Handles request to override default use of A2DP for media. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5349 | // Must be called synchronized on mConnectedDevices |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5350 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5351 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5352 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5353 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5354 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5355 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5356 | } |
| 5357 | } |
| 5358 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5359 | // Must be called synchronized on mConnectedDevices |
| 5360 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5361 | switch (usage) { |
| 5362 | case AudioSystem.FOR_MEDIA: |
| 5363 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5364 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5365 | } else { // config == AudioSystem.FORCE_NONE |
| 5366 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5367 | } |
| 5368 | break; |
| 5369 | case AudioSystem.FOR_DOCK: |
| 5370 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5371 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5372 | } else { // config == AudioSystem.FORCE_NONE |
| 5373 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5374 | } |
| 5375 | break; |
| 5376 | default: |
| 5377 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5378 | } |
| 5379 | AudioSystem.setForceUse(usage, config); |
| 5380 | } |
| 5381 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5382 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5383 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5384 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5385 | mRingtonePlayer = player; |
| 5386 | } |
| 5387 | |
| 5388 | @Override |
| 5389 | public IRingtonePlayer getRingtonePlayer() { |
| 5390 | return mRingtonePlayer; |
| 5391 | } |
| 5392 | |
| 5393 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5394 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5395 | synchronized (mCurAudioRoutes) { |
| 5396 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5397 | mRoutesObservers.register(observer); |
| 5398 | return routes; |
| 5399 | } |
| 5400 | } |
| 5401 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5402 | |
| 5403 | //========================================================================================== |
| 5404 | // Safe media volume management. |
| 5405 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5406 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5407 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5408 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5409 | // property. Platforms with a different limit must set this property accordingly in their |
| 5410 | // overlay. |
| 5411 | //========================================================================================== |
| 5412 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5413 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5414 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5415 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5416 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5417 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5418 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5419 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5420 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5421 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5422 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5423 | private Integer mSafeMediaVolumeState; |
| 5424 | |
| 5425 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5426 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5427 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5428 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5429 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5430 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5431 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5432 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5433 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5434 | private int mMusicActiveMs; |
| 5435 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5436 | 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] | 5437 | 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] | 5438 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5439 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5440 | synchronized (mSafeMediaVolumeState) { |
| 5441 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5442 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5443 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5444 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5445 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5446 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5447 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5448 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5449 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5450 | sendMsg(mAudioHandler, |
| 5451 | MSG_CHECK_MUSIC_ACTIVE, |
| 5452 | SENDMSG_REPLACE, |
| 5453 | 0, |
| 5454 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5455 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5456 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5457 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5458 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5459 | } |
| 5460 | } |
| 5461 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5462 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5463 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5464 | int devices = mSafeMediaVolumeDevices; |
| 5465 | int i = 0; |
| 5466 | |
| 5467 | while (devices != 0) { |
| 5468 | int device = 1 << i++; |
| 5469 | if ((device & devices) == 0) { |
| 5470 | continue; |
| 5471 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5472 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5473 | if (index > mSafeMediaVolumeIndex) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5474 | streamState.setIndex(mSafeMediaVolumeIndex, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5475 | sendMsg(mAudioHandler, |
| 5476 | MSG_SET_DEVICE_VOLUME, |
| 5477 | SENDMSG_QUEUE, |
| 5478 | device, |
| 5479 | 0, |
| 5480 | streamState, |
| 5481 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5482 | } |
| 5483 | devices &= ~device; |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5488 | synchronized (mSafeMediaVolumeState) { |
| 5489 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5490 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5491 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5492 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5493 | return false; |
| 5494 | } |
| 5495 | return true; |
| 5496 | } |
| 5497 | } |
| 5498 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5499 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5500 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5501 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5502 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5503 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5504 | if (mPendingVolumeCommand != null) { |
| 5505 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5506 | mPendingVolumeCommand.mIndex, |
| 5507 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5508 | mPendingVolumeCommand.mDevice, |
| 5509 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5510 | mPendingVolumeCommand = null; |
| 5511 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5515 | //========================================================================================== |
| 5516 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5517 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5518 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5519 | //========================================================================================== |
| 5520 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5521 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5522 | public void onComplete(int status) { |
| 5523 | if (mHdmiManager != null) { |
| 5524 | synchronized (mHdmiManager) { |
| 5525 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5526 | // Television devices without CEC service apply software volume on HDMI output |
| 5527 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5528 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5529 | } |
| 5530 | checkAllFixedVolumeDevices(); |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | }; |
| 5535 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5536 | // If HDMI-CEC system audio is supported |
| 5537 | private boolean mHdmiSystemAudioSupported = false; |
| 5538 | // Set only when device is tv. |
| 5539 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5540 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5541 | // cached HdmiControlManager interface |
| 5542 | private HdmiControlManager mHdmiManager; |
| 5543 | // Set only when device is a set-top box. |
| 5544 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5545 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5546 | private boolean mHdmiCecSink; |
| 5547 | |
| 5548 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5549 | |
| 5550 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5551 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5552 | int device = AudioSystem.DEVICE_NONE; |
| 5553 | if (mHdmiManager != null) { |
| 5554 | synchronized (mHdmiManager) { |
| 5555 | if (mHdmiTvClient == null) { |
| 5556 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5557 | return device; |
| 5558 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5559 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5560 | synchronized (mHdmiTvClient) { |
| 5561 | if (mHdmiSystemAudioSupported != on) { |
| 5562 | mHdmiSystemAudioSupported = on; |
| 5563 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5564 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5565 | AudioSystem.FORCE_NONE); |
| 5566 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5567 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5568 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5569 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5570 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5571 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5572 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5573 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5574 | @Override |
| 5575 | public boolean isHdmiSystemAudioSupported() { |
| 5576 | return mHdmiSystemAudioSupported; |
| 5577 | } |
| 5578 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5579 | //========================================================================================== |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5580 | // Accessibility: taking touch exploration into account for selecting the default |
| 5581 | // stream override timeout when adjusting volume |
| 5582 | //========================================================================================== |
| 5583 | private static class StreamOverride |
| 5584 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 5585 | |
| 5586 | // AudioService.getActiveStreamType() will return: |
| 5587 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5588 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5589 | // stopped |
Jean-Michel Trivi | ccffda8 | 2015-05-21 18:23:57 -0700 | [diff] [blame] | 5590 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5591 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
| 5592 | |
| 5593 | static int sDelayMs; |
| 5594 | |
| 5595 | static void init(Context ctxt) { |
| 5596 | AccessibilityManager accessibilityManager = |
| 5597 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5598 | updateDefaultStreamOverrideDelay( |
| 5599 | accessibilityManager.isTouchExplorationEnabled()); |
| 5600 | accessibilityManager.addTouchExplorationStateChangeListener( |
| 5601 | new StreamOverride()); |
| 5602 | } |
| 5603 | |
| 5604 | @Override |
| 5605 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5606 | updateDefaultStreamOverrideDelay(enabled); |
| 5607 | } |
| 5608 | |
| 5609 | private static void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5610 | if (touchExploreEnabled) { |
| 5611 | sDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5612 | } else { |
| 5613 | sDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5614 | } |
| 5615 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5616 | + " stream override delay is now " + sDelayMs + " ms"); |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5621 | // Camera shutter sound policy. |
| 5622 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5623 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5624 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5625 | //========================================================================================== |
| 5626 | |
| 5627 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5628 | private Boolean mCameraSoundForced; |
| 5629 | |
| 5630 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5631 | public boolean isCameraSoundForced() { |
| 5632 | synchronized (mCameraSoundForced) { |
| 5633 | return mCameraSoundForced; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5638 | "SILENT", |
| 5639 | "VIBRATE", |
| 5640 | "NORMAL" |
| 5641 | }; |
| 5642 | |
| 5643 | private void dumpRingerMode(PrintWriter pw) { |
| 5644 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5645 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5646 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5647 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
| 5648 | dumpRingerModeStreams(pw, "muted", mRingerModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5649 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5652 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 5653 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 5654 | pw.print(Integer.toHexString(streams)); |
| 5655 | if (streams != 0) { |
| 5656 | pw.print(" ("); |
| 5657 | boolean first = true; |
| 5658 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 5659 | final int stream = (1 << i); |
| 5660 | if ((streams & stream) != 0) { |
| 5661 | if (!first) pw.print(','); |
| 5662 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 5663 | streams &= ~stream; |
| 5664 | first = false; |
| 5665 | } |
| 5666 | } |
| 5667 | if (streams != 0) { |
| 5668 | if (!first) pw.print(','); |
| 5669 | pw.print(streams); |
| 5670 | } |
| 5671 | pw.print(')'); |
| 5672 | } |
| 5673 | pw.println(); |
| 5674 | } |
| 5675 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5676 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5677 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5678 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5679 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5680 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5681 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5682 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5683 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5684 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 5685 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5686 | |
| 5687 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5688 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5689 | pw.print(" mSafeMediaVolumeState="); |
| 5690 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5691 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5692 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5693 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5694 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5695 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5696 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5697 | pw.print(" mControllerService="); pw.println(mControllerService); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5698 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5699 | |
| 5700 | dumpAudioPolicies(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5704 | switch(state) { |
| 5705 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5706 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5707 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5708 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5709 | } |
| 5710 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5711 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5712 | |
| 5713 | // Inform AudioFlinger of our device's low RAM attribute |
| 5714 | private static void readAndSetLowRamDevice() |
| 5715 | { |
| 5716 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5717 | if (status != 0) { |
| 5718 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5719 | } |
| 5720 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5721 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5722 | private void enforceVolumeController(String action) { |
| 5723 | if (mControllerService.mUid != 0 && Binder.getCallingUid() == mControllerService.mUid) { |
| 5724 | return; |
| 5725 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5726 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5727 | "Only SystemUI can " + action); |
| 5728 | } |
| 5729 | |
| 5730 | @Override |
| 5731 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5732 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5733 | |
| 5734 | // return early if things are not actually changing |
| 5735 | if (mVolumeController.isSameBinder(controller)) { |
| 5736 | return; |
| 5737 | } |
| 5738 | |
| 5739 | // dismiss the old volume controller |
| 5740 | mVolumeController.postDismiss(); |
| 5741 | if (controller != null) { |
| 5742 | // we are about to register a new controller, listen for its death |
| 5743 | try { |
| 5744 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5745 | @Override |
| 5746 | public void binderDied() { |
| 5747 | if (mVolumeController.isSameBinder(controller)) { |
| 5748 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5749 | setVolumeController(null); |
| 5750 | } |
| 5751 | } |
| 5752 | }, 0); |
| 5753 | } catch (RemoteException e) { |
| 5754 | // noop |
| 5755 | } |
| 5756 | } |
| 5757 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5758 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5759 | } |
| 5760 | |
| 5761 | @Override |
| 5762 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5763 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5764 | |
| 5765 | // return early if the controller is not current |
| 5766 | if (!mVolumeController.isSameBinder(controller)) { |
| 5767 | return; |
| 5768 | } |
| 5769 | |
| 5770 | mVolumeController.setVisible(visible); |
| 5771 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5772 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5773 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5774 | @Override |
| 5775 | public void setVolumePolicy(VolumePolicy policy) { |
| 5776 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5777 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5778 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5779 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5783 | public static class VolumeController { |
| 5784 | private static final String TAG = "VolumeController"; |
| 5785 | |
| 5786 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5787 | private boolean mVisible; |
| 5788 | private long mNextLongPress; |
| 5789 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5790 | |
| 5791 | public void setController(IVolumeController controller) { |
| 5792 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5793 | mVisible = false; |
| 5794 | } |
| 5795 | |
| 5796 | public void loadSettings(ContentResolver cr) { |
| 5797 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5798 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5799 | } |
| 5800 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5801 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5802 | if (isMute) { |
| 5803 | return false; |
| 5804 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5805 | boolean suppress = false; |
| 5806 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5807 | final long now = SystemClock.uptimeMillis(); |
| 5808 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5809 | // ui will become visible |
| 5810 | if (mNextLongPress < now) { |
| 5811 | mNextLongPress = now + mLongPressTimeout; |
| 5812 | } |
| 5813 | suppress = true; |
| 5814 | } else if (mNextLongPress > 0) { // in a long-press |
| 5815 | if (now > mNextLongPress) { |
| 5816 | // long press triggered, no more suppression |
| 5817 | mNextLongPress = 0; |
| 5818 | } else { |
| 5819 | // keep suppressing until the long press triggers |
| 5820 | suppress = true; |
| 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | return suppress; |
| 5825 | } |
| 5826 | |
| 5827 | public void setVisible(boolean visible) { |
| 5828 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | public boolean isSameBinder(IVolumeController controller) { |
| 5832 | return Objects.equals(asBinder(), binder(controller)); |
| 5833 | } |
| 5834 | |
| 5835 | public IBinder asBinder() { |
| 5836 | return binder(mController); |
| 5837 | } |
| 5838 | |
| 5839 | private static IBinder binder(IVolumeController controller) { |
| 5840 | return controller == null ? null : controller.asBinder(); |
| 5841 | } |
| 5842 | |
| 5843 | @Override |
| 5844 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5845 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5849 | if (mController == null) |
| 5850 | return; |
| 5851 | try { |
| 5852 | mController.displaySafeVolumeWarning(flags); |
| 5853 | } catch (RemoteException e) { |
| 5854 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | public void postVolumeChanged(int streamType, int flags) { |
| 5859 | if (mController == null) |
| 5860 | return; |
| 5861 | try { |
| 5862 | mController.volumeChanged(streamType, flags); |
| 5863 | } catch (RemoteException e) { |
| 5864 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5865 | } |
| 5866 | } |
| 5867 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5868 | public void postMasterMuteChanged(int flags) { |
| 5869 | if (mController == null) |
| 5870 | return; |
| 5871 | try { |
| 5872 | mController.masterMuteChanged(flags); |
| 5873 | } catch (RemoteException e) { |
| 5874 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | public void setLayoutDirection(int layoutDirection) { |
| 5879 | if (mController == null) |
| 5880 | return; |
| 5881 | try { |
| 5882 | mController.setLayoutDirection(layoutDirection); |
| 5883 | } catch (RemoteException e) { |
| 5884 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | public void postDismiss() { |
| 5889 | if (mController == null) |
| 5890 | return; |
| 5891 | try { |
| 5892 | mController.dismiss(); |
| 5893 | } catch (RemoteException e) { |
| 5894 | Log.w(TAG, "Error calling dismiss", e); |
| 5895 | } |
| 5896 | } |
| 5897 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5898 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5899 | /** |
| 5900 | * Interface for system components to get some extra functionality through |
| 5901 | * LocalServices. |
| 5902 | */ |
| 5903 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5904 | @Override |
| 5905 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 5906 | mRingerModeDelegate = delegate; |
| 5907 | if (mRingerModeDelegate != null) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5908 | updateRingerModeAffectedStreams(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5909 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 5910 | } |
| 5911 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 5912 | |
| 5913 | @Override |
| 5914 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 5915 | String callingPackage, int uid) { |
| 5916 | // direction and stream type swap here because the public |
| 5917 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5918 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 5919 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5922 | @Override |
| 5923 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 5924 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5925 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 5926 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | @Override |
| 5930 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 5931 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5932 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5933 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 5934 | |
| 5935 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5936 | public int getRingerModeInternal() { |
| 5937 | return AudioService.this.getRingerModeInternal(); |
| 5938 | } |
| 5939 | |
| 5940 | @Override |
| 5941 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 5942 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 5943 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5944 | |
| 5945 | @Override |
| 5946 | public int getVolumeControllerUid() { |
| 5947 | return mControllerService.mUid; |
| 5948 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5949 | |
| 5950 | @Override |
| 5951 | public void updateRingerModeAffectedStreamsInternal() { |
| 5952 | synchronized (mSettingsLock) { |
| 5953 | if (updateRingerModeAffectedStreams()) { |
| 5954 | setRingerModeInt(getRingerModeInternal(), false); |
| 5955 | } |
| 5956 | } |
| 5957 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5958 | } |
| 5959 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5960 | //========================================================================================== |
| 5961 | // Audio policy management |
| 5962 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5963 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 5964 | boolean hasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 5965 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 5966 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5967 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 5968 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5969 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5970 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5971 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5972 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5973 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5974 | if (!hasPermissionForPolicy) { |
| 5975 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 5976 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5977 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5978 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5979 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5980 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5981 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5982 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5983 | Slog.e(TAG, "Cannot re-register policy"); |
| 5984 | return null; |
| 5985 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5986 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 5987 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 5988 | regId = app.getRegistrationId(); |
| 5989 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5990 | } catch (RemoteException e) { |
| 5991 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5992 | 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] | 5993 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5994 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5995 | } |
| 5996 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5997 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5998 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5999 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6000 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 6001 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6002 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6003 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6004 | if (app == null) { |
| 6005 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 6006 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6007 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6008 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6009 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6010 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6011 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6012 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6013 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6014 | } |
| 6015 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6016 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 6017 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 6018 | + " policy " + pcb.asBinder()); |
| 6019 | // error handling |
| 6020 | boolean hasPermissionForPolicy = |
| 6021 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6022 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6023 | if (!hasPermissionForPolicy) { |
| 6024 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 6025 | + Binder.getCallingPid() + " / uid " |
| 6026 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 6027 | return AudioManager.ERROR; |
| 6028 | } |
| 6029 | |
| 6030 | synchronized (mAudioPolicies) { |
| 6031 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6032 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 6033 | return AudioManager.ERROR; |
| 6034 | } |
| 6035 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 6036 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6037 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6038 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6039 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6040 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 6041 | return AudioManager.ERROR; |
| 6042 | } |
| 6043 | } |
| 6044 | } |
| 6045 | app.mFocusDuckBehavior = duckingBehavior; |
| 6046 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 6047 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 6048 | } |
| 6049 | return AudioManager.SUCCESS; |
| 6050 | } |
| 6051 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6052 | private void dumpAudioPolicies(PrintWriter pw) { |
| 6053 | pw.println("\nAudio policies:"); |
| 6054 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6055 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6056 | pw.println(policy.toLogFriendlyString()); |
| 6057 | } |
| 6058 | } |
| 6059 | } |
| 6060 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6061 | //====================== |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6062 | // Audio policy callback from AudioSystem |
| 6063 | //====================== |
| 6064 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 6065 | new AudioSystem.DynamicPolicyCallback() { |
| 6066 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 6067 | if (!TextUtils.isEmpty(regId)) { |
| 6068 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 6069 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 6070 | } |
| 6071 | } |
| 6072 | }; |
| 6073 | |
| 6074 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 6075 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 6076 | synchronized (mAudioPolicies) { |
| 6077 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 6078 | for (AudioMix mix : policy.getMixes()) { |
| 6079 | if (mix.getRegistration().equals(regId)) { |
| 6080 | try { |
| 6081 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 6082 | } catch (RemoteException e) { |
| 6083 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 6084 | + policy.mPolicyCallback.asBinder(), e); |
| 6085 | } |
| 6086 | return; |
| 6087 | } |
| 6088 | } |
| 6089 | } |
| 6090 | } |
| 6091 | |
| 6092 | } |
| 6093 | |
| 6094 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6095 | // Audio policy proxy |
| 6096 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6097 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6098 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 6099 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6100 | */ |
| 6101 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6102 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6103 | IAudioPolicyCallback mPolicyCallback; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6104 | boolean mHasFocusListener; |
| 6105 | /** |
| 6106 | * Audio focus ducking behavior for an audio policy. |
| 6107 | * This variable reflects the value that was successfully set in |
| 6108 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 6109 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 6110 | * is handling ducking for audio focus. |
| 6111 | */ |
| 6112 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 6113 | |
| 6114 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 6115 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6116 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6117 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6118 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6119 | mHasFocusListener = hasFocusListener; |
| 6120 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6121 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6122 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6123 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | public void binderDied() { |
| 6127 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6128 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6129 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6130 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6131 | } |
| 6132 | } |
| 6133 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6134 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6135 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6136 | } |
| 6137 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6138 | void release() { |
| 6139 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6140 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 6141 | } |
| 6142 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6143 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6144 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6145 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6146 | } |
| 6147 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6148 | void connectMixes() { |
| 6149 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6150 | } |
| 6151 | }; |
| 6152 | |
| 6153 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 6154 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6155 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6156 | |
| 6157 | private class ControllerService extends ContentObserver { |
| 6158 | private int mUid; |
| 6159 | private ComponentName mComponent; |
| 6160 | |
| 6161 | public ControllerService() { |
| 6162 | super(null); |
| 6163 | } |
| 6164 | |
| 6165 | @Override |
| 6166 | public String toString() { |
| 6167 | return String.format("{mUid=%d,mComponent=%s}", mUid, mComponent); |
| 6168 | } |
| 6169 | |
| 6170 | public void init() { |
| 6171 | onChange(true); |
| 6172 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 6173 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT), false, this); |
| 6174 | } |
| 6175 | |
| 6176 | @Override |
| 6177 | public void onChange(boolean selfChange) { |
| 6178 | mUid = 0; |
| 6179 | mComponent = null; |
| 6180 | final String setting = Settings.Secure.getString(mContentResolver, |
| 6181 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT); |
| 6182 | if (setting == null) return; |
| 6183 | try { |
| 6184 | mComponent = ComponentName.unflattenFromString(setting); |
| 6185 | if (mComponent == null) return; |
| 6186 | mUid = mContext.getPackageManager() |
| 6187 | .getApplicationInfo(mComponent.getPackageName(), 0).uid; |
| 6188 | } catch (Exception e) { |
| 6189 | Log.w(TAG, "Error loading controller service", e); |
| 6190 | } |
| 6191 | if (DEBUG_VOL) Log.d(TAG, "Reloaded controller service: " + this); |
| 6192 | } |
| 6193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6194 | } |