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; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 28 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 29 | import android.app.AppOpsManager; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 30 | import android.app.NotificationManager; |
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; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 43 | import android.content.pm.ApplicationInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 46 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 47 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 48 | import android.content.res.Resources; |
| 49 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 50 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 51 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 52 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 53 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 54 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 55 | import android.media.AudioAttributes; |
| 56 | import android.media.AudioDevicePort; |
| 57 | import android.media.AudioSystem; |
| 58 | import android.media.AudioFormat; |
| 59 | import android.media.AudioManager; |
| 60 | import android.media.AudioManagerInternal; |
| 61 | import android.media.AudioPort; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 62 | import android.media.AudioPlaybackConfiguration; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 63 | import android.media.AudioRecordingConfiguration; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 64 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 65 | import android.media.IAudioFocusDispatcher; |
| 66 | import android.media.IAudioRoutesObserver; |
| 67 | import android.media.IAudioService; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 68 | import android.media.IPlaybackConfigDispatcher; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 69 | import android.media.IRecordingConfigDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 70 | import android.media.IRingtonePlayer; |
| 71 | import android.media.IVolumeController; |
| 72 | import android.media.MediaPlayer; |
| 73 | import android.media.SoundPool; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 74 | import android.media.VolumePolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.media.MediaPlayer.OnCompletionListener; |
| 76 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 77 | import android.media.PlayerBase; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 78 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 79 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 80 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 81 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 83 | import android.os.Build; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 84 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.os.Environment; |
| 86 | import android.os.Handler; |
| 87 | import android.os.IBinder; |
| 88 | import android.os.Looper; |
| 89 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 90 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 91 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 93 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 94 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 95 | import android.os.UserHandle; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 96 | import android.os.UserManager; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 97 | import android.os.UserManagerInternal; |
| 98 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 99 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.provider.Settings; |
| 101 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 102 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 103 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 104 | import android.util.AndroidRuntimeException; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 105 | import android.util.ArrayMap; |
| 106 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 108 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 109 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 110 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 111 | import android.view.KeyEvent; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 112 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 114 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 115 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 116 | import com.android.server.LocalServices; |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 117 | import com.android.server.SystemService; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 118 | import com.android.server.pm.UserManagerService; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 119 | |
| 120 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 122 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 124 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 125 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 127 | import java.util.HashMap; |
| 128 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 129 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 130 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 131 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * The implementation of the volume manager service. |
| 135 | * <p> |
| 136 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 137 | * asynchronous to external calls. For example, the task of setting a volume |
| 138 | * will update our internal state, but in a separate thread will set the system |
| 139 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 140 | * will update the state and broadcast a change and in a separate thread later |
| 141 | * persist the ringer mode. |
| 142 | * |
| 143 | * @hide |
| 144 | */ |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 145 | public class AudioService extends IAudioService.Stub |
| 146 | implements AccessibilityManager.TouchExplorationStateChangeListener, |
| 147 | AccessibilityManager.AccessibilityStateChangeListener{ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
| 149 | private static final String TAG = "AudioService"; |
| 150 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 151 | /** Debug audio mode */ |
| 152 | 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] | 153 | |
| 154 | /** Debug audio policy feature */ |
| 155 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 156 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 157 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 158 | 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] | 159 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 160 | /** debug calls to devices APIs */ |
| 161 | protected static final boolean DEBUG_DEVICES = Log.isLoggable(TAG + ".DEVICES", Log.DEBUG); |
| 162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 164 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 166 | /** How long to delay after a volume down event before unmuting a stream */ |
| 167 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 168 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 169 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 170 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 171 | */ |
| 172 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 173 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 174 | private final Context mContext; |
| 175 | private final ContentResolver mContentResolver; |
| 176 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 177 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 178 | // the platform type affects volume and silent mode behavior |
| 179 | private final int mPlatformType; |
| 180 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 181 | // indicates whether the system maps all streams to a single stream. |
| 182 | private final boolean mIsSingleVolume; |
| 183 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 184 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 185 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 189 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 190 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 191 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 192 | /** The controller for the volume UI. */ |
| 193 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
| 195 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | /** If the msg is already queued, replace it with this one. */ |
| 197 | private static final int SENDMSG_REPLACE = 0; |
| 198 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 199 | private static final int SENDMSG_NOOP = 1; |
| 200 | /** If the msg is already queued, queue this one and leave the old. */ |
| 201 | private static final int SENDMSG_QUEUE = 2; |
| 202 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 203 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 204 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | private static final int MSG_PERSIST_VOLUME = 1; |
| 206 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 207 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 208 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 209 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 210 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 211 | private static final int MSG_SET_FORCE_USE = 8; |
| 212 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 213 | private static final int MSG_SET_ALL_VOLUMES = 10; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 214 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 215 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 216 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 217 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 218 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 219 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 220 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 221 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 222 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 223 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 224 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 225 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 226 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 227 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 228 | // start of messages handled under wakelock |
| 229 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 230 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 231 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 232 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 233 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 234 | private static final int MSG_A2DP_DEVICE_CONFIG_CHANGE = 103; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 235 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 236 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 237 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 238 | // Timeout for connection to bluetooth headset service |
| 239 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 240 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 241 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 242 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | /** @see AudioSystemThread */ |
| 245 | private AudioSystemThread mAudioSystemThread; |
| 246 | /** @see AudioHandler */ |
| 247 | private AudioHandler mAudioHandler; |
| 248 | /** @see VolumeStreamState */ |
| 249 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 250 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 251 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 252 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 253 | // protects mRingerMode |
| 254 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 257 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
| 260 | /* Sound effect file names */ |
| 261 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 262 | 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] | 263 | |
| 264 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 265 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 266 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 267 | 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] | 268 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 269 | /** Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 270 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 271 | 5, // STREAM_VOICE_CALL |
| 272 | 7, // STREAM_SYSTEM |
| 273 | 7, // STREAM_RING |
| 274 | 15, // STREAM_MUSIC |
| 275 | 7, // STREAM_ALARM |
| 276 | 7, // STREAM_NOTIFICATION |
| 277 | 15, // STREAM_BLUETOOTH_SCO |
| 278 | 7, // STREAM_SYSTEM_ENFORCED |
| 279 | 15, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 280 | 15, // STREAM_TTS |
| 281 | 15 // STREAM_ACCESSIBILITY |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 282 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 283 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 284 | /** Minimum volume index values for audio streams */ |
| 285 | private static int[] MIN_STREAM_VOLUME = new int[] { |
| 286 | 1, // STREAM_VOICE_CALL |
| 287 | 0, // STREAM_SYSTEM |
| 288 | 0, // STREAM_RING |
| 289 | 0, // STREAM_MUSIC |
| 290 | 0, // STREAM_ALARM |
| 291 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 292 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 293 | 0, // STREAM_SYSTEM_ENFORCED |
| 294 | 0, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 295 | 0, // STREAM_TTS |
| 296 | 0 // STREAM_ACCESSIBILITY |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 297 | }; |
| 298 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 299 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 300 | * of another stream: This avoids multiplying the volume settings for hidden |
| 301 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 302 | * NOTE: do not create loops in aliases! |
| 303 | * 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] | 304 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 305 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 306 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 307 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 308 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 309 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 310 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 311 | AudioSystem.STREAM_RING, // STREAM_RING |
| 312 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 313 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 314 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 315 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 316 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 317 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 318 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 319 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 320 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 321 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 322 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 323 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 324 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 325 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 326 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 327 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 328 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 329 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 330 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 331 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 332 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 333 | }; |
| 334 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 335 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 336 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 337 | AudioSystem.STREAM_RING, // STREAM_RING |
| 338 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 339 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 340 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 341 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 342 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 343 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 344 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 345 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 346 | }; |
| 347 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 348 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 351 | * after mapping through mStreamVolumeAlias. |
| 352 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 353 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 354 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 355 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 356 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 357 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 358 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 359 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 360 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 361 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 362 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 363 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 364 | AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME, // STREAM_ACCESSIBILITY |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 367 | private final boolean mUseFixedVolume; |
| 368 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 369 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | public void onError(int error) { |
| 371 | switch (error) { |
| 372 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 373 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 374 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | break; |
| 376 | default: |
| 377 | break; |
| 378 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | /** |
| 383 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 384 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 385 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 386 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 387 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 388 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 389 | 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] | 390 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 391 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 392 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 393 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 394 | // Streams currently muted by ringer mode |
| 395 | private int mRingerModeMutedStreams; |
| 396 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 397 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 398 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | private int mMuteAffectedStreams; |
| 400 | |
| 401 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 402 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 403 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 404 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | */ |
| 406 | private int mVibrateSetting; |
| 407 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 408 | // Is there a vibrator |
| 409 | private final boolean mHasVibrator; |
| 410 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 411 | // Broadcast receiver for device connections intent broadcasts |
| 412 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 413 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 414 | /** Interface for UserManagerService. */ |
| 415 | private final UserManagerInternal mUserManagerInternal; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 416 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 417 | |
| 418 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 419 | new AudioServiceUserRestrictionsListener(); |
| 420 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 421 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 422 | // Use makeDeviceListKey() to make a unique key for this list. |
| 423 | private class DeviceListSpec { |
| 424 | int mDeviceType; |
| 425 | String mDeviceName; |
| 426 | String mDeviceAddress; |
| 427 | |
| 428 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 429 | mDeviceType = deviceType; |
| 430 | mDeviceName = deviceName; |
| 431 | mDeviceAddress = deviceAddress; |
| 432 | } |
| 433 | |
| 434 | public String toString() { |
| 435 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 436 | + " address:" + mDeviceAddress + "]"; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 441 | // and the "address" associated with a specific instance of that device type |
| 442 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 443 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 444 | } |
| 445 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 446 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 447 | |
| 448 | // Forced device usage for communications |
| 449 | private int mForcedUseForComm; |
| 450 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 451 | // List of binder death handlers for setMode() client processes. |
| 452 | // 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] | 453 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 454 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 455 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 456 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 457 | |
| 458 | // BluetoothHeadset API to control SCO connection |
| 459 | private BluetoothHeadset mBluetoothHeadset; |
| 460 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 461 | // Bluetooth headset device |
| 462 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 463 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 464 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 465 | // audio service (internal) or bluetooth headset (external) |
| 466 | private int mScoAudioState; |
| 467 | // SCO audio state is not active |
| 468 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 469 | // SCO audio activation request waiting for headset service to connect |
| 470 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 471 | // 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] | 472 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 473 | // SCO audio deactivation request waiting for headset service to connect |
| 474 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 475 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 476 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 477 | // in call audio) |
| 478 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 479 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 480 | // waiting for headset service to connect |
| 481 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 482 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 483 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 484 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 485 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 486 | // SCO audio mode is undefined |
| 487 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 488 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 489 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 490 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 491 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 492 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 493 | private static final int SCO_MODE_VR = 2; |
| 494 | |
| 495 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 496 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 497 | // Current connection state indicated by bluetooth headset |
| 498 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 499 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 500 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 501 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 502 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 503 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 504 | // listener for SoundPool sample load completion indication |
| 505 | private SoundPoolCallback mSoundPoolCallBack; |
| 506 | // thread for SoundPool listener |
| 507 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 508 | // message looper for SoundPool listener |
| 509 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 510 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 511 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 512 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 513 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 514 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 515 | // is controlled by Vol keys. |
| 516 | private int mVolumeControlStream = -1; |
| 517 | private final Object mForceControlStreamLock = new Object(); |
| 518 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 519 | // server process so in theory it is not necessary to monitor the client death. |
| 520 | // However it is good to be ready for future evolutions. |
| 521 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 522 | // Used to play ringtones outside system_server |
| 523 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 524 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 525 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
| 526 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 527 | // Request to override default use of A2DP for media. |
Sungsoo | 486f7d3 | 2016-09-28 16:20:52 +0900 | [diff] [blame] | 528 | private boolean mBluetoothA2dpEnabled; |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 529 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 530 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 531 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 532 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 533 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 534 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 535 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 536 | // 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] | 537 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 538 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 539 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 540 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 541 | AudioSystem.DEVICE_OUT_SPDIF | |
| 542 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 543 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 544 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 545 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 546 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 547 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 548 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 549 | private boolean mDockAudioMediaEnabled = true; |
| 550 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 551 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 552 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 553 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 554 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 555 | // and used later when/if disableSafeMediaVolume() is called. |
| 556 | private StreamVolumeCommand mPendingVolumeCommand; |
| 557 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 558 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 559 | |
| 560 | private final MediaFocusControl mMediaFocusControl; |
| 561 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 562 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 563 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 564 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 565 | private final Object mA2dpAvrcpLock = new Object(); |
| 566 | // If absolute volume is supported in AVRCP device |
| 567 | private boolean mAvrcpAbsVolSupported = false; |
| 568 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 569 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 570 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 571 | private NotificationManager mNm; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 572 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 573 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 574 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 575 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 576 | // Intent "extra" data keys. |
| 577 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 578 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 579 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 580 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 581 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 582 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 583 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 584 | |
| 585 | // Defines the format for the connection "address" for ALSA devices |
| 586 | public static String makeAlsaAddressString(int card, int device) { |
| 587 | return "card=" + card + ";device=" + device + ";"; |
| 588 | } |
| 589 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 590 | public static final class Lifecycle extends SystemService { |
| 591 | private AudioService mService; |
| 592 | |
| 593 | public Lifecycle(Context context) { |
| 594 | super(context); |
| 595 | mService = new AudioService(context); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public void onStart() { |
| 600 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 601 | } |
| 602 | |
| 603 | @Override |
| 604 | public void onBootPhase(int phase) { |
| 605 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 606 | mService.systemReady(); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | /////////////////////////////////////////////////////////////////////////// |
| 612 | // Construction |
| 613 | /////////////////////////////////////////////////////////////////////////// |
| 614 | |
| 615 | /** @hide */ |
| 616 | public AudioService(Context context) { |
| 617 | mContext = context; |
| 618 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 619 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 620 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 621 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 622 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 623 | mIsSingleVolume = AudioSystem.isSingleVolume(context); |
| 624 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 625 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 626 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 627 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 628 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 629 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 630 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 631 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 632 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 633 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 634 | // Initialize volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 635 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 636 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 637 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 638 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 639 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 640 | } |
| 641 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 642 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 643 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 644 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 645 | if (isPlatformTelevision()) { |
| 646 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume / 4; |
| 647 | } else { |
| 648 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
| 649 | } |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 650 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 651 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 652 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 653 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 654 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 655 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 658 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 659 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 660 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 661 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 662 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 663 | sendMsg(mAudioHandler, |
| 664 | MSG_SET_FORCE_USE, |
| 665 | SENDMSG_QUEUE, |
| 666 | AudioSystem.FOR_SYSTEM, |
| 667 | cameraSoundForced ? |
| 668 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 669 | null, |
| 670 | 0); |
| 671 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 672 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 673 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 674 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 675 | // The default safe volume index read here will be replaced by the actual value when |
| 676 | // the mcc is read by onConfigureSafeVolume() |
| 677 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 678 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 679 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 680 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 681 | com.android.internal.R.bool.config_useFixedVolume); |
| 682 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 683 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 684 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 685 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 687 | readUserRestrictions(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 688 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 689 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 690 | |
Jean-Michel Trivi | d4de20d | 2015-11-04 14:45:54 -0800 | [diff] [blame] | 691 | mMediaFocusControl = new MediaFocusControl(mContext); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 692 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 693 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 694 | |
| 695 | // Call setRingerModeInt() to apply correct mute |
| 696 | // state on streams affected by ringer mode. |
| 697 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 698 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 699 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 700 | // Register for device connection intent broadcasts. |
| 701 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 702 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 703 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 704 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 705 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 706 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 707 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 708 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 709 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 710 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 711 | intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 712 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 713 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 714 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 715 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 716 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 717 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 718 | // initialize orientation in AudioSystem |
| 719 | setOrientationForAudioSystem(); |
| 720 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 721 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 722 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 723 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 724 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 725 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 726 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 727 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 728 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 729 | |
| 730 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 731 | |
| 732 | mRecordMonitor.initMonitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 735 | public void systemReady() { |
| 736 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 737 | 0, 0, null, 0); |
| 738 | } |
| 739 | |
| 740 | public void onSystemReady() { |
| 741 | mSystemReady = true; |
| 742 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 743 | 0, 0, null, 0); |
| 744 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 745 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 746 | resetBluetoothSco(); |
| 747 | getBluetoothHeadset(); |
| 748 | //FIXME: this is to maintain compatibility with deprecated intent |
| 749 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 750 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 751 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 752 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 753 | sendStickyBroadcastToAll(newIntent); |
| 754 | |
| 755 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 756 | if (adapter != null) { |
| 757 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 758 | BluetoothProfile.A2DP); |
| 759 | } |
| 760 | |
Jeff Sharkey | 73ea0ae | 2016-08-10 17:30:38 -0600 | [diff] [blame] | 761 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) { |
| 762 | mHdmiManager = mContext.getSystemService(HdmiControlManager.class); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 763 | synchronized (mHdmiManager) { |
| 764 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 765 | if (mHdmiTvClient != null) { |
| 766 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 767 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 768 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 769 | mHdmiCecSink = false; |
| 770 | } |
| 771 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 772 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 773 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 774 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 775 | sendMsg(mAudioHandler, |
| 776 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 777 | SENDMSG_REPLACE, |
| 778 | 0, |
| 779 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 780 | TAG, |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 781 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 782 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 783 | initA11yMonitoring(mContext); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 784 | onIndicateSystemReady(); |
| 785 | } |
| 786 | |
| 787 | void onIndicateSystemReady() { |
| 788 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 789 | return; |
| 790 | } |
| 791 | sendMsg(mAudioHandler, |
| 792 | MSG_INDICATE_SYSTEM_READY, |
| 793 | SENDMSG_REPLACE, |
| 794 | 0, |
| 795 | 0, |
| 796 | null, |
| 797 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 798 | } |
| 799 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 800 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 801 | if (!mSystemReady || |
| 802 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 803 | Log.e(TAG, "Audioserver died."); |
| 804 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 805 | null, 500); |
| 806 | return; |
| 807 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 808 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 809 | |
| 810 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 811 | // server crash |
| 812 | // Note that we only execute this when the media server |
| 813 | // process restarts after a crash, not the first time it is started. |
| 814 | AudioSystem.setParameters("restarting=true"); |
| 815 | |
| 816 | readAndSetLowRamDevice(); |
| 817 | |
| 818 | // Restore device connection states |
| 819 | synchronized (mConnectedDevices) { |
| 820 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 821 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 822 | AudioSystem.setDeviceConnectionState( |
| 823 | spec.mDeviceType, |
| 824 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 825 | spec.mDeviceAddress, |
| 826 | spec.mDeviceName); |
| 827 | } |
| 828 | } |
| 829 | // Restore call state |
| 830 | AudioSystem.setPhoneState(mMode); |
| 831 | |
| 832 | // Restore forced usage for communcations and record |
| 833 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
| 834 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
| 835 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 836 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
| 837 | |
| 838 | // Restore stream volumes |
| 839 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 840 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 841 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 842 | AudioSystem.initStreamVolume( |
| 843 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 844 | |
| 845 | streamState.applyAllVolumes(); |
| 846 | } |
| 847 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 848 | // Restore mono mode |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 849 | updateMasterMono(mContentResolver); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 850 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 851 | // Restore ringer mode |
| 852 | setRingerModeInt(getRingerModeInternal(), false); |
| 853 | |
| 854 | // Reset device orientation (if monitored for this device) |
| 855 | if (mMonitorOrientation) { |
| 856 | setOrientationForAudioSystem(); |
| 857 | } |
| 858 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 859 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 862 | synchronized (mBluetoothA2dpEnabledLock) { |
| 863 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 864 | mBluetoothA2dpEnabled ? |
| 865 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 866 | } |
| 867 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 868 | synchronized (mSettingsLock) { |
| 869 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 870 | mDockAudioMediaEnabled ? |
| 871 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 872 | sendEncodedSurroundMode(mContentResolver); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 873 | } |
| 874 | if (mHdmiManager != null) { |
| 875 | synchronized (mHdmiManager) { |
| 876 | if (mHdmiTvClient != null) { |
| 877 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | synchronized (mAudioPolicies) { |
| 883 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 884 | policy.connectMixes(); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | onIndicateSystemReady(); |
| 889 | // indicate the end of reconfiguration phase to audio HAL |
| 890 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 891 | } |
| 892 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | private void createAudioSystemThread() { |
| 894 | mAudioSystemThread = new AudioSystemThread(); |
| 895 | mAudioSystemThread.start(); |
| 896 | waitForAudioHandlerCreation(); |
| 897 | } |
| 898 | |
| 899 | /** Waits for the volume handler to be created by the other thread. */ |
| 900 | private void waitForAudioHandlerCreation() { |
| 901 | synchronized(this) { |
| 902 | while (mAudioHandler == null) { |
| 903 | try { |
| 904 | // Wait for mAudioHandler to be set by the other thread |
| 905 | wait(); |
| 906 | } catch (InterruptedException e) { |
| 907 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 913 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 914 | synchronized (VolumeStreamState.class) { |
| 915 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 916 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 917 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 918 | mStreamStates[streamType]. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 919 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], |
| 920 | TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 921 | } |
| 922 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 923 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 924 | mStreamStates[streamType].applyAllVolumes(); |
| 925 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 930 | private void checkAllFixedVolumeDevices() |
| 931 | { |
| 932 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 933 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 934 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 935 | } |
| 936 | } |
| 937 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 938 | private void checkAllFixedVolumeDevices(int streamType) { |
| 939 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 940 | } |
| 941 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 942 | private void checkMuteAffectedStreams() { |
| 943 | // any stream with a min level > 0 is not muteable by definition |
| 944 | for (int i = 0; i < mStreamStates.length; i++) { |
| 945 | final VolumeStreamState vss = mStreamStates[i]; |
| 946 | if (vss.mIndexMin > 0) { |
| 947 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 954 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 955 | |
| 956 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 957 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 960 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 961 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 962 | checkMuteAffectedStreams(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 965 | private void dumpStreamStates(PrintWriter pw) { |
| 966 | pw.println("\nStream volumes (device: index)"); |
| 967 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 968 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 969 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 970 | mStreamStates[i].dump(pw); |
| 971 | pw.println(""); |
| 972 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 973 | pw.print("\n- mute affected streams = 0x"); |
| 974 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 975 | } |
| 976 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 977 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 978 | int dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 979 | final int a11yStreamAlias = sIndependentA11yVolume ? |
| 980 | AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 981 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 982 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 983 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 984 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 985 | } else { |
| 986 | switch (mPlatformType) { |
| 987 | case AudioSystem.PLATFORM_VOICE: |
| 988 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
| 989 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
| 990 | break; |
| 991 | default: |
| 992 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
| 993 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 994 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 995 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 996 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 997 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 998 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 999 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1000 | if (isInCommunication()) { |
| 1001 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 1002 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 1003 | } else { |
| 1004 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 1005 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1006 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1007 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1008 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1009 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY] = a11yStreamAlias; |
| 1010 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1011 | if (updateVolumes) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1012 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], |
| 1013 | caller); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1014 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes( |
| 1015 | mStreamStates[a11yStreamAlias], caller); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1016 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1017 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1018 | sendMsg(mAudioHandler, |
| 1019 | MSG_SET_ALL_VOLUMES, |
| 1020 | SENDMSG_QUEUE, |
| 1021 | 0, |
| 1022 | 0, |
| 1023 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1024 | sendMsg(mAudioHandler, |
| 1025 | MSG_SET_ALL_VOLUMES, |
| 1026 | SENDMSG_QUEUE, |
| 1027 | 0, |
| 1028 | 0, |
| 1029 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY], 0); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1033 | private void readDockAudioSettings(ContentResolver cr) |
| 1034 | { |
| 1035 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1036 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1037 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1038 | sendMsg(mAudioHandler, |
| 1039 | MSG_SET_FORCE_USE, |
| 1040 | SENDMSG_QUEUE, |
| 1041 | AudioSystem.FOR_DOCK, |
| 1042 | mDockAudioMediaEnabled ? |
| 1043 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 1044 | null, |
| 1045 | 0); |
| 1046 | } |
| 1047 | |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1048 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1049 | private void updateMasterMono(ContentResolver cr) |
| 1050 | { |
| 1051 | final boolean masterMono = System.getIntForUser( |
| 1052 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1053 | if (DEBUG_VOL) { |
| 1054 | Log.d(TAG, String.format("Master mono %b", masterMono)); |
| 1055 | } |
| 1056 | AudioSystem.setMasterMono(masterMono); |
| 1057 | } |
| 1058 | |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 1059 | private void sendEncodedSurroundMode(ContentResolver cr) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1060 | { |
| 1061 | int encodedSurroundMode = Settings.Global.getInt( |
| 1062 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 1063 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 1064 | sendEncodedSurroundMode(encodedSurroundMode); |
| 1065 | } |
| 1066 | |
| 1067 | private void sendEncodedSurroundMode(int encodedSurroundMode) |
| 1068 | { |
| 1069 | // initialize to guaranteed bad value |
| 1070 | int forceSetting = AudioSystem.NUM_FORCE_CONFIG; |
| 1071 | switch (encodedSurroundMode) { |
| 1072 | case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO: |
| 1073 | forceSetting = AudioSystem.FORCE_NONE; |
| 1074 | break; |
| 1075 | case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER: |
| 1076 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_NEVER; |
| 1077 | break; |
| 1078 | case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS: |
| 1079 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_ALWAYS; |
| 1080 | break; |
| 1081 | default: |
| 1082 | Log.e(TAG, "updateSurroundSoundSettings: illegal value " |
| 1083 | + encodedSurroundMode); |
| 1084 | break; |
| 1085 | } |
| 1086 | if (forceSetting != AudioSystem.NUM_FORCE_CONFIG) { |
| 1087 | sendMsg(mAudioHandler, |
| 1088 | MSG_SET_FORCE_USE, |
| 1089 | SENDMSG_QUEUE, |
| 1090 | AudioSystem.FOR_ENCODED_SURROUND, |
| 1091 | forceSetting, |
| 1092 | null, |
| 1093 | 0); |
| 1094 | } |
| 1095 | } |
| 1096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | private void readPersistedSettings() { |
| 1098 | final ContentResolver cr = mContentResolver; |
| 1099 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1100 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1101 | Settings.Global.getInt( |
| 1102 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1103 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1104 | // sanity check in case the settings are restored from a device with incompatible |
| 1105 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1106 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1107 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1108 | } |
| 1109 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1110 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1111 | } |
| 1112 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1113 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1114 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1115 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1116 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1117 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1118 | synchronized(mSettingsLock) { |
| 1119 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1120 | if (mRingerModeExternal == -1) { |
| 1121 | mRingerModeExternal = mRingerMode; |
| 1122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1124 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1125 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1126 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1127 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1128 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1129 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1130 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1131 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1132 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1133 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1134 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1136 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1137 | readDockAudioSettings(cr); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 1138 | sendEncodedSurroundMode(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1139 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1140 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1141 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1142 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1143 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1144 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1145 | updateMasterMono(cr); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1146 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | // Each stream will read its own persisted settings |
| 1148 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1149 | // Broadcast the sticky intents |
| 1150 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1151 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | |
| 1153 | // Broadcast vibrate settings |
| 1154 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1155 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1156 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1157 | // Load settings for the volume controller |
| 1158 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1161 | private void readUserRestrictions() { |
| 1162 | final int currentUser = getCurrentUserId(); |
| 1163 | |
| 1164 | // Check the current user restriction. |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1165 | boolean masterMute = |
| 1166 | mUserManagerInternal.getUserRestriction(currentUser, |
| 1167 | UserManager.DISALLLOW_UNMUTE_DEVICE) |
| 1168 | || mUserManagerInternal.getUserRestriction(currentUser, |
| 1169 | UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1170 | if (mUseFixedVolume) { |
| 1171 | masterMute = false; |
| 1172 | AudioSystem.setMasterVolume(1.0f); |
| 1173 | } |
| 1174 | if (DEBUG_VOL) { |
| 1175 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1176 | } |
| 1177 | setSystemAudioMute(masterMute); |
| 1178 | AudioSystem.setMasterMute(masterMute); |
| 1179 | broadcastMasterMuteStatus(masterMute); |
| 1180 | |
| 1181 | boolean microphoneMute = mUserManagerInternal.getUserRestriction( |
| 1182 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1183 | if (DEBUG_VOL) { |
| 1184 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1185 | } |
| 1186 | AudioSystem.muteMicrophone(microphoneMute); |
| 1187 | } |
| 1188 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1189 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 1190 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 1191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | |
| 1193 | /////////////////////////////////////////////////////////////////////////// |
| 1194 | // IPC methods |
| 1195 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1197 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1198 | String callingPackage, String caller) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1199 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1200 | caller, Binder.getCallingUid()); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1204 | String callingPackage, String caller, int uid) { |
| 1205 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
| 1206 | + ", flags=" + flags + ", caller=" + caller); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1207 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1208 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1209 | if (mVolumeControlStream != -1) { |
| 1210 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1211 | } else { |
| 1212 | streamType = getActiveStreamType(suggestedStreamType); |
| 1213 | } |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1214 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1215 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1217 | // Play sounds on STREAM_RING only. |
| 1218 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1219 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1221 | } |
| 1222 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1223 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1224 | // Don't suppress mute/unmute requests |
| 1225 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1226 | direction = 0; |
| 1227 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1228 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1229 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1230 | } |
| 1231 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1232 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1236 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1237 | String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1238 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1239 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | private void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1243 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1244 | if (mUseFixedVolume) { |
| 1245 | return; |
| 1246 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1247 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1248 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | ensureValidDirection(direction); |
| 1251 | ensureValidStreamType(streamType); |
| 1252 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1253 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1254 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1255 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1256 | return; |
| 1257 | } |
| 1258 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1259 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1260 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1261 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1262 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1263 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1264 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1265 | |
| 1266 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1267 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1268 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1270 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1271 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1272 | // skip a2dp absolute volume control request when the device |
| 1273 | // is not an a2dp device |
| 1274 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1275 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1276 | return; |
| 1277 | } |
| 1278 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1279 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1280 | // so we handle user restrictions correctly. |
| 1281 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1282 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1283 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1284 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1285 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1286 | return; |
| 1287 | } |
| 1288 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1289 | // reset any pending volume command |
| 1290 | synchronized (mSafeMediaVolumeState) { |
| 1291 | mPendingVolumeCommand = null; |
| 1292 | } |
| 1293 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1294 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1295 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1296 | ((device & mFixedVolumeDevices) != 0)) { |
| 1297 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1298 | |
| 1299 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1300 | // volume is enforced, and max and 0 for the others. |
| 1301 | // This is simulated by stepping by the full allowed volume range |
| 1302 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1303 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1304 | step = mSafeMediaVolumeIndex; |
| 1305 | } else { |
| 1306 | step = streamState.getMaxIndex(); |
| 1307 | } |
| 1308 | if (aliasIndex != 0) { |
| 1309 | aliasIndex = step; |
| 1310 | } |
| 1311 | } else { |
| 1312 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1313 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1314 | } |
| 1315 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1316 | // If either the client forces allowing ringer modes for this adjustment, |
| 1317 | // or the stream type is one that is affected by ringer modes |
| 1318 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1319 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1320 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1321 | // do not vibrate if already in vibrate mode |
| 1322 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1323 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1324 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1325 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1326 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1327 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1328 | streamState.mIsMuted, callingPackage, flags); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1329 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1330 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1331 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1332 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1333 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1334 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1335 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1336 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1337 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1338 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1339 | // If the ringermode is suppressing media, prevent changes |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1340 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1341 | adjustVolume = false; |
| 1342 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1343 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1344 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1345 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1346 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1347 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1348 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1349 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1350 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1351 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1352 | synchronized (mA2dpAvrcpLock) { |
| 1353 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1354 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1355 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1356 | } |
| 1357 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1358 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1359 | if (isMuteAdjust) { |
| 1360 | boolean state; |
| 1361 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1362 | state = !streamState.mIsMuted; |
| 1363 | } else { |
| 1364 | state = direction == AudioManager.ADJUST_MUTE; |
| 1365 | } |
| 1366 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1367 | setSystemAudioMute(state); |
| 1368 | } |
| 1369 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1370 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1371 | if (!(readCameraSoundForced() |
| 1372 | && (mStreamStates[stream].getStreamType() |
| 1373 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1374 | mStreamStates[stream].mute(state); |
| 1375 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1379 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1380 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1381 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1382 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1383 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1384 | // Post message to set system volume (it in turn will post a |
| 1385 | // message to persist). |
| 1386 | if (streamState.mIsMuted) { |
| 1387 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1388 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1389 | // unmute immediately for volume up |
| 1390 | streamState.mute(false); |
| 1391 | } else if (direction == AudioManager.ADJUST_LOWER) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1392 | if (mIsSingleVolume) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1393 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1394 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1395 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1396 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1397 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1398 | sendMsg(mAudioHandler, |
| 1399 | MSG_SET_DEVICE_VOLUME, |
| 1400 | SENDMSG_QUEUE, |
| 1401 | device, |
| 1402 | 0, |
| 1403 | streamState, |
| 1404 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1405 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1406 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1407 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1408 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1409 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1410 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1411 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1412 | if (mHdmiManager != null) { |
| 1413 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1414 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1415 | if (mHdmiCecSink && |
| 1416 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1417 | oldIndex != newIndex) { |
| 1418 | synchronized (mHdmiPlaybackClient) { |
| 1419 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1420 | KeyEvent.KEYCODE_VOLUME_UP; |
Donghyun Cho | 5f6d404e | 2016-03-17 20:39:25 +0900 | [diff] [blame] | 1421 | final long ident = Binder.clearCallingIdentity(); |
| 1422 | try { |
| 1423 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1424 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1425 | } finally { |
| 1426 | Binder.restoreCallingIdentity(ident); |
| 1427 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1428 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1432 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1433 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1434 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1437 | // Called after a delay when volume down is pressed while muted |
| 1438 | private void onUnmuteStream(int stream, int flags) { |
| 1439 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1440 | streamState.mute(false); |
| 1441 | |
| 1442 | final int device = getDeviceForStream(stream); |
| 1443 | final int index = mStreamStates[stream].getIndex(device); |
| 1444 | sendVolumeUpdate(stream, index, index, flags); |
| 1445 | } |
| 1446 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1447 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1448 | if (mHdmiManager == null |
| 1449 | || mHdmiTvClient == null |
| 1450 | || oldVolume == newVolume |
| 1451 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1452 | |
| 1453 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1454 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1455 | synchronized (mHdmiManager) { |
| 1456 | if (!mHdmiSystemAudioSupported) return; |
| 1457 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1458 | final long token = Binder.clearCallingIdentity(); |
| 1459 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1460 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1461 | } finally { |
| 1462 | Binder.restoreCallingIdentity(token); |
| 1463 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1468 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1469 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1470 | class StreamVolumeCommand { |
| 1471 | public final int mStreamType; |
| 1472 | public final int mIndex; |
| 1473 | public final int mFlags; |
| 1474 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1475 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1476 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1477 | mStreamType = streamType; |
| 1478 | mIndex = index; |
| 1479 | mFlags = flags; |
| 1480 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1481 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1482 | |
| 1483 | @Override |
| 1484 | public String toString() { |
| 1485 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1486 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1487 | .append(mDevice).append('}').toString(); |
| 1488 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1489 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1490 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1491 | private int getNewRingerMode(int stream, int index, int flags) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1492 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1493 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1494 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1495 | int newRingerMode; |
| 1496 | if (index == 0) { |
| 1497 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1498 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1499 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1500 | } else { |
| 1501 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1502 | } |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1503 | return newRingerMode; |
| 1504 | } |
| 1505 | return getRingerModeExternal(); |
| 1506 | } |
| 1507 | |
| 1508 | private boolean isAndroidNPlus(String caller) { |
| 1509 | try { |
| 1510 | final ApplicationInfo applicationInfo = |
| 1511 | mContext.getPackageManager().getApplicationInfoAsUser( |
| 1512 | caller, 0, UserHandle.getUserId(Binder.getCallingUid())); |
| 1513 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1514 | return true; |
| 1515 | } |
| 1516 | return false; |
| 1517 | } catch (PackageManager.NameNotFoundException e) { |
| 1518 | return true; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | private boolean wouldToggleZenMode(int newMode) { |
| 1523 | if (getRingerModeExternal() == AudioManager.RINGER_MODE_SILENT |
| 1524 | && newMode != AudioManager.RINGER_MODE_SILENT) { |
| 1525 | return true; |
| 1526 | } else if (getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT |
| 1527 | && newMode == AudioManager.RINGER_MODE_SILENT) { |
| 1528 | return true; |
| 1529 | } |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1534 | String caller) { |
| 1535 | final int stream = mStreamVolumeAlias[streamType]; |
| 1536 | setStreamVolumeInt(stream, index, device, false, caller); |
| 1537 | // setting volume on ui sounds stream type also controls silent mode |
| 1538 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1539 | (stream == getUiSoundsStreamType())) { |
| 1540 | setRingerMode(getNewRingerMode(stream, index, flags), |
| 1541 | TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1542 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1543 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1544 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1548 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1549 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1550 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1554 | String caller, int uid) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1555 | if (DEBUG_VOL) { |
| 1556 | Log.d(TAG, "setStreamVolume(stream=" + streamType+", index=" + index |
| 1557 | + ", calling=" + callingPackage + ")"); |
| 1558 | } |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1559 | if (mUseFixedVolume) { |
| 1560 | return; |
| 1561 | } |
| 1562 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1563 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1564 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1565 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1566 | |
| 1567 | final int device = getDeviceForStream(streamType); |
| 1568 | int oldIndex; |
| 1569 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1570 | // skip a2dp absolute volume control request when the device |
| 1571 | // is not an a2dp device |
| 1572 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1573 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1574 | return; |
| 1575 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1576 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1577 | // so we handle user restrictions correctly. |
| 1578 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1579 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1580 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1581 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1582 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1583 | return; |
| 1584 | } |
| 1585 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1586 | if (isAndroidNPlus(callingPackage) |
| 1587 | && wouldToggleZenMode(getNewRingerMode(streamTypeAlias, index, flags)) |
| 1588 | && !mNm.isNotificationPolicyAccessGrantedForPackage(callingPackage)) { |
| 1589 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 1590 | } |
| 1591 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1592 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
| 1593 | return; |
| 1594 | } |
| 1595 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1596 | synchronized (mSafeMediaVolumeState) { |
| 1597 | // reset any pending volume command |
| 1598 | mPendingVolumeCommand = null; |
| 1599 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1600 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1601 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1602 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1603 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1604 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1605 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1606 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1607 | synchronized (mA2dpAvrcpLock) { |
| 1608 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1609 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1610 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1614 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1615 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1616 | } |
| 1617 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1618 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1619 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1620 | ((device & mFixedVolumeDevices) != 0)) { |
| 1621 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1622 | |
| 1623 | // volume is either 0 or max allowed for fixed volume devices |
| 1624 | if (index != 0) { |
| 1625 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1626 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1627 | index = mSafeMediaVolumeIndex; |
| 1628 | } else { |
| 1629 | index = streamState.getMaxIndex(); |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1634 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1635 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1636 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1637 | streamType, index, flags, device); |
| 1638 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1639 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1640 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1641 | } |
| 1642 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1643 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1646 | // No ringer affected streams can be changed in total silence mode except those that |
| 1647 | // will cause the device to exit total silence mode. |
| 1648 | private boolean volumeAdjustmentAllowedByDnd(int streamTypeAlias, int flags) { |
| 1649 | if (mNm.getZenMode() == Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 1650 | && isStreamMutedByRingerMode(streamTypeAlias)) { |
| 1651 | if (!(((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1652 | (streamTypeAlias == getUiSoundsStreamType()))) { |
| 1653 | return false; |
| 1654 | } |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1659 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1660 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1661 | synchronized(mForceControlStreamLock) { |
| 1662 | mVolumeControlStream = streamType; |
| 1663 | if (mVolumeControlStream == -1) { |
| 1664 | if (mForceControlStreamClient != null) { |
| 1665 | mForceControlStreamClient.release(); |
| 1666 | mForceControlStreamClient = null; |
| 1667 | } |
| 1668 | } else { |
| 1669 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1675 | private IBinder mCb; // To be notified of client's death |
| 1676 | |
| 1677 | ForceControlStreamClient(IBinder cb) { |
| 1678 | if (cb != null) { |
| 1679 | try { |
| 1680 | cb.linkToDeath(this, 0); |
| 1681 | } catch (RemoteException e) { |
| 1682 | // Client has died! |
| 1683 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1684 | cb = null; |
| 1685 | } |
| 1686 | } |
| 1687 | mCb = cb; |
| 1688 | } |
| 1689 | |
| 1690 | public void binderDied() { |
| 1691 | synchronized(mForceControlStreamLock) { |
| 1692 | Log.w(TAG, "SCO client died"); |
| 1693 | if (mForceControlStreamClient != this) { |
| 1694 | Log.w(TAG, "unregistered control stream client died"); |
| 1695 | } else { |
| 1696 | mForceControlStreamClient = null; |
| 1697 | mVolumeControlStream = -1; |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | public void release() { |
| 1703 | if (mCb != null) { |
| 1704 | mCb.unlinkToDeath(this, 0); |
| 1705 | mCb = null; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1710 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1711 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1712 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1713 | final long ident = Binder.clearCallingIdentity(); |
| 1714 | try { |
| 1715 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1716 | } finally { |
| 1717 | Binder.restoreCallingIdentity(ident); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1722 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1723 | final long ident = Binder.clearCallingIdentity(); |
| 1724 | try { |
| 1725 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1726 | } finally { |
| 1727 | Binder.restoreCallingIdentity(ident); |
| 1728 | } |
| 1729 | } |
| 1730 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1731 | private int getCurrentUserId() { |
| 1732 | final long ident = Binder.clearCallingIdentity(); |
| 1733 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1734 | UserInfo currentUser = ActivityManager.getService().getCurrentUser(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1735 | return currentUser.id; |
| 1736 | } catch (RemoteException e) { |
| 1737 | // Activity manager not running, nothing we can do assume user 0. |
| 1738 | } finally { |
| 1739 | Binder.restoreCallingIdentity(ident); |
| 1740 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 1741 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1742 | } |
| 1743 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1744 | // UI update and Broadcast Intent |
| 1745 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 1746 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1747 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1748 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1749 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1750 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1751 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1754 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1755 | // receives volume notification from Audio Receiver. |
| 1756 | private int updateFlagsForSystemAudio(int flags) { |
| 1757 | if (mHdmiTvClient != null) { |
| 1758 | synchronized (mHdmiTvClient) { |
| 1759 | if (mHdmiSystemAudioSupported && |
| 1760 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1761 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | return flags; |
| 1766 | } |
| 1767 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1768 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1769 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1770 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1771 | broadcastMasterMuteStatus(muted); |
| 1772 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1773 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1774 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1775 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1776 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1777 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1778 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1779 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1780 | } |
| 1781 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | * Sets the stream state's index, and posts a message to set system volume. |
| 1784 | * This will not call out to the UI. Assumes a valid stream type. |
| 1785 | * |
| 1786 | * @param streamType Type of the stream |
| 1787 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1788 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | * @param force If true, set the volume even if the desired volume is same |
| 1790 | * as the current volume. |
| 1791 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1792 | private void setStreamVolumeInt(int streamType, |
| 1793 | int index, |
| 1794 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1795 | boolean force, |
| 1796 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1798 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1799 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1800 | // Post message to set system volume (it in turn will post a message |
| 1801 | // to persist). |
| 1802 | sendMsg(mAudioHandler, |
| 1803 | MSG_SET_DEVICE_VOLUME, |
| 1804 | SENDMSG_QUEUE, |
| 1805 | device, |
| 1806 | 0, |
| 1807 | streamState, |
| 1808 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1812 | private void setSystemAudioMute(boolean state) { |
| 1813 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1814 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1815 | if (!mHdmiSystemAudioSupported) return; |
| 1816 | synchronized (mHdmiTvClient) { |
| 1817 | final long token = Binder.clearCallingIdentity(); |
| 1818 | try { |
| 1819 | mHdmiTvClient.setSystemAudioMute(state); |
| 1820 | } finally { |
| 1821 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1822 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1827 | /** get stream mute state. */ |
| 1828 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1829 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1830 | streamType = getActiveStreamType(streamType); |
| 1831 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1832 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1833 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1834 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1837 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1838 | private IBinder mICallback; // To be notified of client's death |
| 1839 | |
| 1840 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1841 | mICallback = cb; |
| 1842 | try { |
| 1843 | cb.linkToDeath(this, 0/*flags*/); |
| 1844 | } catch (RemoteException e) { |
| 1845 | Log.e(TAG, "can't link to death", e); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | boolean isHandlerFor(IBinder cb) { |
| 1850 | return mICallback.equals(cb); |
| 1851 | } |
| 1852 | |
| 1853 | void forget() { |
| 1854 | try { |
| 1855 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1856 | } catch (NoSuchElementException e) { |
| 1857 | Log.e(TAG, "error unlinking to death", e); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | public void binderDied() { |
| 1862 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1863 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1869 | * @return true if there is a registered death handler, false otherwise */ |
| 1870 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1871 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1872 | while (it.hasNext()) { |
| 1873 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1874 | if (handler.isHandlerFor(cb)) { |
| 1875 | handler.forget(); |
| 1876 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1877 | return true; |
| 1878 | } |
| 1879 | } |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
| 1883 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1884 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1885 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1886 | while (it.hasNext()) { |
| 1887 | if (it.next().isHandlerFor(cb)) { |
| 1888 | return true; |
| 1889 | } |
| 1890 | } |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | private int mRmtSbmxFullVolRefCount = 0; |
| 1895 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1896 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1897 | |
| 1898 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1899 | if (cb == null) { |
| 1900 | return; |
| 1901 | } |
| 1902 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1903 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1904 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1905 | return; |
| 1906 | } |
| 1907 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1908 | boolean applyRequired = false; |
| 1909 | if (startForcing) { |
| 1910 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1911 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1912 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1913 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1914 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1915 | applyRequired = true; |
| 1916 | } |
| 1917 | mRmtSbmxFullVolRefCount++; |
| 1918 | } |
| 1919 | } else { |
| 1920 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1921 | mRmtSbmxFullVolRefCount--; |
| 1922 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1923 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1924 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1925 | applyRequired = true; |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | if (applyRequired) { |
| 1930 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1931 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1932 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1937 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 1938 | int userId) { |
| 1939 | // If we are being called by the system check for user we are going to change |
| 1940 | // so we handle user restrictions correctly. |
| 1941 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1942 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 1943 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1944 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 1945 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1946 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1947 | return; |
| 1948 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1949 | if (userId != UserHandle.getCallingUserId() && |
| 1950 | mContext.checkCallingOrSelfPermission( |
| 1951 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1952 | != PackageManager.PERMISSION_GRANTED) { |
| 1953 | return; |
| 1954 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1955 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 1956 | } |
| 1957 | |
| 1958 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 1959 | if (DEBUG_VOL) { |
| 1960 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 1961 | } |
| 1962 | if (mUseFixedVolume) { |
| 1963 | return; // If using fixed volume, we don't mute. |
| 1964 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1965 | if (getCurrentUserId() == userId) { |
| 1966 | if (mute != AudioSystem.getMasterMute()) { |
| 1967 | setSystemAudioMute(mute); |
| 1968 | AudioSystem.setMasterMute(mute); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1969 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1970 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1971 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1972 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 1973 | sendBroadcastToAll(intent); |
| 1974 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1975 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /** get master mute state. */ |
| 1979 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1980 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1983 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 1984 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 1985 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1986 | } |
| 1987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | /** @see AudioManager#getStreamVolume(int) */ |
| 1989 | public int getStreamVolume(int streamType) { |
| 1990 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1991 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1992 | synchronized (VolumeStreamState.class) { |
| 1993 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1994 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1995 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1996 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1997 | index = 0; |
| 1998 | } |
| 1999 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 2000 | (device & mFixedVolumeDevices) != 0) { |
| 2001 | index = mStreamStates[streamType].getMaxIndex(); |
| 2002 | } |
| 2003 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 2008 | public int getStreamMaxVolume(int streamType) { |
| 2009 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2010 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2013 | /** @see AudioManager#getStreamMinVolume(int) */ |
| 2014 | public int getStreamMinVolume(int streamType) { |
| 2015 | ensureValidStreamType(streamType); |
| 2016 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 2017 | } |
| 2018 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2019 | /** Get last audible volume before stream was muted. */ |
| 2020 | public int getLastAudibleStreamVolume(int streamType) { |
| 2021 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2022 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2023 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2026 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 2027 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 2028 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2031 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 2032 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2033 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 2034 | // If we are being called by the system check for user we are going to change |
| 2035 | // so we handle user restrictions correctly. |
| 2036 | int uid = Binder.getCallingUid(); |
| 2037 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2038 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2039 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2040 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 2041 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2042 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2043 | return; |
| 2044 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 2045 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 2046 | return; |
| 2047 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2048 | if (userId != UserHandle.getCallingUserId() && |
| 2049 | mContext.checkCallingOrSelfPermission( |
| 2050 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2051 | != PackageManager.PERMISSION_GRANTED) { |
| 2052 | return; |
| 2053 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2054 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 2055 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2056 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2057 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 2058 | if (DEBUG_VOL) { |
| 2059 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 2060 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2061 | // If mute is for current user actually mute, else just persist the setting |
| 2062 | // which will be loaded on user switch. |
| 2063 | if (getCurrentUserId() == userId) { |
| 2064 | AudioSystem.muteMicrophone(on); |
| 2065 | } |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 2066 | // Post a persist microphone msg. |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2069 | @Override |
| 2070 | public int getRingerModeExternal() { |
| 2071 | synchronized(mSettingsLock) { |
| 2072 | return mRingerModeExternal; |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | @Override |
| 2077 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2078 | synchronized(mSettingsLock) { |
| 2079 | return mRingerMode; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2084 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2085 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 2086 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2087 | } |
| 2088 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2089 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2090 | public boolean isValidRingerMode(int ringerMode) { |
| 2091 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2092 | } |
| 2093 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2094 | public void setRingerModeExternal(int ringerMode, String caller) { |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2095 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 2096 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller)) { |
| 2097 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 2098 | } |
| 2099 | |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2100 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2104 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2105 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 2109 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2110 | return; |
| 2111 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2112 | if (caller == null || caller.length() == 0) { |
| 2113 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2114 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2115 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2116 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2117 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2118 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2119 | final long identity = Binder.clearCallingIdentity(); |
| 2120 | try { |
| 2121 | synchronized (mSettingsLock) { |
| 2122 | final int ringerModeInternal = getRingerModeInternal(); |
| 2123 | final int ringerModeExternal = getRingerModeExternal(); |
| 2124 | if (external) { |
| 2125 | setRingerModeExt(ringerMode); |
| 2126 | if (mRingerModeDelegate != null) { |
| 2127 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2128 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2129 | } |
| 2130 | if (ringerMode != ringerModeInternal) { |
| 2131 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2132 | } |
| 2133 | } else /*internal*/ { |
| 2134 | if (ringerMode != ringerModeInternal) { |
| 2135 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2136 | } |
| 2137 | if (mRingerModeDelegate != null) { |
| 2138 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2139 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2140 | } |
| 2141 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2142 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2143 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2144 | } finally { |
| 2145 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2149 | private void setRingerModeExt(int ringerMode) { |
| 2150 | synchronized(mSettingsLock) { |
| 2151 | if (ringerMode == mRingerModeExternal) return; |
| 2152 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2153 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2154 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2155 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2156 | } |
| 2157 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2158 | private void muteRingerModeStreams() { |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2159 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 2160 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 2161 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 2162 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2163 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2164 | final boolean ringerModeMute = mRingerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2165 | || mRingerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2166 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2167 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 2168 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 2169 | if (isMuted == shouldMute) continue; |
| 2170 | if (!shouldMute) { |
| 2171 | // unmute |
| 2172 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2173 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2174 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2175 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2176 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2177 | int device = vss.mIndexMap.keyAt(i); |
| 2178 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2179 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2180 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2181 | } |
| 2182 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2183 | // Persist volume for stream ring when it is changed here |
| 2184 | final int device = getDeviceForStream(streamType); |
| 2185 | sendMsg(mAudioHandler, |
| 2186 | MSG_PERSIST_VOLUME, |
| 2187 | SENDMSG_QUEUE, |
| 2188 | device, |
| 2189 | 0, |
| 2190 | mStreamStates[streamType], |
| 2191 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2192 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2193 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2194 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2195 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2196 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2197 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2198 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2199 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2205 | final boolean change; |
| 2206 | synchronized(mSettingsLock) { |
| 2207 | change = mRingerMode != ringerMode; |
| 2208 | mRingerMode = ringerMode; |
| 2209 | } |
| 2210 | |
| 2211 | muteRingerModeStreams(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2212 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2213 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2214 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2215 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2216 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2217 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2218 | if (change) { |
| 2219 | // Send sticky broadcast |
| 2220 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2221 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2224 | /** @see AudioManager#shouldVibrate(int) */ |
| 2225 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2226 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2227 | |
| 2228 | switch (getVibrateSetting(vibrateType)) { |
| 2229 | |
| 2230 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2231 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2232 | |
| 2233 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2234 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | |
| 2236 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2237 | // return false, even for incoming calls |
| 2238 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2239 | |
| 2240 | default: |
| 2241 | return false; |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2246 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2247 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2248 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2249 | } |
| 2250 | |
| 2251 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2252 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2253 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2254 | if (!mHasVibrator) return; |
| 2255 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2256 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2257 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2258 | |
| 2259 | // Broadcast change |
| 2260 | broadcastVibrateSetting(vibrateType); |
| 2261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2264 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2265 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2266 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2267 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2268 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2269 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2270 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2271 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2275 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2276 | synchronized(mSetModeDeathHandlers) { |
| 2277 | Log.w(TAG, "setMode() client died"); |
| 2278 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2279 | if (index < 0) { |
| 2280 | Log.w(TAG, "unregistered setMode() client died"); |
| 2281 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2282 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2285 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2286 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2287 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2288 | final long ident = Binder.clearCallingIdentity(); |
| 2289 | disconnectBluetoothSco(newModeOwnerPid); |
| 2290 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2291 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2292 | } |
| 2293 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2294 | public int getPid() { |
| 2295 | return mPid; |
| 2296 | } |
| 2297 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2298 | public void setMode(int mode) { |
| 2299 | mMode = mode; |
| 2300 | } |
| 2301 | |
| 2302 | public int getMode() { |
| 2303 | return mMode; |
| 2304 | } |
| 2305 | |
| 2306 | public IBinder getBinder() { |
| 2307 | return mCb; |
| 2308 | } |
| 2309 | } |
| 2310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2312 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2313 | 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] | 2314 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2315 | return; |
| 2316 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2317 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2318 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2319 | (mContext.checkCallingOrSelfPermission( |
| 2320 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2321 | != PackageManager.PERMISSION_GRANTED)) { |
| 2322 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2323 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2324 | return; |
| 2325 | } |
| 2326 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2327 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2328 | return; |
| 2329 | } |
| 2330 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2331 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2332 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2333 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2334 | mode = mMode; |
| 2335 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2336 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2337 | } |
| 2338 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2339 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2340 | if (newModeOwnerPid != 0) { |
| 2341 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2342 | } |
| 2343 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2344 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2345 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2346 | // 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] | 2347 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2348 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2349 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2350 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2351 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2352 | if (cb == null) { |
| 2353 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2354 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2355 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2356 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2357 | SetModeDeathHandler hdlr = null; |
| 2358 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2359 | while (iter.hasNext()) { |
| 2360 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2361 | if (h.getPid() == pid) { |
| 2362 | hdlr = h; |
| 2363 | // Remove from client list so that it is re-inserted at top of list |
| 2364 | iter.remove(); |
| 2365 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2366 | break; |
| 2367 | } |
| 2368 | } |
| 2369 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 2370 | do { |
| 2371 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2372 | // get new mode from client at top the list if any |
| 2373 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2374 | hdlr = mSetModeDeathHandlers.get(0); |
| 2375 | cb = hdlr.getBinder(); |
| 2376 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2377 | if (DEBUG_MODE) { |
| 2378 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2379 | + hdlr.mPid); |
| 2380 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2381 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2382 | } else { |
| 2383 | if (hdlr == null) { |
| 2384 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2385 | } |
| 2386 | // Register for client death notification |
| 2387 | try { |
| 2388 | cb.linkToDeath(hdlr, 0); |
| 2389 | } catch (RemoteException e) { |
| 2390 | // Client has died! |
| 2391 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2392 | } |
| 2393 | |
| 2394 | // Last client to call setMode() is always at top of client list |
| 2395 | // as required by SetModeDeathHandler.binderDied() |
| 2396 | mSetModeDeathHandlers.add(0, hdlr); |
| 2397 | hdlr.setMode(mode); |
| 2398 | } |
| 2399 | |
| 2400 | if (mode != mMode) { |
| 2401 | status = AudioSystem.setPhoneState(mode); |
| 2402 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2403 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2404 | mMode = mode; |
| 2405 | } else { |
| 2406 | if (hdlr != null) { |
| 2407 | mSetModeDeathHandlers.remove(hdlr); |
| 2408 | cb.unlinkToDeath(hdlr, 0); |
| 2409 | } |
| 2410 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2411 | 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] | 2412 | mode = AudioSystem.MODE_NORMAL; |
| 2413 | } |
| 2414 | } else { |
| 2415 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2416 | } |
| 2417 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2418 | |
| 2419 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 2420 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2421 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2422 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2423 | } else { |
| 2424 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2426 | } |
| 2427 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2428 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2429 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2430 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2431 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2432 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2433 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2434 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | /** @see AudioManager#getMode() */ |
| 2438 | public int getMode() { |
| 2439 | return mMode; |
| 2440 | } |
| 2441 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2442 | //========================================================================================== |
| 2443 | // Sound Effects |
| 2444 | //========================================================================================== |
| 2445 | |
| 2446 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2447 | private static final String ATTR_VERSION = "version"; |
| 2448 | private static final String TAG_GROUP = "group"; |
| 2449 | private static final String ATTR_GROUP_NAME = "name"; |
| 2450 | private static final String TAG_ASSET = "asset"; |
| 2451 | private static final String ATTR_ASSET_ID = "id"; |
| 2452 | private static final String ATTR_ASSET_FILE = "file"; |
| 2453 | |
| 2454 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2455 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2456 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2457 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2458 | |
| 2459 | class LoadSoundEffectReply { |
| 2460 | public int mStatus = 1; |
| 2461 | }; |
| 2462 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2463 | private void loadTouchSoundAssetDefaults() { |
| 2464 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2465 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2466 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2467 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | private void loadTouchSoundAssets() { |
| 2472 | XmlResourceParser parser = null; |
| 2473 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2474 | // only load assets once. |
| 2475 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2479 | loadTouchSoundAssetDefaults(); |
| 2480 | |
| 2481 | try { |
| 2482 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2483 | |
| 2484 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2485 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2486 | boolean inTouchSoundsGroup = false; |
| 2487 | |
| 2488 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2489 | while (true) { |
| 2490 | XmlUtils.nextElement(parser); |
| 2491 | String element = parser.getName(); |
| 2492 | if (element == null) { |
| 2493 | break; |
| 2494 | } |
| 2495 | if (element.equals(TAG_GROUP)) { |
| 2496 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2497 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2498 | inTouchSoundsGroup = true; |
| 2499 | break; |
| 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | while (inTouchSoundsGroup) { |
| 2504 | XmlUtils.nextElement(parser); |
| 2505 | String element = parser.getName(); |
| 2506 | if (element == null) { |
| 2507 | break; |
| 2508 | } |
| 2509 | if (element.equals(TAG_ASSET)) { |
| 2510 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2511 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2512 | int fx; |
| 2513 | |
| 2514 | try { |
| 2515 | Field field = AudioManager.class.getField(id); |
| 2516 | fx = field.getInt(null); |
| 2517 | } catch (Exception e) { |
| 2518 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2519 | continue; |
| 2520 | } |
| 2521 | |
| 2522 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2523 | if (i == -1) { |
| 2524 | i = SOUND_EFFECT_FILES.size(); |
| 2525 | SOUND_EFFECT_FILES.add(file); |
| 2526 | } |
| 2527 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2528 | } else { |
| 2529 | break; |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | } catch (Resources.NotFoundException e) { |
| 2534 | Log.w(TAG, "audio assets file not found", e); |
| 2535 | } catch (XmlPullParserException e) { |
| 2536 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2537 | } catch (IOException e) { |
| 2538 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2539 | } finally { |
| 2540 | if (parser != null) { |
| 2541 | parser.close(); |
| 2542 | } |
| 2543 | } |
| 2544 | } |
| 2545 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2546 | /** @see AudioManager#playSoundEffect(int) */ |
| 2547 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2548 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2552 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2553 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2554 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2558 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | effectType, (int) (volume * 1000), null, 0); |
| 2560 | } |
| 2561 | |
| 2562 | /** |
| 2563 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2564 | * 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] | 2565 | */ |
| 2566 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2567 | int attempts = 3; |
| 2568 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2569 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2570 | synchronized (reply) { |
| 2571 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2572 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2573 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2574 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2575 | } catch (InterruptedException e) { |
| 2576 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2577 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2580 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | /** |
| 2584 | * Unloads samples from the sound pool. |
| 2585 | * This method can be called to free some memory when |
| 2586 | * sound effects are disabled. |
| 2587 | */ |
| 2588 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2589 | 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] | 2590 | } |
| 2591 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2592 | class SoundPoolListenerThread extends Thread { |
| 2593 | public SoundPoolListenerThread() { |
| 2594 | super("SoundPoolListenerThread"); |
| 2595 | } |
| 2596 | |
| 2597 | @Override |
| 2598 | public void run() { |
| 2599 | |
| 2600 | Looper.prepare(); |
| 2601 | mSoundPoolLooper = Looper.myLooper(); |
| 2602 | |
| 2603 | synchronized (mSoundEffectsLock) { |
| 2604 | if (mSoundPool != null) { |
| 2605 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2606 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2607 | } |
| 2608 | mSoundEffectsLock.notify(); |
| 2609 | } |
| 2610 | Looper.loop(); |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | private final class SoundPoolCallback implements |
| 2615 | android.media.SoundPool.OnLoadCompleteListener { |
| 2616 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2617 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2618 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2619 | |
| 2620 | public int status() { |
| 2621 | return mStatus; |
| 2622 | } |
| 2623 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2624 | public void setSamples(int[] samples) { |
| 2625 | for (int i = 0; i < samples.length; i++) { |
| 2626 | // do not wait ack for samples rejected upfront by SoundPool |
| 2627 | if (samples[i] > 0) { |
| 2628 | mSamples.add(samples[i]); |
| 2629 | } |
| 2630 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2634 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2635 | int i = mSamples.indexOf(sampleId); |
| 2636 | if (i >= 0) { |
| 2637 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2638 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2639 | if ((status != 0) || mSamples. isEmpty()) { |
| 2640 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2641 | mSoundEffectsLock.notify(); |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2647 | /** @see AudioManager#reloadAudioSettings() */ |
| 2648 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2649 | readAudioSettings(false /*userSwitch*/); |
| 2650 | } |
| 2651 | |
| 2652 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2653 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2654 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 2655 | readUserRestrictions(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2656 | |
| 2657 | // restore volume settings |
| 2658 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2659 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2660 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2661 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2662 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2663 | continue; |
| 2664 | } |
| 2665 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2666 | streamState.readSettings(); |
| 2667 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2668 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2669 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2670 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2671 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2672 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2676 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2677 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2678 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2679 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2680 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2681 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2682 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2683 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2684 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2685 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2686 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2687 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2688 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2689 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2690 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2691 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2694 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2695 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2696 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2697 | return; |
| 2698 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2699 | |
| 2700 | if (on) { |
| 2701 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2702 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2703 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2704 | } |
| 2705 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2706 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2707 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2708 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2709 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2710 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2711 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2715 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2716 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2719 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2720 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2721 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2722 | return; |
| 2723 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2724 | setBluetoothScoOnInt(on); |
| 2725 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2726 | |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2727 | public void setBluetoothScoOnInt(boolean on) { |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2728 | if (on) { |
| 2729 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2730 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2731 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2732 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2733 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2734 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2735 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2736 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2737 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2741 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2742 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 2745 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2746 | public void setBluetoothA2dpOn(boolean on) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 2747 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2748 | mBluetoothA2dpEnabled = on; |
| 2749 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2750 | AudioSystem.FOR_MEDIA, |
| 2751 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2752 | null, 0); |
| 2753 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 2756 | /** @see AudioManager#isBluetoothA2dpOn() */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2757 | public boolean isBluetoothA2dpOn() { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 2758 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2759 | return mBluetoothA2dpEnabled; |
| 2760 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2763 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2764 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2765 | int scoAudioMode = |
| 2766 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2767 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2768 | startBluetoothScoInt(cb, scoAudioMode); |
| 2769 | } |
| 2770 | |
| 2771 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2772 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2773 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2774 | } |
| 2775 | |
| 2776 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2777 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2778 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2779 | return; |
| 2780 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2781 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2782 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2783 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2784 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2785 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2786 | // client is created. |
| 2787 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2788 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2789 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /** @see AudioManager#stopBluetoothSco() */ |
| 2793 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2794 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2795 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2796 | return; |
| 2797 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2798 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2799 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2800 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2801 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2802 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2803 | if (client != null) { |
| 2804 | client.decCount(); |
| 2805 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2806 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2809 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2810 | private class ScoClient implements IBinder.DeathRecipient { |
| 2811 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2812 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2813 | private int mStartcount; // number of SCO connections started by this client |
| 2814 | |
| 2815 | ScoClient(IBinder cb) { |
| 2816 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2817 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2818 | mStartcount = 0; |
| 2819 | } |
| 2820 | |
| 2821 | public void binderDied() { |
| 2822 | synchronized(mScoClients) { |
| 2823 | Log.w(TAG, "SCO client died"); |
| 2824 | int index = mScoClients.indexOf(this); |
| 2825 | if (index < 0) { |
| 2826 | Log.w(TAG, "unregistered SCO client died"); |
| 2827 | } else { |
| 2828 | clearCount(true); |
| 2829 | mScoClients.remove(this); |
| 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2834 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2835 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2836 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2837 | if (mStartcount == 0) { |
| 2838 | try { |
| 2839 | mCb.linkToDeath(this, 0); |
| 2840 | } catch (RemoteException e) { |
| 2841 | // client has already died! |
| 2842 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2843 | } |
| 2844 | } |
| 2845 | mStartcount++; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | public void decCount() { |
| 2850 | synchronized(mScoClients) { |
| 2851 | if (mStartcount == 0) { |
| 2852 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2853 | } else { |
| 2854 | mStartcount--; |
| 2855 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2856 | try { |
| 2857 | mCb.unlinkToDeath(this, 0); |
| 2858 | } catch (NoSuchElementException e) { |
| 2859 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2860 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2861 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2862 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | public void clearCount(boolean stopSco) { |
| 2868 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2869 | if (mStartcount != 0) { |
| 2870 | try { |
| 2871 | mCb.unlinkToDeath(this, 0); |
| 2872 | } catch (NoSuchElementException e) { |
| 2873 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2874 | } |
| 2875 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2876 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2877 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2878 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2879 | } |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | public int getCount() { |
| 2884 | return mStartcount; |
| 2885 | } |
| 2886 | |
| 2887 | public IBinder getBinder() { |
| 2888 | return mCb; |
| 2889 | } |
| 2890 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2891 | public int getPid() { |
| 2892 | return mCreatorPid; |
| 2893 | } |
| 2894 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2895 | public int totalCount() { |
| 2896 | synchronized(mScoClients) { |
| 2897 | int count = 0; |
| 2898 | int size = mScoClients.size(); |
| 2899 | for (int i = 0; i < size; i++) { |
| 2900 | count += mScoClients.get(i).getCount(); |
| 2901 | } |
| 2902 | return count; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2906 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2907 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2908 | if (totalCount() == 0) { |
| 2909 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2910 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2911 | // the connection. |
| 2912 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2913 | // 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] | 2914 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2915 | synchronized(mSetModeDeathHandlers) { |
| 2916 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2917 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2918 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2919 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2920 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2921 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2922 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2923 | if (mBluetoothHeadsetDevice != null) { |
| 2924 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2925 | mContentResolver, |
| 2926 | "bluetooth_sco_channel_"+ |
| 2927 | mBluetoothHeadsetDevice.getAddress(), |
| 2928 | SCO_MODE_VIRTUAL_CALL)); |
| 2929 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2930 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2931 | } |
| 2932 | } else { |
| 2933 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2934 | } |
| 2935 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2936 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2937 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2938 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2939 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2940 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2941 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2942 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2943 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2944 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2945 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2946 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2947 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2948 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2949 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2950 | } else { |
| 2951 | broadcastScoConnectionState( |
| 2952 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2953 | } |
| 2954 | } else if (getBluetoothHeadset()) { |
| 2955 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2956 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2957 | } else { |
| 2958 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2959 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2960 | } |
| 2961 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2962 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2963 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2964 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2965 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2966 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2967 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2968 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2969 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2970 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2971 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2972 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2973 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2974 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2975 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2976 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2977 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2978 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2979 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2980 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2981 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2982 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2983 | broadcastScoConnectionState( |
| 2984 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2985 | } |
| 2986 | } else if (getBluetoothHeadset()) { |
| 2987 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2988 | } |
| 2989 | } else { |
| 2990 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2991 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2992 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2993 | } |
| 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2998 | private void checkScoAudioState() { |
| 2999 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3000 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3001 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3002 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 3003 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 3004 | } |
| 3005 | } |
| 3006 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3007 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3008 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3009 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3010 | int size = mScoClients.size(); |
| 3011 | for (int i = 0; i < size; i++) { |
| 3012 | client = mScoClients.get(i); |
| 3013 | if (client.getBinder() == cb) |
| 3014 | return client; |
| 3015 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3016 | if (create) { |
| 3017 | client = new ScoClient(cb); |
| 3018 | mScoClients.add(client); |
| 3019 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3020 | return client; |
| 3021 | } |
| 3022 | } |
| 3023 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3024 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3025 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3026 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3027 | int size = mScoClients.size(); |
| 3028 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3029 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3030 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3031 | cl.clearCount(stopSco); |
| 3032 | } else { |
| 3033 | savedClient = cl; |
| 3034 | } |
| 3035 | } |
| 3036 | mScoClients.clear(); |
| 3037 | if (savedClient != null) { |
| 3038 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3043 | private boolean getBluetoothHeadset() { |
| 3044 | boolean result = false; |
| 3045 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 3046 | if (adapter != null) { |
| 3047 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 3048 | BluetoothProfile.HEADSET); |
| 3049 | } |
| 3050 | // If we could not get a bluetooth headset proxy, send a failure message |
| 3051 | // without delay to reset the SCO audio state and clear SCO clients. |
| 3052 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 3053 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3054 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3055 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 3056 | return result; |
| 3057 | } |
| 3058 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3059 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3060 | synchronized(mScoClients) { |
| 3061 | checkScoAudioState(); |
| 3062 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 3063 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3064 | if (mBluetoothHeadsetDevice != null) { |
| 3065 | if (mBluetoothHeadset != null) { |
| 3066 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 3067 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3068 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3069 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 3070 | } |
| 3071 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 3072 | getBluetoothHeadset()) { |
| 3073 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 3074 | } |
| 3075 | } |
| 3076 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3077 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | private void resetBluetoothSco() { |
| 3083 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3084 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3085 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3086 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3087 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3088 | AudioSystem.setParameters("A2dpSuspended=false"); |
| 3089 | setBluetoothScoOnInt(false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3093 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 3094 | SENDMSG_QUEUE, state, 0, null, 0); |
| 3095 | } |
| 3096 | |
| 3097 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3098 | if (state != mScoConnectionState) { |
| 3099 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 3100 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 3101 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 3102 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3103 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3104 | mScoConnectionState = state; |
| 3105 | } |
| 3106 | } |
| 3107 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3108 | void setBtScoDeviceConnectionState(BluetoothDevice btDevice, int state) { |
| 3109 | if (btDevice == null) { |
| 3110 | return; |
| 3111 | } |
| 3112 | |
| 3113 | String address = btDevice.getAddress(); |
| 3114 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 3115 | int outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 3116 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 3117 | if (btClass != null) { |
| 3118 | switch (btClass.getDeviceClass()) { |
| 3119 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 3120 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 3121 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 3122 | break; |
| 3123 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 3124 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 3125 | break; |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 3130 | address = ""; |
| 3131 | } |
| 3132 | |
| 3133 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
| 3134 | |
| 3135 | String btDeviceName = btDevice.getName(); |
| 3136 | boolean success = |
| 3137 | handleDeviceConnection(connected, outDevice, address, btDeviceName) && |
| 3138 | handleDeviceConnection(connected, inDevice, address, btDeviceName); |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3139 | |
| 3140 | if (!success) { |
| 3141 | return; |
| 3142 | } |
| 3143 | |
| 3144 | /* When one BT headset is disconnected while another BT headset |
| 3145 | * is connected, don't mess with the headset device. |
| 3146 | */ |
| 3147 | if ((state == BluetoothProfile.STATE_DISCONNECTED || |
| 3148 | state == BluetoothProfile.STATE_DISCONNECTING) && |
| 3149 | mBluetoothHeadset != null && |
| 3150 | mBluetoothHeadset.getAudioState(btDevice) == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3151 | Log.w(TAG, "SCO connected through another device, returning"); |
| 3152 | return; |
| 3153 | } |
| 3154 | |
| 3155 | synchronized (mScoClients) { |
| 3156 | if (connected) { |
| 3157 | mBluetoothHeadsetDevice = btDevice; |
| 3158 | } else { |
| 3159 | mBluetoothHeadsetDevice = null; |
| 3160 | resetBluetoothSco(); |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3165 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 3166 | new BluetoothProfile.ServiceListener() { |
| 3167 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3168 | BluetoothDevice btDevice; |
| 3169 | List<BluetoothDevice> deviceList; |
| 3170 | switch(profile) { |
| 3171 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3172 | synchronized (mConnectedDevices) { |
| 3173 | synchronized (mA2dpAvrcpLock) { |
| 3174 | mA2dp = (BluetoothA2dp) proxy; |
| 3175 | deviceList = mA2dp.getConnectedDevices(); |
| 3176 | if (deviceList.size() > 0) { |
| 3177 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3178 | int state = mA2dp.getConnectionState(btDevice); |
| 3179 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3180 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3181 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3182 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3183 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3184 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3185 | 0 /* arg2 unused */, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3186 | btDevice, |
| 3187 | delay); |
| 3188 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3189 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3190 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3191 | break; |
| 3192 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3193 | case BluetoothProfile.A2DP_SINK: |
| 3194 | deviceList = proxy.getConnectedDevices(); |
| 3195 | if (deviceList.size() > 0) { |
| 3196 | btDevice = deviceList.get(0); |
| 3197 | synchronized (mConnectedDevices) { |
| 3198 | int state = proxy.getConnectionState(btDevice); |
| 3199 | queueMsgUnderWakeLock(mAudioHandler, |
| 3200 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 3201 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3202 | 0 /* arg2 unused */, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3203 | btDevice, |
| 3204 | 0 /* delay */); |
| 3205 | } |
| 3206 | } |
| 3207 | break; |
| 3208 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3209 | case BluetoothProfile.HEADSET: |
| 3210 | synchronized (mScoClients) { |
| 3211 | // Discard timeout message |
| 3212 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 3213 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 3214 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 3215 | if (deviceList.size() > 0) { |
| 3216 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 3217 | } else { |
| 3218 | mBluetoothHeadsetDevice = null; |
| 3219 | } |
| 3220 | // Refresh SCO audio state |
| 3221 | checkScoAudioState(); |
| 3222 | // Continue pending action if any |
| 3223 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 3224 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 3225 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3226 | boolean status = false; |
| 3227 | if (mBluetoothHeadsetDevice != null) { |
| 3228 | switch (mScoAudioState) { |
| 3229 | case SCO_STATE_ACTIVATE_REQ: |
| 3230 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3231 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3232 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3233 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3234 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3235 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3236 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3237 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3238 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3239 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3240 | break; |
| 3241 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3242 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3243 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3244 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3245 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3246 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3247 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3248 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3249 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3250 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3251 | break; |
| 3252 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 3253 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3254 | mBluetoothHeadsetDevice); |
| 3255 | } |
| 3256 | } |
| 3257 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3258 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3259 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3260 | } |
| 3261 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3262 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3263 | break; |
| 3264 | |
| 3265 | default: |
| 3266 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3267 | } |
| 3268 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3269 | public void onServiceDisconnected(int profile) { |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3270 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3271 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3272 | case BluetoothProfile.A2DP: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3273 | disconnectA2dp(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3274 | break; |
| 3275 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3276 | case BluetoothProfile.A2DP_SINK: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3277 | disconnectA2dpSink(); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3278 | break; |
| 3279 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3280 | case BluetoothProfile.HEADSET: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3281 | disconnectHeadset(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3282 | break; |
| 3283 | |
| 3284 | default: |
| 3285 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3289 | |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3290 | void disconnectAllBluetoothProfiles() { |
| 3291 | disconnectA2dp(); |
| 3292 | disconnectA2dpSink(); |
| 3293 | disconnectHeadset(); |
| 3294 | } |
| 3295 | |
| 3296 | void disconnectA2dp() { |
| 3297 | synchronized (mConnectedDevices) { |
| 3298 | synchronized (mA2dpAvrcpLock) { |
| 3299 | ArraySet<String> toRemove = null; |
| 3300 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
| 3301 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3302 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3303 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
| 3304 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3305 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3306 | } |
| 3307 | } |
| 3308 | if (toRemove != null) { |
| 3309 | int delay = checkSendBecomingNoisyIntent( |
| 3310 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3311 | 0); |
| 3312 | for (int i = 0; i < toRemove.size(); i++) { |
| 3313 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
| 3314 | } |
| 3315 | } |
| 3316 | } |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | void disconnectA2dpSink() { |
| 3321 | synchronized (mConnectedDevices) { |
| 3322 | ArraySet<String> toRemove = null; |
| 3323 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
| 3324 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3325 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3326 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
| 3327 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3328 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3329 | } |
| 3330 | } |
| 3331 | if (toRemove != null) { |
| 3332 | for (int i = 0; i < toRemove.size(); i++) { |
| 3333 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
| 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | void disconnectHeadset() { |
| 3340 | synchronized (mScoClients) { |
| 3341 | if (mBluetoothHeadsetDevice != null) { |
| 3342 | setBtScoDeviceConnectionState(mBluetoothHeadsetDevice, |
| 3343 | BluetoothProfile.STATE_DISCONNECTED); |
| 3344 | } |
| 3345 | mBluetoothHeadset = null; |
| 3346 | } |
| 3347 | } |
| 3348 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3349 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3350 | synchronized (mSafeMediaVolumeState) { |
| 3351 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3352 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3353 | |
| 3354 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3355 | sendMsg(mAudioHandler, |
| 3356 | MSG_CHECK_MUSIC_ACTIVE, |
| 3357 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3358 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3359 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3360 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3361 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3362 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3363 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 3364 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3365 | // Approximate cumulative active music time |
| 3366 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3367 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3368 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3369 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3370 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3371 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3378 | private void saveMusicActiveMs() { |
| 3379 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3380 | } |
| 3381 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3382 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3383 | synchronized (mSafeMediaVolumeState) { |
| 3384 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3385 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3386 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3387 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3388 | boolean safeMediaVolumeEnabled = |
| 3389 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3390 | || mContext.getResources().getBoolean( |
| 3391 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3392 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3393 | boolean safeMediaVolumeBypass = |
| 3394 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3395 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3396 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3397 | // next time we boot and cannot be "inactive" |
| 3398 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3399 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3400 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3401 | // The state can already be "inactive" here if the user has forced it before |
| 3402 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3403 | // it to "active". |
| 3404 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3405 | if (mMusicActiveMs == 0) { |
| 3406 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3407 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3408 | } else { |
| 3409 | // We have existing playback time recorded, already confirmed. |
| 3410 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3411 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3412 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3413 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3414 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3415 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3416 | } |
| 3417 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3418 | sendMsg(mAudioHandler, |
| 3419 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3420 | SENDMSG_QUEUE, |
| 3421 | persistedState, |
| 3422 | 0, |
| 3423 | null, |
| 3424 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3425 | } |
| 3426 | } |
| 3427 | } |
| 3428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3429 | /////////////////////////////////////////////////////////////////////////// |
| 3430 | // Internal methods |
| 3431 | /////////////////////////////////////////////////////////////////////////// |
| 3432 | |
| 3433 | /** |
| 3434 | * Checks if the adjustment should change ringer mode instead of just |
| 3435 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3436 | * indices on the stream states. |
| 3437 | */ |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3438 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted, |
| 3439 | String caller, int flags) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3440 | int result = FLAG_ADJUST_VOLUME; |
Hank Freund | 21003f6 | 2015-12-08 09:05:46 -0800 | [diff] [blame] | 3441 | if (isPlatformTelevision()) { |
| 3442 | return result; |
| 3443 | } |
| 3444 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3445 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3446 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3447 | switch (ringerMode) { |
| 3448 | case RINGER_MODE_NORMAL: |
| 3449 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3450 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3451 | // "step" is the delta in internal index units corresponding to a |
| 3452 | // change of 1 in UI index units. |
| 3453 | // Because of rounding when rescaling from one stream index range to its alias |
| 3454 | // index range, we cannot simply test oldIndex == step: |
| 3455 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3456 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3457 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3458 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3459 | } |
| 3460 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3461 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3462 | ringerMode = RINGER_MODE_SILENT; |
| 3463 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3464 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3465 | } else if (mIsSingleVolume && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3466 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3467 | if (mHasVibrator) { |
| 3468 | ringerMode = RINGER_MODE_VIBRATE; |
| 3469 | } else { |
| 3470 | ringerMode = RINGER_MODE_SILENT; |
| 3471 | } |
| 3472 | // Setting the ringer mode will toggle mute |
| 3473 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3474 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3475 | break; |
| 3476 | case RINGER_MODE_VIBRATE: |
| 3477 | if (!mHasVibrator) { |
| 3478 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3479 | "but no vibrator is present"); |
| 3480 | break; |
| 3481 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3482 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3483 | // This is the case we were muted with the volume turned up |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3484 | if (mIsSingleVolume && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3485 | ringerMode = RINGER_MODE_NORMAL; |
| 3486 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3487 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3488 | final long diff = SystemClock.uptimeMillis() |
| 3489 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3490 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 3491 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3492 | ringerMode = RINGER_MODE_SILENT; |
| 3493 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3494 | } else { |
| 3495 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3496 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3497 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3498 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3499 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3500 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3501 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3502 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3503 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3504 | break; |
| 3505 | case RINGER_MODE_SILENT: |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3506 | if (mIsSingleVolume && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3507 | // This is the case we were muted with the volume turned up |
| 3508 | ringerMode = RINGER_MODE_NORMAL; |
| 3509 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3510 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3511 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3512 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3513 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3514 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3515 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3516 | ringerMode = RINGER_MODE_VIBRATE; |
| 3517 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3518 | // If we don't have a vibrator or they were toggling mute |
| 3519 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3520 | ringerMode = RINGER_MODE_NORMAL; |
| 3521 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3522 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3523 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3524 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3525 | break; |
| 3526 | default: |
| 3527 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3528 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3531 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 3532 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller) |
| 3533 | && (flags & AudioManager.FLAG_FROM_KEY) == 0) { |
| 3534 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 3535 | } |
| 3536 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3537 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3538 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3539 | mPrevVolDirection = direction; |
| 3540 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3541 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | } |
| 3543 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3544 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3545 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3546 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3547 | } |
| 3548 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3549 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3550 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3551 | } |
| 3552 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3553 | private boolean updateRingerModeAffectedStreams() { |
| 3554 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3555 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3556 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3557 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3558 | UserHandle.USER_CURRENT); |
| 3559 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3560 | if (mIsSingleVolume) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3561 | ringerModeAffectedStreams = 0; |
| 3562 | } else if (mRingerModeDelegate != null) { |
| 3563 | ringerModeAffectedStreams = mRingerModeDelegate |
| 3564 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3565 | } |
| 3566 | synchronized (mCameraSoundForced) { |
| 3567 | if (mCameraSoundForced) { |
| 3568 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3569 | } else { |
| 3570 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3571 | } |
| 3572 | } |
| 3573 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3574 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3575 | } else { |
| 3576 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3577 | } |
| 3578 | |
| 3579 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3580 | Settings.System.putIntForUser(mContentResolver, |
| 3581 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3582 | ringerModeAffectedStreams, |
| 3583 | UserHandle.USER_CURRENT); |
| 3584 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3585 | return true; |
| 3586 | } |
| 3587 | return false; |
| 3588 | } |
| 3589 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3590 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3591 | public boolean isStreamAffectedByMute(int streamType) { |
| 3592 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3593 | } |
| 3594 | |
| 3595 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3596 | switch (direction) { |
| 3597 | case AudioManager.ADJUST_LOWER: |
| 3598 | case AudioManager.ADJUST_RAISE: |
| 3599 | case AudioManager.ADJUST_SAME: |
| 3600 | case AudioManager.ADJUST_MUTE: |
| 3601 | case AudioManager.ADJUST_UNMUTE: |
| 3602 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3603 | break; |
| 3604 | default: |
| 3605 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | private void ensureValidStreamType(int streamType) { |
| 3610 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3611 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3612 | } |
| 3613 | } |
| 3614 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3615 | private boolean isMuteAdjust(int adjust) { |
| 3616 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3617 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3618 | } |
| 3619 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3620 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3621 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3622 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3623 | TelecomManager telecomManager = |
| 3624 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3625 | |
| 3626 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3627 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3628 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3629 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3630 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3631 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3632 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3633 | /** |
| 3634 | * For code clarity for getActiveStreamType(int) |
| 3635 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3636 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3637 | * in the last "delay_ms" ms. |
| 3638 | */ |
| 3639 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3640 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3641 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3642 | } |
| 3643 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3644 | private int getActiveStreamType(int suggestedStreamType) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3645 | if (mIsSingleVolume |
| 3646 | && suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 3647 | return AudioSystem.STREAM_MUSIC; |
| 3648 | } |
| 3649 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3650 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3651 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3652 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3653 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3654 | == AudioSystem.FORCE_BT_SCO) { |
| 3655 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3656 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3657 | } else { |
| 3658 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3659 | return AudioSystem.STREAM_VOICE_CALL; |
| 3660 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3661 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 3662 | if (isAfMusicActiveRecently(sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3663 | if (DEBUG_VOL) |
| 3664 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3665 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3666 | } else { |
| 3667 | if (DEBUG_VOL) |
| 3668 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3669 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3670 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3671 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3672 | if (DEBUG_VOL) |
| 3673 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3674 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3675 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3676 | break; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3677 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3678 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3679 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3680 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3681 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3682 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3683 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3684 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3685 | return AudioSystem.STREAM_VOICE_CALL; |
| 3686 | } |
| 3687 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 3688 | sStreamOverrideDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3689 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 3690 | sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3691 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3692 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3693 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 3694 | if (isAfMusicActiveRecently(sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3695 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3696 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3697 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3698 | if (DEBUG_VOL) Log.v(TAG, |
| 3699 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3700 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3701 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3702 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3703 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3704 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3705 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3706 | + suggestedStreamType); |
| 3707 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3708 | } |
| 3709 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3710 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3711 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3712 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3713 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3714 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3715 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3716 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | private void broadcastVibrateSetting(int vibrateType) { |
| 3720 | // Send broadcast |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 3721 | if (mActivityManagerInternal.isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3722 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3723 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3724 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3725 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3730 | /** |
| 3731 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3732 | * Note that the wake lock needs to be released after the message has been handled. |
| 3733 | */ |
| 3734 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3735 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3736 | final long ident = Binder.clearCallingIdentity(); |
| 3737 | // Always acquire the wake lock as AudioService because it is released by the |
| 3738 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3739 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3740 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3741 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3743 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3744 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | 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] | 3746 | |
| 3747 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3748 | handler.removeMessages(msg); |
| 3749 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3750 | return; |
| 3751 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3752 | synchronized (mLastDeviceConnectMsgTime) { |
| 3753 | long time = SystemClock.uptimeMillis() + delay; |
| 3754 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3755 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3756 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3757 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3758 | mLastDeviceConnectMsgTime = time; |
| 3759 | } |
| 3760 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3764 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3765 | == PackageManager.PERMISSION_GRANTED) { |
| 3766 | return true; |
| 3767 | } |
| 3768 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3769 | + Binder.getCallingPid() |
| 3770 | + ", uid=" + Binder.getCallingUid(); |
| 3771 | Log.w(TAG, msg); |
| 3772 | return false; |
| 3773 | } |
| 3774 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3775 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3776 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3777 | if ((device & (device - 1)) != 0) { |
| 3778 | // Multiple device selection is either: |
| 3779 | // - speaker + one other device: give priority to speaker in this case. |
| 3780 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3781 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3782 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3783 | // - 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] | 3784 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3785 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3786 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3787 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3788 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3789 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3790 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3791 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3792 | } else { |
| 3793 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3794 | } |
| 3795 | } |
| 3796 | return device; |
| 3797 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3799 | private int getDevicesForStream(int stream) { |
| 3800 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 3801 | } |
| 3802 | |
| 3803 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 3804 | ensureValidStreamType(stream); |
| 3805 | synchronized (VolumeStreamState.class) { |
| 3806 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | private void observeDevicesForStreams(int skipStream) { |
| 3811 | synchronized (VolumeStreamState.class) { |
| 3812 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 3813 | if (stream != skipStream) { |
| 3814 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3820 | /* |
| 3821 | * A class just for packaging up a set of connection parameters. |
| 3822 | */ |
| 3823 | private class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3824 | public final int mType; |
| 3825 | public final int mState; |
| 3826 | public final String mAddress; |
| 3827 | public final String mName; |
| 3828 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3829 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3830 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 3831 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3832 | mType = type; |
| 3833 | mState = state; |
| 3834 | mAddress = address; |
| 3835 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3836 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3837 | } |
| 3838 | } |
| 3839 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3840 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 3841 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3842 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3843 | if (DEBUG_DEVICES) { |
| 3844 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 3845 | + address + ")"); |
| 3846 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3847 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3848 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3849 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3850 | 0 /* arg1 unused */, |
| 3851 | 0 /* arg2 unused */, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3852 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3853 | delay); |
| 3854 | } |
| 3855 | } |
| 3856 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3857 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3858 | { |
| 3859 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3860 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3861 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3862 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3863 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3864 | if (profile == BluetoothProfile.A2DP) { |
| 3865 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3866 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3867 | } else { |
| 3868 | delay = 0; |
| 3869 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3870 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3871 | (profile == BluetoothProfile.A2DP ? |
| 3872 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3873 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3874 | 0 /* arg2 unused */, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3875 | device, |
| 3876 | delay); |
| 3877 | } |
| 3878 | return delay; |
| 3879 | } |
| 3880 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3881 | public void handleBluetoothA2dpDeviceConfigChange(BluetoothDevice device) |
| 3882 | { |
| 3883 | synchronized (mConnectedDevices) { |
| 3884 | queueMsgUnderWakeLock(mAudioHandler, |
| 3885 | MSG_A2DP_DEVICE_CONFIG_CHANGE, |
| 3886 | 0 /* arg1 unused */, |
| 3887 | 0 /* arg1 unused */, |
| 3888 | device, |
| 3889 | 0 /* delay */); |
| 3890 | } |
| 3891 | } |
| 3892 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3893 | /////////////////////////////////////////////////////////////////////////// |
| 3894 | // Inner classes |
| 3895 | /////////////////////////////////////////////////////////////////////////// |
| 3896 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3897 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3898 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3899 | // 2 mSetModeDeathHandlers |
| 3900 | // 3 mSettingsLock |
| 3901 | // 4 VolumeStreamState.class |
| 3902 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3903 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3904 | private final int mStreamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3905 | private final int mIndexMin; |
| 3906 | private final int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3907 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3908 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3909 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3910 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3911 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3912 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3913 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3914 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3915 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3916 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3917 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3918 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3919 | |
| 3920 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3921 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 3922 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 3923 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3924 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3925 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3926 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3927 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3928 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 3929 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3930 | } |
| 3931 | |
| 3932 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 3933 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 3934 | if (devices == mObservedDevices) { |
| 3935 | return devices; |
| 3936 | } |
| 3937 | final int prevDevices = mObservedDevices; |
| 3938 | mObservedDevices = devices; |
| 3939 | if (checkOthers) { |
| 3940 | // one stream's devices have changed, check the others |
| 3941 | observeDevicesForStreams(mStreamType); |
| 3942 | } |
| 3943 | // log base stream changes to the event log |
| 3944 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 3945 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 3946 | } |
| 3947 | sendBroadcastToAll(mStreamDevicesChanged |
| 3948 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 3949 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 3950 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3951 | } |
| 3952 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3953 | public String getSettingNameForDevice(int device) { |
| 3954 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3955 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3956 | if (suffix.isEmpty()) { |
| 3957 | return name; |
| 3958 | } |
| 3959 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3962 | public void readSettings() { |
| 3963 | synchronized (VolumeStreamState.class) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3964 | // force maximum volume on all streams if fixed volume property is set |
| 3965 | if (mUseFixedVolume) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3966 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3967 | return; |
| 3968 | } |
| 3969 | // do not read system stream volume from settings: this stream is always aliased |
| 3970 | // to another stream type and its volume is never persisted. Values in settings can |
| 3971 | // only be stale values |
| 3972 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3973 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3974 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3975 | synchronized (mCameraSoundForced) { |
| 3976 | if (mCameraSoundForced) { |
| 3977 | index = mIndexMax; |
| 3978 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3979 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3980 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3981 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3982 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3983 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3984 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3985 | |
| 3986 | for (int i = 0; remainingDevices != 0; i++) { |
| 3987 | int device = (1 << i); |
| 3988 | if ((device & remainingDevices) == 0) { |
| 3989 | continue; |
| 3990 | } |
| 3991 | remainingDevices &= ~device; |
| 3992 | |
| 3993 | // retrieve current volume for device |
| 3994 | String name = getSettingNameForDevice(device); |
| 3995 | // if no volume stored for current stream and device, use default volume if default |
| 3996 | // device, continue otherwise |
| 3997 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3998 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3999 | int index = Settings.System.getIntForUser( |
| 4000 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 4001 | if (index == -1) { |
| 4002 | continue; |
| 4003 | } |
| 4004 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4005 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4006 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4007 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4008 | } |
| 4009 | |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4010 | private int getAbsoluteVolumeIndex(int index) { |
| 4011 | /* Special handling for Bluetooth Absolute Volume scenario |
| 4012 | * If we send full audio gain, some accessories are too loud even at its lowest |
| 4013 | * volume. We are not able to enumerate all such accessories, so here is the |
| 4014 | * workaround from phone side. |
| 4015 | * Pre-scale volume at lowest volume steps 1 2 and 3. |
| 4016 | * For volume step 0, set audio gain to 0 as some accessories won't mute on their end. |
| 4017 | */ |
| 4018 | if (index == 0) { |
| 4019 | // 0% for volume 0 |
| 4020 | index = 0; |
| 4021 | } else if (index == 1) { |
| 4022 | // 50% for volume 1 |
| 4023 | index = (int)(mIndexMax * 0.5) /10; |
| 4024 | } else if (index == 2) { |
| 4025 | // 70% for volume 2 |
| 4026 | index = (int)(mIndexMax * 0.70) /10; |
| 4027 | } else if (index == 3) { |
| 4028 | // 85% for volume 3 |
| 4029 | index = (int)(mIndexMax * 0.85) /10; |
| 4030 | } else { |
| 4031 | // otherwise, full gain |
| 4032 | index = (mIndexMax + 5)/10; |
| 4033 | } |
| 4034 | return index; |
| 4035 | } |
| 4036 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4037 | // must be called while synchronized VolumeStreamState.class |
| 4038 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4039 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4040 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4041 | index = 0; |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4042 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4043 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4044 | } else if ((device & mFullVolumeDevices) != 0) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4045 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4046 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4047 | index = (getIndex(device) + 5)/10; |
| 4048 | } |
| 4049 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4050 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4051 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4052 | public void applyAllVolumes() { |
| 4053 | synchronized (VolumeStreamState.class) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4054 | // apply device specific volumes first |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4055 | int index; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4056 | for (int i = 0; i < mIndexMap.size(); i++) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4057 | final int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4058 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4059 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4060 | index = 0; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4061 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 4062 | mAvrcpAbsVolSupported) { |
| 4063 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
| 4064 | } else if ((device & mFullVolumeDevices) != 0) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4065 | index = (mIndexMax + 5)/10; |
| 4066 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4067 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4068 | } |
| 4069 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4070 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4071 | } |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4072 | // apply default volume last: by convention , default device volume will be used |
| 4073 | // by audio policy manager if no explicit volume is present for a given device type |
| 4074 | if (mIsMuted) { |
| 4075 | index = 0; |
| 4076 | } else { |
| 4077 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 4078 | } |
| 4079 | AudioSystem.setStreamVolumeIndex( |
| 4080 | mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4081 | } |
| 4082 | } |
| 4083 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4084 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 4085 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4088 | public boolean setIndex(int index, int device, String caller) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4089 | boolean changed = false; |
| 4090 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4091 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4092 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4093 | index = getValidIndex(index); |
| 4094 | synchronized (mCameraSoundForced) { |
| 4095 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 4096 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4097 | } |
| 4098 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4099 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4100 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4101 | changed = oldIndex != index; |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 4102 | // Apply change to all streams using this one as alias if: |
| 4103 | // - the index actually changed OR |
| 4104 | // - there is no volume index stored for this device on alias stream. |
| 4105 | // If changing volume of current device, also change volume of current |
| 4106 | // device on aliased stream |
| 4107 | final boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 4108 | final int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4109 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4110 | final VolumeStreamState aliasStreamState = mStreamStates[streamType]; |
| 4111 | if (streamType != mStreamType && |
| 4112 | mStreamVolumeAlias[streamType] == mStreamType && |
| 4113 | (changed || !aliasStreamState.hasIndexForDevice(device))) { |
| 4114 | final int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 4115 | aliasStreamState.setIndex(scaledIndex, device, caller); |
| 4116 | if (currentDevice) { |
| 4117 | aliasStreamState.setIndex(scaledIndex, |
| 4118 | getDeviceForStream(streamType), caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4119 | } |
| 4120 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4122 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4123 | if (changed) { |
| 4124 | oldIndex = (oldIndex + 5) / 10; |
| 4125 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4126 | // log base stream changes to the event log |
| 4127 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4128 | if (caller == null) { |
| 4129 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 4130 | } |
| 4131 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 4132 | caller); |
| 4133 | } |
| 4134 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4135 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 4136 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 4137 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 4138 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4139 | sendBroadcastToAll(mVolumeChanged); |
| 4140 | } |
| 4141 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4142 | } |
| 4143 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4144 | public int getIndex(int device) { |
| 4145 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4146 | int index = mIndexMap.get(device, -1); |
| 4147 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4148 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4149 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4150 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4151 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4152 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 4155 | public boolean hasIndexForDevice(int device) { |
| 4156 | synchronized (VolumeStreamState.class) { |
| 4157 | return (mIndexMap.get(device, -1) != -1); |
| 4158 | } |
| 4159 | } |
| 4160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4162 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4163 | } |
| 4164 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4165 | public int getMinIndex() { |
| 4166 | return mIndexMin; |
| 4167 | } |
| 4168 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4169 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4170 | synchronized (VolumeStreamState.class) { |
| 4171 | int srcStreamType = srcStream.getStreamType(); |
| 4172 | // apply default device volume from source stream to all devices first in case |
| 4173 | // some devices are present in this stream state but not in source stream state |
| 4174 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4175 | index = rescaleIndex(index, srcStreamType, mStreamType); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4176 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4177 | mIndexMap.put(mIndexMap.keyAt(i), index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4178 | } |
| 4179 | // Now apply actual volume for devices in source stream state |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4180 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 4181 | for (int i = 0; i < srcMap.size(); i++) { |
| 4182 | int device = srcMap.keyAt(i); |
| 4183 | index = srcMap.valueAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4184 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4185 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4186 | setIndex(index, device, caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4187 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4188 | } |
| 4189 | } |
| 4190 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4191 | public void setAllIndexesToMax() { |
| 4192 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4193 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4194 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4195 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4196 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4197 | } |
| 4198 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4199 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4200 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4201 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4202 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4203 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4204 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4205 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4206 | // Set the new mute volume. This propagates the values to |
| 4207 | // the audio system, otherwise the volume won't be changed |
| 4208 | // at the lower level. |
| 4209 | sendMsg(mAudioHandler, |
| 4210 | MSG_SET_ALL_VOLUMES, |
| 4211 | SENDMSG_QUEUE, |
| 4212 | 0, |
| 4213 | 0, |
| 4214 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4216 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4217 | if (changed) { |
| 4218 | // Stream mute changed, fire the intent. |
| 4219 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 4220 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4221 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 4222 | sendBroadcastToAll(intent); |
| 4223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4224 | } |
| 4225 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4226 | public int getStreamType() { |
| 4227 | return mStreamType; |
| 4228 | } |
| 4229 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4230 | public void checkFixedVolumeDevices() { |
| 4231 | synchronized (VolumeStreamState.class) { |
| 4232 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 4233 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4234 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4235 | int device = mIndexMap.keyAt(i); |
| 4236 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 4237 | if (((device & mFullVolumeDevices) != 0) |
| 4238 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4239 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4240 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4241 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4242 | } |
| 4243 | } |
| 4244 | } |
| 4245 | } |
| 4246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4247 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4248 | if (index < mIndexMin) { |
| 4249 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 4250 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4251 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | return index; |
| 4255 | } |
| 4256 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4257 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4258 | pw.print(" Muted: "); |
| 4259 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4260 | pw.print(" Min: "); |
| 4261 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4262 | pw.print(" Max: "); |
| 4263 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4264 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4265 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4266 | if (i > 0) { |
| 4267 | pw.print(", "); |
| 4268 | } |
| 4269 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4270 | pw.print(Integer.toHexString(device)); |
| 4271 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 4272 | : AudioSystem.getOutputDeviceName(device); |
| 4273 | if (!deviceName.isEmpty()) { |
| 4274 | pw.print(" ("); |
| 4275 | pw.print(deviceName); |
| 4276 | pw.print(")"); |
| 4277 | } |
| 4278 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4279 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4280 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4281 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4282 | pw.println(); |
| 4283 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4284 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4285 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 4286 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 4287 | // (the default device is not returned by getDevicesForStream) |
| 4288 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4289 | if ((devices & device) != 0) { |
| 4290 | if (n++ > 0) { |
| 4291 | pw.print(", "); |
| 4292 | } |
| 4293 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 4294 | } |
| 4295 | i++; |
| 4296 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4297 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | /** Thread that handles native AudioSystem control. */ |
| 4301 | private class AudioSystemThread extends Thread { |
| 4302 | AudioSystemThread() { |
| 4303 | super("AudioService"); |
| 4304 | } |
| 4305 | |
| 4306 | @Override |
| 4307 | public void run() { |
| 4308 | // Set this thread up so the handler will work on it |
| 4309 | Looper.prepare(); |
| 4310 | |
| 4311 | synchronized(AudioService.this) { |
| 4312 | mAudioHandler = new AudioHandler(); |
| 4313 | |
| 4314 | // Notify that the handler has been created |
| 4315 | AudioService.this.notify(); |
| 4316 | } |
| 4317 | |
| 4318 | // Listen for volume change requests that are set by VolumePanel |
| 4319 | Looper.loop(); |
| 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | /** Handles internal volume messages in separate volume thread. */ |
| 4324 | private class AudioHandler extends Handler { |
| 4325 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4326 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4327 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4328 | synchronized (VolumeStreamState.class) { |
| 4329 | // Apply volume |
| 4330 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4331 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4332 | // Apply change to all streams using this one as alias |
| 4333 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4334 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4335 | if (streamType != streamState.mStreamType && |
| 4336 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 4337 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 4338 | // that may have a different device selected |
| 4339 | int streamDevice = getDeviceForStream(streamType); |
| 4340 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 4341 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 4342 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 4343 | } |
| 4344 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4345 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4346 | } |
| 4347 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4348 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4349 | sendMsg(mAudioHandler, |
| 4350 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 4351 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4352 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4353 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4354 | streamState, |
| 4355 | PERSIST_DELAY); |
| 4356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | } |
| 4358 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4359 | private void setAllVolumes(VolumeStreamState streamState) { |
| 4360 | |
| 4361 | // Apply volume |
| 4362 | streamState.applyAllVolumes(); |
| 4363 | |
| 4364 | // Apply change to all streams using this one as alias |
| 4365 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4366 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4367 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4368 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4369 | mStreamStates[streamType].applyAllVolumes(); |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4374 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4375 | if (mUseFixedVolume) { |
| 4376 | return; |
| 4377 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4378 | if (mIsSingleVolume && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4379 | return; |
| 4380 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4381 | System.putIntForUser(mContentResolver, |
| 4382 | streamState.getSettingNameForDevice(device), |
| 4383 | (streamState.getIndex(device) + 5)/ 10, |
| 4384 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4385 | } |
| 4386 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4387 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4388 | if (mUseFixedVolume) { |
| 4389 | return; |
| 4390 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 4391 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4392 | } |
| 4393 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4394 | private boolean onLoadSoundEffects() { |
| 4395 | int status; |
| 4396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4397 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4398 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4399 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 4400 | return false; |
| 4401 | } |
| 4402 | |
| 4403 | if (mSoundPool != null) { |
| 4404 | return true; |
| 4405 | } |
| 4406 | |
| 4407 | loadTouchSoundAssets(); |
| 4408 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 4409 | mSoundPool = new SoundPool.Builder() |
| 4410 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 4411 | .setAudioAttributes(new AudioAttributes.Builder() |
| 4412 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 4413 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 4414 | .build()) |
| 4415 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4416 | mSoundPoolCallBack = null; |
| 4417 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 4418 | mSoundPoolListenerThread.start(); |
| 4419 | int attempts = 3; |
| 4420 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 4421 | try { |
| 4422 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4423 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4424 | } catch (InterruptedException e) { |
| 4425 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | if (mSoundPoolCallBack == null) { |
| 4430 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 4431 | if (mSoundPoolLooper != null) { |
| 4432 | mSoundPoolLooper.quit(); |
| 4433 | mSoundPoolLooper = null; |
| 4434 | } |
| 4435 | mSoundPoolListenerThread = null; |
| 4436 | mSoundPool.release(); |
| 4437 | mSoundPool = null; |
| 4438 | return false; |
| 4439 | } |
| 4440 | /* |
| 4441 | * poolId table: The value -1 in this table indicates that corresponding |
| 4442 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 4443 | * Once loaded, the value in poolId is the sample ID and the same |
| 4444 | * sample can be reused for another effect using the same file. |
| 4445 | */ |
| 4446 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4447 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4448 | poolId[fileIdx] = -1; |
| 4449 | } |
| 4450 | /* |
| 4451 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 4452 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 4453 | * this indicates we have a valid sample loaded for this effect. |
| 4454 | */ |
| 4455 | |
| 4456 | int numSamples = 0; |
| 4457 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4458 | // Do not load sample if this effect uses the MediaPlayer |
| 4459 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 4460 | continue; |
| 4461 | } |
| 4462 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 4463 | String filePath = Environment.getRootDirectory() |
| 4464 | + SOUND_EFFECTS_PATH |
| 4465 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 4466 | int sampleId = mSoundPool.load(filePath, 0); |
| 4467 | if (sampleId <= 0) { |
| 4468 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 4469 | } else { |
| 4470 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 4471 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 4472 | numSamples++; |
| 4473 | } |
| 4474 | } else { |
| 4475 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 4476 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 4477 | } |
| 4478 | } |
| 4479 | // wait for all samples to be loaded |
| 4480 | if (numSamples > 0) { |
| 4481 | mSoundPoolCallBack.setSamples(poolId); |
| 4482 | |
| 4483 | attempts = 3; |
| 4484 | status = 1; |
| 4485 | while ((status == 1) && (attempts-- > 0)) { |
| 4486 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4487 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4488 | status = mSoundPoolCallBack.status(); |
| 4489 | } catch (InterruptedException e) { |
| 4490 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 4491 | } |
| 4492 | } |
| 4493 | } else { |
| 4494 | status = -1; |
| 4495 | } |
| 4496 | |
| 4497 | if (mSoundPoolLooper != null) { |
| 4498 | mSoundPoolLooper.quit(); |
| 4499 | mSoundPoolLooper = null; |
| 4500 | } |
| 4501 | mSoundPoolListenerThread = null; |
| 4502 | if (status != 0) { |
| 4503 | Log.w(TAG, |
| 4504 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4505 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4506 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4507 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | mSoundPool.release(); |
| 4512 | mSoundPool = null; |
| 4513 | } |
| 4514 | } |
| 4515 | return (status == 0); |
| 4516 | } |
| 4517 | |
| 4518 | /** |
| 4519 | * Unloads samples from the sound pool. |
| 4520 | * This method can be called to free some memory when |
| 4521 | * sound effects are disabled. |
| 4522 | */ |
| 4523 | private void onUnloadSoundEffects() { |
| 4524 | synchronized (mSoundEffectsLock) { |
| 4525 | if (mSoundPool == null) { |
| 4526 | return; |
| 4527 | } |
| 4528 | |
| 4529 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4530 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4531 | poolId[fileIdx] = 0; |
| 4532 | } |
| 4533 | |
| 4534 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4535 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4536 | continue; |
| 4537 | } |
| 4538 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4539 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4540 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4541 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4542 | } |
| 4543 | } |
| 4544 | mSoundPool.release(); |
| 4545 | mSoundPool = null; |
| 4546 | } |
| 4547 | } |
| 4548 | |
| 4549 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4550 | synchronized (mSoundEffectsLock) { |
| 4551 | |
| 4552 | onLoadSoundEffects(); |
| 4553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4554 | if (mSoundPool == null) { |
| 4555 | return; |
| 4556 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4557 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4558 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4559 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4560 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4561 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4562 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4563 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4564 | |
| 4565 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4566 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4567 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4568 | } else { |
| 4569 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4570 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 4571 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 4572 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4573 | mediaPlayer.setDataSource(filePath); |
| 4574 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4575 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4576 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4577 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4578 | public void onCompletion(MediaPlayer mp) { |
| 4579 | cleanupPlayer(mp); |
| 4580 | } |
| 4581 | }); |
| 4582 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4583 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4584 | cleanupPlayer(mp); |
| 4585 | return true; |
| 4586 | } |
| 4587 | }); |
| 4588 | mediaPlayer.start(); |
| 4589 | } catch (IOException ex) { |
| 4590 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4591 | } catch (IllegalArgumentException ex) { |
| 4592 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4593 | } catch (IllegalStateException ex) { |
| 4594 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4595 | } |
| 4596 | } |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | private void cleanupPlayer(MediaPlayer mp) { |
| 4601 | if (mp != null) { |
| 4602 | try { |
| 4603 | mp.stop(); |
| 4604 | mp.release(); |
| 4605 | } catch (IllegalStateException ex) { |
| 4606 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4607 | } |
| 4608 | } |
| 4609 | } |
| 4610 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4611 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4612 | synchronized (mConnectedDevices) { |
| 4613 | setForceUseInt_SyncDevices(usage, config); |
| 4614 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4615 | } |
| 4616 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4617 | private void onPersistSafeVolumeState(int state) { |
| 4618 | Settings.Global.putInt(mContentResolver, |
| 4619 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4620 | state); |
| 4621 | } |
| 4622 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4623 | @Override |
| 4624 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4625 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4626 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4627 | case MSG_SET_DEVICE_VOLUME: |
| 4628 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4629 | break; |
| 4630 | |
| 4631 | case MSG_SET_ALL_VOLUMES: |
| 4632 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4633 | break; |
| 4634 | |
| 4635 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4636 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4637 | break; |
| 4638 | |
| 4639 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4640 | // note that the value persisted is the current ringer mode, not the |
| 4641 | // 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] | 4642 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4643 | break; |
| 4644 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 4645 | case MSG_AUDIO_SERVER_DIED: |
| 4646 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4647 | break; |
| 4648 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4649 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4650 | onUnloadSoundEffects(); |
| 4651 | break; |
| 4652 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4653 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4654 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4655 | // can take several dozens of milliseconds to complete |
| 4656 | boolean loaded = onLoadSoundEffects(); |
| 4657 | if (msg.obj != null) { |
| 4658 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4659 | synchronized (reply) { |
| 4660 | reply.mStatus = loaded ? 0 : -1; |
| 4661 | reply.notify(); |
| 4662 | } |
| 4663 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4664 | break; |
| 4665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4666 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4667 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4668 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4669 | |
| 4670 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4671 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4672 | synchronized (mConnectedDevices) { |
| 4673 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4674 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4675 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4676 | |
| 4677 | case MSG_SET_FORCE_USE: |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 4678 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4679 | setForceUse(msg.arg1, msg.arg2); |
| 4680 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4681 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4682 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4683 | resetBluetoothSco(); |
| 4684 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4685 | |
| 4686 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4687 | { WiredDeviceConnectionState connectState = |
| 4688 | (WiredDeviceConnectionState)msg.obj; |
| 4689 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4690 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4691 | mAudioEventWakeLock.release(); |
| 4692 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4693 | break; |
| 4694 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4695 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4696 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4697 | mAudioEventWakeLock.release(); |
| 4698 | break; |
| 4699 | |
| 4700 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4701 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4702 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4703 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4704 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4705 | case MSG_A2DP_DEVICE_CONFIG_CHANGE: |
| 4706 | onBluetoothA2dpDeviceConfigChange((BluetoothDevice)msg.obj); |
| 4707 | mAudioEventWakeLock.release(); |
| 4708 | break; |
| 4709 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4710 | case MSG_REPORT_NEW_ROUTES: { |
| 4711 | int N = mRoutesObservers.beginBroadcast(); |
| 4712 | if (N > 0) { |
| 4713 | AudioRoutesInfo routes; |
| 4714 | synchronized (mCurAudioRoutes) { |
| 4715 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4716 | } |
| 4717 | while (N > 0) { |
| 4718 | N--; |
| 4719 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4720 | try { |
| 4721 | obs.dispatchAudioRoutesChanged(routes); |
| 4722 | } catch (RemoteException e) { |
| 4723 | } |
| 4724 | } |
| 4725 | } |
| 4726 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4727 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4728 | break; |
| 4729 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4730 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4731 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4732 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4733 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4734 | |
| 4735 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4736 | onSendBecomingNoisyIntent(); |
| 4737 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4738 | |
| 4739 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4740 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4741 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 4742 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4743 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4744 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4745 | onPersistSafeVolumeState(msg.arg1); |
| 4746 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4747 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4748 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4749 | onBroadcastScoConnectionState(msg.arg1); |
| 4750 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4751 | |
| 4752 | case MSG_SYSTEM_READY: |
| 4753 | onSystemReady(); |
| 4754 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4755 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 4756 | case MSG_INDICATE_SYSTEM_READY: |
| 4757 | onIndicateSystemReady(); |
| 4758 | break; |
| 4759 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4760 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4761 | final int musicActiveMs = msg.arg1; |
| 4762 | Settings.Secure.putIntForUser(mContentResolver, |
| 4763 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4764 | UserHandle.USER_CURRENT); |
| 4765 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 4766 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4767 | case MSG_UNMUTE_STREAM: |
| 4768 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4769 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 4770 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 4771 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 4772 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 4773 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4774 | } |
| 4775 | } |
| 4776 | } |
| 4777 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4778 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4779 | |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4780 | private int mEncodedSurroundMode; |
| 4781 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4782 | SettingsObserver() { |
| 4783 | super(new Handler()); |
| 4784 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4785 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4786 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4787 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 4788 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4789 | Settings.System.MASTER_MONO), false, this); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4790 | |
| 4791 | mEncodedSurroundMode = Settings.Global.getInt( |
| 4792 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 4793 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 4794 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4795 | Settings.Global.ENCODED_SURROUND_OUTPUT), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | @Override |
| 4799 | public void onChange(boolean selfChange) { |
| 4800 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4801 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4802 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4803 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4804 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4805 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4806 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4807 | /* |
| 4808 | * Ensure all stream types that should be affected by ringer mode |
| 4809 | * are in the proper state. |
| 4810 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4811 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4812 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4813 | readDockAudioSettings(mContentResolver); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 4814 | updateMasterMono(mContentResolver); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4815 | updateEncodedSurroundOutput(); |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | private void updateEncodedSurroundOutput() { |
| 4820 | int newSurroundMode = Settings.Global.getInt( |
| 4821 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 4822 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 4823 | // Did it change? |
| 4824 | if (mEncodedSurroundMode != newSurroundMode) { |
| 4825 | // Send to AudioPolicyManager |
| 4826 | sendEncodedSurroundMode(newSurroundMode); |
| 4827 | synchronized(mConnectedDevices) { |
| 4828 | // Is HDMI connected? |
| 4829 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HDMI, ""); |
| 4830 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4831 | if (deviceSpec != null) { |
| 4832 | // Toggle HDMI to retrigger broadcast with proper formats. |
| 4833 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 4834 | AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "", |
| 4835 | "android"); // disconnect |
| 4836 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 4837 | AudioSystem.DEVICE_STATE_AVAILABLE, "", "", |
| 4838 | "android"); // reconnect |
| 4839 | } |
| 4840 | } |
| 4841 | mEncodedSurroundMode = newSurroundMode; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4842 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4843 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4844 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4845 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4846 | // must be called synchronized on mConnectedDevices |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4847 | private void makeA2dpDeviceAvailable(String address, String name) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4848 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4849 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4850 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4851 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4852 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 4853 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4854 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4855 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4856 | // Reset A2DP suspend state each time a new sink is connected |
| 4857 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4858 | mConnectedDevices.put( |
| 4859 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4860 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4861 | address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4864 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4865 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4866 | } |
| 4867 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4868 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4869 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4870 | synchronized (mA2dpAvrcpLock) { |
| 4871 | mAvrcpAbsVolSupported = false; |
| 4872 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4873 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4874 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4875 | mConnectedDevices.remove( |
| 4876 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4877 | synchronized (mCurAudioRoutes) { |
| 4878 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4879 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4880 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4881 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4882 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4883 | } |
| 4884 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4887 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4888 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4889 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4890 | // reconnection of the sink. |
| 4891 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4892 | // 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] | 4893 | mConnectedDevices.remove( |
| 4894 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4895 | // send the delayed message to make the device unavailable later |
| 4896 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4897 | mAudioHandler.sendMessageDelayed(msg, delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4898 | |
| 4899 | } |
| 4900 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4901 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4902 | private void makeA2dpSrcAvailable(String address) { |
| 4903 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4904 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4905 | mConnectedDevices.put( |
| 4906 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4907 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4908 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | // must be called synchronized on mConnectedDevices |
| 4912 | private void makeA2dpSrcUnavailable(String address) { |
| 4913 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4914 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4915 | mConnectedDevices.remove( |
| 4916 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4920 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4921 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4922 | } |
| 4923 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4924 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4925 | private boolean hasScheduledA2dpDockTimeout() { |
| 4926 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4927 | } |
| 4928 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4929 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4930 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4931 | if (DEBUG_VOL) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4932 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice=" + btDevice+"state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4933 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4934 | if (btDevice == null) { |
| 4935 | return; |
| 4936 | } |
| 4937 | String address = btDevice.getAddress(); |
| 4938 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4939 | address = ""; |
| 4940 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4941 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4942 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4943 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4944 | btDevice.getAddress()); |
| 4945 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4946 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4947 | |
| 4948 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4949 | if (btDevice.isBluetoothDock()) { |
| 4950 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4951 | // introduction of a delay for transient disconnections of docks when |
| 4952 | // power is rapidly turned off/on, this message will be canceled if |
| 4953 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4954 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4955 | // the next time isConnected is evaluated, it will be false for the dock |
| 4956 | } |
| 4957 | } else { |
| 4958 | makeA2dpDeviceUnavailableNow(address); |
| 4959 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4960 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4961 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4962 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4963 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4964 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4965 | } |
| 4966 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4967 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4968 | if (btDevice.isBluetoothDock()) { |
| 4969 | // this could be a reconnection after a transient disconnection |
| 4970 | cancelA2dpDeviceTimeout(); |
| 4971 | mDockAddress = address; |
| 4972 | } else { |
| 4973 | // this could be a connection of another A2DP device before the timeout of |
| 4974 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4975 | if(hasScheduledA2dpDockTimeout()) { |
| 4976 | cancelA2dpDeviceTimeout(); |
| 4977 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4978 | } |
| 4979 | } |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4980 | makeA2dpDeviceAvailable(address, btDevice.getName()); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4981 | synchronized (mCurAudioRoutes) { |
| 4982 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4983 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4984 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4985 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4986 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4987 | } |
| 4988 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | } |
| 4992 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4993 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4994 | { |
| 4995 | if (DEBUG_VOL) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4996 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice=" + btDevice + " state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4997 | } |
| 4998 | if (btDevice == null) { |
| 4999 | return; |
| 5000 | } |
| 5001 | String address = btDevice.getAddress(); |
| 5002 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5003 | address = ""; |
| 5004 | } |
| 5005 | |
| 5006 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5007 | final String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 5008 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5009 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5010 | |
| 5011 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5012 | makeA2dpSrcUnavailable(address); |
| 5013 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5014 | makeA2dpSrcAvailable(address); |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5019 | private void onBluetoothA2dpDeviceConfigChange(BluetoothDevice btDevice) |
| 5020 | { |
| 5021 | if (DEBUG_VOL) { |
| 5022 | Log.d(TAG, "onBluetoothA2dpDeviceConfigChange btDevice=" + btDevice); |
| 5023 | } |
| 5024 | if (btDevice == null) { |
| 5025 | return; |
| 5026 | } |
| 5027 | String address = btDevice.getAddress(); |
| 5028 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5029 | address = ""; |
| 5030 | } |
| 5031 | |
| 5032 | int device = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; |
| 5033 | synchronized (mConnectedDevices) { |
| 5034 | final String key = makeDeviceListKey(device, address); |
| 5035 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5036 | if (deviceSpec != null) { |
| 5037 | // Device is connected |
| 5038 | AudioSystem.handleDeviceConfigChange(device, address, |
| 5039 | btDevice.getName()); |
| 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5044 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 5045 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 5046 | synchronized (mA2dpAvrcpLock) { |
| 5047 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5048 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 5049 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5050 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 5051 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 5052 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5053 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5057 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 5058 | String deviceName) { |
| 5059 | if (DEBUG_DEVICES) { |
| 5060 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 5061 | + " address:" + address + " name:" + deviceName + ")"); |
| 5062 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5063 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5064 | String deviceKey = makeDeviceListKey(device, address); |
| 5065 | if (DEBUG_DEVICES) { |
| 5066 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 5067 | } |
| 5068 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 5069 | boolean isConnected = deviceSpec != null; |
| 5070 | if (DEBUG_DEVICES) { |
| 5071 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 5072 | } |
| 5073 | if (connect && !isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5074 | final int res = AudioSystem.setDeviceConnectionState(device, |
| 5075 | AudioSystem.DEVICE_STATE_AVAILABLE, address, deviceName); |
| 5076 | if (res != AudioSystem.AUDIO_STATUS_OK) { |
| 5077 | Slog.e(TAG, "not connecting device 0x" + Integer.toHexString(device) + |
| 5078 | " due to command error " + res ); |
| 5079 | return false; |
| 5080 | } |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5081 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
| 5082 | return true; |
| 5083 | } else if (!connect && isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5084 | AudioSystem.setDeviceConnectionState(device, |
| 5085 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, deviceName); |
| 5086 | // always remove even if disconnection failed |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5087 | mConnectedDevices.remove(deviceKey); |
| 5088 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5089 | } |
| 5090 | } |
| 5091 | return false; |
| 5092 | } |
| 5093 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5094 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 5095 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5096 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5097 | int mBecomingNoisyIntentDevices = |
| 5098 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5099 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 5100 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 5101 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5102 | |
| 5103 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5104 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5105 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 5106 | int delay = 0; |
| 5107 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 5108 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 5109 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 5110 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5111 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 5112 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 5113 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5114 | } |
| 5115 | } |
| 5116 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5117 | sendMsg(mAudioHandler, |
| 5118 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5119 | SENDMSG_REPLACE, |
| 5120 | 0, |
| 5121 | 0, |
| 5122 | null, |
| 5123 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5124 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5125 | } |
| 5126 | } |
| 5127 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5128 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 5129 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5130 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 5131 | synchronized (mLastDeviceConnectMsgTime) { |
| 5132 | long time = SystemClock.uptimeMillis(); |
| 5133 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 5134 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 5135 | } |
| 5136 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5137 | } |
| 5138 | return delay; |
| 5139 | } |
| 5140 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5141 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 5142 | String deviceName) { |
| 5143 | if (DEBUG_DEVICES) { |
| 5144 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 5145 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 5146 | " name:" + deviceName + ");"); |
| 5147 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5148 | Intent intent = new Intent(); |
| 5149 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5150 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 5151 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 5152 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 5153 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5154 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 5155 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5156 | int connType = 0; |
| 5157 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5158 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5159 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5160 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 5161 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 5162 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 5163 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 5164 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5165 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5166 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 5167 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 5168 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 5169 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5170 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5171 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5172 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 5173 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5174 | } |
| 5175 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5176 | synchronized (mCurAudioRoutes) { |
| 5177 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5178 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5179 | if (state != 0) { |
| 5180 | newConn |= connType; |
| 5181 | } else { |
| 5182 | newConn &= ~connType; |
| 5183 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5184 | if (newConn != mCurAudioRoutes.mainType) { |
| 5185 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5186 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5187 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5188 | } |
| 5189 | } |
| 5190 | } |
| 5191 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5192 | final long ident = Binder.clearCallingIdentity(); |
| 5193 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5194 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5195 | } finally { |
| 5196 | Binder.restoreCallingIdentity(ident); |
| 5197 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5198 | } |
| 5199 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5200 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5201 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5202 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5203 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 5204 | + " state:" + Integer.toHexString(state) |
| 5205 | + " address:" + address |
| 5206 | + " deviceName:" + deviceName |
| 5207 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5208 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5209 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5210 | synchronized (mConnectedDevices) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5211 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
| 5212 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 5213 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
| 5214 | setBluetoothA2dpOnInt(true); |
| 5215 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 5216 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 5217 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 5218 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5219 | if (!handleDeviceConnection(state == 1, device, address, deviceName)) { |
| 5220 | // change of connection state failed, bailout |
| 5221 | return; |
| 5222 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5223 | if (state != 0) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5224 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
| 5225 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 5226 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
| 5227 | setBluetoothA2dpOnInt(false); |
| 5228 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5229 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 5230 | sendMsg(mAudioHandler, |
| 5231 | MSG_CHECK_MUSIC_ACTIVE, |
| 5232 | SENDMSG_REPLACE, |
| 5233 | 0, |
| 5234 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5235 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5236 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5237 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5238 | // Television devices without CEC service apply software volume on HDMI output |
| 5239 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5240 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 5241 | checkAllFixedVolumeDevices(); |
| 5242 | if (mHdmiManager != null) { |
| 5243 | synchronized (mHdmiManager) { |
| 5244 | if (mHdmiPlaybackClient != null) { |
| 5245 | mHdmiCecSink = false; |
| 5246 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 5247 | } |
| 5248 | } |
| 5249 | } |
| 5250 | } |
| 5251 | } else { |
| 5252 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5253 | if (mHdmiManager != null) { |
| 5254 | synchronized (mHdmiManager) { |
| 5255 | mHdmiCecSink = false; |
| 5256 | } |
| 5257 | } |
| 5258 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5259 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5260 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 5261 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 5262 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5266 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5267 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 5268 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5269 | if (state == 1) { |
| 5270 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 5271 | int[] portGeneration = new int[1]; |
| 5272 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 5273 | if (status == AudioManager.SUCCESS) { |
| 5274 | for (AudioPort port : ports) { |
| 5275 | if (port instanceof AudioDevicePort) { |
| 5276 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 5277 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 5278 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5279 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 5280 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5281 | if (formats.length > 0) { |
| 5282 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 5283 | for (int format : formats) { |
| 5284 | // a format in the list can be 0, skip it |
| 5285 | if (format != AudioFormat.ENCODING_INVALID) { |
| 5286 | encodingList.add(format); |
| 5287 | } |
| 5288 | } |
| 5289 | int[] encodingArray = new int[encodingList.size()]; |
| 5290 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 5291 | encodingArray[i] = encodingList.get(i); |
| 5292 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5293 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5294 | } |
| 5295 | // find the maximum supported number of channels |
| 5296 | int maxChannels = 0; |
| 5297 | for (int mask : devicePort.channelMasks()) { |
| 5298 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 5299 | if (channelCount > maxChannels) { |
| 5300 | maxChannels = channelCount; |
| 5301 | } |
| 5302 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5303 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5311 | /* cache of the address of the last dock the device was connected to */ |
| 5312 | private String mDockAddress; |
| 5313 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5314 | /** |
| 5315 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 5316 | */ |
| 5317 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 5318 | @Override |
| 5319 | public void onReceive(Context context, Intent intent) { |
| 5320 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 5321 | int outDevice; |
| 5322 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5323 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5324 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5325 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 5326 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 5327 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 5328 | int config; |
| 5329 | switch (dockState) { |
| 5330 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 5331 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 5332 | break; |
| 5333 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 5334 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 5335 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5336 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5337 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5338 | break; |
| 5339 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 5340 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 5341 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5342 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 5343 | default: |
| 5344 | config = AudioSystem.FORCE_NONE; |
| 5345 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5346 | // Low end docks have a menu to enable or disable audio |
| 5347 | // (see mDockAudioMediaEnabled) |
| 5348 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 5349 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 5350 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 5351 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 5352 | } |
| 5353 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5354 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5355 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5356 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 5357 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 5358 | setBtScoDeviceConnectionState(btDevice, state); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 5359 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5360 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5361 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5362 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5363 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5364 | // broadcast intent if the connection was initated by AudioService |
| 5365 | if (!mScoClients.isEmpty() && |
| 5366 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 5367 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 5368 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5369 | broadcast = true; |
| 5370 | } |
| 5371 | switch (btState) { |
| 5372 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5373 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5374 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5375 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5376 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5377 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5378 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5379 | break; |
| 5380 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5381 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5382 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 5383 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5384 | break; |
| 5385 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5386 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5387 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5388 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5389 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5390 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5391 | default: |
| 5392 | // do not broadcast CONNECTING or invalid state |
| 5393 | broadcast = false; |
| 5394 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5395 | } |
| 5396 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5397 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5398 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5399 | //FIXME: this is to maintain compatibility with deprecated intent |
| 5400 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5401 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5402 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5403 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5404 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5405 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5406 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5407 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5408 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5409 | AudioSystem.setParameters("screen_state=on"); |
| 5410 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5411 | if (mMonitorRotation) { |
| 5412 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5413 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5414 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5415 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 5416 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5417 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5418 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5419 | if (mUserSwitchedReceived) { |
| 5420 | // attempt to stop music playback for background user except on first user |
| 5421 | // switch (i.e. first boot) |
| 5422 | sendMsg(mAudioHandler, |
| 5423 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5424 | SENDMSG_REPLACE, |
| 5425 | 0, |
| 5426 | 0, |
| 5427 | null, |
| 5428 | 0); |
| 5429 | } |
| 5430 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5431 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5432 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5433 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5434 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5435 | readAudioSettings(true /*userSwitch*/); |
| 5436 | // preserve STREAM_MUSIC volume from one user to the next. |
| 5437 | sendMsg(mAudioHandler, |
| 5438 | MSG_SET_ALL_VOLUMES, |
| 5439 | SENDMSG_QUEUE, |
| 5440 | 0, |
| 5441 | 0, |
| 5442 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5443 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 5444 | // Disable audio recording for the background user/profile |
| 5445 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5446 | if (userId >= 0) { |
| 5447 | // TODO Kill recording streams instead of killing processes holding permission |
| 5448 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 5449 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 5450 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5451 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5452 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 5453 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 5454 | // Enable audio recording for foreground user/profile |
| 5455 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5456 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5457 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 5458 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 5459 | state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 5460 | if (state == BluetoothAdapter.STATE_OFF || |
| 5461 | state == BluetoothAdapter.STATE_TURNING_OFF) { |
| 5462 | disconnectAllBluetoothProfiles(); |
| 5463 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5464 | } |
| 5465 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5466 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5467 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5468 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 5469 | |
| 5470 | @Override |
| 5471 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 5472 | Bundle prevRestrictions) { |
| 5473 | // Update mic mute state. |
| 5474 | { |
| 5475 | final boolean wasRestricted = |
| 5476 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5477 | final boolean isRestricted = |
| 5478 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5479 | if (wasRestricted != isRestricted) { |
| 5480 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | // Update speaker mute state. |
| 5485 | { |
| 5486 | final boolean wasRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5487 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
| 5488 | || prevRestrictions.getBoolean(UserManager.DISALLLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5489 | final boolean isRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5490 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
| 5491 | || newRestrictions.getBoolean(UserManager.DISALLLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5492 | if (wasRestricted != isRestricted) { |
| 5493 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | } // end class AudioServiceUserRestrictionsListener |
| 5498 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5499 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 5500 | PackageManager pm = mContext.getPackageManager(); |
| 5501 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 5502 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 5503 | ComponentName homeActivityName = null; |
| 5504 | if (!oldUser.isManagedProfile()) { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 5505 | homeActivityName = mActivityManagerInternal.getHomeActivityForUser(oldUser.id); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5506 | } |
| 5507 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 5508 | List<PackageInfo> packages; |
| 5509 | try { |
| 5510 | packages = AppGlobals.getPackageManager() |
| 5511 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 5512 | } catch (RemoteException e) { |
| 5513 | throw new AndroidRuntimeException(e); |
| 5514 | } |
| 5515 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 5516 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 5517 | // Skip system processes |
| 5518 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 5519 | continue; |
| 5520 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 5521 | // Skip packages that have permission to interact across users |
| 5522 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 5523 | == PackageManager.PERMISSION_GRANTED) { |
| 5524 | continue; |
| 5525 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5526 | if (homeActivityName != null |
| 5527 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 5528 | && pkg.applicationInfo.isSystemApp()) { |
| 5529 | continue; |
| 5530 | } |
| 5531 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5532 | final int uid = pkg.applicationInfo.uid; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5533 | ActivityManager.getService().killUid(UserHandle.getAppId(uid), |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5534 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5535 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 5536 | } catch (RemoteException e) { |
| 5537 | Log.w(TAG, "Error calling killUid", e); |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5542 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5543 | //========================================================================================== |
| 5544 | // Audio Focus |
| 5545 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5546 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5547 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5548 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5549 | // permission checks |
| 5550 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5551 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5552 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 5553 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 5554 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 5555 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5556 | } |
| 5557 | } else { |
| 5558 | // only a registered audio policy can be used to lock focus |
| 5559 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5560 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5561 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5562 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5563 | } |
| 5564 | } |
| 5565 | } |
| 5566 | } |
| 5567 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5568 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 5569 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5570 | } |
| 5571 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5572 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 5573 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | public void unregisterAudioFocusClient(String clientId) { |
| 5577 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5578 | } |
| 5579 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 5580 | public int getCurrentAudioFocus() { |
| 5581 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 5582 | } |
| 5583 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5584 | private boolean readCameraSoundForced() { |
| 5585 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 5586 | mContext.getResources().getBoolean( |
| 5587 | com.android.internal.R.bool.config_camera_sound_forced); |
| 5588 | } |
| 5589 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5590 | //========================================================================================== |
| 5591 | // Device orientation |
| 5592 | //========================================================================================== |
| 5593 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5594 | * Handles device configuration changes that may map to a change in the orientation |
| 5595 | * or orientation. |
| 5596 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 5597 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5598 | */ |
| 5599 | private void handleConfigurationChanged(Context context) { |
| 5600 | try { |
| 5601 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 5602 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5603 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5604 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5605 | if (mMonitorOrientation) { |
| 5606 | int newOrientation = config.orientation; |
| 5607 | if (newOrientation != mDeviceOrientation) { |
| 5608 | mDeviceOrientation = newOrientation; |
| 5609 | setOrientationForAudioSystem(); |
| 5610 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5611 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5612 | sendMsg(mAudioHandler, |
| 5613 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 5614 | SENDMSG_REPLACE, |
| 5615 | 0, |
| 5616 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5617 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5618 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5619 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5620 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5621 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5622 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5623 | synchronized (mCameraSoundForced) { |
| 5624 | if (cameraSoundForced != mCameraSoundForced) { |
| 5625 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5626 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5627 | } |
| 5628 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5629 | if (cameraSoundForcedChanged) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 5630 | if (!mIsSingleVolume) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5631 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 5632 | if (cameraSoundForced) { |
| 5633 | s.setAllIndexesToMax(); |
| 5634 | mRingerModeAffectedStreams &= |
| 5635 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5636 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5637 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5638 | mRingerModeAffectedStreams |= |
| 5639 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5640 | } |
| 5641 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5642 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | sendMsg(mAudioHandler, |
| 5646 | MSG_SET_FORCE_USE, |
| 5647 | SENDMSG_QUEUE, |
| 5648 | AudioSystem.FOR_SYSTEM, |
| 5649 | cameraSoundForced ? |
| 5650 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 5651 | null, |
| 5652 | 0); |
| 5653 | |
| 5654 | sendMsg(mAudioHandler, |
| 5655 | MSG_SET_ALL_VOLUMES, |
| 5656 | SENDMSG_QUEUE, |
| 5657 | 0, |
| 5658 | 0, |
| 5659 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 5660 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5661 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5662 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5663 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5664 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5668 | //TODO move to an external "orientation helper" class |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5669 | private void setOrientationForAudioSystem() { |
| 5670 | switch (mDeviceOrientation) { |
| 5671 | case Configuration.ORIENTATION_LANDSCAPE: |
| 5672 | //Log.i(TAG, "orientation is landscape"); |
| 5673 | AudioSystem.setParameters("orientation=landscape"); |
| 5674 | break; |
| 5675 | case Configuration.ORIENTATION_PORTRAIT: |
| 5676 | //Log.i(TAG, "orientation is portrait"); |
| 5677 | AudioSystem.setParameters("orientation=portrait"); |
| 5678 | break; |
| 5679 | case Configuration.ORIENTATION_SQUARE: |
| 5680 | //Log.i(TAG, "orientation is square"); |
| 5681 | AudioSystem.setParameters("orientation=square"); |
| 5682 | break; |
| 5683 | case Configuration.ORIENTATION_UNDEFINED: |
| 5684 | //Log.i(TAG, "orientation is undefined"); |
| 5685 | AudioSystem.setParameters("orientation=undefined"); |
| 5686 | break; |
| 5687 | default: |
| 5688 | Log.e(TAG, "Unknown orientation"); |
| 5689 | } |
| 5690 | } |
| 5691 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5692 | // Handles request to override default use of A2DP for media. |
| 5693 | // Must be called synchronized on mConnectedDevices |
| 5694 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5695 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5696 | mBluetoothA2dpEnabled = on; |
| 5697 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
| 5698 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
| 5699 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 5700 | } |
| 5701 | } |
| 5702 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5703 | // Must be called synchronized on mConnectedDevices |
| 5704 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5705 | switch (usage) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5706 | case AudioSystem.FOR_MEDIA: |
| 5707 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5708 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5709 | } else { // config == AudioSystem.FORCE_NONE |
| 5710 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5711 | } |
Sungsoo | 71f3563 | 2016-09-28 16:26:49 +0900 | [diff] [blame] | 5712 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5713 | SENDMSG_NOOP, 0, 0, null, 0); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5714 | break; |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5715 | case AudioSystem.FOR_DOCK: |
| 5716 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5717 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5718 | } else { // config == AudioSystem.FORCE_NONE |
| 5719 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5720 | } |
| 5721 | break; |
| 5722 | default: |
| 5723 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5724 | } |
| 5725 | AudioSystem.setForceUse(usage, config); |
| 5726 | } |
| 5727 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5728 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5729 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5730 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5731 | mRingtonePlayer = player; |
| 5732 | } |
| 5733 | |
| 5734 | @Override |
| 5735 | public IRingtonePlayer getRingtonePlayer() { |
| 5736 | return mRingtonePlayer; |
| 5737 | } |
| 5738 | |
| 5739 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5740 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5741 | synchronized (mCurAudioRoutes) { |
| 5742 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5743 | mRoutesObservers.register(observer); |
| 5744 | return routes; |
| 5745 | } |
| 5746 | } |
| 5747 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5748 | |
| 5749 | //========================================================================================== |
| 5750 | // Safe media volume management. |
| 5751 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5752 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5753 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5754 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5755 | // property. Platforms with a different limit must set this property accordingly in their |
| 5756 | // overlay. |
| 5757 | //========================================================================================== |
| 5758 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5759 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5760 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5761 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5762 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5763 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5764 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5765 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5766 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5767 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5768 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5769 | private Integer mSafeMediaVolumeState; |
| 5770 | |
| 5771 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5772 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5773 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5774 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5775 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5776 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5777 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5778 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5779 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5780 | private int mMusicActiveMs; |
| 5781 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5782 | 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] | 5783 | 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] | 5784 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5785 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5786 | synchronized (mSafeMediaVolumeState) { |
| 5787 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5788 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5789 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5790 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5791 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5792 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5793 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5794 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5795 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5796 | sendMsg(mAudioHandler, |
| 5797 | MSG_CHECK_MUSIC_ACTIVE, |
| 5798 | SENDMSG_REPLACE, |
| 5799 | 0, |
| 5800 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5801 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5802 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5803 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5804 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5805 | } |
| 5806 | } |
| 5807 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5808 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5809 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5810 | int devices = mSafeMediaVolumeDevices; |
| 5811 | int i = 0; |
| 5812 | |
| 5813 | while (devices != 0) { |
| 5814 | int device = 1 << i++; |
| 5815 | if ((device & devices) == 0) { |
| 5816 | continue; |
| 5817 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5818 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5819 | if (index > mSafeMediaVolumeIndex) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5820 | streamState.setIndex(mSafeMediaVolumeIndex, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5821 | sendMsg(mAudioHandler, |
| 5822 | MSG_SET_DEVICE_VOLUME, |
| 5823 | SENDMSG_QUEUE, |
| 5824 | device, |
| 5825 | 0, |
| 5826 | streamState, |
| 5827 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5828 | } |
| 5829 | devices &= ~device; |
| 5830 | } |
| 5831 | } |
| 5832 | |
| 5833 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5834 | synchronized (mSafeMediaVolumeState) { |
| 5835 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5836 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5837 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5838 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5839 | return false; |
| 5840 | } |
| 5841 | return true; |
| 5842 | } |
| 5843 | } |
| 5844 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5845 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5846 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5847 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5848 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5849 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5850 | if (mPendingVolumeCommand != null) { |
| 5851 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5852 | mPendingVolumeCommand.mIndex, |
| 5853 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5854 | mPendingVolumeCommand.mDevice, |
| 5855 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5856 | mPendingVolumeCommand = null; |
| 5857 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5861 | //========================================================================================== |
| 5862 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5863 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5864 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5865 | //========================================================================================== |
| 5866 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5867 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5868 | public void onComplete(int status) { |
| 5869 | if (mHdmiManager != null) { |
| 5870 | synchronized (mHdmiManager) { |
| 5871 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5872 | // Television devices without CEC service apply software volume on HDMI output |
| 5873 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5874 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5875 | } |
| 5876 | checkAllFixedVolumeDevices(); |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | }; |
| 5881 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5882 | // If HDMI-CEC system audio is supported |
| 5883 | private boolean mHdmiSystemAudioSupported = false; |
| 5884 | // Set only when device is tv. |
| 5885 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5886 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5887 | // cached HdmiControlManager interface |
| 5888 | private HdmiControlManager mHdmiManager; |
| 5889 | // Set only when device is a set-top box. |
| 5890 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5891 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5892 | private boolean mHdmiCecSink; |
| 5893 | |
| 5894 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5895 | |
| 5896 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5897 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5898 | int device = AudioSystem.DEVICE_NONE; |
| 5899 | if (mHdmiManager != null) { |
| 5900 | synchronized (mHdmiManager) { |
| 5901 | if (mHdmiTvClient == null) { |
| 5902 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5903 | return device; |
| 5904 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5905 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5906 | synchronized (mHdmiTvClient) { |
| 5907 | if (mHdmiSystemAudioSupported != on) { |
| 5908 | mHdmiSystemAudioSupported = on; |
| 5909 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5910 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5911 | AudioSystem.FORCE_NONE); |
| 5912 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5913 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5914 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5915 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5916 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5917 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5918 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5919 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5920 | @Override |
| 5921 | public boolean isHdmiSystemAudioSupported() { |
| 5922 | return mHdmiSystemAudioSupported; |
| 5923 | } |
| 5924 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5925 | //========================================================================================== |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5926 | // Accessibility |
| 5927 | |
| 5928 | private void initA11yMonitoring(Context ctxt) { |
| 5929 | AccessibilityManager accessibilityManager = |
| 5930 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5931 | updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled()); |
| 5932 | updateA11yVolumeAlias(accessibilityManager.isEnabled()); |
| 5933 | accessibilityManager.addTouchExplorationStateChangeListener(this); |
| 5934 | accessibilityManager.addAccessibilityStateChangeListener(this); |
| 5935 | } |
| 5936 | |
| 5937 | //--------------------------------------------------------------------------------- |
| 5938 | // A11y: taking touch exploration into account for selecting the default |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5939 | // stream override timeout when adjusting volume |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5940 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5941 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5942 | // AudioService.getActiveStreamType() will return: |
| 5943 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5944 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5945 | // stopped |
| 5946 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
| 5947 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5948 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5949 | private static int sStreamOverrideDelayMs; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5950 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5951 | @Override |
| 5952 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5953 | updateDefaultStreamOverrideDelay(enabled); |
| 5954 | } |
| 5955 | |
| 5956 | private void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5957 | if (touchExploreEnabled) { |
| 5958 | sStreamOverrideDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5959 | } else { |
| 5960 | sStreamOverrideDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5961 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5962 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5963 | + " stream override delay is now " + sStreamOverrideDelayMs + " ms"); |
| 5964 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5965 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5966 | //--------------------------------------------------------------------------------- |
| 5967 | // A11y: taking a11y state into account for the handling of a11y prompts volume |
| 5968 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5969 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5970 | private static boolean sIndependentA11yVolume = false; |
| 5971 | |
| 5972 | @Override |
| 5973 | public void onAccessibilityStateChanged(boolean enabled) { |
| 5974 | updateA11yVolumeAlias(enabled); |
| 5975 | } |
| 5976 | |
| 5977 | private void updateA11yVolumeAlias(boolean a11Enabled) { |
| 5978 | if (DEBUG_VOL) Log.d(TAG, "Accessibility mode changed to " + a11Enabled); |
| 5979 | // a11y has its own volume stream when a11y service is enabled |
| 5980 | sIndependentA11yVolume = a11Enabled; |
| 5981 | // update the volume mapping scheme |
| 5982 | updateStreamVolumeAlias(true /*updateVolumes*/, TAG); |
| 5983 | // update the volume controller behavior |
| 5984 | mVolumeController.setA11yMode(sIndependentA11yVolume ? |
| 5985 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 5986 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5987 | } |
| 5988 | |
| 5989 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5990 | // Camera shutter sound policy. |
| 5991 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5992 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5993 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5994 | //========================================================================================== |
| 5995 | |
| 5996 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5997 | private Boolean mCameraSoundForced; |
| 5998 | |
| 5999 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 6000 | public boolean isCameraSoundForced() { |
| 6001 | synchronized (mCameraSoundForced) { |
| 6002 | return mCameraSoundForced; |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 6007 | "SILENT", |
| 6008 | "VIBRATE", |
| 6009 | "NORMAL" |
| 6010 | }; |
| 6011 | |
| 6012 | private void dumpRingerMode(PrintWriter pw) { |
| 6013 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6014 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 6015 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6016 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
| 6017 | dumpRingerModeStreams(pw, "muted", mRingerModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6018 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6019 | } |
| 6020 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6021 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 6022 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 6023 | pw.print(Integer.toHexString(streams)); |
| 6024 | if (streams != 0) { |
| 6025 | pw.print(" ("); |
| 6026 | boolean first = true; |
| 6027 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 6028 | final int stream = (1 << i); |
| 6029 | if ((streams & stream) != 0) { |
| 6030 | if (!first) pw.print(','); |
| 6031 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 6032 | streams &= ~stream; |
| 6033 | first = false; |
| 6034 | } |
| 6035 | } |
| 6036 | if (streams != 0) { |
| 6037 | if (!first) pw.print(','); |
| 6038 | pw.print(streams); |
| 6039 | } |
| 6040 | pw.print(')'); |
| 6041 | } |
| 6042 | pw.println(); |
| 6043 | } |
| 6044 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6045 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6046 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 6047 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 6048 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6049 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 6050 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6051 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6052 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6053 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 6054 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6055 | |
| 6056 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6057 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6058 | pw.print(" mSafeMediaVolumeState="); |
| 6059 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 6060 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 6061 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 6062 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 6063 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6064 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6065 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6066 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6067 | |
| 6068 | dumpAudioPolicies(pw); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6069 | |
| 6070 | mPlaybackMonitor.dump(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | private static String safeMediaVolumeStateToString(Integer state) { |
| 6074 | switch(state) { |
| 6075 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 6076 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 6077 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 6078 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 6079 | } |
| 6080 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6081 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 6082 | |
| 6083 | // Inform AudioFlinger of our device's low RAM attribute |
| 6084 | private static void readAndSetLowRamDevice() |
| 6085 | { |
| 6086 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 6087 | if (status != 0) { |
| 6088 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 6089 | } |
| 6090 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6091 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6092 | private void enforceVolumeController(String action) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6093 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 6094 | "Only SystemUI can " + action); |
| 6095 | } |
| 6096 | |
| 6097 | @Override |
| 6098 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6099 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6100 | |
| 6101 | // return early if things are not actually changing |
| 6102 | if (mVolumeController.isSameBinder(controller)) { |
| 6103 | return; |
| 6104 | } |
| 6105 | |
| 6106 | // dismiss the old volume controller |
| 6107 | mVolumeController.postDismiss(); |
| 6108 | if (controller != null) { |
| 6109 | // we are about to register a new controller, listen for its death |
| 6110 | try { |
| 6111 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 6112 | @Override |
| 6113 | public void binderDied() { |
| 6114 | if (mVolumeController.isSameBinder(controller)) { |
| 6115 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 6116 | setVolumeController(null); |
| 6117 | } |
| 6118 | } |
| 6119 | }, 0); |
| 6120 | } catch (RemoteException e) { |
| 6121 | // noop |
| 6122 | } |
| 6123 | } |
| 6124 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6125 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 6126 | } |
| 6127 | |
| 6128 | @Override |
| 6129 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6130 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6131 | |
| 6132 | // return early if the controller is not current |
| 6133 | if (!mVolumeController.isSameBinder(controller)) { |
| 6134 | return; |
| 6135 | } |
| 6136 | |
| 6137 | mVolumeController.setVisible(visible); |
| 6138 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6139 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6140 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6141 | @Override |
| 6142 | public void setVolumePolicy(VolumePolicy policy) { |
| 6143 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 6144 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6145 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 6146 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6147 | } |
| 6148 | } |
| 6149 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6150 | public static class VolumeController { |
| 6151 | private static final String TAG = "VolumeController"; |
| 6152 | |
| 6153 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6154 | private boolean mVisible; |
| 6155 | private long mNextLongPress; |
| 6156 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6157 | |
| 6158 | public void setController(IVolumeController controller) { |
| 6159 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6160 | mVisible = false; |
| 6161 | } |
| 6162 | |
| 6163 | public void loadSettings(ContentResolver cr) { |
| 6164 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 6165 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 6166 | } |
| 6167 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 6168 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 6169 | if (isMute) { |
| 6170 | return false; |
| 6171 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6172 | boolean suppress = false; |
| 6173 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 6174 | final long now = SystemClock.uptimeMillis(); |
| 6175 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 6176 | // ui will become visible |
| 6177 | if (mNextLongPress < now) { |
| 6178 | mNextLongPress = now + mLongPressTimeout; |
| 6179 | } |
| 6180 | suppress = true; |
| 6181 | } else if (mNextLongPress > 0) { // in a long-press |
| 6182 | if (now > mNextLongPress) { |
| 6183 | // long press triggered, no more suppression |
| 6184 | mNextLongPress = 0; |
| 6185 | } else { |
| 6186 | // keep suppressing until the long press triggers |
| 6187 | suppress = true; |
| 6188 | } |
| 6189 | } |
| 6190 | } |
| 6191 | return suppress; |
| 6192 | } |
| 6193 | |
| 6194 | public void setVisible(boolean visible) { |
| 6195 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6196 | } |
| 6197 | |
| 6198 | public boolean isSameBinder(IVolumeController controller) { |
| 6199 | return Objects.equals(asBinder(), binder(controller)); |
| 6200 | } |
| 6201 | |
| 6202 | public IBinder asBinder() { |
| 6203 | return binder(mController); |
| 6204 | } |
| 6205 | |
| 6206 | private static IBinder binder(IVolumeController controller) { |
| 6207 | return controller == null ? null : controller.asBinder(); |
| 6208 | } |
| 6209 | |
| 6210 | @Override |
| 6211 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6212 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | public void postDisplaySafeVolumeWarning(int flags) { |
| 6216 | if (mController == null) |
| 6217 | return; |
| 6218 | try { |
| 6219 | mController.displaySafeVolumeWarning(flags); |
| 6220 | } catch (RemoteException e) { |
| 6221 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | public void postVolumeChanged(int streamType, int flags) { |
| 6226 | if (mController == null) |
| 6227 | return; |
| 6228 | try { |
| 6229 | mController.volumeChanged(streamType, flags); |
| 6230 | } catch (RemoteException e) { |
| 6231 | Log.w(TAG, "Error calling volumeChanged", e); |
| 6232 | } |
| 6233 | } |
| 6234 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6235 | public void postMasterMuteChanged(int flags) { |
| 6236 | if (mController == null) |
| 6237 | return; |
| 6238 | try { |
| 6239 | mController.masterMuteChanged(flags); |
| 6240 | } catch (RemoteException e) { |
| 6241 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | public void setLayoutDirection(int layoutDirection) { |
| 6246 | if (mController == null) |
| 6247 | return; |
| 6248 | try { |
| 6249 | mController.setLayoutDirection(layoutDirection); |
| 6250 | } catch (RemoteException e) { |
| 6251 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | public void postDismiss() { |
| 6256 | if (mController == null) |
| 6257 | return; |
| 6258 | try { |
| 6259 | mController.dismiss(); |
| 6260 | } catch (RemoteException e) { |
| 6261 | Log.w(TAG, "Error calling dismiss", e); |
| 6262 | } |
| 6263 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6264 | |
| 6265 | public void setA11yMode(int a11yMode) { |
| 6266 | if (mController == null) |
| 6267 | return; |
| 6268 | try { |
| 6269 | mController.setA11yMode(a11yMode); |
| 6270 | } catch (RemoteException e) { |
| 6271 | Log.w(TAG, "Error calling setA11Mode", e); |
| 6272 | } |
| 6273 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6274 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6275 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6276 | /** |
| 6277 | * Interface for system components to get some extra functionality through |
| 6278 | * LocalServices. |
| 6279 | */ |
| 6280 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6281 | @Override |
| 6282 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 6283 | mRingerModeDelegate = delegate; |
| 6284 | if (mRingerModeDelegate != null) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6285 | updateRingerModeAffectedStreams(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6286 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 6287 | } |
| 6288 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6289 | |
| 6290 | @Override |
| 6291 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 6292 | String callingPackage, int uid) { |
| 6293 | // direction and stream type swap here because the public |
| 6294 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6295 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 6296 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6297 | } |
| 6298 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6299 | @Override |
| 6300 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 6301 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6302 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 6303 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6304 | } |
| 6305 | |
| 6306 | @Override |
| 6307 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 6308 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6309 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6310 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 6311 | |
| 6312 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6313 | public int getRingerModeInternal() { |
| 6314 | return AudioService.this.getRingerModeInternal(); |
| 6315 | } |
| 6316 | |
| 6317 | @Override |
| 6318 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 6319 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 6320 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6321 | |
| 6322 | @Override |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6323 | public void updateRingerModeAffectedStreamsInternal() { |
| 6324 | synchronized (mSettingsLock) { |
| 6325 | if (updateRingerModeAffectedStreams()) { |
| 6326 | setRingerModeInt(getRingerModeInternal(), false); |
| 6327 | } |
| 6328 | } |
| 6329 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6330 | } |
| 6331 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6332 | //========================================================================================== |
| 6333 | // Audio policy management |
| 6334 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6335 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 6336 | boolean hasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6337 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 6338 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6339 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 6340 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6341 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6342 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6343 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6344 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6345 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6346 | if (!hasPermissionForPolicy) { |
| 6347 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 6348 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6349 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6350 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6351 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6352 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6353 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6354 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6355 | Slog.e(TAG, "Cannot re-register policy"); |
| 6356 | return null; |
| 6357 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6358 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 6359 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 6360 | regId = app.getRegistrationId(); |
| 6361 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6362 | } catch (RemoteException e) { |
| 6363 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6364 | 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] | 6365 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6366 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6367 | } |
| 6368 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6369 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6370 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6371 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6372 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 6373 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6374 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6375 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6376 | if (app == null) { |
| 6377 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 6378 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6379 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6380 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6381 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6382 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6383 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6384 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6385 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6386 | } |
| 6387 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6388 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 6389 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 6390 | + " policy " + pcb.asBinder()); |
| 6391 | // error handling |
| 6392 | boolean hasPermissionForPolicy = |
| 6393 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6394 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6395 | if (!hasPermissionForPolicy) { |
| 6396 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 6397 | + Binder.getCallingPid() + " / uid " |
| 6398 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 6399 | return AudioManager.ERROR; |
| 6400 | } |
| 6401 | |
| 6402 | synchronized (mAudioPolicies) { |
| 6403 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6404 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 6405 | return AudioManager.ERROR; |
| 6406 | } |
| 6407 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 6408 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6409 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6410 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6411 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6412 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 6413 | return AudioManager.ERROR; |
| 6414 | } |
| 6415 | } |
| 6416 | } |
| 6417 | app.mFocusDuckBehavior = duckingBehavior; |
| 6418 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 6419 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 6420 | } |
| 6421 | return AudioManager.SUCCESS; |
| 6422 | } |
| 6423 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6424 | private void dumpAudioPolicies(PrintWriter pw) { |
| 6425 | pw.println("\nAudio policies:"); |
| 6426 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6427 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6428 | pw.println(policy.toLogFriendlyString()); |
| 6429 | } |
| 6430 | } |
| 6431 | } |
| 6432 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6433 | //====================== |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6434 | // Audio policy callbacks from AudioSystem for dynamic policies |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6435 | //====================== |
| 6436 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 6437 | new AudioSystem.DynamicPolicyCallback() { |
| 6438 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 6439 | if (!TextUtils.isEmpty(regId)) { |
| 6440 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 6441 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 6442 | } |
| 6443 | } |
| 6444 | }; |
| 6445 | |
| 6446 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 6447 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 6448 | synchronized (mAudioPolicies) { |
| 6449 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 6450 | for (AudioMix mix : policy.getMixes()) { |
| 6451 | if (mix.getRegistration().equals(regId)) { |
| 6452 | try { |
| 6453 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 6454 | } catch (RemoteException e) { |
| 6455 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 6456 | + policy.mPolicyCallback.asBinder(), e); |
| 6457 | } |
| 6458 | return; |
| 6459 | } |
| 6460 | } |
| 6461 | } |
| 6462 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6463 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6464 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6465 | //====================== |
| 6466 | // Audio policy callbacks from AudioSystem for recording configuration updates |
| 6467 | //====================== |
| 6468 | private final RecordingActivityMonitor mRecordMonitor = new RecordingActivityMonitor(); |
| 6469 | |
| 6470 | public void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 6471 | mRecordMonitor.registerRecordingCallback(rcdb); |
| 6472 | } |
| 6473 | |
| 6474 | public void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 6475 | mRecordMonitor.unregisterRecordingCallback(rcdb); |
| 6476 | } |
| 6477 | |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 6478 | public List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 6479 | return mRecordMonitor.getActiveRecordingConfigurations(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | //====================== |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6483 | // Audio playback notification |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6484 | //====================== |
| 6485 | private final PlaybackActivityMonitor mPlaybackMonitor = new PlaybackActivityMonitor(); |
| 6486 | |
| 6487 | public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6488 | final boolean isPrivileged = |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6489 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6490 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6491 | mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged); |
| 6492 | } |
| 6493 | |
| 6494 | public void unregisterPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
| 6495 | mPlaybackMonitor.unregisterPlaybackCallback(pcdb); |
| 6496 | } |
| 6497 | |
| 6498 | public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6499 | final boolean isPrivileged = |
| 6500 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6501 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6502 | return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6503 | } |
| 6504 | |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6505 | public int trackPlayer(PlayerBase.PlayerIdCard pic) { |
| 6506 | return mPlaybackMonitor.trackPlayer(pic); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | public void playerAttributes(int piid, AudioAttributes attr) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6510 | mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | public void playerEvent(int piid, int event) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6514 | mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | public void releasePlayer(int piid) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6518 | mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6519 | } |
| 6520 | |
| 6521 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6522 | // Audio policy proxy |
| 6523 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6524 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6525 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 6526 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6527 | */ |
| 6528 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6529 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6530 | IAudioPolicyCallback mPolicyCallback; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6531 | boolean mHasFocusListener; |
| 6532 | /** |
| 6533 | * Audio focus ducking behavior for an audio policy. |
| 6534 | * This variable reflects the value that was successfully set in |
| 6535 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 6536 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 6537 | * is handling ducking for audio focus. |
| 6538 | */ |
| 6539 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 6540 | |
| 6541 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 6542 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6543 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6544 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6545 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6546 | mHasFocusListener = hasFocusListener; |
| 6547 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6548 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6549 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6550 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | public void binderDied() { |
| 6554 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6555 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6556 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6557 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6558 | } |
| 6559 | } |
| 6560 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6561 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6562 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6563 | } |
| 6564 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6565 | void release() { |
| 6566 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6567 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 6568 | } |
| 6569 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6570 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6571 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6572 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6573 | } |
| 6574 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6575 | void connectMixes() { |
| 6576 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6577 | } |
| 6578 | }; |
| 6579 | |
| 6580 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 6581 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6582 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 6583 | } |