The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 17 | package com.android.server.audio; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.REMOTE_AUDIO_PLAYBACK; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 20 | import static android.media.AudioManager.RINGER_MODE_NORMAL; |
| 21 | import static android.media.AudioManager.RINGER_MODE_SILENT; |
| 22 | import static android.media.AudioManager.RINGER_MODE_VIBRATE; |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 23 | import static android.os.Process.FIRST_APPLICATION_UID; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 24 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 25 | import android.Manifest; |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 27 | import android.app.ActivityManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.ActivityManagerNative; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 29 | import android.app.AppGlobals; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 30 | import android.app.AppOpsManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothA2dp; |
| 32 | import android.bluetooth.BluetoothAdapter; |
| 33 | import android.bluetooth.BluetoothClass; |
| 34 | import android.bluetooth.BluetoothDevice; |
| 35 | import android.bluetooth.BluetoothHeadset; |
| 36 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 38 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.ContentResolver; |
| 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 45 | import android.content.pm.UserInfo; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 46 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 47 | import android.content.res.Resources; |
| 48 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 49 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 50 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 51 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 52 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 53 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 54 | import android.media.AudioAttributes; |
| 55 | import android.media.AudioDevicePort; |
| 56 | import android.media.AudioSystem; |
| 57 | import android.media.AudioFormat; |
| 58 | import android.media.AudioManager; |
| 59 | import android.media.AudioManagerInternal; |
| 60 | import android.media.AudioPort; |
| 61 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 62 | import android.media.IAudioFocusDispatcher; |
| 63 | import android.media.IAudioRoutesObserver; |
| 64 | import android.media.IAudioService; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 65 | import android.media.IRingtonePlayer; |
| 66 | import android.media.IVolumeController; |
| 67 | import android.media.MediaPlayer; |
| 68 | import android.media.SoundPool; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 69 | import android.media.VolumePolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.media.MediaPlayer.OnCompletionListener; |
| 71 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 72 | import android.media.audiopolicy.AudioMix; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 73 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 74 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 75 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 77 | import android.os.Build; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 78 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.os.Environment; |
| 80 | import android.os.Handler; |
| 81 | import android.os.IBinder; |
| 82 | import android.os.Looper; |
| 83 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 84 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 85 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 87 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 88 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 89 | import android.os.UserHandle; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 90 | import android.os.UserManager; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 91 | import android.os.UserManagerInternal; |
| 92 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 93 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.provider.Settings; |
| 95 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 96 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 97 | import android.text.TextUtils; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 98 | import android.util.AndroidRuntimeException; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 99 | import android.util.ArrayMap; |
| 100 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 102 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 103 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 104 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 105 | import android.view.KeyEvent; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 106 | import android.view.OrientationEventListener; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 107 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 108 | import android.view.WindowManager; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 109 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 111 | import com.android.internal.util.XmlUtils; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 112 | import com.android.server.EventLogTags; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 113 | import com.android.server.LocalServices; |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 114 | import com.android.server.SystemService; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 115 | import com.android.server.pm.UserManagerService; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 116 | |
| 117 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 119 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 121 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 122 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 124 | import java.util.HashMap; |
| 125 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 126 | import java.util.List; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 127 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 128 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * The implementation of the volume manager service. |
| 132 | * <p> |
| 133 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 134 | * asynchronous to external calls. For example, the task of setting a volume |
| 135 | * will update our internal state, but in a separate thread will set the system |
| 136 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 137 | * will update the state and broadcast a change and in a separate thread later |
| 138 | * persist the ringer mode. |
| 139 | * |
| 140 | * @hide |
| 141 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 142 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
| 144 | private static final String TAG = "AudioService"; |
| 145 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 146 | /** Debug audio mode */ |
| 147 | protected static final boolean DEBUG_MODE = Log.isLoggable(TAG + ".MOD", Log.DEBUG); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 148 | |
| 149 | /** Debug audio policy feature */ |
| 150 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 151 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 152 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 153 | protected static final boolean DEBUG_VOL = Log.isLoggable(TAG + ".VOL", Log.DEBUG); |
Jean-Michel Trivi | 18e7bce | 2011-08-26 12:11:36 -0700 | [diff] [blame] | 154 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 155 | /** debug calls to devices APIs */ |
| 156 | protected static final boolean DEBUG_DEVICES = Log.isLoggable(TAG + ".DEVICES", Log.DEBUG); |
| 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 159 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 161 | /** How long to delay after a volume down event before unmuting a stream */ |
| 162 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 163 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 164 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 165 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 166 | */ |
| 167 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 168 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 169 | private final Context mContext; |
| 170 | private final ContentResolver mContentResolver; |
| 171 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 172 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 173 | // the platform type affects volume and silent mode behavior |
| 174 | private final int mPlatformType; |
| 175 | |
| 176 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 177 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 181 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 182 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 183 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 184 | /** The controller for the volume UI. */ |
| 185 | private final VolumeController mVolumeController = new VolumeController(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 186 | private final ControllerService mControllerService = new ControllerService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
| 188 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | /** If the msg is already queued, replace it with this one. */ |
| 190 | private static final int SENDMSG_REPLACE = 0; |
| 191 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 192 | private static final int SENDMSG_NOOP = 1; |
| 193 | /** If the msg is already queued, queue this one and leave the old. */ |
| 194 | private static final int SENDMSG_QUEUE = 2; |
| 195 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 196 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 197 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | private static final int MSG_PERSIST_VOLUME = 1; |
| 199 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 200 | private static final int MSG_AUDIO_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 201 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 202 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 203 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 204 | private static final int MSG_SET_FORCE_USE = 8; |
| 205 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 206 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 207 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 208 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 209 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 210 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 211 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 212 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 213 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 214 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 215 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 216 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 217 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 218 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 219 | private static final int MSG_PERSIST_MICROPHONE_MUTE = 23; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 220 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 221 | private static final int MSG_DYN_POLICY_MIX_STATE_UPDATE = 25; |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 222 | private static final int MSG_INDICATE_SYSTEM_READY = 26; |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 223 | private static final int MSG_PERSIST_MASTER_MONO = 27; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 224 | // start of messages handled under wakelock |
| 225 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 226 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 227 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 228 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 229 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 230 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 231 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 232 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 233 | // Timeout for connection to bluetooth headset service |
| 234 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 235 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 236 | // retry delay in case of failure to indicate system ready to AudioFlinger |
| 237 | private static final int INDICATE_SYSTEM_READY_RETRY_DELAY_MS = 1000; |
| 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | /** @see AudioSystemThread */ |
| 240 | private AudioSystemThread mAudioSystemThread; |
| 241 | /** @see AudioHandler */ |
| 242 | private AudioHandler mAudioHandler; |
| 243 | /** @see VolumeStreamState */ |
| 244 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 245 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 247 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 248 | // protects mRingerMode |
| 249 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 252 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
| 255 | /* Sound effect file names */ |
| 256 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 257 | 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] | 258 | |
| 259 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 260 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 261 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 262 | 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] | 263 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 264 | /** Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 265 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 266 | 5, // STREAM_VOICE_CALL |
| 267 | 7, // STREAM_SYSTEM |
| 268 | 7, // STREAM_RING |
| 269 | 15, // STREAM_MUSIC |
| 270 | 7, // STREAM_ALARM |
| 271 | 7, // STREAM_NOTIFICATION |
| 272 | 15, // STREAM_BLUETOOTH_SCO |
| 273 | 7, // STREAM_SYSTEM_ENFORCED |
| 274 | 15, // STREAM_DTMF |
| 275 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 276 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 277 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 278 | /** Minimum volume index values for audio streams */ |
| 279 | private static int[] MIN_STREAM_VOLUME = new int[] { |
| 280 | 1, // STREAM_VOICE_CALL |
| 281 | 0, // STREAM_SYSTEM |
| 282 | 0, // STREAM_RING |
| 283 | 0, // STREAM_MUSIC |
| 284 | 0, // STREAM_ALARM |
| 285 | 0, // STREAM_NOTIFICATION |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 286 | 0, // STREAM_BLUETOOTH_SCO |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 287 | 0, // STREAM_SYSTEM_ENFORCED |
| 288 | 0, // STREAM_DTMF |
| 289 | 0 // STREAM_TTS |
| 290 | }; |
| 291 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 292 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 293 | * of another stream: This avoids multiplying the volume settings for hidden |
| 294 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 295 | * NOTE: do not create loops in aliases! |
| 296 | * 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] | 297 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 298 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 299 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 300 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 301 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 302 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 303 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 304 | AudioSystem.STREAM_RING, // STREAM_RING |
| 305 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 306 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 307 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 308 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 309 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 310 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 311 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 312 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 313 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 314 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 315 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 316 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 317 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 318 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 319 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 320 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 321 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 322 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 323 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 324 | }; |
| 325 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 326 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 327 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 328 | AudioSystem.STREAM_RING, // STREAM_RING |
| 329 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 330 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 331 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 332 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 333 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 334 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 335 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 336 | }; |
| 337 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 338 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 341 | * after mapping through mStreamVolumeAlias. |
| 342 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 343 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 344 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 345 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 346 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 347 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 348 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 349 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 350 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 351 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 352 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 353 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 354 | }; |
| 355 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 356 | private final boolean mUseFixedVolume; |
| 357 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 358 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | public void onError(int error) { |
| 360 | switch (error) { |
| 361 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 362 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 363 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | break; |
| 365 | default: |
| 366 | break; |
| 367 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | /** |
| 372 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 373 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 374 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 375 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 376 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 377 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 378 | 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] | 379 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 380 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 381 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 382 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 383 | // Streams currently muted by ringer mode |
| 384 | private int mRingerModeMutedStreams; |
| 385 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 386 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 387 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | private int mMuteAffectedStreams; |
| 389 | |
| 390 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 391 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 392 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 393 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | */ |
| 395 | private int mVibrateSetting; |
| 396 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 397 | // Is there a vibrator |
| 398 | private final boolean mHasVibrator; |
| 399 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 400 | // Broadcast receiver for device connections intent broadcasts |
| 401 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 402 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 403 | /** Interface for UserManagerService. */ |
| 404 | private final UserManagerInternal mUserManagerInternal; |
| 405 | |
| 406 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 407 | new AudioServiceUserRestrictionsListener(); |
| 408 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 409 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 410 | // Use makeDeviceListKey() to make a unique key for this list. |
| 411 | private class DeviceListSpec { |
| 412 | int mDeviceType; |
| 413 | String mDeviceName; |
| 414 | String mDeviceAddress; |
| 415 | |
| 416 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 417 | mDeviceType = deviceType; |
| 418 | mDeviceName = deviceName; |
| 419 | mDeviceAddress = deviceAddress; |
| 420 | } |
| 421 | |
| 422 | public String toString() { |
| 423 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 424 | + " address:" + mDeviceAddress + "]"; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 429 | // and the "address" associated with a specific instance of that device type |
| 430 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 431 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 432 | } |
| 433 | |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 434 | private final ArrayMap<String, DeviceListSpec> mConnectedDevices = new ArrayMap<>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 435 | |
| 436 | // Forced device usage for communications |
| 437 | private int mForcedUseForComm; |
| 438 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 439 | // List of binder death handlers for setMode() client processes. |
| 440 | // The last process to have called setMode() is at the top of the list. |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 441 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 442 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 443 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 444 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 445 | |
| 446 | // BluetoothHeadset API to control SCO connection |
| 447 | private BluetoothHeadset mBluetoothHeadset; |
| 448 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 449 | // Bluetooth headset device |
| 450 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 451 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 452 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 453 | // audio service (internal) or bluetooth headset (external) |
| 454 | private int mScoAudioState; |
| 455 | // SCO audio state is not active |
| 456 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 457 | // SCO audio activation request waiting for headset service to connect |
| 458 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 459 | // SCO audio state is active or starting due to a request from AudioManager API |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 460 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 461 | // SCO audio deactivation request waiting for headset service to connect |
| 462 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 463 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 464 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 465 | // in call audio) |
| 466 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 467 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 468 | // waiting for headset service to connect |
| 469 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 470 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 471 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 472 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 473 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 474 | // SCO audio mode is undefined |
| 475 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 476 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 477 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 478 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 479 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 480 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 481 | private static final int SCO_MODE_VR = 2; |
| 482 | |
| 483 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 484 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 485 | // Current connection state indicated by bluetooth headset |
| 486 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 487 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 488 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 489 | private boolean mSystemReady; |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 490 | // true if Intent.ACTION_USER_SWITCHED has ever been received |
| 491 | private boolean mUserSwitchedReceived; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 492 | // listener for SoundPool sample load completion indication |
| 493 | private SoundPoolCallback mSoundPoolCallBack; |
| 494 | // thread for SoundPool listener |
| 495 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 496 | // message looper for SoundPool listener |
| 497 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 498 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 499 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 500 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 501 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 502 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 503 | // is controlled by Vol keys. |
| 504 | private int mVolumeControlStream = -1; |
| 505 | private final Object mForceControlStreamLock = new Object(); |
| 506 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 507 | // server process so in theory it is not necessary to monitor the client death. |
| 508 | // However it is good to be ready for future evolutions. |
| 509 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 510 | // Used to play ringtones outside system_server |
| 511 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 512 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 513 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
| 514 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 515 | // Request to override default use of A2DP for media. |
| 516 | private boolean mBluetoothA2dpEnabled; |
| 517 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 518 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 519 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 520 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 521 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 522 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 523 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 524 | // 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] | 525 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 526 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 527 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 528 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 529 | AudioSystem.DEVICE_OUT_SPDIF | |
| 530 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 531 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 532 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 533 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 534 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 535 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 536 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 537 | private boolean mDockAudioMediaEnabled = true; |
| 538 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 539 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 540 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 541 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 542 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 543 | // and used later when/if disableSafeMediaVolume() is called. |
| 544 | private StreamVolumeCommand mPendingVolumeCommand; |
| 545 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 546 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 547 | |
| 548 | private final MediaFocusControl mMediaFocusControl; |
| 549 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 550 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 551 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 552 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 553 | private final Object mA2dpAvrcpLock = new Object(); |
| 554 | // If absolute volume is supported in AVRCP device |
| 555 | private boolean mAvrcpAbsVolSupported = false; |
| 556 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 557 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 558 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 559 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 560 | private VolumePolicy mVolumePolicy = VolumePolicy.DEFAULT; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 561 | private long mLoweredFromNormalToVibrateTime; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 562 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 563 | // Intent "extra" data keys. |
| 564 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 565 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 566 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 567 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 568 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 569 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 570 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 571 | |
| 572 | // Defines the format for the connection "address" for ALSA devices |
| 573 | public static String makeAlsaAddressString(int card, int device) { |
| 574 | return "card=" + card + ";device=" + device + ";"; |
| 575 | } |
| 576 | |
Makoto Onuki | e1aef85 | 2015-10-15 17:28:35 -0700 | [diff] [blame] | 577 | public static final class Lifecycle extends SystemService { |
| 578 | private AudioService mService; |
| 579 | |
| 580 | public Lifecycle(Context context) { |
| 581 | super(context); |
| 582 | mService = new AudioService(context); |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public void onStart() { |
| 587 | publishBinderService(Context.AUDIO_SERVICE, mService); |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public void onBootPhase(int phase) { |
| 592 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 593 | mService.systemReady(); |
| 594 | } |
| 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 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 610 | mUserManagerInternal = LocalServices.getService(UserManagerInternal.class); |
| 611 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 612 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 613 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 614 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 615 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 616 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 617 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 618 | // Initialize volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 619 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 620 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 621 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 622 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 623 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 624 | } |
| 625 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 626 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 627 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 628 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 629 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 630 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 631 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 632 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 633 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 634 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 635 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 636 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 638 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 639 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 640 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 641 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 642 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 643 | sendMsg(mAudioHandler, |
| 644 | MSG_SET_FORCE_USE, |
| 645 | SENDMSG_QUEUE, |
| 646 | AudioSystem.FOR_SYSTEM, |
| 647 | cameraSoundForced ? |
| 648 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 649 | null, |
| 650 | 0); |
| 651 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 652 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 653 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 654 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 655 | // The default safe volume index read here will be replaced by the actual value when |
| 656 | // the mcc is read by onConfigureSafeVolume() |
| 657 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 658 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 659 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 660 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 661 | com.android.internal.R.bool.config_useFixedVolume); |
| 662 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 663 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 664 | // array initialized by updateStreamVolumeAlias() |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 665 | updateStreamVolumeAlias(false /*updateVolumes*/, TAG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 667 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 668 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 669 | |
Jean-Michel Trivi | d4de20d | 2015-11-04 14:45:54 -0800 | [diff] [blame] | 670 | mMediaFocusControl = new MediaFocusControl(mContext); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 671 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 672 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 673 | |
| 674 | // Call setRingerModeInt() to apply correct mute |
| 675 | // state on streams affected by ringer mode. |
| 676 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 677 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 678 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 679 | // Register for device connection intent broadcasts. |
| 680 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 681 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 682 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 683 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 684 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 685 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 686 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 687 | intentFilter.addAction(Intent.ACTION_USER_BACKGROUND); |
| 688 | intentFilter.addAction(Intent.ACTION_USER_FOREGROUND); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 689 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 690 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 691 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 692 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 693 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 694 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 695 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 696 | // initialize orientation in AudioSystem |
| 697 | setOrientationForAudioSystem(); |
| 698 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 699 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 700 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 701 | RotationHelper.init(mContext, mAudioHandler); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 702 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 703 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 704 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 705 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 706 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 707 | |
| 708 | mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 711 | public void systemReady() { |
| 712 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 713 | 0, 0, null, 0); |
| 714 | } |
| 715 | |
| 716 | public void onSystemReady() { |
| 717 | mSystemReady = true; |
| 718 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 719 | 0, 0, null, 0); |
| 720 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 721 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 722 | resetBluetoothSco(); |
| 723 | getBluetoothHeadset(); |
| 724 | //FIXME: this is to maintain compatibility with deprecated intent |
| 725 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 726 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 727 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 728 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 729 | sendStickyBroadcastToAll(newIntent); |
| 730 | |
| 731 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 732 | if (adapter != null) { |
| 733 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 734 | BluetoothProfile.A2DP); |
| 735 | } |
| 736 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 737 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 738 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 739 | if (mHdmiManager != null) { |
| 740 | synchronized (mHdmiManager) { |
| 741 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 742 | if (mHdmiTvClient != null) { |
| 743 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 744 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 745 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 746 | mHdmiCecSink = false; |
| 747 | } |
| 748 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 749 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 750 | sendMsg(mAudioHandler, |
| 751 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 752 | SENDMSG_REPLACE, |
| 753 | 0, |
| 754 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 755 | TAG, |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 756 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 757 | |
| 758 | StreamOverride.init(mContext); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 759 | mControllerService.init(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 760 | onIndicateSystemReady(); |
| 761 | } |
| 762 | |
| 763 | void onIndicateSystemReady() { |
| 764 | if (AudioSystem.systemReady() == AudioSystem.SUCCESS) { |
| 765 | return; |
| 766 | } |
| 767 | sendMsg(mAudioHandler, |
| 768 | MSG_INDICATE_SYSTEM_READY, |
| 769 | SENDMSG_REPLACE, |
| 770 | 0, |
| 771 | 0, |
| 772 | null, |
| 773 | INDICATE_SYSTEM_READY_RETRY_DELAY_MS); |
| 774 | } |
| 775 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 776 | public void onAudioServerDied() { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 777 | if (!mSystemReady || |
| 778 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 779 | Log.e(TAG, "Audioserver died."); |
| 780 | sendMsg(mAudioHandler, MSG_AUDIO_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 781 | null, 500); |
| 782 | return; |
| 783 | } |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 784 | Log.e(TAG, "Audioserver started."); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 785 | |
| 786 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 787 | // server crash |
| 788 | // Note that we only execute this when the media server |
| 789 | // process restarts after a crash, not the first time it is started. |
| 790 | AudioSystem.setParameters("restarting=true"); |
| 791 | |
| 792 | readAndSetLowRamDevice(); |
| 793 | |
| 794 | // Restore device connection states |
| 795 | synchronized (mConnectedDevices) { |
| 796 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 797 | DeviceListSpec spec = mConnectedDevices.valueAt(i); |
| 798 | AudioSystem.setDeviceConnectionState( |
| 799 | spec.mDeviceType, |
| 800 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 801 | spec.mDeviceAddress, |
| 802 | spec.mDeviceName); |
| 803 | } |
| 804 | } |
| 805 | // Restore call state |
| 806 | AudioSystem.setPhoneState(mMode); |
| 807 | |
| 808 | // Restore forced usage for communcations and record |
| 809 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
| 810 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
| 811 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 812 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
| 813 | |
| 814 | // Restore stream volumes |
| 815 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 816 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 817 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | e4381ec | 2015-10-29 17:52:48 -0700 | [diff] [blame] | 818 | AudioSystem.initStreamVolume( |
| 819 | streamType, streamState.mIndexMin / 10, streamState.mIndexMax / 10); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 820 | |
| 821 | streamState.applyAllVolumes(); |
| 822 | } |
| 823 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 824 | // Restore mono mode |
| 825 | final boolean masterMono = System.getIntForUser( |
| 826 | mContentResolver, System.MASTER_MONO, |
| 827 | 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 828 | AudioSystem.setMasterMono(masterMono); |
| 829 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 830 | // Restore ringer mode |
| 831 | setRingerModeInt(getRingerModeInternal(), false); |
| 832 | |
| 833 | // Reset device orientation (if monitored for this device) |
| 834 | if (mMonitorOrientation) { |
| 835 | setOrientationForAudioSystem(); |
| 836 | } |
| 837 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 838 | RotationHelper.updateOrientation(); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | synchronized (mBluetoothA2dpEnabledLock) { |
| 842 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 843 | mBluetoothA2dpEnabled ? |
| 844 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 845 | } |
| 846 | |
| 847 | synchronized (mSettingsLock) { |
| 848 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 849 | mDockAudioMediaEnabled ? |
| 850 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 851 | } |
| 852 | if (mHdmiManager != null) { |
| 853 | synchronized (mHdmiManager) { |
| 854 | if (mHdmiTvClient != null) { |
| 855 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | synchronized (mAudioPolicies) { |
| 861 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 862 | policy.connectMixes(); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | onIndicateSystemReady(); |
| 867 | // indicate the end of reconfiguration phase to audio HAL |
| 868 | AudioSystem.setParameters("restarting=false"); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 869 | } |
| 870 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | private void createAudioSystemThread() { |
| 872 | mAudioSystemThread = new AudioSystemThread(); |
| 873 | mAudioSystemThread.start(); |
| 874 | waitForAudioHandlerCreation(); |
| 875 | } |
| 876 | |
| 877 | /** Waits for the volume handler to be created by the other thread. */ |
| 878 | private void waitForAudioHandlerCreation() { |
| 879 | synchronized(this) { |
| 880 | while (mAudioHandler == null) { |
| 881 | try { |
| 882 | // Wait for mAudioHandler to be set by the other thread |
| 883 | wait(); |
| 884 | } catch (InterruptedException e) { |
| 885 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 891 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 892 | synchronized (VolumeStreamState.class) { |
| 893 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 894 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 895 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 896 | mStreamStates[streamType]. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 897 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], |
| 898 | TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 899 | } |
| 900 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 901 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 902 | mStreamStates[streamType].applyAllVolumes(); |
| 903 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 908 | private void checkAllFixedVolumeDevices() |
| 909 | { |
| 910 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 911 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 912 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 913 | } |
| 914 | } |
| 915 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 916 | private void checkAllFixedVolumeDevices(int streamType) { |
| 917 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 918 | } |
| 919 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 920 | private void checkMuteAffectedStreams() { |
| 921 | // any stream with a min level > 0 is not muteable by definition |
| 922 | for (int i = 0; i < mStreamStates.length; i++) { |
| 923 | final VolumeStreamState vss = mStreamStates[i]; |
| 924 | if (vss.mIndexMin > 0) { |
| 925 | mMuteAffectedStreams &= ~(1 << vss.mStreamType); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 932 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 933 | |
| 934 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 935 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 938 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 939 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 940 | checkMuteAffectedStreams(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 943 | private void dumpStreamStates(PrintWriter pw) { |
| 944 | pw.println("\nStream volumes (device: index)"); |
| 945 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 946 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 947 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 948 | mStreamStates[i].dump(pw); |
| 949 | pw.println(""); |
| 950 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 951 | pw.print("\n- mute affected streams = 0x"); |
| 952 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 953 | } |
| 954 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 955 | private void updateStreamVolumeAlias(boolean updateVolumes, String caller) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 956 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 957 | |
| 958 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 959 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 960 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 961 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 962 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 963 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 964 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 965 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 966 | break; |
| 967 | default: |
| 968 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 969 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 970 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 971 | |
| 972 | if (isPlatformTelevision()) { |
| 973 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 974 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 975 | if (isInCommunication()) { |
| 976 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 977 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 978 | } else { |
| 979 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 980 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 981 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 982 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 983 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 984 | if (updateVolumes) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 985 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], |
| 986 | caller); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 987 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 988 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 989 | sendMsg(mAudioHandler, |
| 990 | MSG_SET_ALL_VOLUMES, |
| 991 | SENDMSG_QUEUE, |
| 992 | 0, |
| 993 | 0, |
| 994 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 995 | } |
| 996 | } |
| 997 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 998 | private void readDockAudioSettings(ContentResolver cr) |
| 999 | { |
| 1000 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 1001 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1002 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1003 | sendMsg(mAudioHandler, |
| 1004 | MSG_SET_FORCE_USE, |
| 1005 | SENDMSG_QUEUE, |
| 1006 | AudioSystem.FOR_DOCK, |
| 1007 | mDockAudioMediaEnabled ? |
| 1008 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 1009 | null, |
| 1010 | 0); |
| 1011 | } |
| 1012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | private void readPersistedSettings() { |
| 1014 | final ContentResolver cr = mContentResolver; |
| 1015 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1016 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1017 | Settings.Global.getInt( |
| 1018 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1019 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1020 | // sanity check in case the settings are restored from a device with incompatible |
| 1021 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1022 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1023 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1024 | } |
| 1025 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1026 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1027 | } |
| 1028 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 1029 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1030 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1031 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1032 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1033 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1034 | synchronized(mSettingsLock) { |
| 1035 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1036 | if (mRingerModeExternal == -1) { |
| 1037 | mRingerModeExternal = mRingerMode; |
| 1038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1040 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 1041 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 1042 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1043 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1044 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 1045 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1046 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1047 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 1048 | AudioManager.VIBRATE_TYPE_RINGER, |
| 1049 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 1050 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 1052 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 1053 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1054 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 1055 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1056 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1057 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 1058 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1060 | final int currentUser = getCurrentUserId(); |
| 1061 | |
| 1062 | // In addition to checking the system setting, also check the current user restriction. |
| 1063 | // Because of the delay before persisting VOLUME_MASTER_MUTE, there's a window where |
| 1064 | // DISALLOW_ADJUST_VOLUME will be ignored when it's set right before switching users. |
| 1065 | boolean masterMute = (System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
| 1066 | 0, UserHandle.USER_CURRENT) == 1) |
| 1067 | || mUserManagerInternal.getUserRestriction( |
| 1068 | currentUser, UserManager.DISALLOW_ADJUST_VOLUME); |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1069 | if (mUseFixedVolume) { |
| 1070 | masterMute = false; |
| 1071 | AudioSystem.setMasterVolume(1.0f); |
| 1072 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1073 | if (DEBUG_VOL) { |
| 1074 | Log.d(TAG, String.format("Master mute %s, user=%d", masterMute, currentUser)); |
| 1075 | } |
Jae Seo | 1b434da | 2015-11-11 18:58:51 -0800 | [diff] [blame] | 1076 | setSystemAudioMute(masterMute); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1077 | AudioSystem.setMasterMute(masterMute); |
| 1078 | broadcastMasterMuteStatus(masterMute); |
| 1079 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1080 | boolean microphoneMute = |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1081 | (System.getIntForUser(cr, System.MICROPHONE_MUTE, 0, UserHandle.USER_CURRENT) == 1) |
| 1082 | || mUserManagerInternal.getUserRestriction( |
| 1083 | currentUser, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1084 | if (DEBUG_VOL) { |
| 1085 | Log.d(TAG, String.format("Mic mute %s, user=%d", microphoneMute, currentUser)); |
| 1086 | } |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1087 | AudioSystem.muteMicrophone(microphoneMute); |
| 1088 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1089 | final boolean masterMono = System.getIntForUser( |
| 1090 | cr, System.MASTER_MONO, 0 /* default */, UserHandle.USER_CURRENT) == 1; |
| 1091 | if (DEBUG_VOL) { |
| 1092 | Log.d(TAG, String.format("Master mono %b, user=%d", masterMono, currentUser)); |
| 1093 | } |
| 1094 | AudioSystem.setMasterMono(masterMono); |
| 1095 | broadcastMasterMonoStatus(masterMono); |
| 1096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | // Each stream will read its own persisted settings |
| 1098 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1099 | // Broadcast the sticky intents |
| 1100 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 1101 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | |
| 1103 | // Broadcast vibrate settings |
| 1104 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 1105 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 1106 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1107 | // Load settings for the volume controller |
| 1108 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1111 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 1112 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 1113 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | |
| 1115 | /////////////////////////////////////////////////////////////////////////// |
| 1116 | // IPC methods |
| 1117 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1119 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1120 | String callingPackage, String caller) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1121 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1122 | caller, Binder.getCallingUid()); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1126 | String callingPackage, String caller, int uid) { |
| 1127 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream=" + suggestedStreamType |
| 1128 | + ", flags=" + flags + ", caller=" + caller); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1129 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1130 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1131 | if (mVolumeControlStream != -1) { |
| 1132 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1133 | } else { |
| 1134 | streamType = getActiveStreamType(suggestedStreamType); |
| 1135 | } |
John Spurlock | 0a376af | 2015-03-26 16:24:12 -0400 | [diff] [blame] | 1136 | ensureValidStreamType(streamType); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1137 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 1139 | // Play sounds on STREAM_RING only. |
| 1140 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1141 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1143 | } |
| 1144 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1145 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1146 | // Don't suppress mute/unmute requests |
| 1147 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 1148 | direction = 0; |
| 1149 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 1150 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 1151 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 1152 | } |
| 1153 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1154 | adjustStreamVolume(streamType, direction, flags, callingPackage, caller, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1158 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 1159 | String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1160 | adjustStreamVolume(streamType, direction, flags, callingPackage, callingPackage, |
| 1161 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | private void adjustStreamVolume(int streamType, int direction, int flags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1165 | String callingPackage, String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1166 | if (mUseFixedVolume) { |
| 1167 | return; |
| 1168 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1169 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream=" + streamType + ", dir=" + direction |
| 1170 | + ", flags=" + flags + ", caller=" + caller); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | ensureValidDirection(direction); |
| 1173 | ensureValidStreamType(streamType); |
| 1174 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1175 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1176 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1177 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1181 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1182 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1183 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1184 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1185 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1186 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1187 | |
| 1188 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1189 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1190 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1192 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1193 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1194 | // skip a2dp absolute volume control request when the device |
| 1195 | // is not an a2dp device |
| 1196 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1197 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1198 | return; |
| 1199 | } |
| 1200 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1201 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1202 | // so we handle user restrictions correctly. |
| 1203 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1204 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1205 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1206 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1207 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1208 | return; |
| 1209 | } |
| 1210 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1211 | // reset any pending volume command |
| 1212 | synchronized (mSafeMediaVolumeState) { |
| 1213 | mPendingVolumeCommand = null; |
| 1214 | } |
| 1215 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1216 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1217 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1218 | ((device & mFixedVolumeDevices) != 0)) { |
| 1219 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1220 | |
| 1221 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1222 | // volume is enforced, and max and 0 for the others. |
| 1223 | // This is simulated by stepping by the full allowed volume range |
| 1224 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1225 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1226 | step = mSafeMediaVolumeIndex; |
| 1227 | } else { |
| 1228 | step = streamState.getMaxIndex(); |
| 1229 | } |
| 1230 | if (aliasIndex != 0) { |
| 1231 | aliasIndex = step; |
| 1232 | } |
| 1233 | } else { |
| 1234 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1235 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1236 | } |
| 1237 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1238 | // If either the client forces allowing ringer modes for this adjustment, |
| 1239 | // or the stream type is one that is affected by ringer modes |
| 1240 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1241 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1242 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1243 | // do not vibrate if already in vibrate mode |
| 1244 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1245 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1246 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1247 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1248 | // need to adjust the volume further. |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1249 | final int result = checkForRingerModeChange(aliasIndex, direction, step, |
| 1250 | streamState.mIsMuted); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1251 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1252 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1253 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1254 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1255 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1256 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1257 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1258 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1259 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1260 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1261 | // If the ringermode is suppressing media, prevent changes |
| 1262 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC |
| 1263 | && (mRingerModeMutedStreams & (1 << AudioSystem.STREAM_MUSIC)) != 0) { |
| 1264 | adjustVolume = false; |
| 1265 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1266 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1267 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1268 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1269 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1270 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1271 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1272 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1273 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1274 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1275 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1276 | synchronized (mA2dpAvrcpLock) { |
| 1277 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1278 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1279 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1280 | } |
| 1281 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1282 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1283 | if (isMuteAdjust) { |
| 1284 | boolean state; |
| 1285 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1286 | state = !streamState.mIsMuted; |
| 1287 | } else { |
| 1288 | state = direction == AudioManager.ADJUST_MUTE; |
| 1289 | } |
| 1290 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1291 | setSystemAudioMute(state); |
| 1292 | } |
| 1293 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1294 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
Sungmin Choi | 841ed0a | 2015-07-26 23:09:49 -0700 | [diff] [blame] | 1295 | if (!(readCameraSoundForced() |
| 1296 | && (mStreamStates[stream].getStreamType() |
| 1297 | == AudioSystem.STREAM_SYSTEM_ENFORCED))) { |
| 1298 | mStreamStates[stream].mute(state); |
| 1299 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1303 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1304 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1305 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1306 | } else if (streamState.adjustIndex(direction * step, device, caller) |
| 1307 | || streamState.mIsMuted) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1308 | // Post message to set system volume (it in turn will post a |
| 1309 | // message to persist). |
| 1310 | if (streamState.mIsMuted) { |
| 1311 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1312 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1313 | // unmute immediately for volume up |
| 1314 | streamState.mute(false); |
| 1315 | } else if (direction == AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1316 | if (mPlatformType == AudioSystem.PLATFORM_TELEVISION) { |
| 1317 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1318 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1319 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1320 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1321 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1322 | sendMsg(mAudioHandler, |
| 1323 | MSG_SET_DEVICE_VOLUME, |
| 1324 | SENDMSG_QUEUE, |
| 1325 | device, |
| 1326 | 0, |
| 1327 | streamState, |
| 1328 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1329 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1330 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1331 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1332 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1333 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1334 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1335 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1336 | if (mHdmiManager != null) { |
| 1337 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1338 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1339 | if (mHdmiCecSink && |
| 1340 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1341 | oldIndex != newIndex) { |
| 1342 | synchronized (mHdmiPlaybackClient) { |
| 1343 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1344 | KeyEvent.KEYCODE_VOLUME_UP; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1345 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1346 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1347 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1351 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1352 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1353 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1356 | // Called after a delay when volume down is pressed while muted |
| 1357 | private void onUnmuteStream(int stream, int flags) { |
| 1358 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1359 | streamState.mute(false); |
| 1360 | |
| 1361 | final int device = getDeviceForStream(stream); |
| 1362 | final int index = mStreamStates[stream].getIndex(device); |
| 1363 | sendVolumeUpdate(stream, index, index, flags); |
| 1364 | } |
| 1365 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1366 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1367 | if (mHdmiManager == null |
| 1368 | || mHdmiTvClient == null |
| 1369 | || oldVolume == newVolume |
| 1370 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1371 | |
| 1372 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1373 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1374 | synchronized (mHdmiManager) { |
| 1375 | if (!mHdmiSystemAudioSupported) return; |
| 1376 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1377 | final long token = Binder.clearCallingIdentity(); |
| 1378 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1379 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1380 | } finally { |
| 1381 | Binder.restoreCallingIdentity(token); |
| 1382 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1387 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1388 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1389 | class StreamVolumeCommand { |
| 1390 | public final int mStreamType; |
| 1391 | public final int mIndex; |
| 1392 | public final int mFlags; |
| 1393 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1394 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1395 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1396 | mStreamType = streamType; |
| 1397 | mIndex = index; |
| 1398 | mFlags = flags; |
| 1399 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1400 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1401 | |
| 1402 | @Override |
| 1403 | public String toString() { |
| 1404 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1405 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1406 | .append(mDevice).append('}').toString(); |
| 1407 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1408 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1409 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1410 | private void onSetStreamVolume(int streamType, int index, int flags, int device, |
| 1411 | String caller) { |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1412 | final int stream = mStreamVolumeAlias[streamType]; |
| 1413 | setStreamVolumeInt(stream, index, device, false, caller); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1414 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1415 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1416 | (stream == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1417 | int newRingerMode; |
| 1418 | if (index == 0) { |
| 1419 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 1420 | : mVolumePolicy.volumeDownToEnterSilent ? AudioManager.RINGER_MODE_SILENT |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1421 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1422 | } else { |
| 1423 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1424 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1425 | setRingerMode(newRingerMode, TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1426 | } |
John Spurlock | 75ae23c | 2015-06-02 16:26:43 -0400 | [diff] [blame] | 1427 | // setting non-zero volume for a muted stream unmutes the stream and vice versa |
| 1428 | mStreamStates[stream].mute(index == 0); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1432 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1433 | setStreamVolume(streamType, index, flags, callingPackage, callingPackage, |
| 1434 | Binder.getCallingUid()); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1438 | String caller, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1439 | if (mUseFixedVolume) { |
| 1440 | return; |
| 1441 | } |
| 1442 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1443 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1444 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1445 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1446 | |
| 1447 | final int device = getDeviceForStream(streamType); |
| 1448 | int oldIndex; |
| 1449 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1450 | // skip a2dp absolute volume control request when the device |
| 1451 | // is not an a2dp device |
| 1452 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1453 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1454 | return; |
| 1455 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1456 | // If we are being called by the system (e.g. hardware keys) check for current user |
| 1457 | // so we handle user restrictions correctly. |
| 1458 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1459 | uid = UserHandle.getUid(getCurrentUserId(), UserHandle.getAppId(uid)); |
| 1460 | } |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1461 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1462 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1463 | return; |
| 1464 | } |
| 1465 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1466 | synchronized (mSafeMediaVolumeState) { |
| 1467 | // reset any pending volume command |
| 1468 | mPendingVolumeCommand = null; |
| 1469 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1470 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1471 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1472 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1473 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1474 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1475 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1476 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1477 | synchronized (mA2dpAvrcpLock) { |
| 1478 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1479 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1480 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1484 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1485 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1486 | } |
| 1487 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1488 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1489 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1490 | ((device & mFixedVolumeDevices) != 0)) { |
| 1491 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1492 | |
| 1493 | // volume is either 0 or max allowed for fixed volume devices |
| 1494 | if (index != 0) { |
| 1495 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1496 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1497 | index = mSafeMediaVolumeIndex; |
| 1498 | } else { |
| 1499 | index = streamState.getMaxIndex(); |
| 1500 | } |
| 1501 | } |
| 1502 | } |
| 1503 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1504 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1505 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1506 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1507 | streamType, index, flags, device); |
| 1508 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1509 | onSetStreamVolume(streamType, index, flags, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1510 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1511 | } |
| 1512 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1513 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1516 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1517 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1518 | synchronized(mForceControlStreamLock) { |
| 1519 | mVolumeControlStream = streamType; |
| 1520 | if (mVolumeControlStream == -1) { |
| 1521 | if (mForceControlStreamClient != null) { |
| 1522 | mForceControlStreamClient.release(); |
| 1523 | mForceControlStreamClient = null; |
| 1524 | } |
| 1525 | } else { |
| 1526 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1532 | private IBinder mCb; // To be notified of client's death |
| 1533 | |
| 1534 | ForceControlStreamClient(IBinder cb) { |
| 1535 | if (cb != null) { |
| 1536 | try { |
| 1537 | cb.linkToDeath(this, 0); |
| 1538 | } catch (RemoteException e) { |
| 1539 | // Client has died! |
| 1540 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1541 | cb = null; |
| 1542 | } |
| 1543 | } |
| 1544 | mCb = cb; |
| 1545 | } |
| 1546 | |
| 1547 | public void binderDied() { |
| 1548 | synchronized(mForceControlStreamLock) { |
| 1549 | Log.w(TAG, "SCO client died"); |
| 1550 | if (mForceControlStreamClient != this) { |
| 1551 | Log.w(TAG, "unregistered control stream client died"); |
| 1552 | } else { |
| 1553 | mForceControlStreamClient = null; |
| 1554 | mVolumeControlStream = -1; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | public void release() { |
| 1560 | if (mCb != null) { |
| 1561 | mCb.unlinkToDeath(this, 0); |
| 1562 | mCb = null; |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1567 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1568 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1569 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1570 | final long ident = Binder.clearCallingIdentity(); |
| 1571 | try { |
| 1572 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1573 | } finally { |
| 1574 | Binder.restoreCallingIdentity(ident); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1579 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1580 | final long ident = Binder.clearCallingIdentity(); |
| 1581 | try { |
| 1582 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1583 | } finally { |
| 1584 | Binder.restoreCallingIdentity(ident); |
| 1585 | } |
| 1586 | } |
| 1587 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1588 | private int getCurrentUserId() { |
| 1589 | final long ident = Binder.clearCallingIdentity(); |
| 1590 | try { |
| 1591 | UserInfo currentUser = ActivityManagerNative.getDefault().getCurrentUser(); |
| 1592 | return currentUser.id; |
| 1593 | } catch (RemoteException e) { |
| 1594 | // Activity manager not running, nothing we can do assume user 0. |
| 1595 | } finally { |
| 1596 | Binder.restoreCallingIdentity(ident); |
| 1597 | } |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 1598 | return UserHandle.USER_SYSTEM; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1599 | } |
| 1600 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1601 | // UI update and Broadcast Intent |
| 1602 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
John Spurlock | 72966d6 | 2015-06-18 15:45:07 -0400 | [diff] [blame] | 1603 | streamType = mStreamVolumeAlias[streamType]; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1604 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1605 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1606 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1607 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1608 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1611 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1612 | // receives volume notification from Audio Receiver. |
| 1613 | private int updateFlagsForSystemAudio(int flags) { |
| 1614 | if (mHdmiTvClient != null) { |
| 1615 | synchronized (mHdmiTvClient) { |
| 1616 | if (mHdmiSystemAudioSupported && |
| 1617 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1618 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | return flags; |
| 1623 | } |
| 1624 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1625 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1626 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1627 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1628 | broadcastMasterMuteStatus(muted); |
| 1629 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1630 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1631 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1632 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1633 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1634 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1635 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1636 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1637 | } |
| 1638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | * Sets the stream state's index, and posts a message to set system volume. |
| 1641 | * This will not call out to the UI. Assumes a valid stream type. |
| 1642 | * |
| 1643 | * @param streamType Type of the stream |
| 1644 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1645 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | * @param force If true, set the volume even if the desired volume is same |
| 1647 | * as the current volume. |
| 1648 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1649 | private void setStreamVolumeInt(int streamType, |
| 1650 | int index, |
| 1651 | int device, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1652 | boolean force, |
| 1653 | String caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1655 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 1656 | if (streamState.setIndex(index, device, caller) || force) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1657 | // Post message to set system volume (it in turn will post a message |
| 1658 | // to persist). |
| 1659 | sendMsg(mAudioHandler, |
| 1660 | MSG_SET_DEVICE_VOLUME, |
| 1661 | SENDMSG_QUEUE, |
| 1662 | device, |
| 1663 | 0, |
| 1664 | streamState, |
| 1665 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1669 | private void setSystemAudioMute(boolean state) { |
| 1670 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1671 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1672 | if (!mHdmiSystemAudioSupported) return; |
| 1673 | synchronized (mHdmiTvClient) { |
| 1674 | final long token = Binder.clearCallingIdentity(); |
| 1675 | try { |
| 1676 | mHdmiTvClient.setSystemAudioMute(state); |
| 1677 | } finally { |
| 1678 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1679 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1684 | /** get stream mute state. */ |
| 1685 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1686 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1687 | streamType = getActiveStreamType(streamType); |
| 1688 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1689 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1690 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1691 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1694 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1695 | private IBinder mICallback; // To be notified of client's death |
| 1696 | |
| 1697 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1698 | mICallback = cb; |
| 1699 | try { |
| 1700 | cb.linkToDeath(this, 0/*flags*/); |
| 1701 | } catch (RemoteException e) { |
| 1702 | Log.e(TAG, "can't link to death", e); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | boolean isHandlerFor(IBinder cb) { |
| 1707 | return mICallback.equals(cb); |
| 1708 | } |
| 1709 | |
| 1710 | void forget() { |
| 1711 | try { |
| 1712 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1713 | } catch (NoSuchElementException e) { |
| 1714 | Log.e(TAG, "error unlinking to death", e); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | public void binderDied() { |
| 1719 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1720 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1726 | * @return true if there is a registered death handler, false otherwise */ |
| 1727 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1728 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1729 | while (it.hasNext()) { |
| 1730 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1731 | if (handler.isHandlerFor(cb)) { |
| 1732 | handler.forget(); |
| 1733 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1734 | return true; |
| 1735 | } |
| 1736 | } |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| 1740 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1741 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1742 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1743 | while (it.hasNext()) { |
| 1744 | if (it.next().isHandlerFor(cb)) { |
| 1745 | return true; |
| 1746 | } |
| 1747 | } |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | private int mRmtSbmxFullVolRefCount = 0; |
| 1752 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1753 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1754 | |
| 1755 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1756 | if (cb == null) { |
| 1757 | return; |
| 1758 | } |
| 1759 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1760 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1761 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1762 | return; |
| 1763 | } |
| 1764 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1765 | boolean applyRequired = false; |
| 1766 | if (startForcing) { |
| 1767 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1768 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1769 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1770 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1771 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1772 | applyRequired = true; |
| 1773 | } |
| 1774 | mRmtSbmxFullVolRefCount++; |
| 1775 | } |
| 1776 | } else { |
| 1777 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1778 | mRmtSbmxFullVolRefCount--; |
| 1779 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1780 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1781 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1782 | applyRequired = true; |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | if (applyRequired) { |
| 1787 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1788 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1789 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1794 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid, |
| 1795 | int userId) { |
| 1796 | // If we are being called by the system check for user we are going to change |
| 1797 | // so we handle user restrictions correctly. |
| 1798 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1799 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 1800 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1801 | // If OP_AUDIO_MASTER_VOLUME is set, disallow unmuting. |
| 1802 | if (!mute && mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1803 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1804 | return; |
| 1805 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1806 | if (userId != UserHandle.getCallingUserId() && |
| 1807 | mContext.checkCallingOrSelfPermission( |
| 1808 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1809 | != PackageManager.PERMISSION_GRANTED) { |
| 1810 | return; |
| 1811 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1812 | setMasterMuteInternalNoCallerCheck(mute, flags, userId); |
| 1813 | } |
| 1814 | |
| 1815 | private void setMasterMuteInternalNoCallerCheck(boolean mute, int flags, int userId) { |
| 1816 | if (DEBUG_VOL) { |
| 1817 | Log.d(TAG, String.format("Master mute %s, %d, user=%d", mute, flags, userId)); |
| 1818 | } |
| 1819 | if (mUseFixedVolume) { |
| 1820 | return; // If using fixed volume, we don't mute. |
| 1821 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1822 | if (getCurrentUserId() == userId) { |
| 1823 | if (mute != AudioSystem.getMasterMute()) { |
| 1824 | setSystemAudioMute(mute); |
| 1825 | AudioSystem.setMasterMute(mute); |
| 1826 | // Post a persist master volume msg |
| 1827 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
| 1828 | : 0, userId, null, PERSIST_DELAY); |
| 1829 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1830 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1831 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1832 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
| 1833 | sendBroadcastToAll(intent); |
| 1834 | } |
| 1835 | } else { |
| 1836 | // If not the current user just persist the setting which will be loaded |
| 1837 | // on user switch. |
| 1838 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
| 1839 | : 0, userId, null, PERSIST_DELAY); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1840 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** get master mute state. */ |
| 1844 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1845 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1848 | public void setMasterMute(boolean mute, int flags, String callingPackage, int userId) { |
| 1849 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid(), |
| 1850 | userId); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1851 | } |
| 1852 | |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1853 | /** @hide */ |
| 1854 | public boolean isMasterMono() { |
| 1855 | return AudioSystem.getMasterMono(); |
| 1856 | } |
| 1857 | |
| 1858 | /** @hide */ |
| 1859 | public void setMasterMono(boolean mono, String callingPackage, int userId) { |
| 1860 | int callingUid = Binder.getCallingUid(); |
| 1861 | // If we are being called by the system check for user we are going to change |
| 1862 | // so we handle user restrictions correctly. |
| 1863 | if (callingUid == android.os.Process.SYSTEM_UID) { |
| 1864 | callingUid = UserHandle.getUid(userId, UserHandle.getAppId(callingUid)); |
| 1865 | } |
| 1866 | |
| 1867 | if (userId != UserHandle.getCallingUserId() && |
| 1868 | mContext.checkCallingOrSelfPermission( |
| 1869 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1870 | != PackageManager.PERMISSION_GRANTED) { |
| 1871 | return; |
| 1872 | } |
| 1873 | if (DEBUG_VOL) { |
| 1874 | Log.d(TAG, String.format("Master mono %b, user=%d", mono, userId)); |
| 1875 | } |
| 1876 | |
| 1877 | if (getCurrentUserId() == userId) { |
| 1878 | if (mono != AudioSystem.getMasterMono()) { |
| 1879 | AudioSystem.setMasterMono(mono); |
| 1880 | // Post a persist master mono msg |
| 1881 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_MONO, SENDMSG_REPLACE, mono ? 1 |
| 1882 | : 0 /* value */, userId, null /* obj */, 0 /* delay */); |
| 1883 | // notify apps and settings |
| 1884 | broadcastMasterMonoStatus(mono); |
| 1885 | } |
| 1886 | } else { |
| 1887 | // Post a persist master mono msg |
| 1888 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_MONO, SENDMSG_REPLACE, mono ? 1 |
| 1889 | : 0 /* value */, userId, null /* obj */, 0 /* delay */); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | private void broadcastMasterMonoStatus(boolean mono) { |
| 1894 | Intent intent = new Intent(AudioManager.MASTER_MONO_CHANGED_ACTION); |
| 1895 | intent.putExtra(AudioManager.EXTRA_MASTER_MONO, mono); |
| 1896 | sendBroadcastToAll(intent); |
| 1897 | } |
| 1898 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1899 | /** @see AudioManager#getStreamVolume(int) */ |
| 1900 | public int getStreamVolume(int streamType) { |
| 1901 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1902 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1903 | synchronized (VolumeStreamState.class) { |
| 1904 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1905 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1906 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1907 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1908 | index = 0; |
| 1909 | } |
| 1910 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 1911 | (device & mFixedVolumeDevices) != 0) { |
| 1912 | index = mStreamStates[streamType].getMaxIndex(); |
| 1913 | } |
| 1914 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1915 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1919 | public int getStreamMaxVolume(int streamType) { |
| 1920 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1921 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1924 | /** @see AudioManager#getStreamMinVolume(int) */ |
| 1925 | public int getStreamMinVolume(int streamType) { |
| 1926 | ensureValidStreamType(streamType); |
| 1927 | return (mStreamStates[streamType].getMinIndex() + 5) / 10; |
| 1928 | } |
| 1929 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1930 | /** Get last audible volume before stream was muted. */ |
| 1931 | public int getLastAudibleStreamVolume(int streamType) { |
| 1932 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1933 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1934 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1937 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 1938 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1939 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1942 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 1943 | @Override |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1944 | public void setMicrophoneMute(boolean on, String callingPackage, int userId) { |
| 1945 | // If we are being called by the system check for user we are going to change |
| 1946 | // so we handle user restrictions correctly. |
| 1947 | int uid = Binder.getCallingUid(); |
| 1948 | if (uid == android.os.Process.SYSTEM_UID) { |
| 1949 | uid = UserHandle.getUid(userId, UserHandle.getAppId(uid)); |
| 1950 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1951 | // If OP_MUTE_MICROPHONE is set, disallow unmuting. |
| 1952 | if (!on && mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, uid, callingPackage) |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1953 | != AppOpsManager.MODE_ALLOWED) { |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1954 | return; |
| 1955 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 1956 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 1957 | return; |
| 1958 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1959 | if (userId != UserHandle.getCallingUserId() && |
| 1960 | mContext.checkCallingOrSelfPermission( |
| 1961 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1962 | != PackageManager.PERMISSION_GRANTED) { |
| 1963 | return; |
| 1964 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1965 | setMicrophoneMuteNoCallerCheck(on, userId); |
| 1966 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1967 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1968 | private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) { |
| 1969 | if (DEBUG_VOL) { |
| 1970 | Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId)); |
| 1971 | } |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1972 | // If mute is for current user actually mute, else just persist the setting |
| 1973 | // which will be loaded on user switch. |
| 1974 | if (getCurrentUserId() == userId) { |
| 1975 | AudioSystem.muteMicrophone(on); |
| 1976 | } |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1977 | // Post a persist microphone msg. |
| 1978 | sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1 |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1979 | : 0, userId, null, PERSIST_DELAY); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1980 | } |
| 1981 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1982 | @Override |
| 1983 | public int getRingerModeExternal() { |
| 1984 | synchronized(mSettingsLock) { |
| 1985 | return mRingerModeExternal; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | @Override |
| 1990 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1991 | synchronized(mSettingsLock) { |
| 1992 | return mRingerMode; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1997 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1998 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1999 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2000 | } |
| 2001 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2002 | /** @see AudioManager#isValidRingerMode(int) */ |
| 2003 | public boolean isValidRingerMode(int ringerMode) { |
| 2004 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 2005 | } |
| 2006 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2007 | public void setRingerModeExternal(int ringerMode, String caller) { |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2008 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2012 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2013 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2017 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2018 | return; |
| 2019 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2020 | if (caller == null || caller.length() == 0) { |
| 2021 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 2022 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 2023 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2024 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 2025 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 2026 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2027 | final long identity = Binder.clearCallingIdentity(); |
| 2028 | try { |
| 2029 | synchronized (mSettingsLock) { |
| 2030 | final int ringerModeInternal = getRingerModeInternal(); |
| 2031 | final int ringerModeExternal = getRingerModeExternal(); |
| 2032 | if (external) { |
| 2033 | setRingerModeExt(ringerMode); |
| 2034 | if (mRingerModeDelegate != null) { |
| 2035 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2036 | ringerMode, caller, ringerModeInternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2037 | } |
| 2038 | if (ringerMode != ringerModeInternal) { |
| 2039 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2040 | } |
| 2041 | } else /*internal*/ { |
| 2042 | if (ringerMode != ringerModeInternal) { |
| 2043 | setRingerModeInt(ringerMode, true /*persist*/); |
| 2044 | } |
| 2045 | if (mRingerModeDelegate != null) { |
| 2046 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2047 | ringerMode, caller, ringerModeExternal, mVolumePolicy); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2048 | } |
| 2049 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2050 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2051 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 2052 | } finally { |
| 2053 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2057 | private void setRingerModeExt(int ringerMode) { |
| 2058 | synchronized(mSettingsLock) { |
| 2059 | if (ringerMode == mRingerModeExternal) return; |
| 2060 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2061 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2062 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2063 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 2064 | } |
| 2065 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2066 | private void muteRingerModeStreams() { |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2067 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 2068 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 2069 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 2070 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2071 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2072 | final boolean ringerModeMute = mRingerMode == AudioManager.RINGER_MODE_VIBRATE |
| 2073 | || mRingerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2074 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2075 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 2076 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 2077 | if (isMuted == shouldMute) continue; |
| 2078 | if (!shouldMute) { |
| 2079 | // unmute |
| 2080 | // ring and notifications volume should never be 0 when not silenced |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 2081 | if (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2082 | synchronized (VolumeStreamState.class) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2083 | final VolumeStreamState vss = mStreamStates[streamType]; |
| 2084 | for (int i = 0; i < vss.mIndexMap.size(); i++) { |
| 2085 | int device = vss.mIndexMap.keyAt(i); |
| 2086 | int value = vss.mIndexMap.valueAt(i); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 2087 | if (value == 0) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 2088 | vss.setIndex(10, device, TAG); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2089 | } |
| 2090 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 2091 | // Persist volume for stream ring when it is changed here |
| 2092 | final int device = getDeviceForStream(streamType); |
| 2093 | sendMsg(mAudioHandler, |
| 2094 | MSG_PERSIST_VOLUME, |
| 2095 | SENDMSG_QUEUE, |
| 2096 | device, |
| 2097 | 0, |
| 2098 | mStreamStates[streamType], |
| 2099 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 2100 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2101 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2102 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2103 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2104 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2105 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2106 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2107 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2108 | } |
| 2109 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | private void setRingerModeInt(int ringerMode, boolean persist) { |
| 2113 | final boolean change; |
| 2114 | synchronized(mSettingsLock) { |
| 2115 | change = mRingerMode != ringerMode; |
| 2116 | mRingerMode = ringerMode; |
| 2117 | } |
| 2118 | |
| 2119 | muteRingerModeStreams(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2120 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2121 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2122 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2123 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2124 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 2125 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 2126 | if (change) { |
| 2127 | // Send sticky broadcast |
| 2128 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 2129 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2132 | /** @see AudioManager#shouldVibrate(int) */ |
| 2133 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2134 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2135 | |
| 2136 | switch (getVibrateSetting(vibrateType)) { |
| 2137 | |
| 2138 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2139 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2140 | |
| 2141 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 2142 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2143 | |
| 2144 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 2145 | // return false, even for incoming calls |
| 2146 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2147 | |
| 2148 | default: |
| 2149 | return false; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | /** @see AudioManager#getVibrateSetting(int) */ |
| 2154 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2155 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2156 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 2157 | } |
| 2158 | |
| 2159 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 2160 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 2161 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2162 | if (!mHasVibrator) return; |
| 2163 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 2164 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 2165 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | |
| 2167 | // Broadcast change |
| 2168 | broadcastVibrateSetting(vibrateType); |
| 2169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2170 | } |
| 2171 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2172 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 2173 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2174 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2175 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 2176 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2177 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2178 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2179 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2183 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2184 | synchronized(mSetModeDeathHandlers) { |
| 2185 | Log.w(TAG, "setMode() client died"); |
| 2186 | int index = mSetModeDeathHandlers.indexOf(this); |
| 2187 | if (index < 0) { |
| 2188 | Log.w(TAG, "unregistered setMode() client died"); |
| 2189 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2190 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid, TAG); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2191 | } |
| 2192 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2193 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2194 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2195 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 2196 | final long ident = Binder.clearCallingIdentity(); |
| 2197 | disconnectBluetoothSco(newModeOwnerPid); |
| 2198 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2199 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2200 | } |
| 2201 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2202 | public int getPid() { |
| 2203 | return mPid; |
| 2204 | } |
| 2205 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 2206 | public void setMode(int mode) { |
| 2207 | mMode = mode; |
| 2208 | } |
| 2209 | |
| 2210 | public int getMode() { |
| 2211 | return mMode; |
| 2212 | } |
| 2213 | |
| 2214 | public IBinder getBinder() { |
| 2215 | return mCb; |
| 2216 | } |
| 2217 | } |
| 2218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2219 | /** @see AudioManager#setMode(int) */ |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2220 | public void setMode(int mode, IBinder cb, String callingPackage) { |
| 2221 | 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] | 2222 | if (!checkAudioSettingsPermission("setMode()")) { |
| 2223 | return; |
| 2224 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2225 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 2226 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 2227 | (mContext.checkCallingOrSelfPermission( |
| 2228 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 2229 | != PackageManager.PERMISSION_GRANTED)) { |
| 2230 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 2231 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 2232 | return; |
| 2233 | } |
| 2234 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 2235 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2236 | return; |
| 2237 | } |
| 2238 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2239 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2240 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2241 | if (mode == AudioSystem.MODE_CURRENT) { |
| 2242 | mode = mMode; |
| 2243 | } |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2244 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid(), callingPackage); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2245 | } |
| 2246 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 2247 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2248 | if (newModeOwnerPid != 0) { |
| 2249 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2250 | } |
| 2251 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2252 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2253 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2254 | // 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] | 2255 | // any mode other than NORMAL. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2256 | private int setModeInt(int mode, IBinder cb, int pid, String caller) { |
| 2257 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ", caller=" |
| 2258 | + caller + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2259 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2260 | if (cb == null) { |
| 2261 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2262 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2263 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 2264 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2265 | SetModeDeathHandler hdlr = null; |
| 2266 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 2267 | while (iter.hasNext()) { |
| 2268 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 2269 | if (h.getPid() == pid) { |
| 2270 | hdlr = h; |
| 2271 | // Remove from client list so that it is re-inserted at top of list |
| 2272 | iter.remove(); |
| 2273 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 2274 | break; |
| 2275 | } |
| 2276 | } |
| 2277 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 2278 | do { |
| 2279 | if (mode == AudioSystem.MODE_NORMAL) { |
| 2280 | // get new mode from client at top the list if any |
| 2281 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 2282 | hdlr = mSetModeDeathHandlers.get(0); |
| 2283 | cb = hdlr.getBinder(); |
| 2284 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2285 | if (DEBUG_MODE) { |
| 2286 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 2287 | + hdlr.mPid); |
| 2288 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 2289 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2290 | } else { |
| 2291 | if (hdlr == null) { |
| 2292 | hdlr = new SetModeDeathHandler(cb, pid); |
| 2293 | } |
| 2294 | // Register for client death notification |
| 2295 | try { |
| 2296 | cb.linkToDeath(hdlr, 0); |
| 2297 | } catch (RemoteException e) { |
| 2298 | // Client has died! |
| 2299 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 2300 | } |
| 2301 | |
| 2302 | // Last client to call setMode() is always at top of client list |
| 2303 | // as required by SetModeDeathHandler.binderDied() |
| 2304 | mSetModeDeathHandlers.add(0, hdlr); |
| 2305 | hdlr.setMode(mode); |
| 2306 | } |
| 2307 | |
| 2308 | if (mode != mMode) { |
| 2309 | status = AudioSystem.setPhoneState(mode); |
| 2310 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2311 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2312 | mMode = mode; |
| 2313 | } else { |
| 2314 | if (hdlr != null) { |
| 2315 | mSetModeDeathHandlers.remove(hdlr); |
| 2316 | cb.unlinkToDeath(hdlr, 0); |
| 2317 | } |
| 2318 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2319 | 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] | 2320 | mode = AudioSystem.MODE_NORMAL; |
| 2321 | } |
| 2322 | } else { |
| 2323 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2324 | } |
| 2325 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2326 | |
| 2327 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 2328 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2329 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2330 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2331 | } else { |
| 2332 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2333 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2334 | } |
| 2335 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2336 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2337 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2338 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, caller); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2339 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2340 | updateStreamVolumeAlias(true /*updateVolumes*/, caller); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2341 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2342 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /** @see AudioManager#getMode() */ |
| 2346 | public int getMode() { |
| 2347 | return mMode; |
| 2348 | } |
| 2349 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2350 | //========================================================================================== |
| 2351 | // Sound Effects |
| 2352 | //========================================================================================== |
| 2353 | |
| 2354 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2355 | private static final String ATTR_VERSION = "version"; |
| 2356 | private static final String TAG_GROUP = "group"; |
| 2357 | private static final String ATTR_GROUP_NAME = "name"; |
| 2358 | private static final String TAG_ASSET = "asset"; |
| 2359 | private static final String ATTR_ASSET_ID = "id"; |
| 2360 | private static final String ATTR_ASSET_FILE = "file"; |
| 2361 | |
| 2362 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2363 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2364 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2365 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2366 | |
| 2367 | class LoadSoundEffectReply { |
| 2368 | public int mStatus = 1; |
| 2369 | }; |
| 2370 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2371 | private void loadTouchSoundAssetDefaults() { |
| 2372 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2373 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2374 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2375 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | private void loadTouchSoundAssets() { |
| 2380 | XmlResourceParser parser = null; |
| 2381 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2382 | // only load assets once. |
| 2383 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2384 | return; |
| 2385 | } |
| 2386 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2387 | loadTouchSoundAssetDefaults(); |
| 2388 | |
| 2389 | try { |
| 2390 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2391 | |
| 2392 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2393 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2394 | boolean inTouchSoundsGroup = false; |
| 2395 | |
| 2396 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2397 | while (true) { |
| 2398 | XmlUtils.nextElement(parser); |
| 2399 | String element = parser.getName(); |
| 2400 | if (element == null) { |
| 2401 | break; |
| 2402 | } |
| 2403 | if (element.equals(TAG_GROUP)) { |
| 2404 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2405 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2406 | inTouchSoundsGroup = true; |
| 2407 | break; |
| 2408 | } |
| 2409 | } |
| 2410 | } |
| 2411 | while (inTouchSoundsGroup) { |
| 2412 | XmlUtils.nextElement(parser); |
| 2413 | String element = parser.getName(); |
| 2414 | if (element == null) { |
| 2415 | break; |
| 2416 | } |
| 2417 | if (element.equals(TAG_ASSET)) { |
| 2418 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2419 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2420 | int fx; |
| 2421 | |
| 2422 | try { |
| 2423 | Field field = AudioManager.class.getField(id); |
| 2424 | fx = field.getInt(null); |
| 2425 | } catch (Exception e) { |
| 2426 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2427 | continue; |
| 2428 | } |
| 2429 | |
| 2430 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2431 | if (i == -1) { |
| 2432 | i = SOUND_EFFECT_FILES.size(); |
| 2433 | SOUND_EFFECT_FILES.add(file); |
| 2434 | } |
| 2435 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2436 | } else { |
| 2437 | break; |
| 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | } catch (Resources.NotFoundException e) { |
| 2442 | Log.w(TAG, "audio assets file not found", e); |
| 2443 | } catch (XmlPullParserException e) { |
| 2444 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2445 | } catch (IOException e) { |
| 2446 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2447 | } finally { |
| 2448 | if (parser != null) { |
| 2449 | parser.close(); |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | /** @see AudioManager#playSoundEffect(int) */ |
| 2455 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2456 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2460 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2461 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2462 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2463 | return; |
| 2464 | } |
| 2465 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2466 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2467 | effectType, (int) (volume * 1000), null, 0); |
| 2468 | } |
| 2469 | |
| 2470 | /** |
| 2471 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2472 | * 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] | 2473 | */ |
| 2474 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2475 | int attempts = 3; |
| 2476 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2477 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2478 | synchronized (reply) { |
| 2479 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2480 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2481 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2482 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2483 | } catch (InterruptedException e) { |
| 2484 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2485 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2487 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2488 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * Unloads samples from the sound pool. |
| 2493 | * This method can be called to free some memory when |
| 2494 | * sound effects are disabled. |
| 2495 | */ |
| 2496 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2497 | 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] | 2498 | } |
| 2499 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2500 | class SoundPoolListenerThread extends Thread { |
| 2501 | public SoundPoolListenerThread() { |
| 2502 | super("SoundPoolListenerThread"); |
| 2503 | } |
| 2504 | |
| 2505 | @Override |
| 2506 | public void run() { |
| 2507 | |
| 2508 | Looper.prepare(); |
| 2509 | mSoundPoolLooper = Looper.myLooper(); |
| 2510 | |
| 2511 | synchronized (mSoundEffectsLock) { |
| 2512 | if (mSoundPool != null) { |
| 2513 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2514 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2515 | } |
| 2516 | mSoundEffectsLock.notify(); |
| 2517 | } |
| 2518 | Looper.loop(); |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | private final class SoundPoolCallback implements |
| 2523 | android.media.SoundPool.OnLoadCompleteListener { |
| 2524 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2525 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2526 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2527 | |
| 2528 | public int status() { |
| 2529 | return mStatus; |
| 2530 | } |
| 2531 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2532 | public void setSamples(int[] samples) { |
| 2533 | for (int i = 0; i < samples.length; i++) { |
| 2534 | // do not wait ack for samples rejected upfront by SoundPool |
| 2535 | if (samples[i] > 0) { |
| 2536 | mSamples.add(samples[i]); |
| 2537 | } |
| 2538 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2542 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2543 | int i = mSamples.indexOf(sampleId); |
| 2544 | if (i >= 0) { |
| 2545 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2546 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2547 | if ((status != 0) || mSamples. isEmpty()) { |
| 2548 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2549 | mSoundEffectsLock.notify(); |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2555 | /** @see AudioManager#reloadAudioSettings() */ |
| 2556 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2557 | readAudioSettings(false /*userSwitch*/); |
| 2558 | } |
| 2559 | |
| 2560 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2561 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2562 | readPersistedSettings(); |
| 2563 | |
| 2564 | // restore volume settings |
| 2565 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2566 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2567 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2568 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2569 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2570 | continue; |
| 2571 | } |
| 2572 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2573 | streamState.readSettings(); |
| 2574 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2575 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2576 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2577 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2578 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2579 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2583 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2584 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2585 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2586 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2587 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2588 | checkAllAliasStreamVolumes(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 2589 | checkMuteAffectedStreams(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2590 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2591 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2592 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2593 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2594 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2595 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 2596 | enforceSafeMediaVolume(TAG); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2597 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2598 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2601 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2602 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2603 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2604 | return; |
| 2605 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2606 | |
| 2607 | if (on) { |
| 2608 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2609 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2610 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2611 | } |
| 2612 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2613 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2614 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2615 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2616 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2617 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2618 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2622 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2623 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2626 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2627 | public void setBluetoothScoOn(boolean on) { |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2628 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2629 | return; |
| 2630 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2631 | setBluetoothScoOnInt(on); |
| 2632 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2633 | |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2634 | public void setBluetoothScoOnInt(boolean on) { |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2635 | if (on) { |
| 2636 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2637 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2638 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2639 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2640 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2641 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2642 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2643 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2644 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2648 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2649 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2652 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2653 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2654 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2655 | mBluetoothA2dpEnabled = on; |
| 2656 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2657 | AudioSystem.FOR_MEDIA, |
| 2658 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2659 | null, 0); |
| 2660 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2664 | public boolean isBluetoothA2dpOn() { |
| 2665 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2666 | return mBluetoothA2dpEnabled; |
| 2667 | } |
| 2668 | } |
| 2669 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2670 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2671 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2672 | int scoAudioMode = |
| 2673 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2674 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2675 | startBluetoothScoInt(cb, scoAudioMode); |
| 2676 | } |
| 2677 | |
| 2678 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2679 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2680 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2681 | } |
| 2682 | |
| 2683 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2684 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2685 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2686 | return; |
| 2687 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2688 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2689 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2690 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2691 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2692 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2693 | // client is created. |
| 2694 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2695 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2696 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | /** @see AudioManager#stopBluetoothSco() */ |
| 2700 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2701 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2702 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2703 | return; |
| 2704 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2705 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2706 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2707 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2708 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2709 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2710 | if (client != null) { |
| 2711 | client.decCount(); |
| 2712 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2713 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2716 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2717 | private class ScoClient implements IBinder.DeathRecipient { |
| 2718 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2719 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2720 | private int mStartcount; // number of SCO connections started by this client |
| 2721 | |
| 2722 | ScoClient(IBinder cb) { |
| 2723 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2724 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2725 | mStartcount = 0; |
| 2726 | } |
| 2727 | |
| 2728 | public void binderDied() { |
| 2729 | synchronized(mScoClients) { |
| 2730 | Log.w(TAG, "SCO client died"); |
| 2731 | int index = mScoClients.indexOf(this); |
| 2732 | if (index < 0) { |
| 2733 | Log.w(TAG, "unregistered SCO client died"); |
| 2734 | } else { |
| 2735 | clearCount(true); |
| 2736 | mScoClients.remove(this); |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2741 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2742 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2743 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2744 | if (mStartcount == 0) { |
| 2745 | try { |
| 2746 | mCb.linkToDeath(this, 0); |
| 2747 | } catch (RemoteException e) { |
| 2748 | // client has already died! |
| 2749 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2750 | } |
| 2751 | } |
| 2752 | mStartcount++; |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | public void decCount() { |
| 2757 | synchronized(mScoClients) { |
| 2758 | if (mStartcount == 0) { |
| 2759 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2760 | } else { |
| 2761 | mStartcount--; |
| 2762 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2763 | try { |
| 2764 | mCb.unlinkToDeath(this, 0); |
| 2765 | } catch (NoSuchElementException e) { |
| 2766 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2767 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2768 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2769 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | public void clearCount(boolean stopSco) { |
| 2775 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2776 | if (mStartcount != 0) { |
| 2777 | try { |
| 2778 | mCb.unlinkToDeath(this, 0); |
| 2779 | } catch (NoSuchElementException e) { |
| 2780 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2781 | } |
| 2782 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2783 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2784 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2785 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | public int getCount() { |
| 2791 | return mStartcount; |
| 2792 | } |
| 2793 | |
| 2794 | public IBinder getBinder() { |
| 2795 | return mCb; |
| 2796 | } |
| 2797 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2798 | public int getPid() { |
| 2799 | return mCreatorPid; |
| 2800 | } |
| 2801 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2802 | public int totalCount() { |
| 2803 | synchronized(mScoClients) { |
| 2804 | int count = 0; |
| 2805 | int size = mScoClients.size(); |
| 2806 | for (int i = 0; i < size; i++) { |
| 2807 | count += mScoClients.get(i).getCount(); |
| 2808 | } |
| 2809 | return count; |
| 2810 | } |
| 2811 | } |
| 2812 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2813 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2814 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2815 | if (totalCount() == 0) { |
| 2816 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2817 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2818 | // the connection. |
| 2819 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2820 | // Accept SCO audio activation only in NORMAL audio mode or if the mode is |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2821 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2822 | synchronized(mSetModeDeathHandlers) { |
| 2823 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2824 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2825 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2826 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2827 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2828 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2829 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2830 | if (mBluetoothHeadsetDevice != null) { |
| 2831 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2832 | mContentResolver, |
| 2833 | "bluetooth_sco_channel_"+ |
| 2834 | mBluetoothHeadsetDevice.getAddress(), |
| 2835 | SCO_MODE_VIRTUAL_CALL)); |
| 2836 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2837 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2838 | } |
| 2839 | } else { |
| 2840 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2841 | } |
| 2842 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2843 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2844 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2845 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2846 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2847 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2848 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2849 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2850 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2851 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2852 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2853 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2854 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2855 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2856 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2857 | } else { |
| 2858 | broadcastScoConnectionState( |
| 2859 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2860 | } |
| 2861 | } else if (getBluetoothHeadset()) { |
| 2862 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2863 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2864 | } else { |
| 2865 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2866 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2867 | } |
| 2868 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2869 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2870 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2871 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2872 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2873 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2874 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2875 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2876 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2877 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2878 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2879 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2880 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2881 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2882 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2883 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2884 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2885 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2886 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2887 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2888 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2889 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2890 | broadcastScoConnectionState( |
| 2891 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2892 | } |
| 2893 | } else if (getBluetoothHeadset()) { |
| 2894 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2895 | } |
| 2896 | } else { |
| 2897 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2898 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2899 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2905 | private void checkScoAudioState() { |
| 2906 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2907 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2908 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2909 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2910 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2911 | } |
| 2912 | } |
| 2913 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2914 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2915 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2916 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2917 | int size = mScoClients.size(); |
| 2918 | for (int i = 0; i < size; i++) { |
| 2919 | client = mScoClients.get(i); |
| 2920 | if (client.getBinder() == cb) |
| 2921 | return client; |
| 2922 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2923 | if (create) { |
| 2924 | client = new ScoClient(cb); |
| 2925 | mScoClients.add(client); |
| 2926 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2927 | return client; |
| 2928 | } |
| 2929 | } |
| 2930 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2931 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2932 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2933 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2934 | int size = mScoClients.size(); |
| 2935 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2936 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2937 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2938 | cl.clearCount(stopSco); |
| 2939 | } else { |
| 2940 | savedClient = cl; |
| 2941 | } |
| 2942 | } |
| 2943 | mScoClients.clear(); |
| 2944 | if (savedClient != null) { |
| 2945 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | } |
| 2949 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2950 | private boolean getBluetoothHeadset() { |
| 2951 | boolean result = false; |
| 2952 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2953 | if (adapter != null) { |
| 2954 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2955 | BluetoothProfile.HEADSET); |
| 2956 | } |
| 2957 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2958 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2959 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2960 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2961 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2962 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2963 | return result; |
| 2964 | } |
| 2965 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2966 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2967 | synchronized(mScoClients) { |
| 2968 | checkScoAudioState(); |
| 2969 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2970 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2971 | if (mBluetoothHeadsetDevice != null) { |
| 2972 | if (mBluetoothHeadset != null) { |
| 2973 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2974 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2975 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2976 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2977 | } |
| 2978 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2979 | getBluetoothHeadset()) { |
| 2980 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2981 | } |
| 2982 | } |
| 2983 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2984 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | private void resetBluetoothSco() { |
| 2990 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2991 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2992 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2993 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2994 | } |
Eric Laurent | 4822125 | 2015-09-24 18:41:48 -0700 | [diff] [blame] | 2995 | AudioSystem.setParameters("A2dpSuspended=false"); |
| 2996 | setBluetoothScoOnInt(false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 3000 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 3001 | SENDMSG_QUEUE, state, 0, null, 0); |
| 3002 | } |
| 3003 | |
| 3004 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3005 | if (state != mScoConnectionState) { |
| 3006 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 3007 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 3008 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 3009 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3010 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3011 | mScoConnectionState = state; |
| 3012 | } |
| 3013 | } |
| 3014 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3015 | void setBtScoDeviceConnectionState(BluetoothDevice btDevice, int state) { |
| 3016 | if (btDevice == null) { |
| 3017 | return; |
| 3018 | } |
| 3019 | |
| 3020 | String address = btDevice.getAddress(); |
| 3021 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 3022 | int outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 3023 | int inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 3024 | if (btClass != null) { |
| 3025 | switch (btClass.getDeviceClass()) { |
| 3026 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 3027 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 3028 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
| 3029 | break; |
| 3030 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 3031 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
| 3032 | break; |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 3037 | address = ""; |
| 3038 | } |
| 3039 | |
| 3040 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
| 3041 | |
| 3042 | String btDeviceName = btDevice.getName(); |
| 3043 | boolean success = |
| 3044 | handleDeviceConnection(connected, outDevice, address, btDeviceName) && |
| 3045 | handleDeviceConnection(connected, inDevice, address, btDeviceName); |
| 3046 | if (success) { |
| 3047 | synchronized (mScoClients) { |
| 3048 | if (connected) { |
| 3049 | mBluetoothHeadsetDevice = btDevice; |
| 3050 | } else { |
| 3051 | mBluetoothHeadsetDevice = null; |
| 3052 | resetBluetoothSco(); |
| 3053 | } |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3058 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 3059 | new BluetoothProfile.ServiceListener() { |
| 3060 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3061 | BluetoothDevice btDevice; |
| 3062 | List<BluetoothDevice> deviceList; |
| 3063 | switch(profile) { |
| 3064 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3065 | synchronized (mConnectedDevices) { |
| 3066 | synchronized (mA2dpAvrcpLock) { |
| 3067 | mA2dp = (BluetoothA2dp) proxy; |
| 3068 | deviceList = mA2dp.getConnectedDevices(); |
| 3069 | if (deviceList.size() > 0) { |
| 3070 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3071 | int state = mA2dp.getConnectionState(btDevice); |
| 3072 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3073 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3074 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3075 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3076 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3077 | state, |
| 3078 | 0, |
| 3079 | btDevice, |
| 3080 | delay); |
| 3081 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3082 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 3083 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3084 | break; |
| 3085 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3086 | case BluetoothProfile.A2DP_SINK: |
| 3087 | deviceList = proxy.getConnectedDevices(); |
| 3088 | if (deviceList.size() > 0) { |
| 3089 | btDevice = deviceList.get(0); |
| 3090 | synchronized (mConnectedDevices) { |
| 3091 | int state = proxy.getConnectionState(btDevice); |
| 3092 | queueMsgUnderWakeLock(mAudioHandler, |
| 3093 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 3094 | state, |
| 3095 | 0, |
| 3096 | btDevice, |
| 3097 | 0 /* delay */); |
| 3098 | } |
| 3099 | } |
| 3100 | break; |
| 3101 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3102 | case BluetoothProfile.HEADSET: |
| 3103 | synchronized (mScoClients) { |
| 3104 | // Discard timeout message |
| 3105 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 3106 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 3107 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 3108 | if (deviceList.size() > 0) { |
| 3109 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 3110 | } else { |
| 3111 | mBluetoothHeadsetDevice = null; |
| 3112 | } |
| 3113 | // Refresh SCO audio state |
| 3114 | checkScoAudioState(); |
| 3115 | // Continue pending action if any |
| 3116 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 3117 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 3118 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 3119 | boolean status = false; |
| 3120 | if (mBluetoothHeadsetDevice != null) { |
| 3121 | switch (mScoAudioState) { |
| 3122 | case SCO_STATE_ACTIVATE_REQ: |
| 3123 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3124 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3125 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3126 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3127 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 3128 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3129 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3130 | status = mBluetoothHeadset.startVoiceRecognition( |
| 3131 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3132 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3133 | break; |
| 3134 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3135 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 3136 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3137 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3138 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 3139 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 3140 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 3141 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3142 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 3143 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3144 | break; |
| 3145 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 3146 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 3147 | mBluetoothHeadsetDevice); |
| 3148 | } |
| 3149 | } |
| 3150 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3151 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3152 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3153 | } |
| 3154 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 3155 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3156 | break; |
| 3157 | |
| 3158 | default: |
| 3159 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3160 | } |
| 3161 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 3162 | public void onServiceDisconnected(int profile) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3163 | ArraySet<String> toRemove = null; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3164 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3165 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 3166 | synchronized (mConnectedDevices) { |
| 3167 | synchronized (mA2dpAvrcpLock) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3168 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3169 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 3170 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3171 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3172 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3173 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3174 | } |
| 3175 | } |
| 3176 | if (toRemove != null) { |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 3177 | int delay = checkSendBecomingNoisyIntent( |
| 3178 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3179 | 0); |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3180 | for (int i = 0; i < toRemove.size(); i++) { |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 3181 | makeA2dpDeviceUnavailableLater(toRemove.valueAt(i), delay); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3182 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 3183 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | break; |
| 3187 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3188 | case BluetoothProfile.A2DP_SINK: |
| 3189 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3190 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3191 | for(int i = 0; i < mConnectedDevices.size(); i++) { |
| 3192 | DeviceListSpec deviceSpec = mConnectedDevices.valueAt(i); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3193 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 3194 | toRemove = toRemove != null ? toRemove : new ArraySet<String>(); |
| 3195 | toRemove.add(deviceSpec.mDeviceAddress); |
| 3196 | } |
| 3197 | } |
| 3198 | if (toRemove != null) { |
| 3199 | for (int i = 0; i < toRemove.size(); i++) { |
| 3200 | makeA2dpSrcUnavailable(toRemove.valueAt(i)); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3201 | } |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | break; |
| 3205 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3206 | case BluetoothProfile.HEADSET: |
| 3207 | synchronized (mScoClients) { |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 3208 | if (mBluetoothHeadsetDevice != null) { |
| 3209 | setBtScoDeviceConnectionState(mBluetoothHeadsetDevice, |
| 3210 | BluetoothProfile.STATE_DISCONNECTED); |
| 3211 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3212 | mBluetoothHeadset = null; |
| 3213 | } |
| 3214 | break; |
| 3215 | |
| 3216 | default: |
| 3217 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3221 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3222 | private void onCheckMusicActive(String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3223 | synchronized (mSafeMediaVolumeState) { |
| 3224 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3225 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 3226 | |
| 3227 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 3228 | sendMsg(mAudioHandler, |
| 3229 | MSG_CHECK_MUSIC_ACTIVE, |
| 3230 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3231 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3232 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3233 | caller, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3234 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3235 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 3236 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 3237 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3238 | // Approximate cumulative active music time |
| 3239 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 3240 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3241 | setSafeMediaVolumeEnabled(true, caller); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3242 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3243 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3244 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3251 | private void saveMusicActiveMs() { |
| 3252 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 3253 | } |
| 3254 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3255 | private void onConfigureSafeVolume(boolean force, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3256 | synchronized (mSafeMediaVolumeState) { |
| 3257 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 3258 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 3259 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 3260 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 3261 | boolean safeMediaVolumeEnabled = |
| 3262 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 3263 | || mContext.getResources().getBoolean( |
| 3264 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3265 | |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3266 | boolean safeMediaVolumeBypass = |
| 3267 | SystemProperties.getBoolean("audio.safemedia.bypass", false); |
| 3268 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3269 | // The persisted state is either "disabled" or "active": this is the state applied |
| 3270 | // next time we boot and cannot be "inactive" |
| 3271 | int persistedState; |
Ricardo Garcia | 3a30a76 | 2015-06-23 15:54:45 -0700 | [diff] [blame] | 3272 | if (safeMediaVolumeEnabled && !safeMediaVolumeBypass) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3273 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 3274 | // The state can already be "inactive" here if the user has forced it before |
| 3275 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 3276 | // it to "active". |
| 3277 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3278 | if (mMusicActiveMs == 0) { |
| 3279 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3280 | enforceSafeMediaVolume(caller); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 3281 | } else { |
| 3282 | // We have existing playback time recorded, already confirmed. |
| 3283 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 3284 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3285 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3286 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3287 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3288 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 3289 | } |
| 3290 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3291 | sendMsg(mAudioHandler, |
| 3292 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 3293 | SENDMSG_QUEUE, |
| 3294 | persistedState, |
| 3295 | 0, |
| 3296 | null, |
| 3297 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | /////////////////////////////////////////////////////////////////////////// |
| 3303 | // Internal methods |
| 3304 | /////////////////////////////////////////////////////////////////////////// |
| 3305 | |
| 3306 | /** |
| 3307 | * Checks if the adjustment should change ringer mode instead of just |
| 3308 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 3309 | * indices on the stream states. |
| 3310 | */ |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3311 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted) { |
| 3312 | final boolean isTv = mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3313 | int result = FLAG_ADJUST_VOLUME; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3314 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3316 | switch (ringerMode) { |
| 3317 | case RINGER_MODE_NORMAL: |
| 3318 | if (direction == AudioManager.ADJUST_LOWER) { |
| 3319 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 3320 | // "step" is the delta in internal index units corresponding to a |
| 3321 | // change of 1 in UI index units. |
| 3322 | // Because of rounding when rescaling from one stream index range to its alias |
| 3323 | // index range, we cannot simply test oldIndex == step: |
| 3324 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 3325 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3326 | ringerMode = RINGER_MODE_VIBRATE; |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3327 | mLoweredFromNormalToVibrateTime = SystemClock.uptimeMillis(); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3328 | } |
| 3329 | } else { |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3330 | if (oldIndex == step && mVolumePolicy.volumeDownToEnterSilent) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3331 | ringerMode = RINGER_MODE_SILENT; |
| 3332 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 3333 | } |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3334 | } else if (isTv && (direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3335 | || direction == AudioManager.ADJUST_MUTE)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3336 | if (mHasVibrator) { |
| 3337 | ringerMode = RINGER_MODE_VIBRATE; |
| 3338 | } else { |
| 3339 | ringerMode = RINGER_MODE_SILENT; |
| 3340 | } |
| 3341 | // Setting the ringer mode will toggle mute |
| 3342 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3343 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3344 | break; |
| 3345 | case RINGER_MODE_VIBRATE: |
| 3346 | if (!mHasVibrator) { |
| 3347 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 3348 | "but no vibrator is present"); |
| 3349 | break; |
| 3350 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3351 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3352 | // This is the case we were muted with the volume turned up |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3353 | if (isTv && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3354 | ringerMode = RINGER_MODE_NORMAL; |
| 3355 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3356 | if (mVolumePolicy.volumeDownToEnterSilent) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3357 | final long diff = SystemClock.uptimeMillis() |
| 3358 | - mLoweredFromNormalToVibrateTime; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 3359 | if (diff > mVolumePolicy.vibrateToSilentDebounce |
| 3360 | && mRingerModeDelegate.canVolumeDownEnterSilent()) { |
John Spurlock | 07e7243 | 2015-03-13 11:46:52 -0400 | [diff] [blame] | 3361 | ringerMode = RINGER_MODE_SILENT; |
| 3362 | } |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 3363 | } else { |
| 3364 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 3365 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3366 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3367 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3368 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3369 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3370 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 3371 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3372 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3373 | break; |
| 3374 | case RINGER_MODE_SILENT: |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3375 | if (isTv && direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3376 | // This is the case we were muted with the volume turned up |
| 3377 | ringerMode = RINGER_MODE_NORMAL; |
| 3378 | } else if (direction == AudioManager.ADJUST_RAISE |
| 3379 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 3380 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 3381 | if (!mVolumePolicy.volumeUpToExitSilent) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3382 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3383 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3384 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3385 | ringerMode = RINGER_MODE_VIBRATE; |
| 3386 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 3387 | // If we don't have a vibrator or they were toggling mute |
| 3388 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3389 | ringerMode = RINGER_MODE_NORMAL; |
| 3390 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3391 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3392 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3393 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3394 | break; |
| 3395 | default: |
| 3396 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3397 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3400 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3401 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3402 | mPrevVolDirection = direction; |
| 3403 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3404 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3405 | } |
| 3406 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3407 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3408 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3409 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3410 | } |
| 3411 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3412 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3413 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3414 | } |
| 3415 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3416 | private boolean updateRingerModeAffectedStreams() { |
| 3417 | int ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3418 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3419 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3420 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3421 | UserHandle.USER_CURRENT); |
| 3422 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 3423 | if (mPlatformType == AudioSystem.PLATFORM_TELEVISION) { |
| 3424 | ringerModeAffectedStreams = 0; |
| 3425 | } else if (mRingerModeDelegate != null) { |
| 3426 | ringerModeAffectedStreams = mRingerModeDelegate |
| 3427 | .getRingerModeAffectedStreams(ringerModeAffectedStreams); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3428 | } |
| 3429 | synchronized (mCameraSoundForced) { |
| 3430 | if (mCameraSoundForced) { |
| 3431 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3432 | } else { |
| 3433 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3434 | } |
| 3435 | } |
| 3436 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3437 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3438 | } else { |
| 3439 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3440 | } |
| 3441 | |
| 3442 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3443 | Settings.System.putIntForUser(mContentResolver, |
| 3444 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3445 | ringerModeAffectedStreams, |
| 3446 | UserHandle.USER_CURRENT); |
| 3447 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3448 | return true; |
| 3449 | } |
| 3450 | return false; |
| 3451 | } |
| 3452 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3453 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3454 | public boolean isStreamAffectedByMute(int streamType) { |
| 3455 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3456 | } |
| 3457 | |
| 3458 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3459 | switch (direction) { |
| 3460 | case AudioManager.ADJUST_LOWER: |
| 3461 | case AudioManager.ADJUST_RAISE: |
| 3462 | case AudioManager.ADJUST_SAME: |
| 3463 | case AudioManager.ADJUST_MUTE: |
| 3464 | case AudioManager.ADJUST_UNMUTE: |
| 3465 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3466 | break; |
| 3467 | default: |
| 3468 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | private void ensureValidStreamType(int streamType) { |
| 3473 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3474 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3475 | } |
| 3476 | } |
| 3477 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3478 | private boolean isMuteAdjust(int adjust) { |
| 3479 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3480 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3481 | } |
| 3482 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3483 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3484 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3485 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3486 | TelecomManager telecomManager = |
| 3487 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3488 | |
| 3489 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3490 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3491 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3492 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3493 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3494 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3495 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3496 | /** |
| 3497 | * For code clarity for getActiveStreamType(int) |
| 3498 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3499 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3500 | * in the last "delay_ms" ms. |
| 3501 | */ |
| 3502 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3503 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3504 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3505 | } |
| 3506 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3507 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3508 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3509 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3510 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3511 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3512 | == AudioSystem.FORCE_BT_SCO) { |
| 3513 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3514 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3515 | } else { |
| 3516 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3517 | return AudioSystem.STREAM_VOICE_CALL; |
| 3518 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3519 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3520 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3521 | if (DEBUG_VOL) |
| 3522 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3523 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3524 | } else { |
| 3525 | if (DEBUG_VOL) |
| 3526 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3527 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3528 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3529 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3530 | if (DEBUG_VOL) |
| 3531 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3532 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3533 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3534 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3535 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3536 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 3537 | // TV always defaults to STREAM_MUSIC |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3538 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3539 | } |
| 3540 | break; |
| 3541 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3542 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3543 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3544 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3545 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3546 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3547 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3548 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3549 | return AudioSystem.STREAM_VOICE_CALL; |
| 3550 | } |
| 3551 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3552 | StreamOverride.sDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3553 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3554 | StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3555 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3556 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3557 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3558 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3559 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3560 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3561 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3562 | if (DEBUG_VOL) Log.v(TAG, |
| 3563 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3564 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3565 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3566 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3567 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3569 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3570 | + suggestedStreamType); |
| 3571 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3572 | } |
| 3573 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3574 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3575 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3576 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3577 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3578 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3579 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3580 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | private void broadcastVibrateSetting(int vibrateType) { |
| 3584 | // Send broadcast |
| 3585 | if (ActivityManagerNative.isSystemReady()) { |
| 3586 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3587 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3588 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3589 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3594 | /** |
| 3595 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3596 | * Note that the wake lock needs to be released after the message has been handled. |
| 3597 | */ |
| 3598 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3599 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3600 | final long ident = Binder.clearCallingIdentity(); |
| 3601 | // Always acquire the wake lock as AudioService because it is released by the |
| 3602 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3603 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3604 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3605 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3607 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3608 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3609 | 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] | 3610 | |
| 3611 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3612 | handler.removeMessages(msg); |
| 3613 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3614 | return; |
| 3615 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3616 | synchronized (mLastDeviceConnectMsgTime) { |
| 3617 | long time = SystemClock.uptimeMillis() + delay; |
| 3618 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3619 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3620 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3621 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3622 | mLastDeviceConnectMsgTime = time; |
| 3623 | } |
| 3624 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3628 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3629 | == PackageManager.PERMISSION_GRANTED) { |
| 3630 | return true; |
| 3631 | } |
| 3632 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3633 | + Binder.getCallingPid() |
| 3634 | + ", uid=" + Binder.getCallingUid(); |
| 3635 | Log.w(TAG, msg); |
| 3636 | return false; |
| 3637 | } |
| 3638 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3639 | private int getDeviceForStream(int stream) { |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3640 | int device = getDevicesForStream(stream); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3641 | if ((device & (device - 1)) != 0) { |
| 3642 | // Multiple device selection is either: |
| 3643 | // - speaker + one other device: give priority to speaker in this case. |
| 3644 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3645 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3646 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3647 | // - 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] | 3648 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3649 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3650 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3651 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3652 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3653 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3654 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3655 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3656 | } else { |
| 3657 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3658 | } |
| 3659 | } |
| 3660 | return device; |
| 3661 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3662 | |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3663 | private int getDevicesForStream(int stream) { |
| 3664 | return getDevicesForStream(stream, true /*checkOthers*/); |
| 3665 | } |
| 3666 | |
| 3667 | private int getDevicesForStream(int stream, boolean checkOthers) { |
| 3668 | ensureValidStreamType(stream); |
| 3669 | synchronized (VolumeStreamState.class) { |
| 3670 | return mStreamStates[stream].observeDevicesForStream_syncVSS(checkOthers); |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | private void observeDevicesForStreams(int skipStream) { |
| 3675 | synchronized (VolumeStreamState.class) { |
| 3676 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 3677 | if (stream != skipStream) { |
| 3678 | mStreamStates[stream].observeDevicesForStream_syncVSS(false /*checkOthers*/); |
| 3679 | } |
| 3680 | } |
| 3681 | } |
| 3682 | } |
| 3683 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3684 | /* |
| 3685 | * A class just for packaging up a set of connection parameters. |
| 3686 | */ |
| 3687 | private class WiredDeviceConnectionState { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3688 | public final int mType; |
| 3689 | public final int mState; |
| 3690 | public final String mAddress; |
| 3691 | public final String mName; |
| 3692 | public final String mCaller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3693 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3694 | public WiredDeviceConnectionState(int type, int state, String address, String name, |
| 3695 | String caller) { |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3696 | mType = type; |
| 3697 | mState = state; |
| 3698 | mAddress = address; |
| 3699 | mName = name; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3700 | mCaller = caller; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3704 | public void setWiredDeviceConnectionState(int type, int state, String address, String name, |
| 3705 | String caller) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3706 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3707 | if (DEBUG_DEVICES) { |
| 3708 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 3709 | + address + ")"); |
| 3710 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3711 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3712 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3713 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3714 | 0, |
| 3715 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3716 | new WiredDeviceConnectionState(type, state, address, name, caller), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3717 | delay); |
| 3718 | } |
| 3719 | } |
| 3720 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3721 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3722 | { |
| 3723 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3724 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3725 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3726 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3727 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3728 | if (profile == BluetoothProfile.A2DP) { |
| 3729 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3730 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3731 | } else { |
| 3732 | delay = 0; |
| 3733 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3734 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3735 | (profile == BluetoothProfile.A2DP ? |
| 3736 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3737 | state, |
| 3738 | 0, |
| 3739 | device, |
| 3740 | delay); |
| 3741 | } |
| 3742 | return delay; |
| 3743 | } |
| 3744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | /////////////////////////////////////////////////////////////////////////// |
| 3746 | // Inner classes |
| 3747 | /////////////////////////////////////////////////////////////////////////// |
| 3748 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3749 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3750 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3751 | // 2 mSetModeDeathHandlers |
| 3752 | // 3 mSettingsLock |
| 3753 | // 4 VolumeStreamState.class |
| 3754 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3755 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | private final int mStreamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3757 | private final int mIndexMin; |
| 3758 | private final int mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3759 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3760 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3761 | private String mVolumeIndexSettingName; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3762 | private int mObservedDevices; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3763 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3764 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3765 | private final Intent mVolumeChanged; |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3766 | private final Intent mStreamDevicesChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3767 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3768 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3770 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3771 | |
| 3772 | mStreamType = streamType; |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3773 | mIndexMin = MIN_STREAM_VOLUME[streamType] * 10; |
| 3774 | mIndexMax = MAX_STREAM_VOLUME[streamType] * 10; |
| 3775 | AudioSystem.initStreamVolume(streamType, mIndexMin / 10, mIndexMax / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3776 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3777 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3778 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3779 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 3780 | mStreamDevicesChanged = new Intent(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 3781 | mStreamDevicesChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3782 | } |
| 3783 | |
| 3784 | public int observeDevicesForStream_syncVSS(boolean checkOthers) { |
| 3785 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 3786 | if (devices == mObservedDevices) { |
| 3787 | return devices; |
| 3788 | } |
| 3789 | final int prevDevices = mObservedDevices; |
| 3790 | mObservedDevices = devices; |
| 3791 | if (checkOthers) { |
| 3792 | // one stream's devices have changed, check the others |
| 3793 | observeDevicesForStreams(mStreamType); |
| 3794 | } |
| 3795 | // log base stream changes to the event log |
| 3796 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 3797 | EventLogTags.writeStreamDevicesChanged(mStreamType, prevDevices, devices); |
| 3798 | } |
| 3799 | sendBroadcastToAll(mStreamDevicesChanged |
| 3800 | .putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_DEVICES, prevDevices) |
| 3801 | .putExtra(AudioManager.EXTRA_VOLUME_STREAM_DEVICES, devices)); |
| 3802 | return devices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3803 | } |
| 3804 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3805 | public String getSettingNameForDevice(int device) { |
| 3806 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3807 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3808 | if (suffix.isEmpty()) { |
| 3809 | return name; |
| 3810 | } |
| 3811 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3814 | public void readSettings() { |
| 3815 | synchronized (VolumeStreamState.class) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3816 | // force maximum volume on all streams if fixed volume property is set |
| 3817 | if (mUseFixedVolume) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3818 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3819 | return; |
| 3820 | } |
| 3821 | // do not read system stream volume from settings: this stream is always aliased |
| 3822 | // to another stream type and its volume is never persisted. Values in settings can |
| 3823 | // only be stale values |
| 3824 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3825 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3826 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3827 | synchronized (mCameraSoundForced) { |
| 3828 | if (mCameraSoundForced) { |
| 3829 | index = mIndexMax; |
| 3830 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3831 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3832 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3833 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3834 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3835 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3836 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3837 | |
| 3838 | for (int i = 0; remainingDevices != 0; i++) { |
| 3839 | int device = (1 << i); |
| 3840 | if ((device & remainingDevices) == 0) { |
| 3841 | continue; |
| 3842 | } |
| 3843 | remainingDevices &= ~device; |
| 3844 | |
| 3845 | // retrieve current volume for device |
| 3846 | String name = getSettingNameForDevice(device); |
| 3847 | // if no volume stored for current stream and device, use default volume if default |
| 3848 | // device, continue otherwise |
| 3849 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3850 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3851 | int index = Settings.System.getIntForUser( |
| 3852 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3853 | if (index == -1) { |
| 3854 | continue; |
| 3855 | } |
| 3856 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3857 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3858 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3859 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3860 | } |
| 3861 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3862 | // must be called while synchronized VolumeStreamState.class |
| 3863 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3864 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3865 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3866 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3867 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) |
| 3868 | || ((device & mFullVolumeDevices) != 0)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3869 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3870 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3871 | index = (getIndex(device) + 5)/10; |
| 3872 | } |
| 3873 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3874 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3875 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3876 | public void applyAllVolumes() { |
| 3877 | synchronized (VolumeStreamState.class) { |
| 3878 | // apply default volume first: by convention this will reset all |
| 3879 | // devices volumes in audio policy manager to the supplied value |
| 3880 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3881 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3882 | index = 0; |
| 3883 | } else { |
| 3884 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3885 | } |
| 3886 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3887 | // then apply device specific volumes |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3888 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3889 | int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3890 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3891 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3892 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3893 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3894 | mAvrcpAbsVolSupported) |
| 3895 | || ((device & mFullVolumeDevices) != 0)) |
| 3896 | { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3897 | index = (mIndexMax + 5)/10; |
| 3898 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3899 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3900 | } |
| 3901 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3902 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3907 | public boolean adjustIndex(int deltaIndex, int device, String caller) { |
| 3908 | return setIndex(getIndex(device) + deltaIndex, device, caller); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3909 | } |
| 3910 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3911 | public boolean setIndex(int index, int device, String caller) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3912 | boolean changed = false; |
| 3913 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3914 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3915 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3916 | index = getValidIndex(index); |
| 3917 | synchronized (mCameraSoundForced) { |
| 3918 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3919 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3920 | } |
| 3921 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3922 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3923 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3924 | changed = oldIndex != index; |
| 3925 | if (changed) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3926 | // Apply change to all streams using this one as alias |
| 3927 | // if changing volume of current device, also change volume of current |
| 3928 | // device on aliased stream |
| 3929 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3930 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3931 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3932 | if (streamType != mStreamType && |
| 3933 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3934 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3935 | mStreamStates[streamType].setIndex(scaledIndex, device, caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3936 | if (currentDevice) { |
| 3937 | mStreamStates[streamType].setIndex(scaledIndex, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3938 | getDeviceForStream(streamType), caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3939 | } |
| 3940 | } |
| 3941 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3942 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3943 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3944 | if (changed) { |
| 3945 | oldIndex = (oldIndex + 5) / 10; |
| 3946 | index = (index + 5) / 10; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3947 | // log base stream changes to the event log |
| 3948 | if (mStreamVolumeAlias[mStreamType] == mStreamType) { |
| 3949 | if (caller == null) { |
| 3950 | Log.w(TAG, "No caller for volume_changed event", new Throwable()); |
| 3951 | } |
| 3952 | EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10, |
| 3953 | caller); |
| 3954 | } |
| 3955 | // fire changed intents for all streams |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3956 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 3957 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 3958 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, |
| 3959 | mStreamVolumeAlias[mStreamType]); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3960 | sendBroadcastToAll(mVolumeChanged); |
| 3961 | } |
| 3962 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3963 | } |
| 3964 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3965 | public int getIndex(int device) { |
| 3966 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3967 | int index = mIndexMap.get(device, -1); |
| 3968 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3969 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3970 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3971 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3972 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3973 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3976 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3977 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 3980 | public int getMinIndex() { |
| 3981 | return mIndexMin; |
| 3982 | } |
| 3983 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 3984 | public void setAllIndexes(VolumeStreamState srcStream, String caller) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3985 | synchronized (VolumeStreamState.class) { |
| 3986 | int srcStreamType = srcStream.getStreamType(); |
| 3987 | // apply default device volume from source stream to all devices first in case |
| 3988 | // some devices are present in this stream state but not in source stream state |
| 3989 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3990 | index = rescaleIndex(index, srcStreamType, mStreamType); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3991 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3992 | mIndexMap.put(mIndexMap.keyAt(i), index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3993 | } |
| 3994 | // Now apply actual volume for devices in source stream state |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3995 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 3996 | for (int i = 0; i < srcMap.size(); i++) { |
| 3997 | int device = srcMap.keyAt(i); |
| 3998 | index = srcMap.valueAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3999 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 4000 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4001 | setIndex(index, device, caller); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4002 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4006 | public void setAllIndexesToMax() { |
| 4007 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4008 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4009 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4010 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4011 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4012 | } |
| 4013 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4014 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4015 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4016 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4017 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4018 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4019 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4020 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4021 | // Set the new mute volume. This propagates the values to |
| 4022 | // the audio system, otherwise the volume won't be changed |
| 4023 | // at the lower level. |
| 4024 | sendMsg(mAudioHandler, |
| 4025 | MSG_SET_ALL_VOLUMES, |
| 4026 | SENDMSG_QUEUE, |
| 4027 | 0, |
| 4028 | 0, |
| 4029 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 4030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4031 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 4032 | if (changed) { |
| 4033 | // Stream mute changed, fire the intent. |
| 4034 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 4035 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 4036 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 4037 | sendBroadcastToAll(intent); |
| 4038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4039 | } |
| 4040 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4041 | public int getStreamType() { |
| 4042 | return mStreamType; |
| 4043 | } |
| 4044 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4045 | public void checkFixedVolumeDevices() { |
| 4046 | synchronized (VolumeStreamState.class) { |
| 4047 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 4048 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4049 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4050 | int device = mIndexMap.keyAt(i); |
| 4051 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 4052 | if (((device & mFullVolumeDevices) != 0) |
| 4053 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4054 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4055 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4056 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4057 | } |
| 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4062 | private int getValidIndex(int index) { |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4063 | if (index < mIndexMin) { |
| 4064 | return mIndexMin; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 4065 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4066 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | return index; |
| 4070 | } |
| 4071 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4072 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 4073 | pw.print(" Muted: "); |
| 4074 | pw.println(mIsMuted); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 4075 | pw.print(" Min: "); |
| 4076 | pw.println((mIndexMin + 5) / 10); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4077 | pw.print(" Max: "); |
| 4078 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4079 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4080 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 4081 | if (i > 0) { |
| 4082 | pw.print(", "); |
| 4083 | } |
| 4084 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4085 | pw.print(Integer.toHexString(device)); |
| 4086 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 4087 | : AudioSystem.getOutputDeviceName(device); |
| 4088 | if (!deviceName.isEmpty()) { |
| 4089 | pw.print(" ("); |
| 4090 | pw.print(deviceName); |
| 4091 | pw.print(")"); |
| 4092 | } |
| 4093 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 4094 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 4095 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4096 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4097 | pw.println(); |
| 4098 | pw.print(" Devices: "); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4099 | final int devices = getDevicesForStream(mStreamType); |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4100 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 4101 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 4102 | // (the default device is not returned by getDevicesForStream) |
| 4103 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 4104 | if ((devices & device) != 0) { |
| 4105 | if (n++ > 0) { |
| 4106 | pw.print(", "); |
| 4107 | } |
| 4108 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 4109 | } |
| 4110 | i++; |
| 4111 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 4112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | /** Thread that handles native AudioSystem control. */ |
| 4116 | private class AudioSystemThread extends Thread { |
| 4117 | AudioSystemThread() { |
| 4118 | super("AudioService"); |
| 4119 | } |
| 4120 | |
| 4121 | @Override |
| 4122 | public void run() { |
| 4123 | // Set this thread up so the handler will work on it |
| 4124 | Looper.prepare(); |
| 4125 | |
| 4126 | synchronized(AudioService.this) { |
| 4127 | mAudioHandler = new AudioHandler(); |
| 4128 | |
| 4129 | // Notify that the handler has been created |
| 4130 | AudioService.this.notify(); |
| 4131 | } |
| 4132 | |
| 4133 | // Listen for volume change requests that are set by VolumePanel |
| 4134 | Looper.loop(); |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | /** Handles internal volume messages in separate volume thread. */ |
| 4139 | private class AudioHandler extends Handler { |
| 4140 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4141 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4142 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4143 | synchronized (VolumeStreamState.class) { |
| 4144 | // Apply volume |
| 4145 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4146 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4147 | // Apply change to all streams using this one as alias |
| 4148 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4149 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4150 | if (streamType != streamState.mStreamType && |
| 4151 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 4152 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 4153 | // that may have a different device selected |
| 4154 | int streamDevice = getDeviceForStream(streamType); |
| 4155 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 4156 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 4157 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 4158 | } |
| 4159 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4160 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4161 | } |
| 4162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4163 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4164 | sendMsg(mAudioHandler, |
| 4165 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 4166 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4167 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4168 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4169 | streamState, |
| 4170 | PERSIST_DELAY); |
| 4171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4172 | } |
| 4173 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4174 | private void setAllVolumes(VolumeStreamState streamState) { |
| 4175 | |
| 4176 | // Apply volume |
| 4177 | streamState.applyAllVolumes(); |
| 4178 | |
| 4179 | // Apply change to all streams using this one as alias |
| 4180 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4181 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 4182 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 4183 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4184 | mStreamStates[streamType].applyAllVolumes(); |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4189 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4190 | if (mUseFixedVolume) { |
| 4191 | return; |
| 4192 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4193 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 4194 | return; |
| 4195 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4196 | System.putIntForUser(mContentResolver, |
| 4197 | streamState.getSettingNameForDevice(device), |
| 4198 | (streamState.getIndex(device) + 5)/ 10, |
| 4199 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4200 | } |
| 4201 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4202 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4203 | if (mUseFixedVolume) { |
| 4204 | return; |
| 4205 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 4206 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4209 | private boolean onLoadSoundEffects() { |
| 4210 | int status; |
| 4211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4212 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4213 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4214 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 4215 | return false; |
| 4216 | } |
| 4217 | |
| 4218 | if (mSoundPool != null) { |
| 4219 | return true; |
| 4220 | } |
| 4221 | |
| 4222 | loadTouchSoundAssets(); |
| 4223 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 4224 | mSoundPool = new SoundPool.Builder() |
| 4225 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 4226 | .setAudioAttributes(new AudioAttributes.Builder() |
| 4227 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 4228 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 4229 | .build()) |
| 4230 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4231 | mSoundPoolCallBack = null; |
| 4232 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 4233 | mSoundPoolListenerThread.start(); |
| 4234 | int attempts = 3; |
| 4235 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 4236 | try { |
| 4237 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4238 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4239 | } catch (InterruptedException e) { |
| 4240 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | if (mSoundPoolCallBack == null) { |
| 4245 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 4246 | if (mSoundPoolLooper != null) { |
| 4247 | mSoundPoolLooper.quit(); |
| 4248 | mSoundPoolLooper = null; |
| 4249 | } |
| 4250 | mSoundPoolListenerThread = null; |
| 4251 | mSoundPool.release(); |
| 4252 | mSoundPool = null; |
| 4253 | return false; |
| 4254 | } |
| 4255 | /* |
| 4256 | * poolId table: The value -1 in this table indicates that corresponding |
| 4257 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 4258 | * Once loaded, the value in poolId is the sample ID and the same |
| 4259 | * sample can be reused for another effect using the same file. |
| 4260 | */ |
| 4261 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4262 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4263 | poolId[fileIdx] = -1; |
| 4264 | } |
| 4265 | /* |
| 4266 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 4267 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 4268 | * this indicates we have a valid sample loaded for this effect. |
| 4269 | */ |
| 4270 | |
| 4271 | int numSamples = 0; |
| 4272 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4273 | // Do not load sample if this effect uses the MediaPlayer |
| 4274 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 4275 | continue; |
| 4276 | } |
| 4277 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 4278 | String filePath = Environment.getRootDirectory() |
| 4279 | + SOUND_EFFECTS_PATH |
| 4280 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 4281 | int sampleId = mSoundPool.load(filePath, 0); |
| 4282 | if (sampleId <= 0) { |
| 4283 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 4284 | } else { |
| 4285 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 4286 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 4287 | numSamples++; |
| 4288 | } |
| 4289 | } else { |
| 4290 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 4291 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 4292 | } |
| 4293 | } |
| 4294 | // wait for all samples to be loaded |
| 4295 | if (numSamples > 0) { |
| 4296 | mSoundPoolCallBack.setSamples(poolId); |
| 4297 | |
| 4298 | attempts = 3; |
| 4299 | status = 1; |
| 4300 | while ((status == 1) && (attempts-- > 0)) { |
| 4301 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 4302 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4303 | status = mSoundPoolCallBack.status(); |
| 4304 | } catch (InterruptedException e) { |
| 4305 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 4306 | } |
| 4307 | } |
| 4308 | } else { |
| 4309 | status = -1; |
| 4310 | } |
| 4311 | |
| 4312 | if (mSoundPoolLooper != null) { |
| 4313 | mSoundPoolLooper.quit(); |
| 4314 | mSoundPoolLooper = null; |
| 4315 | } |
| 4316 | mSoundPoolListenerThread = null; |
| 4317 | if (status != 0) { |
| 4318 | Log.w(TAG, |
| 4319 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 4320 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4321 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 4322 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | mSoundPool.release(); |
| 4327 | mSoundPool = null; |
| 4328 | } |
| 4329 | } |
| 4330 | return (status == 0); |
| 4331 | } |
| 4332 | |
| 4333 | /** |
| 4334 | * Unloads samples from the sound pool. |
| 4335 | * This method can be called to free some memory when |
| 4336 | * sound effects are disabled. |
| 4337 | */ |
| 4338 | private void onUnloadSoundEffects() { |
| 4339 | synchronized (mSoundEffectsLock) { |
| 4340 | if (mSoundPool == null) { |
| 4341 | return; |
| 4342 | } |
| 4343 | |
| 4344 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 4345 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 4346 | poolId[fileIdx] = 0; |
| 4347 | } |
| 4348 | |
| 4349 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 4350 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 4351 | continue; |
| 4352 | } |
| 4353 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 4354 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 4355 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 4356 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 4357 | } |
| 4358 | } |
| 4359 | mSoundPool.release(); |
| 4360 | mSoundPool = null; |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | private void onPlaySoundEffect(int effectType, int volume) { |
| 4365 | synchronized (mSoundEffectsLock) { |
| 4366 | |
| 4367 | onLoadSoundEffects(); |
| 4368 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4369 | if (mSoundPool == null) { |
| 4370 | return; |
| 4371 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4372 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 4373 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4374 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 4375 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4376 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 4377 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 4378 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4379 | |
| 4380 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4381 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 4382 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4383 | } else { |
| 4384 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4385 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 4386 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 4387 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4388 | mediaPlayer.setDataSource(filePath); |
| 4389 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 4390 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 4391 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 4392 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 4393 | public void onCompletion(MediaPlayer mp) { |
| 4394 | cleanupPlayer(mp); |
| 4395 | } |
| 4396 | }); |
| 4397 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 4398 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 4399 | cleanupPlayer(mp); |
| 4400 | return true; |
| 4401 | } |
| 4402 | }); |
| 4403 | mediaPlayer.start(); |
| 4404 | } catch (IOException ex) { |
| 4405 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 4406 | } catch (IllegalArgumentException ex) { |
| 4407 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 4408 | } catch (IllegalStateException ex) { |
| 4409 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | private void cleanupPlayer(MediaPlayer mp) { |
| 4416 | if (mp != null) { |
| 4417 | try { |
| 4418 | mp.stop(); |
| 4419 | mp.release(); |
| 4420 | } catch (IllegalStateException ex) { |
| 4421 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 4422 | } |
| 4423 | } |
| 4424 | } |
| 4425 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4426 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4427 | synchronized (mConnectedDevices) { |
| 4428 | setForceUseInt_SyncDevices(usage, config); |
| 4429 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4430 | } |
| 4431 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4432 | private void onPersistSafeVolumeState(int state) { |
| 4433 | Settings.Global.putInt(mContentResolver, |
| 4434 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 4435 | state); |
| 4436 | } |
| 4437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4438 | @Override |
| 4439 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4440 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4441 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4442 | case MSG_SET_DEVICE_VOLUME: |
| 4443 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4444 | break; |
| 4445 | |
| 4446 | case MSG_SET_ALL_VOLUMES: |
| 4447 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4448 | break; |
| 4449 | |
| 4450 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4451 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4452 | break; |
| 4453 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4454 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4455 | if (mUseFixedVolume) { |
| 4456 | return; |
| 4457 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4458 | Settings.System.putIntForUser(mContentResolver, |
| 4459 | Settings.System.VOLUME_MASTER_MUTE, |
| 4460 | msg.arg1, |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4461 | msg.arg2); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4462 | break; |
| 4463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4464 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4465 | // note that the value persisted is the current ringer mode, not the |
| 4466 | // 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] | 4467 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4468 | break; |
| 4469 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 4470 | case MSG_AUDIO_SERVER_DIED: |
| 4471 | onAudioServerDied(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4472 | break; |
| 4473 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4474 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4475 | onUnloadSoundEffects(); |
| 4476 | break; |
| 4477 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4478 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4479 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4480 | // can take several dozens of milliseconds to complete |
| 4481 | boolean loaded = onLoadSoundEffects(); |
| 4482 | if (msg.obj != null) { |
| 4483 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4484 | synchronized (reply) { |
| 4485 | reply.mStatus = loaded ? 0 : -1; |
| 4486 | reply.notify(); |
| 4487 | } |
| 4488 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4489 | break; |
| 4490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4491 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4492 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4493 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4494 | |
| 4495 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4496 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4497 | synchronized (mConnectedDevices) { |
| 4498 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4499 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4500 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4501 | |
| 4502 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4503 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4504 | setForceUse(msg.arg1, msg.arg2); |
| 4505 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4506 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4507 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4508 | resetBluetoothSco(); |
| 4509 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4510 | |
| 4511 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4512 | { WiredDeviceConnectionState connectState = |
| 4513 | (WiredDeviceConnectionState)msg.obj; |
| 4514 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4515 | connectState.mAddress, connectState.mName, connectState.mCaller); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4516 | mAudioEventWakeLock.release(); |
| 4517 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4518 | break; |
| 4519 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4520 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4521 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4522 | mAudioEventWakeLock.release(); |
| 4523 | break; |
| 4524 | |
| 4525 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4526 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4527 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4528 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4529 | |
| 4530 | case MSG_REPORT_NEW_ROUTES: { |
| 4531 | int N = mRoutesObservers.beginBroadcast(); |
| 4532 | if (N > 0) { |
| 4533 | AudioRoutesInfo routes; |
| 4534 | synchronized (mCurAudioRoutes) { |
| 4535 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4536 | } |
| 4537 | while (N > 0) { |
| 4538 | N--; |
| 4539 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4540 | try { |
| 4541 | obs.dispatchAudioRoutesChanged(routes); |
| 4542 | } catch (RemoteException e) { |
| 4543 | } |
| 4544 | } |
| 4545 | } |
| 4546 | mRoutesObservers.finishBroadcast(); |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 4547 | observeDevicesForStreams(-1); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4548 | break; |
| 4549 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4550 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4551 | case MSG_CHECK_MUSIC_ACTIVE: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4552 | onCheckMusicActive((String) msg.obj); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4553 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4554 | |
| 4555 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4556 | onSendBecomingNoisyIntent(); |
| 4557 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4558 | |
| 4559 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4560 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4561 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED), |
| 4562 | (String) msg.obj); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4563 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4564 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4565 | onPersistSafeVolumeState(msg.arg1); |
| 4566 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4567 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4568 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4569 | onBroadcastScoConnectionState(msg.arg1); |
| 4570 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4571 | |
| 4572 | case MSG_SYSTEM_READY: |
| 4573 | onSystemReady(); |
| 4574 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4575 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 4576 | case MSG_INDICATE_SYSTEM_READY: |
| 4577 | onIndicateSystemReady(); |
| 4578 | break; |
| 4579 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4580 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4581 | final int musicActiveMs = msg.arg1; |
| 4582 | Settings.Secure.putIntForUser(mContentResolver, |
| 4583 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4584 | UserHandle.USER_CURRENT); |
| 4585 | break; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4586 | case MSG_PERSIST_MICROPHONE_MUTE: |
| 4587 | Settings.System.putIntForUser(mContentResolver, |
| 4588 | Settings.System.MICROPHONE_MUTE, |
| 4589 | msg.arg1, |
| 4590 | msg.arg2); |
| 4591 | break; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4592 | case MSG_UNMUTE_STREAM: |
| 4593 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4594 | break; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 4595 | case MSG_DYN_POLICY_MIX_STATE_UPDATE: |
| 4596 | onDynPolicyMixStateUpdate((String) msg.obj, msg.arg1); |
| 4597 | break; |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 4598 | |
| 4599 | case MSG_PERSIST_MASTER_MONO: |
| 4600 | Settings.System.putIntForUser(mContentResolver, |
| 4601 | Settings.System.MASTER_MONO, |
| 4602 | msg.arg1 /* value */, |
| 4603 | msg.arg2 /* userHandle */); |
| 4604 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4605 | } |
| 4606 | } |
| 4607 | } |
| 4608 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4609 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4610 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4611 | SettingsObserver() { |
| 4612 | super(new Handler()); |
| 4613 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4614 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4615 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4616 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | @Override |
| 4620 | public void onChange(boolean selfChange) { |
| 4621 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4622 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4623 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4624 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4625 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4626 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4627 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4628 | /* |
| 4629 | * Ensure all stream types that should be affected by ringer mode |
| 4630 | * are in the proper state. |
| 4631 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4632 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4633 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4634 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4635 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4636 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4637 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4638 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4639 | // must be called synchronized on mConnectedDevices |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4640 | private void makeA2dpDeviceAvailable(String address, String name) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4641 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4642 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4643 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4644 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4645 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4646 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4647 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4648 | AudioSystem.DEVICE_STATE_AVAILABLE, address, name); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4649 | // Reset A2DP suspend state each time a new sink is connected |
| 4650 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4651 | mConnectedDevices.put( |
| 4652 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4653 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, name, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4654 | address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4657 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4658 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4661 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4662 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4663 | synchronized (mA2dpAvrcpLock) { |
| 4664 | mAvrcpAbsVolSupported = false; |
| 4665 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4666 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4667 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4668 | mConnectedDevices.remove( |
| 4669 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4670 | synchronized (mCurAudioRoutes) { |
| 4671 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4672 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4673 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4674 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4675 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4676 | } |
| 4677 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4678 | } |
| 4679 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4680 | // must be called synchronized on mConnectedDevices |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4681 | private void makeA2dpDeviceUnavailableLater(String address, int delayMs) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4682 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4683 | // reconnection of the sink. |
| 4684 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4685 | // the device will be made unavailable later, so consider it disconnected right away |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4686 | mConnectedDevices.remove( |
| 4687 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4688 | // send the delayed message to make the device unavailable later |
| 4689 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4690 | mAudioHandler.sendMessageDelayed(msg, delayMs); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4691 | |
| 4692 | } |
| 4693 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4694 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4695 | private void makeA2dpSrcAvailable(String address) { |
| 4696 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4697 | AudioSystem.DEVICE_STATE_AVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4698 | mConnectedDevices.put( |
| 4699 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4700 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, "", |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4701 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | // must be called synchronized on mConnectedDevices |
| 4705 | private void makeA2dpSrcUnavailable(String address) { |
| 4706 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4707 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, ""); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4708 | mConnectedDevices.remove( |
| 4709 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4713 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4714 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4715 | } |
| 4716 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4717 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4718 | private boolean hasScheduledA2dpDockTimeout() { |
| 4719 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4720 | } |
| 4721 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4722 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4723 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4724 | if (DEBUG_VOL) { |
| 4725 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4726 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4727 | if (btDevice == null) { |
| 4728 | return; |
| 4729 | } |
| 4730 | String address = btDevice.getAddress(); |
| 4731 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4732 | address = ""; |
| 4733 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4734 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4735 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4736 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4737 | btDevice.getAddress()); |
| 4738 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4739 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4740 | |
| 4741 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4742 | if (btDevice.isBluetoothDock()) { |
| 4743 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4744 | // introduction of a delay for transient disconnections of docks when |
| 4745 | // power is rapidly turned off/on, this message will be canceled if |
| 4746 | // we reconnect the dock under a preset delay |
Eric Laurent | d138e4e | 2015-05-15 16:41:15 -0700 | [diff] [blame] | 4747 | makeA2dpDeviceUnavailableLater(address, BTA2DP_DOCK_TIMEOUT_MILLIS); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4748 | // the next time isConnected is evaluated, it will be false for the dock |
| 4749 | } |
| 4750 | } else { |
| 4751 | makeA2dpDeviceUnavailableNow(address); |
| 4752 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4753 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4754 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4755 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4756 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4757 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4758 | } |
| 4759 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4760 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4761 | if (btDevice.isBluetoothDock()) { |
| 4762 | // this could be a reconnection after a transient disconnection |
| 4763 | cancelA2dpDeviceTimeout(); |
| 4764 | mDockAddress = address; |
| 4765 | } else { |
| 4766 | // this could be a connection of another A2DP device before the timeout of |
| 4767 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4768 | if(hasScheduledA2dpDockTimeout()) { |
| 4769 | cancelA2dpDeviceTimeout(); |
| 4770 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4771 | } |
| 4772 | } |
Paul McLean | 20eec5b | 2015-05-09 13:02:18 -0700 | [diff] [blame] | 4773 | makeA2dpDeviceAvailable(address, btDevice.getName()); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4774 | synchronized (mCurAudioRoutes) { |
| 4775 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4776 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4777 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4778 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4779 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4780 | } |
| 4781 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4782 | } |
| 4783 | } |
| 4784 | } |
| 4785 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4786 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4787 | { |
| 4788 | if (DEBUG_VOL) { |
| 4789 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4790 | } |
| 4791 | if (btDevice == null) { |
| 4792 | return; |
| 4793 | } |
| 4794 | String address = btDevice.getAddress(); |
| 4795 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4796 | address = ""; |
| 4797 | } |
| 4798 | |
| 4799 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4800 | String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 4801 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4802 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4803 | |
| 4804 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4805 | makeA2dpSrcUnavailable(address); |
| 4806 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4807 | makeA2dpSrcAvailable(address); |
| 4808 | } |
| 4809 | } |
| 4810 | } |
| 4811 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4812 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4813 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4814 | synchronized (mA2dpAvrcpLock) { |
| 4815 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4816 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4817 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4818 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4819 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4820 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4821 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4825 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 4826 | String deviceName) { |
| 4827 | if (DEBUG_DEVICES) { |
| 4828 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 4829 | + " address:" + address + " name:" + deviceName + ")"); |
| 4830 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4831 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4832 | String deviceKey = makeDeviceListKey(device, address); |
| 4833 | if (DEBUG_DEVICES) { |
| 4834 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 4835 | } |
| 4836 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 4837 | boolean isConnected = deviceSpec != null; |
| 4838 | if (DEBUG_DEVICES) { |
| 4839 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 4840 | } |
| 4841 | if (connect && !isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 4842 | final int res = AudioSystem.setDeviceConnectionState(device, |
| 4843 | AudioSystem.DEVICE_STATE_AVAILABLE, address, deviceName); |
| 4844 | if (res != AudioSystem.AUDIO_STATUS_OK) { |
| 4845 | Slog.e(TAG, "not connecting device 0x" + Integer.toHexString(device) + |
| 4846 | " due to command error " + res ); |
| 4847 | return false; |
| 4848 | } |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4849 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
| 4850 | return true; |
| 4851 | } else if (!connect && isConnected) { |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 4852 | AudioSystem.setDeviceConnectionState(device, |
| 4853 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, deviceName); |
| 4854 | // always remove even if disconnection failed |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4855 | mConnectedDevices.remove(deviceKey); |
| 4856 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | return false; |
| 4860 | } |
| 4861 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4862 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4863 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4864 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4865 | int mBecomingNoisyIntentDevices = |
| 4866 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4867 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4868 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4869 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4870 | |
| 4871 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4872 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4873 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4874 | int delay = 0; |
| 4875 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4876 | int devices = 0; |
John Spurlock | 8c3dc85 | 2015-04-23 21:32:37 -0400 | [diff] [blame] | 4877 | for (int i = 0; i < mConnectedDevices.size(); i++) { |
| 4878 | int dev = mConnectedDevices.valueAt(i).mDeviceType; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4879 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 4880 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 4881 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4885 | sendMsg(mAudioHandler, |
| 4886 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4887 | SENDMSG_REPLACE, |
| 4888 | 0, |
| 4889 | 0, |
| 4890 | null, |
| 4891 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4892 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4896 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4897 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4898 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4899 | synchronized (mLastDeviceConnectMsgTime) { |
| 4900 | long time = SystemClock.uptimeMillis(); |
| 4901 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 4902 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4903 | } |
| 4904 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4905 | } |
| 4906 | return delay; |
| 4907 | } |
| 4908 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4909 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 4910 | String deviceName) { |
| 4911 | if (DEBUG_DEVICES) { |
| 4912 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 4913 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 4914 | " name:" + deviceName + ");"); |
| 4915 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4916 | Intent intent = new Intent(); |
| 4917 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4918 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 4919 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 4920 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 4921 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4922 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4923 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4924 | int connType = 0; |
| 4925 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4926 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4927 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4928 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4929 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4930 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 4931 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 4932 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4933 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4934 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4935 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4936 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 4937 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4938 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4939 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4940 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 4941 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4942 | } |
| 4943 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4944 | synchronized (mCurAudioRoutes) { |
| 4945 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4946 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4947 | if (state != 0) { |
| 4948 | newConn |= connType; |
| 4949 | } else { |
| 4950 | newConn &= ~connType; |
| 4951 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4952 | if (newConn != mCurAudioRoutes.mainType) { |
| 4953 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4954 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4955 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4956 | } |
| 4957 | } |
| 4958 | } |
| 4959 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4960 | final long ident = Binder.clearCallingIdentity(); |
| 4961 | try { |
| 4962 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4963 | } finally { |
| 4964 | Binder.restoreCallingIdentity(ident); |
| 4965 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4966 | } |
| 4967 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4968 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4969 | String deviceName, String caller) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4970 | if (DEBUG_DEVICES) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 4971 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) |
| 4972 | + " state:" + Integer.toHexString(state) |
| 4973 | + " address:" + address |
| 4974 | + " deviceName:" + deviceName |
| 4975 | + " caller: " + caller + ");"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4976 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4977 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4978 | synchronized (mConnectedDevices) { |
| 4979 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4980 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4981 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4982 | setBluetoothA2dpOnInt(true); |
| 4983 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4984 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4985 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4986 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Jean-Michel Trivi | 6d00e41 | 2015-08-03 17:26:01 -0700 | [diff] [blame] | 4987 | if (!handleDeviceConnection(state == 1, device, address, deviceName)) { |
| 4988 | // change of connection state failed, bailout |
| 4989 | return; |
| 4990 | } |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4991 | if (state != 0) { |
| 4992 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4993 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4994 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4995 | setBluetoothA2dpOnInt(false); |
| 4996 | } |
| 4997 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4998 | sendMsg(mAudioHandler, |
| 4999 | MSG_CHECK_MUSIC_ACTIVE, |
| 5000 | SENDMSG_REPLACE, |
| 5001 | 0, |
| 5002 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5003 | caller, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 5004 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5005 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5006 | // Television devices without CEC service apply software volume on HDMI output |
| 5007 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5008 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 5009 | checkAllFixedVolumeDevices(); |
| 5010 | if (mHdmiManager != null) { |
| 5011 | synchronized (mHdmiManager) { |
| 5012 | if (mHdmiPlaybackClient != null) { |
| 5013 | mHdmiCecSink = false; |
| 5014 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 5015 | } |
| 5016 | } |
| 5017 | } |
| 5018 | } |
| 5019 | } else { |
| 5020 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 5021 | if (mHdmiManager != null) { |
| 5022 | synchronized (mHdmiManager) { |
| 5023 | mHdmiCecSink = false; |
| 5024 | } |
| 5025 | } |
| 5026 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5027 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5028 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 5029 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 5030 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5034 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5035 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 5036 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5037 | if (state == 1) { |
| 5038 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 5039 | int[] portGeneration = new int[1]; |
| 5040 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 5041 | if (status == AudioManager.SUCCESS) { |
| 5042 | for (AudioPort port : ports) { |
| 5043 | if (port instanceof AudioDevicePort) { |
| 5044 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 5045 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 5046 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5047 | // format the list of supported encodings |
Eric Laurent | cae3466 | 2015-05-19 16:46:52 -0700 | [diff] [blame] | 5048 | int[] formats = AudioFormat.filterPublicFormats(devicePort.formats()); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5049 | if (formats.length > 0) { |
| 5050 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 5051 | for (int format : formats) { |
| 5052 | // a format in the list can be 0, skip it |
| 5053 | if (format != AudioFormat.ENCODING_INVALID) { |
| 5054 | encodingList.add(format); |
| 5055 | } |
| 5056 | } |
| 5057 | int[] encodingArray = new int[encodingList.size()]; |
| 5058 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 5059 | encodingArray[i] = encodingList.get(i); |
| 5060 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5061 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5062 | } |
| 5063 | // find the maximum supported number of channels |
| 5064 | int maxChannels = 0; |
| 5065 | for (int mask : devicePort.channelMasks()) { |
| 5066 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 5067 | if (channelCount > maxChannels) { |
| 5068 | maxChannels = channelCount; |
| 5069 | } |
| 5070 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 5071 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | } |
| 5075 | } |
| 5076 | } |
| 5077 | } |
| 5078 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 5079 | /* cache of the address of the last dock the device was connected to */ |
| 5080 | private String mDockAddress; |
| 5081 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5082 | /** |
| 5083 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 5084 | */ |
| 5085 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 5086 | @Override |
| 5087 | public void onReceive(Context context, Intent intent) { |
| 5088 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 5089 | int outDevice; |
| 5090 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5091 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5092 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5093 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 5094 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 5095 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 5096 | int config; |
| 5097 | switch (dockState) { |
| 5098 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 5099 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 5100 | break; |
| 5101 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 5102 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 5103 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5104 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5105 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 5106 | break; |
| 5107 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 5108 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 5109 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 5110 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 5111 | default: |
| 5112 | config = AudioSystem.FORCE_NONE; |
| 5113 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 5114 | // Low end docks have a menu to enable or disable audio |
| 5115 | // (see mDockAudioMediaEnabled) |
| 5116 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 5117 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 5118 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 5119 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 5120 | } |
| 5121 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5122 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5123 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 5124 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 5125 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 5126 | |
Eric Laurent | 98859b2 | 2015-06-12 14:35:59 -0700 | [diff] [blame] | 5127 | setBtScoDeviceConnectionState(btDevice, state); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 5128 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5129 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5130 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5131 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5132 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5133 | // broadcast intent if the connection was initated by AudioService |
| 5134 | if (!mScoClients.isEmpty() && |
| 5135 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 5136 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 5137 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5138 | broadcast = true; |
| 5139 | } |
| 5140 | switch (btState) { |
| 5141 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5142 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5143 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5144 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5145 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5146 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5147 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5148 | break; |
| 5149 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5150 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5151 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 5152 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5153 | break; |
| 5154 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5155 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 5156 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 5157 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5158 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5159 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5160 | default: |
| 5161 | // do not broadcast CONNECTING or invalid state |
| 5162 | broadcast = false; |
| 5163 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 5164 | } |
| 5165 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5166 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5167 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 5168 | //FIXME: this is to maintain compatibility with deprecated intent |
| 5169 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5170 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5171 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 5172 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 5173 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5174 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5175 | if (mMonitorRotation) { |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5176 | RotationHelper.enable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5177 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5178 | AudioSystem.setParameters("screen_state=on"); |
| 5179 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5180 | if (mMonitorRotation) { |
| 5181 | //reduce wakeups (save current) by only listening when display is on |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5182 | RotationHelper.disable(); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 5183 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 5184 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 5185 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5186 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5187 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Yasuhiro Matsuda | 4ced719 | 2015-07-10 22:08:44 +0900 | [diff] [blame] | 5188 | if (mUserSwitchedReceived) { |
| 5189 | // attempt to stop music playback for background user except on first user |
| 5190 | // switch (i.e. first boot) |
| 5191 | sendMsg(mAudioHandler, |
| 5192 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 5193 | SENDMSG_REPLACE, |
| 5194 | 0, |
| 5195 | 0, |
| 5196 | null, |
| 5197 | 0); |
| 5198 | } |
| 5199 | mUserSwitchedReceived = true; |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5200 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5201 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 5202 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 5203 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 5204 | readAudioSettings(true /*userSwitch*/); |
| 5205 | // preserve STREAM_MUSIC volume from one user to the next. |
| 5206 | sendMsg(mAudioHandler, |
| 5207 | MSG_SET_ALL_VOLUMES, |
| 5208 | SENDMSG_QUEUE, |
| 5209 | 0, |
| 5210 | 0, |
| 5211 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5212 | } else if (action.equals(Intent.ACTION_USER_BACKGROUND)) { |
| 5213 | // Disable audio recording for the background user/profile |
| 5214 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 5215 | if (userId >= 0) { |
| 5216 | // TODO Kill recording streams instead of killing processes holding permission |
| 5217 | UserInfo userInfo = UserManagerService.getInstance().getUserInfo(userId); |
| 5218 | killBackgroundUserProcessesWithRecordAudioPermission(userInfo); |
| 5219 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5220 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5221 | UserManager.DISALLOW_RECORD_AUDIO, true, userId); |
| 5222 | } else if (action.equals(Intent.ACTION_USER_FOREGROUND)) { |
| 5223 | // Enable audio recording for foreground user/profile |
| 5224 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 5225 | UserManagerService.getInstance().setUserRestriction( |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5226 | UserManager.DISALLOW_RECORD_AUDIO, false, userId); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5227 | } |
| 5228 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 5229 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5230 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 5231 | private class AudioServiceUserRestrictionsListener implements UserRestrictionsListener { |
| 5232 | |
| 5233 | @Override |
| 5234 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 5235 | Bundle prevRestrictions) { |
| 5236 | // Update mic mute state. |
| 5237 | { |
| 5238 | final boolean wasRestricted = |
| 5239 | prevRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5240 | final boolean isRestricted = |
| 5241 | newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 5242 | if (wasRestricted != isRestricted) { |
| 5243 | setMicrophoneMuteNoCallerCheck(isRestricted, userId); |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | // Update speaker mute state. |
| 5248 | { |
| 5249 | final boolean wasRestricted = |
| 5250 | prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME); |
| 5251 | final boolean isRestricted = |
| 5252 | newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME); |
| 5253 | if (wasRestricted != isRestricted) { |
| 5254 | setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId); |
| 5255 | } |
| 5256 | } |
| 5257 | } |
| 5258 | } // end class AudioServiceUserRestrictionsListener |
| 5259 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5260 | private void killBackgroundUserProcessesWithRecordAudioPermission(UserInfo oldUser) { |
| 5261 | PackageManager pm = mContext.getPackageManager(); |
| 5262 | // Find the home activity of the user. It should not be killed to avoid expensive restart, |
| 5263 | // when the user switches back. For managed profiles, we should kill all recording apps |
| 5264 | ComponentName homeActivityName = null; |
| 5265 | if (!oldUser.isManagedProfile()) { |
| 5266 | homeActivityName = LocalServices.getService(ActivityManagerInternal.class) |
| 5267 | .getHomeActivityForUser(oldUser.id); |
| 5268 | } |
| 5269 | final String[] permissions = { Manifest.permission.RECORD_AUDIO }; |
| 5270 | List<PackageInfo> packages; |
| 5271 | try { |
| 5272 | packages = AppGlobals.getPackageManager() |
| 5273 | .getPackagesHoldingPermissions(permissions, 0, oldUser.id).getList(); |
| 5274 | } catch (RemoteException e) { |
| 5275 | throw new AndroidRuntimeException(e); |
| 5276 | } |
| 5277 | for (int j = packages.size() - 1; j >= 0; j--) { |
| 5278 | PackageInfo pkg = packages.get(j); |
Fyodor Kupolov | bcb6c1e | 2015-05-11 12:05:15 -0700 | [diff] [blame] | 5279 | // Skip system processes |
| 5280 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 5281 | continue; |
| 5282 | } |
Amith Yamasani | c1cbaab | 2015-07-21 11:46:14 -0700 | [diff] [blame] | 5283 | // Skip packages that have permission to interact across users |
| 5284 | if (pm.checkPermission(Manifest.permission.INTERACT_ACROSS_USERS, pkg.packageName) |
| 5285 | == PackageManager.PERMISSION_GRANTED) { |
| 5286 | continue; |
| 5287 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5288 | if (homeActivityName != null |
| 5289 | && pkg.packageName.equals(homeActivityName.getPackageName()) |
| 5290 | && pkg.applicationInfo.isSystemApp()) { |
| 5291 | continue; |
| 5292 | } |
| 5293 | try { |
Svetoslav | aa41add | 2015-08-06 15:03:55 -0700 | [diff] [blame] | 5294 | final int uid = pkg.applicationInfo.uid; |
| 5295 | ActivityManagerNative.getDefault().killUid(UserHandle.getAppId(uid), |
| 5296 | UserHandle.getUserId(uid), |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 5297 | "killBackgroundUserProcessesWithAudioRecordPermission"); |
| 5298 | } catch (RemoteException e) { |
| 5299 | Log.w(TAG, "Error calling killUid", e); |
| 5300 | } |
| 5301 | } |
| 5302 | } |
| 5303 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5304 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5305 | //========================================================================================== |
| 5306 | // Audio Focus |
| 5307 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5308 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5309 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5310 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5311 | // permission checks |
| 5312 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5313 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5314 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 5315 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 5316 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 5317 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5318 | } |
| 5319 | } else { |
| 5320 | // only a registered audio policy can be used to lock focus |
| 5321 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5322 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5323 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5324 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | } |
| 5329 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 5330 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 5331 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5332 | } |
| 5333 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5334 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 5335 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | public void unregisterAudioFocusClient(String clientId) { |
| 5339 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 5340 | } |
| 5341 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 5342 | public int getCurrentAudioFocus() { |
| 5343 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 5344 | } |
| 5345 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5346 | private boolean readCameraSoundForced() { |
| 5347 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 5348 | mContext.getResources().getBoolean( |
| 5349 | com.android.internal.R.bool.config_camera_sound_forced); |
| 5350 | } |
| 5351 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5352 | //========================================================================================== |
| 5353 | // Device orientation |
| 5354 | //========================================================================================== |
| 5355 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5356 | * Handles device configuration changes that may map to a change in the orientation |
| 5357 | * or orientation. |
| 5358 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 5359 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5360 | */ |
| 5361 | private void handleConfigurationChanged(Context context) { |
| 5362 | try { |
| 5363 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 5364 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5365 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5366 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5367 | if (mMonitorOrientation) { |
| 5368 | int newOrientation = config.orientation; |
| 5369 | if (newOrientation != mDeviceOrientation) { |
| 5370 | mDeviceOrientation = newOrientation; |
| 5371 | setOrientationForAudioSystem(); |
| 5372 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5373 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5374 | sendMsg(mAudioHandler, |
| 5375 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 5376 | SENDMSG_REPLACE, |
| 5377 | 0, |
| 5378 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5379 | TAG, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5380 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5381 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5382 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5383 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5384 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5385 | synchronized (mCameraSoundForced) { |
| 5386 | if (cameraSoundForced != mCameraSoundForced) { |
| 5387 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5388 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5389 | } |
| 5390 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5391 | if (cameraSoundForcedChanged) { |
| 5392 | if (!isPlatformTelevision()) { |
| 5393 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 5394 | if (cameraSoundForced) { |
| 5395 | s.setAllIndexesToMax(); |
| 5396 | mRingerModeAffectedStreams &= |
| 5397 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5398 | } else { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5399 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], TAG); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5400 | mRingerModeAffectedStreams |= |
| 5401 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5402 | } |
| 5403 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5404 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | sendMsg(mAudioHandler, |
| 5408 | MSG_SET_FORCE_USE, |
| 5409 | SENDMSG_QUEUE, |
| 5410 | AudioSystem.FOR_SYSTEM, |
| 5411 | cameraSoundForced ? |
| 5412 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 5413 | null, |
| 5414 | 0); |
| 5415 | |
| 5416 | sendMsg(mAudioHandler, |
| 5417 | MSG_SET_ALL_VOLUMES, |
| 5418 | SENDMSG_QUEUE, |
| 5419 | 0, |
| 5420 | 0, |
| 5421 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 5422 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5423 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5424 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5425 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5426 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5427 | } |
| 5428 | } |
| 5429 | |
Jean-Michel Trivi | 24806db | 2015-10-01 15:00:59 -0700 | [diff] [blame] | 5430 | //TODO move to an external "orientation helper" class |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5431 | private void setOrientationForAudioSystem() { |
| 5432 | switch (mDeviceOrientation) { |
| 5433 | case Configuration.ORIENTATION_LANDSCAPE: |
| 5434 | //Log.i(TAG, "orientation is landscape"); |
| 5435 | AudioSystem.setParameters("orientation=landscape"); |
| 5436 | break; |
| 5437 | case Configuration.ORIENTATION_PORTRAIT: |
| 5438 | //Log.i(TAG, "orientation is portrait"); |
| 5439 | AudioSystem.setParameters("orientation=portrait"); |
| 5440 | break; |
| 5441 | case Configuration.ORIENTATION_SQUARE: |
| 5442 | //Log.i(TAG, "orientation is square"); |
| 5443 | AudioSystem.setParameters("orientation=square"); |
| 5444 | break; |
| 5445 | case Configuration.ORIENTATION_UNDEFINED: |
| 5446 | //Log.i(TAG, "orientation is undefined"); |
| 5447 | AudioSystem.setParameters("orientation=undefined"); |
| 5448 | break; |
| 5449 | default: |
| 5450 | Log.e(TAG, "Unknown orientation"); |
| 5451 | } |
| 5452 | } |
| 5453 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5454 | // Handles request to override default use of A2DP for media. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5455 | // Must be called synchronized on mConnectedDevices |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5456 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5457 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5458 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5459 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5460 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5461 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5465 | // Must be called synchronized on mConnectedDevices |
| 5466 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5467 | switch (usage) { |
| 5468 | case AudioSystem.FOR_MEDIA: |
| 5469 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5470 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5471 | } else { // config == AudioSystem.FORCE_NONE |
| 5472 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5473 | } |
| 5474 | break; |
| 5475 | case AudioSystem.FOR_DOCK: |
| 5476 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5477 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5478 | } else { // config == AudioSystem.FORCE_NONE |
| 5479 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5480 | } |
| 5481 | break; |
| 5482 | default: |
| 5483 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5484 | } |
| 5485 | AudioSystem.setForceUse(usage, config); |
| 5486 | } |
| 5487 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5488 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5489 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5490 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5491 | mRingtonePlayer = player; |
| 5492 | } |
| 5493 | |
| 5494 | @Override |
| 5495 | public IRingtonePlayer getRingtonePlayer() { |
| 5496 | return mRingtonePlayer; |
| 5497 | } |
| 5498 | |
| 5499 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5500 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5501 | synchronized (mCurAudioRoutes) { |
| 5502 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5503 | mRoutesObservers.register(observer); |
| 5504 | return routes; |
| 5505 | } |
| 5506 | } |
| 5507 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5508 | |
| 5509 | //========================================================================================== |
| 5510 | // Safe media volume management. |
| 5511 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5512 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5513 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5514 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5515 | // property. Platforms with a different limit must set this property accordingly in their |
| 5516 | // overlay. |
| 5517 | //========================================================================================== |
| 5518 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5519 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5520 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5521 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5522 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5523 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5524 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5525 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5526 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5527 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5528 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5529 | private Integer mSafeMediaVolumeState; |
| 5530 | |
| 5531 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5532 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5533 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5534 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5535 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5536 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5537 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5538 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5539 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5540 | private int mMusicActiveMs; |
| 5541 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5542 | 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] | 5543 | 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] | 5544 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5545 | private void setSafeMediaVolumeEnabled(boolean on, String caller) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5546 | synchronized (mSafeMediaVolumeState) { |
| 5547 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5548 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5549 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5550 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5551 | enforceSafeMediaVolume(caller); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5552 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5553 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5554 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5555 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5556 | sendMsg(mAudioHandler, |
| 5557 | MSG_CHECK_MUSIC_ACTIVE, |
| 5558 | SENDMSG_REPLACE, |
| 5559 | 0, |
| 5560 | 0, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5561 | caller, |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5562 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5563 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5564 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5565 | } |
| 5566 | } |
| 5567 | |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5568 | private void enforceSafeMediaVolume(String caller) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5569 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5570 | int devices = mSafeMediaVolumeDevices; |
| 5571 | int i = 0; |
| 5572 | |
| 5573 | while (devices != 0) { |
| 5574 | int device = 1 << i++; |
| 5575 | if ((device & devices) == 0) { |
| 5576 | continue; |
| 5577 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5578 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5579 | if (index > mSafeMediaVolumeIndex) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5580 | streamState.setIndex(mSafeMediaVolumeIndex, device, caller); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5581 | sendMsg(mAudioHandler, |
| 5582 | MSG_SET_DEVICE_VOLUME, |
| 5583 | SENDMSG_QUEUE, |
| 5584 | device, |
| 5585 | 0, |
| 5586 | streamState, |
| 5587 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5588 | } |
| 5589 | devices &= ~device; |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5594 | synchronized (mSafeMediaVolumeState) { |
| 5595 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5596 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5597 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5598 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5599 | return false; |
| 5600 | } |
| 5601 | return true; |
| 5602 | } |
| 5603 | } |
| 5604 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5605 | @Override |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5606 | public void disableSafeMediaVolume(String callingPackage) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5607 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5608 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5609 | setSafeMediaVolumeEnabled(false, callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5610 | if (mPendingVolumeCommand != null) { |
| 5611 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5612 | mPendingVolumeCommand.mIndex, |
| 5613 | mPendingVolumeCommand.mFlags, |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5614 | mPendingVolumeCommand.mDevice, |
| 5615 | callingPackage); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5616 | mPendingVolumeCommand = null; |
| 5617 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5618 | } |
| 5619 | } |
| 5620 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5621 | //========================================================================================== |
| 5622 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5623 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5624 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5625 | //========================================================================================== |
| 5626 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5627 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5628 | public void onComplete(int status) { |
| 5629 | if (mHdmiManager != null) { |
| 5630 | synchronized (mHdmiManager) { |
| 5631 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5632 | // Television devices without CEC service apply software volume on HDMI output |
| 5633 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5634 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5635 | } |
| 5636 | checkAllFixedVolumeDevices(); |
| 5637 | } |
| 5638 | } |
| 5639 | } |
| 5640 | }; |
| 5641 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5642 | // If HDMI-CEC system audio is supported |
| 5643 | private boolean mHdmiSystemAudioSupported = false; |
| 5644 | // Set only when device is tv. |
| 5645 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5646 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5647 | // cached HdmiControlManager interface |
| 5648 | private HdmiControlManager mHdmiManager; |
| 5649 | // Set only when device is a set-top box. |
| 5650 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5651 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5652 | private boolean mHdmiCecSink; |
| 5653 | |
| 5654 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5655 | |
| 5656 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5657 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5658 | int device = AudioSystem.DEVICE_NONE; |
| 5659 | if (mHdmiManager != null) { |
| 5660 | synchronized (mHdmiManager) { |
| 5661 | if (mHdmiTvClient == null) { |
| 5662 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5663 | return device; |
| 5664 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5665 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5666 | synchronized (mHdmiTvClient) { |
| 5667 | if (mHdmiSystemAudioSupported != on) { |
| 5668 | mHdmiSystemAudioSupported = on; |
| 5669 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5670 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5671 | AudioSystem.FORCE_NONE); |
| 5672 | } |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 5673 | device = getDevicesForStream(AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5674 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5675 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5676 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5677 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5678 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5679 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5680 | @Override |
| 5681 | public boolean isHdmiSystemAudioSupported() { |
| 5682 | return mHdmiSystemAudioSupported; |
| 5683 | } |
| 5684 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5685 | //========================================================================================== |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5686 | // Accessibility: taking touch exploration into account for selecting the default |
| 5687 | // stream override timeout when adjusting volume |
| 5688 | //========================================================================================== |
| 5689 | private static class StreamOverride |
| 5690 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 5691 | |
| 5692 | // AudioService.getActiveStreamType() will return: |
| 5693 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5694 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5695 | // stopped |
Jean-Michel Trivi | ccffda8 | 2015-05-21 18:23:57 -0700 | [diff] [blame] | 5696 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 0; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5697 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
| 5698 | |
| 5699 | static int sDelayMs; |
| 5700 | |
| 5701 | static void init(Context ctxt) { |
| 5702 | AccessibilityManager accessibilityManager = |
| 5703 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5704 | updateDefaultStreamOverrideDelay( |
| 5705 | accessibilityManager.isTouchExplorationEnabled()); |
| 5706 | accessibilityManager.addTouchExplorationStateChangeListener( |
| 5707 | new StreamOverride()); |
| 5708 | } |
| 5709 | |
| 5710 | @Override |
| 5711 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5712 | updateDefaultStreamOverrideDelay(enabled); |
| 5713 | } |
| 5714 | |
| 5715 | private static void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5716 | if (touchExploreEnabled) { |
| 5717 | sDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5718 | } else { |
| 5719 | sDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5720 | } |
| 5721 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5722 | + " stream override delay is now " + sDelayMs + " ms"); |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5727 | // Camera shutter sound policy. |
| 5728 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5729 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5730 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5731 | //========================================================================================== |
| 5732 | |
| 5733 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5734 | private Boolean mCameraSoundForced; |
| 5735 | |
| 5736 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5737 | public boolean isCameraSoundForced() { |
| 5738 | synchronized (mCameraSoundForced) { |
| 5739 | return mCameraSoundForced; |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5744 | "SILENT", |
| 5745 | "VIBRATE", |
| 5746 | "NORMAL" |
| 5747 | }; |
| 5748 | |
| 5749 | private void dumpRingerMode(PrintWriter pw) { |
| 5750 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5751 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5752 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5753 | dumpRingerModeStreams(pw, "affected", mRingerModeAffectedStreams); |
| 5754 | dumpRingerModeStreams(pw, "muted", mRingerModeMutedStreams); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5755 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5756 | } |
| 5757 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 5758 | private void dumpRingerModeStreams(PrintWriter pw, String type, int streams) { |
| 5759 | pw.print("- ringer mode "); pw.print(type); pw.print(" streams = 0x"); |
| 5760 | pw.print(Integer.toHexString(streams)); |
| 5761 | if (streams != 0) { |
| 5762 | pw.print(" ("); |
| 5763 | boolean first = true; |
| 5764 | for (int i = 0; i < AudioSystem.STREAM_NAMES.length; i++) { |
| 5765 | final int stream = (1 << i); |
| 5766 | if ((streams & stream) != 0) { |
| 5767 | if (!first) pw.print(','); |
| 5768 | pw.print(AudioSystem.STREAM_NAMES[i]); |
| 5769 | streams &= ~stream; |
| 5770 | first = false; |
| 5771 | } |
| 5772 | } |
| 5773 | if (streams != 0) { |
| 5774 | if (!first) pw.print(','); |
| 5775 | pw.print(streams); |
| 5776 | } |
| 5777 | pw.print(')'); |
| 5778 | } |
| 5779 | pw.println(); |
| 5780 | } |
| 5781 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5782 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5783 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5784 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5785 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5786 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5787 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5788 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5789 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5790 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 5791 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5792 | |
| 5793 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5794 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5795 | pw.print(" mSafeMediaVolumeState="); |
| 5796 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5797 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5798 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5799 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5800 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5801 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5802 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5803 | pw.print(" mControllerService="); pw.println(mControllerService); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5804 | pw.print(" mVolumePolicy="); pw.println(mVolumePolicy); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5805 | |
| 5806 | dumpAudioPolicies(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5810 | switch(state) { |
| 5811 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5812 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5813 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5814 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5815 | } |
| 5816 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5817 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5818 | |
| 5819 | // Inform AudioFlinger of our device's low RAM attribute |
| 5820 | private static void readAndSetLowRamDevice() |
| 5821 | { |
| 5822 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5823 | if (status != 0) { |
| 5824 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5825 | } |
| 5826 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5827 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5828 | private void enforceVolumeController(String action) { |
| 5829 | if (mControllerService.mUid != 0 && Binder.getCallingUid() == mControllerService.mUid) { |
| 5830 | return; |
| 5831 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5832 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5833 | "Only SystemUI can " + action); |
| 5834 | } |
| 5835 | |
| 5836 | @Override |
| 5837 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5838 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5839 | |
| 5840 | // return early if things are not actually changing |
| 5841 | if (mVolumeController.isSameBinder(controller)) { |
| 5842 | return; |
| 5843 | } |
| 5844 | |
| 5845 | // dismiss the old volume controller |
| 5846 | mVolumeController.postDismiss(); |
| 5847 | if (controller != null) { |
| 5848 | // we are about to register a new controller, listen for its death |
| 5849 | try { |
| 5850 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5851 | @Override |
| 5852 | public void binderDied() { |
| 5853 | if (mVolumeController.isSameBinder(controller)) { |
| 5854 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5855 | setVolumeController(null); |
| 5856 | } |
| 5857 | } |
| 5858 | }, 0); |
| 5859 | } catch (RemoteException e) { |
| 5860 | // noop |
| 5861 | } |
| 5862 | } |
| 5863 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5864 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5865 | } |
| 5866 | |
| 5867 | @Override |
| 5868 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5869 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5870 | |
| 5871 | // return early if the controller is not current |
| 5872 | if (!mVolumeController.isSameBinder(controller)) { |
| 5873 | return; |
| 5874 | } |
| 5875 | |
| 5876 | mVolumeController.setVisible(visible); |
| 5877 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5878 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5879 | |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5880 | @Override |
| 5881 | public void setVolumePolicy(VolumePolicy policy) { |
| 5882 | enforceVolumeController("set volume policy"); |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5883 | if (policy != null && !policy.equals(mVolumePolicy)) { |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5884 | mVolumePolicy = policy; |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 5885 | if (DEBUG_VOL) Log.d(TAG, "Volume policy changed: " + mVolumePolicy); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 5886 | } |
| 5887 | } |
| 5888 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5889 | public static class VolumeController { |
| 5890 | private static final String TAG = "VolumeController"; |
| 5891 | |
| 5892 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5893 | private boolean mVisible; |
| 5894 | private long mNextLongPress; |
| 5895 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5896 | |
| 5897 | public void setController(IVolumeController controller) { |
| 5898 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5899 | mVisible = false; |
| 5900 | } |
| 5901 | |
| 5902 | public void loadSettings(ContentResolver cr) { |
| 5903 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5904 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5905 | } |
| 5906 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5907 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5908 | if (isMute) { |
| 5909 | return false; |
| 5910 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5911 | boolean suppress = false; |
| 5912 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5913 | final long now = SystemClock.uptimeMillis(); |
| 5914 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5915 | // ui will become visible |
| 5916 | if (mNextLongPress < now) { |
| 5917 | mNextLongPress = now + mLongPressTimeout; |
| 5918 | } |
| 5919 | suppress = true; |
| 5920 | } else if (mNextLongPress > 0) { // in a long-press |
| 5921 | if (now > mNextLongPress) { |
| 5922 | // long press triggered, no more suppression |
| 5923 | mNextLongPress = 0; |
| 5924 | } else { |
| 5925 | // keep suppressing until the long press triggers |
| 5926 | suppress = true; |
| 5927 | } |
| 5928 | } |
| 5929 | } |
| 5930 | return suppress; |
| 5931 | } |
| 5932 | |
| 5933 | public void setVisible(boolean visible) { |
| 5934 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | public boolean isSameBinder(IVolumeController controller) { |
| 5938 | return Objects.equals(asBinder(), binder(controller)); |
| 5939 | } |
| 5940 | |
| 5941 | public IBinder asBinder() { |
| 5942 | return binder(mController); |
| 5943 | } |
| 5944 | |
| 5945 | private static IBinder binder(IVolumeController controller) { |
| 5946 | return controller == null ? null : controller.asBinder(); |
| 5947 | } |
| 5948 | |
| 5949 | @Override |
| 5950 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5951 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5955 | if (mController == null) |
| 5956 | return; |
| 5957 | try { |
| 5958 | mController.displaySafeVolumeWarning(flags); |
| 5959 | } catch (RemoteException e) { |
| 5960 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | public void postVolumeChanged(int streamType, int flags) { |
| 5965 | if (mController == null) |
| 5966 | return; |
| 5967 | try { |
| 5968 | mController.volumeChanged(streamType, flags); |
| 5969 | } catch (RemoteException e) { |
| 5970 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5971 | } |
| 5972 | } |
| 5973 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5974 | public void postMasterMuteChanged(int flags) { |
| 5975 | if (mController == null) |
| 5976 | return; |
| 5977 | try { |
| 5978 | mController.masterMuteChanged(flags); |
| 5979 | } catch (RemoteException e) { |
| 5980 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5981 | } |
| 5982 | } |
| 5983 | |
| 5984 | public void setLayoutDirection(int layoutDirection) { |
| 5985 | if (mController == null) |
| 5986 | return; |
| 5987 | try { |
| 5988 | mController.setLayoutDirection(layoutDirection); |
| 5989 | } catch (RemoteException e) { |
| 5990 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5991 | } |
| 5992 | } |
| 5993 | |
| 5994 | public void postDismiss() { |
| 5995 | if (mController == null) |
| 5996 | return; |
| 5997 | try { |
| 5998 | mController.dismiss(); |
| 5999 | } catch (RemoteException e) { |
| 6000 | Log.w(TAG, "Error calling dismiss", e); |
| 6001 | } |
| 6002 | } |
| 6003 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6004 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6005 | /** |
| 6006 | * Interface for system components to get some extra functionality through |
| 6007 | * LocalServices. |
| 6008 | */ |
| 6009 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6010 | @Override |
| 6011 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 6012 | mRingerModeDelegate = delegate; |
| 6013 | if (mRingerModeDelegate != null) { |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6014 | updateRingerModeAffectedStreams(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6015 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 6016 | } |
| 6017 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6018 | |
| 6019 | @Override |
| 6020 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 6021 | String callingPackage, int uid) { |
| 6022 | // direction and stream type swap here because the public |
| 6023 | // adjustSuggested has a different order than the other methods. |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6024 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, |
| 6025 | callingPackage, uid); |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 6026 | } |
| 6027 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6028 | @Override |
| 6029 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 6030 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6031 | adjustStreamVolume(streamType, direction, flags, callingPackage, |
| 6032 | callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6033 | } |
| 6034 | |
| 6035 | @Override |
| 6036 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 6037 | String callingPackage, int uid) { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 6038 | setStreamVolume(streamType, direction, flags, callingPackage, callingPackage, uid); |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6039 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 6040 | |
| 6041 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6042 | public int getRingerModeInternal() { |
| 6043 | return AudioService.this.getRingerModeInternal(); |
| 6044 | } |
| 6045 | |
| 6046 | @Override |
| 6047 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 6048 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 6049 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6050 | |
| 6051 | @Override |
| 6052 | public int getVolumeControllerUid() { |
| 6053 | return mControllerService.mUid; |
| 6054 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 6055 | |
| 6056 | @Override |
| 6057 | public void updateRingerModeAffectedStreamsInternal() { |
| 6058 | synchronized (mSettingsLock) { |
| 6059 | if (updateRingerModeAffectedStreams()) { |
| 6060 | setRingerModeInt(getRingerModeInternal(), false); |
| 6061 | } |
| 6062 | } |
| 6063 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 6064 | } |
| 6065 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6066 | //========================================================================================== |
| 6067 | // Audio policy management |
| 6068 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6069 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 6070 | boolean hasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6071 | AudioSystem.setDynamicPolicyCallback(mDynPolicyCallback); |
| 6072 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6073 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 6074 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6075 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6076 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6077 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6078 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6079 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6080 | if (!hasPermissionForPolicy) { |
| 6081 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 6082 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6083 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6084 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6085 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6086 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6087 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6088 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6089 | Slog.e(TAG, "Cannot re-register policy"); |
| 6090 | return null; |
| 6091 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6092 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 6093 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 6094 | regId = app.getRegistrationId(); |
| 6095 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6096 | } catch (RemoteException e) { |
| 6097 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6098 | 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] | 6099 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6100 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6101 | } |
| 6102 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6103 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6104 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6105 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6106 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 6107 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6108 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6109 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6110 | if (app == null) { |
| 6111 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 6112 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6113 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6114 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6115 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6116 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6117 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6118 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6119 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6120 | } |
| 6121 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6122 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 6123 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 6124 | + " policy " + pcb.asBinder()); |
| 6125 | // error handling |
| 6126 | boolean hasPermissionForPolicy = |
| 6127 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 6128 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 6129 | if (!hasPermissionForPolicy) { |
| 6130 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 6131 | + Binder.getCallingPid() + " / uid " |
| 6132 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 6133 | return AudioManager.ERROR; |
| 6134 | } |
| 6135 | |
| 6136 | synchronized (mAudioPolicies) { |
| 6137 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 6138 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 6139 | return AudioManager.ERROR; |
| 6140 | } |
| 6141 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 6142 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6143 | // is there already one policy managing ducking? |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6144 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6145 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6146 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 6147 | return AudioManager.ERROR; |
| 6148 | } |
| 6149 | } |
| 6150 | } |
| 6151 | app.mFocusDuckBehavior = duckingBehavior; |
| 6152 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 6153 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 6154 | } |
| 6155 | return AudioManager.SUCCESS; |
| 6156 | } |
| 6157 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6158 | private void dumpAudioPolicies(PrintWriter pw) { |
| 6159 | pw.println("\nAudio policies:"); |
| 6160 | synchronized (mAudioPolicies) { |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 6161 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6162 | pw.println(policy.toLogFriendlyString()); |
| 6163 | } |
| 6164 | } |
| 6165 | } |
| 6166 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6167 | //====================== |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6168 | // Audio policy callback from AudioSystem |
| 6169 | //====================== |
| 6170 | private final AudioSystem.DynamicPolicyCallback mDynPolicyCallback = |
| 6171 | new AudioSystem.DynamicPolicyCallback() { |
| 6172 | public void onDynamicPolicyMixStateUpdate(String regId, int state) { |
| 6173 | if (!TextUtils.isEmpty(regId)) { |
| 6174 | sendMsg(mAudioHandler, MSG_DYN_POLICY_MIX_STATE_UPDATE, SENDMSG_QUEUE, |
| 6175 | state /*arg1*/, 0 /*arg2 ignored*/, regId /*obj*/, 0 /*delay*/); |
| 6176 | } |
| 6177 | } |
| 6178 | }; |
| 6179 | |
| 6180 | private void onDynPolicyMixStateUpdate(String regId, int state) { |
| 6181 | if (DEBUG_AP) Log.d(TAG, "onDynamicPolicyMixStateUpdate("+ regId + ", " + state +")"); |
| 6182 | synchronized (mAudioPolicies) { |
| 6183 | for (AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 6184 | for (AudioMix mix : policy.getMixes()) { |
| 6185 | if (mix.getRegistration().equals(regId)) { |
| 6186 | try { |
| 6187 | policy.mPolicyCallback.notifyMixStateUpdate(regId, state); |
| 6188 | } catch (RemoteException e) { |
| 6189 | Log.e(TAG, "Can't call notifyMixStateUpdate() on IAudioPolicyCallback " |
| 6190 | + policy.mPolicyCallback.asBinder(), e); |
| 6191 | } |
| 6192 | return; |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | } |
| 6197 | |
| 6198 | } |
| 6199 | |
| 6200 | //====================== |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 6201 | // Audio policy proxy |
| 6202 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6203 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6204 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 6205 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6206 | */ |
| 6207 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6208 | private static final String TAG = "AudioPolicyProxy"; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6209 | IAudioPolicyCallback mPolicyCallback; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6210 | boolean mHasFocusListener; |
| 6211 | /** |
| 6212 | * Audio focus ducking behavior for an audio policy. |
| 6213 | * This variable reflects the value that was successfully set in |
| 6214 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 6215 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 6216 | * is handling ducking for audio focus. |
| 6217 | */ |
| 6218 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 6219 | |
| 6220 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 6221 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6222 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6223 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6224 | mPolicyCallback = token; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6225 | mHasFocusListener = hasFocusListener; |
| 6226 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6227 | mMediaFocusControl.addFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6228 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6229 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | public void binderDied() { |
| 6233 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6234 | Log.i(TAG, "audio policy " + mPolicyCallback + " died"); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6235 | release(); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6236 | mAudioPolicies.remove(mPolicyCallback.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6240 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 6241 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6242 | } |
| 6243 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6244 | void release() { |
| 6245 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 6246 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 6247 | } |
| 6248 | if (mHasFocusListener) { |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 6249 | mMediaFocusControl.removeFocusFollower(mPolicyCallback); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 6250 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6251 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6252 | } |
| 6253 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 6254 | void connectMixes() { |
| 6255 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6256 | } |
| 6257 | }; |
| 6258 | |
| 6259 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 6260 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 6261 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 6262 | |
| 6263 | private class ControllerService extends ContentObserver { |
| 6264 | private int mUid; |
| 6265 | private ComponentName mComponent; |
| 6266 | |
| 6267 | public ControllerService() { |
| 6268 | super(null); |
| 6269 | } |
| 6270 | |
| 6271 | @Override |
| 6272 | public String toString() { |
| 6273 | return String.format("{mUid=%d,mComponent=%s}", mUid, mComponent); |
| 6274 | } |
| 6275 | |
| 6276 | public void init() { |
| 6277 | onChange(true); |
| 6278 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 6279 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT), false, this); |
| 6280 | } |
| 6281 | |
| 6282 | @Override |
| 6283 | public void onChange(boolean selfChange) { |
| 6284 | mUid = 0; |
| 6285 | mComponent = null; |
| 6286 | final String setting = Settings.Secure.getString(mContentResolver, |
| 6287 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT); |
| 6288 | if (setting == null) return; |
| 6289 | try { |
| 6290 | mComponent = ComponentName.unflattenFromString(setting); |
| 6291 | if (mComponent == null) return; |
| 6292 | mUid = mContext.getPackageManager() |
| 6293 | .getApplicationInfo(mComponent.getPackageName(), 0).uid; |
| 6294 | } catch (Exception e) { |
| 6295 | Log.w(TAG, "Error loading controller service", e); |
| 6296 | } |
| 6297 | if (DEBUG_VOL) Log.d(TAG, "Reloaded controller service: " + this); |
| 6298 | } |
| 6299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6300 | } |