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_SYSTEM; |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 24 | import static android.os.Process.FIRST_APPLICATION_UID; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 25 | import static android.provider.Settings.Secure.VOLUME_HUSH_MUTE; |
| 26 | import static android.provider.Settings.Secure.VOLUME_HUSH_OFF; |
| 27 | import static android.provider.Settings.Secure.VOLUME_HUSH_VIBRATE; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 28 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 29 | import android.Manifest; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 30 | import android.annotation.IntDef; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 31 | import android.annotation.NonNull; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 32 | import android.annotation.Nullable; |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 34 | import android.app.ActivityManagerInternal; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 35 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 36 | import android.app.AppOpsManager; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 37 | import android.app.IUidObserver; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 38 | import android.app.NotificationManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 39 | import android.bluetooth.BluetoothAdapter; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 40 | import android.bluetooth.BluetoothDevice; |
| 41 | import android.bluetooth.BluetoothHeadset; |
| 42 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 43 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 44 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.ContentResolver; |
| 46 | import android.content.Context; |
| 47 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 48 | import android.content.IntentFilter; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 49 | import android.content.pm.ApplicationInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.pm.PackageManager; |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 52 | import android.content.pm.ResolveInfo; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 53 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 54 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 55 | import android.content.res.Resources; |
| 56 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 57 | import android.database.ContentObserver; |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 58 | import android.hardware.hdmi.HdmiAudioSystemClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 59 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 60 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 61 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 62 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 63 | import android.media.AudioAttributes; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 64 | import android.media.AudioFocusInfo; |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 65 | import android.media.AudioFocusRequest; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 66 | import android.media.AudioFormat; |
| 67 | import android.media.AudioManager; |
| 68 | import android.media.AudioManagerInternal; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 69 | import android.media.AudioPlaybackConfiguration; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 70 | import android.media.AudioRecordingConfiguration; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 71 | import android.media.AudioRoutesInfo; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 72 | import android.media.AudioSystem; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 73 | import android.media.IAudioFocusDispatcher; |
| 74 | import android.media.IAudioRoutesObserver; |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 75 | import android.media.IAudioServerStateDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 76 | import android.media.IAudioService; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 77 | import android.media.IPlaybackConfigDispatcher; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 78 | import android.media.IRecordingConfigDispatcher; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 79 | import android.media.IRingtonePlayer; |
| 80 | import android.media.IVolumeController; |
| 81 | import android.media.MediaPlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.media.MediaPlayer.OnCompletionListener; |
| 83 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 84 | import android.media.PlayerBase; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 85 | import android.media.SoundPool; |
| 86 | import android.media.VolumePolicy; |
| 87 | import android.media.audiofx.AudioEffect; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 88 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 89 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 90 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 91 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 93 | import android.os.Build; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 94 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Environment; |
| 96 | import android.os.Handler; |
| 97 | import android.os.IBinder; |
| 98 | import android.os.Looper; |
| 99 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 100 | import android.os.PowerManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 102 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 103 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 104 | import android.os.UserHandle; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 105 | import android.os.UserManager; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 106 | import android.os.UserManagerInternal; |
| 107 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 108 | import android.os.VibrationEffect; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 109 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.provider.Settings; |
| 111 | import android.provider.Settings.System; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 112 | import android.service.notification.ZenModeConfig; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 113 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 114 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 115 | import android.util.AndroidRuntimeException; |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 116 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 118 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 119 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 120 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 121 | import android.view.KeyEvent; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 122 | import android.view.accessibility.AccessibilityManager; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 123 | import android.widget.Toast; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 125 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 126 | import com.android.internal.util.DumpUtils; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 127 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 128 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 129 | import com.android.server.LocalServices; |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 130 | import com.android.server.SystemService; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 131 | import com.android.server.audio.AudioServiceEvents.PhoneStateEvent; |
| 132 | import com.android.server.audio.AudioServiceEvents.VolumeEvent; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 133 | import com.android.server.pm.UserManagerService; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 134 | import com.android.server.wm.ActivityTaskManagerInternal; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 135 | |
| 136 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 138 | import java.io.File; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 139 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 141 | import java.io.PrintWriter; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 142 | import java.lang.annotation.Retention; |
| 143 | import java.lang.annotation.RetentionPolicy; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 144 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | import java.util.ArrayList; |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 146 | import java.util.Collection; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 147 | import java.util.HashMap; |
| 148 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 149 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 150 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 151 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * The implementation of the volume manager service. |
| 155 | * <p> |
| 156 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 157 | * asynchronous to external calls. For example, the task of setting a volume |
| 158 | * will update our internal state, but in a separate thread will set the system |
| 159 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 160 | * will update the state and broadcast a change and in a separate thread later |
| 161 | * persist the ringer mode. |
| 162 | * |
| 163 | * @hide |
| 164 | */ |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 165 | public class AudioService extends IAudioService.Stub |
| 166 | implements AccessibilityManager.TouchExplorationStateChangeListener, |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 167 | AccessibilityManager.AccessibilityServicesStateChangeListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 169 | private static final String TAG = "AS.AudioService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 171 | /** Debug audio mode */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 172 | protected static final boolean DEBUG_MODE = false; |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 173 | |
| 174 | /** Debug audio policy feature */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 175 | protected static final boolean DEBUG_AP = false; |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 176 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 177 | /** Debug volumes */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 178 | protected static final boolean DEBUG_VOL = false; |
Jean-Michel Trivi | 18e7bce | 2011-08-26 12:11:36 -0700 | [diff] [blame] | 179 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 180 | /** debug calls to devices APIs */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 181 | protected static final boolean DEBUG_DEVICES = false; |
| 182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 184 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 186 | /** How long to delay after a volume down event before unmuting a stream */ |
| 187 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 188 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 189 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 190 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 191 | */ |
| 192 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 193 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 194 | private final Context mContext; |
| 195 | private final ContentResolver mContentResolver; |
| 196 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 197 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 198 | // the platform type affects volume and silent mode behavior |
| 199 | private final int mPlatformType; |
| 200 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 201 | // indicates whether the system maps all streams to a single stream. |
| 202 | private final boolean mIsSingleVolume; |
| 203 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 204 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 205 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 208 | /*package*/ boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 209 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 210 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 211 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 212 | /*package*/ boolean isPlatformAutomotive() { |
Hongwei Wang | daba124 | 2018-05-29 14:36:16 -0700 | [diff] [blame] | 213 | return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); |
| 214 | } |
| 215 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 216 | /** The controller for the volume UI. */ |
| 217 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | |
| 219 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | /** If the msg is already queued, replace it with this one. */ |
| 221 | private static final int SENDMSG_REPLACE = 0; |
| 222 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 223 | private static final int SENDMSG_NOOP = 1; |
| 224 | /** If the msg is already queued, queue this one and leave the old. */ |
| 225 | private static final int SENDMSG_QUEUE = 2; |
| 226 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 227 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 228 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | private static final int MSG_PERSIST_VOLUME = 1; |
| 230 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 231 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 232 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 233 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 234 | private static final int MSG_SET_FORCE_USE = 8; |
| 235 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 236 | private static final int MSG_SET_ALL_VOLUMES = 10; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 237 | private static final int MSG_CHECK_MUSIC_ACTIVE = 11; |
| 238 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 12; |
| 239 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 13; |
| 240 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 14; |
| 241 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 15; |
| 242 | private static final int MSG_SYSTEM_READY = 16; |
| 243 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 17; |
| 244 | private static final int MSG_UNMUTE_STREAM = 18; |
| 245 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 19; |
| 246 | private static final int MSG_INDICATE_SYSTEM_READY = 20; |
| 247 | private static final int MSG_ACCESSORY_PLUG_MEDIA_UNMUTE = 21; |
| 248 | private static final int MSG_NOTIFY_VOL_EVENT = 22; |
| 249 | private static final int MSG_DISPATCH_AUDIO_SERVER_STATE = 23; |
| 250 | private static final int MSG_ENABLE_SURROUND_FORMATS = 24; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 251 | // start of messages handled under wakelock |
| 252 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 253 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 254 | private static final int MSG_DISABLE_AUDIO_FOR_UID = 100; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 255 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 256 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 257 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 258 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | /** @see AudioSystemThread */ |
| 261 | private AudioSystemThread mAudioSystemThread; |
| 262 | /** @see AudioHandler */ |
| 263 | private AudioHandler mAudioHandler; |
| 264 | /** @see VolumeStreamState */ |
| 265 | private VolumeStreamState[] mStreamStates; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 266 | |
| 267 | /*package*/ VolumeStreamState getStreamState(int stream) { |
| 268 | return mStreamStates[stream]; |
| 269 | } |
| 270 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 271 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 272 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 273 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 274 | // protects mRingerMode |
| 275 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 278 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
| 281 | /* Sound effect file names */ |
| 282 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 283 | 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] | 284 | |
| 285 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 286 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 287 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 288 | 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] | 289 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 290 | /** Maximum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 291 | protected static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 292 | 5, // STREAM_VOICE_CALL |
| 293 | 7, // STREAM_SYSTEM |
| 294 | 7, // STREAM_RING |
| 295 | 15, // STREAM_MUSIC |
| 296 | 7, // STREAM_ALARM |
| 297 | 7, // STREAM_NOTIFICATION |
| 298 | 15, // STREAM_BLUETOOTH_SCO |
| 299 | 7, // STREAM_SYSTEM_ENFORCED |
| 300 | 15, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 301 | 15, // STREAM_TTS |
| 302 | 15 // STREAM_ACCESSIBILITY |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 303 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 304 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 305 | /** Minimum volume index values for audio streams */ |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 306 | protected static int[] MIN_STREAM_VOLUME = new int[] { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 307 | 1, // STREAM_VOICE_CALL |
| 308 | 0, // STREAM_SYSTEM |
| 309 | 0, // STREAM_RING |
| 310 | 0, // STREAM_MUSIC |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 311 | 1, // STREAM_ALARM |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 312 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 313 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 314 | 0, // STREAM_SYSTEM_ENFORCED |
| 315 | 0, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 316 | 0, // STREAM_TTS |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 317 | 1 // STREAM_ACCESSIBILITY |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 318 | }; |
| 319 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 320 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 321 | * of another stream: This avoids multiplying the volume settings for hidden |
| 322 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 323 | * NOTE: do not create loops in aliases! |
| 324 | * 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] | 325 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 326 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 327 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 328 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 329 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 330 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 331 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 332 | AudioSystem.STREAM_RING, // STREAM_RING |
| 333 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 334 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 335 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 336 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 337 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 338 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 339 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 340 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 341 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 342 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 343 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 344 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 345 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 346 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 347 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 348 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 349 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 350 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 351 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 352 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 353 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 354 | }; |
| 355 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 356 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 357 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 358 | AudioSystem.STREAM_RING, // STREAM_RING |
| 359 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 360 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 361 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 362 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 363 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 364 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 365 | AudioSystem.STREAM_MUSIC, // STREAM_TTS |
| 366 | AudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 367 | }; |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 368 | protected static int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 369 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 370 | /** |
| 371 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 372 | * after mapping through mStreamVolumeAlias. |
| 373 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 374 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 375 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 376 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 377 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 378 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 379 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 380 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 381 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 382 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 383 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 384 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 385 | AppOpsManager.OP_AUDIO_ACCESSIBILITY_VOLUME, // STREAM_ACCESSIBILITY |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 388 | private final boolean mUseFixedVolume; |
| 389 | |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 390 | /** |
| 391 | * Default stream type used for volume control in the absence of playback |
| 392 | * e.g. user on homescreen, no app playing anything, presses hardware volume buttons, this |
| 393 | * stream type is controlled. |
| 394 | */ |
Eric Laurent | 0758420 | 2019-01-24 18:33:49 -0800 | [diff] [blame^] | 395 | protected static final int DEFAULT_VOL_STREAM_NO_PLAYBACK = AudioSystem.STREAM_MUSIC; |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 396 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 397 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | public void onError(int error) { |
| 399 | switch (error) { |
| 400 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | 0758420 | 2019-01-24 18:33:49 -0800 | [diff] [blame^] | 401 | mRecordMonitor.clear(); |
| 402 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 403 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 404 | SENDMSG_NOOP, 0, 0, null, 0); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 405 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 406 | SENDMSG_QUEUE, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | break; |
| 408 | default: |
| 409 | break; |
| 410 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 411 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | /** |
| 415 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 416 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 417 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 418 | */ |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 419 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 420 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 421 | @GuardedBy("mSettingsLock") |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 422 | 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] | 423 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 424 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 425 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 426 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 427 | private int mZenModeAffectedStreams = 0; |
| 428 | |
| 429 | // Streams currently muted by ringer mode and dnd |
| 430 | private int mRingerAndZenModeMutedStreams; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 431 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 432 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 433 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | private int mMuteAffectedStreams; |
| 435 | |
| 436 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 437 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 438 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 439 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | */ |
| 441 | private int mVibrateSetting; |
| 442 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 443 | // Is there a vibrator |
| 444 | private final boolean mHasVibrator; |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 445 | // Used to play vibrations |
| 446 | private Vibrator mVibrator; |
| 447 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 448 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 449 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 450 | .build(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 451 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 452 | // Broadcast receiver for device connections intent broadcasts |
| 453 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 454 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 455 | /** Interface for UserManagerService. */ |
| 456 | private final UserManagerInternal mUserManagerInternal; |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 457 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 458 | |
| 459 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 460 | new AudioServiceUserRestrictionsListener(); |
| 461 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 462 | // List of binder death handlers for setMode() client processes. |
| 463 | // The last process to have called setMode() is at the top of the list. |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 464 | // package-private so it can be accessed in AudioDeviceBroker.getSetModeDeathHandlers |
| 465 | //TODO candidate to be moved to separate class that handles synchronization |
| 466 | @GuardedBy("mDeviceBroker.mSetModeLock") |
| 467 | /*package*/ final ArrayList<SetModeDeathHandler> mSetModeDeathHandlers = |
| 468 | new ArrayList<SetModeDeathHandler>(); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 469 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 470 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 471 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 472 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 473 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 474 | // listener for SoundPool sample load completion indication |
| 475 | private SoundPoolCallback mSoundPoolCallBack; |
| 476 | // thread for SoundPool listener |
| 477 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 478 | // message looper for SoundPool listener |
| 479 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 480 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 481 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 482 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 483 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 484 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 485 | // is controlled by Vol keys. |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 486 | private int mVolumeControlStream = -1; |
| 487 | // interpretation of whether the volume stream has been selected by the user by clicking on a |
| 488 | // volume slider to change which volume is controlled by the volume keys. Is false |
| 489 | // when mVolumeControlStream is -1. |
| 490 | private boolean mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 491 | private final Object mForceControlStreamLock = new Object(); |
| 492 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 493 | // server process so in theory it is not necessary to monitor the client death. |
| 494 | // However it is good to be ready for future evolutions. |
| 495 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 496 | // Used to play ringtones outside system_server |
| 497 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 498 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 499 | // 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] | 500 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 501 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 502 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 503 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 504 | AudioSystem.DEVICE_OUT_SPDIF | |
| 505 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 506 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 507 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 508 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 509 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 510 | private boolean mDockAudioMediaEnabled = true; |
| 511 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 512 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 513 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 514 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 515 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 516 | // and used later when/if disableSafeMediaVolume() is called. |
| 517 | private StreamVolumeCommand mPendingVolumeCommand; |
| 518 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 519 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 520 | |
| 521 | private final MediaFocusControl mMediaFocusControl; |
| 522 | |
Tomoharu Kasahara | f0fecb1 | 2018-09-06 20:04:38 +0900 | [diff] [blame] | 523 | // Pre-scale for Bluetooth Absolute Volume |
| 524 | private float[] mPrescaleAbsoluteVolume = new float[] { |
| 525 | 0.5f, // Pre-scale for index 1 |
| 526 | 0.7f, // Pre-scale for index 2 |
| 527 | 0.85f, // Pre-scale for index 3 |
| 528 | }; |
| 529 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 530 | private NotificationManager mNm; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 531 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 532 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 533 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 534 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 535 | // Array of Uids of valid accessibility services to check if caller is one of them |
| 536 | private int[] mAccessibilityServiceUids; |
| 537 | private final Object mAccessibilityServiceUidsLock = new Object(); |
| 538 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 539 | private int mEncodedSurroundMode; |
| 540 | private String mEnabledSurroundFormats; |
| 541 | private boolean mSurroundModeChanged; |
| 542 | |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 543 | @GuardedBy("mSettingsLock") |
| 544 | private int mAssistantUid; |
| 545 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 546 | // Defines the format for the connection "address" for ALSA devices |
| 547 | public static String makeAlsaAddressString(int card, int device) { |
| 548 | return "card=" + card + ";device=" + device + ";"; |
| 549 | } |
| 550 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 551 | public static final class Lifecycle extends SystemService { |
| 552 | private AudioService mService; |
| 553 | |
| 554 | public Lifecycle(Context context) { |
| 555 | super(context); |
| 556 | mService = new AudioService(context); |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void onStart() { |
| 561 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public void onBootPhase(int phase) { |
| 566 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 567 | mService.systemReady(); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 572 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 573 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
| 574 | } |
| 575 | |
| 576 | @Override public void onUidGone(int uid, boolean disabled) { |
| 577 | // Once the uid is no longer running, no need to keep trying to disable its audio. |
| 578 | disableAudioForUid(false, uid); |
| 579 | } |
| 580 | |
| 581 | @Override public void onUidActive(int uid) throws RemoteException { |
| 582 | } |
| 583 | |
| 584 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 585 | } |
| 586 | |
| 587 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
| 588 | disableAudioForUid(cached, uid); |
| 589 | } |
| 590 | |
| 591 | private void disableAudioForUid(boolean disable, int uid) { |
| 592 | queueMsgUnderWakeLock(mAudioHandler, MSG_DISABLE_AUDIO_FOR_UID, |
| 593 | disable ? 1 : 0 /* arg1 */, uid /* arg2 */, |
| 594 | null /* obj */, 0 /* delay */); |
| 595 | } |
| 596 | }; |
| 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | /////////////////////////////////////////////////////////////////////////// |
| 599 | // Construction |
| 600 | /////////////////////////////////////////////////////////////////////////// |
| 601 | |
| 602 | /** @hide */ |
| 603 | public AudioService(Context context) { |
| 604 | mContext = context; |
| 605 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 606 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 607 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 608 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 609 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 610 | mIsSingleVolume = AudioSystem.isSingleVolume(context); |
| 611 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 612 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 613 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 614 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 615 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 616 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 617 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 618 | mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 619 | mHasVibrator = mVibrator == null ? false : mVibrator.hasVibrator(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 620 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 621 | // Initialize volume |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 622 | int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1); |
| 623 | if (maxCallVolume != -1) { |
| 624 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume; |
Shuhei Miyazaki | 89091ba | 2018-01-11 18:17:04 +0900 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | int defaultCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_default", -1); |
| 628 | if (defaultCallVolume != -1 && |
| 629 | defaultCallVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] && |
| 630 | defaultCallVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 631 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = defaultCallVolume; |
| 632 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 633 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = |
| 634 | (maxCallVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 635 | } |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 636 | |
| 637 | int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1); |
| 638 | if (maxMusicVolume != -1) { |
| 639 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume; |
| 640 | } |
| 641 | |
| 642 | int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1); |
| 643 | if (defaultMusicVolume != -1 && |
Shuhei Miyazaki | 89091ba | 2018-01-11 18:17:04 +0900 | [diff] [blame] | 644 | defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] && |
| 645 | defaultMusicVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 646 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume; |
| 647 | } else { |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 648 | if (isPlatformTelevision()) { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 649 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 650 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 651 | } else { |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 652 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = |
| 653 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3; |
Hank Freund | 45926dc | 2015-12-11 10:50:45 -0800 | [diff] [blame] | 654 | } |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 655 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 656 | |
Chris Kuiper | 09b6c21 | 2018-01-03 22:22:13 -0800 | [diff] [blame] | 657 | int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1); |
| 658 | if (maxAlarmVolume != -1) { |
| 659 | MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume; |
| 660 | } |
| 661 | |
| 662 | int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1); |
| 663 | if (defaultAlarmVolume != -1 && |
| 664 | defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) { |
| 665 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume; |
| 666 | } else { |
| 667 | // Default is 6 out of 7 (default maximum), so scale accordingly. |
| 668 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = |
| 669 | 6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7; |
| 670 | } |
| 671 | |
| 672 | int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1); |
| 673 | if (maxSystemVolume != -1) { |
| 674 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume; |
| 675 | } |
| 676 | |
| 677 | int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1); |
| 678 | if (defaultSystemVolume != -1 && |
| 679 | defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) { |
| 680 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume; |
| 681 | } else { |
| 682 | // Default is to use maximum. |
| 683 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = |
| 684 | MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]; |
| 685 | } |
| 686 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 687 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 688 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 691 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 692 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 693 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 694 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 695 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 696 | sendMsg(mAudioHandler, |
| 697 | MSG_SET_FORCE_USE, |
| 698 | SENDMSG_QUEUE, |
| 699 | AudioSystem.FOR_SYSTEM, |
| 700 | cameraSoundForced ? |
| 701 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 702 | new String("AudioService ctor"), |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 703 | 0); |
| 704 | |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 705 | mSafeMediaVolumeState = Settings.Global.getInt(mContentResolver, |
| 706 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 707 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 708 | // The default safe volume index read here will be replaced by the actual value when |
| 709 | // the mcc is read by onConfigureSafeVolume() |
| 710 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 711 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 712 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 713 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 714 | com.android.internal.R.bool.config_useFixedVolume); |
| 715 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 716 | mDeviceBroker = new AudioDeviceBroker(mContext, this); |
| 717 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 718 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 719 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 720 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 722 | readUserRestrictions(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 723 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 724 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 725 | |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 726 | // mSafeUsbMediaVolumeIndex must be initialized after createStreamStates() because it |
| 727 | // relies on audio policy having correct ranges for volume indexes. |
| 728 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 729 | |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 730 | mPlaybackMonitor = |
| 731 | new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]); |
| 732 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 733 | mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 734 | |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 735 | mRecordMonitor = new RecordingActivityMonitor(mContext); |
| 736 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 737 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 738 | |
| 739 | // Call setRingerModeInt() to apply correct mute |
| 740 | // state on streams affected by ringer mode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 741 | mRingerAndZenModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 742 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 743 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 744 | // Register for device connection intent broadcasts. |
| 745 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 746 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jack He | 8dd3394 | 2018-01-17 15:45:12 -0800 | [diff] [blame] | 747 | intentFilter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 748 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 749 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 750 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 751 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 752 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 753 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 754 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 755 | intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 756 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 757 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 758 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 759 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 760 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 761 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 762 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 763 | intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); |
| 764 | intentFilter.addAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); |
| 765 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 766 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 767 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 768 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 769 | |
| 770 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 771 | |
| 772 | mRecordMonitor.initMonitor(); |
Tomoharu Kasahara | f0fecb1 | 2018-09-06 20:04:38 +0900 | [diff] [blame] | 773 | |
| 774 | final float[] preScale = new float[3]; |
| 775 | preScale[0] = mContext.getResources().getFraction( |
| 776 | com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index1, |
| 777 | 1, 1); |
| 778 | preScale[1] = mContext.getResources().getFraction( |
| 779 | com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index2, |
| 780 | 1, 1); |
| 781 | preScale[2] = mContext.getResources().getFraction( |
| 782 | com.android.internal.R.fraction.config_prescaleAbsoluteVolume_index3, |
| 783 | 1, 1); |
| 784 | for (int i = 0; i < preScale.length; i++) { |
| 785 | if (0.0f <= preScale[i] && preScale[i] <= 1.0f) { |
| 786 | mPrescaleAbsoluteVolume[i] = preScale[i]; |
| 787 | } |
| 788 | } |
| 789 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 792 | public void systemReady() { |
| 793 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 794 | 0, 0, null, 0); |
Dianne Hackborn | 3e3600e | 2017-08-31 11:15:26 -0700 | [diff] [blame] | 795 | if (false) { |
| 796 | // This is turned off for now, because it is racy and thus causes apps to break. |
| 797 | // Currently banning a uid means that if an app tries to start playing an audio |
| 798 | // stream, that will be preventing, and unbanning it will not allow that stream |
| 799 | // to resume. However these changes in uid state are racy with what the app is doing, |
| 800 | // so that after taking a process out of the cached state we can't guarantee that |
| 801 | // we will unban the uid before the app actually tries to start playing audio. |
| 802 | // (To do that, the activity manager would need to wait until it knows for sure |
| 803 | // that the ban has been removed, before telling the app to do whatever it is |
| 804 | // supposed to do that caused it to go out of the cached state.) |
| 805 | try { |
| 806 | ActivityManager.getService().registerUidObserver(mUidObserver, |
| 807 | ActivityManager.UID_OBSERVER_CACHED | ActivityManager.UID_OBSERVER_GONE, |
| 808 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
| 809 | } catch (RemoteException e) { |
| 810 | // ignored; both services live in system_server |
| 811 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 812 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | public void onSystemReady() { |
| 816 | mSystemReady = true; |
| 817 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 818 | 0, 0, null, 0); |
| 819 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 820 | mDeviceBroker.onSystemReady(); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 821 | |
Jeff Sharkey | 73ea0ae | 2016-08-10 17:30:38 -0600 | [diff] [blame] | 822 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) { |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 823 | synchronized (mHdmiClientLock) { |
| 824 | mHdmiManager = mContext.getSystemService(HdmiControlManager.class); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 825 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 826 | if (mHdmiTvClient != null) { |
| 827 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 828 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 829 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 830 | mHdmiCecSink = false; |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 831 | mHdmiAudioSystemClient = mHdmiManager.getAudioSystemClient(); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 834 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 835 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 836 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 837 | sendMsg(mAudioHandler, |
| 838 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 839 | SENDMSG_REPLACE, |
| 840 | 0, |
| 841 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 842 | TAG, |
Eric Laurent | 03332ab | 2017-02-09 18:29:15 -0800 | [diff] [blame] | 843 | SystemProperties.getBoolean("audio.safemedia.bypass", false) ? |
| 844 | 0 : SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 845 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 846 | initA11yMonitoring(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 847 | onIndicateSystemReady(); |
| 848 | } |
| 849 | |
| 850 | void onIndicateSystemReady() { |
| 851 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 852 | return; |
| 853 | } |
| 854 | sendMsg(mAudioHandler, |
| 855 | MSG_INDICATE_SYSTEM_READY, |
| 856 | SENDMSG_REPLACE, |
| 857 | 0, |
| 858 | 0, |
| 859 | null, |
| 860 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 861 | } |
| 862 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 863 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 864 | if (!mSystemReady || |
| 865 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 866 | Log.e(TAG, "Audioserver died."); |
| 867 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 868 | null, 500); |
| 869 | return; |
| 870 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 871 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 872 | |
| 873 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 874 | // server crash |
| 875 | // Note that we only execute this when the media server |
| 876 | // process restarts after a crash, not the first time it is started. |
| 877 | AudioSystem.setParameters("restarting=true"); |
| 878 | |
| 879 | readAndSetLowRamDevice(); |
| 880 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 881 | // Restore device connection states, BT state |
| 882 | mDeviceBroker.onAudioServerDied(); |
| 883 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 884 | // Restore call state |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 885 | if (AudioSystem.setPhoneState(mMode) == AudioSystem.AUDIO_STATUS_OK) { |
| 886 | mModeLogger.log(new AudioEventLogger.StringEvent( |
| 887 | "onAudioServerDied causes setPhoneState(" + AudioSystem.modeToString(mMode) + ")")); |
| 888 | } |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 889 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 890 | final int forSys; |
| 891 | synchronized (mSettingsLock) { |
| 892 | forSys = mCameraSoundForced ? |
| 893 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE; |
| 894 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 895 | |
| 896 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_SYSTEM, forSys, "onAudioServerDied"); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 897 | |
| 898 | // Restore stream volumes |
| 899 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 900 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 901 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 902 | AudioSystem.initStreamVolume( |
| 903 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 904 | |
| 905 | streamState.applyAllVolumes(); |
| 906 | } |
| 907 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 908 | // Restore mono mode |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 909 | updateMasterMono(mContentResolver); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 910 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 911 | // Restore ringer mode |
| 912 | setRingerModeInt(getRingerModeInternal(), false); |
| 913 | |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 914 | // Reset device rotation (if monitored for this device) |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 915 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 916 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 919 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 920 | final int forDock = mDockAudioMediaEnabled ? |
| 921 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 922 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 923 | sendEncodedSurroundMode(mContentResolver, "onAudioServerDied"); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 924 | sendEnabledSurroundFormats(mContentResolver, true); |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 925 | updateAssistantUId(true); |
| 926 | } |
| 927 | synchronized (mAccessibilityServiceUidsLock) { |
| 928 | AudioSystem.setA11yServicesUids(mAccessibilityServiceUids); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 929 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 930 | synchronized (mHdmiClientLock) { |
| 931 | if (mHdmiManager != null && mHdmiTvClient != null) { |
| 932 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
| 936 | synchronized (mAudioPolicies) { |
| 937 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 938 | policy.connectMixes(); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | onIndicateSystemReady(); |
| 943 | // indicate the end of reconfiguration phase to audio HAL |
| 944 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 945 | |
| 946 | sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE, |
| 947 | SENDMSG_QUEUE, 1, 0, null, 0); |
| 948 | } |
| 949 | |
| 950 | private void onDispatchAudioServerStateChange(boolean state) { |
| 951 | synchronized (mAudioServerStateListeners) { |
| 952 | for (AsdProxy asdp : mAudioServerStateListeners.values()) { |
| 953 | try { |
| 954 | asdp.callback().dispatchAudioServerStateChange(state); |
| 955 | } catch (RemoteException e) { |
| 956 | Log.w(TAG, "Could not call dispatchAudioServerStateChange()", e); |
| 957 | } |
| 958 | } |
| 959 | } |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 960 | } |
| 961 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | private void createAudioSystemThread() { |
| 963 | mAudioSystemThread = new AudioSystemThread(); |
| 964 | mAudioSystemThread.start(); |
| 965 | waitForAudioHandlerCreation(); |
| 966 | } |
| 967 | |
| 968 | /** Waits for the volume handler to be created by the other thread. */ |
| 969 | private void waitForAudioHandlerCreation() { |
| 970 | synchronized(this) { |
| 971 | while (mAudioHandler == null) { |
| 972 | try { |
| 973 | // Wait for mAudioHandler to be set by the other thread |
| 974 | wait(); |
| 975 | } catch (InterruptedException e) { |
| 976 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 982 | private void checkAllAliasStreamVolumes() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 983 | synchronized (mSettingsLock) { |
| 984 | synchronized (VolumeStreamState.class) { |
| 985 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 986 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 987 | mStreamStates[streamType] |
| 988 | .setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], TAG); |
| 989 | // apply stream volume |
| 990 | if (!mStreamStates[streamType].mIsMuted) { |
| 991 | mStreamStates[streamType].applyAllVolumes(); |
| 992 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 993 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 998 | /** |
| 999 | * Called from AudioDeviceBroker when DEVICE_OUT_HDMI is connected or disconnected. |
| 1000 | */ |
| 1001 | /*package*/ void checkVolumeCecOnHdmiConnection(int state, String caller) { |
| 1002 | if (state != 0) { |
| 1003 | // DEVICE_OUT_HDMI is now connected |
| 1004 | if ((AudioSystem.DEVICE_OUT_HDMI & mSafeMediaVolumeDevices) != 0) { |
| 1005 | sendMsg(mAudioHandler, |
| 1006 | MSG_CHECK_MUSIC_ACTIVE, |
| 1007 | SENDMSG_REPLACE, |
| 1008 | 0, |
| 1009 | 0, |
| 1010 | caller, |
| 1011 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 1012 | } |
| 1013 | |
| 1014 | if (isPlatformTelevision()) { |
| 1015 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 1016 | checkAllFixedVolumeDevices(); |
| 1017 | synchronized (mHdmiClientLock) { |
| 1018 | if (mHdmiManager != null && mHdmiPlaybackClient != null) { |
| 1019 | mHdmiCecSink = false; |
| 1020 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | sendEnabledSurroundFormats(mContentResolver, true); |
| 1025 | } else { |
| 1026 | // DEVICE_OUT_HDMI disconnected |
| 1027 | if (isPlatformTelevision()) { |
| 1028 | synchronized (mHdmiClientLock) { |
| 1029 | if (mHdmiManager != null) { |
| 1030 | mHdmiCecSink = false; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1037 | private void checkAllFixedVolumeDevices() |
| 1038 | { |
| 1039 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1040 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 1041 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1045 | private void checkAllFixedVolumeDevices(int streamType) { |
| 1046 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 1047 | } |
| 1048 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1049 | private void checkMuteAffectedStreams() { |
| 1050 | // any stream with a min level > 0 is not muteable by definition |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1051 | // STREAM_VOICE_CALL and STREAM_BLUETOOTH_SCO can be muted by applications |
| 1052 | // that has the the MODIFY_PHONE_STATE permission. |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1053 | for (int i = 0; i < mStreamStates.length; i++) { |
| 1054 | final VolumeStreamState vss = mStreamStates[i]; |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1055 | if (vss.mIndexMin > 0 && |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1056 | (vss.mStreamType != AudioSystem.STREAM_VOICE_CALL && |
| 1057 | vss.mStreamType != AudioSystem.STREAM_BLUETOOTH_SCO)) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1058 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1065 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 1066 | |
| 1067 | for (int i = 0; i < numStreamTypes; i++) { |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 1068 | streams[i] = |
| 1069 | new VolumeStreamState(System.VOLUME_SETTINGS_INT[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1070 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1072 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1073 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1074 | checkMuteAffectedStreams(); |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1075 | updateDefaultVolumes(); |
| 1076 | } |
| 1077 | |
| 1078 | // Update default indexes from aliased streams. Must be called after mStreamStates is created |
| 1079 | private void updateDefaultVolumes() { |
| 1080 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1081 | if (stream != mStreamVolumeAlias[stream]) { |
| 1082 | AudioSystem.DEFAULT_STREAM_VOLUME[stream] = rescaleIndex( |
| 1083 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]], |
| 1084 | mStreamVolumeAlias[stream], |
| 1085 | stream); |
| 1086 | } |
| 1087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1090 | private void dumpStreamStates(PrintWriter pw) { |
| 1091 | pw.println("\nStream volumes (device: index)"); |
| 1092 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 1093 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1094 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1095 | mStreamStates[i].dump(pw); |
| 1096 | pw.println(""); |
| 1097 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1098 | pw.print("\n- mute affected streams = 0x"); |
| 1099 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1102 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1103 | int dtmfStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1104 | final int a11yStreamAlias = sIndependentA11yVolume ? |
| 1105 | AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1106 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1107 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1108 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 1109 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1110 | } else { |
| 1111 | switch (mPlatformType) { |
| 1112 | case AudioSystem.PLATFORM_VOICE: |
| 1113 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
| 1114 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
| 1115 | break; |
| 1116 | default: |
| 1117 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
| 1118 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 1119 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1120 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1121 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1122 | if (mIsSingleVolume) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1123 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1124 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1125 | if (isInCommunication()) { |
| 1126 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 1127 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 1128 | } else { |
| 1129 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 1130 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1131 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1132 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1133 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1134 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY] = a11yStreamAlias; |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1135 | |
Eric Laurent | 403bd34 | 2017-07-11 16:21:44 -0700 | [diff] [blame] | 1136 | if (updateVolumes && mStreamStates != null) { |
| 1137 | updateDefaultVolumes(); |
| 1138 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1139 | synchronized (mSettingsLock) { |
| 1140 | synchronized (VolumeStreamState.class) { |
| 1141 | mStreamStates[AudioSystem.STREAM_DTMF] |
| 1142 | .setAllIndexes(mStreamStates[dtmfStreamAlias], caller); |
| 1143 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].mVolumeIndexSettingName = |
| 1144 | System.VOLUME_SETTINGS_INT[a11yStreamAlias]; |
| 1145 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].setAllIndexes( |
| 1146 | mStreamStates[a11yStreamAlias], caller); |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1147 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].refreshRange( |
| 1148 | mStreamVolumeAlias[AudioSystem.STREAM_ACCESSIBILITY]); |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 1149 | } |
| 1150 | } |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 1151 | if (sIndependentA11yVolume) { |
| 1152 | // restore the a11y values from the settings |
| 1153 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY].readSettings(); |
| 1154 | } |
| 1155 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1156 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1157 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1158 | sendMsg(mAudioHandler, |
| 1159 | MSG_SET_ALL_VOLUMES, |
| 1160 | SENDMSG_QUEUE, |
| 1161 | 0, |
| 1162 | 0, |
| 1163 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1164 | sendMsg(mAudioHandler, |
| 1165 | MSG_SET_ALL_VOLUMES, |
| 1166 | SENDMSG_QUEUE, |
| 1167 | 0, |
| 1168 | 0, |
| 1169 | mStreamStates[AudioSystem.STREAM_ACCESSIBILITY], 0); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1173 | private void readDockAudioSettings(ContentResolver cr) |
| 1174 | { |
| 1175 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1176 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1177 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1178 | sendMsg(mAudioHandler, |
| 1179 | MSG_SET_FORCE_USE, |
| 1180 | SENDMSG_QUEUE, |
| 1181 | AudioSystem.FOR_DOCK, |
| 1182 | mDockAudioMediaEnabled ? |
| 1183 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1184 | new String("readDockAudioSettings"), |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1185 | 0); |
| 1186 | } |
| 1187 | |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1188 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1189 | private void updateMasterMono(ContentResolver cr) |
| 1190 | { |
| 1191 | final boolean masterMono = System.getIntForUser( |
| 1192 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1193 | if (DEBUG_VOL) { |
| 1194 | Log.d(TAG, String.format("Master mono %b", masterMono)); |
| 1195 | } |
| 1196 | AudioSystem.setMasterMono(masterMono); |
| 1197 | } |
| 1198 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1199 | private void sendEncodedSurroundMode(ContentResolver cr, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1200 | { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1201 | final int encodedSurroundMode = Settings.Global.getInt( |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1202 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 1203 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1204 | sendEncodedSurroundMode(encodedSurroundMode, eventSource); |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1207 | private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource) |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1208 | { |
| 1209 | // initialize to guaranteed bad value |
| 1210 | int forceSetting = AudioSystem.NUM_FORCE_CONFIG; |
| 1211 | switch (encodedSurroundMode) { |
| 1212 | case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO: |
| 1213 | forceSetting = AudioSystem.FORCE_NONE; |
| 1214 | break; |
| 1215 | case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER: |
| 1216 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_NEVER; |
| 1217 | break; |
| 1218 | case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS: |
| 1219 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_ALWAYS; |
| 1220 | break; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1221 | case Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL: |
| 1222 | forceSetting = AudioSystem.FORCE_ENCODED_SURROUND_MANUAL; |
| 1223 | break; |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1224 | default: |
| 1225 | Log.e(TAG, "updateSurroundSoundSettings: illegal value " |
| 1226 | + encodedSurroundMode); |
| 1227 | break; |
| 1228 | } |
| 1229 | if (forceSetting != AudioSystem.NUM_FORCE_CONFIG) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1230 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_ENCODED_SURROUND, forceSetting, |
| 1231 | eventSource); |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1235 | private void sendEnabledSurroundFormats(ContentResolver cr, boolean forceUpdate) { |
| 1236 | if (mEncodedSurroundMode != Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL) { |
| 1237 | // Manually enable surround formats only when the setting is in manual mode. |
| 1238 | return; |
| 1239 | } |
| 1240 | String enabledSurroundFormats = Settings.Global.getString( |
| 1241 | cr, Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS); |
jiabin | 6db5f86 | 2018-05-17 10:33:09 -0700 | [diff] [blame] | 1242 | if (enabledSurroundFormats == null) { |
| 1243 | // Never allow enabledSurroundFormats as a null, which could happen when |
| 1244 | // ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS is not appear in settings DB. |
| 1245 | enabledSurroundFormats = ""; |
| 1246 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1247 | if (!forceUpdate && TextUtils.equals(enabledSurroundFormats, mEnabledSurroundFormats)) { |
| 1248 | // Update enabled surround formats to AudioPolicyManager only when forceUpdate |
| 1249 | // is true or enabled surround formats changed. |
| 1250 | return; |
| 1251 | } |
| 1252 | |
| 1253 | mEnabledSurroundFormats = enabledSurroundFormats; |
| 1254 | String[] surroundFormats = TextUtils.split(enabledSurroundFormats, ","); |
| 1255 | ArrayList<Integer> formats = new ArrayList<>(); |
| 1256 | for (String format : surroundFormats) { |
| 1257 | try { |
| 1258 | int audioFormat = Integer.valueOf(format); |
| 1259 | boolean isSurroundFormat = false; |
| 1260 | for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) { |
| 1261 | if (sf == audioFormat) { |
| 1262 | isSurroundFormat = true; |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | if (isSurroundFormat && !formats.contains(audioFormat)) { |
| 1267 | formats.add(audioFormat); |
| 1268 | } |
| 1269 | } catch (Exception e) { |
| 1270 | Log.e(TAG, "Invalid enabled surround format:" + format); |
| 1271 | } |
| 1272 | } |
| 1273 | // Set filtered surround formats to settings DB in case |
| 1274 | // there are invalid surround formats in original settings. |
| 1275 | Settings.Global.putString(mContext.getContentResolver(), |
| 1276 | Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS, |
| 1277 | TextUtils.join(",", formats)); |
| 1278 | sendMsg(mAudioHandler, MSG_ENABLE_SURROUND_FORMATS, SENDMSG_QUEUE, 0, 0, formats, 0); |
| 1279 | } |
| 1280 | |
| 1281 | private void onEnableSurroundFormats(ArrayList<Integer> enabledSurroundFormats) { |
| 1282 | // Set surround format enabled accordingly. |
| 1283 | for (int surroundFormat : AudioFormat.SURROUND_SOUND_ENCODING) { |
| 1284 | boolean enabled = enabledSurroundFormats.contains(surroundFormat); |
| 1285 | int ret = AudioSystem.setSurroundFormatEnabled(surroundFormat, enabled); |
| 1286 | Log.i(TAG, "enable surround format:" + surroundFormat + " " + enabled + " " + ret); |
| 1287 | } |
| 1288 | } |
| 1289 | |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 1290 | @GuardedBy("mSettingsLock") |
| 1291 | private void updateAssistantUId(boolean forceUpdate) { |
| 1292 | int assistantUid = 0; |
| 1293 | |
| 1294 | // Consider assistants in the following order of priority: |
| 1295 | // 1) voice interaction service |
| 1296 | // 2) assistant |
| 1297 | String assistantName = Settings.Secure.getStringForUser( |
| 1298 | mContentResolver, |
| 1299 | Settings.Secure.VOICE_INTERACTION_SERVICE, UserHandle.USER_CURRENT); |
| 1300 | if (TextUtils.isEmpty(assistantName)) { |
| 1301 | assistantName = Settings.Secure.getStringForUser( |
| 1302 | mContentResolver, |
| 1303 | Settings.Secure.ASSISTANT, UserHandle.USER_CURRENT); |
| 1304 | } |
| 1305 | if (!TextUtils.isEmpty(assistantName)) { |
| 1306 | String packageName = ComponentName.unflattenFromString(assistantName).getPackageName(); |
| 1307 | if (!TextUtils.isEmpty(packageName)) { |
| 1308 | try { |
| 1309 | assistantUid = mContext.getPackageManager().getPackageUid(packageName, 0); |
| 1310 | } catch (PackageManager.NameNotFoundException e) { |
| 1311 | Log.e(TAG, |
| 1312 | "updateAssistantUId() could not find UID for package: " + packageName); |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | if (assistantUid != mAssistantUid || forceUpdate) { |
| 1318 | AudioSystem.setAssistantUid(assistantUid); |
| 1319 | mAssistantUid = assistantUid; |
| 1320 | } |
| 1321 | } |
| 1322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | private void readPersistedSettings() { |
| 1324 | final ContentResolver cr = mContentResolver; |
| 1325 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1326 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1327 | Settings.Global.getInt( |
| 1328 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1329 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1330 | // sanity check in case the settings are restored from a device with incompatible |
| 1331 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1332 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1333 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1334 | } |
| 1335 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1336 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1337 | } |
| 1338 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1339 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1340 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1341 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1342 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1343 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1344 | synchronized(mSettingsLock) { |
| 1345 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1346 | if (mRingerModeExternal == -1) { |
| 1347 | mRingerModeExternal = mRingerMode; |
| 1348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1350 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1351 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1352 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1353 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1354 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1355 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1356 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1357 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1358 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1359 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1360 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1362 | updateRingerAndZenModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1363 | readDockAudioSettings(cr); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 1364 | sendEncodedSurroundMode(cr, "readPersistedSettings"); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 1365 | sendEnabledSurroundFormats(cr, true); |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 1366 | updateAssistantUId(true); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1367 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1368 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1369 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1370 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1371 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 1373 | updateMasterMono(cr); |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | // Each stream will read its own persisted settings |
| 1376 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1377 | // Broadcast the sticky intents |
| 1378 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1379 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | |
| 1381 | // Broadcast vibrate settings |
| 1382 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1383 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1384 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1385 | // Load settings for the volume controller |
| 1386 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1389 | private void readUserRestrictions() { |
| 1390 | final int currentUser = getCurrentUserId(); |
| 1391 | |
| 1392 | // Check the current user restriction. |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1393 | boolean masterMute = |
| 1394 | mUserManagerInternal.getUserRestriction(currentUser, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 1395 | UserManager.DISALLOW_UNMUTE_DEVICE) |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 1396 | || mUserManagerInternal.getUserRestriction(currentUser, |
| 1397 | UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 1398 | if (mUseFixedVolume) { |
| 1399 | masterMute = false; |
| 1400 | AudioSystem.setMasterVolume(1.0f); |
| 1401 | } |
| 1402 | if (DEBUG_VOL) { |
| 1403 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1404 | } |
| 1405 | setSystemAudioMute(masterMute); |
| 1406 | AudioSystem.setMasterMute(masterMute); |
| 1407 | broadcastMasterMuteStatus(masterMute); |
| 1408 | |
| 1409 | boolean microphoneMute = mUserManagerInternal.getUserRestriction( |
| 1410 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1411 | if (DEBUG_VOL) { |
| 1412 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1413 | } |
| 1414 | AudioSystem.muteMicrophone(microphoneMute); |
| 1415 | } |
| 1416 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1417 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 1418 | final int rescaled = |
| 1419 | (index * mStreamStates[dstStream].getMaxIndex() |
| 1420 | + mStreamStates[srcStream].getMaxIndex() / 2) |
| 1421 | / mStreamStates[srcStream].getMaxIndex(); |
| 1422 | if (rescaled < mStreamStates[dstStream].getMinIndex()) { |
| 1423 | return mStreamStates[dstStream].getMinIndex(); |
| 1424 | } else { |
| 1425 | return rescaled; |
| 1426 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | |
| 1429 | /////////////////////////////////////////////////////////////////////////// |
| 1430 | // IPC methods |
| 1431 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1433 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1434 | String callingPackage, String caller) { |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1435 | final IAudioPolicyCallback extVolCtlr; |
| 1436 | synchronized (mExtVolumeControllerLock) { |
| 1437 | extVolCtlr = mExtVolumeController; |
| 1438 | } |
| 1439 | if (extVolCtlr != null) { |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 1440 | sendMsg(mAudioHandler, MSG_NOTIFY_VOL_EVENT, SENDMSG_QUEUE, |
| 1441 | direction, 0 /*ignored*/, |
| 1442 | extVolCtlr, 0 /*delay*/); |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 1443 | } else { |
| 1444 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 1445 | caller, Binder.getCallingUid()); |
| 1446 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1450 | String callingPackage, String caller, int uid) { |
| 1451 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1452 | + ", flags=" + flags + ", caller=" + caller |
| 1453 | + ", volControlStream=" + mVolumeControlStream |
| 1454 | + ", userSelect=" + mUserSelectedVolumeControlStream); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1455 | sVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_SUGG_VOL, suggestedStreamType, |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1456 | direction/*val1*/, flags/*val2*/, new StringBuilder(callingPackage) |
| 1457 | .append("/").append(caller).append(" uid:").append(uid).toString())); |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1458 | final int streamType; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1459 | synchronized (mForceControlStreamLock) { |
| 1460 | // Request lock in case mVolumeControlStream is changed by other thread. |
| 1461 | if (mUserSelectedVolumeControlStream) { // implies mVolumeControlStream != -1 |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1462 | streamType = mVolumeControlStream; |
jiabin | 8fd5d5e | 2018-03-23 14:09:28 -0700 | [diff] [blame] | 1463 | } else { |
| 1464 | final int maybeActiveStreamType = getActiveStreamType(suggestedStreamType); |
| 1465 | final boolean activeForReal; |
| 1466 | if (maybeActiveStreamType == AudioSystem.STREAM_RING |
| 1467 | || maybeActiveStreamType == AudioSystem.STREAM_NOTIFICATION) { |
| 1468 | activeForReal = wasStreamActiveRecently(maybeActiveStreamType, 0); |
| 1469 | } else { |
| 1470 | activeForReal = AudioSystem.isStreamActive(maybeActiveStreamType, 0); |
| 1471 | } |
| 1472 | if (activeForReal || mVolumeControlStream == -1) { |
| 1473 | streamType = maybeActiveStreamType; |
| 1474 | } else { |
| 1475 | streamType = mVolumeControlStream; |
| 1476 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1477 | } |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1478 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 1479 | |
| 1480 | final boolean isMute = isMuteAdjust(direction); |
| 1481 | |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1482 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1483 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1485 | // Play sounds on STREAM_RING only. |
| 1486 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1487 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1489 | } |
| 1490 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1491 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1492 | // Don't suppress mute/unmute requests |
Stanley Chang | b77e13d | 2018-08-28 08:16:03 +0800 | [diff] [blame] | 1493 | // Don't suppress adjustments for single volume device |
| 1494 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute) |
| 1495 | && !mIsSingleVolume) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1496 | direction = 0; |
| 1497 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1498 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1499 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1500 | } |
| 1501 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1502 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1506 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1507 | String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1508 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1509 | Log.w(TAG, "Trying to call adjustStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1510 | + "CHANGE_ACCESSIBILITY_VOLUME / callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1511 | return; |
| 1512 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1513 | sVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_ADJUST_STREAM_VOL, streamType, |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1514 | direction/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1515 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1516 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Chinyue Chen | 6affe93 | 2018-01-24 14:51:43 +0800 | [diff] [blame] | 1519 | protected void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1520 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1521 | if (mUseFixedVolume) { |
| 1522 | return; |
| 1523 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1524 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1525 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | ensureValidDirection(direction); |
| 1528 | ensureValidStreamType(streamType); |
| 1529 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1530 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1531 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1532 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1533 | return; |
| 1534 | } |
| 1535 | |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1536 | // If adjust is mute and the stream is STREAM_VOICE_CALL or STREAM_BLUETOOTH_SCO, make sure |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1537 | // that the calling app have the MODIFY_PHONE_STATE permission. |
| 1538 | if (isMuteAdjust && |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1539 | (streamType == AudioSystem.STREAM_VOICE_CALL || |
| 1540 | streamType == AudioSystem.STREAM_BLUETOOTH_SCO) && |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1541 | mContext.checkCallingOrSelfPermission( |
| 1542 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1543 | != PackageManager.PERMISSION_GRANTED) { |
| 1544 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: adjustStreamVolume from pid=" |
| 1545 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1546 | return; |
| 1547 | } |
| 1548 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1549 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1550 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1551 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1552 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1553 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1554 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1555 | |
| 1556 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1557 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1558 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1559 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1560 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1561 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1562 | // skip a2dp absolute volume control request when the device |
| 1563 | // is not an a2dp device |
| 1564 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1565 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1566 | return; |
| 1567 | } |
| 1568 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1569 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1570 | // so we handle user restrictions correctly. |
| 1571 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1572 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1573 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1574 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1575 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1576 | return; |
| 1577 | } |
| 1578 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1579 | // reset any pending volume command |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 1580 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1581 | mPendingVolumeCommand = null; |
| 1582 | } |
| 1583 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1584 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1585 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1586 | ((device & mFixedVolumeDevices) != 0)) { |
| 1587 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1588 | |
| 1589 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1590 | // volume is enforced, and max and 0 for the others. |
| 1591 | // This is simulated by stepping by the full allowed volume range |
| 1592 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1593 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1594 | step = safeMediaVolumeIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1595 | } else { |
| 1596 | step = streamState.getMaxIndex(); |
| 1597 | } |
| 1598 | if (aliasIndex != 0) { |
| 1599 | aliasIndex = step; |
| 1600 | } |
| 1601 | } else { |
| 1602 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1603 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1604 | } |
| 1605 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1606 | // If either the client forces allowing ringer modes for this adjustment, |
| 1607 | // or the stream type is one that is affected by ringer modes |
| 1608 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1609 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1610 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1611 | // do not vibrate if already in vibrate mode |
| 1612 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1613 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1614 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1615 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1616 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1617 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1618 | streamState.mIsMuted, callingPackage, flags); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1619 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1620 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1621 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1622 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1623 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1624 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1625 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1626 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1627 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1628 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1629 | |
| 1630 | // If the ringer mode or zen is muting the stream, do not change stream unless |
| 1631 | // it'll cause us to exit dnd |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1632 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1633 | adjustVolume = false; |
| 1634 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1635 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1636 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1637 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1638 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1639 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1640 | if (isMuteAdjust) { |
| 1641 | boolean state; |
| 1642 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1643 | state = !streamState.mIsMuted; |
| 1644 | } else { |
| 1645 | state = direction == AudioManager.ADJUST_MUTE; |
| 1646 | } |
| 1647 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1648 | setSystemAudioMute(state); |
| 1649 | } |
| 1650 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1651 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1652 | if (!(readCameraSoundForced() |
| 1653 | && (mStreamStates[stream].getStreamType() |
| 1654 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1655 | mStreamStates[stream].mute(state); |
| 1656 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1660 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1661 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1662 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1663 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1664 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1665 | // Post message to set system volume (it in turn will post a |
| 1666 | // message to persist). |
| 1667 | if (streamState.mIsMuted) { |
| 1668 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1669 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1670 | // unmute immediately for volume up |
| 1671 | streamState.mute(false); |
| 1672 | } else if (direction == AudioManager.ADJUST_LOWER) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 1673 | if (mIsSingleVolume) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1674 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1675 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1676 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1677 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1678 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1679 | sendMsg(mAudioHandler, |
| 1680 | MSG_SET_DEVICE_VOLUME, |
| 1681 | SENDMSG_QUEUE, |
| 1682 | device, |
| 1683 | 0, |
| 1684 | streamState, |
| 1685 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1686 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1687 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1688 | int newIndex = mStreamStates[streamType].getIndex(device); |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1689 | |
| 1690 | // Check if volume update should be send to AVRCP |
| 1691 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1692 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1693 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1694 | if (DEBUG_VOL) { |
| 1695 | Log.d(TAG, "adjustSreamVolume: postSetAvrcpAbsoluteVolumeIndex index=" |
| 1696 | + newIndex + "stream=" + streamType); |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1697 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1698 | mDeviceBroker.postSetAvrcpAbsoluteVolumeIndex(newIndex); |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1701 | // Check if volume update should be send to Hearing Aid |
| 1702 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1703 | Log.i(TAG, "adjustSreamVolume postSetHearingAidVolumeIndex index=" + newIndex |
| 1704 | + " stream=" + streamType); |
| 1705 | mDeviceBroker.postSetHearingAidVolumeIndex(newIndex, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
Ajay Panicker | e3946c8 | 2018-02-26 16:04:15 -0800 | [diff] [blame] | 1708 | // Check if volume update should be sent to Hdmi system audio. |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1709 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1710 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1711 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1712 | synchronized (mHdmiClientLock) { |
| 1713 | if (mHdmiManager != null) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1714 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1715 | if (mHdmiCecSink && |
| 1716 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1717 | oldIndex != newIndex) { |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1718 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
| 1719 | KeyEvent.KEYCODE_VOLUME_UP; |
| 1720 | final long ident = Binder.clearCallingIdentity(); |
| 1721 | try { |
| 1722 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1723 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1724 | } finally { |
| 1725 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1726 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1727 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1728 | |
| 1729 | if (mHdmiAudioSystemClient != null && |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 1730 | mHdmiSystemAudioSupported && |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1731 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1732 | (oldIndex != newIndex || isMuteAdjust)) { |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 1733 | final long identity = Binder.clearCallingIdentity(); |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1734 | mHdmiAudioSystemClient.sendReportAudioStatusCecCommand( |
| 1735 | isMuteAdjust, getStreamVolume(AudioSystem.STREAM_MUSIC), |
| 1736 | getStreamMaxVolume(AudioSystem.STREAM_MUSIC), |
| 1737 | isStreamMute(AudioSystem.STREAM_MUSIC)); |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 1738 | Binder.restoreCallingIdentity(identity); |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1739 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1740 | } |
| 1741 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1742 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1743 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1744 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1747 | // Called after a delay when volume down is pressed while muted |
| 1748 | private void onUnmuteStream(int stream, int flags) { |
| 1749 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1750 | streamState.mute(false); |
| 1751 | |
| 1752 | final int device = getDeviceForStream(stream); |
| 1753 | final int index = mStreamStates[stream].getIndex(device); |
| 1754 | sendVolumeUpdate(stream, index, index, flags); |
| 1755 | } |
| 1756 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1757 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1758 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1759 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1760 | synchronized (mHdmiClientLock) { |
| 1761 | if (mHdmiManager == null |
| 1762 | || mHdmiTvClient == null |
| 1763 | || oldVolume == newVolume |
| 1764 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0 |
| 1765 | || !mHdmiSystemAudioSupported) return; |
| 1766 | final long token = Binder.clearCallingIdentity(); |
| 1767 | try { |
| 1768 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
| 1769 | } finally { |
| 1770 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | } |
| 1774 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1775 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1776 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1777 | class StreamVolumeCommand { |
| 1778 | public final int mStreamType; |
| 1779 | public final int mIndex; |
| 1780 | public final int mFlags; |
| 1781 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1782 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1783 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1784 | mStreamType = streamType; |
| 1785 | mIndex = index; |
| 1786 | mFlags = flags; |
| 1787 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1788 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1789 | |
| 1790 | @Override |
| 1791 | public String toString() { |
| 1792 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1793 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1794 | .append(mDevice).append('}').toString(); |
| 1795 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1796 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1797 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1798 | private int getNewRingerMode(int stream, int index, int flags) { |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 1799 | // setRingerMode does nothing if the device is single volume,so the value would be unchanged |
| 1800 | if (mIsSingleVolume) { |
| 1801 | return getRingerModeExternal(); |
| 1802 | } |
| 1803 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1804 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1805 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1806 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1807 | int newRingerMode; |
| 1808 | if (index == 0) { |
| 1809 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1810 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1811 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1812 | } else { |
| 1813 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1814 | } |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1815 | return newRingerMode; |
| 1816 | } |
| 1817 | return getRingerModeExternal(); |
| 1818 | } |
| 1819 | |
| 1820 | private boolean isAndroidNPlus(String caller) { |
| 1821 | try { |
| 1822 | final ApplicationInfo applicationInfo = |
| 1823 | mContext.getPackageManager().getApplicationInfoAsUser( |
| 1824 | caller, 0, UserHandle.getUserId(Binder.getCallingUid())); |
| 1825 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) { |
| 1826 | return true; |
| 1827 | } |
| 1828 | return false; |
| 1829 | } catch (PackageManager.NameNotFoundException e) { |
| 1830 | return true; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | private boolean wouldToggleZenMode(int newMode) { |
| 1835 | if (getRingerModeExternal() == AudioManager.RINGER_MODE_SILENT |
| 1836 | && newMode != AudioManager.RINGER_MODE_SILENT) { |
| 1837 | return true; |
| 1838 | } else if (getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT |
| 1839 | && newMode == AudioManager.RINGER_MODE_SILENT) { |
| 1840 | return true; |
| 1841 | } |
| 1842 | return false; |
| 1843 | } |
| 1844 | |
| 1845 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1846 | String caller) { |
| 1847 | final int stream = mStreamVolumeAlias[streamType]; |
| 1848 | setStreamVolumeInt(stream, index, device, false, caller); |
| 1849 | // setting volume on ui sounds stream type also controls silent mode |
| 1850 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1851 | (stream == getUiSoundsStreamType())) { |
| 1852 | setRingerMode(getNewRingerMode(stream, index, flags), |
| 1853 | TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1854 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1855 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1856 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1860 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1861 | if ((streamType == AudioManager.STREAM_ACCESSIBILITY) && !canChangeAccessibilityVolume()) { |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1862 | Log.w(TAG, "Trying to call setStreamVolume() for a11y without" |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1863 | + " CHANGE_ACCESSIBILITY_VOLUME callingPackage=" + callingPackage); |
Jean-Michel Trivi | ab2d931 | 2017-03-13 15:33:39 -0700 | [diff] [blame] | 1864 | return; |
| 1865 | } |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1866 | if ((streamType == AudioManager.STREAM_VOICE_CALL || |
| 1867 | streamType == AudioManager.STREAM_BLUETOOTH_SCO) && |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1868 | (index == 0) && |
| 1869 | (mContext.checkCallingOrSelfPermission( |
| 1870 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1871 | != PackageManager.PERMISSION_GRANTED)) { |
Tomoharu Kasahara | a0d0c54 | 2019-01-18 20:04:38 +0900 | [diff] [blame] | 1872 | Log.w(TAG, "Trying to call setStreamVolume() for STREAM_VOICE_CALL or" |
| 1873 | + " STREAM_BLUETOOTH_SCO and index 0 without" |
Nadav Bar | 6b7751d | 2017-12-24 16:03:08 +0200 | [diff] [blame] | 1874 | + " MODIFY_PHONE_STATE callingPackage=" + callingPackage); |
| 1875 | return; |
| 1876 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1877 | sVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType, |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 1878 | index/*val1*/, flags/*val2*/, callingPackage)); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1879 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1880 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 1883 | private boolean canChangeAccessibilityVolume() { |
| 1884 | synchronized (mAccessibilityServiceUidsLock) { |
| 1885 | if (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 1886 | android.Manifest.permission.CHANGE_ACCESSIBILITY_VOLUME)) { |
| 1887 | return true; |
| 1888 | } |
| 1889 | if (mAccessibilityServiceUids != null) { |
| 1890 | int callingUid = Binder.getCallingUid(); |
| 1891 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 1892 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 1893 | return true; |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | return false; |
| 1898 | } |
| 1899 | } |
| 1900 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1901 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1902 | String caller, int uid) { |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 1903 | if (DEBUG_VOL) { |
| 1904 | Log.d(TAG, "setStreamVolume(stream=" + streamType+", index=" + index |
| 1905 | + ", calling=" + callingPackage + ")"); |
| 1906 | } |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1907 | if (mUseFixedVolume) { |
| 1908 | return; |
| 1909 | } |
| 1910 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1911 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1912 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1913 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1914 | |
| 1915 | final int device = getDeviceForStream(streamType); |
| 1916 | int oldIndex; |
| 1917 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1918 | // skip a2dp absolute volume control request when the device |
| 1919 | // is not an a2dp device |
| 1920 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1921 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1922 | return; |
| 1923 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1924 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1925 | // so we handle user restrictions correctly. |
| 1926 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1927 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1928 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1929 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1930 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1931 | return; |
| 1932 | } |
| 1933 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1934 | if (isAndroidNPlus(callingPackage) |
| 1935 | && wouldToggleZenMode(getNewRingerMode(streamTypeAlias, index, flags)) |
| 1936 | && !mNm.isNotificationPolicyAccessGrantedForPackage(callingPackage)) { |
| 1937 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 1938 | } |
| 1939 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 1940 | if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) { |
| 1941 | return; |
| 1942 | } |
| 1943 | |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 1944 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1945 | // reset any pending volume command |
| 1946 | mPendingVolumeCommand = null; |
| 1947 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1948 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1949 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1950 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1951 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1952 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC |
| 1953 | && (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 |
| 1954 | && (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1955 | if (DEBUG_VOL) { |
| 1956 | Log.d(TAG, "setStreamVolume postSetAvrcpAbsoluteVolumeIndex index=" + index |
| 1957 | + "stream=" + streamType); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1958 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1959 | mDeviceBroker.postSetAvrcpAbsoluteVolumeIndex(index / 10); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1962 | if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 1963 | Log.i(TAG, "setStreamVolume postSetHearingAidVolumeIndex index=" + index |
| 1964 | + " stream=" + streamType); |
| 1965 | mDeviceBroker.postSetHearingAidVolumeIndex(index, streamType); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1968 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1969 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1970 | } |
| 1971 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1972 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1973 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1974 | ((device & mFixedVolumeDevices) != 0)) { |
| 1975 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1976 | |
| 1977 | // volume is either 0 or max allowed for fixed volume devices |
| 1978 | if (index != 0) { |
| 1979 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1980 | (device & mSafeMediaVolumeDevices) != 0) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1981 | index = safeMediaVolumeIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1982 | } else { |
| 1983 | index = streamState.getMaxIndex(); |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1988 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1989 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1990 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1991 | streamType, index, flags, device); |
| 1992 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1993 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1994 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1995 | } |
| 1996 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 1997 | synchronized (mHdmiClientLock) { |
| 1998 | if (mHdmiManager != null && |
| 1999 | mHdmiAudioSystemClient != null && |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 2000 | mHdmiSystemAudioSupported && |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 2001 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 2002 | (oldIndex != index)) { |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 2003 | final long identity = Binder.clearCallingIdentity(); |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 2004 | mHdmiAudioSystemClient.sendReportAudioStatusCecCommand( |
| 2005 | false, getStreamVolume(AudioSystem.STREAM_MUSIC), |
| 2006 | getStreamMaxVolume(AudioSystem.STREAM_MUSIC), |
| 2007 | isStreamMute(AudioSystem.STREAM_MUSIC)); |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 2008 | Binder.restoreCallingIdentity(identity); |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 2009 | } |
| 2010 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2011 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2012 | } |
| 2013 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2014 | // 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] | 2015 | private boolean volumeAdjustmentAllowedByDnd(int streamTypeAlias, int flags) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2016 | switch (mNm.getZenMode()) { |
| 2017 | case Settings.Global.ZEN_MODE_OFF: |
| 2018 | return true; |
| 2019 | case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: |
| 2020 | case Settings.Global.ZEN_MODE_ALARMS: |
| 2021 | case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 2022 | return !isStreamMutedByRingerOrZenMode(streamTypeAlias) |
| 2023 | || streamTypeAlias == getUiSoundsStreamType() |
| 2024 | || (flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0; |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 2025 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2026 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 2027 | return true; |
| 2028 | } |
| 2029 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2030 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 2031 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
Jean-Michel Trivi | 60eddfd | 2018-03-09 15:31:12 -0800 | [diff] [blame] | 2032 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2033 | != PackageManager.PERMISSION_GRANTED) { |
| 2034 | return; |
| 2035 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2036 | if (DEBUG_VOL) { Log.d(TAG, String.format("forceVolumeControlStream(%d)", streamType)); } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2037 | synchronized(mForceControlStreamLock) { |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2038 | if (mVolumeControlStream != -1 && streamType != -1) { |
| 2039 | mUserSelectedVolumeControlStream = true; |
| 2040 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2041 | mVolumeControlStream = streamType; |
| 2042 | if (mVolumeControlStream == -1) { |
| 2043 | if (mForceControlStreamClient != null) { |
| 2044 | mForceControlStreamClient.release(); |
| 2045 | mForceControlStreamClient = null; |
| 2046 | } |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2047 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2048 | } else { |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 2049 | if (null == mForceControlStreamClient) { |
| 2050 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 2051 | } else { |
| 2052 | if (mForceControlStreamClient.getBinder() == cb) { |
| 2053 | Log.d(TAG, "forceVolumeControlStream cb:" + cb + " is already linked."); |
| 2054 | } else { |
| 2055 | mForceControlStreamClient.release(); |
| 2056 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 2057 | } |
| 2058 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 2064 | private IBinder mCb; // To be notified of client's death |
| 2065 | |
| 2066 | ForceControlStreamClient(IBinder cb) { |
| 2067 | if (cb != null) { |
| 2068 | try { |
| 2069 | cb.linkToDeath(this, 0); |
| 2070 | } catch (RemoteException e) { |
| 2071 | // Client has died! |
| 2072 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 2073 | cb = null; |
| 2074 | } |
| 2075 | } |
| 2076 | mCb = cb; |
| 2077 | } |
| 2078 | |
| 2079 | public void binderDied() { |
| 2080 | synchronized(mForceControlStreamLock) { |
| 2081 | Log.w(TAG, "SCO client died"); |
| 2082 | if (mForceControlStreamClient != this) { |
| 2083 | Log.w(TAG, "unregistered control stream client died"); |
| 2084 | } else { |
| 2085 | mForceControlStreamClient = null; |
| 2086 | mVolumeControlStream = -1; |
Jean-Michel Trivi | a7880d4 | 2017-04-15 12:41:05 -0700 | [diff] [blame] | 2087 | mUserSelectedVolumeControlStream = false; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | public void release() { |
| 2093 | if (mCb != null) { |
| 2094 | mCb.unlinkToDeath(this, 0); |
| 2095 | mCb = null; |
| 2096 | } |
| 2097 | } |
jianzhou | 21fb09f | 2018-02-28 14:03:15 +0800 | [diff] [blame] | 2098 | |
| 2099 | public IBinder getBinder() { |
| 2100 | return mCb; |
| 2101 | } |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2104 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 2105 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2106 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2107 | final long ident = Binder.clearCallingIdentity(); |
| 2108 | try { |
| 2109 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2110 | } finally { |
| 2111 | Binder.restoreCallingIdentity(ident); |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 2116 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2117 | final long ident = Binder.clearCallingIdentity(); |
| 2118 | try { |
| 2119 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2120 | } finally { |
| 2121 | Binder.restoreCallingIdentity(ident); |
| 2122 | } |
| 2123 | } |
| 2124 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2125 | private int getCurrentUserId() { |
| 2126 | final long ident = Binder.clearCallingIdentity(); |
| 2127 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2128 | UserInfo currentUser = ActivityManager.getService().getCurrentUser(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2129 | return currentUser.id; |
| 2130 | } catch (RemoteException e) { |
| 2131 | // Activity manager not running, nothing we can do assume user 0. |
| 2132 | } finally { |
| 2133 | Binder.restoreCallingIdentity(ident); |
| 2134 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 2135 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2138 | // UI update and Broadcast Intent |
Yue Li | 949865b | 2017-05-24 17:25:28 -0700 | [diff] [blame] | 2139 | protected void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 2140 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2141 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2142 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 2143 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 2144 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2145 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2148 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 2149 | // receives volume notification from Audio Receiver. |
| 2150 | private int updateFlagsForSystemAudio(int flags) { |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 2151 | synchronized (mHdmiClientLock) { |
| 2152 | if (mHdmiTvClient != null) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2153 | if (mHdmiSystemAudioSupported && |
| 2154 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 2155 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | return flags; |
| 2160 | } |
| 2161 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2162 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2163 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2164 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2165 | broadcastMasterMuteStatus(muted); |
| 2166 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2167 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2168 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2169 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2170 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 2171 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 2172 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2173 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 2174 | } |
| 2175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2177 | * Sets the stream state's index, and posts a message to set system volume. |
| 2178 | * This will not call out to the UI. Assumes a valid stream type. |
| 2179 | * |
| 2180 | * @param streamType Type of the stream |
| 2181 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2182 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2183 | * @param force If true, set the volume even if the desired volume is same |
| 2184 | * as the current volume. |
| 2185 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2186 | private void setStreamVolumeInt(int streamType, |
| 2187 | int index, |
| 2188 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2189 | boolean force, |
| 2190 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2191 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2192 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2193 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2194 | // Post message to set system volume (it in turn will post a message |
| 2195 | // to persist). |
| 2196 | sendMsg(mAudioHandler, |
| 2197 | MSG_SET_DEVICE_VOLUME, |
| 2198 | SENDMSG_QUEUE, |
| 2199 | device, |
| 2200 | 0, |
| 2201 | streamState, |
| 2202 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2206 | private void setSystemAudioMute(boolean state) { |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 2207 | synchronized (mHdmiClientLock) { |
| 2208 | if (mHdmiManager == null || mHdmiTvClient == null || !mHdmiSystemAudioSupported) return; |
| 2209 | final long token = Binder.clearCallingIdentity(); |
| 2210 | try { |
| 2211 | mHdmiTvClient.setSystemAudioMute(state); |
| 2212 | } finally { |
| 2213 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2218 | /** get stream mute state. */ |
| 2219 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2220 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 2221 | streamType = getActiveStreamType(streamType); |
| 2222 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2223 | synchronized (VolumeStreamState.class) { |
jiabin | 93d32ef | 2017-07-11 13:50:02 -0700 | [diff] [blame] | 2224 | ensureValidStreamType(streamType); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2225 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2226 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 2229 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 2230 | private IBinder mICallback; // To be notified of client's death |
| 2231 | |
| 2232 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 2233 | mICallback = cb; |
| 2234 | try { |
| 2235 | cb.linkToDeath(this, 0/*flags*/); |
| 2236 | } catch (RemoteException e) { |
| 2237 | Log.e(TAG, "can't link to death", e); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | boolean isHandlerFor(IBinder cb) { |
| 2242 | return mICallback.equals(cb); |
| 2243 | } |
| 2244 | |
| 2245 | void forget() { |
| 2246 | try { |
| 2247 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 2248 | } catch (NoSuchElementException e) { |
| 2249 | Log.e(TAG, "error unlinking to death", e); |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | public void binderDied() { |
| 2254 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 2255 | forceRemoteSubmixFullVolume(false, mICallback); |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | /** |
| 2260 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 2261 | * @return true if there is a registered death handler, false otherwise */ |
| 2262 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2263 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2264 | while (it.hasNext()) { |
| 2265 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 2266 | if (handler.isHandlerFor(cb)) { |
| 2267 | handler.forget(); |
| 2268 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 2269 | return true; |
| 2270 | } |
| 2271 | } |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 2276 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 2277 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 2278 | while (it.hasNext()) { |
| 2279 | if (it.next().isHandlerFor(cb)) { |
| 2280 | return true; |
| 2281 | } |
| 2282 | } |
| 2283 | return false; |
| 2284 | } |
| 2285 | |
| 2286 | private int mRmtSbmxFullVolRefCount = 0; |
| 2287 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 2288 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 2289 | |
| 2290 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 2291 | if (cb == null) { |
| 2292 | return; |
| 2293 | } |
| 2294 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 2295 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 2296 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 2297 | return; |
| 2298 | } |
| 2299 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 2300 | boolean applyRequired = false; |
| 2301 | if (startForcing) { |
| 2302 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 2303 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 2304 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2305 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2306 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2307 | applyRequired = true; |
| 2308 | } |
| 2309 | mRmtSbmxFullVolRefCount++; |
| 2310 | } |
| 2311 | } else { |
| 2312 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 2313 | mRmtSbmxFullVolRefCount--; |
| 2314 | if (mRmtSbmxFullVolRefCount == 0) { |
| 2315 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2316 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 2317 | applyRequired = true; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | if (applyRequired) { |
| 2322 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 2323 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 2324 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2329 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 2330 | int userId) { |
| 2331 | // If we are being called by the system check for user we are going to change |
| 2332 | // so we handle user restrictions correctly. |
| 2333 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2334 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2335 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2336 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 2337 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2338 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 2339 | return; |
| 2340 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2341 | if (userId != UserHandle.getCallingUserId() && |
| 2342 | mContext.checkCallingOrSelfPermission( |
| 2343 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2344 | != PackageManager.PERMISSION_GRANTED) { |
| 2345 | return; |
| 2346 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2347 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 2348 | } |
| 2349 | |
| 2350 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 2351 | if (DEBUG_VOL) { |
| 2352 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 2353 | } |
Hongwei Wang | daba124 | 2018-05-29 14:36:16 -0700 | [diff] [blame] | 2354 | if (!isPlatformAutomotive() && mUseFixedVolume) { |
| 2355 | // If using fixed volume, we don't mute. |
| 2356 | // TODO: remove the isPlatformAutomotive check here. |
| 2357 | // The isPlatformAutomotive check is added for safety but may not be necessary. |
| 2358 | return; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2359 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2360 | if (getCurrentUserId() == userId) { |
| 2361 | if (mute != AudioSystem.getMasterMute()) { |
| 2362 | setSystemAudioMute(mute); |
| 2363 | AudioSystem.setMasterMute(mute); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2364 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 2365 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2366 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 2367 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 2368 | sendBroadcastToAll(intent); |
| 2369 | } |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 2370 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /** get master mute state. */ |
| 2374 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 2375 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 2376 | } |
| 2377 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2378 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 2379 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 2380 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2381 | } |
| 2382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2383 | /** @see AudioManager#getStreamVolume(int) */ |
| 2384 | public int getStreamVolume(int streamType) { |
| 2385 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2386 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2387 | synchronized (VolumeStreamState.class) { |
| 2388 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 2389 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2390 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2391 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2392 | index = 0; |
| 2393 | } |
| 2394 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 2395 | (device & mFixedVolumeDevices) != 0) { |
| 2396 | index = mStreamStates[streamType].getMaxIndex(); |
| 2397 | } |
| 2398 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 2403 | public int getStreamMaxVolume(int streamType) { |
| 2404 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2405 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2406 | } |
| 2407 | |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 2408 | /** @see AudioManager#getStreamMinVolumeInt(int) */ |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2409 | public int getStreamMinVolume(int streamType) { |
| 2410 | ensureValidStreamType(streamType); |
| 2411 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 2412 | } |
| 2413 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2414 | /** Get last audible volume before stream was muted. */ |
| 2415 | public int getLastAudibleStreamVolume(int streamType) { |
| 2416 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2417 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2418 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2419 | } |
| 2420 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 2421 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 2422 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 2423 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2426 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 2427 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2428 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 2429 | // If we are being called by the system check for user we are going to change |
| 2430 | // so we handle user restrictions correctly. |
| 2431 | int uid = Binder.getCallingUid(); |
| 2432 | if (uid == android.os.Process.SYSTEM_UID) { |
| 2433 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 2434 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 2435 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 2436 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2437 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2438 | return; |
| 2439 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 2440 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 2441 | return; |
| 2442 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2443 | if (userId != UserHandle.getCallingUserId() && |
| 2444 | mContext.checkCallingOrSelfPermission( |
| 2445 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2446 | != PackageManager.PERMISSION_GRANTED) { |
| 2447 | return; |
| 2448 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2449 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 2450 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2451 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2452 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 2453 | if (DEBUG_VOL) { |
| 2454 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 2455 | } |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2456 | // only mute for the current user |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2457 | if (getCurrentUserId() == userId) { |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2458 | final boolean currentMute = AudioSystem.isMicrophoneMuted(); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2459 | final long identity = Binder.clearCallingIdentity(); |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2460 | AudioSystem.muteMicrophone(on); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2461 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2462 | if (on != currentMute) { |
| 2463 | mContext.sendBroadcast(new Intent(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED) |
| 2464 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)); |
| 2465 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2466 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2467 | } |
| 2468 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2469 | @Override |
| 2470 | public int getRingerModeExternal() { |
| 2471 | synchronized(mSettingsLock) { |
| 2472 | return mRingerModeExternal; |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | @Override |
| 2477 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2478 | synchronized(mSettingsLock) { |
| 2479 | return mRingerMode; |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2484 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2485 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 2486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2489 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2490 | public boolean isValidRingerMode(int ringerMode) { |
| 2491 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2492 | } |
| 2493 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2494 | public void setRingerModeExternal(int ringerMode, String caller) { |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2495 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 2496 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller)) { |
| 2497 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 2498 | } |
| 2499 | |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2500 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2504 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2505 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2506 | } |
| 2507 | |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2508 | public void silenceRingerModeInternal(String reason) { |
| 2509 | VibrationEffect effect = null; |
| 2510 | int ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2511 | int toastText = 0; |
| 2512 | |
| 2513 | int silenceRingerSetting = Settings.Secure.VOLUME_HUSH_OFF; |
| 2514 | if (mContext.getResources() |
| 2515 | .getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled)) { |
| 2516 | silenceRingerSetting = Settings.Secure.getIntForUser(mContentResolver, |
| 2517 | Settings.Secure.VOLUME_HUSH_GESTURE, VOLUME_HUSH_OFF, |
| 2518 | UserHandle.USER_CURRENT); |
| 2519 | } |
| 2520 | |
| 2521 | switch(silenceRingerSetting) { |
| 2522 | case VOLUME_HUSH_MUTE: |
| 2523 | effect = VibrationEffect.get(VibrationEffect.EFFECT_DOUBLE_CLICK); |
| 2524 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2525 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_silent; |
| 2526 | break; |
| 2527 | case VOLUME_HUSH_VIBRATE: |
| 2528 | effect = VibrationEffect.get(VibrationEffect.EFFECT_HEAVY_CLICK); |
| 2529 | ringerMode = AudioManager.RINGER_MODE_VIBRATE; |
| 2530 | toastText = com.android.internal.R.string.volume_dialog_ringer_guidance_vibrate; |
| 2531 | break; |
| 2532 | } |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 2533 | maybeVibrate(effect, reason); |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2534 | setRingerModeInternal(ringerMode, reason); |
| 2535 | Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT).show(); |
| 2536 | } |
| 2537 | |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 2538 | private boolean maybeVibrate(VibrationEffect effect, String reason) { |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2539 | if (!mHasVibrator) { |
| 2540 | return false; |
| 2541 | } |
| 2542 | final boolean hapticsDisabled = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 2543 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) == 0; |
| 2544 | if (hapticsDisabled) { |
| 2545 | return false; |
| 2546 | } |
| 2547 | |
| 2548 | if (effect == null) { |
| 2549 | return false; |
| 2550 | } |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 2551 | mVibrator.vibrate(Binder.getCallingUid(), mContext.getOpPackageName(), effect, |
| 2552 | reason, VIBRATION_ATTRIBUTES); |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 2553 | return true; |
| 2554 | } |
| 2555 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2556 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 2557 | if (mUseFixedVolume || mIsSingleVolume) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2558 | return; |
| 2559 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2560 | if (caller == null || caller.length() == 0) { |
| 2561 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2562 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2563 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2564 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2565 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2566 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2567 | final long identity = Binder.clearCallingIdentity(); |
| 2568 | try { |
| 2569 | synchronized (mSettingsLock) { |
| 2570 | final int ringerModeInternal = getRingerModeInternal(); |
| 2571 | final int ringerModeExternal = getRingerModeExternal(); |
| 2572 | if (external) { |
| 2573 | setRingerModeExt(ringerMode); |
| 2574 | if (mRingerModeDelegate != null) { |
| 2575 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2576 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2577 | } |
| 2578 | if (ringerMode != ringerModeInternal) { |
| 2579 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2580 | } |
| 2581 | } else /*internal*/ { |
| 2582 | if (ringerMode != ringerModeInternal) { |
| 2583 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2584 | } |
| 2585 | if (mRingerModeDelegate != null) { |
| 2586 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2587 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2588 | } |
| 2589 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2590 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2591 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2592 | } finally { |
| 2593 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2597 | private void setRingerModeExt(int ringerMode) { |
| 2598 | synchronized(mSettingsLock) { |
| 2599 | if (ringerMode == mRingerModeExternal) return; |
| 2600 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2601 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2602 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2603 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2604 | } |
| 2605 | |
Jean-Michel Trivi | c106d9f | 2018-06-07 12:28:27 -0700 | [diff] [blame] | 2606 | @GuardedBy("mSettingsLock") |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2607 | private void muteRingerModeStreams() { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2608 | // Mute stream if not previously muted by ringer mode and (ringer mode |
| 2609 | // is not RINGER_MODE_NORMAL OR stream is zen muted) and stream is affected by ringer mode. |
| 2610 | // Unmute stream if previously muted by ringer/zen mode and ringer mode |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2611 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2612 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2613 | |
| 2614 | if (mNm == null) { |
| 2615 | mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 2616 | } |
| 2617 | |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2618 | final int ringerMode = mRingerMode; // Read ringer mode as reading primitives is atomic |
| 2619 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2620 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
| 2621 | final boolean shouldRingSco = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2622 | && isBluetoothScoOn(); |
| 2623 | // Ask audio policy engine to force use Bluetooth SCO channel if needed |
| 2624 | final String eventSource = "muteRingerModeStreams() from u/pid:" + Binder.getCallingUid() |
| 2625 | + "/" + Binder.getCallingPid(); |
| 2626 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, AudioSystem.FOR_VIBRATE_RINGING, |
| 2627 | shouldRingSco ? AudioSystem.FORCE_BT_SCO : AudioSystem.FORCE_NONE, eventSource, 0); |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2628 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2629 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2630 | final boolean isMuted = isStreamMutedByRingerOrZenMode(streamType); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2631 | final boolean muteAllowedBySco = |
| 2632 | !(shouldRingSco && streamType == AudioSystem.STREAM_RING); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2633 | final boolean shouldZenMute = shouldZenMuteStream(streamType); |
| 2634 | final boolean shouldMute = shouldZenMute || (ringerModeMute |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 2635 | && isStreamAffectedByRingerMode(streamType) && muteAllowedBySco); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2636 | if (isMuted == shouldMute) continue; |
| 2637 | if (!shouldMute) { |
| 2638 | // unmute |
| 2639 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2640 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2641 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2642 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2643 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2644 | int device = vss.mIndexMap.keyAt(i); |
| 2645 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2646 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2647 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2648 | } |
| 2649 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2650 | // Persist volume for stream ring when it is changed here |
| 2651 | final int device = getDeviceForStream(streamType); |
| 2652 | sendMsg(mAudioHandler, |
| 2653 | MSG_PERSIST_VOLUME, |
| 2654 | SENDMSG_QUEUE, |
| 2655 | device, |
| 2656 | 0, |
| 2657 | mStreamStates[streamType], |
| 2658 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2659 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2660 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2661 | mStreamStates[streamType].mute(false); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2662 | mRingerAndZenModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2663 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2664 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2665 | mStreamStates[streamType].mute(true); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2666 | mRingerAndZenModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2667 | } |
| 2668 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2669 | } |
| 2670 | |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2671 | private boolean isAlarm(int streamType) { |
| 2672 | return streamType == AudioSystem.STREAM_ALARM; |
| 2673 | } |
| 2674 | |
| 2675 | private boolean isNotificationOrRinger(int streamType) { |
| 2676 | return streamType == AudioSystem.STREAM_NOTIFICATION |
| 2677 | || streamType == AudioSystem.STREAM_RING; |
| 2678 | } |
| 2679 | |
| 2680 | private boolean isMedia(int streamType) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 2681 | return streamType == AudioSystem.STREAM_MUSIC; |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | private boolean isSystem(int streamType) { |
| 2686 | return streamType == AudioSystem.STREAM_SYSTEM; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 2687 | } |
| 2688 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2689 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2690 | final boolean change; |
| 2691 | synchronized(mSettingsLock) { |
| 2692 | change = mRingerMode != ringerMode; |
| 2693 | mRingerMode = ringerMode; |
Jean-Michel Trivi | c106d9f | 2018-06-07 12:28:27 -0700 | [diff] [blame] | 2694 | muteRingerModeStreams(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2695 | } |
| 2696 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2697 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2698 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2699 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2700 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2701 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2702 | if (change) { |
| 2703 | // Send sticky broadcast |
| 2704 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2705 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2708 | /*package*/ void setUpdateRingerModeServiceInt() { |
| 2709 | setRingerModeInt(getRingerModeInternal(), false); |
| 2710 | } |
| 2711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | /** @see AudioManager#shouldVibrate(int) */ |
| 2713 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2714 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | |
| 2716 | switch (getVibrateSetting(vibrateType)) { |
| 2717 | |
| 2718 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2719 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | |
| 2721 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2722 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2723 | |
| 2724 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2725 | // return false, even for incoming calls |
| 2726 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | |
| 2728 | default: |
| 2729 | return false; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2734 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2735 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2736 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2737 | } |
| 2738 | |
| 2739 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2740 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2741 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2742 | if (!mHasVibrator) return; |
| 2743 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2744 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2745 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2746 | |
| 2747 | // Broadcast change |
| 2748 | broadcastVibrateSetting(vibrateType); |
| 2749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | } |
| 2751 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2752 | /*package*/ class SetModeDeathHandler implements IBinder.DeathRecipient { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2753 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2754 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2755 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2756 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2757 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2758 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2759 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | public void binderDied() { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2763 | int oldModeOwnerPid = 0; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2764 | int newModeOwnerPid = 0; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2765 | synchronized (mDeviceBroker.mSetModeLock) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2766 | Log.w(TAG, "setMode() client died"); |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2767 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2768 | oldModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2769 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2770 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2771 | if (index < 0) { |
| 2772 | Log.w(TAG, "unregistered setMode() client died"); |
| 2773 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2774 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2775 | } |
| 2776 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2777 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2778 | // SCO connections not started by the application changing the mode when pid changes |
| 2779 | if ((newModeOwnerPid != oldModeOwnerPid) && (newModeOwnerPid != 0)) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2780 | mDeviceBroker.postDisconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2781 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2782 | } |
| 2783 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2784 | public int getPid() { |
| 2785 | return mPid; |
| 2786 | } |
| 2787 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2788 | public void setMode(int mode) { |
| 2789 | mMode = mode; |
| 2790 | } |
| 2791 | |
| 2792 | public int getMode() { |
| 2793 | return mMode; |
| 2794 | } |
| 2795 | |
| 2796 | public IBinder getBinder() { |
| 2797 | return mCb; |
| 2798 | } |
| 2799 | } |
| 2800 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2802 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2803 | 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] | 2804 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2805 | return; |
| 2806 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2807 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2808 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2809 | (mContext.checkCallingOrSelfPermission( |
| 2810 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2811 | != PackageManager.PERMISSION_GRANTED)) { |
| 2812 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2813 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2814 | return; |
| 2815 | } |
| 2816 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2817 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2818 | return; |
| 2819 | } |
| 2820 | |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2821 | int oldModeOwnerPid = 0; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2822 | int newModeOwnerPid = 0; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2823 | synchronized (mDeviceBroker.mSetModeLock) { |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2824 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2825 | oldModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2826 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2827 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2828 | mode = mMode; |
| 2829 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2830 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2831 | } |
| 2832 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 2833 | // SCO connections not started by the application changing the mode when pid changes |
| 2834 | if ((newModeOwnerPid != oldModeOwnerPid) && (newModeOwnerPid != 0)) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2835 | mDeviceBroker.postDisconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2838 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 2839 | // must be called synchronized on mSetModeLock |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2840 | // 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] | 2841 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2842 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2843 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2844 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2845 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2846 | if (cb == null) { |
| 2847 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2848 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2849 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2850 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2851 | SetModeDeathHandler hdlr = null; |
| 2852 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2853 | while (iter.hasNext()) { |
| 2854 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2855 | if (h.getPid() == pid) { |
| 2856 | hdlr = h; |
| 2857 | // Remove from client list so that it is re-inserted at top of list |
| 2858 | iter.remove(); |
| 2859 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2860 | break; |
| 2861 | } |
| 2862 | } |
| 2863 | int status = AudioSystem.AUDIO_STATUS_OK; |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2864 | int actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2865 | do { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2866 | actualMode = mode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2867 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2868 | // get new mode from client at top the list if any |
| 2869 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2870 | hdlr = mSetModeDeathHandlers.get(0); |
| 2871 | cb = hdlr.getBinder(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2872 | actualMode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2873 | if (DEBUG_MODE) { |
| 2874 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2875 | + hdlr.mPid); |
| 2876 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2877 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2878 | } else { |
| 2879 | if (hdlr == null) { |
| 2880 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2881 | } |
| 2882 | // Register for client death notification |
| 2883 | try { |
| 2884 | cb.linkToDeath(hdlr, 0); |
| 2885 | } catch (RemoteException e) { |
| 2886 | // Client has died! |
| 2887 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2888 | } |
| 2889 | |
| 2890 | // Last client to call setMode() is always at top of client list |
| 2891 | // as required by SetModeDeathHandler.binderDied() |
| 2892 | mSetModeDeathHandlers.add(0, hdlr); |
| 2893 | hdlr.setMode(mode); |
| 2894 | } |
| 2895 | |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2896 | if (actualMode != mMode) { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2897 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2898 | status = AudioSystem.setPhoneState(actualMode); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 2899 | Binder.restoreCallingIdentity(identity); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2900 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2901 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + actualMode); } |
| 2902 | mMode = actualMode; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2903 | } else { |
| 2904 | if (hdlr != null) { |
| 2905 | mSetModeDeathHandlers.remove(hdlr); |
| 2906 | cb.unlinkToDeath(hdlr, 0); |
| 2907 | } |
| 2908 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2909 | 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] | 2910 | mode = AudioSystem.MODE_NORMAL; |
| 2911 | } |
| 2912 | } else { |
| 2913 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2914 | } |
| 2915 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2916 | |
| 2917 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2918 | if (actualMode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2919 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2920 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2921 | } else { |
| 2922 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2924 | } |
Eric Laurent | 6afa650 | 2017-09-28 15:18:19 -0700 | [diff] [blame] | 2925 | // Note: newModeOwnerPid is always 0 when actualMode is MODE_NORMAL |
| 2926 | mModeLogger.log( |
| 2927 | new PhoneStateEvent(caller, pid, mode, newModeOwnerPid, actualMode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2928 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2929 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2930 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2931 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2932 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2933 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2934 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2935 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | /** @see AudioManager#getMode() */ |
| 2939 | public int getMode() { |
| 2940 | return mMode; |
| 2941 | } |
| 2942 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2943 | //========================================================================================== |
| 2944 | // Sound Effects |
| 2945 | //========================================================================================== |
| 2946 | |
| 2947 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2948 | private static final String ATTR_VERSION = "version"; |
| 2949 | private static final String TAG_GROUP = "group"; |
| 2950 | private static final String ATTR_GROUP_NAME = "name"; |
| 2951 | private static final String TAG_ASSET = "asset"; |
| 2952 | private static final String ATTR_ASSET_ID = "id"; |
| 2953 | private static final String ATTR_ASSET_FILE = "file"; |
| 2954 | |
| 2955 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2956 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2957 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2958 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2959 | |
| 2960 | class LoadSoundEffectReply { |
| 2961 | public int mStatus = 1; |
| 2962 | }; |
| 2963 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2964 | private void loadTouchSoundAssetDefaults() { |
| 2965 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2966 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2967 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2968 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | private void loadTouchSoundAssets() { |
| 2973 | XmlResourceParser parser = null; |
| 2974 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2975 | // only load assets once. |
| 2976 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2977 | return; |
| 2978 | } |
| 2979 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2980 | loadTouchSoundAssetDefaults(); |
| 2981 | |
| 2982 | try { |
| 2983 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2984 | |
| 2985 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2986 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2987 | boolean inTouchSoundsGroup = false; |
| 2988 | |
| 2989 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2990 | while (true) { |
| 2991 | XmlUtils.nextElement(parser); |
| 2992 | String element = parser.getName(); |
| 2993 | if (element == null) { |
| 2994 | break; |
| 2995 | } |
| 2996 | if (element.equals(TAG_GROUP)) { |
| 2997 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2998 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2999 | inTouchSoundsGroup = true; |
| 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | } |
| 3004 | while (inTouchSoundsGroup) { |
| 3005 | XmlUtils.nextElement(parser); |
| 3006 | String element = parser.getName(); |
| 3007 | if (element == null) { |
| 3008 | break; |
| 3009 | } |
| 3010 | if (element.equals(TAG_ASSET)) { |
| 3011 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 3012 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 3013 | int fx; |
| 3014 | |
| 3015 | try { |
| 3016 | Field field = AudioManager.class.getField(id); |
| 3017 | fx = field.getInt(null); |
| 3018 | } catch (Exception e) { |
| 3019 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 3020 | continue; |
| 3021 | } |
| 3022 | |
| 3023 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 3024 | if (i == -1) { |
| 3025 | i = SOUND_EFFECT_FILES.size(); |
| 3026 | SOUND_EFFECT_FILES.add(file); |
| 3027 | } |
| 3028 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 3029 | } else { |
| 3030 | break; |
| 3031 | } |
| 3032 | } |
| 3033 | } |
| 3034 | } catch (Resources.NotFoundException e) { |
| 3035 | Log.w(TAG, "audio assets file not found", e); |
| 3036 | } catch (XmlPullParserException e) { |
| 3037 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 3038 | } catch (IOException e) { |
| 3039 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 3040 | } finally { |
| 3041 | if (parser != null) { |
| 3042 | parser.close(); |
| 3043 | } |
| 3044 | } |
| 3045 | } |
| 3046 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3047 | /** @see AudioManager#playSoundEffect(int) */ |
| 3048 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3049 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | public void playSoundEffectVolume(int effectType, float volume) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3054 | // do not try to play the sound effect if the system stream is muted |
| 3055 | if (isStreamMutedByRingerOrZenMode(STREAM_SYSTEM)) { |
| 3056 | return; |
| 3057 | } |
| 3058 | |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 3059 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 3060 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 3061 | return; |
| 3062 | } |
| 3063 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3064 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3065 | effectType, (int) (volume * 1000), null, 0); |
| 3066 | } |
| 3067 | |
| 3068 | /** |
| 3069 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 3070 | * 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] | 3071 | */ |
| 3072 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3073 | int attempts = 3; |
| 3074 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3075 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3076 | synchronized (reply) { |
| 3077 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 3078 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3079 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3080 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3081 | } catch (InterruptedException e) { |
| 3082 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 3083 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3085 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3086 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | /** |
| 3090 | * Unloads samples from the sound pool. |
| 3091 | * This method can be called to free some memory when |
| 3092 | * sound effects are disabled. |
| 3093 | */ |
| 3094 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3095 | 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] | 3096 | } |
| 3097 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3098 | class SoundPoolListenerThread extends Thread { |
| 3099 | public SoundPoolListenerThread() { |
| 3100 | super("SoundPoolListenerThread"); |
| 3101 | } |
| 3102 | |
| 3103 | @Override |
| 3104 | public void run() { |
| 3105 | |
| 3106 | Looper.prepare(); |
| 3107 | mSoundPoolLooper = Looper.myLooper(); |
| 3108 | |
| 3109 | synchronized (mSoundEffectsLock) { |
| 3110 | if (mSoundPool != null) { |
| 3111 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 3112 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 3113 | } |
| 3114 | mSoundEffectsLock.notify(); |
| 3115 | } |
| 3116 | Looper.loop(); |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | private final class SoundPoolCallback implements |
| 3121 | android.media.SoundPool.OnLoadCompleteListener { |
| 3122 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3123 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 3124 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3125 | |
| 3126 | public int status() { |
| 3127 | return mStatus; |
| 3128 | } |
| 3129 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3130 | public void setSamples(int[] samples) { |
| 3131 | for (int i = 0; i < samples.length; i++) { |
| 3132 | // do not wait ack for samples rejected upfront by SoundPool |
| 3133 | if (samples[i] > 0) { |
| 3134 | mSamples.add(samples[i]); |
| 3135 | } |
| 3136 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 3140 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3141 | int i = mSamples.indexOf(sampleId); |
| 3142 | if (i >= 0) { |
| 3143 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3144 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3145 | if ((status != 0) || mSamples. isEmpty()) { |
| 3146 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 3147 | mSoundEffectsLock.notify(); |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | } |
| 3152 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3153 | /** @see AudioManager#reloadAudioSettings() */ |
| 3154 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3155 | readAudioSettings(false /*userSwitch*/); |
| 3156 | } |
| 3157 | |
| 3158 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3159 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 3160 | readPersistedSettings(); |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 3161 | readUserRestrictions(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3162 | |
| 3163 | // restore volume settings |
| 3164 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3165 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 3166 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 3167 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3168 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 3169 | continue; |
| 3170 | } |
| 3171 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3172 | streamState.readSettings(); |
| 3173 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3174 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3175 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3176 | !isStreamMutedByRingerOrZenMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3177 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3178 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3182 | // apply new ringer mode before checking volume for alias streams so that streams |
| 3183 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3184 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3185 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3186 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3187 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3188 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3189 | |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 3190 | synchronized (mSafeMediaVolumeStateLock) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3191 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 3192 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 3193 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3194 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3195 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3196 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3197 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3200 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3201 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3202 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 3203 | return; |
| 3204 | } |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3205 | // for logging only |
| 3206 | final String eventSource = new StringBuilder("setSpeakerphoneOn(").append(on) |
| 3207 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3208 | .append(Binder.getCallingPid()).toString(); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3209 | mDeviceBroker.setSpeakerphoneOn(on, eventSource); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 3213 | public boolean isSpeakerphoneOn() { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3214 | return mDeviceBroker.isSpeakerphoneOn(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 3217 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3218 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 3219 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 3220 | return; |
| 3221 | } |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3222 | |
| 3223 | // Only enable calls from system components |
Vitor Albuquerque | c3bb48c | 2018-03-07 10:39:59 -0300 | [diff] [blame] | 3224 | if (UserHandle.getCallingAppId() >= FIRST_APPLICATION_UID) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3225 | mDeviceBroker.setBluetoothScoOnByApp(on); |
Sharad Sangle | 1d18844 | 2017-05-09 16:05:40 +0530 | [diff] [blame] | 3226 | return; |
| 3227 | } |
| 3228 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3229 | // for logging only |
| 3230 | final String eventSource = new StringBuilder("setBluetoothScoOn(").append(on) |
| 3231 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3232 | .append(Binder.getCallingPid()).toString(); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3233 | |
| 3234 | mDeviceBroker.setBluetoothScoOn(on, eventSource); |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 3235 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 3236 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3237 | /** @see AudioManager#isBluetoothScoOn() |
| 3238 | * Note that it doesn't report internal state, but state seen by apps (which may have |
| 3239 | * called setBluetoothScoOn() */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3240 | public boolean isBluetoothScoOn() { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3241 | return mDeviceBroker.isBluetoothScoOnForApp(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3244 | // TODO investigate internal users due to deprecation of SDK API |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3245 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3246 | public void setBluetoothA2dpOn(boolean on) { |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 3247 | // for logging only |
| 3248 | final String eventSource = new StringBuilder("setBluetoothA2dpOn(").append(on) |
| 3249 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3250 | .append(Binder.getCallingPid()).toString(); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3251 | mDeviceBroker.setBluetoothA2dpOn_Async(on, eventSource); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
Sungsoo | cf09fe6 | 2016-09-28 16:21:48 +0900 | [diff] [blame] | 3254 | /** @see AudioManager#isBluetoothA2dpOn() */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3255 | public boolean isBluetoothA2dpOn() { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3256 | return mDeviceBroker.isBluetoothA2dpOn(); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3257 | } |
| 3258 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3259 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3260 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3261 | final int scoAudioMode = |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3262 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3263 | BtHelper.SCO_MODE_VIRTUAL_CALL : BtHelper.SCO_MODE_UNDEFINED; |
| 3264 | final String eventSource = new StringBuilder("startBluetoothSco()") |
| 3265 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3266 | .append(Binder.getCallingPid()).toString(); |
| 3267 | startBluetoothScoInt(cb, scoAudioMode, eventSource); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 3271 | public void startBluetoothScoVirtualCall(IBinder cb) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3272 | final String eventSource = new StringBuilder("startBluetoothScoVirtualCall()") |
| 3273 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3274 | .append(Binder.getCallingPid()).toString(); |
| 3275 | startBluetoothScoInt(cb, BtHelper.SCO_MODE_VIRTUAL_CALL, eventSource); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3278 | void startBluetoothScoInt(IBinder cb, int scoAudioMode, @NonNull String eventSource) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3279 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3280 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3281 | return; |
| 3282 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3283 | mDeviceBroker.startBluetoothScoForClient_Sync(cb, scoAudioMode, eventSource); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /** @see AudioManager#stopBluetoothSco() */ |
| 3287 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3288 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3289 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3290 | return; |
| 3291 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3292 | final String eventSource = new StringBuilder("stopBluetoothSco()") |
| 3293 | .append(") from u/pid:").append(Binder.getCallingUid()).append("/") |
| 3294 | .append(Binder.getCallingPid()).toString(); |
| 3295 | mDeviceBroker.stopBluetoothScoForClient_Sync(cb, eventSource); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3296 | } |
| 3297 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3298 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3299 | /*package*/ ContentResolver getContentResolver() { |
| 3300 | return mContentResolver; |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3301 | } |
| 3302 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3303 | private void onCheckMusicActive(String caller) { |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 3304 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3305 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3306 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3307 | |
| 3308 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3309 | sendMsg(mAudioHandler, |
| 3310 | MSG_CHECK_MUSIC_ACTIVE, |
| 3311 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3312 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3313 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3314 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3315 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3316 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3317 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) |
| 3318 | && (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3319 | // Approximate cumulative active music time |
| 3320 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3321 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3322 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3323 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3324 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3325 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3332 | private void saveMusicActiveMs() { |
| 3333 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3334 | } |
| 3335 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3336 | private int getSafeUsbMediaVolumeIndex() { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3337 | // determine UI volume index corresponding to the wanted safe gain in dBFS |
| 3338 | int min = MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3339 | int max = MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 3340 | |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3341 | mSafeUsbMediaVolumeDbfs = mContext.getResources().getInteger( |
| 3342 | com.android.internal.R.integer.config_safe_media_volume_usb_mB) / 100.0f; |
| 3343 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3344 | while (Math.abs(max - min) > 1) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3345 | int index = (max + min) / 2; |
| 3346 | float gainDB = AudioSystem.getStreamVolumeDB( |
| 3347 | AudioSystem.STREAM_MUSIC, index, AudioSystem.DEVICE_OUT_USB_HEADSET); |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 3348 | if (Float.isNaN(gainDB)) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3349 | //keep last min in case of read error |
| 3350 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3351 | } else if (gainDB == mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3352 | min = index; |
| 3353 | break; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 3354 | } else if (gainDB < mSafeUsbMediaVolumeDbfs) { |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3355 | min = index; |
| 3356 | } else { |
| 3357 | max = index; |
| 3358 | } |
| 3359 | } |
| 3360 | return min * 10; |
| 3361 | } |
| 3362 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3363 | private void onConfigureSafeVolume(boolean force, String caller) { |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 3364 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3365 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3366 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3367 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3368 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 3369 | |
| 3370 | mSafeUsbMediaVolumeIndex = getSafeUsbMediaVolumeIndex(); |
| 3371 | |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3372 | boolean safeMediaVolumeEnabled = |
| 3373 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3374 | || mContext.getResources().getBoolean( |
| 3375 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3376 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3377 | boolean safeMediaVolumeBypass = |
| 3378 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3379 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3380 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3381 | // next time we boot and cannot be "inactive" |
| 3382 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3383 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3384 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3385 | // The state can already be "inactive" here if the user has forced it before |
| 3386 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3387 | // it to "active". |
| 3388 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3389 | if (mMusicActiveMs == 0) { |
| 3390 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3391 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3392 | } else { |
| 3393 | // We have existing playback time recorded, already confirmed. |
| 3394 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3395 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3396 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3397 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3398 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3399 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3400 | } |
| 3401 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3402 | sendMsg(mAudioHandler, |
| 3403 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3404 | SENDMSG_QUEUE, |
| 3405 | persistedState, |
| 3406 | 0, |
| 3407 | null, |
| 3408 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3413 | /////////////////////////////////////////////////////////////////////////// |
| 3414 | // Internal methods |
| 3415 | /////////////////////////////////////////////////////////////////////////// |
| 3416 | |
| 3417 | /** |
| 3418 | * Checks if the adjustment should change ringer mode instead of just |
| 3419 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3420 | * indices on the stream states. |
| 3421 | */ |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3422 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted, |
| 3423 | String caller, int flags) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3424 | int result = FLAG_ADJUST_VOLUME; |
Zak Cohen | 4779829 | 2017-11-30 12:34:20 -0800 | [diff] [blame] | 3425 | if (isPlatformTelevision() || mIsSingleVolume) { |
Hank Freund | 21003f6 | 2015-12-08 09:05:46 -0800 | [diff] [blame] | 3426 | return result; |
| 3427 | } |
| 3428 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3429 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3430 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3431 | switch (ringerMode) { |
| 3432 | case RINGER_MODE_NORMAL: |
| 3433 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3434 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3435 | // "step" is the delta in internal index units corresponding to a |
| 3436 | // change of 1 in UI index units. |
| 3437 | // Because of rounding when rescaling from one stream index range to its alias |
| 3438 | // index range, we cannot simply test oldIndex == step: |
| 3439 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3440 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3441 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3442 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3443 | } |
| 3444 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3445 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3446 | ringerMode = RINGER_MODE_SILENT; |
| 3447 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3448 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3449 | } else if (mIsSingleVolume && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3450 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3451 | if (mHasVibrator) { |
| 3452 | ringerMode = RINGER_MODE_VIBRATE; |
| 3453 | } else { |
| 3454 | ringerMode = RINGER_MODE_SILENT; |
| 3455 | } |
| 3456 | // Setting the ringer mode will toggle mute |
| 3457 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3458 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3459 | break; |
| 3460 | case RINGER_MODE_VIBRATE: |
| 3461 | if (!mHasVibrator) { |
| 3462 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3463 | "but no vibrator is present"); |
| 3464 | break; |
| 3465 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3466 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3467 | // This is the case we were muted with the volume turned up |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3468 | if (mIsSingleVolume && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3469 | ringerMode = RINGER_MODE_NORMAL; |
| 3470 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3471 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3472 | final long diff = SystemClock.uptimeMillis() |
| 3473 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3474 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 3475 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3476 | ringerMode = RINGER_MODE_SILENT; |
| 3477 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3478 | } else { |
| 3479 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3480 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3481 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3482 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3483 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3484 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3485 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3486 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3487 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3488 | break; |
| 3489 | case RINGER_MODE_SILENT: |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3490 | if (mIsSingleVolume && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3491 | // This is the case we were muted with the volume turned up |
| 3492 | ringerMode = RINGER_MODE_NORMAL; |
| 3493 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3494 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3495 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3496 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3497 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3498 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3499 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3500 | ringerMode = RINGER_MODE_VIBRATE; |
| 3501 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3502 | // If we don't have a vibrator or they were toggling mute |
| 3503 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3504 | ringerMode = RINGER_MODE_NORMAL; |
| 3505 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3506 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3507 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3508 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3509 | break; |
| 3510 | default: |
| 3511 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3512 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3513 | } |
| 3514 | |
Julia Reynolds | ed78379 | 2016-04-08 15:27:35 -0400 | [diff] [blame] | 3515 | if (isAndroidNPlus(caller) && wouldToggleZenMode(ringerMode) |
| 3516 | && !mNm.isNotificationPolicyAccessGrantedForPackage(caller) |
| 3517 | && (flags & AudioManager.FLAG_FROM_KEY) == 0) { |
| 3518 | throw new SecurityException("Not allowed to change Do Not Disturb state"); |
| 3519 | } |
| 3520 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3521 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3522 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3523 | mPrevVolDirection = direction; |
| 3524 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3525 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3528 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3529 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3530 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | } |
| 3532 | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3533 | private boolean shouldZenMuteStream(int streamType) { |
| 3534 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 3535 | return false; |
| 3536 | } |
| 3537 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 3538 | NotificationManager.Policy zenPolicy = mNm.getConsolidatedNotificationPolicy(); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3539 | final boolean muteAlarms = (zenPolicy.priorityCategories |
| 3540 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0; |
| 3541 | final boolean muteMedia = (zenPolicy.priorityCategories |
| 3542 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0; |
| 3543 | final boolean muteSystem = (zenPolicy.priorityCategories |
| 3544 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0; |
| 3545 | final boolean muteNotificationAndRing = ZenModeConfig |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 3546 | .areAllPriorityOnlyNotificationZenSoundsMuted( |
| 3547 | mNm.getConsolidatedNotificationPolicy()); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3548 | return muteAlarms && isAlarm(streamType) |
| 3549 | || muteMedia && isMedia(streamType) |
| 3550 | || muteSystem && isSystem(streamType) |
| 3551 | || muteNotificationAndRing && isNotificationOrRinger(streamType); |
| 3552 | } |
| 3553 | |
| 3554 | private boolean isStreamMutedByRingerOrZenMode(int streamType) { |
| 3555 | return (mRingerAndZenModeMutedStreams & (1 << streamType)) != 0; |
| 3556 | } |
| 3557 | |
| 3558 | /** |
| 3559 | * DND total silence: media and alarms streams are tied to the muted ringer |
| 3560 | * {@link ZenModeHelper.RingerModeDelegate#getRingerModeAffectedStreams(int)} |
| 3561 | * DND alarms only: notification, ringer + system muted (by default tied to muted ringer mode) |
| 3562 | * DND priority only: alarms, media, system streams can be muted separate from ringer based on |
| 3563 | * zenPolicy (this method determines which streams) |
| 3564 | * @return true if changed, else false |
| 3565 | */ |
| 3566 | private boolean updateZenModeAffectedStreams() { |
| 3567 | int zenModeAffectedStreams = 0; |
| 3568 | if (mSystemReady && mNm.getZenMode() == Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 3569 | NotificationManager.Policy zenPolicy = mNm.getConsolidatedNotificationPolicy(); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3570 | if ((zenPolicy.priorityCategories |
| 3571 | & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { |
| 3572 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_ALARM; |
| 3573 | } |
| 3574 | |
| 3575 | if ((zenPolicy.priorityCategories |
| 3576 | & NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA) == 0) { |
| 3577 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_MUSIC; |
| 3578 | } |
| 3579 | |
| 3580 | if ((zenPolicy.priorityCategories |
| 3581 | & NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM) == 0) { |
| 3582 | zenModeAffectedStreams |= 1 << AudioManager.STREAM_SYSTEM; |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | if (mZenModeAffectedStreams != zenModeAffectedStreams) { |
| 3587 | mZenModeAffectedStreams = zenModeAffectedStreams; |
| 3588 | return true; |
| 3589 | } |
| 3590 | |
| 3591 | return false; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 3594 | @GuardedBy("mSettingsLock") |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3595 | private boolean updateRingerAndZenModeAffectedStreams() { |
| 3596 | boolean updatedZenModeAffectedStreams = updateZenModeAffectedStreams(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3597 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3598 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3599 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3600 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3601 | UserHandle.USER_CURRENT); |
| 3602 | |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3603 | if (mIsSingleVolume) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3604 | ringerModeAffectedStreams = 0; |
| 3605 | } else if (mRingerModeDelegate != null) { |
| 3606 | ringerModeAffectedStreams = mRingerModeDelegate |
| 3607 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3608 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 3609 | if (mCameraSoundForced) { |
| 3610 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3611 | } else { |
| 3612 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3613 | } |
| 3614 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3615 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3616 | } else { |
| 3617 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3618 | } |
| 3619 | |
| 3620 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3621 | Settings.System.putIntForUser(mContentResolver, |
| 3622 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3623 | ringerModeAffectedStreams, |
| 3624 | UserHandle.USER_CURRENT); |
| 3625 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3626 | return true; |
| 3627 | } |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3628 | return updatedZenModeAffectedStreams; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3629 | } |
| 3630 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3631 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3632 | public boolean isStreamAffectedByMute(int streamType) { |
| 3633 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3634 | } |
| 3635 | |
| 3636 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3637 | switch (direction) { |
| 3638 | case AudioManager.ADJUST_LOWER: |
| 3639 | case AudioManager.ADJUST_RAISE: |
| 3640 | case AudioManager.ADJUST_SAME: |
| 3641 | case AudioManager.ADJUST_MUTE: |
| 3642 | case AudioManager.ADJUST_UNMUTE: |
| 3643 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3644 | break; |
| 3645 | default: |
| 3646 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3647 | } |
| 3648 | } |
| 3649 | |
| 3650 | private void ensureValidStreamType(int streamType) { |
| 3651 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3652 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3653 | } |
| 3654 | } |
| 3655 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3656 | private boolean isMuteAdjust(int adjust) { |
| 3657 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3658 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3659 | } |
| 3660 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3661 | /*package*/ boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3662 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3663 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3664 | TelecomManager telecomManager = |
| 3665 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3666 | |
| 3667 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3668 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3669 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3670 | |
Eric Laurent | da1af76 | 2017-12-15 16:54:35 -0800 | [diff] [blame] | 3671 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION || |
| 3672 | getMode() == AudioManager.MODE_IN_CALL); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3673 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3674 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3675 | /** |
| 3676 | * For code clarity for getActiveStreamType(int) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3677 | * @param delay_ms max time since last stream activity to consider |
| 3678 | * @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] | 3679 | * in the last "delay_ms" ms. |
| 3680 | */ |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3681 | private boolean wasStreamActiveRecently(int stream, int delay_ms) { |
| 3682 | return AudioSystem.isStreamActive(stream, delay_ms) |
| 3683 | || AudioSystem.isStreamActiveRemotely(stream, delay_ms); |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3684 | } |
| 3685 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3686 | private int getActiveStreamType(int suggestedStreamType) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 3687 | if (mIsSingleVolume |
| 3688 | && suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 3689 | return AudioSystem.STREAM_MUSIC; |
| 3690 | } |
| 3691 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3692 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3693 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3694 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3695 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3696 | == AudioSystem.FORCE_BT_SCO) { |
| 3697 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3698 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3699 | } else { |
| 3700 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3701 | return AudioSystem.STREAM_VOICE_CALL; |
| 3702 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3703 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3704 | if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3705 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3706 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 3707 | return AudioSystem.STREAM_RING; |
| 3708 | } else if (wasStreamActiveRecently( |
| 3709 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 3710 | if (DEBUG_VOL) |
| 3711 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 3712 | return AudioSystem.STREAM_NOTIFICATION; |
| 3713 | } else { |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 3714 | if (DEBUG_VOL) { |
| 3715 | Log.v(TAG, "getActiveStreamType: Forcing DEFAULT_VOL_STREAM_NO_PLAYBACK(" |
| 3716 | + DEFAULT_VOL_STREAM_NO_PLAYBACK + ") b/c default"); |
| 3717 | } |
| 3718 | return DEFAULT_VOL_STREAM_NO_PLAYBACK; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3719 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3720 | } else if ( |
| 3721 | wasStreamActiveRecently(AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3722 | if (DEBUG_VOL) |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3723 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION stream active"); |
| 3724 | return AudioSystem.STREAM_NOTIFICATION; |
| 3725 | } else if (wasStreamActiveRecently(AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3726 | if (DEBUG_VOL) |
| 3727 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING stream active"); |
| 3728 | return AudioSystem.STREAM_RING; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3729 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3730 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3731 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3732 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3733 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3734 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3735 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3736 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3737 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3738 | return AudioSystem.STREAM_VOICE_CALL; |
| 3739 | } |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3740 | } else if (AudioSystem.isStreamActive( |
| 3741 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3742 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3743 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3744 | } else if (AudioSystem.isStreamActive( |
| 3745 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3746 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 3747 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3748 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3749 | if (AudioSystem.isStreamActive( |
| 3750 | AudioSystem.STREAM_NOTIFICATION, sStreamOverrideDelayMs)) { |
| 3751 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3752 | return AudioSystem.STREAM_NOTIFICATION; |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 3753 | } else if (AudioSystem.isStreamActive( |
| 3754 | AudioSystem.STREAM_RING, sStreamOverrideDelayMs)) { |
| 3755 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING"); |
| 3756 | return AudioSystem.STREAM_RING; |
| 3757 | } else { |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 3758 | if (DEBUG_VOL) { |
| 3759 | Log.v(TAG, "getActiveStreamType: Forcing DEFAULT_VOL_STREAM_NO_PLAYBACK(" |
| 3760 | + DEFAULT_VOL_STREAM_NO_PLAYBACK + ") b/c default"); |
| 3761 | } |
| 3762 | return DEFAULT_VOL_STREAM_NO_PLAYBACK; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3763 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3764 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3765 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3767 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3768 | + suggestedStreamType); |
| 3769 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3772 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3773 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3774 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3775 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3776 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3777 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3778 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | private void broadcastVibrateSetting(int vibrateType) { |
| 3782 | // Send broadcast |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 3783 | if (mActivityManagerInternal.isSystemReady()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3784 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3785 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3786 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3787 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3792 | /** |
| 3793 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3794 | * Note that the wake lock needs to be released after the message has been handled. |
| 3795 | */ |
| 3796 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3797 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3798 | final long ident = Binder.clearCallingIdentity(); |
| 3799 | // Always acquire the wake lock as AudioService because it is released by the |
| 3800 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3801 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3802 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3803 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3806 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3807 | 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] | 3808 | |
| 3809 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3810 | handler.removeMessages(msg); |
| 3811 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3812 | return; |
| 3813 | } |
Eric Laurent | 3c4636c | 2018-06-13 19:36:42 -0700 | [diff] [blame] | 3814 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3815 | final long time = SystemClock.uptimeMillis() + delay; |
| 3816 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3820 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3821 | == PackageManager.PERMISSION_GRANTED) { |
| 3822 | return true; |
| 3823 | } |
| 3824 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3825 | + Binder.getCallingPid() |
| 3826 | + ", uid=" + Binder.getCallingUid(); |
| 3827 | Log.w(TAG, msg); |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3831 | /*package*/ int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3832 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3833 | if ((device & (device - 1)) != 0) { |
| 3834 | // Multiple device selection is either: |
| 3835 | // - speaker + one other device: give priority to speaker in this case. |
| 3836 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3837 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3838 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3839 | // - 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] | 3840 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3841 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3842 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3843 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3844 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3845 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3846 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3847 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3848 | } else { |
| 3849 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3850 | } |
| 3851 | } |
| 3852 | return device; |
| 3853 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3855 | private int getDevicesForStream(int stream) { |
| 3856 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 3857 | } |
| 3858 | |
| 3859 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 3860 | ensureValidStreamType(stream); |
| 3861 | synchronized (VolumeStreamState.class) { |
| 3862 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | private void observeDevicesForStreams(int skipStream) { |
| 3867 | synchronized (VolumeStreamState.class) { |
| 3868 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 3869 | if (stream != skipStream) { |
| 3870 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3876 | |
| 3877 | /*package*/ void observeDevicesForAllStreams() { |
| 3878 | observeDevicesForStreams(-1); |
Aniket Kumar Lata | 9fbc205 | 2019-01-11 02:02:33 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3881 | /*package*/ static final int CONNECTION_STATE_DISCONNECTED = 0; |
| 3882 | /*package*/ static final int CONNECTION_STATE_CONNECTED = 1; |
| 3883 | /** |
| 3884 | * The states that can be used with AudioService.setWiredDeviceConnectionState() |
| 3885 | * Attention: those values differ from those in BluetoothProfile, follow annotations to |
| 3886 | * distinguish between @ConnectionState and @BtProfileConnectionState |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3887 | */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3888 | @IntDef({ |
| 3889 | CONNECTION_STATE_DISCONNECTED, |
| 3890 | CONNECTION_STATE_CONNECTED, |
| 3891 | }) |
| 3892 | @Retention(RetentionPolicy.SOURCE) |
| 3893 | public @interface ConnectionState {} |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3894 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3895 | /** |
| 3896 | * see AudioManager.setWiredDeviceConnectionState() |
| 3897 | */ |
| 3898 | public void setWiredDeviceConnectionState(int type, |
| 3899 | @ConnectionState int state, String address, String name, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3900 | String caller) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3901 | if (state != CONNECTION_STATE_CONNECTED |
| 3902 | && state != CONNECTION_STATE_DISCONNECTED) { |
| 3903 | throw new IllegalArgumentException("Invalid state " + state); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3904 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3905 | mDeviceBroker.setWiredDeviceConnectionState(type, state, address, name, caller); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3906 | } |
| 3907 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3908 | /** |
| 3909 | * @hide |
| 3910 | * The states that can be used with AudioService.setBluetoothHearingAidDeviceConnectionState() |
| 3911 | * and AudioService.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent() |
| 3912 | */ |
| 3913 | @IntDef({ |
| 3914 | BluetoothProfile.STATE_DISCONNECTED, |
| 3915 | BluetoothProfile.STATE_CONNECTED, |
| 3916 | }) |
| 3917 | @Retention(RetentionPolicy.SOURCE) |
| 3918 | public @interface BtProfileConnectionState {} |
| 3919 | |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3920 | public int setBluetoothHearingAidDeviceConnectionState( |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3921 | @NonNull BluetoothDevice device, @BtProfileConnectionState int state, |
| 3922 | boolean suppressNoisyIntent, int musicDevice) |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3923 | { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3924 | if (device == null) { |
| 3925 | throw new IllegalArgumentException("Illegal null device"); |
Jakub Pawlowski | f9570f3 | 2018-02-21 17:15:12 -0800 | [diff] [blame] | 3926 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3927 | if (state != BluetoothProfile.STATE_CONNECTED |
| 3928 | && state != BluetoothProfile.STATE_DISCONNECTED) { |
| 3929 | throw new IllegalArgumentException("Illegal BluetoothProfile state for device " |
| 3930 | + " (dis)connection, got " + state); |
| 3931 | } |
| 3932 | return mDeviceBroker.setBluetoothHearingAidDeviceConnectionState( |
| 3933 | device, state, suppressNoisyIntent, musicDevice, "AudioService"); |
Jakub Pawlowski | 10c9061 | 2018-02-21 13:28:46 -0800 | [diff] [blame] | 3934 | } |
| 3935 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3936 | /** |
| 3937 | * See AudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent() |
| 3938 | */ |
| 3939 | public int setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent( |
| 3940 | @NonNull BluetoothDevice device, @AudioService.BtProfileConnectionState int state, |
| 3941 | int profile, boolean suppressNoisyIntent, int a2dpVolume) { |
| 3942 | if (device == null) { |
| 3943 | throw new IllegalArgumentException("Illegal null device"); |
| 3944 | } |
| 3945 | if (state != BluetoothProfile.STATE_CONNECTED |
| 3946 | && state != BluetoothProfile.STATE_DISCONNECTED) { |
| 3947 | throw new IllegalArgumentException("Illegal BluetoothProfile state for device " |
| 3948 | + " (dis)connection, got " + state); |
| 3949 | } |
| 3950 | return mDeviceBroker.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(device, state, |
| 3951 | profile, suppressNoisyIntent, a2dpVolume); |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 3952 | } |
| 3953 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3954 | /** |
| 3955 | * See AudioManager.handleBluetoothA2dpDeviceConfigChange() |
| 3956 | * @param device |
| 3957 | */ |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3958 | public void handleBluetoothA2dpDeviceConfigChange(BluetoothDevice device) |
| 3959 | { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3960 | if (device == null) { |
| 3961 | throw new IllegalArgumentException("Illegal null device"); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3962 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3963 | mDeviceBroker.postBluetoothA2dpDeviceConfigChange(device); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 3964 | } |
| 3965 | |
Aniket Kumar Lata | a8439e2 | 2019-01-11 03:59:00 -0800 | [diff] [blame] | 3966 | /** |
| 3967 | * @see AudioManager#handleBluetoothA2dpActiveDeviceChange(BluetoothDevice, int, int, |
| 3968 | * boolean, int) |
| 3969 | */ |
| 3970 | public int handleBluetoothA2dpActiveDeviceChange( |
| 3971 | BluetoothDevice device, int state, int profile, boolean suppressNoisyIntent, |
| 3972 | int a2dpVolume) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3973 | if (device == null) { |
| 3974 | throw new IllegalArgumentException("Illegal null device"); |
| 3975 | } |
Aniket Kumar Lata | a8439e2 | 2019-01-11 03:59:00 -0800 | [diff] [blame] | 3976 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3977 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3978 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3979 | if (state != BluetoothProfile.STATE_CONNECTED |
| 3980 | && state != BluetoothProfile.STATE_DISCONNECTED) { |
| 3981 | throw new IllegalArgumentException("Invalid state " + state); |
Aniket Kumar Lata | a8439e2 | 2019-01-11 03:59:00 -0800 | [diff] [blame] | 3982 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3983 | return mDeviceBroker.handleBluetoothA2dpActiveDeviceChange(device, state, profile, |
| 3984 | suppressNoisyIntent, a2dpVolume); |
Aniket Kumar Lata | a8439e2 | 2019-01-11 03:59:00 -0800 | [diff] [blame] | 3985 | } |
| 3986 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 3987 | private static final int DEVICE_MEDIA_UNMUTED_ON_PLUG = |
| 3988 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
| 3989 | AudioSystem.DEVICE_OUT_LINE | |
| 3990 | AudioSystem.DEVICE_OUT_ALL_A2DP | |
| 3991 | AudioSystem.DEVICE_OUT_ALL_USB | |
| 3992 | AudioSystem.DEVICE_OUT_HDMI; |
| 3993 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 3994 | /*package*/ void postAccessoryPlugMediaUnmute(int newDevice) { |
| 3995 | sendMsg(mAudioHandler, MSG_ACCESSORY_PLUG_MEDIA_UNMUTE, SENDMSG_QUEUE, |
| 3996 | newDevice, 0, null, 0); |
| 3997 | } |
| 3998 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 3999 | private void onAccessoryPlugMediaUnmute(int newDevice) { |
| 4000 | if (DEBUG_VOL) { |
| 4001 | Log.i(TAG, String.format("onAccessoryPlugMediaUnmute newDevice=%d [%s]", |
| 4002 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
| 4003 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4004 | |
| 4005 | if (mNm.getZenMode() != Settings.Global.ZEN_MODE_NO_INTERRUPTIONS |
| 4006 | && (newDevice & DEVICE_MEDIA_UNMUTED_ON_PLUG) != 0 |
| 4007 | && mStreamStates[AudioSystem.STREAM_MUSIC].mIsMuted |
| 4008 | && mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(newDevice) != 0 |
| 4009 | && (newDevice & AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC)) != 0) { |
| 4010 | if (DEBUG_VOL) { |
| 4011 | Log.i(TAG, String.format(" onAccessoryPlugMediaUnmute unmuting device=%d [%s]", |
| 4012 | newDevice, AudioSystem.getOutputDeviceName(newDevice))); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4013 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4014 | mStreamStates[AudioSystem.STREAM_MUSIC].mute(false); |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | /////////////////////////////////////////////////////////////////////////// |
| 4019 | // Inner classes |
| 4020 | /////////////////////////////////////////////////////////////////////////// |
| 4021 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4022 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 4023 | // 1 mScoclient OR mSafeMediaVolumeState |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4024 | // 2 mSetModeLock |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4025 | // 3 mSettingsLock |
| 4026 | // 4 VolumeStreamState.class |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4027 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4028 | private final int mStreamType; |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 4029 | private int mIndexMin; |
| 4030 | private int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4031 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4032 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4033 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4034 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4035 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4036 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4037 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4038 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4039 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4040 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4042 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4043 | |
| 4044 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4045 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 4046 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 4047 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4048 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4049 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4050 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 4051 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4052 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 4053 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4054 | } |
| 4055 | |
| 4056 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 4057 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 4058 | if (devices == mObservedDevices) { |
| 4059 | return devices; |
| 4060 | } |
| 4061 | final int prevDevices = mObservedDevices; |
| 4062 | mObservedDevices = devices; |
| 4063 | if (checkOthers) { |
| 4064 | // one stream's devices have changed, check the others |
| 4065 | observeDevicesForStreams(mStreamType); |
| 4066 | } |
| 4067 | // log base stream changes to the event log |
| 4068 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4069 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 4070 | } |
| 4071 | sendBroadcastToAll(mStreamDevicesChanged |
| 4072 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 4073 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 4074 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4075 | } |
| 4076 | |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4077 | public @Nullable String getSettingNameForDevice(int device) { |
| 4078 | if (!hasValidSettingsName()) { |
| 4079 | return null; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4080 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4081 | final String suffix = AudioSystem.getOutputDeviceName(device); |
| 4082 | if (suffix.isEmpty()) { |
| 4083 | return mVolumeIndexSettingName; |
| 4084 | } |
| 4085 | return mVolumeIndexSettingName + "_" + suffix; |
| 4086 | } |
| 4087 | |
| 4088 | private boolean hasValidSettingsName() { |
| 4089 | return (mVolumeIndexSettingName != null && !mVolumeIndexSettingName.isEmpty()); |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4092 | public void readSettings() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4093 | synchronized (mSettingsLock) { |
| 4094 | synchronized (VolumeStreamState.class) { |
| 4095 | // force maximum volume on all streams if fixed volume property is set |
| 4096 | if (mUseFixedVolume) { |
| 4097 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 4098 | return; |
| 4099 | } |
| 4100 | // do not read system stream volume from settings: this stream is always aliased |
| 4101 | // to another stream type and its volume is never persisted. Values in settings can |
| 4102 | // only be stale values |
| 4103 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 4104 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
| 4105 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4106 | if (mCameraSoundForced) { |
| 4107 | index = mIndexMax; |
| 4108 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4109 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 4110 | return; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4111 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4112 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4113 | } |
| 4114 | synchronized (VolumeStreamState.class) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4115 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 4116 | |
| 4117 | for (int i = 0; remainingDevices != 0; i++) { |
| 4118 | int device = (1 << i); |
| 4119 | if ((device & remainingDevices) == 0) { |
| 4120 | continue; |
| 4121 | } |
| 4122 | remainingDevices &= ~device; |
| 4123 | |
| 4124 | // retrieve current volume for device |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4125 | // if no volume stored for current stream and device, use default volume if default |
| 4126 | // device, continue otherwise |
| 4127 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4128 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4129 | int index; |
| 4130 | if (!hasValidSettingsName()) { |
| 4131 | index = defaultIndex; |
| 4132 | } else { |
| 4133 | String name = getSettingNameForDevice(device); |
| 4134 | index = Settings.System.getIntForUser( |
| 4135 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 4136 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4137 | if (index == -1) { |
| 4138 | continue; |
| 4139 | } |
| 4140 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4141 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4142 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4146 | private int getAbsoluteVolumeIndex(int index) { |
| 4147 | /* Special handling for Bluetooth Absolute Volume scenario |
| 4148 | * If we send full audio gain, some accessories are too loud even at its lowest |
| 4149 | * volume. We are not able to enumerate all such accessories, so here is the |
| 4150 | * workaround from phone side. |
| 4151 | * Pre-scale volume at lowest volume steps 1 2 and 3. |
| 4152 | * For volume step 0, set audio gain to 0 as some accessories won't mute on their end. |
| 4153 | */ |
| 4154 | if (index == 0) { |
| 4155 | // 0% for volume 0 |
| 4156 | index = 0; |
Tomoharu Kasahara | f0fecb1 | 2018-09-06 20:04:38 +0900 | [diff] [blame] | 4157 | } else if (index > 0 && index <= 3) { |
| 4158 | // Pre-scale for volume steps 1 2 and 3 |
| 4159 | index = (int) (mIndexMax * mPrescaleAbsoluteVolume[index - 1]) / 10; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4160 | } else { |
| 4161 | // otherwise, full gain |
Tomoharu Kasahara | f0fecb1 | 2018-09-06 20:04:38 +0900 | [diff] [blame] | 4162 | index = (mIndexMax + 5) / 10; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4163 | } |
| 4164 | return index; |
| 4165 | } |
| 4166 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4167 | // must be called while synchronized VolumeStreamState.class |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4168 | /*package*/ void applyDeviceVolume_syncVSS(int device, boolean isAvrcpAbsVolSupported) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4169 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4170 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4171 | index = 0; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4172 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && isAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4173 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
Liejun Tao | 4565a47 | 2016-01-20 17:52:20 -0600 | [diff] [blame] | 4174 | } else if ((device & mFullVolumeDevices) != 0) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4175 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4176 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4177 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4178 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4179 | index = (getIndex(device) + 5)/10; |
| 4180 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4181 | AudioSystem.setStreamVolumeIndexAS(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4182 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4183 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4184 | public void applyAllVolumes() { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4185 | final boolean isAvrcpAbsVolSupported = mDeviceBroker.isAvrcpAbsoluteVolumeSupported(); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4186 | synchronized (VolumeStreamState.class) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4187 | // apply device specific volumes first |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4188 | int index; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4189 | for (int i = 0; i < mIndexMap.size(); i++) { |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4190 | final int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4191 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4192 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4193 | index = 0; |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4194 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4195 | isAvrcpAbsVolSupported) { |
Liejun Tao | 39fb567 | 2016-03-09 15:52:13 -0600 | [diff] [blame] | 4196 | index = getAbsoluteVolumeIndex((getIndex(device) + 5)/10); |
| 4197 | } else if ((device & mFullVolumeDevices) != 0) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4198 | index = (mIndexMax + 5)/10; |
Jakub Pawlowski | c274fb2 | 2018-04-04 10:15:12 -0700 | [diff] [blame] | 4199 | } else if ((device & AudioSystem.DEVICE_OUT_HEARING_AID) != 0) { |
| 4200 | index = (mIndexMax + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4201 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4202 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4203 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4204 | AudioSystem.setStreamVolumeIndexAS(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4205 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4206 | } |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4207 | // apply default volume last: by convention , default device volume will be used |
| 4208 | // by audio policy manager if no explicit volume is present for a given device type |
| 4209 | if (mIsMuted) { |
| 4210 | index = 0; |
| 4211 | } else { |
| 4212 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 4213 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4214 | AudioSystem.setStreamVolumeIndexAS( |
Eric Laurent | d9ac2be | 2016-04-07 14:04:23 -0700 | [diff] [blame] | 4215 | mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4219 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 4220 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4221 | } |
| 4222 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4223 | public boolean setIndex(int index, int device, String caller) { |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4224 | boolean changed; |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4225 | int oldIndex; |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4226 | synchronized (mSettingsLock) { |
| 4227 | synchronized (VolumeStreamState.class) { |
| 4228 | oldIndex = getIndex(device); |
| 4229 | index = getValidIndex(index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4230 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 4231 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4232 | } |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4233 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4234 | |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4235 | changed = oldIndex != index; |
| 4236 | // Apply change to all streams using this one as alias if: |
| 4237 | // - the index actually changed OR |
| 4238 | // - there is no volume index stored for this device on alias stream. |
| 4239 | // If changing volume of current device, also change volume of current |
| 4240 | // device on aliased stream |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4241 | final boolean isCurrentDevice = (device == getDeviceForStream(mStreamType)); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4242 | final int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4243 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4244 | final VolumeStreamState aliasStreamState = mStreamStates[streamType]; |
| 4245 | if (streamType != mStreamType && |
| 4246 | mStreamVolumeAlias[streamType] == mStreamType && |
| 4247 | (changed || !aliasStreamState.hasIndexForDevice(device))) { |
| 4248 | final int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 4249 | aliasStreamState.setIndex(scaledIndex, device, caller); |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4250 | if (isCurrentDevice) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 4251 | aliasStreamState.setIndex(scaledIndex, |
| 4252 | getDeviceForStream(streamType), caller); |
| 4253 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4254 | } |
| 4255 | } |
Jack He | 6dd78c1 | 2018-02-12 21:00:24 -0800 | [diff] [blame] | 4256 | // Mirror changes in SPEAKER ringtone volume on SCO when |
| 4257 | if (changed && mStreamType == AudioSystem.STREAM_RING |
| 4258 | && device == AudioSystem.DEVICE_OUT_SPEAKER) { |
| 4259 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4260 | int otherDevice = mIndexMap.keyAt(i); |
| 4261 | if ((otherDevice & AudioSystem.DEVICE_OUT_ALL_SCO) != 0) { |
| 4262 | mIndexMap.put(otherDevice, index); |
| 4263 | } |
| 4264 | } |
| 4265 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4268 | if (changed) { |
| 4269 | oldIndex = (oldIndex + 5) / 10; |
| 4270 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4271 | // log base stream changes to the event log |
| 4272 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 4273 | if (caller == null) { |
| 4274 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 4275 | } |
| 4276 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 4277 | caller); |
| 4278 | } |
| 4279 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4280 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 4281 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 4282 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 4283 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 4284 | sendBroadcastToAll(mVolumeChanged); |
| 4285 | } |
| 4286 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4287 | } |
| 4288 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4289 | public int getIndex(int device) { |
| 4290 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4291 | int index = mIndexMap.get(device, -1); |
| 4292 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4293 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4294 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4295 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4296 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4297 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 4298 | } |
| 4299 | |
Eric Laurent | 3fb608e | 2016-11-03 16:27:40 -0700 | [diff] [blame] | 4300 | public boolean hasIndexForDevice(int device) { |
| 4301 | synchronized (VolumeStreamState.class) { |
| 4302 | return (mIndexMap.get(device, -1) != -1); |
| 4303 | } |
| 4304 | } |
| 4305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4306 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4307 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4308 | } |
| 4309 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4310 | public int getMinIndex() { |
| 4311 | return mIndexMin; |
| 4312 | } |
| 4313 | |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4314 | /** |
Jean-Michel Trivi | e05eef8 | 2018-03-08 18:56:34 -0800 | [diff] [blame] | 4315 | * Updates the min/max index values from another stream. Use this when changing the alias |
| 4316 | * for the current stream type. |
| 4317 | * @param sourceStreamType |
| 4318 | */ |
| 4319 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4320 | @GuardedBy("VolumeStreamState.class") |
| 4321 | public void refreshRange(int sourceStreamType) { |
| 4322 | mIndexMin = MIN_STREAM_VOLUME[sourceStreamType] * 10; |
| 4323 | mIndexMax = MAX_STREAM_VOLUME[sourceStreamType] * 10; |
| 4324 | // verify all current volumes are within bounds |
| 4325 | for (int i = 0 ; i < mIndexMap.size(); i++) { |
| 4326 | final int device = mIndexMap.keyAt(i); |
| 4327 | final int index = mIndexMap.valueAt(i); |
| 4328 | mIndexMap.put(device, getValidIndex(index)); |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | /** |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4333 | * Copies all device/index pairs from the given VolumeStreamState after initializing |
| 4334 | * them with the volume for DEVICE_OUT_DEFAULT. No-op if the source VolumeStreamState |
| 4335 | * has the same stream type as this instance. |
| 4336 | * @param srcStream |
| 4337 | * @param caller |
| 4338 | */ |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4339 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4340 | @GuardedBy("VolumeStreamState.class") |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4341 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Jean-Michel Trivi | 170a0ee | 2017-04-14 18:13:26 -0700 | [diff] [blame] | 4342 | if (mStreamType == srcStream.mStreamType) { |
| 4343 | return; |
| 4344 | } |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4345 | int srcStreamType = srcStream.getStreamType(); |
| 4346 | // apply default device volume from source stream to all devices first in case |
| 4347 | // some devices are present in this stream state but not in source stream state |
| 4348 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
| 4349 | index = rescaleIndex(index, srcStreamType, mStreamType); |
| 4350 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4351 | mIndexMap.put(mIndexMap.keyAt(i), index); |
| 4352 | } |
| 4353 | // Now apply actual volume for devices in source stream state |
| 4354 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 4355 | for (int i = 0; i < srcMap.size(); i++) { |
| 4356 | int device = srcMap.keyAt(i); |
| 4357 | index = srcMap.valueAt(i); |
| 4358 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4359 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4360 | setIndex(index, device, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4364 | // must be sync'd on mSettingsLock before VolumeStreamState.class |
| 4365 | @GuardedBy("VolumeStreamState.class") |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4366 | public void setAllIndexesToMax() { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 4367 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4368 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4369 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4372 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4373 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4374 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4375 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4376 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4377 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4378 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4379 | // Set the new mute volume. This propagates the values to |
| 4380 | // the audio system, otherwise the volume won't be changed |
| 4381 | // at the lower level. |
| 4382 | sendMsg(mAudioHandler, |
| 4383 | MSG_SET_ALL_VOLUMES, |
| 4384 | SENDMSG_QUEUE, |
| 4385 | 0, |
| 4386 | 0, |
| 4387 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4389 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4390 | if (changed) { |
| 4391 | // Stream mute changed, fire the intent. |
| 4392 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 4393 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4394 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 4395 | sendBroadcastToAll(intent); |
| 4396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4397 | } |
| 4398 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4399 | public int getStreamType() { |
| 4400 | return mStreamType; |
| 4401 | } |
| 4402 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4403 | public void checkFixedVolumeDevices() { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4404 | final boolean isAvrcpAbsVolSupported = mDeviceBroker.isAvrcpAbsoluteVolumeSupported(); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4405 | synchronized (VolumeStreamState.class) { |
| 4406 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 4407 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4408 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4409 | int device = mIndexMap.keyAt(i); |
| 4410 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 4411 | if (((device & mFullVolumeDevices) != 0) |
| 4412 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4413 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4414 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4415 | applyDeviceVolume_syncVSS(device, isAvrcpAbsVolSupported); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | } |
| 4420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4421 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4422 | if (index < mIndexMin) { |
| 4423 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 4424 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4425 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | return index; |
| 4429 | } |
| 4430 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4431 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4432 | pw.print(" Muted: "); |
| 4433 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4434 | pw.print(" Min: "); |
| 4435 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4436 | pw.print(" Max: "); |
| 4437 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4438 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4439 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4440 | if (i > 0) { |
| 4441 | pw.print(", "); |
| 4442 | } |
| 4443 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4444 | pw.print(Integer.toHexString(device)); |
| 4445 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 4446 | : AudioSystem.getOutputDeviceName(device); |
| 4447 | if (!deviceName.isEmpty()) { |
| 4448 | pw.print(" ("); |
| 4449 | pw.print(deviceName); |
| 4450 | pw.print(")"); |
| 4451 | } |
| 4452 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4453 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4454 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4455 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4456 | pw.println(); |
| 4457 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4458 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4459 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 4460 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 4461 | // (the default device is not returned by getDevicesForStream) |
| 4462 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4463 | if ((devices & device) != 0) { |
| 4464 | if (n++ > 0) { |
| 4465 | pw.print(", "); |
| 4466 | } |
| 4467 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 4468 | } |
| 4469 | i++; |
| 4470 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | /** Thread that handles native AudioSystem control. */ |
| 4475 | private class AudioSystemThread extends Thread { |
| 4476 | AudioSystemThread() { |
| 4477 | super("AudioService"); |
| 4478 | } |
| 4479 | |
| 4480 | @Override |
| 4481 | public void run() { |
| 4482 | // Set this thread up so the handler will work on it |
| 4483 | Looper.prepare(); |
| 4484 | |
| 4485 | synchronized(AudioService.this) { |
| 4486 | mAudioHandler = new AudioHandler(); |
| 4487 | |
| 4488 | // Notify that the handler has been created |
| 4489 | AudioService.this.notify(); |
| 4490 | } |
| 4491 | |
| 4492 | // Listen for volume change requests that are set by VolumePanel |
| 4493 | Looper.loop(); |
| 4494 | } |
| 4495 | } |
| 4496 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4497 | /*package*/ void setDeviceVolume(VolumeStreamState streamState, int device) { |
| 4498 | |
| 4499 | final boolean isAvrcpAbsVolSupported = mDeviceBroker.isAvrcpAbsoluteVolumeSupported(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4500 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4501 | synchronized (VolumeStreamState.class) { |
| 4502 | // Apply volume |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4503 | streamState.applyDeviceVolume_syncVSS(device, isAvrcpAbsVolSupported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4504 | |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4505 | // Apply change to all streams using this one as alias |
| 4506 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4507 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4508 | if (streamType != streamState.mStreamType && |
| 4509 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 4510 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 4511 | // that may have a different device selected |
| 4512 | int streamDevice = getDeviceForStream(streamType); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4513 | if ((device != streamDevice) && isAvrcpAbsVolSupported |
| 4514 | && ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 4515 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device, |
| 4516 | isAvrcpAbsVolSupported); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4517 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4518 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice, |
| 4519 | isAvrcpAbsVolSupported); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4520 | } |
| 4521 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4522 | } |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 4523 | // Post a persist volume msg |
| 4524 | sendMsg(mAudioHandler, |
| 4525 | MSG_PERSIST_VOLUME, |
| 4526 | SENDMSG_QUEUE, |
| 4527 | device, |
| 4528 | 0, |
| 4529 | streamState, |
| 4530 | PERSIST_DELAY); |
| 4531 | |
| 4532 | } |
| 4533 | |
| 4534 | /** Handles internal volume messages in separate volume thread. */ |
| 4535 | private class AudioHandler extends Handler { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4536 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4537 | private void setAllVolumes(VolumeStreamState streamState) { |
| 4538 | |
| 4539 | // Apply volume |
| 4540 | streamState.applyAllVolumes(); |
| 4541 | |
| 4542 | // Apply change to all streams using this one as alias |
| 4543 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4544 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4545 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4546 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4547 | mStreamStates[streamType].applyAllVolumes(); |
| 4548 | } |
| 4549 | } |
| 4550 | } |
| 4551 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4552 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4553 | if (mUseFixedVolume) { |
| 4554 | return; |
| 4555 | } |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 4556 | if (mIsSingleVolume && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4557 | return; |
| 4558 | } |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 4559 | if (streamState.hasValidSettingsName()) { |
| 4560 | System.putIntForUser(mContentResolver, |
| 4561 | streamState.getSettingNameForDevice(device), |
| 4562 | (streamState.getIndex(device) + 5)/ 10, |
| 4563 | UserHandle.USER_CURRENT); |
| 4564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4565 | } |
| 4566 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4567 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4568 | if (mUseFixedVolume) { |
| 4569 | return; |
| 4570 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 4571 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4572 | } |
| 4573 | |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 4574 | private String getSoundEffectFilePath(int effectType) { |
| 4575 | String filePath = Environment.getProductDirectory() + SOUND_EFFECTS_PATH |
| 4576 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 4577 | if (!new File(filePath).isFile()) { |
| 4578 | filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH |
| 4579 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
| 4580 | } |
| 4581 | return filePath; |
| 4582 | } |
| 4583 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4584 | private boolean onLoadSoundEffects() { |
| 4585 | int status; |
| 4586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4587 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4588 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4589 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 4590 | return false; |
| 4591 | } |
| 4592 | |
| 4593 | if (mSoundPool != null) { |
| 4594 | return true; |
| 4595 | } |
| 4596 | |
| 4597 | loadTouchSoundAssets(); |
| 4598 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 4599 | mSoundPool = new SoundPool.Builder() |
| 4600 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 4601 | .setAudioAttributes(new AudioAttributes.Builder() |
| 4602 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 4603 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 4604 | .build()) |
| 4605 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4606 | mSoundPoolCallBack = null; |
| 4607 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 4608 | mSoundPoolListenerThread.start(); |
| 4609 | int attempts = 3; |
| 4610 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 4611 | try { |
| 4612 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4613 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4614 | } catch (InterruptedException e) { |
| 4615 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | if (mSoundPoolCallBack == null) { |
| 4620 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 4621 | if (mSoundPoolLooper != null) { |
| 4622 | mSoundPoolLooper.quit(); |
| 4623 | mSoundPoolLooper = null; |
| 4624 | } |
| 4625 | mSoundPoolListenerThread = null; |
| 4626 | mSoundPool.release(); |
| 4627 | mSoundPool = null; |
| 4628 | return false; |
| 4629 | } |
| 4630 | /* |
| 4631 | * poolId table: The value -1 in this table indicates that corresponding |
| 4632 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 4633 | * Once loaded, the value in poolId is the sample ID and the same |
| 4634 | * sample can be reused for another effect using the same file. |
| 4635 | */ |
| 4636 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4637 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4638 | poolId[fileIdx] = -1; |
| 4639 | } |
| 4640 | /* |
| 4641 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 4642 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 4643 | * this indicates we have a valid sample loaded for this effect. |
| 4644 | */ |
| 4645 | |
| 4646 | int numSamples = 0; |
| 4647 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4648 | // Do not load sample if this effect uses the MediaPlayer |
| 4649 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 4650 | continue; |
| 4651 | } |
| 4652 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 4653 | String filePath = getSoundEffectFilePath(effect); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4654 | int sampleId = mSoundPool.load(filePath, 0); |
| 4655 | if (sampleId <= 0) { |
| 4656 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 4657 | } else { |
| 4658 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 4659 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 4660 | numSamples++; |
| 4661 | } |
| 4662 | } else { |
| 4663 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 4664 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 4665 | } |
| 4666 | } |
| 4667 | // wait for all samples to be loaded |
| 4668 | if (numSamples > 0) { |
| 4669 | mSoundPoolCallBack.setSamples(poolId); |
| 4670 | |
| 4671 | attempts = 3; |
| 4672 | status = 1; |
| 4673 | while ((status == 1) && (attempts-- > 0)) { |
| 4674 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4675 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4676 | status = mSoundPoolCallBack.status(); |
| 4677 | } catch (InterruptedException e) { |
| 4678 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 4679 | } |
| 4680 | } |
| 4681 | } else { |
| 4682 | status = -1; |
| 4683 | } |
| 4684 | |
| 4685 | if (mSoundPoolLooper != null) { |
| 4686 | mSoundPoolLooper.quit(); |
| 4687 | mSoundPoolLooper = null; |
| 4688 | } |
| 4689 | mSoundPoolListenerThread = null; |
| 4690 | if (status != 0) { |
| 4691 | Log.w(TAG, |
| 4692 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4693 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4694 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4695 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | mSoundPool.release(); |
| 4700 | mSoundPool = null; |
| 4701 | } |
| 4702 | } |
| 4703 | return (status == 0); |
| 4704 | } |
| 4705 | |
| 4706 | /** |
| 4707 | * Unloads samples from the sound pool. |
| 4708 | * This method can be called to free some memory when |
| 4709 | * sound effects are disabled. |
| 4710 | */ |
| 4711 | private void onUnloadSoundEffects() { |
| 4712 | synchronized (mSoundEffectsLock) { |
| 4713 | if (mSoundPool == null) { |
| 4714 | return; |
| 4715 | } |
| 4716 | |
| 4717 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4718 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4719 | poolId[fileIdx] = 0; |
| 4720 | } |
| 4721 | |
| 4722 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4723 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4724 | continue; |
| 4725 | } |
| 4726 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4727 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4728 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4729 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4730 | } |
| 4731 | } |
| 4732 | mSoundPool.release(); |
| 4733 | mSoundPool = null; |
| 4734 | } |
| 4735 | } |
| 4736 | |
| 4737 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4738 | synchronized (mSoundEffectsLock) { |
| 4739 | |
| 4740 | onLoadSoundEffects(); |
| 4741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4742 | if (mSoundPool == null) { |
| 4743 | return; |
| 4744 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4745 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4746 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4747 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4748 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4749 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4750 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4751 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4752 | |
| 4753 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4754 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4755 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4756 | } else { |
| 4757 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4758 | try { |
Jaekyun Seok | c31033f | 2018-01-15 14:53:17 +0900 | [diff] [blame] | 4759 | String filePath = getSoundEffectFilePath(effectType); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4760 | mediaPlayer.setDataSource(filePath); |
| 4761 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4762 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4763 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4764 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4765 | public void onCompletion(MediaPlayer mp) { |
| 4766 | cleanupPlayer(mp); |
| 4767 | } |
| 4768 | }); |
| 4769 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4770 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4771 | cleanupPlayer(mp); |
| 4772 | return true; |
| 4773 | } |
| 4774 | }); |
| 4775 | mediaPlayer.start(); |
| 4776 | } catch (IOException ex) { |
| 4777 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4778 | } catch (IllegalArgumentException ex) { |
| 4779 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4780 | } catch (IllegalStateException ex) { |
| 4781 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | private void cleanupPlayer(MediaPlayer mp) { |
| 4788 | if (mp != null) { |
| 4789 | try { |
| 4790 | mp.stop(); |
| 4791 | mp.release(); |
| 4792 | } catch (IllegalStateException ex) { |
| 4793 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4798 | private void onPersistSafeVolumeState(int state) { |
| 4799 | Settings.Global.putInt(mContentResolver, |
| 4800 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4801 | state); |
| 4802 | } |
| 4803 | |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 4804 | private void onNotifyVolumeEvent(@NonNull IAudioPolicyCallback apc, |
| 4805 | @AudioManager.VolumeAdjustment int direction) { |
| 4806 | try { |
| 4807 | apc.notifyVolumeAdjust(direction); |
| 4808 | } catch(Exception e) { |
| 4809 | // nothing we can do about this. Do not log error, too much potential for spam |
| 4810 | } |
| 4811 | } |
| 4812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4813 | @Override |
| 4814 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4815 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4816 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4817 | case MSG_SET_DEVICE_VOLUME: |
| 4818 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4819 | break; |
| 4820 | |
| 4821 | case MSG_SET_ALL_VOLUMES: |
| 4822 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4823 | break; |
| 4824 | |
| 4825 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4826 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4827 | break; |
| 4828 | |
| 4829 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4830 | // note that the value persisted is the current ringer mode, not the |
| 4831 | // 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] | 4832 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4833 | break; |
| 4834 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 4835 | case MSG_AUDIO_SERVER_DIED: |
| 4836 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4837 | break; |
| 4838 | |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 4839 | case MSG_DISPATCH_AUDIO_SERVER_STATE: |
| 4840 | onDispatchAudioServerStateChange(msg.arg1 == 1); |
| 4841 | break; |
| 4842 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4843 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4844 | onUnloadSoundEffects(); |
| 4845 | break; |
| 4846 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4847 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4848 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4849 | // can take several dozens of milliseconds to complete |
| 4850 | boolean loaded = onLoadSoundEffects(); |
| 4851 | if (msg.obj != null) { |
| 4852 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4853 | synchronized (reply) { |
| 4854 | reply.mStatus = loaded ? 0 : -1; |
| 4855 | reply.notify(); |
| 4856 | } |
| 4857 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4858 | break; |
| 4859 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4860 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4861 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4862 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4863 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4864 | case MSG_SET_FORCE_USE: |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4865 | { |
| 4866 | final String eventSource = (String) msg.obj; |
| 4867 | final int useCase = msg.arg1; |
| 4868 | final int config = msg.arg2; |
| 4869 | if (useCase == AudioSystem.FOR_MEDIA) { |
| 4870 | Log.wtf(TAG, "Invalid force use FOR_MEDIA in AudioService from " |
| 4871 | + eventSource); |
| 4872 | break; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4873 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4874 | sForceUseLogger.log( |
| 4875 | new AudioServiceEvents.ForceUseEvent(useCase, config, eventSource)); |
| 4876 | AudioSystem.setForceUse(useCase, config); |
| 4877 | } |
Aniket Kumar Lata | a8439e2 | 2019-01-11 03:59:00 -0800 | [diff] [blame] | 4878 | break; |
| 4879 | |
Jean-Michel Trivi | 92ed7bf | 2017-06-26 19:32:38 -0700 | [diff] [blame] | 4880 | case MSG_DISABLE_AUDIO_FOR_UID: |
| 4881 | mPlaybackMonitor.disableAudioForUid( msg.arg1 == 1 /* disable */, |
| 4882 | msg.arg2 /* uid */); |
| 4883 | mAudioEventWakeLock.release(); |
| 4884 | break; |
| 4885 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4886 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4887 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4888 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4889 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4890 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4891 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4892 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 4893 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4894 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4895 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4896 | onPersistSafeVolumeState(msg.arg1); |
| 4897 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4898 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4899 | case MSG_SYSTEM_READY: |
| 4900 | onSystemReady(); |
| 4901 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4902 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 4903 | case MSG_INDICATE_SYSTEM_READY: |
| 4904 | onIndicateSystemReady(); |
| 4905 | break; |
| 4906 | |
Jean-Michel Trivi | 1ab43f6 | 2017-04-27 15:37:34 -0700 | [diff] [blame] | 4907 | case MSG_ACCESSORY_PLUG_MEDIA_UNMUTE: |
| 4908 | onAccessoryPlugMediaUnmute(msg.arg1); |
| 4909 | break; |
| 4910 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4911 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4912 | final int musicActiveMs = msg.arg1; |
| 4913 | Settings.Secure.putIntForUser(mContentResolver, |
| 4914 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4915 | UserHandle.USER_CURRENT); |
| 4916 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 4917 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4918 | case MSG_UNMUTE_STREAM: |
| 4919 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4920 | break; |
Eric Laurent | c023248 | 2016-03-15 18:19:23 -0700 | [diff] [blame] | 4921 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 4922 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 4923 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 4924 | break; |
Jean-Michel Trivi | 7ed7147 | 2018-02-02 16:52:09 -0800 | [diff] [blame] | 4925 | |
| 4926 | case MSG_NOTIFY_VOL_EVENT: |
| 4927 | onNotifyVolumeEvent((IAudioPolicyCallback) msg.obj, msg.arg1); |
| 4928 | break; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 4929 | |
| 4930 | case MSG_ENABLE_SURROUND_FORMATS: |
| 4931 | onEnableSurroundFormats((ArrayList<Integer>) msg.obj); |
| 4932 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | } |
| 4936 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4937 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4938 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4939 | SettingsObserver() { |
| 4940 | super(new Handler()); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4941 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4942 | Settings.Global.ZEN_MODE), false, this); |
| 4943 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4944 | Settings.Global.ZEN_MODE_CONFIG_ETAG), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4945 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4946 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4947 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4948 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 4949 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4950 | Settings.System.MASTER_MONO), false, this); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4951 | |
| 4952 | mEncodedSurroundMode = Settings.Global.getInt( |
| 4953 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 4954 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 4955 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4956 | Settings.Global.ENCODED_SURROUND_OUTPUT), false, this); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 4957 | |
| 4958 | mEnabledSurroundFormats = Settings.Global.getString( |
| 4959 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS); |
| 4960 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4961 | Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS), false, this); |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 4962 | |
| 4963 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 4964 | Settings.Secure.VOICE_INTERACTION_SERVICE), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | @Override |
| 4968 | public void onChange(boolean selfChange) { |
| 4969 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4970 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4971 | // However there appear to be some missing locks around mRingerAndZenModeMutedStreams |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4972 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4973 | // mRingerAndZenModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4974 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 4975 | if (updateRingerAndZenModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4976 | /* |
| 4977 | * Ensure all stream types that should be affected by ringer mode |
| 4978 | * are in the proper state. |
| 4979 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4980 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4981 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4982 | readDockAudioSettings(mContentResolver); |
Andy Hung | 7b98e9a | 2016-02-25 18:34:50 -0800 | [diff] [blame] | 4983 | updateMasterMono(mContentResolver); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4984 | updateEncodedSurroundOutput(); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 4985 | sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged); |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 4986 | updateAssistantUId(false); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | |
| 4990 | private void updateEncodedSurroundOutput() { |
| 4991 | int newSurroundMode = Settings.Global.getInt( |
| 4992 | mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT, |
| 4993 | Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO); |
| 4994 | // Did it change? |
| 4995 | if (mEncodedSurroundMode != newSurroundMode) { |
| 4996 | // Send to AudioPolicyManager |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 4997 | sendEncodedSurroundMode(newSurroundMode, "SettingsObserver"); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 4998 | mDeviceBroker.toggleHdmiIfConnected_Async(); |
Phil Burk | ed43bf5 | 2016-03-01 17:01:35 -0800 | [diff] [blame] | 4999 | mEncodedSurroundMode = newSurroundMode; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 5000 | mSurroundModeChanged = true; |
| 5001 | } else { |
| 5002 | mSurroundModeChanged = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5003 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5004 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 5005 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5006 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 5007 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 5008 | // address is not used for now, but may be used when multiple a2dp devices are supported |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5009 | mDeviceBroker.setAvrcpAbsoluteVolumeSupported(support); |
| 5010 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | 3e6fb63 | 2018-05-21 09:28:46 -0700 | [diff] [blame] | 5011 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 5012 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 5015 | /** |
| 5016 | * @return true if there is currently a registered dynamic mixing policy that affects media |
| 5017 | */ |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5018 | /*package*/ boolean hasMediaDynamicPolicy() { |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 5019 | synchronized (mAudioPolicies) { |
| 5020 | if (mAudioPolicies.isEmpty()) { |
| 5021 | return false; |
| 5022 | } |
| 5023 | final Collection<AudioPolicyProxy> appColl = mAudioPolicies.values(); |
| 5024 | for (AudioPolicyProxy app : appColl) { |
| 5025 | if (app.hasMixAffectingUsage(AudioAttributes.USAGE_MEDIA)) { |
| 5026 | return true; |
| 5027 | } |
| 5028 | } |
| 5029 | return false; |
| 5030 | } |
| 5031 | } |
| 5032 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5033 | /*package*/ void checkMusicActive(int deviceType, String caller) { |
| 5034 | if ((deviceType & mSafeMediaVolumeDevices) != 0) { |
| 5035 | sendMsg(mAudioHandler, |
| 5036 | MSG_CHECK_MUSIC_ACTIVE, |
| 5037 | SENDMSG_REPLACE, |
| 5038 | 0, |
| 5039 | 0, |
| 5040 | caller, |
| 5041 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5042 | } |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5043 | } |
| 5044 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5045 | /** |
| 5046 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 5047 | */ |
| 5048 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 5049 | @Override |
| 5050 | public void onReceive(Context context, Intent intent) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5051 | final String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 5052 | int outDevice; |
| 5053 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5054 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5055 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5056 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 5057 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 5058 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 5059 | int config; |
| 5060 | switch (dockState) { |
| 5061 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 5062 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 5063 | break; |
| 5064 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 5065 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 5066 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5067 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5068 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5069 | break; |
| 5070 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 5071 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 5072 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5073 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 5074 | default: |
| 5075 | config = AudioSystem.FORCE_NONE; |
| 5076 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5077 | // Low end docks have a menu to enable or disable audio |
| 5078 | // (see mDockAudioMediaEnabled) |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5079 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) |
| 5080 | || ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) |
| 5081 | && (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 5082 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, config, |
| 5083 | "ACTION_DOCK_EVENT intent"); |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5084 | } |
| 5085 | mDockState = dockState; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5086 | } else if (action.equals(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED) |
| 5087 | || action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
| 5088 | mDeviceBroker.receiveBtEvent(intent); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5089 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5090 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5091 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5092 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5093 | AudioSystem.setParameters("screen_state=on"); |
| 5094 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5095 | if (mMonitorRotation) { |
| 5096 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5097 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5098 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5099 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 5100 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5101 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5102 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5103 | if (mUserSwitchedReceived) { |
| 5104 | // attempt to stop music playback for background user except on first user |
| 5105 | // switch (i.e. first boot) |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5106 | mDeviceBroker.broadcastBecomingNoisy(); |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5107 | } |
| 5108 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5109 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5110 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5111 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5112 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5113 | readAudioSettings(true /*userSwitch*/); |
| 5114 | // preserve STREAM_MUSIC volume from one user to the next. |
| 5115 | sendMsg(mAudioHandler, |
| 5116 | MSG_SET_ALL_VOLUMES, |
| 5117 | SENDMSG_QUEUE, |
| 5118 | 0, |
| 5119 | 0, |
| 5120 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5121 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 5122 | // Disable audio recording for the background user/profile |
| 5123 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5124 | if (userId >= 0) { |
| 5125 | // TODO Kill recording streams instead of killing processes holding permission |
| 5126 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 5127 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 5128 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5129 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5130 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 5131 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 5132 | // Enable audio recording for foreground user/profile |
| 5133 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5134 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5135 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 5136 | } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { |
| 5137 | state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 5138 | if (state == BluetoothAdapter.STATE_OFF || |
| 5139 | state == BluetoothAdapter.STATE_TURNING_OFF) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5140 | mDeviceBroker.disconnectAllBluetoothProfiles(); |
Eric Laurent | b70b78a | 2016-01-13 19:16:04 -0800 | [diff] [blame] | 5141 | } |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 5142 | } else if (action.equals(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION) || |
| 5143 | action.equals(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)) { |
| 5144 | handleAudioEffectBroadcast(context, intent); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5145 | } |
| 5146 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5147 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5148 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5149 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 5150 | |
| 5151 | @Override |
| 5152 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 5153 | Bundle prevRestrictions) { |
| 5154 | // Update mic mute state. |
| 5155 | { |
| 5156 | final boolean wasRestricted = |
| 5157 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5158 | final boolean isRestricted = |
| 5159 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5160 | if (wasRestricted != isRestricted) { |
| 5161 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | // Update speaker mute state. |
| 5166 | { |
| 5167 | final boolean wasRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5168 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 5169 | || prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5170 | final boolean isRestricted = |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 5171 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME) |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 5172 | || newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_DEVICE); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5173 | if (wasRestricted != isRestricted) { |
| 5174 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | } // end class AudioServiceUserRestrictionsListener |
| 5179 | |
Marco Nelissen | fb6df0b | 2017-02-15 15:25:24 -0800 | [diff] [blame] | 5180 | private void handleAudioEffectBroadcast(Context context, Intent intent) { |
| 5181 | String target = intent.getPackage(); |
| 5182 | if (target != null) { |
| 5183 | Log.w(TAG, "effect broadcast already targeted to " + target); |
| 5184 | return; |
| 5185 | } |
| 5186 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 5187 | // TODO this should target a user-selected panel |
| 5188 | List<ResolveInfo> ril = context.getPackageManager().queryBroadcastReceivers( |
| 5189 | intent, 0 /* flags */); |
| 5190 | if (ril != null && ril.size() != 0) { |
| 5191 | ResolveInfo ri = ril.get(0); |
| 5192 | if (ri != null && ri.activityInfo != null && ri.activityInfo.packageName != null) { |
| 5193 | intent.setPackage(ri.activityInfo.packageName); |
| 5194 | context.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 5195 | return; |
| 5196 | } |
| 5197 | } |
| 5198 | Log.w(TAG, "couldn't find receiver package for effect intent"); |
| 5199 | } |
| 5200 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5201 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 5202 | PackageManager pm = mContext.getPackageManager(); |
| 5203 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 5204 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 5205 | ComponentName homeActivityName = null; |
| 5206 | if (!oldUser.isManagedProfile()) { |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 5207 | homeActivityName = LocalServices.getService( |
| 5208 | ActivityTaskManagerInternal.class).getHomeActivityForUser(oldUser.id); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5209 | } |
| 5210 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 5211 | List<PackageInfo> packages; |
| 5212 | try { |
| 5213 | packages = AppGlobals.getPackageManager() |
| 5214 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 5215 | } catch (RemoteException e) { |
| 5216 | throw new AndroidRuntimeException(e); |
| 5217 | } |
| 5218 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 5219 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 5220 | // Skip system processes |
| 5221 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 5222 | continue; |
| 5223 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 5224 | // Skip packages that have permission to interact across users |
| 5225 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 5226 | == PackageManager.PERMISSION_GRANTED) { |
| 5227 | continue; |
| 5228 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5229 | if (homeActivityName != null |
| 5230 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 5231 | && pkg.applicationInfo.isSystemApp()) { |
| 5232 | continue; |
| 5233 | } |
| 5234 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5235 | final int uid = pkg.applicationInfo.uid; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5236 | ActivityManager.getService().killUid(UserHandle.getAppId(uid), |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5237 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5238 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 5239 | } catch (RemoteException e) { |
| 5240 | Log.w(TAG, "Error calling killUid", e); |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5245 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5246 | //========================================================================================== |
| 5247 | // Audio Focus |
| 5248 | //========================================================================================== |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 5249 | /** |
| 5250 | * Returns whether a focus request is eligible to force ducking. |
| 5251 | * Will return true if: |
| 5252 | * - the AudioAttributes have a usage of USAGE_ASSISTANCE_ACCESSIBILITY, |
| 5253 | * - the focus request is AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 5254 | * - the associated Bundle has KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING set to true, |
| 5255 | * - the uid of the requester is a known accessibility service or root. |
| 5256 | * @param aa AudioAttributes of the focus request |
| 5257 | * @param uid uid of the focus requester |
| 5258 | * @return true if ducking is to be forced |
| 5259 | */ |
| 5260 | private boolean forceFocusDuckingForAccessibility(@Nullable AudioAttributes aa, |
| 5261 | int request, int uid) { |
| 5262 | if (aa == null || aa.getUsage() != AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY |
| 5263 | || request != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK) { |
| 5264 | return false; |
| 5265 | } |
| 5266 | final Bundle extraInfo = aa.getBundle(); |
| 5267 | if (extraInfo == null || |
| 5268 | !extraInfo.getBoolean(AudioFocusRequest.KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING)) { |
| 5269 | return false; |
| 5270 | } |
| 5271 | if (uid == 0) { |
| 5272 | return true; |
| 5273 | } |
| 5274 | synchronized (mAccessibilityServiceUidsLock) { |
| 5275 | if (mAccessibilityServiceUids != null) { |
| 5276 | int callingUid = Binder.getCallingUid(); |
| 5277 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 5278 | if (mAccessibilityServiceUids[i] == callingUid) { |
| 5279 | return true; |
| 5280 | } |
| 5281 | } |
| 5282 | } |
| 5283 | } |
| 5284 | return false; |
| 5285 | } |
| 5286 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5287 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5288 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 5289 | IAudioPolicyCallback pcb, int sdk) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5290 | // permission checks |
| 5291 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5292 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5293 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 5294 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 5295 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 5296 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5297 | } |
| 5298 | } else { |
| 5299 | // only a registered audio policy can be used to lock focus |
| 5300 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5301 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5302 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5303 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5309 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
Jean-Michel Trivi | 9228af6 | 2018-01-05 17:06:17 -0800 | [diff] [blame] | 5310 | clientId, callingPackageName, flags, sdk, |
| 5311 | forceFocusDuckingForAccessibility(aa, durationHint, Binder.getCallingUid())); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5312 | } |
| 5313 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 5314 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa, |
| 5315 | String callingPackageName) { |
| 5316 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa, callingPackageName); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | public void unregisterAudioFocusClient(String clientId) { |
| 5320 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5321 | } |
| 5322 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 5323 | public int getCurrentAudioFocus() { |
| 5324 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 5325 | } |
| 5326 | |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 5327 | public int getFocusRampTimeMs(int focusGain, AudioAttributes attr) { |
| 5328 | return mMediaFocusControl.getFocusRampTimeMs(focusGain, attr); |
| 5329 | } |
| 5330 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 5331 | //========================================================================================== |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5332 | private boolean readCameraSoundForced() { |
| 5333 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 5334 | mContext.getResources().getBoolean( |
| 5335 | com.android.internal.R.bool.config_camera_sound_forced); |
| 5336 | } |
| 5337 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5338 | //========================================================================================== |
| 5339 | // Device orientation |
| 5340 | //========================================================================================== |
| 5341 | /** |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 5342 | * Handles device configuration changes that may map to a change in rotation. |
| 5343 | * Monitoring rotation is optional, and is defined by the definition and value |
| 5344 | * of the "ro.audio.monitorRotation" system property. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5345 | */ |
| 5346 | private void handleConfigurationChanged(Context context) { |
| 5347 | try { |
Mikhail Naganov | b668bc6 | 2018-02-13 13:46:38 -0800 | [diff] [blame] | 5348 | // reading new configuration "safely" (i.e. under try catch) in case anything |
| 5349 | // goes wrong. |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5350 | Configuration config = context.getResources().getConfiguration(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5351 | sendMsg(mAudioHandler, |
| 5352 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 5353 | SENDMSG_REPLACE, |
| 5354 | 0, |
| 5355 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5356 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5357 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5358 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5359 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5360 | synchronized (mSettingsLock) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 5361 | final boolean cameraSoundForcedChanged = (cameraSoundForced != mCameraSoundForced); |
| 5362 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5363 | if (cameraSoundForcedChanged) { |
Muyuan Li | 1ed6df6 | 2016-06-18 11:16:52 -0700 | [diff] [blame] | 5364 | if (!mIsSingleVolume) { |
Jean-Michel Trivi | 085346a | 2018-02-14 10:48:51 -0800 | [diff] [blame] | 5365 | synchronized (VolumeStreamState.class) { |
| 5366 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 5367 | if (cameraSoundForced) { |
| 5368 | s.setAllIndexesToMax(); |
| 5369 | mRingerModeAffectedStreams &= |
| 5370 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5371 | } else { |
| 5372 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
| 5373 | mRingerModeAffectedStreams |= |
| 5374 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5375 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5376 | } |
| 5377 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5378 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5379 | } |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5380 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_SYSTEM, |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5381 | cameraSoundForced ? |
| 5382 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5383 | "handleConfigurationChanged"); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5384 | sendMsg(mAudioHandler, |
| 5385 | MSG_SET_ALL_VOLUMES, |
| 5386 | SENDMSG_QUEUE, |
| 5387 | 0, |
| 5388 | 0, |
| 5389 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5390 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5391 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5392 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5393 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5394 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5395 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5399 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5400 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5401 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5402 | mRingtonePlayer = player; |
| 5403 | } |
| 5404 | |
| 5405 | @Override |
| 5406 | public IRingtonePlayer getRingtonePlayer() { |
| 5407 | return mRingtonePlayer; |
| 5408 | } |
| 5409 | |
| 5410 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5411 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5412 | return mDeviceBroker.startWatchingRoutes(observer); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5413 | } |
| 5414 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5415 | |
| 5416 | //========================================================================================== |
| 5417 | // Safe media volume management. |
| 5418 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5419 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5420 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5421 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5422 | // property. Platforms with a different limit must set this property accordingly in their |
| 5423 | // overlay. |
| 5424 | //========================================================================================== |
| 5425 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5426 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5427 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5428 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5429 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5430 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5431 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5432 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5433 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5434 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5435 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 5436 | private int mSafeMediaVolumeState; |
| 5437 | private final Object mSafeMediaVolumeStateLock = new Object(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5438 | |
| 5439 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5440 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5441 | private int mSafeMediaVolumeIndex; |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 5442 | // mSafeUsbMediaVolumeDbfs is the cached value of the config_safe_media_volume_usb_mB |
| 5443 | // property, divided by 100.0. |
| 5444 | private float mSafeUsbMediaVolumeDbfs; |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 5445 | // 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] | 5446 | // corresponding to a gain of mSafeUsbMediaVolumeDbfs (defaulting to -37dB) in audio |
| 5447 | // flinger mixer. |
Eric Laurent | 0e5deb3 | 2017-09-01 15:12:42 -0700 | [diff] [blame] | 5448 | // We remove -22 dBs from the theoretical -15dB to account for the EQ + bass boost |
| 5449 | // amplification when both effects are on with all band gains at maximum. |
Eric Laurent | b378a13a | 2017-07-11 14:08:11 -0700 | [diff] [blame] | 5450 | // This level corresponds to a loudness of 85 dB SPL for the warning to be displayed when |
| 5451 | // 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] | 5452 | private int mSafeUsbMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5453 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5454 | /*package*/ final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET |
| 5455 | | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE |
| 5456 | | AudioSystem.DEVICE_OUT_USB_HEADSET; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5457 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5458 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5459 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5460 | private int mMusicActiveMs; |
| 5461 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5462 | 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] | 5463 | 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] | 5464 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5465 | private int safeMediaVolumeIndex(int device) { |
| 5466 | if ((device & mSafeMediaVolumeDevices) == 0) { |
| 5467 | return MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]; |
| 5468 | } |
| 5469 | if (device == AudioSystem.DEVICE_OUT_USB_HEADSET) { |
| 5470 | return mSafeUsbMediaVolumeIndex; |
| 5471 | } else { |
| 5472 | return mSafeMediaVolumeIndex; |
| 5473 | } |
| 5474 | } |
| 5475 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5476 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 5477 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5478 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5479 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5480 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5481 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5482 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5483 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5484 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5485 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5486 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5487 | sendMsg(mAudioHandler, |
| 5488 | MSG_CHECK_MUSIC_ACTIVE, |
| 5489 | SENDMSG_REPLACE, |
| 5490 | 0, |
| 5491 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5492 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5493 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5494 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5495 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5496 | } |
| 5497 | } |
| 5498 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5499 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5500 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5501 | int devices = mSafeMediaVolumeDevices; |
| 5502 | int i = 0; |
| 5503 | |
| 5504 | while (devices != 0) { |
| 5505 | int device = 1 << i++; |
| 5506 | if ((device & devices) == 0) { |
| 5507 | continue; |
| 5508 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5509 | int index = streamState.getIndex(device); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5510 | if (index > safeMediaVolumeIndex(device)) { |
| 5511 | streamState.setIndex(safeMediaVolumeIndex(device), device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5512 | sendMsg(mAudioHandler, |
| 5513 | MSG_SET_DEVICE_VOLUME, |
| 5514 | SENDMSG_QUEUE, |
| 5515 | device, |
| 5516 | 0, |
| 5517 | streamState, |
| 5518 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5519 | } |
| 5520 | devices &= ~device; |
| 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 5525 | synchronized (mSafeMediaVolumeStateLock) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5526 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5527 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5528 | ((device & mSafeMediaVolumeDevices) != 0) && |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5529 | (index > safeMediaVolumeIndex(device))) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5530 | return false; |
| 5531 | } |
| 5532 | return true; |
| 5533 | } |
| 5534 | } |
| 5535 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5536 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5537 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5538 | enforceVolumeController("disable the safe media volume"); |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 5539 | synchronized (mSafeMediaVolumeStateLock) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5540 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5541 | if (mPendingVolumeCommand != null) { |
| 5542 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5543 | mPendingVolumeCommand.mIndex, |
| 5544 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5545 | mPendingVolumeCommand.mDevice, |
| 5546 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5547 | mPendingVolumeCommand = null; |
| 5548 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5552 | //========================================================================================== |
| 5553 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5554 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5555 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5556 | //========================================================================================== |
| 5557 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5558 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5559 | public void onComplete(int status) { |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5560 | synchronized (mHdmiClientLock) { |
| 5561 | if (mHdmiManager != null) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5562 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5563 | // Television devices without CEC service apply software volume on HDMI output |
| 5564 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5565 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5566 | } |
| 5567 | checkAllFixedVolumeDevices(); |
| 5568 | } |
| 5569 | } |
| 5570 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | private final Object mHdmiClientLock = new Object(); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5574 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5575 | // If HDMI-CEC system audio is supported |
| 5576 | private boolean mHdmiSystemAudioSupported = false; |
| 5577 | // Set only when device is tv. |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5578 | @GuardedBy("mHdmiClientLock") |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5579 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5580 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5581 | // cached HdmiControlManager interface |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5582 | @GuardedBy("mHdmiClientLock") |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5583 | private HdmiControlManager mHdmiManager; |
| 5584 | // Set only when device is a set-top box. |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5585 | @GuardedBy("mHdmiClientLock") |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5586 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5587 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5588 | private boolean mHdmiCecSink; |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5589 | // Set only when device is an audio system. |
| 5590 | @GuardedBy("mHdmiClientLock") |
| 5591 | private HdmiAudioSystemClient mHdmiAudioSystemClient; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5592 | |
| 5593 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5594 | |
| 5595 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5596 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5597 | int device = AudioSystem.DEVICE_NONE; |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5598 | synchronized (mHdmiClientLock) { |
| 5599 | if (mHdmiManager != null) { |
Shubang | de72882 | 2018-07-16 16:46:51 -0700 | [diff] [blame] | 5600 | if (mHdmiTvClient == null && mHdmiAudioSystemClient == null) { |
| 5601 | Log.w(TAG, "Only Hdmi-Cec enabled TV or audio system device supports" |
| 5602 | + "system audio mode."); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5603 | return device; |
| 5604 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5605 | if (mHdmiSystemAudioSupported != on) { |
| 5606 | mHdmiSystemAudioSupported = on; |
| 5607 | final int config = on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5608 | AudioSystem.FORCE_NONE; |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5609 | mDeviceBroker.setForceUse_Async(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, config, |
| 5610 | "setHdmiSystemAudioSupported"); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5611 | } |
Shubang | c480a71 | 2018-06-11 18:02:42 -0700 | [diff] [blame] | 5612 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5613 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5614 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5615 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5616 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5617 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5618 | @Override |
| 5619 | public boolean isHdmiSystemAudioSupported() { |
| 5620 | return mHdmiSystemAudioSupported; |
| 5621 | } |
| 5622 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5623 | //========================================================================================== |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5624 | // Accessibility |
| 5625 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 5626 | private void initA11yMonitoring() { |
| 5627 | final AccessibilityManager accessibilityManager = |
| 5628 | (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5629 | updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled()); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 5630 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
Phil Weaver | 26d709f | 2017-04-20 17:19:14 -0700 | [diff] [blame] | 5631 | accessibilityManager.addTouchExplorationStateChangeListener(this, null); |
| 5632 | accessibilityManager.addAccessibilityServicesStateChangeListener(this, null); |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | //--------------------------------------------------------------------------------- |
| 5636 | // A11y: taking touch exploration into account for selecting the default |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5637 | // stream override timeout when adjusting volume |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5638 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5639 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5640 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
Julia Reynolds | d9e96e2 | 2017-11-07 10:33:08 -0500 | [diff] [blame] | 5641 | // - STREAM_RING on phones during this period after a notification stopped |
| 5642 | // - STREAM_MUSIC otherwise |
| 5643 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5644 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
| 5645 | 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] | 5646 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5647 | private static int sStreamOverrideDelayMs; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5648 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5649 | @Override |
| 5650 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5651 | updateDefaultStreamOverrideDelay(enabled); |
| 5652 | } |
| 5653 | |
| 5654 | private void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5655 | if (touchExploreEnabled) { |
| 5656 | sStreamOverrideDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5657 | } else { |
| 5658 | sStreamOverrideDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5659 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5660 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5661 | + " stream override delay is now " + sStreamOverrideDelayMs + " ms"); |
| 5662 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5663 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5664 | //--------------------------------------------------------------------------------- |
| 5665 | // A11y: taking a11y state into account for the handling of a11y prompts volume |
| 5666 | //--------------------------------------------------------------------------------- |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5667 | |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 5668 | private static boolean sIndependentA11yVolume = false; |
| 5669 | |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 5670 | // implementation of AccessibilityServicesStateChangeListener |
| 5671 | @Override |
Phil Weaver | 4cab930 | 2017-03-30 15:27:39 -0700 | [diff] [blame] | 5672 | public void onAccessibilityServicesStateChanged(AccessibilityManager accessibilityManager) { |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 5673 | updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive()); |
| 5674 | } |
| 5675 | |
| 5676 | private void updateA11yVolumeAlias(boolean a11VolEnabled) { |
| 5677 | if (DEBUG_VOL) Log.d(TAG, "Accessibility volume enabled = " + a11VolEnabled); |
| 5678 | if (sIndependentA11yVolume != a11VolEnabled) { |
| 5679 | sIndependentA11yVolume = a11VolEnabled; |
| 5680 | // update the volume mapping scheme |
| 5681 | updateStreamVolumeAlias(true /*updateVolumes*/, TAG); |
| 5682 | // update the volume controller behavior |
| 5683 | mVolumeController.setA11yMode(sIndependentA11yVolume ? |
| 5684 | VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME : |
| 5685 | VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME); |
Jean-Michel Trivi | a53b705 | 2017-04-12 18:27:01 -0700 | [diff] [blame] | 5686 | mVolumeController.postVolumeChanged(AudioManager.STREAM_ACCESSIBILITY, 0); |
Jean-Michel Trivi | cfa5553 | 2017-01-18 11:17:51 -0800 | [diff] [blame] | 5687 | } |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5691 | // Camera shutter sound policy. |
| 5692 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5693 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5694 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5695 | //========================================================================================== |
| 5696 | |
| 5697 | // 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] | 5698 | @GuardedBy("mSettingsLock") |
| 5699 | private boolean mCameraSoundForced; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5700 | |
| 5701 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5702 | public boolean isCameraSoundForced() { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 5703 | synchronized (mSettingsLock) { |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5704 | return mCameraSoundForced; |
| 5705 | } |
| 5706 | } |
| 5707 | |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5708 | //========================================================================================== |
| 5709 | // AudioService logging and dumpsys |
| 5710 | //========================================================================================== |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 5711 | static final int LOG_NB_EVENTS_PHONE_STATE = 20; |
| 5712 | static final int LOG_NB_EVENTS_DEVICE_CONNECTION = 30; |
| 5713 | static final int LOG_NB_EVENTS_FORCE_USE = 20; |
| 5714 | static final int LOG_NB_EVENTS_VOLUME = 40; |
| 5715 | static final int LOG_NB_EVENTS_DYN_POLICY = 10; |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5716 | |
| 5717 | final private AudioEventLogger mModeLogger = new AudioEventLogger(LOG_NB_EVENTS_PHONE_STATE, |
| 5718 | "phone state (logged after successfull call to AudioSystem.setPhoneState(int))"); |
| 5719 | |
Jean-Michel Trivi | 3bf7578 | 2018-07-02 10:48:04 -0700 | [diff] [blame] | 5720 | // logs for wired + A2DP device connections: |
| 5721 | // - wired: logged before onSetWiredDeviceConnectionState() is executed |
| 5722 | // - A2DP: logged at reception of method call |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5723 | /*package*/ static final AudioEventLogger sDeviceLogger = new AudioEventLogger( |
| 5724 | LOG_NB_EVENTS_DEVICE_CONNECTION, "wired/A2DP/hearing aid device connection BLABLI"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5725 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5726 | static final AudioEventLogger sForceUseLogger = new AudioEventLogger( |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5727 | LOG_NB_EVENTS_FORCE_USE, |
| 5728 | "force use (logged before setForceUse() is executed)"); |
| 5729 | |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5730 | static final AudioEventLogger sVolumeLogger = new AudioEventLogger(LOG_NB_EVENTS_VOLUME, |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 5731 | "volume changes (logged when command received by AudioService)"); |
| 5732 | |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 5733 | final private AudioEventLogger mDynPolicyLogger = new AudioEventLogger(LOG_NB_EVENTS_DYN_POLICY, |
| 5734 | "dynamic policy events (logged when command received by AudioService)"); |
| 5735 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5736 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5737 | "SILENT", |
| 5738 | "VIBRATE", |
| 5739 | "NORMAL" |
| 5740 | }; |
| 5741 | |
| 5742 | private void dumpRingerMode(PrintWriter pw) { |
| 5743 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5744 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5745 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5746 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 5747 | dumpRingerModeStreams(pw, "muted", mRingerAndZenModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5748 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5749 | } |
| 5750 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5751 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 5752 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 5753 | pw.print(Integer.toHexString(streams)); |
| 5754 | if (streams != 0) { |
| 5755 | pw.print(" ("); |
| 5756 | boolean first = true; |
| 5757 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 5758 | final int stream = (1 << i); |
| 5759 | if ((streams & stream) != 0) { |
| 5760 | if (!first) pw.print(','); |
| 5761 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 5762 | streams &= ~stream; |
| 5763 | first = false; |
| 5764 | } |
| 5765 | } |
| 5766 | if (streams != 0) { |
| 5767 | if (!first) pw.print(','); |
| 5768 | pw.print(streams); |
| 5769 | } |
| 5770 | pw.print(')'); |
| 5771 | } |
| 5772 | pw.println(); |
| 5773 | } |
| 5774 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5775 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5776 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5777 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5778 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5779 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5780 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5781 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5782 | pw.println("\nAudio routes:"); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5783 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString( |
| 5784 | mDeviceBroker.getCurAudioRoutes().mainType)); |
| 5785 | pw.print(" mBluetoothName="); pw.println(mDeviceBroker.getCurAudioRoutes().bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5786 | |
| 5787 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5788 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5789 | pw.print(" mSafeMediaVolumeState="); |
| 5790 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5791 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 5792 | pw.print(" mSafeUsbMediaVolumeIndex="); pw.println(mSafeUsbMediaVolumeIndex); |
John Muir | 8b8bddd | 2018-02-16 14:29:14 -0800 | [diff] [blame] | 5793 | pw.print(" mSafeUsbMediaVolumeDbfs="); pw.println(mSafeUsbMediaVolumeDbfs); |
Jean-Michel Trivi | 7592b98 | 2017-02-01 15:12:15 -0800 | [diff] [blame] | 5794 | pw.print(" sIndependentA11yVolume="); pw.println(sIndependentA11yVolume); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5795 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5796 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5797 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5798 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5799 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5800 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5801 | pw.print(" mAvrcpAbsVolSupported="); |
| 5802 | pw.println(mDeviceBroker.isAvrcpAbsoluteVolumeSupported()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5803 | |
| 5804 | dumpAudioPolicies(pw); |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 5805 | mDynPolicyLogger.dump(pw); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 5806 | |
| 5807 | mPlaybackMonitor.dump(pw); |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 5808 | |
| 5809 | mRecordMonitor.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5810 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 5811 | pw.println("\n"); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5812 | pw.println("\nEvent logs:"); |
| 5813 | mModeLogger.dump(pw); |
| 5814 | pw.println("\n"); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5815 | sDeviceLogger.dump(pw); |
Jean-Michel Trivi | 011f39e | 2017-08-19 18:08:06 -0700 | [diff] [blame] | 5816 | pw.println("\n"); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5817 | sForceUseLogger.dump(pw); |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 5818 | pw.println("\n"); |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 5819 | sVolumeLogger.dump(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5820 | } |
| 5821 | |
hyomin.oh | d0446dc | 2018-10-18 13:58:27 +0900 | [diff] [blame] | 5822 | private static String safeMediaVolumeStateToString(int state) { |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5823 | switch(state) { |
| 5824 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5825 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5826 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5827 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5828 | } |
| 5829 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5830 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5831 | |
| 5832 | // Inform AudioFlinger of our device's low RAM attribute |
| 5833 | private static void readAndSetLowRamDevice() |
| 5834 | { |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 5835 | boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic(); |
| 5836 | long totalMemory = 1024 * 1024 * 1024; // 1GB is the default if ActivityManager fails. |
| 5837 | |
| 5838 | try { |
| 5839 | final ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); |
| 5840 | ActivityManager.getService().getMemoryInfo(info); |
| 5841 | totalMemory = info.totalMem; |
| 5842 | } catch (RemoteException e) { |
| 5843 | Log.w(TAG, "Cannot obtain MemoryInfo from ActivityManager, assume low memory device"); |
| 5844 | isLowRamDevice = true; |
| 5845 | } |
| 5846 | |
| 5847 | final int status = AudioSystem.setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5848 | if (status != 0) { |
| 5849 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5850 | } |
| 5851 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5852 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5853 | private void enforceVolumeController(String action) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5854 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5855 | "Only SystemUI can " + action); |
| 5856 | } |
| 5857 | |
| 5858 | @Override |
| 5859 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5860 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5861 | |
| 5862 | // return early if things are not actually changing |
| 5863 | if (mVolumeController.isSameBinder(controller)) { |
| 5864 | return; |
| 5865 | } |
| 5866 | |
| 5867 | // dismiss the old volume controller |
| 5868 | mVolumeController.postDismiss(); |
| 5869 | if (controller != null) { |
| 5870 | // we are about to register a new controller, listen for its death |
| 5871 | try { |
| 5872 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5873 | @Override |
| 5874 | public void binderDied() { |
| 5875 | if (mVolumeController.isSameBinder(controller)) { |
| 5876 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5877 | setVolumeController(null); |
| 5878 | } |
| 5879 | } |
| 5880 | }, 0); |
| 5881 | } catch (RemoteException e) { |
| 5882 | // noop |
| 5883 | } |
| 5884 | } |
| 5885 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5886 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5887 | } |
| 5888 | |
| 5889 | @Override |
| 5890 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5891 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5892 | |
| 5893 | // return early if the controller is not current |
| 5894 | if (!mVolumeController.isSameBinder(controller)) { |
| 5895 | return; |
| 5896 | } |
| 5897 | |
| 5898 | mVolumeController.setVisible(visible); |
| 5899 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5900 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5901 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5902 | @Override |
| 5903 | public void setVolumePolicy(VolumePolicy policy) { |
| 5904 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5905 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5906 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5907 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5908 | } |
| 5909 | } |
| 5910 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5911 | public static class VolumeController { |
| 5912 | private static final String TAG = "VolumeController"; |
| 5913 | |
| 5914 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5915 | private boolean mVisible; |
| 5916 | private long mNextLongPress; |
| 5917 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5918 | |
| 5919 | public void setController(IVolumeController controller) { |
| 5920 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5921 | mVisible = false; |
| 5922 | } |
| 5923 | |
| 5924 | public void loadSettings(ContentResolver cr) { |
| 5925 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5926 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5927 | } |
| 5928 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5929 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5930 | if (isMute) { |
| 5931 | return false; |
| 5932 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5933 | boolean suppress = false; |
Julia Reynolds | eb0ce47 | 2018-05-04 15:34:55 -0400 | [diff] [blame] | 5934 | if (resolvedStream == DEFAULT_VOL_STREAM_NO_PLAYBACK && mController != null) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5935 | final long now = SystemClock.uptimeMillis(); |
| 5936 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5937 | // ui will become visible |
| 5938 | if (mNextLongPress < now) { |
| 5939 | mNextLongPress = now + mLongPressTimeout; |
| 5940 | } |
| 5941 | suppress = true; |
| 5942 | } else if (mNextLongPress > 0) { // in a long-press |
| 5943 | if (now > mNextLongPress) { |
| 5944 | // long press triggered, no more suppression |
| 5945 | mNextLongPress = 0; |
| 5946 | } else { |
| 5947 | // keep suppressing until the long press triggers |
| 5948 | suppress = true; |
| 5949 | } |
| 5950 | } |
| 5951 | } |
| 5952 | return suppress; |
| 5953 | } |
| 5954 | |
| 5955 | public void setVisible(boolean visible) { |
| 5956 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | public boolean isSameBinder(IVolumeController controller) { |
| 5960 | return Objects.equals(asBinder(), binder(controller)); |
| 5961 | } |
| 5962 | |
| 5963 | public IBinder asBinder() { |
| 5964 | return binder(mController); |
| 5965 | } |
| 5966 | |
| 5967 | private static IBinder binder(IVolumeController controller) { |
| 5968 | return controller == null ? null : controller.asBinder(); |
| 5969 | } |
| 5970 | |
| 5971 | @Override |
| 5972 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5973 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5977 | if (mController == null) |
| 5978 | return; |
| 5979 | try { |
| 5980 | mController.displaySafeVolumeWarning(flags); |
| 5981 | } catch (RemoteException e) { |
| 5982 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | public void postVolumeChanged(int streamType, int flags) { |
| 5987 | if (mController == null) |
| 5988 | return; |
| 5989 | try { |
| 5990 | mController.volumeChanged(streamType, flags); |
| 5991 | } catch (RemoteException e) { |
| 5992 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5993 | } |
| 5994 | } |
| 5995 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5996 | public void postMasterMuteChanged(int flags) { |
| 5997 | if (mController == null) |
| 5998 | return; |
| 5999 | try { |
| 6000 | mController.masterMuteChanged(flags); |
| 6001 | } catch (RemoteException e) { |
| 6002 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | public void setLayoutDirection(int layoutDirection) { |
| 6007 | if (mController == null) |
| 6008 | return; |
| 6009 | try { |
| 6010 | mController.setLayoutDirection(layoutDirection); |
| 6011 | } catch (RemoteException e) { |
| 6012 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | public void postDismiss() { |
| 6017 | if (mController == null) |
| 6018 | return; |
| 6019 | try { |
| 6020 | mController.dismiss(); |
| 6021 | } catch (RemoteException e) { |
| 6022 | Log.w(TAG, "Error calling dismiss", e); |
| 6023 | } |
| 6024 | } |
Jean-Michel Trivi | ac48767 | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 6025 | |
| 6026 | public void setA11yMode(int a11yMode) { |
| 6027 | if (mController == null) |
| 6028 | return; |
| 6029 | try { |
| 6030 | mController.setA11yMode(a11yMode); |
| 6031 | } catch (RemoteException e) { |
| 6032 | Log.w(TAG, "Error calling setA11Mode", e); |
| 6033 | } |
| 6034 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 6035 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6036 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6037 | /** |
| 6038 | * Interface for system components to get some extra functionality through |
| 6039 | * LocalServices. |
| 6040 | */ |
| 6041 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6042 | @Override |
| 6043 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 6044 | mRingerModeDelegate = delegate; |
| 6045 | if (mRingerModeDelegate != null) { |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6046 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 6047 | updateRingerAndZenModeAffectedStreams(); |
Jean-Michel Trivi | 8719a1d | 2017-11-06 11:52:51 -0800 | [diff] [blame] | 6048 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6049 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 6050 | } |
| 6051 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6052 | |
| 6053 | @Override |
| 6054 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 6055 | String callingPackage, int uid) { |
| 6056 | // direction and stream type swap here because the public |
| 6057 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6058 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 6059 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6060 | } |
| 6061 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6062 | @Override |
| 6063 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 6064 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6065 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 6066 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | @Override |
| 6070 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 6071 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6072 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6073 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 6074 | |
| 6075 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6076 | public int getRingerModeInternal() { |
| 6077 | return AudioService.this.getRingerModeInternal(); |
| 6078 | } |
| 6079 | |
| 6080 | @Override |
| 6081 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 6082 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 6083 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6084 | |
| 6085 | @Override |
Mike Digman | 5527286 | 2018-02-20 14:35:17 -0800 | [diff] [blame] | 6086 | public void silenceRingerModeInternal(String caller) { |
| 6087 | AudioService.this.silenceRingerModeInternal(caller); |
| 6088 | } |
| 6089 | |
| 6090 | @Override |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6091 | public void updateRingerModeAffectedStreamsInternal() { |
| 6092 | synchronized (mSettingsLock) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 6093 | if (updateRingerAndZenModeAffectedStreams()) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6094 | setRingerModeInt(getRingerModeInternal(), false); |
| 6095 | } |
| 6096 | } |
| 6097 | } |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 6098 | |
| 6099 | @Override |
| 6100 | public void setAccessibilityServiceUids(IntArray uids) { |
| 6101 | synchronized (mAccessibilityServiceUidsLock) { |
| 6102 | if (uids.size() == 0) { |
| 6103 | mAccessibilityServiceUids = null; |
| 6104 | } else { |
| 6105 | boolean changed = (mAccessibilityServiceUids == null) |
| 6106 | || (mAccessibilityServiceUids.length != uids.size()); |
| 6107 | if (!changed) { |
| 6108 | for (int i = 0; i < mAccessibilityServiceUids.length; i++) { |
| 6109 | if (uids.get(i) != mAccessibilityServiceUids[i]) { |
| 6110 | changed = true; |
| 6111 | break; |
| 6112 | } |
| 6113 | } |
| 6114 | } |
| 6115 | if (changed) { |
| 6116 | mAccessibilityServiceUids = uids.toArray(); |
| 6117 | } |
| 6118 | } |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 6119 | AudioSystem.setA11yServicesUids(mAccessibilityServiceUids); |
Phil Weaver | f1a9aff | 2017-03-23 17:21:29 -0700 | [diff] [blame] | 6120 | } |
| 6121 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6122 | } |
| 6123 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6124 | //========================================================================================== |
| 6125 | // Audio policy management |
| 6126 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6127 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6128 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6129 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 6130 | |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6131 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6132 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6133 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6134 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6135 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6136 | if (!hasPermissionForPolicy) { |
| 6137 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 6138 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6139 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6140 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6141 | |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 6142 | mDynPolicyLogger.log((new AudioEventLogger.StringEvent("registerAudioPolicy for " |
| 6143 | + pcb.asBinder() + " with config:" + policyConfig)).printLog(TAG)); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6144 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6145 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6146 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6147 | Slog.e(TAG, "Cannot re-register policy"); |
| 6148 | return null; |
| 6149 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6150 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6151 | isFocusPolicy, isVolumeController); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6152 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 6153 | regId = app.getRegistrationId(); |
| 6154 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6155 | } catch (RemoteException e) { |
| 6156 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6157 | 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] | 6158 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6159 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6160 | } |
| 6161 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6162 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6163 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6164 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6165 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 12a8676 | 2018-04-24 16:57:49 -0700 | [diff] [blame] | 6166 | mDynPolicyLogger.log((new AudioEventLogger.StringEvent("unregisterAudioPolicyAsync for " |
| 6167 | + pcb.asBinder()).printLog(TAG))); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6168 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6169 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6170 | if (app == null) { |
| 6171 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 6172 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6173 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6174 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6175 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6176 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6177 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6178 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6179 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6182 | /** |
| 6183 | * Checks whether caller has MODIFY_AUDIO_ROUTING permission, and the policy is registered. |
| 6184 | * @param errorMsg log warning if permission check failed. |
| 6185 | * @return null if the operation on the audio mixes should be cancelled. |
| 6186 | */ |
| 6187 | @GuardedBy("mAudioPolicies") |
| 6188 | private AudioPolicyProxy checkUpdateForPolicy(IAudioPolicyCallback pcb, String errorMsg) { |
| 6189 | // permission check |
| 6190 | final boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6191 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6192 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6193 | if (!hasPermissionForPolicy) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6194 | Slog.w(TAG, errorMsg + " for pid " + |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6195 | + Binder.getCallingPid() + " / uid " |
| 6196 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6197 | return null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6198 | } |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6199 | // policy registered? |
| 6200 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 6201 | if (app == null) { |
| 6202 | Slog.w(TAG, errorMsg + " for pid " + |
| 6203 | + Binder.getCallingPid() + " / uid " |
| 6204 | + Binder.getCallingUid() + ", unregistered policy"); |
| 6205 | return null; |
| 6206 | } |
| 6207 | return app; |
| 6208 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6209 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6210 | public int addMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 6211 | if (DEBUG_AP) { Log.d(TAG, "addMixForPolicy for " + pcb.asBinder() |
| 6212 | + " with config:" + policyConfig); } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6213 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6214 | final AudioPolicyProxy app = |
| 6215 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 6216 | if (app == null){ |
| 6217 | return AudioManager.ERROR; |
| 6218 | } |
| 6219 | app.addMixes(policyConfig.getMixes()); |
| 6220 | } |
| 6221 | return AudioManager.SUCCESS; |
| 6222 | } |
| 6223 | |
| 6224 | public int removeMixForPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb) { |
| 6225 | if (DEBUG_AP) { Log.d(TAG, "removeMixForPolicy for " + pcb.asBinder() |
| 6226 | + " with config:" + policyConfig); } |
| 6227 | synchronized (mAudioPolicies) { |
| 6228 | final AudioPolicyProxy app = |
| 6229 | checkUpdateForPolicy(pcb, "Cannot add AudioMix in audio policy"); |
| 6230 | if (app == null) { |
| 6231 | return AudioManager.ERROR; |
| 6232 | } |
| 6233 | app.removeMixes(policyConfig.getMixes()); |
| 6234 | } |
| 6235 | return AudioManager.SUCCESS; |
| 6236 | } |
| 6237 | |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6238 | /** see AudioPolicy.setUidDeviceAffinity() */ |
| 6239 | public int setUidDeviceAffinity(IAudioPolicyCallback pcb, int uid, |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6240 | @NonNull int[] deviceTypes, @NonNull String[] deviceAddresses) { |
| 6241 | if (DEBUG_AP) { |
| 6242 | Log.d(TAG, "setUidDeviceAffinity for " + pcb.asBinder() + " uid:" + uid); |
| 6243 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6244 | synchronized (mAudioPolicies) { |
| 6245 | final AudioPolicyProxy app = |
| 6246 | checkUpdateForPolicy(pcb, "Cannot change device affinity in audio policy"); |
| 6247 | if (app == null) { |
| 6248 | return AudioManager.ERROR; |
| 6249 | } |
| 6250 | if (!app.hasMixRoutedToDevices(deviceTypes, deviceAddresses)) { |
| 6251 | return AudioManager.ERROR; |
| 6252 | } |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6253 | return app.setUidDeviceAffinities(uid, deviceTypes, deviceAddresses); |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6254 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6255 | } |
| 6256 | |
| 6257 | /** see AudioPolicy.removeUidDeviceAffinity() */ |
| 6258 | public int removeUidDeviceAffinity(IAudioPolicyCallback pcb, int uid) { |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6259 | if (DEBUG_AP) { |
| 6260 | Log.d(TAG, "removeUidDeviceAffinity for " + pcb.asBinder() + " uid:" + uid); |
| 6261 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6262 | synchronized (mAudioPolicies) { |
| 6263 | final AudioPolicyProxy app = |
| 6264 | checkUpdateForPolicy(pcb, "Cannot remove device affinity in audio policy"); |
| 6265 | if (app == null) { |
| 6266 | return AudioManager.ERROR; |
| 6267 | } |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6268 | return app.removeUidDeviceAffinities(uid); |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6269 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6270 | } |
| 6271 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6272 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 6273 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 6274 | + " policy " + pcb.asBinder()); |
| 6275 | synchronized (mAudioPolicies) { |
| 6276 | final AudioPolicyProxy app = |
| 6277 | checkUpdateForPolicy(pcb, "Cannot change audio policy focus properties"); |
| 6278 | if (app == null){ |
| 6279 | return AudioManager.ERROR; |
| 6280 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6281 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6282 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 6283 | return AudioManager.ERROR; |
| 6284 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6285 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6286 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6287 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6288 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6289 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 6290 | return AudioManager.ERROR; |
| 6291 | } |
| 6292 | } |
| 6293 | } |
| 6294 | app.mFocusDuckBehavior = duckingBehavior; |
| 6295 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 6296 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 6297 | } |
| 6298 | return AudioManager.SUCCESS; |
| 6299 | } |
| 6300 | |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6301 | private final Object mExtVolumeControllerLock = new Object(); |
| 6302 | private IAudioPolicyCallback mExtVolumeController; |
| 6303 | private void setExtVolumeController(IAudioPolicyCallback apc) { |
| 6304 | if (!mContext.getResources().getBoolean( |
| 6305 | com.android.internal.R.bool.config_handleVolumeKeysInWindowManager)) { |
| 6306 | Log.e(TAG, "Cannot set external volume controller: device not set for volume keys" + |
| 6307 | " handled in PhoneWindowManager"); |
| 6308 | return; |
| 6309 | } |
| 6310 | synchronized (mExtVolumeControllerLock) { |
| 6311 | if (mExtVolumeController != null && !mExtVolumeController.asBinder().pingBinder()) { |
| 6312 | Log.e(TAG, "Cannot set external volume controller: existing controller"); |
| 6313 | } |
| 6314 | mExtVolumeController = apc; |
| 6315 | } |
| 6316 | } |
| 6317 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6318 | private void dumpAudioPolicies(PrintWriter pw) { |
| 6319 | pw.println("\nAudio policies:"); |
| 6320 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6321 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6322 | pw.println(policy.toLogFriendlyString()); |
| 6323 | } |
| 6324 | } |
| 6325 | } |
| 6326 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6327 | //====================== |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6328 | // Audio policy callbacks from AudioSystem for dynamic policies |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6329 | //====================== |
| 6330 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 6331 | new AudioSystem.DynamicPolicyCallback() { |
| 6332 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 6333 | if (!TextUtils.isEmpty(regId)) { |
| 6334 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 6335 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 6336 | } |
| 6337 | } |
| 6338 | }; |
| 6339 | |
| 6340 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 6341 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 6342 | synchronized (mAudioPolicies) { |
| 6343 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 6344 | for (AudioMix mix : policy.getMixes()) { |
| 6345 | if (mix.getRegistration().equals(regId)) { |
| 6346 | try { |
| 6347 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 6348 | } catch (RemoteException e) { |
| 6349 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 6350 | + policy.mPolicyCallback.asBinder(), e); |
| 6351 | } |
| 6352 | return; |
| 6353 | } |
| 6354 | } |
| 6355 | } |
| 6356 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6357 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6358 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6359 | //====================== |
| 6360 | // Audio policy callbacks from AudioSystem for recording configuration updates |
| 6361 | //====================== |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 6362 | private final RecordingActivityMonitor mRecordMonitor; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6363 | |
| 6364 | public void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 6365 | final boolean isPrivileged = |
| 6366 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6367 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6368 | mRecordMonitor.registerRecordingCallback(rcdb, isPrivileged); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 6369 | } |
| 6370 | |
| 6371 | public void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 6372 | mRecordMonitor.unregisterRecordingCallback(rcdb); |
| 6373 | } |
| 6374 | |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 6375 | public List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 6376 | final boolean isPrivileged = |
| 6377 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6378 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6379 | return mRecordMonitor.getActiveRecordingConfigurations(isPrivileged); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6380 | } |
| 6381 | |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 6382 | public void disableRingtoneSync(final int userId) { |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 6383 | final int callingUserId = UserHandle.getCallingUserId(); |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 6384 | if (callingUserId != userId) { |
| 6385 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 6386 | "disable sound settings syncing for another profile"); |
| 6387 | } |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 6388 | final long token = Binder.clearCallingIdentity(); |
| 6389 | try { |
Robin Lee | 7af9a74 | 2017-02-20 14:47:30 +0000 | [diff] [blame] | 6390 | // Disable the sync setting so the profile uses its own sound settings. |
| 6391 | Settings.Secure.putIntForUser(mContentResolver, Settings.Secure.SYNC_PARENT_SOUNDS, |
| 6392 | 0 /* false */, userId); |
Andre Lago | 7bdc6d8 | 2016-09-22 18:00:41 +0100 | [diff] [blame] | 6393 | } finally { |
| 6394 | Binder.restoreCallingIdentity(token); |
| 6395 | } |
| 6396 | } |
| 6397 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6398 | //====================== |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6399 | // Audio playback notification |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6400 | //====================== |
Eric Laurent | e5a351c | 2017-09-27 20:11:51 -0700 | [diff] [blame] | 6401 | private final PlaybackActivityMonitor mPlaybackMonitor; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6402 | |
| 6403 | public void registerPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6404 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 6405 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6406 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6407 | mPlaybackMonitor.registerPlaybackCallback(pcdb, isPrivileged); |
| 6408 | } |
| 6409 | |
| 6410 | public void unregisterPlaybackCallback(IPlaybackConfigDispatcher pcdb) { |
| 6411 | mPlaybackMonitor.unregisterPlaybackCallback(pcdb); |
| 6412 | } |
| 6413 | |
| 6414 | public List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6415 | final boolean isPrivileged = |
Jaewan Kim | 92dea33 | 2017-02-02 11:52:08 +0900 | [diff] [blame] | 6416 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6417 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6418 | return mPlaybackMonitor.getActivePlaybackConfigurations(isPrivileged); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6419 | } |
| 6420 | |
Jean-Michel Trivi | 44a8f53 | 2017-01-02 14:36:43 -0800 | [diff] [blame] | 6421 | public int trackPlayer(PlayerBase.PlayerIdCard pic) { |
| 6422 | return mPlaybackMonitor.trackPlayer(pic); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | public void playerAttributes(int piid, AudioAttributes attr) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6426 | mPlaybackMonitor.playerAttributes(piid, attr, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | public void playerEvent(int piid, int event) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6430 | mPlaybackMonitor.playerEvent(piid, event, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6431 | } |
| 6432 | |
Jean-Michel Trivi | 3120059d | 2017-08-28 12:40:55 -0700 | [diff] [blame] | 6433 | public void playerHasOpPlayAudio(int piid, boolean hasOpPlayAudio) { |
| 6434 | mPlaybackMonitor.playerHasOpPlayAudio(piid, hasOpPlayAudio, Binder.getCallingUid()); |
| 6435 | } |
| 6436 | |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6437 | public void releasePlayer(int piid) { |
Jean-Michel Trivi | 46e310b | 2017-01-04 15:58:02 -0800 | [diff] [blame] | 6438 | mPlaybackMonitor.releasePlayer(piid, Binder.getCallingUid()); |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | //====================== |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 6442 | // Audio device management |
| 6443 | //====================== |
| 6444 | private final AudioDeviceBroker mDeviceBroker; |
| 6445 | |
| 6446 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6447 | // Audio policy proxy |
| 6448 | //====================== |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6449 | private static final class AudioDeviceArray { |
| 6450 | final @NonNull int[] mDeviceTypes; |
| 6451 | final @NonNull String[] mDeviceAddresses; |
| 6452 | AudioDeviceArray(@NonNull int[] types, @NonNull String[] addresses) { |
| 6453 | mDeviceTypes = types; |
| 6454 | mDeviceAddresses = addresses; |
| 6455 | } |
| 6456 | } |
| 6457 | |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6458 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6459 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 6460 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6461 | */ |
| 6462 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6463 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6464 | final IAudioPolicyCallback mPolicyCallback; |
| 6465 | final boolean mHasFocusListener; |
| 6466 | final boolean mIsVolumeController; |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6467 | final HashMap<Integer, AudioDeviceArray> mUidDeviceAffinities = |
| 6468 | new HashMap<Integer, AudioDeviceArray>(); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6469 | /** |
| 6470 | * Audio focus ducking behavior for an audio policy. |
| 6471 | * This variable reflects the value that was successfully set in |
| 6472 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 6473 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 6474 | * is handling ducking for audio focus. |
| 6475 | */ |
| 6476 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6477 | boolean mIsFocusPolicy = false; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6478 | |
| 6479 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6480 | boolean hasFocusListener, boolean isFocusPolicy, boolean isVolumeController) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6481 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6482 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6483 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6484 | mHasFocusListener = hasFocusListener; |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6485 | mIsVolumeController = isVolumeController; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6486 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6487 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6488 | // can only ever be true if there is a focus listener |
| 6489 | if (isFocusPolicy) { |
| 6490 | mIsFocusPolicy = true; |
| 6491 | mMediaFocusControl.setFocusPolicy(mPolicyCallback); |
| 6492 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6493 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6494 | if (mIsVolumeController) { |
| 6495 | setExtVolumeController(mPolicyCallback); |
| 6496 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6497 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6498 | } |
| 6499 | |
| 6500 | public void binderDied() { |
| 6501 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6502 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6503 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6504 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6505 | } |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 6506 | if (mIsVolumeController) { |
| 6507 | synchronized (mExtVolumeControllerLock) { |
| 6508 | mExtVolumeController = null; |
| 6509 | } |
| 6510 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6511 | } |
| 6512 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6513 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6514 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6515 | } |
| 6516 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6517 | void release() { |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6518 | if (mIsFocusPolicy) { |
| 6519 | mMediaFocusControl.unsetFocusPolicy(mPolicyCallback); |
| 6520 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6521 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6522 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 6523 | } |
| 6524 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6525 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6526 | } |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 6527 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6528 | AudioSystem.registerPolicyMixes(mMixes, false); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 6529 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6530 | } |
| 6531 | |
Jean-Michel Trivi | 7db2d8f | 2018-03-06 10:30:59 -0800 | [diff] [blame] | 6532 | boolean hasMixAffectingUsage(int usage) { |
| 6533 | for (AudioMix mix : mMixes) { |
| 6534 | if (mix.isAffectingUsage(usage)) { |
| 6535 | return true; |
| 6536 | } |
| 6537 | } |
| 6538 | return false; |
| 6539 | } |
| 6540 | |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6541 | // Verify all the devices in the array are served by mixes defined in this policy |
| 6542 | boolean hasMixRoutedToDevices(@NonNull int[] deviceTypes, |
| 6543 | @NonNull String[] deviceAddresses) { |
| 6544 | for (int i = 0; i < deviceTypes.length; i++) { |
| 6545 | boolean hasDevice = false; |
| 6546 | for (AudioMix mix : mMixes) { |
| 6547 | // this will check both that the mix has ROUTE_FLAG_RENDER and the device |
| 6548 | // is reached by this mix |
| 6549 | if (mix.isRoutedToDevice(deviceTypes[i], deviceAddresses[i])) { |
| 6550 | hasDevice = true; |
| 6551 | break; |
| 6552 | } |
| 6553 | } |
| 6554 | if (!hasDevice) { |
| 6555 | return false; |
| 6556 | } |
| 6557 | } |
| 6558 | return true; |
| 6559 | } |
| 6560 | |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6561 | void addMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 6562 | // TODO optimize to not have to unregister the mixes already in place |
| 6563 | synchronized (mMixes) { |
| 6564 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 6565 | this.add(mixes); |
| 6566 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | void removeMixes(@NonNull ArrayList<AudioMix> mixes) { |
| 6571 | // TODO optimize to not have to unregister the mixes already in place |
| 6572 | synchronized (mMixes) { |
| 6573 | AudioSystem.registerPolicyMixes(mMixes, false); |
| 6574 | this.remove(mixes); |
| 6575 | AudioSystem.registerPolicyMixes(mMixes, true); |
| 6576 | } |
| 6577 | } |
| 6578 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6579 | void connectMixes() { |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 6580 | final long identity = Binder.clearCallingIdentity(); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6581 | AudioSystem.registerPolicyMixes(mMixes, true); |
Eric Laurent | 66b6967 | 2018-01-26 18:30:51 -0800 | [diff] [blame] | 6582 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6583 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6584 | |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6585 | int setUidDeviceAffinities(int uid, @NonNull int[] types, @NonNull String[] addresses) { |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6586 | final Integer Uid = new Integer(uid); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6587 | int res; |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6588 | if (mUidDeviceAffinities.remove(Uid) != null) { |
| 6589 | final long identity = Binder.clearCallingIdentity(); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6590 | res = AudioSystem.removeUidDeviceAffinities(uid); |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6591 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6592 | if (res != AudioSystem.SUCCESS) { |
| 6593 | Log.e(TAG, "AudioSystem. removeUidDeviceAffinities(" + uid + ") failed, " |
| 6594 | + " cannot call AudioSystem.setUidDeviceAffinities"); |
| 6595 | return AudioManager.ERROR; |
| 6596 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6597 | } |
| 6598 | final long identity = Binder.clearCallingIdentity(); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6599 | res = AudioSystem.setUidDeviceAffinities(uid, types, addresses); |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6600 | Binder.restoreCallingIdentity(identity); |
| 6601 | if (res == AudioSystem.SUCCESS) { |
| 6602 | mUidDeviceAffinities.put(Uid, new AudioDeviceArray(types, addresses)); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6603 | return AudioManager.SUCCESS; |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6604 | } |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6605 | Log.e(TAG, "AudioSystem. setUidDeviceAffinities(" + uid + ") failed"); |
| 6606 | return AudioManager.ERROR; |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6607 | } |
| 6608 | |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6609 | int removeUidDeviceAffinities(int uid) { |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6610 | if (mUidDeviceAffinities.remove(new Integer(uid)) != null) { |
| 6611 | final long identity = Binder.clearCallingIdentity(); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6612 | final int res = AudioSystem.removeUidDeviceAffinities(uid); |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6613 | Binder.restoreCallingIdentity(identity); |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6614 | if (res == AudioSystem.SUCCESS) { |
| 6615 | return AudioManager.SUCCESS; |
| 6616 | } |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6617 | } |
Jean-Michel Trivi | f93107f | 2019-01-08 09:38:25 -0800 | [diff] [blame] | 6618 | Log.e(TAG, "AudioSystem. removeUidDeviceAffinities failed"); |
| 6619 | return AudioManager.ERROR; |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6620 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6621 | }; |
| 6622 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6623 | //====================== |
| 6624 | // Audio policy: focus |
| 6625 | //====================== |
| 6626 | /** */ |
| 6627 | public int dispatchFocusChange(AudioFocusInfo afi, int focusChange, IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 6628 | if (afi == null) { |
| 6629 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 6630 | } |
| 6631 | if (pcb == null) { |
| 6632 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 6633 | } |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6634 | synchronized (mAudioPolicies) { |
| 6635 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6636 | throw new IllegalStateException("Unregistered AudioPolicy for focus dispatch"); |
| 6637 | } |
| 6638 | return mMediaFocusControl.dispatchFocusChange(afi, focusChange); |
| 6639 | } |
| 6640 | } |
| 6641 | |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 6642 | public void setFocusRequestResultFromExtPolicy(AudioFocusInfo afi, int requestResult, |
| 6643 | IAudioPolicyCallback pcb) { |
| 6644 | if (afi == null) { |
| 6645 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 6646 | } |
| 6647 | if (pcb == null) { |
| 6648 | throw new IllegalArgumentException("Illegal null AudioPolicy callback"); |
| 6649 | } |
| 6650 | synchronized (mAudioPolicies) { |
| 6651 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6652 | throw new IllegalStateException("Unregistered AudioPolicy for external focus"); |
| 6653 | } |
| 6654 | mMediaFocusControl.setFocusRequestResultFromExtPolicy(afi, requestResult); |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6659 | //====================== |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 6660 | // Audioserver state displatch |
| 6661 | //====================== |
| 6662 | private class AsdProxy implements IBinder.DeathRecipient { |
| 6663 | private final IAudioServerStateDispatcher mAsd; |
| 6664 | |
| 6665 | AsdProxy(IAudioServerStateDispatcher asd) { |
| 6666 | mAsd = asd; |
| 6667 | } |
| 6668 | |
| 6669 | public void binderDied() { |
| 6670 | synchronized (mAudioServerStateListeners) { |
| 6671 | mAudioServerStateListeners.remove(mAsd.asBinder()); |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | IAudioServerStateDispatcher callback() { |
| 6676 | return mAsd; |
| 6677 | } |
| 6678 | } |
| 6679 | |
| 6680 | private HashMap<IBinder, AsdProxy> mAudioServerStateListeners = |
| 6681 | new HashMap<IBinder, AsdProxy>(); |
| 6682 | |
| 6683 | private void checkMonitorAudioServerStatePermission() { |
| 6684 | if (!(mContext.checkCallingOrSelfPermission( |
| 6685 | android.Manifest.permission.MODIFY_PHONE_STATE) == |
| 6686 | PackageManager.PERMISSION_GRANTED || |
| 6687 | mContext.checkCallingOrSelfPermission( |
| 6688 | android.Manifest.permission.MODIFY_AUDIO_ROUTING) == |
| 6689 | PackageManager.PERMISSION_GRANTED)) { |
| 6690 | throw new SecurityException("Not allowed to monitor audioserver state"); |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | public void registerAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 6695 | checkMonitorAudioServerStatePermission(); |
| 6696 | synchronized (mAudioServerStateListeners) { |
| 6697 | if (mAudioServerStateListeners.containsKey(asd.asBinder())) { |
| 6698 | Slog.w(TAG, "Cannot re-register audio server state dispatcher"); |
| 6699 | return; |
| 6700 | } |
| 6701 | AsdProxy asdp = new AsdProxy(asd); |
| 6702 | try { |
| 6703 | asd.asBinder().linkToDeath(asdp, 0/*flags*/); |
| 6704 | } catch (RemoteException e) { |
| 6705 | |
| 6706 | } |
| 6707 | mAudioServerStateListeners.put(asd.asBinder(), asdp); |
| 6708 | } |
| 6709 | } |
| 6710 | |
| 6711 | public void unregisterAudioServerStateDispatcher(IAudioServerStateDispatcher asd) { |
| 6712 | checkMonitorAudioServerStatePermission(); |
| 6713 | synchronized (mAudioServerStateListeners) { |
| 6714 | AsdProxy asdp = mAudioServerStateListeners.remove(asd.asBinder()); |
| 6715 | if (asdp == null) { |
| 6716 | Slog.w(TAG, "Trying to unregister unknown audioserver state dispatcher for pid " |
| 6717 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
| 6718 | return; |
| 6719 | } else { |
| 6720 | asd.asBinder().unlinkToDeath(asdp, 0/*flags*/); |
| 6721 | } |
| 6722 | } |
| 6723 | } |
| 6724 | |
| 6725 | public boolean isAudioServerRunning() { |
| 6726 | checkMonitorAudioServerStatePermission(); |
| 6727 | return (AudioSystem.checkAudioFlinger() == AudioSystem.AUDIO_STATUS_OK); |
| 6728 | } |
| 6729 | |
| 6730 | //====================== |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 6731 | // misc |
| 6732 | //====================== |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6733 | private final HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6734 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | af576a0 | 2018-02-14 08:57:46 -0800 | [diff] [blame] | 6735 | @GuardedBy("mAudioPolicies") |
| 6736 | private int mAudioPolicyCounter = 0; |
Phil Burk | ac0f704 | 2016-02-24 12:19:08 -0800 | [diff] [blame] | 6737 | } |