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; |
| 23 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.app.ActivityManagerNative; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 27 | import android.app.KeyguardManager; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothA2dp; |
| 29 | import android.bluetooth.BluetoothAdapter; |
| 30 | import android.bluetooth.BluetoothClass; |
| 31 | import android.bluetooth.BluetoothDevice; |
| 32 | import android.bluetooth.BluetoothHeadset; |
| 33 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 34 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 35 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ContentResolver; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 39 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 41 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
| 43 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 44 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 45 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 46 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 47 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 48 | import android.hardware.usb.UsbManager; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 49 | import android.media.AudioAttributes; |
| 50 | import android.media.AudioDevicePort; |
| 51 | import android.media.AudioSystem; |
| 52 | import android.media.AudioFormat; |
| 53 | import android.media.AudioManager; |
| 54 | import android.media.AudioManagerInternal; |
| 55 | import android.media.AudioPort; |
| 56 | import android.media.AudioRoutesInfo; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 57 | import android.media.IAudioFocusDispatcher; |
| 58 | import android.media.IAudioRoutesObserver; |
| 59 | import android.media.IAudioService; |
| 60 | import android.media.IRemoteControlDisplay; |
| 61 | import android.media.IRingtonePlayer; |
| 62 | import android.media.IVolumeController; |
| 63 | import android.media.MediaPlayer; |
| 64 | import android.media.SoundPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.media.MediaPlayer.OnCompletionListener; |
| 66 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 67 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 68 | import android.media.audiopolicy.AudioPolicyConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 69 | import android.media.audiopolicy.IAudioPolicyCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 71 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.os.Environment; |
| 73 | import android.os.Handler; |
| 74 | import android.os.IBinder; |
| 75 | import android.os.Looper; |
| 76 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 77 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 78 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.os.RemoteException; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 80 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 81 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 82 | import android.os.UserHandle; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 83 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.provider.Settings; |
| 85 | import android.provider.Settings.System; |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 86 | import android.telecom.TelecomManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 87 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 89 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 90 | import android.util.Slog; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 91 | import android.util.SparseIntArray; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 92 | import android.view.KeyEvent; |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 93 | import android.view.OrientationEventListener; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 94 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 95 | import android.view.WindowManager; |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 96 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 98 | import com.android.internal.util.XmlUtils; |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 99 | import com.android.server.LocalServices; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 100 | |
| 101 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 103 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 105 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 106 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import java.util.ArrayList; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 108 | import java.util.HashMap; |
| 109 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 110 | import java.util.List; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 111 | import java.util.Map; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 112 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 113 | import java.util.Objects; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 114 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * The implementation of the volume manager service. |
| 118 | * <p> |
| 119 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 120 | * asynchronous to external calls. For example, the task of setting a volume |
| 121 | * will update our internal state, but in a separate thread will set the system |
| 122 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 123 | * will update the state and broadcast a change and in a separate thread later |
| 124 | * persist the ringer mode. |
| 125 | * |
| 126 | * @hide |
| 127 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 128 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
| 130 | private static final String TAG = "AudioService"; |
| 131 | |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 132 | /** Debug audio mode */ |
| 133 | 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] | 134 | |
| 135 | /** Debug audio policy feature */ |
| 136 | protected static final boolean DEBUG_AP = Log.isLoggable(TAG + ".AP", Log.DEBUG); |
| 137 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 138 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 139 | 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] | 140 | |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 141 | /** debug calls to media session apis */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 142 | private static final boolean DEBUG_SESSIONS = Log.isLoggable(TAG + ".SESSIONS", Log.DEBUG); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 143 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 144 | /** debug calls to devices APIs */ |
| 145 | protected static final boolean DEBUG_DEVICES = Log.isLoggable(TAG + ".DEVICES", Log.DEBUG); |
| 146 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 147 | /** Allow volume changes to set ringer mode to silent? */ |
| 148 | private static final boolean VOLUME_SETS_RINGER_MODE_SILENT = false; |
| 149 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 150 | /** In silent mode, are volume adjustments (raises) prevented? */ |
| 151 | private static final boolean PREVENT_VOLUME_ADJUSTMENT_IF_SILENT = true; |
| 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 154 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 156 | /** How long to delay after a volume down event before unmuting a stream */ |
| 157 | private static final int UNMUTE_STREAM_DELAY = 350; |
| 158 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 159 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 160 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 161 | */ |
| 162 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 163 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 164 | private final Context mContext; |
| 165 | private final ContentResolver mContentResolver; |
| 166 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 167 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 168 | // the platform type affects volume and silent mode behavior |
| 169 | private final int mPlatformType; |
| 170 | |
| 171 | private boolean isPlatformVoice() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 172 | return mPlatformType == AudioSystem.PLATFORM_VOICE; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private boolean isPlatformTelevision() { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 176 | return mPlatformType == AudioSystem.PLATFORM_TELEVISION; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 177 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 178 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 179 | /** The controller for the volume UI. */ |
| 180 | private final VolumeController mVolumeController = new VolumeController(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 181 | private final ControllerService mControllerService = new ControllerService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | |
| 183 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** If the msg is already queued, replace it with this one. */ |
| 185 | private static final int SENDMSG_REPLACE = 0; |
| 186 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 187 | private static final int SENDMSG_NOOP = 1; |
| 188 | /** If the msg is already queued, queue this one and leave the old. */ |
| 189 | private static final int SENDMSG_QUEUE = 2; |
| 190 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 191 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 192 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | private static final int MSG_PERSIST_VOLUME = 1; |
| 194 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 195 | private static final int MSG_MEDIA_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 196 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 197 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 198 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 199 | private static final int MSG_SET_FORCE_USE = 8; |
| 200 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 201 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 202 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 203 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 204 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 205 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 206 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 207 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 208 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 209 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 210 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 211 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 212 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 213 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 214 | private static final int MSG_PERSIST_MICROPHONE_MUTE = 23; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 215 | private static final int MSG_UNMUTE_STREAM = 24; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 216 | // start of messages handled under wakelock |
| 217 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 218 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 219 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 220 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 221 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 222 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 223 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 224 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 225 | // Timeout for connection to bluetooth headset service |
| 226 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | /** @see AudioSystemThread */ |
| 229 | private AudioSystemThread mAudioSystemThread; |
| 230 | /** @see AudioHandler */ |
| 231 | private AudioHandler mAudioHandler; |
| 232 | /** @see VolumeStreamState */ |
| 233 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 234 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 235 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 236 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 237 | // protects mRingerMode |
| 238 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 241 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 244 | // Maximum volume adjust steps allowed in a single batch call. |
| 245 | private static final int MAX_BATCH_VOLUME_ADJUST_STEPS = 4; |
| 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | /* Sound effect file names */ |
| 248 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 249 | 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] | 250 | |
| 251 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 252 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 253 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 254 | 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] | 255 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 256 | /** @hide Maximum volume index values for audio streams */ |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 257 | private static int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 258 | 5, // STREAM_VOICE_CALL |
| 259 | 7, // STREAM_SYSTEM |
| 260 | 7, // STREAM_RING |
| 261 | 15, // STREAM_MUSIC |
| 262 | 7, // STREAM_ALARM |
| 263 | 7, // STREAM_NOTIFICATION |
| 264 | 15, // STREAM_BLUETOOTH_SCO |
| 265 | 7, // STREAM_SYSTEM_ENFORCED |
| 266 | 15, // STREAM_DTMF |
| 267 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 268 | }; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 269 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 270 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 271 | * of another stream: This avoids multiplying the volume settings for hidden |
| 272 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 273 | * NOTE: do not create loops in aliases! |
| 274 | * 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] | 275 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 276 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 277 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 278 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 279 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 280 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 281 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 282 | AudioSystem.STREAM_RING, // STREAM_RING |
| 283 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 284 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 285 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 286 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 287 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 288 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 289 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 290 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 291 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 292 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 293 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 294 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 295 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 296 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 297 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 298 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 299 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 300 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 301 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 302 | }; |
| 303 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 304 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 305 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 306 | AudioSystem.STREAM_RING, // STREAM_RING |
| 307 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 308 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 309 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 310 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 311 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 312 | AudioSystem.STREAM_RING, // STREAM_DTMF |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 313 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 314 | }; |
| 315 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 316 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 319 | * after mapping through mStreamVolumeAlias. |
| 320 | */ |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 321 | private static final int[] STREAM_VOLUME_OPS = new int[] { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 322 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 323 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 324 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 325 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 326 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 327 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 328 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 329 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 330 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 331 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 332 | }; |
| 333 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 334 | private final boolean mUseFixedVolume; |
| 335 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 336 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | public void onError(int error) { |
| 338 | switch (error) { |
| 339 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 340 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, |
| 341 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | break; |
| 345 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 346 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | /** |
| 350 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 351 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 352 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 353 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 354 | // protected by mSettingsLock |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 355 | private int mRingerMode; // internal ringer mode, affects muting of underlying streams |
| 356 | 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] | 357 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 358 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 359 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 360 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 361 | // Streams currently muted by ringer mode |
| 362 | private int mRingerModeMutedStreams; |
| 363 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 364 | /** Streams that can be muted. Do not resolve to aliases when checking. |
| 365 | * @see System#MUTE_STREAMS_AFFECTED */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | private int mMuteAffectedStreams; |
| 367 | |
| 368 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 369 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 370 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 371 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | */ |
| 373 | private int mVibrateSetting; |
| 374 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 375 | // Is there a vibrator |
| 376 | private final boolean mHasVibrator; |
| 377 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 378 | // Broadcast receiver for device connections intent broadcasts |
| 379 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 380 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 381 | // Devices currently connected |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 382 | // Use makeDeviceListKey() to make a unique key for this list. |
| 383 | private class DeviceListSpec { |
| 384 | int mDeviceType; |
| 385 | String mDeviceName; |
| 386 | String mDeviceAddress; |
| 387 | |
| 388 | public DeviceListSpec(int deviceType, String deviceName, String deviceAddress) { |
| 389 | mDeviceType = deviceType; |
| 390 | mDeviceName = deviceName; |
| 391 | mDeviceAddress = deviceAddress; |
| 392 | } |
| 393 | |
| 394 | public String toString() { |
| 395 | return "[type:0x" + Integer.toHexString(mDeviceType) + " name:" + mDeviceName |
| 396 | + " address:" + mDeviceAddress + "]"; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Generate a unique key for the mConnectedDevices List by composing the device "type" |
| 401 | // and the "address" associated with a specific instance of that device type |
| 402 | private String makeDeviceListKey(int device, String deviceAddress) { |
| 403 | return "0x" + Integer.toHexString(device) + ":" + deviceAddress; |
| 404 | } |
| 405 | |
| 406 | private final HashMap<String, DeviceListSpec> mConnectedDevices = |
| 407 | new HashMap<String, DeviceListSpec>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 408 | |
| 409 | // Forced device usage for communications |
| 410 | private int mForcedUseForComm; |
| 411 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 412 | // List of binder death handlers for setMode() client processes. |
| 413 | // 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] | 414 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 415 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 416 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 417 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 418 | |
| 419 | // BluetoothHeadset API to control SCO connection |
| 420 | private BluetoothHeadset mBluetoothHeadset; |
| 421 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 422 | // Bluetooth headset device |
| 423 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 424 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 425 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 426 | // audio service (internal) or bluetooth headset (external) |
| 427 | private int mScoAudioState; |
| 428 | // SCO audio state is not active |
| 429 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 430 | // SCO audio activation request waiting for headset service to connect |
| 431 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 432 | // 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] | 433 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 434 | // SCO audio deactivation request waiting for headset service to connect |
| 435 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 436 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 437 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 438 | // in call audio) |
| 439 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 440 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 441 | // waiting for headset service to connect |
| 442 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 443 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 444 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 445 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 446 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 447 | // SCO audio mode is undefined |
| 448 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 449 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 450 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 451 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 452 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 453 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 454 | private static final int SCO_MODE_VR = 2; |
| 455 | |
| 456 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 457 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 458 | // Current connection state indicated by bluetooth headset |
| 459 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 460 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 461 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 462 | private boolean mSystemReady; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 463 | // listener for SoundPool sample load completion indication |
| 464 | private SoundPoolCallback mSoundPoolCallBack; |
| 465 | // thread for SoundPool listener |
| 466 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 467 | // message looper for SoundPool listener |
| 468 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 469 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 470 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 471 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 472 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 473 | // Keyguard manager proxy |
| 474 | private KeyguardManager mKeyguardManager; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 475 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 476 | // is controlled by Vol keys. |
| 477 | private int mVolumeControlStream = -1; |
| 478 | private final Object mForceControlStreamLock = new Object(); |
| 479 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 480 | // server process so in theory it is not necessary to monitor the client death. |
| 481 | // However it is good to be ready for future evolutions. |
| 482 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 483 | // Used to play ringtones outside system_server |
| 484 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 485 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 486 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 487 | private int mDeviceRotation = Surface.ROTATION_0; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 488 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 489 | // Request to override default use of A2DP for media. |
| 490 | private boolean mBluetoothA2dpEnabled; |
| 491 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 492 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 493 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 494 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 495 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 496 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 497 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 498 | // 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] | 499 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 500 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 501 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 502 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 503 | AudioSystem.DEVICE_OUT_SPDIF | |
| 504 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 505 | int mFullVolumeDevices = 0; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 506 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 507 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 508 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 509 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 510 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 511 | private boolean mDockAudioMediaEnabled = true; |
| 512 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 513 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 514 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 515 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 516 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 517 | // and used later when/if disableSafeMediaVolume() is called. |
| 518 | private StreamVolumeCommand mPendingVolumeCommand; |
| 519 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 520 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 521 | |
| 522 | private final MediaFocusControl mMediaFocusControl; |
| 523 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 524 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 525 | private BluetoothA2dp mA2dp; |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 526 | // lock always taken synchronized on mConnectedDevices |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 527 | private final Object mA2dpAvrcpLock = new Object(); |
| 528 | // If absolute volume is supported in AVRCP device |
| 529 | private boolean mAvrcpAbsVolSupported = false; |
| 530 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 531 | private AudioOrientationEventListener mOrientationListener; |
| 532 | |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 533 | private static Long mLastDeviceConnectMsgTime = new Long(0); |
| 534 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 535 | private AudioManagerInternal.RingerModeDelegate mRingerModeDelegate; |
| 536 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 537 | // Intent "extra" data keys. |
| 538 | public static final String CONNECT_INTENT_KEY_PORT_NAME = "portName"; |
| 539 | public static final String CONNECT_INTENT_KEY_STATE = "state"; |
| 540 | public static final String CONNECT_INTENT_KEY_ADDRESS = "address"; |
| 541 | public static final String CONNECT_INTENT_KEY_HAS_PLAYBACK = "hasPlayback"; |
| 542 | public static final String CONNECT_INTENT_KEY_HAS_CAPTURE = "hasCapture"; |
| 543 | public static final String CONNECT_INTENT_KEY_HAS_MIDI = "hasMIDI"; |
| 544 | public static final String CONNECT_INTENT_KEY_DEVICE_CLASS = "class"; |
| 545 | |
| 546 | // Defines the format for the connection "address" for ALSA devices |
| 547 | public static String makeAlsaAddressString(int card, int device) { |
| 548 | return "card=" + card + ";device=" + device + ";"; |
| 549 | } |
| 550 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 551 | private final String DEVICE_NAME_A2DP = "a2dp-device"; |
| 552 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | /////////////////////////////////////////////////////////////////////////// |
| 554 | // Construction |
| 555 | /////////////////////////////////////////////////////////////////////////// |
| 556 | |
| 557 | /** @hide */ |
| 558 | public AudioService(Context context) { |
| 559 | mContext = context; |
| 560 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 561 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 562 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 563 | mPlatformType = AudioSystem.getPlatformType(context); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 564 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 565 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 566 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 567 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 568 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 569 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 570 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 571 | // Intialized volume |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 572 | int maxVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", |
| 573 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 574 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) { |
| 575 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 576 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 577 | } |
| 578 | maxVolume = SystemProperties.getInt("ro.config.media_vol_steps", |
| 579 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]); |
| 580 | if (maxVolume != MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) { |
| 581 | MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxVolume; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 582 | AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = (maxVolume * 3) / 4; |
Eric Laurent | 91377de | 2014-10-10 15:24:04 -0700 | [diff] [blame] | 583 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 584 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 585 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 586 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 587 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 588 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 591 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 592 | mMediaFocusControl = new MediaFocusControl(mAudioHandler.getLooper(), |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 593 | mContext, mVolumeController, this); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 594 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 595 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 596 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 597 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 598 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 599 | sendMsg(mAudioHandler, |
| 600 | MSG_SET_FORCE_USE, |
| 601 | SENDMSG_QUEUE, |
| 602 | AudioSystem.FOR_SYSTEM, |
| 603 | cameraSoundForced ? |
| 604 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 605 | null, |
| 606 | 0); |
| 607 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 608 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 609 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 610 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 611 | // The default safe volume index read here will be replaced by the actual value when |
| 612 | // the mcc is read by onConfigureSafeVolume() |
| 613 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 614 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 615 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 616 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 617 | com.android.internal.R.bool.config_useFixedVolume); |
| 618 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 619 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 620 | // array initialized by updateStreamVolumeAlias() |
| 621 | updateStreamVolumeAlias(false /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 623 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 624 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 625 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 626 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 627 | |
| 628 | // Call setRingerModeInt() to apply correct mute |
| 629 | // state on streams affected by ringer mode. |
| 630 | mRingerModeMutedStreams = 0; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 631 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 632 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 633 | // Register for device connection intent broadcasts. |
| 634 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 635 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 636 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 637 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 638 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 639 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 640 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 641 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 642 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 643 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 644 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 645 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 646 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 647 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 648 | // initialize orientation in AudioSystem |
| 649 | setOrientationForAudioSystem(); |
| 650 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 651 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 652 | if (mMonitorRotation) { |
| 653 | mDeviceRotation = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 654 | .getDefaultDisplay().getRotation(); |
| 655 | Log.v(TAG, "monitoring device rotation, initial=" + mDeviceRotation); |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 656 | |
| 657 | mOrientationListener = new AudioOrientationEventListener(mContext); |
| 658 | mOrientationListener.enable(); |
| 659 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 660 | // initialize rotation in AudioSystem |
| 661 | setRotationForAudioSystem(); |
| 662 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 663 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 664 | context.registerReceiver(mReceiver, intentFilter); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 665 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 666 | LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 669 | public void systemReady() { |
| 670 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 671 | 0, 0, null, 0); |
| 672 | } |
| 673 | |
| 674 | public void onSystemReady() { |
| 675 | mSystemReady = true; |
| 676 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 677 | 0, 0, null, 0); |
| 678 | |
| 679 | mKeyguardManager = |
| 680 | (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 681 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 682 | resetBluetoothSco(); |
| 683 | getBluetoothHeadset(); |
| 684 | //FIXME: this is to maintain compatibility with deprecated intent |
| 685 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 686 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 687 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 688 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 689 | sendStickyBroadcastToAll(newIntent); |
| 690 | |
| 691 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 692 | if (adapter != null) { |
| 693 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 694 | BluetoothProfile.A2DP); |
| 695 | } |
| 696 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 697 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 698 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 699 | if (mHdmiManager != null) { |
| 700 | synchronized (mHdmiManager) { |
| 701 | mHdmiTvClient = mHdmiManager.getTvClient(); |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 702 | if (mHdmiTvClient != null) { |
| 703 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER; |
| 704 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 705 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 706 | mHdmiCecSink = false; |
| 707 | } |
| 708 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 709 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 710 | sendMsg(mAudioHandler, |
| 711 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 712 | SENDMSG_REPLACE, |
| 713 | 0, |
| 714 | 0, |
| 715 | null, |
| 716 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 717 | |
| 718 | StreamOverride.init(mContext); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 719 | mControllerService.init(); |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 720 | } |
| 721 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | private void createAudioSystemThread() { |
| 723 | mAudioSystemThread = new AudioSystemThread(); |
| 724 | mAudioSystemThread.start(); |
| 725 | waitForAudioHandlerCreation(); |
| 726 | } |
| 727 | |
| 728 | /** Waits for the volume handler to be created by the other thread. */ |
| 729 | private void waitForAudioHandlerCreation() { |
| 730 | synchronized(this) { |
| 731 | while (mAudioHandler == null) { |
| 732 | try { |
| 733 | // Wait for mAudioHandler to be set by the other thread |
| 734 | wait(); |
| 735 | } catch (InterruptedException e) { |
| 736 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 742 | private void checkAllAliasStreamVolumes() { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 743 | synchronized (VolumeStreamState.class) { |
| 744 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 745 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 746 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 747 | mStreamStates[streamType]. |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 748 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]]); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 749 | } |
| 750 | // apply stream volume |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 751 | if (!mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 752 | mStreamStates[streamType].applyAllVolumes(); |
| 753 | } |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 758 | private void checkAllFixedVolumeDevices() |
| 759 | { |
| 760 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 761 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 762 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 763 | } |
| 764 | } |
| 765 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 766 | private void checkAllFixedVolumeDevices(int streamType) { |
| 767 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 768 | } |
| 769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 772 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 773 | |
| 774 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 775 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 776 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 778 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 779 | checkAllAliasStreamVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 782 | private void dumpStreamStates(PrintWriter pw) { |
| 783 | pw.println("\nStream volumes (device: index)"); |
| 784 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 785 | for (int i = 0; i < numStreamTypes; i++) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 786 | pw.println("- " + AudioSystem.STREAM_NAMES[i] + ":"); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 787 | mStreamStates[i].dump(pw); |
| 788 | pw.println(""); |
| 789 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 790 | pw.print("\n- mute affected streams = 0x"); |
| 791 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 794 | private void updateStreamVolumeAlias(boolean updateVolumes) { |
| 795 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 796 | |
| 797 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 798 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 799 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 800 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 801 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 802 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 803 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 804 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 805 | break; |
| 806 | default: |
| 807 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 808 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 809 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 810 | |
| 811 | if (isPlatformTelevision()) { |
| 812 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 813 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 814 | if (isInCommunication()) { |
| 815 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 816 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 817 | } else { |
| 818 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 819 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 820 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 821 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 822 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 823 | if (updateVolumes) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 824 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias]); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 825 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 826 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 827 | sendMsg(mAudioHandler, |
| 828 | MSG_SET_ALL_VOLUMES, |
| 829 | SENDMSG_QUEUE, |
| 830 | 0, |
| 831 | 0, |
| 832 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 833 | } |
| 834 | } |
| 835 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 836 | private void readDockAudioSettings(ContentResolver cr) |
| 837 | { |
| 838 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 839 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 840 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 841 | sendMsg(mAudioHandler, |
| 842 | MSG_SET_FORCE_USE, |
| 843 | SENDMSG_QUEUE, |
| 844 | AudioSystem.FOR_DOCK, |
| 845 | mDockAudioMediaEnabled ? |
| 846 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 847 | null, |
| 848 | 0); |
| 849 | } |
| 850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | private void readPersistedSettings() { |
| 852 | final ContentResolver cr = mContentResolver; |
| 853 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 854 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 855 | Settings.Global.getInt( |
| 856 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 857 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 858 | // sanity check in case the settings are restored from a device with incompatible |
| 859 | // ringer modes |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 860 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 861 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 862 | } |
| 863 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 864 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 865 | } |
| 866 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 867 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 868 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 869 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 870 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 871 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 872 | synchronized(mSettingsLock) { |
| 873 | mRingerMode = ringerMode; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 874 | if (mRingerModeExternal == -1) { |
| 875 | mRingerModeExternal = mRingerMode; |
| 876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 878 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 879 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 880 | // deprecated. |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 881 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(0, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 882 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 883 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 884 | : AudioManager.VIBRATE_SETTING_OFF); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 885 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 886 | AudioManager.VIBRATE_TYPE_RINGER, |
| 887 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 888 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 890 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 891 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 892 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 893 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 894 | mMuteAffectedStreams = System.getIntForUser(cr, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 895 | System.MUTE_STREAMS_AFFECTED, AudioSystem.DEFAULT_MUTE_STREAMS_AFFECTED, |
John Spurlock | 24c0518 | 2015-02-05 12:30:36 -0500 | [diff] [blame] | 896 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 898 | boolean masterMute = System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 899 | 0, UserHandle.USER_CURRENT) == 1; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 900 | if (mUseFixedVolume) { |
| 901 | masterMute = false; |
| 902 | AudioSystem.setMasterVolume(1.0f); |
| 903 | } |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 904 | AudioSystem.setMasterMute(masterMute); |
| 905 | broadcastMasterMuteStatus(masterMute); |
| 906 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 907 | boolean microphoneMute = |
| 908 | System.getIntForUser(cr, System.MICROPHONE_MUTE, 0, UserHandle.USER_CURRENT) == 1; |
| 909 | AudioSystem.muteMicrophone(microphoneMute); |
| 910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | // Each stream will read its own persisted settings |
| 912 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 913 | // Broadcast the sticky intents |
| 914 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, mRingerModeExternal); |
| 915 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, mRingerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | |
| 917 | // Broadcast vibrate settings |
| 918 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 919 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 920 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 921 | // Load settings for the volume controller |
| 922 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 925 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 926 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 927 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 929 | private class AudioOrientationEventListener |
| 930 | extends OrientationEventListener { |
| 931 | public AudioOrientationEventListener(Context context) { |
| 932 | super(context); |
| 933 | } |
| 934 | |
| 935 | @Override |
| 936 | public void onOrientationChanged(int orientation) { |
| 937 | //Even though we're responding to phone orientation events, |
| 938 | //use display rotation so audio stays in sync with video/dialogs |
| 939 | int newRotation = ((WindowManager) mContext.getSystemService( |
| 940 | Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); |
| 941 | if (newRotation != mDeviceRotation) { |
| 942 | mDeviceRotation = newRotation; |
| 943 | setRotationForAudioSystem(); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | /////////////////////////////////////////////////////////////////////////// |
| 949 | // IPC methods |
| 950 | /////////////////////////////////////////////////////////////////////////// |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 952 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 953 | String callingPackage) { |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 954 | adjustSuggestedStreamVolume(direction, suggestedStreamType, flags, callingPackage, |
| 955 | Binder.getCallingUid()); |
| 956 | } |
| 957 | |
| 958 | private void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 959 | String callingPackage, int uid) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 960 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream="+suggestedStreamType |
| 961 | + ", flags=" + flags); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 962 | int streamType; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 963 | boolean isMute = isMuteAdjust(direction); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 964 | if (mVolumeControlStream != -1) { |
| 965 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 966 | } else { |
| 967 | streamType = getActiveStreamType(suggestedStreamType); |
| 968 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 969 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 971 | // Play sounds on STREAM_RING only. |
| 972 | if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 973 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 975 | } |
| 976 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 977 | // For notifications/ring, show the ui before making any adjustments |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 978 | // Don't suppress mute/unmute requests |
| 979 | if (mVolumeController.suppressAdjustment(resolvedStream, flags, isMute)) { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 980 | direction = 0; |
| 981 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 982 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 983 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 984 | } |
| 985 | |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 986 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 990 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 991 | String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 992 | adjustStreamVolume(streamType, direction, flags, callingPackage, Binder.getCallingUid()); |
| 993 | } |
| 994 | |
| 995 | private void adjustStreamVolume(int streamType, int direction, int flags, |
| 996 | String callingPackage, int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 997 | if (mUseFixedVolume) { |
| 998 | return; |
| 999 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1000 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream="+streamType+", dir="+direction |
| 1001 | + ", flags="+flags); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1002 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | ensureValidDirection(direction); |
| 1004 | ensureValidStreamType(streamType); |
| 1005 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1006 | boolean isMuteAdjust = isMuteAdjust(direction); |
| 1007 | |
John Spurlock | 3ce3725 | 2015-02-17 13:20:45 -0500 | [diff] [blame] | 1008 | if (isMuteAdjust && !isStreamAffectedByMute(streamType)) { |
| 1009 | return; |
| 1010 | } |
| 1011 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 1012 | // use stream type alias here so that streams with same alias have the same behavior, |
| 1013 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 1014 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1015 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1016 | |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1017 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1018 | |
| 1019 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1020 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1021 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1023 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1024 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1025 | // skip a2dp absolute volume control request when the device |
| 1026 | // is not an a2dp device |
| 1027 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1028 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1029 | return; |
| 1030 | } |
| 1031 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1032 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1033 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1037 | // reset any pending volume command |
| 1038 | synchronized (mSafeMediaVolumeState) { |
| 1039 | mPendingVolumeCommand = null; |
| 1040 | } |
| 1041 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1042 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 1043 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 1044 | ((device & mFixedVolumeDevices) != 0)) { |
| 1045 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1046 | |
| 1047 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1048 | // volume is enforced, and max and 0 for the others. |
| 1049 | // This is simulated by stepping by the full allowed volume range |
| 1050 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1051 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1052 | step = mSafeMediaVolumeIndex; |
| 1053 | } else { |
| 1054 | step = streamState.getMaxIndex(); |
| 1055 | } |
| 1056 | if (aliasIndex != 0) { |
| 1057 | aliasIndex = step; |
| 1058 | } |
| 1059 | } else { |
| 1060 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1061 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1062 | } |
| 1063 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1064 | // If either the client forces allowing ringer modes for this adjustment, |
| 1065 | // or the stream type is one that is affected by ringer modes |
| 1066 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1067 | (streamTypeAlias == getUiSoundsStreamType())) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1068 | int ringerMode = getRingerModeInternal(); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1069 | // do not vibrate if already in vibrate mode |
| 1070 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1071 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1072 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1073 | // Check if the ringer mode handles this adjustment. If it does we don't |
| 1074 | // need to adjust the volume further. |
| 1075 | final int result = checkForRingerModeChange(aliasIndex, direction, step, streamState.mIsMuted); |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1076 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1077 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1078 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1079 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1080 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1081 | // If suppressing a volume down adjustment in vibrate mode, display the UI hint |
| 1082 | if ((result & AudioManager.FLAG_SHOW_VIBRATE_HINT) != 0) { |
| 1083 | flags |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 1084 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1085 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1086 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1087 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1088 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1089 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1090 | mAudioHandler.removeMessages(MSG_UNMUTE_STREAM); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1091 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1092 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1093 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1094 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1095 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1096 | synchronized (mA2dpAvrcpLock) { |
| 1097 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1098 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1099 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1102 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1103 | if (isMuteAdjust) { |
| 1104 | boolean state; |
| 1105 | if (direction == AudioManager.ADJUST_TOGGLE_MUTE) { |
| 1106 | state = !streamState.mIsMuted; |
| 1107 | } else { |
| 1108 | state = direction == AudioManager.ADJUST_MUTE; |
| 1109 | } |
| 1110 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1111 | setSystemAudioMute(state); |
| 1112 | } |
| 1113 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1114 | if (streamTypeAlias == mStreamVolumeAlias[stream]) { |
| 1115 | mStreamStates[stream].mute(state); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } else if ((direction == AudioManager.ADJUST_RAISE) && |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1119 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1120 | Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1121 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1122 | } else if (streamState.adjustIndex(direction * step, device) || streamState.mIsMuted) { |
| 1123 | // Post message to set system volume (it in turn will post a |
| 1124 | // message to persist). |
| 1125 | if (streamState.mIsMuted) { |
| 1126 | // Unmute the stream if it was previously muted |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1127 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1128 | // unmute immediately for volume up |
| 1129 | streamState.mute(false); |
| 1130 | } else if (direction == AudioManager.ADJUST_LOWER) { |
| 1131 | sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE, |
| 1132 | streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY); |
| 1133 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1134 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1135 | sendMsg(mAudioHandler, |
| 1136 | MSG_SET_DEVICE_VOLUME, |
| 1137 | SENDMSG_QUEUE, |
| 1138 | device, |
| 1139 | 0, |
| 1140 | streamState, |
| 1141 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1142 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1143 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1144 | // Check if volume update should be sent to Hdmi system audio. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1145 | int newIndex = mStreamStates[streamType].getIndex(device); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1146 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1147 | setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags); |
| 1148 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1149 | if (mHdmiManager != null) { |
| 1150 | synchronized (mHdmiManager) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1151 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1152 | if (mHdmiCecSink && |
| 1153 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1154 | oldIndex != newIndex) { |
| 1155 | synchronized (mHdmiPlaybackClient) { |
| 1156 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1157 | KeyEvent.KEYCODE_VOLUME_UP; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1158 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1159 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1160 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1164 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1165 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1166 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 1169 | // Called after a delay when volume down is pressed while muted |
| 1170 | private void onUnmuteStream(int stream, int flags) { |
| 1171 | VolumeStreamState streamState = mStreamStates[stream]; |
| 1172 | streamState.mute(false); |
| 1173 | |
| 1174 | final int device = getDeviceForStream(stream); |
| 1175 | final int index = mStreamStates[stream].getIndex(device); |
| 1176 | sendVolumeUpdate(stream, index, index, flags); |
| 1177 | } |
| 1178 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1179 | private void setSystemAudioVolume(int oldVolume, int newVolume, int maxVolume, int flags) { |
| 1180 | if (mHdmiManager == null |
| 1181 | || mHdmiTvClient == null |
| 1182 | || oldVolume == newVolume |
| 1183 | || (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) != 0) return; |
| 1184 | |
| 1185 | // Sets the audio volume of AVR when we are in system audio mode. The new volume info |
| 1186 | // is tranformed to HDMI-CEC commands and passed through CEC bus. |
| 1187 | synchronized (mHdmiManager) { |
| 1188 | if (!mHdmiSystemAudioSupported) return; |
| 1189 | synchronized (mHdmiTvClient) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1190 | final long token = Binder.clearCallingIdentity(); |
| 1191 | try { |
Jinsuk Kim | 7a9ba42 | 2015-02-16 16:47:38 +0900 | [diff] [blame] | 1192 | mHdmiTvClient.setSystemAudioVolume(oldVolume, newVolume, maxVolume); |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1193 | } finally { |
| 1194 | Binder.restoreCallingIdentity(token); |
| 1195 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1200 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1201 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1202 | class StreamVolumeCommand { |
| 1203 | public final int mStreamType; |
| 1204 | public final int mIndex; |
| 1205 | public final int mFlags; |
| 1206 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1207 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1208 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1209 | mStreamType = streamType; |
| 1210 | mIndex = index; |
| 1211 | mFlags = flags; |
| 1212 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1213 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1214 | |
| 1215 | @Override |
| 1216 | public String toString() { |
| 1217 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1218 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1219 | .append(mDevice).append('}').toString(); |
| 1220 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1221 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1222 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1223 | private void onSetStreamVolume(int streamType, int index, int flags, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1224 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1225 | // setting volume on ui sounds stream type also controls silent mode |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1226 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1227 | (mStreamVolumeAlias[streamType] == getUiSoundsStreamType())) { |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1228 | int newRingerMode; |
| 1229 | if (index == 0) { |
| 1230 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1231 | : VOLUME_SETS_RINGER_MODE_SILENT ? AudioManager.RINGER_MODE_SILENT |
| 1232 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1233 | } else { |
| 1234 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1235 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1236 | setRingerMode(newRingerMode, TAG + ".onSetStreamVolume", false /*external*/); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1237 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1241 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1242 | setStreamVolume(streamType, index, flags, callingPackage, Binder.getCallingUid()); |
| 1243 | } |
| 1244 | |
| 1245 | private void setStreamVolume(int streamType, int index, int flags, String callingPackage, |
| 1246 | int uid) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1247 | if (mUseFixedVolume) { |
| 1248 | return; |
| 1249 | } |
| 1250 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1251 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1252 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1253 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1254 | |
| 1255 | final int device = getDeviceForStream(streamType); |
| 1256 | int oldIndex; |
| 1257 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1258 | // skip a2dp absolute volume control request when the device |
| 1259 | // is not an a2dp device |
| 1260 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1261 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1262 | return; |
| 1263 | } |
| 1264 | |
John Spurlock | 59dc9c1 | 2015-03-02 11:20:15 -0500 | [diff] [blame] | 1265 | if (mAppOps.noteOp(STREAM_VOLUME_OPS[streamTypeAlias], uid, callingPackage) |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 1266 | != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1270 | synchronized (mSafeMediaVolumeState) { |
| 1271 | // reset any pending volume command |
| 1272 | mPendingVolumeCommand = null; |
| 1273 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1274 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1275 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1276 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1277 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1278 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1279 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1280 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1281 | synchronized (mA2dpAvrcpLock) { |
| 1282 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1283 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1284 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1288 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC) { |
| 1289 | setSystemAudioVolume(oldIndex, index, getStreamMaxVolume(streamType), flags); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1290 | } |
| 1291 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1292 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1293 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1294 | ((device & mFixedVolumeDevices) != 0)) { |
| 1295 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1296 | |
| 1297 | // volume is either 0 or max allowed for fixed volume devices |
| 1298 | if (index != 0) { |
| 1299 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1300 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1301 | index = mSafeMediaVolumeIndex; |
| 1302 | } else { |
| 1303 | index = streamState.getMaxIndex(); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1308 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1309 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1310 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1311 | streamType, index, flags, device); |
| 1312 | } else { |
| 1313 | onSetStreamVolume(streamType, index, flags, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1314 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1315 | } |
| 1316 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1317 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1320 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1321 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1322 | synchronized(mForceControlStreamLock) { |
| 1323 | mVolumeControlStream = streamType; |
| 1324 | if (mVolumeControlStream == -1) { |
| 1325 | if (mForceControlStreamClient != null) { |
| 1326 | mForceControlStreamClient.release(); |
| 1327 | mForceControlStreamClient = null; |
| 1328 | } |
| 1329 | } else { |
| 1330 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1336 | private IBinder mCb; // To be notified of client's death |
| 1337 | |
| 1338 | ForceControlStreamClient(IBinder cb) { |
| 1339 | if (cb != null) { |
| 1340 | try { |
| 1341 | cb.linkToDeath(this, 0); |
| 1342 | } catch (RemoteException e) { |
| 1343 | // Client has died! |
| 1344 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1345 | cb = null; |
| 1346 | } |
| 1347 | } |
| 1348 | mCb = cb; |
| 1349 | } |
| 1350 | |
| 1351 | public void binderDied() { |
| 1352 | synchronized(mForceControlStreamLock) { |
| 1353 | Log.w(TAG, "SCO client died"); |
| 1354 | if (mForceControlStreamClient != this) { |
| 1355 | Log.w(TAG, "unregistered control stream client died"); |
| 1356 | } else { |
| 1357 | mForceControlStreamClient = null; |
| 1358 | mVolumeControlStream = -1; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | public void release() { |
| 1364 | if (mCb != null) { |
| 1365 | mCb.unlinkToDeath(this, 0); |
| 1366 | mCb = null; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1371 | private void sendBroadcastToAll(Intent intent) { |
Christopher Tate | 267603f | 2015-01-20 14:21:21 -0800 | [diff] [blame] | 1372 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1373 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1374 | final long ident = Binder.clearCallingIdentity(); |
| 1375 | try { |
| 1376 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1377 | } finally { |
| 1378 | Binder.restoreCallingIdentity(ident); |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | private void sendStickyBroadcastToAll(Intent intent) { |
John Spurlock | 8649086 | 2015-02-25 11:22:52 -0500 | [diff] [blame] | 1383 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1384 | final long ident = Binder.clearCallingIdentity(); |
| 1385 | try { |
| 1386 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1387 | } finally { |
| 1388 | Binder.restoreCallingIdentity(ident); |
| 1389 | } |
| 1390 | } |
| 1391 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1392 | // UI update and Broadcast Intent |
| 1393 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1394 | if (!isPlatformVoice() && (streamType == AudioSystem.STREAM_RING)) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1395 | streamType = AudioSystem.STREAM_NOTIFICATION; |
| 1396 | } |
| 1397 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1398 | if (streamType == AudioSystem.STREAM_MUSIC) { |
| 1399 | flags = updateFlagsForSystemAudio(flags); |
Jungshik Jang | 1a6be6e | 2014-09-16 11:04:54 +0900 | [diff] [blame] | 1400 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1401 | mVolumeController.postVolumeChanged(streamType, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1404 | // If Hdmi-CEC system audio mode is on, we show volume bar only when TV |
| 1405 | // receives volume notification from Audio Receiver. |
| 1406 | private int updateFlagsForSystemAudio(int flags) { |
| 1407 | if (mHdmiTvClient != null) { |
| 1408 | synchronized (mHdmiTvClient) { |
| 1409 | if (mHdmiSystemAudioSupported && |
| 1410 | ((flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0)) { |
| 1411 | flags &= ~AudioManager.FLAG_SHOW_UI; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | return flags; |
| 1416 | } |
| 1417 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1418 | // UI update and Broadcast Intent |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1419 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1420 | mVolumeController.postMasterMuteChanged(updateFlagsForSystemAudio(flags)); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1421 | broadcastMasterMuteStatus(muted); |
| 1422 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1423 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1424 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1425 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1426 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1427 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1428 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1429 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1430 | } |
| 1431 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1433 | * Sets the stream state's index, and posts a message to set system volume. |
| 1434 | * This will not call out to the UI. Assumes a valid stream type. |
| 1435 | * |
| 1436 | * @param streamType Type of the stream |
| 1437 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1438 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | * @param force If true, set the volume even if the desired volume is same |
| 1440 | * as the current volume. |
| 1441 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1442 | private void setStreamVolumeInt(int streamType, |
| 1443 | int index, |
| 1444 | int device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1445 | boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1447 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1448 | if (streamState.setIndex(index, device) || force) { |
| 1449 | // Post message to set system volume (it in turn will post a message |
| 1450 | // to persist). |
| 1451 | sendMsg(mAudioHandler, |
| 1452 | MSG_SET_DEVICE_VOLUME, |
| 1453 | SENDMSG_QUEUE, |
| 1454 | device, |
| 1455 | 0, |
| 1456 | streamState, |
| 1457 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1461 | private void setSystemAudioMute(boolean state) { |
| 1462 | if (mHdmiManager == null || mHdmiTvClient == null) return; |
| 1463 | synchronized (mHdmiManager) { |
Jinsuk Kim | 48cbf29 | 2014-12-13 02:16:28 +0900 | [diff] [blame] | 1464 | if (!mHdmiSystemAudioSupported) return; |
| 1465 | synchronized (mHdmiTvClient) { |
| 1466 | final long token = Binder.clearCallingIdentity(); |
| 1467 | try { |
| 1468 | mHdmiTvClient.setSystemAudioMute(state); |
| 1469 | } finally { |
| 1470 | Binder.restoreCallingIdentity(token); |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1471 | } |
Jinsuk Kim | 336cdb4 | 2014-11-29 06:27:00 +0900 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1476 | /** get stream mute state. */ |
| 1477 | public boolean isStreamMute(int streamType) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1478 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1479 | streamType = getActiveStreamType(streamType); |
| 1480 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1481 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1482 | return mStreamStates[streamType].mIsMuted; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1483 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 1486 | private class RmtSbmxFullVolDeathHandler implements IBinder.DeathRecipient { |
| 1487 | private IBinder mICallback; // To be notified of client's death |
| 1488 | |
| 1489 | RmtSbmxFullVolDeathHandler(IBinder cb) { |
| 1490 | mICallback = cb; |
| 1491 | try { |
| 1492 | cb.linkToDeath(this, 0/*flags*/); |
| 1493 | } catch (RemoteException e) { |
| 1494 | Log.e(TAG, "can't link to death", e); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | boolean isHandlerFor(IBinder cb) { |
| 1499 | return mICallback.equals(cb); |
| 1500 | } |
| 1501 | |
| 1502 | void forget() { |
| 1503 | try { |
| 1504 | mICallback.unlinkToDeath(this, 0/*flags*/); |
| 1505 | } catch (NoSuchElementException e) { |
| 1506 | Log.e(TAG, "error unlinking to death", e); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | public void binderDied() { |
| 1511 | Log.w(TAG, "Recorder with remote submix at full volume died " + mICallback); |
| 1512 | forceRemoteSubmixFullVolume(false, mICallback); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /** |
| 1517 | * call must be synchronized on mRmtSbmxFullVolDeathHandlers |
| 1518 | * @return true if there is a registered death handler, false otherwise */ |
| 1519 | private boolean discardRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1520 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1521 | while (it.hasNext()) { |
| 1522 | final RmtSbmxFullVolDeathHandler handler = it.next(); |
| 1523 | if (handler.isHandlerFor(cb)) { |
| 1524 | handler.forget(); |
| 1525 | mRmtSbmxFullVolDeathHandlers.remove(handler); |
| 1526 | return true; |
| 1527 | } |
| 1528 | } |
| 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | /** call synchronized on mRmtSbmxFullVolDeathHandlers */ |
| 1533 | private boolean hasRmtSbmxFullVolDeathHandlerFor(IBinder cb) { |
| 1534 | Iterator<RmtSbmxFullVolDeathHandler> it = mRmtSbmxFullVolDeathHandlers.iterator(); |
| 1535 | while (it.hasNext()) { |
| 1536 | if (it.next().isHandlerFor(cb)) { |
| 1537 | return true; |
| 1538 | } |
| 1539 | } |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
| 1543 | private int mRmtSbmxFullVolRefCount = 0; |
| 1544 | private ArrayList<RmtSbmxFullVolDeathHandler> mRmtSbmxFullVolDeathHandlers = |
| 1545 | new ArrayList<RmtSbmxFullVolDeathHandler>(); |
| 1546 | |
| 1547 | public void forceRemoteSubmixFullVolume(boolean startForcing, IBinder cb) { |
| 1548 | if (cb == null) { |
| 1549 | return; |
| 1550 | } |
| 1551 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1552 | android.Manifest.permission.CAPTURE_AUDIO_OUTPUT))) { |
| 1553 | Log.w(TAG, "Trying to call forceRemoteSubmixFullVolume() without CAPTURE_AUDIO_OUTPUT"); |
| 1554 | return; |
| 1555 | } |
| 1556 | synchronized(mRmtSbmxFullVolDeathHandlers) { |
| 1557 | boolean applyRequired = false; |
| 1558 | if (startForcing) { |
| 1559 | if (!hasRmtSbmxFullVolDeathHandlerFor(cb)) { |
| 1560 | mRmtSbmxFullVolDeathHandlers.add(new RmtSbmxFullVolDeathHandler(cb)); |
| 1561 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1562 | mFullVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1563 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1564 | applyRequired = true; |
| 1565 | } |
| 1566 | mRmtSbmxFullVolRefCount++; |
| 1567 | } |
| 1568 | } else { |
| 1569 | if (discardRmtSbmxFullVolDeathHandlerFor(cb) && (mRmtSbmxFullVolRefCount > 0)) { |
| 1570 | mRmtSbmxFullVolRefCount--; |
| 1571 | if (mRmtSbmxFullVolRefCount == 0) { |
| 1572 | mFullVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1573 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 1574 | applyRequired = true; |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | if (applyRequired) { |
| 1579 | // Assumes only STREAM_MUSIC going through DEVICE_OUT_REMOTE_SUBMIX |
| 1580 | checkAllFixedVolumeDevices(AudioSystem.STREAM_MUSIC); |
| 1581 | mStreamStates[AudioSystem.STREAM_MUSIC].applyAllVolumes(); |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1586 | private void setMasterMuteInternal(boolean mute, int flags, String callingPackage, int uid) { |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1587 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, uid, callingPackage) |
| 1588 | != AppOpsManager.MODE_ALLOWED) { |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1589 | return; |
| 1590 | } |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1591 | if (mute != AudioSystem.getMasterMute()) { |
| 1592 | setSystemAudioMute(mute); |
| 1593 | AudioSystem.setMasterMute(mute); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1594 | // Post a persist master volume msg |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1595 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, mute ? 1 |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1596 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1597 | sendMasterMuteUpdate(mute, flags); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1598 | |
| 1599 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1600 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, mute); |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 1601 | sendBroadcastToAll(intent); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1602 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /** get master mute state. */ |
| 1606 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1607 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1610 | public void setMasterMute(boolean mute, int flags, String callingPackage) { |
| 1611 | setMasterMuteInternal(mute, flags, callingPackage, Binder.getCallingUid()); |
| 1612 | } |
| 1613 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 1614 | protected static int getMaxStreamVolume(int streamType) { |
| 1615 | return MAX_STREAM_VOLUME[streamType]; |
| 1616 | } |
| 1617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | /** @see AudioManager#getStreamVolume(int) */ |
| 1619 | public int getStreamVolume(int streamType) { |
| 1620 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1621 | int device = getDeviceForStream(streamType); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1622 | synchronized (VolumeStreamState.class) { |
| 1623 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1624 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1625 | // by convention getStreamVolume() returns 0 when a stream is muted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1626 | if (mStreamStates[streamType].mIsMuted) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 1627 | index = 0; |
| 1628 | } |
| 1629 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 1630 | (device & mFixedVolumeDevices) != 0) { |
| 1631 | index = mStreamStates[streamType].getMaxIndex(); |
| 1632 | } |
| 1633 | return (index + 5) / 10; |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1638 | public int getStreamMaxVolume(int streamType) { |
| 1639 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1640 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1643 | /** Get last audible volume before stream was muted. */ |
| 1644 | public int getLastAudibleStreamVolume(int streamType) { |
| 1645 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1646 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1647 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1648 | } |
| 1649 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1650 | /** @see AudioManager#getUiSoundsStreamType() */ |
| 1651 | public int getUiSoundsStreamType() { |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1652 | return mStreamVolumeAlias[AudioSystem.STREAM_SYSTEM]; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1655 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 1656 | public void setMicrophoneMute(boolean on, String callingPackage) { |
| 1657 | if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, Binder.getCallingUid(), |
| 1658 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1659 | return; |
| 1660 | } |
Jean-Michel Trivi | 4a4fea0 | 2014-08-29 18:14:09 -0700 | [diff] [blame] | 1661 | if (!checkAudioSettingsPermission("setMicrophoneMute()")) { |
| 1662 | return; |
| 1663 | } |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1664 | |
| 1665 | AudioSystem.muteMicrophone(on); |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 1666 | // Post a persist microphone msg. |
| 1667 | sendMsg(mAudioHandler, MSG_PERSIST_MICROPHONE_MUTE, SENDMSG_REPLACE, on ? 1 |
| 1668 | : 0, UserHandle.getCallingUserId(), null, PERSIST_DELAY); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1669 | } |
| 1670 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1671 | @Override |
| 1672 | public int getRingerModeExternal() { |
| 1673 | synchronized(mSettingsLock) { |
| 1674 | return mRingerModeExternal; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | @Override |
| 1679 | public int getRingerModeInternal() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1680 | synchronized(mSettingsLock) { |
| 1681 | return mRingerMode; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | private void ensureValidRingerMode(int ringerMode) { |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1686 | if (!isValidRingerMode(ringerMode)) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1687 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1691 | /** @see AudioManager#isValidRingerMode(int) */ |
| 1692 | public boolean isValidRingerMode(int ringerMode) { |
| 1693 | return ringerMode >= 0 && ringerMode <= AudioManager.RINGER_MODE_MAX; |
| 1694 | } |
| 1695 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1696 | public void setRingerModeExternal(int ringerMode, String caller) { |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1697 | setRingerMode(ringerMode, caller, true /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | public void setRingerModeInternal(int ringerMode, String caller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1701 | enforceVolumeController("setRingerModeInternal"); |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1702 | setRingerMode(ringerMode, caller, false /*external*/); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | private void setRingerMode(int ringerMode, String caller, boolean external) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1706 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1707 | return; |
| 1708 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1709 | if (caller == null || caller.length() == 0) { |
| 1710 | throw new IllegalArgumentException("Bad caller: " + caller); |
| 1711 | } |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1712 | ensureValidRingerMode(ringerMode); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1713 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1714 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1715 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1716 | final long identity = Binder.clearCallingIdentity(); |
| 1717 | try { |
| 1718 | synchronized (mSettingsLock) { |
| 1719 | final int ringerModeInternal = getRingerModeInternal(); |
| 1720 | final int ringerModeExternal = getRingerModeExternal(); |
| 1721 | if (external) { |
| 1722 | setRingerModeExt(ringerMode); |
| 1723 | if (mRingerModeDelegate != null) { |
| 1724 | ringerMode = mRingerModeDelegate.onSetRingerModeExternal(ringerModeExternal, |
| 1725 | ringerMode, caller, ringerModeInternal); |
| 1726 | } |
| 1727 | if (ringerMode != ringerModeInternal) { |
| 1728 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1729 | } |
| 1730 | } else /*internal*/ { |
| 1731 | if (ringerMode != ringerModeInternal) { |
| 1732 | setRingerModeInt(ringerMode, true /*persist*/); |
| 1733 | } |
| 1734 | if (mRingerModeDelegate != null) { |
| 1735 | ringerMode = mRingerModeDelegate.onSetRingerModeInternal(ringerModeInternal, |
| 1736 | ringerMode, caller, ringerModeExternal); |
| 1737 | } |
| 1738 | setRingerModeExt(ringerMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1739 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1740 | } |
John Spurlock | af88a19 | 2014-12-23 16:14:44 -0500 | [diff] [blame] | 1741 | } finally { |
| 1742 | Binder.restoreCallingIdentity(identity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1746 | private void setRingerModeExt(int ringerMode) { |
| 1747 | synchronized(mSettingsLock) { |
| 1748 | if (ringerMode == mRingerModeExternal) return; |
| 1749 | mRingerModeExternal = ringerMode; |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1750 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1751 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1752 | broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode); |
John Spurlock | e5b42d9 | 2014-10-15 12:03:48 -0400 | [diff] [blame] | 1753 | } |
| 1754 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1755 | private void setRingerModeInt(int ringerMode, boolean persist) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1756 | final boolean change; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1757 | synchronized(mSettingsLock) { |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1758 | change = mRingerMode != ringerMode; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1759 | mRingerMode = ringerMode; |
| 1760 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1761 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1762 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 1763 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 1764 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 1765 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1766 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1767 | final boolean ringerModeMute = ringerMode == AudioManager.RINGER_MODE_VIBRATE |
| 1768 | || ringerMode == AudioManager.RINGER_MODE_SILENT; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1769 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1770 | final boolean isMuted = isStreamMutedByRingerMode(streamType); |
| 1771 | final boolean shouldMute = ringerModeMute && isStreamAffectedByRingerMode(streamType); |
| 1772 | if (isMuted == shouldMute) continue; |
| 1773 | if (!shouldMute) { |
| 1774 | // unmute |
| 1775 | // ring and notifications volume should never be 0 when not silenced |
| 1776 | // on voice capable devices or devices that support vibration |
| 1777 | if ((isPlatformVoice() || mHasVibrator) && |
| 1778 | mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
| 1779 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 1780 | SparseIntArray indexMap = mStreamStates[streamType].mIndexMap; |
| 1781 | for (int i = 0; i < indexMap.size(); i++) { |
| 1782 | int device = indexMap.keyAt(i); |
| 1783 | int value = indexMap.valueAt(i); |
| 1784 | if (value == 0) { |
| 1785 | indexMap.put(device, 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1786 | } |
| 1787 | } |
Eric Laurent | 9e0d25f | 2015-02-12 17:28:53 -0800 | [diff] [blame] | 1788 | // Persist volume for stream ring when it is changed here |
| 1789 | final int device = getDeviceForStream(streamType); |
| 1790 | sendMsg(mAudioHandler, |
| 1791 | MSG_PERSIST_VOLUME, |
| 1792 | SENDMSG_QUEUE, |
| 1793 | device, |
| 1794 | 0, |
| 1795 | mStreamStates[streamType], |
| 1796 | PERSIST_DELAY); |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1797 | } |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 1798 | } |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1799 | mStreamStates[streamType].mute(false); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1800 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1801 | } else { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1802 | // mute |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1803 | mStreamStates[streamType].mute(true); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1804 | mRingerModeMutedStreams |= (1 << streamType); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1805 | } |
| 1806 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1807 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1808 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1809 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 1810 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1811 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 1812 | } |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 1813 | if (change) { |
| 1814 | // Send sticky broadcast |
| 1815 | broadcastRingerMode(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION, ringerMode); |
| 1816 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1819 | /** @see AudioManager#shouldVibrate(int) */ |
| 1820 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1821 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | |
| 1823 | switch (getVibrateSetting(vibrateType)) { |
| 1824 | |
| 1825 | case AudioManager.VIBRATE_SETTING_ON: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1826 | return getRingerModeExternal() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | |
| 1828 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1829 | return getRingerModeExternal() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | |
| 1831 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 1832 | // return false, even for incoming calls |
| 1833 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | |
| 1835 | default: |
| 1836 | return false; |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | /** @see AudioManager#getVibrateSetting(int) */ |
| 1841 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1842 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 1844 | } |
| 1845 | |
| 1846 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 1847 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 1848 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1849 | if (!mHasVibrator) return; |
| 1850 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 1851 | mVibrateSetting = AudioSystem.getValueForVibrateSetting(mVibrateSetting, vibrateType, |
| 1852 | vibrateSetting); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | |
| 1854 | // Broadcast change |
| 1855 | broadcastVibrateSetting(vibrateType); |
| 1856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1859 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 1860 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1861 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1862 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 1863 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1864 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1865 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1866 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1870 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1871 | synchronized(mSetModeDeathHandlers) { |
| 1872 | Log.w(TAG, "setMode() client died"); |
| 1873 | int index = mSetModeDeathHandlers.indexOf(this); |
| 1874 | if (index < 0) { |
| 1875 | Log.w(TAG, "unregistered setMode() client died"); |
| 1876 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1877 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1878 | } |
| 1879 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1880 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1881 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1882 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 1883 | final long ident = Binder.clearCallingIdentity(); |
| 1884 | disconnectBluetoothSco(newModeOwnerPid); |
| 1885 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1886 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1889 | public int getPid() { |
| 1890 | return mPid; |
| 1891 | } |
| 1892 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1893 | public void setMode(int mode) { |
| 1894 | mMode = mode; |
| 1895 | } |
| 1896 | |
| 1897 | public int getMode() { |
| 1898 | return mMode; |
| 1899 | } |
| 1900 | |
| 1901 | public IBinder getBinder() { |
| 1902 | return mCb; |
| 1903 | } |
| 1904 | } |
| 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | /** @see AudioManager#setMode(int) */ |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1907 | public void setMode(int mode, IBinder cb) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1908 | if (DEBUG_MODE) { Log.v(TAG, "setMode(mode=" + mode + ")"); } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1909 | if (!checkAudioSettingsPermission("setMode()")) { |
| 1910 | return; |
| 1911 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1912 | |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 1913 | if ( (mode == AudioSystem.MODE_IN_CALL) && |
| 1914 | (mContext.checkCallingOrSelfPermission( |
| 1915 | android.Manifest.permission.MODIFY_PHONE_STATE) |
| 1916 | != PackageManager.PERMISSION_GRANTED)) { |
| 1917 | Log.w(TAG, "MODIFY_PHONE_STATE Permission Denial: setMode(MODE_IN_CALL) from pid=" |
| 1918 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1919 | return; |
| 1920 | } |
| 1921 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1922 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1923 | return; |
| 1924 | } |
| 1925 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1926 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1927 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1928 | if (mode == AudioSystem.MODE_CURRENT) { |
| 1929 | mode = mMode; |
| 1930 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1931 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid()); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1932 | } |
| 1933 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1934 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1935 | if (newModeOwnerPid != 0) { |
| 1936 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1939 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1940 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1941 | // 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] | 1942 | // any mode other than NORMAL. |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 1943 | private int setModeInt(int mode, IBinder cb, int pid) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1944 | if (DEBUG_MODE) { Log.v(TAG, "setModeInt(mode=" + mode + ", pid=" + pid + ")"); } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1945 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1946 | if (cb == null) { |
| 1947 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1948 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1949 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1950 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1951 | SetModeDeathHandler hdlr = null; |
| 1952 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 1953 | while (iter.hasNext()) { |
| 1954 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 1955 | if (h.getPid() == pid) { |
| 1956 | hdlr = h; |
| 1957 | // Remove from client list so that it is re-inserted at top of list |
| 1958 | iter.remove(); |
| 1959 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 1964 | do { |
| 1965 | if (mode == AudioSystem.MODE_NORMAL) { |
| 1966 | // get new mode from client at top the list if any |
| 1967 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 1968 | hdlr = mSetModeDeathHandlers.get(0); |
| 1969 | cb = hdlr.getBinder(); |
| 1970 | mode = hdlr.getMode(); |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1971 | if (DEBUG_MODE) { |
| 1972 | Log.w(TAG, " using mode=" + mode + " instead due to death hdlr at pid=" |
| 1973 | + hdlr.mPid); |
| 1974 | } |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 1975 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1976 | } else { |
| 1977 | if (hdlr == null) { |
| 1978 | hdlr = new SetModeDeathHandler(cb, pid); |
| 1979 | } |
| 1980 | // Register for client death notification |
| 1981 | try { |
| 1982 | cb.linkToDeath(hdlr, 0); |
| 1983 | } catch (RemoteException e) { |
| 1984 | // Client has died! |
| 1985 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 1986 | } |
| 1987 | |
| 1988 | // Last client to call setMode() is always at top of client list |
| 1989 | // as required by SetModeDeathHandler.binderDied() |
| 1990 | mSetModeDeathHandlers.add(0, hdlr); |
| 1991 | hdlr.setMode(mode); |
| 1992 | } |
| 1993 | |
| 1994 | if (mode != mMode) { |
| 1995 | status = AudioSystem.setPhoneState(mode); |
| 1996 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 1997 | if (DEBUG_MODE) { Log.v(TAG, " mode successfully set to " + mode); } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1998 | mMode = mode; |
| 1999 | } else { |
| 2000 | if (hdlr != null) { |
| 2001 | mSetModeDeathHandlers.remove(hdlr); |
| 2002 | cb.unlinkToDeath(hdlr, 0); |
| 2003 | } |
| 2004 | // force reading new top of mSetModeDeathHandlers stack |
Jean-Michel Trivi | 339567d | 2014-07-29 09:53:34 -0700 | [diff] [blame] | 2005 | 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] | 2006 | mode = AudioSystem.MODE_NORMAL; |
| 2007 | } |
| 2008 | } else { |
| 2009 | status = AudioSystem.AUDIO_STATUS_OK; |
| 2010 | } |
| 2011 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 2012 | |
| 2013 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 2014 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2015 | if (mSetModeDeathHandlers.isEmpty()) { |
| 2016 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 2017 | } else { |
| 2018 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 2019 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | } |
| 2021 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2022 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2023 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
| 2024 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2025 | |
| 2026 | updateStreamVolumeAlias(true /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2027 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2028 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /** @see AudioManager#getMode() */ |
| 2032 | public int getMode() { |
| 2033 | return mMode; |
| 2034 | } |
| 2035 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2036 | //========================================================================================== |
| 2037 | // Sound Effects |
| 2038 | //========================================================================================== |
| 2039 | |
| 2040 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 2041 | private static final String ATTR_VERSION = "version"; |
| 2042 | private static final String TAG_GROUP = "group"; |
| 2043 | private static final String ATTR_GROUP_NAME = "name"; |
| 2044 | private static final String TAG_ASSET = "asset"; |
| 2045 | private static final String ATTR_ASSET_ID = "id"; |
| 2046 | private static final String ATTR_ASSET_FILE = "file"; |
| 2047 | |
| 2048 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 2049 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 2050 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2051 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2052 | |
| 2053 | class LoadSoundEffectReply { |
| 2054 | public int mStatus = 1; |
| 2055 | }; |
| 2056 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2057 | private void loadTouchSoundAssetDefaults() { |
| 2058 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 2059 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 2060 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 2061 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | private void loadTouchSoundAssets() { |
| 2066 | XmlResourceParser parser = null; |
| 2067 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2068 | // only load assets once. |
| 2069 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 2070 | return; |
| 2071 | } |
| 2072 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 2073 | loadTouchSoundAssetDefaults(); |
| 2074 | |
| 2075 | try { |
| 2076 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 2077 | |
| 2078 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 2079 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 2080 | boolean inTouchSoundsGroup = false; |
| 2081 | |
| 2082 | if (ASSET_FILE_VERSION.equals(version)) { |
| 2083 | while (true) { |
| 2084 | XmlUtils.nextElement(parser); |
| 2085 | String element = parser.getName(); |
| 2086 | if (element == null) { |
| 2087 | break; |
| 2088 | } |
| 2089 | if (element.equals(TAG_GROUP)) { |
| 2090 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 2091 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 2092 | inTouchSoundsGroup = true; |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | while (inTouchSoundsGroup) { |
| 2098 | XmlUtils.nextElement(parser); |
| 2099 | String element = parser.getName(); |
| 2100 | if (element == null) { |
| 2101 | break; |
| 2102 | } |
| 2103 | if (element.equals(TAG_ASSET)) { |
| 2104 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 2105 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 2106 | int fx; |
| 2107 | |
| 2108 | try { |
| 2109 | Field field = AudioManager.class.getField(id); |
| 2110 | fx = field.getInt(null); |
| 2111 | } catch (Exception e) { |
| 2112 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 2113 | continue; |
| 2114 | } |
| 2115 | |
| 2116 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 2117 | if (i == -1) { |
| 2118 | i = SOUND_EFFECT_FILES.size(); |
| 2119 | SOUND_EFFECT_FILES.add(file); |
| 2120 | } |
| 2121 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 2122 | } else { |
| 2123 | break; |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | } catch (Resources.NotFoundException e) { |
| 2128 | Log.w(TAG, "audio assets file not found", e); |
| 2129 | } catch (XmlPullParserException e) { |
| 2130 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2131 | } catch (IOException e) { |
| 2132 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2133 | } finally { |
| 2134 | if (parser != null) { |
| 2135 | parser.close(); |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2140 | /** @see AudioManager#playSoundEffect(int) */ |
| 2141 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2142 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2147 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2148 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2149 | return; |
| 2150 | } |
| 2151 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2152 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2153 | effectType, (int) (volume * 1000), null, 0); |
| 2154 | } |
| 2155 | |
| 2156 | /** |
| 2157 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2158 | * 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] | 2159 | */ |
| 2160 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2161 | int attempts = 3; |
| 2162 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2163 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2164 | synchronized (reply) { |
| 2165 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2166 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2167 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2168 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2169 | } catch (InterruptedException e) { |
| 2170 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2171 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2172 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2173 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2174 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | /** |
| 2178 | * Unloads samples from the sound pool. |
| 2179 | * This method can be called to free some memory when |
| 2180 | * sound effects are disabled. |
| 2181 | */ |
| 2182 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2183 | 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] | 2184 | } |
| 2185 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2186 | class SoundPoolListenerThread extends Thread { |
| 2187 | public SoundPoolListenerThread() { |
| 2188 | super("SoundPoolListenerThread"); |
| 2189 | } |
| 2190 | |
| 2191 | @Override |
| 2192 | public void run() { |
| 2193 | |
| 2194 | Looper.prepare(); |
| 2195 | mSoundPoolLooper = Looper.myLooper(); |
| 2196 | |
| 2197 | synchronized (mSoundEffectsLock) { |
| 2198 | if (mSoundPool != null) { |
| 2199 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2200 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2201 | } |
| 2202 | mSoundEffectsLock.notify(); |
| 2203 | } |
| 2204 | Looper.loop(); |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | private final class SoundPoolCallback implements |
| 2209 | android.media.SoundPool.OnLoadCompleteListener { |
| 2210 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2211 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2212 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2213 | |
| 2214 | public int status() { |
| 2215 | return mStatus; |
| 2216 | } |
| 2217 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2218 | public void setSamples(int[] samples) { |
| 2219 | for (int i = 0; i < samples.length; i++) { |
| 2220 | // do not wait ack for samples rejected upfront by SoundPool |
| 2221 | if (samples[i] > 0) { |
| 2222 | mSamples.add(samples[i]); |
| 2223 | } |
| 2224 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2228 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2229 | int i = mSamples.indexOf(sampleId); |
| 2230 | if (i >= 0) { |
| 2231 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2232 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2233 | if ((status != 0) || mSamples. isEmpty()) { |
| 2234 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2235 | mSoundEffectsLock.notify(); |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2241 | /** @see AudioManager#reloadAudioSettings() */ |
| 2242 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2243 | readAudioSettings(false /*userSwitch*/); |
| 2244 | } |
| 2245 | |
| 2246 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2247 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2248 | readPersistedSettings(); |
| 2249 | |
| 2250 | // restore volume settings |
| 2251 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2252 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2253 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2254 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2255 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2256 | continue; |
| 2257 | } |
| 2258 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 2259 | streamState.readSettings(); |
| 2260 | synchronized (VolumeStreamState.class) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2261 | // unmute stream that was muted but is not affect by mute anymore |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2262 | if (streamState.mIsMuted && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2263 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 2264 | streamState.mIsMuted = false; |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2265 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2269 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2270 | // muted by ringer mode have the correct volume |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2271 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2272 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2273 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2274 | checkAllAliasStreamVolumes(); |
| 2275 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2276 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2277 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2278 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2279 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2280 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2281 | enforceSafeMediaVolume(); |
| 2282 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2283 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2286 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2287 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2288 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2289 | return; |
| 2290 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2291 | |
| 2292 | if (on) { |
| 2293 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2294 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2295 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2296 | } |
| 2297 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2298 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2299 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2300 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2301 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2302 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2303 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2307 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2308 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2311 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2312 | public void setBluetoothScoOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2313 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2314 | return; |
| 2315 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2316 | |
| 2317 | if (on) { |
| 2318 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2319 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2320 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2321 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2322 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2323 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2324 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2325 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2326 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2330 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2331 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2334 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2335 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2336 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2337 | mBluetoothA2dpEnabled = on; |
| 2338 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2339 | AudioSystem.FOR_MEDIA, |
| 2340 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2341 | null, 0); |
| 2342 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2346 | public boolean isBluetoothA2dpOn() { |
| 2347 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2348 | return mBluetoothA2dpEnabled; |
| 2349 | } |
| 2350 | } |
| 2351 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2352 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2353 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2354 | int scoAudioMode = |
| 2355 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2356 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2357 | startBluetoothScoInt(cb, scoAudioMode); |
| 2358 | } |
| 2359 | |
| 2360 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2361 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2362 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2363 | } |
| 2364 | |
| 2365 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2366 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2367 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2368 | return; |
| 2369 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2370 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2371 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2372 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2373 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2374 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2375 | // client is created. |
| 2376 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2377 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2378 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /** @see AudioManager#stopBluetoothSco() */ |
| 2382 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2383 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2384 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2385 | return; |
| 2386 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2387 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2388 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2389 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2390 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2391 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2392 | if (client != null) { |
| 2393 | client.decCount(); |
| 2394 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2395 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2398 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2399 | private class ScoClient implements IBinder.DeathRecipient { |
| 2400 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2401 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2402 | private int mStartcount; // number of SCO connections started by this client |
| 2403 | |
| 2404 | ScoClient(IBinder cb) { |
| 2405 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2406 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2407 | mStartcount = 0; |
| 2408 | } |
| 2409 | |
| 2410 | public void binderDied() { |
| 2411 | synchronized(mScoClients) { |
| 2412 | Log.w(TAG, "SCO client died"); |
| 2413 | int index = mScoClients.indexOf(this); |
| 2414 | if (index < 0) { |
| 2415 | Log.w(TAG, "unregistered SCO client died"); |
| 2416 | } else { |
| 2417 | clearCount(true); |
| 2418 | mScoClients.remove(this); |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2423 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2424 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2425 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2426 | if (mStartcount == 0) { |
| 2427 | try { |
| 2428 | mCb.linkToDeath(this, 0); |
| 2429 | } catch (RemoteException e) { |
| 2430 | // client has already died! |
| 2431 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2432 | } |
| 2433 | } |
| 2434 | mStartcount++; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | public void decCount() { |
| 2439 | synchronized(mScoClients) { |
| 2440 | if (mStartcount == 0) { |
| 2441 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2442 | } else { |
| 2443 | mStartcount--; |
| 2444 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2445 | try { |
| 2446 | mCb.unlinkToDeath(this, 0); |
| 2447 | } catch (NoSuchElementException e) { |
| 2448 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2449 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2450 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2451 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | public void clearCount(boolean stopSco) { |
| 2457 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2458 | if (mStartcount != 0) { |
| 2459 | try { |
| 2460 | mCb.unlinkToDeath(this, 0); |
| 2461 | } catch (NoSuchElementException e) { |
| 2462 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2463 | } |
| 2464 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2465 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2466 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2467 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | public int getCount() { |
| 2473 | return mStartcount; |
| 2474 | } |
| 2475 | |
| 2476 | public IBinder getBinder() { |
| 2477 | return mCb; |
| 2478 | } |
| 2479 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2480 | public int getPid() { |
| 2481 | return mCreatorPid; |
| 2482 | } |
| 2483 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2484 | public int totalCount() { |
| 2485 | synchronized(mScoClients) { |
| 2486 | int count = 0; |
| 2487 | int size = mScoClients.size(); |
| 2488 | for (int i = 0; i < size; i++) { |
| 2489 | count += mScoClients.get(i).getCount(); |
| 2490 | } |
| 2491 | return count; |
| 2492 | } |
| 2493 | } |
| 2494 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2495 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2496 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2497 | if (totalCount() == 0) { |
| 2498 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2499 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2500 | // the connection. |
| 2501 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2502 | // 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] | 2503 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2504 | synchronized(mSetModeDeathHandlers) { |
| 2505 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2506 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2507 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2508 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2509 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2510 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2511 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
Andre Eisenbach | 570cc53 | 2014-10-28 17:03:18 -0700 | [diff] [blame] | 2512 | if (mBluetoothHeadsetDevice != null) { |
| 2513 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2514 | mContentResolver, |
| 2515 | "bluetooth_sco_channel_"+ |
| 2516 | mBluetoothHeadsetDevice.getAddress(), |
| 2517 | SCO_MODE_VIRTUAL_CALL)); |
| 2518 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2519 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2520 | } |
| 2521 | } else { |
| 2522 | mScoAudioMode = SCO_MODE_RAW; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2523 | } |
| 2524 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2525 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2526 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2527 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2528 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2529 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2530 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2531 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2532 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2533 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2534 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2535 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2536 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2537 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2538 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2539 | } else { |
| 2540 | broadcastScoConnectionState( |
| 2541 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2542 | } |
| 2543 | } else if (getBluetoothHeadset()) { |
| 2544 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2545 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2546 | } else { |
| 2547 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2548 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2549 | } |
| 2550 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2551 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2552 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2553 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2554 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2555 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2556 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2557 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2558 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2559 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2560 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2561 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2562 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2563 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2564 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2565 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2566 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2567 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2568 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2569 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2570 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2571 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2572 | broadcastScoConnectionState( |
| 2573 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2574 | } |
| 2575 | } else if (getBluetoothHeadset()) { |
| 2576 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2577 | } |
| 2578 | } else { |
| 2579 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2580 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2581 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2587 | private void checkScoAudioState() { |
| 2588 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2589 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2590 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2591 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2592 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2593 | } |
| 2594 | } |
| 2595 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2596 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2597 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2598 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2599 | int size = mScoClients.size(); |
| 2600 | for (int i = 0; i < size; i++) { |
| 2601 | client = mScoClients.get(i); |
| 2602 | if (client.getBinder() == cb) |
| 2603 | return client; |
| 2604 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2605 | if (create) { |
| 2606 | client = new ScoClient(cb); |
| 2607 | mScoClients.add(client); |
| 2608 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2609 | return client; |
| 2610 | } |
| 2611 | } |
| 2612 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2613 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2614 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2615 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2616 | int size = mScoClients.size(); |
| 2617 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2618 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2619 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2620 | cl.clearCount(stopSco); |
| 2621 | } else { |
| 2622 | savedClient = cl; |
| 2623 | } |
| 2624 | } |
| 2625 | mScoClients.clear(); |
| 2626 | if (savedClient != null) { |
| 2627 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2632 | private boolean getBluetoothHeadset() { |
| 2633 | boolean result = false; |
| 2634 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2635 | if (adapter != null) { |
| 2636 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2637 | BluetoothProfile.HEADSET); |
| 2638 | } |
| 2639 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2640 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2641 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2642 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2643 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2644 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2645 | return result; |
| 2646 | } |
| 2647 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2648 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2649 | synchronized(mScoClients) { |
| 2650 | checkScoAudioState(); |
| 2651 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2652 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2653 | if (mBluetoothHeadsetDevice != null) { |
| 2654 | if (mBluetoothHeadset != null) { |
| 2655 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2656 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2657 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2658 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2659 | } |
| 2660 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2661 | getBluetoothHeadset()) { |
| 2662 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2663 | } |
| 2664 | } |
| 2665 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2666 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | private void resetBluetoothSco() { |
| 2672 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2673 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2674 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2675 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2680 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 2681 | SENDMSG_QUEUE, state, 0, null, 0); |
| 2682 | } |
| 2683 | |
| 2684 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2685 | if (state != mScoConnectionState) { |
| 2686 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 2687 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 2688 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 2689 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2690 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2691 | mScoConnectionState = state; |
| 2692 | } |
| 2693 | } |
| 2694 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2695 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 2696 | new BluetoothProfile.ServiceListener() { |
| 2697 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2698 | BluetoothDevice btDevice; |
| 2699 | List<BluetoothDevice> deviceList; |
| 2700 | switch(profile) { |
| 2701 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2702 | synchronized (mConnectedDevices) { |
| 2703 | synchronized (mA2dpAvrcpLock) { |
| 2704 | mA2dp = (BluetoothA2dp) proxy; |
| 2705 | deviceList = mA2dp.getConnectedDevices(); |
| 2706 | if (deviceList.size() > 0) { |
| 2707 | btDevice = deviceList.get(0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2708 | int state = mA2dp.getConnectionState(btDevice); |
| 2709 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2710 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 2711 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2712 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2713 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2714 | state, |
| 2715 | 0, |
| 2716 | btDevice, |
| 2717 | delay); |
| 2718 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 2719 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2720 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2721 | break; |
| 2722 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2723 | case BluetoothProfile.A2DP_SINK: |
| 2724 | deviceList = proxy.getConnectedDevices(); |
| 2725 | if (deviceList.size() > 0) { |
| 2726 | btDevice = deviceList.get(0); |
| 2727 | synchronized (mConnectedDevices) { |
| 2728 | int state = proxy.getConnectionState(btDevice); |
| 2729 | queueMsgUnderWakeLock(mAudioHandler, |
| 2730 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 2731 | state, |
| 2732 | 0, |
| 2733 | btDevice, |
| 2734 | 0 /* delay */); |
| 2735 | } |
| 2736 | } |
| 2737 | break; |
| 2738 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2739 | case BluetoothProfile.HEADSET: |
| 2740 | synchronized (mScoClients) { |
| 2741 | // Discard timeout message |
| 2742 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 2743 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 2744 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 2745 | if (deviceList.size() > 0) { |
| 2746 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 2747 | } else { |
| 2748 | mBluetoothHeadsetDevice = null; |
| 2749 | } |
| 2750 | // Refresh SCO audio state |
| 2751 | checkScoAudioState(); |
| 2752 | // Continue pending action if any |
| 2753 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 2754 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 2755 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2756 | boolean status = false; |
| 2757 | if (mBluetoothHeadsetDevice != null) { |
| 2758 | switch (mScoAudioState) { |
| 2759 | case SCO_STATE_ACTIVATE_REQ: |
| 2760 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2761 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2762 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2763 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2764 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2765 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2766 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2767 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2768 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2769 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2770 | break; |
| 2771 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2772 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2773 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2774 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2775 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2776 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2777 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2778 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2779 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2780 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2781 | break; |
| 2782 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 2783 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2784 | mBluetoothHeadsetDevice); |
| 2785 | } |
| 2786 | } |
| 2787 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2788 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2789 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2790 | } |
| 2791 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2792 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2793 | break; |
| 2794 | |
| 2795 | default: |
| 2796 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2797 | } |
| 2798 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2799 | public void onServiceDisconnected(int profile) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 2800 | switch (profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2801 | case BluetoothProfile.A2DP: |
seunghwan.hong | 4fe7795 | 2014-10-29 17:43:20 +0900 | [diff] [blame] | 2802 | synchronized (mConnectedDevices) { |
| 2803 | synchronized (mA2dpAvrcpLock) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 2804 | // Disconnect ALL DEVICE_OUT_BLUETOOTH_A2DP devices |
| 2805 | for(Map.Entry<String, DeviceListSpec> entry |
| 2806 | : mConnectedDevices.entrySet()) { |
| 2807 | DeviceListSpec deviceSpec = entry.getValue(); |
| 2808 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) { |
| 2809 | makeA2dpDeviceUnavailableNow(deviceSpec.mDeviceAddress); |
| 2810 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2811 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | break; |
| 2815 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2816 | case BluetoothProfile.A2DP_SINK: |
| 2817 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 2818 | // Disconnect ALL DEVICE_IN_BLUETOOTH_A2DP devices |
| 2819 | for(Map.Entry<String, DeviceListSpec> entry |
| 2820 | : mConnectedDevices.entrySet()) { |
| 2821 | DeviceListSpec deviceSpec = entry.getValue(); |
| 2822 | if (deviceSpec.mDeviceType == AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) { |
| 2823 | makeA2dpSrcUnavailable(deviceSpec.mDeviceAddress); |
| 2824 | } |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | break; |
| 2828 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2829 | case BluetoothProfile.HEADSET: |
| 2830 | synchronized (mScoClients) { |
| 2831 | mBluetoothHeadset = null; |
| 2832 | } |
| 2833 | break; |
| 2834 | |
| 2835 | default: |
| 2836 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 2840 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2841 | private void onCheckMusicActive() { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2842 | synchronized (mSafeMediaVolumeState) { |
| 2843 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2844 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 2845 | |
| 2846 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 2847 | sendMsg(mAudioHandler, |
| 2848 | MSG_CHECK_MUSIC_ACTIVE, |
| 2849 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2850 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2851 | 0, |
| 2852 | null, |
| 2853 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2854 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2855 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 2856 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2857 | // Approximate cumulative active music time |
| 2858 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 2859 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
| 2860 | setSafeMediaVolumeEnabled(true); |
| 2861 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2862 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2863 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | } |
| 2868 | } |
| 2869 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2870 | private void saveMusicActiveMs() { |
| 2871 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 2872 | } |
| 2873 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2874 | private void onConfigureSafeVolume(boolean force) { |
| 2875 | synchronized (mSafeMediaVolumeState) { |
| 2876 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 2877 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 2878 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 2879 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 2880 | boolean safeMediaVolumeEnabled = |
| 2881 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 2882 | || mContext.getResources().getBoolean( |
| 2883 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2884 | |
| 2885 | // The persisted state is either "disabled" or "active": this is the state applied |
| 2886 | // next time we boot and cannot be "inactive" |
| 2887 | int persistedState; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2888 | if (safeMediaVolumeEnabled) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2889 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2890 | // The state can already be "inactive" here if the user has forced it before |
| 2891 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 2892 | // it to "active". |
| 2893 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 2894 | if (mMusicActiveMs == 0) { |
| 2895 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2896 | enforceSafeMediaVolume(); |
| 2897 | } else { |
| 2898 | // We have existing playback time recorded, already confirmed. |
| 2899 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 2900 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2901 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2902 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2903 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2904 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 2905 | } |
| 2906 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2907 | sendMsg(mAudioHandler, |
| 2908 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 2909 | SENDMSG_QUEUE, |
| 2910 | persistedState, |
| 2911 | 0, |
| 2912 | null, |
| 2913 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | } |
| 2917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | /////////////////////////////////////////////////////////////////////////// |
| 2919 | // Internal methods |
| 2920 | /////////////////////////////////////////////////////////////////////////// |
| 2921 | |
| 2922 | /** |
| 2923 | * Checks if the adjustment should change ringer mode instead of just |
| 2924 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 2925 | * indices on the stream states. |
| 2926 | */ |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2927 | private int checkForRingerModeChange(int oldIndex, int direction, int step, boolean isMuted) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2928 | int result = FLAG_ADJUST_VOLUME; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2929 | int ringerMode = getRingerModeInternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2930 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2931 | switch (ringerMode) { |
| 2932 | case RINGER_MODE_NORMAL: |
| 2933 | if (direction == AudioManager.ADJUST_LOWER) { |
| 2934 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2935 | // "step" is the delta in internal index units corresponding to a |
| 2936 | // change of 1 in UI index units. |
| 2937 | // Because of rounding when rescaling from one stream index range to its alias |
| 2938 | // index range, we cannot simply test oldIndex == step: |
| 2939 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 2940 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2941 | ringerMode = RINGER_MODE_VIBRATE; |
| 2942 | } |
| 2943 | } else { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2944 | // (oldIndex < step) is equivalent to (old UI index == 0) |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 2945 | if ((oldIndex < step) |
| 2946 | && VOLUME_SETS_RINGER_MODE_SILENT |
| 2947 | && mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2948 | ringerMode = RINGER_MODE_SILENT; |
| 2949 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 2950 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2951 | } else if (direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2952 | || direction == AudioManager.ADJUST_MUTE) { |
| 2953 | if (mHasVibrator) { |
| 2954 | ringerMode = RINGER_MODE_VIBRATE; |
| 2955 | } else { |
| 2956 | ringerMode = RINGER_MODE_SILENT; |
| 2957 | } |
| 2958 | // Setting the ringer mode will toggle mute |
| 2959 | result &= ~FLAG_ADJUST_VOLUME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2960 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2961 | break; |
| 2962 | case RINGER_MODE_VIBRATE: |
| 2963 | if (!mHasVibrator) { |
| 2964 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 2965 | "but no vibrator is present"); |
| 2966 | break; |
| 2967 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2968 | if ((direction == AudioManager.ADJUST_LOWER)) { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2969 | // This is the case we were muted with the volume turned up |
| 2970 | if (oldIndex >= 2 * step && isMuted) { |
| 2971 | ringerMode = RINGER_MODE_NORMAL; |
| 2972 | } else if (mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
John Spurlock | 795a514 | 2014-12-08 14:09:35 -0500 | [diff] [blame] | 2973 | if (VOLUME_SETS_RINGER_MODE_SILENT) { |
| 2974 | ringerMode = RINGER_MODE_SILENT; |
| 2975 | } else { |
| 2976 | result |= AudioManager.FLAG_SHOW_VIBRATE_HINT; |
| 2977 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2978 | } |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2979 | } else if (direction == AudioManager.ADJUST_RAISE |
| 2980 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2981 | || direction == AudioManager.ADJUST_UNMUTE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2982 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2983 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2984 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2985 | break; |
| 2986 | case RINGER_MODE_SILENT: |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2987 | if (direction == AudioManager.ADJUST_LOWER && oldIndex >= 2 * step && isMuted) { |
| 2988 | // This is the case we were muted with the volume turned up |
| 2989 | ringerMode = RINGER_MODE_NORMAL; |
| 2990 | } else if (direction == AudioManager.ADJUST_RAISE |
| 2991 | || direction == AudioManager.ADJUST_TOGGLE_MUTE |
| 2992 | || direction == AudioManager.ADJUST_UNMUTE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2993 | if (PREVENT_VOLUME_ADJUSTMENT_IF_SILENT) { |
| 2994 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2995 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2996 | if (mHasVibrator && direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2997 | ringerMode = RINGER_MODE_VIBRATE; |
| 2998 | } else { |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 2999 | // If we don't have a vibrator or they were toggling mute |
| 3000 | // go straight back to normal. |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3001 | ringerMode = RINGER_MODE_NORMAL; |
| 3002 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3003 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 3004 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3005 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3006 | break; |
| 3007 | default: |
| 3008 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 3009 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3010 | } |
| 3011 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3012 | setRingerMode(ringerMode, TAG + ".checkForRingerModeChange", false /*external*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3013 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3014 | mPrevVolDirection = direction; |
| 3015 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 3016 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 3019 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3020 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 3021 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3022 | } |
| 3023 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3024 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 3025 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 3026 | } |
| 3027 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3028 | boolean updateRingerModeAffectedStreams() { |
| 3029 | int ringerModeAffectedStreams; |
| 3030 | // make sure settings for ringer mode are consistent with device type: non voice capable |
| 3031 | // devices (tablets) include media stream in silent mode whereas phones don't. |
| 3032 | ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
| 3033 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3034 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3035 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 3036 | UserHandle.USER_CURRENT); |
| 3037 | |
| 3038 | // ringtone, notification and system streams are always affected by ringer mode |
| 3039 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_RING)| |
| 3040 | (1 << AudioSystem.STREAM_NOTIFICATION)| |
| 3041 | (1 << AudioSystem.STREAM_SYSTEM); |
| 3042 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3043 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3044 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3045 | ringerModeAffectedStreams = 0; |
| 3046 | break; |
| 3047 | default: |
John Spurlock | 77e54d9 | 2014-08-11 12:16:24 -0400 | [diff] [blame] | 3048 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_MUSIC); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3049 | break; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3050 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3051 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3052 | synchronized (mCameraSoundForced) { |
| 3053 | if (mCameraSoundForced) { |
| 3054 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3055 | } else { |
| 3056 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 3057 | } |
| 3058 | } |
| 3059 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 3060 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 3061 | } else { |
| 3062 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 3063 | } |
| 3064 | |
| 3065 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 3066 | Settings.System.putIntForUser(mContentResolver, |
| 3067 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 3068 | ringerModeAffectedStreams, |
| 3069 | UserHandle.USER_CURRENT); |
| 3070 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 3071 | return true; |
| 3072 | } |
| 3073 | return false; |
| 3074 | } |
| 3075 | |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 3076 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3077 | public boolean isStreamAffectedByMute(int streamType) { |
| 3078 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 3079 | } |
| 3080 | |
| 3081 | private void ensureValidDirection(int direction) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3082 | switch (direction) { |
| 3083 | case AudioManager.ADJUST_LOWER: |
| 3084 | case AudioManager.ADJUST_RAISE: |
| 3085 | case AudioManager.ADJUST_SAME: |
| 3086 | case AudioManager.ADJUST_MUTE: |
| 3087 | case AudioManager.ADJUST_UNMUTE: |
| 3088 | case AudioManager.ADJUST_TOGGLE_MUTE: |
| 3089 | break; |
| 3090 | default: |
| 3091 | throw new IllegalArgumentException("Bad direction " + direction); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 3095 | private void ensureValidSteps(int steps) { |
| 3096 | if (Math.abs(steps) > MAX_BATCH_VOLUME_ADJUST_STEPS) { |
| 3097 | throw new IllegalArgumentException("Bad volume adjust steps " + steps); |
| 3098 | } |
| 3099 | } |
| 3100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3101 | private void ensureValidStreamType(int streamType) { |
| 3102 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 3103 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 3104 | } |
| 3105 | } |
| 3106 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3107 | private boolean isMuteAdjust(int adjust) { |
| 3108 | return adjust == AudioManager.ADJUST_MUTE || adjust == AudioManager.ADJUST_UNMUTE |
| 3109 | || adjust == AudioManager.ADJUST_TOGGLE_MUTE; |
| 3110 | } |
| 3111 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3112 | private boolean isInCommunication() { |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3113 | boolean IsInCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3114 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3115 | TelecomManager telecomManager = |
| 3116 | (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3117 | |
| 3118 | final long ident = Binder.clearCallingIdentity(); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 3119 | IsInCall = telecomManager.isInCall(); |
Eric Laurent | 38edfda | 2014-12-18 17:38:04 -0800 | [diff] [blame] | 3120 | Binder.restoreCallingIdentity(ident); |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 3121 | |
Nancy Chen | 0eb1e40 | 2014-08-21 22:52:29 -0700 | [diff] [blame] | 3122 | return (IsInCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3123 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3124 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3125 | /** |
| 3126 | * For code clarity for getActiveStreamType(int) |
| 3127 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 3128 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 3129 | * in the last "delay_ms" ms. |
| 3130 | */ |
| 3131 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 3132 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 3133 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 3134 | } |
| 3135 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3136 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3137 | switch (mPlatformType) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3138 | case AudioSystem.PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3139 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3140 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3141 | == AudioSystem.FORCE_BT_SCO) { |
| 3142 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 3143 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3144 | } else { |
| 3145 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 3146 | return AudioSystem.STREAM_VOICE_CALL; |
| 3147 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3148 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3149 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3150 | if (DEBUG_VOL) |
| 3151 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3152 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3153 | } else { |
| 3154 | if (DEBUG_VOL) |
| 3155 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 3156 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3157 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3158 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3159 | if (DEBUG_VOL) |
| 3160 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 3161 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3162 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3163 | break; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3164 | case AudioSystem.PLATFORM_TELEVISION: |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3165 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
RoboErik | 2811dd3 | 2014-08-12 09:48:13 -0700 | [diff] [blame] | 3166 | // TV always defaults to STREAM_MUSIC |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3167 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3168 | } |
| 3169 | break; |
| 3170 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3171 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3172 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3173 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3174 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3175 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3176 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3177 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3178 | return AudioSystem.STREAM_VOICE_CALL; |
| 3179 | } |
| 3180 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3181 | StreamOverride.sDelayMs) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3182 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3183 | StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3184 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3185 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3186 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 3187 | if (isAfMusicActiveRecently(StreamOverride.sDelayMs)) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3188 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3189 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3190 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3191 | if (DEBUG_VOL) Log.v(TAG, |
| 3192 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3193 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3194 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3195 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3196 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3197 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3198 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3199 | + suggestedStreamType); |
| 3200 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3203 | private void broadcastRingerMode(String action, int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3204 | // Send sticky broadcast |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 3205 | Intent broadcast = new Intent(action); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3206 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3207 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3208 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3209 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | private void broadcastVibrateSetting(int vibrateType) { |
| 3213 | // Send broadcast |
| 3214 | if (ActivityManagerNative.isSystemReady()) { |
| 3215 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3216 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3217 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3218 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3223 | /** |
| 3224 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3225 | * Note that the wake lock needs to be released after the message has been handled. |
| 3226 | */ |
| 3227 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3228 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3229 | final long ident = Binder.clearCallingIdentity(); |
| 3230 | // Always acquire the wake lock as AudioService because it is released by the |
| 3231 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3232 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3233 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3234 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3236 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3237 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3238 | 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] | 3239 | |
| 3240 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3241 | handler.removeMessages(msg); |
| 3242 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3243 | return; |
| 3244 | } |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 3245 | synchronized (mLastDeviceConnectMsgTime) { |
| 3246 | long time = SystemClock.uptimeMillis() + delay; |
| 3247 | handler.sendMessageAtTime(handler.obtainMessage(msg, arg1, arg2, obj), time); |
| 3248 | if (msg == MSG_SET_WIRED_DEVICE_CONNECTION_STATE || |
| 3249 | msg == MSG_SET_A2DP_SRC_CONNECTION_STATE || |
| 3250 | msg == MSG_SET_A2DP_SINK_CONNECTION_STATE) { |
| 3251 | mLastDeviceConnectMsgTime = time; |
| 3252 | } |
| 3253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | boolean checkAudioSettingsPermission(String method) { |
Jean-Michel Trivi | ccd654e | 2014-09-03 17:48:47 -0700 | [diff] [blame] | 3257 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3258 | == PackageManager.PERMISSION_GRANTED) { |
| 3259 | return true; |
| 3260 | } |
| 3261 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3262 | + Binder.getCallingPid() |
| 3263 | + ", uid=" + Binder.getCallingUid(); |
| 3264 | Log.w(TAG, msg); |
| 3265 | return false; |
| 3266 | } |
| 3267 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3268 | private int getDeviceForStream(int stream) { |
| 3269 | int device = AudioSystem.getDevicesForStream(stream); |
| 3270 | if ((device & (device - 1)) != 0) { |
| 3271 | // Multiple device selection is either: |
| 3272 | // - speaker + one other device: give priority to speaker in this case. |
| 3273 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3274 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3275 | // selection if not the speaker. |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3276 | // - 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] | 3277 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3278 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
Jungshik Jang | c9ff968 | 2014-09-15 17:41:06 +0900 | [diff] [blame] | 3279 | } else if ((device & AudioSystem.DEVICE_OUT_HDMI_ARC) != 0) { |
| 3280 | device = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 3281 | } else if ((device & AudioSystem.DEVICE_OUT_SPDIF) != 0) { |
| 3282 | device = AudioSystem.DEVICE_OUT_SPDIF; |
| 3283 | } else if ((device & AudioSystem.DEVICE_OUT_AUX_LINE) != 0) { |
| 3284 | device = AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3285 | } else { |
| 3286 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3287 | } |
| 3288 | } |
| 3289 | return device; |
| 3290 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3291 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3292 | /* |
| 3293 | * A class just for packaging up a set of connection parameters. |
| 3294 | */ |
| 3295 | private class WiredDeviceConnectionState { |
| 3296 | public int mType; |
| 3297 | public int mState; |
| 3298 | public String mAddress; |
| 3299 | public String mName; |
| 3300 | |
| 3301 | public WiredDeviceConnectionState(int type, int state, String address, String name) { |
| 3302 | mType = type; |
| 3303 | mState = state; |
| 3304 | mAddress = address; |
| 3305 | mName = name; |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | public void setWiredDeviceConnectionState(int type, int state, String address, |
| 3310 | String name) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3311 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 3312 | if (DEBUG_DEVICES) { |
| 3313 | Slog.i(TAG, "setWiredDeviceConnectionState(" + state + " nm: " + name + " addr:" |
| 3314 | + address + ")"); |
| 3315 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3316 | int delay = checkSendBecomingNoisyIntent(type, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3317 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3318 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 3319 | 0, |
| 3320 | 0, |
| 3321 | new WiredDeviceConnectionState(type, state, address, name), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3322 | delay); |
| 3323 | } |
| 3324 | } |
| 3325 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3326 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3327 | { |
| 3328 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3329 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3330 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3331 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3332 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3333 | if (profile == BluetoothProfile.A2DP) { |
| 3334 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3335 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3336 | } else { |
| 3337 | delay = 0; |
| 3338 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3339 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3340 | (profile == BluetoothProfile.A2DP ? |
| 3341 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3342 | state, |
| 3343 | 0, |
| 3344 | device, |
| 3345 | delay); |
| 3346 | } |
| 3347 | return delay; |
| 3348 | } |
| 3349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3350 | /////////////////////////////////////////////////////////////////////////// |
| 3351 | // Inner classes |
| 3352 | /////////////////////////////////////////////////////////////////////////// |
| 3353 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3354 | // NOTE: Locking order for synchronized objects related to volume or ringer mode management: |
| 3355 | // 1 mScoclient OR mSafeMediaVolumeState |
| 3356 | // 2 mSetModeDeathHandlers |
| 3357 | // 3 mSettingsLock |
| 3358 | // 4 VolumeStreamState.class |
| 3359 | // 5 mCameraSoundForced |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3360 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3361 | private final int mStreamType; |
| 3362 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3363 | private boolean mIsMuted; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3364 | private String mVolumeIndexSettingName; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3365 | private int mIndexMax; |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3366 | private final SparseIntArray mIndexMap = new SparseIntArray(8); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3367 | private final Intent mVolumeChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3368 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3369 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3370 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3371 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3372 | |
| 3373 | mStreamType = streamType; |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 3374 | mIndexMax = MAX_STREAM_VOLUME[streamType]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3375 | AudioSystem.initStreamVolume(streamType, 0, mIndexMax); |
| 3376 | mIndexMax *= 10; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3377 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3378 | readSettings(); |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3379 | mVolumeChanged = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 3380 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | } |
| 3382 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3383 | public String getSettingNameForDevice(int device) { |
| 3384 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3385 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3386 | if (suffix.isEmpty()) { |
| 3387 | return name; |
| 3388 | } |
| 3389 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3392 | public void readSettings() { |
| 3393 | synchronized (VolumeStreamState.class) { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3394 | // force maximum volume on all streams if fixed volume property is set |
| 3395 | if (mUseFixedVolume) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3396 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | // do not read system stream volume from settings: this stream is always aliased |
| 3400 | // to another stream type and its volume is never persisted. Values in settings can |
| 3401 | // only be stale values |
| 3402 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3403 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3404 | int index = 10 * AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType]; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3405 | synchronized (mCameraSoundForced) { |
| 3406 | if (mCameraSoundForced) { |
| 3407 | index = mIndexMax; |
| 3408 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3409 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3410 | mIndexMap.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3411 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3412 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3413 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3414 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3415 | |
| 3416 | for (int i = 0; remainingDevices != 0; i++) { |
| 3417 | int device = (1 << i); |
| 3418 | if ((device & remainingDevices) == 0) { |
| 3419 | continue; |
| 3420 | } |
| 3421 | remainingDevices &= ~device; |
| 3422 | |
| 3423 | // retrieve current volume for device |
| 3424 | String name = getSettingNameForDevice(device); |
| 3425 | // if no volume stored for current stream and device, use default volume if default |
| 3426 | // device, continue otherwise |
| 3427 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 3428 | AudioSystem.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3429 | int index = Settings.System.getIntForUser( |
| 3430 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3431 | if (index == -1) { |
| 3432 | continue; |
| 3433 | } |
| 3434 | |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3435 | mIndexMap.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3436 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3437 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3438 | } |
| 3439 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3440 | // must be called while synchronized VolumeStreamState.class |
| 3441 | public void applyDeviceVolume_syncVSS(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3442 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3443 | if (mIsMuted) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3444 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3445 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) |
| 3446 | || ((device & mFullVolumeDevices) != 0)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3447 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3448 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3449 | index = (getIndex(device) + 5)/10; |
| 3450 | } |
| 3451 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3452 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3454 | public void applyAllVolumes() { |
| 3455 | synchronized (VolumeStreamState.class) { |
| 3456 | // apply default volume first: by convention this will reset all |
| 3457 | // devices volumes in audio policy manager to the supplied value |
| 3458 | int index; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3459 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3460 | index = 0; |
| 3461 | } else { |
| 3462 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3463 | } |
| 3464 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3465 | // then apply device specific volumes |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3466 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3467 | int device = mIndexMap.keyAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3468 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3469 | if (mIsMuted) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3470 | index = 0; |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3471 | } else if (((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3472 | mAvrcpAbsVolSupported) |
| 3473 | || ((device & mFullVolumeDevices) != 0)) |
| 3474 | { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3475 | index = (mIndexMax + 5)/10; |
| 3476 | } else { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3477 | index = (mIndexMap.valueAt(i) + 5)/10; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3478 | } |
| 3479 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3480 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | public boolean adjustIndex(int deltaIndex, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3486 | return setIndex(getIndex(device) + deltaIndex, |
| 3487 | device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3490 | public boolean setIndex(int index, int device) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3491 | boolean changed = false; |
| 3492 | int oldIndex; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3493 | synchronized (VolumeStreamState.class) { |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3494 | oldIndex = getIndex(device); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3495 | index = getValidIndex(index); |
| 3496 | synchronized (mCameraSoundForced) { |
| 3497 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3498 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3499 | } |
| 3500 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3501 | mIndexMap.put(device, index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3502 | |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3503 | changed = oldIndex != index; |
| 3504 | if (changed) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3505 | // Apply change to all streams using this one as alias |
| 3506 | // if changing volume of current device, also change volume of current |
| 3507 | // device on aliased stream |
| 3508 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3509 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3510 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3511 | if (streamType != mStreamType && |
| 3512 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3513 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 3514 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3515 | device); |
| 3516 | if (currentDevice) { |
| 3517 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3518 | getDeviceForStream(streamType)); |
| 3519 | } |
| 3520 | } |
| 3521 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3523 | } |
John Spurlock | f63860c | 2015-02-19 09:46:27 -0500 | [diff] [blame] | 3524 | if (changed) { |
| 3525 | oldIndex = (oldIndex + 5) / 10; |
| 3526 | index = (index + 5) / 10; |
| 3527 | mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 3528 | mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
| 3529 | sendBroadcastToAll(mVolumeChanged); |
| 3530 | } |
| 3531 | return changed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3532 | } |
| 3533 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3534 | public int getIndex(int device) { |
| 3535 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3536 | int index = mIndexMap.get(device, -1); |
| 3537 | if (index == -1) { |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3538 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3539 | index = mIndexMap.get(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3540 | } |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3541 | return index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3542 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3545 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3546 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3547 | } |
| 3548 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3549 | public void setAllIndexes(VolumeStreamState srcStream) { |
| 3550 | synchronized (VolumeStreamState.class) { |
| 3551 | int srcStreamType = srcStream.getStreamType(); |
| 3552 | // apply default device volume from source stream to all devices first in case |
| 3553 | // some devices are present in this stream state but not in source stream state |
| 3554 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3555 | index = rescaleIndex(index, srcStreamType, mStreamType); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3556 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3557 | mIndexMap.put(mIndexMap.keyAt(i), index); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3558 | } |
| 3559 | // Now apply actual volume for devices in source stream state |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3560 | SparseIntArray srcMap = srcStream.mIndexMap; |
| 3561 | for (int i = 0; i < srcMap.size(); i++) { |
| 3562 | int device = srcMap.keyAt(i); |
| 3563 | index = srcMap.valueAt(i); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3564 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3565 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3566 | setIndex(index, device); |
| 3567 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3571 | public void setAllIndexesToMax() { |
| 3572 | synchronized (VolumeStreamState.class) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3573 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3574 | mIndexMap.put(mIndexMap.keyAt(i), mIndexMax); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3575 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3576 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3579 | public void mute(boolean state) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3580 | boolean changed = false; |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3581 | synchronized (VolumeStreamState.class) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3582 | if (state != mIsMuted) { |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3583 | changed = true; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3584 | mIsMuted = state; |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3585 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3586 | // Set the new mute volume. This propagates the values to |
| 3587 | // the audio system, otherwise the volume won't be changed |
| 3588 | // at the lower level. |
| 3589 | sendMsg(mAudioHandler, |
| 3590 | MSG_SET_ALL_VOLUMES, |
| 3591 | SENDMSG_QUEUE, |
| 3592 | 0, |
| 3593 | 0, |
| 3594 | this, 0); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3596 | } |
John Spurlock | 22b9ee1 | 2015-02-18 22:51:44 -0500 | [diff] [blame] | 3597 | if (changed) { |
| 3598 | // Stream mute changed, fire the intent. |
| 3599 | Intent intent = new Intent(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 3600 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType); |
| 3601 | intent.putExtra(AudioManager.EXTRA_STREAM_VOLUME_MUTED, state); |
| 3602 | sendBroadcastToAll(intent); |
| 3603 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3604 | } |
| 3605 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3606 | public int getStreamType() { |
| 3607 | return mStreamType; |
| 3608 | } |
| 3609 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3610 | public void checkFixedVolumeDevices() { |
| 3611 | synchronized (VolumeStreamState.class) { |
| 3612 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 3613 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3614 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3615 | int device = mIndexMap.keyAt(i); |
| 3616 | int index = mIndexMap.valueAt(i); |
Jean-Michel Trivi | ba5270b | 2014-10-01 17:49:29 -0700 | [diff] [blame] | 3617 | if (((device & mFullVolumeDevices) != 0) |
| 3618 | || (((device & mFixedVolumeDevices) != 0) && index != 0)) { |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3619 | mIndexMap.put(device, mIndexMax); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3620 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3621 | applyDeviceVolume_syncVSS(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3622 | } |
| 3623 | } |
| 3624 | } |
| 3625 | } |
| 3626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3627 | private int getValidIndex(int index) { |
| 3628 | if (index < 0) { |
| 3629 | return 0; |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 3630 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3631 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | return index; |
| 3635 | } |
| 3636 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3637 | private void dump(PrintWriter pw) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 3638 | pw.print(" Muted: "); |
| 3639 | pw.println(mIsMuted); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3640 | pw.print(" Max: "); |
| 3641 | pw.println((mIndexMax + 5) / 10); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3642 | pw.print(" Current: "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3643 | for (int i = 0; i < mIndexMap.size(); i++) { |
| 3644 | if (i > 0) { |
| 3645 | pw.print(", "); |
| 3646 | } |
| 3647 | final int device = mIndexMap.keyAt(i); |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3648 | pw.print(Integer.toHexString(device)); |
| 3649 | final String deviceName = device == AudioSystem.DEVICE_OUT_DEFAULT ? "default" |
| 3650 | : AudioSystem.getOutputDeviceName(device); |
| 3651 | if (!deviceName.isEmpty()) { |
| 3652 | pw.print(" ("); |
| 3653 | pw.print(deviceName); |
| 3654 | pw.print(")"); |
| 3655 | } |
| 3656 | pw.print(": "); |
John Spurlock | 2bb02ec | 2015-03-02 13:13:06 -0500 | [diff] [blame] | 3657 | final int index = (mIndexMap.valueAt(i) + 5) / 10; |
John Spurlock | 2b29bc4 | 2014-08-26 16:40:35 -0400 | [diff] [blame] | 3658 | pw.print(index); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3659 | } |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 3660 | pw.println(); |
| 3661 | pw.print(" Devices: "); |
| 3662 | final int devices = AudioSystem.getDevicesForStream(mStreamType); |
| 3663 | int device, i = 0, n = 0; |
John Spurlock | 1ff1e6e | 2015-03-09 14:21:20 -0400 | [diff] [blame] | 3664 | // iterate all devices from 1 to DEVICE_OUT_DEFAULT exclusive |
| 3665 | // (the default device is not returned by getDevicesForStream) |
| 3666 | while ((device = 1 << i) != AudioSystem.DEVICE_OUT_DEFAULT) { |
John Spurlock | b32fc97 | 2015-03-05 13:58:00 -0500 | [diff] [blame] | 3667 | if ((devices & device) != 0) { |
| 3668 | if (n++ > 0) { |
| 3669 | pw.print(", "); |
| 3670 | } |
| 3671 | pw.print(AudioSystem.getOutputDeviceName(device)); |
| 3672 | } |
| 3673 | i++; |
| 3674 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | /** Thread that handles native AudioSystem control. */ |
| 3679 | private class AudioSystemThread extends Thread { |
| 3680 | AudioSystemThread() { |
| 3681 | super("AudioService"); |
| 3682 | } |
| 3683 | |
| 3684 | @Override |
| 3685 | public void run() { |
| 3686 | // Set this thread up so the handler will work on it |
| 3687 | Looper.prepare(); |
| 3688 | |
| 3689 | synchronized(AudioService.this) { |
| 3690 | mAudioHandler = new AudioHandler(); |
| 3691 | |
| 3692 | // Notify that the handler has been created |
| 3693 | AudioService.this.notify(); |
| 3694 | } |
| 3695 | |
| 3696 | // Listen for volume change requests that are set by VolumePanel |
| 3697 | Looper.loop(); |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | /** Handles internal volume messages in separate volume thread. */ |
| 3702 | private class AudioHandler extends Handler { |
| 3703 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3704 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3705 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3706 | synchronized (VolumeStreamState.class) { |
| 3707 | // Apply volume |
| 3708 | streamState.applyDeviceVolume_syncVSS(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3709 | |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 3710 | // Apply change to all streams using this one as alias |
| 3711 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3712 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3713 | if (streamType != streamState.mStreamType && |
| 3714 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
| 3715 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 3716 | // that may have a different device selected |
| 3717 | int streamDevice = getDeviceForStream(streamType); |
| 3718 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 3719 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 3720 | mStreamStates[streamType].applyDeviceVolume_syncVSS(device); |
| 3721 | } |
| 3722 | mStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice); |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3723 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3724 | } |
| 3725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3726 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3727 | sendMsg(mAudioHandler, |
| 3728 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 3729 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3730 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3731 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3732 | streamState, |
| 3733 | PERSIST_DELAY); |
| 3734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3735 | } |
| 3736 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3737 | private void setAllVolumes(VolumeStreamState streamState) { |
| 3738 | |
| 3739 | // Apply volume |
| 3740 | streamState.applyAllVolumes(); |
| 3741 | |
| 3742 | // Apply change to all streams using this one as alias |
| 3743 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3744 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3745 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3746 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3747 | mStreamStates[streamType].applyAllVolumes(); |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3752 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3753 | if (mUseFixedVolume) { |
| 3754 | return; |
| 3755 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3756 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 3757 | return; |
| 3758 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3759 | System.putIntForUser(mContentResolver, |
| 3760 | streamState.getSettingNameForDevice(device), |
| 3761 | (streamState.getIndex(device) + 5)/ 10, |
| 3762 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | } |
| 3764 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3765 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3766 | if (mUseFixedVolume) { |
| 3767 | return; |
| 3768 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 3769 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3770 | } |
| 3771 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3772 | private boolean onLoadSoundEffects() { |
| 3773 | int status; |
| 3774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3775 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3776 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3777 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 3778 | return false; |
| 3779 | } |
| 3780 | |
| 3781 | if (mSoundPool != null) { |
| 3782 | return true; |
| 3783 | } |
| 3784 | |
| 3785 | loadTouchSoundAssets(); |
| 3786 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 3787 | mSoundPool = new SoundPool.Builder() |
| 3788 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 3789 | .setAudioAttributes(new AudioAttributes.Builder() |
| 3790 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 3791 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 3792 | .build()) |
| 3793 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3794 | mSoundPoolCallBack = null; |
| 3795 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 3796 | mSoundPoolListenerThread.start(); |
| 3797 | int attempts = 3; |
| 3798 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 3799 | try { |
| 3800 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3801 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3802 | } catch (InterruptedException e) { |
| 3803 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | if (mSoundPoolCallBack == null) { |
| 3808 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 3809 | if (mSoundPoolLooper != null) { |
| 3810 | mSoundPoolLooper.quit(); |
| 3811 | mSoundPoolLooper = null; |
| 3812 | } |
| 3813 | mSoundPoolListenerThread = null; |
| 3814 | mSoundPool.release(); |
| 3815 | mSoundPool = null; |
| 3816 | return false; |
| 3817 | } |
| 3818 | /* |
| 3819 | * poolId table: The value -1 in this table indicates that corresponding |
| 3820 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 3821 | * Once loaded, the value in poolId is the sample ID and the same |
| 3822 | * sample can be reused for another effect using the same file. |
| 3823 | */ |
| 3824 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3825 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3826 | poolId[fileIdx] = -1; |
| 3827 | } |
| 3828 | /* |
| 3829 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 3830 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 3831 | * this indicates we have a valid sample loaded for this effect. |
| 3832 | */ |
| 3833 | |
| 3834 | int numSamples = 0; |
| 3835 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3836 | // Do not load sample if this effect uses the MediaPlayer |
| 3837 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 3838 | continue; |
| 3839 | } |
| 3840 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 3841 | String filePath = Environment.getRootDirectory() |
| 3842 | + SOUND_EFFECTS_PATH |
| 3843 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 3844 | int sampleId = mSoundPool.load(filePath, 0); |
| 3845 | if (sampleId <= 0) { |
| 3846 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 3847 | } else { |
| 3848 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 3849 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 3850 | numSamples++; |
| 3851 | } |
| 3852 | } else { |
| 3853 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 3854 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 3855 | } |
| 3856 | } |
| 3857 | // wait for all samples to be loaded |
| 3858 | if (numSamples > 0) { |
| 3859 | mSoundPoolCallBack.setSamples(poolId); |
| 3860 | |
| 3861 | attempts = 3; |
| 3862 | status = 1; |
| 3863 | while ((status == 1) && (attempts-- > 0)) { |
| 3864 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3865 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3866 | status = mSoundPoolCallBack.status(); |
| 3867 | } catch (InterruptedException e) { |
| 3868 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 3869 | } |
| 3870 | } |
| 3871 | } else { |
| 3872 | status = -1; |
| 3873 | } |
| 3874 | |
| 3875 | if (mSoundPoolLooper != null) { |
| 3876 | mSoundPoolLooper.quit(); |
| 3877 | mSoundPoolLooper = null; |
| 3878 | } |
| 3879 | mSoundPoolListenerThread = null; |
| 3880 | if (status != 0) { |
| 3881 | Log.w(TAG, |
| 3882 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 3883 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3884 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 3885 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | mSoundPool.release(); |
| 3890 | mSoundPool = null; |
| 3891 | } |
| 3892 | } |
| 3893 | return (status == 0); |
| 3894 | } |
| 3895 | |
| 3896 | /** |
| 3897 | * Unloads samples from the sound pool. |
| 3898 | * This method can be called to free some memory when |
| 3899 | * sound effects are disabled. |
| 3900 | */ |
| 3901 | private void onUnloadSoundEffects() { |
| 3902 | synchronized (mSoundEffectsLock) { |
| 3903 | if (mSoundPool == null) { |
| 3904 | return; |
| 3905 | } |
| 3906 | |
| 3907 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3908 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3909 | poolId[fileIdx] = 0; |
| 3910 | } |
| 3911 | |
| 3912 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3913 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 3914 | continue; |
| 3915 | } |
| 3916 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 3917 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 3918 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3919 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 3920 | } |
| 3921 | } |
| 3922 | mSoundPool.release(); |
| 3923 | mSoundPool = null; |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | private void onPlaySoundEffect(int effectType, int volume) { |
| 3928 | synchronized (mSoundEffectsLock) { |
| 3929 | |
| 3930 | onLoadSoundEffects(); |
| 3931 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | if (mSoundPool == null) { |
| 3933 | return; |
| 3934 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3935 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3936 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3937 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 3938 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3939 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 3940 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3942 | |
| 3943 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3944 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 3945 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3946 | } else { |
| 3947 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3948 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 3949 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 3950 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3951 | mediaPlayer.setDataSource(filePath); |
| 3952 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 3953 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 3954 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3955 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 3956 | public void onCompletion(MediaPlayer mp) { |
| 3957 | cleanupPlayer(mp); |
| 3958 | } |
| 3959 | }); |
| 3960 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 3961 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 3962 | cleanupPlayer(mp); |
| 3963 | return true; |
| 3964 | } |
| 3965 | }); |
| 3966 | mediaPlayer.start(); |
| 3967 | } catch (IOException ex) { |
| 3968 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 3969 | } catch (IllegalArgumentException ex) { |
| 3970 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 3971 | } catch (IllegalStateException ex) { |
| 3972 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | private void cleanupPlayer(MediaPlayer mp) { |
| 3979 | if (mp != null) { |
| 3980 | try { |
| 3981 | mp.stop(); |
| 3982 | mp.release(); |
| 3983 | } catch (IllegalStateException ex) { |
| 3984 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3989 | private void setForceUse(int usage, int config) { |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 3990 | synchronized (mConnectedDevices) { |
| 3991 | setForceUseInt_SyncDevices(usage, config); |
| 3992 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3993 | } |
| 3994 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3995 | private void onPersistSafeVolumeState(int state) { |
| 3996 | Settings.Global.putInt(mContentResolver, |
| 3997 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 3998 | state); |
| 3999 | } |
| 4000 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4001 | @Override |
| 4002 | public void handleMessage(Message msg) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4003 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4005 | case MSG_SET_DEVICE_VOLUME: |
| 4006 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 4007 | break; |
| 4008 | |
| 4009 | case MSG_SET_ALL_VOLUMES: |
| 4010 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4011 | break; |
| 4012 | |
| 4013 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4014 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4015 | break; |
| 4016 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4017 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 4018 | if (mUseFixedVolume) { |
| 4019 | return; |
| 4020 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4021 | Settings.System.putIntForUser(mContentResolver, |
| 4022 | Settings.System.VOLUME_MASTER_MUTE, |
| 4023 | msg.arg1, |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4024 | msg.arg2); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 4025 | break; |
| 4026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4027 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4028 | // note that the value persisted is the current ringer mode, not the |
| 4029 | // 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] | 4030 | persistRingerMode(getRingerModeInternal()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4031 | break; |
| 4032 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | case MSG_MEDIA_SERVER_DIED: |
Eric Laurent | eb4b8a2 | 2014-07-21 13:10:07 -0700 | [diff] [blame] | 4034 | if (!mSystemReady || |
| 4035 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4036 | Log.e(TAG, "Media server died."); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 4037 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4038 | null, 500); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4039 | break; |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 4040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | Log.e(TAG, "Media server started."); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4042 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4043 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 4044 | // server crash |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 4045 | // Note that we only execute this when the media server |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4046 | // process restarts after a crash, not the first time it is started. |
| 4047 | AudioSystem.setParameters("restarting=true"); |
| 4048 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 4049 | readAndSetLowRamDevice(); |
| 4050 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4051 | // Restore device connection states |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4052 | synchronized (mConnectedDevices) { |
| 4053 | Set set = mConnectedDevices.entrySet(); |
| 4054 | Iterator i = set.iterator(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4055 | while (i.hasNext()) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4056 | Map.Entry device = (Map.Entry)i.next(); |
| 4057 | AudioSystem.setDeviceConnectionState( |
| 4058 | ((Integer)device.getKey()).intValue(), |
| 4059 | AudioSystem.DEVICE_STATE_AVAILABLE, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4060 | (String)device.getValue(), |
| 4061 | "unknown-device"); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4062 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4063 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4064 | // Restore call state |
| 4065 | AudioSystem.setPhoneState(mMode); |
| 4066 | |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4067 | // Restore forced usage for communcations and record |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4068 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4069 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4070 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 4071 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4072 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4073 | // Restore stream volumes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4074 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 4075 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4076 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4077 | AudioSystem.initStreamVolume(streamType, 0, (streamState.mIndexMax + 5) / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 4078 | |
| 4079 | streamState.applyAllVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4080 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 4081 | |
| 4082 | // Restore ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4083 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4084 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4085 | // Reset device orientation (if monitored for this device) |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4086 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4087 | setOrientationForAudioSystem(); |
| 4088 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4089 | if (mMonitorRotation) { |
| 4090 | setRotationForAudioSystem(); |
| 4091 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4092 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4093 | synchronized (mBluetoothA2dpEnabledLock) { |
| 4094 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 4095 | mBluetoothA2dpEnabled ? |
| 4096 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 4097 | } |
Eric Laurent | bff5ca5 | 2012-11-02 16:48:26 -0700 | [diff] [blame] | 4098 | |
| 4099 | synchronized (mSettingsLock) { |
| 4100 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 4101 | mDockAudioMediaEnabled ? |
| 4102 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 4103 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4104 | if (mHdmiManager != null) { |
| 4105 | synchronized (mHdmiManager) { |
| 4106 | if (mHdmiTvClient != null) { |
| 4107 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 4108 | } |
| 4109 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 4110 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 4111 | |
| 4112 | synchronized (mAudioPolicies) { |
| 4113 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 4114 | policy.connectMixes(); |
| 4115 | } |
| 4116 | } |
| 4117 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 4118 | // indicate the end of reconfiguration phase to audio HAL |
| 4119 | AudioSystem.setParameters("restarting=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4120 | break; |
| 4121 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4122 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 4123 | onUnloadSoundEffects(); |
| 4124 | break; |
| 4125 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4126 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4127 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4128 | // can take several dozens of milliseconds to complete |
| 4129 | boolean loaded = onLoadSoundEffects(); |
| 4130 | if (msg.obj != null) { |
| 4131 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4132 | synchronized (reply) { |
| 4133 | reply.mStatus = loaded ? 0 : -1; |
| 4134 | reply.notify(); |
| 4135 | } |
| 4136 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4137 | break; |
| 4138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4139 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4140 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4142 | |
| 4143 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4144 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4145 | synchronized (mConnectedDevices) { |
| 4146 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4147 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4148 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4149 | |
| 4150 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4151 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4152 | setForceUse(msg.arg1, msg.arg2); |
| 4153 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4154 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4155 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4156 | resetBluetoothSco(); |
| 4157 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4158 | |
| 4159 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4160 | { WiredDeviceConnectionState connectState = |
| 4161 | (WiredDeviceConnectionState)msg.obj; |
| 4162 | onSetWiredDeviceConnectionState(connectState.mType, connectState.mState, |
| 4163 | connectState.mAddress, connectState.mName); |
| 4164 | mAudioEventWakeLock.release(); |
| 4165 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4166 | break; |
| 4167 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4168 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4169 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4170 | mAudioEventWakeLock.release(); |
| 4171 | break; |
| 4172 | |
| 4173 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4174 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4175 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4176 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4177 | |
| 4178 | case MSG_REPORT_NEW_ROUTES: { |
| 4179 | int N = mRoutesObservers.beginBroadcast(); |
| 4180 | if (N > 0) { |
| 4181 | AudioRoutesInfo routes; |
| 4182 | synchronized (mCurAudioRoutes) { |
| 4183 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4184 | } |
| 4185 | while (N > 0) { |
| 4186 | N--; |
| 4187 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4188 | try { |
| 4189 | obs.dispatchAudioRoutesChanged(routes); |
| 4190 | } catch (RemoteException e) { |
| 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | mRoutesObservers.finishBroadcast(); |
| 4195 | break; |
| 4196 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4197 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4198 | case MSG_CHECK_MUSIC_ACTIVE: |
| 4199 | onCheckMusicActive(); |
| 4200 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4201 | |
| 4202 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4203 | onSendBecomingNoisyIntent(); |
| 4204 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4205 | |
| 4206 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4207 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
| 4208 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED)); |
| 4209 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4210 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4211 | onPersistSafeVolumeState(msg.arg1); |
| 4212 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4213 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4214 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4215 | onBroadcastScoConnectionState(msg.arg1); |
| 4216 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4217 | |
| 4218 | case MSG_SYSTEM_READY: |
| 4219 | onSystemReady(); |
| 4220 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 4221 | |
| 4222 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4223 | final int musicActiveMs = msg.arg1; |
| 4224 | Settings.Secure.putIntForUser(mContentResolver, |
| 4225 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4226 | UserHandle.USER_CURRENT); |
| 4227 | break; |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4228 | case MSG_PERSIST_MICROPHONE_MUTE: |
| 4229 | Settings.System.putIntForUser(mContentResolver, |
| 4230 | Settings.System.MICROPHONE_MUTE, |
| 4231 | msg.arg1, |
| 4232 | msg.arg2); |
| 4233 | break; |
RoboErik | 5452e25 | 2015-02-06 15:33:53 -0800 | [diff] [blame] | 4234 | case MSG_UNMUTE_STREAM: |
| 4235 | onUnmuteStream(msg.arg1, msg.arg2); |
| 4236 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | } |
| 4240 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4241 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4242 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4243 | SettingsObserver() { |
| 4244 | super(new Handler()); |
| 4245 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4246 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4247 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4248 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | @Override |
| 4252 | public void onChange(boolean selfChange) { |
| 4253 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4254 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4255 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4256 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4257 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4258 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4259 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4260 | /* |
| 4261 | * Ensure all stream types that should be affected by ringer mode |
| 4262 | * are in the proper state. |
| 4263 | */ |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 4264 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4265 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4266 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4267 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4268 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4269 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4270 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4271 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4272 | private void makeA2dpDeviceAvailable(String address) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4273 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4274 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4275 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4276 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4277 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4278 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4279 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4280 | AudioSystem.DEVICE_STATE_AVAILABLE, address, DEVICE_NAME_A2DP); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4281 | // Reset A2DP suspend state each time a new sink is connected |
| 4282 | AudioSystem.setParameters("A2dpSuspended=false"); |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4283 | mConnectedDevices.put( |
| 4284 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address), |
| 4285 | new DeviceListSpec(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, DEVICE_NAME_A2DP, |
| 4286 | address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4289 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4290 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4293 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4294 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4295 | synchronized (mA2dpAvrcpLock) { |
| 4296 | mAvrcpAbsVolSupported = false; |
| 4297 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4298 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4299 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, DEVICE_NAME_A2DP); |
| 4300 | mConnectedDevices.remove( |
| 4301 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4302 | synchronized (mCurAudioRoutes) { |
| 4303 | // Remove A2DP routes as well |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4304 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4305 | mCurAudioRoutes.bluetoothName = null; |
RoboErik | 5535ea8 | 2014-09-25 14:53:16 -0700 | [diff] [blame] | 4306 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4307 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4308 | } |
| 4309 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4312 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4313 | private void makeA2dpDeviceUnavailableLater(String address) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4314 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4315 | // reconnection of the sink. |
| 4316 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4317 | // 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] | 4318 | mConnectedDevices.remove( |
| 4319 | makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address)); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4320 | // send the delayed message to make the device unavailable later |
| 4321 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
| 4322 | mAudioHandler.sendMessageDelayed(msg, BTA2DP_DOCK_TIMEOUT_MILLIS); |
| 4323 | |
| 4324 | } |
| 4325 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4326 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4327 | private void makeA2dpSrcAvailable(String address) { |
| 4328 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4329 | AudioSystem.DEVICE_STATE_AVAILABLE, address, DEVICE_NAME_A2DP); |
| 4330 | mConnectedDevices.put( |
| 4331 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address), |
| 4332 | new DeviceListSpec(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, DEVICE_NAME_A2DP, |
| 4333 | address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | // must be called synchronized on mConnectedDevices |
| 4337 | private void makeA2dpSrcUnavailable(String address) { |
| 4338 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4339 | AudioSystem.DEVICE_STATE_UNAVAILABLE, address, DEVICE_NAME_A2DP); |
| 4340 | mConnectedDevices.remove( |
| 4341 | makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address)); |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4345 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4346 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4347 | } |
| 4348 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4349 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4350 | private boolean hasScheduledA2dpDockTimeout() { |
| 4351 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4352 | } |
| 4353 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4354 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4355 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4356 | if (DEBUG_VOL) { |
| 4357 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4358 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4359 | if (btDevice == null) { |
| 4360 | return; |
| 4361 | } |
| 4362 | String address = btDevice.getAddress(); |
| 4363 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4364 | address = ""; |
| 4365 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4366 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4367 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4368 | String key = makeDeviceListKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4369 | btDevice.getAddress()); |
| 4370 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4371 | boolean isConnected = deviceSpec != null; |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4372 | |
| 4373 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4374 | if (btDevice.isBluetoothDock()) { |
| 4375 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4376 | // introduction of a delay for transient disconnections of docks when |
| 4377 | // power is rapidly turned off/on, this message will be canceled if |
| 4378 | // we reconnect the dock under a preset delay |
| 4379 | makeA2dpDeviceUnavailableLater(address); |
| 4380 | // the next time isConnected is evaluated, it will be false for the dock |
| 4381 | } |
| 4382 | } else { |
| 4383 | makeA2dpDeviceUnavailableNow(address); |
| 4384 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4385 | synchronized (mCurAudioRoutes) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4386 | if (mCurAudioRoutes.bluetoothName != null) { |
| 4387 | mCurAudioRoutes.bluetoothName = null; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4388 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4389 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4390 | } |
| 4391 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4392 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4393 | if (btDevice.isBluetoothDock()) { |
| 4394 | // this could be a reconnection after a transient disconnection |
| 4395 | cancelA2dpDeviceTimeout(); |
| 4396 | mDockAddress = address; |
| 4397 | } else { |
| 4398 | // this could be a connection of another A2DP device before the timeout of |
| 4399 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4400 | if(hasScheduledA2dpDockTimeout()) { |
| 4401 | cancelA2dpDeviceTimeout(); |
| 4402 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4403 | } |
| 4404 | } |
| 4405 | makeA2dpDeviceAvailable(address); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4406 | synchronized (mCurAudioRoutes) { |
| 4407 | String name = btDevice.getAliasName(); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4408 | if (!TextUtils.equals(mCurAudioRoutes.bluetoothName, name)) { |
| 4409 | mCurAudioRoutes.bluetoothName = name; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4410 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4411 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4412 | } |
| 4413 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4418 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4419 | { |
| 4420 | if (DEBUG_VOL) { |
| 4421 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4422 | } |
| 4423 | if (btDevice == null) { |
| 4424 | return; |
| 4425 | } |
| 4426 | String address = btDevice.getAddress(); |
| 4427 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4428 | address = ""; |
| 4429 | } |
| 4430 | |
| 4431 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4432 | String key = makeDeviceListKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, address); |
| 4433 | DeviceListSpec deviceSpec = mConnectedDevices.get(key); |
| 4434 | boolean isConnected = deviceSpec != null; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4435 | |
| 4436 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4437 | makeA2dpSrcUnavailable(address); |
| 4438 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4439 | makeA2dpSrcAvailable(address); |
| 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4444 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4445 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4446 | synchronized (mA2dpAvrcpLock) { |
| 4447 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4448 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4449 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4450 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4451 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4452 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4453 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4457 | private boolean handleDeviceConnection(boolean connect, int device, String address, |
| 4458 | String deviceName) { |
| 4459 | if (DEBUG_DEVICES) { |
| 4460 | Slog.i(TAG, "handleDeviceConnection(" + connect + " dev:" + Integer.toHexString(device) |
| 4461 | + " address:" + address + " name:" + deviceName + ")"); |
| 4462 | } |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4463 | synchronized (mConnectedDevices) { |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4464 | String deviceKey = makeDeviceListKey(device, address); |
| 4465 | if (DEBUG_DEVICES) { |
| 4466 | Slog.i(TAG, "deviceKey:" + deviceKey); |
| 4467 | } |
| 4468 | DeviceListSpec deviceSpec = mConnectedDevices.get(deviceKey); |
| 4469 | boolean isConnected = deviceSpec != null; |
| 4470 | if (DEBUG_DEVICES) { |
| 4471 | Slog.i(TAG, "deviceSpec:" + deviceSpec + " is(already)Connected:" + isConnected); |
| 4472 | } |
| 4473 | if (connect && !isConnected) { |
| 4474 | AudioSystem.setDeviceConnectionState(device, AudioSystem.DEVICE_STATE_AVAILABLE, |
| 4475 | address, deviceName); |
| 4476 | mConnectedDevices.put(deviceKey, new DeviceListSpec(device, deviceName, address)); |
| 4477 | return true; |
| 4478 | } else if (!connect && isConnected) { |
| 4479 | AudioSystem.setDeviceConnectionState(device, AudioSystem.DEVICE_STATE_UNAVAILABLE, |
| 4480 | address, deviceName); |
| 4481 | mConnectedDevices.remove(deviceKey); |
| 4482 | return true; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | return false; |
| 4486 | } |
| 4487 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4488 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4489 | // sent if none of these devices is connected. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4490 | // Access synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4491 | int mBecomingNoisyIntentDevices = |
| 4492 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4493 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4494 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4495 | AudioSystem.DEVICE_OUT_ALL_USB | AudioSystem.DEVICE_OUT_LINE; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4496 | |
| 4497 | // must be called before removing the device from mConnectedDevices |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 4498 | // Called synchronized on mConnectedDevices |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4499 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4500 | int delay = 0; |
| 4501 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4502 | int devices = 0; |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4503 | for (String key : mConnectedDevices.keySet()) { |
| 4504 | int dev = mConnectedDevices.get(key).mDeviceType; |
| 4505 | if (((dev & AudioSystem.DEVICE_BIT_IN) == 0) |
| 4506 | && ((dev & mBecomingNoisyIntentDevices) != 0)) { |
| 4507 | devices |= dev; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4511 | sendMsg(mAudioHandler, |
| 4512 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4513 | SENDMSG_REPLACE, |
| 4514 | 0, |
| 4515 | 0, |
| 4516 | null, |
| 4517 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4518 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4519 | } |
| 4520 | } |
| 4521 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4522 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4523 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4524 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4525 | synchronized (mLastDeviceConnectMsgTime) { |
| 4526 | long time = SystemClock.uptimeMillis(); |
| 4527 | if (mLastDeviceConnectMsgTime > time) { |
Matthew Xie | c525cf7 | 2015-01-22 20:13:17 -0800 | [diff] [blame] | 4528 | delay = (int)(mLastDeviceConnectMsgTime - time) + 30; |
Eric Laurent | adbe8bf | 2014-11-03 18:26:32 -0800 | [diff] [blame] | 4529 | } |
| 4530 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4531 | } |
| 4532 | return delay; |
| 4533 | } |
| 4534 | |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4535 | private void sendDeviceConnectionIntent(int device, int state, String address, |
| 4536 | String deviceName) { |
| 4537 | if (DEBUG_DEVICES) { |
| 4538 | Slog.i(TAG, "sendDeviceConnectionIntent(dev:0x" + Integer.toHexString(device) + |
| 4539 | " state:0x" + Integer.toHexString(state) + " address:" + address + |
| 4540 | " name:" + deviceName + ");"); |
| 4541 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4542 | Intent intent = new Intent(); |
| 4543 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4544 | intent.putExtra(CONNECT_INTENT_KEY_STATE, state); |
| 4545 | intent.putExtra(CONNECT_INTENT_KEY_ADDRESS, address); |
| 4546 | intent.putExtra(CONNECT_INTENT_KEY_PORT_NAME, deviceName); |
| 4547 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4548 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4549 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4550 | int connType = 0; |
| 4551 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4552 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4553 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4554 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4555 | intent.putExtra("microphone", 1); |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4556 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE || |
| 4557 | device == AudioSystem.DEVICE_OUT_LINE) { |
| 4558 | /*do apps care about line-out vs headphones?*/ |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4559 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4560 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4561 | intent.putExtra("microphone", 0); |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4562 | } else if (device == AudioSystem.DEVICE_OUT_HDMI || |
| 4563 | device == AudioSystem.DEVICE_OUT_HDMI_ARC) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4564 | connType = AudioRoutesInfo.MAIN_HDMI; |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4565 | configureHdmiPlugIntent(intent, state); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4566 | } else if (device == AudioSystem.DEVICE_OUT_USB_DEVICE) { |
| 4567 | connType = AudioRoutesInfo.MAIN_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4570 | synchronized (mCurAudioRoutes) { |
| 4571 | if (connType != 0) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4572 | int newConn = mCurAudioRoutes.mainType; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4573 | if (state != 0) { |
| 4574 | newConn |= connType; |
| 4575 | } else { |
| 4576 | newConn &= ~connType; |
| 4577 | } |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4578 | if (newConn != mCurAudioRoutes.mainType) { |
| 4579 | mCurAudioRoutes.mainType = newConn; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4580 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4581 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4586 | final long ident = Binder.clearCallingIdentity(); |
| 4587 | try { |
| 4588 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4589 | } finally { |
| 4590 | Binder.restoreCallingIdentity(ident); |
| 4591 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4592 | } |
| 4593 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4594 | private void onSetWiredDeviceConnectionState(int device, int state, String address, |
Paul McLean | 394a8e1 | 2015-03-03 10:29:19 -0700 | [diff] [blame] | 4595 | String deviceName) { |
| 4596 | if (DEBUG_DEVICES) { |
| 4597 | Slog.i(TAG, "onSetWiredDeviceConnectionState(dev:" + Integer.toHexString(device) + |
| 4598 | " state:" + Integer.toHexString(state) + " address:" + address + |
| 4599 | " deviceName:" + deviceName + ");"); |
| 4600 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4601 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4602 | synchronized (mConnectedDevices) { |
| 4603 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4604 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4605 | (device == AudioSystem.DEVICE_OUT_LINE))) { |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4606 | setBluetoothA2dpOnInt(true); |
| 4607 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4608 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4609 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4610 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4611 | handleDeviceConnection(state == 1, device, address, deviceName); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4612 | if (state != 0) { |
| 4613 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
Jon Eklund | 43cc8bb | 2014-07-28 16:07:24 -0500 | [diff] [blame] | 4614 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) || |
| 4615 | (device == AudioSystem.DEVICE_OUT_LINE)) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4616 | setBluetoothA2dpOnInt(false); |
| 4617 | } |
| 4618 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4619 | sendMsg(mAudioHandler, |
| 4620 | MSG_CHECK_MUSIC_ACTIVE, |
| 4621 | SENDMSG_REPLACE, |
| 4622 | 0, |
| 4623 | 0, |
| 4624 | null, |
| 4625 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4626 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4627 | // Television devices without CEC service apply software volume on HDMI output |
| 4628 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4629 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 4630 | checkAllFixedVolumeDevices(); |
| 4631 | if (mHdmiManager != null) { |
| 4632 | synchronized (mHdmiManager) { |
| 4633 | if (mHdmiPlaybackClient != null) { |
| 4634 | mHdmiCecSink = false; |
| 4635 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | } |
| 4640 | } else { |
| 4641 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4642 | if (mHdmiManager != null) { |
| 4643 | synchronized (mHdmiManager) { |
| 4644 | mHdmiCecSink = false; |
| 4645 | } |
| 4646 | } |
| 4647 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4648 | } |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4649 | if (!isUsb && device != AudioSystem.DEVICE_IN_WIRED_HEADSET) { |
| 4650 | sendDeviceConnectionIntent(device, state, address, deviceName); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4651 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4655 | private void configureHdmiPlugIntent(Intent intent, int state) { |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4656 | intent.setAction(AudioManager.ACTION_HDMI_AUDIO_PLUG); |
| 4657 | intent.putExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, state); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4658 | if (state == 1) { |
| 4659 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 4660 | int[] portGeneration = new int[1]; |
| 4661 | int status = AudioSystem.listAudioPorts(ports, portGeneration); |
| 4662 | if (status == AudioManager.SUCCESS) { |
| 4663 | for (AudioPort port : ports) { |
| 4664 | if (port instanceof AudioDevicePort) { |
| 4665 | final AudioDevicePort devicePort = (AudioDevicePort) port; |
Eric Laurent | 6fa4245 | 2015-01-09 15:09:40 -0800 | [diff] [blame] | 4666 | if (devicePort.type() == AudioManager.DEVICE_OUT_HDMI || |
| 4667 | devicePort.type() == AudioManager.DEVICE_OUT_HDMI_ARC) { |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4668 | // format the list of supported encodings |
| 4669 | int[] formats = devicePort.formats(); |
| 4670 | if (formats.length > 0) { |
| 4671 | ArrayList<Integer> encodingList = new ArrayList(1); |
| 4672 | for (int format : formats) { |
| 4673 | // a format in the list can be 0, skip it |
| 4674 | if (format != AudioFormat.ENCODING_INVALID) { |
| 4675 | encodingList.add(format); |
| 4676 | } |
| 4677 | } |
| 4678 | int[] encodingArray = new int[encodingList.size()]; |
| 4679 | for (int i = 0 ; i < encodingArray.length ; i++) { |
| 4680 | encodingArray[i] = encodingList.get(i); |
| 4681 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4682 | intent.putExtra(AudioManager.EXTRA_ENCODINGS, encodingArray); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4683 | } |
| 4684 | // find the maximum supported number of channels |
| 4685 | int maxChannels = 0; |
| 4686 | for (int mask : devicePort.channelMasks()) { |
| 4687 | int channelCount = AudioFormat.channelCountFromOutChannelMask(mask); |
| 4688 | if (channelCount > maxChannels) { |
| 4689 | maxChannels = channelCount; |
| 4690 | } |
| 4691 | } |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 4692 | intent.putExtra(AudioManager.EXTRA_MAX_CHANNEL_COUNT, maxChannels); |
Jean-Michel Trivi | 37d7804 | 2014-08-10 14:59:15 -0700 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | } |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4700 | /* cache of the address of the last dock the device was connected to */ |
| 4701 | private String mDockAddress; |
| 4702 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4703 | /** |
| 4704 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 4705 | */ |
| 4706 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 4707 | @Override |
| 4708 | public void onReceive(Context context, Intent intent) { |
| 4709 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4710 | int outDevice; |
| 4711 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4712 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4713 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4714 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 4715 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4716 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4717 | int config; |
| 4718 | switch (dockState) { |
| 4719 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 4720 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 4721 | break; |
| 4722 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 4723 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 4724 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4725 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4726 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4727 | break; |
| 4728 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 4729 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 4730 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4731 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 4732 | default: |
| 4733 | config = AudioSystem.FORCE_NONE; |
| 4734 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4735 | // Low end docks have a menu to enable or disable audio |
| 4736 | // (see mDockAudioMediaEnabled) |
| 4737 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 4738 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 4739 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 4740 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 4741 | } |
| 4742 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4743 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4744 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4745 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4746 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 4747 | inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Nick Pelly | a56d1c7 | 2009-08-19 14:49:29 -0700 | [diff] [blame] | 4748 | String address = null; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4749 | |
| 4750 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 4751 | if (btDevice == null) { |
| 4752 | return; |
| 4753 | } |
| 4754 | |
| 4755 | address = btDevice.getAddress(); |
| 4756 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 4757 | if (btClass != null) { |
| 4758 | switch (btClass.getDeviceClass()) { |
| 4759 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 4760 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4761 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4762 | break; |
| 4763 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4764 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4765 | break; |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4766 | } |
| 4767 | } |
| 4768 | |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4769 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4770 | address = ""; |
| 4771 | } |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4772 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4773 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 4774 | boolean success = |
| 4775 | handleDeviceConnection(connected, outDevice, address, "Bluetooth Headset") && |
| 4776 | handleDeviceConnection(connected, inDevice, address, "Bluetooth Headset"); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4777 | if (success) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4778 | synchronized (mScoClients) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4779 | if (connected) { |
| 4780 | mBluetoothHeadsetDevice = btDevice; |
| 4781 | } else { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4782 | mBluetoothHeadsetDevice = null; |
| 4783 | resetBluetoothSco(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4784 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4785 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4786 | } |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4787 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4788 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4789 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4790 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4791 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4792 | // broadcast intent if the connection was initated by AudioService |
| 4793 | if (!mScoClients.isEmpty() && |
| 4794 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 4795 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 4796 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4797 | broadcast = true; |
| 4798 | } |
| 4799 | switch (btState) { |
| 4800 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4801 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4802 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4803 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4804 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4805 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4806 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4807 | break; |
| 4808 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4809 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4810 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 4811 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4812 | break; |
| 4813 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4814 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4815 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4816 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4817 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4818 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4819 | default: |
| 4820 | // do not broadcast CONNECTING or invalid state |
| 4821 | broadcast = false; |
| 4822 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4823 | } |
| 4824 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4825 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4826 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4827 | //FIXME: this is to maintain compatibility with deprecated intent |
| 4828 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4829 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4830 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4831 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4832 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4833 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4834 | if (mMonitorRotation) { |
| 4835 | mOrientationListener.onOrientationChanged(0); //argument is ignored anyway |
| 4836 | mOrientationListener.enable(); |
| 4837 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4838 | AudioSystem.setParameters("screen_state=on"); |
| 4839 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Jon Eklund | 318f0fe | 2014-01-23 17:53:48 -0600 | [diff] [blame] | 4840 | if (mMonitorRotation) { |
| 4841 | //reduce wakeups (save current) by only listening when display is on |
| 4842 | mOrientationListener.disable(); |
| 4843 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4844 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4845 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4846 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4847 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4848 | // attempt to stop music playback for background user |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4849 | sendMsg(mAudioHandler, |
| 4850 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4851 | SENDMSG_REPLACE, |
| 4852 | 0, |
| 4853 | 0, |
| 4854 | null, |
| 4855 | 0); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4856 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4857 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4858 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4859 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4860 | readAudioSettings(true /*userSwitch*/); |
| 4861 | // preserve STREAM_MUSIC volume from one user to the next. |
| 4862 | sendMsg(mAudioHandler, |
| 4863 | MSG_SET_ALL_VOLUMES, |
| 4864 | SENDMSG_QUEUE, |
| 4865 | 0, |
| 4866 | 0, |
| 4867 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4868 | } |
| 4869 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4870 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4871 | |
| 4872 | //========================================================================================== |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4873 | // RemoteControlDisplay / RemoteControlClient / Remote info |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4874 | //========================================================================================== |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4875 | public boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, |
| 4876 | ComponentName listenerComp) { |
| 4877 | return mMediaFocusControl.registerRemoteController(rcd, w, h, listenerComp); |
| 4878 | } |
| 4879 | |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4880 | public boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4881 | return mMediaFocusControl.registerRemoteControlDisplay(rcd, w, h); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4882 | } |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4883 | |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4884 | public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4885 | mMediaFocusControl.unregisterRemoteControlDisplay(rcd); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4889 | mMediaFocusControl.remoteControlDisplayUsesBitmapSize(rcd, w, h); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 4892 | public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, |
| 4893 | boolean wantsSync) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4894 | mMediaFocusControl.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync); |
| 4895 | } |
| 4896 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4897 | @Override |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4898 | public void setRemoteStreamVolume(int index) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 4899 | enforceVolumeController("set the remote stream volume"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4900 | mMediaFocusControl.setRemoteStreamVolume(index); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4903 | //========================================================================================== |
| 4904 | // Audio Focus |
| 4905 | //========================================================================================== |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4906 | public int requestAudioFocus(AudioAttributes aa, int durationHint, IBinder cb, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4907 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4908 | IAudioPolicyCallback pcb) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4909 | // permission checks |
| 4910 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_LOCK) == AudioManager.AUDIOFOCUS_FLAG_LOCK) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4911 | if (AudioSystem.IN_VOICE_COMM_FOCUS_ID.equals(clientId)) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4912 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 4913 | android.Manifest.permission.MODIFY_PHONE_STATE)) { |
| 4914 | Log.e(TAG, "Invalid permission to (un)lock audio focus", new Exception()); |
| 4915 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 4916 | } |
| 4917 | } else { |
| 4918 | // only a registered audio policy can be used to lock focus |
| 4919 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4920 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 4921 | Log.e(TAG, "Invalid unregistered AudioPolicy to (un)lock audio focus"); |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4922 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4928 | return mMediaFocusControl.requestAudioFocus(aa, durationHint, cb, fd, |
| 4929 | clientId, callingPackageName, flags); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4930 | } |
| 4931 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4932 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId, AudioAttributes aa) { |
| 4933 | return mMediaFocusControl.abandonAudioFocus(fd, clientId, aa); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | public void unregisterAudioFocusClient(String clientId) { |
| 4937 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4938 | } |
| 4939 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 4940 | public int getCurrentAudioFocus() { |
| 4941 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 4942 | } |
| 4943 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 4944 | private boolean readCameraSoundForced() { |
| 4945 | return SystemProperties.getBoolean("audio.camerasound.force", false) || |
| 4946 | mContext.getResources().getBoolean( |
| 4947 | com.android.internal.R.bool.config_camera_sound_forced); |
| 4948 | } |
| 4949 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4950 | //========================================================================================== |
| 4951 | // Device orientation |
| 4952 | //========================================================================================== |
| 4953 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4954 | * Handles device configuration changes that may map to a change in the orientation |
| 4955 | * or orientation. |
| 4956 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 4957 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4958 | */ |
| 4959 | private void handleConfigurationChanged(Context context) { |
| 4960 | try { |
| 4961 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 4962 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4963 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4964 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4965 | if (mMonitorOrientation) { |
| 4966 | int newOrientation = config.orientation; |
| 4967 | if (newOrientation != mDeviceOrientation) { |
| 4968 | mDeviceOrientation = newOrientation; |
| 4969 | setOrientationForAudioSystem(); |
| 4970 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4971 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4972 | sendMsg(mAudioHandler, |
| 4973 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 4974 | SENDMSG_REPLACE, |
| 4975 | 0, |
| 4976 | 0, |
| 4977 | null, |
| 4978 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4979 | |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 4980 | boolean cameraSoundForced = readCameraSoundForced(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4981 | synchronized (mSettingsLock) { |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4982 | boolean cameraSoundForcedChanged = false; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4983 | synchronized (mCameraSoundForced) { |
| 4984 | if (cameraSoundForced != mCameraSoundForced) { |
| 4985 | mCameraSoundForced = cameraSoundForced; |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4986 | cameraSoundForcedChanged = true; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4987 | } |
| 4988 | } |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 4989 | if (cameraSoundForcedChanged) { |
| 4990 | if (!isPlatformTelevision()) { |
| 4991 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 4992 | if (cameraSoundForced) { |
| 4993 | s.setAllIndexesToMax(); |
| 4994 | mRingerModeAffectedStreams &= |
| 4995 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4996 | } else { |
| 4997 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM]); |
| 4998 | mRingerModeAffectedStreams |= |
| 4999 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 5000 | } |
| 5001 | // take new state into account for streams muted by ringer mode |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5002 | setRingerModeInt(getRingerModeInternal(), false); |
Eric Laurent | 8fa4d6f | 2014-10-30 14:13:03 -0700 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | sendMsg(mAudioHandler, |
| 5006 | MSG_SET_FORCE_USE, |
| 5007 | SENDMSG_QUEUE, |
| 5008 | AudioSystem.FOR_SYSTEM, |
| 5009 | cameraSoundForced ? |
| 5010 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 5011 | null, |
| 5012 | 0); |
| 5013 | |
| 5014 | sendMsg(mAudioHandler, |
| 5015 | MSG_SET_ALL_VOLUMES, |
| 5016 | SENDMSG_QUEUE, |
| 5017 | 0, |
| 5018 | 0, |
| 5019 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 5020 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5021 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5022 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5023 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5024 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | private void setOrientationForAudioSystem() { |
| 5029 | switch (mDeviceOrientation) { |
| 5030 | case Configuration.ORIENTATION_LANDSCAPE: |
| 5031 | //Log.i(TAG, "orientation is landscape"); |
| 5032 | AudioSystem.setParameters("orientation=landscape"); |
| 5033 | break; |
| 5034 | case Configuration.ORIENTATION_PORTRAIT: |
| 5035 | //Log.i(TAG, "orientation is portrait"); |
| 5036 | AudioSystem.setParameters("orientation=portrait"); |
| 5037 | break; |
| 5038 | case Configuration.ORIENTATION_SQUARE: |
| 5039 | //Log.i(TAG, "orientation is square"); |
| 5040 | AudioSystem.setParameters("orientation=square"); |
| 5041 | break; |
| 5042 | case Configuration.ORIENTATION_UNDEFINED: |
| 5043 | //Log.i(TAG, "orientation is undefined"); |
| 5044 | AudioSystem.setParameters("orientation=undefined"); |
| 5045 | break; |
| 5046 | default: |
| 5047 | Log.e(TAG, "Unknown orientation"); |
| 5048 | } |
| 5049 | } |
| 5050 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 5051 | private void setRotationForAudioSystem() { |
| 5052 | switch (mDeviceRotation) { |
| 5053 | case Surface.ROTATION_0: |
| 5054 | AudioSystem.setParameters("rotation=0"); |
| 5055 | break; |
| 5056 | case Surface.ROTATION_90: |
| 5057 | AudioSystem.setParameters("rotation=90"); |
| 5058 | break; |
| 5059 | case Surface.ROTATION_180: |
| 5060 | AudioSystem.setParameters("rotation=180"); |
| 5061 | break; |
| 5062 | case Surface.ROTATION_270: |
| 5063 | AudioSystem.setParameters("rotation=270"); |
| 5064 | break; |
| 5065 | default: |
| 5066 | Log.e(TAG, "Unknown device rotation"); |
| 5067 | } |
| 5068 | } |
| 5069 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 5070 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5071 | // Handles request to override default use of A2DP for media. |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5072 | // Must be called synchronized on mConnectedDevices |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5073 | public void setBluetoothA2dpOnInt(boolean on) { |
| 5074 | synchronized (mBluetoothA2dpEnabledLock) { |
| 5075 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5076 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5077 | setForceUseInt_SyncDevices(AudioSystem.FOR_MEDIA, |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 5078 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 5079 | } |
| 5080 | } |
| 5081 | |
Jean-Michel Trivi | 7e2f371 | 2015-01-12 11:01:55 -0800 | [diff] [blame] | 5082 | // Must be called synchronized on mConnectedDevices |
| 5083 | private void setForceUseInt_SyncDevices(int usage, int config) { |
| 5084 | switch (usage) { |
| 5085 | case AudioSystem.FOR_MEDIA: |
| 5086 | if (config == AudioSystem.FORCE_NO_BT_A2DP) { |
| 5087 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5088 | } else { // config == AudioSystem.FORCE_NONE |
| 5089 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 5090 | } |
| 5091 | break; |
| 5092 | case AudioSystem.FOR_DOCK: |
| 5093 | if (config == AudioSystem.FORCE_ANALOG_DOCK) { |
| 5094 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5095 | } else { // config == AudioSystem.FORCE_NONE |
| 5096 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 5097 | } |
| 5098 | break; |
| 5099 | default: |
| 5100 | // usage doesn't affect the broadcast of ACTION_AUDIO_BECOMING_NOISY |
| 5101 | } |
| 5102 | AudioSystem.setForceUse(usage, config); |
| 5103 | } |
| 5104 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5105 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5106 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 5107 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 5108 | mRingtonePlayer = player; |
| 5109 | } |
| 5110 | |
| 5111 | @Override |
| 5112 | public IRingtonePlayer getRingtonePlayer() { |
| 5113 | return mRingtonePlayer; |
| 5114 | } |
| 5115 | |
| 5116 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5117 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 5118 | synchronized (mCurAudioRoutes) { |
| 5119 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 5120 | mRoutesObservers.register(observer); |
| 5121 | return routes; |
| 5122 | } |
| 5123 | } |
| 5124 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5125 | |
| 5126 | //========================================================================================== |
| 5127 | // Safe media volume management. |
| 5128 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 5129 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 5130 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 5131 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 5132 | // property. Platforms with a different limit must set this property accordingly in their |
| 5133 | // overlay. |
| 5134 | //========================================================================================== |
| 5135 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5136 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 5137 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 5138 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 5139 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 5140 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 5141 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5142 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 5143 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 5144 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 5145 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5146 | private Integer mSafeMediaVolumeState; |
| 5147 | |
| 5148 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5149 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5150 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5151 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 5152 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 5153 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 5154 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 5155 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 5156 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 5157 | private int mMusicActiveMs; |
| 5158 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 5159 | 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] | 5160 | 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] | 5161 | |
| 5162 | private void setSafeMediaVolumeEnabled(boolean on) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5163 | synchronized (mSafeMediaVolumeState) { |
| 5164 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 5165 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 5166 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 5167 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 5168 | enforceSafeMediaVolume(); |
| 5169 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 5170 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5171 | mMusicActiveMs = 1; // nonzero = confirmed |
| 5172 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5173 | sendMsg(mAudioHandler, |
| 5174 | MSG_CHECK_MUSIC_ACTIVE, |
| 5175 | SENDMSG_REPLACE, |
| 5176 | 0, |
| 5177 | 0, |
| 5178 | null, |
| 5179 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 5180 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5181 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | private void enforceSafeMediaVolume() { |
| 5186 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5187 | int devices = mSafeMediaVolumeDevices; |
| 5188 | int i = 0; |
| 5189 | |
| 5190 | while (devices != 0) { |
| 5191 | int device = 1 << i++; |
| 5192 | if ((device & devices) == 0) { |
| 5193 | continue; |
| 5194 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5195 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5196 | if (index > mSafeMediaVolumeIndex) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 5197 | streamState.setIndex(mSafeMediaVolumeIndex, device); |
| 5198 | sendMsg(mAudioHandler, |
| 5199 | MSG_SET_DEVICE_VOLUME, |
| 5200 | SENDMSG_QUEUE, |
| 5201 | device, |
| 5202 | 0, |
| 5203 | streamState, |
| 5204 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5205 | } |
| 5206 | devices &= ~device; |
| 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5211 | synchronized (mSafeMediaVolumeState) { |
| 5212 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5213 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 5214 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 5215 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5216 | return false; |
| 5217 | } |
| 5218 | return true; |
| 5219 | } |
| 5220 | } |
| 5221 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5222 | @Override |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5223 | public void disableSafeMediaVolume() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5224 | enforceVolumeController("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 5225 | synchronized (mSafeMediaVolumeState) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5226 | setSafeMediaVolumeEnabled(false); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 5227 | if (mPendingVolumeCommand != null) { |
| 5228 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 5229 | mPendingVolumeCommand.mIndex, |
| 5230 | mPendingVolumeCommand.mFlags, |
| 5231 | mPendingVolumeCommand.mDevice); |
| 5232 | mPendingVolumeCommand = null; |
| 5233 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 5234 | } |
| 5235 | } |
| 5236 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5237 | //========================================================================================== |
| 5238 | // Hdmi Cec system audio mode. |
John Spurlock | bc82b12 | 2015-03-02 16:12:38 -0500 | [diff] [blame] | 5239 | // If Hdmi Cec's system audio mode is on, audio service should send the volume change |
| 5240 | // to HdmiControlService so that the audio receiver can handle it. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5241 | //========================================================================================== |
| 5242 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5243 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 5244 | public void onComplete(int status) { |
| 5245 | if (mHdmiManager != null) { |
| 5246 | synchronized (mHdmiManager) { |
| 5247 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5248 | // Television devices without CEC service apply software volume on HDMI output |
| 5249 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5250 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5251 | } |
| 5252 | checkAllFixedVolumeDevices(); |
| 5253 | } |
| 5254 | } |
| 5255 | } |
| 5256 | }; |
| 5257 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5258 | // If HDMI-CEC system audio is supported |
| 5259 | private boolean mHdmiSystemAudioSupported = false; |
| 5260 | // Set only when device is tv. |
| 5261 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 0b03f99 | 2014-11-18 18:08:02 -0800 | [diff] [blame] | 5262 | // true if the device has system feature PackageManager.FEATURE_LEANBACK. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5263 | // cached HdmiControlManager interface |
| 5264 | private HdmiControlManager mHdmiManager; |
| 5265 | // Set only when device is a set-top box. |
| 5266 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5267 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5268 | private boolean mHdmiCecSink; |
| 5269 | |
| 5270 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5271 | |
| 5272 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5273 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5274 | int device = AudioSystem.DEVICE_NONE; |
| 5275 | if (mHdmiManager != null) { |
| 5276 | synchronized (mHdmiManager) { |
| 5277 | if (mHdmiTvClient == null) { |
| 5278 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5279 | return device; |
| 5280 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5281 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5282 | synchronized (mHdmiTvClient) { |
| 5283 | if (mHdmiSystemAudioSupported != on) { |
| 5284 | mHdmiSystemAudioSupported = on; |
| 5285 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5286 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5287 | AudioSystem.FORCE_NONE); |
| 5288 | } |
| 5289 | device = AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC); |
| 5290 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5291 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5292 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5293 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5294 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5295 | |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 5296 | @Override |
| 5297 | public boolean isHdmiSystemAudioSupported() { |
| 5298 | return mHdmiSystemAudioSupported; |
| 5299 | } |
| 5300 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5301 | //========================================================================================== |
Jean-Michel Trivi | 873cc45 | 2014-09-11 17:25:09 -0700 | [diff] [blame] | 5302 | // Accessibility: taking touch exploration into account for selecting the default |
| 5303 | // stream override timeout when adjusting volume |
| 5304 | //========================================================================================== |
| 5305 | private static class StreamOverride |
| 5306 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 5307 | |
| 5308 | // AudioService.getActiveStreamType() will return: |
| 5309 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 5310 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
| 5311 | // stopped |
| 5312 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 5000; |
| 5313 | private static final int TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS = 1000; |
| 5314 | |
| 5315 | static int sDelayMs; |
| 5316 | |
| 5317 | static void init(Context ctxt) { |
| 5318 | AccessibilityManager accessibilityManager = |
| 5319 | (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 5320 | updateDefaultStreamOverrideDelay( |
| 5321 | accessibilityManager.isTouchExplorationEnabled()); |
| 5322 | accessibilityManager.addTouchExplorationStateChangeListener( |
| 5323 | new StreamOverride()); |
| 5324 | } |
| 5325 | |
| 5326 | @Override |
| 5327 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 5328 | updateDefaultStreamOverrideDelay(enabled); |
| 5329 | } |
| 5330 | |
| 5331 | private static void updateDefaultStreamOverrideDelay(boolean touchExploreEnabled) { |
| 5332 | if (touchExploreEnabled) { |
| 5333 | sDelayMs = TOUCH_EXPLORE_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5334 | } else { |
| 5335 | sDelayMs = DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS; |
| 5336 | } |
| 5337 | if (DEBUG_VOL) Log.d(TAG, "Touch exploration enabled=" + touchExploreEnabled |
| 5338 | + " stream override delay is now " + sDelayMs + " ms"); |
| 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | //========================================================================================== |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5343 | // Camera shutter sound policy. |
| 5344 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5345 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5346 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5347 | //========================================================================================== |
| 5348 | |
| 5349 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5350 | private Boolean mCameraSoundForced; |
| 5351 | |
| 5352 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5353 | public boolean isCameraSoundForced() { |
| 5354 | synchronized (mCameraSoundForced) { |
| 5355 | return mCameraSoundForced; |
| 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5360 | "SILENT", |
| 5361 | "VIBRATE", |
| 5362 | "NORMAL" |
| 5363 | }; |
| 5364 | |
| 5365 | private void dumpRingerMode(PrintWriter pw) { |
| 5366 | pw.println("\nRinger mode: "); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5367 | pw.println("- mode (internal) = " + RINGER_MODE_NAMES[mRingerMode]); |
| 5368 | pw.println("- mode (external) = " + RINGER_MODE_NAMES[mRingerModeExternal]); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5369 | pw.print("- ringer mode affected streams = 0x"); |
| 5370 | pw.println(Integer.toHexString(mRingerModeAffectedStreams)); |
| 5371 | pw.print("- ringer mode muted streams = 0x"); |
| 5372 | pw.println(Integer.toHexString(mRingerModeMutedStreams)); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5373 | pw.print("- delegate = "); pw.println(mRingerModeDelegate); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5374 | } |
| 5375 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5376 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5377 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5378 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5379 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5380 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5381 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5382 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5383 | pw.println("\nAudio routes:"); |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 5384 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mainType)); |
| 5385 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.bluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5386 | |
| 5387 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5388 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5389 | pw.print(" mSafeMediaVolumeState="); |
| 5390 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5391 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5392 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5393 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame] | 5394 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 5e78373 | 2015-02-19 10:28:59 -0500 | [diff] [blame] | 5395 | pw.print(" mCameraSoundForced="); pw.println(mCameraSoundForced); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5396 | pw.print(" mHasVibrator="); pw.println(mHasVibrator); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5397 | pw.print(" mControllerService="); pw.println(mControllerService); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5398 | |
| 5399 | dumpAudioPolicies(pw); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5403 | switch(state) { |
| 5404 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5405 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5406 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5407 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5408 | } |
| 5409 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5410 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5411 | |
| 5412 | // Inform AudioFlinger of our device's low RAM attribute |
| 5413 | private static void readAndSetLowRamDevice() |
| 5414 | { |
| 5415 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5416 | if (status != 0) { |
| 5417 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5418 | } |
| 5419 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5420 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5421 | private void enforceVolumeController(String action) { |
| 5422 | if (mControllerService.mUid != 0 && Binder.getCallingUid() == mControllerService.mUid) { |
| 5423 | return; |
| 5424 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5425 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5426 | "Only SystemUI can " + action); |
| 5427 | } |
| 5428 | |
| 5429 | @Override |
| 5430 | public void setVolumeController(final IVolumeController controller) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5431 | enforceVolumeController("set the volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5432 | |
| 5433 | // return early if things are not actually changing |
| 5434 | if (mVolumeController.isSameBinder(controller)) { |
| 5435 | return; |
| 5436 | } |
| 5437 | |
| 5438 | // dismiss the old volume controller |
| 5439 | mVolumeController.postDismiss(); |
| 5440 | if (controller != null) { |
| 5441 | // we are about to register a new controller, listen for its death |
| 5442 | try { |
| 5443 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5444 | @Override |
| 5445 | public void binderDied() { |
| 5446 | if (mVolumeController.isSameBinder(controller)) { |
| 5447 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5448 | setVolumeController(null); |
| 5449 | } |
| 5450 | } |
| 5451 | }, 0); |
| 5452 | } catch (RemoteException e) { |
| 5453 | // noop |
| 5454 | } |
| 5455 | } |
| 5456 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5457 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5458 | } |
| 5459 | |
| 5460 | @Override |
| 5461 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5462 | enforceVolumeController("notify about volume controller visibility"); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5463 | |
| 5464 | // return early if the controller is not current |
| 5465 | if (!mVolumeController.isSameBinder(controller)) { |
| 5466 | return; |
| 5467 | } |
| 5468 | |
| 5469 | mVolumeController.setVisible(visible); |
| 5470 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5471 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5472 | |
| 5473 | public static class VolumeController { |
| 5474 | private static final String TAG = "VolumeController"; |
| 5475 | |
| 5476 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5477 | private boolean mVisible; |
| 5478 | private long mNextLongPress; |
| 5479 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5480 | |
| 5481 | public void setController(IVolumeController controller) { |
| 5482 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5483 | mVisible = false; |
| 5484 | } |
| 5485 | |
| 5486 | public void loadSettings(ContentResolver cr) { |
| 5487 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5488 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5489 | } |
| 5490 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 5491 | public boolean suppressAdjustment(int resolvedStream, int flags, boolean isMute) { |
| 5492 | if (isMute) { |
| 5493 | return false; |
| 5494 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5495 | boolean suppress = false; |
| 5496 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5497 | final long now = SystemClock.uptimeMillis(); |
| 5498 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5499 | // ui will become visible |
| 5500 | if (mNextLongPress < now) { |
| 5501 | mNextLongPress = now + mLongPressTimeout; |
| 5502 | } |
| 5503 | suppress = true; |
| 5504 | } else if (mNextLongPress > 0) { // in a long-press |
| 5505 | if (now > mNextLongPress) { |
| 5506 | // long press triggered, no more suppression |
| 5507 | mNextLongPress = 0; |
| 5508 | } else { |
| 5509 | // keep suppressing until the long press triggers |
| 5510 | suppress = true; |
| 5511 | } |
| 5512 | } |
| 5513 | } |
| 5514 | return suppress; |
| 5515 | } |
| 5516 | |
| 5517 | public void setVisible(boolean visible) { |
| 5518 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | public boolean isSameBinder(IVolumeController controller) { |
| 5522 | return Objects.equals(asBinder(), binder(controller)); |
| 5523 | } |
| 5524 | |
| 5525 | public IBinder asBinder() { |
| 5526 | return binder(mController); |
| 5527 | } |
| 5528 | |
| 5529 | private static IBinder binder(IVolumeController controller) { |
| 5530 | return controller == null ? null : controller.asBinder(); |
| 5531 | } |
| 5532 | |
| 5533 | @Override |
| 5534 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5535 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5539 | if (mController == null) |
| 5540 | return; |
| 5541 | try { |
| 5542 | mController.displaySafeVolumeWarning(flags); |
| 5543 | } catch (RemoteException e) { |
| 5544 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | public void postVolumeChanged(int streamType, int flags) { |
| 5549 | if (mController == null) |
| 5550 | return; |
| 5551 | try { |
| 5552 | mController.volumeChanged(streamType, flags); |
| 5553 | } catch (RemoteException e) { |
| 5554 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5555 | } |
| 5556 | } |
| 5557 | |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5558 | public void postMasterMuteChanged(int flags) { |
| 5559 | if (mController == null) |
| 5560 | return; |
| 5561 | try { |
| 5562 | mController.masterMuteChanged(flags); |
| 5563 | } catch (RemoteException e) { |
| 5564 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | public void setLayoutDirection(int layoutDirection) { |
| 5569 | if (mController == null) |
| 5570 | return; |
| 5571 | try { |
| 5572 | mController.setLayoutDirection(layoutDirection); |
| 5573 | } catch (RemoteException e) { |
| 5574 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | public void postDismiss() { |
| 5579 | if (mController == null) |
| 5580 | return; |
| 5581 | try { |
| 5582 | mController.dismiss(); |
| 5583 | } catch (RemoteException e) { |
| 5584 | Log.w(TAG, "Error calling dismiss", e); |
| 5585 | } |
| 5586 | } |
| 5587 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5588 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5589 | /** |
| 5590 | * Interface for system components to get some extra functionality through |
| 5591 | * LocalServices. |
| 5592 | */ |
| 5593 | final class AudioServiceInternal extends AudioManagerInternal { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5594 | @Override |
| 5595 | public void setRingerModeDelegate(RingerModeDelegate delegate) { |
| 5596 | mRingerModeDelegate = delegate; |
| 5597 | if (mRingerModeDelegate != null) { |
| 5598 | setRingerModeInternal(getRingerModeInternal(), TAG + ".setRingerModeDelegate"); |
| 5599 | } |
| 5600 | } |
RoboErik | 272e161 | 2014-09-05 11:39:29 -0700 | [diff] [blame] | 5601 | |
| 5602 | @Override |
| 5603 | public void adjustSuggestedStreamVolumeForUid(int streamType, int direction, int flags, |
| 5604 | String callingPackage, int uid) { |
| 5605 | // direction and stream type swap here because the public |
| 5606 | // adjustSuggested has a different order than the other methods. |
| 5607 | adjustSuggestedStreamVolume(direction, streamType, flags, callingPackage, uid); |
| 5608 | } |
| 5609 | |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5610 | @Override |
| 5611 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 5612 | String callingPackage, int uid) { |
| 5613 | adjustStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5614 | } |
| 5615 | |
| 5616 | @Override |
| 5617 | public void setStreamVolumeForUid(int streamType, int direction, int flags, |
| 5618 | String callingPackage, int uid) { |
| 5619 | setStreamVolume(streamType, direction, flags, callingPackage, uid); |
| 5620 | } |
RoboErik | 519c774 | 2014-11-18 10:59:09 -0800 | [diff] [blame] | 5621 | |
| 5622 | @Override |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 5623 | public int getRingerModeInternal() { |
| 5624 | return AudioService.this.getRingerModeInternal(); |
| 5625 | } |
| 5626 | |
| 5627 | @Override |
| 5628 | public void setRingerModeInternal(int ringerMode, String caller) { |
| 5629 | AudioService.this.setRingerModeInternal(ringerMode, caller); |
| 5630 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5631 | |
| 5632 | @Override |
| 5633 | public int getVolumeControllerUid() { |
| 5634 | return mControllerService.mUid; |
| 5635 | } |
RoboErik | 0dac35a | 2014-08-12 15:48:49 -0700 | [diff] [blame] | 5636 | } |
| 5637 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5638 | //========================================================================================== |
| 5639 | // Audio policy management |
| 5640 | //========================================================================================== |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5641 | public String registerAudioPolicy(AudioPolicyConfig policyConfig, IAudioPolicyCallback pcb, |
| 5642 | boolean hasFocusListener) { |
| 5643 | if (DEBUG_AP) Log.d(TAG, "registerAudioPolicy for " + pcb.asBinder() |
| 5644 | + " with config:" + policyConfig); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5645 | String regId = null; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5646 | // error handling |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5647 | boolean hasPermissionForPolicy = |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5648 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5649 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5650 | if (!hasPermissionForPolicy) { |
| 5651 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 5652 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5653 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5654 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5655 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5656 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5657 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5658 | if (mAudioPolicies.containsKey(pcb.asBinder())) { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5659 | Slog.e(TAG, "Cannot re-register policy"); |
| 5660 | return null; |
| 5661 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5662 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, pcb, hasFocusListener); |
| 5663 | pcb.asBinder().linkToDeath(app, 0/*flags*/); |
| 5664 | regId = app.getRegistrationId(); |
| 5665 | mAudioPolicies.put(pcb.asBinder(), app); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5666 | } catch (RemoteException e) { |
| 5667 | // audio policy owner has already died! |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5668 | 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] | 5669 | " binder death", e); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5670 | return null; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5671 | } |
| 5672 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5673 | return regId; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5674 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5675 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5676 | public void unregisterAudioPolicyAsync(IAudioPolicyCallback pcb) { |
| 5677 | if (DEBUG_AP) Log.d(TAG, "unregisterAudioPolicyAsync for " + pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5678 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5679 | AudioPolicyProxy app = mAudioPolicies.remove(pcb.asBinder()); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5680 | if (app == null) { |
| 5681 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 5682 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5683 | return; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5684 | } else { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5685 | pcb.asBinder().unlinkToDeath(app, 0/*flags*/); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5686 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5687 | app.release(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5688 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5689 | // TODO implement clearing mix attribute matching info in native audio policy |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5692 | public int setFocusPropertiesForPolicy(int duckingBehavior, IAudioPolicyCallback pcb) { |
| 5693 | if (DEBUG_AP) Log.d(TAG, "setFocusPropertiesForPolicy() duck behavior=" + duckingBehavior |
| 5694 | + " policy " + pcb.asBinder()); |
| 5695 | // error handling |
| 5696 | boolean hasPermissionForPolicy = |
| 5697 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingPermission( |
| 5698 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5699 | if (!hasPermissionForPolicy) { |
| 5700 | Slog.w(TAG, "Cannot change audio policy ducking handling for pid " + |
| 5701 | + Binder.getCallingPid() + " / uid " |
| 5702 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 5703 | return AudioManager.ERROR; |
| 5704 | } |
| 5705 | |
| 5706 | synchronized (mAudioPolicies) { |
| 5707 | if (!mAudioPolicies.containsKey(pcb.asBinder())) { |
| 5708 | Slog.e(TAG, "Cannot change audio policy focus properties, unregistered policy"); |
| 5709 | return AudioManager.ERROR; |
| 5710 | } |
| 5711 | final AudioPolicyProxy app = mAudioPolicies.get(pcb.asBinder()); |
| 5712 | if (duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5713 | // is there already one policy managing ducking? |
| 5714 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5715 | if (policy.mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5716 | Slog.e(TAG, "Cannot change audio policy ducking behavior, already handled"); |
| 5717 | return AudioManager.ERROR; |
| 5718 | } |
| 5719 | } |
| 5720 | } |
| 5721 | app.mFocusDuckBehavior = duckingBehavior; |
| 5722 | mMediaFocusControl.setDuckingInExtPolicyAvailable( |
| 5723 | duckingBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY); |
| 5724 | } |
| 5725 | return AudioManager.SUCCESS; |
| 5726 | } |
| 5727 | |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5728 | private void dumpAudioPolicies(PrintWriter pw) { |
| 5729 | pw.println("\nAudio policies:"); |
| 5730 | synchronized (mAudioPolicies) { |
| 5731 | for(AudioPolicyProxy policy : mAudioPolicies.values()) { |
| 5732 | pw.println(policy.toLogFriendlyString()); |
| 5733 | } |
| 5734 | } |
| 5735 | } |
| 5736 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 5737 | //====================== |
| 5738 | // Audio policy proxy |
| 5739 | //====================== |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5740 | /** |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5741 | * This internal class inherits from AudioPolicyConfig, each instance contains all the |
| 5742 | * mixes of an AudioPolicy and their configurations. |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5743 | */ |
| 5744 | public class AudioPolicyProxy extends AudioPolicyConfig implements IBinder.DeathRecipient { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5745 | private static final String TAG = "AudioPolicyProxy"; |
| 5746 | AudioPolicyConfig mConfig; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5747 | IAudioPolicyCallback mPolicyToken; |
| 5748 | boolean mHasFocusListener; |
| 5749 | /** |
| 5750 | * Audio focus ducking behavior for an audio policy. |
| 5751 | * This variable reflects the value that was successfully set in |
| 5752 | * {@link AudioService#setFocusPropertiesForPolicy(int, IAudioPolicyCallback)}. This |
| 5753 | * implies that a value of FOCUS_POLICY_DUCKING_IN_POLICY means the corresponding policy |
| 5754 | * is handling ducking for audio focus. |
| 5755 | */ |
| 5756 | int mFocusDuckBehavior = AudioPolicy.FOCUS_POLICY_DUCKING_DEFAULT; |
| 5757 | |
| 5758 | AudioPolicyProxy(AudioPolicyConfig config, IAudioPolicyCallback token, |
| 5759 | boolean hasFocusListener) { |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5760 | super(config); |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5761 | setRegistration(new String(config.hashCode() + ":ap:" + mAudioPolicyCounter++)); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5762 | mPolicyToken = token; |
| 5763 | mHasFocusListener = hasFocusListener; |
| 5764 | if (mHasFocusListener) { |
| 5765 | mMediaFocusControl.addFocusFollower(mPolicyToken); |
| 5766 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5767 | connectMixes(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5768 | } |
| 5769 | |
| 5770 | public void binderDied() { |
| 5771 | synchronized (mAudioPolicies) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5772 | Log.i(TAG, "audio policy " + mPolicyToken + " died"); |
| 5773 | release(); |
| 5774 | mAudioPolicies.remove(mPolicyToken.asBinder()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5775 | } |
| 5776 | } |
| 5777 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5778 | String getRegistrationId() { |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 5779 | return getRegistration(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5780 | } |
| 5781 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 5782 | void release() { |
| 5783 | if (mFocusDuckBehavior == AudioPolicy.FOCUS_POLICY_DUCKING_IN_POLICY) { |
| 5784 | mMediaFocusControl.setDuckingInExtPolicyAvailable(false); |
| 5785 | } |
| 5786 | if (mHasFocusListener) { |
| 5787 | mMediaFocusControl.removeFocusFollower(mPolicyToken); |
| 5788 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5789 | AudioSystem.registerPolicyMixes(mMixes, false); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5790 | } |
| 5791 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 5792 | void connectMixes() { |
| 5793 | AudioSystem.registerPolicyMixes(mMixes, true); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5794 | } |
| 5795 | }; |
| 5796 | |
| 5797 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 5798 | new HashMap<IBinder, AudioPolicyProxy>(); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 5799 | private int mAudioPolicyCounter = 0; // always accessed synchronized on mAudioPolicies |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 5800 | |
| 5801 | private class ControllerService extends ContentObserver { |
| 5802 | private int mUid; |
| 5803 | private ComponentName mComponent; |
| 5804 | |
| 5805 | public ControllerService() { |
| 5806 | super(null); |
| 5807 | } |
| 5808 | |
| 5809 | @Override |
| 5810 | public String toString() { |
| 5811 | return String.format("{mUid=%d,mComponent=%s}", mUid, mComponent); |
| 5812 | } |
| 5813 | |
| 5814 | public void init() { |
| 5815 | onChange(true); |
| 5816 | mContentResolver.registerContentObserver(Settings.Secure.getUriFor( |
| 5817 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT), false, this); |
| 5818 | } |
| 5819 | |
| 5820 | @Override |
| 5821 | public void onChange(boolean selfChange) { |
| 5822 | mUid = 0; |
| 5823 | mComponent = null; |
| 5824 | final String setting = Settings.Secure.getString(mContentResolver, |
| 5825 | Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT); |
| 5826 | if (setting == null) return; |
| 5827 | try { |
| 5828 | mComponent = ComponentName.unflattenFromString(setting); |
| 5829 | if (mComponent == null) return; |
| 5830 | mUid = mContext.getPackageManager() |
| 5831 | .getApplicationInfo(mComponent.getPackageName(), 0).uid; |
| 5832 | } catch (Exception e) { |
| 5833 | Log.w(TAG, "Error loading controller service", e); |
| 5834 | } |
| 5835 | if (DEBUG_VOL) Log.d(TAG, "Reloaded controller service: " + this); |
| 5836 | } |
| 5837 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5838 | } |