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 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 19 | import com.android.server.audio.AudioServiceEvents.ForceUseEvent; |
| 20 | import com.android.server.audio.AudioServiceEvents.PhoneStateEvent; |
| 21 | import com.android.server.audio.AudioServiceEvents.VolumeEvent; |
| 22 | import com.android.server.audio.AudioServiceEvents.WiredDevConnectEvent; |
| 23 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 24 | import static android.Manifest.permission.REMOTE_AUDIO_PLAYBACK; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 25 | import static android.media.AudioManager.RINGER_MODE_NORMAL; |
| 26 | import static android.media.AudioManager.RINGER_MODE_SILENT; |
| 27 | import static android.media.AudioManager.RINGER_MODE_VIBRATE; |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 28 | import static android.os.Process.FIRST_APPLICATION_UID; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 29 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 30 | import android.Manifest; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 31 | import android.annotation.Nullable; |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 32 | import android.app.ActivityManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 33 | import android.app.ActivityManagerInternal; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 34 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 35 | import android.app.AppOpsManager; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 36 | import android.app.IUidObserver; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 37 | import android.app.NotificationManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 38 | import android.bluetooth.BluetoothA2dp; |
| 39 | import android.bluetooth.BluetoothAdapter; |
| 40 | import android.bluetooth.BluetoothClass; |
| 41 | import android.bluetooth.BluetoothDevice; |
| 42 | import android.bluetooth.BluetoothHeadset; |
| 43 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 44 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 45 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.ContentResolver; |
| 47 | import android.content.Context; |
| 48 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 49 | import android.content.IntentFilter; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 50 | import android.content.pm.ApplicationInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 51 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.content.pm.PackageManager; |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 53 | import android.content.pm.ResolveInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 54 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 55 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 56 | import android.content.res.Resources; |
| 57 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 58 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 59 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 60 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 61 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 62 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 63 | import android.media.AudioAttributes; |
| 64 | import android.media.AudioDevicePort; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 65 | import android.media.AudioFocusInfo; |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 66 | import android.media.AudioFocusRequest; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 67 | import android.media.AudioSystem; |
| 68 | import android.media.AudioFormat; |
| 69 | import android.media.AudioManager; |
| 70 | import android.media.AudioManagerInternal; |
| 71 | import android.media.AudioPort; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 72 | import android.media.AudioPlaybackConfiguration; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 73 | import android.media.AudioRecordingConfiguration; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 74 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 75 | import android.media.IAudioFocusDispatcher; |
| 76 | import android.media.IAudioRoutesObserver; |
| 77 | import android.media.IAudioService; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 78 | import android.media.IPlaybackConfigDispatcher; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 79 | import android.media.IRecordingConfigDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 80 | import android.media.IRingtonePlayer; |
| 81 | import android.media.IVolumeController; |
| 82 | import android.media.MediaPlayer; |
| 83 | import android.media.SoundPool; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 84 | import android.media.VolumePolicy; |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 85 | import android.media.audiofx.AudioEffect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.media.MediaPlayer.OnCompletionListener; |
| 87 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 88 | import android.media.PlayerBase; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 89 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 90 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 91 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 92 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 94 | import android.os.Build; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 95 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.Environment; |
| 97 | import android.os.Handler; |
| 98 | import android.os.IBinder; |
| 99 | import android.os.Looper; |
| 100 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 101 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 102 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 104 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 105 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 106 | import android.os.UserHandle; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 107 | import android.os.UserManager; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 108 | import android.os.UserManagerInternal; |
| 109 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 110 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.provider.Settings; |
| 112 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 113 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 114 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 115 | import android.util.AndroidRuntimeException; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 116 | import android.util.ArrayMap; |
| 117 | import android.util.ArraySet; |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 118 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 120 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 121 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 122 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 123 | import android.view.KeyEvent; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 124 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 126 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 127 | import com.android.internal.util.DumpUtils; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 128 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 129 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 130 | import com.android.server.LocalServices; |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 131 | import com.android.server.SystemService; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 132 | import com.android.server.pm.UserManagerService; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 133 | |
| 134 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 136 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 138 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 139 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import java.util.ArrayList; |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 141 | import java.util.Arrays; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 142 | import java.util.HashMap; |
| 143 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 144 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 145 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 146 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * The implementation of the volume manager service. |
| 150 | * <p> |
| 151 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 152 | * asynchronous to external calls. For example, the task of setting a volume |
| 153 | * will update our internal state, but in a separate thread will set the system |
| 154 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 155 | * will update the state and broadcast a change and in a separate thread later |
| 156 | * persist the ringer mode. |
| 157 | * |
| 158 | * @hide |
| 159 | */ |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 160 | public class AudioService extends IAudioService.Stub |
| 161 | implements AccessibilityManager.TouchExplorationStateChangeListener, |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 162 | AccessibilityManager.AccessibilityServicesStateChangeListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | private static final String TAG = "AudioService"; |
| 165 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 166 | /** Debug audio mode */ |
| 167 | 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] | 168 | |
| 169 | /** Debug audio policy feature */ |
| 170 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 171 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 172 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 173 | 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] | 174 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 175 | /** debug calls to devices APIs */ |
| 176 | protected static final boolean DEBUG_DEVICES = Log.isLoggable(TAG + ".DEVICES", Log.DEBUG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 178 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 180 | /** How long to delay after a volume down event before unmuting a stream */ |
| 181 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 182 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 183 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 184 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 185 | */ |
| 186 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 187 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 188 | private final Context mContext; |
| 189 | private final ContentResolver mContentResolver; |
| 190 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 191 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 192 | // the platform type affects volume and silent mode behavior |
| 193 | private final int mPlatformType; |
| 194 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 195 | // indicates whether the system maps all streams to a single stream. |
| 196 | private final boolean mIsSingleVolume; |
| 197 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 198 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 199 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 203 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 204 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 205 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 206 | /** The controller for the volume UI. */ |
| 207 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
| 209 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | /** If the msg is already queued, replace it with this one. */ |
| 211 | private static final int SENDMSG_REPLACE = 0; |
| 212 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 213 | private static final int SENDMSG_NOOP = 1; |
| 214 | /** If the msg is already queued, queue this one and leave the old. */ |
| 215 | private static final int SENDMSG_QUEUE = 2; |
| 216 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 217 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 218 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | private static final int MSG_PERSIST_VOLUME = 1; |
| 220 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 221 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 222 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 223 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 224 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 225 | private static final int MSG_SET_FORCE_USE = 8; |
| 226 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 227 | private static final int MSG_SET_ALL_VOLUMES = 10; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 228 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 229 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 230 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 231 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 232 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 233 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 234 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 235 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 236 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 237 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 238 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 239 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 240 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 241 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 242 | private static final int MSG_ACCESSORY_PLUG_MEDIA_UNMUTE = 27; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 243 | // start of messages handled under wakelock |
| 244 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 245 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 246 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 247 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 248 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 249 | private static final int MSG_A2DP_DEVICE_CONFIG_CHANGE = 103; |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 250 | private static final int MSG_DISABLE_AUDIO_FOR_UID = 104; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 251 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 252 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 253 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 254 | // Timeout for connection to bluetooth headset service |
| 255 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 256 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 257 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 258 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | /** @see AudioSystemThread */ |
| 261 | private AudioSystemThread mAudioSystemThread; |
| 262 | /** @see AudioHandler */ |
| 263 | private AudioHandler mAudioHandler; |
| 264 | /** @see VolumeStreamState */ |
| 265 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 266 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 267 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 268 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 269 | // protects mRingerMode |
| 270 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 273 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
| 276 | /* Sound effect file names */ |
| 277 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 278 | 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] | 279 | |
| 280 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 281 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 282 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 283 | 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] | 284 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 285 | /** Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 286 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 287 | 5, // STREAM_VOICE_CALL |
| 288 | 7, // STREAM_SYSTEM |
| 289 | 7, // STREAM_RING |
| 290 | 15, // STREAM_MUSIC |
| 291 | 7, // STREAM_ALARM |
| 292 | 7, // STREAM_NOTIFICATION |
| 293 | 15, // STREAM_BLUETOOTH_SCO |
| 294 | 7, // STREAM_SYSTEM_ENFORCED |
| 295 | 15, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 296 | 15, // STREAM_TTS |
| 297 | 15 // STREAM_ACCESSIBILITY |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 298 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 299 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 300 | /** Minimum volume index values for audio streams */ |
| 301 | private static int[] MIN_STREAM_VOLUME = new int[] { |
| 302 | 1, // STREAM_VOICE_CALL |
| 303 | 0, // STREAM_SYSTEM |
| 304 | 0, // STREAM_RING |
| 305 | 0, // STREAM_MUSIC |
| 306 | 0, // STREAM_ALARM |
| 307 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 308 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 309 | 0, // STREAM_SYSTEM_ENFORCED |
| 310 | 0, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 311 | 0, // STREAM_TTS |
| 312 | 0 // STREAM_ACCESSIBILITY |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 313 | }; |
| 314 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 315 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 316 | * of another stream: This avoids multiplying the volume settings for hidden |
| 317 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 318 | * NOTE: do not create loops in aliases! |
| 319 | * 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] | 320 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 321 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 322 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 323 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 324 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 325 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 326 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 327 | AudioSystem.STREAM_RING, // STREAM_RING |
| 328 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 329 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 330 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 331 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 332 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 333 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 334 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 335 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 336 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 337 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 338 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 339 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 340 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 341 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 342 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 343 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 344 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 345 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 346 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 347 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 348 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 349 | }; |
| 350 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 351 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 352 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 353 | AudioSystem.STREAM_RING, // STREAM_RING |
| 354 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 355 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 356 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 357 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 358 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 359 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 360 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 361 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 362 | }; |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 363 | protected static int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 364 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 365 | /** |
| 366 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 367 | * after mapping through mStreamVolumeAlias. |
| 368 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 369 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 370 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 371 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 372 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 373 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 374 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 375 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 376 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 377 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 378 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 379 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 380 | AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME, // STREAM_ACCESSIBILITY |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 383 | private final boolean mUseFixedVolume; |
| 384 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 385 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | public void onError(int error) { |
| 387 | switch (error) { |
| 388 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 389 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 390 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | break; |
| 392 | default: |
| 393 | break; |
| 394 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | /** |
| 399 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 400 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 401 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 402 | */ |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 403 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 404 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 405 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 406 | 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] | 407 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 408 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 409 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 410 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 411 | // Streams currently muted by ringer mode |
| 412 | private int mRingerModeMutedStreams; |
| 413 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 414 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 415 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | private int mMuteAffectedStreams; |
| 417 | |
| 418 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 419 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 420 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 421 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | */ |
| 423 | private int mVibrateSetting; |
| 424 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 425 | // Is there a vibrator |
| 426 | private final boolean mHasVibrator; |
| 427 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 428 | // Broadcast receiver for device connections intent broadcasts |
| 429 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 430 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 431 | /** Interface for UserManagerService. */ |
| 432 | private final UserManagerInternal mUserManagerInternal; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 433 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 434 | |
| 435 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 436 | new AudioServiceUserRestrictionsListener(); |
| 437 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 438 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 439 | // Use makeDeviceListKey() to make a unique key for this list. |
| 440 | private class DeviceListSpec { |
| 441 | int mDeviceType; |
| 442 | String mDeviceName; |
| 443 | String mDeviceAddress; |
| 444 | |
| 445 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 446 | mDeviceType = deviceType; |
| 447 | mDeviceName = deviceName; |
| 448 | mDeviceAddress = deviceAddress; |
| 449 | } |
| 450 | |
| 451 | public String toString() { |
| 452 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 453 | + " address:" + mDeviceAddress + "]"; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 458 | // and the "address" associated with a specific instance of that device type |
| 459 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 460 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 461 | } |
| 462 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 463 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 464 | |
| 465 | // Forced device usage for communications |
| 466 | private int mForcedUseForComm; |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 467 | private int mForcedUseForCommExt; // External state returned by getters: always consistent |
| 468 | // with requests by setters |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 469 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 470 | // List of binder death handlers for setMode() client processes. |
| 471 | // 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] | 472 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 473 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 474 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 475 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 476 | |
| 477 | // BluetoothHeadset API to control SCO connection |
| 478 | private BluetoothHeadset mBluetoothHeadset; |
| 479 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 480 | // Bluetooth headset device |
| 481 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 482 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 483 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 484 | // audio service (internal) or bluetooth headset (external) |
| 485 | private int mScoAudioState; |
| 486 | // SCO audio state is not active |
| 487 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 488 | // SCO audio activation request waiting for headset service to connect |
| 489 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 490 | // 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] | 491 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 492 | // SCO audio deactivation request waiting for headset service to connect |
| 493 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 494 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 495 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 496 | // in call audio) |
| 497 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 498 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 499 | // waiting for headset service to connect |
| 500 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 501 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 502 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 503 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 504 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 505 | // SCO audio mode is undefined |
| 506 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 507 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 508 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 509 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 510 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 511 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 512 | private static final int SCO_MODE_VR = 2; |
| 513 | |
| 514 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 515 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 516 | // Current connection state indicated by bluetooth headset |
| 517 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 518 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 519 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 520 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 521 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 522 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 523 | // listener for SoundPool sample load completion indication |
| 524 | private SoundPoolCallback mSoundPoolCallBack; |
| 525 | // thread for SoundPool listener |
| 526 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 527 | // message looper for SoundPool listener |
| 528 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 529 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 530 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 531 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 532 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 533 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 534 | // is controlled by Vol keys. |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 535 | private int mVolumeControlStream = -1; |
| 536 | // interpretation of whether the volume stream has been selected by the user by clicking on a |
| 537 | // volume slider to change which volume is controlled by the volume keys. Is false |
| 538 | // when mVolumeControlStream is -1. |
| 539 | private boolean mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 540 | private final Object mForceControlStreamLock = new Object(); |
| 541 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 542 | // server process so in theory it is not necessary to monitor the client death. |
| 543 | // However it is good to be ready for future evolutions. |
| 544 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 545 | // Used to play ringtones outside system_server |
| 546 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 547 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 548 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
| 549 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 550 | // Request to override default use of A2DP for media. |
Sungsoo | 486f7d3 | 2016-09-28 16:20:52 +0900 | [diff] [blame] | 551 | private boolean mBluetoothA2dpEnabled; |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 552 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 553 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 554 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 555 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 556 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 557 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 558 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 559 | // 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] | 560 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 561 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 562 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 563 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 564 | AudioSystem.DEVICE_OUT_SPDIF | |
| 565 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 566 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 567 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 568 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 569 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 570 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 571 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 572 | private boolean mDockAudioMediaEnabled = true; |
| 573 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 574 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 575 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 576 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 577 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 578 | // and used later when/if disableSafeMediaVolume() is called. |
| 579 | private StreamVolumeCommand mPendingVolumeCommand; |
| 580 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 581 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 582 | |
| 583 | private final MediaFocusControl mMediaFocusControl; |
| 584 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 585 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 586 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 587 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 588 | private final Object mA2dpAvrcpLock = new Object(); |
| 589 | // If absolute volume is supported in AVRCP device |
| 590 | private boolean mAvrcpAbsVolSupported = false; |
| 591 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 592 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 593 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 594 | private NotificationManager mNm; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 595 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 596 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 597 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 598 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 599 | // Array of Uids of valid accessibility services to check if caller is one of them |
| 600 | private int[] mAccessibilityServiceUids; |
| 601 | private final Object mAccessibilityServiceUidsLock = new Object(); |
| 602 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 603 | // Intent "extra" data keys. |
| 604 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 605 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 606 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 607 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 608 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 609 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 610 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 611 | |
| 612 | // Defines the format for the connection "address" for ALSA devices |
| 613 | public static String makeAlsaAddressString(int card, int device) { |
| 614 | return "card=" + card + ";device=" + device + ";"; |
| 615 | } |
| 616 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 617 | public static final class Lifecycle extends SystemService { |
| 618 | private AudioService mService; |
| 619 | |
| 620 | public Lifecycle(Context context) { |
| 621 | super(context); |
| 622 | mService = new AudioService(context); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void onStart() { |
| 627 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void onBootPhase(int phase) { |
| 632 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 633 | mService.systemReady(); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 638 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 639 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
| 640 | } |
| 641 | |
| 642 | @Override public void onUidGone(int uid, boolean disabled) { |
| 643 | // Once the uid is no longer running, no need to keep trying to disable its audio. |
| 644 | disableAudioForUid(false, uid); |
| 645 | } |
| 646 | |
| 647 | @Override public void onUidActive(int uid) throws RemoteException { |
| 648 | } |
| 649 | |
| 650 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 651 | } |
| 652 | |
| 653 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 654 | disableAudioForUid(cached, uid); |
| 655 | } |
| 656 | |
| 657 | private void disableAudioForUid(boolean disable, int uid) { |
| 658 | queueMsgUnderWakeLock(mAudioHandler, MSG_DISABLE_AUDIO_FOR_UID, |
| 659 | disable ? 1 : 0 /* arg1 */, uid /* arg2 */, |
| 660 | null /* obj */, 0 /* delay */); |
| 661 | } |
| 662 | }; |
| 663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | /////////////////////////////////////////////////////////////////////////// |
| 665 | // Construction |
| 666 | /////////////////////////////////////////////////////////////////////////// |
| 667 | |
| 668 | /** @hide */ |
| 669 | public AudioService(Context context) { |
| 670 | mContext = context; |
| 671 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 672 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 673 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 674 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 675 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 676 | mIsSingleVolume = AudioSystem.isSingleVolume(context); |
| 677 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 678 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 679 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 680 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 681 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 682 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 683 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 684 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 685 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 686 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 687 | // Initialize volume |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 688 | int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1); |
| 689 | if (maxCallVolume != -1) { |
| 690 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume; |
| 691 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = |
| 692 | (maxCallVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 693 | } |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 694 | |
| 695 | int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1); |
| 696 | if (maxMusicVolume != -1) { |
| 697 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume; |
| 698 | } |
| 699 | |
| 700 | int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1); |
| 701 | if (defaultMusicVolume != -1 && |
| 702 | defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 703 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume; |
| 704 | } else { |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 705 | if (isPlatformTelevision()) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 706 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 707 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 708 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 709 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 710 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 711 | } |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 712 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 713 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 714 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 715 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 716 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 717 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 720 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 721 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 722 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 723 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 724 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 725 | sendMsg(mAudioHandler, |
| 726 | MSG_SET_FORCE_USE, |
| 727 | SENDMSG_QUEUE, |
| 728 | AudioSystem.FOR_SYSTEM, |
| 729 | cameraSoundForced ? |
| 730 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 731 | new String("AudioService ctor"), |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 732 | 0); |
| 733 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 734 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 735 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 736 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 737 | // The default safe volume index read here will be replaced by the actual value when |
| 738 | // the mcc is read by onConfigureSafeVolume() |
| 739 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 740 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 741 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 742 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 743 | com.android.internal.R.bool.config_useFixedVolume); |
| 744 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 745 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 746 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 747 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 749 | readUserRestrictions(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 750 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 751 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 752 | |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 753 | // mSafeUsbMediaVolumeIndex must be initialized after createStreamStates() because it |
| 754 | // relies on audio policy having correct ranges for volume indexes. |
| 755 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 756 | |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 757 | mPlaybackMonitor = |
| 758 | new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]); |
| 759 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 760 | mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 761 | |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 762 | mRecordMonitor = new RecordingActivityMonitor(mContext); |
| 763 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 764 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 765 | |
| 766 | // Call setRingerModeInt() to apply correct mute |
| 767 | // state on streams affected by ringer mode. |
| 768 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 769 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 770 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 771 | // Register for device connection intent broadcasts. |
| 772 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 773 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 774 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 775 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 776 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 777 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 778 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 779 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 780 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 781 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 782 | intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 783 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 784 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 785 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 786 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 787 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 788 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 789 | // initialize orientation in AudioSystem |
| 790 | setOrientationForAudioSystem(); |
| 791 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 792 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 793 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 794 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 795 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 796 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 797 | intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); |
| 798 | intentFilter.addAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); |
| 799 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 800 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 801 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 802 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 803 | |
| 804 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 805 | |
| 806 | mRecordMonitor.initMonitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 809 | public void systemReady() { |
| 810 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 811 | 0, 0, null, 0); |
Dianne Hackborn | 3e3600e | 2017-08-31 11:15:26 -0700 | [diff] [blame] | 812 | if (false) { |
| 813 | // This is turned off for now, because it is racy and thus causes apps to break. |
| 814 | // Currently banning a uid means that if an app tries to start playing an audio |
| 815 | // stream, that will be preventing, and unbanning it will not allow that stream |
| 816 | // to resume. However these changes in uid state are racy with what the app is doing, |
| 817 | // so that after taking a process out of the cached state we can't guarantee that |
| 818 | // we will unban the uid before the app actually tries to start playing audio. |
| 819 | // (To do that, the activity manager would need to wait until it knows for sure |
| 820 | // that the ban has been removed, before telling the app to do whatever it is |
| 821 | // supposed to do that caused it to go out of the cached state.) |
| 822 | try { |
| 823 | ActivityManager.getService().registerUidObserver(mUidObserver, |
| 824 | ActivityManager.UID_OBSERVER_CACHED | ActivityManager.UID_OBSERVER_GONE, |
| 825 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 826 | } catch (RemoteException e) { |
| 827 | // ignored; both services live in system_server |
| 828 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 829 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | public void onSystemReady() { |
| 833 | mSystemReady = true; |
| 834 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 835 | 0, 0, null, 0); |
| 836 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 837 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 838 | resetBluetoothSco(); |
| 839 | getBluetoothHeadset(); |
| 840 | //FIXME: this is to maintain compatibility with deprecated intent |
| 841 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 842 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 843 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 844 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 845 | sendStickyBroadcastToAll(newIntent); |
| 846 | |
| 847 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 848 | if (adapter != null) { |
| 849 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 850 | BluetoothProfile.A2DP); |
| 851 | } |
| 852 | |
Jeff Sharkey | 73ea0ae | 2016-08-10 17:30:38 -0600 | [diff] [blame] | 853 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) { |
| 854 | mHdmiManager = mContext.getSystemService(HdmiControlManager.class); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 855 | synchronized (mHdmiManager) { |
| 856 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 857 | if (mHdmiTvClient != null) { |
| 858 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 859 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 860 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 861 | mHdmiCecSink = false; |
| 862 | } |
| 863 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 864 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 865 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 866 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 867 | sendMsg(mAudioHandler, |
| 868 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 869 | SENDMSG_REPLACE, |
| 870 | 0, |
| 871 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 872 | TAG, |
Eric Laurent | 03332ab | 2017-02-09 18:29:15 -0800 | [diff] [blame] | 873 | SystemProperties.getBoolean("audio.safemedia.bypass", false) ? |
| 874 | 0 : SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 875 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 876 | initA11yMonitoring(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 877 | onIndicateSystemReady(); |
| 878 | } |
| 879 | |
| 880 | void onIndicateSystemReady() { |
| 881 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 882 | return; |
| 883 | } |
| 884 | sendMsg(mAudioHandler, |
| 885 | MSG_INDICATE_SYSTEM_READY, |
| 886 | SENDMSG_REPLACE, |
| 887 | 0, |
| 888 | 0, |
| 889 | null, |
| 890 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 891 | } |
| 892 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 893 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 894 | if (!mSystemReady || |
| 895 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 896 | Log.e(TAG, "Audioserver died."); |
| 897 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 898 | null, 500); |
| 899 | return; |
| 900 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 901 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 902 | |
| 903 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 904 | // server crash |
| 905 | // Note that we only execute this when the media server |
| 906 | // process restarts after a crash, not the first time it is started. |
| 907 | AudioSystem.setParameters("restarting=true"); |
| 908 | |
| 909 | readAndSetLowRamDevice(); |
| 910 | |
| 911 | // Restore device connection states |
| 912 | synchronized (mConnectedDevices) { |
| 913 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 914 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 915 | AudioSystem.setDeviceConnectionState( |
| 916 | spec.mDeviceType, |
| 917 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 918 | spec.mDeviceAddress, |
| 919 | spec.mDeviceName); |
| 920 | } |
| 921 | } |
| 922 | // Restore call state |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 923 | if (AudioSystem.setPhoneState(mMode) == AudioSystem.AUDIO_STATUS_OK) { |
| 924 | mModeLogger.log(new AudioEventLogger.StringEvent( |
| 925 | "onAudioServerDied causes setPhoneState(" + AudioSystem.modeToString(mMode) + ")")); |
| 926 | } |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 927 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 928 | // Restore forced usage for communications and record |
| 929 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, |
| 930 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 931 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 932 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_RECORD, mForcedUseForComm, |
| 933 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 934 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 935 | final int forSys; |
| 936 | synchronized (mSettingsLock) { |
| 937 | forSys = mCameraSoundForced ? |
| 938 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE; |
| 939 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 940 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_SYSTEM, forSys, |
| 941 | "onAudioServerDied")); |
| 942 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, forSys); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 943 | |
| 944 | // Restore stream volumes |
| 945 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 946 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 947 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 948 | AudioSystem.initStreamVolume( |
| 949 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 950 | |
| 951 | streamState.applyAllVolumes(); |
| 952 | } |
| 953 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 954 | // Restore mono mode |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 955 | updateMasterMono(mContentResolver); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 956 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 957 | // Restore ringer mode |
| 958 | setRingerModeInt(getRingerModeInternal(), false); |
| 959 | |
| 960 | // Reset device orientation (if monitored for this device) |
| 961 | if (mMonitorOrientation) { |
| 962 | setOrientationForAudioSystem(); |
| 963 | } |
| 964 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 965 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 968 | synchronized (mBluetoothA2dpEnabledLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 969 | final int forMed = mBluetoothA2dpEnabled ? |
| 970 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP; |
| 971 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_MEDIA, forMed, |
| 972 | "onAudioServerDied")); |
| 973 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, forMed); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 974 | } |
| 975 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 976 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 977 | final int forDock = mDockAudioMediaEnabled ? |
| 978 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE; |
| 979 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, forDock, |
| 980 | "onAudioServerDied")); |
| 981 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, forDock); |
| 982 | sendEncodedSurroundMode(mContentResolver, "onAudioServerDied"); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 983 | } |
| 984 | if (mHdmiManager != null) { |
| 985 | synchronized (mHdmiManager) { |
| 986 | if (mHdmiTvClient != null) { |
| 987 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | synchronized (mAudioPolicies) { |
| 993 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 994 | policy.connectMixes(); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | onIndicateSystemReady(); |
| 999 | // indicate the end of reconfiguration phase to audio HAL |
| 1000 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | private void createAudioSystemThread() { |
| 1004 | mAudioSystemThread = new AudioSystemThread(); |
| 1005 | mAudioSystemThread.start(); |
| 1006 | waitForAudioHandlerCreation(); |
| 1007 | } |
| 1008 | |
| 1009 | /** Waits for the volume handler to be created by the other thread. */ |
| 1010 | private void waitForAudioHandlerCreation() { |
| 1011 | synchronized(this) { |
| 1012 | while (mAudioHandler == null) { |
| 1013 | try { |
| 1014 | // Wait for mAudioHandler to be set by the other thread |
| 1015 | wait(); |
| 1016 | } catch (InterruptedException e) { |
| 1017 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1023 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1024 | synchronized (VolumeStreamState.class) { |
| 1025 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1026 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1027 | mStreamStates[streamType] |
| 1028 | .setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1029 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1030 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1031 | mStreamStates[streamType].applyAllVolumes(); |
| 1032 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1037 | private void checkAllFixedVolumeDevices() |
| 1038 | { |
| 1039 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1040 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1041 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1045 | private void checkAllFixedVolumeDevices(int streamType) { |
| 1046 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1047 | } |
| 1048 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1049 | private void checkMuteAffectedStreams() { |
| 1050 | // any stream with a min level > 0 is not muteable by definition |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1051 | // STREAM_VOICE_CALL can be muted by applications that has the the MODIFY_PHONE_STATE permission. |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1052 | for (int i = 0; i < mStreamStates.length; i++) { |
| 1053 | final VolumeStreamState vss = mStreamStates[i]; |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1054 | if (vss.mIndexMin > 0 && |
| 1055 | vss.mStreamType != AudioSystem.STREAM_VOICE_CALL) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1056 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1063 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 1064 | |
| 1065 | for (int i = 0; i < numStreamTypes; i++) { |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 1066 | streams[i] = |
| 1067 | new VolumeStreamState(System.VOLUME_SETTINGS_INT[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1070 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1071 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1072 | checkMuteAffectedStreams(); |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1073 | updateDefaultVolumes(); |
| 1074 | } |
| 1075 | |
| 1076 | // Update default indexes from aliased streams. Must be called after mStreamStates is created |
| 1077 | private void updateDefaultVolumes() { |
| 1078 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1079 | if (stream != mStreamVolumeAlias[stream]) { |
| 1080 | AudioSystem.DEFAULT_STREAM_VOLUME[stream] = rescaleIndex( |
| 1081 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]], |
| 1082 | mStreamVolumeAlias[stream], |
| 1083 | stream); |
| 1084 | } |
| 1085 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1088 | private void dumpStreamStates(PrintWriter pw) { |
| 1089 | pw.println("\nStream volumes (device: index)"); |
| 1090 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1091 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1092 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1093 | mStreamStates[i].dump(pw); |
| 1094 | pw.println(""); |
| 1095 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1096 | pw.print("\n- mute affected streams = 0x"); |
| 1097 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1100 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1101 | int dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1102 | final int a11yStreamAlias = sIndependentA11yVolume ? |
| 1103 | AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1104 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1105 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1106 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 1107 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1108 | } else { |
| 1109 | switch (mPlatformType) { |
| 1110 | case AudioSystem.PLATFORM_VOICE: |
| 1111 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
| 1112 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
| 1113 | break; |
| 1114 | default: |
| 1115 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
| 1116 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 1117 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1118 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1119 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1120 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1121 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1122 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1123 | if (isInCommunication()) { |
| 1124 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 1125 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 1126 | } else { |
| 1127 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 1128 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1129 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1130 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1131 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1132 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY] = a11yStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1133 | |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1134 | if (updateVolumes && mStreamStates != null) { |
| 1135 | updateDefaultVolumes(); |
| 1136 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1137 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], |
| 1138 | caller); |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1139 | |
| 1140 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].mVolumeIndexSettingName = |
| 1141 | System.VOLUME_SETTINGS_INT[a11yStreamAlias]; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1142 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes( |
| 1143 | mStreamStates[a11yStreamAlias], caller); |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1144 | if (sIndependentA11yVolume) { |
| 1145 | // restore the a11y values from the settings |
| 1146 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].readSettings(); |
| 1147 | } |
| 1148 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1149 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1150 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1151 | sendMsg(mAudioHandler, |
| 1152 | MSG_SET_ALL_VOLUMES, |
| 1153 | SENDMSG_QUEUE, |
| 1154 | 0, |
| 1155 | 0, |
| 1156 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1157 | sendMsg(mAudioHandler, |
| 1158 | MSG_SET_ALL_VOLUMES, |
| 1159 | SENDMSG_QUEUE, |
| 1160 | 0, |
| 1161 | 0, |
| 1162 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY], 0); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1166 | private void readDockAudioSettings(ContentResolver cr) |
| 1167 | { |
| 1168 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1169 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1170 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1171 | sendMsg(mAudioHandler, |
| 1172 | MSG_SET_FORCE_USE, |
| 1173 | SENDMSG_QUEUE, |
| 1174 | AudioSystem.FOR_DOCK, |
| 1175 | mDockAudioMediaEnabled ? |
| 1176 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1177 | new String("readDockAudioSettings"), |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1178 | 0); |
| 1179 | } |
| 1180 | |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1181 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1182 | private void updateMasterMono(ContentResolver cr) |
| 1183 | { |
| 1184 | final boolean masterMono = System.getIntForUser( |
| 1185 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1186 | if (DEBUG_VOL) { |
| 1187 | Log.d(TAG, String.format("Master mono %b", masterMono)); |
| 1188 | } |
| 1189 | AudioSystem.setMasterMono(masterMono); |
| 1190 | } |
| 1191 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1192 | private void sendEncodedSurroundMode(ContentResolver cr, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1193 | { |
| 1194 | int encodedSurroundMode = Settings.Global.getInt( |
| 1195 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 1196 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1197 | sendEncodedSurroundMode(encodedSurroundMode, eventSource); |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1200 | private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1201 | { |
| 1202 | // initialize to guaranteed bad value |
| 1203 | int forceSetting = AudioSystem.NUM_FORCE_CONFIG; |
| 1204 | switch (encodedSurroundMode) { |
| 1205 | case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO: |
| 1206 | forceSetting = AudioSystem.FORCE_NONE; |
| 1207 | break; |
| 1208 | case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER: |
| 1209 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_NEVER; |
| 1210 | break; |
| 1211 | case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS: |
| 1212 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_ALWAYS; |
| 1213 | break; |
| 1214 | default: |
| 1215 | Log.e(TAG, "updateSurroundSoundSettings: illegal value " |
| 1216 | + encodedSurroundMode); |
| 1217 | break; |
| 1218 | } |
| 1219 | if (forceSetting != AudioSystem.NUM_FORCE_CONFIG) { |
| 1220 | sendMsg(mAudioHandler, |
| 1221 | MSG_SET_FORCE_USE, |
| 1222 | SENDMSG_QUEUE, |
| 1223 | AudioSystem.FOR_ENCODED_SURROUND, |
| 1224 | forceSetting, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1225 | eventSource, |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1226 | 0); |
| 1227 | } |
| 1228 | } |
| 1229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | private void readPersistedSettings() { |
| 1231 | final ContentResolver cr = mContentResolver; |
| 1232 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1233 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1234 | Settings.Global.getInt( |
| 1235 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1236 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1237 | // sanity check in case the settings are restored from a device with incompatible |
| 1238 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1239 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1240 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1241 | } |
| 1242 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1243 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1244 | } |
| 1245 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1246 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1247 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1248 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1249 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1250 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1251 | synchronized(mSettingsLock) { |
| 1252 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1253 | if (mRingerModeExternal == -1) { |
| 1254 | mRingerModeExternal = mRingerMode; |
| 1255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1257 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1258 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1259 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1260 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1261 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1262 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1263 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1264 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1265 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1266 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1267 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1269 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1270 | readDockAudioSettings(cr); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1271 | sendEncodedSurroundMode(cr, "readPersistedSettings"); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1272 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1273 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1274 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1275 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1276 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1278 | updateMasterMono(cr); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | // Each stream will read its own persisted settings |
| 1281 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1282 | // Broadcast the sticky intents |
| 1283 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1284 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | |
| 1286 | // Broadcast vibrate settings |
| 1287 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1288 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1289 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1290 | // Load settings for the volume controller |
| 1291 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1294 | private void readUserRestrictions() { |
| 1295 | final int currentUser = getCurrentUserId(); |
| 1296 | |
| 1297 | // Check the current user restriction. |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1298 | boolean masterMute = |
| 1299 | mUserManagerInternal.getUserRestriction(currentUser, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 1300 | UserManager.DISALLOW_UNMUTE_DEVICE) |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1301 | || mUserManagerInternal.getUserRestriction(currentUser, |
| 1302 | UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1303 | if (mUseFixedVolume) { |
| 1304 | masterMute = false; |
| 1305 | AudioSystem.setMasterVolume(1.0f); |
| 1306 | } |
| 1307 | if (DEBUG_VOL) { |
| 1308 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1309 | } |
| 1310 | setSystemAudioMute(masterMute); |
| 1311 | AudioSystem.setMasterMute(masterMute); |
| 1312 | broadcastMasterMuteStatus(masterMute); |
| 1313 | |
| 1314 | boolean microphoneMute = mUserManagerInternal.getUserRestriction( |
| 1315 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1316 | if (DEBUG_VOL) { |
| 1317 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1318 | } |
| 1319 | AudioSystem.muteMicrophone(microphoneMute); |
| 1320 | } |
| 1321 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1322 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 1323 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 1324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | |
| 1326 | /////////////////////////////////////////////////////////////////////////// |
| 1327 | // IPC methods |
| 1328 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1330 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1331 | String callingPackage, String caller) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1332 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1333 | caller, Binder.getCallingUid()); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1337 | String callingPackage, String caller, int uid) { |
| 1338 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1339 | + ", flags=" + flags + ", caller=" + caller |
| 1340 | + ", volControlStream=" + mVolumeControlStream |
| 1341 | + ", userSelect=" + mUserSelectedVolumeControlStream); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1342 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_SUGG_VOL, suggestedStreamType, |
| 1343 | direction/*val1*/, flags/*val2*/, new StringBuilder(callingPackage) |
| 1344 | .append("/").append(caller).append(" uid:").append(uid).toString())); |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1345 | final int streamType; |
| 1346 | if (mUserSelectedVolumeControlStream) { // implies mVolumeControlStream != -1 |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1347 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1348 | } else { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1349 | final int maybeActiveStreamType = getActiveStreamType(suggestedStreamType); |
| 1350 | final boolean activeForReal; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 1351 | if (maybeActiveStreamType == AudioSystem.STREAM_RING |
| 1352 | || maybeActiveStreamType == AudioSystem.STREAM_NOTIFICATION) { |
| 1353 | activeForReal = wasStreamActiveRecently(maybeActiveStreamType, 0); |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1354 | } else { |
| 1355 | activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, 0); |
| 1356 | } |
| 1357 | if (activeForReal || mVolumeControlStream == -1) { |
| 1358 | streamType = maybeActiveStreamType; |
| 1359 | } else { |
| 1360 | streamType = mVolumeControlStream; |
| 1361 | } |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1362 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1363 | |
| 1364 | final boolean isMute = isMuteAdjust(direction); |
| 1365 | |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1366 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1367 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1369 | // Play sounds on STREAM_RING only. |
| 1370 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1371 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1373 | } |
| 1374 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1375 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1376 | // Don't suppress mute/unmute requests |
| 1377 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1378 | direction = 0; |
| 1379 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1380 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1381 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1382 | } |
| 1383 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1384 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1388 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1389 | String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1390 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1391 | Log.w(TAG, "Trying to call adjustStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1392 | + "CHANGE_ACCESSIBILITY_VOLUME / callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1393 | return; |
| 1394 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1395 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_STREAM_VOL, streamType, |
| 1396 | direction/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1397 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1398 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | private void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1402 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1403 | if (mUseFixedVolume) { |
| 1404 | return; |
| 1405 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1406 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1407 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | ensureValidDirection(direction); |
| 1410 | ensureValidStreamType(streamType); |
| 1411 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1412 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1413 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1414 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1415 | return; |
| 1416 | } |
| 1417 | |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1418 | // If adjust is mute and the stream is STREAM_VOICE_CALL, make sure |
| 1419 | // that the calling app have the MODIFY_PHONE_STATE permission. |
| 1420 | if (isMuteAdjust && |
| 1421 | streamType == AudioSystem.STREAM_VOICE_CALL && |
| 1422 | mContext.checkCallingOrSelfPermission( |
| 1423 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1424 | != PackageManager.PERMISSION_GRANTED) { |
| 1425 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: adjustStreamVolume from pid=" |
| 1426 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1427 | return; |
| 1428 | } |
| 1429 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1430 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1431 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1432 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1433 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1434 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1435 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1436 | |
| 1437 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1438 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1439 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1441 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1442 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1443 | // skip a2dp absolute volume control request when the device |
| 1444 | // is not an a2dp device |
| 1445 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1446 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1447 | return; |
| 1448 | } |
| 1449 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1450 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1451 | // so we handle user restrictions correctly. |
| 1452 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1453 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1454 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1455 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1456 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1460 | // reset any pending volume command |
| 1461 | synchronized (mSafeMediaVolumeState) { |
| 1462 | mPendingVolumeCommand = null; |
| 1463 | } |
| 1464 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1465 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1466 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1467 | ((device & mFixedVolumeDevices) != 0)) { |
| 1468 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1469 | |
| 1470 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1471 | // volume is enforced, and max and 0 for the others. |
| 1472 | // This is simulated by stepping by the full allowed volume range |
| 1473 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1474 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1475 | step = safeMediaVolumeIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1476 | } else { |
| 1477 | step = streamState.getMaxIndex(); |
| 1478 | } |
| 1479 | if (aliasIndex != 0) { |
| 1480 | aliasIndex = step; |
| 1481 | } |
| 1482 | } else { |
| 1483 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1484 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1485 | } |
| 1486 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1487 | // If either the client forces allowing ringer modes for this adjustment, |
| 1488 | // or the stream type is one that is affected by ringer modes |
| 1489 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1490 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1491 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1492 | // do not vibrate if already in vibrate mode |
| 1493 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1494 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1495 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1496 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1497 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1498 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1499 | streamState.mIsMuted, callingPackage, flags); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1500 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1501 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1502 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1503 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1504 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1505 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1506 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1507 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1508 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1509 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1510 | // If the ringermode is suppressing media, prevent changes |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1511 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1512 | adjustVolume = false; |
| 1513 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1514 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1515 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1516 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1517 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1518 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1519 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1520 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1521 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1522 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1523 | synchronized (mA2dpAvrcpLock) { |
| 1524 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1525 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1526 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1527 | } |
| 1528 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1529 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1530 | if (isMuteAdjust) { |
| 1531 | boolean state; |
| 1532 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1533 | state = !streamState.mIsMuted; |
| 1534 | } else { |
| 1535 | state = direction == AudioManager.ADJUST_MUTE; |
| 1536 | } |
| 1537 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1538 | setSystemAudioMute(state); |
| 1539 | } |
| 1540 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1541 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1542 | if (!(readCameraSoundForced() |
| 1543 | && (mStreamStates[stream].getStreamType() |
| 1544 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1545 | mStreamStates[stream].mute(state); |
| 1546 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1550 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1551 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1552 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1553 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1554 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1555 | // Post message to set system volume (it in turn will post a |
| 1556 | // message to persist). |
| 1557 | if (streamState.mIsMuted) { |
| 1558 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1559 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1560 | // unmute immediately for volume up |
| 1561 | streamState.mute(false); |
| 1562 | } else if (direction == AudioManager.ADJUST_LOWER) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1563 | if (mIsSingleVolume) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1564 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1565 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1566 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1567 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1568 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1569 | sendMsg(mAudioHandler, |
| 1570 | MSG_SET_DEVICE_VOLUME, |
| 1571 | SENDMSG_QUEUE, |
| 1572 | device, |
| 1573 | 0, |
| 1574 | streamState, |
| 1575 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1576 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1577 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1578 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1579 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1580 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1581 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1582 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1583 | if (mHdmiManager != null) { |
| 1584 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1585 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1586 | if (mHdmiCecSink && |
| 1587 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1588 | oldIndex != newIndex) { |
| 1589 | synchronized (mHdmiPlaybackClient) { |
| 1590 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1591 | KeyEvent.KEYCODE_VOLUME_UP; |
Donghyun Cho | 5f6d404e | 2016-03-17 20:39:25 +0900 | [diff] [blame] | 1592 | final long ident = Binder.clearCallingIdentity(); |
| 1593 | try { |
| 1594 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1595 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1596 | } finally { |
| 1597 | Binder.restoreCallingIdentity(ident); |
| 1598 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1599 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1603 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1604 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1605 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1608 | // Called after a delay when volume down is pressed while muted |
| 1609 | private void onUnmuteStream(int stream, int flags) { |
| 1610 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1611 | streamState.mute(false); |
| 1612 | |
| 1613 | final int device = getDeviceForStream(stream); |
| 1614 | final int index = mStreamStates[stream].getIndex(device); |
| 1615 | sendVolumeUpdate(stream, index, index, flags); |
| 1616 | } |
| 1617 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1618 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1619 | if (mHdmiManager == null |
| 1620 | || mHdmiTvClient == null |
| 1621 | || oldVolume == newVolume |
| 1622 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1623 | |
| 1624 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1625 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1626 | synchronized (mHdmiManager) { |
| 1627 | if (!mHdmiSystemAudioSupported) return; |
| 1628 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1629 | final long token = Binder.clearCallingIdentity(); |
| 1630 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1631 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1632 | } finally { |
| 1633 | Binder.restoreCallingIdentity(token); |
| 1634 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1639 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1640 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1641 | class StreamVolumeCommand { |
| 1642 | public final int mStreamType; |
| 1643 | public final int mIndex; |
| 1644 | public final int mFlags; |
| 1645 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1646 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1647 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1648 | mStreamType = streamType; |
| 1649 | mIndex = index; |
| 1650 | mFlags = flags; |
| 1651 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1652 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1653 | |
| 1654 | @Override |
| 1655 | public String toString() { |
| 1656 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1657 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1658 | .append(mDevice).append('}').toString(); |
| 1659 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1660 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1661 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1662 | private int getNewRingerMode(int stream, int index, int flags) { |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 1663 | // setRingerMode does nothing if the device is single volume,so the value would be unchanged |
| 1664 | if (mIsSingleVolume) { |
| 1665 | return getRingerModeExternal(); |
| 1666 | } |
| 1667 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1668 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1669 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1670 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1671 | int newRingerMode; |
| 1672 | if (index == 0) { |
| 1673 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1674 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1675 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1676 | } else { |
| 1677 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1678 | } |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1679 | return newRingerMode; |
| 1680 | } |
| 1681 | return getRingerModeExternal(); |
| 1682 | } |
| 1683 | |
| 1684 | private boolean isAndroidNPlus(String caller) { |
| 1685 | try { |
| 1686 | final ApplicationInfo applicationInfo = |
| 1687 | mContext.getPackageManager().getApplicationInfoAsUser( |
| 1688 | caller, 0, UserHandle.getUserId(Binder.getCallingUid())); |
| 1689 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1690 | return true; |
| 1691 | } |
| 1692 | return false; |
| 1693 | } catch (PackageManager.NameNotFoundException e) { |
| 1694 | return true; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | private boolean wouldToggleZenMode(int newMode) { |
| 1699 | if (getRingerModeExternal() == AudioManager.RINGER_MODE_SILENT |
| 1700 | && newMode != AudioManager.RINGER_MODE_SILENT) { |
| 1701 | return true; |
| 1702 | } else if (getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT |
| 1703 | && newMode == AudioManager.RINGER_MODE_SILENT) { |
| 1704 | return true; |
| 1705 | } |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
| 1709 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1710 | String caller) { |
| 1711 | final int stream = mStreamVolumeAlias[streamType]; |
| 1712 | setStreamVolumeInt(stream, index, device, false, caller); |
| 1713 | // setting volume on ui sounds stream type also controls silent mode |
| 1714 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1715 | (stream == getUiSoundsStreamType())) { |
| 1716 | setRingerMode(getNewRingerMode(stream, index, flags), |
| 1717 | TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1718 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1719 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1720 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1724 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1725 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1726 | Log.w(TAG, "Trying to call setStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1727 | + " CHANGE_ACCESSIBILITY_VOLUME callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1728 | return; |
| 1729 | } |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1730 | if ((streamType == AudioManager.STREAM_VOICE_CALL) && |
| 1731 | (index == 0) && |
| 1732 | (mContext.checkCallingOrSelfPermission( |
| 1733 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1734 | != PackageManager.PERMISSION_GRANTED)) { |
| 1735 | Log.w(TAG, "Trying to call setStreamVolume() for STREAM_VOICE_CALL and index 0 without" |
| 1736 | + " MODIFY_PHONE_STATE callingPackage=" + callingPackage); |
| 1737 | return; |
| 1738 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1739 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType, |
| 1740 | index/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1741 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1742 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1745 | private boolean canChangeAccessibilityVolume() { |
| 1746 | synchronized (mAccessibilityServiceUidsLock) { |
| 1747 | if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 1748 | android.Manifest.permission.CHANGE_ACCESSIBILITY_VOLUME)) { |
| 1749 | return true; |
| 1750 | } |
| 1751 | if (mAccessibilityServiceUids != null) { |
| 1752 | int callingUid = Binder.getCallingUid(); |
| 1753 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 1754 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 1755 | return true; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | return false; |
| 1760 | } |
| 1761 | } |
| 1762 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1763 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1764 | String caller, int uid) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1765 | if (DEBUG_VOL) { |
| 1766 | Log.d(TAG, "setStreamVolume(stream=" + streamType+", index=" + index |
| 1767 | + ", calling=" + callingPackage + ")"); |
| 1768 | } |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1769 | if (mUseFixedVolume) { |
| 1770 | return; |
| 1771 | } |
| 1772 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1773 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1774 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1775 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1776 | |
| 1777 | final int device = getDeviceForStream(streamType); |
| 1778 | int oldIndex; |
| 1779 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1780 | // skip a2dp absolute volume control request when the device |
| 1781 | // is not an a2dp device |
| 1782 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1783 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1784 | return; |
| 1785 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1786 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1787 | // so we handle user restrictions correctly. |
| 1788 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1789 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1790 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1791 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1792 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1793 | return; |
| 1794 | } |
| 1795 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1796 | if (isAndroidNPlus(callingPackage) |
| 1797 | && wouldToggleZenMode(getNewRingerMode(streamTypeAlias, index, flags)) |
| 1798 | && !mNm.isNotificationPolicyAccessGrantedForPackage(callingPackage)) { |
| 1799 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 1800 | } |
| 1801 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1802 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
| 1803 | return; |
| 1804 | } |
| 1805 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1806 | synchronized (mSafeMediaVolumeState) { |
| 1807 | // reset any pending volume command |
| 1808 | mPendingVolumeCommand = null; |
| 1809 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1810 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1811 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1812 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1813 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1814 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1815 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1816 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1817 | synchronized (mA2dpAvrcpLock) { |
| 1818 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1819 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1820 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1824 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1825 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1826 | } |
| 1827 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1828 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1829 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1830 | ((device & mFixedVolumeDevices) != 0)) { |
| 1831 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1832 | |
| 1833 | // volume is either 0 or max allowed for fixed volume devices |
| 1834 | if (index != 0) { |
| 1835 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1836 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1837 | index = safeMediaVolumeIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1838 | } else { |
| 1839 | index = streamState.getMaxIndex(); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1844 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1845 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1846 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1847 | streamType, index, flags, device); |
| 1848 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1849 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1850 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1851 | } |
| 1852 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1853 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1856 | // No ringer affected streams can be changed in total silence mode except those that |
| 1857 | // will cause the device to exit total silence mode. |
| 1858 | private boolean volumeAdjustmentAllowedByDnd(int streamTypeAlias, int flags) { |
| 1859 | if (mNm.getZenMode() == Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 1860 | && isStreamMutedByRingerMode(streamTypeAlias)) { |
| 1861 | if (!(((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1862 | (streamTypeAlias == getUiSoundsStreamType()))) { |
| 1863 | return false; |
| 1864 | } |
| 1865 | } |
| 1866 | return true; |
| 1867 | } |
| 1868 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1869 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1870 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1871 | if (DEBUG_VOL) { Log.d(TAG, String.format("forceVolumeControlStream(%d)", streamType)); } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1872 | synchronized(mForceControlStreamLock) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1873 | if (mVolumeControlStream != -1 && streamType != -1) { |
| 1874 | mUserSelectedVolumeControlStream = true; |
| 1875 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1876 | mVolumeControlStream = streamType; |
| 1877 | if (mVolumeControlStream == -1) { |
| 1878 | if (mForceControlStreamClient != null) { |
| 1879 | mForceControlStreamClient.release(); |
| 1880 | mForceControlStreamClient = null; |
| 1881 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1882 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1883 | } else { |
| 1884 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1890 | private IBinder mCb; // To be notified of client's death |
| 1891 | |
| 1892 | ForceControlStreamClient(IBinder cb) { |
| 1893 | if (cb != null) { |
| 1894 | try { |
| 1895 | cb.linkToDeath(this, 0); |
| 1896 | } catch (RemoteException e) { |
| 1897 | // Client has died! |
| 1898 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1899 | cb = null; |
| 1900 | } |
| 1901 | } |
| 1902 | mCb = cb; |
| 1903 | } |
| 1904 | |
| 1905 | public void binderDied() { |
| 1906 | synchronized(mForceControlStreamLock) { |
| 1907 | Log.w(TAG, "SCO client died"); |
| 1908 | if (mForceControlStreamClient != this) { |
| 1909 | Log.w(TAG, "unregistered control stream client died"); |
| 1910 | } else { |
| 1911 | mForceControlStreamClient = null; |
| 1912 | mVolumeControlStream = -1; |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1913 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | public void release() { |
| 1919 | if (mCb != null) { |
| 1920 | mCb.unlinkToDeath(this, 0); |
| 1921 | mCb = null; |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1926 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1927 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1928 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1929 | final long ident = Binder.clearCallingIdentity(); |
| 1930 | try { |
| 1931 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1932 | } finally { |
| 1933 | Binder.restoreCallingIdentity(ident); |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1938 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1939 | final long ident = Binder.clearCallingIdentity(); |
| 1940 | try { |
| 1941 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1942 | } finally { |
| 1943 | Binder.restoreCallingIdentity(ident); |
| 1944 | } |
| 1945 | } |
| 1946 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1947 | private int getCurrentUserId() { |
| 1948 | final long ident = Binder.clearCallingIdentity(); |
| 1949 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1950 | UserInfo currentUser = ActivityManager.getService().getCurrentUser(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1951 | return currentUser.id; |
| 1952 | } catch (RemoteException e) { |
| 1953 | // Activity manager not running, nothing we can do assume user 0. |
| 1954 | } finally { |
| 1955 | Binder.restoreCallingIdentity(ident); |
| 1956 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 1957 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1960 | // UI update and Broadcast Intent |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 1961 | protected void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 1962 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1963 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1964 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1965 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1966 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1967 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | } |
| 1969 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1970 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1971 | // receives volume notification from Audio Receiver. |
| 1972 | private int updateFlagsForSystemAudio(int flags) { |
| 1973 | if (mHdmiTvClient != null) { |
| 1974 | synchronized (mHdmiTvClient) { |
| 1975 | if (mHdmiSystemAudioSupported && |
| 1976 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1977 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | return flags; |
| 1982 | } |
| 1983 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1984 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1985 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1986 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1987 | broadcastMasterMuteStatus(muted); |
| 1988 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1989 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1990 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1991 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1992 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1993 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1994 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1995 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1996 | } |
| 1997 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | * Sets the stream state's index, and posts a message to set system volume. |
| 2000 | * This will not call out to the UI. Assumes a valid stream type. |
| 2001 | * |
| 2002 | * @param streamType Type of the stream |
| 2003 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2004 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2005 | * @param force If true, set the volume even if the desired volume is same |
| 2006 | * as the current volume. |
| 2007 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2008 | private void setStreamVolumeInt(int streamType, |
| 2009 | int index, |
| 2010 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2011 | boolean force, |
| 2012 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2014 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2015 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2016 | // Post message to set system volume (it in turn will post a message |
| 2017 | // to persist). |
| 2018 | sendMsg(mAudioHandler, |
| 2019 | MSG_SET_DEVICE_VOLUME, |
| 2020 | SENDMSG_QUEUE, |
| 2021 | device, |
| 2022 | 0, |
| 2023 | streamState, |
| 2024 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2028 | private void setSystemAudioMute(boolean state) { |
| 2029 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 2030 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 2031 | if (!mHdmiSystemAudioSupported) return; |
| 2032 | synchronized (mHdmiTvClient) { |
| 2033 | final long token = Binder.clearCallingIdentity(); |
| 2034 | try { |
| 2035 | mHdmiTvClient.setSystemAudioMute(state); |
| 2036 | } finally { |
| 2037 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2038 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2043 | /** get stream mute state. */ |
| 2044 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2045 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 2046 | streamType = getActiveStreamType(streamType); |
| 2047 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2048 | synchronized (VolumeStreamState.class) { |
jiabin | 93d32ef | 2017-07-11 13:50:02 -0700 | [diff] [blame] | 2049 | ensureValidStreamType(streamType); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2050 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2051 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 2054 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 2055 | private IBinder mICallback; // To be notified of client's death |
| 2056 | |
| 2057 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 2058 | mICallback = cb; |
| 2059 | try { |
| 2060 | cb.linkToDeath(this, 0/*flags*/); |
| 2061 | } catch (RemoteException e) { |
| 2062 | Log.e(TAG, "can't link to death", e); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | boolean isHandlerFor(IBinder cb) { |
| 2067 | return mICallback.equals(cb); |
| 2068 | } |
| 2069 | |
| 2070 | void forget() { |
| 2071 | try { |
| 2072 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 2073 | } catch (NoSuchElementException e) { |
| 2074 | Log.e(TAG, "error unlinking to death", e); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | public void binderDied() { |
| 2079 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 2080 | forceRemoteSubmixFullVolume(false, mICallback); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | /** |
| 2085 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 2086 | * @return true if there is a registered death handler, false otherwise */ |
| 2087 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2088 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2089 | while (it.hasNext()) { |
| 2090 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 2091 | if (handler.isHandlerFor(cb)) { |
| 2092 | handler.forget(); |
| 2093 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 2094 | return true; |
| 2095 | } |
| 2096 | } |
| 2097 | return false; |
| 2098 | } |
| 2099 | |
| 2100 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 2101 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2102 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2103 | while (it.hasNext()) { |
| 2104 | if (it.next().isHandlerFor(cb)) { |
| 2105 | return true; |
| 2106 | } |
| 2107 | } |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
| 2111 | private int mRmtSbmxFullVolRefCount = 0; |
| 2112 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 2113 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 2114 | |
| 2115 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 2116 | if (cb == null) { |
| 2117 | return; |
| 2118 | } |
| 2119 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 2120 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 2121 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 2122 | return; |
| 2123 | } |
| 2124 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 2125 | boolean applyRequired = false; |
| 2126 | if (startForcing) { |
| 2127 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 2128 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 2129 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2130 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2131 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2132 | applyRequired = true; |
| 2133 | } |
| 2134 | mRmtSbmxFullVolRefCount++; |
| 2135 | } |
| 2136 | } else { |
| 2137 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 2138 | mRmtSbmxFullVolRefCount--; |
| 2139 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2140 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2141 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2142 | applyRequired = true; |
| 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | if (applyRequired) { |
| 2147 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 2148 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 2149 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 2150 | } |
| 2151 | } |
| 2152 | } |
| 2153 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2154 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 2155 | int userId) { |
| 2156 | // If we are being called by the system check for user we are going to change |
| 2157 | // so we handle user restrictions correctly. |
| 2158 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2159 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2160 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2161 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 2162 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2163 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 2164 | return; |
| 2165 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2166 | if (userId != UserHandle.getCallingUserId() && |
| 2167 | mContext.checkCallingOrSelfPermission( |
| 2168 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2169 | != PackageManager.PERMISSION_GRANTED) { |
| 2170 | return; |
| 2171 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2172 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 2173 | } |
| 2174 | |
| 2175 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 2176 | if (DEBUG_VOL) { |
| 2177 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 2178 | } |
| 2179 | if (mUseFixedVolume) { |
| 2180 | return; // If using fixed volume, we don't mute. |
| 2181 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2182 | if (getCurrentUserId() == userId) { |
| 2183 | if (mute != AudioSystem.getMasterMute()) { |
| 2184 | setSystemAudioMute(mute); |
| 2185 | AudioSystem.setMasterMute(mute); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2186 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2187 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2188 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2189 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 2190 | sendBroadcastToAll(intent); |
| 2191 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 2192 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | /** get master mute state. */ |
| 2196 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 2197 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2200 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 2201 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 2202 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2203 | } |
| 2204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2205 | /** @see AudioManager#getStreamVolume(int) */ |
| 2206 | public int getStreamVolume(int streamType) { |
| 2207 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2208 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2209 | synchronized (VolumeStreamState.class) { |
| 2210 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 2211 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2212 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2213 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2214 | index = 0; |
| 2215 | } |
| 2216 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 2217 | (device & mFixedVolumeDevices) != 0) { |
| 2218 | index = mStreamStates[streamType].getMaxIndex(); |
| 2219 | } |
| 2220 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 2225 | public int getStreamMaxVolume(int streamType) { |
| 2226 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2227 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 2230 | /** @see AudioManager#getStreamMinVolumeInt(int) */ |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2231 | public int getStreamMinVolume(int streamType) { |
| 2232 | ensureValidStreamType(streamType); |
| 2233 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 2234 | } |
| 2235 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2236 | /** Get last audible volume before stream was muted. */ |
| 2237 | public int getLastAudibleStreamVolume(int streamType) { |
| 2238 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2239 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2240 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2243 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 2244 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 2245 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2248 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 2249 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2250 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 2251 | // If we are being called by the system check for user we are going to change |
| 2252 | // so we handle user restrictions correctly. |
| 2253 | int uid = Binder.getCallingUid(); |
| 2254 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2255 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2256 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2257 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 2258 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2259 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2260 | return; |
| 2261 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 2262 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 2263 | return; |
| 2264 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2265 | if (userId != UserHandle.getCallingUserId() && |
| 2266 | mContext.checkCallingOrSelfPermission( |
| 2267 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2268 | != PackageManager.PERMISSION_GRANTED) { |
| 2269 | return; |
| 2270 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2271 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 2272 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2273 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2274 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 2275 | if (DEBUG_VOL) { |
| 2276 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 2277 | } |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2278 | // only mute for the current user |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2279 | if (getCurrentUserId() == userId) { |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2280 | final boolean currentMute = AudioSystem.isMicrophoneMuted(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2281 | AudioSystem.muteMicrophone(on); |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2282 | if (on != currentMute) { |
| 2283 | mContext.sendBroadcast(new Intent(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED) |
| 2284 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)); |
| 2285 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2286 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2287 | } |
| 2288 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2289 | @Override |
| 2290 | public int getRingerModeExternal() { |
| 2291 | synchronized(mSettingsLock) { |
| 2292 | return mRingerModeExternal; |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | @Override |
| 2297 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2298 | synchronized(mSettingsLock) { |
| 2299 | return mRingerMode; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2304 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2305 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 2306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2309 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2310 | public boolean isValidRingerMode(int ringerMode) { |
| 2311 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2312 | } |
| 2313 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2314 | public void setRingerModeExternal(int ringerMode, String caller) { |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2315 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 2316 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller)) { |
| 2317 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 2318 | } |
| 2319 | |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2320 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2324 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2325 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 2329 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2330 | return; |
| 2331 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2332 | if (caller == null || caller.length() == 0) { |
| 2333 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2334 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2335 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2336 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2337 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2338 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2339 | final long identity = Binder.clearCallingIdentity(); |
| 2340 | try { |
| 2341 | synchronized (mSettingsLock) { |
| 2342 | final int ringerModeInternal = getRingerModeInternal(); |
| 2343 | final int ringerModeExternal = getRingerModeExternal(); |
| 2344 | if (external) { |
| 2345 | setRingerModeExt(ringerMode); |
| 2346 | if (mRingerModeDelegate != null) { |
| 2347 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2348 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2349 | } |
| 2350 | if (ringerMode != ringerModeInternal) { |
| 2351 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2352 | } |
| 2353 | } else /*internal*/ { |
| 2354 | if (ringerMode != ringerModeInternal) { |
| 2355 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2356 | } |
| 2357 | if (mRingerModeDelegate != null) { |
| 2358 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2359 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2360 | } |
| 2361 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2362 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2363 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2364 | } finally { |
| 2365 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2369 | private void setRingerModeExt(int ringerMode) { |
| 2370 | synchronized(mSettingsLock) { |
| 2371 | if (ringerMode == mRingerModeExternal) return; |
| 2372 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2373 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2374 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2375 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2376 | } |
| 2377 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2378 | private void muteRingerModeStreams() { |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2379 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 2380 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 2381 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 2382 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2383 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2384 | final boolean ringerModeMute = mRingerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2385 | || mRingerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2386 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2387 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 2388 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 2389 | if (isMuted == shouldMute) continue; |
| 2390 | if (!shouldMute) { |
| 2391 | // unmute |
| 2392 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2393 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2394 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2395 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2396 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2397 | int device = vss.mIndexMap.keyAt(i); |
| 2398 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2399 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2400 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2401 | } |
| 2402 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2403 | // Persist volume for stream ring when it is changed here |
| 2404 | final int device = getDeviceForStream(streamType); |
| 2405 | sendMsg(mAudioHandler, |
| 2406 | MSG_PERSIST_VOLUME, |
| 2407 | SENDMSG_QUEUE, |
| 2408 | device, |
| 2409 | 0, |
| 2410 | mStreamStates[streamType], |
| 2411 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2412 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2413 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2414 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2415 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2416 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2417 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2418 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2419 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2425 | final boolean change; |
| 2426 | synchronized(mSettingsLock) { |
| 2427 | change = mRingerMode != ringerMode; |
| 2428 | mRingerMode = ringerMode; |
| 2429 | } |
| 2430 | |
| 2431 | muteRingerModeStreams(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2432 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2433 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2434 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2435 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2436 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2437 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2438 | if (change) { |
| 2439 | // Send sticky broadcast |
| 2440 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2441 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2444 | /** @see AudioManager#shouldVibrate(int) */ |
| 2445 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2446 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2447 | |
| 2448 | switch (getVibrateSetting(vibrateType)) { |
| 2449 | |
| 2450 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2451 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2452 | |
| 2453 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2454 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | |
| 2456 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2457 | // return false, even for incoming calls |
| 2458 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2459 | |
| 2460 | default: |
| 2461 | return false; |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2466 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2467 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2468 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2469 | } |
| 2470 | |
| 2471 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2472 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2473 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2474 | if (!mHasVibrator) return; |
| 2475 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2476 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2477 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2478 | |
| 2479 | // Broadcast change |
| 2480 | broadcastVibrateSetting(vibrateType); |
| 2481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2482 | } |
| 2483 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2484 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2485 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2486 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2487 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2488 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2489 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2490 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2491 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2495 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2496 | synchronized(mSetModeDeathHandlers) { |
| 2497 | Log.w(TAG, "setMode() client died"); |
| 2498 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2499 | if (index < 0) { |
| 2500 | Log.w(TAG, "unregistered setMode() client died"); |
| 2501 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2502 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2503 | } |
| 2504 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2505 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2506 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2507 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2508 | final long ident = Binder.clearCallingIdentity(); |
| 2509 | disconnectBluetoothSco(newModeOwnerPid); |
| 2510 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2511 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2514 | public int getPid() { |
| 2515 | return mPid; |
| 2516 | } |
| 2517 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2518 | public void setMode(int mode) { |
| 2519 | mMode = mode; |
| 2520 | } |
| 2521 | |
| 2522 | public int getMode() { |
| 2523 | return mMode; |
| 2524 | } |
| 2525 | |
| 2526 | public IBinder getBinder() { |
| 2527 | return mCb; |
| 2528 | } |
| 2529 | } |
| 2530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2531 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2532 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2533 | 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] | 2534 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2535 | return; |
| 2536 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2537 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2538 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2539 | (mContext.checkCallingOrSelfPermission( |
| 2540 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2541 | != PackageManager.PERMISSION_GRANTED)) { |
| 2542 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2543 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2544 | return; |
| 2545 | } |
| 2546 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2547 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2548 | return; |
| 2549 | } |
| 2550 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2551 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2552 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2553 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2554 | mode = mMode; |
| 2555 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2556 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2557 | } |
| 2558 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2559 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2560 | if (newModeOwnerPid != 0) { |
| 2561 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2562 | } |
| 2563 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2564 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2565 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2566 | // 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] | 2567 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2568 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2569 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2570 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2571 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2572 | if (cb == null) { |
| 2573 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2574 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2575 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2576 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2577 | SetModeDeathHandler hdlr = null; |
| 2578 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2579 | while (iter.hasNext()) { |
| 2580 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2581 | if (h.getPid() == pid) { |
| 2582 | hdlr = h; |
| 2583 | // Remove from client list so that it is re-inserted at top of list |
| 2584 | iter.remove(); |
| 2585 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2586 | break; |
| 2587 | } |
| 2588 | } |
| 2589 | int status = AudioSystem.AUDIO_STATUS_OK; |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2590 | int actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2591 | do { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2592 | actualMode = mode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2593 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2594 | // get new mode from client at top the list if any |
| 2595 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2596 | hdlr = mSetModeDeathHandlers.get(0); |
| 2597 | cb = hdlr.getBinder(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2598 | actualMode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2599 | if (DEBUG_MODE) { |
| 2600 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2601 | + hdlr.mPid); |
| 2602 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2603 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2604 | } else { |
| 2605 | if (hdlr == null) { |
| 2606 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2607 | } |
| 2608 | // Register for client death notification |
| 2609 | try { |
| 2610 | cb.linkToDeath(hdlr, 0); |
| 2611 | } catch (RemoteException e) { |
| 2612 | // Client has died! |
| 2613 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2614 | } |
| 2615 | |
| 2616 | // Last client to call setMode() is always at top of client list |
| 2617 | // as required by SetModeDeathHandler.binderDied() |
| 2618 | mSetModeDeathHandlers.add(0, hdlr); |
| 2619 | hdlr.setMode(mode); |
| 2620 | } |
| 2621 | |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2622 | if (actualMode != mMode) { |
| 2623 | status = AudioSystem.setPhoneState(actualMode); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2624 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2625 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + actualMode); } |
| 2626 | mMode = actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2627 | } else { |
| 2628 | if (hdlr != null) { |
| 2629 | mSetModeDeathHandlers.remove(hdlr); |
| 2630 | cb.unlinkToDeath(hdlr, 0); |
| 2631 | } |
| 2632 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2633 | 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] | 2634 | mode = AudioSystem.MODE_NORMAL; |
| 2635 | } |
| 2636 | } else { |
| 2637 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2638 | } |
| 2639 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2640 | |
| 2641 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2642 | if (actualMode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2643 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2644 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2645 | } else { |
| 2646 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2647 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2648 | } |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2649 | // Note: newModeOwnerPid is always 0 when actualMode is MODE_NORMAL |
| 2650 | mModeLogger.log( |
| 2651 | new PhoneStateEvent(caller, pid, mode, newModeOwnerPid, actualMode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2652 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2653 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2654 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2655 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2656 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2657 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2658 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2659 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | /** @see AudioManager#getMode() */ |
| 2663 | public int getMode() { |
| 2664 | return mMode; |
| 2665 | } |
| 2666 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2667 | //========================================================================================== |
| 2668 | // Sound Effects |
| 2669 | //========================================================================================== |
| 2670 | |
| 2671 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2672 | private static final String ATTR_VERSION = "version"; |
| 2673 | private static final String TAG_GROUP = "group"; |
| 2674 | private static final String ATTR_GROUP_NAME = "name"; |
| 2675 | private static final String TAG_ASSET = "asset"; |
| 2676 | private static final String ATTR_ASSET_ID = "id"; |
| 2677 | private static final String ATTR_ASSET_FILE = "file"; |
| 2678 | |
| 2679 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2680 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2681 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2682 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2683 | |
| 2684 | class LoadSoundEffectReply { |
| 2685 | public int mStatus = 1; |
| 2686 | }; |
| 2687 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2688 | private void loadTouchSoundAssetDefaults() { |
| 2689 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2690 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2691 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2692 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | private void loadTouchSoundAssets() { |
| 2697 | XmlResourceParser parser = null; |
| 2698 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2699 | // only load assets once. |
| 2700 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2701 | return; |
| 2702 | } |
| 2703 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2704 | loadTouchSoundAssetDefaults(); |
| 2705 | |
| 2706 | try { |
| 2707 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2708 | |
| 2709 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2710 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2711 | boolean inTouchSoundsGroup = false; |
| 2712 | |
| 2713 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2714 | while (true) { |
| 2715 | XmlUtils.nextElement(parser); |
| 2716 | String element = parser.getName(); |
| 2717 | if (element == null) { |
| 2718 | break; |
| 2719 | } |
| 2720 | if (element.equals(TAG_GROUP)) { |
| 2721 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2722 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2723 | inTouchSoundsGroup = true; |
| 2724 | break; |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | while (inTouchSoundsGroup) { |
| 2729 | XmlUtils.nextElement(parser); |
| 2730 | String element = parser.getName(); |
| 2731 | if (element == null) { |
| 2732 | break; |
| 2733 | } |
| 2734 | if (element.equals(TAG_ASSET)) { |
| 2735 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2736 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2737 | int fx; |
| 2738 | |
| 2739 | try { |
| 2740 | Field field = AudioManager.class.getField(id); |
| 2741 | fx = field.getInt(null); |
| 2742 | } catch (Exception e) { |
| 2743 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2744 | continue; |
| 2745 | } |
| 2746 | |
| 2747 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2748 | if (i == -1) { |
| 2749 | i = SOUND_EFFECT_FILES.size(); |
| 2750 | SOUND_EFFECT_FILES.add(file); |
| 2751 | } |
| 2752 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2753 | } else { |
| 2754 | break; |
| 2755 | } |
| 2756 | } |
| 2757 | } |
| 2758 | } catch (Resources.NotFoundException e) { |
| 2759 | Log.w(TAG, "audio assets file not found", e); |
| 2760 | } catch (XmlPullParserException e) { |
| 2761 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2762 | } catch (IOException e) { |
| 2763 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2764 | } finally { |
| 2765 | if (parser != null) { |
| 2766 | parser.close(); |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | /** @see AudioManager#playSoundEffect(int) */ |
| 2772 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2773 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2778 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2779 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2780 | return; |
| 2781 | } |
| 2782 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2783 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | effectType, (int) (volume * 1000), null, 0); |
| 2785 | } |
| 2786 | |
| 2787 | /** |
| 2788 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2789 | * 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] | 2790 | */ |
| 2791 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2792 | int attempts = 3; |
| 2793 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2794 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2795 | synchronized (reply) { |
| 2796 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2797 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2798 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2799 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2800 | } catch (InterruptedException e) { |
| 2801 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2802 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2803 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2804 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2805 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | /** |
| 2809 | * Unloads samples from the sound pool. |
| 2810 | * This method can be called to free some memory when |
| 2811 | * sound effects are disabled. |
| 2812 | */ |
| 2813 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2814 | 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] | 2815 | } |
| 2816 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2817 | class SoundPoolListenerThread extends Thread { |
| 2818 | public SoundPoolListenerThread() { |
| 2819 | super("SoundPoolListenerThread"); |
| 2820 | } |
| 2821 | |
| 2822 | @Override |
| 2823 | public void run() { |
| 2824 | |
| 2825 | Looper.prepare(); |
| 2826 | mSoundPoolLooper = Looper.myLooper(); |
| 2827 | |
| 2828 | synchronized (mSoundEffectsLock) { |
| 2829 | if (mSoundPool != null) { |
| 2830 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2831 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2832 | } |
| 2833 | mSoundEffectsLock.notify(); |
| 2834 | } |
| 2835 | Looper.loop(); |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | private final class SoundPoolCallback implements |
| 2840 | android.media.SoundPool.OnLoadCompleteListener { |
| 2841 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2842 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2843 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2844 | |
| 2845 | public int status() { |
| 2846 | return mStatus; |
| 2847 | } |
| 2848 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2849 | public void setSamples(int[] samples) { |
| 2850 | for (int i = 0; i < samples.length; i++) { |
| 2851 | // do not wait ack for samples rejected upfront by SoundPool |
| 2852 | if (samples[i] > 0) { |
| 2853 | mSamples.add(samples[i]); |
| 2854 | } |
| 2855 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2859 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2860 | int i = mSamples.indexOf(sampleId); |
| 2861 | if (i >= 0) { |
| 2862 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2863 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2864 | if ((status != 0) || mSamples. isEmpty()) { |
| 2865 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2866 | mSoundEffectsLock.notify(); |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | } |
| 2871 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2872 | /** @see AudioManager#reloadAudioSettings() */ |
| 2873 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2874 | readAudioSettings(false /*userSwitch*/); |
| 2875 | } |
| 2876 | |
| 2877 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2878 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2879 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 2880 | readUserRestrictions(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2881 | |
| 2882 | // restore volume settings |
| 2883 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2884 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2885 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2886 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2887 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2888 | continue; |
| 2889 | } |
| 2890 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2891 | streamState.readSettings(); |
| 2892 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2893 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2894 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2895 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2896 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2897 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2901 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2902 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2903 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2904 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2905 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2906 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2907 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2908 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2909 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2910 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2911 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2912 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2913 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2914 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2915 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2916 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2919 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2920 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2921 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2922 | return; |
| 2923 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2924 | // for logging only |
| 2925 | final String eventSource = new StringBuilder("setSpeakerphoneOn(").append(on) |
| 2926 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 2927 | .append(Binder.getCallingPid()).toString(); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2928 | |
| 2929 | if (on) { |
| 2930 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2931 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2932 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, |
| 2933 | eventSource, 0); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2934 | } |
| 2935 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2936 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2937 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2938 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2939 | |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2940 | mForcedUseForCommExt = mForcedUseForComm; |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2941 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2942 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2946 | public boolean isSpeakerphoneOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2947 | return (mForcedUseForCommExt == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2948 | } |
| 2949 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2950 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2951 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2952 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2953 | return; |
| 2954 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2955 | |
| 2956 | // Only enable calls from system components |
| 2957 | if (Binder.getCallingUid() >= FIRST_APPLICATION_UID) { |
| 2958 | mForcedUseForCommExt = on ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE; |
| 2959 | return; |
| 2960 | } |
| 2961 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2962 | // for logging only |
| 2963 | final String eventSource = new StringBuilder("setBluetoothScoOn(").append(on) |
| 2964 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 2965 | .append(Binder.getCallingPid()).toString(); |
| 2966 | setBluetoothScoOnInt(on, eventSource); |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2967 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2968 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2969 | public void setBluetoothScoOnInt(boolean on, String eventSource) { |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2970 | if (on) { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2971 | // do not accept SCO ON if SCO audio is not connected |
| 2972 | synchronized(mScoClients) { |
| 2973 | if ((mBluetoothHeadset != null) && |
| 2974 | (mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2975 | != BluetoothHeadset.STATE_AUDIO_CONNECTED)) { |
| 2976 | mForcedUseForCommExt = AudioSystem.FORCE_BT_SCO; |
| 2977 | return; |
| 2978 | } |
| 2979 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2980 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2981 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2982 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2983 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2984 | mForcedUseForCommExt = mForcedUseForComm; |
| 2985 | AudioSystem.setParameters("BT_SCO="+ (on ? "on" : "off")); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2986 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2987 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2988 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2989 | AudioSystem.FOR_RECORD, mForcedUseForComm, eventSource, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2993 | public boolean isBluetoothScoOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 2994 | return (mForcedUseForCommExt == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 2997 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2998 | public void setBluetoothA2dpOn(boolean on) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 2999 | // for logging only |
| 3000 | final String eventSource = new StringBuilder("setBluetoothA2dpOn(").append(on) |
| 3001 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3002 | .append(Binder.getCallingPid()).toString(); |
| 3003 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3004 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3005 | mBluetoothA2dpEnabled = on; |
| 3006 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 3007 | AudioSystem.FOR_MEDIA, |
| 3008 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3009 | eventSource, 0); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3010 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3013 | /** @see AudioManager#isBluetoothA2dpOn() */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3014 | public boolean isBluetoothA2dpOn() { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3015 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3016 | return mBluetoothA2dpEnabled; |
| 3017 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3018 | } |
| 3019 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3020 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3021 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 3022 | int scoAudioMode = |
| 3023 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3024 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3025 | startBluetoothScoInt(cb, scoAudioMode); |
| 3026 | } |
| 3027 | |
| 3028 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 3029 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 3030 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 3031 | } |
| 3032 | |
| 3033 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3034 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3035 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3036 | return; |
| 3037 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3038 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3039 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 3040 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3041 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3042 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 3043 | // client is created. |
| 3044 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3045 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3046 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /** @see AudioManager#stopBluetoothSco() */ |
| 3050 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3051 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3052 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3053 | return; |
| 3054 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3055 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3056 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 3057 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3058 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3059 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3060 | if (client != null) { |
| 3061 | client.decCount(); |
| 3062 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3063 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3066 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3067 | private class ScoClient implements IBinder.DeathRecipient { |
| 3068 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3069 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3070 | private int mStartcount; // number of SCO connections started by this client |
| 3071 | |
| 3072 | ScoClient(IBinder cb) { |
| 3073 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3074 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3075 | mStartcount = 0; |
| 3076 | } |
| 3077 | |
| 3078 | public void binderDied() { |
| 3079 | synchronized(mScoClients) { |
| 3080 | Log.w(TAG, "SCO client died"); |
| 3081 | int index = mScoClients.indexOf(this); |
| 3082 | if (index < 0) { |
| 3083 | Log.w(TAG, "unregistered SCO client died"); |
| 3084 | } else { |
| 3085 | clearCount(true); |
| 3086 | mScoClients.remove(this); |
| 3087 | } |
| 3088 | } |
| 3089 | } |
| 3090 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3091 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3092 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3093 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3094 | if (mStartcount == 0) { |
| 3095 | try { |
| 3096 | mCb.linkToDeath(this, 0); |
| 3097 | } catch (RemoteException e) { |
| 3098 | // client has already died! |
| 3099 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 3100 | } |
| 3101 | } |
| 3102 | mStartcount++; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | public void decCount() { |
| 3107 | synchronized(mScoClients) { |
| 3108 | if (mStartcount == 0) { |
| 3109 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 3110 | } else { |
| 3111 | mStartcount--; |
| 3112 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3113 | try { |
| 3114 | mCb.unlinkToDeath(this, 0); |
| 3115 | } catch (NoSuchElementException e) { |
| 3116 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 3117 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3118 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3119 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3120 | } |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | public void clearCount(boolean stopSco) { |
| 3125 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3126 | if (mStartcount != 0) { |
| 3127 | try { |
| 3128 | mCb.unlinkToDeath(this, 0); |
| 3129 | } catch (NoSuchElementException e) { |
| 3130 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 3131 | } |
| 3132 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3133 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3134 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3135 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | public int getCount() { |
| 3141 | return mStartcount; |
| 3142 | } |
| 3143 | |
| 3144 | public IBinder getBinder() { |
| 3145 | return mCb; |
| 3146 | } |
| 3147 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3148 | public int getPid() { |
| 3149 | return mCreatorPid; |
| 3150 | } |
| 3151 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3152 | public int totalCount() { |
| 3153 | synchronized(mScoClients) { |
| 3154 | int count = 0; |
| 3155 | int size = mScoClients.size(); |
| 3156 | for (int i = 0; i < size; i++) { |
| 3157 | count += mScoClients.get(i).getCount(); |
| 3158 | } |
| 3159 | return count; |
| 3160 | } |
| 3161 | } |
| 3162 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3163 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3164 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3165 | if (totalCount() == 0) { |
| 3166 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3167 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 3168 | // the connection. |
| 3169 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 3170 | // 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] | 3171 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3172 | synchronized(mSetModeDeathHandlers) { |
| 3173 | if ((mSetModeDeathHandlers.isEmpty() || |
| 3174 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 3175 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 3176 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 3177 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3178 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3179 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 3180 | if (mBluetoothHeadsetDevice != null) { |
| 3181 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 3182 | mContentResolver, |
| 3183 | "bluetooth_sco_channel_"+ |
| 3184 | mBluetoothHeadsetDevice.getAddress(), |
| 3185 | SCO_MODE_VIRTUAL_CALL)); |
| 3186 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 3187 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 3188 | } |
| 3189 | } else { |
| 3190 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3191 | } |
| 3192 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3193 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3194 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3195 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3196 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3197 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3198 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3199 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3200 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3201 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3202 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3203 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3204 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3205 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3206 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3207 | } else { |
| 3208 | broadcastScoConnectionState( |
| 3209 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3210 | } |
| 3211 | } else if (getBluetoothHeadset()) { |
| 3212 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3213 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3214 | } else { |
| 3215 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3216 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3217 | } |
| 3218 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3219 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3220 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3221 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3222 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3223 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 3224 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 3225 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 3226 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3227 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3228 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3229 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3230 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3231 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3232 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3233 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3234 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3235 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3236 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3237 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3238 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3239 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3240 | broadcastScoConnectionState( |
| 3241 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3242 | } |
| 3243 | } else if (getBluetoothHeadset()) { |
| 3244 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 3245 | } |
| 3246 | } else { |
| 3247 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3248 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3249 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3255 | private void checkScoAudioState() { |
| 3256 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3257 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3258 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3259 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 3260 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 3261 | } |
| 3262 | } |
| 3263 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3264 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3265 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3266 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3267 | int size = mScoClients.size(); |
| 3268 | for (int i = 0; i < size; i++) { |
| 3269 | client = mScoClients.get(i); |
| 3270 | if (client.getBinder() == cb) |
| 3271 | return client; |
| 3272 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3273 | if (create) { |
| 3274 | client = new ScoClient(cb); |
| 3275 | mScoClients.add(client); |
| 3276 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3277 | return client; |
| 3278 | } |
| 3279 | } |
| 3280 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3281 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3282 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3283 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3284 | int size = mScoClients.size(); |
| 3285 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3286 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3287 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3288 | cl.clearCount(stopSco); |
| 3289 | } else { |
| 3290 | savedClient = cl; |
| 3291 | } |
| 3292 | } |
| 3293 | mScoClients.clear(); |
| 3294 | if (savedClient != null) { |
| 3295 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3300 | private boolean getBluetoothHeadset() { |
| 3301 | boolean result = false; |
| 3302 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 3303 | if (adapter != null) { |
| 3304 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 3305 | BluetoothProfile.HEADSET); |
| 3306 | } |
| 3307 | // If we could not get a bluetooth headset proxy, send a failure message |
| 3308 | // without delay to reset the SCO audio state and clear SCO clients. |
| 3309 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 3310 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3311 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3312 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 3313 | return result; |
| 3314 | } |
| 3315 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3316 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3317 | synchronized(mScoClients) { |
| 3318 | checkScoAudioState(); |
| 3319 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 3320 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3321 | if (mBluetoothHeadsetDevice != null) { |
| 3322 | if (mBluetoothHeadset != null) { |
| 3323 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 3324 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3325 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3326 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 3327 | } |
| 3328 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 3329 | getBluetoothHeadset()) { |
| 3330 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 3331 | } |
| 3332 | } |
| 3333 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3334 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | private void resetBluetoothSco() { |
| 3340 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3341 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3342 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3343 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3344 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3345 | AudioSystem.setParameters("A2dpSuspended=false"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3346 | setBluetoothScoOnInt(false, "resetBluetoothSco"); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3350 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 3351 | SENDMSG_QUEUE, state, 0, null, 0); |
| 3352 | } |
| 3353 | |
| 3354 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3355 | if (state != mScoConnectionState) { |
| 3356 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 3357 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 3358 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 3359 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3360 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3361 | mScoConnectionState = state; |
| 3362 | } |
| 3363 | } |
| 3364 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3365 | void setBtScoDeviceConnectionState(BluetoothDevice btDevice, int state) { |
| 3366 | if (btDevice == null) { |
| 3367 | return; |
| 3368 | } |
| 3369 | |
| 3370 | String address = btDevice.getAddress(); |
| 3371 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 3372 | int outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 3373 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 3374 | if (btClass != null) { |
| 3375 | switch (btClass.getDeviceClass()) { |
| 3376 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 3377 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 3378 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 3379 | break; |
| 3380 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 3381 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 3382 | break; |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 3387 | address = ""; |
| 3388 | } |
| 3389 | |
| 3390 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
| 3391 | |
| 3392 | String btDeviceName = btDevice.getName(); |
| 3393 | boolean success = |
| 3394 | handleDeviceConnection(connected, outDevice, address, btDeviceName) && |
| 3395 | handleDeviceConnection(connected, inDevice, address, btDeviceName); |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3396 | |
| 3397 | if (!success) { |
| 3398 | return; |
| 3399 | } |
| 3400 | |
| 3401 | /* When one BT headset is disconnected while another BT headset |
| 3402 | * is connected, don't mess with the headset device. |
| 3403 | */ |
| 3404 | if ((state == BluetoothProfile.STATE_DISCONNECTED || |
| 3405 | state == BluetoothProfile.STATE_DISCONNECTING) && |
| 3406 | mBluetoothHeadset != null && |
| 3407 | mBluetoothHeadset.getAudioState(btDevice) == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3408 | Log.w(TAG, "SCO connected through another device, returning"); |
| 3409 | return; |
| 3410 | } |
| 3411 | |
| 3412 | synchronized (mScoClients) { |
| 3413 | if (connected) { |
| 3414 | mBluetoothHeadsetDevice = btDevice; |
| 3415 | } else { |
| 3416 | mBluetoothHeadsetDevice = null; |
| 3417 | resetBluetoothSco(); |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3418 | } |
| 3419 | } |
| 3420 | } |
| 3421 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3422 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 3423 | new BluetoothProfile.ServiceListener() { |
| 3424 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3425 | BluetoothDevice btDevice; |
| 3426 | List<BluetoothDevice> deviceList; |
| 3427 | switch(profile) { |
| 3428 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3429 | synchronized (mConnectedDevices) { |
| 3430 | synchronized (mA2dpAvrcpLock) { |
| 3431 | mA2dp = (BluetoothA2dp) proxy; |
| 3432 | deviceList = mA2dp.getConnectedDevices(); |
| 3433 | if (deviceList.size() > 0) { |
| 3434 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3435 | int state = mA2dp.getConnectionState(btDevice); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3436 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3437 | int delay = checkSendBecomingNoisyIntent( |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3438 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, intState, |
| 3439 | AudioSystem.DEVICE_NONE); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3440 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3441 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3442 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3443 | 0 /* arg2 unused */, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3444 | btDevice, |
| 3445 | delay); |
| 3446 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3447 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3448 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3449 | break; |
| 3450 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3451 | case BluetoothProfile.A2DP_SINK: |
| 3452 | deviceList = proxy.getConnectedDevices(); |
| 3453 | if (deviceList.size() > 0) { |
| 3454 | btDevice = deviceList.get(0); |
| 3455 | synchronized (mConnectedDevices) { |
| 3456 | int state = proxy.getConnectionState(btDevice); |
| 3457 | queueMsgUnderWakeLock(mAudioHandler, |
| 3458 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 3459 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3460 | 0 /* arg2 unused */, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3461 | btDevice, |
| 3462 | 0 /* delay */); |
| 3463 | } |
| 3464 | } |
| 3465 | break; |
| 3466 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3467 | case BluetoothProfile.HEADSET: |
| 3468 | synchronized (mScoClients) { |
| 3469 | // Discard timeout message |
| 3470 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 3471 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 3472 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 3473 | if (deviceList.size() > 0) { |
| 3474 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 3475 | } else { |
| 3476 | mBluetoothHeadsetDevice = null; |
| 3477 | } |
| 3478 | // Refresh SCO audio state |
| 3479 | checkScoAudioState(); |
| 3480 | // Continue pending action if any |
| 3481 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 3482 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 3483 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3484 | boolean status = false; |
| 3485 | if (mBluetoothHeadsetDevice != null) { |
| 3486 | switch (mScoAudioState) { |
| 3487 | case SCO_STATE_ACTIVATE_REQ: |
| 3488 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3489 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3490 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3491 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3492 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3493 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3494 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3495 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3496 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3497 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3498 | break; |
| 3499 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3500 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3501 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3502 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3503 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3504 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3505 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3506 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3507 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3508 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3509 | break; |
| 3510 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 3511 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3512 | mBluetoothHeadsetDevice); |
| 3513 | } |
| 3514 | } |
| 3515 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3516 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3517 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3518 | } |
| 3519 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3520 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3521 | break; |
| 3522 | |
| 3523 | default: |
| 3524 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3525 | } |
| 3526 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3527 | public void onServiceDisconnected(int profile) { |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3528 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3529 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3530 | case BluetoothProfile.A2DP: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3531 | disconnectA2dp(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3532 | break; |
| 3533 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3534 | case BluetoothProfile.A2DP_SINK: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3535 | disconnectA2dpSink(); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3536 | break; |
| 3537 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3538 | case BluetoothProfile.HEADSET: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3539 | disconnectHeadset(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3540 | break; |
| 3541 | |
| 3542 | default: |
| 3543 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3544 | } |
| 3545 | } |
| 3546 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3547 | |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3548 | void disconnectAllBluetoothProfiles() { |
| 3549 | disconnectA2dp(); |
| 3550 | disconnectA2dpSink(); |
| 3551 | disconnectHeadset(); |
| 3552 | } |
| 3553 | |
| 3554 | void disconnectA2dp() { |
| 3555 | synchronized (mConnectedDevices) { |
| 3556 | synchronized (mA2dpAvrcpLock) { |
| 3557 | ArraySet<String> toRemove = null; |
| 3558 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
| 3559 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3560 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3561 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
| 3562 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3563 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3564 | } |
| 3565 | } |
| 3566 | if (toRemove != null) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3567 | int delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3568 | 0, AudioSystem.DEVICE_NONE); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3569 | for (int i = 0; i < toRemove.size(); i++) { |
| 3570 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
| 3571 | } |
| 3572 | } |
| 3573 | } |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | void disconnectA2dpSink() { |
| 3578 | synchronized (mConnectedDevices) { |
| 3579 | ArraySet<String> toRemove = null; |
| 3580 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
| 3581 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3582 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3583 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
| 3584 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3585 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3586 | } |
| 3587 | } |
| 3588 | if (toRemove != null) { |
| 3589 | for (int i = 0; i < toRemove.size(); i++) { |
| 3590 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
| 3591 | } |
| 3592 | } |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | void disconnectHeadset() { |
| 3597 | synchronized (mScoClients) { |
| 3598 | if (mBluetoothHeadsetDevice != null) { |
| 3599 | setBtScoDeviceConnectionState(mBluetoothHeadsetDevice, |
| 3600 | BluetoothProfile.STATE_DISCONNECTED); |
| 3601 | } |
| 3602 | mBluetoothHeadset = null; |
| 3603 | } |
| 3604 | } |
| 3605 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3606 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3607 | synchronized (mSafeMediaVolumeState) { |
| 3608 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3609 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3610 | |
| 3611 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3612 | sendMsg(mAudioHandler, |
| 3613 | MSG_CHECK_MUSIC_ACTIVE, |
| 3614 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3615 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3616 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3617 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3618 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3619 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3620 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3621 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3622 | // Approximate cumulative active music time |
| 3623 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3624 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3625 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3626 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3627 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3628 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3635 | private void saveMusicActiveMs() { |
| 3636 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3637 | } |
| 3638 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3639 | private int getSafeUsbMediaVolumeIndex() |
| 3640 | { |
| 3641 | // determine UI volume index corresponding to the wanted safe gain in dBFS |
| 3642 | int min = MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3643 | int max = MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3644 | |
| 3645 | while (Math.abs(max-min) > 1) { |
| 3646 | int index = (max + min) / 2; |
| 3647 | float gainDB = AudioSystem.getStreamVolumeDB( |
| 3648 | AudioSystem.STREAM_MUSIC, index, AudioSystem.DEVICE_OUT_USB_HEADSET); |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 3649 | if (Float.isNaN(gainDB)) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3650 | //keep last min in case of read error |
| 3651 | break; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 3652 | } else if (gainDB == SAFE_VOLUME_GAIN_DBFS) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3653 | min = index; |
| 3654 | break; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 3655 | } else if (gainDB < SAFE_VOLUME_GAIN_DBFS) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3656 | min = index; |
| 3657 | } else { |
| 3658 | max = index; |
| 3659 | } |
| 3660 | } |
| 3661 | return min * 10; |
| 3662 | } |
| 3663 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3664 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3665 | synchronized (mSafeMediaVolumeState) { |
| 3666 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3667 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3668 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3669 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3670 | |
| 3671 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 3672 | |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3673 | boolean safeMediaVolumeEnabled = |
| 3674 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3675 | || mContext.getResources().getBoolean( |
| 3676 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3677 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3678 | boolean safeMediaVolumeBypass = |
| 3679 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3680 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3681 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3682 | // next time we boot and cannot be "inactive" |
| 3683 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3684 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3685 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3686 | // The state can already be "inactive" here if the user has forced it before |
| 3687 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3688 | // it to "active". |
| 3689 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3690 | if (mMusicActiveMs == 0) { |
| 3691 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3692 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3693 | } else { |
| 3694 | // We have existing playback time recorded, already confirmed. |
| 3695 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3696 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3697 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3698 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3699 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3700 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3701 | } |
| 3702 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3703 | sendMsg(mAudioHandler, |
| 3704 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3705 | SENDMSG_QUEUE, |
| 3706 | persistedState, |
| 3707 | 0, |
| 3708 | null, |
| 3709 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3710 | } |
| 3711 | } |
| 3712 | } |
| 3713 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | /////////////////////////////////////////////////////////////////////////// |
| 3715 | // Internal methods |
| 3716 | /////////////////////////////////////////////////////////////////////////// |
| 3717 | |
| 3718 | /** |
| 3719 | * Checks if the adjustment should change ringer mode instead of just |
| 3720 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3721 | * indices on the stream states. |
| 3722 | */ |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3723 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted, |
| 3724 | String caller, int flags) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3725 | int result = FLAG_ADJUST_VOLUME; |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 3726 | if (isPlatformTelevision() || mIsSingleVolume) { |
Hank Freund | 21003f6 | 2015-12-08 09:05:46 -0800 | [diff] [blame] | 3727 | return result; |
| 3728 | } |
| 3729 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3730 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3731 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3732 | switch (ringerMode) { |
| 3733 | case RINGER_MODE_NORMAL: |
| 3734 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3735 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3736 | // "step" is the delta in internal index units corresponding to a |
| 3737 | // change of 1 in UI index units. |
| 3738 | // Because of rounding when rescaling from one stream index range to its alias |
| 3739 | // index range, we cannot simply test oldIndex == step: |
| 3740 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3741 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3742 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3743 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3744 | } |
| 3745 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3746 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3747 | ringerMode = RINGER_MODE_SILENT; |
| 3748 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3749 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3750 | } else if (mIsSingleVolume && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3751 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3752 | if (mHasVibrator) { |
| 3753 | ringerMode = RINGER_MODE_VIBRATE; |
| 3754 | } else { |
| 3755 | ringerMode = RINGER_MODE_SILENT; |
| 3756 | } |
| 3757 | // Setting the ringer mode will toggle mute |
| 3758 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3759 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3760 | break; |
| 3761 | case RINGER_MODE_VIBRATE: |
| 3762 | if (!mHasVibrator) { |
| 3763 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3764 | "but no vibrator is present"); |
| 3765 | break; |
| 3766 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3767 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3768 | // This is the case we were muted with the volume turned up |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3769 | if (mIsSingleVolume && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3770 | ringerMode = RINGER_MODE_NORMAL; |
| 3771 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3772 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3773 | final long diff = SystemClock.uptimeMillis() |
| 3774 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3775 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 3776 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3777 | ringerMode = RINGER_MODE_SILENT; |
| 3778 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3779 | } else { |
| 3780 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3781 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3782 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3783 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3784 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3785 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3786 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3787 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3788 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3789 | break; |
| 3790 | case RINGER_MODE_SILENT: |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3791 | if (mIsSingleVolume && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3792 | // This is the case we were muted with the volume turned up |
| 3793 | ringerMode = RINGER_MODE_NORMAL; |
| 3794 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3795 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3796 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3797 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3798 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3799 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3800 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3801 | ringerMode = RINGER_MODE_VIBRATE; |
| 3802 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3803 | // If we don't have a vibrator or they were toggling mute |
| 3804 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3805 | ringerMode = RINGER_MODE_NORMAL; |
| 3806 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3807 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3808 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3809 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3810 | break; |
| 3811 | default: |
| 3812 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3813 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3814 | } |
| 3815 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3816 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 3817 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller) |
| 3818 | && (flags & AudioManager.FLAG_FROM_KEY) == 0) { |
| 3819 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 3820 | } |
| 3821 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3822 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3823 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3824 | mPrevVolDirection = direction; |
| 3825 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3826 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | } |
| 3828 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3829 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3831 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3832 | } |
| 3833 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3834 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3835 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3836 | } |
| 3837 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 3838 | @GuardedBy("mSettingsLock") |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3839 | private boolean updateRingerModeAffectedStreams() { |
| 3840 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3841 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3842 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3843 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3844 | UserHandle.USER_CURRENT); |
| 3845 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3846 | if (mIsSingleVolume) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3847 | ringerModeAffectedStreams = 0; |
| 3848 | } else if (mRingerModeDelegate != null) { |
| 3849 | ringerModeAffectedStreams = mRingerModeDelegate |
| 3850 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3851 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 3852 | if (mCameraSoundForced) { |
| 3853 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3854 | } else { |
| 3855 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3856 | } |
| 3857 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3858 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3859 | } else { |
| 3860 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3861 | } |
| 3862 | |
| 3863 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3864 | Settings.System.putIntForUser(mContentResolver, |
| 3865 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3866 | ringerModeAffectedStreams, |
| 3867 | UserHandle.USER_CURRENT); |
| 3868 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3869 | return true; |
| 3870 | } |
| 3871 | return false; |
| 3872 | } |
| 3873 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3874 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3875 | public boolean isStreamAffectedByMute(int streamType) { |
| 3876 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3877 | } |
| 3878 | |
| 3879 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3880 | switch (direction) { |
| 3881 | case AudioManager.ADJUST_LOWER: |
| 3882 | case AudioManager.ADJUST_RAISE: |
| 3883 | case AudioManager.ADJUST_SAME: |
| 3884 | case AudioManager.ADJUST_MUTE: |
| 3885 | case AudioManager.ADJUST_UNMUTE: |
| 3886 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3887 | break; |
| 3888 | default: |
| 3889 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | private void ensureValidStreamType(int streamType) { |
| 3894 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3895 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3896 | } |
| 3897 | } |
| 3898 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3899 | private boolean isMuteAdjust(int adjust) { |
| 3900 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3901 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3902 | } |
| 3903 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3904 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3905 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3906 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3907 | TelecomManager telecomManager = |
| 3908 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3909 | |
| 3910 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3911 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3912 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3913 | |
Eric Laurent | da1af76 | 2017-12-15 16:54:35 -0800 | [diff] [blame] | 3914 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION || |
| 3915 | getMode() == AudioManager.MODE_IN_CALL); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3916 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3917 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3918 | /** |
| 3919 | * For code clarity for getActiveStreamType(int) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3920 | * @param delay_ms max time since last stream activity to consider |
| 3921 | * @return true if stream is active in streams handled by AudioFlinger now or |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3922 | * in the last "delay_ms" ms. |
| 3923 | */ |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3924 | private boolean wasStreamActiveRecently(int stream, int delay_ms) { |
| 3925 | return AudioSystem.isStreamActive(stream, delay_ms) |
| 3926 | || AudioSystem.isStreamActiveRemotely(stream, delay_ms); |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3929 | private int getActiveStreamType(int suggestedStreamType) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3930 | if (mIsSingleVolume |
| 3931 | && suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 3932 | return AudioSystem.STREAM_MUSIC; |
| 3933 | } |
| 3934 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3935 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3936 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3937 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3938 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3939 | == AudioSystem.FORCE_BT_SCO) { |
| 3940 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3941 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3942 | } else { |
| 3943 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3944 | return AudioSystem.STREAM_VOICE_CALL; |
| 3945 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3946 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3947 | if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3948 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3949 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 3950 | return AudioSystem.STREAM_RING; |
| 3951 | } else if (wasStreamActiveRecently( |
| 3952 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 3953 | if (DEBUG_VOL) |
| 3954 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 3955 | return AudioSystem.STREAM_NOTIFICATION; |
| 3956 | } else { |
| 3957 | if (DEBUG_VOL) |
| 3958 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC b/c default"); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3959 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3960 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3961 | } else if ( |
| 3962 | wasStreamActiveRecently(AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3963 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3964 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 3965 | return AudioSystem.STREAM_NOTIFICATION; |
| 3966 | } else if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3967 | if (DEBUG_VOL) |
| 3968 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 3969 | return AudioSystem.STREAM_RING; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3970 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3971 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3972 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3973 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3974 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3975 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3976 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3977 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3978 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3979 | return AudioSystem.STREAM_VOICE_CALL; |
| 3980 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3981 | } else if (AudioSystem.isStreamActive( |
| 3982 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3983 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3984 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3985 | } else if (AudioSystem.isStreamActive( |
| 3986 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3987 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 3988 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3989 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3990 | if (AudioSystem.isStreamActive( |
| 3991 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 3992 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3993 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3994 | } else if (AudioSystem.isStreamActive( |
| 3995 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3996 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 3997 | return AudioSystem.STREAM_RING; |
| 3998 | } else { |
| 3999 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: using STREAM_MUSIC as default"); |
| 4000 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4001 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 4002 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4003 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4005 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 4006 | + suggestedStreamType); |
| 4007 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4008 | } |
| 4009 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4010 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4011 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4012 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4013 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 4014 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 4015 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4016 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | private void broadcastVibrateSetting(int vibrateType) { |
| 4020 | // Send broadcast |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 4021 | if (mActivityManagerInternal.isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4022 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 4023 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 4024 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4025 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4030 | /** |
| 4031 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 4032 | * Note that the wake lock needs to be released after the message has been handled. |
| 4033 | */ |
| 4034 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 4035 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4036 | final long ident = Binder.clearCallingIdentity(); |
| 4037 | // Always acquire the wake lock as AudioService because it is released by the |
| 4038 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4039 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4040 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4041 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 4042 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4043 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4044 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4045 | 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] | 4046 | |
| 4047 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 4048 | handler.removeMessages(msg); |
| 4049 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 4050 | return; |
| 4051 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4052 | synchronized (mLastDeviceConnectMsgTime) { |
| 4053 | long time = SystemClock.uptimeMillis() + delay; |
| 4054 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 4055 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 4056 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 4057 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 4058 | mLastDeviceConnectMsgTime = time; |
| 4059 | } |
| 4060 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 4064 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4065 | == PackageManager.PERMISSION_GRANTED) { |
| 4066 | return true; |
| 4067 | } |
| 4068 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 4069 | + Binder.getCallingPid() |
| 4070 | + ", uid=" + Binder.getCallingUid(); |
| 4071 | Log.w(TAG, msg); |
| 4072 | return false; |
| 4073 | } |
| 4074 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4075 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4076 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4077 | if ((device & (device - 1)) != 0) { |
| 4078 | // Multiple device selection is either: |
| 4079 | // - speaker + one other device: give priority to speaker in this case. |
| 4080 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 4081 | // retain the device on the A2DP output as the other must not correspond to an active |
| 4082 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4083 | // - 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] | 4084 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 4085 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4086 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 4087 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 4088 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 4089 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 4090 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 4091 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4092 | } else { |
| 4093 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 4094 | } |
| 4095 | } |
| 4096 | return device; |
| 4097 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4098 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4099 | private int getDevicesForStream(int stream) { |
| 4100 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 4101 | } |
| 4102 | |
| 4103 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 4104 | ensureValidStreamType(stream); |
| 4105 | synchronized (VolumeStreamState.class) { |
| 4106 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | private void observeDevicesForStreams(int skipStream) { |
| 4111 | synchronized (VolumeStreamState.class) { |
| 4112 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 4113 | if (stream != skipStream) { |
| 4114 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 4115 | } |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4120 | /* |
| 4121 | * A class just for packaging up a set of connection parameters. |
| 4122 | */ |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 4123 | class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4124 | public final int mType; |
| 4125 | public final int mState; |
| 4126 | public final String mAddress; |
| 4127 | public final String mName; |
| 4128 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4129 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4130 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 4131 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4132 | mType = type; |
| 4133 | mState = state; |
| 4134 | mAddress = address; |
| 4135 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4136 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4137 | } |
| 4138 | } |
| 4139 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4140 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 4141 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4142 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4143 | if (DEBUG_DEVICES) { |
| 4144 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 4145 | + address + ")"); |
| 4146 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4147 | int delay = checkSendBecomingNoisyIntent(type, state, AudioSystem.DEVICE_NONE); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4148 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4149 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4150 | 0 /* arg1 unused */, |
| 4151 | 0 /* arg2 unused */, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4152 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4153 | delay); |
| 4154 | } |
| 4155 | } |
| 4156 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4157 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4158 | { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4159 | return setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent( |
| 4160 | device, state, profile, false /* suppressNoisyIntent */); |
| 4161 | } |
| 4162 | |
| 4163 | public int setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(BluetoothDevice device, |
| 4164 | int state, int profile, boolean suppressNoisyIntent) |
| 4165 | { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 4166 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, device)) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4167 | return 0; |
| 4168 | } |
| 4169 | return setBluetoothA2dpDeviceConnectionStateInt( |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4170 | device, state, profile, suppressNoisyIntent, AudioSystem.DEVICE_NONE); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | public int setBluetoothA2dpDeviceConnectionStateInt( |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4174 | BluetoothDevice device, int state, int profile, boolean suppressNoisyIntent, |
| 4175 | int musicDevice) |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4176 | { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4177 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4178 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 4179 | throw new IllegalArgumentException("invalid profile " + profile); |
| 4180 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4181 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4182 | if (profile == BluetoothProfile.A2DP && !suppressNoisyIntent) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4183 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4184 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4185 | intState, musicDevice); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4186 | } else { |
| 4187 | delay = 0; |
| 4188 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4189 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4190 | (profile == BluetoothProfile.A2DP ? |
| 4191 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4192 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4193 | 0 /* arg2 unused */, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4194 | device, |
| 4195 | delay); |
| 4196 | } |
| 4197 | return delay; |
| 4198 | } |
| 4199 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4200 | public void handleBluetoothA2dpDeviceConfigChange(BluetoothDevice device) |
| 4201 | { |
| 4202 | synchronized (mConnectedDevices) { |
| 4203 | queueMsgUnderWakeLock(mAudioHandler, |
| 4204 | MSG_A2DP_DEVICE_CONFIG_CHANGE, |
| 4205 | 0 /* arg1 unused */, |
| 4206 | 0 /* arg1 unused */, |
| 4207 | device, |
| 4208 | 0 /* delay */); |
| 4209 | } |
| 4210 | } |
| 4211 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4212 | private static final int DEVICE_MEDIA_UNMUTED_ON_PLUG = |
| 4213 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 4214 | AudioSystem.DEVICE_OUT_LINE | |
| 4215 | AudioSystem.DEVICE_OUT_ALL_A2DP | |
| 4216 | AudioSystem.DEVICE_OUT_ALL_USB | |
| 4217 | AudioSystem.DEVICE_OUT_HDMI; |
| 4218 | |
| 4219 | private void onAccessoryPlugMediaUnmute(int newDevice) { |
| 4220 | if (DEBUG_VOL) { |
| 4221 | Log.i(TAG, String.format("onAccessoryPlugMediaUnmute newDevice=%d [%s]", |
| 4222 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4223 | } |
| 4224 | synchronized (mConnectedDevices) { |
Eric Laurent | 7fb83d9 | 2017-09-28 09:36:36 -0700 | [diff] [blame] | 4225 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 4226 | && (newDevice & DEVICE_MEDIA_UNMUTED_ON_PLUG) != 0 |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4227 | && mStreamStates[AudioSystem.STREAM_MUSIC].mIsMuted |
| 4228 | && mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(newDevice) != 0 |
| 4229 | && (newDevice & AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC)) != 0) |
| 4230 | { |
| 4231 | if (DEBUG_VOL) { |
| 4232 | Log.i(TAG, String.format(" onAccessoryPlugMediaUnmute unmuting device=%d [%s]", |
| 4233 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4234 | } |
| 4235 | mStreamStates[AudioSystem.STREAM_MUSIC].mute(false); |
| 4236 | } |
| 4237 | } |
| 4238 | } |
| 4239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4240 | /////////////////////////////////////////////////////////////////////////// |
| 4241 | // Inner classes |
| 4242 | /////////////////////////////////////////////////////////////////////////// |
| 4243 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4244 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 4245 | // 1 mScoclient OR mSafeMediaVolumeState |
| 4246 | // 2 mSetModeDeathHandlers |
| 4247 | // 3 mSettingsLock |
| 4248 | // 4 VolumeStreamState.class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4249 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4250 | private final int mStreamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4251 | private final int mIndexMin; |
| 4252 | private final int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4253 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4254 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4255 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4256 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4257 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4258 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4259 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4260 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4262 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4263 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4264 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4265 | |
| 4266 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4267 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 4268 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 4269 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4270 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4271 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4272 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 4273 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4274 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 4275 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4276 | } |
| 4277 | |
| 4278 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 4279 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 4280 | if (devices == mObservedDevices) { |
| 4281 | return devices; |
| 4282 | } |
| 4283 | final int prevDevices = mObservedDevices; |
| 4284 | mObservedDevices = devices; |
| 4285 | if (checkOthers) { |
| 4286 | // one stream's devices have changed, check the others |
| 4287 | observeDevicesForStreams(mStreamType); |
| 4288 | } |
| 4289 | // log base stream changes to the event log |
| 4290 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4291 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 4292 | } |
| 4293 | sendBroadcastToAll(mStreamDevicesChanged |
| 4294 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 4295 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 4296 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4297 | } |
| 4298 | |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4299 | public @Nullable String getSettingNameForDevice(int device) { |
| 4300 | if (!hasValidSettingsName()) { |
| 4301 | return null; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4302 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4303 | final String suffix = AudioSystem.getOutputDeviceName(device); |
| 4304 | if (suffix.isEmpty()) { |
| 4305 | return mVolumeIndexSettingName; |
| 4306 | } |
| 4307 | return mVolumeIndexSettingName + "_" + suffix; |
| 4308 | } |
| 4309 | |
| 4310 | private boolean hasValidSettingsName() { |
| 4311 | return (mVolumeIndexSettingName != null && !mVolumeIndexSettingName.isEmpty()); |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4312 | } |
| 4313 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4314 | public void readSettings() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4315 | synchronized (mSettingsLock) { |
| 4316 | synchronized (VolumeStreamState.class) { |
| 4317 | // force maximum volume on all streams if fixed volume property is set |
| 4318 | if (mUseFixedVolume) { |
| 4319 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 4320 | return; |
| 4321 | } |
| 4322 | // do not read system stream volume from settings: this stream is always aliased |
| 4323 | // to another stream type and its volume is never persisted. Values in settings can |
| 4324 | // only be stale values |
| 4325 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 4326 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
| 4327 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4328 | if (mCameraSoundForced) { |
| 4329 | index = mIndexMax; |
| 4330 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4331 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 4332 | return; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4333 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4334 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4335 | } |
| 4336 | synchronized (VolumeStreamState.class) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4337 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 4338 | |
| 4339 | for (int i = 0; remainingDevices != 0; i++) { |
| 4340 | int device = (1 << i); |
| 4341 | if ((device & remainingDevices) == 0) { |
| 4342 | continue; |
| 4343 | } |
| 4344 | remainingDevices &= ~device; |
| 4345 | |
| 4346 | // retrieve current volume for device |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4347 | // if no volume stored for current stream and device, use default volume if default |
| 4348 | // device, continue otherwise |
| 4349 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4350 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4351 | int index; |
| 4352 | if (!hasValidSettingsName()) { |
| 4353 | index = defaultIndex; |
| 4354 | } else { |
| 4355 | String name = getSettingNameForDevice(device); |
| 4356 | index = Settings.System.getIntForUser( |
| 4357 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 4358 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4359 | if (index == -1) { |
| 4360 | continue; |
| 4361 | } |
| 4362 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4363 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4364 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4368 | private int getAbsoluteVolumeIndex(int index) { |
| 4369 | /* Special handling for Bluetooth Absolute Volume scenario |
| 4370 | * If we send full audio gain, some accessories are too loud even at its lowest |
| 4371 | * volume. We are not able to enumerate all such accessories, so here is the |
| 4372 | * workaround from phone side. |
| 4373 | * Pre-scale volume at lowest volume steps 1 2 and 3. |
| 4374 | * For volume step 0, set audio gain to 0 as some accessories won't mute on their end. |
| 4375 | */ |
| 4376 | if (index == 0) { |
| 4377 | // 0% for volume 0 |
| 4378 | index = 0; |
| 4379 | } else if (index == 1) { |
| 4380 | // 50% for volume 1 |
| 4381 | index = (int)(mIndexMax * 0.5) /10; |
| 4382 | } else if (index == 2) { |
| 4383 | // 70% for volume 2 |
| 4384 | index = (int)(mIndexMax * 0.70) /10; |
| 4385 | } else if (index == 3) { |
| 4386 | // 85% for volume 3 |
| 4387 | index = (int)(mIndexMax * 0.85) /10; |
| 4388 | } else { |
| 4389 | // otherwise, full gain |
| 4390 | index = (mIndexMax + 5)/10; |
| 4391 | } |
| 4392 | return index; |
| 4393 | } |
| 4394 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4395 | // must be called while synchronized VolumeStreamState.class |
| 4396 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4397 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4398 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4399 | index = 0; |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4400 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4401 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4402 | } else if ((device & mFullVolumeDevices) != 0) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4403 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4404 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4405 | index = (getIndex(device) + 5)/10; |
| 4406 | } |
| 4407 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4408 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4409 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4410 | public void applyAllVolumes() { |
| 4411 | synchronized (VolumeStreamState.class) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4412 | // apply device specific volumes first |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4413 | int index; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4414 | for (int i = 0; i < mIndexMap.size(); i++) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4415 | final int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4416 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4417 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4418 | index = 0; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4419 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 4420 | mAvrcpAbsVolSupported) { |
| 4421 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
| 4422 | } else if ((device & mFullVolumeDevices) != 0) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4423 | index = (mIndexMax + 5)/10; |
| 4424 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4425 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4426 | } |
| 4427 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4428 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4429 | } |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4430 | // apply default volume last: by convention , default device volume will be used |
| 4431 | // by audio policy manager if no explicit volume is present for a given device type |
| 4432 | if (mIsMuted) { |
| 4433 | index = 0; |
| 4434 | } else { |
| 4435 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 4436 | } |
| 4437 | AudioSystem.setStreamVolumeIndex( |
| 4438 | mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4442 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 4443 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4444 | } |
| 4445 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4446 | public boolean setIndex(int index, int device, String caller) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4447 | boolean changed = false; |
| 4448 | int oldIndex; |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4449 | synchronized (mSettingsLock) { |
| 4450 | synchronized (VolumeStreamState.class) { |
| 4451 | oldIndex = getIndex(device); |
| 4452 | index = getValidIndex(index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4453 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 4454 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4455 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4456 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4457 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4458 | changed = oldIndex != index; |
| 4459 | // Apply change to all streams using this one as alias if: |
| 4460 | // - the index actually changed OR |
| 4461 | // - there is no volume index stored for this device on alias stream. |
| 4462 | // If changing volume of current device, also change volume of current |
| 4463 | // device on aliased stream |
| 4464 | final boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 4465 | final int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4466 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4467 | final VolumeStreamState aliasStreamState = mStreamStates[streamType]; |
| 4468 | if (streamType != mStreamType && |
| 4469 | mStreamVolumeAlias[streamType] == mStreamType && |
| 4470 | (changed || !aliasStreamState.hasIndexForDevice(device))) { |
| 4471 | final int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 4472 | aliasStreamState.setIndex(scaledIndex, device, caller); |
| 4473 | if (currentDevice) { |
| 4474 | aliasStreamState.setIndex(scaledIndex, |
| 4475 | getDeviceForStream(streamType), caller); |
| 4476 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4477 | } |
| 4478 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4481 | if (changed) { |
| 4482 | oldIndex = (oldIndex + 5) / 10; |
| 4483 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4484 | // log base stream changes to the event log |
| 4485 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4486 | if (caller == null) { |
| 4487 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 4488 | } |
| 4489 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 4490 | caller); |
| 4491 | } |
| 4492 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4493 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 4494 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 4495 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 4496 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4497 | sendBroadcastToAll(mVolumeChanged); |
| 4498 | } |
| 4499 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4500 | } |
| 4501 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4502 | public int getIndex(int device) { |
| 4503 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4504 | int index = mIndexMap.get(device, -1); |
| 4505 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4506 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4507 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4508 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4509 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4510 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4511 | } |
| 4512 | |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 4513 | public boolean hasIndexForDevice(int device) { |
| 4514 | synchronized (VolumeStreamState.class) { |
| 4515 | return (mIndexMap.get(device, -1) != -1); |
| 4516 | } |
| 4517 | } |
| 4518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4519 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4520 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4521 | } |
| 4522 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4523 | public int getMinIndex() { |
| 4524 | return mIndexMin; |
| 4525 | } |
| 4526 | |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4527 | /** |
| 4528 | * Copies all device/index pairs from the given VolumeStreamState after initializing |
| 4529 | * them with the volume for DEVICE_OUT_DEFAULT. No-op if the source VolumeStreamState |
| 4530 | * has the same stream type as this instance. |
| 4531 | * @param srcStream |
| 4532 | * @param caller |
| 4533 | */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4534 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4535 | if (mStreamType == srcStream.mStreamType) { |
| 4536 | return; |
| 4537 | } |
Jean-Michel Trivi | ad37c2c | 2018-01-19 09:40:36 -0800 | [diff] [blame] | 4538 | synchronized (mSettingsLock) { |
| 4539 | synchronized (VolumeStreamState.class) { |
| 4540 | int srcStreamType = srcStream.getStreamType(); |
| 4541 | // apply default device volume from source stream to all devices first in case |
| 4542 | // some devices are present in this stream state but not in source stream state |
| 4543 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4544 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Jean-Michel Trivi | ad37c2c | 2018-01-19 09:40:36 -0800 | [diff] [blame] | 4545 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4546 | mIndexMap.put(mIndexMap.keyAt(i), index); |
| 4547 | } |
| 4548 | // Now apply actual volume for devices in source stream state |
| 4549 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 4550 | for (int i = 0; i < srcMap.size(); i++) { |
| 4551 | int device = srcMap.keyAt(i); |
| 4552 | index = srcMap.valueAt(i); |
| 4553 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4554 | |
Jean-Michel Trivi | ad37c2c | 2018-01-19 09:40:36 -0800 | [diff] [blame] | 4555 | setIndex(index, device, caller); |
| 4556 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4557 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | |
Jean-Michel Trivi | ad37c2c | 2018-01-19 09:40:36 -0800 | [diff] [blame] | 4561 | @GuardedBy("mSettingsLock") |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4562 | public void setAllIndexesToMax() { |
| 4563 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4564 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4565 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4566 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4567 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4570 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4571 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4572 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4573 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4574 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4575 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4576 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4577 | // Set the new mute volume. This propagates the values to |
| 4578 | // the audio system, otherwise the volume won't be changed |
| 4579 | // at the lower level. |
| 4580 | sendMsg(mAudioHandler, |
| 4581 | MSG_SET_ALL_VOLUMES, |
| 4582 | SENDMSG_QUEUE, |
| 4583 | 0, |
| 4584 | 0, |
| 4585 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4586 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4587 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4588 | if (changed) { |
| 4589 | // Stream mute changed, fire the intent. |
| 4590 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 4591 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4592 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 4593 | sendBroadcastToAll(intent); |
| 4594 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4595 | } |
| 4596 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4597 | public int getStreamType() { |
| 4598 | return mStreamType; |
| 4599 | } |
| 4600 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4601 | public void checkFixedVolumeDevices() { |
| 4602 | synchronized (VolumeStreamState.class) { |
| 4603 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 4604 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4605 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4606 | int device = mIndexMap.keyAt(i); |
| 4607 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 4608 | if (((device & mFullVolumeDevices) != 0) |
| 4609 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4610 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4611 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4612 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4618 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4619 | if (index < mIndexMin) { |
| 4620 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 4621 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4622 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | return index; |
| 4626 | } |
| 4627 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4628 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4629 | pw.print(" Muted: "); |
| 4630 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4631 | pw.print(" Min: "); |
| 4632 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4633 | pw.print(" Max: "); |
| 4634 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4635 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4636 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4637 | if (i > 0) { |
| 4638 | pw.print(", "); |
| 4639 | } |
| 4640 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4641 | pw.print(Integer.toHexString(device)); |
| 4642 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 4643 | : AudioSystem.getOutputDeviceName(device); |
| 4644 | if (!deviceName.isEmpty()) { |
| 4645 | pw.print(" ("); |
| 4646 | pw.print(deviceName); |
| 4647 | pw.print(")"); |
| 4648 | } |
| 4649 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4650 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4651 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4652 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4653 | pw.println(); |
| 4654 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4655 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4656 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 4657 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 4658 | // (the default device is not returned by getDevicesForStream) |
| 4659 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4660 | if ((devices & device) != 0) { |
| 4661 | if (n++ > 0) { |
| 4662 | pw.print(", "); |
| 4663 | } |
| 4664 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 4665 | } |
| 4666 | i++; |
| 4667 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4668 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | /** Thread that handles native AudioSystem control. */ |
| 4672 | private class AudioSystemThread extends Thread { |
| 4673 | AudioSystemThread() { |
| 4674 | super("AudioService"); |
| 4675 | } |
| 4676 | |
| 4677 | @Override |
| 4678 | public void run() { |
| 4679 | // Set this thread up so the handler will work on it |
| 4680 | Looper.prepare(); |
| 4681 | |
| 4682 | synchronized(AudioService.this) { |
| 4683 | mAudioHandler = new AudioHandler(); |
| 4684 | |
| 4685 | // Notify that the handler has been created |
| 4686 | AudioService.this.notify(); |
| 4687 | } |
| 4688 | |
| 4689 | // Listen for volume change requests that are set by VolumePanel |
| 4690 | Looper.loop(); |
| 4691 | } |
| 4692 | } |
| 4693 | |
| 4694 | /** Handles internal volume messages in separate volume thread. */ |
| 4695 | private class AudioHandler extends Handler { |
| 4696 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4697 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4699 | synchronized (VolumeStreamState.class) { |
| 4700 | // Apply volume |
| 4701 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4702 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4703 | // Apply change to all streams using this one as alias |
| 4704 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4705 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4706 | if (streamType != streamState.mStreamType && |
| 4707 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 4708 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 4709 | // that may have a different device selected |
| 4710 | int streamDevice = getDeviceForStream(streamType); |
| 4711 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 4712 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 4713 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 4714 | } |
| 4715 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4716 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4717 | } |
| 4718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4719 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4720 | sendMsg(mAudioHandler, |
| 4721 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 4722 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4723 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4724 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4725 | streamState, |
| 4726 | PERSIST_DELAY); |
| 4727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4728 | } |
| 4729 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4730 | private void setAllVolumes(VolumeStreamState streamState) { |
| 4731 | |
| 4732 | // Apply volume |
| 4733 | streamState.applyAllVolumes(); |
| 4734 | |
| 4735 | // Apply change to all streams using this one as alias |
| 4736 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4737 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4738 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4739 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4740 | mStreamStates[streamType].applyAllVolumes(); |
| 4741 | } |
| 4742 | } |
| 4743 | } |
| 4744 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4745 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4746 | if (mUseFixedVolume) { |
| 4747 | return; |
| 4748 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4749 | if (mIsSingleVolume && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4750 | return; |
| 4751 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4752 | if (streamState.hasValidSettingsName()) { |
| 4753 | System.putIntForUser(mContentResolver, |
| 4754 | streamState.getSettingNameForDevice(device), |
| 4755 | (streamState.getIndex(device) + 5)/ 10, |
| 4756 | UserHandle.USER_CURRENT); |
| 4757 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4758 | } |
| 4759 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4760 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4761 | if (mUseFixedVolume) { |
| 4762 | return; |
| 4763 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 4764 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4765 | } |
| 4766 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4767 | private boolean onLoadSoundEffects() { |
| 4768 | int status; |
| 4769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4770 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4771 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4772 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 4773 | return false; |
| 4774 | } |
| 4775 | |
| 4776 | if (mSoundPool != null) { |
| 4777 | return true; |
| 4778 | } |
| 4779 | |
| 4780 | loadTouchSoundAssets(); |
| 4781 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 4782 | mSoundPool = new SoundPool.Builder() |
| 4783 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 4784 | .setAudioAttributes(new AudioAttributes.Builder() |
| 4785 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 4786 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 4787 | .build()) |
| 4788 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4789 | mSoundPoolCallBack = null; |
| 4790 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 4791 | mSoundPoolListenerThread.start(); |
| 4792 | int attempts = 3; |
| 4793 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 4794 | try { |
| 4795 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4796 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4797 | } catch (InterruptedException e) { |
| 4798 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | if (mSoundPoolCallBack == null) { |
| 4803 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 4804 | if (mSoundPoolLooper != null) { |
| 4805 | mSoundPoolLooper.quit(); |
| 4806 | mSoundPoolLooper = null; |
| 4807 | } |
| 4808 | mSoundPoolListenerThread = null; |
| 4809 | mSoundPool.release(); |
| 4810 | mSoundPool = null; |
| 4811 | return false; |
| 4812 | } |
| 4813 | /* |
| 4814 | * poolId table: The value -1 in this table indicates that corresponding |
| 4815 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 4816 | * Once loaded, the value in poolId is the sample ID and the same |
| 4817 | * sample can be reused for another effect using the same file. |
| 4818 | */ |
| 4819 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4820 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4821 | poolId[fileIdx] = -1; |
| 4822 | } |
| 4823 | /* |
| 4824 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 4825 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 4826 | * this indicates we have a valid sample loaded for this effect. |
| 4827 | */ |
| 4828 | |
| 4829 | int numSamples = 0; |
| 4830 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4831 | // Do not load sample if this effect uses the MediaPlayer |
| 4832 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 4833 | continue; |
| 4834 | } |
| 4835 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 4836 | String filePath = Environment.getRootDirectory() |
| 4837 | + SOUND_EFFECTS_PATH |
| 4838 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 4839 | int sampleId = mSoundPool.load(filePath, 0); |
| 4840 | if (sampleId <= 0) { |
| 4841 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 4842 | } else { |
| 4843 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 4844 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 4845 | numSamples++; |
| 4846 | } |
| 4847 | } else { |
| 4848 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 4849 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 4850 | } |
| 4851 | } |
| 4852 | // wait for all samples to be loaded |
| 4853 | if (numSamples > 0) { |
| 4854 | mSoundPoolCallBack.setSamples(poolId); |
| 4855 | |
| 4856 | attempts = 3; |
| 4857 | status = 1; |
| 4858 | while ((status == 1) && (attempts-- > 0)) { |
| 4859 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4860 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4861 | status = mSoundPoolCallBack.status(); |
| 4862 | } catch (InterruptedException e) { |
| 4863 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 4864 | } |
| 4865 | } |
| 4866 | } else { |
| 4867 | status = -1; |
| 4868 | } |
| 4869 | |
| 4870 | if (mSoundPoolLooper != null) { |
| 4871 | mSoundPoolLooper.quit(); |
| 4872 | mSoundPoolLooper = null; |
| 4873 | } |
| 4874 | mSoundPoolListenerThread = null; |
| 4875 | if (status != 0) { |
| 4876 | Log.w(TAG, |
| 4877 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4878 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4879 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4880 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | mSoundPool.release(); |
| 4885 | mSoundPool = null; |
| 4886 | } |
| 4887 | } |
| 4888 | return (status == 0); |
| 4889 | } |
| 4890 | |
| 4891 | /** |
| 4892 | * Unloads samples from the sound pool. |
| 4893 | * This method can be called to free some memory when |
| 4894 | * sound effects are disabled. |
| 4895 | */ |
| 4896 | private void onUnloadSoundEffects() { |
| 4897 | synchronized (mSoundEffectsLock) { |
| 4898 | if (mSoundPool == null) { |
| 4899 | return; |
| 4900 | } |
| 4901 | |
| 4902 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4903 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4904 | poolId[fileIdx] = 0; |
| 4905 | } |
| 4906 | |
| 4907 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4908 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4909 | continue; |
| 4910 | } |
| 4911 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4912 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4913 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4914 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4915 | } |
| 4916 | } |
| 4917 | mSoundPool.release(); |
| 4918 | mSoundPool = null; |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4923 | synchronized (mSoundEffectsLock) { |
| 4924 | |
| 4925 | onLoadSoundEffects(); |
| 4926 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4927 | if (mSoundPool == null) { |
| 4928 | return; |
| 4929 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4930 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4931 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4932 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4933 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4934 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4935 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4937 | |
| 4938 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4939 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4940 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | } else { |
| 4942 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4943 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 4944 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 4945 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4946 | mediaPlayer.setDataSource(filePath); |
| 4947 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4948 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4949 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4950 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4951 | public void onCompletion(MediaPlayer mp) { |
| 4952 | cleanupPlayer(mp); |
| 4953 | } |
| 4954 | }); |
| 4955 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4956 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4957 | cleanupPlayer(mp); |
| 4958 | return true; |
| 4959 | } |
| 4960 | }); |
| 4961 | mediaPlayer.start(); |
| 4962 | } catch (IOException ex) { |
| 4963 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4964 | } catch (IllegalArgumentException ex) { |
| 4965 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4966 | } catch (IllegalStateException ex) { |
| 4967 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | } |
| 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | private void cleanupPlayer(MediaPlayer mp) { |
| 4974 | if (mp != null) { |
| 4975 | try { |
| 4976 | mp.stop(); |
| 4977 | mp.release(); |
| 4978 | } catch (IllegalStateException ex) { |
| 4979 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 4984 | private void setForceUse(int usage, int config, String eventSource) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4985 | synchronized (mConnectedDevices) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 4986 | setForceUseInt_SyncDevices(usage, config, eventSource); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4987 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4988 | } |
| 4989 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4990 | private void onPersistSafeVolumeState(int state) { |
| 4991 | Settings.Global.putInt(mContentResolver, |
| 4992 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4993 | state); |
| 4994 | } |
| 4995 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4996 | @Override |
| 4997 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4998 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4999 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5000 | case MSG_SET_DEVICE_VOLUME: |
| 5001 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 5002 | break; |
| 5003 | |
| 5004 | case MSG_SET_ALL_VOLUMES: |
| 5005 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5006 | break; |
| 5007 | |
| 5008 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5009 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5010 | break; |
| 5011 | |
| 5012 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5013 | // note that the value persisted is the current ringer mode, not the |
| 5014 | // 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] | 5015 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5016 | break; |
| 5017 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 5018 | case MSG_AUDIO_SERVER_DIED: |
| 5019 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5020 | break; |
| 5021 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5022 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 5023 | onUnloadSoundEffects(); |
| 5024 | break; |
| 5025 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5026 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5027 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 5028 | // can take several dozens of milliseconds to complete |
| 5029 | boolean loaded = onLoadSoundEffects(); |
| 5030 | if (msg.obj != null) { |
| 5031 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 5032 | synchronized (reply) { |
| 5033 | reply.mStatus = loaded ? 0 : -1; |
| 5034 | reply.notify(); |
| 5035 | } |
| 5036 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5037 | break; |
| 5038 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5039 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5040 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5041 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5042 | |
| 5043 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 5044 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5045 | synchronized (mConnectedDevices) { |
| 5046 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 5047 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5048 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5049 | |
| 5050 | case MSG_SET_FORCE_USE: |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5051 | case MSG_SET_FORCE_BT_A2DP_USE: |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5052 | setForceUse(msg.arg1, msg.arg2, (String) msg.obj); |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5053 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 5054 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5055 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 5056 | resetBluetoothSco(); |
| 5057 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5058 | |
| 5059 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5060 | { WiredDeviceConnectionState connectState = |
| 5061 | (WiredDeviceConnectionState)msg.obj; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5062 | mWiredDevLogger.log(new WiredDevConnectEvent(connectState)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5063 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5064 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5065 | mAudioEventWakeLock.release(); |
| 5066 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5067 | break; |
| 5068 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5069 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 5070 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 5071 | mAudioEventWakeLock.release(); |
| 5072 | break; |
| 5073 | |
| 5074 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 5075 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5076 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5077 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5078 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5079 | case MSG_A2DP_DEVICE_CONFIG_CHANGE: |
| 5080 | onBluetoothA2dpDeviceConfigChange((BluetoothDevice)msg.obj); |
| 5081 | mAudioEventWakeLock.release(); |
| 5082 | break; |
| 5083 | |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 5084 | case MSG_DISABLE_AUDIO_FOR_UID: |
| 5085 | mPlaybackMonitor.disableAudioForUid( msg.arg1 == 1 /* disable */, |
| 5086 | msg.arg2 /* uid */); |
| 5087 | mAudioEventWakeLock.release(); |
| 5088 | break; |
| 5089 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5090 | case MSG_REPORT_NEW_ROUTES: { |
| 5091 | int N = mRoutesObservers.beginBroadcast(); |
| 5092 | if (N > 0) { |
| 5093 | AudioRoutesInfo routes; |
| 5094 | synchronized (mCurAudioRoutes) { |
| 5095 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5096 | } |
| 5097 | while (N > 0) { |
| 5098 | N--; |
| 5099 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 5100 | try { |
| 5101 | obs.dispatchAudioRoutesChanged(routes); |
| 5102 | } catch (RemoteException e) { |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5107 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5108 | break; |
| 5109 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5110 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5111 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5112 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5113 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5114 | |
| 5115 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 5116 | onSendBecomingNoisyIntent(); |
| 5117 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5118 | |
| 5119 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 5120 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5121 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 5122 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5123 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 5124 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 5125 | onPersistSafeVolumeState(msg.arg1); |
| 5126 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 5127 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 5128 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 5129 | onBroadcastScoConnectionState(msg.arg1); |
| 5130 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 5131 | |
| 5132 | case MSG_SYSTEM_READY: |
| 5133 | onSystemReady(); |
| 5134 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5135 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 5136 | case MSG_INDICATE_SYSTEM_READY: |
| 5137 | onIndicateSystemReady(); |
| 5138 | break; |
| 5139 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5140 | case MSG_ACCESSORY_PLUG_MEDIA_UNMUTE: |
| 5141 | onAccessoryPlugMediaUnmute(msg.arg1); |
| 5142 | break; |
| 5143 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5144 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 5145 | final int musicActiveMs = msg.arg1; |
| 5146 | Settings.Secure.putIntForUser(mContentResolver, |
| 5147 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 5148 | UserHandle.USER_CURRENT); |
| 5149 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5150 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 5151 | case MSG_UNMUTE_STREAM: |
| 5152 | onUnmuteStream(msg.arg1, msg.arg2); |
| 5153 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5154 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 5155 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 5156 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 5157 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | } |
| 5161 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5162 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5163 | |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5164 | private int mEncodedSurroundMode; |
| 5165 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5166 | SettingsObserver() { |
| 5167 | super(new Handler()); |
| 5168 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5169 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5170 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5171 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5172 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5173 | Settings.System.MASTER_MONO), false, this); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5174 | |
| 5175 | mEncodedSurroundMode = Settings.Global.getInt( |
| 5176 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5177 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5178 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5179 | Settings.Global.ENCODED_SURROUND_OUTPUT), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | @Override |
| 5183 | public void onChange(boolean selfChange) { |
| 5184 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5185 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 5186 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 5187 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 5188 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5189 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 5190 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5191 | /* |
| 5192 | * Ensure all stream types that should be affected by ringer mode |
| 5193 | * are in the proper state. |
| 5194 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5195 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5196 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5197 | readDockAudioSettings(mContentResolver); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5198 | updateMasterMono(mContentResolver); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5199 | updateEncodedSurroundOutput(); |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | private void updateEncodedSurroundOutput() { |
| 5204 | int newSurroundMode = Settings.Global.getInt( |
| 5205 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5206 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5207 | // Did it change? |
| 5208 | if (mEncodedSurroundMode != newSurroundMode) { |
| 5209 | // Send to AudioPolicyManager |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5210 | sendEncodedSurroundMode(newSurroundMode, "SettingsObserver"); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5211 | synchronized(mConnectedDevices) { |
| 5212 | // Is HDMI connected? |
| 5213 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HDMI, ""); |
| 5214 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5215 | if (deviceSpec != null) { |
| 5216 | // Toggle HDMI to retrigger broadcast with proper formats. |
| 5217 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5218 | AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "", |
| 5219 | "android"); // disconnect |
| 5220 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5221 | AudioSystem.DEVICE_STATE_AVAILABLE, "", "", |
| 5222 | "android"); // reconnect |
| 5223 | } |
| 5224 | } |
| 5225 | mEncodedSurroundMode = newSurroundMode; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5226 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5227 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5228 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5229 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5230 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5231 | private void makeA2dpDeviceAvailable(String address, String name, String eventSource) { |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5232 | // enable A2DP before notifying A2DP connection to avoid unnecessary processing in |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5233 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5234 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 5235 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 5236 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5237 | setBluetoothA2dpOnInt(true, eventSource); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5238 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5239 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5240 | // Reset A2DP suspend state each time a new sink is connected |
| 5241 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5242 | mConnectedDevices.put( |
| 5243 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5244 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5245 | address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5246 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5247 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, null, 0); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5248 | } |
| 5249 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5250 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5251 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 5252 | } |
| 5253 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5254 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5255 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5256 | synchronized (mA2dpAvrcpLock) { |
| 5257 | mAvrcpAbsVolSupported = false; |
| 5258 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5259 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5260 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5261 | mConnectedDevices.remove( |
| 5262 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 5263 | synchronized (mCurAudioRoutes) { |
| 5264 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5265 | if (mCurAudioRoutes.bluetoothName != null) { |
| 5266 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 5267 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5268 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5269 | } |
| 5270 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5271 | } |
| 5272 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5273 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5274 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 5275 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 5276 | // reconnection of the sink. |
| 5277 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5278 | // 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] | 5279 | mConnectedDevices.remove( |
| 5280 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5281 | // send the delayed message to make the device unavailable later |
| 5282 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5283 | mAudioHandler.sendMessageDelayed(msg, delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5284 | |
| 5285 | } |
| 5286 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5287 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5288 | private void makeA2dpSrcAvailable(String address) { |
| 5289 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5290 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5291 | mConnectedDevices.put( |
| 5292 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5293 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5294 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | // must be called synchronized on mConnectedDevices |
| 5298 | private void makeA2dpSrcUnavailable(String address) { |
| 5299 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5300 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5301 | mConnectedDevices.remove( |
| 5302 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5306 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5307 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5308 | } |
| 5309 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5310 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5311 | private boolean hasScheduledA2dpDockTimeout() { |
| 5312 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5313 | } |
| 5314 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5315 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5316 | { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 5317 | if (DEBUG_DEVICES) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5318 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice=" + btDevice+"state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5319 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5320 | if (btDevice == null) { |
| 5321 | return; |
| 5322 | } |
| 5323 | String address = btDevice.getAddress(); |
| 5324 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5325 | address = ""; |
| 5326 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5327 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5328 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5329 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 5330 | btDevice.getAddress()); |
| 5331 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5332 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5333 | |
| 5334 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5335 | if (btDevice.isBluetoothDock()) { |
| 5336 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 5337 | // introduction of a delay for transient disconnections of docks when |
| 5338 | // power is rapidly turned off/on, this message will be canceled if |
| 5339 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5340 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5341 | // the next time isConnected is evaluated, it will be false for the dock |
| 5342 | } |
| 5343 | } else { |
| 5344 | makeA2dpDeviceUnavailableNow(address); |
| 5345 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5346 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5347 | if (mCurAudioRoutes.bluetoothName != null) { |
| 5348 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5349 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5350 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5351 | } |
| 5352 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5353 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5354 | if (btDevice.isBluetoothDock()) { |
| 5355 | // this could be a reconnection after a transient disconnection |
| 5356 | cancelA2dpDeviceTimeout(); |
| 5357 | mDockAddress = address; |
| 5358 | } else { |
| 5359 | // this could be a connection of another A2DP device before the timeout of |
| 5360 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 5361 | if(hasScheduledA2dpDockTimeout()) { |
| 5362 | cancelA2dpDeviceTimeout(); |
| 5363 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 5364 | } |
| 5365 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5366 | makeA2dpDeviceAvailable(address, btDevice.getName(), |
| 5367 | "onSetA2dpSinkConnectionState"); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5368 | synchronized (mCurAudioRoutes) { |
| 5369 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5370 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 5371 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5372 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5373 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5374 | } |
| 5375 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5376 | } |
| 5377 | } |
| 5378 | } |
| 5379 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5380 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 5381 | { |
| 5382 | if (DEBUG_VOL) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5383 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice=" + btDevice + " state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5384 | } |
| 5385 | if (btDevice == null) { |
| 5386 | return; |
| 5387 | } |
| 5388 | String address = btDevice.getAddress(); |
| 5389 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5390 | address = ""; |
| 5391 | } |
| 5392 | |
| 5393 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5394 | final String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 5395 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5396 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5397 | |
| 5398 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5399 | makeA2dpSrcUnavailable(address); |
| 5400 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5401 | makeA2dpSrcAvailable(address); |
| 5402 | } |
| 5403 | } |
| 5404 | } |
| 5405 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5406 | private void onBluetoothA2dpDeviceConfigChange(BluetoothDevice btDevice) |
| 5407 | { |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5408 | if (DEBUG_DEVICES) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5409 | Log.d(TAG, "onBluetoothA2dpDeviceConfigChange btDevice=" + btDevice); |
| 5410 | } |
| 5411 | if (btDevice == null) { |
| 5412 | return; |
| 5413 | } |
| 5414 | String address = btDevice.getAddress(); |
| 5415 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5416 | address = ""; |
| 5417 | } |
| 5418 | |
| 5419 | int device = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; |
| 5420 | synchronized (mConnectedDevices) { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 5421 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, btDevice)) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5422 | return; |
| 5423 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5424 | final String key = makeDeviceListKey(device, address); |
| 5425 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5426 | if (deviceSpec != null) { |
| 5427 | // Device is connected |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5428 | int musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5429 | if (AudioSystem.handleDeviceConfigChange(device, address, |
| 5430 | btDevice.getName()) != AudioSystem.AUDIO_STATUS_OK) { |
| 5431 | // force A2DP device disconnection in case of error so that AudioService state is |
| 5432 | // consistent with audio policy manager state |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5433 | setBluetoothA2dpDeviceConnectionStateInt( |
| 5434 | btDevice, BluetoothA2dp.STATE_DISCONNECTED, BluetoothProfile.A2DP, |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 5435 | false /* suppressNoisyIntent */, musicDevice); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5436 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5441 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 5442 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 5443 | synchronized (mA2dpAvrcpLock) { |
| 5444 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5445 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 5446 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5447 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 5448 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 5449 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5450 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5451 | } |
| 5452 | } |
| 5453 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5454 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 5455 | String deviceName) { |
| 5456 | if (DEBUG_DEVICES) { |
| 5457 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 5458 | + " address:" + address + " name:" + deviceName + ")"); |
| 5459 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5460 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5461 | String deviceKey = makeDeviceListKey(device, address); |
| 5462 | if (DEBUG_DEVICES) { |
| 5463 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 5464 | } |
| 5465 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 5466 | boolean isConnected = deviceSpec != null; |
| 5467 | if (DEBUG_DEVICES) { |
| 5468 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 5469 | } |
| 5470 | if (connect && !isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5471 | final int res = AudioSystem.setDeviceConnectionState(device, |
| 5472 | AudioSystem.DEVICE_STATE_AVAILABLE, address, deviceName); |
| 5473 | if (res != AudioSystem.AUDIO_STATUS_OK) { |
| 5474 | Slog.e(TAG, "not connecting device 0x" + Integer.toHexString(device) + |
| 5475 | " due to command error " + res ); |
| 5476 | return false; |
| 5477 | } |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5478 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5479 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5480 | device, 0, null, 0); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5481 | return true; |
| 5482 | } else if (!connect && isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5483 | AudioSystem.setDeviceConnectionState(device, |
| 5484 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, deviceName); |
| 5485 | // always remove even if disconnection failed |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5486 | mConnectedDevices.remove(deviceKey); |
| 5487 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | return false; |
| 5491 | } |
| 5492 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5493 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
Eric Laurent | 9a5b262 | 2017-04-18 18:20:56 -0700 | [diff] [blame] | 5494 | // sent if: |
| 5495 | // - none of these devices are connected anymore after one is disconnected AND |
| 5496 | // - the device being disconnected is actually used for music. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5497 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5498 | int mBecomingNoisyIntentDevices = |
| 5499 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5500 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 5501 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 5502 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5503 | |
| 5504 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5505 | // Called synchronized on mConnectedDevices |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5506 | // musicDevice argument is used when not AudioSystem.DEVICE_NONE instead of querying |
| 5507 | // from AudioSystem |
| 5508 | private int checkSendBecomingNoisyIntent(int device, int state, int musicDevice) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5509 | int delay = 0; |
| 5510 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 5511 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 5512 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 5513 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5514 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 5515 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 5516 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5517 | } |
| 5518 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5519 | if (musicDevice == AudioSystem.DEVICE_NONE) { |
| 5520 | musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 5521 | } |
| 5522 | // ignore condition on device being actually used for music when in communication |
| 5523 | // because music routing is altered in this case. |
| 5524 | if (((device == musicDevice) || isInCommunication()) && (device == devices)) { |
| 5525 | mAudioHandler.removeMessages(MSG_BROADCAST_AUDIO_BECOMING_NOISY); |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5526 | sendMsg(mAudioHandler, |
| 5527 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5528 | SENDMSG_REPLACE, |
| 5529 | 0, |
| 5530 | 0, |
| 5531 | null, |
| 5532 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5533 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5534 | } |
| 5535 | } |
| 5536 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5537 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 5538 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5539 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 5540 | synchronized (mLastDeviceConnectMsgTime) { |
| 5541 | long time = SystemClock.uptimeMillis(); |
| 5542 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 5543 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 5544 | } |
| 5545 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5546 | } |
| 5547 | return delay; |
| 5548 | } |
| 5549 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5550 | private void updateAudioRoutes(int device, int state) |
| 5551 | { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5552 | int connType = 0; |
| 5553 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5554 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5555 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 5556 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 5557 | device == AudioSystem.DEVICE_OUT_LINE) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5558 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 5559 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 5560 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5561 | connType = AudioRoutesInfo.MAIN_HDMI; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5562 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE|| |
| 5563 | device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5564 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5565 | } |
| 5566 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5567 | synchronized (mCurAudioRoutes) { |
| 5568 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5569 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5570 | if (state != 0) { |
| 5571 | newConn |= connType; |
| 5572 | } else { |
| 5573 | newConn &= ~connType; |
| 5574 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5575 | if (newConn != mCurAudioRoutes.mainType) { |
| 5576 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5577 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5578 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5579 | } |
| 5580 | } |
| 5581 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 5585 | String deviceName) { |
| 5586 | if (DEBUG_DEVICES) { |
| 5587 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 5588 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 5589 | " name:" + deviceName + ");"); |
| 5590 | } |
| 5591 | Intent intent = new Intent(); |
| 5592 | |
| 5593 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
| 5594 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 5595 | intent.putExtra("microphone", 1); |
| 5596 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 5597 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 5598 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 5599 | intent.putExtra("microphone", 0); |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 5600 | } else if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 5601 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 5602 | intent.putExtra("microphone", |
| 5603 | AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_IN_USB_HEADSET, "") |
| 5604 | == AudioSystem.DEVICE_STATE_AVAILABLE ? 1 : 0); |
| 5605 | } else if (device == AudioSystem.DEVICE_IN_USB_HEADSET) { |
| 5606 | if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_USB_HEADSET, "") |
| 5607 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
| 5608 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 5609 | intent.putExtra("microphone", 1); |
| 5610 | } else { |
| 5611 | // do not send ACTION_HEADSET_PLUG when only the input side is seen as changing |
| 5612 | return; |
| 5613 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5614 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 5615 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
| 5616 | configureHdmiPlugIntent(intent, state); |
| 5617 | } |
| 5618 | |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 5619 | if (intent.getAction() == null) { |
| 5620 | return; |
| 5621 | } |
| 5622 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5623 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 5624 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 5625 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 5626 | |
| 5627 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5628 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5629 | final long ident = Binder.clearCallingIdentity(); |
| 5630 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5631 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5632 | } finally { |
| 5633 | Binder.restoreCallingIdentity(ident); |
| 5634 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5635 | } |
| 5636 | |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 5637 | private static final int DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG = |
| 5638 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 5639 | AudioSystem.DEVICE_OUT_LINE | |
| 5640 | AudioSystem.DEVICE_OUT_ALL_USB; |
| 5641 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5642 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5643 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5644 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5645 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 5646 | + " state:" + Integer.toHexString(state) |
| 5647 | + " address:" + address |
| 5648 | + " deviceName:" + deviceName |
| 5649 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5650 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5651 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5652 | synchronized (mConnectedDevices) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 5653 | if ((state == 0) && ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0)) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5654 | setBluetoothA2dpOnInt(true, "onSetWiredDeviceConnectionState state 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5655 | } |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 5656 | |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5657 | if (!handleDeviceConnection(state == 1, device, address, deviceName)) { |
| 5658 | // change of connection state failed, bailout |
| 5659 | return; |
| 5660 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5661 | if (state != 0) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 5662 | if ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5663 | setBluetoothA2dpOnInt(false, "onSetWiredDeviceConnectionState state not 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5664 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5665 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 5666 | sendMsg(mAudioHandler, |
| 5667 | MSG_CHECK_MUSIC_ACTIVE, |
| 5668 | SENDMSG_REPLACE, |
| 5669 | 0, |
| 5670 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5671 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5672 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5673 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5674 | // Television devices without CEC service apply software volume on HDMI output |
| 5675 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5676 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 5677 | checkAllFixedVolumeDevices(); |
| 5678 | if (mHdmiManager != null) { |
| 5679 | synchronized (mHdmiManager) { |
| 5680 | if (mHdmiPlaybackClient != null) { |
| 5681 | mHdmiCecSink = false; |
| 5682 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 5683 | } |
| 5684 | } |
| 5685 | } |
| 5686 | } |
| 5687 | } else { |
| 5688 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5689 | if (mHdmiManager != null) { |
| 5690 | synchronized (mHdmiManager) { |
| 5691 | mHdmiCecSink = false; |
| 5692 | } |
| 5693 | } |
| 5694 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5695 | } |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 5696 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5697 | updateAudioRoutes(device, state); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5698 | } |
| 5699 | } |
| 5700 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5701 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5702 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 5703 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5704 | if (state == 1) { |
| 5705 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 5706 | int[] portGeneration = new int[1]; |
| 5707 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 5708 | if (status == AudioManager.SUCCESS) { |
| 5709 | for (AudioPort port : ports) { |
| 5710 | if (port instanceof AudioDevicePort) { |
| 5711 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 5712 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 5713 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5714 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 5715 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5716 | if (formats.length > 0) { |
| 5717 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 5718 | for (int format : formats) { |
| 5719 | // a format in the list can be 0, skip it |
| 5720 | if (format != AudioFormat.ENCODING_INVALID) { |
| 5721 | encodingList.add(format); |
| 5722 | } |
| 5723 | } |
| 5724 | int[] encodingArray = new int[encodingList.size()]; |
| 5725 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 5726 | encodingArray[i] = encodingList.get(i); |
| 5727 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5728 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5729 | } |
| 5730 | // find the maximum supported number of channels |
| 5731 | int maxChannels = 0; |
| 5732 | for (int mask : devicePort.channelMasks()) { |
| 5733 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 5734 | if (channelCount > maxChannels) { |
| 5735 | maxChannels = channelCount; |
| 5736 | } |
| 5737 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5738 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | } |
| 5743 | } |
| 5744 | } |
| 5745 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5746 | /* cache of the address of the last dock the device was connected to */ |
| 5747 | private String mDockAddress; |
| 5748 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5749 | /** |
| 5750 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 5751 | */ |
| 5752 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 5753 | @Override |
| 5754 | public void onReceive(Context context, Intent intent) { |
| 5755 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 5756 | int outDevice; |
| 5757 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5758 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5759 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5760 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 5761 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 5762 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 5763 | int config; |
| 5764 | switch (dockState) { |
| 5765 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 5766 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 5767 | break; |
| 5768 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 5769 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 5770 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5771 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5772 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5773 | break; |
| 5774 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 5775 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 5776 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5777 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 5778 | default: |
| 5779 | config = AudioSystem.FORCE_NONE; |
| 5780 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5781 | // Low end docks have a menu to enable or disable audio |
| 5782 | // (see mDockAudioMediaEnabled) |
| 5783 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 5784 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 5785 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5786 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, config, |
| 5787 | "ACTION_DOCK_EVENT intent")); |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5788 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 5789 | } |
| 5790 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5791 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5792 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5793 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 5794 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 5795 | setBtScoDeviceConnectionState(btDevice, state); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 5796 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5797 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5798 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5799 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5800 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5801 | // broadcast intent if the connection was initated by AudioService |
| 5802 | if (!mScoClients.isEmpty() && |
| 5803 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 5804 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 5805 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5806 | broadcast = true; |
| 5807 | } |
| 5808 | switch (btState) { |
| 5809 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5810 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5811 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5812 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5813 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5814 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5815 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5816 | break; |
| 5817 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5818 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5819 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 5820 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5821 | break; |
| 5822 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5823 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5824 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5825 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5826 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5827 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5828 | default: |
| 5829 | // do not broadcast CONNECTING or invalid state |
| 5830 | broadcast = false; |
| 5831 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5832 | } |
| 5833 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5834 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5835 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5836 | //FIXME: this is to maintain compatibility with deprecated intent |
| 5837 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5838 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5839 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5840 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5841 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5842 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5843 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5844 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5845 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5846 | AudioSystem.setParameters("screen_state=on"); |
| 5847 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5848 | if (mMonitorRotation) { |
| 5849 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5850 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5851 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5852 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 5853 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5854 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5855 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5856 | if (mUserSwitchedReceived) { |
| 5857 | // attempt to stop music playback for background user except on first user |
| 5858 | // switch (i.e. first boot) |
| 5859 | sendMsg(mAudioHandler, |
| 5860 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5861 | SENDMSG_REPLACE, |
| 5862 | 0, |
| 5863 | 0, |
| 5864 | null, |
| 5865 | 0); |
| 5866 | } |
| 5867 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5868 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5869 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5870 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5871 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5872 | readAudioSettings(true /*userSwitch*/); |
| 5873 | // preserve STREAM_MUSIC volume from one user to the next. |
| 5874 | sendMsg(mAudioHandler, |
| 5875 | MSG_SET_ALL_VOLUMES, |
| 5876 | SENDMSG_QUEUE, |
| 5877 | 0, |
| 5878 | 0, |
| 5879 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5880 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 5881 | // Disable audio recording for the background user/profile |
| 5882 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5883 | if (userId >= 0) { |
| 5884 | // TODO Kill recording streams instead of killing processes holding permission |
| 5885 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 5886 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 5887 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5888 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5889 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 5890 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 5891 | // Enable audio recording for foreground user/profile |
| 5892 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5893 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5894 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 5895 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 5896 | state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 5897 | if (state == BluetoothAdapter.STATE_OFF || |
| 5898 | state == BluetoothAdapter.STATE_TURNING_OFF) { |
| 5899 | disconnectAllBluetoothProfiles(); |
| 5900 | } |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 5901 | } else if (action.equals(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION) || |
| 5902 | action.equals(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)) { |
| 5903 | handleAudioEffectBroadcast(context, intent); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5904 | } |
| 5905 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5906 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5907 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5908 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 5909 | |
| 5910 | @Override |
| 5911 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 5912 | Bundle prevRestrictions) { |
| 5913 | // Update mic mute state. |
| 5914 | { |
| 5915 | final boolean wasRestricted = |
| 5916 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5917 | final boolean isRestricted = |
| 5918 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5919 | if (wasRestricted != isRestricted) { |
| 5920 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | // Update speaker mute state. |
| 5925 | { |
| 5926 | final boolean wasRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5927 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 5928 | || prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5929 | final boolean isRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5930 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 5931 | || newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5932 | if (wasRestricted != isRestricted) { |
| 5933 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 5934 | } |
| 5935 | } |
| 5936 | } |
| 5937 | } // end class AudioServiceUserRestrictionsListener |
| 5938 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 5939 | private void handleAudioEffectBroadcast(Context context, Intent intent) { |
| 5940 | String target = intent.getPackage(); |
| 5941 | if (target != null) { |
| 5942 | Log.w(TAG, "effect broadcast already targeted to " + target); |
| 5943 | return; |
| 5944 | } |
| 5945 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 5946 | // TODO this should target a user-selected panel |
| 5947 | List<ResolveInfo> ril = context.getPackageManager().queryBroadcastReceivers( |
| 5948 | intent, 0 /* flags */); |
| 5949 | if (ril != null && ril.size() != 0) { |
| 5950 | ResolveInfo ri = ril.get(0); |
| 5951 | if (ri != null && ri.activityInfo != null && ri.activityInfo.packageName != null) { |
| 5952 | intent.setPackage(ri.activityInfo.packageName); |
| 5953 | context.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 5954 | return; |
| 5955 | } |
| 5956 | } |
| 5957 | Log.w(TAG, "couldn't find receiver package for effect intent"); |
| 5958 | } |
| 5959 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5960 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 5961 | PackageManager pm = mContext.getPackageManager(); |
| 5962 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 5963 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 5964 | ComponentName homeActivityName = null; |
| 5965 | if (!oldUser.isManagedProfile()) { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 5966 | homeActivityName = mActivityManagerInternal.getHomeActivityForUser(oldUser.id); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5967 | } |
| 5968 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 5969 | List<PackageInfo> packages; |
| 5970 | try { |
| 5971 | packages = AppGlobals.getPackageManager() |
| 5972 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 5973 | } catch (RemoteException e) { |
| 5974 | throw new AndroidRuntimeException(e); |
| 5975 | } |
| 5976 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 5977 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 5978 | // Skip system processes |
| 5979 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 5980 | continue; |
| 5981 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 5982 | // Skip packages that have permission to interact across users |
| 5983 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 5984 | == PackageManager.PERMISSION_GRANTED) { |
| 5985 | continue; |
| 5986 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5987 | if (homeActivityName != null |
| 5988 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 5989 | && pkg.applicationInfo.isSystemApp()) { |
| 5990 | continue; |
| 5991 | } |
| 5992 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5993 | final int uid = pkg.applicationInfo.uid; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5994 | ActivityManager.getService().killUid(UserHandle.getAppId(uid), |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5995 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5996 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 5997 | } catch (RemoteException e) { |
| 5998 | Log.w(TAG, "Error calling killUid", e); |
| 5999 | } |
| 6000 | } |
| 6001 | } |
| 6002 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6003 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6004 | //========================================================================================== |
| 6005 | // Audio Focus |
| 6006 | //========================================================================================== |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6007 | /** |
| 6008 | * Returns whether a focus request is eligible to force ducking. |
| 6009 | * Will return true if: |
| 6010 | * - the AudioAttributes have a usage of USAGE_ASSISTANCE_ACCESSIBILITY, |
| 6011 | * - the focus request is AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 6012 | * - the associated Bundle has KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING set to true, |
| 6013 | * - the uid of the requester is a known accessibility service or root. |
| 6014 | * @param aa AudioAttributes of the focus request |
| 6015 | * @param uid uid of the focus requester |
| 6016 | * @return true if ducking is to be forced |
| 6017 | */ |
| 6018 | private boolean forceFocusDuckingForAccessibility(@Nullable AudioAttributes aa, |
| 6019 | int request, int uid) { |
| 6020 | if (aa == null || aa.getUsage() != AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY |
| 6021 | || request != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) { |
| 6022 | return false; |
| 6023 | } |
| 6024 | final Bundle extraInfo = aa.getBundle(); |
| 6025 | if (extraInfo == null || |
| 6026 | !extraInfo.getBoolean(AudioFocusRequest.KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING)) { |
| 6027 | return false; |
| 6028 | } |
| 6029 | if (uid == 0) { |
| 6030 | return true; |
| 6031 | } |
| 6032 | synchronized (mAccessibilityServiceUidsLock) { |
| 6033 | if (mAccessibilityServiceUids != null) { |
| 6034 | int callingUid = Binder.getCallingUid(); |
| 6035 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 6036 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 6037 | return true; |
| 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | } |
| 6042 | return false; |
| 6043 | } |
| 6044 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6045 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6046 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 6047 | IAudioPolicyCallback pcb, int sdk) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6048 | // permission checks |
| 6049 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6050 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6051 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 6052 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 6053 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 6054 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6055 | } |
| 6056 | } else { |
| 6057 | // only a registered audio policy can be used to lock focus |
| 6058 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6059 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6060 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6061 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6062 | } |
| 6063 | } |
| 6064 | } |
| 6065 | } |
| 6066 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6067 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6068 | clientId, callingPackageName, flags, sdk, |
| 6069 | forceFocusDuckingForAccessibility(aa, durationHint, Binder.getCallingUid())); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6070 | } |
| 6071 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6072 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa, |
| 6073 | String callingPackageName) { |
| 6074 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | public void unregisterAudioFocusClient(String clientId) { |
| 6078 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6079 | } |
| 6080 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 6081 | public int getCurrentAudioFocus() { |
| 6082 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 6083 | } |
| 6084 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 6085 | public int getFocusRampTimeMs(int focusGain, AudioAttributes attr) { |
| 6086 | return mMediaFocusControl.getFocusRampTimeMs(focusGain, attr); |
| 6087 | } |
| 6088 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6089 | //========================================================================================== |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6090 | private boolean readCameraSoundForced() { |
| 6091 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 6092 | mContext.getResources().getBoolean( |
| 6093 | com.android.internal.R.bool.config_camera_sound_forced); |
| 6094 | } |
| 6095 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6096 | //========================================================================================== |
| 6097 | // Device orientation |
| 6098 | //========================================================================================== |
| 6099 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 6100 | * Handles device configuration changes that may map to a change in the orientation |
| 6101 | * or orientation. |
| 6102 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 6103 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6104 | */ |
| 6105 | private void handleConfigurationChanged(Context context) { |
| 6106 | try { |
| 6107 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 6108 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6109 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 6110 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6111 | if (mMonitorOrientation) { |
| 6112 | int newOrientation = config.orientation; |
| 6113 | if (newOrientation != mDeviceOrientation) { |
| 6114 | mDeviceOrientation = newOrientation; |
| 6115 | setOrientationForAudioSystem(); |
| 6116 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6117 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6118 | sendMsg(mAudioHandler, |
| 6119 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 6120 | SENDMSG_REPLACE, |
| 6121 | 0, |
| 6122 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6123 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6124 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6125 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6126 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6127 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6128 | final boolean cameraSoundForcedChanged = (cameraSoundForced != mCameraSoundForced); |
| 6129 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6130 | if (cameraSoundForcedChanged) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 6131 | if (!mIsSingleVolume) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6132 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 6133 | if (cameraSoundForced) { |
| 6134 | s.setAllIndexesToMax(); |
| 6135 | mRingerModeAffectedStreams &= |
| 6136 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6137 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6138 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6139 | mRingerModeAffectedStreams |= |
| 6140 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6141 | } |
| 6142 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6143 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | sendMsg(mAudioHandler, |
| 6147 | MSG_SET_FORCE_USE, |
| 6148 | SENDMSG_QUEUE, |
| 6149 | AudioSystem.FOR_SYSTEM, |
| 6150 | cameraSoundForced ? |
| 6151 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6152 | new String("handleConfigurationChanged"), |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6153 | 0); |
| 6154 | |
| 6155 | sendMsg(mAudioHandler, |
| 6156 | MSG_SET_ALL_VOLUMES, |
| 6157 | SENDMSG_QUEUE, |
| 6158 | 0, |
| 6159 | 0, |
| 6160 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 6161 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6162 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6163 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6164 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 6165 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 6169 | //TODO move to an external "orientation helper" class |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6170 | private void setOrientationForAudioSystem() { |
| 6171 | switch (mDeviceOrientation) { |
| 6172 | case Configuration.ORIENTATION_LANDSCAPE: |
| 6173 | //Log.i(TAG, "orientation is landscape"); |
| 6174 | AudioSystem.setParameters("orientation=landscape"); |
| 6175 | break; |
| 6176 | case Configuration.ORIENTATION_PORTRAIT: |
| 6177 | //Log.i(TAG, "orientation is portrait"); |
| 6178 | AudioSystem.setParameters("orientation=portrait"); |
| 6179 | break; |
| 6180 | case Configuration.ORIENTATION_SQUARE: |
| 6181 | //Log.i(TAG, "orientation is square"); |
| 6182 | AudioSystem.setParameters("orientation=square"); |
| 6183 | break; |
| 6184 | case Configuration.ORIENTATION_UNDEFINED: |
| 6185 | //Log.i(TAG, "orientation is undefined"); |
| 6186 | AudioSystem.setParameters("orientation=undefined"); |
| 6187 | break; |
| 6188 | default: |
| 6189 | Log.e(TAG, "Unknown orientation"); |
| 6190 | } |
| 6191 | } |
| 6192 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6193 | // Handles request to override default use of A2DP for media. |
| 6194 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6195 | public void setBluetoothA2dpOnInt(boolean on, String eventSource) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6196 | synchronized (mBluetoothA2dpEnabledLock) { |
| 6197 | mBluetoothA2dpEnabled = on; |
| 6198 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
| 6199 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6200 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 6201 | eventSource); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6202 | } |
| 6203 | } |
| 6204 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6205 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6206 | private void setForceUseInt_SyncDevices(int usage, int config, String eventSource) { |
Eric Laurent | 9a5b262 | 2017-04-18 18:20:56 -0700 | [diff] [blame] | 6207 | if (usage == AudioSystem.FOR_MEDIA) { |
| 6208 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6209 | SENDMSG_NOOP, 0, 0, null, 0); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6210 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6211 | mForceUseLogger.log(new ForceUseEvent(usage, config, eventSource)); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6212 | AudioSystem.setForceUse(usage, config); |
| 6213 | } |
| 6214 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6215 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 6216 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 6217 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 6218 | mRingtonePlayer = player; |
| 6219 | } |
| 6220 | |
| 6221 | @Override |
| 6222 | public IRingtonePlayer getRingtonePlayer() { |
| 6223 | return mRingtonePlayer; |
| 6224 | } |
| 6225 | |
| 6226 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6227 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 6228 | synchronized (mCurAudioRoutes) { |
| 6229 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 6230 | mRoutesObservers.register(observer); |
| 6231 | return routes; |
| 6232 | } |
| 6233 | } |
| 6234 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6235 | |
| 6236 | //========================================================================================== |
| 6237 | // Safe media volume management. |
| 6238 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 6239 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 6240 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 6241 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 6242 | // property. Platforms with a different limit must set this property accordingly in their |
| 6243 | // overlay. |
| 6244 | //========================================================================================== |
| 6245 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6246 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 6247 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 6248 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 6249 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 6250 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 6251 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6252 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 6253 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 6254 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 6255 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6256 | private Integer mSafeMediaVolumeState; |
| 6257 | |
| 6258 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6259 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6260 | private int mSafeMediaVolumeIndex; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6261 | // mSafeUsbMediaVolumeIndex is used for USB Headsets and is the music volume UI index |
| 6262 | // corresponding to a gain of -30 dBFS in audio flinger mixer. |
Eric Laurent | 0e5deb3 | 2017-09-01 15:12:42 -0700 | [diff] [blame] | 6263 | // We remove -22 dBs from the theoretical -15dB to account for the EQ + bass boost |
| 6264 | // amplification when both effects are on with all band gains at maximum. |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6265 | // This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when |
| 6266 | // the headset is compliant to EN 60950 with a max loudness of 100dB SPL. |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6267 | private int mSafeUsbMediaVolumeIndex; |
Eric Laurent | 0e5deb3 | 2017-09-01 15:12:42 -0700 | [diff] [blame] | 6268 | private static final float SAFE_VOLUME_GAIN_DBFS = -37.0f; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6269 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 6270 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6271 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 6272 | AudioSystem.DEVICE_OUT_USB_HEADSET; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6273 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 6274 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 6275 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 6276 | private int mMusicActiveMs; |
| 6277 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 6278 | 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] | 6279 | 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] | 6280 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6281 | private int safeMediaVolumeIndex(int device) { |
| 6282 | if ((device & mSafeMediaVolumeDevices) == 0) { |
| 6283 | return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 6284 | } |
| 6285 | if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 6286 | return mSafeUsbMediaVolumeIndex; |
| 6287 | } else { |
| 6288 | return mSafeMediaVolumeIndex; |
| 6289 | } |
| 6290 | } |
| 6291 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6292 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6293 | synchronized (mSafeMediaVolumeState) { |
| 6294 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 6295 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 6296 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 6297 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6298 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6299 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 6300 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 6301 | mMusicActiveMs = 1; // nonzero = confirmed |
| 6302 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6303 | sendMsg(mAudioHandler, |
| 6304 | MSG_CHECK_MUSIC_ACTIVE, |
| 6305 | SENDMSG_REPLACE, |
| 6306 | 0, |
| 6307 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6308 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6309 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 6310 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6311 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6312 | } |
| 6313 | } |
| 6314 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6315 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6316 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6317 | int devices = mSafeMediaVolumeDevices; |
| 6318 | int i = 0; |
| 6319 | |
| 6320 | while (devices != 0) { |
| 6321 | int device = 1 << i++; |
| 6322 | if ((device & devices) == 0) { |
| 6323 | continue; |
| 6324 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 6325 | int index = streamState.getIndex(device); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6326 | if (index > safeMediaVolumeIndex(device)) { |
| 6327 | streamState.setIndex(safeMediaVolumeIndex(device), device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 6328 | sendMsg(mAudioHandler, |
| 6329 | MSG_SET_DEVICE_VOLUME, |
| 6330 | SENDMSG_QUEUE, |
| 6331 | device, |
| 6332 | 0, |
| 6333 | streamState, |
| 6334 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6335 | } |
| 6336 | devices &= ~device; |
| 6337 | } |
| 6338 | } |
| 6339 | |
| 6340 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6341 | synchronized (mSafeMediaVolumeState) { |
| 6342 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6343 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 6344 | ((device & mSafeMediaVolumeDevices) != 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6345 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6346 | return false; |
| 6347 | } |
| 6348 | return true; |
| 6349 | } |
| 6350 | } |
| 6351 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6352 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6353 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6354 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6355 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6356 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 6357 | if (mPendingVolumeCommand != null) { |
| 6358 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 6359 | mPendingVolumeCommand.mIndex, |
| 6360 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6361 | mPendingVolumeCommand.mDevice, |
| 6362 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 6363 | mPendingVolumeCommand = null; |
| 6364 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6368 | //========================================================================================== |
| 6369 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 6370 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 6371 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6372 | //========================================================================================== |
| 6373 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6374 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 6375 | public void onComplete(int status) { |
| 6376 | if (mHdmiManager != null) { |
| 6377 | synchronized (mHdmiManager) { |
| 6378 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 6379 | // Television devices without CEC service apply software volume on HDMI output |
| 6380 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 6381 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 6382 | } |
| 6383 | checkAllFixedVolumeDevices(); |
| 6384 | } |
| 6385 | } |
| 6386 | } |
| 6387 | }; |
| 6388 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6389 | // If HDMI-CEC system audio is supported |
| 6390 | private boolean mHdmiSystemAudioSupported = false; |
| 6391 | // Set only when device is tv. |
| 6392 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 6393 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6394 | // cached HdmiControlManager interface |
| 6395 | private HdmiControlManager mHdmiManager; |
| 6396 | // Set only when device is a set-top box. |
| 6397 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 6398 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 6399 | private boolean mHdmiCecSink; |
| 6400 | |
| 6401 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6402 | |
| 6403 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 6404 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6405 | int device = AudioSystem.DEVICE_NONE; |
| 6406 | if (mHdmiManager != null) { |
| 6407 | synchronized (mHdmiManager) { |
| 6408 | if (mHdmiTvClient == null) { |
| 6409 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 6410 | return device; |
| 6411 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6412 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6413 | synchronized (mHdmiTvClient) { |
| 6414 | if (mHdmiSystemAudioSupported != on) { |
| 6415 | mHdmiSystemAudioSupported = on; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6416 | final int config = on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 6417 | AudioSystem.FORCE_NONE; |
| 6418 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 6419 | config, "setHdmiSystemAudioSupported")); |
| 6420 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, config); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6421 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 6422 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6423 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6424 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6425 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6426 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6427 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6428 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 6429 | @Override |
| 6430 | public boolean isHdmiSystemAudioSupported() { |
| 6431 | return mHdmiSystemAudioSupported; |
| 6432 | } |
| 6433 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6434 | //========================================================================================== |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6435 | // Accessibility |
| 6436 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6437 | private void initA11yMonitoring() { |
| 6438 | final AccessibilityManager accessibilityManager = |
| 6439 | (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6440 | updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled()); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 6441 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
Phil Weaver | 26d709f | 2017-04-20 17:19:14 -0700 | [diff] [blame] | 6442 | accessibilityManager.addTouchExplorationStateChangeListener(this, null); |
| 6443 | accessibilityManager.addAccessibilityServicesStateChangeListener(this, null); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | //--------------------------------------------------------------------------------- |
| 6447 | // A11y: taking touch exploration into account for selecting the default |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6448 | // stream override timeout when adjusting volume |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6449 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6450 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6451 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 6452 | // - STREAM_RING on phones during this period after a notification stopped |
| 6453 | // - STREAM_MUSIC otherwise |
| 6454 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6455 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
| 6456 | 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] | 6457 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6458 | private static int sStreamOverrideDelayMs; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6459 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6460 | @Override |
| 6461 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 6462 | updateDefaultStreamOverrideDelay(enabled); |
| 6463 | } |
| 6464 | |
| 6465 | private void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 6466 | if (touchExploreEnabled) { |
| 6467 | sStreamOverrideDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 6468 | } else { |
| 6469 | sStreamOverrideDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6470 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6471 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 6472 | + " stream override delay is now " + sStreamOverrideDelayMs + " ms"); |
| 6473 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6474 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6475 | //--------------------------------------------------------------------------------- |
| 6476 | // A11y: taking a11y state into account for the handling of a11y prompts volume |
| 6477 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6478 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6479 | private static boolean sIndependentA11yVolume = false; |
| 6480 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6481 | // implementation of AccessibilityServicesStateChangeListener |
| 6482 | @Override |
Phil Weaver | 4cab930 | 2017-03-30 15:27:39 -0700 | [diff] [blame] | 6483 | public void onAccessibilityServicesStateChanged(AccessibilityManager accessibilityManager) { |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6484 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
| 6485 | } |
| 6486 | |
| 6487 | private void updateA11yVolumeAlias(boolean a11VolEnabled) { |
| 6488 | if (DEBUG_VOL) Log.d(TAG, "Accessibility volume enabled = " + a11VolEnabled); |
| 6489 | if (sIndependentA11yVolume != a11VolEnabled) { |
| 6490 | sIndependentA11yVolume = a11VolEnabled; |
| 6491 | // update the volume mapping scheme |
| 6492 | updateStreamVolumeAlias(true /*updateVolumes*/, TAG); |
| 6493 | // update the volume controller behavior |
| 6494 | mVolumeController.setA11yMode(sIndependentA11yVolume ? |
| 6495 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 6496 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 6497 | mVolumeController.postVolumeChanged(AudioManager.STREAM_ACCESSIBILITY, 0); |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6498 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6502 | // Camera shutter sound policy. |
| 6503 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 6504 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 6505 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 6506 | //========================================================================================== |
| 6507 | |
| 6508 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6509 | @GuardedBy("mSettingsLock") |
| 6510 | private boolean mCameraSoundForced; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6511 | |
| 6512 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 6513 | public boolean isCameraSoundForced() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6514 | synchronized (mSettingsLock) { |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6515 | return mCameraSoundForced; |
| 6516 | } |
| 6517 | } |
| 6518 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6519 | //========================================================================================== |
| 6520 | // AudioService logging and dumpsys |
| 6521 | //========================================================================================== |
| 6522 | final int LOG_NB_EVENTS_PHONE_STATE = 20; |
| 6523 | final int LOG_NB_EVENTS_WIRED_DEV_CONNECTION = 30; |
| 6524 | final int LOG_NB_EVENTS_FORCE_USE = 20; |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 6525 | final int LOG_NB_EVENTS_VOLUME = 40; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6526 | |
| 6527 | final private AudioEventLogger mModeLogger = new AudioEventLogger(LOG_NB_EVENTS_PHONE_STATE, |
| 6528 | "phone state (logged after successfull call to AudioSystem.setPhoneState(int))"); |
| 6529 | |
| 6530 | final private AudioEventLogger mWiredDevLogger = new AudioEventLogger( |
| 6531 | LOG_NB_EVENTS_WIRED_DEV_CONNECTION, |
| 6532 | "wired device connection (logged before onSetWiredDeviceConnectionState() is executed)" |
| 6533 | ); |
| 6534 | |
| 6535 | final private AudioEventLogger mForceUseLogger = new AudioEventLogger( |
| 6536 | LOG_NB_EVENTS_FORCE_USE, |
| 6537 | "force use (logged before setForceUse() is executed)"); |
| 6538 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 6539 | final private AudioEventLogger mVolumeLogger = new AudioEventLogger(LOG_NB_EVENTS_VOLUME, |
| 6540 | "volume changes (logged when command received by AudioService)"); |
| 6541 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6542 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 6543 | "SILENT", |
| 6544 | "VIBRATE", |
| 6545 | "NORMAL" |
| 6546 | }; |
| 6547 | |
| 6548 | private void dumpRingerMode(PrintWriter pw) { |
| 6549 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6550 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 6551 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6552 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
| 6553 | dumpRingerModeStreams(pw, "muted", mRingerModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6554 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6555 | } |
| 6556 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6557 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 6558 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 6559 | pw.print(Integer.toHexString(streams)); |
| 6560 | if (streams != 0) { |
| 6561 | pw.print(" ("); |
| 6562 | boolean first = true; |
| 6563 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 6564 | final int stream = (1 << i); |
| 6565 | if ((streams & stream) != 0) { |
| 6566 | if (!first) pw.print(','); |
| 6567 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 6568 | streams &= ~stream; |
| 6569 | first = false; |
| 6570 | } |
| 6571 | } |
| 6572 | if (streams != 0) { |
| 6573 | if (!first) pw.print(','); |
| 6574 | pw.print(streams); |
| 6575 | } |
| 6576 | pw.print(')'); |
| 6577 | } |
| 6578 | pw.println(); |
| 6579 | } |
| 6580 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6581 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6582 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 6583 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 6584 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6585 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 6586 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6587 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6588 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6589 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 6590 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6591 | |
| 6592 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6593 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6594 | pw.print(" mSafeMediaVolumeState="); |
| 6595 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 6596 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6597 | pw.print(" mSafeUsbMediaVolumeIndex="); pw.println(mSafeUsbMediaVolumeIndex); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 6598 | pw.print(" sIndependentA11yVolume="); pw.println(sIndependentA11yVolume); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6599 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 6600 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 6601 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6602 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6603 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6604 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 6605 | pw.print(" mAvrcpAbsVolSupported="); pw.println(mAvrcpAbsVolSupported); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6606 | |
| 6607 | dumpAudioPolicies(pw); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6608 | |
| 6609 | mPlaybackMonitor.dump(pw); |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 6610 | |
| 6611 | mRecordMonitor.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6612 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 6613 | pw.println("\n"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6614 | pw.println("\nEvent logs:"); |
| 6615 | mModeLogger.dump(pw); |
| 6616 | pw.println("\n"); |
| 6617 | mWiredDevLogger.dump(pw); |
| 6618 | pw.println("\n"); |
| 6619 | mForceUseLogger.dump(pw); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 6620 | pw.println("\n"); |
| 6621 | mVolumeLogger.dump(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | private static String safeMediaVolumeStateToString(Integer state) { |
| 6625 | switch(state) { |
| 6626 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 6627 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 6628 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 6629 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 6630 | } |
| 6631 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6632 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 6633 | |
| 6634 | // Inform AudioFlinger of our device's low RAM attribute |
| 6635 | private static void readAndSetLowRamDevice() |
| 6636 | { |
| 6637 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 6638 | if (status != 0) { |
| 6639 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 6640 | } |
| 6641 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6642 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6643 | private void enforceVolumeController(String action) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6644 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 6645 | "Only SystemUI can " + action); |
| 6646 | } |
| 6647 | |
| 6648 | @Override |
| 6649 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6650 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6651 | |
| 6652 | // return early if things are not actually changing |
| 6653 | if (mVolumeController.isSameBinder(controller)) { |
| 6654 | return; |
| 6655 | } |
| 6656 | |
| 6657 | // dismiss the old volume controller |
| 6658 | mVolumeController.postDismiss(); |
| 6659 | if (controller != null) { |
| 6660 | // we are about to register a new controller, listen for its death |
| 6661 | try { |
| 6662 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 6663 | @Override |
| 6664 | public void binderDied() { |
| 6665 | if (mVolumeController.isSameBinder(controller)) { |
| 6666 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 6667 | setVolumeController(null); |
| 6668 | } |
| 6669 | } |
| 6670 | }, 0); |
| 6671 | } catch (RemoteException e) { |
| 6672 | // noop |
| 6673 | } |
| 6674 | } |
| 6675 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6676 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 6677 | } |
| 6678 | |
| 6679 | @Override |
| 6680 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6681 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6682 | |
| 6683 | // return early if the controller is not current |
| 6684 | if (!mVolumeController.isSameBinder(controller)) { |
| 6685 | return; |
| 6686 | } |
| 6687 | |
| 6688 | mVolumeController.setVisible(visible); |
| 6689 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6690 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6691 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6692 | @Override |
| 6693 | public void setVolumePolicy(VolumePolicy policy) { |
| 6694 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 6695 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6696 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 6697 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6698 | } |
| 6699 | } |
| 6700 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6701 | public static class VolumeController { |
| 6702 | private static final String TAG = "VolumeController"; |
| 6703 | |
| 6704 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6705 | private boolean mVisible; |
| 6706 | private long mNextLongPress; |
| 6707 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6708 | |
| 6709 | public void setController(IVolumeController controller) { |
| 6710 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6711 | mVisible = false; |
| 6712 | } |
| 6713 | |
| 6714 | public void loadSettings(ContentResolver cr) { |
| 6715 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 6716 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 6717 | } |
| 6718 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 6719 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 6720 | if (isMute) { |
| 6721 | return false; |
| 6722 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6723 | boolean suppress = false; |
| 6724 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 6725 | final long now = SystemClock.uptimeMillis(); |
| 6726 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 6727 | // ui will become visible |
| 6728 | if (mNextLongPress < now) { |
| 6729 | mNextLongPress = now + mLongPressTimeout; |
| 6730 | } |
| 6731 | suppress = true; |
| 6732 | } else if (mNextLongPress > 0) { // in a long-press |
| 6733 | if (now > mNextLongPress) { |
| 6734 | // long press triggered, no more suppression |
| 6735 | mNextLongPress = 0; |
| 6736 | } else { |
| 6737 | // keep suppressing until the long press triggers |
| 6738 | suppress = true; |
| 6739 | } |
| 6740 | } |
| 6741 | } |
| 6742 | return suppress; |
| 6743 | } |
| 6744 | |
| 6745 | public void setVisible(boolean visible) { |
| 6746 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | public boolean isSameBinder(IVolumeController controller) { |
| 6750 | return Objects.equals(asBinder(), binder(controller)); |
| 6751 | } |
| 6752 | |
| 6753 | public IBinder asBinder() { |
| 6754 | return binder(mController); |
| 6755 | } |
| 6756 | |
| 6757 | private static IBinder binder(IVolumeController controller) { |
| 6758 | return controller == null ? null : controller.asBinder(); |
| 6759 | } |
| 6760 | |
| 6761 | @Override |
| 6762 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 6763 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | public void postDisplaySafeVolumeWarning(int flags) { |
| 6767 | if (mController == null) |
| 6768 | return; |
| 6769 | try { |
| 6770 | mController.displaySafeVolumeWarning(flags); |
| 6771 | } catch (RemoteException e) { |
| 6772 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | public void postVolumeChanged(int streamType, int flags) { |
| 6777 | if (mController == null) |
| 6778 | return; |
| 6779 | try { |
| 6780 | mController.volumeChanged(streamType, flags); |
| 6781 | } catch (RemoteException e) { |
| 6782 | Log.w(TAG, "Error calling volumeChanged", e); |
| 6783 | } |
| 6784 | } |
| 6785 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6786 | public void postMasterMuteChanged(int flags) { |
| 6787 | if (mController == null) |
| 6788 | return; |
| 6789 | try { |
| 6790 | mController.masterMuteChanged(flags); |
| 6791 | } catch (RemoteException e) { |
| 6792 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 6793 | } |
| 6794 | } |
| 6795 | |
| 6796 | public void setLayoutDirection(int layoutDirection) { |
| 6797 | if (mController == null) |
| 6798 | return; |
| 6799 | try { |
| 6800 | mController.setLayoutDirection(layoutDirection); |
| 6801 | } catch (RemoteException e) { |
| 6802 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 6803 | } |
| 6804 | } |
| 6805 | |
| 6806 | public void postDismiss() { |
| 6807 | if (mController == null) |
| 6808 | return; |
| 6809 | try { |
| 6810 | mController.dismiss(); |
| 6811 | } catch (RemoteException e) { |
| 6812 | Log.w(TAG, "Error calling dismiss", e); |
| 6813 | } |
| 6814 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6815 | |
| 6816 | public void setA11yMode(int a11yMode) { |
| 6817 | if (mController == null) |
| 6818 | return; |
| 6819 | try { |
| 6820 | mController.setA11yMode(a11yMode); |
| 6821 | } catch (RemoteException e) { |
| 6822 | Log.w(TAG, "Error calling setA11Mode", e); |
| 6823 | } |
| 6824 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6825 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6826 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6827 | /** |
| 6828 | * Interface for system components to get some extra functionality through |
| 6829 | * LocalServices. |
| 6830 | */ |
| 6831 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6832 | @Override |
| 6833 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 6834 | mRingerModeDelegate = delegate; |
| 6835 | if (mRingerModeDelegate != null) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6836 | synchronized (mSettingsLock) { |
| 6837 | updateRingerModeAffectedStreams(); |
| 6838 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6839 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 6840 | } |
| 6841 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6842 | |
| 6843 | @Override |
| 6844 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 6845 | String callingPackage, int uid) { |
| 6846 | // direction and stream type swap here because the public |
| 6847 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6848 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 6849 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6850 | } |
| 6851 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6852 | @Override |
| 6853 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 6854 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6855 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 6856 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | @Override |
| 6860 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 6861 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6862 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6863 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 6864 | |
| 6865 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6866 | public int getRingerModeInternal() { |
| 6867 | return AudioService.this.getRingerModeInternal(); |
| 6868 | } |
| 6869 | |
| 6870 | @Override |
| 6871 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 6872 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 6873 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6874 | |
| 6875 | @Override |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6876 | public void updateRingerModeAffectedStreamsInternal() { |
| 6877 | synchronized (mSettingsLock) { |
| 6878 | if (updateRingerModeAffectedStreams()) { |
| 6879 | setRingerModeInt(getRingerModeInternal(), false); |
| 6880 | } |
| 6881 | } |
| 6882 | } |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 6883 | |
| 6884 | @Override |
| 6885 | public void setAccessibilityServiceUids(IntArray uids) { |
| 6886 | synchronized (mAccessibilityServiceUidsLock) { |
| 6887 | if (uids.size() == 0) { |
| 6888 | mAccessibilityServiceUids = null; |
| 6889 | } else { |
| 6890 | boolean changed = (mAccessibilityServiceUids == null) |
| 6891 | || (mAccessibilityServiceUids.length != uids.size()); |
| 6892 | if (!changed) { |
| 6893 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 6894 | if (uids.get(i) != mAccessibilityServiceUids[i]) { |
| 6895 | changed = true; |
| 6896 | break; |
| 6897 | } |
| 6898 | } |
| 6899 | } |
| 6900 | if (changed) { |
| 6901 | mAccessibilityServiceUids = uids.toArray(); |
| 6902 | } |
| 6903 | } |
| 6904 | } |
| 6905 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6908 | //========================================================================================== |
| 6909 | // Audio policy management |
| 6910 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6911 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6912 | boolean hasFocusListener, boolean isFocusPolicy) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6913 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 6914 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6915 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 6916 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6917 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6918 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6919 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6920 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6921 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6922 | if (!hasPermissionForPolicy) { |
| 6923 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 6924 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6925 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6926 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6927 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6928 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6929 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6930 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6931 | Slog.e(TAG, "Cannot re-register policy"); |
| 6932 | return null; |
| 6933 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6934 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener, |
| 6935 | isFocusPolicy); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6936 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 6937 | regId = app.getRegistrationId(); |
| 6938 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6939 | } catch (RemoteException e) { |
| 6940 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6941 | 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] | 6942 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6943 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6944 | } |
| 6945 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6946 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6947 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6948 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6949 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 6950 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6951 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6952 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6953 | if (app == null) { |
| 6954 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 6955 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6956 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6957 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6958 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6959 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6960 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6961 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6962 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6963 | } |
| 6964 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6965 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 6966 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 6967 | + " policy " + pcb.asBinder()); |
| 6968 | // error handling |
| 6969 | boolean hasPermissionForPolicy = |
| 6970 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6971 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6972 | if (!hasPermissionForPolicy) { |
| 6973 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 6974 | + Binder.getCallingPid() + " / uid " |
| 6975 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 6976 | return AudioManager.ERROR; |
| 6977 | } |
| 6978 | |
| 6979 | synchronized (mAudioPolicies) { |
| 6980 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6981 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 6982 | return AudioManager.ERROR; |
| 6983 | } |
| 6984 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 6985 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6986 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6987 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6988 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6989 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 6990 | return AudioManager.ERROR; |
| 6991 | } |
| 6992 | } |
| 6993 | } |
| 6994 | app.mFocusDuckBehavior = duckingBehavior; |
| 6995 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 6996 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 6997 | } |
| 6998 | return AudioManager.SUCCESS; |
| 6999 | } |
| 7000 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7001 | private void dumpAudioPolicies(PrintWriter pw) { |
| 7002 | pw.println("\nAudio policies:"); |
| 7003 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 7004 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7005 | pw.println(policy.toLogFriendlyString()); |
| 7006 | } |
| 7007 | } |
| 7008 | } |
| 7009 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7010 | //====================== |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7011 | // Audio policy callbacks from AudioSystem for dynamic policies |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7012 | //====================== |
| 7013 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 7014 | new AudioSystem.DynamicPolicyCallback() { |
| 7015 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 7016 | if (!TextUtils.isEmpty(regId)) { |
| 7017 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 7018 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 7019 | } |
| 7020 | } |
| 7021 | }; |
| 7022 | |
| 7023 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 7024 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 7025 | synchronized (mAudioPolicies) { |
| 7026 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 7027 | for (AudioMix mix : policy.getMixes()) { |
| 7028 | if (mix.getRegistration().equals(regId)) { |
| 7029 | try { |
| 7030 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 7031 | } catch (RemoteException e) { |
| 7032 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 7033 | + policy.mPolicyCallback.asBinder(), e); |
| 7034 | } |
| 7035 | return; |
| 7036 | } |
| 7037 | } |
| 7038 | } |
| 7039 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7040 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7041 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7042 | //====================== |
| 7043 | // Audio policy callbacks from AudioSystem for recording configuration updates |
| 7044 | //====================== |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7045 | private final RecordingActivityMonitor mRecordMonitor; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7046 | |
| 7047 | public void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7048 | final boolean isPrivileged = |
| 7049 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7050 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7051 | mRecordMonitor.registerRecordingCallback(rcdb, isPrivileged); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | public void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 7055 | mRecordMonitor.unregisterRecordingCallback(rcdb); |
| 7056 | } |
| 7057 | |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 7058 | public List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7059 | final boolean isPrivileged = |
| 7060 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7061 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7062 | return mRecordMonitor.getActiveRecordingConfigurations(isPrivileged); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7063 | } |
| 7064 | |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7065 | public void disableRingtoneSync(final int userId) { |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7066 | final int callingUserId = UserHandle.getCallingUserId(); |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7067 | if (callingUserId != userId) { |
| 7068 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 7069 | "disable sound settings syncing for another profile"); |
| 7070 | } |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7071 | final long token = Binder.clearCallingIdentity(); |
| 7072 | try { |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7073 | // Disable the sync setting so the profile uses its own sound settings. |
| 7074 | Settings.Secure.putIntForUser(mContentResolver, Settings.Secure.SYNC_PARENT_SOUNDS, |
| 7075 | 0 /* false */, userId); |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7076 | } finally { |
| 7077 | Binder.restoreCallingIdentity(token); |
| 7078 | } |
| 7079 | } |
| 7080 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7081 | //====================== |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7082 | // Audio playback notification |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7083 | //====================== |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 7084 | private final PlaybackActivityMonitor mPlaybackMonitor; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7085 | |
| 7086 | public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7087 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7088 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7089 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7090 | mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged); |
| 7091 | } |
| 7092 | |
| 7093 | public void unregisterPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
| 7094 | mPlaybackMonitor.unregisterPlaybackCallback(pcdb); |
| 7095 | } |
| 7096 | |
| 7097 | public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7098 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7099 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7100 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7101 | return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7102 | } |
| 7103 | |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7104 | public int trackPlayer(PlayerBase.PlayerIdCard pic) { |
| 7105 | return mPlaybackMonitor.trackPlayer(pic); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | public void playerAttributes(int piid, AudioAttributes attr) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7109 | mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | public void playerEvent(int piid, int event) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7113 | mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7114 | } |
| 7115 | |
Jean-Michel Trivi | 3120059d | 2017-08-28 12:40:55 -0700 | [diff] [blame] | 7116 | public void playerHasOpPlayAudio(int piid, boolean hasOpPlayAudio) { |
| 7117 | mPlaybackMonitor.playerHasOpPlayAudio(piid, hasOpPlayAudio, Binder.getCallingUid()); |
| 7118 | } |
| 7119 | |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7120 | public void releasePlayer(int piid) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7121 | mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7122 | } |
| 7123 | |
| 7124 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7125 | // Audio policy proxy |
| 7126 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7127 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7128 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 7129 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7130 | */ |
| 7131 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7132 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7133 | IAudioPolicyCallback mPolicyCallback; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7134 | boolean mHasFocusListener; |
| 7135 | /** |
| 7136 | * Audio focus ducking behavior for an audio policy. |
| 7137 | * This variable reflects the value that was successfully set in |
| 7138 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 7139 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 7140 | * is handling ducking for audio focus. |
| 7141 | */ |
| 7142 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7143 | boolean mIsFocusPolicy = false; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7144 | |
| 7145 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7146 | boolean hasFocusListener, boolean isFocusPolicy) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7147 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7148 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7149 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7150 | mHasFocusListener = hasFocusListener; |
| 7151 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7152 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7153 | // can only ever be true if there is a focus listener |
| 7154 | if (isFocusPolicy) { |
| 7155 | mIsFocusPolicy = true; |
| 7156 | mMediaFocusControl.setFocusPolicy(mPolicyCallback); |
| 7157 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7158 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7159 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7160 | } |
| 7161 | |
| 7162 | public void binderDied() { |
| 7163 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7164 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7165 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7166 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7167 | } |
| 7168 | } |
| 7169 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7170 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7171 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7172 | } |
| 7173 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7174 | void release() { |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7175 | if (mIsFocusPolicy) { |
| 7176 | mMediaFocusControl.unsetFocusPolicy(mPolicyCallback); |
| 7177 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7178 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7179 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 7180 | } |
| 7181 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7182 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7183 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7184 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7185 | } |
| 7186 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7187 | void connectMixes() { |
| 7188 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7189 | } |
| 7190 | }; |
| 7191 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7192 | //====================== |
| 7193 | // Audio policy: focus |
| 7194 | //====================== |
| 7195 | /** */ |
| 7196 | public int dispatchFocusChange(AudioFocusInfo afi, int focusChange, IAudioPolicyCallback pcb) { |
| 7197 | synchronized (mAudioPolicies) { |
| 7198 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 7199 | throw new IllegalStateException("Unregistered AudioPolicy for focus dispatch"); |
| 7200 | } |
| 7201 | return mMediaFocusControl.dispatchFocusChange(afi, focusChange); |
| 7202 | } |
| 7203 | } |
| 7204 | |
| 7205 | //====================== |
| 7206 | // misc |
| 7207 | //====================== |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7208 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 7209 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7210 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 7211 | } |