The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 17 | package com.android.server.audio; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.REMOTE_AUDIO_PLAYBACK; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 20 | import static android.media.AudioManager.RINGER_MODE_NORMAL; |
| 21 | import static android.media.AudioManager.RINGER_MODE_SILENT; |
| 22 | import static android.media.AudioManager.RINGER_MODE_VIBRATE; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 23 | import static android.media.AudioManager.STREAM_ALARM; |
| 24 | import static android.media.AudioManager.STREAM_MUSIC; |
| 25 | import static android.media.AudioManager.STREAM_SYSTEM; |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 26 | import static android.os.Process.FIRST_APPLICATION_UID; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 27 | import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE; |
| 28 | import static android.provider.Settings.Secure.VOLUME_HUSH_OFF; |
| 29 | import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 30 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 31 | import android.Manifest; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 32 | import android.annotation.NonNull; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 33 | import android.annotation.Nullable; |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 34 | import android.app.ActivityManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 35 | import android.app.ActivityManagerInternal; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 36 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 37 | import android.app.AppOpsManager; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 38 | import android.app.IUidObserver; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 39 | import android.app.NotificationManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 40 | import android.bluetooth.BluetoothA2dp; |
| 41 | import android.bluetooth.BluetoothAdapter; |
| 42 | import android.bluetooth.BluetoothClass; |
| 43 | import android.bluetooth.BluetoothDevice; |
| 44 | import android.bluetooth.BluetoothHeadset; |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 45 | import android.bluetooth.BluetoothHearingAid; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 46 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 48 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.ContentResolver; |
| 50 | import android.content.Context; |
| 51 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 52 | import android.content.IntentFilter; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 53 | import android.content.pm.ApplicationInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 56 | import android.content.pm.ResolveInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 57 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 58 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 59 | import android.content.res.Resources; |
| 60 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 61 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 62 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 63 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 64 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 65 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 66 | import android.media.AudioAttributes; |
| 67 | import android.media.AudioDevicePort; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 68 | import android.media.AudioFocusInfo; |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 69 | import android.media.AudioFocusRequest; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 70 | import android.media.AudioFormat; |
| 71 | import android.media.AudioManager; |
| 72 | import android.media.AudioManagerInternal; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 73 | import android.media.AudioPlaybackConfiguration; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 74 | import android.media.AudioPort; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 75 | import android.media.AudioRecordingConfiguration; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 76 | import android.media.AudioRoutesInfo; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 77 | import android.media.AudioSystem; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 78 | import android.media.IAudioFocusDispatcher; |
| 79 | import android.media.IAudioRoutesObserver; |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 80 | import android.media.IAudioServerStateDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 81 | import android.media.IAudioService; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 82 | import android.media.IPlaybackConfigDispatcher; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 83 | import android.media.IRecordingConfigDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 84 | import android.media.IRingtonePlayer; |
| 85 | import android.media.IVolumeController; |
| 86 | import android.media.MediaPlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.media.MediaPlayer.OnCompletionListener; |
| 88 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 89 | import android.media.PlayerBase; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 90 | import android.media.SoundPool; |
| 91 | import android.media.VolumePolicy; |
| 92 | import android.media.audiofx.AudioEffect; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 93 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 94 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 95 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 96 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 98 | import android.os.Build; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 99 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.Environment; |
| 101 | import android.os.Handler; |
| 102 | import android.os.IBinder; |
| 103 | import android.os.Looper; |
| 104 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 105 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 106 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 108 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 109 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 110 | import android.os.UserHandle; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 111 | import android.os.UserManager; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 112 | import android.os.UserManagerInternal; |
| 113 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 114 | import android.os.VibrationEffect; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 115 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.provider.Settings; |
| 117 | import android.provider.Settings.System; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 118 | import android.service.notification.ZenModeConfig; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 119 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 120 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 121 | import android.util.AndroidRuntimeException; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 122 | import android.util.ArrayMap; |
| 123 | import android.util.ArraySet; |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 124 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 126 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 127 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 128 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 129 | import android.view.KeyEvent; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 130 | import android.view.accessibility.AccessibilityManager; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 131 | import android.widget.Toast; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 133 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 134 | import com.android.internal.util.DumpUtils; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 135 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 136 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 137 | import com.android.server.LocalServices; |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 138 | import com.android.server.SystemService; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 139 | import com.android.server.audio.AudioServiceEvents.ForceUseEvent; |
| 140 | import com.android.server.audio.AudioServiceEvents.PhoneStateEvent; |
| 141 | import com.android.server.audio.AudioServiceEvents.VolumeEvent; |
| 142 | import com.android.server.audio.AudioServiceEvents.WiredDevConnectEvent; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 143 | import com.android.server.pm.UserManagerService; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 144 | |
| 145 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 147 | import java.io.File; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 148 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 150 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 151 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | import java.util.ArrayList; |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 153 | import java.util.Collection; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 154 | import java.util.HashMap; |
| 155 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 156 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 157 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 158 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * The implementation of the volume manager service. |
| 162 | * <p> |
| 163 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 164 | * asynchronous to external calls. For example, the task of setting a volume |
| 165 | * will update our internal state, but in a separate thread will set the system |
| 166 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 167 | * will update the state and broadcast a change and in a separate thread later |
| 168 | * persist the ringer mode. |
| 169 | * |
| 170 | * @hide |
| 171 | */ |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 172 | public class AudioService extends IAudioService.Stub |
| 173 | implements AccessibilityManager.TouchExplorationStateChangeListener, |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 174 | AccessibilityManager.AccessibilityServicesStateChangeListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | |
| 176 | private static final String TAG = "AudioService"; |
| 177 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 178 | /** Debug audio mode */ |
| 179 | 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] | 180 | |
| 181 | /** Debug audio policy feature */ |
| 182 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 183 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 184 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 185 | 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] | 186 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 187 | /** debug calls to devices APIs */ |
| 188 | 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] | 189 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 190 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 192 | /** How long to delay after a volume down event before unmuting a stream */ |
| 193 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 194 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 195 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 196 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 197 | */ |
| 198 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 199 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 200 | private final Context mContext; |
| 201 | private final ContentResolver mContentResolver; |
| 202 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 203 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 204 | // the platform type affects volume and silent mode behavior |
| 205 | private final int mPlatformType; |
| 206 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 207 | // indicates whether the system maps all streams to a single stream. |
| 208 | private final boolean mIsSingleVolume; |
| 209 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 210 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 211 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 215 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 216 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 217 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 218 | /** The controller for the volume UI. */ |
| 219 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | |
| 221 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | /** If the msg is already queued, replace it with this one. */ |
| 223 | private static final int SENDMSG_REPLACE = 0; |
| 224 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 225 | private static final int SENDMSG_NOOP = 1; |
| 226 | /** If the msg is already queued, queue this one and leave the old. */ |
| 227 | private static final int SENDMSG_QUEUE = 2; |
| 228 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 229 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 230 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | private static final int MSG_PERSIST_VOLUME = 1; |
| 232 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 233 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 234 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 235 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 236 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 237 | private static final int MSG_SET_FORCE_USE = 8; |
| 238 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 239 | private static final int MSG_SET_ALL_VOLUMES = 10; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 240 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 241 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 242 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 243 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 244 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 245 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 246 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 247 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 248 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 249 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 250 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 251 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 252 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 253 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 254 | private static final int MSG_ACCESSORY_PLUG_MEDIA_UNMUTE = 27; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 255 | private static final int MSG_NOTIFY_VOL_EVENT = 28; |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 256 | private static final int MSG_DISPATCH_AUDIO_SERVER_STATE = 29; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 257 | // start of messages handled under wakelock |
| 258 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 259 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 260 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 261 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 262 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 263 | private static final int MSG_A2DP_DEVICE_CONFIG_CHANGE = 103; |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 264 | private static final int MSG_DISABLE_AUDIO_FOR_UID = 104; |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 265 | private static final int MSG_SET_HEARING_AID_CONNECTION_STATE = 105; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 266 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 267 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 268 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 269 | // Timeout for connection to bluetooth headset service |
| 270 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 271 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 272 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 273 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 274 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 275 | private static final int BT_HEARING_AID_GAIN_MIN = -128; |
| 276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | /** @see AudioSystemThread */ |
| 278 | private AudioSystemThread mAudioSystemThread; |
| 279 | /** @see AudioHandler */ |
| 280 | private AudioHandler mAudioHandler; |
| 281 | /** @see VolumeStreamState */ |
| 282 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 283 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 284 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 285 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 286 | // protects mRingerMode |
| 287 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 290 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
| 293 | /* Sound effect file names */ |
| 294 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 295 | 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] | 296 | |
| 297 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 298 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 299 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 300 | 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] | 301 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 302 | /** Maximum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 303 | protected static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 304 | 5, // STREAM_VOICE_CALL |
| 305 | 7, // STREAM_SYSTEM |
| 306 | 7, // STREAM_RING |
| 307 | 15, // STREAM_MUSIC |
| 308 | 7, // STREAM_ALARM |
| 309 | 7, // STREAM_NOTIFICATION |
| 310 | 15, // STREAM_BLUETOOTH_SCO |
| 311 | 7, // STREAM_SYSTEM_ENFORCED |
| 312 | 15, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 313 | 15, // STREAM_TTS |
| 314 | 15 // STREAM_ACCESSIBILITY |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 315 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 316 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 317 | /** Minimum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 318 | protected static int[] MIN_STREAM_VOLUME = new int[] { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 319 | 1, // STREAM_VOICE_CALL |
| 320 | 0, // STREAM_SYSTEM |
| 321 | 0, // STREAM_RING |
| 322 | 0, // STREAM_MUSIC |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 323 | 1, // STREAM_ALARM |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 324 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 325 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 326 | 0, // STREAM_SYSTEM_ENFORCED |
| 327 | 0, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 328 | 0, // STREAM_TTS |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 329 | 1 // STREAM_ACCESSIBILITY |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 330 | }; |
| 331 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 332 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 333 | * of another stream: This avoids multiplying the volume settings for hidden |
| 334 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 335 | * NOTE: do not create loops in aliases! |
| 336 | * 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] | 337 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 338 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 339 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 340 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 341 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 342 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 343 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 344 | AudioSystem.STREAM_RING, // STREAM_RING |
| 345 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 346 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 347 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 348 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 349 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 350 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 351 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 352 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 353 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 354 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 355 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 356 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 357 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 358 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 359 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 360 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 361 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 362 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 363 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 364 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 365 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 366 | }; |
| 367 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 368 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 369 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 370 | AudioSystem.STREAM_RING, // STREAM_RING |
| 371 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 372 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 373 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 374 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 375 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 376 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 377 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 378 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 379 | }; |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 380 | protected static int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 381 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 384 | * after mapping through mStreamVolumeAlias. |
| 385 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 386 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 387 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 388 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 389 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 390 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 391 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 392 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 393 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 394 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 395 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 396 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 397 | AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME, // STREAM_ACCESSIBILITY |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 400 | private final boolean mUseFixedVolume; |
| 401 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 402 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | public void onError(int error) { |
| 404 | switch (error) { |
| 405 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 406 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 407 | SENDMSG_NOOP, 0, 0, null, 0); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 408 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 409 | SENDMSG_QUEUE, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | break; |
| 411 | default: |
| 412 | break; |
| 413 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 414 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | /** |
| 418 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 419 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 420 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 421 | */ |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 422 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 423 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 424 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 425 | 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] | 426 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 427 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 428 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 429 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 430 | private int mZenModeAffectedStreams = 0; |
| 431 | |
| 432 | // Streams currently muted by ringer mode and dnd |
| 433 | private int mRingerAndZenModeMutedStreams; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 434 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 435 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 436 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | private int mMuteAffectedStreams; |
| 438 | |
| 439 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 440 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 441 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 442 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | */ |
| 444 | private int mVibrateSetting; |
| 445 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 446 | // Is there a vibrator |
| 447 | private final boolean mHasVibrator; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 448 | // Used to play vibrations |
| 449 | private Vibrator mVibrator; |
| 450 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 451 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 452 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 453 | .build(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 454 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 455 | // Broadcast receiver for device connections intent broadcasts |
| 456 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 457 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 458 | /** Interface for UserManagerService. */ |
| 459 | private final UserManagerInternal mUserManagerInternal; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 460 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 461 | |
| 462 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 463 | new AudioServiceUserRestrictionsListener(); |
| 464 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 465 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 466 | // Use makeDeviceListKey() to make a unique key for this list. |
| 467 | private class DeviceListSpec { |
| 468 | int mDeviceType; |
| 469 | String mDeviceName; |
| 470 | String mDeviceAddress; |
| 471 | |
| 472 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 473 | mDeviceType = deviceType; |
| 474 | mDeviceName = deviceName; |
| 475 | mDeviceAddress = deviceAddress; |
| 476 | } |
| 477 | |
| 478 | public String toString() { |
| 479 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 480 | + " address:" + mDeviceAddress + "]"; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 485 | // and the "address" associated with a specific instance of that device type |
| 486 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 487 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 488 | } |
| 489 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 490 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 491 | |
| 492 | // Forced device usage for communications |
| 493 | private int mForcedUseForComm; |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 494 | private int mForcedUseForCommExt; // External state returned by getters: always consistent |
| 495 | // with requests by setters |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 496 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 497 | // List of binder death handlers for setMode() client processes. |
| 498 | // 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] | 499 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 500 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 501 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 502 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 503 | |
| 504 | // BluetoothHeadset API to control SCO connection |
| 505 | private BluetoothHeadset mBluetoothHeadset; |
| 506 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 507 | // Bluetooth headset device |
| 508 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 509 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 510 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 511 | // audio service (internal) or bluetooth headset (external) |
| 512 | private int mScoAudioState; |
| 513 | // SCO audio state is not active |
| 514 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 515 | // SCO audio activation request waiting for headset service to connect |
| 516 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 517 | // 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] | 518 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 519 | // SCO audio deactivation request waiting for headset service to connect |
| 520 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 521 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 522 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 523 | // in call audio) |
| 524 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 525 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 526 | // waiting for headset service to connect |
| 527 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 528 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 529 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 530 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 531 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 532 | // SCO audio mode is undefined |
| 533 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 534 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 535 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 536 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 537 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 538 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 539 | private static final int SCO_MODE_VR = 2; |
| 540 | |
| 541 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 542 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 543 | // Current connection state indicated by bluetooth headset |
| 544 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 545 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 546 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 547 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 548 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 549 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 550 | // listener for SoundPool sample load completion indication |
| 551 | private SoundPoolCallback mSoundPoolCallBack; |
| 552 | // thread for SoundPool listener |
| 553 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 554 | // message looper for SoundPool listener |
| 555 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 556 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 557 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 558 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 559 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 560 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 561 | // is controlled by Vol keys. |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 562 | private int mVolumeControlStream = -1; |
| 563 | // interpretation of whether the volume stream has been selected by the user by clicking on a |
| 564 | // volume slider to change which volume is controlled by the volume keys. Is false |
| 565 | // when mVolumeControlStream is -1. |
| 566 | private boolean mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 567 | private final Object mForceControlStreamLock = new Object(); |
| 568 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 569 | // server process so in theory it is not necessary to monitor the client death. |
| 570 | // However it is good to be ready for future evolutions. |
| 571 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 572 | // Used to play ringtones outside system_server |
| 573 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 574 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 575 | // Request to override default use of A2DP for media. |
Sungsoo | 486f7d3 | 2016-09-28 16:20:52 +0900 | [diff] [blame] | 576 | private boolean mBluetoothA2dpEnabled; |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 577 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 578 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 579 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 580 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 581 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 582 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 583 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 584 | // 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] | 585 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 586 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 587 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 588 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 589 | AudioSystem.DEVICE_OUT_SPDIF | |
| 590 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 591 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 592 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 593 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 594 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 595 | private boolean mDockAudioMediaEnabled = true; |
| 596 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 597 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 598 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 599 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 600 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 601 | // and used later when/if disableSafeMediaVolume() is called. |
| 602 | private StreamVolumeCommand mPendingVolumeCommand; |
| 603 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 604 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 605 | |
| 606 | private final MediaFocusControl mMediaFocusControl; |
| 607 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 608 | // Reference to BluetoothA2dp to query for volume. |
| 609 | private BluetoothHearingAid mHearingAid; |
| 610 | // lock always taken synchronized on mConnectedDevices |
| 611 | private final Object mHearingAidLock = new Object(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 612 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 613 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 614 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 615 | private final Object mA2dpAvrcpLock = new Object(); |
| 616 | // If absolute volume is supported in AVRCP device |
| 617 | private boolean mAvrcpAbsVolSupported = false; |
| 618 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 619 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 620 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 621 | private NotificationManager mNm; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 622 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 623 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 624 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 625 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 626 | // Array of Uids of valid accessibility services to check if caller is one of them |
| 627 | private int[] mAccessibilityServiceUids; |
| 628 | private final Object mAccessibilityServiceUidsLock = new Object(); |
| 629 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 630 | // Intent "extra" data keys. |
| 631 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 632 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 633 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 634 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 635 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 636 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 637 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 638 | |
| 639 | // Defines the format for the connection "address" for ALSA devices |
| 640 | public static String makeAlsaAddressString(int card, int device) { |
| 641 | return "card=" + card + ";device=" + device + ";"; |
| 642 | } |
| 643 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 644 | public static final class Lifecycle extends SystemService { |
| 645 | private AudioService mService; |
| 646 | |
| 647 | public Lifecycle(Context context) { |
| 648 | super(context); |
| 649 | mService = new AudioService(context); |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public void onStart() { |
| 654 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public void onBootPhase(int phase) { |
| 659 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 660 | mService.systemReady(); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 665 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 666 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
| 667 | } |
| 668 | |
| 669 | @Override public void onUidGone(int uid, boolean disabled) { |
| 670 | // Once the uid is no longer running, no need to keep trying to disable its audio. |
| 671 | disableAudioForUid(false, uid); |
| 672 | } |
| 673 | |
| 674 | @Override public void onUidActive(int uid) throws RemoteException { |
| 675 | } |
| 676 | |
| 677 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 678 | } |
| 679 | |
| 680 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 681 | disableAudioForUid(cached, uid); |
| 682 | } |
| 683 | |
| 684 | private void disableAudioForUid(boolean disable, int uid) { |
| 685 | queueMsgUnderWakeLock(mAudioHandler, MSG_DISABLE_AUDIO_FOR_UID, |
| 686 | disable ? 1 : 0 /* arg1 */, uid /* arg2 */, |
| 687 | null /* obj */, 0 /* delay */); |
| 688 | } |
| 689 | }; |
| 690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | /////////////////////////////////////////////////////////////////////////// |
| 692 | // Construction |
| 693 | /////////////////////////////////////////////////////////////////////////// |
| 694 | |
| 695 | /** @hide */ |
| 696 | public AudioService(Context context) { |
| 697 | mContext = context; |
| 698 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 699 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 700 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 701 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 702 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 703 | mIsSingleVolume = AudioSystem.isSingleVolume(context); |
| 704 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 705 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 706 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 707 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 708 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 709 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 710 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 711 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 712 | mHasVibrator = mVibrator == null ? false : mVibrator.hasVibrator(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 713 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 714 | // Initialize volume |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 715 | int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1); |
| 716 | if (maxCallVolume != -1) { |
| 717 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume; |
| 718 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = |
| 719 | (maxCallVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 720 | } |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 721 | |
| 722 | int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1); |
| 723 | if (maxMusicVolume != -1) { |
| 724 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume; |
| 725 | } |
| 726 | |
| 727 | int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1); |
| 728 | if (defaultMusicVolume != -1 && |
| 729 | defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 730 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume; |
| 731 | } else { |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 732 | if (isPlatformTelevision()) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 733 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 734 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 735 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 736 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 737 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 738 | } |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 739 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 740 | |
Chris Kuiper | 09b6c21 | 2018-01-03 22:22:13 -0800 | [diff] [blame] | 741 | int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1); |
| 742 | if (maxAlarmVolume != -1) { |
| 743 | MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume; |
| 744 | } |
| 745 | |
| 746 | int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1); |
| 747 | if (defaultAlarmVolume != -1 && |
| 748 | defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) { |
| 749 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume; |
| 750 | } else { |
| 751 | // Default is 6 out of 7 (default maximum), so scale accordingly. |
| 752 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = |
| 753 | 6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7; |
| 754 | } |
| 755 | |
| 756 | int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1); |
| 757 | if (maxSystemVolume != -1) { |
| 758 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume; |
| 759 | } |
| 760 | |
| 761 | int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1); |
| 762 | if (defaultSystemVolume != -1 && |
| 763 | defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) { |
| 764 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume; |
| 765 | } else { |
| 766 | // Default is to use maximum. |
| 767 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = |
| 768 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]; |
| 769 | } |
| 770 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 771 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 772 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 773 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 774 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 777 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 778 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 779 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 780 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 781 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 782 | sendMsg(mAudioHandler, |
| 783 | MSG_SET_FORCE_USE, |
| 784 | SENDMSG_QUEUE, |
| 785 | AudioSystem.FOR_SYSTEM, |
| 786 | cameraSoundForced ? |
| 787 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 788 | new String("AudioService ctor"), |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 789 | 0); |
| 790 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 791 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 792 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 793 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 794 | // The default safe volume index read here will be replaced by the actual value when |
| 795 | // the mcc is read by onConfigureSafeVolume() |
| 796 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 797 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 798 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 799 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 800 | com.android.internal.R.bool.config_useFixedVolume); |
| 801 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 802 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 803 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 804 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 806 | readUserRestrictions(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 807 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 808 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 809 | |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 810 | // mSafeUsbMediaVolumeIndex must be initialized after createStreamStates() because it |
| 811 | // relies on audio policy having correct ranges for volume indexes. |
| 812 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 813 | |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 814 | mPlaybackMonitor = |
| 815 | new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]); |
| 816 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 817 | mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 818 | |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 819 | mRecordMonitor = new RecordingActivityMonitor(mContext); |
| 820 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 821 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 822 | |
| 823 | // Call setRingerModeInt() to apply correct mute |
| 824 | // state on streams affected by ringer mode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 825 | mRingerAndZenModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 826 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 827 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 828 | // Register for device connection intent broadcasts. |
| 829 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 830 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 831 | intentFilter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 832 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 833 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 834 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 835 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 836 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 837 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 838 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 839 | intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 840 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 841 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 842 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 843 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 844 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 845 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 846 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 847 | intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); |
| 848 | intentFilter.addAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); |
| 849 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 850 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 851 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 852 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 853 | |
| 854 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 855 | |
| 856 | mRecordMonitor.initMonitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 859 | public void systemReady() { |
| 860 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 861 | 0, 0, null, 0); |
Dianne Hackborn | 3e3600e | 2017-08-31 11:15:26 -0700 | [diff] [blame] | 862 | if (false) { |
| 863 | // This is turned off for now, because it is racy and thus causes apps to break. |
| 864 | // Currently banning a uid means that if an app tries to start playing an audio |
| 865 | // stream, that will be preventing, and unbanning it will not allow that stream |
| 866 | // to resume. However these changes in uid state are racy with what the app is doing, |
| 867 | // so that after taking a process out of the cached state we can't guarantee that |
| 868 | // we will unban the uid before the app actually tries to start playing audio. |
| 869 | // (To do that, the activity manager would need to wait until it knows for sure |
| 870 | // that the ban has been removed, before telling the app to do whatever it is |
| 871 | // supposed to do that caused it to go out of the cached state.) |
| 872 | try { |
| 873 | ActivityManager.getService().registerUidObserver(mUidObserver, |
| 874 | ActivityManager.UID_OBSERVER_CACHED | ActivityManager.UID_OBSERVER_GONE, |
| 875 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 876 | } catch (RemoteException e) { |
| 877 | // ignored; both services live in system_server |
| 878 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 879 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | public void onSystemReady() { |
| 883 | mSystemReady = true; |
| 884 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 885 | 0, 0, null, 0); |
| 886 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 887 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 888 | resetBluetoothSco(); |
| 889 | getBluetoothHeadset(); |
| 890 | //FIXME: this is to maintain compatibility with deprecated intent |
| 891 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 892 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 893 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 894 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 895 | sendStickyBroadcastToAll(newIntent); |
| 896 | |
| 897 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 898 | if (adapter != null) { |
| 899 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 900 | BluetoothProfile.A2DP); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 901 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 902 | BluetoothProfile.HEARING_AID); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jeff Sharkey | 73ea0ae | 2016-08-10 17:30:38 -0600 | [diff] [blame] | 905 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) { |
| 906 | mHdmiManager = mContext.getSystemService(HdmiControlManager.class); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 907 | synchronized (mHdmiManager) { |
| 908 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 909 | if (mHdmiTvClient != null) { |
| 910 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 911 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 912 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 913 | mHdmiCecSink = false; |
| 914 | } |
| 915 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 916 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 917 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 918 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 919 | sendMsg(mAudioHandler, |
| 920 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 921 | SENDMSG_REPLACE, |
| 922 | 0, |
| 923 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 924 | TAG, |
Eric Laurent | 03332ab | 2017-02-09 18:29:15 -0800 | [diff] [blame] | 925 | SystemProperties.getBoolean("audio.safemedia.bypass", false) ? |
| 926 | 0 : SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 927 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 928 | initA11yMonitoring(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 929 | onIndicateSystemReady(); |
| 930 | } |
| 931 | |
| 932 | void onIndicateSystemReady() { |
| 933 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 934 | return; |
| 935 | } |
| 936 | sendMsg(mAudioHandler, |
| 937 | MSG_INDICATE_SYSTEM_READY, |
| 938 | SENDMSG_REPLACE, |
| 939 | 0, |
| 940 | 0, |
| 941 | null, |
| 942 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 943 | } |
| 944 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 945 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 946 | if (!mSystemReady || |
| 947 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 948 | Log.e(TAG, "Audioserver died."); |
| 949 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 950 | null, 500); |
| 951 | return; |
| 952 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 953 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 954 | |
| 955 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 956 | // server crash |
| 957 | // Note that we only execute this when the media server |
| 958 | // process restarts after a crash, not the first time it is started. |
| 959 | AudioSystem.setParameters("restarting=true"); |
| 960 | |
| 961 | readAndSetLowRamDevice(); |
| 962 | |
| 963 | // Restore device connection states |
| 964 | synchronized (mConnectedDevices) { |
| 965 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 966 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 967 | AudioSystem.setDeviceConnectionState( |
| 968 | spec.mDeviceType, |
| 969 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 970 | spec.mDeviceAddress, |
| 971 | spec.mDeviceName); |
| 972 | } |
| 973 | } |
| 974 | // Restore call state |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 975 | if (AudioSystem.setPhoneState(mMode) == AudioSystem.AUDIO_STATUS_OK) { |
| 976 | mModeLogger.log(new AudioEventLogger.StringEvent( |
| 977 | "onAudioServerDied causes setPhoneState(" + AudioSystem.modeToString(mMode) + ")")); |
| 978 | } |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 979 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 980 | // Restore forced usage for communications and record |
| 981 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, |
| 982 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 983 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 984 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_RECORD, mForcedUseForComm, |
| 985 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 986 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 987 | final int forSys; |
| 988 | synchronized (mSettingsLock) { |
| 989 | forSys = mCameraSoundForced ? |
| 990 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE; |
| 991 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 992 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_SYSTEM, forSys, |
| 993 | "onAudioServerDied")); |
| 994 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, forSys); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 995 | |
| 996 | // Restore stream volumes |
| 997 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 998 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 999 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 1000 | AudioSystem.initStreamVolume( |
| 1001 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1002 | |
| 1003 | streamState.applyAllVolumes(); |
| 1004 | } |
| 1005 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1006 | // Restore mono mode |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1007 | updateMasterMono(mContentResolver); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1008 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1009 | // Restore ringer mode |
| 1010 | setRingerModeInt(getRingerModeInternal(), false); |
| 1011 | |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 1012 | // Reset device rotation (if monitored for this device) |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1013 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 1014 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 1017 | synchronized (mBluetoothA2dpEnabledLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1018 | final int forMed = mBluetoothA2dpEnabled ? |
| 1019 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP; |
| 1020 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_MEDIA, forMed, |
| 1021 | "onAudioServerDied")); |
| 1022 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, forMed); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 1023 | } |
| 1024 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1025 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1026 | final int forDock = mDockAudioMediaEnabled ? |
| 1027 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE; |
| 1028 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, forDock, |
| 1029 | "onAudioServerDied")); |
| 1030 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, forDock); |
| 1031 | sendEncodedSurroundMode(mContentResolver, "onAudioServerDied"); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1032 | } |
| 1033 | if (mHdmiManager != null) { |
| 1034 | synchronized (mHdmiManager) { |
| 1035 | if (mHdmiTvClient != null) { |
| 1036 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | synchronized (mAudioPolicies) { |
| 1042 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 1043 | policy.connectMixes(); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | onIndicateSystemReady(); |
| 1048 | // indicate the end of reconfiguration phase to audio HAL |
| 1049 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 1050 | |
| 1051 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 1052 | SENDMSG_QUEUE, 1, 0, null, 0); |
| 1053 | } |
| 1054 | |
| 1055 | private void onDispatchAudioServerStateChange(boolean state) { |
| 1056 | synchronized (mAudioServerStateListeners) { |
| 1057 | for (AsdProxy asdp : mAudioServerStateListeners.values()) { |
| 1058 | try { |
| 1059 | asdp.callback().dispatchAudioServerStateChange(state); |
| 1060 | } catch (RemoteException e) { |
| 1061 | Log.w(TAG, "Could not call dispatchAudioServerStateChange()", e); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | private void createAudioSystemThread() { |
| 1068 | mAudioSystemThread = new AudioSystemThread(); |
| 1069 | mAudioSystemThread.start(); |
| 1070 | waitForAudioHandlerCreation(); |
| 1071 | } |
| 1072 | |
| 1073 | /** Waits for the volume handler to be created by the other thread. */ |
| 1074 | private void waitForAudioHandlerCreation() { |
| 1075 | synchronized(this) { |
| 1076 | while (mAudioHandler == null) { |
| 1077 | try { |
| 1078 | // Wait for mAudioHandler to be set by the other thread |
| 1079 | wait(); |
| 1080 | } catch (InterruptedException e) { |
| 1081 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1087 | private void checkAllAliasStreamVolumes() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1088 | synchronized (mSettingsLock) { |
| 1089 | synchronized (VolumeStreamState.class) { |
| 1090 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1091 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1092 | mStreamStates[streamType] |
| 1093 | .setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], TAG); |
| 1094 | // apply stream volume |
| 1095 | if (!mStreamStates[streamType].mIsMuted) { |
| 1096 | mStreamStates[streamType].applyAllVolumes(); |
| 1097 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1098 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1103 | private void checkAllFixedVolumeDevices() |
| 1104 | { |
| 1105 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1106 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1107 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1108 | } |
| 1109 | } |
| 1110 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1111 | private void checkAllFixedVolumeDevices(int streamType) { |
| 1112 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1113 | } |
| 1114 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1115 | private void checkMuteAffectedStreams() { |
| 1116 | // any stream with a min level > 0 is not muteable by definition |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1117 | // 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] | 1118 | for (int i = 0; i < mStreamStates.length; i++) { |
| 1119 | final VolumeStreamState vss = mStreamStates[i]; |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1120 | if (vss.mIndexMin > 0 && |
| 1121 | vss.mStreamType != AudioSystem.STREAM_VOICE_CALL) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1122 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1129 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 1130 | |
| 1131 | for (int i = 0; i < numStreamTypes; i++) { |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 1132 | streams[i] = |
| 1133 | new VolumeStreamState(System.VOLUME_SETTINGS_INT[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1134 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1136 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1137 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1138 | checkMuteAffectedStreams(); |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1139 | updateDefaultVolumes(); |
| 1140 | } |
| 1141 | |
| 1142 | // Update default indexes from aliased streams. Must be called after mStreamStates is created |
| 1143 | private void updateDefaultVolumes() { |
| 1144 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1145 | if (stream != mStreamVolumeAlias[stream]) { |
| 1146 | AudioSystem.DEFAULT_STREAM_VOLUME[stream] = rescaleIndex( |
| 1147 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]], |
| 1148 | mStreamVolumeAlias[stream], |
| 1149 | stream); |
| 1150 | } |
| 1151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1154 | private void dumpStreamStates(PrintWriter pw) { |
| 1155 | pw.println("\nStream volumes (device: index)"); |
| 1156 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1157 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1158 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1159 | mStreamStates[i].dump(pw); |
| 1160 | pw.println(""); |
| 1161 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1162 | pw.print("\n- mute affected streams = 0x"); |
| 1163 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1166 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1167 | int dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1168 | final int a11yStreamAlias = sIndependentA11yVolume ? |
| 1169 | AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1170 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1171 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1172 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 1173 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1174 | } else { |
| 1175 | switch (mPlatformType) { |
| 1176 | case AudioSystem.PLATFORM_VOICE: |
| 1177 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
| 1178 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
| 1179 | break; |
| 1180 | default: |
| 1181 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
| 1182 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 1183 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1184 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1185 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1186 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1187 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1188 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1189 | if (isInCommunication()) { |
| 1190 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 1191 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 1192 | } else { |
| 1193 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 1194 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1195 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1196 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1197 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1198 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY] = a11yStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1199 | |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1200 | if (updateVolumes && mStreamStates != null) { |
| 1201 | updateDefaultVolumes(); |
| 1202 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1203 | synchronized (mSettingsLock) { |
| 1204 | synchronized (VolumeStreamState.class) { |
| 1205 | mStreamStates[AudioSystem.STREAM_DTMF] |
| 1206 | .setAllIndexes(mStreamStates[dtmfStreamAlias], caller); |
| 1207 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].mVolumeIndexSettingName = |
| 1208 | System.VOLUME_SETTINGS_INT[a11yStreamAlias]; |
| 1209 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes( |
| 1210 | mStreamStates[a11yStreamAlias], caller); |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1211 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].refreshRange( |
| 1212 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY]); |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1213 | } |
| 1214 | } |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1215 | if (sIndependentA11yVolume) { |
| 1216 | // restore the a11y values from the settings |
| 1217 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].readSettings(); |
| 1218 | } |
| 1219 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1220 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1221 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1222 | sendMsg(mAudioHandler, |
| 1223 | MSG_SET_ALL_VOLUMES, |
| 1224 | SENDMSG_QUEUE, |
| 1225 | 0, |
| 1226 | 0, |
| 1227 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1228 | sendMsg(mAudioHandler, |
| 1229 | MSG_SET_ALL_VOLUMES, |
| 1230 | SENDMSG_QUEUE, |
| 1231 | 0, |
| 1232 | 0, |
| 1233 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY], 0); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1237 | private void readDockAudioSettings(ContentResolver cr) |
| 1238 | { |
| 1239 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1240 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1241 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1242 | sendMsg(mAudioHandler, |
| 1243 | MSG_SET_FORCE_USE, |
| 1244 | SENDMSG_QUEUE, |
| 1245 | AudioSystem.FOR_DOCK, |
| 1246 | mDockAudioMediaEnabled ? |
| 1247 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1248 | new String("readDockAudioSettings"), |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1249 | 0); |
| 1250 | } |
| 1251 | |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1252 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1253 | private void updateMasterMono(ContentResolver cr) |
| 1254 | { |
| 1255 | final boolean masterMono = System.getIntForUser( |
| 1256 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1257 | if (DEBUG_VOL) { |
| 1258 | Log.d(TAG, String.format("Master mono %b", masterMono)); |
| 1259 | } |
| 1260 | AudioSystem.setMasterMono(masterMono); |
| 1261 | } |
| 1262 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1263 | private void sendEncodedSurroundMode(ContentResolver cr, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1264 | { |
| 1265 | int encodedSurroundMode = Settings.Global.getInt( |
| 1266 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 1267 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1268 | sendEncodedSurroundMode(encodedSurroundMode, eventSource); |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1271 | private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1272 | { |
| 1273 | // initialize to guaranteed bad value |
| 1274 | int forceSetting = AudioSystem.NUM_FORCE_CONFIG; |
| 1275 | switch (encodedSurroundMode) { |
| 1276 | case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO: |
| 1277 | forceSetting = AudioSystem.FORCE_NONE; |
| 1278 | break; |
| 1279 | case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER: |
| 1280 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_NEVER; |
| 1281 | break; |
| 1282 | case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS: |
| 1283 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_ALWAYS; |
| 1284 | break; |
| 1285 | default: |
| 1286 | Log.e(TAG, "updateSurroundSoundSettings: illegal value " |
| 1287 | + encodedSurroundMode); |
| 1288 | break; |
| 1289 | } |
| 1290 | if (forceSetting != AudioSystem.NUM_FORCE_CONFIG) { |
| 1291 | sendMsg(mAudioHandler, |
| 1292 | MSG_SET_FORCE_USE, |
| 1293 | SENDMSG_QUEUE, |
| 1294 | AudioSystem.FOR_ENCODED_SURROUND, |
| 1295 | forceSetting, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1296 | eventSource, |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1297 | 0); |
| 1298 | } |
| 1299 | } |
| 1300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | private void readPersistedSettings() { |
| 1302 | final ContentResolver cr = mContentResolver; |
| 1303 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1304 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1305 | Settings.Global.getInt( |
| 1306 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1307 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1308 | // sanity check in case the settings are restored from a device with incompatible |
| 1309 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1310 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1311 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1312 | } |
| 1313 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1314 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1315 | } |
| 1316 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1317 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1318 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1319 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1320 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1321 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1322 | synchronized(mSettingsLock) { |
| 1323 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1324 | if (mRingerModeExternal == -1) { |
| 1325 | mRingerModeExternal = mRingerMode; |
| 1326 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1328 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1329 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1330 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1331 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1332 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1333 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1334 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1335 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1336 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1337 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1338 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1340 | updateRingerAndZenModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1341 | readDockAudioSettings(cr); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1342 | sendEncodedSurroundMode(cr, "readPersistedSettings"); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1343 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1344 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1345 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1346 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1347 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1349 | updateMasterMono(cr); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | // Each stream will read its own persisted settings |
| 1352 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1353 | // Broadcast the sticky intents |
| 1354 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1355 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | |
| 1357 | // Broadcast vibrate settings |
| 1358 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1359 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1360 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1361 | // Load settings for the volume controller |
| 1362 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | } |
| 1364 | |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1365 | private void readUserRestrictions() { |
| 1366 | final int currentUser = getCurrentUserId(); |
| 1367 | |
| 1368 | // Check the current user restriction. |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1369 | boolean masterMute = |
| 1370 | mUserManagerInternal.getUserRestriction(currentUser, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 1371 | UserManager.DISALLOW_UNMUTE_DEVICE) |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1372 | || mUserManagerInternal.getUserRestriction(currentUser, |
| 1373 | UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1374 | if (mUseFixedVolume) { |
| 1375 | masterMute = false; |
| 1376 | AudioSystem.setMasterVolume(1.0f); |
| 1377 | } |
| 1378 | if (DEBUG_VOL) { |
| 1379 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1380 | } |
| 1381 | setSystemAudioMute(masterMute); |
| 1382 | AudioSystem.setMasterMute(masterMute); |
| 1383 | broadcastMasterMuteStatus(masterMute); |
| 1384 | |
| 1385 | boolean microphoneMute = mUserManagerInternal.getUserRestriction( |
| 1386 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1387 | if (DEBUG_VOL) { |
| 1388 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1389 | } |
| 1390 | AudioSystem.muteMicrophone(microphoneMute); |
| 1391 | } |
| 1392 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1393 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1394 | final int rescaled = |
| 1395 | (index * mStreamStates[dstStream].getMaxIndex() |
| 1396 | + mStreamStates[srcStream].getMaxIndex() / 2) |
| 1397 | / mStreamStates[srcStream].getMaxIndex(); |
| 1398 | if (rescaled < mStreamStates[dstStream].getMinIndex()) { |
| 1399 | return mStreamStates[dstStream].getMinIndex(); |
| 1400 | } else { |
| 1401 | return rescaled; |
| 1402 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | |
| 1405 | /////////////////////////////////////////////////////////////////////////// |
| 1406 | // IPC methods |
| 1407 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1409 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1410 | String callingPackage, String caller) { |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1411 | final IAudioPolicyCallback extVolCtlr; |
| 1412 | synchronized (mExtVolumeControllerLock) { |
| 1413 | extVolCtlr = mExtVolumeController; |
| 1414 | } |
| 1415 | if (extVolCtlr != null) { |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 1416 | sendMsg(mAudioHandler, MSG_NOTIFY_VOL_EVENT, SENDMSG_QUEUE, |
| 1417 | direction, 0 /*ignored*/, |
| 1418 | extVolCtlr, 0 /*delay*/); |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1419 | } else { |
| 1420 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 1421 | caller, Binder.getCallingUid()); |
| 1422 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1426 | String callingPackage, String caller, int uid) { |
| 1427 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1428 | + ", flags=" + flags + ", caller=" + caller |
| 1429 | + ", volControlStream=" + mVolumeControlStream |
| 1430 | + ", userSelect=" + mUserSelectedVolumeControlStream); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1431 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_SUGG_VOL, suggestedStreamType, |
| 1432 | direction/*val1*/, flags/*val2*/, new StringBuilder(callingPackage) |
| 1433 | .append("/").append(caller).append(" uid:").append(uid).toString())); |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1434 | final int streamType; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1435 | synchronized (mForceControlStreamLock) { |
| 1436 | // Request lock in case mVolumeControlStream is changed by other thread. |
| 1437 | if (mUserSelectedVolumeControlStream) { // implies mVolumeControlStream != -1 |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1438 | streamType = mVolumeControlStream; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1439 | } else { |
| 1440 | final int maybeActiveStreamType = getActiveStreamType(suggestedStreamType); |
| 1441 | final boolean activeForReal; |
| 1442 | if (maybeActiveStreamType == AudioSystem.STREAM_RING |
| 1443 | || maybeActiveStreamType == AudioSystem.STREAM_NOTIFICATION) { |
| 1444 | activeForReal = wasStreamActiveRecently(maybeActiveStreamType, 0); |
| 1445 | } else { |
| 1446 | activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, 0); |
| 1447 | } |
| 1448 | if (activeForReal || mVolumeControlStream == -1) { |
| 1449 | streamType = maybeActiveStreamType; |
| 1450 | } else { |
| 1451 | streamType = mVolumeControlStream; |
| 1452 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1453 | } |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1454 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1455 | |
| 1456 | final boolean isMute = isMuteAdjust(direction); |
| 1457 | |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1458 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1459 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1461 | // Play sounds on STREAM_RING only. |
| 1462 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1463 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1465 | } |
| 1466 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1467 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1468 | // Don't suppress mute/unmute requests |
| 1469 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1470 | direction = 0; |
| 1471 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1472 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1473 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1474 | } |
| 1475 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1476 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1480 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1481 | String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1482 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1483 | Log.w(TAG, "Trying to call adjustStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1484 | + "CHANGE_ACCESSIBILITY_VOLUME / callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1485 | return; |
| 1486 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1487 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_STREAM_VOL, streamType, |
| 1488 | direction/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1489 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1490 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 1493 | protected void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1494 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1495 | if (mUseFixedVolume) { |
| 1496 | return; |
| 1497 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1498 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1499 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1500 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | ensureValidDirection(direction); |
| 1502 | ensureValidStreamType(streamType); |
| 1503 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1504 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1505 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1506 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1507 | return; |
| 1508 | } |
| 1509 | |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1510 | // If adjust is mute and the stream is STREAM_VOICE_CALL, make sure |
| 1511 | // that the calling app have the MODIFY_PHONE_STATE permission. |
| 1512 | if (isMuteAdjust && |
| 1513 | streamType == AudioSystem.STREAM_VOICE_CALL && |
| 1514 | mContext.checkCallingOrSelfPermission( |
| 1515 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1516 | != PackageManager.PERMISSION_GRANTED) { |
| 1517 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: adjustStreamVolume from pid=" |
| 1518 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1519 | return; |
| 1520 | } |
| 1521 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1522 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1523 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1524 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1525 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1526 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1527 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1528 | |
| 1529 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1530 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1531 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1533 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1534 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1535 | // skip a2dp absolute volume control request when the device |
| 1536 | // is not an a2dp device |
| 1537 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1538 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1539 | return; |
| 1540 | } |
| 1541 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1542 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1543 | // so we handle user restrictions correctly. |
| 1544 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1545 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1546 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1547 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1548 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1549 | return; |
| 1550 | } |
| 1551 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1552 | // reset any pending volume command |
| 1553 | synchronized (mSafeMediaVolumeState) { |
| 1554 | mPendingVolumeCommand = null; |
| 1555 | } |
| 1556 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1557 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1558 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1559 | ((device & mFixedVolumeDevices) != 0)) { |
| 1560 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1561 | |
| 1562 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1563 | // volume is enforced, and max and 0 for the others. |
| 1564 | // This is simulated by stepping by the full allowed volume range |
| 1565 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1566 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1567 | step = safeMediaVolumeIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1568 | } else { |
| 1569 | step = streamState.getMaxIndex(); |
| 1570 | } |
| 1571 | if (aliasIndex != 0) { |
| 1572 | aliasIndex = step; |
| 1573 | } |
| 1574 | } else { |
| 1575 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1576 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1577 | } |
| 1578 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1579 | // If either the client forces allowing ringer modes for this adjustment, |
| 1580 | // or the stream type is one that is affected by ringer modes |
| 1581 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1582 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1583 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1584 | // do not vibrate if already in vibrate mode |
| 1585 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1586 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1587 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1588 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1589 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1590 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1591 | streamState.mIsMuted, callingPackage, flags); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1592 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1593 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1594 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1595 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1596 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1597 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1598 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1599 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1600 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1601 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1602 | |
| 1603 | // If the ringer mode or zen is muting the stream, do not change stream unless |
| 1604 | // it'll cause us to exit dnd |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1605 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1606 | adjustVolume = false; |
| 1607 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1608 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1609 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1610 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1611 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1612 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1613 | if (isMuteAdjust) { |
| 1614 | boolean state; |
| 1615 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1616 | state = !streamState.mIsMuted; |
| 1617 | } else { |
| 1618 | state = direction == AudioManager.ADJUST_MUTE; |
| 1619 | } |
| 1620 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1621 | setSystemAudioMute(state); |
| 1622 | } |
| 1623 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1624 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1625 | if (!(readCameraSoundForced() |
| 1626 | && (mStreamStates[stream].getStreamType() |
| 1627 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1628 | mStreamStates[stream].mute(state); |
| 1629 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1633 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1634 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1635 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1636 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1637 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1638 | // Post message to set system volume (it in turn will post a |
| 1639 | // message to persist). |
| 1640 | if (streamState.mIsMuted) { |
| 1641 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1642 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1643 | // unmute immediately for volume up |
| 1644 | streamState.mute(false); |
| 1645 | } else if (direction == AudioManager.ADJUST_LOWER) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1646 | if (mIsSingleVolume) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1647 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1648 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1649 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1650 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1651 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1652 | sendMsg(mAudioHandler, |
| 1653 | MSG_SET_DEVICE_VOLUME, |
| 1654 | SENDMSG_QUEUE, |
| 1655 | device, |
| 1656 | 0, |
| 1657 | streamState, |
| 1658 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1659 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1660 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1661 | int newIndex = mStreamStates[streamType].getIndex(device); |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Check if volume update should be send to AVRCP |
| 1664 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1665 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1666 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1667 | synchronized (mA2dpAvrcpLock) { |
| 1668 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1669 | mA2dp.setAvrcpAbsoluteVolume(newIndex / 10); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1674 | // Check if volume update should be send to Hearing Aid |
| 1675 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 1676 | setHearingAidVolume(newIndex, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1679 | // Check if volume update should be sent to Hdmi system audio. |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1680 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1681 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1682 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1683 | if (mHdmiManager != null) { |
| 1684 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1685 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1686 | if (mHdmiCecSink && |
| 1687 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1688 | oldIndex != newIndex) { |
| 1689 | synchronized (mHdmiPlaybackClient) { |
| 1690 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1691 | KeyEvent.KEYCODE_VOLUME_UP; |
Donghyun Cho | 5f6d404e | 2016-03-17 20:39:25 +0900 | [diff] [blame] | 1692 | final long ident = Binder.clearCallingIdentity(); |
| 1693 | try { |
| 1694 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1695 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1696 | } finally { |
| 1697 | Binder.restoreCallingIdentity(ident); |
| 1698 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1699 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1703 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1704 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1705 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1708 | // Called after a delay when volume down is pressed while muted |
| 1709 | private void onUnmuteStream(int stream, int flags) { |
| 1710 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1711 | streamState.mute(false); |
| 1712 | |
| 1713 | final int device = getDeviceForStream(stream); |
| 1714 | final int index = mStreamStates[stream].getIndex(device); |
| 1715 | sendVolumeUpdate(stream, index, index, flags); |
| 1716 | } |
| 1717 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1718 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1719 | if (mHdmiManager == null |
| 1720 | || mHdmiTvClient == null |
| 1721 | || oldVolume == newVolume |
| 1722 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1723 | |
| 1724 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1725 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1726 | synchronized (mHdmiManager) { |
| 1727 | if (!mHdmiSystemAudioSupported) return; |
| 1728 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1729 | final long token = Binder.clearCallingIdentity(); |
| 1730 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1731 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1732 | } finally { |
| 1733 | Binder.restoreCallingIdentity(token); |
| 1734 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | } |
| 1738 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1739 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1740 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1741 | class StreamVolumeCommand { |
| 1742 | public final int mStreamType; |
| 1743 | public final int mIndex; |
| 1744 | public final int mFlags; |
| 1745 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1746 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1747 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1748 | mStreamType = streamType; |
| 1749 | mIndex = index; |
| 1750 | mFlags = flags; |
| 1751 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1752 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1753 | |
| 1754 | @Override |
| 1755 | public String toString() { |
| 1756 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1757 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1758 | .append(mDevice).append('}').toString(); |
| 1759 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1760 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1761 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1762 | private int getNewRingerMode(int stream, int index, int flags) { |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 1763 | // setRingerMode does nothing if the device is single volume,so the value would be unchanged |
| 1764 | if (mIsSingleVolume) { |
| 1765 | return getRingerModeExternal(); |
| 1766 | } |
| 1767 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1768 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1769 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1770 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1771 | int newRingerMode; |
| 1772 | if (index == 0) { |
| 1773 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1774 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1775 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1776 | } else { |
| 1777 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1778 | } |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1779 | return newRingerMode; |
| 1780 | } |
| 1781 | return getRingerModeExternal(); |
| 1782 | } |
| 1783 | |
| 1784 | private boolean isAndroidNPlus(String caller) { |
| 1785 | try { |
| 1786 | final ApplicationInfo applicationInfo = |
| 1787 | mContext.getPackageManager().getApplicationInfoAsUser( |
| 1788 | caller, 0, UserHandle.getUserId(Binder.getCallingUid())); |
| 1789 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1790 | return true; |
| 1791 | } |
| 1792 | return false; |
| 1793 | } catch (PackageManager.NameNotFoundException e) { |
| 1794 | return true; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | private boolean wouldToggleZenMode(int newMode) { |
| 1799 | if (getRingerModeExternal() == AudioManager.RINGER_MODE_SILENT |
| 1800 | && newMode != AudioManager.RINGER_MODE_SILENT) { |
| 1801 | return true; |
| 1802 | } else if (getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT |
| 1803 | && newMode == AudioManager.RINGER_MODE_SILENT) { |
| 1804 | return true; |
| 1805 | } |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1810 | String caller) { |
| 1811 | final int stream = mStreamVolumeAlias[streamType]; |
| 1812 | setStreamVolumeInt(stream, index, device, false, caller); |
| 1813 | // setting volume on ui sounds stream type also controls silent mode |
| 1814 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1815 | (stream == getUiSoundsStreamType())) { |
| 1816 | setRingerMode(getNewRingerMode(stream, index, flags), |
| 1817 | TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1818 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1819 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1820 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1824 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1825 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1826 | Log.w(TAG, "Trying to call setStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1827 | + " CHANGE_ACCESSIBILITY_VOLUME callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1828 | return; |
| 1829 | } |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1830 | if ((streamType == AudioManager.STREAM_VOICE_CALL) && |
| 1831 | (index == 0) && |
| 1832 | (mContext.checkCallingOrSelfPermission( |
| 1833 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1834 | != PackageManager.PERMISSION_GRANTED)) { |
| 1835 | Log.w(TAG, "Trying to call setStreamVolume() for STREAM_VOICE_CALL and index 0 without" |
| 1836 | + " MODIFY_PHONE_STATE callingPackage=" + callingPackage); |
| 1837 | return; |
| 1838 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1839 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType, |
| 1840 | index/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1841 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1842 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1845 | private boolean canChangeAccessibilityVolume() { |
| 1846 | synchronized (mAccessibilityServiceUidsLock) { |
| 1847 | if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 1848 | android.Manifest.permission.CHANGE_ACCESSIBILITY_VOLUME)) { |
| 1849 | return true; |
| 1850 | } |
| 1851 | if (mAccessibilityServiceUids != null) { |
| 1852 | int callingUid = Binder.getCallingUid(); |
| 1853 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 1854 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 1855 | return true; |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | return false; |
| 1860 | } |
| 1861 | } |
| 1862 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1863 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1864 | String caller, int uid) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1865 | if (DEBUG_VOL) { |
| 1866 | Log.d(TAG, "setStreamVolume(stream=" + streamType+", index=" + index |
| 1867 | + ", calling=" + callingPackage + ")"); |
| 1868 | } |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1869 | if (mUseFixedVolume) { |
| 1870 | return; |
| 1871 | } |
| 1872 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1873 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1874 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1875 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1876 | |
| 1877 | final int device = getDeviceForStream(streamType); |
| 1878 | int oldIndex; |
| 1879 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1880 | // skip a2dp absolute volume control request when the device |
| 1881 | // is not an a2dp device |
| 1882 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1883 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1884 | return; |
| 1885 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1886 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1887 | // so we handle user restrictions correctly. |
| 1888 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1889 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1890 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1891 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1892 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1893 | return; |
| 1894 | } |
| 1895 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1896 | if (isAndroidNPlus(callingPackage) |
| 1897 | && wouldToggleZenMode(getNewRingerMode(streamTypeAlias, index, flags)) |
| 1898 | && !mNm.isNotificationPolicyAccessGrantedForPackage(callingPackage)) { |
| 1899 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 1900 | } |
| 1901 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1902 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
| 1903 | return; |
| 1904 | } |
| 1905 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1906 | synchronized (mSafeMediaVolumeState) { |
| 1907 | // reset any pending volume command |
| 1908 | mPendingVolumeCommand = null; |
| 1909 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1910 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1911 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1912 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1913 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1914 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1915 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1916 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1917 | synchronized (mA2dpAvrcpLock) { |
| 1918 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1919 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1920 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1924 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 1925 | setHearingAidVolume(index, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1928 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1929 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1930 | } |
| 1931 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1932 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1933 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1934 | ((device & mFixedVolumeDevices) != 0)) { |
| 1935 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1936 | |
| 1937 | // volume is either 0 or max allowed for fixed volume devices |
| 1938 | if (index != 0) { |
| 1939 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1940 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1941 | index = safeMediaVolumeIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1942 | } else { |
| 1943 | index = streamState.getMaxIndex(); |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1948 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1949 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1950 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1951 | streamType, index, flags, device); |
| 1952 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1953 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1954 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1955 | } |
| 1956 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1957 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1960 | // No ringer or zen muted stream volumes can be changed unless it'll exit dnd |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1961 | private boolean volumeAdjustmentAllowedByDnd(int streamTypeAlias, int flags) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1962 | switch (mNm.getZenMode()) { |
| 1963 | case Settings.Global.ZEN_MODE_OFF: |
| 1964 | return true; |
| 1965 | case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: |
| 1966 | case Settings.Global.ZEN_MODE_ALARMS: |
| 1967 | case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 1968 | return !isStreamMutedByRingerOrZenMode(streamTypeAlias) |
| 1969 | || streamTypeAlias == getUiSoundsStreamType() |
| 1970 | || (flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0; |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1971 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1972 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1973 | return true; |
| 1974 | } |
| 1975 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1976 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1977 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1978 | if (DEBUG_VOL) { Log.d(TAG, String.format("forceVolumeControlStream(%d)", streamType)); } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1979 | synchronized(mForceControlStreamLock) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1980 | if (mVolumeControlStream != -1 && streamType != -1) { |
| 1981 | mUserSelectedVolumeControlStream = true; |
| 1982 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1983 | mVolumeControlStream = streamType; |
| 1984 | if (mVolumeControlStream == -1) { |
| 1985 | if (mForceControlStreamClient != null) { |
| 1986 | mForceControlStreamClient.release(); |
| 1987 | mForceControlStreamClient = null; |
| 1988 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1989 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1990 | } else { |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 1991 | if (null == mForceControlStreamClient) { |
| 1992 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1993 | } else { |
| 1994 | if (mForceControlStreamClient.getBinder() == cb) { |
| 1995 | Log.d(TAG, "forceVolumeControlStream cb:" + cb + " is already linked."); |
| 1996 | } else { |
| 1997 | mForceControlStreamClient.release(); |
| 1998 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1999 | } |
| 2000 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 2006 | private IBinder mCb; // To be notified of client's death |
| 2007 | |
| 2008 | ForceControlStreamClient(IBinder cb) { |
| 2009 | if (cb != null) { |
| 2010 | try { |
| 2011 | cb.linkToDeath(this, 0); |
| 2012 | } catch (RemoteException e) { |
| 2013 | // Client has died! |
| 2014 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 2015 | cb = null; |
| 2016 | } |
| 2017 | } |
| 2018 | mCb = cb; |
| 2019 | } |
| 2020 | |
| 2021 | public void binderDied() { |
| 2022 | synchronized(mForceControlStreamLock) { |
| 2023 | Log.w(TAG, "SCO client died"); |
| 2024 | if (mForceControlStreamClient != this) { |
| 2025 | Log.w(TAG, "unregistered control stream client died"); |
| 2026 | } else { |
| 2027 | mForceControlStreamClient = null; |
| 2028 | mVolumeControlStream = -1; |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2029 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | public void release() { |
| 2035 | if (mCb != null) { |
| 2036 | mCb.unlinkToDeath(this, 0); |
| 2037 | mCb = null; |
| 2038 | } |
| 2039 | } |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 2040 | |
| 2041 | public IBinder getBinder() { |
| 2042 | return mCb; |
| 2043 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2046 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 2047 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2048 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2049 | final long ident = Binder.clearCallingIdentity(); |
| 2050 | try { |
| 2051 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2052 | } finally { |
| 2053 | Binder.restoreCallingIdentity(ident); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2058 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2059 | final long ident = Binder.clearCallingIdentity(); |
| 2060 | try { |
| 2061 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2062 | } finally { |
| 2063 | Binder.restoreCallingIdentity(ident); |
| 2064 | } |
| 2065 | } |
| 2066 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2067 | private int getCurrentUserId() { |
| 2068 | final long ident = Binder.clearCallingIdentity(); |
| 2069 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2070 | UserInfo currentUser = ActivityManager.getService().getCurrentUser(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2071 | return currentUser.id; |
| 2072 | } catch (RemoteException e) { |
| 2073 | // Activity manager not running, nothing we can do assume user 0. |
| 2074 | } finally { |
| 2075 | Binder.restoreCallingIdentity(ident); |
| 2076 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 2077 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2080 | // UI update and Broadcast Intent |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 2081 | protected void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 2082 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2083 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2084 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 2085 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 2086 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2087 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2090 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 2091 | // receives volume notification from Audio Receiver. |
| 2092 | private int updateFlagsForSystemAudio(int flags) { |
| 2093 | if (mHdmiTvClient != null) { |
| 2094 | synchronized (mHdmiTvClient) { |
| 2095 | if (mHdmiSystemAudioSupported && |
| 2096 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 2097 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | return flags; |
| 2102 | } |
| 2103 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2104 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2105 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2106 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2107 | broadcastMasterMuteStatus(muted); |
| 2108 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2109 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2110 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2111 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2112 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2113 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 2114 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2115 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2116 | } |
| 2117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2119 | * Sets the stream state's index, and posts a message to set system volume. |
| 2120 | * This will not call out to the UI. Assumes a valid stream type. |
| 2121 | * |
| 2122 | * @param streamType Type of the stream |
| 2123 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2124 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | * @param force If true, set the volume even if the desired volume is same |
| 2126 | * as the current volume. |
| 2127 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2128 | private void setStreamVolumeInt(int streamType, |
| 2129 | int index, |
| 2130 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2131 | boolean force, |
| 2132 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2134 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2135 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2136 | // Post message to set system volume (it in turn will post a message |
| 2137 | // to persist). |
| 2138 | sendMsg(mAudioHandler, |
| 2139 | MSG_SET_DEVICE_VOLUME, |
| 2140 | SENDMSG_QUEUE, |
| 2141 | device, |
| 2142 | 0, |
| 2143 | streamState, |
| 2144 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2148 | private void setSystemAudioMute(boolean state) { |
| 2149 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 2150 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 2151 | if (!mHdmiSystemAudioSupported) return; |
| 2152 | synchronized (mHdmiTvClient) { |
| 2153 | final long token = Binder.clearCallingIdentity(); |
| 2154 | try { |
| 2155 | mHdmiTvClient.setSystemAudioMute(state); |
| 2156 | } finally { |
| 2157 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2158 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | } |
| 2162 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2163 | /** get stream mute state. */ |
| 2164 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2165 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 2166 | streamType = getActiveStreamType(streamType); |
| 2167 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2168 | synchronized (VolumeStreamState.class) { |
jiabin | 93d32ef | 2017-07-11 13:50:02 -0700 | [diff] [blame] | 2169 | ensureValidStreamType(streamType); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2170 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2171 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 2174 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 2175 | private IBinder mICallback; // To be notified of client's death |
| 2176 | |
| 2177 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 2178 | mICallback = cb; |
| 2179 | try { |
| 2180 | cb.linkToDeath(this, 0/*flags*/); |
| 2181 | } catch (RemoteException e) { |
| 2182 | Log.e(TAG, "can't link to death", e); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | boolean isHandlerFor(IBinder cb) { |
| 2187 | return mICallback.equals(cb); |
| 2188 | } |
| 2189 | |
| 2190 | void forget() { |
| 2191 | try { |
| 2192 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 2193 | } catch (NoSuchElementException e) { |
| 2194 | Log.e(TAG, "error unlinking to death", e); |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | public void binderDied() { |
| 2199 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 2200 | forceRemoteSubmixFullVolume(false, mICallback); |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | /** |
| 2205 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 2206 | * @return true if there is a registered death handler, false otherwise */ |
| 2207 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2208 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2209 | while (it.hasNext()) { |
| 2210 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 2211 | if (handler.isHandlerFor(cb)) { |
| 2212 | handler.forget(); |
| 2213 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 2214 | return true; |
| 2215 | } |
| 2216 | } |
| 2217 | return false; |
| 2218 | } |
| 2219 | |
| 2220 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 2221 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2222 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2223 | while (it.hasNext()) { |
| 2224 | if (it.next().isHandlerFor(cb)) { |
| 2225 | return true; |
| 2226 | } |
| 2227 | } |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
| 2231 | private int mRmtSbmxFullVolRefCount = 0; |
| 2232 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 2233 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 2234 | |
| 2235 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 2236 | if (cb == null) { |
| 2237 | return; |
| 2238 | } |
| 2239 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 2240 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 2241 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 2242 | return; |
| 2243 | } |
| 2244 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 2245 | boolean applyRequired = false; |
| 2246 | if (startForcing) { |
| 2247 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 2248 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 2249 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2250 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2251 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2252 | applyRequired = true; |
| 2253 | } |
| 2254 | mRmtSbmxFullVolRefCount++; |
| 2255 | } |
| 2256 | } else { |
| 2257 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 2258 | mRmtSbmxFullVolRefCount--; |
| 2259 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2260 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2261 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2262 | applyRequired = true; |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | if (applyRequired) { |
| 2267 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 2268 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 2269 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2274 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 2275 | int userId) { |
| 2276 | // If we are being called by the system check for user we are going to change |
| 2277 | // so we handle user restrictions correctly. |
| 2278 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2279 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2280 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2281 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 2282 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2283 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 2284 | return; |
| 2285 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2286 | if (userId != UserHandle.getCallingUserId() && |
| 2287 | mContext.checkCallingOrSelfPermission( |
| 2288 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2289 | != PackageManager.PERMISSION_GRANTED) { |
| 2290 | return; |
| 2291 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2292 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 2293 | } |
| 2294 | |
| 2295 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 2296 | if (DEBUG_VOL) { |
| 2297 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 2298 | } |
| 2299 | if (mUseFixedVolume) { |
| 2300 | return; // If using fixed volume, we don't mute. |
| 2301 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2302 | if (getCurrentUserId() == userId) { |
| 2303 | if (mute != AudioSystem.getMasterMute()) { |
| 2304 | setSystemAudioMute(mute); |
| 2305 | AudioSystem.setMasterMute(mute); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2306 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2307 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2308 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2309 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 2310 | sendBroadcastToAll(intent); |
| 2311 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 2312 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /** get master mute state. */ |
| 2316 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 2317 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2320 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 2321 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 2322 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2323 | } |
| 2324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2325 | /** @see AudioManager#getStreamVolume(int) */ |
| 2326 | public int getStreamVolume(int streamType) { |
| 2327 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2328 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2329 | synchronized (VolumeStreamState.class) { |
| 2330 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 2331 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2332 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2333 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2334 | index = 0; |
| 2335 | } |
| 2336 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 2337 | (device & mFixedVolumeDevices) != 0) { |
| 2338 | index = mStreamStates[streamType].getMaxIndex(); |
| 2339 | } |
| 2340 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2341 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 2345 | public int getStreamMaxVolume(int streamType) { |
| 2346 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2347 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 2350 | /** @see AudioManager#getStreamMinVolumeInt(int) */ |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2351 | public int getStreamMinVolume(int streamType) { |
| 2352 | ensureValidStreamType(streamType); |
| 2353 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 2354 | } |
| 2355 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2356 | /** Get last audible volume before stream was muted. */ |
| 2357 | public int getLastAudibleStreamVolume(int streamType) { |
| 2358 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2359 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2360 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2361 | } |
| 2362 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2363 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 2364 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 2365 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2368 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 2369 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2370 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 2371 | // If we are being called by the system check for user we are going to change |
| 2372 | // so we handle user restrictions correctly. |
| 2373 | int uid = Binder.getCallingUid(); |
| 2374 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2375 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2376 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2377 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 2378 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2379 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2380 | return; |
| 2381 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 2382 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 2383 | return; |
| 2384 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2385 | if (userId != UserHandle.getCallingUserId() && |
| 2386 | mContext.checkCallingOrSelfPermission( |
| 2387 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2388 | != PackageManager.PERMISSION_GRANTED) { |
| 2389 | return; |
| 2390 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2391 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 2392 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2393 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2394 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 2395 | if (DEBUG_VOL) { |
| 2396 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 2397 | } |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2398 | // only mute for the current user |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2399 | if (getCurrentUserId() == userId) { |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2400 | final boolean currentMute = AudioSystem.isMicrophoneMuted(); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2401 | final long identity = Binder.clearCallingIdentity(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2402 | AudioSystem.muteMicrophone(on); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2403 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2404 | if (on != currentMute) { |
| 2405 | mContext.sendBroadcast(new Intent(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED) |
| 2406 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)); |
| 2407 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2408 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2409 | } |
| 2410 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2411 | @Override |
| 2412 | public int getRingerModeExternal() { |
| 2413 | synchronized(mSettingsLock) { |
| 2414 | return mRingerModeExternal; |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | @Override |
| 2419 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2420 | synchronized(mSettingsLock) { |
| 2421 | return mRingerMode; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2426 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2427 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 2428 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2431 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2432 | public boolean isValidRingerMode(int ringerMode) { |
| 2433 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2434 | } |
| 2435 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2436 | public void setRingerModeExternal(int ringerMode, String caller) { |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2437 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 2438 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller)) { |
| 2439 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 2440 | } |
| 2441 | |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2442 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2446 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2447 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2448 | } |
| 2449 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2450 | public void silenceRingerModeInternal(String reason) { |
| 2451 | VibrationEffect effect = null; |
| 2452 | int ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2453 | int toastText = 0; |
| 2454 | |
| 2455 | int silenceRingerSetting = Settings.Secure.VOLUME_HUSH_OFF; |
| 2456 | if (mContext.getResources() |
| 2457 | .getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled)) { |
| 2458 | silenceRingerSetting = Settings.Secure.getIntForUser(mContentResolver, |
| 2459 | Settings.Secure.VOLUME_HUSH_GESTURE, VOLUME_HUSH_OFF, |
| 2460 | UserHandle.USER_CURRENT); |
| 2461 | } |
| 2462 | |
| 2463 | switch(silenceRingerSetting) { |
| 2464 | case VOLUME_HUSH_MUTE: |
| 2465 | effect = VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK); |
| 2466 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2467 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_silent; |
| 2468 | break; |
| 2469 | case VOLUME_HUSH_VIBRATE: |
| 2470 | effect = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK); |
| 2471 | ringerMode = AudioManager.RINGER_MODE_VIBRATE; |
| 2472 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_vibrate; |
| 2473 | break; |
| 2474 | } |
| 2475 | maybeVibrate(effect); |
| 2476 | setRingerModeInternal(ringerMode, reason); |
| 2477 | Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT).show(); |
| 2478 | } |
| 2479 | |
| 2480 | private boolean maybeVibrate(VibrationEffect effect) { |
| 2481 | if (!mHasVibrator) { |
| 2482 | return false; |
| 2483 | } |
| 2484 | final boolean hapticsDisabled = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 2485 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) == 0; |
| 2486 | if (hapticsDisabled) { |
| 2487 | return false; |
| 2488 | } |
| 2489 | |
| 2490 | if (effect == null) { |
| 2491 | return false; |
| 2492 | } |
| 2493 | mVibrator.vibrate( |
| 2494 | Binder.getCallingUid(), mContext.getOpPackageName(), effect, VIBRATION_ATTRIBUTES); |
| 2495 | return true; |
| 2496 | } |
| 2497 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2498 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 2499 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2500 | return; |
| 2501 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2502 | if (caller == null || caller.length() == 0) { |
| 2503 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2504 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2505 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2506 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2507 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2508 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2509 | final long identity = Binder.clearCallingIdentity(); |
| 2510 | try { |
| 2511 | synchronized (mSettingsLock) { |
| 2512 | final int ringerModeInternal = getRingerModeInternal(); |
| 2513 | final int ringerModeExternal = getRingerModeExternal(); |
| 2514 | if (external) { |
| 2515 | setRingerModeExt(ringerMode); |
| 2516 | if (mRingerModeDelegate != null) { |
| 2517 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2518 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2519 | } |
| 2520 | if (ringerMode != ringerModeInternal) { |
| 2521 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2522 | } |
| 2523 | } else /*internal*/ { |
| 2524 | if (ringerMode != ringerModeInternal) { |
| 2525 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2526 | } |
| 2527 | if (mRingerModeDelegate != null) { |
| 2528 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2529 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2530 | } |
| 2531 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2532 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2533 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2534 | } finally { |
| 2535 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2539 | private void setRingerModeExt(int ringerMode) { |
| 2540 | synchronized(mSettingsLock) { |
| 2541 | if (ringerMode == mRingerModeExternal) return; |
| 2542 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2543 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2544 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2545 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2546 | } |
| 2547 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2548 | private void muteRingerModeStreams() { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2549 | // Mute stream if not previously muted by ringer mode and (ringer mode |
| 2550 | // is not RINGER_MODE_NORMAL OR stream is zen muted) and stream is affected by ringer mode. |
| 2551 | // Unmute stream if previously muted by ringer/zen mode and ringer mode |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2552 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2553 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2554 | |
| 2555 | if (mNm == null) { |
| 2556 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 2557 | } |
| 2558 | |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2559 | final int ringerMode = mRingerMode; // Read ringer mode as reading primitives is atomic |
| 2560 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2561 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
| 2562 | final boolean shouldRingSco = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2563 | && isBluetoothScoOn(); |
| 2564 | // Ask audio policy engine to force use Bluetooth SCO channel if needed |
| 2565 | final String eventSource = "muteRingerModeStreams() from u/pid:" + Binder.getCallingUid() |
| 2566 | + "/" + Binder.getCallingPid(); |
| 2567 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, AudioSystem.FOR_VIBRATE_RINGING, |
| 2568 | shouldRingSco ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE, eventSource, 0); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2569 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2570 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2571 | final boolean isMuted = isStreamMutedByRingerOrZenMode(streamType); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2572 | final boolean muteAllowedBySco = |
| 2573 | !(shouldRingSco && streamType == AudioSystem.STREAM_RING); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2574 | final boolean shouldZenMute = shouldZenMuteStream(streamType); |
| 2575 | final boolean shouldMute = shouldZenMute || (ringerModeMute |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2576 | && isStreamAffectedByRingerMode(streamType) && muteAllowedBySco); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2577 | if (isMuted == shouldMute) continue; |
| 2578 | if (!shouldMute) { |
| 2579 | // unmute |
| 2580 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2581 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2582 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2583 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2584 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2585 | int device = vss.mIndexMap.keyAt(i); |
| 2586 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2587 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2588 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2589 | } |
| 2590 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2591 | // Persist volume for stream ring when it is changed here |
| 2592 | final int device = getDeviceForStream(streamType); |
| 2593 | sendMsg(mAudioHandler, |
| 2594 | MSG_PERSIST_VOLUME, |
| 2595 | SENDMSG_QUEUE, |
| 2596 | device, |
| 2597 | 0, |
| 2598 | mStreamStates[streamType], |
| 2599 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2600 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2601 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2602 | mStreamStates[streamType].mute(false); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2603 | mRingerAndZenModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2604 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2605 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2606 | mStreamStates[streamType].mute(true); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2607 | mRingerAndZenModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2608 | } |
| 2609 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2610 | } |
| 2611 | |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2612 | private boolean isAlarm(int streamType) { |
| 2613 | return streamType == AudioSystem.STREAM_ALARM; |
| 2614 | } |
| 2615 | |
| 2616 | private boolean isNotificationOrRinger(int streamType) { |
| 2617 | return streamType == AudioSystem.STREAM_NOTIFICATION |
| 2618 | || streamType == AudioSystem.STREAM_RING; |
| 2619 | } |
| 2620 | |
| 2621 | private boolean isMedia(int streamType) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2622 | return streamType == AudioSystem.STREAM_MUSIC; |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | private boolean isSystem(int streamType) { |
| 2627 | return streamType == AudioSystem.STREAM_SYSTEM; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2628 | } |
| 2629 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2630 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2631 | final boolean change; |
| 2632 | synchronized(mSettingsLock) { |
| 2633 | change = mRingerMode != ringerMode; |
| 2634 | mRingerMode = ringerMode; |
| 2635 | } |
| 2636 | |
| 2637 | muteRingerModeStreams(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2638 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2639 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2640 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2641 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2642 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2643 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2644 | if (change) { |
| 2645 | // Send sticky broadcast |
| 2646 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2647 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2650 | /** @see AudioManager#shouldVibrate(int) */ |
| 2651 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2652 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | |
| 2654 | switch (getVibrateSetting(vibrateType)) { |
| 2655 | |
| 2656 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2657 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2658 | |
| 2659 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2660 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | |
| 2662 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2663 | // return false, even for incoming calls |
| 2664 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2665 | |
| 2666 | default: |
| 2667 | return false; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2672 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2673 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2675 | } |
| 2676 | |
| 2677 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2678 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2679 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2680 | if (!mHasVibrator) return; |
| 2681 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2682 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2683 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | |
| 2685 | // Broadcast change |
| 2686 | broadcastVibrateSetting(vibrateType); |
| 2687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2690 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2691 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2692 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2693 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2694 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2695 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2696 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2697 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2701 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2702 | synchronized(mSetModeDeathHandlers) { |
| 2703 | Log.w(TAG, "setMode() client died"); |
| 2704 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2705 | if (index < 0) { |
| 2706 | Log.w(TAG, "unregistered setMode() client died"); |
| 2707 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2708 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2709 | } |
| 2710 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2711 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2712 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2713 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2714 | final long ident = Binder.clearCallingIdentity(); |
| 2715 | disconnectBluetoothSco(newModeOwnerPid); |
| 2716 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2717 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2718 | } |
| 2719 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2720 | public int getPid() { |
| 2721 | return mPid; |
| 2722 | } |
| 2723 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2724 | public void setMode(int mode) { |
| 2725 | mMode = mode; |
| 2726 | } |
| 2727 | |
| 2728 | public int getMode() { |
| 2729 | return mMode; |
| 2730 | } |
| 2731 | |
| 2732 | public IBinder getBinder() { |
| 2733 | return mCb; |
| 2734 | } |
| 2735 | } |
| 2736 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2738 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2739 | 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] | 2740 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2741 | return; |
| 2742 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2743 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2744 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2745 | (mContext.checkCallingOrSelfPermission( |
| 2746 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2747 | != PackageManager.PERMISSION_GRANTED)) { |
| 2748 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2749 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2750 | return; |
| 2751 | } |
| 2752 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2753 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2754 | return; |
| 2755 | } |
| 2756 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2757 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2758 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2759 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2760 | mode = mMode; |
| 2761 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2762 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2763 | } |
| 2764 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2765 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2766 | if (newModeOwnerPid != 0) { |
| 2767 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2768 | } |
| 2769 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2770 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2771 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2772 | // 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] | 2773 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2774 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2775 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2776 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2777 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2778 | if (cb == null) { |
| 2779 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2780 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2781 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2782 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2783 | SetModeDeathHandler hdlr = null; |
| 2784 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2785 | while (iter.hasNext()) { |
| 2786 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2787 | if (h.getPid() == pid) { |
| 2788 | hdlr = h; |
| 2789 | // Remove from client list so that it is re-inserted at top of list |
| 2790 | iter.remove(); |
| 2791 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2792 | break; |
| 2793 | } |
| 2794 | } |
| 2795 | int status = AudioSystem.AUDIO_STATUS_OK; |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2796 | int actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2797 | do { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2798 | actualMode = mode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2799 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2800 | // get new mode from client at top the list if any |
| 2801 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2802 | hdlr = mSetModeDeathHandlers.get(0); |
| 2803 | cb = hdlr.getBinder(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2804 | actualMode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2805 | if (DEBUG_MODE) { |
| 2806 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2807 | + hdlr.mPid); |
| 2808 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2809 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2810 | } else { |
| 2811 | if (hdlr == null) { |
| 2812 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2813 | } |
| 2814 | // Register for client death notification |
| 2815 | try { |
| 2816 | cb.linkToDeath(hdlr, 0); |
| 2817 | } catch (RemoteException e) { |
| 2818 | // Client has died! |
| 2819 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2820 | } |
| 2821 | |
| 2822 | // Last client to call setMode() is always at top of client list |
| 2823 | // as required by SetModeDeathHandler.binderDied() |
| 2824 | mSetModeDeathHandlers.add(0, hdlr); |
| 2825 | hdlr.setMode(mode); |
| 2826 | } |
| 2827 | |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2828 | if (actualMode != mMode) { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2829 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2830 | status = AudioSystem.setPhoneState(actualMode); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2831 | Binder.restoreCallingIdentity(identity); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2832 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2833 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + actualMode); } |
| 2834 | mMode = actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2835 | } else { |
| 2836 | if (hdlr != null) { |
| 2837 | mSetModeDeathHandlers.remove(hdlr); |
| 2838 | cb.unlinkToDeath(hdlr, 0); |
| 2839 | } |
| 2840 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2841 | 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] | 2842 | mode = AudioSystem.MODE_NORMAL; |
| 2843 | } |
| 2844 | } else { |
| 2845 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2846 | } |
| 2847 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2848 | |
| 2849 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2850 | if (actualMode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2851 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2852 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2853 | } else { |
| 2854 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2855 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | } |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2857 | // Note: newModeOwnerPid is always 0 when actualMode is MODE_NORMAL |
| 2858 | mModeLogger.log( |
| 2859 | new PhoneStateEvent(caller, pid, mode, newModeOwnerPid, actualMode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2861 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2862 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2863 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2864 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2865 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2867 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | /** @see AudioManager#getMode() */ |
| 2871 | public int getMode() { |
| 2872 | return mMode; |
| 2873 | } |
| 2874 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2875 | //========================================================================================== |
| 2876 | // Sound Effects |
| 2877 | //========================================================================================== |
| 2878 | |
| 2879 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2880 | private static final String ATTR_VERSION = "version"; |
| 2881 | private static final String TAG_GROUP = "group"; |
| 2882 | private static final String ATTR_GROUP_NAME = "name"; |
| 2883 | private static final String TAG_ASSET = "asset"; |
| 2884 | private static final String ATTR_ASSET_ID = "id"; |
| 2885 | private static final String ATTR_ASSET_FILE = "file"; |
| 2886 | |
| 2887 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2888 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2889 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2890 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2891 | |
| 2892 | class LoadSoundEffectReply { |
| 2893 | public int mStatus = 1; |
| 2894 | }; |
| 2895 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2896 | private void loadTouchSoundAssetDefaults() { |
| 2897 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2898 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2899 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2900 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | private void loadTouchSoundAssets() { |
| 2905 | XmlResourceParser parser = null; |
| 2906 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2907 | // only load assets once. |
| 2908 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2909 | return; |
| 2910 | } |
| 2911 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2912 | loadTouchSoundAssetDefaults(); |
| 2913 | |
| 2914 | try { |
| 2915 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2916 | |
| 2917 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2918 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2919 | boolean inTouchSoundsGroup = false; |
| 2920 | |
| 2921 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2922 | while (true) { |
| 2923 | XmlUtils.nextElement(parser); |
| 2924 | String element = parser.getName(); |
| 2925 | if (element == null) { |
| 2926 | break; |
| 2927 | } |
| 2928 | if (element.equals(TAG_GROUP)) { |
| 2929 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2930 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2931 | inTouchSoundsGroup = true; |
| 2932 | break; |
| 2933 | } |
| 2934 | } |
| 2935 | } |
| 2936 | while (inTouchSoundsGroup) { |
| 2937 | XmlUtils.nextElement(parser); |
| 2938 | String element = parser.getName(); |
| 2939 | if (element == null) { |
| 2940 | break; |
| 2941 | } |
| 2942 | if (element.equals(TAG_ASSET)) { |
| 2943 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2944 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2945 | int fx; |
| 2946 | |
| 2947 | try { |
| 2948 | Field field = AudioManager.class.getField(id); |
| 2949 | fx = field.getInt(null); |
| 2950 | } catch (Exception e) { |
| 2951 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2952 | continue; |
| 2953 | } |
| 2954 | |
| 2955 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2956 | if (i == -1) { |
| 2957 | i = SOUND_EFFECT_FILES.size(); |
| 2958 | SOUND_EFFECT_FILES.add(file); |
| 2959 | } |
| 2960 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2961 | } else { |
| 2962 | break; |
| 2963 | } |
| 2964 | } |
| 2965 | } |
| 2966 | } catch (Resources.NotFoundException e) { |
| 2967 | Log.w(TAG, "audio assets file not found", e); |
| 2968 | } catch (XmlPullParserException e) { |
| 2969 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2970 | } catch (IOException e) { |
| 2971 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2972 | } finally { |
| 2973 | if (parser != null) { |
| 2974 | parser.close(); |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2979 | /** @see AudioManager#playSoundEffect(int) */ |
| 2980 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2981 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | public void playSoundEffectVolume(int effectType, float volume) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2986 | // do not try to play the sound effect if the system stream is muted |
| 2987 | if (isStreamMutedByRingerOrZenMode(STREAM_SYSTEM)) { |
| 2988 | return; |
| 2989 | } |
| 2990 | |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2991 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2992 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2993 | return; |
| 2994 | } |
| 2995 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2996 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2997 | effectType, (int) (volume * 1000), null, 0); |
| 2998 | } |
| 2999 | |
| 3000 | /** |
| 3001 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 3002 | * 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] | 3003 | */ |
| 3004 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3005 | int attempts = 3; |
| 3006 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3007 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3008 | synchronized (reply) { |
| 3009 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 3010 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3011 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3012 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3013 | } catch (InterruptedException e) { |
| 3014 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3015 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3016 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3017 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3018 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | /** |
| 3022 | * Unloads samples from the sound pool. |
| 3023 | * This method can be called to free some memory when |
| 3024 | * sound effects are disabled. |
| 3025 | */ |
| 3026 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3027 | 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] | 3028 | } |
| 3029 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3030 | class SoundPoolListenerThread extends Thread { |
| 3031 | public SoundPoolListenerThread() { |
| 3032 | super("SoundPoolListenerThread"); |
| 3033 | } |
| 3034 | |
| 3035 | @Override |
| 3036 | public void run() { |
| 3037 | |
| 3038 | Looper.prepare(); |
| 3039 | mSoundPoolLooper = Looper.myLooper(); |
| 3040 | |
| 3041 | synchronized (mSoundEffectsLock) { |
| 3042 | if (mSoundPool != null) { |
| 3043 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 3044 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 3045 | } |
| 3046 | mSoundEffectsLock.notify(); |
| 3047 | } |
| 3048 | Looper.loop(); |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | private final class SoundPoolCallback implements |
| 3053 | android.media.SoundPool.OnLoadCompleteListener { |
| 3054 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3055 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 3056 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3057 | |
| 3058 | public int status() { |
| 3059 | return mStatus; |
| 3060 | } |
| 3061 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3062 | public void setSamples(int[] samples) { |
| 3063 | for (int i = 0; i < samples.length; i++) { |
| 3064 | // do not wait ack for samples rejected upfront by SoundPool |
| 3065 | if (samples[i] > 0) { |
| 3066 | mSamples.add(samples[i]); |
| 3067 | } |
| 3068 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 3072 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3073 | int i = mSamples.indexOf(sampleId); |
| 3074 | if (i >= 0) { |
| 3075 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3076 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3077 | if ((status != 0) || mSamples. isEmpty()) { |
| 3078 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3079 | mSoundEffectsLock.notify(); |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3085 | /** @see AudioManager#reloadAudioSettings() */ |
| 3086 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3087 | readAudioSettings(false /*userSwitch*/); |
| 3088 | } |
| 3089 | |
| 3090 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3091 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 3092 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 3093 | readUserRestrictions(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3094 | |
| 3095 | // restore volume settings |
| 3096 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3097 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 3098 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 3099 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3100 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 3101 | continue; |
| 3102 | } |
| 3103 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3104 | streamState.readSettings(); |
| 3105 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3106 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3107 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3108 | !isStreamMutedByRingerOrZenMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3109 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3110 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3114 | // apply new ringer mode before checking volume for alias streams so that streams |
| 3115 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3116 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3117 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3118 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3119 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3120 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3121 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3122 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3123 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 3124 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 3125 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3126 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3127 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3128 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3129 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3132 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3133 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3134 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 3135 | return; |
| 3136 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3137 | // for logging only |
| 3138 | final String eventSource = new StringBuilder("setSpeakerphoneOn(").append(on) |
| 3139 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3140 | .append(Binder.getCallingPid()).toString(); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3141 | |
| 3142 | if (on) { |
| 3143 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 3144 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3145 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, |
| 3146 | eventSource, 0); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3147 | } |
| 3148 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 3149 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 3150 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 3151 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3152 | |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3153 | mForcedUseForCommExt = mForcedUseForComm; |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3154 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3155 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 3159 | public boolean isSpeakerphoneOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3160 | return (mForcedUseForCommExt == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3163 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3164 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3165 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 3166 | return; |
| 3167 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3168 | |
| 3169 | // Only enable calls from system components |
| 3170 | if (Binder.getCallingUid() >= FIRST_APPLICATION_UID) { |
| 3171 | mForcedUseForCommExt = on ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE; |
| 3172 | return; |
| 3173 | } |
| 3174 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3175 | // for logging only |
| 3176 | final String eventSource = new StringBuilder("setBluetoothScoOn(").append(on) |
| 3177 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3178 | .append(Binder.getCallingPid()).toString(); |
| 3179 | setBluetoothScoOnInt(on, eventSource); |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3180 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3181 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3182 | public void setBluetoothScoOnInt(boolean on, String eventSource) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3183 | if (DEBUG_DEVICES) { |
| 3184 | Log.d(TAG, "setBluetoothScoOnInt: " + on + " " + eventSource); |
| 3185 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3186 | if (on) { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3187 | // do not accept SCO ON if SCO audio is not connected |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3188 | synchronized (mScoClients) { |
| 3189 | if (mBluetoothHeadset != null) { |
| 3190 | if (mBluetoothHeadsetDevice == null) { |
| 3191 | BluetoothDevice activeDevice = mBluetoothHeadset.getActiveDevice(); |
| 3192 | if (activeDevice != null) { |
| 3193 | // setBtScoActiveDevice() might trigger resetBluetoothSco() which |
| 3194 | // will call setBluetoothScoOnInt(false, "resetBluetoothSco") |
| 3195 | setBtScoActiveDevice(activeDevice); |
| 3196 | } |
| 3197 | } |
| 3198 | if (mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3199 | != BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3200 | mForcedUseForCommExt = AudioSystem.FORCE_BT_SCO; |
| 3201 | Log.w(TAG, "setBluetoothScoOnInt(true) failed because " |
| 3202 | + mBluetoothHeadsetDevice + " is not in audio connected mode"); |
| 3203 | return; |
| 3204 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3205 | } |
| 3206 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3207 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 3208 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 3209 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 3210 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3211 | mForcedUseForCommExt = mForcedUseForComm; |
| 3212 | AudioSystem.setParameters("BT_SCO="+ (on ? "on" : "off")); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3213 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3214 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3215 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3216 | AudioSystem.FOR_RECORD, mForcedUseForComm, eventSource, 0); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 3217 | // Un-mute ringtone stream volume |
| 3218 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | /** @see AudioManager#isBluetoothScoOn() */ |
| 3222 | public boolean isBluetoothScoOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3223 | return (mForcedUseForCommExt == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3226 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3227 | public void setBluetoothA2dpOn(boolean on) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3228 | // for logging only |
| 3229 | final String eventSource = new StringBuilder("setBluetoothA2dpOn(").append(on) |
| 3230 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3231 | .append(Binder.getCallingPid()).toString(); |
| 3232 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3233 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3234 | mBluetoothA2dpEnabled = on; |
| 3235 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 3236 | AudioSystem.FOR_MEDIA, |
| 3237 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3238 | eventSource, 0); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3239 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3242 | /** @see AudioManager#isBluetoothA2dpOn() */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3243 | public boolean isBluetoothA2dpOn() { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3244 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3245 | return mBluetoothA2dpEnabled; |
| 3246 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3249 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3250 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 3251 | int scoAudioMode = |
| 3252 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3253 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3254 | startBluetoothScoInt(cb, scoAudioMode); |
| 3255 | } |
| 3256 | |
| 3257 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 3258 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 3259 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 3260 | } |
| 3261 | |
| 3262 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3263 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3264 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3265 | return; |
| 3266 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3267 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3268 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 3269 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3270 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3271 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 3272 | // client is created. |
| 3273 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3274 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3275 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | /** @see AudioManager#stopBluetoothSco() */ |
| 3279 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3280 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3281 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3282 | return; |
| 3283 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3284 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3285 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 3286 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3287 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3288 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3289 | if (client != null) { |
| 3290 | client.decCount(); |
| 3291 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3292 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3295 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3296 | private class ScoClient implements IBinder.DeathRecipient { |
| 3297 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3298 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3299 | private int mStartcount; // number of SCO connections started by this client |
| 3300 | |
| 3301 | ScoClient(IBinder cb) { |
| 3302 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3303 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3304 | mStartcount = 0; |
| 3305 | } |
| 3306 | |
| 3307 | public void binderDied() { |
| 3308 | synchronized(mScoClients) { |
| 3309 | Log.w(TAG, "SCO client died"); |
| 3310 | int index = mScoClients.indexOf(this); |
| 3311 | if (index < 0) { |
| 3312 | Log.w(TAG, "unregistered SCO client died"); |
| 3313 | } else { |
| 3314 | clearCount(true); |
| 3315 | mScoClients.remove(this); |
| 3316 | } |
| 3317 | } |
| 3318 | } |
| 3319 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3320 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3321 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3322 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3323 | if (mStartcount == 0) { |
| 3324 | try { |
| 3325 | mCb.linkToDeath(this, 0); |
| 3326 | } catch (RemoteException e) { |
| 3327 | // client has already died! |
| 3328 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 3329 | } |
| 3330 | } |
| 3331 | mStartcount++; |
| 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | public void decCount() { |
| 3336 | synchronized(mScoClients) { |
| 3337 | if (mStartcount == 0) { |
| 3338 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 3339 | } else { |
| 3340 | mStartcount--; |
| 3341 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3342 | try { |
| 3343 | mCb.unlinkToDeath(this, 0); |
| 3344 | } catch (NoSuchElementException e) { |
| 3345 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 3346 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3347 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3348 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | public void clearCount(boolean stopSco) { |
| 3354 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3355 | if (mStartcount != 0) { |
| 3356 | try { |
| 3357 | mCb.unlinkToDeath(this, 0); |
| 3358 | } catch (NoSuchElementException e) { |
| 3359 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 3360 | } |
| 3361 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3362 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3363 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3364 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | public int getCount() { |
| 3370 | return mStartcount; |
| 3371 | } |
| 3372 | |
| 3373 | public IBinder getBinder() { |
| 3374 | return mCb; |
| 3375 | } |
| 3376 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3377 | public int getPid() { |
| 3378 | return mCreatorPid; |
| 3379 | } |
| 3380 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3381 | public int totalCount() { |
| 3382 | synchronized(mScoClients) { |
| 3383 | int count = 0; |
| 3384 | int size = mScoClients.size(); |
| 3385 | for (int i = 0; i < size; i++) { |
| 3386 | count += mScoClients.get(i).getCount(); |
| 3387 | } |
| 3388 | return count; |
| 3389 | } |
| 3390 | } |
| 3391 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3392 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3393 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3394 | if (totalCount() == 0) { |
| 3395 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3396 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 3397 | // the connection. |
| 3398 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 3399 | // 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] | 3400 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3401 | synchronized(mSetModeDeathHandlers) { |
| 3402 | if ((mSetModeDeathHandlers.isEmpty() || |
| 3403 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 3404 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 3405 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 3406 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3407 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3408 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 3409 | if (mBluetoothHeadsetDevice != null) { |
| 3410 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 3411 | mContentResolver, |
| 3412 | "bluetooth_sco_channel_"+ |
| 3413 | mBluetoothHeadsetDevice.getAddress(), |
| 3414 | SCO_MODE_VIRTUAL_CALL)); |
| 3415 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 3416 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 3417 | } |
| 3418 | } else { |
| 3419 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3420 | } |
| 3421 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3422 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3423 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3424 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3425 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3426 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3427 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3428 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3429 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3430 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3431 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3432 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3433 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3434 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3435 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3436 | } else { |
| 3437 | broadcastScoConnectionState( |
| 3438 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3439 | } |
| 3440 | } else if (getBluetoothHeadset()) { |
| 3441 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3442 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3443 | } else { |
| 3444 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3445 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3446 | } |
| 3447 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 3448 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3449 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3450 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3451 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3452 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 3453 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 3454 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 3455 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3456 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3457 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3458 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3459 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3460 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3461 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3462 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3463 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3464 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3465 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3466 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3467 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3468 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3469 | broadcastScoConnectionState( |
| 3470 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3471 | } |
| 3472 | } else if (getBluetoothHeadset()) { |
| 3473 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 3474 | } |
| 3475 | } else { |
| 3476 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3477 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3478 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3479 | } |
| 3480 | } |
| 3481 | } |
| 3482 | } |
| 3483 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3484 | private void checkScoAudioState() { |
| 3485 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3486 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3487 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3488 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 3489 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 3490 | } |
| 3491 | } |
| 3492 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3493 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3494 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3495 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3496 | int size = mScoClients.size(); |
| 3497 | for (int i = 0; i < size; i++) { |
| 3498 | client = mScoClients.get(i); |
| 3499 | if (client.getBinder() == cb) |
| 3500 | return client; |
| 3501 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3502 | if (create) { |
| 3503 | client = new ScoClient(cb); |
| 3504 | mScoClients.add(client); |
| 3505 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3506 | return client; |
| 3507 | } |
| 3508 | } |
| 3509 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3510 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3511 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3512 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3513 | int size = mScoClients.size(); |
| 3514 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3515 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3516 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3517 | cl.clearCount(stopSco); |
| 3518 | } else { |
| 3519 | savedClient = cl; |
| 3520 | } |
| 3521 | } |
| 3522 | mScoClients.clear(); |
| 3523 | if (savedClient != null) { |
| 3524 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3525 | } |
| 3526 | } |
| 3527 | } |
| 3528 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3529 | private boolean getBluetoothHeadset() { |
| 3530 | boolean result = false; |
| 3531 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 3532 | if (adapter != null) { |
| 3533 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 3534 | BluetoothProfile.HEADSET); |
| 3535 | } |
| 3536 | // If we could not get a bluetooth headset proxy, send a failure message |
| 3537 | // without delay to reset the SCO audio state and clear SCO clients. |
| 3538 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 3539 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3540 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3541 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 3542 | return result; |
| 3543 | } |
| 3544 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3545 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3546 | synchronized(mScoClients) { |
| 3547 | checkScoAudioState(); |
| 3548 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 3549 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3550 | if (mBluetoothHeadsetDevice != null) { |
| 3551 | if (mBluetoothHeadset != null) { |
| 3552 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 3553 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3554 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3555 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 3556 | } |
| 3557 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 3558 | getBluetoothHeadset()) { |
| 3559 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 3560 | } |
| 3561 | } |
| 3562 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3563 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3564 | } |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | private void resetBluetoothSco() { |
| 3569 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3570 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3571 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3572 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3573 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3574 | AudioSystem.setParameters("A2dpSuspended=false"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3575 | setBluetoothScoOnInt(false, "resetBluetoothSco"); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3579 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 3580 | SENDMSG_QUEUE, state, 0, null, 0); |
| 3581 | } |
| 3582 | |
| 3583 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3584 | if (state != mScoConnectionState) { |
| 3585 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 3586 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 3587 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 3588 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3589 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3590 | mScoConnectionState = state; |
| 3591 | } |
| 3592 | } |
| 3593 | |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3594 | private boolean handleBtScoActiveDeviceChange(BluetoothDevice btDevice, boolean isActive) { |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3595 | if (btDevice == null) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3596 | return true; |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3597 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3598 | String address = btDevice.getAddress(); |
| 3599 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 3600 | int outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 3601 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 3602 | if (btClass != null) { |
| 3603 | switch (btClass.getDeviceClass()) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3604 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 3605 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 3606 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 3607 | break; |
| 3608 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 3609 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 3610 | break; |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 3615 | address = ""; |
| 3616 | } |
| 3617 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3618 | String btDeviceName = btDevice.getName(); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3619 | boolean result = handleDeviceConnection(isActive, outDevice, address, btDeviceName); |
| 3620 | // handleDeviceConnection() && result to make sure the method get executed |
| 3621 | result = handleDeviceConnection(isActive, inDevice, address, btDeviceName) && result; |
| 3622 | return result; |
| 3623 | } |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3624 | |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3625 | void setBtScoActiveDevice(BluetoothDevice btDevice) { |
| 3626 | if (DEBUG_DEVICES) { |
| 3627 | Log.d(TAG, "setBtScoActiveDevice(" + btDevice + ")"); |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3628 | } |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3629 | synchronized (mScoClients) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3630 | final BluetoothDevice previousActiveDevice = mBluetoothHeadsetDevice; |
| 3631 | if (!Objects.equals(btDevice, previousActiveDevice)) { |
| 3632 | if (!handleBtScoActiveDeviceChange(previousActiveDevice, false)) { |
| 3633 | Log.w(TAG, "setBtScoActiveDevice() failed to remove previous device " |
| 3634 | + previousActiveDevice); |
| 3635 | } |
| 3636 | if (!handleBtScoActiveDeviceChange(btDevice, true)) { |
| 3637 | Log.e(TAG, "setBtScoActiveDevice() failed to add new device " + btDevice); |
| 3638 | // set mBluetoothHeadsetDevice to null when failing to add new device |
| 3639 | btDevice = null; |
| 3640 | } |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3641 | mBluetoothHeadsetDevice = btDevice; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3642 | if (mBluetoothHeadsetDevice == null) { |
| 3643 | resetBluetoothSco(); |
| 3644 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3645 | } |
| 3646 | } |
| 3647 | } |
| 3648 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3649 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 3650 | new BluetoothProfile.ServiceListener() { |
| 3651 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3652 | BluetoothDevice btDevice; |
| 3653 | List<BluetoothDevice> deviceList; |
| 3654 | switch(profile) { |
| 3655 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3656 | synchronized (mConnectedDevices) { |
| 3657 | synchronized (mA2dpAvrcpLock) { |
| 3658 | mA2dp = (BluetoothA2dp) proxy; |
| 3659 | deviceList = mA2dp.getConnectedDevices(); |
| 3660 | if (deviceList.size() > 0) { |
| 3661 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3662 | int state = mA2dp.getConnectionState(btDevice); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3663 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3664 | int delay = checkSendBecomingNoisyIntent( |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3665 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, intState, |
| 3666 | AudioSystem.DEVICE_NONE); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3667 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3668 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3669 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3670 | 0 /* arg2 unused */, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3671 | btDevice, |
| 3672 | delay); |
| 3673 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3674 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3675 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3676 | break; |
| 3677 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3678 | case BluetoothProfile.A2DP_SINK: |
| 3679 | deviceList = proxy.getConnectedDevices(); |
| 3680 | if (deviceList.size() > 0) { |
| 3681 | btDevice = deviceList.get(0); |
| 3682 | synchronized (mConnectedDevices) { |
| 3683 | int state = proxy.getConnectionState(btDevice); |
| 3684 | queueMsgUnderWakeLock(mAudioHandler, |
| 3685 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 3686 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3687 | 0 /* arg2 unused */, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3688 | btDevice, |
| 3689 | 0 /* delay */); |
| 3690 | } |
| 3691 | } |
| 3692 | break; |
| 3693 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3694 | case BluetoothProfile.HEADSET: |
| 3695 | synchronized (mScoClients) { |
| 3696 | // Discard timeout message |
| 3697 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 3698 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3699 | setBtScoActiveDevice(mBluetoothHeadset.getActiveDevice()); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3700 | // Refresh SCO audio state |
| 3701 | checkScoAudioState(); |
| 3702 | // Continue pending action if any |
| 3703 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 3704 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 3705 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3706 | boolean status = false; |
| 3707 | if (mBluetoothHeadsetDevice != null) { |
| 3708 | switch (mScoAudioState) { |
| 3709 | case SCO_STATE_ACTIVATE_REQ: |
| 3710 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3711 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3712 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3713 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3714 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3715 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3716 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3717 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3718 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3719 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3720 | break; |
| 3721 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3722 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3723 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3724 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3725 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3726 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3727 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3728 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3729 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3730 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3731 | break; |
| 3732 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 3733 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3734 | mBluetoothHeadsetDevice); |
| 3735 | } |
| 3736 | } |
| 3737 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3738 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3739 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3740 | } |
| 3741 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3742 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3743 | break; |
| 3744 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3745 | case BluetoothProfile.HEARING_AID: |
| 3746 | synchronized (mConnectedDevices) { |
| 3747 | synchronized (mHearingAidLock) { |
| 3748 | mHearingAid = (BluetoothHearingAid) proxy; |
| 3749 | deviceList = mHearingAid.getConnectedDevices(); |
| 3750 | if (deviceList.size() > 0) { |
| 3751 | btDevice = deviceList.get(0); |
| 3752 | int state = mHearingAid.getConnectionState(btDevice); |
| 3753 | int intState = (state == BluetoothHearingAid.STATE_CONNECTED) ? 1 : 0; |
| 3754 | int delay = checkSendBecomingNoisyIntent( |
| 3755 | AudioSystem.DEVICE_OUT_HEARING_AID, intState, |
| 3756 | AudioSystem.DEVICE_NONE); |
| 3757 | queueMsgUnderWakeLock(mAudioHandler, |
| 3758 | MSG_SET_HEARING_AID_CONNECTION_STATE, |
| 3759 | state, |
| 3760 | 0 /* arg2 unused */, |
| 3761 | btDevice, |
| 3762 | delay); |
| 3763 | } |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | break; |
| 3768 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3769 | default: |
| 3770 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3771 | } |
| 3772 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3773 | public void onServiceDisconnected(int profile) { |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3774 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3775 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3776 | case BluetoothProfile.A2DP: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3777 | disconnectA2dp(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3778 | break; |
| 3779 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3780 | case BluetoothProfile.A2DP_SINK: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3781 | disconnectA2dpSink(); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3782 | break; |
| 3783 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3784 | case BluetoothProfile.HEADSET: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3785 | disconnectHeadset(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3786 | break; |
| 3787 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3788 | case BluetoothProfile.HEARING_AID: |
| 3789 | disconnectHearingAid(); |
| 3790 | break; |
| 3791 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3792 | default: |
| 3793 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3794 | } |
| 3795 | } |
| 3796 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3797 | |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3798 | void disconnectAllBluetoothProfiles() { |
| 3799 | disconnectA2dp(); |
| 3800 | disconnectA2dpSink(); |
| 3801 | disconnectHeadset(); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3802 | disconnectHearingAid(); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | void disconnectA2dp() { |
| 3806 | synchronized (mConnectedDevices) { |
| 3807 | synchronized (mA2dpAvrcpLock) { |
| 3808 | ArraySet<String> toRemove = null; |
| 3809 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
| 3810 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3811 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3812 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
| 3813 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3814 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3815 | } |
| 3816 | } |
| 3817 | if (toRemove != null) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3818 | int delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3819 | 0, AudioSystem.DEVICE_NONE); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3820 | for (int i = 0; i < toRemove.size(); i++) { |
| 3821 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | void disconnectA2dpSink() { |
| 3829 | synchronized (mConnectedDevices) { |
| 3830 | ArraySet<String> toRemove = null; |
| 3831 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
| 3832 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3833 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3834 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
| 3835 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3836 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3837 | } |
| 3838 | } |
| 3839 | if (toRemove != null) { |
| 3840 | for (int i = 0; i < toRemove.size(); i++) { |
| 3841 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
| 3842 | } |
| 3843 | } |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | void disconnectHeadset() { |
| 3848 | synchronized (mScoClients) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3849 | setBtScoActiveDevice(null); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3850 | mBluetoothHeadset = null; |
| 3851 | } |
| 3852 | } |
| 3853 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3854 | void disconnectHearingAid() { |
| 3855 | synchronized (mConnectedDevices) { |
| 3856 | synchronized (mHearingAidLock) { |
| 3857 | ArraySet<String> toRemove = null; |
| 3858 | // Disconnect ALL DEVICE_OUT_HEARING_AID devices |
| 3859 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3860 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3861 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_HEARING_AID) { |
| 3862 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3863 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3864 | } |
| 3865 | } |
| 3866 | if (toRemove != null) { |
| 3867 | int delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 3868 | 0, AudioSystem.DEVICE_NONE); |
| 3869 | for (int i = 0; i < toRemove.size(); i++) { |
| 3870 | makeHearingAidDeviceUnavailable(toRemove.valueAt(i) /*, delay*/); |
| 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3877 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3878 | synchronized (mSafeMediaVolumeState) { |
| 3879 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3880 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3881 | |
| 3882 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3883 | sendMsg(mAudioHandler, |
| 3884 | MSG_CHECK_MUSIC_ACTIVE, |
| 3885 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3886 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3887 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3888 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3889 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3890 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3891 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3892 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3893 | // Approximate cumulative active music time |
| 3894 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3895 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3896 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3897 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3898 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3899 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | } |
| 3905 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3906 | private void saveMusicActiveMs() { |
| 3907 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3908 | } |
| 3909 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3910 | private int getSafeUsbMediaVolumeIndex() |
| 3911 | { |
| 3912 | // determine UI volume index corresponding to the wanted safe gain in dBFS |
| 3913 | int min = MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3914 | int max = MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3915 | |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3916 | mSafeUsbMediaVolumeDbfs = mContext.getResources().getInteger( |
| 3917 | com.android.internal.R.integer.config_safe_media_volume_usb_mB) / 100.0f; |
| 3918 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3919 | while (Math.abs(max-min) > 1) { |
| 3920 | int index = (max + min) / 2; |
| 3921 | float gainDB = AudioSystem.getStreamVolumeDB( |
| 3922 | AudioSystem.STREAM_MUSIC, index, AudioSystem.DEVICE_OUT_USB_HEADSET); |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 3923 | if (Float.isNaN(gainDB)) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3924 | //keep last min in case of read error |
| 3925 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3926 | } else if (gainDB == mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3927 | min = index; |
| 3928 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3929 | } else if (gainDB < mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3930 | min = index; |
| 3931 | } else { |
| 3932 | max = index; |
| 3933 | } |
| 3934 | } |
| 3935 | return min * 10; |
| 3936 | } |
| 3937 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3938 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3939 | synchronized (mSafeMediaVolumeState) { |
| 3940 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3941 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3942 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3943 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3944 | |
| 3945 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 3946 | |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3947 | boolean safeMediaVolumeEnabled = |
| 3948 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3949 | || mContext.getResources().getBoolean( |
| 3950 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3951 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3952 | boolean safeMediaVolumeBypass = |
| 3953 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3954 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3955 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3956 | // next time we boot and cannot be "inactive" |
| 3957 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3958 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3959 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3960 | // The state can already be "inactive" here if the user has forced it before |
| 3961 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3962 | // it to "active". |
| 3963 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3964 | if (mMusicActiveMs == 0) { |
| 3965 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3966 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3967 | } else { |
| 3968 | // We have existing playback time recorded, already confirmed. |
| 3969 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3970 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3971 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3972 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3973 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3974 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3975 | } |
| 3976 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3977 | sendMsg(mAudioHandler, |
| 3978 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3979 | SENDMSG_QUEUE, |
| 3980 | persistedState, |
| 3981 | 0, |
| 3982 | null, |
| 3983 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | } |
| 3987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3988 | /////////////////////////////////////////////////////////////////////////// |
| 3989 | // Internal methods |
| 3990 | /////////////////////////////////////////////////////////////////////////// |
| 3991 | |
| 3992 | /** |
| 3993 | * Checks if the adjustment should change ringer mode instead of just |
| 3994 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3995 | * indices on the stream states. |
| 3996 | */ |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3997 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted, |
| 3998 | String caller, int flags) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3999 | int result = FLAG_ADJUST_VOLUME; |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 4000 | if (isPlatformTelevision() || mIsSingleVolume) { |
Hank Freund | 21003f6 | 2015-12-08 09:05:46 -0800 | [diff] [blame] | 4001 | return result; |
| 4002 | } |
| 4003 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4004 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4005 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4006 | switch (ringerMode) { |
| 4007 | case RINGER_MODE_NORMAL: |
| 4008 | if (direction == AudioManager.ADJUST_LOWER) { |
| 4009 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 4010 | // "step" is the delta in internal index units corresponding to a |
| 4011 | // change of 1 in UI index units. |
| 4012 | // Because of rounding when rescaling from one stream index range to its alias |
| 4013 | // index range, we cannot simply test oldIndex == step: |
| 4014 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 4015 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4016 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4017 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4018 | } |
| 4019 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 4020 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4021 | ringerMode = RINGER_MODE_SILENT; |
| 4022 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 4023 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4024 | } else if (mIsSingleVolume && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4025 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4026 | if (mHasVibrator) { |
| 4027 | ringerMode = RINGER_MODE_VIBRATE; |
| 4028 | } else { |
| 4029 | ringerMode = RINGER_MODE_SILENT; |
| 4030 | } |
| 4031 | // Setting the ringer mode will toggle mute |
| 4032 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4034 | break; |
| 4035 | case RINGER_MODE_VIBRATE: |
| 4036 | if (!mHasVibrator) { |
| 4037 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 4038 | "but no vibrator is present"); |
| 4039 | break; |
| 4040 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4041 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4042 | // This is the case we were muted with the volume turned up |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4043 | if (mIsSingleVolume && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4044 | ringerMode = RINGER_MODE_NORMAL; |
| 4045 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4046 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4047 | final long diff = SystemClock.uptimeMillis() |
| 4048 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 4049 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 4050 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4051 | ringerMode = RINGER_MODE_SILENT; |
| 4052 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 4053 | } else { |
| 4054 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 4055 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4056 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4057 | } else if (direction == AudioManager.ADJUST_RAISE |
| 4058 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 4059 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4060 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4061 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4062 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4063 | break; |
| 4064 | case RINGER_MODE_SILENT: |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4065 | if (mIsSingleVolume && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4066 | // This is the case we were muted with the volume turned up |
| 4067 | ringerMode = RINGER_MODE_NORMAL; |
| 4068 | } else if (direction == AudioManager.ADJUST_RAISE |
| 4069 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 4070 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4071 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4072 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4073 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4074 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4075 | ringerMode = RINGER_MODE_VIBRATE; |
| 4076 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4077 | // If we don't have a vibrator or they were toggling mute |
| 4078 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4079 | ringerMode = RINGER_MODE_NORMAL; |
| 4080 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4081 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 4082 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4083 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4084 | break; |
| 4085 | default: |
| 4086 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 4087 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4088 | } |
| 4089 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 4090 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 4091 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller) |
| 4092 | && (flags & AudioManager.FLAG_FROM_KEY) == 0) { |
| 4093 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 4094 | } |
| 4095 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4096 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4097 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4098 | mPrevVolDirection = direction; |
| 4099 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4100 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4103 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4104 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 4105 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4106 | } |
| 4107 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4108 | private boolean shouldZenMuteStream(int streamType) { |
| 4109 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 4110 | return false; |
| 4111 | } |
| 4112 | |
| 4113 | NotificationManager.Policy zenPolicy = mNm.getNotificationPolicy(); |
| 4114 | final boolean muteAlarms = (zenPolicy.priorityCategories |
| 4115 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0; |
| 4116 | final boolean muteMedia = (zenPolicy.priorityCategories |
| 4117 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0; |
| 4118 | final boolean muteSystem = (zenPolicy.priorityCategories |
| 4119 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0; |
| 4120 | final boolean muteNotificationAndRing = ZenModeConfig |
| 4121 | .areAllPriorityOnlyNotificationZenSoundsMuted(mNm.getNotificationPolicy()); |
| 4122 | return muteAlarms && isAlarm(streamType) |
| 4123 | || muteMedia && isMedia(streamType) |
| 4124 | || muteSystem && isSystem(streamType) |
| 4125 | || muteNotificationAndRing && isNotificationOrRinger(streamType); |
| 4126 | } |
| 4127 | |
| 4128 | private boolean isStreamMutedByRingerOrZenMode(int streamType) { |
| 4129 | return (mRingerAndZenModeMutedStreams & (1 << streamType)) != 0; |
| 4130 | } |
| 4131 | |
| 4132 | /** |
| 4133 | * DND total silence: media and alarms streams are tied to the muted ringer |
| 4134 | * {@link ZenModeHelper.RingerModeDelegate#getRingerModeAffectedStreams(int)} |
| 4135 | * DND alarms only: notification, ringer + system muted (by default tied to muted ringer mode) |
| 4136 | * DND priority only: alarms, media, system streams can be muted separate from ringer based on |
| 4137 | * zenPolicy (this method determines which streams) |
| 4138 | * @return true if changed, else false |
| 4139 | */ |
| 4140 | private boolean updateZenModeAffectedStreams() { |
| 4141 | int zenModeAffectedStreams = 0; |
| 4142 | if (mSystemReady && mNm.getZenMode() == Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 4143 | NotificationManager.Policy zenPolicy = mNm.getNotificationPolicy(); |
| 4144 | if ((zenPolicy.priorityCategories |
| 4145 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { |
| 4146 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_ALARM; |
| 4147 | } |
| 4148 | |
| 4149 | if ((zenPolicy.priorityCategories |
| 4150 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0) { |
| 4151 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_MUSIC; |
| 4152 | } |
| 4153 | |
| 4154 | if ((zenPolicy.priorityCategories |
| 4155 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0) { |
| 4156 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_SYSTEM; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (mZenModeAffectedStreams != zenModeAffectedStreams) { |
| 4161 | mZenModeAffectedStreams = zenModeAffectedStreams; |
| 4162 | return true; |
| 4163 | } |
| 4164 | |
| 4165 | return false; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4168 | @GuardedBy("mSettingsLock") |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4169 | private boolean updateRingerAndZenModeAffectedStreams() { |
| 4170 | boolean updatedZenModeAffectedStreams = updateZenModeAffectedStreams(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 4171 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4172 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 4173 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 4174 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 4175 | UserHandle.USER_CURRENT); |
| 4176 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4177 | if (mIsSingleVolume) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 4178 | ringerModeAffectedStreams = 0; |
| 4179 | } else if (mRingerModeDelegate != null) { |
| 4180 | ringerModeAffectedStreams = mRingerModeDelegate |
| 4181 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4182 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4183 | if (mCameraSoundForced) { |
| 4184 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4185 | } else { |
| 4186 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4187 | } |
| 4188 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 4189 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 4190 | } else { |
| 4191 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 4192 | } |
| 4193 | |
| 4194 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 4195 | Settings.System.putIntForUser(mContentResolver, |
| 4196 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 4197 | ringerModeAffectedStreams, |
| 4198 | UserHandle.USER_CURRENT); |
| 4199 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 4200 | return true; |
| 4201 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4202 | return updatedZenModeAffectedStreams; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 4205 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4206 | public boolean isStreamAffectedByMute(int streamType) { |
| 4207 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 4208 | } |
| 4209 | |
| 4210 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4211 | switch (direction) { |
| 4212 | case AudioManager.ADJUST_LOWER: |
| 4213 | case AudioManager.ADJUST_RAISE: |
| 4214 | case AudioManager.ADJUST_SAME: |
| 4215 | case AudioManager.ADJUST_MUTE: |
| 4216 | case AudioManager.ADJUST_UNMUTE: |
| 4217 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 4218 | break; |
| 4219 | default: |
| 4220 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | private void ensureValidStreamType(int streamType) { |
| 4225 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 4226 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 4227 | } |
| 4228 | } |
| 4229 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4230 | private boolean isMuteAdjust(int adjust) { |
| 4231 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 4232 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 4233 | } |
| 4234 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4235 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 4236 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 4238 | TelecomManager telecomManager = |
| 4239 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 4240 | |
| 4241 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 4242 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 4243 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 4244 | |
Eric Laurent | da1af76 | 2017-12-15 16:54:35 -0800 | [diff] [blame] | 4245 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION || |
| 4246 | getMode() == AudioManager.MODE_IN_CALL); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4247 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4248 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 4249 | /** |
| 4250 | * For code clarity for getActiveStreamType(int) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4251 | * @param delay_ms max time since last stream activity to consider |
| 4252 | * @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] | 4253 | * in the last "delay_ms" ms. |
| 4254 | */ |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4255 | private boolean wasStreamActiveRecently(int stream, int delay_ms) { |
| 4256 | return AudioSystem.isStreamActive(stream, delay_ms) |
| 4257 | || AudioSystem.isStreamActiveRemotely(stream, delay_ms); |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4260 | private int getActiveStreamType(int suggestedStreamType) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4261 | if (mIsSingleVolume |
| 4262 | && suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 4263 | return AudioSystem.STREAM_MUSIC; |
| 4264 | } |
| 4265 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4266 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4267 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4268 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4269 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 4270 | == AudioSystem.FORCE_BT_SCO) { |
| 4271 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 4272 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 4273 | } else { |
| 4274 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 4275 | return AudioSystem.STREAM_VOICE_CALL; |
| 4276 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4277 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4278 | if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4279 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4280 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 4281 | return AudioSystem.STREAM_RING; |
| 4282 | } else if (wasStreamActiveRecently( |
| 4283 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 4284 | if (DEBUG_VOL) |
| 4285 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 4286 | return AudioSystem.STREAM_NOTIFICATION; |
| 4287 | } else { |
| 4288 | if (DEBUG_VOL) |
| 4289 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC b/c default"); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4290 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4291 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4292 | } else if ( |
| 4293 | wasStreamActiveRecently(AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4294 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4295 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 4296 | return AudioSystem.STREAM_NOTIFICATION; |
| 4297 | } else if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4298 | if (DEBUG_VOL) |
| 4299 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 4300 | return AudioSystem.STREAM_RING; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4301 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4302 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4303 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4304 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 4305 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4306 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4307 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 4308 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4309 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4310 | return AudioSystem.STREAM_VOICE_CALL; |
| 4311 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4312 | } else if (AudioSystem.isStreamActive( |
| 4313 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4314 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4315 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4316 | } else if (AudioSystem.isStreamActive( |
| 4317 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4318 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 4319 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4320 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4321 | if (AudioSystem.isStreamActive( |
| 4322 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 4323 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 4324 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4325 | } else if (AudioSystem.isStreamActive( |
| 4326 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4327 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 4328 | return AudioSystem.STREAM_RING; |
| 4329 | } else { |
| 4330 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: using STREAM_MUSIC as default"); |
| 4331 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4332 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 4333 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4334 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4335 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4336 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 4337 | + suggestedStreamType); |
| 4338 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4339 | } |
| 4340 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4341 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4342 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4343 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4344 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 4345 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 4346 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4347 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | private void broadcastVibrateSetting(int vibrateType) { |
| 4351 | // Send broadcast |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 4352 | if (mActivityManagerInternal.isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4353 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 4354 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 4355 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4356 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4361 | /** |
| 4362 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 4363 | * Note that the wake lock needs to be released after the message has been handled. |
| 4364 | */ |
| 4365 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 4366 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4367 | final long ident = Binder.clearCallingIdentity(); |
| 4368 | // Always acquire the wake lock as AudioService because it is released by the |
| 4369 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4370 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4371 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4372 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 4373 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4374 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4375 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4376 | 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] | 4377 | |
| 4378 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 4379 | handler.removeMessages(msg); |
| 4380 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 4381 | return; |
| 4382 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4383 | synchronized (mLastDeviceConnectMsgTime) { |
| 4384 | long time = SystemClock.uptimeMillis() + delay; |
| 4385 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 4386 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 4387 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 4388 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE || |
| 4389 | msg == MSG_SET_HEARING_AID_CONNECTION_STATE) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4390 | mLastDeviceConnectMsgTime = time; |
| 4391 | } |
| 4392 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 4396 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4397 | == PackageManager.PERMISSION_GRANTED) { |
| 4398 | return true; |
| 4399 | } |
| 4400 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 4401 | + Binder.getCallingPid() |
| 4402 | + ", uid=" + Binder.getCallingUid(); |
| 4403 | Log.w(TAG, msg); |
| 4404 | return false; |
| 4405 | } |
| 4406 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4407 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4408 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4409 | if ((device & (device - 1)) != 0) { |
| 4410 | // Multiple device selection is either: |
| 4411 | // - speaker + one other device: give priority to speaker in this case. |
| 4412 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 4413 | // retain the device on the A2DP output as the other must not correspond to an active |
| 4414 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4415 | // - 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] | 4416 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 4417 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4418 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 4419 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 4420 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 4421 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 4422 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 4423 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4424 | } else { |
| 4425 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 4426 | } |
| 4427 | } |
| 4428 | return device; |
| 4429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4430 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4431 | private int getDevicesForStream(int stream) { |
| 4432 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 4433 | } |
| 4434 | |
| 4435 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 4436 | ensureValidStreamType(stream); |
| 4437 | synchronized (VolumeStreamState.class) { |
| 4438 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 4439 | } |
| 4440 | } |
| 4441 | |
| 4442 | private void observeDevicesForStreams(int skipStream) { |
| 4443 | synchronized (VolumeStreamState.class) { |
| 4444 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 4445 | if (stream != skipStream) { |
| 4446 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 4447 | } |
| 4448 | } |
| 4449 | } |
| 4450 | } |
| 4451 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4452 | /* |
| 4453 | * A class just for packaging up a set of connection parameters. |
| 4454 | */ |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 4455 | class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4456 | public final int mType; |
| 4457 | public final int mState; |
| 4458 | public final String mAddress; |
| 4459 | public final String mName; |
| 4460 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4461 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4462 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 4463 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4464 | mType = type; |
| 4465 | mState = state; |
| 4466 | mAddress = address; |
| 4467 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4468 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4472 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 4473 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4474 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4475 | if (DEBUG_DEVICES) { |
| 4476 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 4477 | + address + ")"); |
| 4478 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4479 | int delay = checkSendBecomingNoisyIntent(type, state, AudioSystem.DEVICE_NONE); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4480 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4481 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4482 | 0 /* arg1 unused */, |
| 4483 | 0 /* arg2 unused */, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4484 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4485 | delay); |
| 4486 | } |
| 4487 | } |
| 4488 | |
Jakub Pawlowski | 10c9061 | 2018-02-21 13:28:46 -0800 | [diff] [blame] | 4489 | @Override |
| 4490 | public void setHearingAidDeviceConnectionState(BluetoothDevice device, int state) |
| 4491 | { |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 4492 | Log.i(TAG, "setBluetoothHearingAidDeviceConnectionState"); |
| 4493 | |
| 4494 | setBluetoothHearingAidDeviceConnectionState( |
| 4495 | device, state, false /* suppressNoisyIntent */, AudioSystem.DEVICE_NONE); |
| 4496 | } |
| 4497 | |
| 4498 | public int setBluetoothHearingAidDeviceConnectionState( |
| 4499 | BluetoothDevice device, int state, boolean suppressNoisyIntent, |
| 4500 | int musicDevice) |
| 4501 | { |
| 4502 | int delay; |
| 4503 | synchronized (mConnectedDevices) { |
| 4504 | if (!suppressNoisyIntent) { |
| 4505 | int intState = (state == BluetoothHearingAid.STATE_CONNECTED) ? 1 : 0; |
| 4506 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 4507 | intState, musicDevice); |
| 4508 | } else { |
| 4509 | delay = 0; |
| 4510 | } |
| 4511 | queueMsgUnderWakeLock(mAudioHandler, |
| 4512 | MSG_SET_HEARING_AID_CONNECTION_STATE, |
| 4513 | state, |
| 4514 | 0 /* arg2 unused */, |
| 4515 | device, |
| 4516 | delay); |
| 4517 | } |
| 4518 | return delay; |
Jakub Pawlowski | 10c9061 | 2018-02-21 13:28:46 -0800 | [diff] [blame] | 4519 | } |
| 4520 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4521 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4522 | { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4523 | return setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent( |
| 4524 | device, state, profile, false /* suppressNoisyIntent */); |
| 4525 | } |
| 4526 | |
| 4527 | public int setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(BluetoothDevice device, |
| 4528 | int state, int profile, boolean suppressNoisyIntent) |
| 4529 | { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 4530 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, device)) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | return setBluetoothA2dpDeviceConnectionStateInt( |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4534 | device, state, profile, suppressNoisyIntent, AudioSystem.DEVICE_NONE); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | public int setBluetoothA2dpDeviceConnectionStateInt( |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4538 | BluetoothDevice device, int state, int profile, boolean suppressNoisyIntent, |
| 4539 | int musicDevice) |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4540 | { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4541 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4542 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 4543 | throw new IllegalArgumentException("invalid profile " + profile); |
| 4544 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4545 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4546 | if (profile == BluetoothProfile.A2DP && !suppressNoisyIntent) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4547 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4548 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4549 | intState, musicDevice); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4550 | } else { |
| 4551 | delay = 0; |
| 4552 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4553 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4554 | (profile == BluetoothProfile.A2DP ? |
| 4555 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4556 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4557 | 0 /* arg2 unused */, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4558 | device, |
| 4559 | delay); |
| 4560 | } |
| 4561 | return delay; |
| 4562 | } |
| 4563 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4564 | public void handleBluetoothA2dpDeviceConfigChange(BluetoothDevice device) |
| 4565 | { |
| 4566 | synchronized (mConnectedDevices) { |
| 4567 | queueMsgUnderWakeLock(mAudioHandler, |
| 4568 | MSG_A2DP_DEVICE_CONFIG_CHANGE, |
| 4569 | 0 /* arg1 unused */, |
| 4570 | 0 /* arg1 unused */, |
| 4571 | device, |
| 4572 | 0 /* delay */); |
| 4573 | } |
| 4574 | } |
| 4575 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4576 | private static final int DEVICE_MEDIA_UNMUTED_ON_PLUG = |
| 4577 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 4578 | AudioSystem.DEVICE_OUT_LINE | |
| 4579 | AudioSystem.DEVICE_OUT_ALL_A2DP | |
| 4580 | AudioSystem.DEVICE_OUT_ALL_USB | |
| 4581 | AudioSystem.DEVICE_OUT_HDMI; |
| 4582 | |
| 4583 | private void onAccessoryPlugMediaUnmute(int newDevice) { |
| 4584 | if (DEBUG_VOL) { |
| 4585 | Log.i(TAG, String.format("onAccessoryPlugMediaUnmute newDevice=%d [%s]", |
| 4586 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4587 | } |
| 4588 | synchronized (mConnectedDevices) { |
Eric Laurent | 7fb83d9 | 2017-09-28 09:36:36 -0700 | [diff] [blame] | 4589 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 4590 | && (newDevice & DEVICE_MEDIA_UNMUTED_ON_PLUG) != 0 |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4591 | && mStreamStates[AudioSystem.STREAM_MUSIC].mIsMuted |
| 4592 | && mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(newDevice) != 0 |
| 4593 | && (newDevice & AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC)) != 0) |
| 4594 | { |
| 4595 | if (DEBUG_VOL) { |
| 4596 | Log.i(TAG, String.format(" onAccessoryPlugMediaUnmute unmuting device=%d [%s]", |
| 4597 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4598 | } |
| 4599 | mStreamStates[AudioSystem.STREAM_MUSIC].mute(false); |
| 4600 | } |
| 4601 | } |
| 4602 | } |
| 4603 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4604 | /////////////////////////////////////////////////////////////////////////// |
| 4605 | // Inner classes |
| 4606 | /////////////////////////////////////////////////////////////////////////// |
| 4607 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4608 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 4609 | // 1 mScoclient OR mSafeMediaVolumeState |
| 4610 | // 2 mSetModeDeathHandlers |
| 4611 | // 3 mSettingsLock |
| 4612 | // 4 VolumeStreamState.class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4613 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4614 | private final int mStreamType; |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 4615 | private int mIndexMin; |
| 4616 | private int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4618 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4619 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4620 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4621 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4622 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4623 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4624 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4625 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4626 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4627 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4628 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | |
| 4630 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4631 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 4632 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 4633 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4634 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4635 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4636 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 4637 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4638 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 4639 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4640 | } |
| 4641 | |
| 4642 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 4643 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 4644 | if (devices == mObservedDevices) { |
| 4645 | return devices; |
| 4646 | } |
| 4647 | final int prevDevices = mObservedDevices; |
| 4648 | mObservedDevices = devices; |
| 4649 | if (checkOthers) { |
| 4650 | // one stream's devices have changed, check the others |
| 4651 | observeDevicesForStreams(mStreamType); |
| 4652 | } |
| 4653 | // log base stream changes to the event log |
| 4654 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4655 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 4656 | } |
| 4657 | sendBroadcastToAll(mStreamDevicesChanged |
| 4658 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 4659 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 4660 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4661 | } |
| 4662 | |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4663 | public @Nullable String getSettingNameForDevice(int device) { |
| 4664 | if (!hasValidSettingsName()) { |
| 4665 | return null; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4666 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4667 | final String suffix = AudioSystem.getOutputDeviceName(device); |
| 4668 | if (suffix.isEmpty()) { |
| 4669 | return mVolumeIndexSettingName; |
| 4670 | } |
| 4671 | return mVolumeIndexSettingName + "_" + suffix; |
| 4672 | } |
| 4673 | |
| 4674 | private boolean hasValidSettingsName() { |
| 4675 | return (mVolumeIndexSettingName != null && !mVolumeIndexSettingName.isEmpty()); |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4676 | } |
| 4677 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4678 | public void readSettings() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4679 | synchronized (mSettingsLock) { |
| 4680 | synchronized (VolumeStreamState.class) { |
| 4681 | // force maximum volume on all streams if fixed volume property is set |
| 4682 | if (mUseFixedVolume) { |
| 4683 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 4684 | return; |
| 4685 | } |
| 4686 | // do not read system stream volume from settings: this stream is always aliased |
| 4687 | // to another stream type and its volume is never persisted. Values in settings can |
| 4688 | // only be stale values |
| 4689 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 4690 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
| 4691 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4692 | if (mCameraSoundForced) { |
| 4693 | index = mIndexMax; |
| 4694 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4695 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 4696 | return; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4697 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4698 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4699 | } |
| 4700 | synchronized (VolumeStreamState.class) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4701 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 4702 | |
| 4703 | for (int i = 0; remainingDevices != 0; i++) { |
| 4704 | int device = (1 << i); |
| 4705 | if ((device & remainingDevices) == 0) { |
| 4706 | continue; |
| 4707 | } |
| 4708 | remainingDevices &= ~device; |
| 4709 | |
| 4710 | // retrieve current volume for device |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4711 | // if no volume stored for current stream and device, use default volume if default |
| 4712 | // device, continue otherwise |
| 4713 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4714 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4715 | int index; |
| 4716 | if (!hasValidSettingsName()) { |
| 4717 | index = defaultIndex; |
| 4718 | } else { |
| 4719 | String name = getSettingNameForDevice(device); |
| 4720 | index = Settings.System.getIntForUser( |
| 4721 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 4722 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4723 | if (index == -1) { |
| 4724 | continue; |
| 4725 | } |
| 4726 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4727 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4728 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4729 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4732 | private int getAbsoluteVolumeIndex(int index) { |
| 4733 | /* Special handling for Bluetooth Absolute Volume scenario |
| 4734 | * If we send full audio gain, some accessories are too loud even at its lowest |
| 4735 | * volume. We are not able to enumerate all such accessories, so here is the |
| 4736 | * workaround from phone side. |
| 4737 | * Pre-scale volume at lowest volume steps 1 2 and 3. |
| 4738 | * For volume step 0, set audio gain to 0 as some accessories won't mute on their end. |
| 4739 | */ |
| 4740 | if (index == 0) { |
| 4741 | // 0% for volume 0 |
| 4742 | index = 0; |
| 4743 | } else if (index == 1) { |
| 4744 | // 50% for volume 1 |
| 4745 | index = (int)(mIndexMax * 0.5) /10; |
| 4746 | } else if (index == 2) { |
| 4747 | // 70% for volume 2 |
| 4748 | index = (int)(mIndexMax * 0.70) /10; |
| 4749 | } else if (index == 3) { |
| 4750 | // 85% for volume 3 |
| 4751 | index = (int)(mIndexMax * 0.85) /10; |
| 4752 | } else { |
| 4753 | // otherwise, full gain |
| 4754 | index = (mIndexMax + 5)/10; |
| 4755 | } |
| 4756 | return index; |
| 4757 | } |
| 4758 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4759 | // must be called while synchronized VolumeStreamState.class |
| 4760 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4761 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4762 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4763 | index = 0; |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4764 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4765 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4766 | } else if ((device & mFullVolumeDevices) != 0) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4767 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4768 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4769 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4770 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4771 | index = (getIndex(device) + 5)/10; |
| 4772 | } |
| 4773 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4774 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4775 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4776 | public void applyAllVolumes() { |
| 4777 | synchronized (VolumeStreamState.class) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4778 | // apply device specific volumes first |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4779 | int index; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4780 | for (int i = 0; i < mIndexMap.size(); i++) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4781 | final int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4782 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4783 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4784 | index = 0; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4785 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 4786 | mAvrcpAbsVolSupported) { |
| 4787 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
| 4788 | } else if ((device & mFullVolumeDevices) != 0) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4789 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4790 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4791 | index = (mIndexMax + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4792 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4793 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4794 | } |
| 4795 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4796 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4797 | } |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4798 | // apply default volume last: by convention , default device volume will be used |
| 4799 | // by audio policy manager if no explicit volume is present for a given device type |
| 4800 | if (mIsMuted) { |
| 4801 | index = 0; |
| 4802 | } else { |
| 4803 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 4804 | } |
| 4805 | AudioSystem.setStreamVolumeIndex( |
| 4806 | mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4810 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 4811 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4812 | } |
| 4813 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4814 | public boolean setIndex(int index, int device, String caller) { |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4815 | boolean changed; |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4816 | int oldIndex; |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4817 | synchronized (mSettingsLock) { |
| 4818 | synchronized (VolumeStreamState.class) { |
| 4819 | oldIndex = getIndex(device); |
| 4820 | index = getValidIndex(index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4821 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 4822 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4823 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4824 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4825 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4826 | changed = oldIndex != index; |
| 4827 | // Apply change to all streams using this one as alias if: |
| 4828 | // - the index actually changed OR |
| 4829 | // - there is no volume index stored for this device on alias stream. |
| 4830 | // If changing volume of current device, also change volume of current |
| 4831 | // device on aliased stream |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4832 | final boolean isCurrentDevice = (device == getDeviceForStream(mStreamType)); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4833 | final int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4834 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4835 | final VolumeStreamState aliasStreamState = mStreamStates[streamType]; |
| 4836 | if (streamType != mStreamType && |
| 4837 | mStreamVolumeAlias[streamType] == mStreamType && |
| 4838 | (changed || !aliasStreamState.hasIndexForDevice(device))) { |
| 4839 | final int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 4840 | aliasStreamState.setIndex(scaledIndex, device, caller); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4841 | if (isCurrentDevice) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4842 | aliasStreamState.setIndex(scaledIndex, |
| 4843 | getDeviceForStream(streamType), caller); |
| 4844 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4845 | } |
| 4846 | } |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4847 | // Mirror changes in SPEAKER ringtone volume on SCO when |
| 4848 | if (changed && mStreamType == AudioSystem.STREAM_RING |
| 4849 | && device == AudioSystem.DEVICE_OUT_SPEAKER) { |
| 4850 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4851 | int otherDevice = mIndexMap.keyAt(i); |
| 4852 | if ((otherDevice & AudioSystem.DEVICE_OUT_ALL_SCO) != 0) { |
| 4853 | mIndexMap.put(otherDevice, index); |
| 4854 | } |
| 4855 | } |
| 4856 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4858 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4859 | if (changed) { |
| 4860 | oldIndex = (oldIndex + 5) / 10; |
| 4861 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4862 | // log base stream changes to the event log |
| 4863 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4864 | if (caller == null) { |
| 4865 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 4866 | } |
| 4867 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 4868 | caller); |
| 4869 | } |
| 4870 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4871 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 4872 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 4873 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 4874 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4875 | sendBroadcastToAll(mVolumeChanged); |
| 4876 | } |
| 4877 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4880 | public int getIndex(int device) { |
| 4881 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4882 | int index = mIndexMap.get(device, -1); |
| 4883 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4884 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4885 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4886 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4887 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4888 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4889 | } |
| 4890 | |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 4891 | public boolean hasIndexForDevice(int device) { |
| 4892 | synchronized (VolumeStreamState.class) { |
| 4893 | return (mIndexMap.get(device, -1) != -1); |
| 4894 | } |
| 4895 | } |
| 4896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4897 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4898 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4901 | public int getMinIndex() { |
| 4902 | return mIndexMin; |
| 4903 | } |
| 4904 | |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4905 | /** |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 4906 | * Updates the min/max index values from another stream. Use this when changing the alias |
| 4907 | * for the current stream type. |
| 4908 | * @param sourceStreamType |
| 4909 | */ |
| 4910 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4911 | @GuardedBy("VolumeStreamState.class") |
| 4912 | public void refreshRange(int sourceStreamType) { |
| 4913 | mIndexMin = MIN_STREAM_VOLUME[sourceStreamType] * 10; |
| 4914 | mIndexMax = MAX_STREAM_VOLUME[sourceStreamType] * 10; |
| 4915 | // verify all current volumes are within bounds |
| 4916 | for (int i = 0 ; i < mIndexMap.size(); i++) { |
| 4917 | final int device = mIndexMap.keyAt(i); |
| 4918 | final int index = mIndexMap.valueAt(i); |
| 4919 | mIndexMap.put(device, getValidIndex(index)); |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | /** |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4924 | * Copies all device/index pairs from the given VolumeStreamState after initializing |
| 4925 | * them with the volume for DEVICE_OUT_DEFAULT. No-op if the source VolumeStreamState |
| 4926 | * has the same stream type as this instance. |
| 4927 | * @param srcStream |
| 4928 | * @param caller |
| 4929 | */ |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4930 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4931 | @GuardedBy("VolumeStreamState.class") |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4932 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4933 | if (mStreamType == srcStream.mStreamType) { |
| 4934 | return; |
| 4935 | } |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4936 | int srcStreamType = srcStream.getStreamType(); |
| 4937 | // apply default device volume from source stream to all devices first in case |
| 4938 | // some devices are present in this stream state but not in source stream state |
| 4939 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
| 4940 | index = rescaleIndex(index, srcStreamType, mStreamType); |
| 4941 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4942 | mIndexMap.put(mIndexMap.keyAt(i), index); |
| 4943 | } |
| 4944 | // Now apply actual volume for devices in source stream state |
| 4945 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 4946 | for (int i = 0; i < srcMap.size(); i++) { |
| 4947 | int device = srcMap.keyAt(i); |
| 4948 | index = srcMap.valueAt(i); |
| 4949 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4950 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4951 | setIndex(index, device, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4952 | } |
| 4953 | } |
| 4954 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4955 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4956 | @GuardedBy("VolumeStreamState.class") |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4957 | public void setAllIndexesToMax() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4958 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4959 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4960 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4963 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4964 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4965 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4966 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4967 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4968 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4969 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4970 | // Set the new mute volume. This propagates the values to |
| 4971 | // the audio system, otherwise the volume won't be changed |
| 4972 | // at the lower level. |
| 4973 | sendMsg(mAudioHandler, |
| 4974 | MSG_SET_ALL_VOLUMES, |
| 4975 | SENDMSG_QUEUE, |
| 4976 | 0, |
| 4977 | 0, |
| 4978 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4981 | if (changed) { |
| 4982 | // Stream mute changed, fire the intent. |
| 4983 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 4984 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4985 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 4986 | sendBroadcastToAll(intent); |
| 4987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4988 | } |
| 4989 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4990 | public int getStreamType() { |
| 4991 | return mStreamType; |
| 4992 | } |
| 4993 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4994 | public void checkFixedVolumeDevices() { |
| 4995 | synchronized (VolumeStreamState.class) { |
| 4996 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 4997 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4998 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4999 | int device = mIndexMap.keyAt(i); |
| 5000 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 5001 | if (((device & mFullVolumeDevices) != 0) |
| 5002 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5003 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5004 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5005 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5011 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 5012 | if (index < mIndexMin) { |
| 5013 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 5014 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5015 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | return index; |
| 5019 | } |
| 5020 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5021 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5022 | pw.print(" Muted: "); |
| 5023 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 5024 | pw.print(" Min: "); |
| 5025 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5026 | pw.print(" Max: "); |
| 5027 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5028 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5029 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5030 | if (i > 0) { |
| 5031 | pw.print(", "); |
| 5032 | } |
| 5033 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5034 | pw.print(Integer.toHexString(device)); |
| 5035 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 5036 | : AudioSystem.getOutputDeviceName(device); |
| 5037 | if (!deviceName.isEmpty()) { |
| 5038 | pw.print(" ("); |
| 5039 | pw.print(deviceName); |
| 5040 | pw.print(")"); |
| 5041 | } |
| 5042 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5043 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5044 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5045 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5046 | pw.println(); |
| 5047 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5048 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5049 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 5050 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 5051 | // (the default device is not returned by getDevicesForStream) |
| 5052 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5053 | if ((devices & device) != 0) { |
| 5054 | if (n++ > 0) { |
| 5055 | pw.print(", "); |
| 5056 | } |
| 5057 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 5058 | } |
| 5059 | i++; |
| 5060 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5061 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | /** Thread that handles native AudioSystem control. */ |
| 5065 | private class AudioSystemThread extends Thread { |
| 5066 | AudioSystemThread() { |
| 5067 | super("AudioService"); |
| 5068 | } |
| 5069 | |
| 5070 | @Override |
| 5071 | public void run() { |
| 5072 | // Set this thread up so the handler will work on it |
| 5073 | Looper.prepare(); |
| 5074 | |
| 5075 | synchronized(AudioService.this) { |
| 5076 | mAudioHandler = new AudioHandler(); |
| 5077 | |
| 5078 | // Notify that the handler has been created |
| 5079 | AudioService.this.notify(); |
| 5080 | } |
| 5081 | |
| 5082 | // Listen for volume change requests that are set by VolumePanel |
| 5083 | Looper.loop(); |
| 5084 | } |
| 5085 | } |
| 5086 | |
| 5087 | /** Handles internal volume messages in separate volume thread. */ |
| 5088 | private class AudioHandler extends Handler { |
| 5089 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5090 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5091 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5092 | synchronized (VolumeStreamState.class) { |
| 5093 | // Apply volume |
| 5094 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5095 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5096 | // Apply change to all streams using this one as alias |
| 5097 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 5098 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 5099 | if (streamType != streamState.mStreamType && |
| 5100 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 5101 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 5102 | // that may have a different device selected |
| 5103 | int streamDevice = getDeviceForStream(streamType); |
| 5104 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 5105 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 5106 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 5107 | } |
| 5108 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 5109 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5110 | } |
| 5111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5112 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 5113 | sendMsg(mAudioHandler, |
| 5114 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 5115 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5116 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5117 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 5118 | streamState, |
| 5119 | PERSIST_DELAY); |
| 5120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5121 | } |
| 5122 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5123 | private void setAllVolumes(VolumeStreamState streamState) { |
| 5124 | |
| 5125 | // Apply volume |
| 5126 | streamState.applyAllVolumes(); |
| 5127 | |
| 5128 | // Apply change to all streams using this one as alias |
| 5129 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 5130 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 5131 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 5132 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5133 | mStreamStates[streamType].applyAllVolumes(); |
| 5134 | } |
| 5135 | } |
| 5136 | } |
| 5137 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5138 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 5139 | if (mUseFixedVolume) { |
| 5140 | return; |
| 5141 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 5142 | if (mIsSingleVolume && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5143 | return; |
| 5144 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 5145 | if (streamState.hasValidSettingsName()) { |
| 5146 | System.putIntForUser(mContentResolver, |
| 5147 | streamState.getSettingNameForDevice(device), |
| 5148 | (streamState.getIndex(device) + 5)/ 10, |
| 5149 | UserHandle.USER_CURRENT); |
| 5150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5151 | } |
| 5152 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5153 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 5154 | if (mUseFixedVolume) { |
| 5155 | return; |
| 5156 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 5157 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5158 | } |
| 5159 | |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5160 | private String getSoundEffectFilePath(int effectType) { |
| 5161 | String filePath = Environment.getProductDirectory() + SOUND_EFFECTS_PATH |
| 5162 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 5163 | if (!new File(filePath).isFile()) { |
| 5164 | filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH |
| 5165 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 5166 | } |
| 5167 | return filePath; |
| 5168 | } |
| 5169 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5170 | private boolean onLoadSoundEffects() { |
| 5171 | int status; |
| 5172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5173 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 5174 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5175 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 5176 | return false; |
| 5177 | } |
| 5178 | |
| 5179 | if (mSoundPool != null) { |
| 5180 | return true; |
| 5181 | } |
| 5182 | |
| 5183 | loadTouchSoundAssets(); |
| 5184 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 5185 | mSoundPool = new SoundPool.Builder() |
| 5186 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 5187 | .setAudioAttributes(new AudioAttributes.Builder() |
| 5188 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 5189 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 5190 | .build()) |
| 5191 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5192 | mSoundPoolCallBack = null; |
| 5193 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 5194 | mSoundPoolListenerThread.start(); |
| 5195 | int attempts = 3; |
| 5196 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 5197 | try { |
| 5198 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 5199 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5200 | } catch (InterruptedException e) { |
| 5201 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 5202 | } |
| 5203 | } |
| 5204 | |
| 5205 | if (mSoundPoolCallBack == null) { |
| 5206 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 5207 | if (mSoundPoolLooper != null) { |
| 5208 | mSoundPoolLooper.quit(); |
| 5209 | mSoundPoolLooper = null; |
| 5210 | } |
| 5211 | mSoundPoolListenerThread = null; |
| 5212 | mSoundPool.release(); |
| 5213 | mSoundPool = null; |
| 5214 | return false; |
| 5215 | } |
| 5216 | /* |
| 5217 | * poolId table: The value -1 in this table indicates that corresponding |
| 5218 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 5219 | * Once loaded, the value in poolId is the sample ID and the same |
| 5220 | * sample can be reused for another effect using the same file. |
| 5221 | */ |
| 5222 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 5223 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 5224 | poolId[fileIdx] = -1; |
| 5225 | } |
| 5226 | /* |
| 5227 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 5228 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 5229 | * this indicates we have a valid sample loaded for this effect. |
| 5230 | */ |
| 5231 | |
| 5232 | int numSamples = 0; |
| 5233 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5234 | // Do not load sample if this effect uses the MediaPlayer |
| 5235 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 5236 | continue; |
| 5237 | } |
| 5238 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5239 | String filePath = getSoundEffectFilePath(effect); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5240 | int sampleId = mSoundPool.load(filePath, 0); |
| 5241 | if (sampleId <= 0) { |
| 5242 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 5243 | } else { |
| 5244 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 5245 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 5246 | numSamples++; |
| 5247 | } |
| 5248 | } else { |
| 5249 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 5250 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 5251 | } |
| 5252 | } |
| 5253 | // wait for all samples to be loaded |
| 5254 | if (numSamples > 0) { |
| 5255 | mSoundPoolCallBack.setSamples(poolId); |
| 5256 | |
| 5257 | attempts = 3; |
| 5258 | status = 1; |
| 5259 | while ((status == 1) && (attempts-- > 0)) { |
| 5260 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 5261 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5262 | status = mSoundPoolCallBack.status(); |
| 5263 | } catch (InterruptedException e) { |
| 5264 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 5265 | } |
| 5266 | } |
| 5267 | } else { |
| 5268 | status = -1; |
| 5269 | } |
| 5270 | |
| 5271 | if (mSoundPoolLooper != null) { |
| 5272 | mSoundPoolLooper.quit(); |
| 5273 | mSoundPoolLooper = null; |
| 5274 | } |
| 5275 | mSoundPoolListenerThread = null; |
| 5276 | if (status != 0) { |
| 5277 | Log.w(TAG, |
| 5278 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 5279 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5280 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 5281 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 5282 | } |
| 5283 | } |
| 5284 | |
| 5285 | mSoundPool.release(); |
| 5286 | mSoundPool = null; |
| 5287 | } |
| 5288 | } |
| 5289 | return (status == 0); |
| 5290 | } |
| 5291 | |
| 5292 | /** |
| 5293 | * Unloads samples from the sound pool. |
| 5294 | * This method can be called to free some memory when |
| 5295 | * sound effects are disabled. |
| 5296 | */ |
| 5297 | private void onUnloadSoundEffects() { |
| 5298 | synchronized (mSoundEffectsLock) { |
| 5299 | if (mSoundPool == null) { |
| 5300 | return; |
| 5301 | } |
| 5302 | |
| 5303 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 5304 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 5305 | poolId[fileIdx] = 0; |
| 5306 | } |
| 5307 | |
| 5308 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5309 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 5310 | continue; |
| 5311 | } |
| 5312 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 5313 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 5314 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 5315 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 5316 | } |
| 5317 | } |
| 5318 | mSoundPool.release(); |
| 5319 | mSoundPool = null; |
| 5320 | } |
| 5321 | } |
| 5322 | |
| 5323 | private void onPlaySoundEffect(int effectType, int volume) { |
| 5324 | synchronized (mSoundEffectsLock) { |
| 5325 | |
| 5326 | onLoadSoundEffects(); |
| 5327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5328 | if (mSoundPool == null) { |
| 5329 | return; |
| 5330 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5331 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 5332 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5333 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 5334 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5335 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 5336 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5338 | |
| 5339 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5340 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 5341 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5342 | } else { |
| 5343 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5344 | try { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5345 | String filePath = getSoundEffectFilePath(effectType); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5346 | mediaPlayer.setDataSource(filePath); |
| 5347 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 5348 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 5349 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5350 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 5351 | public void onCompletion(MediaPlayer mp) { |
| 5352 | cleanupPlayer(mp); |
| 5353 | } |
| 5354 | }); |
| 5355 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 5356 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 5357 | cleanupPlayer(mp); |
| 5358 | return true; |
| 5359 | } |
| 5360 | }); |
| 5361 | mediaPlayer.start(); |
| 5362 | } catch (IOException ex) { |
| 5363 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 5364 | } catch (IllegalArgumentException ex) { |
| 5365 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 5366 | } catch (IllegalStateException ex) { |
| 5367 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5368 | } |
| 5369 | } |
| 5370 | } |
| 5371 | } |
| 5372 | |
| 5373 | private void cleanupPlayer(MediaPlayer mp) { |
| 5374 | if (mp != null) { |
| 5375 | try { |
| 5376 | mp.stop(); |
| 5377 | mp.release(); |
| 5378 | } catch (IllegalStateException ex) { |
| 5379 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5384 | private void setForceUse(int usage, int config, String eventSource) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5385 | synchronized (mConnectedDevices) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5386 | setForceUseInt_SyncDevices(usage, config, eventSource); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5387 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5388 | } |
| 5389 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 5390 | private void onPersistSafeVolumeState(int state) { |
| 5391 | Settings.Global.putInt(mContentResolver, |
| 5392 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 5393 | state); |
| 5394 | } |
| 5395 | |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 5396 | private void onNotifyVolumeEvent(@NonNull IAudioPolicyCallback apc, |
| 5397 | @AudioManager.VolumeAdjustment int direction) { |
| 5398 | try { |
| 5399 | apc.notifyVolumeAdjust(direction); |
| 5400 | } catch(Exception e) { |
| 5401 | // nothing we can do about this. Do not log error, too much potential for spam |
| 5402 | } |
| 5403 | } |
| 5404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5405 | @Override |
| 5406 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 5407 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5408 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5409 | case MSG_SET_DEVICE_VOLUME: |
| 5410 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 5411 | break; |
| 5412 | |
| 5413 | case MSG_SET_ALL_VOLUMES: |
| 5414 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5415 | break; |
| 5416 | |
| 5417 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5418 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5419 | break; |
| 5420 | |
| 5421 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5422 | // note that the value persisted is the current ringer mode, not the |
| 5423 | // 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] | 5424 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5425 | break; |
| 5426 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 5427 | case MSG_AUDIO_SERVER_DIED: |
| 5428 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5429 | break; |
| 5430 | |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 5431 | case MSG_DISPATCH_AUDIO_SERVER_STATE: |
| 5432 | onDispatchAudioServerStateChange(msg.arg1 == 1); |
| 5433 | break; |
| 5434 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5435 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 5436 | onUnloadSoundEffects(); |
| 5437 | break; |
| 5438 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5439 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5440 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 5441 | // can take several dozens of milliseconds to complete |
| 5442 | boolean loaded = onLoadSoundEffects(); |
| 5443 | if (msg.obj != null) { |
| 5444 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 5445 | synchronized (reply) { |
| 5446 | reply.mStatus = loaded ? 0 : -1; |
| 5447 | reply.notify(); |
| 5448 | } |
| 5449 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5450 | break; |
| 5451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5452 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5453 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5455 | |
| 5456 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 5457 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5458 | synchronized (mConnectedDevices) { |
| 5459 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 5460 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5461 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5462 | |
| 5463 | case MSG_SET_FORCE_USE: |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5464 | case MSG_SET_FORCE_BT_A2DP_USE: |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5465 | setForceUse(msg.arg1, msg.arg2, (String) msg.obj); |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5466 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 5467 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5468 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 5469 | resetBluetoothSco(); |
| 5470 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5471 | |
| 5472 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5473 | { WiredDeviceConnectionState connectState = |
| 5474 | (WiredDeviceConnectionState)msg.obj; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5475 | mWiredDevLogger.log(new WiredDevConnectEvent(connectState)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5476 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5477 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5478 | mAudioEventWakeLock.release(); |
| 5479 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5480 | break; |
| 5481 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5482 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 5483 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 5484 | mAudioEventWakeLock.release(); |
| 5485 | break; |
| 5486 | |
| 5487 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 5488 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5489 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5490 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5491 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5492 | case MSG_SET_HEARING_AID_CONNECTION_STATE: |
| 5493 | onSetHearingAidConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 5494 | mAudioEventWakeLock.release(); |
| 5495 | break; |
| 5496 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5497 | case MSG_A2DP_DEVICE_CONFIG_CHANGE: |
| 5498 | onBluetoothA2dpDeviceConfigChange((BluetoothDevice)msg.obj); |
| 5499 | mAudioEventWakeLock.release(); |
| 5500 | break; |
| 5501 | |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 5502 | case MSG_DISABLE_AUDIO_FOR_UID: |
| 5503 | mPlaybackMonitor.disableAudioForUid( msg.arg1 == 1 /* disable */, |
| 5504 | msg.arg2 /* uid */); |
| 5505 | mAudioEventWakeLock.release(); |
| 5506 | break; |
| 5507 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5508 | case MSG_REPORT_NEW_ROUTES: { |
| 5509 | int N = mRoutesObservers.beginBroadcast(); |
| 5510 | if (N > 0) { |
| 5511 | AudioRoutesInfo routes; |
| 5512 | synchronized (mCurAudioRoutes) { |
| 5513 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5514 | } |
| 5515 | while (N > 0) { |
| 5516 | N--; |
| 5517 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 5518 | try { |
| 5519 | obs.dispatchAudioRoutesChanged(routes); |
| 5520 | } catch (RemoteException e) { |
| 5521 | } |
| 5522 | } |
| 5523 | } |
| 5524 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5525 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5526 | break; |
| 5527 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5528 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5529 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5530 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5531 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5532 | |
| 5533 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 5534 | onSendBecomingNoisyIntent(); |
| 5535 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5536 | |
| 5537 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 5538 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5539 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 5540 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5541 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 5542 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 5543 | onPersistSafeVolumeState(msg.arg1); |
| 5544 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 5545 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 5546 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 5547 | onBroadcastScoConnectionState(msg.arg1); |
| 5548 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 5549 | |
| 5550 | case MSG_SYSTEM_READY: |
| 5551 | onSystemReady(); |
| 5552 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5553 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 5554 | case MSG_INDICATE_SYSTEM_READY: |
| 5555 | onIndicateSystemReady(); |
| 5556 | break; |
| 5557 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5558 | case MSG_ACCESSORY_PLUG_MEDIA_UNMUTE: |
| 5559 | onAccessoryPlugMediaUnmute(msg.arg1); |
| 5560 | break; |
| 5561 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5562 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 5563 | final int musicActiveMs = msg.arg1; |
| 5564 | Settings.Secure.putIntForUser(mContentResolver, |
| 5565 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 5566 | UserHandle.USER_CURRENT); |
| 5567 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5568 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 5569 | case MSG_UNMUTE_STREAM: |
| 5570 | onUnmuteStream(msg.arg1, msg.arg2); |
| 5571 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5572 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 5573 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 5574 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 5575 | break; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 5576 | |
| 5577 | case MSG_NOTIFY_VOL_EVENT: |
| 5578 | onNotifyVolumeEvent((IAudioPolicyCallback) msg.obj, msg.arg1); |
| 5579 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5580 | } |
| 5581 | } |
| 5582 | } |
| 5583 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5584 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5585 | |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5586 | private int mEncodedSurroundMode; |
| 5587 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5588 | SettingsObserver() { |
| 5589 | super(new Handler()); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5590 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5591 | Settings.Global.ZEN_MODE), false, this); |
| 5592 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5593 | Settings.Global.ZEN_MODE_CONFIG_ETAG), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5594 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5595 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5596 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5597 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5598 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5599 | Settings.System.MASTER_MONO), false, this); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5600 | |
| 5601 | mEncodedSurroundMode = Settings.Global.getInt( |
| 5602 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5603 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5604 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5605 | Settings.Global.ENCODED_SURROUND_OUTPUT), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | @Override |
| 5609 | public void onChange(boolean selfChange) { |
| 5610 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5611 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5612 | // However there appear to be some missing locks around mRingerAndZenModeMutedStreams |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5613 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5614 | // mRingerAndZenModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5615 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5616 | if (updateRingerAndZenModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5617 | /* |
| 5618 | * Ensure all stream types that should be affected by ringer mode |
| 5619 | * are in the proper state. |
| 5620 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5621 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5622 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5623 | readDockAudioSettings(mContentResolver); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5624 | updateMasterMono(mContentResolver); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5625 | updateEncodedSurroundOutput(); |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | private void updateEncodedSurroundOutput() { |
| 5630 | int newSurroundMode = Settings.Global.getInt( |
| 5631 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5632 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5633 | // Did it change? |
| 5634 | if (mEncodedSurroundMode != newSurroundMode) { |
| 5635 | // Send to AudioPolicyManager |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5636 | sendEncodedSurroundMode(newSurroundMode, "SettingsObserver"); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5637 | synchronized(mConnectedDevices) { |
| 5638 | // Is HDMI connected? |
| 5639 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HDMI, ""); |
| 5640 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5641 | if (deviceSpec != null) { |
| 5642 | // Toggle HDMI to retrigger broadcast with proper formats. |
| 5643 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5644 | AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "", |
| 5645 | "android"); // disconnect |
| 5646 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5647 | AudioSystem.DEVICE_STATE_AVAILABLE, "", "", |
| 5648 | "android"); // reconnect |
| 5649 | } |
| 5650 | } |
| 5651 | mEncodedSurroundMode = newSurroundMode; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5652 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5653 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5654 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5655 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5656 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5657 | private void makeA2dpDeviceAvailable(String address, String name, String eventSource) { |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5658 | // enable A2DP before notifying A2DP connection to avoid unnecessary processing in |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5659 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5660 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 5661 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 5662 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5663 | setBluetoothA2dpOnInt(true, eventSource); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5664 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5665 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5666 | // Reset A2DP suspend state each time a new sink is connected |
| 5667 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5668 | mConnectedDevices.put( |
| 5669 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5670 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5671 | address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5672 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5673 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, null, 0); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5674 | } |
| 5675 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5676 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5677 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 5678 | } |
| 5679 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5680 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5681 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5682 | synchronized (mA2dpAvrcpLock) { |
| 5683 | mAvrcpAbsVolSupported = false; |
| 5684 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5685 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5686 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5687 | mConnectedDevices.remove( |
| 5688 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5689 | // Remove A2DP routes as well |
| 5690 | setCurrentAudioRouteName(null); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5693 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5694 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 5695 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 5696 | // reconnection of the sink. |
| 5697 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5698 | // 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] | 5699 | mConnectedDevices.remove( |
| 5700 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5701 | // send the delayed message to make the device unavailable later |
| 5702 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5703 | mAudioHandler.sendMessageDelayed(msg, delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5704 | |
| 5705 | } |
| 5706 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5707 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5708 | private void makeA2dpSrcAvailable(String address) { |
| 5709 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5710 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5711 | mConnectedDevices.put( |
| 5712 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5713 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5714 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | // must be called synchronized on mConnectedDevices |
| 5718 | private void makeA2dpSrcUnavailable(String address) { |
| 5719 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5720 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5721 | mConnectedDevices.remove( |
| 5722 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5723 | } |
| 5724 | |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5725 | private void setHearingAidVolume(int index, int streamType) { |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5726 | synchronized (mHearingAidLock) { |
| 5727 | if (mHearingAid != null) { |
| 5728 | //hearing aid expect volume value in range -128dB to 0dB |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5729 | int gainDB = (int)AudioSystem.getStreamVolumeDB(streamType, index/10, |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5730 | AudioSystem.DEVICE_OUT_HEARING_AID); |
| 5731 | if (gainDB < BT_HEARING_AID_GAIN_MIN) |
| 5732 | gainDB = BT_HEARING_AID_GAIN_MIN; |
| 5733 | mHearingAid.setVolume(gainDB); |
| 5734 | } |
| 5735 | } |
| 5736 | } |
| 5737 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5738 | // must be called synchronized on mConnectedDevices |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5739 | private void makeHearingAidDeviceAvailable(String address, String name, String eventSource) { |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5740 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(AudioSystem.DEVICE_OUT_HEARING_AID); |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5741 | setHearingAidVolume(index, AudioSystem.STREAM_MUSIC); |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5742 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5743 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 5744 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
| 5745 | mConnectedDevices.put( |
| 5746 | makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, address), |
| 5747 | new DeviceListSpec(AudioSystem.DEVICE_OUT_HEARING_AID, name, |
| 5748 | address)); |
| 5749 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5750 | AudioSystem.DEVICE_OUT_HEARING_AID, 0, null, 0); |
| 5751 | } |
| 5752 | |
| 5753 | // must be called synchronized on mConnectedDevices |
| 5754 | private void makeHearingAidDeviceUnavailable(String address) { |
| 5755 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 5756 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
| 5757 | mConnectedDevices.remove( |
| 5758 | makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, address)); |
| 5759 | // Remove Hearing Aid routes as well |
| 5760 | setCurrentAudioRouteName(null); |
| 5761 | } |
| 5762 | |
| 5763 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5764 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5765 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5766 | } |
| 5767 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5768 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5769 | private boolean hasScheduledA2dpDockTimeout() { |
| 5770 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5771 | } |
| 5772 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5773 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5774 | { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 5775 | if (DEBUG_DEVICES) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5776 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice=" + btDevice+"state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5777 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5778 | if (btDevice == null) { |
| 5779 | return; |
| 5780 | } |
| 5781 | String address = btDevice.getAddress(); |
| 5782 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5783 | address = ""; |
| 5784 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5785 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5786 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5787 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 5788 | btDevice.getAddress()); |
| 5789 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5790 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5791 | |
| 5792 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5793 | if (btDevice.isBluetoothDock()) { |
| 5794 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 5795 | // introduction of a delay for transient disconnections of docks when |
| 5796 | // power is rapidly turned off/on, this message will be canceled if |
| 5797 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5798 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5799 | // the next time isConnected is evaluated, it will be false for the dock |
| 5800 | } |
| 5801 | } else { |
| 5802 | makeA2dpDeviceUnavailableNow(address); |
| 5803 | } |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5804 | setCurrentAudioRouteName(null); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5805 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5806 | if (btDevice.isBluetoothDock()) { |
| 5807 | // this could be a reconnection after a transient disconnection |
| 5808 | cancelA2dpDeviceTimeout(); |
| 5809 | mDockAddress = address; |
| 5810 | } else { |
| 5811 | // this could be a connection of another A2DP device before the timeout of |
| 5812 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 5813 | if(hasScheduledA2dpDockTimeout()) { |
| 5814 | cancelA2dpDeviceTimeout(); |
| 5815 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 5816 | } |
| 5817 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5818 | makeA2dpDeviceAvailable(address, btDevice.getName(), |
| 5819 | "onSetA2dpSinkConnectionState"); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5820 | setCurrentAudioRouteName(btDevice.getAliasName()); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5825 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 5826 | { |
| 5827 | if (DEBUG_VOL) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5828 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice=" + btDevice + " state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5829 | } |
| 5830 | if (btDevice == null) { |
| 5831 | return; |
| 5832 | } |
| 5833 | String address = btDevice.getAddress(); |
| 5834 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5835 | address = ""; |
| 5836 | } |
| 5837 | |
| 5838 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5839 | final String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 5840 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5841 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5842 | |
| 5843 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5844 | makeA2dpSrcUnavailable(address); |
| 5845 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5846 | makeA2dpSrcAvailable(address); |
| 5847 | } |
| 5848 | } |
| 5849 | } |
| 5850 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5851 | private void onSetHearingAidConnectionState(BluetoothDevice btDevice, int state) |
| 5852 | { |
| 5853 | if (DEBUG_DEVICES) { |
| 5854 | Log.d(TAG, "onSetHearingAidConnectionState btDevice=" + btDevice+", state=" + state); |
| 5855 | } |
| 5856 | if (btDevice == null) { |
| 5857 | return; |
| 5858 | } |
| 5859 | String address = btDevice.getAddress(); |
| 5860 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5861 | address = ""; |
| 5862 | } |
| 5863 | |
| 5864 | synchronized (mConnectedDevices) { |
| 5865 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 5866 | btDevice.getAddress()); |
| 5867 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5868 | boolean isConnected = deviceSpec != null; |
| 5869 | |
| 5870 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5871 | makeHearingAidDeviceUnavailable(address); |
| 5872 | setCurrentAudioRouteName(null); |
| 5873 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5874 | makeHearingAidDeviceAvailable(address, btDevice.getName(), |
| 5875 | "onSetHearingAidConnectionState"); |
| 5876 | setCurrentAudioRouteName(btDevice.getAliasName()); |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | private void setCurrentAudioRouteName(String name){ |
| 5882 | synchronized (mCurAudioRoutes) { |
| 5883 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 5884 | mCurAudioRoutes.bluetoothName = name; |
| 5885 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 5886 | SENDMSG_NOOP, 0, 0, null, 0); |
| 5887 | } |
| 5888 | } |
| 5889 | } |
| 5890 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5891 | private void onBluetoothA2dpDeviceConfigChange(BluetoothDevice btDevice) |
| 5892 | { |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5893 | if (DEBUG_DEVICES) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5894 | Log.d(TAG, "onBluetoothA2dpDeviceConfigChange btDevice=" + btDevice); |
| 5895 | } |
| 5896 | if (btDevice == null) { |
| 5897 | return; |
| 5898 | } |
| 5899 | String address = btDevice.getAddress(); |
| 5900 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5901 | address = ""; |
| 5902 | } |
| 5903 | |
| 5904 | int device = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; |
| 5905 | synchronized (mConnectedDevices) { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 5906 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, btDevice)) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5907 | return; |
| 5908 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5909 | final String key = makeDeviceListKey(device, address); |
| 5910 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5911 | if (deviceSpec != null) { |
| 5912 | // Device is connected |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5913 | int musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5914 | if (AudioSystem.handleDeviceConfigChange(device, address, |
| 5915 | btDevice.getName()) != AudioSystem.AUDIO_STATUS_OK) { |
| 5916 | // force A2DP device disconnection in case of error so that AudioService state is |
| 5917 | // consistent with audio policy manager state |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5918 | setBluetoothA2dpDeviceConnectionStateInt( |
| 5919 | btDevice, BluetoothA2dp.STATE_DISCONNECTED, BluetoothProfile.A2DP, |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 5920 | false /* suppressNoisyIntent */, musicDevice); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 5921 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5922 | } |
| 5923 | } |
| 5924 | } |
| 5925 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5926 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 5927 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 5928 | synchronized (mA2dpAvrcpLock) { |
| 5929 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5930 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 5931 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5932 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 5933 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 5934 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5935 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5936 | } |
| 5937 | } |
| 5938 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5939 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 5940 | String deviceName) { |
| 5941 | if (DEBUG_DEVICES) { |
| 5942 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 5943 | + " address:" + address + " name:" + deviceName + ")"); |
| 5944 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5945 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5946 | String deviceKey = makeDeviceListKey(device, address); |
| 5947 | if (DEBUG_DEVICES) { |
| 5948 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 5949 | } |
| 5950 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 5951 | boolean isConnected = deviceSpec != null; |
| 5952 | if (DEBUG_DEVICES) { |
| 5953 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 5954 | } |
| 5955 | if (connect && !isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5956 | final int res = AudioSystem.setDeviceConnectionState(device, |
| 5957 | AudioSystem.DEVICE_STATE_AVAILABLE, address, deviceName); |
| 5958 | if (res != AudioSystem.AUDIO_STATUS_OK) { |
| 5959 | Slog.e(TAG, "not connecting device 0x" + Integer.toHexString(device) + |
| 5960 | " due to command error " + res ); |
| 5961 | return false; |
| 5962 | } |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5963 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5964 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5965 | device, 0, null, 0); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5966 | return true; |
| 5967 | } else if (!connect && isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 5968 | AudioSystem.setDeviceConnectionState(device, |
| 5969 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, deviceName); |
| 5970 | // always remove even if disconnection failed |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5971 | mConnectedDevices.remove(deviceKey); |
| 5972 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5973 | } |
Jack He | f05e516 | 2018-04-19 15:03:43 -0700 | [diff] [blame] | 5974 | Log.w(TAG, "handleDeviceConnection() failed, deviceKey=" + deviceKey + ", deviceSpec=" |
| 5975 | + deviceSpec + ", connect=" + connect); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5976 | } |
| 5977 | return false; |
| 5978 | } |
| 5979 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5980 | // 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] | 5981 | // sent if: |
| 5982 | // - none of these devices are connected anymore after one is disconnected AND |
| 5983 | // - the device being disconnected is actually used for music. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5984 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5985 | int mBecomingNoisyIntentDevices = |
| 5986 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5987 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 5988 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jakub Pawlowski | 26fac41 | 2018-03-22 15:00:34 -0700 | [diff] [blame] | 5989 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE | |
| 5990 | AudioSystem.DEVICE_OUT_HEARING_AID; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5991 | |
| 5992 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5993 | // Called synchronized on mConnectedDevices |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 5994 | // musicDevice argument is used when not AudioSystem.DEVICE_NONE instead of querying |
| 5995 | // from AudioSystem |
| 5996 | private int checkSendBecomingNoisyIntent(int device, int state, int musicDevice) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5997 | int delay = 0; |
| 5998 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 5999 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 6000 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 6001 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6002 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 6003 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 6004 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6005 | } |
| 6006 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6007 | if (musicDevice == AudioSystem.DEVICE_NONE) { |
| 6008 | musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 6009 | } |
| 6010 | // ignore condition on device being actually used for music when in communication |
| 6011 | // because music routing is altered in this case. |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 6012 | // also checks whether media routing if affected by a dynamic policy |
| 6013 | if (((device == musicDevice) || isInCommunication()) && (device == devices) |
| 6014 | && !hasMediaDynamicPolicy()) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6015 | mAudioHandler.removeMessages(MSG_BROADCAST_AUDIO_BECOMING_NOISY); |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 6016 | sendMsg(mAudioHandler, |
| 6017 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 6018 | SENDMSG_REPLACE, |
| 6019 | 0, |
| 6020 | 0, |
| 6021 | null, |
| 6022 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6023 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6024 | } |
| 6025 | } |
| 6026 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 6027 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 6028 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 6029 | mAudioHandler.hasMessages(MSG_SET_HEARING_AID_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6030 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 6031 | synchronized (mLastDeviceConnectMsgTime) { |
| 6032 | long time = SystemClock.uptimeMillis(); |
| 6033 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 6034 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 6035 | } |
| 6036 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6037 | } |
| 6038 | return delay; |
| 6039 | } |
| 6040 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 6041 | /** |
| 6042 | * @return true if there is currently a registered dynamic mixing policy that affects media |
| 6043 | */ |
| 6044 | private boolean hasMediaDynamicPolicy() { |
| 6045 | synchronized (mAudioPolicies) { |
| 6046 | if (mAudioPolicies.isEmpty()) { |
| 6047 | return false; |
| 6048 | } |
| 6049 | final Collection<AudioPolicyProxy> appColl = mAudioPolicies.values(); |
| 6050 | for (AudioPolicyProxy app : appColl) { |
| 6051 | if (app.hasMixAffectingUsage(AudioAttributes.USAGE_MEDIA)) { |
| 6052 | return true; |
| 6053 | } |
| 6054 | } |
| 6055 | return false; |
| 6056 | } |
| 6057 | } |
| 6058 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6059 | private void updateAudioRoutes(int device, int state) |
| 6060 | { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6061 | int connType = 0; |
| 6062 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6063 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6064 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 6065 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 6066 | device == AudioSystem.DEVICE_OUT_LINE) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6067 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 6068 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 6069 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6070 | connType = AudioRoutesInfo.MAIN_HDMI; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6071 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE|| |
| 6072 | device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6073 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6074 | } |
| 6075 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6076 | synchronized (mCurAudioRoutes) { |
| 6077 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6078 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6079 | if (state != 0) { |
| 6080 | newConn |= connType; |
| 6081 | } else { |
| 6082 | newConn &= ~connType; |
| 6083 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6084 | if (newConn != mCurAudioRoutes.mainType) { |
| 6085 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6086 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6087 | SENDMSG_NOOP, 0, 0, null, 0); |
| 6088 | } |
| 6089 | } |
| 6090 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6091 | } |
| 6092 | |
| 6093 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 6094 | String deviceName) { |
| 6095 | if (DEBUG_DEVICES) { |
| 6096 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 6097 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 6098 | " name:" + deviceName + ");"); |
| 6099 | } |
| 6100 | Intent intent = new Intent(); |
| 6101 | |
| 6102 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
| 6103 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 6104 | intent.putExtra("microphone", 1); |
| 6105 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 6106 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 6107 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 6108 | intent.putExtra("microphone", 0); |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 6109 | } else if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 6110 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 6111 | intent.putExtra("microphone", |
| 6112 | AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_IN_USB_HEADSET, "") |
| 6113 | == AudioSystem.DEVICE_STATE_AVAILABLE ? 1 : 0); |
| 6114 | } else if (device == AudioSystem.DEVICE_IN_USB_HEADSET) { |
| 6115 | if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_USB_HEADSET, "") |
| 6116 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
| 6117 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 6118 | intent.putExtra("microphone", 1); |
| 6119 | } else { |
| 6120 | // do not send ACTION_HEADSET_PLUG when only the input side is seen as changing |
| 6121 | return; |
| 6122 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6123 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 6124 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
| 6125 | configureHdmiPlugIntent(intent, state); |
| 6126 | } |
| 6127 | |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 6128 | if (intent.getAction() == null) { |
| 6129 | return; |
| 6130 | } |
| 6131 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6132 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 6133 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 6134 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 6135 | |
| 6136 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6137 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6138 | final long ident = Binder.clearCallingIdentity(); |
| 6139 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 6140 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6141 | } finally { |
| 6142 | Binder.restoreCallingIdentity(ident); |
| 6143 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6144 | } |
| 6145 | |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6146 | private static final int DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG = |
| 6147 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 6148 | AudioSystem.DEVICE_OUT_LINE | |
| 6149 | AudioSystem.DEVICE_OUT_ALL_USB; |
| 6150 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6151 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6152 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6153 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6154 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 6155 | + " state:" + Integer.toHexString(state) |
| 6156 | + " address:" + address |
| 6157 | + " deviceName:" + deviceName |
| 6158 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6159 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6160 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6161 | synchronized (mConnectedDevices) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6162 | if ((state == 0) && ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0)) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6163 | setBluetoothA2dpOnInt(true, "onSetWiredDeviceConnectionState state 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6164 | } |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 6165 | |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 6166 | if (!handleDeviceConnection(state == 1, device, address, deviceName)) { |
| 6167 | // change of connection state failed, bailout |
| 6168 | return; |
| 6169 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6170 | if (state != 0) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6171 | if ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6172 | setBluetoothA2dpOnInt(false, "onSetWiredDeviceConnectionState state not 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6173 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6174 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 6175 | sendMsg(mAudioHandler, |
| 6176 | MSG_CHECK_MUSIC_ACTIVE, |
| 6177 | SENDMSG_REPLACE, |
| 6178 | 0, |
| 6179 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6180 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6181 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 6182 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6183 | // Television devices without CEC service apply software volume on HDMI output |
| 6184 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 6185 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 6186 | checkAllFixedVolumeDevices(); |
| 6187 | if (mHdmiManager != null) { |
| 6188 | synchronized (mHdmiManager) { |
| 6189 | if (mHdmiPlaybackClient != null) { |
| 6190 | mHdmiCecSink = false; |
| 6191 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 6192 | } |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | } else { |
| 6197 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 6198 | if (mHdmiManager != null) { |
| 6199 | synchronized (mHdmiManager) { |
| 6200 | mHdmiCecSink = false; |
| 6201 | } |
| 6202 | } |
| 6203 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6204 | } |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 6205 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6206 | updateAudioRoutes(device, state); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6207 | } |
| 6208 | } |
| 6209 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6210 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6211 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 6212 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6213 | if (state == 1) { |
| 6214 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 6215 | int[] portGeneration = new int[1]; |
| 6216 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 6217 | if (status == AudioManager.SUCCESS) { |
| 6218 | for (AudioPort port : ports) { |
| 6219 | if (port instanceof AudioDevicePort) { |
| 6220 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 6221 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 6222 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6223 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 6224 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6225 | if (formats.length > 0) { |
| 6226 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 6227 | for (int format : formats) { |
| 6228 | // a format in the list can be 0, skip it |
| 6229 | if (format != AudioFormat.ENCODING_INVALID) { |
| 6230 | encodingList.add(format); |
| 6231 | } |
| 6232 | } |
| 6233 | int[] encodingArray = new int[encodingList.size()]; |
| 6234 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 6235 | encodingArray[i] = encodingList.get(i); |
| 6236 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6237 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6238 | } |
| 6239 | // find the maximum supported number of channels |
| 6240 | int maxChannels = 0; |
| 6241 | for (int mask : devicePort.channelMasks()) { |
| 6242 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 6243 | if (channelCount > maxChannels) { |
| 6244 | maxChannels = channelCount; |
| 6245 | } |
| 6246 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6247 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6248 | } |
| 6249 | } |
| 6250 | } |
| 6251 | } |
| 6252 | } |
| 6253 | } |
| 6254 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 6255 | /* cache of the address of the last dock the device was connected to */ |
| 6256 | private String mDockAddress; |
| 6257 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6258 | /** |
| 6259 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 6260 | */ |
| 6261 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 6262 | @Override |
| 6263 | public void onReceive(Context context, Intent intent) { |
| 6264 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 6265 | int outDevice; |
| 6266 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6267 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6268 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 6269 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 6270 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 6271 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 6272 | int config; |
| 6273 | switch (dockState) { |
| 6274 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 6275 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 6276 | break; |
| 6277 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 6278 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 6279 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 6280 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6281 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 6282 | break; |
| 6283 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 6284 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 6285 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 6286 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 6287 | default: |
| 6288 | config = AudioSystem.FORCE_NONE; |
| 6289 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6290 | // Low end docks have a menu to enable or disable audio |
| 6291 | // (see mDockAudioMediaEnabled) |
| 6292 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 6293 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 6294 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6295 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, config, |
| 6296 | "ACTION_DOCK_EVENT intent")); |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6297 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 6298 | } |
| 6299 | mDockState = dockState; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 6300 | } else if (action.equals(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED)) { |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 6301 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 6302 | setBtScoActiveDevice(btDevice); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 6303 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6304 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6305 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6306 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6307 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6308 | // broadcast intent if the connection was initated by AudioService |
| 6309 | if (!mScoClients.isEmpty() && |
| 6310 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 6311 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 6312 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6313 | broadcast = true; |
| 6314 | } |
| 6315 | switch (btState) { |
| 6316 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6317 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6318 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 6319 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 6320 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6321 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6322 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6323 | break; |
| 6324 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6325 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6326 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 6327 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6328 | break; |
| 6329 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6330 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 6331 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 6332 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6333 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6334 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6335 | default: |
| 6336 | // do not broadcast CONNECTING or invalid state |
| 6337 | broadcast = false; |
| 6338 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6339 | } |
| 6340 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6341 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6342 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6343 | //FIXME: this is to maintain compatibility with deprecated intent |
| 6344 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6345 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6346 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6347 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6348 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6349 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6350 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 6351 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6352 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6353 | AudioSystem.setParameters("screen_state=on"); |
| 6354 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6355 | if (mMonitorRotation) { |
| 6356 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 6357 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6358 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6359 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 6360 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6361 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 6362 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 6363 | if (mUserSwitchedReceived) { |
| 6364 | // attempt to stop music playback for background user except on first user |
| 6365 | // switch (i.e. first boot) |
| 6366 | sendMsg(mAudioHandler, |
| 6367 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 6368 | SENDMSG_REPLACE, |
| 6369 | 0, |
| 6370 | 0, |
| 6371 | null, |
| 6372 | 0); |
| 6373 | } |
| 6374 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 6375 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6376 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 6377 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 6378 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 6379 | readAudioSettings(true /*userSwitch*/); |
| 6380 | // preserve STREAM_MUSIC volume from one user to the next. |
| 6381 | sendMsg(mAudioHandler, |
| 6382 | MSG_SET_ALL_VOLUMES, |
| 6383 | SENDMSG_QUEUE, |
| 6384 | 0, |
| 6385 | 0, |
| 6386 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6387 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 6388 | // Disable audio recording for the background user/profile |
| 6389 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 6390 | if (userId >= 0) { |
| 6391 | // TODO Kill recording streams instead of killing processes holding permission |
| 6392 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 6393 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 6394 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 6395 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6396 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 6397 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 6398 | // Enable audio recording for foreground user/profile |
| 6399 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 6400 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6401 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 6402 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 6403 | state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 6404 | if (state == BluetoothAdapter.STATE_OFF || |
| 6405 | state == BluetoothAdapter.STATE_TURNING_OFF) { |
| 6406 | disconnectAllBluetoothProfiles(); |
| 6407 | } |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 6408 | } else if (action.equals(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION) || |
| 6409 | action.equals(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)) { |
| 6410 | handleAudioEffectBroadcast(context, intent); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6411 | } |
| 6412 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 6413 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6414 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6415 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 6416 | |
| 6417 | @Override |
| 6418 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 6419 | Bundle prevRestrictions) { |
| 6420 | // Update mic mute state. |
| 6421 | { |
| 6422 | final boolean wasRestricted = |
| 6423 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 6424 | final boolean isRestricted = |
| 6425 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 6426 | if (wasRestricted != isRestricted) { |
| 6427 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | // Update speaker mute state. |
| 6432 | { |
| 6433 | final boolean wasRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 6434 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 6435 | || prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6436 | final boolean isRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 6437 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 6438 | || newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6439 | if (wasRestricted != isRestricted) { |
| 6440 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 6441 | } |
| 6442 | } |
| 6443 | } |
| 6444 | } // end class AudioServiceUserRestrictionsListener |
| 6445 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 6446 | private void handleAudioEffectBroadcast(Context context, Intent intent) { |
| 6447 | String target = intent.getPackage(); |
| 6448 | if (target != null) { |
| 6449 | Log.w(TAG, "effect broadcast already targeted to " + target); |
| 6450 | return; |
| 6451 | } |
| 6452 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 6453 | // TODO this should target a user-selected panel |
| 6454 | List<ResolveInfo> ril = context.getPackageManager().queryBroadcastReceivers( |
| 6455 | intent, 0 /* flags */); |
| 6456 | if (ril != null && ril.size() != 0) { |
| 6457 | ResolveInfo ri = ril.get(0); |
| 6458 | if (ri != null && ri.activityInfo != null && ri.activityInfo.packageName != null) { |
| 6459 | intent.setPackage(ri.activityInfo.packageName); |
| 6460 | context.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 6461 | return; |
| 6462 | } |
| 6463 | } |
| 6464 | Log.w(TAG, "couldn't find receiver package for effect intent"); |
| 6465 | } |
| 6466 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6467 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 6468 | PackageManager pm = mContext.getPackageManager(); |
| 6469 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 6470 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 6471 | ComponentName homeActivityName = null; |
| 6472 | if (!oldUser.isManagedProfile()) { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 6473 | homeActivityName = mActivityManagerInternal.getHomeActivityForUser(oldUser.id); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6474 | } |
| 6475 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 6476 | List<PackageInfo> packages; |
| 6477 | try { |
| 6478 | packages = AppGlobals.getPackageManager() |
| 6479 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 6480 | } catch (RemoteException e) { |
| 6481 | throw new AndroidRuntimeException(e); |
| 6482 | } |
| 6483 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 6484 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 6485 | // Skip system processes |
| 6486 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 6487 | continue; |
| 6488 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 6489 | // Skip packages that have permission to interact across users |
| 6490 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 6491 | == PackageManager.PERMISSION_GRANTED) { |
| 6492 | continue; |
| 6493 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6494 | if (homeActivityName != null |
| 6495 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 6496 | && pkg.applicationInfo.isSystemApp()) { |
| 6497 | continue; |
| 6498 | } |
| 6499 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 6500 | final int uid = pkg.applicationInfo.uid; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 6501 | ActivityManager.getService().killUid(UserHandle.getAppId(uid), |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 6502 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6503 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 6504 | } catch (RemoteException e) { |
| 6505 | Log.w(TAG, "Error calling killUid", e); |
| 6506 | } |
| 6507 | } |
| 6508 | } |
| 6509 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6510 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6511 | //========================================================================================== |
| 6512 | // Audio Focus |
| 6513 | //========================================================================================== |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6514 | /** |
| 6515 | * Returns whether a focus request is eligible to force ducking. |
| 6516 | * Will return true if: |
| 6517 | * - the AudioAttributes have a usage of USAGE_ASSISTANCE_ACCESSIBILITY, |
| 6518 | * - the focus request is AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 6519 | * - the associated Bundle has KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING set to true, |
| 6520 | * - the uid of the requester is a known accessibility service or root. |
| 6521 | * @param aa AudioAttributes of the focus request |
| 6522 | * @param uid uid of the focus requester |
| 6523 | * @return true if ducking is to be forced |
| 6524 | */ |
| 6525 | private boolean forceFocusDuckingForAccessibility(@Nullable AudioAttributes aa, |
| 6526 | int request, int uid) { |
| 6527 | if (aa == null || aa.getUsage() != AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY |
| 6528 | || request != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) { |
| 6529 | return false; |
| 6530 | } |
| 6531 | final Bundle extraInfo = aa.getBundle(); |
| 6532 | if (extraInfo == null || |
| 6533 | !extraInfo.getBoolean(AudioFocusRequest.KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING)) { |
| 6534 | return false; |
| 6535 | } |
| 6536 | if (uid == 0) { |
| 6537 | return true; |
| 6538 | } |
| 6539 | synchronized (mAccessibilityServiceUidsLock) { |
| 6540 | if (mAccessibilityServiceUids != null) { |
| 6541 | int callingUid = Binder.getCallingUid(); |
| 6542 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 6543 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 6544 | return true; |
| 6545 | } |
| 6546 | } |
| 6547 | } |
| 6548 | } |
| 6549 | return false; |
| 6550 | } |
| 6551 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6552 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6553 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 6554 | IAudioPolicyCallback pcb, int sdk) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6555 | // permission checks |
| 6556 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6557 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6558 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 6559 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 6560 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 6561 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6562 | } |
| 6563 | } else { |
| 6564 | // only a registered audio policy can be used to lock focus |
| 6565 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6566 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6567 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6568 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6569 | } |
| 6570 | } |
| 6571 | } |
| 6572 | } |
| 6573 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6574 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6575 | clientId, callingPackageName, flags, sdk, |
| 6576 | forceFocusDuckingForAccessibility(aa, durationHint, Binder.getCallingUid())); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6577 | } |
| 6578 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6579 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa, |
| 6580 | String callingPackageName) { |
| 6581 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | public void unregisterAudioFocusClient(String clientId) { |
| 6585 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6586 | } |
| 6587 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 6588 | public int getCurrentAudioFocus() { |
| 6589 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 6590 | } |
| 6591 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 6592 | public int getFocusRampTimeMs(int focusGain, AudioAttributes attr) { |
| 6593 | return mMediaFocusControl.getFocusRampTimeMs(focusGain, attr); |
| 6594 | } |
| 6595 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6596 | //========================================================================================== |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6597 | private boolean readCameraSoundForced() { |
| 6598 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 6599 | mContext.getResources().getBoolean( |
| 6600 | com.android.internal.R.bool.config_camera_sound_forced); |
| 6601 | } |
| 6602 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6603 | //========================================================================================== |
| 6604 | // Device orientation |
| 6605 | //========================================================================================== |
| 6606 | /** |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 6607 | * Handles device configuration changes that may map to a change in rotation. |
| 6608 | * Monitoring rotation is optional, and is defined by the definition and value |
| 6609 | * of the "ro.audio.monitorRotation" system property. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6610 | */ |
| 6611 | private void handleConfigurationChanged(Context context) { |
| 6612 | try { |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 6613 | // reading new configuration "safely" (i.e. under try catch) in case anything |
| 6614 | // goes wrong. |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6615 | Configuration config = context.getResources().getConfiguration(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6616 | sendMsg(mAudioHandler, |
| 6617 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 6618 | SENDMSG_REPLACE, |
| 6619 | 0, |
| 6620 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6621 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6622 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6623 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6624 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6625 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6626 | final boolean cameraSoundForcedChanged = (cameraSoundForced != mCameraSoundForced); |
| 6627 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6628 | if (cameraSoundForcedChanged) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 6629 | if (!mIsSingleVolume) { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 6630 | synchronized (VolumeStreamState.class) { |
| 6631 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 6632 | if (cameraSoundForced) { |
| 6633 | s.setAllIndexesToMax(); |
| 6634 | mRingerModeAffectedStreams &= |
| 6635 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6636 | } else { |
| 6637 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
| 6638 | mRingerModeAffectedStreams |= |
| 6639 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6640 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6641 | } |
| 6642 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6643 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | sendMsg(mAudioHandler, |
| 6647 | MSG_SET_FORCE_USE, |
| 6648 | SENDMSG_QUEUE, |
| 6649 | AudioSystem.FOR_SYSTEM, |
| 6650 | cameraSoundForced ? |
| 6651 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6652 | new String("handleConfigurationChanged"), |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6653 | 0); |
| 6654 | |
| 6655 | sendMsg(mAudioHandler, |
| 6656 | MSG_SET_ALL_VOLUMES, |
| 6657 | SENDMSG_QUEUE, |
| 6658 | 0, |
| 6659 | 0, |
| 6660 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 6661 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6662 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6663 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6664 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 6665 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6666 | } |
| 6667 | } |
| 6668 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6669 | // Handles request to override default use of A2DP for media. |
| 6670 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6671 | public void setBluetoothA2dpOnInt(boolean on, String eventSource) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6672 | synchronized (mBluetoothA2dpEnabledLock) { |
| 6673 | mBluetoothA2dpEnabled = on; |
| 6674 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
| 6675 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6676 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 6677 | eventSource); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6678 | } |
| 6679 | } |
| 6680 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6681 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6682 | private void setForceUseInt_SyncDevices(int usage, int config, String eventSource) { |
Eric Laurent | 9a5b262 | 2017-04-18 18:20:56 -0700 | [diff] [blame] | 6683 | if (usage == AudioSystem.FOR_MEDIA) { |
| 6684 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6685 | SENDMSG_NOOP, 0, 0, null, 0); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6686 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6687 | mForceUseLogger.log(new ForceUseEvent(usage, config, eventSource)); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6688 | AudioSystem.setForceUse(usage, config); |
| 6689 | } |
| 6690 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6691 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 6692 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 6693 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 6694 | mRingtonePlayer = player; |
| 6695 | } |
| 6696 | |
| 6697 | @Override |
| 6698 | public IRingtonePlayer getRingtonePlayer() { |
| 6699 | return mRingtonePlayer; |
| 6700 | } |
| 6701 | |
| 6702 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6703 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 6704 | synchronized (mCurAudioRoutes) { |
| 6705 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 6706 | mRoutesObservers.register(observer); |
| 6707 | return routes; |
| 6708 | } |
| 6709 | } |
| 6710 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6711 | |
| 6712 | //========================================================================================== |
| 6713 | // Safe media volume management. |
| 6714 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 6715 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 6716 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 6717 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 6718 | // property. Platforms with a different limit must set this property accordingly in their |
| 6719 | // overlay. |
| 6720 | //========================================================================================== |
| 6721 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6722 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 6723 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 6724 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 6725 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 6726 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 6727 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6728 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 6729 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 6730 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 6731 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6732 | private Integer mSafeMediaVolumeState; |
| 6733 | |
| 6734 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6735 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6736 | private int mSafeMediaVolumeIndex; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 6737 | // mSafeUsbMediaVolumeDbfs is the cached value of the config_safe_media_volume_usb_mB |
| 6738 | // property, divided by 100.0. |
| 6739 | private float mSafeUsbMediaVolumeDbfs; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6740 | // mSafeUsbMediaVolumeIndex is used for USB Headsets and is the music volume UI index |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 6741 | // corresponding to a gain of mSafeUsbMediaVolumeDbfs (defaulting to -37dB) in audio |
| 6742 | // flinger mixer. |
Eric Laurent | 0e5deb3 | 2017-09-01 15:12:42 -0700 | [diff] [blame] | 6743 | // We remove -22 dBs from the theoretical -15dB to account for the EQ + bass boost |
| 6744 | // amplification when both effects are on with all band gains at maximum. |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6745 | // This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when |
| 6746 | // 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] | 6747 | private int mSafeUsbMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6748 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 6749 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6750 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 6751 | AudioSystem.DEVICE_OUT_USB_HEADSET; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6752 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 6753 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 6754 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 6755 | private int mMusicActiveMs; |
| 6756 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 6757 | 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] | 6758 | 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] | 6759 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6760 | private int safeMediaVolumeIndex(int device) { |
| 6761 | if ((device & mSafeMediaVolumeDevices) == 0) { |
| 6762 | return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 6763 | } |
| 6764 | if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 6765 | return mSafeUsbMediaVolumeIndex; |
| 6766 | } else { |
| 6767 | return mSafeMediaVolumeIndex; |
| 6768 | } |
| 6769 | } |
| 6770 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6771 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6772 | synchronized (mSafeMediaVolumeState) { |
| 6773 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 6774 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 6775 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 6776 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6777 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6778 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 6779 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 6780 | mMusicActiveMs = 1; // nonzero = confirmed |
| 6781 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6782 | sendMsg(mAudioHandler, |
| 6783 | MSG_CHECK_MUSIC_ACTIVE, |
| 6784 | SENDMSG_REPLACE, |
| 6785 | 0, |
| 6786 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6787 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6788 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 6789 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6790 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6791 | } |
| 6792 | } |
| 6793 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6794 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6795 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6796 | int devices = mSafeMediaVolumeDevices; |
| 6797 | int i = 0; |
| 6798 | |
| 6799 | while (devices != 0) { |
| 6800 | int device = 1 << i++; |
| 6801 | if ((device & devices) == 0) { |
| 6802 | continue; |
| 6803 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 6804 | int index = streamState.getIndex(device); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6805 | if (index > safeMediaVolumeIndex(device)) { |
| 6806 | streamState.setIndex(safeMediaVolumeIndex(device), device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 6807 | sendMsg(mAudioHandler, |
| 6808 | MSG_SET_DEVICE_VOLUME, |
| 6809 | SENDMSG_QUEUE, |
| 6810 | device, |
| 6811 | 0, |
| 6812 | streamState, |
| 6813 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6814 | } |
| 6815 | devices &= ~device; |
| 6816 | } |
| 6817 | } |
| 6818 | |
| 6819 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6820 | synchronized (mSafeMediaVolumeState) { |
| 6821 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6822 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 6823 | ((device & mSafeMediaVolumeDevices) != 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6824 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6825 | return false; |
| 6826 | } |
| 6827 | return true; |
| 6828 | } |
| 6829 | } |
| 6830 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6831 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6832 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6833 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6834 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6835 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 6836 | if (mPendingVolumeCommand != null) { |
| 6837 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 6838 | mPendingVolumeCommand.mIndex, |
| 6839 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6840 | mPendingVolumeCommand.mDevice, |
| 6841 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 6842 | mPendingVolumeCommand = null; |
| 6843 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6844 | } |
| 6845 | } |
| 6846 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6847 | //========================================================================================== |
| 6848 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 6849 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 6850 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6851 | //========================================================================================== |
| 6852 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6853 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 6854 | public void onComplete(int status) { |
| 6855 | if (mHdmiManager != null) { |
| 6856 | synchronized (mHdmiManager) { |
| 6857 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 6858 | // Television devices without CEC service apply software volume on HDMI output |
| 6859 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 6860 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 6861 | } |
| 6862 | checkAllFixedVolumeDevices(); |
| 6863 | } |
| 6864 | } |
| 6865 | } |
| 6866 | }; |
| 6867 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6868 | // If HDMI-CEC system audio is supported |
| 6869 | private boolean mHdmiSystemAudioSupported = false; |
| 6870 | // Set only when device is tv. |
| 6871 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 6872 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6873 | // cached HdmiControlManager interface |
| 6874 | private HdmiControlManager mHdmiManager; |
| 6875 | // Set only when device is a set-top box. |
| 6876 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 6877 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 6878 | private boolean mHdmiCecSink; |
| 6879 | |
| 6880 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6881 | |
| 6882 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 6883 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6884 | int device = AudioSystem.DEVICE_NONE; |
| 6885 | if (mHdmiManager != null) { |
| 6886 | synchronized (mHdmiManager) { |
| 6887 | if (mHdmiTvClient == null) { |
| 6888 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 6889 | return device; |
| 6890 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6891 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6892 | synchronized (mHdmiTvClient) { |
| 6893 | if (mHdmiSystemAudioSupported != on) { |
| 6894 | mHdmiSystemAudioSupported = on; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6895 | final int config = on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 6896 | AudioSystem.FORCE_NONE; |
| 6897 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 6898 | config, "setHdmiSystemAudioSupported")); |
| 6899 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, config); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6900 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 6901 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6902 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6903 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6904 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6905 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6906 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6907 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 6908 | @Override |
| 6909 | public boolean isHdmiSystemAudioSupported() { |
| 6910 | return mHdmiSystemAudioSupported; |
| 6911 | } |
| 6912 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6913 | //========================================================================================== |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6914 | // Accessibility |
| 6915 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6916 | private void initA11yMonitoring() { |
| 6917 | final AccessibilityManager accessibilityManager = |
| 6918 | (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6919 | updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled()); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 6920 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
Phil Weaver | 26d709f | 2017-04-20 17:19:14 -0700 | [diff] [blame] | 6921 | accessibilityManager.addTouchExplorationStateChangeListener(this, null); |
| 6922 | accessibilityManager.addAccessibilityServicesStateChangeListener(this, null); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | //--------------------------------------------------------------------------------- |
| 6926 | // A11y: taking touch exploration into account for selecting the default |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6927 | // stream override timeout when adjusting volume |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6928 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6929 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6930 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 6931 | // - STREAM_RING on phones during this period after a notification stopped |
| 6932 | // - STREAM_MUSIC otherwise |
| 6933 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6934 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
| 6935 | 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] | 6936 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6937 | private static int sStreamOverrideDelayMs; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6938 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6939 | @Override |
| 6940 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 6941 | updateDefaultStreamOverrideDelay(enabled); |
| 6942 | } |
| 6943 | |
| 6944 | private void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 6945 | if (touchExploreEnabled) { |
| 6946 | sStreamOverrideDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 6947 | } else { |
| 6948 | sStreamOverrideDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6949 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6950 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 6951 | + " stream override delay is now " + sStreamOverrideDelayMs + " ms"); |
| 6952 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6953 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6954 | //--------------------------------------------------------------------------------- |
| 6955 | // A11y: taking a11y state into account for the handling of a11y prompts volume |
| 6956 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6957 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6958 | private static boolean sIndependentA11yVolume = false; |
| 6959 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6960 | // implementation of AccessibilityServicesStateChangeListener |
| 6961 | @Override |
Phil Weaver | 4cab930 | 2017-03-30 15:27:39 -0700 | [diff] [blame] | 6962 | public void onAccessibilityServicesStateChanged(AccessibilityManager accessibilityManager) { |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6963 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
| 6964 | } |
| 6965 | |
| 6966 | private void updateA11yVolumeAlias(boolean a11VolEnabled) { |
| 6967 | if (DEBUG_VOL) Log.d(TAG, "Accessibility volume enabled = " + a11VolEnabled); |
| 6968 | if (sIndependentA11yVolume != a11VolEnabled) { |
| 6969 | sIndependentA11yVolume = a11VolEnabled; |
| 6970 | // update the volume mapping scheme |
| 6971 | updateStreamVolumeAlias(true /*updateVolumes*/, TAG); |
| 6972 | // update the volume controller behavior |
| 6973 | mVolumeController.setA11yMode(sIndependentA11yVolume ? |
| 6974 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 6975 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 6976 | mVolumeController.postVolumeChanged(AudioManager.STREAM_ACCESSIBILITY, 0); |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 6977 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6981 | // Camera shutter sound policy. |
| 6982 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 6983 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 6984 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 6985 | //========================================================================================== |
| 6986 | |
| 6987 | // 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] | 6988 | @GuardedBy("mSettingsLock") |
| 6989 | private boolean mCameraSoundForced; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6990 | |
| 6991 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 6992 | public boolean isCameraSoundForced() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6993 | synchronized (mSettingsLock) { |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6994 | return mCameraSoundForced; |
| 6995 | } |
| 6996 | } |
| 6997 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6998 | //========================================================================================== |
| 6999 | // AudioService logging and dumpsys |
| 7000 | //========================================================================================== |
| 7001 | final int LOG_NB_EVENTS_PHONE_STATE = 20; |
| 7002 | final int LOG_NB_EVENTS_WIRED_DEV_CONNECTION = 30; |
| 7003 | final int LOG_NB_EVENTS_FORCE_USE = 20; |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7004 | final int LOG_NB_EVENTS_VOLUME = 40; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7005 | |
| 7006 | final private AudioEventLogger mModeLogger = new AudioEventLogger(LOG_NB_EVENTS_PHONE_STATE, |
| 7007 | "phone state (logged after successfull call to AudioSystem.setPhoneState(int))"); |
| 7008 | |
| 7009 | final private AudioEventLogger mWiredDevLogger = new AudioEventLogger( |
| 7010 | LOG_NB_EVENTS_WIRED_DEV_CONNECTION, |
| 7011 | "wired device connection (logged before onSetWiredDeviceConnectionState() is executed)" |
| 7012 | ); |
| 7013 | |
| 7014 | final private AudioEventLogger mForceUseLogger = new AudioEventLogger( |
| 7015 | LOG_NB_EVENTS_FORCE_USE, |
| 7016 | "force use (logged before setForceUse() is executed)"); |
| 7017 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7018 | final private AudioEventLogger mVolumeLogger = new AudioEventLogger(LOG_NB_EVENTS_VOLUME, |
| 7019 | "volume changes (logged when command received by AudioService)"); |
| 7020 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7021 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 7022 | "SILENT", |
| 7023 | "VIBRATE", |
| 7024 | "NORMAL" |
| 7025 | }; |
| 7026 | |
| 7027 | private void dumpRingerMode(PrintWriter pw) { |
| 7028 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7029 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 7030 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7031 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7032 | dumpRingerModeStreams(pw, "muted", mRingerAndZenModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7033 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7034 | } |
| 7035 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7036 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 7037 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 7038 | pw.print(Integer.toHexString(streams)); |
| 7039 | if (streams != 0) { |
| 7040 | pw.print(" ("); |
| 7041 | boolean first = true; |
| 7042 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 7043 | final int stream = (1 << i); |
| 7044 | if ((streams & stream) != 0) { |
| 7045 | if (!first) pw.print(','); |
| 7046 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 7047 | streams &= ~stream; |
| 7048 | first = false; |
| 7049 | } |
| 7050 | } |
| 7051 | if (streams != 0) { |
| 7052 | if (!first) pw.print(','); |
| 7053 | pw.print(streams); |
| 7054 | } |
| 7055 | pw.print(')'); |
| 7056 | } |
| 7057 | pw.println(); |
| 7058 | } |
| 7059 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 7060 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 7061 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 7062 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 7063 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 7064 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 7065 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7066 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 7067 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 7068 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 7069 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7070 | |
| 7071 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7072 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7073 | pw.print(" mSafeMediaVolumeState="); |
| 7074 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 7075 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 7076 | pw.print(" mSafeUsbMediaVolumeIndex="); pw.println(mSafeUsbMediaVolumeIndex); |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 7077 | pw.print(" mSafeUsbMediaVolumeDbfs="); pw.println(mSafeUsbMediaVolumeDbfs); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 7078 | pw.print(" sIndependentA11yVolume="); pw.println(sIndependentA11yVolume); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7079 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 7080 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 7081 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 7082 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7083 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7084 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 7085 | pw.print(" mAvrcpAbsVolSupported="); pw.println(mAvrcpAbsVolSupported); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7086 | |
| 7087 | dumpAudioPolicies(pw); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7088 | |
| 7089 | mPlaybackMonitor.dump(pw); |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7090 | |
| 7091 | mRecordMonitor.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7092 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7093 | pw.println("\n"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7094 | pw.println("\nEvent logs:"); |
| 7095 | mModeLogger.dump(pw); |
| 7096 | pw.println("\n"); |
| 7097 | mWiredDevLogger.dump(pw); |
| 7098 | pw.println("\n"); |
| 7099 | mForceUseLogger.dump(pw); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7100 | pw.println("\n"); |
| 7101 | mVolumeLogger.dump(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | private static String safeMediaVolumeStateToString(Integer state) { |
| 7105 | switch(state) { |
| 7106 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 7107 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 7108 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 7109 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 7110 | } |
| 7111 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 7112 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 7113 | |
| 7114 | // Inform AudioFlinger of our device's low RAM attribute |
| 7115 | private static void readAndSetLowRamDevice() |
| 7116 | { |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 7117 | boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic(); |
| 7118 | long totalMemory = 1024 * 1024 * 1024; // 1GB is the default if ActivityManager fails. |
| 7119 | |
| 7120 | try { |
| 7121 | final ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); |
| 7122 | ActivityManager.getService().getMemoryInfo(info); |
| 7123 | totalMemory = info.totalMem; |
| 7124 | } catch (RemoteException e) { |
| 7125 | Log.w(TAG, "Cannot obtain MemoryInfo from ActivityManager, assume low memory device"); |
| 7126 | isLowRamDevice = true; |
| 7127 | } |
| 7128 | |
| 7129 | final int status = AudioSystem.setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 7130 | if (status != 0) { |
| 7131 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 7132 | } |
| 7133 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7134 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7135 | private void enforceVolumeController(String action) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7136 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 7137 | "Only SystemUI can " + action); |
| 7138 | } |
| 7139 | |
| 7140 | @Override |
| 7141 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7142 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7143 | |
| 7144 | // return early if things are not actually changing |
| 7145 | if (mVolumeController.isSameBinder(controller)) { |
| 7146 | return; |
| 7147 | } |
| 7148 | |
| 7149 | // dismiss the old volume controller |
| 7150 | mVolumeController.postDismiss(); |
| 7151 | if (controller != null) { |
| 7152 | // we are about to register a new controller, listen for its death |
| 7153 | try { |
| 7154 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 7155 | @Override |
| 7156 | public void binderDied() { |
| 7157 | if (mVolumeController.isSameBinder(controller)) { |
| 7158 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 7159 | setVolumeController(null); |
| 7160 | } |
| 7161 | } |
| 7162 | }, 0); |
| 7163 | } catch (RemoteException e) { |
| 7164 | // noop |
| 7165 | } |
| 7166 | } |
| 7167 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7168 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 7169 | } |
| 7170 | |
| 7171 | @Override |
| 7172 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7173 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7174 | |
| 7175 | // return early if the controller is not current |
| 7176 | if (!mVolumeController.isSameBinder(controller)) { |
| 7177 | return; |
| 7178 | } |
| 7179 | |
| 7180 | mVolumeController.setVisible(visible); |
| 7181 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7182 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7183 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7184 | @Override |
| 7185 | public void setVolumePolicy(VolumePolicy policy) { |
| 7186 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 7187 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7188 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 7189 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7190 | } |
| 7191 | } |
| 7192 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7193 | public static class VolumeController { |
| 7194 | private static final String TAG = "VolumeController"; |
| 7195 | |
| 7196 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7197 | private boolean mVisible; |
| 7198 | private long mNextLongPress; |
| 7199 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7200 | |
| 7201 | public void setController(IVolumeController controller) { |
| 7202 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7203 | mVisible = false; |
| 7204 | } |
| 7205 | |
| 7206 | public void loadSettings(ContentResolver cr) { |
| 7207 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 7208 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 7209 | } |
| 7210 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 7211 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 7212 | if (isMute) { |
| 7213 | return false; |
| 7214 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7215 | boolean suppress = false; |
| 7216 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 7217 | final long now = SystemClock.uptimeMillis(); |
| 7218 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 7219 | // ui will become visible |
| 7220 | if (mNextLongPress < now) { |
| 7221 | mNextLongPress = now + mLongPressTimeout; |
| 7222 | } |
| 7223 | suppress = true; |
| 7224 | } else if (mNextLongPress > 0) { // in a long-press |
| 7225 | if (now > mNextLongPress) { |
| 7226 | // long press triggered, no more suppression |
| 7227 | mNextLongPress = 0; |
| 7228 | } else { |
| 7229 | // keep suppressing until the long press triggers |
| 7230 | suppress = true; |
| 7231 | } |
| 7232 | } |
| 7233 | } |
| 7234 | return suppress; |
| 7235 | } |
| 7236 | |
| 7237 | public void setVisible(boolean visible) { |
| 7238 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | public boolean isSameBinder(IVolumeController controller) { |
| 7242 | return Objects.equals(asBinder(), binder(controller)); |
| 7243 | } |
| 7244 | |
| 7245 | public IBinder asBinder() { |
| 7246 | return binder(mController); |
| 7247 | } |
| 7248 | |
| 7249 | private static IBinder binder(IVolumeController controller) { |
| 7250 | return controller == null ? null : controller.asBinder(); |
| 7251 | } |
| 7252 | |
| 7253 | @Override |
| 7254 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7255 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | public void postDisplaySafeVolumeWarning(int flags) { |
| 7259 | if (mController == null) |
| 7260 | return; |
| 7261 | try { |
| 7262 | mController.displaySafeVolumeWarning(flags); |
| 7263 | } catch (RemoteException e) { |
| 7264 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 7265 | } |
| 7266 | } |
| 7267 | |
| 7268 | public void postVolumeChanged(int streamType, int flags) { |
| 7269 | if (mController == null) |
| 7270 | return; |
| 7271 | try { |
| 7272 | mController.volumeChanged(streamType, flags); |
| 7273 | } catch (RemoteException e) { |
| 7274 | Log.w(TAG, "Error calling volumeChanged", e); |
| 7275 | } |
| 7276 | } |
| 7277 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7278 | public void postMasterMuteChanged(int flags) { |
| 7279 | if (mController == null) |
| 7280 | return; |
| 7281 | try { |
| 7282 | mController.masterMuteChanged(flags); |
| 7283 | } catch (RemoteException e) { |
| 7284 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 7285 | } |
| 7286 | } |
| 7287 | |
| 7288 | public void setLayoutDirection(int layoutDirection) { |
| 7289 | if (mController == null) |
| 7290 | return; |
| 7291 | try { |
| 7292 | mController.setLayoutDirection(layoutDirection); |
| 7293 | } catch (RemoteException e) { |
| 7294 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | public void postDismiss() { |
| 7299 | if (mController == null) |
| 7300 | return; |
| 7301 | try { |
| 7302 | mController.dismiss(); |
| 7303 | } catch (RemoteException e) { |
| 7304 | Log.w(TAG, "Error calling dismiss", e); |
| 7305 | } |
| 7306 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7307 | |
| 7308 | public void setA11yMode(int a11yMode) { |
| 7309 | if (mController == null) |
| 7310 | return; |
| 7311 | try { |
| 7312 | mController.setA11yMode(a11yMode); |
| 7313 | } catch (RemoteException e) { |
| 7314 | Log.w(TAG, "Error calling setA11Mode", e); |
| 7315 | } |
| 7316 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7317 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7318 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7319 | /** |
| 7320 | * Interface for system components to get some extra functionality through |
| 7321 | * LocalServices. |
| 7322 | */ |
| 7323 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7324 | @Override |
| 7325 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 7326 | mRingerModeDelegate = delegate; |
| 7327 | if (mRingerModeDelegate != null) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 7328 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7329 | updateRingerAndZenModeAffectedStreams(); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 7330 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7331 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 7332 | } |
| 7333 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 7334 | |
| 7335 | @Override |
| 7336 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 7337 | String callingPackage, int uid) { |
| 7338 | // direction and stream type swap here because the public |
| 7339 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7340 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 7341 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 7342 | } |
| 7343 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7344 | @Override |
| 7345 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 7346 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7347 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 7348 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7349 | } |
| 7350 | |
| 7351 | @Override |
| 7352 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 7353 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7354 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7355 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 7356 | |
| 7357 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7358 | public int getRingerModeInternal() { |
| 7359 | return AudioService.this.getRingerModeInternal(); |
| 7360 | } |
| 7361 | |
| 7362 | @Override |
| 7363 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 7364 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 7365 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7366 | |
| 7367 | @Override |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 7368 | public void silenceRingerModeInternal(String caller) { |
| 7369 | AudioService.this.silenceRingerModeInternal(caller); |
| 7370 | } |
| 7371 | |
| 7372 | @Override |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7373 | public void updateRingerModeAffectedStreamsInternal() { |
| 7374 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7375 | if (updateRingerAndZenModeAffectedStreams()) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7376 | setRingerModeInt(getRingerModeInternal(), false); |
| 7377 | } |
| 7378 | } |
| 7379 | } |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 7380 | |
| 7381 | @Override |
| 7382 | public void setAccessibilityServiceUids(IntArray uids) { |
| 7383 | synchronized (mAccessibilityServiceUidsLock) { |
| 7384 | if (uids.size() == 0) { |
| 7385 | mAccessibilityServiceUids = null; |
| 7386 | } else { |
| 7387 | boolean changed = (mAccessibilityServiceUids == null) |
| 7388 | || (mAccessibilityServiceUids.length != uids.size()); |
| 7389 | if (!changed) { |
| 7390 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 7391 | if (uids.get(i) != mAccessibilityServiceUids[i]) { |
| 7392 | changed = true; |
| 7393 | break; |
| 7394 | } |
| 7395 | } |
| 7396 | } |
| 7397 | if (changed) { |
| 7398 | mAccessibilityServiceUids = uids.toArray(); |
| 7399 | } |
| 7400 | } |
| 7401 | } |
| 7402 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7403 | } |
| 7404 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7405 | //========================================================================================== |
| 7406 | // Audio policy management |
| 7407 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7408 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7409 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7410 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 7411 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7412 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 7413 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7414 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7415 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7416 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7417 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7418 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7419 | if (!hasPermissionForPolicy) { |
| 7420 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 7421 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7422 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7423 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7424 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7425 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7426 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7427 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7428 | Slog.e(TAG, "Cannot re-register policy"); |
| 7429 | return null; |
| 7430 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7431 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7432 | isFocusPolicy, isVolumeController); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7433 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 7434 | regId = app.getRegistrationId(); |
| 7435 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7436 | } catch (RemoteException e) { |
| 7437 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7438 | 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] | 7439 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7440 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7441 | } |
| 7442 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7443 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7444 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7445 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7446 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 7447 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7448 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7449 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7450 | if (app == null) { |
| 7451 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 7452 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7453 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7454 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7455 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7456 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7457 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7458 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7459 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7460 | } |
| 7461 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7462 | /** |
| 7463 | * Checks whether caller has MODIFY_AUDIO_ROUTING permission, and the policy is registered. |
| 7464 | * @param errorMsg log warning if permission check failed. |
| 7465 | * @return null if the operation on the audio mixes should be cancelled. |
| 7466 | */ |
| 7467 | @GuardedBy("mAudioPolicies") |
| 7468 | private AudioPolicyProxy checkUpdateForPolicy(IAudioPolicyCallback pcb, String errorMsg) { |
| 7469 | // permission check |
| 7470 | final boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7471 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7472 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7473 | if (!hasPermissionForPolicy) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7474 | Slog.w(TAG, errorMsg + " for pid " + |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7475 | + Binder.getCallingPid() + " / uid " |
| 7476 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7477 | return null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7478 | } |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7479 | // policy registered? |
| 7480 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 7481 | if (app == null) { |
| 7482 | Slog.w(TAG, errorMsg + " for pid " + |
| 7483 | + Binder.getCallingPid() + " / uid " |
| 7484 | + Binder.getCallingUid() + ", unregistered policy"); |
| 7485 | return null; |
| 7486 | } |
| 7487 | return app; |
| 7488 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7489 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7490 | public int addMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 7491 | if (DEBUG_AP) { Log.d(TAG, "addMixForPolicy for " + pcb.asBinder() |
| 7492 | + " with config:" + policyConfig); } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7493 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7494 | final AudioPolicyProxy app = |
| 7495 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 7496 | if (app == null){ |
| 7497 | return AudioManager.ERROR; |
| 7498 | } |
| 7499 | app.addMixes(policyConfig.getMixes()); |
| 7500 | } |
| 7501 | return AudioManager.SUCCESS; |
| 7502 | } |
| 7503 | |
| 7504 | public int removeMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 7505 | if (DEBUG_AP) { Log.d(TAG, "removeMixForPolicy for " + pcb.asBinder() |
| 7506 | + " with config:" + policyConfig); } |
| 7507 | synchronized (mAudioPolicies) { |
| 7508 | final AudioPolicyProxy app = |
| 7509 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 7510 | if (app == null) { |
| 7511 | return AudioManager.ERROR; |
| 7512 | } |
| 7513 | app.removeMixes(policyConfig.getMixes()); |
| 7514 | } |
| 7515 | return AudioManager.SUCCESS; |
| 7516 | } |
| 7517 | |
| 7518 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 7519 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 7520 | + " policy " + pcb.asBinder()); |
| 7521 | synchronized (mAudioPolicies) { |
| 7522 | final AudioPolicyProxy app = |
| 7523 | checkUpdateForPolicy(pcb, "Cannot change audio policy focus properties"); |
| 7524 | if (app == null){ |
| 7525 | return AudioManager.ERROR; |
| 7526 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7527 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 7528 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 7529 | return AudioManager.ERROR; |
| 7530 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7531 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7532 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 7533 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7534 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7535 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 7536 | return AudioManager.ERROR; |
| 7537 | } |
| 7538 | } |
| 7539 | } |
| 7540 | app.mFocusDuckBehavior = duckingBehavior; |
| 7541 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 7542 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 7543 | } |
| 7544 | return AudioManager.SUCCESS; |
| 7545 | } |
| 7546 | |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7547 | private final Object mExtVolumeControllerLock = new Object(); |
| 7548 | private IAudioPolicyCallback mExtVolumeController; |
| 7549 | private void setExtVolumeController(IAudioPolicyCallback apc) { |
| 7550 | if (!mContext.getResources().getBoolean( |
| 7551 | com.android.internal.R.bool.config_handleVolumeKeysInWindowManager)) { |
| 7552 | Log.e(TAG, "Cannot set external volume controller: device not set for volume keys" + |
| 7553 | " handled in PhoneWindowManager"); |
| 7554 | return; |
| 7555 | } |
| 7556 | synchronized (mExtVolumeControllerLock) { |
| 7557 | if (mExtVolumeController != null && !mExtVolumeController.asBinder().pingBinder()) { |
| 7558 | Log.e(TAG, "Cannot set external volume controller: existing controller"); |
| 7559 | } |
| 7560 | mExtVolumeController = apc; |
| 7561 | } |
| 7562 | } |
| 7563 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7564 | private void dumpAudioPolicies(PrintWriter pw) { |
| 7565 | pw.println("\nAudio policies:"); |
| 7566 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 7567 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7568 | pw.println(policy.toLogFriendlyString()); |
| 7569 | } |
| 7570 | } |
| 7571 | } |
| 7572 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7573 | //====================== |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7574 | // Audio policy callbacks from AudioSystem for dynamic policies |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7575 | //====================== |
| 7576 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 7577 | new AudioSystem.DynamicPolicyCallback() { |
| 7578 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 7579 | if (!TextUtils.isEmpty(regId)) { |
| 7580 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 7581 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 7582 | } |
| 7583 | } |
| 7584 | }; |
| 7585 | |
| 7586 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 7587 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 7588 | synchronized (mAudioPolicies) { |
| 7589 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 7590 | for (AudioMix mix : policy.getMixes()) { |
| 7591 | if (mix.getRegistration().equals(regId)) { |
| 7592 | try { |
| 7593 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 7594 | } catch (RemoteException e) { |
| 7595 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 7596 | + policy.mPolicyCallback.asBinder(), e); |
| 7597 | } |
| 7598 | return; |
| 7599 | } |
| 7600 | } |
| 7601 | } |
| 7602 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7603 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7604 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7605 | //====================== |
| 7606 | // Audio policy callbacks from AudioSystem for recording configuration updates |
| 7607 | //====================== |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7608 | private final RecordingActivityMonitor mRecordMonitor; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7609 | |
| 7610 | public void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7611 | final boolean isPrivileged = |
| 7612 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7613 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7614 | mRecordMonitor.registerRecordingCallback(rcdb, isPrivileged); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7615 | } |
| 7616 | |
| 7617 | public void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 7618 | mRecordMonitor.unregisterRecordingCallback(rcdb); |
| 7619 | } |
| 7620 | |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 7621 | public List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7622 | final boolean isPrivileged = |
| 7623 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7624 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7625 | return mRecordMonitor.getActiveRecordingConfigurations(isPrivileged); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7626 | } |
| 7627 | |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7628 | public void disableRingtoneSync(final int userId) { |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7629 | final int callingUserId = UserHandle.getCallingUserId(); |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7630 | if (callingUserId != userId) { |
| 7631 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 7632 | "disable sound settings syncing for another profile"); |
| 7633 | } |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7634 | final long token = Binder.clearCallingIdentity(); |
| 7635 | try { |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7636 | // Disable the sync setting so the profile uses its own sound settings. |
| 7637 | Settings.Secure.putIntForUser(mContentResolver, Settings.Secure.SYNC_PARENT_SOUNDS, |
| 7638 | 0 /* false */, userId); |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7639 | } finally { |
| 7640 | Binder.restoreCallingIdentity(token); |
| 7641 | } |
| 7642 | } |
| 7643 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7644 | //====================== |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7645 | // Audio playback notification |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7646 | //====================== |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 7647 | private final PlaybackActivityMonitor mPlaybackMonitor; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7648 | |
| 7649 | public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7650 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7651 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7652 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7653 | mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged); |
| 7654 | } |
| 7655 | |
| 7656 | public void unregisterPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
| 7657 | mPlaybackMonitor.unregisterPlaybackCallback(pcdb); |
| 7658 | } |
| 7659 | |
| 7660 | public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7661 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7662 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7663 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7664 | return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7665 | } |
| 7666 | |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7667 | public int trackPlayer(PlayerBase.PlayerIdCard pic) { |
| 7668 | return mPlaybackMonitor.trackPlayer(pic); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | public void playerAttributes(int piid, AudioAttributes attr) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7672 | mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7673 | } |
| 7674 | |
| 7675 | public void playerEvent(int piid, int event) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7676 | mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7677 | } |
| 7678 | |
Jean-Michel Trivi | 3120059d | 2017-08-28 12:40:55 -0700 | [diff] [blame] | 7679 | public void playerHasOpPlayAudio(int piid, boolean hasOpPlayAudio) { |
| 7680 | mPlaybackMonitor.playerHasOpPlayAudio(piid, hasOpPlayAudio, Binder.getCallingUid()); |
| 7681 | } |
| 7682 | |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7683 | public void releasePlayer(int piid) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7684 | mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7685 | } |
| 7686 | |
| 7687 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7688 | // Audio policy proxy |
| 7689 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7690 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7691 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 7692 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7693 | */ |
| 7694 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7695 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7696 | final IAudioPolicyCallback mPolicyCallback; |
| 7697 | final boolean mHasFocusListener; |
| 7698 | final boolean mIsVolumeController; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7699 | /** |
| 7700 | * Audio focus ducking behavior for an audio policy. |
| 7701 | * This variable reflects the value that was successfully set in |
| 7702 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 7703 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 7704 | * is handling ducking for audio focus. |
| 7705 | */ |
| 7706 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7707 | boolean mIsFocusPolicy = false; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7708 | |
| 7709 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7710 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7711 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7712 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7713 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7714 | mHasFocusListener = hasFocusListener; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7715 | mIsVolumeController = isVolumeController; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7716 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7717 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7718 | // can only ever be true if there is a focus listener |
| 7719 | if (isFocusPolicy) { |
| 7720 | mIsFocusPolicy = true; |
| 7721 | mMediaFocusControl.setFocusPolicy(mPolicyCallback); |
| 7722 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7723 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7724 | if (mIsVolumeController) { |
| 7725 | setExtVolumeController(mPolicyCallback); |
| 7726 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7727 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | public void binderDied() { |
| 7731 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7732 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7733 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7734 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7735 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7736 | if (mIsVolumeController) { |
| 7737 | synchronized (mExtVolumeControllerLock) { |
| 7738 | mExtVolumeController = null; |
| 7739 | } |
| 7740 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7741 | } |
| 7742 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7743 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7744 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7745 | } |
| 7746 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7747 | void release() { |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7748 | if (mIsFocusPolicy) { |
| 7749 | mMediaFocusControl.unsetFocusPolicy(mPolicyCallback); |
| 7750 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7751 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7752 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 7753 | } |
| 7754 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7755 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7756 | } |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7757 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7758 | AudioSystem.registerPolicyMixes(mMixes, false); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7759 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7760 | } |
| 7761 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 7762 | boolean hasMixAffectingUsage(int usage) { |
| 7763 | for (AudioMix mix : mMixes) { |
| 7764 | if (mix.isAffectingUsage(usage)) { |
| 7765 | return true; |
| 7766 | } |
| 7767 | } |
| 7768 | return false; |
| 7769 | } |
| 7770 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7771 | void addMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 7772 | // TODO optimize to not have to unregister the mixes already in place |
| 7773 | synchronized (mMixes) { |
| 7774 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 7775 | this.add(mixes); |
| 7776 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | void removeMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 7781 | // TODO optimize to not have to unregister the mixes already in place |
| 7782 | synchronized (mMixes) { |
| 7783 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 7784 | this.remove(mixes); |
| 7785 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 7786 | } |
| 7787 | } |
| 7788 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7789 | void connectMixes() { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7790 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7791 | AudioSystem.registerPolicyMixes(mMixes, true); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7792 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7793 | } |
| 7794 | }; |
| 7795 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7796 | //====================== |
| 7797 | // Audio policy: focus |
| 7798 | //====================== |
| 7799 | /** */ |
| 7800 | public int dispatchFocusChange(AudioFocusInfo afi, int focusChange, IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 7801 | if (afi == null) { |
| 7802 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 7803 | } |
| 7804 | if (pcb == null) { |
| 7805 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 7806 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7807 | synchronized (mAudioPolicies) { |
| 7808 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 7809 | throw new IllegalStateException("Unregistered AudioPolicy for focus dispatch"); |
| 7810 | } |
| 7811 | return mMediaFocusControl.dispatchFocusChange(afi, focusChange); |
| 7812 | } |
| 7813 | } |
| 7814 | |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 7815 | public void setFocusRequestResultFromExtPolicy(AudioFocusInfo afi, int requestResult, |
| 7816 | IAudioPolicyCallback pcb) { |
| 7817 | if (afi == null) { |
| 7818 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 7819 | } |
| 7820 | if (pcb == null) { |
| 7821 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 7822 | } |
| 7823 | synchronized (mAudioPolicies) { |
| 7824 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 7825 | throw new IllegalStateException("Unregistered AudioPolicy for external focus"); |
| 7826 | } |
| 7827 | mMediaFocusControl.setFocusRequestResultFromExtPolicy(afi, requestResult); |
| 7828 | } |
| 7829 | } |
| 7830 | |
| 7831 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7832 | //====================== |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 7833 | // Audioserver state displatch |
| 7834 | //====================== |
| 7835 | private class AsdProxy implements IBinder.DeathRecipient { |
| 7836 | private final IAudioServerStateDispatcher mAsd; |
| 7837 | |
| 7838 | AsdProxy(IAudioServerStateDispatcher asd) { |
| 7839 | mAsd = asd; |
| 7840 | } |
| 7841 | |
| 7842 | public void binderDied() { |
| 7843 | synchronized (mAudioServerStateListeners) { |
| 7844 | mAudioServerStateListeners.remove(mAsd.asBinder()); |
| 7845 | } |
| 7846 | } |
| 7847 | |
| 7848 | IAudioServerStateDispatcher callback() { |
| 7849 | return mAsd; |
| 7850 | } |
| 7851 | } |
| 7852 | |
| 7853 | private HashMap<IBinder, AsdProxy> mAudioServerStateListeners = |
| 7854 | new HashMap<IBinder, AsdProxy>(); |
| 7855 | |
| 7856 | private void checkMonitorAudioServerStatePermission() { |
| 7857 | if (!(mContext.checkCallingOrSelfPermission( |
| 7858 | android.Manifest.permission.MODIFY_PHONE_STATE) == |
| 7859 | PackageManager.PERMISSION_GRANTED || |
| 7860 | mContext.checkCallingOrSelfPermission( |
| 7861 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) == |
| 7862 | PackageManager.PERMISSION_GRANTED)) { |
| 7863 | throw new SecurityException("Not allowed to monitor audioserver state"); |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | public void registerAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 7868 | checkMonitorAudioServerStatePermission(); |
| 7869 | synchronized (mAudioServerStateListeners) { |
| 7870 | if (mAudioServerStateListeners.containsKey(asd.asBinder())) { |
| 7871 | Slog.w(TAG, "Cannot re-register audio server state dispatcher"); |
| 7872 | return; |
| 7873 | } |
| 7874 | AsdProxy asdp = new AsdProxy(asd); |
| 7875 | try { |
| 7876 | asd.asBinder().linkToDeath(asdp, 0/*flags*/); |
| 7877 | } catch (RemoteException e) { |
| 7878 | |
| 7879 | } |
| 7880 | mAudioServerStateListeners.put(asd.asBinder(), asdp); |
| 7881 | } |
| 7882 | } |
| 7883 | |
| 7884 | public void unregisterAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 7885 | checkMonitorAudioServerStatePermission(); |
| 7886 | synchronized (mAudioServerStateListeners) { |
| 7887 | AsdProxy asdp = mAudioServerStateListeners.remove(asd.asBinder()); |
| 7888 | if (asdp == null) { |
| 7889 | Slog.w(TAG, "Trying to unregister unknown audioserver state dispatcher for pid " |
| 7890 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
| 7891 | return; |
| 7892 | } else { |
| 7893 | asd.asBinder().unlinkToDeath(asdp, 0/*flags*/); |
| 7894 | } |
| 7895 | } |
| 7896 | } |
| 7897 | |
| 7898 | public boolean isAudioServerRunning() { |
| 7899 | checkMonitorAudioServerStatePermission(); |
| 7900 | return (AudioSystem.checkAudioFlinger() == AudioSystem.AUDIO_STATUS_OK); |
| 7901 | } |
| 7902 | |
| 7903 | //====================== |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7904 | // misc |
| 7905 | //====================== |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7906 | private final HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7907 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7908 | @GuardedBy("mAudioPolicies") |
| 7909 | private int mAudioPolicyCounter = 0; |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 7910 | } |