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 | |
Hongwei Wang | daba124 | 2018-05-29 14:36:16 -0700 | [diff] [blame] | 218 | private boolean isPlatformAutomotive() { |
| 219 | return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); |
| 220 | } |
| 221 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 222 | /** The controller for the volume UI. */ |
| 223 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
| 225 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | /** If the msg is already queued, replace it with this one. */ |
| 227 | private static final int SENDMSG_REPLACE = 0; |
| 228 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 229 | private static final int SENDMSG_NOOP = 1; |
| 230 | /** If the msg is already queued, queue this one and leave the old. */ |
| 231 | private static final int SENDMSG_QUEUE = 2; |
| 232 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 233 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 234 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | private static final int MSG_PERSIST_VOLUME = 1; |
| 236 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 237 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 238 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 239 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 240 | private static final int MSG_SET_FORCE_USE = 8; |
| 241 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 242 | private static final int MSG_SET_ALL_VOLUMES = 10; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 243 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 244 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 245 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 246 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 247 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 248 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 249 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 250 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 251 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 252 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 253 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 254 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 255 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 256 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 257 | private static final int MSG_ACCESSORY_PLUG_MEDIA_UNMUTE = 27; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 258 | private static final int MSG_NOTIFY_VOL_EVENT = 28; |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 259 | private static final int MSG_DISPATCH_AUDIO_SERVER_STATE = 29; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 260 | private static final int MSG_ENABLE_SURROUND_FORMATS = 30; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 261 | // start of messages handled under wakelock |
| 262 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 263 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 264 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 265 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 266 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 267 | private static final int MSG_A2DP_DEVICE_CONFIG_CHANGE = 103; |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 268 | private static final int MSG_DISABLE_AUDIO_FOR_UID = 104; |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 269 | private static final int MSG_SET_HEARING_AID_CONNECTION_STATE = 105; |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 270 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 106; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 271 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 272 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 273 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 274 | // Timeout for connection to bluetooth headset service |
| 275 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 276 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 277 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 278 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 279 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 280 | private static final int BT_HEARING_AID_GAIN_MIN = -128; |
| 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | /** @see AudioSystemThread */ |
| 283 | private AudioSystemThread mAudioSystemThread; |
| 284 | /** @see AudioHandler */ |
| 285 | private AudioHandler mAudioHandler; |
| 286 | /** @see VolumeStreamState */ |
| 287 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 288 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 289 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 290 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 291 | // protects mRingerMode |
| 292 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 295 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
| 298 | /* Sound effect file names */ |
| 299 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 300 | 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] | 301 | |
| 302 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 303 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 304 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 305 | 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] | 306 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 307 | /** Maximum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 308 | protected static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 309 | 5, // STREAM_VOICE_CALL |
| 310 | 7, // STREAM_SYSTEM |
| 311 | 7, // STREAM_RING |
| 312 | 15, // STREAM_MUSIC |
| 313 | 7, // STREAM_ALARM |
| 314 | 7, // STREAM_NOTIFICATION |
| 315 | 15, // STREAM_BLUETOOTH_SCO |
| 316 | 7, // STREAM_SYSTEM_ENFORCED |
| 317 | 15, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 318 | 15, // STREAM_TTS |
| 319 | 15 // STREAM_ACCESSIBILITY |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 320 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 321 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 322 | /** Minimum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 323 | protected static int[] MIN_STREAM_VOLUME = new int[] { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 324 | 1, // STREAM_VOICE_CALL |
| 325 | 0, // STREAM_SYSTEM |
| 326 | 0, // STREAM_RING |
| 327 | 0, // STREAM_MUSIC |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 328 | 1, // STREAM_ALARM |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 329 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 330 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 331 | 0, // STREAM_SYSTEM_ENFORCED |
| 332 | 0, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 333 | 0, // STREAM_TTS |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 334 | 1 // STREAM_ACCESSIBILITY |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 335 | }; |
| 336 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 337 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 338 | * of another stream: This avoids multiplying the volume settings for hidden |
| 339 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 340 | * NOTE: do not create loops in aliases! |
| 341 | * 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] | 342 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 343 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 344 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 345 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 346 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 347 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 348 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 349 | AudioSystem.STREAM_RING, // STREAM_RING |
| 350 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 351 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 352 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 353 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 354 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 355 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 356 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 357 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 358 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 359 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 360 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 361 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 362 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 363 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 364 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 365 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 366 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 367 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 368 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 369 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 370 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 371 | }; |
| 372 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 373 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 374 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 375 | AudioSystem.STREAM_RING, // STREAM_RING |
| 376 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 377 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 378 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 379 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 380 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 381 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 382 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 383 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 384 | }; |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 385 | protected static int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 386 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 389 | * after mapping through mStreamVolumeAlias. |
| 390 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 391 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 392 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 393 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 394 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 395 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 396 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 397 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 398 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 399 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 400 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 401 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 402 | AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME, // STREAM_ACCESSIBILITY |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 403 | }; |
| 404 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 405 | private final boolean mUseFixedVolume; |
| 406 | |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 407 | /** |
| 408 | * Default stream type used for volume control in the absence of playback |
| 409 | * e.g. user on homescreen, no app playing anything, presses hardware volume buttons, this |
| 410 | * stream type is controlled. |
| 411 | */ |
| 412 | protected static final int DEFAULT_VOL_STREAM_NO_PLAYBACK = AudioSystem.STREAM_MUSIC; |
| 413 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 414 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | public void onError(int error) { |
| 416 | switch (error) { |
| 417 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 418 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 419 | SENDMSG_NOOP, 0, 0, null, 0); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 420 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 421 | SENDMSG_QUEUE, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | break; |
| 423 | default: |
| 424 | break; |
| 425 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 431 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 432 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 433 | */ |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 434 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 435 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 436 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 437 | 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] | 438 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 439 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 440 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 441 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 442 | private int mZenModeAffectedStreams = 0; |
| 443 | |
| 444 | // Streams currently muted by ringer mode and dnd |
| 445 | private int mRingerAndZenModeMutedStreams; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 446 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 447 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 448 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | private int mMuteAffectedStreams; |
| 450 | |
| 451 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 452 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 453 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 454 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | */ |
| 456 | private int mVibrateSetting; |
| 457 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 458 | // Is there a vibrator |
| 459 | private final boolean mHasVibrator; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 460 | // Used to play vibrations |
| 461 | private Vibrator mVibrator; |
| 462 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 463 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 464 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 465 | .build(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 466 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 467 | // Broadcast receiver for device connections intent broadcasts |
| 468 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 469 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 470 | /** Interface for UserManagerService. */ |
| 471 | private final UserManagerInternal mUserManagerInternal; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 472 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 473 | |
| 474 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 475 | new AudioServiceUserRestrictionsListener(); |
| 476 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 477 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 478 | // Use makeDeviceListKey() to make a unique key for this list. |
| 479 | private class DeviceListSpec { |
| 480 | int mDeviceType; |
| 481 | String mDeviceName; |
| 482 | String mDeviceAddress; |
| 483 | |
| 484 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 485 | mDeviceType = deviceType; |
| 486 | mDeviceName = deviceName; |
| 487 | mDeviceAddress = deviceAddress; |
| 488 | } |
| 489 | |
| 490 | public String toString() { |
| 491 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 492 | + " address:" + mDeviceAddress + "]"; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 497 | // and the "address" associated with a specific instance of that device type |
| 498 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 499 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 500 | } |
| 501 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 502 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 503 | |
| 504 | // Forced device usage for communications |
| 505 | private int mForcedUseForComm; |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 506 | private int mForcedUseForCommExt; // External state returned by getters: always consistent |
| 507 | // with requests by setters |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 508 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 509 | // List of binder death handlers for setMode() client processes. |
| 510 | // 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] | 511 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 512 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 513 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 514 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 515 | |
| 516 | // BluetoothHeadset API to control SCO connection |
| 517 | private BluetoothHeadset mBluetoothHeadset; |
| 518 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 519 | // Bluetooth headset device |
| 520 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 521 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 522 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 523 | // audio service (internal) or bluetooth headset (external) |
| 524 | private int mScoAudioState; |
| 525 | // SCO audio state is not active |
| 526 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 527 | // SCO audio activation request waiting for headset service to connect |
| 528 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 529 | // 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] | 530 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 531 | // SCO audio deactivation request waiting for headset service to connect |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 532 | private static final int SCO_STATE_DEACTIVATE_REQ = 4; |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 533 | // SCO audio deactivation in progress, waiting for Bluetooth audio intent |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 534 | private static final int SCO_STATE_DEACTIVATING = 5; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 535 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 536 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 537 | // in call audio) |
| 538 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 539 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 540 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 541 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 542 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 543 | // SCO audio mode is undefined |
| 544 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 545 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 546 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 547 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 548 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 549 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 550 | private static final int SCO_MODE_VR = 2; |
| 551 | |
| 552 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 553 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 554 | // Current connection state indicated by bluetooth headset |
| 555 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 556 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 557 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 558 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 559 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 560 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 561 | // listener for SoundPool sample load completion indication |
| 562 | private SoundPoolCallback mSoundPoolCallBack; |
| 563 | // thread for SoundPool listener |
| 564 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 565 | // message looper for SoundPool listener |
| 566 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 567 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 568 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 569 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 570 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 571 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 572 | // is controlled by Vol keys. |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 573 | private int mVolumeControlStream = -1; |
| 574 | // interpretation of whether the volume stream has been selected by the user by clicking on a |
| 575 | // volume slider to change which volume is controlled by the volume keys. Is false |
| 576 | // when mVolumeControlStream is -1. |
| 577 | private boolean mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 578 | private final Object mForceControlStreamLock = new Object(); |
| 579 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 580 | // server process so in theory it is not necessary to monitor the client death. |
| 581 | // However it is good to be ready for future evolutions. |
| 582 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 583 | // Used to play ringtones outside system_server |
| 584 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 585 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 586 | // Request to override default use of A2DP for media. |
Sungsoo | 486f7d3 | 2016-09-28 16:20:52 +0900 | [diff] [blame] | 587 | private boolean mBluetoothA2dpEnabled; |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 588 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 589 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 590 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 591 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 592 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 593 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 594 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 595 | // 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] | 596 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 597 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 598 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 599 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 600 | AudioSystem.DEVICE_OUT_SPDIF | |
| 601 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 602 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 603 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 604 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 605 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 606 | private boolean mDockAudioMediaEnabled = true; |
| 607 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 608 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 609 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 610 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 611 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 612 | // and used later when/if disableSafeMediaVolume() is called. |
| 613 | private StreamVolumeCommand mPendingVolumeCommand; |
| 614 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 615 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 616 | |
| 617 | private final MediaFocusControl mMediaFocusControl; |
| 618 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 619 | // Reference to BluetoothA2dp to query for volume. |
| 620 | private BluetoothHearingAid mHearingAid; |
| 621 | // lock always taken synchronized on mConnectedDevices |
| 622 | private final Object mHearingAidLock = new Object(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 623 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 624 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 625 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 626 | private final Object mA2dpAvrcpLock = new Object(); |
| 627 | // If absolute volume is supported in AVRCP device |
| 628 | private boolean mAvrcpAbsVolSupported = false; |
| 629 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 630 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 631 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 632 | private NotificationManager mNm; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 633 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 634 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 635 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 636 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 637 | // Array of Uids of valid accessibility services to check if caller is one of them |
| 638 | private int[] mAccessibilityServiceUids; |
| 639 | private final Object mAccessibilityServiceUidsLock = new Object(); |
| 640 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 641 | private int mEncodedSurroundMode; |
| 642 | private String mEnabledSurroundFormats; |
| 643 | private boolean mSurroundModeChanged; |
| 644 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 645 | // Intent "extra" data keys. |
| 646 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 647 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 648 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 649 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 650 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 651 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 652 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 653 | |
| 654 | // Defines the format for the connection "address" for ALSA devices |
| 655 | public static String makeAlsaAddressString(int card, int device) { |
| 656 | return "card=" + card + ";device=" + device + ";"; |
| 657 | } |
| 658 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 659 | public static final class Lifecycle extends SystemService { |
| 660 | private AudioService mService; |
| 661 | |
| 662 | public Lifecycle(Context context) { |
| 663 | super(context); |
| 664 | mService = new AudioService(context); |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public void onStart() { |
| 669 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public void onBootPhase(int phase) { |
| 674 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 675 | mService.systemReady(); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 680 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 681 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
| 682 | } |
| 683 | |
| 684 | @Override public void onUidGone(int uid, boolean disabled) { |
| 685 | // Once the uid is no longer running, no need to keep trying to disable its audio. |
| 686 | disableAudioForUid(false, uid); |
| 687 | } |
| 688 | |
| 689 | @Override public void onUidActive(int uid) throws RemoteException { |
| 690 | } |
| 691 | |
| 692 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 693 | } |
| 694 | |
| 695 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 696 | disableAudioForUid(cached, uid); |
| 697 | } |
| 698 | |
| 699 | private void disableAudioForUid(boolean disable, int uid) { |
| 700 | queueMsgUnderWakeLock(mAudioHandler, MSG_DISABLE_AUDIO_FOR_UID, |
| 701 | disable ? 1 : 0 /* arg1 */, uid /* arg2 */, |
| 702 | null /* obj */, 0 /* delay */); |
| 703 | } |
| 704 | }; |
| 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | /////////////////////////////////////////////////////////////////////////// |
| 707 | // Construction |
| 708 | /////////////////////////////////////////////////////////////////////////// |
| 709 | |
| 710 | /** @hide */ |
| 711 | public AudioService(Context context) { |
| 712 | mContext = context; |
| 713 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 714 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 715 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 716 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 717 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 718 | mIsSingleVolume = AudioSystem.isSingleVolume(context); |
| 719 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 720 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 721 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 722 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 723 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 724 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 725 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 726 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 727 | mHasVibrator = mVibrator == null ? false : mVibrator.hasVibrator(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 728 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 729 | // Initialize volume |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 730 | int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1); |
| 731 | if (maxCallVolume != -1) { |
| 732 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume; |
Shuhei Miyazaki | 89091ba | 2018-01-11 18:17:04 +0900 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | int defaultCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_default", -1); |
| 736 | if (defaultCallVolume != -1 && |
| 737 | defaultCallVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] && |
| 738 | defaultCallVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 739 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = defaultCallVolume; |
| 740 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 741 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = |
| 742 | (maxCallVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 743 | } |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 744 | |
| 745 | int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1); |
| 746 | if (maxMusicVolume != -1) { |
| 747 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume; |
| 748 | } |
| 749 | |
| 750 | int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1); |
| 751 | if (defaultMusicVolume != -1 && |
Shuhei Miyazaki | 89091ba | 2018-01-11 18:17:04 +0900 | [diff] [blame] | 752 | defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] && |
| 753 | defaultMusicVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 754 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume; |
| 755 | } else { |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 756 | if (isPlatformTelevision()) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 757 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 758 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 759 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 760 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 761 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 762 | } |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 763 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 764 | |
Chris Kuiper | 09b6c21 | 2018-01-03 22:22:13 -0800 | [diff] [blame] | 765 | int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1); |
| 766 | if (maxAlarmVolume != -1) { |
| 767 | MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume; |
| 768 | } |
| 769 | |
| 770 | int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1); |
| 771 | if (defaultAlarmVolume != -1 && |
| 772 | defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) { |
| 773 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume; |
| 774 | } else { |
| 775 | // Default is 6 out of 7 (default maximum), so scale accordingly. |
| 776 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = |
| 777 | 6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7; |
| 778 | } |
| 779 | |
| 780 | int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1); |
| 781 | if (maxSystemVolume != -1) { |
| 782 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume; |
| 783 | } |
| 784 | |
| 785 | int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1); |
| 786 | if (defaultSystemVolume != -1 && |
| 787 | defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) { |
| 788 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume; |
| 789 | } else { |
| 790 | // Default is to use maximum. |
| 791 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = |
| 792 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]; |
| 793 | } |
| 794 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 795 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 796 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 797 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 798 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 801 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 802 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 803 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 804 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 805 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 806 | sendMsg(mAudioHandler, |
| 807 | MSG_SET_FORCE_USE, |
| 808 | SENDMSG_QUEUE, |
| 809 | AudioSystem.FOR_SYSTEM, |
| 810 | cameraSoundForced ? |
| 811 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 812 | new String("AudioService ctor"), |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 813 | 0); |
| 814 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 815 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 816 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 817 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 818 | // The default safe volume index read here will be replaced by the actual value when |
| 819 | // the mcc is read by onConfigureSafeVolume() |
| 820 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 821 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 822 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 823 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 824 | com.android.internal.R.bool.config_useFixedVolume); |
| 825 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 826 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 827 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 828 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 830 | readUserRestrictions(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 831 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 832 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 833 | |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 834 | // mSafeUsbMediaVolumeIndex must be initialized after createStreamStates() because it |
| 835 | // relies on audio policy having correct ranges for volume indexes. |
| 836 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 837 | |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 838 | mPlaybackMonitor = |
| 839 | new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]); |
| 840 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 841 | mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 842 | |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 843 | mRecordMonitor = new RecordingActivityMonitor(mContext); |
| 844 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 845 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 846 | |
| 847 | // Call setRingerModeInt() to apply correct mute |
| 848 | // state on streams affected by ringer mode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 849 | mRingerAndZenModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 850 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 851 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 852 | // Register for device connection intent broadcasts. |
| 853 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 854 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 855 | intentFilter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 856 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 857 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 858 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 859 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 860 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 861 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 862 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 863 | intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 864 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 865 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 866 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 867 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 868 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 869 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 870 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 871 | intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); |
| 872 | intentFilter.addAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); |
| 873 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 874 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 875 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 876 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 877 | |
| 878 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 879 | |
| 880 | mRecordMonitor.initMonitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 883 | public void systemReady() { |
| 884 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 885 | 0, 0, null, 0); |
Dianne Hackborn | 3e3600e | 2017-08-31 11:15:26 -0700 | [diff] [blame] | 886 | if (false) { |
| 887 | // This is turned off for now, because it is racy and thus causes apps to break. |
| 888 | // Currently banning a uid means that if an app tries to start playing an audio |
| 889 | // stream, that will be preventing, and unbanning it will not allow that stream |
| 890 | // to resume. However these changes in uid state are racy with what the app is doing, |
| 891 | // so that after taking a process out of the cached state we can't guarantee that |
| 892 | // we will unban the uid before the app actually tries to start playing audio. |
| 893 | // (To do that, the activity manager would need to wait until it knows for sure |
| 894 | // that the ban has been removed, before telling the app to do whatever it is |
| 895 | // supposed to do that caused it to go out of the cached state.) |
| 896 | try { |
| 897 | ActivityManager.getService().registerUidObserver(mUidObserver, |
| 898 | ActivityManager.UID_OBSERVER_CACHED | ActivityManager.UID_OBSERVER_GONE, |
| 899 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 900 | } catch (RemoteException e) { |
| 901 | // ignored; both services live in system_server |
| 902 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 903 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | public void onSystemReady() { |
| 907 | mSystemReady = true; |
| 908 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 909 | 0, 0, null, 0); |
| 910 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 911 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 912 | resetBluetoothSco(); |
| 913 | getBluetoothHeadset(); |
| 914 | //FIXME: this is to maintain compatibility with deprecated intent |
| 915 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 916 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 917 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 918 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 919 | sendStickyBroadcastToAll(newIntent); |
| 920 | |
| 921 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 922 | if (adapter != null) { |
| 923 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 924 | BluetoothProfile.A2DP); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 925 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 926 | BluetoothProfile.HEARING_AID); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Jeff Sharkey | 73ea0ae | 2016-08-10 17:30:38 -0600 | [diff] [blame] | 929 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) { |
| 930 | mHdmiManager = mContext.getSystemService(HdmiControlManager.class); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 931 | synchronized (mHdmiManager) { |
| 932 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 933 | if (mHdmiTvClient != null) { |
| 934 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 935 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 936 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 937 | mHdmiCecSink = false; |
| 938 | } |
| 939 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 940 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 941 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 942 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 943 | sendMsg(mAudioHandler, |
| 944 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 945 | SENDMSG_REPLACE, |
| 946 | 0, |
| 947 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 948 | TAG, |
Eric Laurent | 03332ab | 2017-02-09 18:29:15 -0800 | [diff] [blame] | 949 | SystemProperties.getBoolean("audio.safemedia.bypass", false) ? |
| 950 | 0 : SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 951 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 952 | initA11yMonitoring(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 953 | onIndicateSystemReady(); |
| 954 | } |
| 955 | |
| 956 | void onIndicateSystemReady() { |
| 957 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 958 | return; |
| 959 | } |
| 960 | sendMsg(mAudioHandler, |
| 961 | MSG_INDICATE_SYSTEM_READY, |
| 962 | SENDMSG_REPLACE, |
| 963 | 0, |
| 964 | 0, |
| 965 | null, |
| 966 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 967 | } |
| 968 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 969 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 970 | if (!mSystemReady || |
| 971 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 972 | Log.e(TAG, "Audioserver died."); |
| 973 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 974 | null, 500); |
| 975 | return; |
| 976 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 977 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 978 | |
| 979 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 980 | // server crash |
| 981 | // Note that we only execute this when the media server |
| 982 | // process restarts after a crash, not the first time it is started. |
| 983 | AudioSystem.setParameters("restarting=true"); |
| 984 | |
| 985 | readAndSetLowRamDevice(); |
| 986 | |
| 987 | // Restore device connection states |
| 988 | synchronized (mConnectedDevices) { |
| 989 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 990 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 991 | AudioSystem.setDeviceConnectionState( |
| 992 | spec.mDeviceType, |
| 993 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 994 | spec.mDeviceAddress, |
| 995 | spec.mDeviceName); |
| 996 | } |
| 997 | } |
| 998 | // Restore call state |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 999 | if (AudioSystem.setPhoneState(mMode) == AudioSystem.AUDIO_STATUS_OK) { |
| 1000 | mModeLogger.log(new AudioEventLogger.StringEvent( |
| 1001 | "onAudioServerDied causes setPhoneState(" + AudioSystem.modeToString(mMode) + ")")); |
| 1002 | } |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1003 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1004 | // Restore forced usage for communications and record |
| 1005 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, |
| 1006 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1007 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1008 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_RECORD, mForcedUseForComm, |
| 1009 | "onAudioServerDied")); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1010 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 1011 | final int forSys; |
| 1012 | synchronized (mSettingsLock) { |
| 1013 | forSys = mCameraSoundForced ? |
| 1014 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE; |
| 1015 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1016 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_SYSTEM, forSys, |
| 1017 | "onAudioServerDied")); |
| 1018 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, forSys); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1019 | |
| 1020 | // Restore stream volumes |
| 1021 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1022 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 1023 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 1024 | AudioSystem.initStreamVolume( |
| 1025 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1026 | |
| 1027 | streamState.applyAllVolumes(); |
| 1028 | } |
| 1029 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1030 | // Restore mono mode |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1031 | updateMasterMono(mContentResolver); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1032 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1033 | // Restore ringer mode |
| 1034 | setRingerModeInt(getRingerModeInternal(), false); |
| 1035 | |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 1036 | // Reset device rotation (if monitored for this device) |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1037 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 1038 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 1041 | synchronized (mBluetoothA2dpEnabledLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1042 | final int forMed = mBluetoothA2dpEnabled ? |
| 1043 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP; |
| 1044 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_MEDIA, forMed, |
| 1045 | "onAudioServerDied")); |
| 1046 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, forMed); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 1047 | } |
| 1048 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1049 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1050 | final int forDock = mDockAudioMediaEnabled ? |
| 1051 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE; |
| 1052 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, forDock, |
| 1053 | "onAudioServerDied")); |
| 1054 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, forDock); |
| 1055 | sendEncodedSurroundMode(mContentResolver, "onAudioServerDied"); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1056 | sendEnabledSurroundFormats(mContentResolver, true); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1057 | } |
| 1058 | if (mHdmiManager != null) { |
| 1059 | synchronized (mHdmiManager) { |
| 1060 | if (mHdmiTvClient != null) { |
| 1061 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | synchronized (mAudioPolicies) { |
| 1067 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 1068 | policy.connectMixes(); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | onIndicateSystemReady(); |
| 1073 | // indicate the end of reconfiguration phase to audio HAL |
| 1074 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 1075 | |
| 1076 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 1077 | SENDMSG_QUEUE, 1, 0, null, 0); |
| 1078 | } |
| 1079 | |
| 1080 | private void onDispatchAudioServerStateChange(boolean state) { |
| 1081 | synchronized (mAudioServerStateListeners) { |
| 1082 | for (AsdProxy asdp : mAudioServerStateListeners.values()) { |
| 1083 | try { |
| 1084 | asdp.callback().dispatchAudioServerStateChange(state); |
| 1085 | } catch (RemoteException e) { |
| 1086 | Log.w(TAG, "Could not call dispatchAudioServerStateChange()", e); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | private void createAudioSystemThread() { |
| 1093 | mAudioSystemThread = new AudioSystemThread(); |
| 1094 | mAudioSystemThread.start(); |
| 1095 | waitForAudioHandlerCreation(); |
| 1096 | } |
| 1097 | |
| 1098 | /** Waits for the volume handler to be created by the other thread. */ |
| 1099 | private void waitForAudioHandlerCreation() { |
| 1100 | synchronized(this) { |
| 1101 | while (mAudioHandler == null) { |
| 1102 | try { |
| 1103 | // Wait for mAudioHandler to be set by the other thread |
| 1104 | wait(); |
| 1105 | } catch (InterruptedException e) { |
| 1106 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1112 | private void checkAllAliasStreamVolumes() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1113 | synchronized (mSettingsLock) { |
| 1114 | synchronized (VolumeStreamState.class) { |
| 1115 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1116 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1117 | mStreamStates[streamType] |
| 1118 | .setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], TAG); |
| 1119 | // apply stream volume |
| 1120 | if (!mStreamStates[streamType].mIsMuted) { |
| 1121 | mStreamStates[streamType].applyAllVolumes(); |
| 1122 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1123 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1128 | private void checkAllFixedVolumeDevices() |
| 1129 | { |
| 1130 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1131 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1132 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1136 | private void checkAllFixedVolumeDevices(int streamType) { |
| 1137 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1138 | } |
| 1139 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1140 | private void checkMuteAffectedStreams() { |
| 1141 | // any stream with a min level > 0 is not muteable by definition |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1142 | // 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] | 1143 | for (int i = 0; i < mStreamStates.length; i++) { |
| 1144 | final VolumeStreamState vss = mStreamStates[i]; |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1145 | if (vss.mIndexMin > 0 && |
| 1146 | vss.mStreamType != AudioSystem.STREAM_VOICE_CALL) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1147 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1154 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 1155 | |
| 1156 | for (int i = 0; i < numStreamTypes; i++) { |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 1157 | streams[i] = |
| 1158 | new VolumeStreamState(System.VOLUME_SETTINGS_INT[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1161 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1162 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1163 | checkMuteAffectedStreams(); |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1164 | updateDefaultVolumes(); |
| 1165 | } |
| 1166 | |
| 1167 | // Update default indexes from aliased streams. Must be called after mStreamStates is created |
| 1168 | private void updateDefaultVolumes() { |
| 1169 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1170 | if (stream != mStreamVolumeAlias[stream]) { |
| 1171 | AudioSystem.DEFAULT_STREAM_VOLUME[stream] = rescaleIndex( |
| 1172 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]], |
| 1173 | mStreamVolumeAlias[stream], |
| 1174 | stream); |
| 1175 | } |
| 1176 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1179 | private void dumpStreamStates(PrintWriter pw) { |
| 1180 | pw.println("\nStream volumes (device: index)"); |
| 1181 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1182 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1183 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1184 | mStreamStates[i].dump(pw); |
| 1185 | pw.println(""); |
| 1186 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1187 | pw.print("\n- mute affected streams = 0x"); |
| 1188 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1191 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1192 | int dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1193 | final int a11yStreamAlias = sIndependentA11yVolume ? |
| 1194 | AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1195 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1196 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1197 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 1198 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | switch (mPlatformType) { |
| 1201 | case AudioSystem.PLATFORM_VOICE: |
| 1202 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
| 1203 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
| 1204 | break; |
| 1205 | default: |
| 1206 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
| 1207 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 1208 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1209 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1210 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1211 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1212 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1213 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1214 | if (isInCommunication()) { |
| 1215 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 1216 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 1217 | } else { |
| 1218 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 1219 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1220 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1221 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1222 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1223 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY] = a11yStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1224 | |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1225 | if (updateVolumes && mStreamStates != null) { |
| 1226 | updateDefaultVolumes(); |
| 1227 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1228 | synchronized (mSettingsLock) { |
| 1229 | synchronized (VolumeStreamState.class) { |
| 1230 | mStreamStates[AudioSystem.STREAM_DTMF] |
| 1231 | .setAllIndexes(mStreamStates[dtmfStreamAlias], caller); |
| 1232 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].mVolumeIndexSettingName = |
| 1233 | System.VOLUME_SETTINGS_INT[a11yStreamAlias]; |
| 1234 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes( |
| 1235 | mStreamStates[a11yStreamAlias], caller); |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1236 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].refreshRange( |
| 1237 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY]); |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1238 | } |
| 1239 | } |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1240 | if (sIndependentA11yVolume) { |
| 1241 | // restore the a11y values from the settings |
| 1242 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].readSettings(); |
| 1243 | } |
| 1244 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1245 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1246 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1247 | sendMsg(mAudioHandler, |
| 1248 | MSG_SET_ALL_VOLUMES, |
| 1249 | SENDMSG_QUEUE, |
| 1250 | 0, |
| 1251 | 0, |
| 1252 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1253 | sendMsg(mAudioHandler, |
| 1254 | MSG_SET_ALL_VOLUMES, |
| 1255 | SENDMSG_QUEUE, |
| 1256 | 0, |
| 1257 | 0, |
| 1258 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY], 0); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1262 | private void readDockAudioSettings(ContentResolver cr) |
| 1263 | { |
| 1264 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1265 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1266 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1267 | sendMsg(mAudioHandler, |
| 1268 | MSG_SET_FORCE_USE, |
| 1269 | SENDMSG_QUEUE, |
| 1270 | AudioSystem.FOR_DOCK, |
| 1271 | mDockAudioMediaEnabled ? |
| 1272 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1273 | new String("readDockAudioSettings"), |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1274 | 0); |
| 1275 | } |
| 1276 | |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1277 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1278 | private void updateMasterMono(ContentResolver cr) |
| 1279 | { |
| 1280 | final boolean masterMono = System.getIntForUser( |
| 1281 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1282 | if (DEBUG_VOL) { |
| 1283 | Log.d(TAG, String.format("Master mono %b", masterMono)); |
| 1284 | } |
| 1285 | AudioSystem.setMasterMono(masterMono); |
| 1286 | } |
| 1287 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1288 | private void sendEncodedSurroundMode(ContentResolver cr, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1289 | { |
| 1290 | int encodedSurroundMode = Settings.Global.getInt( |
| 1291 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 1292 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1293 | sendEncodedSurroundMode(encodedSurroundMode, eventSource); |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1296 | private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1297 | { |
| 1298 | // initialize to guaranteed bad value |
| 1299 | int forceSetting = AudioSystem.NUM_FORCE_CONFIG; |
| 1300 | switch (encodedSurroundMode) { |
| 1301 | case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO: |
| 1302 | forceSetting = AudioSystem.FORCE_NONE; |
| 1303 | break; |
| 1304 | case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER: |
| 1305 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_NEVER; |
| 1306 | break; |
| 1307 | case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS: |
| 1308 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_ALWAYS; |
| 1309 | break; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1310 | case Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL: |
| 1311 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_MANUAL; |
| 1312 | break; |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1313 | default: |
| 1314 | Log.e(TAG, "updateSurroundSoundSettings: illegal value " |
| 1315 | + encodedSurroundMode); |
| 1316 | break; |
| 1317 | } |
| 1318 | if (forceSetting != AudioSystem.NUM_FORCE_CONFIG) { |
| 1319 | sendMsg(mAudioHandler, |
| 1320 | MSG_SET_FORCE_USE, |
| 1321 | SENDMSG_QUEUE, |
| 1322 | AudioSystem.FOR_ENCODED_SURROUND, |
| 1323 | forceSetting, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1324 | eventSource, |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1325 | 0); |
| 1326 | } |
| 1327 | } |
| 1328 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1329 | private void sendEnabledSurroundFormats(ContentResolver cr, boolean forceUpdate) { |
| 1330 | if (mEncodedSurroundMode != Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL) { |
| 1331 | // Manually enable surround formats only when the setting is in manual mode. |
| 1332 | return; |
| 1333 | } |
| 1334 | String enabledSurroundFormats = Settings.Global.getString( |
| 1335 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS); |
jiabin | 6db5f86 | 2018-05-17 10:33:09 -0700 | [diff] [blame] | 1336 | if (enabledSurroundFormats == null) { |
| 1337 | // Never allow enabledSurroundFormats as a null, which could happen when |
| 1338 | // ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS is not appear in settings DB. |
| 1339 | enabledSurroundFormats = ""; |
| 1340 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1341 | if (!forceUpdate && TextUtils.equals(enabledSurroundFormats, mEnabledSurroundFormats)) { |
| 1342 | // Update enabled surround formats to AudioPolicyManager only when forceUpdate |
| 1343 | // is true or enabled surround formats changed. |
| 1344 | return; |
| 1345 | } |
| 1346 | |
| 1347 | mEnabledSurroundFormats = enabledSurroundFormats; |
| 1348 | String[] surroundFormats = TextUtils.split(enabledSurroundFormats, ","); |
| 1349 | ArrayList<Integer> formats = new ArrayList<>(); |
| 1350 | for (String format : surroundFormats) { |
| 1351 | try { |
| 1352 | int audioFormat = Integer.valueOf(format); |
| 1353 | boolean isSurroundFormat = false; |
| 1354 | for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) { |
| 1355 | if (sf == audioFormat) { |
| 1356 | isSurroundFormat = true; |
| 1357 | break; |
| 1358 | } |
| 1359 | } |
| 1360 | if (isSurroundFormat && !formats.contains(audioFormat)) { |
| 1361 | formats.add(audioFormat); |
| 1362 | } |
| 1363 | } catch (Exception e) { |
| 1364 | Log.e(TAG, "Invalid enabled surround format:" + format); |
| 1365 | } |
| 1366 | } |
| 1367 | // Set filtered surround formats to settings DB in case |
| 1368 | // there are invalid surround formats in original settings. |
| 1369 | Settings.Global.putString(mContext.getContentResolver(), |
| 1370 | Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS, |
| 1371 | TextUtils.join(",", formats)); |
| 1372 | sendMsg(mAudioHandler, MSG_ENABLE_SURROUND_FORMATS, SENDMSG_QUEUE, 0, 0, formats, 0); |
| 1373 | } |
| 1374 | |
| 1375 | private void onEnableSurroundFormats(ArrayList<Integer> enabledSurroundFormats) { |
| 1376 | // Set surround format enabled accordingly. |
| 1377 | for (int surroundFormat : AudioFormat.SURROUND_SOUND_ENCODING) { |
| 1378 | boolean enabled = enabledSurroundFormats.contains(surroundFormat); |
| 1379 | int ret = AudioSystem.setSurroundFormatEnabled(surroundFormat, enabled); |
| 1380 | Log.i(TAG, "enable surround format:" + surroundFormat + " " + enabled + " " + ret); |
| 1381 | } |
| 1382 | } |
| 1383 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | private void readPersistedSettings() { |
| 1385 | final ContentResolver cr = mContentResolver; |
| 1386 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1387 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1388 | Settings.Global.getInt( |
| 1389 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1390 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1391 | // sanity check in case the settings are restored from a device with incompatible |
| 1392 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1393 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1394 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1395 | } |
| 1396 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1397 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1398 | } |
| 1399 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1400 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1401 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1402 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1403 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1404 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1405 | synchronized(mSettingsLock) { |
| 1406 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1407 | if (mRingerModeExternal == -1) { |
| 1408 | mRingerModeExternal = mRingerMode; |
| 1409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1411 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1412 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1413 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1414 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1415 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1416 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1417 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1418 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1419 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1420 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1421 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1423 | updateRingerAndZenModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1424 | readDockAudioSettings(cr); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1425 | sendEncodedSurroundMode(cr, "readPersistedSettings"); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1426 | sendEnabledSurroundFormats(cr, true); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1427 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1428 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1429 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1430 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1431 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1433 | updateMasterMono(cr); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | // Each stream will read its own persisted settings |
| 1436 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1437 | // Broadcast the sticky intents |
| 1438 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1439 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | |
| 1441 | // Broadcast vibrate settings |
| 1442 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1443 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1444 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1445 | // Load settings for the volume controller |
| 1446 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1449 | private void readUserRestrictions() { |
| 1450 | final int currentUser = getCurrentUserId(); |
| 1451 | |
| 1452 | // Check the current user restriction. |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1453 | boolean masterMute = |
| 1454 | mUserManagerInternal.getUserRestriction(currentUser, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 1455 | UserManager.DISALLOW_UNMUTE_DEVICE) |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1456 | || mUserManagerInternal.getUserRestriction(currentUser, |
| 1457 | UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1458 | if (mUseFixedVolume) { |
| 1459 | masterMute = false; |
| 1460 | AudioSystem.setMasterVolume(1.0f); |
| 1461 | } |
| 1462 | if (DEBUG_VOL) { |
| 1463 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1464 | } |
| 1465 | setSystemAudioMute(masterMute); |
| 1466 | AudioSystem.setMasterMute(masterMute); |
| 1467 | broadcastMasterMuteStatus(masterMute); |
| 1468 | |
| 1469 | boolean microphoneMute = mUserManagerInternal.getUserRestriction( |
| 1470 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1471 | if (DEBUG_VOL) { |
| 1472 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1473 | } |
| 1474 | AudioSystem.muteMicrophone(microphoneMute); |
| 1475 | } |
| 1476 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1477 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1478 | final int rescaled = |
| 1479 | (index * mStreamStates[dstStream].getMaxIndex() |
| 1480 | + mStreamStates[srcStream].getMaxIndex() / 2) |
| 1481 | / mStreamStates[srcStream].getMaxIndex(); |
| 1482 | if (rescaled < mStreamStates[dstStream].getMinIndex()) { |
| 1483 | return mStreamStates[dstStream].getMinIndex(); |
| 1484 | } else { |
| 1485 | return rescaled; |
| 1486 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | |
| 1489 | /////////////////////////////////////////////////////////////////////////// |
| 1490 | // IPC methods |
| 1491 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1493 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1494 | String callingPackage, String caller) { |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1495 | final IAudioPolicyCallback extVolCtlr; |
| 1496 | synchronized (mExtVolumeControllerLock) { |
| 1497 | extVolCtlr = mExtVolumeController; |
| 1498 | } |
| 1499 | if (extVolCtlr != null) { |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 1500 | sendMsg(mAudioHandler, MSG_NOTIFY_VOL_EVENT, SENDMSG_QUEUE, |
| 1501 | direction, 0 /*ignored*/, |
| 1502 | extVolCtlr, 0 /*delay*/); |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1503 | } else { |
| 1504 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 1505 | caller, Binder.getCallingUid()); |
| 1506 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1510 | String callingPackage, String caller, int uid) { |
| 1511 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1512 | + ", flags=" + flags + ", caller=" + caller |
| 1513 | + ", volControlStream=" + mVolumeControlStream |
| 1514 | + ", userSelect=" + mUserSelectedVolumeControlStream); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1515 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_SUGG_VOL, suggestedStreamType, |
| 1516 | direction/*val1*/, flags/*val2*/, new StringBuilder(callingPackage) |
| 1517 | .append("/").append(caller).append(" uid:").append(uid).toString())); |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1518 | final int streamType; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1519 | synchronized (mForceControlStreamLock) { |
| 1520 | // Request lock in case mVolumeControlStream is changed by other thread. |
| 1521 | if (mUserSelectedVolumeControlStream) { // implies mVolumeControlStream != -1 |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1522 | streamType = mVolumeControlStream; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1523 | } else { |
| 1524 | final int maybeActiveStreamType = getActiveStreamType(suggestedStreamType); |
| 1525 | final boolean activeForReal; |
| 1526 | if (maybeActiveStreamType == AudioSystem.STREAM_RING |
| 1527 | || maybeActiveStreamType == AudioSystem.STREAM_NOTIFICATION) { |
| 1528 | activeForReal = wasStreamActiveRecently(maybeActiveStreamType, 0); |
| 1529 | } else { |
| 1530 | activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, 0); |
| 1531 | } |
| 1532 | if (activeForReal || mVolumeControlStream == -1) { |
| 1533 | streamType = maybeActiveStreamType; |
| 1534 | } else { |
| 1535 | streamType = mVolumeControlStream; |
| 1536 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1537 | } |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1538 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1539 | |
| 1540 | final boolean isMute = isMuteAdjust(direction); |
| 1541 | |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1542 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1543 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1545 | // Play sounds on STREAM_RING only. |
| 1546 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1547 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1549 | } |
| 1550 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1551 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1552 | // Don't suppress mute/unmute requests |
| 1553 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1554 | direction = 0; |
| 1555 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1556 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1557 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1558 | } |
| 1559 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1560 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1564 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1565 | String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1566 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1567 | Log.w(TAG, "Trying to call adjustStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1568 | + "CHANGE_ACCESSIBILITY_VOLUME / callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1569 | return; |
| 1570 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1571 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_STREAM_VOL, streamType, |
| 1572 | direction/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1573 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1574 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 1577 | protected void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1578 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1579 | if (mUseFixedVolume) { |
| 1580 | return; |
| 1581 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1582 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1583 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | ensureValidDirection(direction); |
| 1586 | ensureValidStreamType(streamType); |
| 1587 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1588 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1589 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1590 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1591 | return; |
| 1592 | } |
| 1593 | |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1594 | // If adjust is mute and the stream is STREAM_VOICE_CALL, make sure |
| 1595 | // that the calling app have the MODIFY_PHONE_STATE permission. |
| 1596 | if (isMuteAdjust && |
| 1597 | streamType == AudioSystem.STREAM_VOICE_CALL && |
| 1598 | mContext.checkCallingOrSelfPermission( |
| 1599 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1600 | != PackageManager.PERMISSION_GRANTED) { |
| 1601 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: adjustStreamVolume from pid=" |
| 1602 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1603 | return; |
| 1604 | } |
| 1605 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1606 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1607 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1608 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1609 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1610 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1611 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1612 | |
| 1613 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1614 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1615 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1617 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1618 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1619 | // skip a2dp absolute volume control request when the device |
| 1620 | // is not an a2dp device |
| 1621 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1622 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1623 | return; |
| 1624 | } |
| 1625 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1626 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1627 | // so we handle user restrictions correctly. |
| 1628 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1629 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1630 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1631 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1632 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1633 | return; |
| 1634 | } |
| 1635 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1636 | // reset any pending volume command |
| 1637 | synchronized (mSafeMediaVolumeState) { |
| 1638 | mPendingVolumeCommand = null; |
| 1639 | } |
| 1640 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1641 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1642 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1643 | ((device & mFixedVolumeDevices) != 0)) { |
| 1644 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1645 | |
| 1646 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1647 | // volume is enforced, and max and 0 for the others. |
| 1648 | // This is simulated by stepping by the full allowed volume range |
| 1649 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1650 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1651 | step = safeMediaVolumeIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1652 | } else { |
| 1653 | step = streamState.getMaxIndex(); |
| 1654 | } |
| 1655 | if (aliasIndex != 0) { |
| 1656 | aliasIndex = step; |
| 1657 | } |
| 1658 | } else { |
| 1659 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1660 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1661 | } |
| 1662 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1663 | // If either the client forces allowing ringer modes for this adjustment, |
| 1664 | // or the stream type is one that is affected by ringer modes |
| 1665 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1666 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1667 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1668 | // do not vibrate if already in vibrate mode |
| 1669 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1670 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1671 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1672 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1673 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1674 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1675 | streamState.mIsMuted, callingPackage, flags); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1676 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1677 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1678 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1679 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1680 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1681 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1682 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1683 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1684 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1685 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1686 | |
| 1687 | // If the ringer mode or zen is muting the stream, do not change stream unless |
| 1688 | // it'll cause us to exit dnd |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1689 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1690 | adjustVolume = false; |
| 1691 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1692 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1693 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1694 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1695 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1696 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1697 | if (isMuteAdjust) { |
| 1698 | boolean state; |
| 1699 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1700 | state = !streamState.mIsMuted; |
| 1701 | } else { |
| 1702 | state = direction == AudioManager.ADJUST_MUTE; |
| 1703 | } |
| 1704 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1705 | setSystemAudioMute(state); |
| 1706 | } |
| 1707 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1708 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1709 | if (!(readCameraSoundForced() |
| 1710 | && (mStreamStates[stream].getStreamType() |
| 1711 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1712 | mStreamStates[stream].mute(state); |
| 1713 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1717 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1718 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1719 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1720 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1721 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1722 | // Post message to set system volume (it in turn will post a |
| 1723 | // message to persist). |
| 1724 | if (streamState.mIsMuted) { |
| 1725 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1726 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1727 | // unmute immediately for volume up |
| 1728 | streamState.mute(false); |
| 1729 | } else if (direction == AudioManager.ADJUST_LOWER) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1730 | if (mIsSingleVolume) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1731 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1732 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1733 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1734 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1735 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1736 | sendMsg(mAudioHandler, |
| 1737 | MSG_SET_DEVICE_VOLUME, |
| 1738 | SENDMSG_QUEUE, |
| 1739 | device, |
| 1740 | 0, |
| 1741 | streamState, |
| 1742 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1743 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1744 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1745 | int newIndex = mStreamStates[streamType].getIndex(device); |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1746 | |
| 1747 | // Check if volume update should be send to AVRCP |
| 1748 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1749 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1750 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1751 | synchronized (mA2dpAvrcpLock) { |
| 1752 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1753 | mA2dp.setAvrcpAbsoluteVolume(newIndex / 10); |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1758 | // Check if volume update should be send to Hearing Aid |
| 1759 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 1760 | setHearingAidVolume(newIndex, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1763 | // Check if volume update should be sent to Hdmi system audio. |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1764 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1765 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1766 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1767 | if (mHdmiManager != null) { |
| 1768 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1769 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1770 | if (mHdmiCecSink && |
| 1771 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1772 | oldIndex != newIndex) { |
| 1773 | synchronized (mHdmiPlaybackClient) { |
| 1774 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1775 | KeyEvent.KEYCODE_VOLUME_UP; |
Donghyun Cho | 5f6d404e | 2016-03-17 20:39:25 +0900 | [diff] [blame] | 1776 | final long ident = Binder.clearCallingIdentity(); |
| 1777 | try { |
| 1778 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1779 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1780 | } finally { |
| 1781 | Binder.restoreCallingIdentity(ident); |
| 1782 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1783 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1787 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1788 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1789 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1792 | // Called after a delay when volume down is pressed while muted |
| 1793 | private void onUnmuteStream(int stream, int flags) { |
| 1794 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1795 | streamState.mute(false); |
| 1796 | |
| 1797 | final int device = getDeviceForStream(stream); |
| 1798 | final int index = mStreamStates[stream].getIndex(device); |
| 1799 | sendVolumeUpdate(stream, index, index, flags); |
| 1800 | } |
| 1801 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1802 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1803 | if (mHdmiManager == null |
| 1804 | || mHdmiTvClient == null |
| 1805 | || oldVolume == newVolume |
| 1806 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1807 | |
| 1808 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1809 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1810 | synchronized (mHdmiManager) { |
| 1811 | if (!mHdmiSystemAudioSupported) return; |
| 1812 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1813 | final long token = Binder.clearCallingIdentity(); |
| 1814 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1815 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1816 | } finally { |
| 1817 | Binder.restoreCallingIdentity(token); |
| 1818 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1823 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1824 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1825 | class StreamVolumeCommand { |
| 1826 | public final int mStreamType; |
| 1827 | public final int mIndex; |
| 1828 | public final int mFlags; |
| 1829 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1830 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1831 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1832 | mStreamType = streamType; |
| 1833 | mIndex = index; |
| 1834 | mFlags = flags; |
| 1835 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1836 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1837 | |
| 1838 | @Override |
| 1839 | public String toString() { |
| 1840 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1841 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1842 | .append(mDevice).append('}').toString(); |
| 1843 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1844 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1845 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1846 | private int getNewRingerMode(int stream, int index, int flags) { |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 1847 | // setRingerMode does nothing if the device is single volume,so the value would be unchanged |
| 1848 | if (mIsSingleVolume) { |
| 1849 | return getRingerModeExternal(); |
| 1850 | } |
| 1851 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1852 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1853 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1854 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1855 | int newRingerMode; |
| 1856 | if (index == 0) { |
| 1857 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1858 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1859 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1860 | } else { |
| 1861 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1862 | } |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1863 | return newRingerMode; |
| 1864 | } |
| 1865 | return getRingerModeExternal(); |
| 1866 | } |
| 1867 | |
| 1868 | private boolean isAndroidNPlus(String caller) { |
| 1869 | try { |
| 1870 | final ApplicationInfo applicationInfo = |
| 1871 | mContext.getPackageManager().getApplicationInfoAsUser( |
| 1872 | caller, 0, UserHandle.getUserId(Binder.getCallingUid())); |
| 1873 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1874 | return true; |
| 1875 | } |
| 1876 | return false; |
| 1877 | } catch (PackageManager.NameNotFoundException e) { |
| 1878 | return true; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | private boolean wouldToggleZenMode(int newMode) { |
| 1883 | if (getRingerModeExternal() == AudioManager.RINGER_MODE_SILENT |
| 1884 | && newMode != AudioManager.RINGER_MODE_SILENT) { |
| 1885 | return true; |
| 1886 | } else if (getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT |
| 1887 | && newMode == AudioManager.RINGER_MODE_SILENT) { |
| 1888 | return true; |
| 1889 | } |
| 1890 | return false; |
| 1891 | } |
| 1892 | |
| 1893 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1894 | String caller) { |
| 1895 | final int stream = mStreamVolumeAlias[streamType]; |
| 1896 | setStreamVolumeInt(stream, index, device, false, caller); |
| 1897 | // setting volume on ui sounds stream type also controls silent mode |
| 1898 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1899 | (stream == getUiSoundsStreamType())) { |
| 1900 | setRingerMode(getNewRingerMode(stream, index, flags), |
| 1901 | TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1902 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1903 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1904 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1908 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1909 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1910 | Log.w(TAG, "Trying to call setStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1911 | + " CHANGE_ACCESSIBILITY_VOLUME callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1912 | return; |
| 1913 | } |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1914 | if ((streamType == AudioManager.STREAM_VOICE_CALL) && |
| 1915 | (index == 0) && |
| 1916 | (mContext.checkCallingOrSelfPermission( |
| 1917 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1918 | != PackageManager.PERMISSION_GRANTED)) { |
| 1919 | Log.w(TAG, "Trying to call setStreamVolume() for STREAM_VOICE_CALL and index 0 without" |
| 1920 | + " MODIFY_PHONE_STATE callingPackage=" + callingPackage); |
| 1921 | return; |
| 1922 | } |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1923 | mVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType, |
| 1924 | index/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1925 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1926 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1929 | private boolean canChangeAccessibilityVolume() { |
| 1930 | synchronized (mAccessibilityServiceUidsLock) { |
| 1931 | if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 1932 | android.Manifest.permission.CHANGE_ACCESSIBILITY_VOLUME)) { |
| 1933 | return true; |
| 1934 | } |
| 1935 | if (mAccessibilityServiceUids != null) { |
| 1936 | int callingUid = Binder.getCallingUid(); |
| 1937 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 1938 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 1939 | return true; |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | return false; |
| 1944 | } |
| 1945 | } |
| 1946 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1947 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1948 | String caller, int uid) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1949 | if (DEBUG_VOL) { |
| 1950 | Log.d(TAG, "setStreamVolume(stream=" + streamType+", index=" + index |
| 1951 | + ", calling=" + callingPackage + ")"); |
| 1952 | } |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1953 | if (mUseFixedVolume) { |
| 1954 | return; |
| 1955 | } |
| 1956 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1957 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1958 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1959 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1960 | |
| 1961 | final int device = getDeviceForStream(streamType); |
| 1962 | int oldIndex; |
| 1963 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1964 | // skip a2dp absolute volume control request when the device |
| 1965 | // is not an a2dp device |
| 1966 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1967 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1968 | return; |
| 1969 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1970 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1971 | // so we handle user restrictions correctly. |
| 1972 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1973 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1974 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1975 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1976 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1977 | return; |
| 1978 | } |
| 1979 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1980 | if (isAndroidNPlus(callingPackage) |
| 1981 | && wouldToggleZenMode(getNewRingerMode(streamTypeAlias, index, flags)) |
| 1982 | && !mNm.isNotificationPolicyAccessGrantedForPackage(callingPackage)) { |
| 1983 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 1984 | } |
| 1985 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1986 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
| 1987 | return; |
| 1988 | } |
| 1989 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1990 | synchronized (mSafeMediaVolumeState) { |
| 1991 | // reset any pending volume command |
| 1992 | mPendingVolumeCommand = null; |
| 1993 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1994 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1995 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1996 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1997 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1998 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1999 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 2000 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 2001 | synchronized (mA2dpAvrcpLock) { |
| 2002 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 2003 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 2004 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 2008 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 2009 | setHearingAidVolume(index, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2012 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 2013 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 2014 | } |
| 2015 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 2016 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 2017 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 2018 | ((device & mFixedVolumeDevices) != 0)) { |
| 2019 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 2020 | |
| 2021 | // volume is either 0 or max allowed for fixed volume devices |
| 2022 | if (index != 0) { |
| 2023 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 2024 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 2025 | index = safeMediaVolumeIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 2026 | } else { |
| 2027 | index = streamState.getMaxIndex(); |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 2032 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2033 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 2034 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 2035 | streamType, index, flags, device); |
| 2036 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2037 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2038 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 2039 | } |
| 2040 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2041 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2044 | // 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] | 2045 | private boolean volumeAdjustmentAllowedByDnd(int streamTypeAlias, int flags) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2046 | switch (mNm.getZenMode()) { |
| 2047 | case Settings.Global.ZEN_MODE_OFF: |
| 2048 | return true; |
| 2049 | case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: |
| 2050 | case Settings.Global.ZEN_MODE_ALARMS: |
| 2051 | case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 2052 | return !isStreamMutedByRingerOrZenMode(streamTypeAlias) |
| 2053 | || streamTypeAlias == getUiSoundsStreamType() |
| 2054 | || (flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0; |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 2055 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2056 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 2057 | return true; |
| 2058 | } |
| 2059 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2060 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 2061 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2062 | if (DEBUG_VOL) { Log.d(TAG, String.format("forceVolumeControlStream(%d)", streamType)); } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2063 | synchronized(mForceControlStreamLock) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2064 | if (mVolumeControlStream != -1 && streamType != -1) { |
| 2065 | mUserSelectedVolumeControlStream = true; |
| 2066 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2067 | mVolumeControlStream = streamType; |
| 2068 | if (mVolumeControlStream == -1) { |
| 2069 | if (mForceControlStreamClient != null) { |
| 2070 | mForceControlStreamClient.release(); |
| 2071 | mForceControlStreamClient = null; |
| 2072 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2073 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2074 | } else { |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 2075 | if (null == mForceControlStreamClient) { |
| 2076 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 2077 | } else { |
| 2078 | if (mForceControlStreamClient.getBinder() == cb) { |
| 2079 | Log.d(TAG, "forceVolumeControlStream cb:" + cb + " is already linked."); |
| 2080 | } else { |
| 2081 | mForceControlStreamClient.release(); |
| 2082 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 2083 | } |
| 2084 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 2090 | private IBinder mCb; // To be notified of client's death |
| 2091 | |
| 2092 | ForceControlStreamClient(IBinder cb) { |
| 2093 | if (cb != null) { |
| 2094 | try { |
| 2095 | cb.linkToDeath(this, 0); |
| 2096 | } catch (RemoteException e) { |
| 2097 | // Client has died! |
| 2098 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 2099 | cb = null; |
| 2100 | } |
| 2101 | } |
| 2102 | mCb = cb; |
| 2103 | } |
| 2104 | |
| 2105 | public void binderDied() { |
| 2106 | synchronized(mForceControlStreamLock) { |
| 2107 | Log.w(TAG, "SCO client died"); |
| 2108 | if (mForceControlStreamClient != this) { |
| 2109 | Log.w(TAG, "unregistered control stream client died"); |
| 2110 | } else { |
| 2111 | mForceControlStreamClient = null; |
| 2112 | mVolumeControlStream = -1; |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2113 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | public void release() { |
| 2119 | if (mCb != null) { |
| 2120 | mCb.unlinkToDeath(this, 0); |
| 2121 | mCb = null; |
| 2122 | } |
| 2123 | } |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 2124 | |
| 2125 | public IBinder getBinder() { |
| 2126 | return mCb; |
| 2127 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2130 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 2131 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2132 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2133 | final long ident = Binder.clearCallingIdentity(); |
| 2134 | try { |
| 2135 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2136 | } finally { |
| 2137 | Binder.restoreCallingIdentity(ident); |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2142 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2143 | final long ident = Binder.clearCallingIdentity(); |
| 2144 | try { |
| 2145 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2146 | } finally { |
| 2147 | Binder.restoreCallingIdentity(ident); |
| 2148 | } |
| 2149 | } |
| 2150 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2151 | private int getCurrentUserId() { |
| 2152 | final long ident = Binder.clearCallingIdentity(); |
| 2153 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2154 | UserInfo currentUser = ActivityManager.getService().getCurrentUser(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2155 | return currentUser.id; |
| 2156 | } catch (RemoteException e) { |
| 2157 | // Activity manager not running, nothing we can do assume user 0. |
| 2158 | } finally { |
| 2159 | Binder.restoreCallingIdentity(ident); |
| 2160 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 2161 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2164 | // UI update and Broadcast Intent |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 2165 | protected void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 2166 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2167 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2168 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 2169 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 2170 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2171 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2174 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 2175 | // receives volume notification from Audio Receiver. |
| 2176 | private int updateFlagsForSystemAudio(int flags) { |
| 2177 | if (mHdmiTvClient != null) { |
| 2178 | synchronized (mHdmiTvClient) { |
| 2179 | if (mHdmiSystemAudioSupported && |
| 2180 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 2181 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | return flags; |
| 2186 | } |
| 2187 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2188 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2189 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2190 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2191 | broadcastMasterMuteStatus(muted); |
| 2192 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2193 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2194 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2195 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2196 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2197 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 2198 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2199 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2200 | } |
| 2201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2202 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | * Sets the stream state's index, and posts a message to set system volume. |
| 2204 | * This will not call out to the UI. Assumes a valid stream type. |
| 2205 | * |
| 2206 | * @param streamType Type of the stream |
| 2207 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2208 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2209 | * @param force If true, set the volume even if the desired volume is same |
| 2210 | * as the current volume. |
| 2211 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2212 | private void setStreamVolumeInt(int streamType, |
| 2213 | int index, |
| 2214 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2215 | boolean force, |
| 2216 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2218 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2219 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2220 | // Post message to set system volume (it in turn will post a message |
| 2221 | // to persist). |
| 2222 | sendMsg(mAudioHandler, |
| 2223 | MSG_SET_DEVICE_VOLUME, |
| 2224 | SENDMSG_QUEUE, |
| 2225 | device, |
| 2226 | 0, |
| 2227 | streamState, |
| 2228 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2232 | private void setSystemAudioMute(boolean state) { |
| 2233 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 2234 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 2235 | if (!mHdmiSystemAudioSupported) return; |
| 2236 | synchronized (mHdmiTvClient) { |
| 2237 | final long token = Binder.clearCallingIdentity(); |
| 2238 | try { |
| 2239 | mHdmiTvClient.setSystemAudioMute(state); |
| 2240 | } finally { |
| 2241 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2242 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2247 | /** get stream mute state. */ |
| 2248 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2249 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 2250 | streamType = getActiveStreamType(streamType); |
| 2251 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2252 | synchronized (VolumeStreamState.class) { |
jiabin | 93d32ef | 2017-07-11 13:50:02 -0700 | [diff] [blame] | 2253 | ensureValidStreamType(streamType); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2254 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2255 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2256 | } |
| 2257 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 2258 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 2259 | private IBinder mICallback; // To be notified of client's death |
| 2260 | |
| 2261 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 2262 | mICallback = cb; |
| 2263 | try { |
| 2264 | cb.linkToDeath(this, 0/*flags*/); |
| 2265 | } catch (RemoteException e) { |
| 2266 | Log.e(TAG, "can't link to death", e); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | boolean isHandlerFor(IBinder cb) { |
| 2271 | return mICallback.equals(cb); |
| 2272 | } |
| 2273 | |
| 2274 | void forget() { |
| 2275 | try { |
| 2276 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 2277 | } catch (NoSuchElementException e) { |
| 2278 | Log.e(TAG, "error unlinking to death", e); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | public void binderDied() { |
| 2283 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 2284 | forceRemoteSubmixFullVolume(false, mICallback); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 2290 | * @return true if there is a registered death handler, false otherwise */ |
| 2291 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2292 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2293 | while (it.hasNext()) { |
| 2294 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 2295 | if (handler.isHandlerFor(cb)) { |
| 2296 | handler.forget(); |
| 2297 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 2298 | return true; |
| 2299 | } |
| 2300 | } |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
| 2304 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 2305 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2306 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2307 | while (it.hasNext()) { |
| 2308 | if (it.next().isHandlerFor(cb)) { |
| 2309 | return true; |
| 2310 | } |
| 2311 | } |
| 2312 | return false; |
| 2313 | } |
| 2314 | |
| 2315 | private int mRmtSbmxFullVolRefCount = 0; |
| 2316 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 2317 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 2318 | |
| 2319 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 2320 | if (cb == null) { |
| 2321 | return; |
| 2322 | } |
| 2323 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 2324 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 2325 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 2326 | return; |
| 2327 | } |
| 2328 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 2329 | boolean applyRequired = false; |
| 2330 | if (startForcing) { |
| 2331 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 2332 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 2333 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2334 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2335 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2336 | applyRequired = true; |
| 2337 | } |
| 2338 | mRmtSbmxFullVolRefCount++; |
| 2339 | } |
| 2340 | } else { |
| 2341 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 2342 | mRmtSbmxFullVolRefCount--; |
| 2343 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2344 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2345 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2346 | applyRequired = true; |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | if (applyRequired) { |
| 2351 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 2352 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 2353 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2358 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 2359 | int userId) { |
| 2360 | // If we are being called by the system check for user we are going to change |
| 2361 | // so we handle user restrictions correctly. |
| 2362 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2363 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2364 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2365 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 2366 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2367 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 2368 | return; |
| 2369 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2370 | if (userId != UserHandle.getCallingUserId() && |
| 2371 | mContext.checkCallingOrSelfPermission( |
| 2372 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2373 | != PackageManager.PERMISSION_GRANTED) { |
| 2374 | return; |
| 2375 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2376 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 2377 | } |
| 2378 | |
| 2379 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 2380 | if (DEBUG_VOL) { |
| 2381 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 2382 | } |
Hongwei Wang | daba124 | 2018-05-29 14:36:16 -0700 | [diff] [blame] | 2383 | if (!isPlatformAutomotive() && mUseFixedVolume) { |
| 2384 | // If using fixed volume, we don't mute. |
| 2385 | // TODO: remove the isPlatformAutomotive check here. |
| 2386 | // The isPlatformAutomotive check is added for safety but may not be necessary. |
| 2387 | return; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2388 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2389 | if (getCurrentUserId() == userId) { |
| 2390 | if (mute != AudioSystem.getMasterMute()) { |
| 2391 | setSystemAudioMute(mute); |
| 2392 | AudioSystem.setMasterMute(mute); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2393 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2394 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2395 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2396 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 2397 | sendBroadcastToAll(intent); |
| 2398 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 2399 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** get master mute state. */ |
| 2403 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 2404 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2407 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 2408 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 2409 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2410 | } |
| 2411 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2412 | /** @see AudioManager#getStreamVolume(int) */ |
| 2413 | public int getStreamVolume(int streamType) { |
| 2414 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2415 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2416 | synchronized (VolumeStreamState.class) { |
| 2417 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 2418 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2419 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2420 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2421 | index = 0; |
| 2422 | } |
| 2423 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 2424 | (device & mFixedVolumeDevices) != 0) { |
| 2425 | index = mStreamStates[streamType].getMaxIndex(); |
| 2426 | } |
| 2427 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2428 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 2432 | public int getStreamMaxVolume(int streamType) { |
| 2433 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2434 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2435 | } |
| 2436 | |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 2437 | /** @see AudioManager#getStreamMinVolumeInt(int) */ |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2438 | public int getStreamMinVolume(int streamType) { |
| 2439 | ensureValidStreamType(streamType); |
| 2440 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 2441 | } |
| 2442 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2443 | /** Get last audible volume before stream was muted. */ |
| 2444 | public int getLastAudibleStreamVolume(int streamType) { |
| 2445 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2446 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2447 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2448 | } |
| 2449 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2450 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 2451 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 2452 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2455 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 2456 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2457 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 2458 | // If we are being called by the system check for user we are going to change |
| 2459 | // so we handle user restrictions correctly. |
| 2460 | int uid = Binder.getCallingUid(); |
| 2461 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2462 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2463 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2464 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 2465 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2466 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2467 | return; |
| 2468 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 2469 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 2470 | return; |
| 2471 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2472 | if (userId != UserHandle.getCallingUserId() && |
| 2473 | mContext.checkCallingOrSelfPermission( |
| 2474 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2475 | != PackageManager.PERMISSION_GRANTED) { |
| 2476 | return; |
| 2477 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2478 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 2479 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2480 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2481 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 2482 | if (DEBUG_VOL) { |
| 2483 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 2484 | } |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2485 | // only mute for the current user |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2486 | if (getCurrentUserId() == userId) { |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2487 | final boolean currentMute = AudioSystem.isMicrophoneMuted(); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2488 | final long identity = Binder.clearCallingIdentity(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2489 | AudioSystem.muteMicrophone(on); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2490 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2491 | if (on != currentMute) { |
| 2492 | mContext.sendBroadcast(new Intent(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED) |
| 2493 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)); |
| 2494 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2495 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2496 | } |
| 2497 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2498 | @Override |
| 2499 | public int getRingerModeExternal() { |
| 2500 | synchronized(mSettingsLock) { |
| 2501 | return mRingerModeExternal; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | @Override |
| 2506 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2507 | synchronized(mSettingsLock) { |
| 2508 | return mRingerMode; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2513 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2514 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 2515 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2516 | } |
| 2517 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2518 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2519 | public boolean isValidRingerMode(int ringerMode) { |
| 2520 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2521 | } |
| 2522 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2523 | public void setRingerModeExternal(int ringerMode, String caller) { |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2524 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 2525 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller)) { |
| 2526 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 2527 | } |
| 2528 | |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2529 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2533 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2534 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2535 | } |
| 2536 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2537 | public void silenceRingerModeInternal(String reason) { |
| 2538 | VibrationEffect effect = null; |
| 2539 | int ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2540 | int toastText = 0; |
| 2541 | |
| 2542 | int silenceRingerSetting = Settings.Secure.VOLUME_HUSH_OFF; |
| 2543 | if (mContext.getResources() |
| 2544 | .getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled)) { |
| 2545 | silenceRingerSetting = Settings.Secure.getIntForUser(mContentResolver, |
| 2546 | Settings.Secure.VOLUME_HUSH_GESTURE, VOLUME_HUSH_OFF, |
| 2547 | UserHandle.USER_CURRENT); |
| 2548 | } |
| 2549 | |
| 2550 | switch(silenceRingerSetting) { |
| 2551 | case VOLUME_HUSH_MUTE: |
| 2552 | effect = VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK); |
| 2553 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2554 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_silent; |
| 2555 | break; |
| 2556 | case VOLUME_HUSH_VIBRATE: |
| 2557 | effect = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK); |
| 2558 | ringerMode = AudioManager.RINGER_MODE_VIBRATE; |
| 2559 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_vibrate; |
| 2560 | break; |
| 2561 | } |
| 2562 | maybeVibrate(effect); |
| 2563 | setRingerModeInternal(ringerMode, reason); |
| 2564 | Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT).show(); |
| 2565 | } |
| 2566 | |
| 2567 | private boolean maybeVibrate(VibrationEffect effect) { |
| 2568 | if (!mHasVibrator) { |
| 2569 | return false; |
| 2570 | } |
| 2571 | final boolean hapticsDisabled = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 2572 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) == 0; |
| 2573 | if (hapticsDisabled) { |
| 2574 | return false; |
| 2575 | } |
| 2576 | |
| 2577 | if (effect == null) { |
| 2578 | return false; |
| 2579 | } |
| 2580 | mVibrator.vibrate( |
| 2581 | Binder.getCallingUid(), mContext.getOpPackageName(), effect, VIBRATION_ATTRIBUTES); |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2585 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 2586 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2587 | return; |
| 2588 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2589 | if (caller == null || caller.length() == 0) { |
| 2590 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2591 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2592 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2593 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2594 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2595 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2596 | final long identity = Binder.clearCallingIdentity(); |
| 2597 | try { |
| 2598 | synchronized (mSettingsLock) { |
| 2599 | final int ringerModeInternal = getRingerModeInternal(); |
| 2600 | final int ringerModeExternal = getRingerModeExternal(); |
| 2601 | if (external) { |
| 2602 | setRingerModeExt(ringerMode); |
| 2603 | if (mRingerModeDelegate != null) { |
| 2604 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2605 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2606 | } |
| 2607 | if (ringerMode != ringerModeInternal) { |
| 2608 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2609 | } |
| 2610 | } else /*internal*/ { |
| 2611 | if (ringerMode != ringerModeInternal) { |
| 2612 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2613 | } |
| 2614 | if (mRingerModeDelegate != null) { |
| 2615 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2616 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2617 | } |
| 2618 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2619 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2620 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2621 | } finally { |
| 2622 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2626 | private void setRingerModeExt(int ringerMode) { |
| 2627 | synchronized(mSettingsLock) { |
| 2628 | if (ringerMode == mRingerModeExternal) return; |
| 2629 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2630 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2631 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2632 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2633 | } |
| 2634 | |
Jean-Michel Trivi | c106d9f | 2018-06-07 12:28:27 -0700 | [diff] [blame] | 2635 | @GuardedBy("mSettingsLock") |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2636 | private void muteRingerModeStreams() { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2637 | // Mute stream if not previously muted by ringer mode and (ringer mode |
| 2638 | // is not RINGER_MODE_NORMAL OR stream is zen muted) and stream is affected by ringer mode. |
| 2639 | // Unmute stream if previously muted by ringer/zen mode and ringer mode |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2640 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2641 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2642 | |
| 2643 | if (mNm == null) { |
| 2644 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 2645 | } |
| 2646 | |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2647 | final int ringerMode = mRingerMode; // Read ringer mode as reading primitives is atomic |
| 2648 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2649 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
| 2650 | final boolean shouldRingSco = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2651 | && isBluetoothScoOn(); |
| 2652 | // Ask audio policy engine to force use Bluetooth SCO channel if needed |
| 2653 | final String eventSource = "muteRingerModeStreams() from u/pid:" + Binder.getCallingUid() |
| 2654 | + "/" + Binder.getCallingPid(); |
| 2655 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, AudioSystem.FOR_VIBRATE_RINGING, |
| 2656 | shouldRingSco ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE, eventSource, 0); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2657 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2658 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2659 | final boolean isMuted = isStreamMutedByRingerOrZenMode(streamType); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2660 | final boolean muteAllowedBySco = |
| 2661 | !(shouldRingSco && streamType == AudioSystem.STREAM_RING); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2662 | final boolean shouldZenMute = shouldZenMuteStream(streamType); |
| 2663 | final boolean shouldMute = shouldZenMute || (ringerModeMute |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2664 | && isStreamAffectedByRingerMode(streamType) && muteAllowedBySco); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2665 | if (isMuted == shouldMute) continue; |
| 2666 | if (!shouldMute) { |
| 2667 | // unmute |
| 2668 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2669 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2670 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2671 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2672 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2673 | int device = vss.mIndexMap.keyAt(i); |
| 2674 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2675 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2676 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2677 | } |
| 2678 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2679 | // Persist volume for stream ring when it is changed here |
| 2680 | final int device = getDeviceForStream(streamType); |
| 2681 | sendMsg(mAudioHandler, |
| 2682 | MSG_PERSIST_VOLUME, |
| 2683 | SENDMSG_QUEUE, |
| 2684 | device, |
| 2685 | 0, |
| 2686 | mStreamStates[streamType], |
| 2687 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2688 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2689 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2690 | mStreamStates[streamType].mute(false); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2691 | mRingerAndZenModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2692 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2693 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2694 | mStreamStates[streamType].mute(true); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2695 | mRingerAndZenModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2696 | } |
| 2697 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2698 | } |
| 2699 | |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2700 | private boolean isAlarm(int streamType) { |
| 2701 | return streamType == AudioSystem.STREAM_ALARM; |
| 2702 | } |
| 2703 | |
| 2704 | private boolean isNotificationOrRinger(int streamType) { |
| 2705 | return streamType == AudioSystem.STREAM_NOTIFICATION |
| 2706 | || streamType == AudioSystem.STREAM_RING; |
| 2707 | } |
| 2708 | |
| 2709 | private boolean isMedia(int streamType) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2710 | return streamType == AudioSystem.STREAM_MUSIC; |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | private boolean isSystem(int streamType) { |
| 2715 | return streamType == AudioSystem.STREAM_SYSTEM; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2716 | } |
| 2717 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2718 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2719 | final boolean change; |
| 2720 | synchronized(mSettingsLock) { |
| 2721 | change = mRingerMode != ringerMode; |
| 2722 | mRingerMode = ringerMode; |
Jean-Michel Trivi | c106d9f | 2018-06-07 12:28:27 -0700 | [diff] [blame] | 2723 | muteRingerModeStreams(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2724 | } |
| 2725 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2726 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2727 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2728 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2729 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2730 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2731 | if (change) { |
| 2732 | // Send sticky broadcast |
| 2733 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2734 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | /** @see AudioManager#shouldVibrate(int) */ |
| 2738 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2739 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | |
| 2741 | switch (getVibrateSetting(vibrateType)) { |
| 2742 | |
| 2743 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2744 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | |
| 2746 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2747 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | |
| 2749 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2750 | // return false, even for incoming calls |
| 2751 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2752 | |
| 2753 | default: |
| 2754 | return false; |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2759 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2760 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2761 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2762 | } |
| 2763 | |
| 2764 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2765 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2766 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2767 | if (!mHasVibrator) return; |
| 2768 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2769 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2770 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | |
| 2772 | // Broadcast change |
| 2773 | broadcastVibrateSetting(vibrateType); |
| 2774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2775 | } |
| 2776 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2777 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2778 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2779 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2780 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2781 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2782 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2783 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2784 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | public void binderDied() { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2788 | int oldModeOwnerPid = 0; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2789 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2790 | synchronized(mSetModeDeathHandlers) { |
| 2791 | Log.w(TAG, "setMode() client died"); |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2792 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2793 | oldModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2794 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2795 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2796 | if (index < 0) { |
| 2797 | Log.w(TAG, "unregistered setMode() client died"); |
| 2798 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2799 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2800 | } |
| 2801 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2802 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2803 | // SCO connections not started by the application changing the mode when pid changes |
| 2804 | if ((newModeOwnerPid != oldModeOwnerPid) && (newModeOwnerPid != 0)) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2805 | final long ident = Binder.clearCallingIdentity(); |
| 2806 | disconnectBluetoothSco(newModeOwnerPid); |
| 2807 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2808 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2809 | } |
| 2810 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2811 | public int getPid() { |
| 2812 | return mPid; |
| 2813 | } |
| 2814 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2815 | public void setMode(int mode) { |
| 2816 | mMode = mode; |
| 2817 | } |
| 2818 | |
| 2819 | public int getMode() { |
| 2820 | return mMode; |
| 2821 | } |
| 2822 | |
| 2823 | public IBinder getBinder() { |
| 2824 | return mCb; |
| 2825 | } |
| 2826 | } |
| 2827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2828 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2829 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2830 | 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] | 2831 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2832 | return; |
| 2833 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2834 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2835 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2836 | (mContext.checkCallingOrSelfPermission( |
| 2837 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2838 | != PackageManager.PERMISSION_GRANTED)) { |
| 2839 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2840 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2841 | return; |
| 2842 | } |
| 2843 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2844 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2845 | return; |
| 2846 | } |
| 2847 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2848 | int oldModeOwnerPid = 0; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2849 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2850 | synchronized(mSetModeDeathHandlers) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2851 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2852 | oldModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2853 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2854 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2855 | mode = mMode; |
| 2856 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2857 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2858 | } |
| 2859 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2860 | // SCO connections not started by the application changing the mode when pid changes |
| 2861 | if ((newModeOwnerPid != oldModeOwnerPid) && (newModeOwnerPid != 0)) { |
| 2862 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2865 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2866 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2867 | // 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] | 2868 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2869 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2870 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2871 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2872 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2873 | if (cb == null) { |
| 2874 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2875 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2876 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2877 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2878 | SetModeDeathHandler hdlr = null; |
| 2879 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2880 | while (iter.hasNext()) { |
| 2881 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2882 | if (h.getPid() == pid) { |
| 2883 | hdlr = h; |
| 2884 | // Remove from client list so that it is re-inserted at top of list |
| 2885 | iter.remove(); |
| 2886 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2887 | break; |
| 2888 | } |
| 2889 | } |
| 2890 | int status = AudioSystem.AUDIO_STATUS_OK; |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2891 | int actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2892 | do { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2893 | actualMode = mode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2894 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2895 | // get new mode from client at top the list if any |
| 2896 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2897 | hdlr = mSetModeDeathHandlers.get(0); |
| 2898 | cb = hdlr.getBinder(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2899 | actualMode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2900 | if (DEBUG_MODE) { |
| 2901 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2902 | + hdlr.mPid); |
| 2903 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2904 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2905 | } else { |
| 2906 | if (hdlr == null) { |
| 2907 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2908 | } |
| 2909 | // Register for client death notification |
| 2910 | try { |
| 2911 | cb.linkToDeath(hdlr, 0); |
| 2912 | } catch (RemoteException e) { |
| 2913 | // Client has died! |
| 2914 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2915 | } |
| 2916 | |
| 2917 | // Last client to call setMode() is always at top of client list |
| 2918 | // as required by SetModeDeathHandler.binderDied() |
| 2919 | mSetModeDeathHandlers.add(0, hdlr); |
| 2920 | hdlr.setMode(mode); |
| 2921 | } |
| 2922 | |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2923 | if (actualMode != mMode) { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2924 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2925 | status = AudioSystem.setPhoneState(actualMode); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2926 | Binder.restoreCallingIdentity(identity); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2927 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2928 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + actualMode); } |
| 2929 | mMode = actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2930 | } else { |
| 2931 | if (hdlr != null) { |
| 2932 | mSetModeDeathHandlers.remove(hdlr); |
| 2933 | cb.unlinkToDeath(hdlr, 0); |
| 2934 | } |
| 2935 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2936 | 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] | 2937 | mode = AudioSystem.MODE_NORMAL; |
| 2938 | } |
| 2939 | } else { |
| 2940 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2941 | } |
| 2942 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2943 | |
| 2944 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2945 | if (actualMode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2946 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2947 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2948 | } else { |
| 2949 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | } |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2952 | // Note: newModeOwnerPid is always 0 when actualMode is MODE_NORMAL |
| 2953 | mModeLogger.log( |
| 2954 | new PhoneStateEvent(caller, pid, mode, newModeOwnerPid, actualMode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2955 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2956 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2957 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2958 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2959 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2960 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2961 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2962 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | /** @see AudioManager#getMode() */ |
| 2966 | public int getMode() { |
| 2967 | return mMode; |
| 2968 | } |
| 2969 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2970 | //========================================================================================== |
| 2971 | // Sound Effects |
| 2972 | //========================================================================================== |
| 2973 | |
| 2974 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2975 | private static final String ATTR_VERSION = "version"; |
| 2976 | private static final String TAG_GROUP = "group"; |
| 2977 | private static final String ATTR_GROUP_NAME = "name"; |
| 2978 | private static final String TAG_ASSET = "asset"; |
| 2979 | private static final String ATTR_ASSET_ID = "id"; |
| 2980 | private static final String ATTR_ASSET_FILE = "file"; |
| 2981 | |
| 2982 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2983 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2984 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2985 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2986 | |
| 2987 | class LoadSoundEffectReply { |
| 2988 | public int mStatus = 1; |
| 2989 | }; |
| 2990 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2991 | private void loadTouchSoundAssetDefaults() { |
| 2992 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2993 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2994 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2995 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | private void loadTouchSoundAssets() { |
| 3000 | XmlResourceParser parser = null; |
| 3001 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3002 | // only load assets once. |
| 3003 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 3004 | return; |
| 3005 | } |
| 3006 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 3007 | loadTouchSoundAssetDefaults(); |
| 3008 | |
| 3009 | try { |
| 3010 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 3011 | |
| 3012 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 3013 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 3014 | boolean inTouchSoundsGroup = false; |
| 3015 | |
| 3016 | if (ASSET_FILE_VERSION.equals(version)) { |
| 3017 | while (true) { |
| 3018 | XmlUtils.nextElement(parser); |
| 3019 | String element = parser.getName(); |
| 3020 | if (element == null) { |
| 3021 | break; |
| 3022 | } |
| 3023 | if (element.equals(TAG_GROUP)) { |
| 3024 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 3025 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 3026 | inTouchSoundsGroup = true; |
| 3027 | break; |
| 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | while (inTouchSoundsGroup) { |
| 3032 | XmlUtils.nextElement(parser); |
| 3033 | String element = parser.getName(); |
| 3034 | if (element == null) { |
| 3035 | break; |
| 3036 | } |
| 3037 | if (element.equals(TAG_ASSET)) { |
| 3038 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 3039 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 3040 | int fx; |
| 3041 | |
| 3042 | try { |
| 3043 | Field field = AudioManager.class.getField(id); |
| 3044 | fx = field.getInt(null); |
| 3045 | } catch (Exception e) { |
| 3046 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 3047 | continue; |
| 3048 | } |
| 3049 | |
| 3050 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 3051 | if (i == -1) { |
| 3052 | i = SOUND_EFFECT_FILES.size(); |
| 3053 | SOUND_EFFECT_FILES.add(file); |
| 3054 | } |
| 3055 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 3056 | } else { |
| 3057 | break; |
| 3058 | } |
| 3059 | } |
| 3060 | } |
| 3061 | } catch (Resources.NotFoundException e) { |
| 3062 | Log.w(TAG, "audio assets file not found", e); |
| 3063 | } catch (XmlPullParserException e) { |
| 3064 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 3065 | } catch (IOException e) { |
| 3066 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 3067 | } finally { |
| 3068 | if (parser != null) { |
| 3069 | parser.close(); |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3074 | /** @see AudioManager#playSoundEffect(int) */ |
| 3075 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3076 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3080 | public void playSoundEffectVolume(int effectType, float volume) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3081 | // do not try to play the sound effect if the system stream is muted |
| 3082 | if (isStreamMutedByRingerOrZenMode(STREAM_SYSTEM)) { |
| 3083 | return; |
| 3084 | } |
| 3085 | |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 3086 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 3087 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 3088 | return; |
| 3089 | } |
| 3090 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3091 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3092 | effectType, (int) (volume * 1000), null, 0); |
| 3093 | } |
| 3094 | |
| 3095 | /** |
| 3096 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 3097 | * 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] | 3098 | */ |
| 3099 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3100 | int attempts = 3; |
| 3101 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3102 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3103 | synchronized (reply) { |
| 3104 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 3105 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3106 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3107 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3108 | } catch (InterruptedException e) { |
| 3109 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3110 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3112 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3113 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | /** |
| 3117 | * Unloads samples from the sound pool. |
| 3118 | * This method can be called to free some memory when |
| 3119 | * sound effects are disabled. |
| 3120 | */ |
| 3121 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3122 | 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] | 3123 | } |
| 3124 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3125 | class SoundPoolListenerThread extends Thread { |
| 3126 | public SoundPoolListenerThread() { |
| 3127 | super("SoundPoolListenerThread"); |
| 3128 | } |
| 3129 | |
| 3130 | @Override |
| 3131 | public void run() { |
| 3132 | |
| 3133 | Looper.prepare(); |
| 3134 | mSoundPoolLooper = Looper.myLooper(); |
| 3135 | |
| 3136 | synchronized (mSoundEffectsLock) { |
| 3137 | if (mSoundPool != null) { |
| 3138 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 3139 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 3140 | } |
| 3141 | mSoundEffectsLock.notify(); |
| 3142 | } |
| 3143 | Looper.loop(); |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | private final class SoundPoolCallback implements |
| 3148 | android.media.SoundPool.OnLoadCompleteListener { |
| 3149 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3150 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 3151 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3152 | |
| 3153 | public int status() { |
| 3154 | return mStatus; |
| 3155 | } |
| 3156 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3157 | public void setSamples(int[] samples) { |
| 3158 | for (int i = 0; i < samples.length; i++) { |
| 3159 | // do not wait ack for samples rejected upfront by SoundPool |
| 3160 | if (samples[i] > 0) { |
| 3161 | mSamples.add(samples[i]); |
| 3162 | } |
| 3163 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 3167 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3168 | int i = mSamples.indexOf(sampleId); |
| 3169 | if (i >= 0) { |
| 3170 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3171 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3172 | if ((status != 0) || mSamples. isEmpty()) { |
| 3173 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3174 | mSoundEffectsLock.notify(); |
| 3175 | } |
| 3176 | } |
| 3177 | } |
| 3178 | } |
| 3179 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3180 | /** @see AudioManager#reloadAudioSettings() */ |
| 3181 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3182 | readAudioSettings(false /*userSwitch*/); |
| 3183 | } |
| 3184 | |
| 3185 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3186 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 3187 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 3188 | readUserRestrictions(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3189 | |
| 3190 | // restore volume settings |
| 3191 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3192 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 3193 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 3194 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3195 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 3196 | continue; |
| 3197 | } |
| 3198 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3199 | streamState.readSettings(); |
| 3200 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3201 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3202 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3203 | !isStreamMutedByRingerOrZenMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3204 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3205 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3209 | // apply new ringer mode before checking volume for alias streams so that streams |
| 3210 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3211 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3212 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3213 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3214 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3215 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3216 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3217 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3218 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 3219 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 3220 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3221 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3222 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3223 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3224 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3225 | } |
| 3226 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3227 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3228 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3229 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 3230 | return; |
| 3231 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3232 | // for logging only |
| 3233 | final String eventSource = new StringBuilder("setSpeakerphoneOn(").append(on) |
| 3234 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3235 | .append(Binder.getCallingPid()).toString(); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3236 | |
| 3237 | if (on) { |
| 3238 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 3239 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3240 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, |
| 3241 | eventSource, 0); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3242 | } |
| 3243 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 3244 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 3245 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 3246 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3247 | |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3248 | mForcedUseForCommExt = mForcedUseForComm; |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3249 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3250 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 3254 | public boolean isSpeakerphoneOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3255 | return (mForcedUseForCommExt == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3258 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3259 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3260 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 3261 | return; |
| 3262 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3263 | |
| 3264 | // Only enable calls from system components |
Vitor Albuquerque | c3bb48c | 2018-03-07 10:39:59 -0300 | [diff] [blame] | 3265 | if (UserHandle.getCallingAppId() >= FIRST_APPLICATION_UID) { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3266 | mForcedUseForCommExt = on ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE; |
| 3267 | return; |
| 3268 | } |
| 3269 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3270 | // for logging only |
| 3271 | final String eventSource = new StringBuilder("setBluetoothScoOn(").append(on) |
| 3272 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3273 | .append(Binder.getCallingPid()).toString(); |
| 3274 | setBluetoothScoOnInt(on, eventSource); |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3275 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3276 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3277 | public void setBluetoothScoOnInt(boolean on, String eventSource) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3278 | Log.i(TAG, "setBluetoothScoOnInt: " + on + " " + eventSource); |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3279 | if (on) { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3280 | // do not accept SCO ON if SCO audio is not connected |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3281 | synchronized (mScoClients) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3282 | if ((mBluetoothHeadset != null) |
| 3283 | && (mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3284 | != BluetoothHeadset.STATE_AUDIO_CONNECTED)) { |
| 3285 | mForcedUseForCommExt = AudioSystem.FORCE_BT_SCO; |
| 3286 | Log.w(TAG, "setBluetoothScoOnInt(true) failed because " |
| 3287 | + mBluetoothHeadsetDevice + " is not in audio connected mode"); |
| 3288 | return; |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3289 | } |
| 3290 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3291 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 3292 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 3293 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 3294 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3295 | mForcedUseForCommExt = mForcedUseForComm; |
| 3296 | AudioSystem.setParameters("BT_SCO="+ (on ? "on" : "off")); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3297 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3298 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, eventSource, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3299 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3300 | AudioSystem.FOR_RECORD, mForcedUseForComm, eventSource, 0); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 3301 | // Un-mute ringtone stream volume |
| 3302 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | /** @see AudioManager#isBluetoothScoOn() */ |
| 3306 | public boolean isBluetoothScoOn() { |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3307 | return (mForcedUseForCommExt == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3310 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3311 | public void setBluetoothA2dpOn(boolean on) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3312 | // for logging only |
| 3313 | final String eventSource = new StringBuilder("setBluetoothA2dpOn(").append(on) |
| 3314 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3315 | .append(Binder.getCallingPid()).toString(); |
| 3316 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3317 | synchronized (mBluetoothA2dpEnabledLock) { |
zengjing | 452da33 | 2018-05-25 11:15:20 +0800 | [diff] [blame] | 3318 | if (mBluetoothA2dpEnabled == on) { |
| 3319 | return; |
| 3320 | } |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3321 | mBluetoothA2dpEnabled = on; |
| 3322 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 3323 | AudioSystem.FOR_MEDIA, |
| 3324 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3325 | eventSource, 0); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3326 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3329 | /** @see AudioManager#isBluetoothA2dpOn() */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3330 | public boolean isBluetoothA2dpOn() { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3331 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3332 | return mBluetoothA2dpEnabled; |
| 3333 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3334 | } |
| 3335 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3336 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3337 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 3338 | int scoAudioMode = |
| 3339 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3340 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3341 | startBluetoothScoInt(cb, scoAudioMode); |
| 3342 | } |
| 3343 | |
| 3344 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 3345 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 3346 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 3347 | } |
| 3348 | |
| 3349 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3350 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3351 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3352 | return; |
| 3353 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3354 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3355 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 3356 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3357 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3358 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 3359 | // client is created. |
| 3360 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3361 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3362 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | /** @see AudioManager#stopBluetoothSco() */ |
| 3366 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3367 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3368 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3369 | return; |
| 3370 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3371 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 3372 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 3373 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 3374 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 3375 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3376 | if (client != null) { |
| 3377 | client.decCount(); |
| 3378 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3379 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3382 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3383 | private class ScoClient implements IBinder.DeathRecipient { |
| 3384 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3385 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3386 | private int mStartcount; // number of SCO connections started by this client |
| 3387 | |
| 3388 | ScoClient(IBinder cb) { |
| 3389 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 3390 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3391 | mStartcount = 0; |
| 3392 | } |
| 3393 | |
| 3394 | public void binderDied() { |
| 3395 | synchronized(mScoClients) { |
| 3396 | Log.w(TAG, "SCO client died"); |
| 3397 | int index = mScoClients.indexOf(this); |
| 3398 | if (index < 0) { |
| 3399 | Log.w(TAG, "unregistered SCO client died"); |
| 3400 | } else { |
| 3401 | clearCount(true); |
| 3402 | mScoClients.remove(this); |
| 3403 | } |
| 3404 | } |
| 3405 | } |
| 3406 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3407 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3408 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3409 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3410 | if (mStartcount == 0) { |
| 3411 | try { |
| 3412 | mCb.linkToDeath(this, 0); |
| 3413 | } catch (RemoteException e) { |
| 3414 | // client has already died! |
| 3415 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 3416 | } |
| 3417 | } |
| 3418 | mStartcount++; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | public void decCount() { |
| 3423 | synchronized(mScoClients) { |
| 3424 | if (mStartcount == 0) { |
| 3425 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 3426 | } else { |
| 3427 | mStartcount--; |
| 3428 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3429 | try { |
| 3430 | mCb.unlinkToDeath(this, 0); |
| 3431 | } catch (NoSuchElementException e) { |
| 3432 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 3433 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3434 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3435 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | public void clearCount(boolean stopSco) { |
| 3441 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 3442 | if (mStartcount != 0) { |
| 3443 | try { |
| 3444 | mCb.unlinkToDeath(this, 0); |
| 3445 | } catch (NoSuchElementException e) { |
| 3446 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 3447 | } |
| 3448 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3449 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3450 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3451 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | public int getCount() { |
| 3457 | return mStartcount; |
| 3458 | } |
| 3459 | |
| 3460 | public IBinder getBinder() { |
| 3461 | return mCb; |
| 3462 | } |
| 3463 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3464 | public int getPid() { |
| 3465 | return mCreatorPid; |
| 3466 | } |
| 3467 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3468 | public int totalCount() { |
| 3469 | synchronized(mScoClients) { |
| 3470 | int count = 0; |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3471 | for (ScoClient mScoClient : mScoClients) { |
| 3472 | count += mScoClient.getCount(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3473 | } |
| 3474 | return count; |
| 3475 | } |
| 3476 | } |
| 3477 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3478 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3479 | checkScoAudioState(); |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3480 | int clientCount = totalCount(); |
| 3481 | if (clientCount != 0) { |
| 3482 | Log.i(TAG, "requestScoState: state=" + state + ", scoAudioMode=" + scoAudioMode |
| 3483 | + ", clientCount=" + clientCount); |
| 3484 | return; |
| 3485 | } |
| 3486 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 3487 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 3488 | // the connection. |
| 3489 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 3490 | // Accept SCO audio activation only in NORMAL audio mode or if the mode is |
| 3491 | // currently controlled by the same client process. |
| 3492 | synchronized(mSetModeDeathHandlers) { |
| 3493 | int modeOwnerPid = mSetModeDeathHandlers.isEmpty() |
| 3494 | ? 0 : mSetModeDeathHandlers.get(0).getPid(); |
| 3495 | if (modeOwnerPid != 0 && (modeOwnerPid != mCreatorPid)) { |
| 3496 | Log.w(TAG, "requestScoState: audio mode is not NORMAL and modeOwnerPid " |
| 3497 | + modeOwnerPid + " != creatorPid " + mCreatorPid); |
| 3498 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3499 | return; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3500 | } |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3501 | switch (mScoAudioState) { |
| 3502 | case SCO_STATE_INACTIVE: |
| 3503 | mScoAudioMode = scoAudioMode; |
| 3504 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
| 3505 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 3506 | if (mBluetoothHeadsetDevice != null) { |
| 3507 | mScoAudioMode = Settings.Global.getInt(mContentResolver, |
| 3508 | "bluetooth_sco_channel_" |
| 3509 | + mBluetoothHeadsetDevice.getAddress(), |
| 3510 | SCO_MODE_VIRTUAL_CALL); |
| 3511 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 3512 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 3513 | } |
| 3514 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3515 | } |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3516 | if (mBluetoothHeadset == null) { |
| 3517 | if (getBluetoothHeadset()) { |
| 3518 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
| 3519 | } else { |
| 3520 | Log.w(TAG, "requestScoState: getBluetoothHeadset failed during" |
| 3521 | + " connection, mScoAudioMode=" + mScoAudioMode); |
| 3522 | broadcastScoConnectionState( |
| 3523 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3524 | } |
| 3525 | break; |
| 3526 | } |
| 3527 | if (mBluetoothHeadsetDevice == null) { |
| 3528 | Log.w(TAG, "requestScoState: no active device while connecting," |
| 3529 | + " mScoAudioMode=" + mScoAudioMode); |
| 3530 | broadcastScoConnectionState( |
| 3531 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3532 | break; |
| 3533 | } |
| 3534 | if (connectBluetoothScoAudioHelper(mBluetoothHeadset, |
| 3535 | mBluetoothHeadsetDevice, mScoAudioMode)) { |
| 3536 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3537 | } else { |
| 3538 | Log.w(TAG, "requestScoState: connect to " + mBluetoothHeadsetDevice |
| 3539 | + " failed, mScoAudioMode=" + mScoAudioMode); |
| 3540 | broadcastScoConnectionState( |
| 3541 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3542 | } |
| 3543 | break; |
| 3544 | case SCO_STATE_DEACTIVATING: |
| 3545 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
| 3546 | break; |
| 3547 | case SCO_STATE_DEACTIVATE_REQ: |
| 3548 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3549 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
| 3550 | break; |
| 3551 | default: |
| 3552 | Log.w(TAG, "requestScoState: failed to connect in state " |
| 3553 | + mScoAudioState + ", scoAudioMode=" + scoAudioMode); |
| 3554 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3555 | break; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3556 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 3560 | switch (mScoAudioState) { |
| 3561 | case SCO_STATE_ACTIVE_INTERNAL: |
| 3562 | if (mBluetoothHeadset == null) { |
| 3563 | if (getBluetoothHeadset()) { |
| 3564 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 3565 | } else { |
| 3566 | Log.w(TAG, "requestScoState: getBluetoothHeadset failed during" |
| 3567 | + " disconnection, mScoAudioMode=" + mScoAudioMode); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3568 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3569 | broadcastScoConnectionState( |
| 3570 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3571 | } |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3572 | break; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3573 | } |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3574 | if (mBluetoothHeadsetDevice == null) { |
| 3575 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3576 | broadcastScoConnectionState( |
| 3577 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3578 | break; |
| 3579 | } |
| 3580 | if (disconnectBluetoothScoAudioHelper(mBluetoothHeadset, |
| 3581 | mBluetoothHeadsetDevice, mScoAudioMode)) { |
| 3582 | mScoAudioState = SCO_STATE_DEACTIVATING; |
| 3583 | } else { |
| 3584 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3585 | broadcastScoConnectionState( |
| 3586 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3587 | } |
| 3588 | break; |
| 3589 | case SCO_STATE_ACTIVATE_REQ: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3590 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3591 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3592 | break; |
| 3593 | default: |
| 3594 | Log.w(TAG, "requestScoState: failed to disconnect in state " |
| 3595 | + mScoAudioState + ", scoAudioMode=" + scoAudioMode); |
| 3596 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3597 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3598 | } |
| 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3603 | private void checkScoAudioState() { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3604 | synchronized (mScoClients) { |
| 3605 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
| 3606 | mScoAudioState == SCO_STATE_INACTIVE && |
| 3607 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 3608 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 3609 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 3610 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3614 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3615 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3616 | synchronized(mScoClients) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3617 | for (ScoClient existingClient : mScoClients) { |
| 3618 | if (existingClient.getBinder() == cb) { |
| 3619 | return existingClient; |
| 3620 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3621 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3622 | if (create) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3623 | ScoClient newClient = new ScoClient(cb); |
| 3624 | mScoClients.add(newClient); |
| 3625 | return newClient; |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3626 | } |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3627 | return null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3631 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3632 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3633 | ScoClient savedClient = null; |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3634 | for (ScoClient cl : mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3635 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 3636 | cl.clearCount(stopSco); |
| 3637 | } else { |
| 3638 | savedClient = cl; |
| 3639 | } |
| 3640 | } |
| 3641 | mScoClients.clear(); |
| 3642 | if (savedClient != null) { |
| 3643 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3648 | private boolean getBluetoothHeadset() { |
| 3649 | boolean result = false; |
| 3650 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 3651 | if (adapter != null) { |
| 3652 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 3653 | BluetoothProfile.HEADSET); |
| 3654 | } |
| 3655 | // If we could not get a bluetooth headset proxy, send a failure message |
| 3656 | // without delay to reset the SCO audio state and clear SCO clients. |
| 3657 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 3658 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3659 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3660 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 3661 | return result; |
| 3662 | } |
| 3663 | |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 3664 | /** |
| 3665 | * Disconnect all SCO connections started by {@link AudioManager} except those started by |
| 3666 | * {@param exceptPid} |
| 3667 | * |
| 3668 | * @param exceptPid pid whose SCO connections through {@link AudioManager} should be kept |
| 3669 | */ |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3670 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3671 | synchronized(mScoClients) { |
| 3672 | checkScoAudioState(); |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 3673 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL) { |
| 3674 | return; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3675 | } |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 3676 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3680 | private static boolean disconnectBluetoothScoAudioHelper(BluetoothHeadset bluetoothHeadset, |
| 3681 | BluetoothDevice device, int scoAudioMode) { |
| 3682 | switch (scoAudioMode) { |
| 3683 | case SCO_MODE_RAW: |
| 3684 | return bluetoothHeadset.disconnectAudio(); |
| 3685 | case SCO_MODE_VIRTUAL_CALL: |
| 3686 | return bluetoothHeadset.stopScoUsingVirtualVoiceCall(); |
| 3687 | case SCO_MODE_VR: |
| 3688 | return bluetoothHeadset.stopVoiceRecognition(device); |
| 3689 | default: |
| 3690 | return false; |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | private static boolean connectBluetoothScoAudioHelper(BluetoothHeadset bluetoothHeadset, |
| 3695 | BluetoothDevice device, int scoAudioMode) { |
| 3696 | switch (scoAudioMode) { |
| 3697 | case SCO_MODE_RAW: |
| 3698 | return bluetoothHeadset.connectAudio(); |
| 3699 | case SCO_MODE_VIRTUAL_CALL: |
| 3700 | return bluetoothHeadset.startScoUsingVirtualVoiceCall(); |
| 3701 | case SCO_MODE_VR: |
| 3702 | return bluetoothHeadset.startVoiceRecognition(device); |
| 3703 | default: |
| 3704 | return false; |
| 3705 | } |
| 3706 | } |
| 3707 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3708 | private void resetBluetoothSco() { |
| 3709 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 3710 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3711 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3712 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 3713 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3714 | AudioSystem.setParameters("A2dpSuspended=false"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3715 | setBluetoothScoOnInt(false, "resetBluetoothSco"); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3719 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 3720 | SENDMSG_QUEUE, state, 0, null, 0); |
| 3721 | } |
| 3722 | |
| 3723 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3724 | if (state != mScoConnectionState) { |
| 3725 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 3726 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 3727 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 3728 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3729 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3730 | mScoConnectionState = state; |
| 3731 | } |
| 3732 | } |
| 3733 | |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3734 | private boolean handleBtScoActiveDeviceChange(BluetoothDevice btDevice, boolean isActive) { |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3735 | if (btDevice == null) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3736 | return true; |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3737 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3738 | String address = btDevice.getAddress(); |
| 3739 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3740 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Ugo Yu | 8f8ee84 | 2018-05-14 19:30:32 +0800 | [diff] [blame] | 3741 | int[] outDeviceTypes = { |
| 3742 | AudioSystem.DEVICE_OUT_BLUETOOTH_SCO, |
| 3743 | AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET, |
| 3744 | AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
| 3745 | }; |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3746 | if (btClass != null) { |
| 3747 | switch (btClass.getDeviceClass()) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3748 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 3749 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
Ugo Yu | 8f8ee84 | 2018-05-14 19:30:32 +0800 | [diff] [blame] | 3750 | outDeviceTypes = new int[] { AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET }; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3751 | break; |
| 3752 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
Ugo Yu | 8f8ee84 | 2018-05-14 19:30:32 +0800 | [diff] [blame] | 3753 | outDeviceTypes = new int[] { AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT }; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3754 | break; |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3755 | } |
| 3756 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3757 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 3758 | address = ""; |
| 3759 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3760 | String btDeviceName = btDevice.getName(); |
Ugo Yu | 8f8ee84 | 2018-05-14 19:30:32 +0800 | [diff] [blame] | 3761 | boolean result = false; |
| 3762 | if (isActive) { |
| 3763 | result |= handleDeviceConnection(isActive, outDeviceTypes[0], address, btDeviceName); |
| 3764 | } else { |
| 3765 | for (int outDeviceType : outDeviceTypes) { |
| 3766 | result |= handleDeviceConnection(isActive, outDeviceType, address, btDeviceName); |
| 3767 | } |
| 3768 | } |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3769 | // handleDeviceConnection() && result to make sure the method get executed |
| 3770 | result = handleDeviceConnection(isActive, inDevice, address, btDeviceName) && result; |
| 3771 | return result; |
| 3772 | } |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3773 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3774 | private void setBtScoActiveDevice(BluetoothDevice btDevice) { |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3775 | synchronized (mScoClients) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3776 | Log.i(TAG, "setBtScoActiveDevice: " + mBluetoothHeadsetDevice + " -> " + btDevice); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3777 | final BluetoothDevice previousActiveDevice = mBluetoothHeadsetDevice; |
| 3778 | if (!Objects.equals(btDevice, previousActiveDevice)) { |
| 3779 | if (!handleBtScoActiveDeviceChange(previousActiveDevice, false)) { |
| 3780 | Log.w(TAG, "setBtScoActiveDevice() failed to remove previous device " |
| 3781 | + previousActiveDevice); |
| 3782 | } |
| 3783 | if (!handleBtScoActiveDeviceChange(btDevice, true)) { |
| 3784 | Log.e(TAG, "setBtScoActiveDevice() failed to add new device " + btDevice); |
| 3785 | // set mBluetoothHeadsetDevice to null when failing to add new device |
| 3786 | btDevice = null; |
| 3787 | } |
Satish Kodishala | 2980980 | 2016-01-18 14:23:12 +0530 | [diff] [blame] | 3788 | mBluetoothHeadsetDevice = btDevice; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3789 | if (mBluetoothHeadsetDevice == null) { |
| 3790 | resetBluetoothSco(); |
| 3791 | } |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | } |
| 3795 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3796 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 3797 | new BluetoothProfile.ServiceListener() { |
| 3798 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3799 | BluetoothDevice btDevice; |
| 3800 | List<BluetoothDevice> deviceList; |
| 3801 | switch(profile) { |
| 3802 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3803 | synchronized (mConnectedDevices) { |
| 3804 | synchronized (mA2dpAvrcpLock) { |
| 3805 | mA2dp = (BluetoothA2dp) proxy; |
| 3806 | deviceList = mA2dp.getConnectedDevices(); |
| 3807 | if (deviceList.size() > 0) { |
| 3808 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3809 | int state = mA2dp.getConnectionState(btDevice); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3810 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3811 | int delay = checkSendBecomingNoisyIntent( |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3812 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, intState, |
| 3813 | AudioSystem.DEVICE_NONE); |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 3814 | final String addr = btDevice == null ? "null" : btDevice.getAddress(); |
| 3815 | mDeviceLogger.log(new AudioEventLogger.StringEvent( |
| 3816 | "A2DP service connected: device addr=" + addr |
| 3817 | + " state=" + state)); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3818 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3819 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3820 | state, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 3821 | -1, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3822 | btDevice, |
| 3823 | delay); |
| 3824 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3825 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3826 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3827 | break; |
| 3828 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3829 | case BluetoothProfile.A2DP_SINK: |
| 3830 | deviceList = proxy.getConnectedDevices(); |
| 3831 | if (deviceList.size() > 0) { |
| 3832 | btDevice = deviceList.get(0); |
| 3833 | synchronized (mConnectedDevices) { |
| 3834 | int state = proxy.getConnectionState(btDevice); |
| 3835 | queueMsgUnderWakeLock(mAudioHandler, |
| 3836 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 3837 | state, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3838 | 0 /* arg2 unused */, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3839 | btDevice, |
| 3840 | 0 /* delay */); |
| 3841 | } |
| 3842 | } |
| 3843 | break; |
| 3844 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3845 | case BluetoothProfile.HEADSET: |
| 3846 | synchronized (mScoClients) { |
| 3847 | // Discard timeout message |
| 3848 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 3849 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3850 | setBtScoActiveDevice(mBluetoothHeadset.getActiveDevice()); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3851 | // Refresh SCO audio state |
| 3852 | checkScoAudioState(); |
| 3853 | // Continue pending action if any |
| 3854 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 3855 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3856 | boolean status = false; |
| 3857 | if (mBluetoothHeadsetDevice != null) { |
| 3858 | switch (mScoAudioState) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3859 | case SCO_STATE_ACTIVATE_REQ: |
| 3860 | status = connectBluetoothScoAudioHelper(mBluetoothHeadset, |
| 3861 | mBluetoothHeadsetDevice, mScoAudioMode); |
| 3862 | if (status) { |
| 3863 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 3864 | } |
| 3865 | break; |
| 3866 | case SCO_STATE_DEACTIVATE_REQ: |
| 3867 | status = disconnectBluetoothScoAudioHelper(mBluetoothHeadset, |
| 3868 | mBluetoothHeadsetDevice, mScoAudioMode); |
| 3869 | if (status) { |
| 3870 | mScoAudioState = SCO_STATE_DEACTIVATING; |
| 3871 | } |
| 3872 | break; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | if (!status) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 3876 | mScoAudioState = SCO_STATE_INACTIVE; |
| 3877 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3878 | } |
| 3879 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3880 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3881 | break; |
| 3882 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3883 | case BluetoothProfile.HEARING_AID: |
| 3884 | synchronized (mConnectedDevices) { |
| 3885 | synchronized (mHearingAidLock) { |
| 3886 | mHearingAid = (BluetoothHearingAid) proxy; |
| 3887 | deviceList = mHearingAid.getConnectedDevices(); |
| 3888 | if (deviceList.size() > 0) { |
| 3889 | btDevice = deviceList.get(0); |
| 3890 | int state = mHearingAid.getConnectionState(btDevice); |
| 3891 | int intState = (state == BluetoothHearingAid.STATE_CONNECTED) ? 1 : 0; |
| 3892 | int delay = checkSendBecomingNoisyIntent( |
| 3893 | AudioSystem.DEVICE_OUT_HEARING_AID, intState, |
| 3894 | AudioSystem.DEVICE_NONE); |
| 3895 | queueMsgUnderWakeLock(mAudioHandler, |
| 3896 | MSG_SET_HEARING_AID_CONNECTION_STATE, |
| 3897 | state, |
| 3898 | 0 /* arg2 unused */, |
| 3899 | btDevice, |
| 3900 | delay); |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | break; |
| 3906 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3907 | default: |
| 3908 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3909 | } |
| 3910 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3911 | public void onServiceDisconnected(int profile) { |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3912 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3913 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3914 | case BluetoothProfile.A2DP: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3915 | disconnectA2dp(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3916 | break; |
| 3917 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3918 | case BluetoothProfile.A2DP_SINK: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3919 | disconnectA2dpSink(); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3920 | break; |
| 3921 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3922 | case BluetoothProfile.HEADSET: |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3923 | disconnectHeadset(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3924 | break; |
| 3925 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3926 | case BluetoothProfile.HEARING_AID: |
| 3927 | disconnectHearingAid(); |
| 3928 | break; |
| 3929 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3930 | default: |
| 3931 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3935 | |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3936 | void disconnectAllBluetoothProfiles() { |
| 3937 | disconnectA2dp(); |
| 3938 | disconnectA2dpSink(); |
| 3939 | disconnectHeadset(); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3940 | disconnectHearingAid(); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | void disconnectA2dp() { |
| 3944 | synchronized (mConnectedDevices) { |
| 3945 | synchronized (mA2dpAvrcpLock) { |
| 3946 | ArraySet<String> toRemove = null; |
| 3947 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
| 3948 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3949 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3950 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
| 3951 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3952 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3953 | } |
| 3954 | } |
| 3955 | if (toRemove != null) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 3956 | int delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3957 | 0, AudioSystem.DEVICE_NONE); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3958 | for (int i = 0; i < toRemove.size(); i++) { |
| 3959 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
| 3960 | } |
| 3961 | } |
| 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | void disconnectA2dpSink() { |
| 3967 | synchronized (mConnectedDevices) { |
| 3968 | ArraySet<String> toRemove = null; |
| 3969 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
| 3970 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3971 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3972 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
| 3973 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3974 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3975 | } |
| 3976 | } |
| 3977 | if (toRemove != null) { |
| 3978 | for (int i = 0; i < toRemove.size(); i++) { |
| 3979 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
| 3980 | } |
| 3981 | } |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | void disconnectHeadset() { |
| 3986 | synchronized (mScoClients) { |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 3987 | setBtScoActiveDevice(null); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 3988 | mBluetoothHeadset = null; |
| 3989 | } |
| 3990 | } |
| 3991 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3992 | void disconnectHearingAid() { |
| 3993 | synchronized (mConnectedDevices) { |
| 3994 | synchronized (mHearingAidLock) { |
| 3995 | ArraySet<String> toRemove = null; |
| 3996 | // Disconnect ALL DEVICE_OUT_HEARING_AID devices |
| 3997 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3998 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
| 3999 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_HEARING_AID) { |
| 4000 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 4001 | toRemove.add(deviceSpec.mDeviceAddress); |
| 4002 | } |
| 4003 | } |
| 4004 | if (toRemove != null) { |
| 4005 | int delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 4006 | 0, AudioSystem.DEVICE_NONE); |
| 4007 | for (int i = 0; i < toRemove.size(); i++) { |
| 4008 | makeHearingAidDeviceUnavailable(toRemove.valueAt(i) /*, delay*/); |
| 4009 | } |
| 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | } |
| 4014 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4015 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4016 | synchronized (mSafeMediaVolumeState) { |
| 4017 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4018 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 4019 | |
| 4020 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4021 | sendMsg(mAudioHandler, |
| 4022 | MSG_CHECK_MUSIC_ACTIVE, |
| 4023 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4024 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4025 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4026 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4027 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4028 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4029 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4030 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4031 | // Approximate cumulative active music time |
| 4032 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 4033 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4034 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4035 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4036 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4037 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | } |
| 4041 | } |
| 4042 | } |
| 4043 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4044 | private void saveMusicActiveMs() { |
| 4045 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 4046 | } |
| 4047 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4048 | private int getSafeUsbMediaVolumeIndex() |
| 4049 | { |
| 4050 | // determine UI volume index corresponding to the wanted safe gain in dBFS |
| 4051 | int min = MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 4052 | int max = MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 4053 | |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 4054 | mSafeUsbMediaVolumeDbfs = mContext.getResources().getInteger( |
| 4055 | com.android.internal.R.integer.config_safe_media_volume_usb_mB) / 100.0f; |
| 4056 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4057 | while (Math.abs(max-min) > 1) { |
| 4058 | int index = (max + min) / 2; |
| 4059 | float gainDB = AudioSystem.getStreamVolumeDB( |
| 4060 | AudioSystem.STREAM_MUSIC, index, AudioSystem.DEVICE_OUT_USB_HEADSET); |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 4061 | if (Float.isNaN(gainDB)) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4062 | //keep last min in case of read error |
| 4063 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 4064 | } else if (gainDB == mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4065 | min = index; |
| 4066 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 4067 | } else if (gainDB < mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4068 | min = index; |
| 4069 | } else { |
| 4070 | max = index; |
| 4071 | } |
| 4072 | } |
| 4073 | return min * 10; |
| 4074 | } |
| 4075 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4076 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4077 | synchronized (mSafeMediaVolumeState) { |
| 4078 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 4079 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 4080 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 4081 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 4082 | |
| 4083 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 4084 | |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 4085 | boolean safeMediaVolumeEnabled = |
| 4086 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 4087 | || mContext.getResources().getBoolean( |
| 4088 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4089 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 4090 | boolean safeMediaVolumeBypass = |
| 4091 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 4092 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4093 | // The persisted state is either "disabled" or "active": this is the state applied |
| 4094 | // next time we boot and cannot be "inactive" |
| 4095 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 4096 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4097 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 4098 | // The state can already be "inactive" here if the user has forced it before |
| 4099 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 4100 | // it to "active". |
| 4101 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4102 | if (mMusicActiveMs == 0) { |
| 4103 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4104 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4105 | } else { |
| 4106 | // We have existing playback time recorded, already confirmed. |
| 4107 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 4108 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4109 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4110 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4111 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4112 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 4113 | } |
| 4114 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4115 | sendMsg(mAudioHandler, |
| 4116 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 4117 | SENDMSG_QUEUE, |
| 4118 | persistedState, |
| 4119 | 0, |
| 4120 | null, |
| 4121 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4126 | /////////////////////////////////////////////////////////////////////////// |
| 4127 | // Internal methods |
| 4128 | /////////////////////////////////////////////////////////////////////////// |
| 4129 | |
| 4130 | /** |
| 4131 | * Checks if the adjustment should change ringer mode instead of just |
| 4132 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 4133 | * indices on the stream states. |
| 4134 | */ |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 4135 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted, |
| 4136 | String caller, int flags) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4137 | int result = FLAG_ADJUST_VOLUME; |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 4138 | if (isPlatformTelevision() || mIsSingleVolume) { |
Hank Freund | 21003f6 | 2015-12-08 09:05:46 -0800 | [diff] [blame] | 4139 | return result; |
| 4140 | } |
| 4141 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4142 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4144 | switch (ringerMode) { |
| 4145 | case RINGER_MODE_NORMAL: |
| 4146 | if (direction == AudioManager.ADJUST_LOWER) { |
| 4147 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 4148 | // "step" is the delta in internal index units corresponding to a |
| 4149 | // change of 1 in UI index units. |
| 4150 | // Because of rounding when rescaling from one stream index range to its alias |
| 4151 | // index range, we cannot simply test oldIndex == step: |
| 4152 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 4153 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4154 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4155 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4156 | } |
| 4157 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 4158 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4159 | ringerMode = RINGER_MODE_SILENT; |
| 4160 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 4161 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4162 | } else if (mIsSingleVolume && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4163 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4164 | if (mHasVibrator) { |
| 4165 | ringerMode = RINGER_MODE_VIBRATE; |
| 4166 | } else { |
| 4167 | ringerMode = RINGER_MODE_SILENT; |
| 4168 | } |
| 4169 | // Setting the ringer mode will toggle mute |
| 4170 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4171 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4172 | break; |
| 4173 | case RINGER_MODE_VIBRATE: |
| 4174 | if (!mHasVibrator) { |
| 4175 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 4176 | "but no vibrator is present"); |
| 4177 | break; |
| 4178 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4179 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4180 | // This is the case we were muted with the volume turned up |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4181 | if (mIsSingleVolume && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4182 | ringerMode = RINGER_MODE_NORMAL; |
| 4183 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4184 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4185 | final long diff = SystemClock.uptimeMillis() |
| 4186 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 4187 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 4188 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 4189 | ringerMode = RINGER_MODE_SILENT; |
| 4190 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 4191 | } else { |
| 4192 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 4193 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4194 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4195 | } else if (direction == AudioManager.ADJUST_RAISE |
| 4196 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 4197 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4198 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 4199 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4200 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4201 | break; |
| 4202 | case RINGER_MODE_SILENT: |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4203 | if (mIsSingleVolume && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4204 | // This is the case we were muted with the volume turned up |
| 4205 | ringerMode = RINGER_MODE_NORMAL; |
| 4206 | } else if (direction == AudioManager.ADJUST_RAISE |
| 4207 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 4208 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 4209 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4210 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4211 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4212 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4213 | ringerMode = RINGER_MODE_VIBRATE; |
| 4214 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4215 | // If we don't have a vibrator or they were toggling mute |
| 4216 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4217 | ringerMode = RINGER_MODE_NORMAL; |
| 4218 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4219 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 4220 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4221 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4222 | break; |
| 4223 | default: |
| 4224 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 4225 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4226 | } |
| 4227 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 4228 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 4229 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller) |
| 4230 | && (flags & AudioManager.FLAG_FROM_KEY) == 0) { |
| 4231 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 4232 | } |
| 4233 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4234 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4235 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4236 | mPrevVolDirection = direction; |
| 4237 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 4238 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4239 | } |
| 4240 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4241 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4242 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 4243 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4244 | } |
| 4245 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4246 | private boolean shouldZenMuteStream(int streamType) { |
| 4247 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 4248 | return false; |
| 4249 | } |
| 4250 | |
| 4251 | NotificationManager.Policy zenPolicy = mNm.getNotificationPolicy(); |
| 4252 | final boolean muteAlarms = (zenPolicy.priorityCategories |
| 4253 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0; |
| 4254 | final boolean muteMedia = (zenPolicy.priorityCategories |
| 4255 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0; |
| 4256 | final boolean muteSystem = (zenPolicy.priorityCategories |
| 4257 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0; |
| 4258 | final boolean muteNotificationAndRing = ZenModeConfig |
| 4259 | .areAllPriorityOnlyNotificationZenSoundsMuted(mNm.getNotificationPolicy()); |
| 4260 | return muteAlarms && isAlarm(streamType) |
| 4261 | || muteMedia && isMedia(streamType) |
| 4262 | || muteSystem && isSystem(streamType) |
| 4263 | || muteNotificationAndRing && isNotificationOrRinger(streamType); |
| 4264 | } |
| 4265 | |
| 4266 | private boolean isStreamMutedByRingerOrZenMode(int streamType) { |
| 4267 | return (mRingerAndZenModeMutedStreams & (1 << streamType)) != 0; |
| 4268 | } |
| 4269 | |
| 4270 | /** |
| 4271 | * DND total silence: media and alarms streams are tied to the muted ringer |
| 4272 | * {@link ZenModeHelper.RingerModeDelegate#getRingerModeAffectedStreams(int)} |
| 4273 | * DND alarms only: notification, ringer + system muted (by default tied to muted ringer mode) |
| 4274 | * DND priority only: alarms, media, system streams can be muted separate from ringer based on |
| 4275 | * zenPolicy (this method determines which streams) |
| 4276 | * @return true if changed, else false |
| 4277 | */ |
| 4278 | private boolean updateZenModeAffectedStreams() { |
| 4279 | int zenModeAffectedStreams = 0; |
| 4280 | if (mSystemReady && mNm.getZenMode() == Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 4281 | NotificationManager.Policy zenPolicy = mNm.getNotificationPolicy(); |
| 4282 | if ((zenPolicy.priorityCategories |
| 4283 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { |
| 4284 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_ALARM; |
| 4285 | } |
| 4286 | |
| 4287 | if ((zenPolicy.priorityCategories |
| 4288 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0) { |
| 4289 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_MUSIC; |
| 4290 | } |
| 4291 | |
| 4292 | if ((zenPolicy.priorityCategories |
| 4293 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0) { |
| 4294 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_SYSTEM; |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | if (mZenModeAffectedStreams != zenModeAffectedStreams) { |
| 4299 | mZenModeAffectedStreams = zenModeAffectedStreams; |
| 4300 | return true; |
| 4301 | } |
| 4302 | |
| 4303 | return false; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4306 | @GuardedBy("mSettingsLock") |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4307 | private boolean updateRingerAndZenModeAffectedStreams() { |
| 4308 | boolean updatedZenModeAffectedStreams = updateZenModeAffectedStreams(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 4309 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4310 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 4311 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 4312 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 4313 | UserHandle.USER_CURRENT); |
| 4314 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4315 | if (mIsSingleVolume) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 4316 | ringerModeAffectedStreams = 0; |
| 4317 | } else if (mRingerModeDelegate != null) { |
| 4318 | ringerModeAffectedStreams = mRingerModeDelegate |
| 4319 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4320 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4321 | if (mCameraSoundForced) { |
| 4322 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4323 | } else { |
| 4324 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4325 | } |
| 4326 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 4327 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 4328 | } else { |
| 4329 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 4330 | } |
| 4331 | |
| 4332 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 4333 | Settings.System.putIntForUser(mContentResolver, |
| 4334 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 4335 | ringerModeAffectedStreams, |
| 4336 | UserHandle.USER_CURRENT); |
| 4337 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 4338 | return true; |
| 4339 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4340 | return updatedZenModeAffectedStreams; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 4343 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4344 | public boolean isStreamAffectedByMute(int streamType) { |
| 4345 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 4346 | } |
| 4347 | |
| 4348 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4349 | switch (direction) { |
| 4350 | case AudioManager.ADJUST_LOWER: |
| 4351 | case AudioManager.ADJUST_RAISE: |
| 4352 | case AudioManager.ADJUST_SAME: |
| 4353 | case AudioManager.ADJUST_MUTE: |
| 4354 | case AudioManager.ADJUST_UNMUTE: |
| 4355 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 4356 | break; |
| 4357 | default: |
| 4358 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | private void ensureValidStreamType(int streamType) { |
| 4363 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 4364 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 4365 | } |
| 4366 | } |
| 4367 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4368 | private boolean isMuteAdjust(int adjust) { |
| 4369 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 4370 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 4371 | } |
| 4372 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4373 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 4374 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4375 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 4376 | TelecomManager telecomManager = |
| 4377 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 4378 | |
| 4379 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 4380 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 4381 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 4382 | |
Eric Laurent | da1af76 | 2017-12-15 16:54:35 -0800 | [diff] [blame] | 4383 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION || |
| 4384 | getMode() == AudioManager.MODE_IN_CALL); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4385 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4386 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 4387 | /** |
| 4388 | * For code clarity for getActiveStreamType(int) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4389 | * @param delay_ms max time since last stream activity to consider |
| 4390 | * @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] | 4391 | * in the last "delay_ms" ms. |
| 4392 | */ |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4393 | private boolean wasStreamActiveRecently(int stream, int delay_ms) { |
| 4394 | return AudioSystem.isStreamActive(stream, delay_ms) |
| 4395 | || AudioSystem.isStreamActiveRemotely(stream, delay_ms); |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 4396 | } |
| 4397 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4398 | private int getActiveStreamType(int suggestedStreamType) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4399 | if (mIsSingleVolume |
| 4400 | && suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 4401 | return AudioSystem.STREAM_MUSIC; |
| 4402 | } |
| 4403 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4404 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4405 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4406 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4407 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 4408 | == AudioSystem.FORCE_BT_SCO) { |
| 4409 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 4410 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 4411 | } else { |
| 4412 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 4413 | return AudioSystem.STREAM_VOICE_CALL; |
| 4414 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4415 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4416 | if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4417 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4418 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 4419 | return AudioSystem.STREAM_RING; |
| 4420 | } else if (wasStreamActiveRecently( |
| 4421 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 4422 | if (DEBUG_VOL) |
| 4423 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 4424 | return AudioSystem.STREAM_NOTIFICATION; |
| 4425 | } else { |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 4426 | if (DEBUG_VOL) { |
| 4427 | Log.v(TAG, "getActiveStreamType: Forcing DEFAULT_VOL_STREAM_NO_PLAYBACK(" |
| 4428 | + DEFAULT_VOL_STREAM_NO_PLAYBACK + ") b/c default"); |
| 4429 | } |
| 4430 | return DEFAULT_VOL_STREAM_NO_PLAYBACK; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4431 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4432 | } else if ( |
| 4433 | wasStreamActiveRecently(AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4434 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4435 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 4436 | return AudioSystem.STREAM_NOTIFICATION; |
| 4437 | } else if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4438 | if (DEBUG_VOL) |
| 4439 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 4440 | return AudioSystem.STREAM_RING; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4441 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4442 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4443 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4444 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 4445 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4446 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4447 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 4448 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4449 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4450 | return AudioSystem.STREAM_VOICE_CALL; |
| 4451 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4452 | } else if (AudioSystem.isStreamActive( |
| 4453 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4454 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4455 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4456 | } else if (AudioSystem.isStreamActive( |
| 4457 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4458 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 4459 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4460 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4461 | if (AudioSystem.isStreamActive( |
| 4462 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 4463 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 4464 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 4465 | } else if (AudioSystem.isStreamActive( |
| 4466 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 4467 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 4468 | return AudioSystem.STREAM_RING; |
| 4469 | } else { |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 4470 | if (DEBUG_VOL) { |
| 4471 | Log.v(TAG, "getActiveStreamType: Forcing DEFAULT_VOL_STREAM_NO_PLAYBACK(" |
| 4472 | + DEFAULT_VOL_STREAM_NO_PLAYBACK + ") b/c default"); |
| 4473 | } |
| 4474 | return DEFAULT_VOL_STREAM_NO_PLAYBACK; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4475 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 4476 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4477 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4478 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4479 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 4480 | + suggestedStreamType); |
| 4481 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4482 | } |
| 4483 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4484 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4485 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 4486 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4487 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 4488 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 4489 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4490 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | private void broadcastVibrateSetting(int vibrateType) { |
| 4494 | // Send broadcast |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 4495 | if (mActivityManagerInternal.isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4496 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 4497 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 4498 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4499 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4504 | /** |
| 4505 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 4506 | * Note that the wake lock needs to be released after the message has been handled. |
| 4507 | */ |
| 4508 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 4509 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4510 | final long ident = Binder.clearCallingIdentity(); |
| 4511 | // Always acquire the wake lock as AudioService because it is released by the |
| 4512 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4513 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 4514 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4515 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 4516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4517 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4518 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4519 | 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] | 4520 | |
| 4521 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 4522 | handler.removeMessages(msg); |
| 4523 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 4524 | return; |
| 4525 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4526 | synchronized (mLastDeviceConnectMsgTime) { |
| 4527 | long time = SystemClock.uptimeMillis() + delay; |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 4528 | |
| 4529 | if (msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 4530 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE || |
| 4531 | msg == MSG_SET_HEARING_AID_CONNECTION_STATE || |
| 4532 | msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 4533 | msg == MSG_A2DP_DEVICE_CONFIG_CHANGE || |
| 4534 | msg == MSG_BTA2DP_DOCK_TIMEOUT) { |
| 4535 | if (mLastDeviceConnectMsgTime >= time) { |
| 4536 | // add a little delay to make sure messages are ordered as expected |
| 4537 | time = mLastDeviceConnectMsgTime + 30; |
| 4538 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4539 | mLastDeviceConnectMsgTime = time; |
| 4540 | } |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 4541 | |
| 4542 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 4547 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4548 | == PackageManager.PERMISSION_GRANTED) { |
| 4549 | return true; |
| 4550 | } |
| 4551 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 4552 | + Binder.getCallingPid() |
| 4553 | + ", uid=" + Binder.getCallingUid(); |
| 4554 | Log.w(TAG, msg); |
| 4555 | return false; |
| 4556 | } |
| 4557 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4558 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4559 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4560 | if ((device & (device - 1)) != 0) { |
| 4561 | // Multiple device selection is either: |
| 4562 | // - speaker + one other device: give priority to speaker in this case. |
| 4563 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 4564 | // retain the device on the A2DP output as the other must not correspond to an active |
| 4565 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4566 | // - 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] | 4567 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 4568 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 4569 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 4570 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 4571 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 4572 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 4573 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 4574 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4575 | } else { |
| 4576 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 4577 | } |
| 4578 | } |
| 4579 | return device; |
| 4580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4581 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4582 | private int getDevicesForStream(int stream) { |
| 4583 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 4584 | } |
| 4585 | |
| 4586 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 4587 | ensureValidStreamType(stream); |
| 4588 | synchronized (VolumeStreamState.class) { |
| 4589 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | private void observeDevicesForStreams(int skipStream) { |
| 4594 | synchronized (VolumeStreamState.class) { |
| 4595 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 4596 | if (stream != skipStream) { |
| 4597 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 4598 | } |
| 4599 | } |
| 4600 | } |
| 4601 | } |
| 4602 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4603 | /* |
| 4604 | * A class just for packaging up a set of connection parameters. |
| 4605 | */ |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 4606 | class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4607 | public final int mType; |
| 4608 | public final int mState; |
| 4609 | public final String mAddress; |
| 4610 | public final String mName; |
| 4611 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4612 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4613 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 4614 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4615 | mType = type; |
| 4616 | mState = state; |
| 4617 | mAddress = address; |
| 4618 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4619 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4623 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 4624 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4625 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4626 | if (DEBUG_DEVICES) { |
| 4627 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 4628 | + address + ")"); |
| 4629 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4630 | int delay = checkSendBecomingNoisyIntent(type, state, AudioSystem.DEVICE_NONE); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4631 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4632 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4633 | 0 /* arg1 unused */, |
| 4634 | 0 /* arg2 unused */, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4635 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4636 | delay); |
| 4637 | } |
| 4638 | } |
| 4639 | |
Jakub Pawlowski | 10c9061 | 2018-02-21 13:28:46 -0800 | [diff] [blame] | 4640 | @Override |
| 4641 | public void setHearingAidDeviceConnectionState(BluetoothDevice device, int state) |
| 4642 | { |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 4643 | Log.i(TAG, "setBluetoothHearingAidDeviceConnectionState"); |
| 4644 | |
| 4645 | setBluetoothHearingAidDeviceConnectionState( |
| 4646 | device, state, false /* suppressNoisyIntent */, AudioSystem.DEVICE_NONE); |
| 4647 | } |
| 4648 | |
| 4649 | public int setBluetoothHearingAidDeviceConnectionState( |
| 4650 | BluetoothDevice device, int state, boolean suppressNoisyIntent, |
| 4651 | int musicDevice) |
| 4652 | { |
| 4653 | int delay; |
| 4654 | synchronized (mConnectedDevices) { |
| 4655 | if (!suppressNoisyIntent) { |
| 4656 | int intState = (state == BluetoothHearingAid.STATE_CONNECTED) ? 1 : 0; |
| 4657 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 4658 | intState, musicDevice); |
| 4659 | } else { |
| 4660 | delay = 0; |
| 4661 | } |
| 4662 | queueMsgUnderWakeLock(mAudioHandler, |
| 4663 | MSG_SET_HEARING_AID_CONNECTION_STATE, |
| 4664 | state, |
| 4665 | 0 /* arg2 unused */, |
| 4666 | device, |
| 4667 | delay); |
| 4668 | } |
| 4669 | return delay; |
Jakub Pawlowski | 10c9061 | 2018-02-21 13:28:46 -0800 | [diff] [blame] | 4670 | } |
| 4671 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4672 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4673 | { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4674 | return setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent( |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4675 | device, state, profile, false /* suppressNoisyIntent */, -1 /* a2dpVolume */); |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | public int setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(BluetoothDevice device, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4679 | int state, int profile, boolean suppressNoisyIntent, int a2dpVolume) |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4680 | { |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 4681 | mDeviceLogger.log(new AudioEventLogger.StringEvent( |
| 4682 | "setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent state=" + state |
| 4683 | // only querying address as this is the only readily available field on the device |
| 4684 | + " addr=" + device.getAddress() |
| 4685 | + " prof=" + profile + " supprNoisy=" + suppressNoisyIntent |
| 4686 | + " vol=" + a2dpVolume)); |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 4687 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, device)) { |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 4688 | mDeviceLogger.log(new AudioEventLogger.StringEvent("A2DP connection state ignored")); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4689 | return 0; |
| 4690 | } |
| 4691 | return setBluetoothA2dpDeviceConnectionStateInt( |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4692 | device, state, profile, suppressNoisyIntent, AudioSystem.DEVICE_NONE, a2dpVolume); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | public int setBluetoothA2dpDeviceConnectionStateInt( |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4696 | BluetoothDevice device, int state, int profile, boolean suppressNoisyIntent, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4697 | int musicDevice, int a2dpVolume) |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4698 | { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4699 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4700 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 4701 | throw new IllegalArgumentException("invalid profile " + profile); |
| 4702 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4703 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 4704 | if (profile == BluetoothProfile.A2DP && !suppressNoisyIntent) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4705 | int intState = (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4706 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 4707 | intState, musicDevice); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4708 | } else { |
| 4709 | delay = 0; |
| 4710 | } |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 4711 | |
| 4712 | if (DEBUG_DEVICES) { |
| 4713 | Log.d(TAG, "setBluetoothA2dpDeviceConnectionStateInt device: " + device |
| 4714 | + " state: " + state + " delay(ms): " + delay |
| 4715 | + " suppressNoisyIntent: " + suppressNoisyIntent); |
| 4716 | } |
| 4717 | |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 4718 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4719 | (profile == BluetoothProfile.A2DP ? |
| 4720 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4721 | state, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4722 | a2dpVolume, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4723 | device, |
| 4724 | delay); |
| 4725 | } |
| 4726 | return delay; |
| 4727 | } |
| 4728 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 4729 | public void handleBluetoothA2dpDeviceConfigChange(BluetoothDevice device) |
| 4730 | { |
| 4731 | synchronized (mConnectedDevices) { |
| 4732 | queueMsgUnderWakeLock(mAudioHandler, |
| 4733 | MSG_A2DP_DEVICE_CONFIG_CHANGE, |
| 4734 | 0 /* arg1 unused */, |
| 4735 | 0 /* arg1 unused */, |
| 4736 | device, |
| 4737 | 0 /* delay */); |
| 4738 | } |
| 4739 | } |
| 4740 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4741 | private static final int DEVICE_MEDIA_UNMUTED_ON_PLUG = |
| 4742 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 4743 | AudioSystem.DEVICE_OUT_LINE | |
| 4744 | AudioSystem.DEVICE_OUT_ALL_A2DP | |
| 4745 | AudioSystem.DEVICE_OUT_ALL_USB | |
| 4746 | AudioSystem.DEVICE_OUT_HDMI; |
| 4747 | |
| 4748 | private void onAccessoryPlugMediaUnmute(int newDevice) { |
| 4749 | if (DEBUG_VOL) { |
| 4750 | Log.i(TAG, String.format("onAccessoryPlugMediaUnmute newDevice=%d [%s]", |
| 4751 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4752 | } |
| 4753 | synchronized (mConnectedDevices) { |
Eric Laurent | 7fb83d9 | 2017-09-28 09:36:36 -0700 | [diff] [blame] | 4754 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 4755 | && (newDevice & DEVICE_MEDIA_UNMUTED_ON_PLUG) != 0 |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4756 | && mStreamStates[AudioSystem.STREAM_MUSIC].mIsMuted |
| 4757 | && mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(newDevice) != 0 |
| 4758 | && (newDevice & AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC)) != 0) |
| 4759 | { |
| 4760 | if (DEBUG_VOL) { |
| 4761 | Log.i(TAG, String.format(" onAccessoryPlugMediaUnmute unmuting device=%d [%s]", |
| 4762 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4763 | } |
| 4764 | mStreamStates[AudioSystem.STREAM_MUSIC].mute(false); |
| 4765 | } |
| 4766 | } |
| 4767 | } |
| 4768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4769 | /////////////////////////////////////////////////////////////////////////// |
| 4770 | // Inner classes |
| 4771 | /////////////////////////////////////////////////////////////////////////// |
| 4772 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4773 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 4774 | // 1 mScoclient OR mSafeMediaVolumeState |
| 4775 | // 2 mSetModeDeathHandlers |
| 4776 | // 3 mSettingsLock |
| 4777 | // 4 VolumeStreamState.class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4778 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4779 | private final int mStreamType; |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 4780 | private int mIndexMin; |
| 4781 | private int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4782 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4783 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4784 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4785 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4786 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4787 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4788 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4789 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4790 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4791 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4792 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4793 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4794 | |
| 4795 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4796 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 4797 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 4798 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4799 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4800 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4801 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 4802 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4803 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 4804 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4805 | } |
| 4806 | |
| 4807 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 4808 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 4809 | if (devices == mObservedDevices) { |
| 4810 | return devices; |
| 4811 | } |
| 4812 | final int prevDevices = mObservedDevices; |
| 4813 | mObservedDevices = devices; |
| 4814 | if (checkOthers) { |
| 4815 | // one stream's devices have changed, check the others |
| 4816 | observeDevicesForStreams(mStreamType); |
| 4817 | } |
| 4818 | // log base stream changes to the event log |
| 4819 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4820 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 4821 | } |
| 4822 | sendBroadcastToAll(mStreamDevicesChanged |
| 4823 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 4824 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 4825 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4826 | } |
| 4827 | |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4828 | public @Nullable String getSettingNameForDevice(int device) { |
| 4829 | if (!hasValidSettingsName()) { |
| 4830 | return null; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4831 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4832 | final String suffix = AudioSystem.getOutputDeviceName(device); |
| 4833 | if (suffix.isEmpty()) { |
| 4834 | return mVolumeIndexSettingName; |
| 4835 | } |
| 4836 | return mVolumeIndexSettingName + "_" + suffix; |
| 4837 | } |
| 4838 | |
| 4839 | private boolean hasValidSettingsName() { |
| 4840 | return (mVolumeIndexSettingName != null && !mVolumeIndexSettingName.isEmpty()); |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4843 | public void readSettings() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4844 | synchronized (mSettingsLock) { |
| 4845 | synchronized (VolumeStreamState.class) { |
| 4846 | // force maximum volume on all streams if fixed volume property is set |
| 4847 | if (mUseFixedVolume) { |
| 4848 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 4849 | return; |
| 4850 | } |
| 4851 | // do not read system stream volume from settings: this stream is always aliased |
| 4852 | // to another stream type and its volume is never persisted. Values in settings can |
| 4853 | // only be stale values |
| 4854 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 4855 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
| 4856 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4857 | if (mCameraSoundForced) { |
| 4858 | index = mIndexMax; |
| 4859 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4860 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 4861 | return; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4862 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4863 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4864 | } |
| 4865 | synchronized (VolumeStreamState.class) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4866 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 4867 | |
| 4868 | for (int i = 0; remainingDevices != 0; i++) { |
| 4869 | int device = (1 << i); |
| 4870 | if ((device & remainingDevices) == 0) { |
| 4871 | continue; |
| 4872 | } |
| 4873 | remainingDevices &= ~device; |
| 4874 | |
| 4875 | // retrieve current volume for device |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4876 | // if no volume stored for current stream and device, use default volume if default |
| 4877 | // device, continue otherwise |
| 4878 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4879 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4880 | int index; |
| 4881 | if (!hasValidSettingsName()) { |
| 4882 | index = defaultIndex; |
| 4883 | } else { |
| 4884 | String name = getSettingNameForDevice(device); |
| 4885 | index = Settings.System.getIntForUser( |
| 4886 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 4887 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4888 | if (index == -1) { |
| 4889 | continue; |
| 4890 | } |
| 4891 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4892 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4893 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4895 | } |
| 4896 | |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4897 | private int getAbsoluteVolumeIndex(int index) { |
| 4898 | /* Special handling for Bluetooth Absolute Volume scenario |
| 4899 | * If we send full audio gain, some accessories are too loud even at its lowest |
| 4900 | * volume. We are not able to enumerate all such accessories, so here is the |
| 4901 | * workaround from phone side. |
| 4902 | * Pre-scale volume at lowest volume steps 1 2 and 3. |
| 4903 | * For volume step 0, set audio gain to 0 as some accessories won't mute on their end. |
| 4904 | */ |
| 4905 | if (index == 0) { |
| 4906 | // 0% for volume 0 |
| 4907 | index = 0; |
| 4908 | } else if (index == 1) { |
| 4909 | // 50% for volume 1 |
| 4910 | index = (int)(mIndexMax * 0.5) /10; |
| 4911 | } else if (index == 2) { |
| 4912 | // 70% for volume 2 |
| 4913 | index = (int)(mIndexMax * 0.70) /10; |
| 4914 | } else if (index == 3) { |
| 4915 | // 85% for volume 3 |
| 4916 | index = (int)(mIndexMax * 0.85) /10; |
| 4917 | } else { |
| 4918 | // otherwise, full gain |
| 4919 | index = (mIndexMax + 5)/10; |
| 4920 | } |
| 4921 | return index; |
| 4922 | } |
| 4923 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4924 | // must be called while synchronized VolumeStreamState.class |
| 4925 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4926 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4927 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4928 | index = 0; |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4929 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4930 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4931 | } else if ((device & mFullVolumeDevices) != 0) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4932 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4933 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4934 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4935 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4936 | index = (getIndex(device) + 5)/10; |
| 4937 | } |
| 4938 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4939 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4940 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4941 | public void applyAllVolumes() { |
| 4942 | synchronized (VolumeStreamState.class) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4943 | // apply device specific volumes first |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4944 | int index; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4945 | for (int i = 0; i < mIndexMap.size(); i++) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4946 | final int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4947 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4948 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4949 | index = 0; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4950 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 4951 | mAvrcpAbsVolSupported) { |
| 4952 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
| 4953 | } else if ((device & mFullVolumeDevices) != 0) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4954 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4955 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4956 | index = (mIndexMax + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4957 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4958 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4959 | } |
| 4960 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4961 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4962 | } |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4963 | // apply default volume last: by convention , default device volume will be used |
| 4964 | // by audio policy manager if no explicit volume is present for a given device type |
| 4965 | if (mIsMuted) { |
| 4966 | index = 0; |
| 4967 | } else { |
| 4968 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 4969 | } |
| 4970 | AudioSystem.setStreamVolumeIndex( |
| 4971 | mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4972 | } |
| 4973 | } |
| 4974 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4975 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 4976 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4977 | } |
| 4978 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4979 | public boolean setIndex(int index, int device, String caller) { |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4980 | boolean changed; |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4981 | int oldIndex; |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4982 | synchronized (mSettingsLock) { |
| 4983 | synchronized (VolumeStreamState.class) { |
| 4984 | oldIndex = getIndex(device); |
| 4985 | index = getValidIndex(index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4986 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 4987 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4988 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4989 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4990 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4991 | changed = oldIndex != index; |
| 4992 | // Apply change to all streams using this one as alias if: |
| 4993 | // - the index actually changed OR |
| 4994 | // - there is no volume index stored for this device on alias stream. |
| 4995 | // If changing volume of current device, also change volume of current |
| 4996 | // device on aliased stream |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4997 | final boolean isCurrentDevice = (device == getDeviceForStream(mStreamType)); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4998 | final int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4999 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 5000 | final VolumeStreamState aliasStreamState = mStreamStates[streamType]; |
| 5001 | if (streamType != mStreamType && |
| 5002 | mStreamVolumeAlias[streamType] == mStreamType && |
| 5003 | (changed || !aliasStreamState.hasIndexForDevice(device))) { |
| 5004 | final int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 5005 | aliasStreamState.setIndex(scaledIndex, device, caller); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 5006 | if (isCurrentDevice) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 5007 | aliasStreamState.setIndex(scaledIndex, |
| 5008 | getDeviceForStream(streamType), caller); |
| 5009 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5010 | } |
| 5011 | } |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 5012 | // Mirror changes in SPEAKER ringtone volume on SCO when |
| 5013 | if (changed && mStreamType == AudioSystem.STREAM_RING |
| 5014 | && device == AudioSystem.DEVICE_OUT_SPEAKER) { |
| 5015 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5016 | int otherDevice = mIndexMap.keyAt(i); |
| 5017 | if ((otherDevice & AudioSystem.DEVICE_OUT_ALL_SCO) != 0) { |
| 5018 | mIndexMap.put(otherDevice, index); |
| 5019 | } |
| 5020 | } |
| 5021 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5022 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5023 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 5024 | if (changed) { |
| 5025 | oldIndex = (oldIndex + 5) / 10; |
| 5026 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5027 | // log base stream changes to the event log |
| 5028 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 5029 | if (caller == null) { |
| 5030 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 5031 | } |
| 5032 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 5033 | caller); |
| 5034 | } |
| 5035 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 5036 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 5037 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 5038 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 5039 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 5040 | sendBroadcastToAll(mVolumeChanged); |
| 5041 | } |
| 5042 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5043 | } |
| 5044 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5045 | public int getIndex(int device) { |
| 5046 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5047 | int index = mIndexMap.get(device, -1); |
| 5048 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5049 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5050 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5051 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5052 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5053 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 5056 | public boolean hasIndexForDevice(int device) { |
| 5057 | synchronized (VolumeStreamState.class) { |
| 5058 | return (mIndexMap.get(device, -1) != -1); |
| 5059 | } |
| 5060 | } |
| 5061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5062 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5063 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5064 | } |
| 5065 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 5066 | public int getMinIndex() { |
| 5067 | return mIndexMin; |
| 5068 | } |
| 5069 | |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 5070 | /** |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 5071 | * Updates the min/max index values from another stream. Use this when changing the alias |
| 5072 | * for the current stream type. |
| 5073 | * @param sourceStreamType |
| 5074 | */ |
| 5075 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 5076 | @GuardedBy("VolumeStreamState.class") |
| 5077 | public void refreshRange(int sourceStreamType) { |
| 5078 | mIndexMin = MIN_STREAM_VOLUME[sourceStreamType] * 10; |
| 5079 | mIndexMax = MAX_STREAM_VOLUME[sourceStreamType] * 10; |
| 5080 | // verify all current volumes are within bounds |
| 5081 | for (int i = 0 ; i < mIndexMap.size(); i++) { |
| 5082 | final int device = mIndexMap.keyAt(i); |
| 5083 | final int index = mIndexMap.valueAt(i); |
| 5084 | mIndexMap.put(device, getValidIndex(index)); |
| 5085 | } |
| 5086 | } |
| 5087 | |
| 5088 | /** |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 5089 | * Copies all device/index pairs from the given VolumeStreamState after initializing |
| 5090 | * them with the volume for DEVICE_OUT_DEFAULT. No-op if the source VolumeStreamState |
| 5091 | * has the same stream type as this instance. |
| 5092 | * @param srcStream |
| 5093 | * @param caller |
| 5094 | */ |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5095 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 5096 | @GuardedBy("VolumeStreamState.class") |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5097 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 5098 | if (mStreamType == srcStream.mStreamType) { |
| 5099 | return; |
| 5100 | } |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5101 | int srcStreamType = srcStream.getStreamType(); |
| 5102 | // apply default device volume from source stream to all devices first in case |
| 5103 | // some devices are present in this stream state but not in source stream state |
| 5104 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
| 5105 | index = rescaleIndex(index, srcStreamType, mStreamType); |
| 5106 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5107 | mIndexMap.put(mIndexMap.keyAt(i), index); |
| 5108 | } |
| 5109 | // Now apply actual volume for devices in source stream state |
| 5110 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 5111 | for (int i = 0; i < srcMap.size(); i++) { |
| 5112 | int device = srcMap.keyAt(i); |
| 5113 | index = srcMap.valueAt(i); |
| 5114 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 5115 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5116 | setIndex(index, device, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 5117 | } |
| 5118 | } |
| 5119 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5120 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 5121 | @GuardedBy("VolumeStreamState.class") |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5122 | public void setAllIndexesToMax() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5123 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5124 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5125 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5128 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 5129 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5130 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5131 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 5132 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5133 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 5134 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5135 | // Set the new mute volume. This propagates the values to |
| 5136 | // the audio system, otherwise the volume won't be changed |
| 5137 | // at the lower level. |
| 5138 | sendMsg(mAudioHandler, |
| 5139 | MSG_SET_ALL_VOLUMES, |
| 5140 | SENDMSG_QUEUE, |
| 5141 | 0, |
| 5142 | 0, |
| 5143 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 5144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 5146 | if (changed) { |
| 5147 | // Stream mute changed, fire the intent. |
| 5148 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 5149 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 5150 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 5151 | sendBroadcastToAll(intent); |
| 5152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5153 | } |
| 5154 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 5155 | public int getStreamType() { |
| 5156 | return mStreamType; |
| 5157 | } |
| 5158 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5159 | public void checkFixedVolumeDevices() { |
| 5160 | synchronized (VolumeStreamState.class) { |
| 5161 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 5162 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5163 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5164 | int device = mIndexMap.keyAt(i); |
| 5165 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 5166 | if (((device & mFullVolumeDevices) != 0) |
| 5167 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5168 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5169 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5170 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5171 | } |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5176 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 5177 | if (index < mIndexMin) { |
| 5178 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 5179 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5180 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | return index; |
| 5184 | } |
| 5185 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5186 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5187 | pw.print(" Muted: "); |
| 5188 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 5189 | pw.print(" Min: "); |
| 5190 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5191 | pw.print(" Max: "); |
| 5192 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5193 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5194 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 5195 | if (i > 0) { |
| 5196 | pw.print(", "); |
| 5197 | } |
| 5198 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5199 | pw.print(Integer.toHexString(device)); |
| 5200 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 5201 | : AudioSystem.getOutputDeviceName(device); |
| 5202 | if (!deviceName.isEmpty()) { |
| 5203 | pw.print(" ("); |
| 5204 | pw.print(deviceName); |
| 5205 | pw.print(")"); |
| 5206 | } |
| 5207 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 5208 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 5209 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5210 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5211 | pw.println(); |
| 5212 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5213 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5214 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 5215 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 5216 | // (the default device is not returned by getDevicesForStream) |
| 5217 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 5218 | if ((devices & device) != 0) { |
| 5219 | if (n++ > 0) { |
| 5220 | pw.print(", "); |
| 5221 | } |
| 5222 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 5223 | } |
| 5224 | i++; |
| 5225 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | /** Thread that handles native AudioSystem control. */ |
| 5230 | private class AudioSystemThread extends Thread { |
| 5231 | AudioSystemThread() { |
| 5232 | super("AudioService"); |
| 5233 | } |
| 5234 | |
| 5235 | @Override |
| 5236 | public void run() { |
| 5237 | // Set this thread up so the handler will work on it |
| 5238 | Looper.prepare(); |
| 5239 | |
| 5240 | synchronized(AudioService.this) { |
| 5241 | mAudioHandler = new AudioHandler(); |
| 5242 | |
| 5243 | // Notify that the handler has been created |
| 5244 | AudioService.this.notify(); |
| 5245 | } |
| 5246 | |
| 5247 | // Listen for volume change requests that are set by VolumePanel |
| 5248 | Looper.loop(); |
| 5249 | } |
| 5250 | } |
| 5251 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5252 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5253 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5254 | synchronized (VolumeStreamState.class) { |
| 5255 | // Apply volume |
| 5256 | streamState.applyDeviceVolume_syncVSS(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5257 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5258 | // Apply change to all streams using this one as alias |
| 5259 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 5260 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 5261 | if (streamType != streamState.mStreamType && |
| 5262 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 5263 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 5264 | // that may have a different device selected |
| 5265 | int streamDevice = getDeviceForStream(streamType); |
| 5266 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 5267 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 5268 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 5269 | } |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5270 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5271 | } |
| 5272 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5273 | } |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5274 | // Post a persist volume msg |
| 5275 | sendMsg(mAudioHandler, |
| 5276 | MSG_PERSIST_VOLUME, |
| 5277 | SENDMSG_QUEUE, |
| 5278 | device, |
| 5279 | 0, |
| 5280 | streamState, |
| 5281 | PERSIST_DELAY); |
| 5282 | |
| 5283 | } |
| 5284 | |
| 5285 | /** Handles internal volume messages in separate volume thread. */ |
| 5286 | private class AudioHandler extends Handler { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5287 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5288 | private void setAllVolumes(VolumeStreamState streamState) { |
| 5289 | |
| 5290 | // Apply volume |
| 5291 | streamState.applyAllVolumes(); |
| 5292 | |
| 5293 | // Apply change to all streams using this one as alias |
| 5294 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 5295 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 5296 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 5297 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5298 | mStreamStates[streamType].applyAllVolumes(); |
| 5299 | } |
| 5300 | } |
| 5301 | } |
| 5302 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5303 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 5304 | if (mUseFixedVolume) { |
| 5305 | return; |
| 5306 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 5307 | if (mIsSingleVolume && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5308 | return; |
| 5309 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 5310 | if (streamState.hasValidSettingsName()) { |
| 5311 | System.putIntForUser(mContentResolver, |
| 5312 | streamState.getSettingNameForDevice(device), |
| 5313 | (streamState.getIndex(device) + 5)/ 10, |
| 5314 | UserHandle.USER_CURRENT); |
| 5315 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5316 | } |
| 5317 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5318 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 5319 | if (mUseFixedVolume) { |
| 5320 | return; |
| 5321 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 5322 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5323 | } |
| 5324 | |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5325 | private String getSoundEffectFilePath(int effectType) { |
| 5326 | String filePath = Environment.getProductDirectory() + SOUND_EFFECTS_PATH |
| 5327 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 5328 | if (!new File(filePath).isFile()) { |
| 5329 | filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH |
| 5330 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 5331 | } |
| 5332 | return filePath; |
| 5333 | } |
| 5334 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5335 | private boolean onLoadSoundEffects() { |
| 5336 | int status; |
| 5337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5338 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 5339 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5340 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 5341 | return false; |
| 5342 | } |
| 5343 | |
| 5344 | if (mSoundPool != null) { |
| 5345 | return true; |
| 5346 | } |
| 5347 | |
| 5348 | loadTouchSoundAssets(); |
| 5349 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 5350 | mSoundPool = new SoundPool.Builder() |
| 5351 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 5352 | .setAudioAttributes(new AudioAttributes.Builder() |
| 5353 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 5354 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 5355 | .build()) |
| 5356 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5357 | mSoundPoolCallBack = null; |
| 5358 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 5359 | mSoundPoolListenerThread.start(); |
| 5360 | int attempts = 3; |
| 5361 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 5362 | try { |
| 5363 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 5364 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5365 | } catch (InterruptedException e) { |
| 5366 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | if (mSoundPoolCallBack == null) { |
| 5371 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 5372 | if (mSoundPoolLooper != null) { |
| 5373 | mSoundPoolLooper.quit(); |
| 5374 | mSoundPoolLooper = null; |
| 5375 | } |
| 5376 | mSoundPoolListenerThread = null; |
| 5377 | mSoundPool.release(); |
| 5378 | mSoundPool = null; |
| 5379 | return false; |
| 5380 | } |
| 5381 | /* |
| 5382 | * poolId table: The value -1 in this table indicates that corresponding |
| 5383 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 5384 | * Once loaded, the value in poolId is the sample ID and the same |
| 5385 | * sample can be reused for another effect using the same file. |
| 5386 | */ |
| 5387 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 5388 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 5389 | poolId[fileIdx] = -1; |
| 5390 | } |
| 5391 | /* |
| 5392 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 5393 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 5394 | * this indicates we have a valid sample loaded for this effect. |
| 5395 | */ |
| 5396 | |
| 5397 | int numSamples = 0; |
| 5398 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5399 | // Do not load sample if this effect uses the MediaPlayer |
| 5400 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 5401 | continue; |
| 5402 | } |
| 5403 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5404 | String filePath = getSoundEffectFilePath(effect); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5405 | int sampleId = mSoundPool.load(filePath, 0); |
| 5406 | if (sampleId <= 0) { |
| 5407 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 5408 | } else { |
| 5409 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 5410 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 5411 | numSamples++; |
| 5412 | } |
| 5413 | } else { |
| 5414 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 5415 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 5416 | } |
| 5417 | } |
| 5418 | // wait for all samples to be loaded |
| 5419 | if (numSamples > 0) { |
| 5420 | mSoundPoolCallBack.setSamples(poolId); |
| 5421 | |
| 5422 | attempts = 3; |
| 5423 | status = 1; |
| 5424 | while ((status == 1) && (attempts-- > 0)) { |
| 5425 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 5426 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5427 | status = mSoundPoolCallBack.status(); |
| 5428 | } catch (InterruptedException e) { |
| 5429 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 5430 | } |
| 5431 | } |
| 5432 | } else { |
| 5433 | status = -1; |
| 5434 | } |
| 5435 | |
| 5436 | if (mSoundPoolLooper != null) { |
| 5437 | mSoundPoolLooper.quit(); |
| 5438 | mSoundPoolLooper = null; |
| 5439 | } |
| 5440 | mSoundPoolListenerThread = null; |
| 5441 | if (status != 0) { |
| 5442 | Log.w(TAG, |
| 5443 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 5444 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5445 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 5446 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | mSoundPool.release(); |
| 5451 | mSoundPool = null; |
| 5452 | } |
| 5453 | } |
| 5454 | return (status == 0); |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * Unloads samples from the sound pool. |
| 5459 | * This method can be called to free some memory when |
| 5460 | * sound effects are disabled. |
| 5461 | */ |
| 5462 | private void onUnloadSoundEffects() { |
| 5463 | synchronized (mSoundEffectsLock) { |
| 5464 | if (mSoundPool == null) { |
| 5465 | return; |
| 5466 | } |
| 5467 | |
| 5468 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 5469 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 5470 | poolId[fileIdx] = 0; |
| 5471 | } |
| 5472 | |
| 5473 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 5474 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 5475 | continue; |
| 5476 | } |
| 5477 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 5478 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 5479 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 5480 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 5481 | } |
| 5482 | } |
| 5483 | mSoundPool.release(); |
| 5484 | mSoundPool = null; |
| 5485 | } |
| 5486 | } |
| 5487 | |
| 5488 | private void onPlaySoundEffect(int effectType, int volume) { |
| 5489 | synchronized (mSoundEffectsLock) { |
| 5490 | |
| 5491 | onLoadSoundEffects(); |
| 5492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5493 | if (mSoundPool == null) { |
| 5494 | return; |
| 5495 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5496 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 5497 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5498 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 5499 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5500 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 5501 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 5502 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5503 | |
| 5504 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5505 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 5506 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5507 | } else { |
| 5508 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5509 | try { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 5510 | String filePath = getSoundEffectFilePath(effectType); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5511 | mediaPlayer.setDataSource(filePath); |
| 5512 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 5513 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 5514 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 5515 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 5516 | public void onCompletion(MediaPlayer mp) { |
| 5517 | cleanupPlayer(mp); |
| 5518 | } |
| 5519 | }); |
| 5520 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 5521 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 5522 | cleanupPlayer(mp); |
| 5523 | return true; |
| 5524 | } |
| 5525 | }); |
| 5526 | mediaPlayer.start(); |
| 5527 | } catch (IOException ex) { |
| 5528 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 5529 | } catch (IllegalArgumentException ex) { |
| 5530 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 5531 | } catch (IllegalStateException ex) { |
| 5532 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | } |
| 5534 | } |
| 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | private void cleanupPlayer(MediaPlayer mp) { |
| 5539 | if (mp != null) { |
| 5540 | try { |
| 5541 | mp.stop(); |
| 5542 | mp.release(); |
| 5543 | } catch (IllegalStateException ex) { |
| 5544 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 5545 | } |
| 5546 | } |
| 5547 | } |
| 5548 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5549 | private void setForceUse(int usage, int config, String eventSource) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5550 | synchronized (mConnectedDevices) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5551 | setForceUseInt_SyncDevices(usage, config, eventSource); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5552 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5553 | } |
| 5554 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 5555 | private void onPersistSafeVolumeState(int state) { |
| 5556 | Settings.Global.putInt(mContentResolver, |
| 5557 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 5558 | state); |
| 5559 | } |
| 5560 | |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 5561 | private void onNotifyVolumeEvent(@NonNull IAudioPolicyCallback apc, |
| 5562 | @AudioManager.VolumeAdjustment int direction) { |
| 5563 | try { |
| 5564 | apc.notifyVolumeAdjust(direction); |
| 5565 | } catch(Exception e) { |
| 5566 | // nothing we can do about this. Do not log error, too much potential for spam |
| 5567 | } |
| 5568 | } |
| 5569 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5570 | @Override |
| 5571 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 5572 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5573 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 5574 | case MSG_SET_DEVICE_VOLUME: |
| 5575 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 5576 | break; |
| 5577 | |
| 5578 | case MSG_SET_ALL_VOLUMES: |
| 5579 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5580 | break; |
| 5581 | |
| 5582 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5583 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5584 | break; |
| 5585 | |
| 5586 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5587 | // note that the value persisted is the current ringer mode, not the |
| 5588 | // 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] | 5589 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5590 | break; |
| 5591 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 5592 | case MSG_AUDIO_SERVER_DIED: |
| 5593 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5594 | break; |
| 5595 | |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 5596 | case MSG_DISPATCH_AUDIO_SERVER_STATE: |
| 5597 | onDispatchAudioServerStateChange(msg.arg1 == 1); |
| 5598 | break; |
| 5599 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5600 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 5601 | onUnloadSoundEffects(); |
| 5602 | break; |
| 5603 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5604 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5605 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 5606 | // can take several dozens of milliseconds to complete |
| 5607 | boolean loaded = onLoadSoundEffects(); |
| 5608 | if (msg.obj != null) { |
| 5609 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 5610 | synchronized (reply) { |
| 5611 | reply.mStatus = loaded ? 0 : -1; |
| 5612 | reply.notify(); |
| 5613 | } |
| 5614 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 5615 | break; |
| 5616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5617 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 5618 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5619 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5620 | |
| 5621 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 5622 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5623 | synchronized (mConnectedDevices) { |
| 5624 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 5625 | } |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5626 | mAudioEventWakeLock.release(); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5627 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5628 | |
| 5629 | case MSG_SET_FORCE_USE: |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 5630 | case MSG_SET_FORCE_BT_A2DP_USE: |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5631 | setForceUse(msg.arg1, msg.arg2, (String) msg.obj); |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 5632 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 5633 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5634 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 5635 | resetBluetoothSco(); |
| 5636 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5637 | |
| 5638 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5639 | { WiredDeviceConnectionState connectState = |
| 5640 | (WiredDeviceConnectionState)msg.obj; |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 5641 | mDeviceLogger.log(new WiredDevConnectEvent(connectState)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5642 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5643 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5644 | mAudioEventWakeLock.release(); |
| 5645 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5646 | break; |
| 5647 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5648 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 5649 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 5650 | mAudioEventWakeLock.release(); |
| 5651 | break; |
| 5652 | |
| 5653 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5654 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1, msg.arg2); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5655 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5656 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5657 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5658 | case MSG_SET_HEARING_AID_CONNECTION_STATE: |
| 5659 | onSetHearingAidConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 5660 | mAudioEventWakeLock.release(); |
| 5661 | break; |
| 5662 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5663 | case MSG_A2DP_DEVICE_CONFIG_CHANGE: |
| 5664 | onBluetoothA2dpDeviceConfigChange((BluetoothDevice)msg.obj); |
| 5665 | mAudioEventWakeLock.release(); |
| 5666 | break; |
| 5667 | |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 5668 | case MSG_DISABLE_AUDIO_FOR_UID: |
| 5669 | mPlaybackMonitor.disableAudioForUid( msg.arg1 == 1 /* disable */, |
| 5670 | msg.arg2 /* uid */); |
| 5671 | mAudioEventWakeLock.release(); |
| 5672 | break; |
| 5673 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5674 | case MSG_REPORT_NEW_ROUTES: { |
| 5675 | int N = mRoutesObservers.beginBroadcast(); |
| 5676 | if (N > 0) { |
| 5677 | AudioRoutesInfo routes; |
| 5678 | synchronized (mCurAudioRoutes) { |
| 5679 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5680 | } |
| 5681 | while (N > 0) { |
| 5682 | N--; |
| 5683 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 5684 | try { |
| 5685 | obs.dispatchAudioRoutesChanged(routes); |
| 5686 | } catch (RemoteException e) { |
| 5687 | } |
| 5688 | } |
| 5689 | } |
| 5690 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5691 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5692 | break; |
| 5693 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5694 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5695 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5696 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5697 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5698 | |
| 5699 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 5700 | onSendBecomingNoisyIntent(); |
| 5701 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5702 | |
| 5703 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 5704 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5705 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 5706 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5707 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 5708 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 5709 | onPersistSafeVolumeState(msg.arg1); |
| 5710 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 5711 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 5712 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 5713 | onBroadcastScoConnectionState(msg.arg1); |
| 5714 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 5715 | |
| 5716 | case MSG_SYSTEM_READY: |
| 5717 | onSystemReady(); |
| 5718 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5719 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 5720 | case MSG_INDICATE_SYSTEM_READY: |
| 5721 | onIndicateSystemReady(); |
| 5722 | break; |
| 5723 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5724 | case MSG_ACCESSORY_PLUG_MEDIA_UNMUTE: |
| 5725 | onAccessoryPlugMediaUnmute(msg.arg1); |
| 5726 | break; |
| 5727 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5728 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 5729 | final int musicActiveMs = msg.arg1; |
| 5730 | Settings.Secure.putIntForUser(mContentResolver, |
| 5731 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 5732 | UserHandle.USER_CURRENT); |
| 5733 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5734 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 5735 | case MSG_UNMUTE_STREAM: |
| 5736 | onUnmuteStream(msg.arg1, msg.arg2); |
| 5737 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 5738 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 5739 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 5740 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 5741 | break; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 5742 | |
| 5743 | case MSG_NOTIFY_VOL_EVENT: |
| 5744 | onNotifyVolumeEvent((IAudioPolicyCallback) msg.obj, msg.arg1); |
| 5745 | break; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 5746 | |
| 5747 | case MSG_ENABLE_SURROUND_FORMATS: |
| 5748 | onEnableSurroundFormats((ArrayList<Integer>) msg.obj); |
| 5749 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5750 | } |
| 5751 | } |
| 5752 | } |
| 5753 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5754 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5755 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5756 | SettingsObserver() { |
| 5757 | super(new Handler()); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5758 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5759 | Settings.Global.ZEN_MODE), false, this); |
| 5760 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5761 | Settings.Global.ZEN_MODE_CONFIG_ETAG), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5762 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5763 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5764 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5765 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5766 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 5767 | Settings.System.MASTER_MONO), false, this); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5768 | |
| 5769 | mEncodedSurroundMode = Settings.Global.getInt( |
| 5770 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5771 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5772 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5773 | Settings.Global.ENCODED_SURROUND_OUTPUT), false, this); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 5774 | |
| 5775 | mEnabledSurroundFormats = Settings.Global.getString( |
| 5776 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS); |
| 5777 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 5778 | Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | @Override |
| 5782 | public void onChange(boolean selfChange) { |
| 5783 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5784 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5785 | // However there appear to be some missing locks around mRingerAndZenModeMutedStreams |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 5786 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5787 | // mRingerAndZenModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5788 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5789 | if (updateRingerAndZenModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5790 | /* |
| 5791 | * Ensure all stream types that should be affected by ringer mode |
| 5792 | * are in the proper state. |
| 5793 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5794 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5795 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 5796 | readDockAudioSettings(mContentResolver); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 5797 | updateMasterMono(mContentResolver); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5798 | updateEncodedSurroundOutput(); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 5799 | sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | |
| 5803 | private void updateEncodedSurroundOutput() { |
| 5804 | int newSurroundMode = Settings.Global.getInt( |
| 5805 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 5806 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 5807 | // Did it change? |
| 5808 | if (mEncodedSurroundMode != newSurroundMode) { |
| 5809 | // Send to AudioPolicyManager |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5810 | sendEncodedSurroundMode(newSurroundMode, "SettingsObserver"); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 5811 | synchronized(mConnectedDevices) { |
| 5812 | // Is HDMI connected? |
| 5813 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HDMI, ""); |
| 5814 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 5815 | if (deviceSpec != null) { |
| 5816 | // Toggle HDMI to retrigger broadcast with proper formats. |
| 5817 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5818 | AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "", |
| 5819 | "android"); // disconnect |
| 5820 | setWiredDeviceConnectionState(AudioSystem.DEVICE_OUT_HDMI, |
| 5821 | AudioSystem.DEVICE_STATE_AVAILABLE, "", "", |
| 5822 | "android"); // reconnect |
| 5823 | } |
| 5824 | } |
| 5825 | mEncodedSurroundMode = newSurroundMode; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 5826 | mSurroundModeChanged = true; |
| 5827 | } else { |
| 5828 | mSurroundModeChanged = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5829 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5830 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5831 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5832 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5833 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5834 | private void makeA2dpDeviceAvailable(String address, String name, String eventSource) { |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5835 | // enable A2DP before notifying A2DP connection to avoid unnecessary processing in |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5836 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5837 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5838 | setBluetoothA2dpOnInt(true, eventSource); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5839 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5840 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5841 | // Reset A2DP suspend state each time a new sink is connected |
| 5842 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5843 | mConnectedDevices.put( |
| 5844 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5845 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5846 | address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 5847 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5848 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, null, 0); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5849 | } |
| 5850 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5851 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5852 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 5853 | } |
| 5854 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5855 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5856 | private void makeA2dpDeviceUnavailableNow(String address) { |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5857 | if (address == null) { |
| 5858 | return; |
| 5859 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 5860 | synchronized (mA2dpAvrcpLock) { |
| 5861 | mAvrcpAbsVolSupported = false; |
| 5862 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5863 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5864 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5865 | mConnectedDevices.remove( |
| 5866 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5867 | // Remove A2DP routes as well |
| 5868 | setCurrentAudioRouteName(null); |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5869 | if (mDockAddress == address) { |
| 5870 | mDockAddress = null; |
| 5871 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5872 | } |
| 5873 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5874 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5875 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 5876 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 5877 | // reconnection of the sink. |
| 5878 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5879 | // 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] | 5880 | mConnectedDevices.remove( |
| 5881 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5882 | // send the delayed message to make the device unavailable later |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5883 | queueMsgUnderWakeLock(mAudioHandler, |
| 5884 | MSG_BTA2DP_DOCK_TIMEOUT, |
| 5885 | 0, |
| 5886 | 0, |
| 5887 | address, |
| 5888 | delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5889 | } |
| 5890 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5891 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5892 | private void makeA2dpSrcAvailable(String address) { |
| 5893 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5894 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5895 | mConnectedDevices.put( |
| 5896 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5897 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5898 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | // must be called synchronized on mConnectedDevices |
| 5902 | private void makeA2dpSrcUnavailable(String address) { |
| 5903 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 5904 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5905 | mConnectedDevices.remove( |
| 5906 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5907 | } |
| 5908 | |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5909 | private void setHearingAidVolume(int index, int streamType) { |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5910 | synchronized (mHearingAidLock) { |
| 5911 | if (mHearingAid != null) { |
| 5912 | //hearing aid expect volume value in range -128dB to 0dB |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5913 | int gainDB = (int)AudioSystem.getStreamVolumeDB(streamType, index/10, |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5914 | AudioSystem.DEVICE_OUT_HEARING_AID); |
| 5915 | if (gainDB < BT_HEARING_AID_GAIN_MIN) |
| 5916 | gainDB = BT_HEARING_AID_GAIN_MIN; |
| 5917 | mHearingAid.setVolume(gainDB); |
| 5918 | } |
| 5919 | } |
| 5920 | } |
| 5921 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5922 | // must be called synchronized on mConnectedDevices |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5923 | private void makeHearingAidDeviceAvailable(String address, String name, String eventSource) { |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5924 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(AudioSystem.DEVICE_OUT_HEARING_AID); |
Jakub Pawlowski | 21b7f49 | 2018-03-22 12:31:21 -0700 | [diff] [blame] | 5925 | setHearingAidVolume(index, AudioSystem.STREAM_MUSIC); |
Jakub Pawlowski | 09592d5 | 2018-03-19 12:23:59 -0700 | [diff] [blame] | 5926 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5927 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 5928 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
| 5929 | mConnectedDevices.put( |
| 5930 | makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, address), |
| 5931 | new DeviceListSpec(AudioSystem.DEVICE_OUT_HEARING_AID, name, |
| 5932 | address)); |
| 5933 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 5934 | AudioSystem.DEVICE_OUT_HEARING_AID, 0, null, 0); |
| 5935 | } |
| 5936 | |
| 5937 | // must be called synchronized on mConnectedDevices |
| 5938 | private void makeHearingAidDeviceUnavailable(String address) { |
| 5939 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 5940 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
| 5941 | mConnectedDevices.remove( |
| 5942 | makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, address)); |
| 5943 | // Remove Hearing Aid routes as well |
| 5944 | setCurrentAudioRouteName(null); |
| 5945 | } |
| 5946 | |
| 5947 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5948 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 5949 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5950 | } |
| 5951 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5952 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5953 | private boolean hasScheduledA2dpDockTimeout() { |
| 5954 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 5955 | } |
| 5956 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5957 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state, int a2dpVolume) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5958 | { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 5959 | if (DEBUG_DEVICES) { |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5960 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice= " + btDevice+" state= " + state |
| 5961 | + " is dock: "+btDevice.isBluetoothDock()); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 5962 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5963 | if (btDevice == null) { |
| 5964 | return; |
| 5965 | } |
| 5966 | String address = btDevice.getAddress(); |
| 5967 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 5968 | address = ""; |
| 5969 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5970 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5971 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5972 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 5973 | btDevice.getAddress()); |
| 5974 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 5975 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5976 | |
| 5977 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 5978 | if (btDevice.isBluetoothDock()) { |
| 5979 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 5980 | // introduction of a delay for transient disconnections of docks when |
| 5981 | // power is rapidly turned off/on, this message will be canceled if |
| 5982 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 5983 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5984 | // the next time isConnected is evaluated, it will be false for the dock |
| 5985 | } |
| 5986 | } else { |
| 5987 | makeA2dpDeviceUnavailableNow(address); |
| 5988 | } |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 5989 | setCurrentAudioRouteName(null); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5990 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 5991 | if (btDevice.isBluetoothDock()) { |
| 5992 | // this could be a reconnection after a transient disconnection |
| 5993 | cancelA2dpDeviceTimeout(); |
| 5994 | mDockAddress = address; |
| 5995 | } else { |
| 5996 | // this could be a connection of another A2DP device before the timeout of |
| 5997 | // a dock: cancel the dock timeout, and make the dock unavailable now |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 5998 | if (hasScheduledA2dpDockTimeout() && mDockAddress != null) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 5999 | cancelA2dpDeviceTimeout(); |
| 6000 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 6001 | } |
| 6002 | } |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 6003 | if (a2dpVolume != -1) { |
| 6004 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 6005 | // Convert index to internal representation in VolumeStreamState |
| 6006 | a2dpVolume = a2dpVolume * 10; |
| 6007 | streamState.setIndex(a2dpVolume, AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 6008 | "onSetA2dpSinkConnectionState"); |
| 6009 | setDeviceVolume(streamState, AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
| 6010 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6011 | makeA2dpDeviceAvailable(address, btDevice.getName(), |
| 6012 | "onSetA2dpSinkConnectionState"); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 6013 | setCurrentAudioRouteName(btDevice.getAliasName()); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 6018 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 6019 | { |
| 6020 | if (DEBUG_VOL) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6021 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice=" + btDevice + " state=" + state); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 6022 | } |
| 6023 | if (btDevice == null) { |
| 6024 | return; |
| 6025 | } |
| 6026 | String address = btDevice.getAddress(); |
| 6027 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 6028 | address = ""; |
| 6029 | } |
| 6030 | |
| 6031 | synchronized (mConnectedDevices) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6032 | final String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 6033 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6034 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 6035 | |
| 6036 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 6037 | makeA2dpSrcUnavailable(address); |
| 6038 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 6039 | makeA2dpSrcAvailable(address); |
| 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 6044 | private void onSetHearingAidConnectionState(BluetoothDevice btDevice, int state) |
| 6045 | { |
| 6046 | if (DEBUG_DEVICES) { |
| 6047 | Log.d(TAG, "onSetHearingAidConnectionState btDevice=" + btDevice+", state=" + state); |
| 6048 | } |
| 6049 | if (btDevice == null) { |
| 6050 | return; |
| 6051 | } |
| 6052 | String address = btDevice.getAddress(); |
| 6053 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 6054 | address = ""; |
| 6055 | } |
| 6056 | |
| 6057 | synchronized (mConnectedDevices) { |
| 6058 | final String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_HEARING_AID, |
| 6059 | btDevice.getAddress()); |
| 6060 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 6061 | boolean isConnected = deviceSpec != null; |
| 6062 | |
| 6063 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 6064 | makeHearingAidDeviceUnavailable(address); |
| 6065 | setCurrentAudioRouteName(null); |
| 6066 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 6067 | makeHearingAidDeviceAvailable(address, btDevice.getName(), |
| 6068 | "onSetHearingAidConnectionState"); |
| 6069 | setCurrentAudioRouteName(btDevice.getAliasName()); |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | private void setCurrentAudioRouteName(String name){ |
| 6075 | synchronized (mCurAudioRoutes) { |
| 6076 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 6077 | mCurAudioRoutes.bluetoothName = name; |
| 6078 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6079 | SENDMSG_NOOP, 0, 0, null, 0); |
| 6080 | } |
| 6081 | } |
| 6082 | } |
| 6083 | |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6084 | private void onBluetoothA2dpDeviceConfigChange(BluetoothDevice btDevice) |
| 6085 | { |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 6086 | if (DEBUG_DEVICES) { |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6087 | Log.d(TAG, "onBluetoothA2dpDeviceConfigChange btDevice=" + btDevice); |
| 6088 | } |
| 6089 | if (btDevice == null) { |
| 6090 | return; |
| 6091 | } |
| 6092 | String address = btDevice.getAddress(); |
| 6093 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 6094 | address = ""; |
| 6095 | } |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 6096 | mDeviceLogger.log(new AudioEventLogger.StringEvent( |
| 6097 | "onBluetoothA2dpDeviceConfigChange addr=" + address)); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6098 | |
| 6099 | int device = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; |
| 6100 | synchronized (mConnectedDevices) { |
Eric Laurent | 4724ea7 | 2017-05-23 10:39:38 -0700 | [diff] [blame] | 6101 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE, btDevice)) { |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 6102 | mDeviceLogger.log(new AudioEventLogger.StringEvent( |
| 6103 | "A2dp config change ignored")); |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6104 | return; |
| 6105 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6106 | final String key = makeDeviceListKey(device, address); |
| 6107 | final DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 6108 | if (deviceSpec != null) { |
| 6109 | // Device is connected |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6110 | int musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 6111 | if (AudioSystem.handleDeviceConfigChange(device, address, |
| 6112 | btDevice.getName()) != AudioSystem.AUDIO_STATUS_OK) { |
| 6113 | // force A2DP device disconnection in case of error so that AudioService state is |
| 6114 | // consistent with audio policy manager state |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6115 | setBluetoothA2dpDeviceConnectionStateInt( |
| 6116 | btDevice, BluetoothA2dp.STATE_DISCONNECTED, BluetoothProfile.A2DP, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 6117 | false /* suppressNoisyIntent */, musicDevice, -1 /* a2dpVolume */); |
Eric Laurent | 5205a35 | 2017-04-27 18:31:22 -0700 | [diff] [blame] | 6118 | } |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 6119 | } |
| 6120 | } |
| 6121 | } |
| 6122 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 6123 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 6124 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 6125 | synchronized (mA2dpAvrcpLock) { |
| 6126 | mAvrcpAbsVolSupported = support; |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 6127 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 6128 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 6129 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 6130 | } |
| 6131 | } |
| 6132 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6133 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 6134 | String deviceName) { |
| 6135 | if (DEBUG_DEVICES) { |
| 6136 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 6137 | + " address:" + address + " name:" + deviceName + ")"); |
| 6138 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6139 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6140 | String deviceKey = makeDeviceListKey(device, address); |
| 6141 | if (DEBUG_DEVICES) { |
| 6142 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 6143 | } |
| 6144 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 6145 | boolean isConnected = deviceSpec != null; |
| 6146 | if (DEBUG_DEVICES) { |
| 6147 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 6148 | } |
| 6149 | if (connect && !isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 6150 | final int res = AudioSystem.setDeviceConnectionState(device, |
| 6151 | AudioSystem.DEVICE_STATE_AVAILABLE, address, deviceName); |
| 6152 | if (res != AudioSystem.AUDIO_STATUS_OK) { |
| 6153 | Slog.e(TAG, "not connecting device 0x" + Integer.toHexString(device) + |
| 6154 | " due to command error " + res ); |
| 6155 | return false; |
| 6156 | } |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6157 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 6158 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 6159 | device, 0, null, 0); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6160 | return true; |
| 6161 | } else if (!connect && isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 6162 | AudioSystem.setDeviceConnectionState(device, |
| 6163 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, deviceName); |
| 6164 | // always remove even if disconnection failed |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6165 | mConnectedDevices.remove(deviceKey); |
| 6166 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6167 | } |
Jack He | f05e516 | 2018-04-19 15:03:43 -0700 | [diff] [blame] | 6168 | Log.w(TAG, "handleDeviceConnection() failed, deviceKey=" + deviceKey + ", deviceSpec=" |
| 6169 | + deviceSpec + ", connect=" + connect); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6170 | } |
| 6171 | return false; |
| 6172 | } |
| 6173 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6174 | // 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] | 6175 | // sent if: |
| 6176 | // - none of these devices are connected anymore after one is disconnected AND |
| 6177 | // - the device being disconnected is actually used for music. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6178 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6179 | int mBecomingNoisyIntentDevices = |
| 6180 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 6181 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 6182 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jakub Pawlowski | 26fac41 | 2018-03-22 15:00:34 -0700 | [diff] [blame] | 6183 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE | |
| 6184 | AudioSystem.DEVICE_OUT_HEARING_AID; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6185 | |
| 6186 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6187 | // Called synchronized on mConnectedDevices |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6188 | // musicDevice argument is used when not AudioSystem.DEVICE_NONE instead of querying |
| 6189 | // from AudioSystem |
| 6190 | private int checkSendBecomingNoisyIntent(int device, int state, int musicDevice) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6191 | int delay = 0; |
| 6192 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 6193 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 6194 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 6195 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6196 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 6197 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 6198 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6199 | } |
| 6200 | } |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6201 | if (musicDevice == AudioSystem.DEVICE_NONE) { |
| 6202 | musicDevice = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 6203 | } |
| 6204 | // ignore condition on device being actually used for music when in communication |
| 6205 | // because music routing is altered in this case. |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 6206 | // also checks whether media routing if affected by a dynamic policy |
| 6207 | if (((device == musicDevice) || isInCommunication()) && (device == devices) |
| 6208 | && !hasMediaDynamicPolicy()) { |
Eric Laurent | cdae476 | 2017-04-28 18:00:04 -0700 | [diff] [blame] | 6209 | mAudioHandler.removeMessages(MSG_BROADCAST_AUDIO_BECOMING_NOISY); |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 6210 | sendMsg(mAudioHandler, |
| 6211 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 6212 | SENDMSG_REPLACE, |
| 6213 | 0, |
| 6214 | 0, |
| 6215 | null, |
| 6216 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6217 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6218 | } |
| 6219 | } |
| 6220 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6221 | return delay; |
| 6222 | } |
| 6223 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 6224 | /** |
| 6225 | * @return true if there is currently a registered dynamic mixing policy that affects media |
| 6226 | */ |
| 6227 | private boolean hasMediaDynamicPolicy() { |
| 6228 | synchronized (mAudioPolicies) { |
| 6229 | if (mAudioPolicies.isEmpty()) { |
| 6230 | return false; |
| 6231 | } |
| 6232 | final Collection<AudioPolicyProxy> appColl = mAudioPolicies.values(); |
| 6233 | for (AudioPolicyProxy app : appColl) { |
| 6234 | if (app.hasMixAffectingUsage(AudioAttributes.USAGE_MEDIA)) { |
| 6235 | return true; |
| 6236 | } |
| 6237 | } |
| 6238 | return false; |
| 6239 | } |
| 6240 | } |
| 6241 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6242 | private void updateAudioRoutes(int device, int state) |
| 6243 | { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6244 | int connType = 0; |
| 6245 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6246 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6247 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 6248 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 6249 | device == AudioSystem.DEVICE_OUT_LINE) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6250 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 6251 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 6252 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6253 | connType = AudioRoutesInfo.MAIN_HDMI; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6254 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE|| |
| 6255 | device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6256 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6257 | } |
| 6258 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6259 | synchronized (mCurAudioRoutes) { |
| 6260 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6261 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6262 | if (state != 0) { |
| 6263 | newConn |= connType; |
| 6264 | } else { |
| 6265 | newConn &= ~connType; |
| 6266 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6267 | if (newConn != mCurAudioRoutes.mainType) { |
| 6268 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6269 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6270 | SENDMSG_NOOP, 0, 0, null, 0); |
| 6271 | } |
| 6272 | } |
| 6273 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6274 | } |
| 6275 | |
| 6276 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 6277 | String deviceName) { |
| 6278 | if (DEBUG_DEVICES) { |
| 6279 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 6280 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 6281 | " name:" + deviceName + ");"); |
| 6282 | } |
| 6283 | Intent intent = new Intent(); |
| 6284 | |
| 6285 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
| 6286 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 6287 | intent.putExtra("microphone", 1); |
| 6288 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 6289 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 6290 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 6291 | intent.putExtra("microphone", 0); |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 6292 | } else if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 6293 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
Jean-Michel Trivi | 87d31ec | 2017-08-11 18:28:20 -0700 | [diff] [blame] | 6294 | intent.putExtra("microphone", |
| 6295 | AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_IN_USB_HEADSET, "") |
| 6296 | == AudioSystem.DEVICE_STATE_AVAILABLE ? 1 : 0); |
| 6297 | } else if (device == AudioSystem.DEVICE_IN_USB_HEADSET) { |
| 6298 | if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_USB_HEADSET, "") |
| 6299 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
| 6300 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 6301 | intent.putExtra("microphone", 1); |
| 6302 | } else { |
| 6303 | // do not send ACTION_HEADSET_PLUG when only the input side is seen as changing |
| 6304 | return; |
| 6305 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6306 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 6307 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
| 6308 | configureHdmiPlugIntent(intent, state); |
| 6309 | } |
| 6310 | |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 6311 | if (intent.getAction() == null) { |
| 6312 | return; |
| 6313 | } |
| 6314 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6315 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 6316 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 6317 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 6318 | |
| 6319 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6320 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6321 | final long ident = Binder.clearCallingIdentity(); |
| 6322 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 6323 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6324 | } finally { |
| 6325 | Binder.restoreCallingIdentity(ident); |
| 6326 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6327 | } |
| 6328 | |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6329 | private static final int DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG = |
| 6330 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 6331 | AudioSystem.DEVICE_OUT_LINE | |
| 6332 | AudioSystem.DEVICE_OUT_ALL_USB; |
| 6333 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6334 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6335 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6336 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6337 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 6338 | + " state:" + Integer.toHexString(state) |
| 6339 | + " address:" + address |
| 6340 | + " deviceName:" + deviceName |
| 6341 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 6342 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 6343 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6344 | synchronized (mConnectedDevices) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6345 | if ((state == 0) && ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0)) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6346 | setBluetoothA2dpOnInt(true, "onSetWiredDeviceConnectionState state 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6347 | } |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 6348 | |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 6349 | if (!handleDeviceConnection(state == 1, device, address, deviceName)) { |
| 6350 | // change of connection state failed, bailout |
| 6351 | return; |
| 6352 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6353 | if (state != 0) { |
Eric Laurent | bbe3e74 | 2017-04-28 18:11:50 -0700 | [diff] [blame] | 6354 | if ((device & DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG) != 0) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6355 | setBluetoothA2dpOnInt(false, "onSetWiredDeviceConnectionState state not 0"); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6356 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6357 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 6358 | sendMsg(mAudioHandler, |
| 6359 | MSG_CHECK_MUSIC_ACTIVE, |
| 6360 | SENDMSG_REPLACE, |
| 6361 | 0, |
| 6362 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6363 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 6364 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 6365 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6366 | // Television devices without CEC service apply software volume on HDMI output |
| 6367 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 6368 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 6369 | checkAllFixedVolumeDevices(); |
| 6370 | if (mHdmiManager != null) { |
| 6371 | synchronized (mHdmiManager) { |
| 6372 | if (mHdmiPlaybackClient != null) { |
| 6373 | mHdmiCecSink = false; |
| 6374 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 6379 | if ((device & AudioSystem.DEVICE_OUT_HDMI) != 0) { |
| 6380 | sendEnabledSurroundFormats(mContentResolver, true); |
| 6381 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 6382 | } else { |
| 6383 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 6384 | if (mHdmiManager != null) { |
| 6385 | synchronized (mHdmiManager) { |
| 6386 | mHdmiCecSink = false; |
| 6387 | } |
| 6388 | } |
| 6389 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6390 | } |
Jean-Michel Trivi | 87a264d | 2017-08-15 17:52:22 -0700 | [diff] [blame] | 6391 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6392 | updateAudioRoutes(device, state); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 6393 | } |
| 6394 | } |
| 6395 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6396 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6397 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 6398 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6399 | if (state == 1) { |
| 6400 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 6401 | int[] portGeneration = new int[1]; |
| 6402 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 6403 | if (status == AudioManager.SUCCESS) { |
| 6404 | for (AudioPort port : ports) { |
| 6405 | if (port instanceof AudioDevicePort) { |
| 6406 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 6407 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 6408 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6409 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 6410 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6411 | if (formats.length > 0) { |
| 6412 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 6413 | for (int format : formats) { |
| 6414 | // a format in the list can be 0, skip it |
| 6415 | if (format != AudioFormat.ENCODING_INVALID) { |
| 6416 | encodingList.add(format); |
| 6417 | } |
| 6418 | } |
| 6419 | int[] encodingArray = new int[encodingList.size()]; |
| 6420 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 6421 | encodingArray[i] = encodingList.get(i); |
| 6422 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6423 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6424 | } |
| 6425 | // find the maximum supported number of channels |
| 6426 | int maxChannels = 0; |
| 6427 | for (int mask : devicePort.channelMasks()) { |
| 6428 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 6429 | if (channelCount > maxChannels) { |
| 6430 | maxChannels = channelCount; |
| 6431 | } |
| 6432 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 6433 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | } |
| 6437 | } |
| 6438 | } |
| 6439 | } |
| 6440 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 6441 | /* cache of the address of the last dock the device was connected to */ |
| 6442 | private String mDockAddress; |
| 6443 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6444 | /** |
| 6445 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 6446 | */ |
| 6447 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 6448 | @Override |
| 6449 | public void onReceive(Context context, Intent intent) { |
| 6450 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 6451 | int outDevice; |
| 6452 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6453 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6454 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 6455 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 6456 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 6457 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 6458 | int config; |
| 6459 | switch (dockState) { |
| 6460 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 6461 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 6462 | break; |
| 6463 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 6464 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 6465 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 6466 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6467 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 6468 | break; |
| 6469 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 6470 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 6471 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 6472 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 6473 | default: |
| 6474 | config = AudioSystem.FORCE_NONE; |
| 6475 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6476 | // Low end docks have a menu to enable or disable audio |
| 6477 | // (see mDockAudioMediaEnabled) |
| 6478 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 6479 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 6480 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6481 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_DOCK, config, |
| 6482 | "ACTION_DOCK_EVENT intent")); |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 6483 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 6484 | } |
| 6485 | mDockState = dockState; |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 6486 | } else if (action.equals(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED)) { |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 6487 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 6488 | setBtScoActiveDevice(btDevice); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 6489 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6490 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6491 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6492 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6493 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6494 | // broadcast intent if the connection was initated by AudioService |
| 6495 | if (!mScoClients.isEmpty() && |
| 6496 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 6497 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 6498 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 6499 | mScoAudioState == SCO_STATE_DEACTIVATING)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6500 | broadcast = true; |
| 6501 | } |
| 6502 | switch (btState) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 6503 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
| 6504 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
| 6505 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 6506 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 6507 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 6508 | } |
| 6509 | setBluetoothScoOn(true); |
| 6510 | break; |
| 6511 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
| 6512 | setBluetoothScoOn(false); |
| 6513 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
| 6514 | // startBluetoothSco called after stopBluetoothSco |
| 6515 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ) { |
| 6516 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null |
| 6517 | && connectBluetoothScoAudioHelper(mBluetoothHeadset, |
| 6518 | mBluetoothHeadsetDevice, mScoAudioMode)) { |
| 6519 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 6520 | broadcast = false; |
| 6521 | break; |
| 6522 | } |
| 6523 | } |
| 6524 | // Tear down SCO if disconnected from external |
| 6525 | clearAllScoClients(0, mScoAudioState == SCO_STATE_ACTIVE_INTERNAL); |
| 6526 | mScoAudioState = SCO_STATE_INACTIVE; |
| 6527 | break; |
| 6528 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
| 6529 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
Jack He | 33c3a17 | 2018-05-08 14:51:07 -0700 | [diff] [blame] | 6530 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 6531 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 6532 | } |
| 6533 | default: |
| 6534 | // do not broadcast CONNECTING or invalid state |
| 6535 | broadcast = false; |
| 6536 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 6537 | } |
| 6538 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6539 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6540 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 6541 | //FIXME: this is to maintain compatibility with deprecated intent |
| 6542 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6543 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 6544 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 6545 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 6546 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6547 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6548 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 6549 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6550 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6551 | AudioSystem.setParameters("screen_state=on"); |
| 6552 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6553 | if (mMonitorRotation) { |
| 6554 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 6555 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 6556 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 6557 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 6558 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6559 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 6560 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 6561 | if (mUserSwitchedReceived) { |
| 6562 | // attempt to stop music playback for background user except on first user |
| 6563 | // switch (i.e. first boot) |
| 6564 | sendMsg(mAudioHandler, |
| 6565 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 6566 | SENDMSG_REPLACE, |
| 6567 | 0, |
| 6568 | 0, |
| 6569 | null, |
| 6570 | 0); |
| 6571 | } |
| 6572 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 6573 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6574 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 6575 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 6576 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 6577 | readAudioSettings(true /*userSwitch*/); |
| 6578 | // preserve STREAM_MUSIC volume from one user to the next. |
| 6579 | sendMsg(mAudioHandler, |
| 6580 | MSG_SET_ALL_VOLUMES, |
| 6581 | SENDMSG_QUEUE, |
| 6582 | 0, |
| 6583 | 0, |
| 6584 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6585 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 6586 | // Disable audio recording for the background user/profile |
| 6587 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 6588 | if (userId >= 0) { |
| 6589 | // TODO Kill recording streams instead of killing processes holding permission |
| 6590 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 6591 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 6592 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 6593 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6594 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 6595 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 6596 | // Enable audio recording for foreground user/profile |
| 6597 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 6598 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6599 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 6600 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 6601 | state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 6602 | if (state == BluetoothAdapter.STATE_OFF || |
| 6603 | state == BluetoothAdapter.STATE_TURNING_OFF) { |
| 6604 | disconnectAllBluetoothProfiles(); |
| 6605 | } |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 6606 | } else if (action.equals(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION) || |
| 6607 | action.equals(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)) { |
| 6608 | handleAudioEffectBroadcast(context, intent); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 6609 | } |
| 6610 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 6611 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6612 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6613 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 6614 | |
| 6615 | @Override |
| 6616 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 6617 | Bundle prevRestrictions) { |
| 6618 | // Update mic mute state. |
| 6619 | { |
| 6620 | final boolean wasRestricted = |
| 6621 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 6622 | final boolean isRestricted = |
| 6623 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 6624 | if (wasRestricted != isRestricted) { |
| 6625 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 6626 | } |
| 6627 | } |
| 6628 | |
| 6629 | // Update speaker mute state. |
| 6630 | { |
| 6631 | final boolean wasRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 6632 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 6633 | || prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6634 | final boolean isRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 6635 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 6636 | || newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 6637 | if (wasRestricted != isRestricted) { |
| 6638 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 6639 | } |
| 6640 | } |
| 6641 | } |
| 6642 | } // end class AudioServiceUserRestrictionsListener |
| 6643 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 6644 | private void handleAudioEffectBroadcast(Context context, Intent intent) { |
| 6645 | String target = intent.getPackage(); |
| 6646 | if (target != null) { |
| 6647 | Log.w(TAG, "effect broadcast already targeted to " + target); |
| 6648 | return; |
| 6649 | } |
| 6650 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 6651 | // TODO this should target a user-selected panel |
| 6652 | List<ResolveInfo> ril = context.getPackageManager().queryBroadcastReceivers( |
| 6653 | intent, 0 /* flags */); |
| 6654 | if (ril != null && ril.size() != 0) { |
| 6655 | ResolveInfo ri = ril.get(0); |
| 6656 | if (ri != null && ri.activityInfo != null && ri.activityInfo.packageName != null) { |
| 6657 | intent.setPackage(ri.activityInfo.packageName); |
| 6658 | context.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 6659 | return; |
| 6660 | } |
| 6661 | } |
| 6662 | Log.w(TAG, "couldn't find receiver package for effect intent"); |
| 6663 | } |
| 6664 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6665 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 6666 | PackageManager pm = mContext.getPackageManager(); |
| 6667 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 6668 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 6669 | ComponentName homeActivityName = null; |
| 6670 | if (!oldUser.isManagedProfile()) { |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 6671 | homeActivityName = mActivityManagerInternal.getHomeActivityForUser(oldUser.id); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6672 | } |
| 6673 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 6674 | List<PackageInfo> packages; |
| 6675 | try { |
| 6676 | packages = AppGlobals.getPackageManager() |
| 6677 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 6678 | } catch (RemoteException e) { |
| 6679 | throw new AndroidRuntimeException(e); |
| 6680 | } |
| 6681 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 6682 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 6683 | // Skip system processes |
| 6684 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 6685 | continue; |
| 6686 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 6687 | // Skip packages that have permission to interact across users |
| 6688 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 6689 | == PackageManager.PERMISSION_GRANTED) { |
| 6690 | continue; |
| 6691 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6692 | if (homeActivityName != null |
| 6693 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 6694 | && pkg.applicationInfo.isSystemApp()) { |
| 6695 | continue; |
| 6696 | } |
| 6697 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 6698 | final int uid = pkg.applicationInfo.uid; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 6699 | ActivityManager.getService().killUid(UserHandle.getAppId(uid), |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 6700 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 6701 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 6702 | } catch (RemoteException e) { |
| 6703 | Log.w(TAG, "Error calling killUid", e); |
| 6704 | } |
| 6705 | } |
| 6706 | } |
| 6707 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6708 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6709 | //========================================================================================== |
| 6710 | // Audio Focus |
| 6711 | //========================================================================================== |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6712 | /** |
| 6713 | * Returns whether a focus request is eligible to force ducking. |
| 6714 | * Will return true if: |
| 6715 | * - the AudioAttributes have a usage of USAGE_ASSISTANCE_ACCESSIBILITY, |
| 6716 | * - the focus request is AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 6717 | * - the associated Bundle has KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING set to true, |
| 6718 | * - the uid of the requester is a known accessibility service or root. |
| 6719 | * @param aa AudioAttributes of the focus request |
| 6720 | * @param uid uid of the focus requester |
| 6721 | * @return true if ducking is to be forced |
| 6722 | */ |
| 6723 | private boolean forceFocusDuckingForAccessibility(@Nullable AudioAttributes aa, |
| 6724 | int request, int uid) { |
| 6725 | if (aa == null || aa.getUsage() != AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY |
| 6726 | || request != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) { |
| 6727 | return false; |
| 6728 | } |
| 6729 | final Bundle extraInfo = aa.getBundle(); |
| 6730 | if (extraInfo == null || |
| 6731 | !extraInfo.getBoolean(AudioFocusRequest.KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING)) { |
| 6732 | return false; |
| 6733 | } |
| 6734 | if (uid == 0) { |
| 6735 | return true; |
| 6736 | } |
| 6737 | synchronized (mAccessibilityServiceUidsLock) { |
| 6738 | if (mAccessibilityServiceUids != null) { |
| 6739 | int callingUid = Binder.getCallingUid(); |
| 6740 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 6741 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 6742 | return true; |
| 6743 | } |
| 6744 | } |
| 6745 | } |
| 6746 | } |
| 6747 | return false; |
| 6748 | } |
| 6749 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6750 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6751 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 6752 | IAudioPolicyCallback pcb, int sdk) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6753 | // permission checks |
| 6754 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 6755 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6756 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 6757 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 6758 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 6759 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6760 | } |
| 6761 | } else { |
| 6762 | // only a registered audio policy can be used to lock focus |
| 6763 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6764 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6765 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6766 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 6767 | } |
| 6768 | } |
| 6769 | } |
| 6770 | } |
| 6771 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 6772 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 6773 | clientId, callingPackageName, flags, sdk, |
| 6774 | forceFocusDuckingForAccessibility(aa, durationHint, Binder.getCallingUid())); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6775 | } |
| 6776 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6777 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa, |
| 6778 | String callingPackageName) { |
| 6779 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | public void unregisterAudioFocusClient(String clientId) { |
| 6783 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 6784 | } |
| 6785 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 6786 | public int getCurrentAudioFocus() { |
| 6787 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 6788 | } |
| 6789 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 6790 | public int getFocusRampTimeMs(int focusGain, AudioAttributes attr) { |
| 6791 | return mMediaFocusControl.getFocusRampTimeMs(focusGain, attr); |
| 6792 | } |
| 6793 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6794 | //========================================================================================== |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6795 | private boolean readCameraSoundForced() { |
| 6796 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 6797 | mContext.getResources().getBoolean( |
| 6798 | com.android.internal.R.bool.config_camera_sound_forced); |
| 6799 | } |
| 6800 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6801 | //========================================================================================== |
| 6802 | // Device orientation |
| 6803 | //========================================================================================== |
| 6804 | /** |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 6805 | * Handles device configuration changes that may map to a change in rotation. |
| 6806 | * Monitoring rotation is optional, and is defined by the definition and value |
| 6807 | * of the "ro.audio.monitorRotation" system property. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6808 | */ |
| 6809 | private void handleConfigurationChanged(Context context) { |
| 6810 | try { |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 6811 | // reading new configuration "safely" (i.e. under try catch) in case anything |
| 6812 | // goes wrong. |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6813 | Configuration config = context.getResources().getConfiguration(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6814 | sendMsg(mAudioHandler, |
| 6815 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 6816 | SENDMSG_REPLACE, |
| 6817 | 0, |
| 6818 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6819 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6820 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6821 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 6822 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6823 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6824 | final boolean cameraSoundForcedChanged = (cameraSoundForced != mCameraSoundForced); |
| 6825 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6826 | if (cameraSoundForcedChanged) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 6827 | if (!mIsSingleVolume) { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 6828 | synchronized (VolumeStreamState.class) { |
| 6829 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 6830 | if (cameraSoundForced) { |
| 6831 | s.setAllIndexesToMax(); |
| 6832 | mRingerModeAffectedStreams &= |
| 6833 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6834 | } else { |
| 6835 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
| 6836 | mRingerModeAffectedStreams |= |
| 6837 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 6838 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6839 | } |
| 6840 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6841 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | sendMsg(mAudioHandler, |
| 6845 | MSG_SET_FORCE_USE, |
| 6846 | SENDMSG_QUEUE, |
| 6847 | AudioSystem.FOR_SYSTEM, |
| 6848 | cameraSoundForced ? |
| 6849 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6850 | new String("handleConfigurationChanged"), |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 6851 | 0); |
| 6852 | |
| 6853 | sendMsg(mAudioHandler, |
| 6854 | MSG_SET_ALL_VOLUMES, |
| 6855 | SENDMSG_QUEUE, |
| 6856 | 0, |
| 6857 | 0, |
| 6858 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 6859 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 6860 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6861 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6862 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 6863 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 6864 | } |
| 6865 | } |
| 6866 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6867 | // Handles request to override default use of A2DP for media. |
| 6868 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6869 | public void setBluetoothA2dpOnInt(boolean on, String eventSource) { |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6870 | synchronized (mBluetoothA2dpEnabledLock) { |
| 6871 | mBluetoothA2dpEnabled = on; |
| 6872 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
| 6873 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6874 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 6875 | eventSource); |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 6876 | } |
| 6877 | } |
| 6878 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6879 | // Must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6880 | private void setForceUseInt_SyncDevices(int usage, int config, String eventSource) { |
Eric Laurent | 9a5b262 | 2017-04-18 18:20:56 -0700 | [diff] [blame] | 6881 | if (usage == AudioSystem.FOR_MEDIA) { |
| 6882 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 6883 | SENDMSG_NOOP, 0, 0, null, 0); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6884 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 6885 | mForceUseLogger.log(new ForceUseEvent(usage, config, eventSource)); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 6886 | AudioSystem.setForceUse(usage, config); |
| 6887 | } |
| 6888 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 6889 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 6890 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 6891 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 6892 | mRingtonePlayer = player; |
| 6893 | } |
| 6894 | |
| 6895 | @Override |
| 6896 | public IRingtonePlayer getRingtonePlayer() { |
| 6897 | return mRingtonePlayer; |
| 6898 | } |
| 6899 | |
| 6900 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 6901 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 6902 | synchronized (mCurAudioRoutes) { |
| 6903 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 6904 | mRoutesObservers.register(observer); |
| 6905 | return routes; |
| 6906 | } |
| 6907 | } |
| 6908 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6909 | |
| 6910 | //========================================================================================== |
| 6911 | // Safe media volume management. |
| 6912 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 6913 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 6914 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 6915 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 6916 | // property. Platforms with a different limit must set this property accordingly in their |
| 6917 | // overlay. |
| 6918 | //========================================================================================== |
| 6919 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6920 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 6921 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 6922 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 6923 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 6924 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 6925 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 6926 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 6927 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 6928 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 6929 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6930 | private Integer mSafeMediaVolumeState; |
| 6931 | |
| 6932 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6933 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6934 | private int mSafeMediaVolumeIndex; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 6935 | // mSafeUsbMediaVolumeDbfs is the cached value of the config_safe_media_volume_usb_mB |
| 6936 | // property, divided by 100.0. |
| 6937 | private float mSafeUsbMediaVolumeDbfs; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6938 | // 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] | 6939 | // corresponding to a gain of mSafeUsbMediaVolumeDbfs (defaulting to -37dB) in audio |
| 6940 | // flinger mixer. |
Eric Laurent | 0e5deb3 | 2017-09-01 15:12:42 -0700 | [diff] [blame] | 6941 | // We remove -22 dBs from the theoretical -15dB to account for the EQ + bass boost |
| 6942 | // amplification when both effects are on with all band gains at maximum. |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 6943 | // This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when |
| 6944 | // 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] | 6945 | private int mSafeUsbMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6946 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 6947 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6948 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 6949 | AudioSystem.DEVICE_OUT_USB_HEADSET; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6950 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 6951 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 6952 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 6953 | private int mMusicActiveMs; |
| 6954 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 6955 | 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] | 6956 | 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] | 6957 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 6958 | private int safeMediaVolumeIndex(int device) { |
| 6959 | if ((device & mSafeMediaVolumeDevices) == 0) { |
| 6960 | return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 6961 | } |
| 6962 | if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 6963 | return mSafeUsbMediaVolumeIndex; |
| 6964 | } else { |
| 6965 | return mSafeMediaVolumeIndex; |
| 6966 | } |
| 6967 | } |
| 6968 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6969 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6970 | synchronized (mSafeMediaVolumeState) { |
| 6971 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 6972 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 6973 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 6974 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6975 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6976 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 6977 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 6978 | mMusicActiveMs = 1; // nonzero = confirmed |
| 6979 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6980 | sendMsg(mAudioHandler, |
| 6981 | MSG_CHECK_MUSIC_ACTIVE, |
| 6982 | SENDMSG_REPLACE, |
| 6983 | 0, |
| 6984 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6985 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 6986 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 6987 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6988 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6989 | } |
| 6990 | } |
| 6991 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6992 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6993 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 6994 | int devices = mSafeMediaVolumeDevices; |
| 6995 | int i = 0; |
| 6996 | |
| 6997 | while (devices != 0) { |
| 6998 | int device = 1 << i++; |
| 6999 | if ((device & devices) == 0) { |
| 7000 | continue; |
| 7001 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 7002 | int index = streamState.getIndex(device); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 7003 | if (index > safeMediaVolumeIndex(device)) { |
| 7004 | streamState.setIndex(safeMediaVolumeIndex(device), device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 7005 | sendMsg(mAudioHandler, |
| 7006 | MSG_SET_DEVICE_VOLUME, |
| 7007 | SENDMSG_QUEUE, |
| 7008 | device, |
| 7009 | 0, |
| 7010 | streamState, |
| 7011 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 7012 | } |
| 7013 | devices &= ~device; |
| 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 7018 | synchronized (mSafeMediaVolumeState) { |
| 7019 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 7020 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 7021 | ((device & mSafeMediaVolumeDevices) != 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 7022 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 7023 | return false; |
| 7024 | } |
| 7025 | return true; |
| 7026 | } |
| 7027 | } |
| 7028 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7029 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7030 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7031 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 7032 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7033 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 7034 | if (mPendingVolumeCommand != null) { |
| 7035 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 7036 | mPendingVolumeCommand.mIndex, |
| 7037 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7038 | mPendingVolumeCommand.mDevice, |
| 7039 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 7040 | mPendingVolumeCommand = null; |
| 7041 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 7042 | } |
| 7043 | } |
| 7044 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7045 | //========================================================================================== |
| 7046 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 7047 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 7048 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7049 | //========================================================================================== |
| 7050 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7051 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 7052 | public void onComplete(int status) { |
| 7053 | if (mHdmiManager != null) { |
| 7054 | synchronized (mHdmiManager) { |
| 7055 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 7056 | // Television devices without CEC service apply software volume on HDMI output |
| 7057 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 7058 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 7059 | } |
| 7060 | checkAllFixedVolumeDevices(); |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | }; |
| 7065 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7066 | // If HDMI-CEC system audio is supported |
| 7067 | private boolean mHdmiSystemAudioSupported = false; |
| 7068 | // Set only when device is tv. |
| 7069 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 7070 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7071 | // cached HdmiControlManager interface |
| 7072 | private HdmiControlManager mHdmiManager; |
| 7073 | // Set only when device is a set-top box. |
| 7074 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 7075 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 7076 | private boolean mHdmiCecSink; |
| 7077 | |
| 7078 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7079 | |
| 7080 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 7081 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7082 | int device = AudioSystem.DEVICE_NONE; |
| 7083 | if (mHdmiManager != null) { |
| 7084 | synchronized (mHdmiManager) { |
| 7085 | if (mHdmiTvClient == null) { |
| 7086 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 7087 | return device; |
| 7088 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7089 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7090 | synchronized (mHdmiTvClient) { |
| 7091 | if (mHdmiSystemAudioSupported != on) { |
| 7092 | mHdmiSystemAudioSupported = on; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7093 | final int config = on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 7094 | AudioSystem.FORCE_NONE; |
| 7095 | mForceUseLogger.log(new ForceUseEvent(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 7096 | config, "setHdmiSystemAudioSupported")); |
| 7097 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, config); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7098 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 7099 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7100 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 7101 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 7102 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 7103 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 7104 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 7105 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 7106 | @Override |
| 7107 | public boolean isHdmiSystemAudioSupported() { |
| 7108 | return mHdmiSystemAudioSupported; |
| 7109 | } |
| 7110 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7111 | //========================================================================================== |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7112 | // Accessibility |
| 7113 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 7114 | private void initA11yMonitoring() { |
| 7115 | final AccessibilityManager accessibilityManager = |
| 7116 | (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7117 | updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled()); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 7118 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
Phil Weaver | 26d709f | 2017-04-20 17:19:14 -0700 | [diff] [blame] | 7119 | accessibilityManager.addTouchExplorationStateChangeListener(this, null); |
| 7120 | accessibilityManager.addAccessibilityServicesStateChangeListener(this, null); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | //--------------------------------------------------------------------------------- |
| 7124 | // A11y: taking touch exploration into account for selecting the default |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7125 | // stream override timeout when adjusting volume |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7126 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7127 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7128 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 7129 | // - STREAM_RING on phones during this period after a notification stopped |
| 7130 | // - STREAM_MUSIC otherwise |
| 7131 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7132 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
| 7133 | 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] | 7134 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7135 | private static int sStreamOverrideDelayMs; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7136 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7137 | @Override |
| 7138 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 7139 | updateDefaultStreamOverrideDelay(enabled); |
| 7140 | } |
| 7141 | |
| 7142 | private void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 7143 | if (touchExploreEnabled) { |
| 7144 | sStreamOverrideDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 7145 | } else { |
| 7146 | sStreamOverrideDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7147 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7148 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 7149 | + " stream override delay is now " + sStreamOverrideDelayMs + " ms"); |
| 7150 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7151 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7152 | //--------------------------------------------------------------------------------- |
| 7153 | // A11y: taking a11y state into account for the handling of a11y prompts volume |
| 7154 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7155 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7156 | private static boolean sIndependentA11yVolume = false; |
| 7157 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 7158 | // implementation of AccessibilityServicesStateChangeListener |
| 7159 | @Override |
Phil Weaver | 4cab930 | 2017-03-30 15:27:39 -0700 | [diff] [blame] | 7160 | public void onAccessibilityServicesStateChanged(AccessibilityManager accessibilityManager) { |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 7161 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
| 7162 | } |
| 7163 | |
| 7164 | private void updateA11yVolumeAlias(boolean a11VolEnabled) { |
| 7165 | if (DEBUG_VOL) Log.d(TAG, "Accessibility volume enabled = " + a11VolEnabled); |
| 7166 | if (sIndependentA11yVolume != a11VolEnabled) { |
| 7167 | sIndependentA11yVolume = a11VolEnabled; |
| 7168 | // update the volume mapping scheme |
| 7169 | updateStreamVolumeAlias(true /*updateVolumes*/, TAG); |
| 7170 | // update the volume controller behavior |
| 7171 | mVolumeController.setA11yMode(sIndependentA11yVolume ? |
| 7172 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 7173 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 7174 | mVolumeController.postVolumeChanged(AudioManager.STREAM_ACCESSIBILITY, 0); |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 7175 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7179 | // Camera shutter sound policy. |
| 7180 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 7181 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 7182 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 7183 | //========================================================================================== |
| 7184 | |
| 7185 | // 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] | 7186 | @GuardedBy("mSettingsLock") |
| 7187 | private boolean mCameraSoundForced; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7188 | |
| 7189 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 7190 | public boolean isCameraSoundForced() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 7191 | synchronized (mSettingsLock) { |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7192 | return mCameraSoundForced; |
| 7193 | } |
| 7194 | } |
| 7195 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7196 | //========================================================================================== |
| 7197 | // AudioService logging and dumpsys |
| 7198 | //========================================================================================== |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 7199 | static final int LOG_NB_EVENTS_PHONE_STATE = 20; |
| 7200 | static final int LOG_NB_EVENTS_DEVICE_CONNECTION = 30; |
| 7201 | static final int LOG_NB_EVENTS_FORCE_USE = 20; |
| 7202 | static final int LOG_NB_EVENTS_VOLUME = 40; |
| 7203 | static final int LOG_NB_EVENTS_DYN_POLICY = 10; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7204 | |
| 7205 | final private AudioEventLogger mModeLogger = new AudioEventLogger(LOG_NB_EVENTS_PHONE_STATE, |
| 7206 | "phone state (logged after successfull call to AudioSystem.setPhoneState(int))"); |
| 7207 | |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 7208 | // logs for wired + A2DP device connections: |
| 7209 | // - wired: logged before onSetWiredDeviceConnectionState() is executed |
| 7210 | // - A2DP: logged at reception of method call |
| 7211 | final private AudioEventLogger mDeviceLogger = new AudioEventLogger( |
| 7212 | LOG_NB_EVENTS_DEVICE_CONNECTION, "wired/A2DP device connection"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7213 | |
| 7214 | final private AudioEventLogger mForceUseLogger = new AudioEventLogger( |
| 7215 | LOG_NB_EVENTS_FORCE_USE, |
| 7216 | "force use (logged before setForceUse() is executed)"); |
| 7217 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7218 | final private AudioEventLogger mVolumeLogger = new AudioEventLogger(LOG_NB_EVENTS_VOLUME, |
| 7219 | "volume changes (logged when command received by AudioService)"); |
| 7220 | |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 7221 | final private AudioEventLogger mDynPolicyLogger = new AudioEventLogger(LOG_NB_EVENTS_DYN_POLICY, |
| 7222 | "dynamic policy events (logged when command received by AudioService)"); |
| 7223 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7224 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 7225 | "SILENT", |
| 7226 | "VIBRATE", |
| 7227 | "NORMAL" |
| 7228 | }; |
| 7229 | |
| 7230 | private void dumpRingerMode(PrintWriter pw) { |
| 7231 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7232 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 7233 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7234 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7235 | dumpRingerModeStreams(pw, "muted", mRingerAndZenModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7236 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7237 | } |
| 7238 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7239 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 7240 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 7241 | pw.print(Integer.toHexString(streams)); |
| 7242 | if (streams != 0) { |
| 7243 | pw.print(" ("); |
| 7244 | boolean first = true; |
| 7245 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 7246 | final int stream = (1 << i); |
| 7247 | if ((streams & stream) != 0) { |
| 7248 | if (!first) pw.print(','); |
| 7249 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 7250 | streams &= ~stream; |
| 7251 | first = false; |
| 7252 | } |
| 7253 | } |
| 7254 | if (streams != 0) { |
| 7255 | if (!first) pw.print(','); |
| 7256 | pw.print(streams); |
| 7257 | } |
| 7258 | pw.print(')'); |
| 7259 | } |
| 7260 | pw.println(); |
| 7261 | } |
| 7262 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 7263 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 7264 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 7265 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 7266 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 7267 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 7268 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 7269 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 7270 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 7271 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 7272 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7273 | |
| 7274 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7275 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7276 | pw.print(" mSafeMediaVolumeState="); |
| 7277 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 7278 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 7279 | pw.print(" mSafeUsbMediaVolumeIndex="); pw.println(mSafeUsbMediaVolumeIndex); |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 7280 | pw.print(" mSafeUsbMediaVolumeDbfs="); pw.println(mSafeUsbMediaVolumeDbfs); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 7281 | pw.print(" sIndependentA11yVolume="); pw.println(sIndependentA11yVolume); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7282 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 7283 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 7284 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 7285 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7286 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7287 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 7288 | pw.print(" mAvrcpAbsVolSupported="); pw.println(mAvrcpAbsVolSupported); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7289 | |
| 7290 | dumpAudioPolicies(pw); |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 7291 | mDynPolicyLogger.dump(pw); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7292 | |
| 7293 | mPlaybackMonitor.dump(pw); |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7294 | |
| 7295 | mRecordMonitor.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7296 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7297 | pw.println("\n"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7298 | pw.println("\nEvent logs:"); |
| 7299 | mModeLogger.dump(pw); |
| 7300 | pw.println("\n"); |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 7301 | mDeviceLogger.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 7302 | pw.println("\n"); |
| 7303 | mForceUseLogger.dump(pw); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 7304 | pw.println("\n"); |
| 7305 | mVolumeLogger.dump(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 7306 | } |
| 7307 | |
| 7308 | private static String safeMediaVolumeStateToString(Integer state) { |
| 7309 | switch(state) { |
| 7310 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 7311 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 7312 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 7313 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 7314 | } |
| 7315 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 7316 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 7317 | |
| 7318 | // Inform AudioFlinger of our device's low RAM attribute |
| 7319 | private static void readAndSetLowRamDevice() |
| 7320 | { |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 7321 | boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic(); |
| 7322 | long totalMemory = 1024 * 1024 * 1024; // 1GB is the default if ActivityManager fails. |
| 7323 | |
| 7324 | try { |
| 7325 | final ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); |
| 7326 | ActivityManager.getService().getMemoryInfo(info); |
| 7327 | totalMemory = info.totalMem; |
| 7328 | } catch (RemoteException e) { |
| 7329 | Log.w(TAG, "Cannot obtain MemoryInfo from ActivityManager, assume low memory device"); |
| 7330 | isLowRamDevice = true; |
| 7331 | } |
| 7332 | |
| 7333 | final int status = AudioSystem.setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 7334 | if (status != 0) { |
| 7335 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 7336 | } |
| 7337 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7338 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7339 | private void enforceVolumeController(String action) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7340 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 7341 | "Only SystemUI can " + action); |
| 7342 | } |
| 7343 | |
| 7344 | @Override |
| 7345 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7346 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7347 | |
| 7348 | // return early if things are not actually changing |
| 7349 | if (mVolumeController.isSameBinder(controller)) { |
| 7350 | return; |
| 7351 | } |
| 7352 | |
| 7353 | // dismiss the old volume controller |
| 7354 | mVolumeController.postDismiss(); |
| 7355 | if (controller != null) { |
| 7356 | // we are about to register a new controller, listen for its death |
| 7357 | try { |
| 7358 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 7359 | @Override |
| 7360 | public void binderDied() { |
| 7361 | if (mVolumeController.isSameBinder(controller)) { |
| 7362 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 7363 | setVolumeController(null); |
| 7364 | } |
| 7365 | } |
| 7366 | }, 0); |
| 7367 | } catch (RemoteException e) { |
| 7368 | // noop |
| 7369 | } |
| 7370 | } |
| 7371 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7372 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 7373 | } |
| 7374 | |
| 7375 | @Override |
| 7376 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7377 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7378 | |
| 7379 | // return early if the controller is not current |
| 7380 | if (!mVolumeController.isSameBinder(controller)) { |
| 7381 | return; |
| 7382 | } |
| 7383 | |
| 7384 | mVolumeController.setVisible(visible); |
| 7385 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 7386 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7387 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7388 | @Override |
| 7389 | public void setVolumePolicy(VolumePolicy policy) { |
| 7390 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 7391 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7392 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 7393 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 7394 | } |
| 7395 | } |
| 7396 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7397 | public static class VolumeController { |
| 7398 | private static final String TAG = "VolumeController"; |
| 7399 | |
| 7400 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7401 | private boolean mVisible; |
| 7402 | private long mNextLongPress; |
| 7403 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7404 | |
| 7405 | public void setController(IVolumeController controller) { |
| 7406 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7407 | mVisible = false; |
| 7408 | } |
| 7409 | |
| 7410 | public void loadSettings(ContentResolver cr) { |
| 7411 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 7412 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 7413 | } |
| 7414 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 7415 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 7416 | if (isMute) { |
| 7417 | return false; |
| 7418 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7419 | boolean suppress = false; |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 7420 | if (resolvedStream == DEFAULT_VOL_STREAM_NO_PLAYBACK && mController != null) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7421 | final long now = SystemClock.uptimeMillis(); |
| 7422 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 7423 | // ui will become visible |
| 7424 | if (mNextLongPress < now) { |
| 7425 | mNextLongPress = now + mLongPressTimeout; |
| 7426 | } |
| 7427 | suppress = true; |
| 7428 | } else if (mNextLongPress > 0) { // in a long-press |
| 7429 | if (now > mNextLongPress) { |
| 7430 | // long press triggered, no more suppression |
| 7431 | mNextLongPress = 0; |
| 7432 | } else { |
| 7433 | // keep suppressing until the long press triggers |
| 7434 | suppress = true; |
| 7435 | } |
| 7436 | } |
| 7437 | } |
| 7438 | return suppress; |
| 7439 | } |
| 7440 | |
| 7441 | public void setVisible(boolean visible) { |
| 7442 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | public boolean isSameBinder(IVolumeController controller) { |
| 7446 | return Objects.equals(asBinder(), binder(controller)); |
| 7447 | } |
| 7448 | |
| 7449 | public IBinder asBinder() { |
| 7450 | return binder(mController); |
| 7451 | } |
| 7452 | |
| 7453 | private static IBinder binder(IVolumeController controller) { |
| 7454 | return controller == null ? null : controller.asBinder(); |
| 7455 | } |
| 7456 | |
| 7457 | @Override |
| 7458 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 7459 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7460 | } |
| 7461 | |
| 7462 | public void postDisplaySafeVolumeWarning(int flags) { |
| 7463 | if (mController == null) |
| 7464 | return; |
| 7465 | try { |
| 7466 | mController.displaySafeVolumeWarning(flags); |
| 7467 | } catch (RemoteException e) { |
| 7468 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 7469 | } |
| 7470 | } |
| 7471 | |
| 7472 | public void postVolumeChanged(int streamType, int flags) { |
| 7473 | if (mController == null) |
| 7474 | return; |
| 7475 | try { |
| 7476 | mController.volumeChanged(streamType, flags); |
| 7477 | } catch (RemoteException e) { |
| 7478 | Log.w(TAG, "Error calling volumeChanged", e); |
| 7479 | } |
| 7480 | } |
| 7481 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7482 | public void postMasterMuteChanged(int flags) { |
| 7483 | if (mController == null) |
| 7484 | return; |
| 7485 | try { |
| 7486 | mController.masterMuteChanged(flags); |
| 7487 | } catch (RemoteException e) { |
| 7488 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | public void setLayoutDirection(int layoutDirection) { |
| 7493 | if (mController == null) |
| 7494 | return; |
| 7495 | try { |
| 7496 | mController.setLayoutDirection(layoutDirection); |
| 7497 | } catch (RemoteException e) { |
| 7498 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | public void postDismiss() { |
| 7503 | if (mController == null) |
| 7504 | return; |
| 7505 | try { |
| 7506 | mController.dismiss(); |
| 7507 | } catch (RemoteException e) { |
| 7508 | Log.w(TAG, "Error calling dismiss", e); |
| 7509 | } |
| 7510 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 7511 | |
| 7512 | public void setA11yMode(int a11yMode) { |
| 7513 | if (mController == null) |
| 7514 | return; |
| 7515 | try { |
| 7516 | mController.setA11yMode(a11yMode); |
| 7517 | } catch (RemoteException e) { |
| 7518 | Log.w(TAG, "Error calling setA11Mode", e); |
| 7519 | } |
| 7520 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 7521 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7522 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7523 | /** |
| 7524 | * Interface for system components to get some extra functionality through |
| 7525 | * LocalServices. |
| 7526 | */ |
| 7527 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7528 | @Override |
| 7529 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 7530 | mRingerModeDelegate = delegate; |
| 7531 | if (mRingerModeDelegate != null) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 7532 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7533 | updateRingerAndZenModeAffectedStreams(); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 7534 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7535 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 7536 | } |
| 7537 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 7538 | |
| 7539 | @Override |
| 7540 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 7541 | String callingPackage, int uid) { |
| 7542 | // direction and stream type swap here because the public |
| 7543 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7544 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 7545 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 7546 | } |
| 7547 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7548 | @Override |
| 7549 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 7550 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7551 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 7552 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7553 | } |
| 7554 | |
| 7555 | @Override |
| 7556 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 7557 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 7558 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7559 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 7560 | |
| 7561 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 7562 | public int getRingerModeInternal() { |
| 7563 | return AudioService.this.getRingerModeInternal(); |
| 7564 | } |
| 7565 | |
| 7566 | @Override |
| 7567 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 7568 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 7569 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 7570 | |
| 7571 | @Override |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 7572 | public void silenceRingerModeInternal(String caller) { |
| 7573 | AudioService.this.silenceRingerModeInternal(caller); |
| 7574 | } |
| 7575 | |
| 7576 | @Override |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7577 | public void updateRingerModeAffectedStreamsInternal() { |
| 7578 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 7579 | if (updateRingerAndZenModeAffectedStreams()) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 7580 | setRingerModeInt(getRingerModeInternal(), false); |
| 7581 | } |
| 7582 | } |
| 7583 | } |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 7584 | |
| 7585 | @Override |
| 7586 | public void setAccessibilityServiceUids(IntArray uids) { |
| 7587 | synchronized (mAccessibilityServiceUidsLock) { |
| 7588 | if (uids.size() == 0) { |
| 7589 | mAccessibilityServiceUids = null; |
| 7590 | } else { |
| 7591 | boolean changed = (mAccessibilityServiceUids == null) |
| 7592 | || (mAccessibilityServiceUids.length != uids.size()); |
| 7593 | if (!changed) { |
| 7594 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 7595 | if (uids.get(i) != mAccessibilityServiceUids[i]) { |
| 7596 | changed = true; |
| 7597 | break; |
| 7598 | } |
| 7599 | } |
| 7600 | } |
| 7601 | if (changed) { |
| 7602 | mAccessibilityServiceUids = uids.toArray(); |
| 7603 | } |
| 7604 | } |
| 7605 | } |
| 7606 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 7607 | } |
| 7608 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7609 | //========================================================================================== |
| 7610 | // Audio policy management |
| 7611 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7612 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7613 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7614 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 7615 | |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7616 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7617 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7618 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7619 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7620 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7621 | if (!hasPermissionForPolicy) { |
| 7622 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 7623 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7624 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7625 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7626 | |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 7627 | mDynPolicyLogger.log((new AudioEventLogger.StringEvent("registerAudioPolicy for " |
| 7628 | + pcb.asBinder() + " with config:" + policyConfig)).printLog(TAG)); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7629 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7630 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7631 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7632 | Slog.e(TAG, "Cannot re-register policy"); |
| 7633 | return null; |
| 7634 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7635 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7636 | isFocusPolicy, isVolumeController); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7637 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 7638 | regId = app.getRegistrationId(); |
| 7639 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7640 | } catch (RemoteException e) { |
| 7641 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7642 | 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] | 7643 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7644 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7645 | } |
| 7646 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7647 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7648 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7649 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7650 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 7651 | mDynPolicyLogger.log((new AudioEventLogger.StringEvent("unregisterAudioPolicyAsync for " |
| 7652 | + pcb.asBinder()).printLog(TAG))); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7653 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7654 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7655 | if (app == null) { |
| 7656 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 7657 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7658 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7659 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7660 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7661 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7662 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7663 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7664 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7665 | } |
| 7666 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7667 | /** |
| 7668 | * Checks whether caller has MODIFY_AUDIO_ROUTING permission, and the policy is registered. |
| 7669 | * @param errorMsg log warning if permission check failed. |
| 7670 | * @return null if the operation on the audio mixes should be cancelled. |
| 7671 | */ |
| 7672 | @GuardedBy("mAudioPolicies") |
| 7673 | private AudioPolicyProxy checkUpdateForPolicy(IAudioPolicyCallback pcb, String errorMsg) { |
| 7674 | // permission check |
| 7675 | final boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7676 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7677 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7678 | if (!hasPermissionForPolicy) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7679 | Slog.w(TAG, errorMsg + " for pid " + |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7680 | + Binder.getCallingPid() + " / uid " |
| 7681 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7682 | return null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7683 | } |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7684 | // policy registered? |
| 7685 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 7686 | if (app == null) { |
| 7687 | Slog.w(TAG, errorMsg + " for pid " + |
| 7688 | + Binder.getCallingPid() + " / uid " |
| 7689 | + Binder.getCallingUid() + ", unregistered policy"); |
| 7690 | return null; |
| 7691 | } |
| 7692 | return app; |
| 7693 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7694 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7695 | public int addMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 7696 | if (DEBUG_AP) { Log.d(TAG, "addMixForPolicy for " + pcb.asBinder() |
| 7697 | + " with config:" + policyConfig); } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7698 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7699 | final AudioPolicyProxy app = |
| 7700 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 7701 | if (app == null){ |
| 7702 | return AudioManager.ERROR; |
| 7703 | } |
| 7704 | app.addMixes(policyConfig.getMixes()); |
| 7705 | } |
| 7706 | return AudioManager.SUCCESS; |
| 7707 | } |
| 7708 | |
| 7709 | public int removeMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 7710 | if (DEBUG_AP) { Log.d(TAG, "removeMixForPolicy for " + pcb.asBinder() |
| 7711 | + " with config:" + policyConfig); } |
| 7712 | synchronized (mAudioPolicies) { |
| 7713 | final AudioPolicyProxy app = |
| 7714 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 7715 | if (app == null) { |
| 7716 | return AudioManager.ERROR; |
| 7717 | } |
| 7718 | app.removeMixes(policyConfig.getMixes()); |
| 7719 | } |
| 7720 | return AudioManager.SUCCESS; |
| 7721 | } |
| 7722 | |
| 7723 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 7724 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 7725 | + " policy " + pcb.asBinder()); |
| 7726 | synchronized (mAudioPolicies) { |
| 7727 | final AudioPolicyProxy app = |
| 7728 | checkUpdateForPolicy(pcb, "Cannot change audio policy focus properties"); |
| 7729 | if (app == null){ |
| 7730 | return AudioManager.ERROR; |
| 7731 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7732 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 7733 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 7734 | return AudioManager.ERROR; |
| 7735 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7736 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7737 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 7738 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7739 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7740 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 7741 | return AudioManager.ERROR; |
| 7742 | } |
| 7743 | } |
| 7744 | } |
| 7745 | app.mFocusDuckBehavior = duckingBehavior; |
| 7746 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 7747 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 7748 | } |
| 7749 | return AudioManager.SUCCESS; |
| 7750 | } |
| 7751 | |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7752 | private final Object mExtVolumeControllerLock = new Object(); |
| 7753 | private IAudioPolicyCallback mExtVolumeController; |
| 7754 | private void setExtVolumeController(IAudioPolicyCallback apc) { |
| 7755 | if (!mContext.getResources().getBoolean( |
| 7756 | com.android.internal.R.bool.config_handleVolumeKeysInWindowManager)) { |
| 7757 | Log.e(TAG, "Cannot set external volume controller: device not set for volume keys" + |
| 7758 | " handled in PhoneWindowManager"); |
| 7759 | return; |
| 7760 | } |
| 7761 | synchronized (mExtVolumeControllerLock) { |
| 7762 | if (mExtVolumeController != null && !mExtVolumeController.asBinder().pingBinder()) { |
| 7763 | Log.e(TAG, "Cannot set external volume controller: existing controller"); |
| 7764 | } |
| 7765 | mExtVolumeController = apc; |
| 7766 | } |
| 7767 | } |
| 7768 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7769 | private void dumpAudioPolicies(PrintWriter pw) { |
| 7770 | pw.println("\nAudio policies:"); |
| 7771 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 7772 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7773 | pw.println(policy.toLogFriendlyString()); |
| 7774 | } |
| 7775 | } |
| 7776 | } |
| 7777 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7778 | //====================== |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7779 | // Audio policy callbacks from AudioSystem for dynamic policies |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7780 | //====================== |
| 7781 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 7782 | new AudioSystem.DynamicPolicyCallback() { |
| 7783 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 7784 | if (!TextUtils.isEmpty(regId)) { |
| 7785 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 7786 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 7787 | } |
| 7788 | } |
| 7789 | }; |
| 7790 | |
| 7791 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 7792 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 7793 | synchronized (mAudioPolicies) { |
| 7794 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 7795 | for (AudioMix mix : policy.getMixes()) { |
| 7796 | if (mix.getRegistration().equals(regId)) { |
| 7797 | try { |
| 7798 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 7799 | } catch (RemoteException e) { |
| 7800 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 7801 | + policy.mPolicyCallback.asBinder(), e); |
| 7802 | } |
| 7803 | return; |
| 7804 | } |
| 7805 | } |
| 7806 | } |
| 7807 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7808 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7809 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7810 | //====================== |
| 7811 | // Audio policy callbacks from AudioSystem for recording configuration updates |
| 7812 | //====================== |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7813 | private final RecordingActivityMonitor mRecordMonitor; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7814 | |
| 7815 | public void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7816 | final boolean isPrivileged = |
| 7817 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7818 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7819 | mRecordMonitor.registerRecordingCallback(rcdb, isPrivileged); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 7820 | } |
| 7821 | |
| 7822 | public void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 7823 | mRecordMonitor.unregisterRecordingCallback(rcdb); |
| 7824 | } |
| 7825 | |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 7826 | public List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 7827 | final boolean isPrivileged = |
| 7828 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 7829 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7830 | return mRecordMonitor.getActiveRecordingConfigurations(isPrivileged); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7831 | } |
| 7832 | |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7833 | public void disableRingtoneSync(final int userId) { |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7834 | final int callingUserId = UserHandle.getCallingUserId(); |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7835 | if (callingUserId != userId) { |
| 7836 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 7837 | "disable sound settings syncing for another profile"); |
| 7838 | } |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7839 | final long token = Binder.clearCallingIdentity(); |
| 7840 | try { |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 7841 | // Disable the sync setting so the profile uses its own sound settings. |
| 7842 | Settings.Secure.putIntForUser(mContentResolver, Settings.Secure.SYNC_PARENT_SOUNDS, |
| 7843 | 0 /* false */, userId); |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 7844 | } finally { |
| 7845 | Binder.restoreCallingIdentity(token); |
| 7846 | } |
| 7847 | } |
| 7848 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7849 | //====================== |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7850 | // Audio playback notification |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7851 | //====================== |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 7852 | private final PlaybackActivityMonitor mPlaybackMonitor; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7853 | |
| 7854 | public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7855 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7856 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7857 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7858 | mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged); |
| 7859 | } |
| 7860 | |
| 7861 | public void unregisterPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
| 7862 | mPlaybackMonitor.unregisterPlaybackCallback(pcdb); |
| 7863 | } |
| 7864 | |
| 7865 | public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7866 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 7867 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7868 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 7869 | return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7870 | } |
| 7871 | |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 7872 | public int trackPlayer(PlayerBase.PlayerIdCard pic) { |
| 7873 | return mPlaybackMonitor.trackPlayer(pic); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | public void playerAttributes(int piid, AudioAttributes attr) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7877 | mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | public void playerEvent(int piid, int event) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7881 | mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7882 | } |
| 7883 | |
Jean-Michel Trivi | 3120059d | 2017-08-28 12:40:55 -0700 | [diff] [blame] | 7884 | public void playerHasOpPlayAudio(int piid, boolean hasOpPlayAudio) { |
| 7885 | mPlaybackMonitor.playerHasOpPlayAudio(piid, hasOpPlayAudio, Binder.getCallingUid()); |
| 7886 | } |
| 7887 | |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7888 | public void releasePlayer(int piid) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 7889 | mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 7890 | } |
| 7891 | |
| 7892 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 7893 | // Audio policy proxy |
| 7894 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7895 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7896 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 7897 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7898 | */ |
| 7899 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7900 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7901 | final IAudioPolicyCallback mPolicyCallback; |
| 7902 | final boolean mHasFocusListener; |
| 7903 | final boolean mIsVolumeController; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7904 | /** |
| 7905 | * Audio focus ducking behavior for an audio policy. |
| 7906 | * This variable reflects the value that was successfully set in |
| 7907 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 7908 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 7909 | * is handling ducking for audio focus. |
| 7910 | */ |
| 7911 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7912 | boolean mIsFocusPolicy = false; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7913 | |
| 7914 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7915 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7916 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7917 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7918 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7919 | mHasFocusListener = hasFocusListener; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7920 | mIsVolumeController = isVolumeController; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7921 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7922 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7923 | // can only ever be true if there is a focus listener |
| 7924 | if (isFocusPolicy) { |
| 7925 | mIsFocusPolicy = true; |
| 7926 | mMediaFocusControl.setFocusPolicy(mPolicyCallback); |
| 7927 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7928 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7929 | if (mIsVolumeController) { |
| 7930 | setExtVolumeController(mPolicyCallback); |
| 7931 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7932 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7933 | } |
| 7934 | |
| 7935 | public void binderDied() { |
| 7936 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7937 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7938 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7939 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7940 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 7941 | if (mIsVolumeController) { |
| 7942 | synchronized (mExtVolumeControllerLock) { |
| 7943 | mExtVolumeController = null; |
| 7944 | } |
| 7945 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7946 | } |
| 7947 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7948 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 7949 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7950 | } |
| 7951 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7952 | void release() { |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 7953 | if (mIsFocusPolicy) { |
| 7954 | mMediaFocusControl.unsetFocusPolicy(mPolicyCallback); |
| 7955 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7956 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 7957 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 7958 | } |
| 7959 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 7960 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 7961 | } |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7962 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7963 | AudioSystem.registerPolicyMixes(mMixes, false); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7964 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 7965 | } |
| 7966 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 7967 | boolean hasMixAffectingUsage(int usage) { |
| 7968 | for (AudioMix mix : mMixes) { |
| 7969 | if (mix.isAffectingUsage(usage)) { |
| 7970 | return true; |
| 7971 | } |
| 7972 | } |
| 7973 | return false; |
| 7974 | } |
| 7975 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 7976 | void addMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 7977 | // TODO optimize to not have to unregister the mixes already in place |
| 7978 | synchronized (mMixes) { |
| 7979 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 7980 | this.add(mixes); |
| 7981 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 7982 | } |
| 7983 | } |
| 7984 | |
| 7985 | void removeMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 7986 | // TODO optimize to not have to unregister the mixes already in place |
| 7987 | synchronized (mMixes) { |
| 7988 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 7989 | this.remove(mixes); |
| 7990 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 7991 | } |
| 7992 | } |
| 7993 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7994 | void connectMixes() { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7995 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 7996 | AudioSystem.registerPolicyMixes(mMixes, true); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 7997 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 7998 | } |
| 7999 | }; |
| 8000 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 8001 | //====================== |
| 8002 | // Audio policy: focus |
| 8003 | //====================== |
| 8004 | /** */ |
| 8005 | public int dispatchFocusChange(AudioFocusInfo afi, int focusChange, IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 8006 | if (afi == null) { |
| 8007 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 8008 | } |
| 8009 | if (pcb == null) { |
| 8010 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 8011 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 8012 | synchronized (mAudioPolicies) { |
| 8013 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 8014 | throw new IllegalStateException("Unregistered AudioPolicy for focus dispatch"); |
| 8015 | } |
| 8016 | return mMediaFocusControl.dispatchFocusChange(afi, focusChange); |
| 8017 | } |
| 8018 | } |
| 8019 | |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 8020 | public void setFocusRequestResultFromExtPolicy(AudioFocusInfo afi, int requestResult, |
| 8021 | IAudioPolicyCallback pcb) { |
| 8022 | if (afi == null) { |
| 8023 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 8024 | } |
| 8025 | if (pcb == null) { |
| 8026 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 8027 | } |
| 8028 | synchronized (mAudioPolicies) { |
| 8029 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 8030 | throw new IllegalStateException("Unregistered AudioPolicy for external focus"); |
| 8031 | } |
| 8032 | mMediaFocusControl.setFocusRequestResultFromExtPolicy(afi, requestResult); |
| 8033 | } |
| 8034 | } |
| 8035 | |
| 8036 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 8037 | //====================== |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 8038 | // Audioserver state displatch |
| 8039 | //====================== |
| 8040 | private class AsdProxy implements IBinder.DeathRecipient { |
| 8041 | private final IAudioServerStateDispatcher mAsd; |
| 8042 | |
| 8043 | AsdProxy(IAudioServerStateDispatcher asd) { |
| 8044 | mAsd = asd; |
| 8045 | } |
| 8046 | |
| 8047 | public void binderDied() { |
| 8048 | synchronized (mAudioServerStateListeners) { |
| 8049 | mAudioServerStateListeners.remove(mAsd.asBinder()); |
| 8050 | } |
| 8051 | } |
| 8052 | |
| 8053 | IAudioServerStateDispatcher callback() { |
| 8054 | return mAsd; |
| 8055 | } |
| 8056 | } |
| 8057 | |
| 8058 | private HashMap<IBinder, AsdProxy> mAudioServerStateListeners = |
| 8059 | new HashMap<IBinder, AsdProxy>(); |
| 8060 | |
| 8061 | private void checkMonitorAudioServerStatePermission() { |
| 8062 | if (!(mContext.checkCallingOrSelfPermission( |
| 8063 | android.Manifest.permission.MODIFY_PHONE_STATE) == |
| 8064 | PackageManager.PERMISSION_GRANTED || |
| 8065 | mContext.checkCallingOrSelfPermission( |
| 8066 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) == |
| 8067 | PackageManager.PERMISSION_GRANTED)) { |
| 8068 | throw new SecurityException("Not allowed to monitor audioserver state"); |
| 8069 | } |
| 8070 | } |
| 8071 | |
| 8072 | public void registerAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 8073 | checkMonitorAudioServerStatePermission(); |
| 8074 | synchronized (mAudioServerStateListeners) { |
| 8075 | if (mAudioServerStateListeners.containsKey(asd.asBinder())) { |
| 8076 | Slog.w(TAG, "Cannot re-register audio server state dispatcher"); |
| 8077 | return; |
| 8078 | } |
| 8079 | AsdProxy asdp = new AsdProxy(asd); |
| 8080 | try { |
| 8081 | asd.asBinder().linkToDeath(asdp, 0/*flags*/); |
| 8082 | } catch (RemoteException e) { |
| 8083 | |
| 8084 | } |
| 8085 | mAudioServerStateListeners.put(asd.asBinder(), asdp); |
| 8086 | } |
| 8087 | } |
| 8088 | |
| 8089 | public void unregisterAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 8090 | checkMonitorAudioServerStatePermission(); |
| 8091 | synchronized (mAudioServerStateListeners) { |
| 8092 | AsdProxy asdp = mAudioServerStateListeners.remove(asd.asBinder()); |
| 8093 | if (asdp == null) { |
| 8094 | Slog.w(TAG, "Trying to unregister unknown audioserver state dispatcher for pid " |
| 8095 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
| 8096 | return; |
| 8097 | } else { |
| 8098 | asd.asBinder().unlinkToDeath(asdp, 0/*flags*/); |
| 8099 | } |
| 8100 | } |
| 8101 | } |
| 8102 | |
| 8103 | public boolean isAudioServerRunning() { |
| 8104 | checkMonitorAudioServerStatePermission(); |
| 8105 | return (AudioSystem.checkAudioFlinger() == AudioSystem.AUDIO_STATUS_OK); |
| 8106 | } |
| 8107 | |
| 8108 | //====================== |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 8109 | // misc |
| 8110 | //====================== |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 8111 | private final HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 8112 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 8113 | @GuardedBy("mAudioPolicies") |
| 8114 | private int mAudioPolicyCounter = 0; |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 8115 | } |