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 | |
| 17 | package android.media; |
| 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; |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 28 | import android.app.PendingIntent; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 29 | import android.bluetooth.BluetoothA2dp; |
| 30 | import android.bluetooth.BluetoothAdapter; |
| 31 | import android.bluetooth.BluetoothClass; |
| 32 | import android.bluetooth.BluetoothDevice; |
| 33 | import android.bluetooth.BluetoothHeadset; |
| 34 | import android.bluetooth.BluetoothProfile; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 36 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 40 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 42 | import android.content.res.Configuration; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 43 | import android.content.res.Resources; |
| 44 | import android.content.res.XmlResourceParser; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 45 | import android.database.ContentObserver; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 46 | import android.hardware.hdmi.HdmiControlManager; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 47 | import android.hardware.hdmi.HdmiPlaybackClient; |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 48 | import android.hardware.hdmi.HdmiTvClient; |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 49 | import android.hardware.usb.UsbManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.media.MediaPlayer.OnCompletionListener; |
| 51 | import android.media.MediaPlayer.OnErrorListener; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 52 | import android.media.audiopolicy.AudioPolicyConfig; |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 53 | import android.media.session.MediaSessionLegacyHelper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Binder; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 55 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.Environment; |
| 57 | import android.os.Handler; |
| 58 | import android.os.IBinder; |
| 59 | import android.os.Looper; |
| 60 | import android.os.Message; |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 61 | import android.os.PowerManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 62 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.os.RemoteException; |
| 64 | import android.os.ServiceManager; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 65 | import android.os.SystemClock; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 66 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 68 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.provider.Settings; |
| 70 | import android.provider.Settings.System; |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 71 | import android.telecomm.TelecommManager; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 72 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.util.Log; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 74 | import android.util.MathUtils; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 75 | import android.util.Slog; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 76 | import android.view.KeyEvent; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 77 | import android.view.Surface; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 78 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | import com.android.internal.telephony.ITelephony; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 81 | import com.android.internal.util.XmlUtils; |
| 82 | |
| 83 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 85 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import java.io.IOException; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 87 | import java.io.PrintWriter; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 88 | import java.lang.reflect.Field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import java.util.ArrayList; |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 90 | import java.util.concurrent.ConcurrentHashMap; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 91 | import java.util.Arrays; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 92 | import java.util.HashMap; |
| 93 | import java.util.Iterator; |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 94 | import java.util.List; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 95 | import java.util.Map; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 96 | import java.util.NoSuchElementException; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 97 | import java.util.Objects; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 98 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * The implementation of the volume manager service. |
| 102 | * <p> |
| 103 | * This implementation focuses on delivering a responsive UI. Most methods are |
| 104 | * asynchronous to external calls. For example, the task of setting a volume |
| 105 | * will update our internal state, but in a separate thread will set the system |
| 106 | * volume and later persist to the database. Similarly, setting the ringer mode |
| 107 | * will update the state and broadcast a change and in a separate thread later |
| 108 | * persist the ringer mode. |
| 109 | * |
| 110 | * @hide |
| 111 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 112 | public class AudioService extends IAudioService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
| 114 | private static final String TAG = "AudioService"; |
| 115 | |
Jean-Michel Trivi | 18e7bce | 2011-08-26 12:11:36 -0700 | [diff] [blame] | 116 | /** Debug remote control client/display feature */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 117 | protected static final boolean DEBUG_RC = Log.isLoggable(TAG + ".RC", Log.DEBUG); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 118 | /** Debug volumes */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 119 | 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] | 120 | |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 121 | /** debug calls to media session apis */ |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 122 | private static final boolean DEBUG_SESSIONS = Log.isLoggable(TAG + ".SESSIONS", Log.DEBUG); |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 123 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 124 | /** Allow volume changes to set ringer mode to silent? */ |
| 125 | private static final boolean VOLUME_SETS_RINGER_MODE_SILENT = false; |
| 126 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 127 | /** In silent mode, are volume adjustments (raises) prevented? */ |
| 128 | private static final boolean PREVENT_VOLUME_ADJUSTMENT_IF_SILENT = true; |
| 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | /** How long to delay before persisting a change in volume/ringer mode. */ |
RoboErik | 45edba1 | 2012-03-27 17:54:36 -0700 | [diff] [blame] | 131 | private static final int PERSIST_DELAY = 500; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 133 | /** |
| 134 | * The delay before playing a sound. This small period exists so the user |
| 135 | * can press another key (non-volume keys, too) to have it NOT be audible. |
| 136 | * <p> |
| 137 | * PhoneWindow will implement this part. |
| 138 | */ |
| 139 | public static final int PLAY_SOUND_DELAY = 300; |
| 140 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 141 | /** |
| 142 | * Only used in the result from {@link #checkForRingerModeChange(int, int, int)} |
| 143 | */ |
| 144 | private static final int FLAG_ADJUST_VOLUME = 1; |
| 145 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 146 | private final Context mContext; |
| 147 | private final ContentResolver mContentResolver; |
| 148 | private final AppOpsManager mAppOps; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 149 | |
| 150 | // the platform has no specific capabilities |
| 151 | private static final int PLATFORM_DEFAULT = 0; |
| 152 | // the platform is voice call capable (a phone) |
| 153 | private static final int PLATFORM_VOICE = 1; |
| 154 | // the platform is a television or a set-top box |
| 155 | private static final int PLATFORM_TELEVISION = 2; |
| 156 | // the platform type affects volume and silent mode behavior |
| 157 | private final int mPlatformType; |
| 158 | |
| 159 | private boolean isPlatformVoice() { |
| 160 | return mPlatformType == PLATFORM_VOICE; |
| 161 | } |
| 162 | |
| 163 | private boolean isPlatformTelevision() { |
| 164 | return mPlatformType == PLATFORM_TELEVISION; |
| 165 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 166 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 167 | /** The controller for the volume UI. */ |
| 168 | private final VolumeController mVolumeController = new VolumeController(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
| 170 | // sendMsg() flags |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | /** If the msg is already queued, replace it with this one. */ |
| 172 | private static final int SENDMSG_REPLACE = 0; |
| 173 | /** If the msg is already queued, ignore this one and leave the old. */ |
| 174 | private static final int SENDMSG_NOOP = 1; |
| 175 | /** If the msg is already queued, queue this one and leave the old. */ |
| 176 | private static final int SENDMSG_QUEUE = 2; |
| 177 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 178 | // AudioHandler messages |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 179 | private static final int MSG_SET_DEVICE_VOLUME = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | private static final int MSG_PERSIST_VOLUME = 1; |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 181 | private static final int MSG_PERSIST_MASTER_VOLUME = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | private static final int MSG_PERSIST_RINGER_MODE = 3; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 183 | private static final int MSG_MEDIA_SERVER_DIED = 4; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 184 | private static final int MSG_PLAY_SOUND_EFFECT = 5; |
| 185 | private static final int MSG_BTA2DP_DOCK_TIMEOUT = 6; |
| 186 | private static final int MSG_LOAD_SOUND_EFFECTS = 7; |
| 187 | private static final int MSG_SET_FORCE_USE = 8; |
| 188 | private static final int MSG_BT_HEADSET_CNCT_FAILED = 9; |
| 189 | private static final int MSG_SET_ALL_VOLUMES = 10; |
| 190 | private static final int MSG_PERSIST_MASTER_VOLUME_MUTE = 11; |
| 191 | private static final int MSG_REPORT_NEW_ROUTES = 12; |
| 192 | private static final int MSG_SET_FORCE_BT_A2DP_USE = 13; |
| 193 | private static final int MSG_CHECK_MUSIC_ACTIVE = 14; |
| 194 | private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 15; |
| 195 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 16; |
| 196 | private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 17; |
| 197 | private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 18; |
| 198 | private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19; |
| 199 | private static final int MSG_UNLOAD_SOUND_EFFECTS = 20; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 200 | private static final int MSG_SYSTEM_READY = 21; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 201 | private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 202 | // start of messages handled under wakelock |
| 203 | // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), |
Jean-Michel Trivi | e12c39b | 2012-06-06 10:51:58 -0700 | [diff] [blame] | 204 | // and not with sendMsg(..., ..., SENDMSG_QUEUE, ...) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 205 | private static final int MSG_SET_WIRED_DEVICE_CONNECTION_STATE = 100; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 206 | private static final int MSG_SET_A2DP_SRC_CONNECTION_STATE = 101; |
| 207 | private static final int MSG_SET_A2DP_SINK_CONNECTION_STATE = 102; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 208 | // end of messages handled under wakelock |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 209 | |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 210 | private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 211 | // Timeout for connection to bluetooth headset service |
| 212 | private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | /** @see AudioSystemThread */ |
| 215 | private AudioSystemThread mAudioSystemThread; |
| 216 | /** @see AudioHandler */ |
| 217 | private AudioHandler mAudioHandler; |
| 218 | /** @see VolumeStreamState */ |
| 219 | private VolumeStreamState[] mStreamStates; |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 220 | private SettingsObserver mSettingsObserver; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 221 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 222 | private int mMode = AudioSystem.MODE_NORMAL; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 223 | // protects mRingerMode |
| 224 | private final Object mSettingsLock = new Object(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 225 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | private SoundPool mSoundPool; |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 227 | private final Object mSoundEffectsLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private static final int NUM_SOUNDPOOL_CHANNELS = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 230 | // Internally master volume is a float in the 0.0 - 1.0 range, |
| 231 | // but to support integer based AudioManager API we translate it to 0 - 100 |
| 232 | private static final int MAX_MASTER_VOLUME = 100; |
| 233 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 234 | // Maximum volume adjust steps allowed in a single batch call. |
| 235 | private static final int MAX_BATCH_VOLUME_ADJUST_STEPS = 4; |
| 236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | /* Sound effect file names */ |
| 238 | private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/"; |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 239 | 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] | 240 | |
| 241 | /* Sound effect file name mapping sound effect id (AudioManager.FX_xxx) to |
| 242 | * file index in SOUND_EFFECT_FILES[] (first column) and indicating if effect |
| 243 | * uses soundpool (second column) */ |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 244 | 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] | 245 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 246 | /** @hide Maximum volume index values for audio streams */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 247 | private static final int[] MAX_STREAM_VOLUME = new int[] { |
Eric Laurent | 6ee9952 | 2009-08-25 06:30:59 -0700 | [diff] [blame] | 248 | 5, // STREAM_VOICE_CALL |
| 249 | 7, // STREAM_SYSTEM |
| 250 | 7, // STREAM_RING |
| 251 | 15, // STREAM_MUSIC |
| 252 | 7, // STREAM_ALARM |
| 253 | 7, // STREAM_NOTIFICATION |
| 254 | 15, // STREAM_BLUETOOTH_SCO |
| 255 | 7, // STREAM_SYSTEM_ENFORCED |
| 256 | 15, // STREAM_DTMF |
| 257 | 15 // STREAM_TTS |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 258 | }; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 259 | /* mStreamVolumeAlias[] indicates for each stream if it uses the volume settings |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 260 | * of another stream: This avoids multiplying the volume settings for hidden |
| 261 | * stream types that follow other stream behavior for volume settings |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 262 | * NOTE: do not create loops in aliases! |
| 263 | * 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] | 264 | * use case (in call vs off call...). See updateStreamVolumeAlias() for more details. |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 265 | * mStreamVolumeAlias contains STREAM_VOLUME_ALIAS_VOICE aliases for a voice capable device |
| 266 | * (phone), STREAM_VOLUME_ALIAS_TELEVISION for a television or set-top box and |
| 267 | * STREAM_VOLUME_ALIAS_DEFAULT for other devices (e.g. tablets).*/ |
| 268 | private final int[] STREAM_VOLUME_ALIAS_VOICE = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 269 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 270 | AudioSystem.STREAM_RING, // STREAM_SYSTEM |
| 271 | AudioSystem.STREAM_RING, // STREAM_RING |
| 272 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 273 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 274 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 275 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 276 | AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED |
| 277 | AudioSystem.STREAM_RING, // STREAM_DTMF |
| 278 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 279 | }; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 280 | private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] { |
| 281 | AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALL |
| 282 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 283 | AudioSystem.STREAM_MUSIC, // STREAM_RING |
| 284 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 285 | AudioSystem.STREAM_MUSIC, // STREAM_ALARM |
| 286 | AudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATION |
| 287 | AudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCO |
| 288 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 289 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 290 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 291 | }; |
| 292 | private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 293 | AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL |
| 294 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM |
| 295 | AudioSystem.STREAM_RING, // STREAM_RING |
| 296 | AudioSystem.STREAM_MUSIC, // STREAM_MUSIC |
| 297 | AudioSystem.STREAM_ALARM, // STREAM_ALARM |
| 298 | AudioSystem.STREAM_RING, // STREAM_NOTIFICATION |
| 299 | AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO |
| 300 | AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED |
| 301 | AudioSystem.STREAM_MUSIC, // STREAM_DTMF |
| 302 | AudioSystem.STREAM_MUSIC // STREAM_TTS |
| 303 | }; |
| 304 | private int[] mStreamVolumeAlias; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 305 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Map AudioSystem.STREAM_* constants to app ops. This should be used |
| 308 | * after mapping through mStreamVolumeAlias. |
| 309 | */ |
| 310 | private static final int[] STEAM_VOLUME_OPS = new int[] { |
| 311 | AppOpsManager.OP_AUDIO_VOICE_VOLUME, // STREAM_VOICE_CALL |
| 312 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM |
| 313 | AppOpsManager.OP_AUDIO_RING_VOLUME, // STREAM_RING |
| 314 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_MUSIC |
| 315 | AppOpsManager.OP_AUDIO_ALARM_VOLUME, // STREAM_ALARM |
| 316 | AppOpsManager.OP_AUDIO_NOTIFICATION_VOLUME, // STREAM_NOTIFICATION |
| 317 | AppOpsManager.OP_AUDIO_BLUETOOTH_VOLUME, // STREAM_BLUETOOTH_SCO |
| 318 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_SYSTEM_ENFORCED |
| 319 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_DTMF |
| 320 | AppOpsManager.OP_AUDIO_MEDIA_VOLUME, // STREAM_TTS |
| 321 | }; |
| 322 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 323 | private final boolean mUseFixedVolume; |
| 324 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 325 | // stream names used by dumpStreamStates() |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 326 | private static final String[] STREAM_NAMES = new String[] { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 327 | "STREAM_VOICE_CALL", |
| 328 | "STREAM_SYSTEM", |
| 329 | "STREAM_RING", |
| 330 | "STREAM_MUSIC", |
| 331 | "STREAM_ALARM", |
| 332 | "STREAM_NOTIFICATION", |
| 333 | "STREAM_BLUETOOTH_SCO", |
| 334 | "STREAM_SYSTEM_ENFORCED", |
| 335 | "STREAM_DTMF", |
| 336 | "STREAM_TTS" |
| 337 | }; |
| 338 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 339 | private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | public void onError(int error) { |
| 341 | switch (error) { |
| 342 | case AudioSystem.AUDIO_STATUS_SERVER_DIED: |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 343 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, |
| 344 | SENDMSG_NOOP, 0, 0, null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | /** |
| 353 | * Current ringer mode from one of {@link AudioManager#RINGER_MODE_NORMAL}, |
| 354 | * {@link AudioManager#RINGER_MODE_SILENT}, or |
| 355 | * {@link AudioManager#RINGER_MODE_VIBRATE}. |
| 356 | */ |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 357 | // protected by mSettingsLock |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | private int mRingerMode; |
| 359 | |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 360 | /** @see System#MODE_RINGER_STREAMS_AFFECTED */ |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 361 | private int mRingerModeAffectedStreams = 0; |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 362 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 363 | // Streams currently muted by ringer mode |
| 364 | private int mRingerModeMutedStreams; |
| 365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | /** @see System#MUTE_STREAMS_AFFECTED */ |
| 367 | private int mMuteAffectedStreams; |
| 368 | |
| 369 | /** |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 370 | * NOTE: setVibrateSetting(), getVibrateSetting(), shouldVibrate() are deprecated. |
| 371 | * mVibrateSetting is just maintained during deprecation period but vibration policy is |
| 372 | * now only controlled by mHasVibrator and mRingerMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | */ |
| 374 | private int mVibrateSetting; |
| 375 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 376 | // Is there a vibrator |
| 377 | private final boolean mHasVibrator; |
| 378 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 379 | // Broadcast receiver for device connections intent broadcasts |
| 380 | private final BroadcastReceiver mReceiver = new AudioServiceBroadcastReceiver(); |
| 381 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 382 | // Devices currently connected |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 383 | private final HashMap <Integer, String> mConnectedDevices = new HashMap <Integer, String>(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 384 | |
| 385 | // Forced device usage for communications |
| 386 | private int mForcedUseForComm; |
| 387 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 388 | // True if we have master volume support |
| 389 | private final boolean mUseMasterVolume; |
| 390 | |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 391 | private final int[] mMasterVolumeRamp; |
| 392 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 393 | // List of binder death handlers for setMode() client processes. |
| 394 | // 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] | 395 | private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>(); |
Eric Laurent | eb14a78 | 2009-12-17 03:12:59 -0800 | [diff] [blame] | 396 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 397 | // List of clients having issued a SCO start request |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 398 | private final ArrayList <ScoClient> mScoClients = new ArrayList <ScoClient>(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 399 | |
| 400 | // BluetoothHeadset API to control SCO connection |
| 401 | private BluetoothHeadset mBluetoothHeadset; |
| 402 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 403 | // Bluetooth headset device |
| 404 | private BluetoothDevice mBluetoothHeadsetDevice; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 405 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 406 | // Indicate if SCO audio connection is currently active and if the initiator is |
| 407 | // audio service (internal) or bluetooth headset (external) |
| 408 | private int mScoAudioState; |
| 409 | // SCO audio state is not active |
| 410 | private static final int SCO_STATE_INACTIVE = 0; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 411 | // SCO audio activation request waiting for headset service to connect |
| 412 | private static final int SCO_STATE_ACTIVATE_REQ = 1; |
Eric Laurent | 25fc29b | 2013-04-05 12:13:54 -0700 | [diff] [blame] | 413 | // 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] | 414 | private static final int SCO_STATE_ACTIVE_INTERNAL = 3; |
| 415 | // SCO audio deactivation request waiting for headset service to connect |
| 416 | private static final int SCO_STATE_DEACTIVATE_REQ = 5; |
| 417 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 418 | // SCO audio state is active due to an action in BT handsfree (either voice recognition or |
| 419 | // in call audio) |
| 420 | private static final int SCO_STATE_ACTIVE_EXTERNAL = 2; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 421 | // Deactivation request for all SCO connections (initiated by audio mode change) |
| 422 | // waiting for headset service to connect |
| 423 | private static final int SCO_STATE_DEACTIVATE_EXT_REQ = 4; |
| 424 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 425 | // Indicates the mode used for SCO audio connection. The mode is virtual call if the request |
| 426 | // originated from an app targeting an API version before JB MR2 and raw audio after that. |
| 427 | private int mScoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 428 | // SCO audio mode is undefined |
| 429 | private static final int SCO_MODE_UNDEFINED = -1; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 430 | // SCO audio mode is virtual voice call (BluetoothHeadset.startScoUsingVirtualVoiceCall()) |
| 431 | private static final int SCO_MODE_VIRTUAL_CALL = 0; |
| 432 | // SCO audio mode is raw audio (BluetoothHeadset.connectAudio()) |
| 433 | private static final int SCO_MODE_RAW = 1; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 434 | // SCO audio mode is Voice Recognition (BluetoothHeadset.startVoiceRecognition()) |
| 435 | private static final int SCO_MODE_VR = 2; |
| 436 | |
| 437 | private static final int SCO_MODE_MAX = 2; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 438 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 439 | // Current connection state indicated by bluetooth headset |
| 440 | private int mScoConnectionState; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 441 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 442 | // true if boot sequence has been completed |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 443 | private boolean mSystemReady; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 444 | // listener for SoundPool sample load completion indication |
| 445 | private SoundPoolCallback mSoundPoolCallBack; |
| 446 | // thread for SoundPool listener |
| 447 | private SoundPoolListenerThread mSoundPoolListenerThread; |
| 448 | // message looper for SoundPool listener |
| 449 | private Looper mSoundPoolLooper = null; |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 450 | // volume applied to sound played with playSoundEffect() |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 451 | private static int sSoundEffectVolumeDb; |
Eric Laurent | 9903e26 | 2012-09-21 18:10:32 -0700 | [diff] [blame] | 452 | // getActiveStreamType() will return: |
| 453 | // - STREAM_NOTIFICATION on tablets during this period after a notification stopped |
| 454 | // - STREAM_MUSIC on phones during this period after music or talkback/voice search prompt |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 455 | // stopped |
Eric Laurent | 9903e26 | 2012-09-21 18:10:32 -0700 | [diff] [blame] | 456 | private static final int DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS = 5000; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 457 | // previous volume adjustment direction received by checkForRingerModeChange() |
| 458 | private int mPrevVolDirection = AudioManager.ADJUST_SAME; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 459 | // Keyguard manager proxy |
| 460 | private KeyguardManager mKeyguardManager; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 461 | // mVolumeControlStream is set by VolumePanel to temporarily force the stream type which volume |
| 462 | // is controlled by Vol keys. |
| 463 | private int mVolumeControlStream = -1; |
| 464 | private final Object mForceControlStreamLock = new Object(); |
| 465 | // VolumePanel is currently the only client of forceVolumeControlStream() and runs in system |
| 466 | // server process so in theory it is not necessary to monitor the client death. |
| 467 | // However it is good to be ready for future evolutions. |
| 468 | private ForceControlStreamClient mForceControlStreamClient = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 469 | // Used to play ringtones outside system_server |
| 470 | private volatile IRingtonePlayer mRingtonePlayer; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 471 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 472 | private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 473 | private int mDeviceRotation = Surface.ROTATION_0; |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 474 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 475 | // Request to override default use of A2DP for media. |
| 476 | private boolean mBluetoothA2dpEnabled; |
| 477 | private final Object mBluetoothA2dpEnabledLock = new Object(); |
| 478 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 479 | // Monitoring of audio routes. Protected by mCurAudioRoutes. |
| 480 | final AudioRoutesInfo mCurAudioRoutes = new AudioRoutesInfo(); |
| 481 | final RemoteCallbackList<IAudioRoutesObserver> mRoutesObservers |
| 482 | = new RemoteCallbackList<IAudioRoutesObserver>(); |
| 483 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 484 | /** |
| 485 | * A fake stream type to match the notion of remote media playback |
| 486 | */ |
| 487 | public final static int STREAM_REMOTE_MUSIC = -200; |
| 488 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 489 | // 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] | 490 | int mFixedVolumeDevices = AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 491 | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 492 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | |
| 493 | AudioSystem.DEVICE_OUT_HDMI_ARC | |
| 494 | AudioSystem.DEVICE_OUT_SPDIF | |
| 495 | AudioSystem.DEVICE_OUT_AUX_LINE; |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 496 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 497 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 498 | private final boolean mMonitorOrientation; |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 499 | private final boolean mMonitorRotation; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 500 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 501 | private boolean mDockAudioMediaEnabled = true; |
| 502 | |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 503 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 504 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 505 | // Used when safe volume warning message display is requested by setStreamVolume(). In this |
| 506 | // case, the new requested volume, stream type and device are stored in mPendingVolumeCommand |
| 507 | // and used later when/if disableSafeMediaVolume() is called. |
| 508 | private StreamVolumeCommand mPendingVolumeCommand; |
| 509 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 510 | private PowerManager.WakeLock mAudioEventWakeLock; |
| 511 | |
| 512 | private final MediaFocusControl mMediaFocusControl; |
| 513 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 514 | // Reference to BluetoothA2dp to query for AbsoluteVolume. |
| 515 | private BluetoothA2dp mA2dp; |
| 516 | private final Object mA2dpAvrcpLock = new Object(); |
| 517 | // If absolute volume is supported in AVRCP device |
| 518 | private boolean mAvrcpAbsVolSupported = false; |
| 519 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | /////////////////////////////////////////////////////////////////////////// |
| 521 | // Construction |
| 522 | /////////////////////////////////////////////////////////////////////////// |
| 523 | |
| 524 | /** @hide */ |
| 525 | public AudioService(Context context) { |
| 526 | mContext = context; |
| 527 | mContentResolver = context.getContentResolver(); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 528 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 529 | |
| 530 | if (mContext.getResources().getBoolean( |
| 531 | com.android.internal.R.bool.config_voice_capable)) { |
| 532 | mPlatformType = PLATFORM_VOICE; |
| 533 | } else if (context.getPackageManager().hasSystemFeature( |
| 534 | PackageManager.FEATURE_TELEVISION)) { |
| 535 | mPlatformType = PLATFORM_TELEVISION; |
| 536 | } else { |
| 537 | mPlatformType = PLATFORM_DEFAULT; |
| 538 | } |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 539 | |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 540 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 541 | mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent"); |
Jean-Michel Trivi | c680222 | 2012-04-30 11:15:03 -0700 | [diff] [blame] | 542 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 543 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); |
| 544 | mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); |
| 545 | |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 546 | // Intialized volume |
| 547 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = SystemProperties.getInt( |
| 548 | "ro.config.vc_call_vol_steps", |
| 549 | MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]); |
| 550 | |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 551 | sSoundEffectVolumeDb = context.getResources().getInteger( |
Jean-Michel Trivi | c55b393 | 2012-06-05 11:57:59 -0700 | [diff] [blame] | 552 | com.android.internal.R.integer.config_soundEffectVolumeDb); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 553 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 554 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 555 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | createAudioSystemThread(); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 557 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 558 | mMediaFocusControl = new MediaFocusControl(mAudioHandler.getLooper(), |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 559 | mContext, mVolumeController, this); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 560 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 561 | AudioSystem.setErrorCallback(mAudioSystemCallback); |
| 562 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 563 | boolean cameraSoundForced = mContext.getResources().getBoolean( |
| 564 | com.android.internal.R.bool.config_camera_sound_forced); |
| 565 | mCameraSoundForced = new Boolean(cameraSoundForced); |
| 566 | sendMsg(mAudioHandler, |
| 567 | MSG_SET_FORCE_USE, |
| 568 | SENDMSG_QUEUE, |
| 569 | AudioSystem.FOR_SYSTEM, |
| 570 | cameraSoundForced ? |
| 571 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 572 | null, |
| 573 | 0); |
| 574 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 575 | mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver, |
| 576 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 577 | SAFE_MEDIA_VOLUME_NOT_CONFIGURED)); |
| 578 | // The default safe volume index read here will be replaced by the actual value when |
| 579 | // the mcc is read by onConfigureSafeVolume() |
| 580 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 581 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
| 582 | |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 583 | mUseFixedVolume = mContext.getResources().getBoolean( |
| 584 | com.android.internal.R.bool.config_useFixedVolume); |
| 585 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 586 | // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[] |
| 587 | // array initialized by updateStreamVolumeAlias() |
| 588 | updateStreamVolumeAlias(false /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | readPersistedSettings(); |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 590 | mSettingsObserver = new SettingsObserver(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 591 | createStreamStates(); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 592 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 593 | readAndSetLowRamDevice(); |
Eric Laurent | 3891c4c | 2010-04-20 09:40:57 -0700 | [diff] [blame] | 594 | |
| 595 | // Call setRingerModeInt() to apply correct mute |
| 596 | // state on streams affected by ringer mode. |
| 597 | mRingerModeMutedStreams = 0; |
| 598 | setRingerModeInt(getRingerMode(), false); |
| 599 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 600 | // Register for device connection intent broadcasts. |
| 601 | IntentFilter intentFilter = |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 602 | new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 603 | intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 604 | intentFilter.addAction(Intent.ACTION_DOCK_EVENT); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 605 | intentFilter.addAction(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG); |
| 606 | intentFilter.addAction(Intent.ACTION_USB_AUDIO_DEVICE_PLUG); |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 607 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 608 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 609 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 610 | intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 611 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 612 | intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 613 | // TODO merge orientation and rotation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 614 | mMonitorOrientation = SystemProperties.getBoolean("ro.audio.monitorOrientation", false); |
| 615 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 616 | Log.v(TAG, "monitoring device orientation"); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 617 | // initialize orientation in AudioSystem |
| 618 | setOrientationForAudioSystem(); |
| 619 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 620 | mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false); |
| 621 | if (mMonitorRotation) { |
| 622 | mDeviceRotation = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 623 | .getDefaultDisplay().getRotation(); |
| 624 | Log.v(TAG, "monitoring device rotation, initial=" + mDeviceRotation); |
| 625 | // initialize rotation in AudioSystem |
| 626 | setRotationForAudioSystem(); |
| 627 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 628 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 629 | context.registerReceiver(mReceiver, intentFilter); |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 630 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 631 | mUseMasterVolume = context.getResources().getBoolean( |
| 632 | com.android.internal.R.bool.config_useMasterVolume); |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 633 | restoreMasterVolume(); |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 634 | |
| 635 | mMasterVolumeRamp = context.getResources().getIntArray( |
| 636 | com.android.internal.R.array.config_masterVolumeRamp); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 637 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 640 | public void systemReady() { |
| 641 | sendMsg(mAudioHandler, MSG_SYSTEM_READY, SENDMSG_QUEUE, |
| 642 | 0, 0, null, 0); |
| 643 | } |
| 644 | |
| 645 | public void onSystemReady() { |
| 646 | mSystemReady = true; |
| 647 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, |
| 648 | 0, 0, null, 0); |
| 649 | |
| 650 | mKeyguardManager = |
| 651 | (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 652 | mScoConnectionState = AudioManager.SCO_AUDIO_STATE_ERROR; |
| 653 | resetBluetoothSco(); |
| 654 | getBluetoothHeadset(); |
| 655 | //FIXME: this is to maintain compatibility with deprecated intent |
| 656 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
| 657 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
| 658 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, |
| 659 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 660 | sendStickyBroadcastToAll(newIntent); |
| 661 | |
| 662 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 663 | if (adapter != null) { |
| 664 | adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 665 | BluetoothProfile.A2DP); |
| 666 | } |
| 667 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 668 | mHdmiManager = |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 669 | (HdmiControlManager) mContext.getSystemService(Context.HDMI_CONTROL_SERVICE); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 670 | if (mHdmiManager != null) { |
| 671 | synchronized (mHdmiManager) { |
| 672 | mHdmiTvClient = mHdmiManager.getTvClient(); |
| 673 | mHdmiPlaybackClient = mHdmiManager.getPlaybackClient(); |
| 674 | mHdmiCecSink = false; |
| 675 | } |
| 676 | } |
Wonsik Kim | 7f4342e | 2014-07-20 23:04:59 +0900 | [diff] [blame] | 677 | |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 678 | sendMsg(mAudioHandler, |
| 679 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED, |
| 680 | SENDMSG_REPLACE, |
| 681 | 0, |
| 682 | 0, |
| 683 | null, |
| 684 | SAFE_VOLUME_CONFIGURE_TIMEOUT_MS); |
| 685 | } |
| 686 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | private void createAudioSystemThread() { |
| 688 | mAudioSystemThread = new AudioSystemThread(); |
| 689 | mAudioSystemThread.start(); |
| 690 | waitForAudioHandlerCreation(); |
| 691 | } |
| 692 | |
| 693 | /** Waits for the volume handler to be created by the other thread. */ |
| 694 | private void waitForAudioHandlerCreation() { |
| 695 | synchronized(this) { |
| 696 | while (mAudioHandler == null) { |
| 697 | try { |
| 698 | // Wait for mAudioHandler to be set by the other thread |
| 699 | wait(); |
| 700 | } catch (InterruptedException e) { |
| 701 | Log.e(TAG, "Interrupted while waiting on volume handler."); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 707 | private void checkAllAliasStreamVolumes() { |
| 708 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 709 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 710 | if (streamType != mStreamVolumeAlias[streamType]) { |
| 711 | mStreamStates[streamType]. |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 712 | setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]]); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 713 | } |
| 714 | // apply stream volume |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 715 | if (!mStreamStates[streamType].isMuted()) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 716 | mStreamStates[streamType].applyAllVolumes(); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 721 | private void checkAllFixedVolumeDevices() |
| 722 | { |
| 723 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 724 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 725 | mStreamStates[streamType].checkFixedVolumeDevices(); |
| 726 | } |
| 727 | } |
| 728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | private void createStreamStates() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 731 | VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes]; |
| 732 | |
| 733 | for (int i = 0; i < numStreamTypes; i++) { |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 734 | streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 735 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 737 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 738 | checkAllAliasStreamVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 741 | private void dumpStreamStates(PrintWriter pw) { |
| 742 | pw.println("\nStream volumes (device: index)"); |
| 743 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 744 | for (int i = 0; i < numStreamTypes; i++) { |
| 745 | pw.println("- "+STREAM_NAMES[i]+":"); |
| 746 | mStreamStates[i].dump(pw); |
| 747 | pw.println(""); |
| 748 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 749 | pw.print("\n- mute affected streams = 0x"); |
| 750 | pw.println(Integer.toHexString(mMuteAffectedStreams)); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 751 | } |
| 752 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 753 | /** @hide */ |
| 754 | public static String streamToString(int stream) { |
| 755 | if (stream >= 0 && stream < STREAM_NAMES.length) return STREAM_NAMES[stream]; |
| 756 | if (stream == AudioManager.USE_DEFAULT_STREAM_TYPE) return "USE_DEFAULT_STREAM_TYPE"; |
| 757 | return "UNKNOWN_STREAM_" + stream; |
| 758 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 759 | |
| 760 | private void updateStreamVolumeAlias(boolean updateVolumes) { |
| 761 | int dtmfStreamAlias; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 762 | |
| 763 | switch (mPlatformType) { |
| 764 | case PLATFORM_VOICE: |
| 765 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 766 | dtmfStreamAlias = AudioSystem.STREAM_RING; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 767 | break; |
| 768 | case PLATFORM_TELEVISION: |
| 769 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION; |
| 770 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 771 | break; |
| 772 | default: |
| 773 | mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 774 | dtmfStreamAlias = AudioSystem.STREAM_MUSIC; |
| 775 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 776 | |
| 777 | if (isPlatformTelevision()) { |
| 778 | mRingerModeAffectedStreams = 0; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 779 | } else { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 780 | if (isInCommunication()) { |
| 781 | dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL; |
| 782 | mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 783 | } else { |
| 784 | mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 785 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 786 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 787 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 788 | mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; |
| 789 | if (updateVolumes) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 790 | mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias]); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 791 | // apply stream mute states according to new value of mRingerModeAffectedStreams |
| 792 | setRingerModeInt(getRingerMode(), false); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 793 | sendMsg(mAudioHandler, |
| 794 | MSG_SET_ALL_VOLUMES, |
| 795 | SENDMSG_QUEUE, |
| 796 | 0, |
| 797 | 0, |
| 798 | mStreamStates[AudioSystem.STREAM_DTMF], 0); |
| 799 | } |
| 800 | } |
| 801 | |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 802 | private void readDockAudioSettings(ContentResolver cr) |
| 803 | { |
| 804 | mDockAudioMediaEnabled = Settings.Global.getInt( |
Eric Laurent | 5ba0ffa0 | 2012-10-29 12:31:09 -0700 | [diff] [blame] | 805 | cr, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 806 | |
| 807 | if (mDockAudioMediaEnabled) { |
| 808 | mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 809 | } else { |
| 810 | mBecomingNoisyIntentDevices &= ~AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
| 811 | } |
| 812 | |
| 813 | sendMsg(mAudioHandler, |
| 814 | MSG_SET_FORCE_USE, |
| 815 | SENDMSG_QUEUE, |
| 816 | AudioSystem.FOR_DOCK, |
| 817 | mDockAudioMediaEnabled ? |
| 818 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, |
| 819 | null, |
| 820 | 0); |
| 821 | } |
| 822 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | private void readPersistedSettings() { |
| 824 | final ContentResolver cr = mContentResolver; |
| 825 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 826 | int ringerModeFromSettings = |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 827 | Settings.Global.getInt( |
| 828 | cr, Settings.Global.MODE_RINGER, AudioManager.RINGER_MODE_NORMAL); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 829 | int ringerMode = ringerModeFromSettings; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 830 | // sanity check in case the settings are restored from a device with incompatible |
| 831 | // ringer modes |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 832 | if (!AudioManager.isValidRingerMode(ringerMode)) { |
| 833 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 834 | } |
| 835 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 836 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 837 | } |
| 838 | if (ringerMode != ringerModeFromSettings) { |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 839 | Settings.Global.putInt(cr, Settings.Global.MODE_RINGER, ringerMode); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 840 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 841 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 842 | ringerMode = AudioManager.RINGER_MODE_NORMAL; |
| 843 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 844 | synchronized(mSettingsLock) { |
| 845 | mRingerMode = ringerMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 847 | // System.VIBRATE_ON is not used any more but defaults for mVibrateSetting |
| 848 | // are still needed while setVibrateSetting() and getVibrateSetting() are being |
| 849 | // deprecated. |
| 850 | mVibrateSetting = getValueForVibrateSetting(0, |
| 851 | AudioManager.VIBRATE_TYPE_NOTIFICATION, |
| 852 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 853 | : AudioManager.VIBRATE_SETTING_OFF); |
| 854 | mVibrateSetting = getValueForVibrateSetting(mVibrateSetting, |
| 855 | AudioManager.VIBRATE_TYPE_RINGER, |
| 856 | mHasVibrator ? AudioManager.VIBRATE_SETTING_ONLY_SILENT |
| 857 | : AudioManager.VIBRATE_SETTING_OFF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 859 | updateRingerModeAffectedStreams(); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 860 | readDockAudioSettings(cr); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 861 | } |
Eric Laurent | c1d4166 | 2011-07-19 11:21:13 -0700 | [diff] [blame] | 862 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 863 | mMuteAffectedStreams = System.getIntForUser(cr, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | System.MUTE_STREAMS_AFFECTED, |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 865 | ((1 << AudioSystem.STREAM_MUSIC)| |
| 866 | (1 << AudioSystem.STREAM_RING)| |
| 867 | (1 << AudioSystem.STREAM_SYSTEM)), |
| 868 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 870 | boolean masterMute = System.getIntForUser(cr, System.VOLUME_MASTER_MUTE, |
| 871 | 0, UserHandle.USER_CURRENT) == 1; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 872 | if (mUseFixedVolume) { |
| 873 | masterMute = false; |
| 874 | AudioSystem.setMasterVolume(1.0f); |
| 875 | } |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 876 | AudioSystem.setMasterMute(masterMute); |
| 877 | broadcastMasterMuteStatus(masterMute); |
| 878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | // Each stream will read its own persisted settings |
| 880 | |
| 881 | // Broadcast the sticky intent |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 882 | broadcastRingerMode(ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | |
| 884 | // Broadcast vibrate settings |
| 885 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 886 | broadcastVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION); |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 887 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 888 | // Load settings for the volume controller |
| 889 | mVolumeController.loadSettings(cr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 892 | private int rescaleIndex(int index, int srcStream, int dstStream) { |
| 893 | return (index * mStreamStates[dstStream].getMaxIndex() + mStreamStates[srcStream].getMaxIndex() / 2) / mStreamStates[srcStream].getMaxIndex(); |
| 894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | |
| 896 | /////////////////////////////////////////////////////////////////////////// |
| 897 | // IPC methods |
| 898 | /////////////////////////////////////////////////////////////////////////// |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 899 | /** @see AudioManager#isLocalOrRemoteMusicActive() */ |
| 900 | public boolean isLocalOrRemoteMusicActive() { |
| 901 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0)) { |
| 902 | // local / wired / BT playback active |
| 903 | if (DEBUG_VOL) Log.d(TAG, "isLocalOrRemoteMusicActive(): local"); |
| 904 | return true; |
| 905 | } |
| 906 | if (mMediaFocusControl.checkUpdateRemoteStateIfActive(AudioSystem.STREAM_MUSIC)) { |
| 907 | // remote "cast-like" playback active |
| 908 | if (DEBUG_VOL) Log.d(TAG, "isLocalOrRemoteMusicActive(): has PLAYBACK_TYPE_REMOTE"); |
| 909 | return true; |
| 910 | } |
| 911 | if (AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, 0)) { |
| 912 | // remote submix playback active |
| 913 | if (DEBUG_VOL) Log.d(TAG, "isLocalOrRemoteMusicActive(): remote submix"); |
| 914 | return true; |
| 915 | } |
| 916 | if (DEBUG_VOL) Log.d(TAG, "isLocalOrRemoteMusicActive(): no"); |
| 917 | return false; |
| 918 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | |
| 920 | /** @see AudioManager#adjustVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 921 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags, |
| 922 | String callingPackage) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 923 | if (DEBUG_VOL) Log.d(TAG, "adjustSuggestedStreamVolume() stream="+suggestedStreamType |
| 924 | + ", flags=" + flags); |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 925 | int streamType; |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 926 | if (mVolumeControlStream != -1) { |
| 927 | streamType = mVolumeControlStream; |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 928 | } else { |
| 929 | streamType = getActiveStreamType(suggestedStreamType); |
| 930 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 931 | final int resolvedStream = mStreamVolumeAlias[streamType]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | |
John Spurlock | 6bd096c | 2014-07-10 09:39:04 -0400 | [diff] [blame] | 933 | // Play sounds on STREAM_RING and STREAM_REMOTE_MUSIC only. |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 934 | if ((streamType != STREAM_REMOTE_MUSIC) && |
| 935 | (flags & AudioManager.FLAG_PLAY_SOUND) != 0 && |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 936 | resolvedStream != AudioSystem.STREAM_RING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 938 | } |
| 939 | |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 940 | // For notifications/ring, show the ui before making any adjustments |
| 941 | if (mVolumeController.suppressAdjustment(resolvedStream, flags)) { |
| 942 | direction = 0; |
| 943 | flags &= ~AudioManager.FLAG_PLAY_SOUND; |
| 944 | flags &= ~AudioManager.FLAG_VIBRATE; |
| 945 | if (DEBUG_VOL) Log.d(TAG, "Volume controller suppressed adjustment"); |
| 946 | } |
| 947 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 948 | if (streamType == STREAM_REMOTE_MUSIC) { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 949 | // TODO bounce it to MediaSessionService to find an appropriate |
| 950 | // session |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 951 | } else { |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 952 | adjustStreamVolume(streamType, direction, flags, callingPackage); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /** @see AudioManager#adjustStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 957 | public void adjustStreamVolume(int streamType, int direction, int flags, |
| 958 | String callingPackage) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 959 | if (mUseFixedVolume) { |
| 960 | return; |
| 961 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 962 | if (DEBUG_VOL) Log.d(TAG, "adjustStreamVolume() stream="+streamType+", dir="+direction |
| 963 | + ", flags="+flags); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | ensureValidDirection(direction); |
| 966 | ensureValidStreamType(streamType); |
| 967 | |
Eric Laurent | 96a33d1 | 2011-11-08 10:31:57 -0800 | [diff] [blame] | 968 | // use stream type alias here so that streams with same alias have the same behavior, |
| 969 | // including with regard to silent mode control (e.g the use of STREAM_RING below and in |
| 970 | // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION) |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 971 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 972 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 973 | |
| 974 | final int device = getDeviceForStream(streamTypeAlias); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 975 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 976 | int aliasIndex = streamState.getIndex(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | boolean adjustVolume = true; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 978 | int step; |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 979 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 980 | // skip a2dp absolute volume control request when the device |
| 981 | // is not an a2dp device |
| 982 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 983 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 984 | return; |
| 985 | } |
| 986 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 987 | if (mAppOps.noteOp(STEAM_VOLUME_OPS[streamTypeAlias], Binder.getCallingUid(), |
| 988 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 989 | return; |
| 990 | } |
| 991 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 992 | // reset any pending volume command |
| 993 | synchronized (mSafeMediaVolumeState) { |
| 994 | mPendingVolumeCommand = null; |
| 995 | } |
| 996 | |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 997 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
| 998 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
| 999 | ((device & mFixedVolumeDevices) != 0)) { |
| 1000 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1001 | |
| 1002 | // Always toggle between max safe volume and 0 for fixed volume devices where safe |
| 1003 | // volume is enforced, and max and 0 for the others. |
| 1004 | // This is simulated by stepping by the full allowed volume range |
| 1005 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1006 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1007 | step = mSafeMediaVolumeIndex; |
| 1008 | } else { |
| 1009 | step = streamState.getMaxIndex(); |
| 1010 | } |
| 1011 | if (aliasIndex != 0) { |
| 1012 | aliasIndex = step; |
| 1013 | } |
| 1014 | } else { |
| 1015 | // convert one UI step (+/-1) into a number of internal units on the stream alias |
| 1016 | step = rescaleIndex(10, streamType, streamTypeAlias); |
| 1017 | } |
| 1018 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1019 | // If either the client forces allowing ringer modes for this adjustment, |
| 1020 | // or the stream type is one that is affected by ringer modes |
| 1021 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1022 | (streamTypeAlias == getMasterStreamType())) { |
| 1023 | int ringerMode = getRingerMode(); |
| 1024 | // do not vibrate if already in vibrate mode |
| 1025 | if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { |
| 1026 | flags &= ~AudioManager.FLAG_VIBRATE; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1027 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1028 | // Check if the ringer mode changes with this volume adjustment. If |
| 1029 | // it does, it will handle adjusting the volume, so we won't below |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 1030 | final int result = checkForRingerModeChange(aliasIndex, direction, step); |
| 1031 | adjustVolume = (result & FLAG_ADJUST_VOLUME) != 0; |
| 1032 | // If suppressing a volume adjustment in silent mode, display the UI hint |
| 1033 | if ((result & AudioManager.FLAG_SHOW_SILENT_HINT) != 0) { |
| 1034 | flags |= AudioManager.FLAG_SHOW_SILENT_HINT; |
| 1035 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1036 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1037 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1038 | int oldIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1039 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1040 | if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1041 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1042 | // Check if volume update should be send to AVRCP |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1043 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1044 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1045 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1046 | synchronized (mA2dpAvrcpLock) { |
| 1047 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
| 1048 | mA2dp.adjustAvrcpAbsoluteVolume(direction); |
| 1049 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1052 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1053 | if ((direction == AudioManager.ADJUST_RAISE) && |
| 1054 | !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { |
| 1055 | Log.e(TAG, "adjustStreamVolume() safe volume index = "+oldIndex); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1056 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1057 | } else if (streamState.adjustIndex(direction * step, device)) { |
| 1058 | // Post message to set system volume (it in turn will post a message |
| 1059 | // to persist). Do not change volume if stream is muted. |
| 1060 | sendMsg(mAudioHandler, |
| 1061 | MSG_SET_DEVICE_VOLUME, |
| 1062 | SENDMSG_QUEUE, |
| 1063 | device, |
| 1064 | 0, |
| 1065 | streamState, |
| 1066 | 0); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1067 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1068 | |
| 1069 | // Check if volume update should be send to Hdmi system audio. |
| 1070 | int newIndex = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1071 | if (mHdmiManager != null) { |
| 1072 | synchronized (mHdmiManager) { |
| 1073 | if (mHdmiTvClient != null && |
| 1074 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1075 | (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0 && |
| 1076 | oldIndex != newIndex) { |
| 1077 | int maxIndex = getStreamMaxVolume(streamType); |
| 1078 | synchronized (mHdmiTvClient) { |
| 1079 | if (mHdmiSystemAudioSupported) { |
| 1080 | mHdmiTvClient.setSystemAudioVolume( |
| 1081 | (oldIndex + 5) / 10, (newIndex + 5) / 10, maxIndex); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | // mHdmiCecSink true => mHdmiPlaybackClient != null |
| 1086 | if (mHdmiCecSink && |
| 1087 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1088 | oldIndex != newIndex) { |
| 1089 | synchronized (mHdmiPlaybackClient) { |
| 1090 | int keyCode = (direction == -1) ? KeyEvent.KEYCODE_VOLUME_DOWN : |
| 1091 | KeyEvent.KEYCODE_VOLUME_UP; |
| 1092 | mHdmiPlaybackClient.sendKeyEvent(keyCode, true); |
| 1093 | mHdmiPlaybackClient.sendKeyEvent(keyCode, false); |
| 1094 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | } |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1098 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1099 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1100 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 1103 | /** @see AudioManager#adjustMasterVolume(int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1104 | public void adjustMasterVolume(int steps, int flags, String callingPackage) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1105 | if (mUseFixedVolume) { |
| 1106 | return; |
| 1107 | } |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1108 | ensureValidSteps(steps); |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 1109 | int volume = Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME); |
| 1110 | int delta = 0; |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1111 | int numSteps = Math.abs(steps); |
| 1112 | int direction = steps > 0 ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER; |
| 1113 | for (int i = 0; i < numSteps; ++i) { |
| 1114 | delta = findVolumeDelta(direction, volume); |
| 1115 | volume += delta; |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1116 | } |
RoboErik | 24b082f | 2012-02-24 14:21:16 -0800 | [diff] [blame] | 1117 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1118 | //Log.d(TAG, "adjustMasterVolume volume: " + volume + " steps: " + steps); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1119 | setMasterVolume(volume, flags, callingPackage); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1120 | } |
| 1121 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1122 | // StreamVolumeCommand contains the information needed to defer the process of |
| 1123 | // setStreamVolume() in case the user has to acknowledge the safe volume warning message. |
| 1124 | class StreamVolumeCommand { |
| 1125 | public final int mStreamType; |
| 1126 | public final int mIndex; |
| 1127 | public final int mFlags; |
| 1128 | public final int mDevice; |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 1129 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1130 | StreamVolumeCommand(int streamType, int index, int flags, int device) { |
| 1131 | mStreamType = streamType; |
| 1132 | mIndex = index; |
| 1133 | mFlags = flags; |
| 1134 | mDevice = device; |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1135 | } |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 1136 | |
| 1137 | @Override |
| 1138 | public String toString() { |
| 1139 | return new StringBuilder().append("{streamType=").append(mStreamType).append(",index=") |
| 1140 | .append(mIndex).append(",flags=").append(mFlags).append(",device=") |
| 1141 | .append(mDevice).append('}').toString(); |
| 1142 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1143 | }; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1144 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1145 | private void onSetStreamVolume(int streamType, int index, int flags, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1146 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false); |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1147 | // setting volume on master stream type also controls silent mode |
| 1148 | if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || |
| 1149 | (mStreamVolumeAlias[streamType] == getMasterStreamType())) { |
| 1150 | int newRingerMode; |
| 1151 | if (index == 0) { |
| 1152 | newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1153 | : VOLUME_SETS_RINGER_MODE_SILENT ? AudioManager.RINGER_MODE_SILENT |
| 1154 | : AudioManager.RINGER_MODE_NORMAL; |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1155 | } else { |
| 1156 | newRingerMode = AudioManager.RINGER_MODE_NORMAL; |
| 1157 | } |
| 1158 | setRingerMode(newRingerMode); |
| 1159 | } |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /** @see AudioManager#setStreamVolume(int, int, int) */ |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1163 | public void setStreamVolume(int streamType, int index, int flags, String callingPackage) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1164 | if (mUseFixedVolume) { |
| 1165 | return; |
| 1166 | } |
| 1167 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1168 | ensureValidStreamType(streamType); |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1169 | int streamTypeAlias = mStreamVolumeAlias[streamType]; |
| 1170 | VolumeStreamState streamState = mStreamStates[streamTypeAlias]; |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1171 | |
| 1172 | final int device = getDeviceForStream(streamType); |
| 1173 | int oldIndex; |
| 1174 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1175 | // skip a2dp absolute volume control request when the device |
| 1176 | // is not an a2dp device |
| 1177 | if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) == 0 && |
| 1178 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) != 0) { |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1182 | if (mAppOps.noteOp(STEAM_VOLUME_OPS[streamTypeAlias], Binder.getCallingUid(), |
| 1183 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1184 | return; |
| 1185 | } |
| 1186 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1187 | synchronized (mSafeMediaVolumeState) { |
| 1188 | // reset any pending volume command |
| 1189 | mPendingVolumeCommand = null; |
| 1190 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1191 | oldIndex = streamState.getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1192 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1193 | index = rescaleIndex(index * 10, streamType, streamTypeAlias); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1194 | |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1195 | if (streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1196 | (device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 1197 | (flags & AudioManager.FLAG_BLUETOOTH_ABS_VOLUME) == 0) { |
| 1198 | synchronized (mA2dpAvrcpLock) { |
| 1199 | if (mA2dp != null && mAvrcpAbsVolSupported) { |
Zhihai Xu | 2f4a2b1 | 2014-01-10 16:44:39 -0800 | [diff] [blame] | 1200 | mA2dp.setAvrcpAbsoluteVolume(index / 10); |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 1201 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1205 | if (mHdmiManager != null) { |
| 1206 | synchronized (mHdmiManager) { |
| 1207 | if (mHdmiTvClient != null && |
| 1208 | streamTypeAlias == AudioSystem.STREAM_MUSIC && |
| 1209 | (flags & AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME) == 0 && |
| 1210 | oldIndex != index) { |
| 1211 | int maxIndex = getStreamMaxVolume(streamType); |
| 1212 | synchronized (mHdmiTvClient) { |
| 1213 | if (mHdmiSystemAudioSupported) { |
| 1214 | mHdmiTvClient.setSystemAudioVolume( |
| 1215 | (oldIndex + 5) / 10, (index + 5) / 10, maxIndex); |
| 1216 | } |
| 1217 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1222 | flags &= ~AudioManager.FLAG_FIXED_VOLUME; |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1223 | if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1224 | ((device & mFixedVolumeDevices) != 0)) { |
| 1225 | flags |= AudioManager.FLAG_FIXED_VOLUME; |
| 1226 | |
| 1227 | // volume is either 0 or max allowed for fixed volume devices |
| 1228 | if (index != 0) { |
| 1229 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE && |
| 1230 | (device & mSafeMediaVolumeDevices) != 0) { |
| 1231 | index = mSafeMediaVolumeIndex; |
| 1232 | } else { |
| 1233 | index = streamState.getMaxIndex(); |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1238 | if (!checkSafeMediaVolume(streamTypeAlias, index, device)) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1239 | mVolumeController.postDisplaySafeVolumeWarning(flags); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1240 | mPendingVolumeCommand = new StreamVolumeCommand( |
| 1241 | streamType, index, flags, device); |
| 1242 | } else { |
| 1243 | onSetStreamVolume(streamType, index, flags, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1244 | index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 1245 | } |
| 1246 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1247 | sendVolumeUpdate(streamType, oldIndex, index, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1250 | /** @see AudioManager#forceVolumeControlStream(int) */ |
| 1251 | public void forceVolumeControlStream(int streamType, IBinder cb) { |
| 1252 | synchronized(mForceControlStreamLock) { |
| 1253 | mVolumeControlStream = streamType; |
| 1254 | if (mVolumeControlStream == -1) { |
| 1255 | if (mForceControlStreamClient != null) { |
| 1256 | mForceControlStreamClient.release(); |
| 1257 | mForceControlStreamClient = null; |
| 1258 | } |
| 1259 | } else { |
| 1260 | mForceControlStreamClient = new ForceControlStreamClient(cb); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | private class ForceControlStreamClient implements IBinder.DeathRecipient { |
| 1266 | private IBinder mCb; // To be notified of client's death |
| 1267 | |
| 1268 | ForceControlStreamClient(IBinder cb) { |
| 1269 | if (cb != null) { |
| 1270 | try { |
| 1271 | cb.linkToDeath(this, 0); |
| 1272 | } catch (RemoteException e) { |
| 1273 | // Client has died! |
| 1274 | Log.w(TAG, "ForceControlStreamClient() could not link to "+cb+" binder death"); |
| 1275 | cb = null; |
| 1276 | } |
| 1277 | } |
| 1278 | mCb = cb; |
| 1279 | } |
| 1280 | |
| 1281 | public void binderDied() { |
| 1282 | synchronized(mForceControlStreamLock) { |
| 1283 | Log.w(TAG, "SCO client died"); |
| 1284 | if (mForceControlStreamClient != this) { |
| 1285 | Log.w(TAG, "unregistered control stream client died"); |
| 1286 | } else { |
| 1287 | mForceControlStreamClient = null; |
| 1288 | mVolumeControlStream = -1; |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | public void release() { |
| 1294 | if (mCb != null) { |
| 1295 | mCb.unlinkToDeath(this, 0); |
| 1296 | mCb = null; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 1301 | private int findVolumeDelta(int direction, int volume) { |
| 1302 | int delta = 0; |
| 1303 | if (direction == AudioManager.ADJUST_RAISE) { |
| 1304 | if (volume == MAX_MASTER_VOLUME) { |
| 1305 | return 0; |
| 1306 | } |
| 1307 | // This is the default value if we make it to the end |
| 1308 | delta = mMasterVolumeRamp[1]; |
| 1309 | // If we're raising the volume move down the ramp array until we |
| 1310 | // find the volume we're above and use that groups delta. |
| 1311 | for (int i = mMasterVolumeRamp.length - 1; i > 1; i -= 2) { |
| 1312 | if (volume >= mMasterVolumeRamp[i - 1]) { |
| 1313 | delta = mMasterVolumeRamp[i]; |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | } else if (direction == AudioManager.ADJUST_LOWER){ |
| 1318 | if (volume == 0) { |
| 1319 | return 0; |
| 1320 | } |
| 1321 | int length = mMasterVolumeRamp.length; |
| 1322 | // This is the default value if we make it to the end |
| 1323 | delta = -mMasterVolumeRamp[length - 1]; |
| 1324 | // If we're lowering the volume move up the ramp array until we |
| 1325 | // find the volume we're below and use the group below it's delta |
| 1326 | for (int i = 2; i < length; i += 2) { |
| 1327 | if (volume <= mMasterVolumeRamp[i]) { |
| 1328 | delta = -mMasterVolumeRamp[i - 1]; |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | return delta; |
| 1334 | } |
| 1335 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1336 | private void sendBroadcastToAll(Intent intent) { |
| 1337 | final long ident = Binder.clearCallingIdentity(); |
| 1338 | try { |
| 1339 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1340 | } finally { |
| 1341 | Binder.restoreCallingIdentity(ident); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | private void sendStickyBroadcastToAll(Intent intent) { |
| 1346 | final long ident = Binder.clearCallingIdentity(); |
| 1347 | try { |
| 1348 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1349 | } finally { |
| 1350 | Binder.restoreCallingIdentity(ident); |
| 1351 | } |
| 1352 | } |
| 1353 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1354 | // UI update and Broadcast Intent |
| 1355 | private void sendVolumeUpdate(int streamType, int oldIndex, int index, int flags) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1356 | if (!isPlatformVoice() && (streamType == AudioSystem.STREAM_RING)) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1357 | streamType = AudioSystem.STREAM_NOTIFICATION; |
| 1358 | } |
| 1359 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1360 | mVolumeController.postVolumeChanged(streamType, flags); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1361 | |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1362 | if ((flags & AudioManager.FLAG_FIXED_VOLUME) == 0) { |
| 1363 | oldIndex = (oldIndex + 5) / 10; |
| 1364 | index = (index + 5) / 10; |
| 1365 | Intent intent = new Intent(AudioManager.VOLUME_CHANGED_ACTION); |
| 1366 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, streamType); |
| 1367 | intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); |
| 1368 | intent.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); |
| 1369 | sendBroadcastToAll(intent); |
| 1370 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1373 | // UI update and Broadcast Intent |
| 1374 | private void sendMasterVolumeUpdate(int flags, int oldVolume, int newVolume) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1375 | mVolumeController.postMasterVolumeChanged(flags); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1376 | |
| 1377 | Intent intent = new Intent(AudioManager.MASTER_VOLUME_CHANGED_ACTION); |
| 1378 | intent.putExtra(AudioManager.EXTRA_PREV_MASTER_VOLUME_VALUE, oldVolume); |
| 1379 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_VALUE, newVolume); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1380 | sendBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | // UI update and Broadcast Intent |
| 1384 | private void sendMasterMuteUpdate(boolean muted, int flags) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1385 | mVolumeController.postMasterMuteChanged(flags); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1386 | broadcastMasterMuteStatus(muted); |
| 1387 | } |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1388 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1389 | private void broadcastMasterMuteStatus(boolean muted) { |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1390 | Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION); |
| 1391 | intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1392 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 1393 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1394 | sendStickyBroadcastToAll(intent); |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 1395 | } |
| 1396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | * Sets the stream state's index, and posts a message to set system volume. |
| 1399 | * This will not call out to the UI. Assumes a valid stream type. |
| 1400 | * |
| 1401 | * @param streamType Type of the stream |
| 1402 | * @param index Desired volume index of the stream |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1403 | * @param device the device whose volume must be changed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | * @param force If true, set the volume even if the desired volume is same |
| 1405 | * as the current volume. |
| 1406 | */ |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1407 | private void setStreamVolumeInt(int streamType, |
| 1408 | int index, |
| 1409 | int device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1410 | boolean force) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1412 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1413 | if (streamState.setIndex(index, device) || force) { |
| 1414 | // Post message to set system volume (it in turn will post a message |
| 1415 | // to persist). |
| 1416 | sendMsg(mAudioHandler, |
| 1417 | MSG_SET_DEVICE_VOLUME, |
| 1418 | SENDMSG_QUEUE, |
| 1419 | device, |
| 1420 | 0, |
| 1421 | streamState, |
| 1422 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | /** @see AudioManager#setStreamSolo(int, boolean) */ |
| 1427 | public void setStreamSolo(int streamType, boolean state, IBinder cb) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1428 | if (mUseFixedVolume) { |
| 1429 | return; |
| 1430 | } |
| 1431 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | for (int stream = 0; stream < mStreamStates.length; stream++) { |
| 1433 | if (!isStreamAffectedByMute(stream) || stream == streamType) continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | mStreamStates[stream].mute(cb, state); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /** @see AudioManager#setStreamMute(int, boolean) */ |
| 1439 | public void setStreamMute(int streamType, boolean state, IBinder cb) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1440 | if (mUseFixedVolume) { |
| 1441 | return; |
| 1442 | } |
| 1443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | if (isStreamAffectedByMute(streamType)) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1445 | if (mHdmiManager != null) { |
| 1446 | synchronized (mHdmiManager) { |
| 1447 | if (streamType == AudioSystem.STREAM_MUSIC && mHdmiTvClient != null) { |
| 1448 | synchronized (mHdmiTvClient) { |
| 1449 | if (mHdmiSystemAudioSupported) { |
| 1450 | mHdmiTvClient.setSystemAudioMute(state); |
| 1451 | } |
| 1452 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | mStreamStates[streamType].mute(cb, state); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1460 | /** get stream mute state. */ |
| 1461 | public boolean isStreamMute(int streamType) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1462 | return mStreamStates[streamType].isMuted(); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 1465 | /** @see AudioManager#setMasterMute(boolean, int) */ |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1466 | public void setMasterMute(boolean state, int flags, String callingPackage, IBinder cb) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1467 | if (mUseFixedVolume) { |
| 1468 | return; |
| 1469 | } |
| 1470 | |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 1471 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, Binder.getCallingUid(), |
| 1472 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1473 | return; |
| 1474 | } |
| 1475 | |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1476 | if (state != AudioSystem.getMasterMute()) { |
| 1477 | AudioSystem.setMasterMute(state); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1478 | // Post a persist master volume msg |
Justin Koh | 75cf9e1 | 2012-04-04 15:27:37 -0700 | [diff] [blame] | 1479 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME_MUTE, SENDMSG_REPLACE, state ? 1 |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 1480 | : 0, 0, null, PERSIST_DELAY); |
Justin Koh | 0273af5 | 2012-04-04 18:29:50 -0700 | [diff] [blame] | 1481 | sendMasterMuteUpdate(state, flags); |
Jason Simmons | 1ce5b26 | 2012-02-02 13:00:17 -0800 | [diff] [blame] | 1482 | } |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /** get master mute state. */ |
| 1486 | public boolean isMasterMute() { |
Mike Lockwood | 3194ea9 | 2011-12-07 11:47:31 -0800 | [diff] [blame] | 1487 | return AudioSystem.getMasterMute(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 1490 | protected static int getMaxStreamVolume(int streamType) { |
| 1491 | return MAX_STREAM_VOLUME[streamType]; |
| 1492 | } |
| 1493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | /** @see AudioManager#getStreamVolume(int) */ |
| 1495 | public int getStreamVolume(int streamType) { |
| 1496 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1497 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1498 | int index = mStreamStates[streamType].getIndex(device); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1499 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1500 | // by convention getStreamVolume() returns 0 when a stream is muted. |
| 1501 | if (mStreamStates[streamType].isMuted()) { |
| 1502 | index = 0; |
| 1503 | } |
Eric Laurent | 3ef7549 | 2012-11-28 12:12:23 -0800 | [diff] [blame] | 1504 | if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1505 | (device & mFixedVolumeDevices) != 0) { |
| 1506 | index = mStreamStates[streamType].getMaxIndex(); |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 1507 | } |
| 1508 | return (index + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 1511 | public int getMasterVolume() { |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1512 | if (isMasterMute()) return 0; |
| 1513 | return getLastAudibleMasterVolume(); |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1516 | public void setMasterVolume(int volume, int flags, String callingPackage) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1517 | if (mUseFixedVolume) { |
| 1518 | return; |
| 1519 | } |
| 1520 | |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 1521 | if (mAppOps.noteOp(AppOpsManager.OP_AUDIO_MASTER_VOLUME, Binder.getCallingUid(), |
| 1522 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1523 | return; |
| 1524 | } |
| 1525 | |
Mike Lockwood | 9760647 | 2012-02-09 11:24:10 -0800 | [diff] [blame] | 1526 | if (volume < 0) { |
| 1527 | volume = 0; |
| 1528 | } else if (volume > MAX_MASTER_VOLUME) { |
| 1529 | volume = MAX_MASTER_VOLUME; |
| 1530 | } |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1531 | doSetMasterVolume((float)volume / MAX_MASTER_VOLUME, flags); |
| 1532 | } |
| 1533 | |
| 1534 | private void doSetMasterVolume(float volume, int flags) { |
| 1535 | // don't allow changing master volume when muted |
| 1536 | if (!AudioSystem.getMasterMute()) { |
| 1537 | int oldVolume = getMasterVolume(); |
| 1538 | AudioSystem.setMasterVolume(volume); |
| 1539 | |
| 1540 | int newVolume = getMasterVolume(); |
| 1541 | if (newVolume != oldVolume) { |
| 1542 | // Post a persist master volume msg |
| 1543 | sendMsg(mAudioHandler, MSG_PERSIST_MASTER_VOLUME, SENDMSG_REPLACE, |
| 1544 | Math.round(volume * (float)1000.0), 0, null, PERSIST_DELAY); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1545 | } |
Justin Koh | 3caba51 | 2012-04-02 15:32:18 -0700 | [diff] [blame] | 1546 | // Send the volume update regardless whether there was a change. |
| 1547 | sendMasterVolumeUpdate(flags, oldVolume, newVolume); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 1548 | } |
Mike Lockwood | 8dc1dab | 2011-10-27 09:52:41 -0400 | [diff] [blame] | 1549 | } |
| 1550 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | /** @see AudioManager#getStreamMaxVolume(int) */ |
| 1552 | public int getStreamMaxVolume(int streamType) { |
| 1553 | ensureValidStreamType(streamType); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1554 | return (mStreamStates[streamType].getMaxIndex() + 5) / 10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Mike Lockwood | 4767690 | 2011-11-08 10:31:21 -0800 | [diff] [blame] | 1557 | public int getMasterMaxVolume() { |
| 1558 | return MAX_MASTER_VOLUME; |
| 1559 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1560 | |
| 1561 | /** Get last audible volume before stream was muted. */ |
| 1562 | public int getLastAudibleStreamVolume(int streamType) { |
| 1563 | ensureValidStreamType(streamType); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1564 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1565 | return (mStreamStates[streamType].getIndex(device) + 5) / 10; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1568 | /** Get last audible master volume before it was muted. */ |
| 1569 | public int getLastAudibleMasterVolume() { |
| 1570 | return Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME); |
| 1571 | } |
| 1572 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 1573 | /** @see AudioManager#getMasterStreamType() */ |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1574 | public int getMasterStreamType() { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1575 | switch (mPlatformType) { |
| 1576 | case PLATFORM_VOICE: |
| 1577 | return AudioSystem.STREAM_RING; |
| 1578 | case PLATFORM_TELEVISION: |
| 1579 | return AudioSystem.STREAM_MUSIC; |
| 1580 | default: |
| 1581 | break; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1582 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1583 | return AudioSystem.STREAM_NOTIFICATION; |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 1586 | /** @see AudioManager#setMicrophoneMute(boolean) */ |
| 1587 | public void setMicrophoneMute(boolean on, String callingPackage) { |
| 1588 | if (mAppOps.noteOp(AppOpsManager.OP_MUTE_MICROPHONE, Binder.getCallingUid(), |
| 1589 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1590 | return; |
| 1591 | } |
| 1592 | |
| 1593 | AudioSystem.muteMicrophone(on); |
| 1594 | } |
| 1595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | /** @see AudioManager#getRingerMode() */ |
| 1597 | public int getRingerMode() { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1598 | synchronized(mSettingsLock) { |
| 1599 | return mRingerMode; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | private void ensureValidRingerMode(int ringerMode) { |
| 1604 | if (!AudioManager.isValidRingerMode(ringerMode)) { |
| 1605 | throw new IllegalArgumentException("Bad ringer mode " + ringerMode); |
| 1606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /** @see AudioManager#setRingerMode(int) */ |
| 1610 | public void setRingerMode(int ringerMode) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1611 | if (mUseFixedVolume || isPlatformTelevision()) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1612 | return; |
| 1613 | } |
| 1614 | |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 1615 | if ((ringerMode == AudioManager.RINGER_MODE_VIBRATE) && !mHasVibrator) { |
| 1616 | ringerMode = AudioManager.RINGER_MODE_SILENT; |
| 1617 | } |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1618 | if (ringerMode != getRingerMode()) { |
| 1619 | setRingerModeInt(ringerMode, true); |
| 1620 | // Send sticky broadcast |
| 1621 | broadcastRingerMode(ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1625 | private void setRingerModeInt(int ringerMode, boolean persist) { |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1626 | synchronized(mSettingsLock) { |
| 1627 | mRingerMode = ringerMode; |
| 1628 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1629 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1630 | // Mute stream if not previously muted by ringer mode and ringer mode |
| 1631 | // is not RINGER_MODE_NORMAL and stream is affected by ringer mode. |
| 1632 | // Unmute stream if previously muted by ringer mode and ringer mode |
| 1633 | // is RINGER_MODE_NORMAL or stream is not affected by ringer mode. |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1634 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1635 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 1636 | if (isStreamMutedByRingerMode(streamType)) { |
| 1637 | if (!isStreamAffectedByRingerMode(streamType) || |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1638 | ringerMode == AudioManager.RINGER_MODE_NORMAL) { |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1639 | // ring and notifications volume should never be 0 when not silenced |
| 1640 | // on voice capable devices |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 1641 | if (isPlatformVoice() && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1642 | mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 1643 | synchronized (mStreamStates[streamType]) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1644 | Set set = mStreamStates[streamType].mIndex.entrySet(); |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 1645 | Iterator i = set.iterator(); |
| 1646 | while (i.hasNext()) { |
| 1647 | Map.Entry entry = (Map.Entry)i.next(); |
| 1648 | if ((Integer)entry.getValue() == 0) { |
| 1649 | entry.setValue(10); |
| 1650 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1651 | } |
| 1652 | } |
Eric Laurent | b024c30 | 2011-10-14 17:19:27 -0700 | [diff] [blame] | 1653 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1654 | mStreamStates[streamType].mute(null, false); |
| 1655 | mRingerModeMutedStreams &= ~(1 << streamType); |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 1656 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1657 | } else { |
| 1658 | if (isStreamAffectedByRingerMode(streamType) && |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1659 | ringerMode != AudioManager.RINGER_MODE_NORMAL) { |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 1660 | mStreamStates[streamType].mute(null, true); |
| 1661 | mRingerModeMutedStreams |= (1 << streamType); |
| 1662 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1665 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1666 | // Post a persist ringer mode msg |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1667 | if (persist) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 1668 | sendMsg(mAudioHandler, MSG_PERSIST_RINGER_MODE, |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 1669 | SENDMSG_REPLACE, 0, 0, null, PERSIST_DELAY); |
| 1670 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1673 | private void restoreMasterVolume() { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 1674 | if (mUseFixedVolume) { |
| 1675 | AudioSystem.setMasterVolume(1.0f); |
| 1676 | return; |
| 1677 | } |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1678 | if (mUseMasterVolume) { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 1679 | float volume = Settings.System.getFloatForUser(mContentResolver, |
| 1680 | Settings.System.VOLUME_MASTER, -1.0f, UserHandle.USER_CURRENT); |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 1681 | if (volume >= 0.0f) { |
| 1682 | AudioSystem.setMasterVolume(volume); |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1687 | /** @see AudioManager#shouldVibrate(int) */ |
| 1688 | public boolean shouldVibrate(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1689 | if (!mHasVibrator) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | |
| 1691 | switch (getVibrateSetting(vibrateType)) { |
| 1692 | |
| 1693 | case AudioManager.VIBRATE_SETTING_ON: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1694 | return getRingerMode() != AudioManager.RINGER_MODE_SILENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | |
| 1696 | case AudioManager.VIBRATE_SETTING_ONLY_SILENT: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 1697 | return getRingerMode() == AudioManager.RINGER_MODE_VIBRATE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | |
| 1699 | case AudioManager.VIBRATE_SETTING_OFF: |
Daniel Sandler | bcac496 | 2010-04-12 13:23:57 -0400 | [diff] [blame] | 1700 | // return false, even for incoming calls |
| 1701 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | |
| 1703 | default: |
| 1704 | return false; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | /** @see AudioManager#getVibrateSetting(int) */ |
| 1709 | public int getVibrateSetting(int vibrateType) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1710 | if (!mHasVibrator) return AudioManager.VIBRATE_SETTING_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | return (mVibrateSetting >> (vibrateType * 2)) & 3; |
| 1712 | } |
| 1713 | |
| 1714 | /** @see AudioManager#setVibrateSetting(int, int) */ |
| 1715 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
| 1716 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1717 | if (!mHasVibrator) return; |
| 1718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | mVibrateSetting = getValueForVibrateSetting(mVibrateSetting, vibrateType, vibrateSetting); |
| 1720 | |
| 1721 | // Broadcast change |
| 1722 | broadcastVibrateSetting(vibrateType); |
| 1723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | /** |
| 1727 | * @see #setVibrateSetting(int, int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | */ |
| 1729 | public static int getValueForVibrateSetting(int existingValue, int vibrateType, |
| 1730 | int vibrateSetting) { |
| 1731 | |
| 1732 | // First clear the existing setting. Each vibrate type has two bits in |
| 1733 | // the value. Note '3' is '11' in binary. |
| 1734 | existingValue &= ~(3 << (vibrateType * 2)); |
| 1735 | |
| 1736 | // Set into the old value |
| 1737 | existingValue |= (vibrateSetting & 3) << (vibrateType * 2); |
| 1738 | |
| 1739 | return existingValue; |
| 1740 | } |
| 1741 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1742 | private class SetModeDeathHandler implements IBinder.DeathRecipient { |
| 1743 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1744 | private int mPid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1745 | private int mMode = AudioSystem.MODE_NORMAL; // Current mode set by this client |
| 1746 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1747 | SetModeDeathHandler(IBinder cb, int pid) { |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1748 | mCb = cb; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1749 | mPid = pid; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | public void binderDied() { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1753 | int newModeOwnerPid = 0; |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1754 | synchronized(mSetModeDeathHandlers) { |
| 1755 | Log.w(TAG, "setMode() client died"); |
| 1756 | int index = mSetModeDeathHandlers.indexOf(this); |
| 1757 | if (index < 0) { |
| 1758 | Log.w(TAG, "unregistered setMode() client died"); |
| 1759 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1760 | newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid); |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1761 | } |
| 1762 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1763 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1764 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1765 | if (newModeOwnerPid != 0) { |
Eric Laurent | 6b5e22d | 2013-03-28 16:10:45 -0700 | [diff] [blame] | 1766 | final long ident = Binder.clearCallingIdentity(); |
| 1767 | disconnectBluetoothSco(newModeOwnerPid); |
| 1768 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1769 | } |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 1772 | public int getPid() { |
| 1773 | return mPid; |
| 1774 | } |
| 1775 | |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1776 | public void setMode(int mode) { |
| 1777 | mMode = mode; |
| 1778 | } |
| 1779 | |
| 1780 | public int getMode() { |
| 1781 | return mMode; |
| 1782 | } |
| 1783 | |
| 1784 | public IBinder getBinder() { |
| 1785 | return mCb; |
| 1786 | } |
| 1787 | } |
| 1788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | /** @see AudioManager#setMode(int) */ |
Eric Laurent | 9272b4b | 2010-01-23 17:12:59 -0800 | [diff] [blame] | 1790 | public void setMode(int mode, IBinder cb) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | if (!checkAudioSettingsPermission("setMode()")) { |
| 1792 | return; |
| 1793 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1794 | |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 1795 | if (mode < AudioSystem.MODE_CURRENT || mode >= AudioSystem.NUM_MODES) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| 1798 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1799 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1800 | synchronized(mSetModeDeathHandlers) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1801 | if (mode == AudioSystem.MODE_CURRENT) { |
| 1802 | mode = mMode; |
| 1803 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1804 | newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid()); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1805 | } |
| 1806 | // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all |
| 1807 | // SCO connections not started by the application changing the mode |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1808 | if (newModeOwnerPid != 0) { |
| 1809 | disconnectBluetoothSco(newModeOwnerPid); |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1810 | } |
| 1811 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1812 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1813 | // must be called synchronized on mSetModeDeathHandlers |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1814 | // 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] | 1815 | // any mode other than NORMAL. |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1816 | int setModeInt(int mode, IBinder cb, int pid) { |
| 1817 | int newModeOwnerPid = 0; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1818 | if (cb == null) { |
| 1819 | Log.e(TAG, "setModeInt() called with null binder"); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1820 | return newModeOwnerPid; |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1821 | } |
Jean-Michel Trivi | 2ade576 | 2010-12-11 13:18:30 -0800 | [diff] [blame] | 1822 | |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1823 | SetModeDeathHandler hdlr = null; |
| 1824 | Iterator iter = mSetModeDeathHandlers.iterator(); |
| 1825 | while (iter.hasNext()) { |
| 1826 | SetModeDeathHandler h = (SetModeDeathHandler)iter.next(); |
| 1827 | if (h.getPid() == pid) { |
| 1828 | hdlr = h; |
| 1829 | // Remove from client list so that it is re-inserted at top of list |
| 1830 | iter.remove(); |
| 1831 | hdlr.getBinder().unlinkToDeath(hdlr, 0); |
| 1832 | break; |
| 1833 | } |
| 1834 | } |
| 1835 | int status = AudioSystem.AUDIO_STATUS_OK; |
| 1836 | do { |
| 1837 | if (mode == AudioSystem.MODE_NORMAL) { |
| 1838 | // get new mode from client at top the list if any |
| 1839 | if (!mSetModeDeathHandlers.isEmpty()) { |
| 1840 | hdlr = mSetModeDeathHandlers.get(0); |
| 1841 | cb = hdlr.getBinder(); |
| 1842 | mode = hdlr.getMode(); |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 1843 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1844 | } else { |
| 1845 | if (hdlr == null) { |
| 1846 | hdlr = new SetModeDeathHandler(cb, pid); |
| 1847 | } |
| 1848 | // Register for client death notification |
| 1849 | try { |
| 1850 | cb.linkToDeath(hdlr, 0); |
| 1851 | } catch (RemoteException e) { |
| 1852 | // Client has died! |
| 1853 | Log.w(TAG, "setMode() could not link to "+cb+" binder death"); |
| 1854 | } |
| 1855 | |
| 1856 | // Last client to call setMode() is always at top of client list |
| 1857 | // as required by SetModeDeathHandler.binderDied() |
| 1858 | mSetModeDeathHandlers.add(0, hdlr); |
| 1859 | hdlr.setMode(mode); |
| 1860 | } |
| 1861 | |
| 1862 | if (mode != mMode) { |
| 1863 | status = AudioSystem.setPhoneState(mode); |
| 1864 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 1865 | mMode = mode; |
| 1866 | } else { |
| 1867 | if (hdlr != null) { |
| 1868 | mSetModeDeathHandlers.remove(hdlr); |
| 1869 | cb.unlinkToDeath(hdlr, 0); |
| 1870 | } |
| 1871 | // force reading new top of mSetModeDeathHandlers stack |
| 1872 | mode = AudioSystem.MODE_NORMAL; |
| 1873 | } |
| 1874 | } else { |
| 1875 | status = AudioSystem.AUDIO_STATUS_OK; |
| 1876 | } |
| 1877 | } while (status != AudioSystem.AUDIO_STATUS_OK && !mSetModeDeathHandlers.isEmpty()); |
| 1878 | |
| 1879 | if (status == AudioSystem.AUDIO_STATUS_OK) { |
| 1880 | if (mode != AudioSystem.MODE_NORMAL) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1881 | if (mSetModeDeathHandlers.isEmpty()) { |
| 1882 | Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack"); |
| 1883 | } else { |
| 1884 | newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid(); |
| 1885 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | } |
| 1887 | int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 1888 | if (streamType == STREAM_REMOTE_MUSIC) { |
| 1889 | // here handle remote media playback the same way as local playback |
| 1890 | streamType = AudioManager.STREAM_MUSIC; |
| 1891 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1892 | int device = getDeviceForStream(streamType); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 1893 | int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); |
| 1894 | setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1895 | |
| 1896 | updateStreamVolumeAlias(true /*updateVolumes*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1897 | } |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 1898 | return newModeOwnerPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /** @see AudioManager#getMode() */ |
| 1902 | public int getMode() { |
| 1903 | return mMode; |
| 1904 | } |
| 1905 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 1906 | //========================================================================================== |
| 1907 | // Sound Effects |
| 1908 | //========================================================================================== |
| 1909 | |
| 1910 | private static final String TAG_AUDIO_ASSETS = "audio_assets"; |
| 1911 | private static final String ATTR_VERSION = "version"; |
| 1912 | private static final String TAG_GROUP = "group"; |
| 1913 | private static final String ATTR_GROUP_NAME = "name"; |
| 1914 | private static final String TAG_ASSET = "asset"; |
| 1915 | private static final String ATTR_ASSET_ID = "id"; |
| 1916 | private static final String ATTR_ASSET_FILE = "file"; |
| 1917 | |
| 1918 | private static final String ASSET_FILE_VERSION = "1.0"; |
| 1919 | private static final String GROUP_TOUCH_SOUNDS = "touch_sounds"; |
| 1920 | |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 1921 | private static final int SOUND_EFFECTS_LOAD_TIMEOUT_MS = 5000; |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 1922 | |
| 1923 | class LoadSoundEffectReply { |
| 1924 | public int mStatus = 1; |
| 1925 | }; |
| 1926 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 1927 | private void loadTouchSoundAssetDefaults() { |
| 1928 | SOUND_EFFECT_FILES.add("Effect_Tick.ogg"); |
| 1929 | for (int i = 0; i < AudioManager.NUM_SOUND_EFFECTS; i++) { |
| 1930 | SOUND_EFFECT_FILES_MAP[i][0] = 0; |
| 1931 | SOUND_EFFECT_FILES_MAP[i][1] = -1; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | private void loadTouchSoundAssets() { |
| 1936 | XmlResourceParser parser = null; |
| 1937 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 1938 | // only load assets once. |
| 1939 | if (!SOUND_EFFECT_FILES.isEmpty()) { |
| 1940 | return; |
| 1941 | } |
| 1942 | |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 1943 | loadTouchSoundAssetDefaults(); |
| 1944 | |
| 1945 | try { |
| 1946 | parser = mContext.getResources().getXml(com.android.internal.R.xml.audio_assets); |
| 1947 | |
| 1948 | XmlUtils.beginDocument(parser, TAG_AUDIO_ASSETS); |
| 1949 | String version = parser.getAttributeValue(null, ATTR_VERSION); |
| 1950 | boolean inTouchSoundsGroup = false; |
| 1951 | |
| 1952 | if (ASSET_FILE_VERSION.equals(version)) { |
| 1953 | while (true) { |
| 1954 | XmlUtils.nextElement(parser); |
| 1955 | String element = parser.getName(); |
| 1956 | if (element == null) { |
| 1957 | break; |
| 1958 | } |
| 1959 | if (element.equals(TAG_GROUP)) { |
| 1960 | String name = parser.getAttributeValue(null, ATTR_GROUP_NAME); |
| 1961 | if (GROUP_TOUCH_SOUNDS.equals(name)) { |
| 1962 | inTouchSoundsGroup = true; |
| 1963 | break; |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | while (inTouchSoundsGroup) { |
| 1968 | XmlUtils.nextElement(parser); |
| 1969 | String element = parser.getName(); |
| 1970 | if (element == null) { |
| 1971 | break; |
| 1972 | } |
| 1973 | if (element.equals(TAG_ASSET)) { |
| 1974 | String id = parser.getAttributeValue(null, ATTR_ASSET_ID); |
| 1975 | String file = parser.getAttributeValue(null, ATTR_ASSET_FILE); |
| 1976 | int fx; |
| 1977 | |
| 1978 | try { |
| 1979 | Field field = AudioManager.class.getField(id); |
| 1980 | fx = field.getInt(null); |
| 1981 | } catch (Exception e) { |
| 1982 | Log.w(TAG, "Invalid touch sound ID: "+id); |
| 1983 | continue; |
| 1984 | } |
| 1985 | |
| 1986 | int i = SOUND_EFFECT_FILES.indexOf(file); |
| 1987 | if (i == -1) { |
| 1988 | i = SOUND_EFFECT_FILES.size(); |
| 1989 | SOUND_EFFECT_FILES.add(file); |
| 1990 | } |
| 1991 | SOUND_EFFECT_FILES_MAP[fx][0] = i; |
| 1992 | } else { |
| 1993 | break; |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | } catch (Resources.NotFoundException e) { |
| 1998 | Log.w(TAG, "audio assets file not found", e); |
| 1999 | } catch (XmlPullParserException e) { |
| 2000 | Log.w(TAG, "XML parser exception reading touch sound assets", e); |
| 2001 | } catch (IOException e) { |
| 2002 | Log.w(TAG, "I/O exception reading touch sound assets", e); |
| 2003 | } finally { |
| 2004 | if (parser != null) { |
| 2005 | parser.close(); |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | /** @see AudioManager#playSoundEffect(int) */ |
| 2011 | public void playSoundEffect(int effectType) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2012 | playSoundEffectVolume(effectType, -1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | /** @see AudioManager#playSoundEffect(int, float) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | public void playSoundEffectVolume(int effectType, float volume) { |
Natalie Silvanovich | 559c76d | 2014-05-01 10:16:24 -0700 | [diff] [blame] | 2017 | if (effectType >= AudioManager.NUM_SOUND_EFFECTS || effectType < 0) { |
| 2018 | Log.w(TAG, "AudioService effectType value " + effectType + " out of range"); |
| 2019 | return; |
| 2020 | } |
| 2021 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2022 | sendMsg(mAudioHandler, MSG_PLAY_SOUND_EFFECT, SENDMSG_QUEUE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | effectType, (int) (volume * 1000), null, 0); |
| 2024 | } |
| 2025 | |
| 2026 | /** |
| 2027 | * Loads samples into the soundpool. |
Glenn Kasten | 5c17a82 | 2011-11-30 09:41:01 -0800 | [diff] [blame] | 2028 | * 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] | 2029 | */ |
| 2030 | public boolean loadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2031 | int attempts = 3; |
| 2032 | LoadSoundEffectReply reply = new LoadSoundEffectReply(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2033 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2034 | synchronized (reply) { |
| 2035 | sendMsg(mAudioHandler, MSG_LOAD_SOUND_EFFECTS, SENDMSG_QUEUE, 0, 0, reply, 0); |
| 2036 | while ((reply.mStatus == 1) && (attempts-- > 0)) { |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2037 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 2038 | reply.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2039 | } catch (InterruptedException e) { |
| 2040 | Log.w(TAG, "loadSoundEffects Interrupted while waiting sound pool loaded."); |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 2041 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2042 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2044 | return (reply.mStatus == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * Unloads samples from the sound pool. |
| 2049 | * This method can be called to free some memory when |
| 2050 | * sound effects are disabled. |
| 2051 | */ |
| 2052 | public void unloadSoundEffects() { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2053 | 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] | 2054 | } |
| 2055 | |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2056 | class SoundPoolListenerThread extends Thread { |
| 2057 | public SoundPoolListenerThread() { |
| 2058 | super("SoundPoolListenerThread"); |
| 2059 | } |
| 2060 | |
| 2061 | @Override |
| 2062 | public void run() { |
| 2063 | |
| 2064 | Looper.prepare(); |
| 2065 | mSoundPoolLooper = Looper.myLooper(); |
| 2066 | |
| 2067 | synchronized (mSoundEffectsLock) { |
| 2068 | if (mSoundPool != null) { |
| 2069 | mSoundPoolCallBack = new SoundPoolCallback(); |
| 2070 | mSoundPool.setOnLoadCompleteListener(mSoundPoolCallBack); |
| 2071 | } |
| 2072 | mSoundEffectsLock.notify(); |
| 2073 | } |
| 2074 | Looper.loop(); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | private final class SoundPoolCallback implements |
| 2079 | android.media.SoundPool.OnLoadCompleteListener { |
| 2080 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2081 | int mStatus = 1; // 1 means neither error nor last sample loaded yet |
| 2082 | List<Integer> mSamples = new ArrayList<Integer>(); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2083 | |
| 2084 | public int status() { |
| 2085 | return mStatus; |
| 2086 | } |
| 2087 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2088 | public void setSamples(int[] samples) { |
| 2089 | for (int i = 0; i < samples.length; i++) { |
| 2090 | // do not wait ack for samples rejected upfront by SoundPool |
| 2091 | if (samples[i] > 0) { |
| 2092 | mSamples.add(samples[i]); |
| 2093 | } |
| 2094 | } |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { |
| 2098 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2099 | int i = mSamples.indexOf(sampleId); |
| 2100 | if (i >= 0) { |
| 2101 | mSamples.remove(i); |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2102 | } |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 2103 | if ((status != 0) || mSamples. isEmpty()) { |
| 2104 | mStatus = status; |
Eric Laurent | a60e212 | 2010-12-28 16:49:07 -0800 | [diff] [blame] | 2105 | mSoundEffectsLock.notify(); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2111 | /** @see AudioManager#reloadAudioSettings() */ |
| 2112 | public void reloadAudioSettings() { |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2113 | readAudioSettings(false /*userSwitch*/); |
| 2114 | } |
| 2115 | |
| 2116 | private void readAudioSettings(boolean userSwitch) { |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2117 | // restore ringer mode, ringer mode affected streams, mute affected streams and vibrate settings |
| 2118 | readPersistedSettings(); |
| 2119 | |
| 2120 | // restore volume settings |
| 2121 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 2122 | for (int streamType = 0; streamType < numStreamTypes; streamType++) { |
| 2123 | VolumeStreamState streamState = mStreamStates[streamType]; |
| 2124 | |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 2125 | if (userSwitch && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) { |
| 2126 | continue; |
| 2127 | } |
| 2128 | |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2129 | synchronized (streamState) { |
| 2130 | streamState.readSettings(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2131 | |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2132 | // unmute stream that was muted but is not affect by mute anymore |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2133 | if (streamState.isMuted() && ((!isStreamAffectedByMute(streamType) && |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 2134 | !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 2135 | int size = streamState.mDeathHandlers.size(); |
| 2136 | for (int i = 0; i < size; i++) { |
| 2137 | streamState.mDeathHandlers.get(i).mMuteCount = 1; |
| 2138 | streamState.mDeathHandlers.get(i).mute(false); |
| 2139 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2140 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 2144 | // apply new ringer mode before checking volume for alias streams so that streams |
| 2145 | // muted by ringer mode have the correct volume |
| 2146 | setRingerModeInt(getRingerMode(), false); |
| 2147 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2148 | checkAllFixedVolumeDevices(); |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2149 | checkAllAliasStreamVolumes(); |
| 2150 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2151 | synchronized (mSafeMediaVolumeState) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 2152 | mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver, |
| 2153 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT), |
| 2154 | 0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2155 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) { |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2156 | enforceSafeMediaVolume(); |
| 2157 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2158 | } |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2161 | /** @see AudioManager#setSpeakerphoneOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2162 | public void setSpeakerphoneOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2163 | if (!checkAudioSettingsPermission("setSpeakerphoneOn()")) { |
| 2164 | return; |
| 2165 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2166 | |
| 2167 | if (on) { |
| 2168 | if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2169 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
| 2170 | AudioSystem.FOR_RECORD, AudioSystem.FORCE_NONE, null, 0); |
| 2171 | } |
| 2172 | mForcedUseForComm = AudioSystem.FORCE_SPEAKER; |
| 2173 | } else if (mForcedUseForComm == AudioSystem.FORCE_SPEAKER){ |
| 2174 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2175 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2176 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2177 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2178 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /** @see AudioManager#isSpeakerphoneOn() */ |
| 2182 | public boolean isSpeakerphoneOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2183 | return (mForcedUseForComm == AudioSystem.FORCE_SPEAKER); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2186 | /** @see AudioManager#setBluetoothScoOn(boolean) */ |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2187 | public void setBluetoothScoOn(boolean on){ |
Eric Laurent | dc1d17a | 2009-09-10 00:48:21 -0700 | [diff] [blame] | 2188 | if (!checkAudioSettingsPermission("setBluetoothScoOn()")) { |
| 2189 | return; |
| 2190 | } |
Johan Gustavsson | 7337bee | 2013-03-01 15:53:30 +0100 | [diff] [blame] | 2191 | |
| 2192 | if (on) { |
| 2193 | mForcedUseForComm = AudioSystem.FORCE_BT_SCO; |
| 2194 | } else if (mForcedUseForComm == AudioSystem.FORCE_BT_SCO) { |
| 2195 | mForcedUseForComm = AudioSystem.FORCE_NONE; |
| 2196 | } |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2197 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2198 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2199 | AudioSystem.FOR_COMMUNICATION, mForcedUseForComm, null, 0); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2200 | sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2201 | AudioSystem.FOR_RECORD, mForcedUseForComm, null, 0); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /** @see AudioManager#isBluetoothScoOn() */ |
| 2205 | public boolean isBluetoothScoOn() { |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 2206 | return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 2209 | /** @see AudioManager#setBluetoothA2dpOn(boolean) */ |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2210 | public void setBluetoothA2dpOn(boolean on) { |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 2211 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2212 | mBluetoothA2dpEnabled = on; |
| 2213 | sendMsg(mAudioHandler, MSG_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE, |
| 2214 | AudioSystem.FOR_MEDIA, |
| 2215 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP, |
| 2216 | null, 0); |
| 2217 | } |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | /** @see AudioManager#isBluetoothA2dpOn() */ |
| 2221 | public boolean isBluetoothA2dpOn() { |
| 2222 | synchronized (mBluetoothA2dpEnabledLock) { |
| 2223 | return mBluetoothA2dpEnabled; |
| 2224 | } |
| 2225 | } |
| 2226 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2227 | /** @see AudioManager#startBluetoothSco() */ |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2228 | public void startBluetoothSco(IBinder cb, int targetSdkVersion) { |
| 2229 | int scoAudioMode = |
| 2230 | (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) ? |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2231 | SCO_MODE_VIRTUAL_CALL : SCO_MODE_UNDEFINED; |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2232 | startBluetoothScoInt(cb, scoAudioMode); |
| 2233 | } |
| 2234 | |
| 2235 | /** @see AudioManager#startBluetoothScoVirtualCall() */ |
| 2236 | public void startBluetoothScoVirtualCall(IBinder cb) { |
| 2237 | startBluetoothScoInt(cb, SCO_MODE_VIRTUAL_CALL); |
| 2238 | } |
| 2239 | |
| 2240 | void startBluetoothScoInt(IBinder cb, int scoAudioMode){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2241 | if (!checkAudioSettingsPermission("startBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2242 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2243 | return; |
| 2244 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2245 | ScoClient client = getScoClient(cb, true); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2246 | // The calling identity must be cleared before calling ScoClient.incCount(). |
| 2247 | // inCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2248 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2249 | // The caller identity must be cleared after getScoClient() because it is needed if a new |
| 2250 | // client is created. |
| 2251 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2252 | client.incCount(scoAudioMode); |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2253 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /** @see AudioManager#stopBluetoothSco() */ |
| 2257 | public void stopBluetoothSco(IBinder cb){ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2258 | if (!checkAudioSettingsPermission("stopBluetoothSco()") || |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 2259 | !mSystemReady) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2260 | return; |
| 2261 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2262 | ScoClient client = getScoClient(cb, false); |
Eric Laurent | f5a1fc3 | 2013-03-11 18:52:57 -0700 | [diff] [blame] | 2263 | // The calling identity must be cleared before calling ScoClient.decCount(). |
| 2264 | // decCount() calls requestScoState() which in turn can call BluetoothHeadset APIs |
| 2265 | // and this must be done on behalf of system server to make sure permissions are granted. |
| 2266 | final long ident = Binder.clearCallingIdentity(); |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2267 | if (client != null) { |
| 2268 | client.decCount(); |
| 2269 | } |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2270 | Binder.restoreCallingIdentity(ident); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 2273 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2274 | private class ScoClient implements IBinder.DeathRecipient { |
| 2275 | private IBinder mCb; // To be notified of client's death |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2276 | private int mCreatorPid; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2277 | private int mStartcount; // number of SCO connections started by this client |
| 2278 | |
| 2279 | ScoClient(IBinder cb) { |
| 2280 | mCb = cb; |
Marco Nelissen | f1ddd51 | 2011-08-10 14:15:44 -0700 | [diff] [blame] | 2281 | mCreatorPid = Binder.getCallingPid(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2282 | mStartcount = 0; |
| 2283 | } |
| 2284 | |
| 2285 | public void binderDied() { |
| 2286 | synchronized(mScoClients) { |
| 2287 | Log.w(TAG, "SCO client died"); |
| 2288 | int index = mScoClients.indexOf(this); |
| 2289 | if (index < 0) { |
| 2290 | Log.w(TAG, "unregistered SCO client died"); |
| 2291 | } else { |
| 2292 | clearCount(true); |
| 2293 | mScoClients.remove(this); |
| 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2298 | public void incCount(int scoAudioMode) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2299 | synchronized(mScoClients) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2300 | requestScoState(BluetoothHeadset.STATE_AUDIO_CONNECTED, scoAudioMode); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2301 | if (mStartcount == 0) { |
| 2302 | try { |
| 2303 | mCb.linkToDeath(this, 0); |
| 2304 | } catch (RemoteException e) { |
| 2305 | // client has already died! |
| 2306 | Log.w(TAG, "ScoClient incCount() could not link to "+mCb+" binder death"); |
| 2307 | } |
| 2308 | } |
| 2309 | mStartcount++; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | public void decCount() { |
| 2314 | synchronized(mScoClients) { |
| 2315 | if (mStartcount == 0) { |
| 2316 | Log.w(TAG, "ScoClient.decCount() already 0"); |
| 2317 | } else { |
| 2318 | mStartcount--; |
| 2319 | if (mStartcount == 0) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2320 | try { |
| 2321 | mCb.unlinkToDeath(this, 0); |
| 2322 | } catch (NoSuchElementException e) { |
| 2323 | Log.w(TAG, "decCount() going to 0 but not registered to binder"); |
| 2324 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2325 | } |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2326 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | public void clearCount(boolean stopSco) { |
| 2332 | synchronized(mScoClients) { |
Eric Laurent | e2dd8c4 | 2010-06-30 19:41:56 -0700 | [diff] [blame] | 2333 | if (mStartcount != 0) { |
| 2334 | try { |
| 2335 | mCb.unlinkToDeath(this, 0); |
| 2336 | } catch (NoSuchElementException e) { |
| 2337 | Log.w(TAG, "clearCount() mStartcount: "+mStartcount+" != 0 but not registered to binder"); |
| 2338 | } |
| 2339 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2340 | mStartcount = 0; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2341 | if (stopSco) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2342 | requestScoState(BluetoothHeadset.STATE_AUDIO_DISCONNECTED, 0); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | public int getCount() { |
| 2348 | return mStartcount; |
| 2349 | } |
| 2350 | |
| 2351 | public IBinder getBinder() { |
| 2352 | return mCb; |
| 2353 | } |
| 2354 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2355 | public int getPid() { |
| 2356 | return mCreatorPid; |
| 2357 | } |
| 2358 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2359 | public int totalCount() { |
| 2360 | synchronized(mScoClients) { |
| 2361 | int count = 0; |
| 2362 | int size = mScoClients.size(); |
| 2363 | for (int i = 0; i < size; i++) { |
| 2364 | count += mScoClients.get(i).getCount(); |
| 2365 | } |
| 2366 | return count; |
| 2367 | } |
| 2368 | } |
| 2369 | |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2370 | private void requestScoState(int state, int scoAudioMode) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2371 | checkScoAudioState(); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2372 | if (totalCount() == 0) { |
| 2373 | if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) { |
| 2374 | // Make sure that the state transitions to CONNECTING even if we cannot initiate |
| 2375 | // the connection. |
| 2376 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTING); |
| 2377 | // 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] | 2378 | // currently controlled by the same client process. |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2379 | synchronized(mSetModeDeathHandlers) { |
| 2380 | if ((mSetModeDeathHandlers.isEmpty() || |
| 2381 | mSetModeDeathHandlers.get(0).getPid() == mCreatorPid) && |
| 2382 | (mScoAudioState == SCO_STATE_INACTIVE || |
| 2383 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
| 2384 | if (mScoAudioState == SCO_STATE_INACTIVE) { |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2385 | mScoAudioMode = scoAudioMode; |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2386 | if (scoAudioMode == SCO_MODE_UNDEFINED) { |
| 2387 | mScoAudioMode = new Integer(Settings.Global.getInt( |
| 2388 | mContentResolver, |
| 2389 | "bluetooth_sco_channel_"+ |
| 2390 | mBluetoothHeadsetDevice.getAddress(), |
| 2391 | SCO_MODE_VIRTUAL_CALL)); |
| 2392 | if (mScoAudioMode > SCO_MODE_MAX || mScoAudioMode < 0) { |
| 2393 | mScoAudioMode = SCO_MODE_VIRTUAL_CALL; |
| 2394 | } |
| 2395 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2396 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2397 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2398 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2399 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2400 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2401 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2402 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2403 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2404 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2405 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2406 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2407 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2408 | if (status) { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2409 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2410 | } else { |
| 2411 | broadcastScoConnectionState( |
| 2412 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2413 | } |
| 2414 | } else if (getBluetoothHeadset()) { |
| 2415 | mScoAudioState = SCO_STATE_ACTIVATE_REQ; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2416 | } |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2417 | } else { |
| 2418 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
| 2419 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_CONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2420 | } |
| 2421 | } else { |
Eric Laurent | 9f103de | 2011-09-08 15:04:23 -0700 | [diff] [blame] | 2422 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2423 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2424 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2425 | } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2426 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 2427 | mScoAudioState == SCO_STATE_ACTIVATE_REQ)) { |
| 2428 | if (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL) { |
Marco Nelissen | 671db6f | 2011-09-06 16:29:12 -0700 | [diff] [blame] | 2429 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null) { |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2430 | boolean status = false; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2431 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2432 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2433 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2434 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2435 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2436 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2437 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2438 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2439 | } |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2440 | |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2441 | if (!status) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2442 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2443 | broadcastScoConnectionState( |
| 2444 | AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2445 | } |
| 2446 | } else if (getBluetoothHeadset()) { |
| 2447 | mScoAudioState = SCO_STATE_DEACTIVATE_REQ; |
| 2448 | } |
| 2449 | } else { |
| 2450 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2451 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2452 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2458 | private void checkScoAudioState() { |
| 2459 | if (mBluetoothHeadset != null && mBluetoothHeadsetDevice != null && |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2460 | mScoAudioState == SCO_STATE_INACTIVE && |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2461 | mBluetoothHeadset.getAudioState(mBluetoothHeadsetDevice) |
| 2462 | != BluetoothHeadset.STATE_AUDIO_DISCONNECTED) { |
| 2463 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
| 2464 | } |
| 2465 | } |
| 2466 | |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2467 | private ScoClient getScoClient(IBinder cb, boolean create) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2468 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2469 | ScoClient client = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2470 | int size = mScoClients.size(); |
| 2471 | for (int i = 0; i < size; i++) { |
| 2472 | client = mScoClients.get(i); |
| 2473 | if (client.getBinder() == cb) |
| 2474 | return client; |
| 2475 | } |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2476 | if (create) { |
| 2477 | client = new ScoClient(cb); |
| 2478 | mScoClients.add(client); |
| 2479 | } |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2480 | return client; |
| 2481 | } |
| 2482 | } |
| 2483 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2484 | public void clearAllScoClients(int exceptPid, boolean stopSco) { |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2485 | synchronized(mScoClients) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2486 | ScoClient savedClient = null; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2487 | int size = mScoClients.size(); |
| 2488 | for (int i = 0; i < size; i++) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2489 | ScoClient cl = mScoClients.get(i); |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2490 | if (cl.getPid() != exceptPid) { |
Eric Laurent | 854938a | 2011-02-22 12:05:20 -0800 | [diff] [blame] | 2491 | cl.clearCount(stopSco); |
| 2492 | } else { |
| 2493 | savedClient = cl; |
| 2494 | } |
| 2495 | } |
| 2496 | mScoClients.clear(); |
| 2497 | if (savedClient != null) { |
| 2498 | mScoClients.add(savedClient); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2503 | private boolean getBluetoothHeadset() { |
| 2504 | boolean result = false; |
| 2505 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2506 | if (adapter != null) { |
| 2507 | result = adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener, |
| 2508 | BluetoothProfile.HEADSET); |
| 2509 | } |
| 2510 | // If we could not get a bluetooth headset proxy, send a failure message |
| 2511 | // without delay to reset the SCO audio state and clear SCO clients. |
| 2512 | // If we could get a proxy, send a delayed failure message that will reset our state |
| 2513 | // in case we don't receive onServiceConnected(). |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2514 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2515 | SENDMSG_REPLACE, 0, 0, null, result ? BT_HEADSET_CNCT_TIMEOUT_MS : 0); |
| 2516 | return result; |
| 2517 | } |
| 2518 | |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2519 | private void disconnectBluetoothSco(int exceptPid) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2520 | synchronized(mScoClients) { |
| 2521 | checkScoAudioState(); |
| 2522 | if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL || |
| 2523 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2524 | if (mBluetoothHeadsetDevice != null) { |
| 2525 | if (mBluetoothHeadset != null) { |
| 2526 | if (!mBluetoothHeadset.stopVoiceRecognition( |
Eric Laurent | b06ac83 | 2011-05-25 15:55:18 -0700 | [diff] [blame] | 2527 | mBluetoothHeadsetDevice)) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2528 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2529 | SENDMSG_REPLACE, 0, 0, null, 0); |
| 2530 | } |
| 2531 | } else if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL && |
| 2532 | getBluetoothHeadset()) { |
| 2533 | mScoAudioState = SCO_STATE_DEACTIVATE_EXT_REQ; |
| 2534 | } |
| 2535 | } |
| 2536 | } else { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2537 | clearAllScoClients(exceptPid, true); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | private void resetBluetoothSco() { |
| 2543 | synchronized(mScoClients) { |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 2544 | clearAllScoClients(0, false); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2545 | mScoAudioState = SCO_STATE_INACTIVE; |
| 2546 | broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | private void broadcastScoConnectionState(int state) { |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 2551 | sendMsg(mAudioHandler, MSG_BROADCAST_BT_CONNECTION_STATE, |
| 2552 | SENDMSG_QUEUE, state, 0, null, 0); |
| 2553 | } |
| 2554 | |
| 2555 | private void onBroadcastScoConnectionState(int state) { |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2556 | if (state != mScoConnectionState) { |
| 2557 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); |
| 2558 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, state); |
| 2559 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, |
| 2560 | mScoConnectionState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2561 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2562 | mScoConnectionState = state; |
| 2563 | } |
| 2564 | } |
| 2565 | |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2566 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 2567 | new BluetoothProfile.ServiceListener() { |
| 2568 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2569 | BluetoothDevice btDevice; |
| 2570 | List<BluetoothDevice> deviceList; |
| 2571 | switch(profile) { |
| 2572 | case BluetoothProfile.A2DP: |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2573 | synchronized (mA2dpAvrcpLock) { |
| 2574 | mA2dp = (BluetoothA2dp) proxy; |
| 2575 | deviceList = mA2dp.getConnectedDevices(); |
| 2576 | if (deviceList.size() > 0) { |
| 2577 | btDevice = deviceList.get(0); |
| 2578 | synchronized (mConnectedDevices) { |
| 2579 | int state = mA2dp.getConnectionState(btDevice); |
| 2580 | int delay = checkSendBecomingNoisyIntent( |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2581 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 2582 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2583 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2584 | MSG_SET_A2DP_SINK_CONNECTION_STATE, |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2585 | state, |
| 2586 | 0, |
| 2587 | btDevice, |
| 2588 | delay); |
| 2589 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 2590 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 2591 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2592 | break; |
| 2593 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2594 | case BluetoothProfile.A2DP_SINK: |
| 2595 | deviceList = proxy.getConnectedDevices(); |
| 2596 | if (deviceList.size() > 0) { |
| 2597 | btDevice = deviceList.get(0); |
| 2598 | synchronized (mConnectedDevices) { |
| 2599 | int state = proxy.getConnectionState(btDevice); |
| 2600 | queueMsgUnderWakeLock(mAudioHandler, |
| 2601 | MSG_SET_A2DP_SRC_CONNECTION_STATE, |
| 2602 | state, |
| 2603 | 0, |
| 2604 | btDevice, |
| 2605 | 0 /* delay */); |
| 2606 | } |
| 2607 | } |
| 2608 | break; |
| 2609 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2610 | case BluetoothProfile.HEADSET: |
| 2611 | synchronized (mScoClients) { |
| 2612 | // Discard timeout message |
| 2613 | mAudioHandler.removeMessages(MSG_BT_HEADSET_CNCT_FAILED); |
| 2614 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 2615 | deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 2616 | if (deviceList.size() > 0) { |
| 2617 | mBluetoothHeadsetDevice = deviceList.get(0); |
| 2618 | } else { |
| 2619 | mBluetoothHeadsetDevice = null; |
| 2620 | } |
| 2621 | // Refresh SCO audio state |
| 2622 | checkScoAudioState(); |
| 2623 | // Continue pending action if any |
| 2624 | if (mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 2625 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ || |
| 2626 | mScoAudioState == SCO_STATE_DEACTIVATE_EXT_REQ) { |
| 2627 | boolean status = false; |
| 2628 | if (mBluetoothHeadsetDevice != null) { |
| 2629 | switch (mScoAudioState) { |
| 2630 | case SCO_STATE_ACTIVATE_REQ: |
| 2631 | mScoAudioState = SCO_STATE_ACTIVE_INTERNAL; |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2632 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2633 | status = mBluetoothHeadset.connectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2634 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2635 | status = mBluetoothHeadset.startScoUsingVirtualVoiceCall( |
| 2636 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2637 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2638 | status = mBluetoothHeadset.startVoiceRecognition( |
| 2639 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2640 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2641 | break; |
| 2642 | case SCO_STATE_DEACTIVATE_REQ: |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2643 | if (mScoAudioMode == SCO_MODE_RAW) { |
| 2644 | status = mBluetoothHeadset.disconnectAudio(); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2645 | } else if (mScoAudioMode == SCO_MODE_VIRTUAL_CALL) { |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2646 | status = mBluetoothHeadset.stopScoUsingVirtualVoiceCall( |
| 2647 | mBluetoothHeadsetDevice); |
Liejun Tao | f4e51d8 | 2014-07-16 11:18:29 -0700 | [diff] [blame] | 2648 | } else if (mScoAudioMode == SCO_MODE_VR) { |
| 2649 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2650 | mBluetoothHeadsetDevice); |
Eric Laurent | c18c913 | 2013-04-12 17:24:56 -0700 | [diff] [blame] | 2651 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2652 | break; |
| 2653 | case SCO_STATE_DEACTIVATE_EXT_REQ: |
| 2654 | status = mBluetoothHeadset.stopVoiceRecognition( |
| 2655 | mBluetoothHeadsetDevice); |
| 2656 | } |
| 2657 | } |
| 2658 | if (!status) { |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 2659 | sendMsg(mAudioHandler, MSG_BT_HEADSET_CNCT_FAILED, |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2660 | SENDMSG_REPLACE, 0, 0, null, 0); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2661 | } |
| 2662 | } |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2663 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2664 | break; |
| 2665 | |
| 2666 | default: |
| 2667 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2668 | } |
| 2669 | } |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 2670 | public void onServiceDisconnected(int profile) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2671 | switch(profile) { |
| 2672 | case BluetoothProfile.A2DP: |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 2673 | synchronized (mA2dpAvrcpLock) { |
| 2674 | mA2dp = null; |
| 2675 | synchronized (mConnectedDevices) { |
| 2676 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)) { |
| 2677 | makeA2dpDeviceUnavailableNow( |
| 2678 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP)); |
| 2679 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2680 | } |
| 2681 | } |
| 2682 | break; |
| 2683 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 2684 | case BluetoothProfile.A2DP_SINK: |
| 2685 | synchronized (mConnectedDevices) { |
| 2686 | if (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)) { |
| 2687 | makeA2dpSrcUnavailable( |
| 2688 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP)); |
| 2689 | } |
| 2690 | } |
| 2691 | break; |
| 2692 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 2693 | case BluetoothProfile.HEADSET: |
| 2694 | synchronized (mScoClients) { |
| 2695 | mBluetoothHeadset = null; |
| 2696 | } |
| 2697 | break; |
| 2698 | |
| 2699 | default: |
| 2700 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | }; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 2704 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2705 | private void onCheckMusicActive() { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2706 | synchronized (mSafeMediaVolumeState) { |
| 2707 | if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2708 | int device = getDeviceForStream(AudioSystem.STREAM_MUSIC); |
| 2709 | |
| 2710 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 2711 | sendMsg(mAudioHandler, |
| 2712 | MSG_CHECK_MUSIC_ACTIVE, |
| 2713 | SENDMSG_REPLACE, |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2714 | 0, |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2715 | 0, |
| 2716 | null, |
| 2717 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 2718 | int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 2719 | if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && |
| 2720 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2721 | // Approximate cumulative active music time |
| 2722 | mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS; |
| 2723 | if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX) { |
| 2724 | setSafeMediaVolumeEnabled(true); |
| 2725 | mMusicActiveMs = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2726 | } |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 2727 | saveMusicActiveMs(); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | } |
| 2733 | |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 2734 | private void saveMusicActiveMs() { |
| 2735 | mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget(); |
| 2736 | } |
| 2737 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2738 | private void onConfigureSafeVolume(boolean force) { |
| 2739 | synchronized (mSafeMediaVolumeState) { |
| 2740 | int mcc = mContext.getResources().getConfiguration().mcc; |
| 2741 | if ((mMcc != mcc) || ((mMcc == 0) && force)) { |
| 2742 | mSafeMediaVolumeIndex = mContext.getResources().getInteger( |
| 2743 | com.android.internal.R.integer.config_safe_media_volume_index) * 10; |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 2744 | boolean safeMediaVolumeEnabled = |
| 2745 | SystemProperties.getBoolean("audio.safemedia.force", false) |
| 2746 | || mContext.getResources().getBoolean( |
| 2747 | com.android.internal.R.bool.config_safe_media_volume_enabled); |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2748 | |
| 2749 | // The persisted state is either "disabled" or "active": this is the state applied |
| 2750 | // next time we boot and cannot be "inactive" |
| 2751 | int persistedState; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2752 | if (safeMediaVolumeEnabled) { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2753 | persistedState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2754 | // The state can already be "inactive" here if the user has forced it before |
| 2755 | // the 30 seconds timeout for forced configuration. In this case we don't reset |
| 2756 | // it to "active". |
| 2757 | if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) { |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 2758 | if (mMusicActiveMs == 0) { |
| 2759 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 2760 | enforceSafeMediaVolume(); |
| 2761 | } else { |
| 2762 | // We have existing playback time recorded, already confirmed. |
| 2763 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
| 2764 | } |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2765 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2766 | } else { |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2767 | persistedState = SAFE_MEDIA_VOLUME_DISABLED; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2768 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED; |
| 2769 | } |
| 2770 | mMcc = mcc; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 2771 | sendMsg(mAudioHandler, |
| 2772 | MSG_PERSIST_SAFE_VOLUME_STATE, |
| 2773 | SENDMSG_QUEUE, |
| 2774 | persistedState, |
| 2775 | 0, |
| 2776 | null, |
| 2777 | 0); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2782 | /////////////////////////////////////////////////////////////////////////// |
| 2783 | // Internal methods |
| 2784 | /////////////////////////////////////////////////////////////////////////// |
| 2785 | |
| 2786 | /** |
| 2787 | * Checks if the adjustment should change ringer mode instead of just |
| 2788 | * adjusting volume. If so, this will set the proper ringer mode and volume |
| 2789 | * indices on the stream states. |
| 2790 | */ |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2791 | private int checkForRingerModeChange(int oldIndex, int direction, int step) { |
| 2792 | int result = FLAG_ADJUST_VOLUME; |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 2793 | int ringerMode = getRingerMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2794 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2795 | switch (ringerMode) { |
| 2796 | case RINGER_MODE_NORMAL: |
| 2797 | if (direction == AudioManager.ADJUST_LOWER) { |
| 2798 | if (mHasVibrator) { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2799 | // "step" is the delta in internal index units corresponding to a |
| 2800 | // change of 1 in UI index units. |
| 2801 | // Because of rounding when rescaling from one stream index range to its alias |
| 2802 | // index range, we cannot simply test oldIndex == step: |
| 2803 | // (step <= oldIndex < 2 * step) is equivalent to: (old UI index == 1) |
| 2804 | if (step <= oldIndex && oldIndex < 2 * step) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2805 | ringerMode = RINGER_MODE_VIBRATE; |
| 2806 | } |
| 2807 | } else { |
Eric Laurent | 2448201 | 2012-05-10 09:41:17 -0700 | [diff] [blame] | 2808 | // (oldIndex < step) is equivalent to (old UI index == 0) |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 2809 | if ((oldIndex < step) |
| 2810 | && VOLUME_SETS_RINGER_MODE_SILENT |
| 2811 | && mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2812 | ringerMode = RINGER_MODE_SILENT; |
| 2813 | } |
Eric Laurent | 3d4c06f | 2011-08-15 19:58:28 -0700 | [diff] [blame] | 2814 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2816 | break; |
| 2817 | case RINGER_MODE_VIBRATE: |
| 2818 | if (!mHasVibrator) { |
| 2819 | Log.e(TAG, "checkForRingerModeChange() current ringer mode is vibrate" + |
| 2820 | "but no vibrator is present"); |
| 2821 | break; |
| 2822 | } |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2823 | if ((direction == AudioManager.ADJUST_LOWER)) { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 2824 | if (VOLUME_SETS_RINGER_MODE_SILENT |
| 2825 | && mPrevVolDirection != AudioManager.ADJUST_LOWER) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2826 | ringerMode = RINGER_MODE_SILENT; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2827 | } |
| 2828 | } else if (direction == AudioManager.ADJUST_RAISE) { |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2829 | ringerMode = RINGER_MODE_NORMAL; |
Amith Yamasani | c696a53 | 2011-10-28 17:02:37 -0700 | [diff] [blame] | 2830 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2831 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2832 | break; |
| 2833 | case RINGER_MODE_SILENT: |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 2834 | if (direction == AudioManager.ADJUST_RAISE) { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2835 | if (PREVENT_VOLUME_ADJUSTMENT_IF_SILENT) { |
| 2836 | result |= AudioManager.FLAG_SHOW_SILENT_HINT; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2837 | } else { |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2838 | if (mHasVibrator) { |
| 2839 | ringerMode = RINGER_MODE_VIBRATE; |
| 2840 | } else { |
| 2841 | ringerMode = RINGER_MODE_NORMAL; |
| 2842 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2843 | } |
Daniel Sandler | 6329bf7 | 2010-02-26 15:17:44 -0500 | [diff] [blame] | 2844 | } |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2845 | result &= ~FLAG_ADJUST_VOLUME; |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2846 | break; |
| 2847 | default: |
| 2848 | Log.e(TAG, "checkForRingerModeChange() wrong ringer mode: "+ringerMode); |
| 2849 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 2852 | setRingerMode(ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2853 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2854 | mPrevVolDirection = direction; |
| 2855 | |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 2856 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | } |
| 2858 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 2859 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | public boolean isStreamAffectedByRingerMode(int streamType) { |
Eric Laurent | 9bcf401 | 2009-06-12 06:09:28 -0700 | [diff] [blame] | 2861 | return (mRingerModeAffectedStreams & (1 << streamType)) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 2864 | private boolean isStreamMutedByRingerMode(int streamType) { |
| 2865 | return (mRingerModeMutedStreams & (1 << streamType)) != 0; |
| 2866 | } |
| 2867 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 2868 | boolean updateRingerModeAffectedStreams() { |
| 2869 | int ringerModeAffectedStreams; |
| 2870 | // make sure settings for ringer mode are consistent with device type: non voice capable |
| 2871 | // devices (tablets) include media stream in silent mode whereas phones don't. |
| 2872 | ringerModeAffectedStreams = Settings.System.getIntForUser(mContentResolver, |
| 2873 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 2874 | ((1 << AudioSystem.STREAM_RING)|(1 << AudioSystem.STREAM_NOTIFICATION)| |
| 2875 | (1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)), |
| 2876 | UserHandle.USER_CURRENT); |
| 2877 | |
| 2878 | // ringtone, notification and system streams are always affected by ringer mode |
| 2879 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_RING)| |
| 2880 | (1 << AudioSystem.STREAM_NOTIFICATION)| |
| 2881 | (1 << AudioSystem.STREAM_SYSTEM); |
| 2882 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2883 | switch (mPlatformType) { |
| 2884 | case PLATFORM_VOICE: |
| 2885 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_MUSIC); |
| 2886 | break; |
| 2887 | case PLATFORM_TELEVISION: |
| 2888 | ringerModeAffectedStreams = 0; |
| 2889 | break; |
| 2890 | default: |
| 2891 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_MUSIC); |
| 2892 | break; |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 2893 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2894 | |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 2895 | synchronized (mCameraSoundForced) { |
| 2896 | if (mCameraSoundForced) { |
| 2897 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 2898 | } else { |
| 2899 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 2900 | } |
| 2901 | } |
| 2902 | if (mStreamVolumeAlias[AudioSystem.STREAM_DTMF] == AudioSystem.STREAM_RING) { |
| 2903 | ringerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF); |
| 2904 | } else { |
| 2905 | ringerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF); |
| 2906 | } |
| 2907 | |
| 2908 | if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { |
| 2909 | Settings.System.putIntForUser(mContentResolver, |
| 2910 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, |
| 2911 | ringerModeAffectedStreams, |
| 2912 | UserHandle.USER_CURRENT); |
| 2913 | mRingerModeAffectedStreams = ringerModeAffectedStreams; |
| 2914 | return true; |
| 2915 | } |
| 2916 | return false; |
| 2917 | } |
| 2918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | public boolean isStreamAffectedByMute(int streamType) { |
| 2920 | return (mMuteAffectedStreams & (1 << streamType)) != 0; |
| 2921 | } |
| 2922 | |
| 2923 | private void ensureValidDirection(int direction) { |
| 2924 | if (direction < AudioManager.ADJUST_LOWER || direction > AudioManager.ADJUST_RAISE) { |
| 2925 | throw new IllegalArgumentException("Bad direction " + direction); |
| 2926 | } |
| 2927 | } |
| 2928 | |
Lei Zhang | 6c79897 | 2012-03-02 11:40:12 -0800 | [diff] [blame] | 2929 | private void ensureValidSteps(int steps) { |
| 2930 | if (Math.abs(steps) > MAX_BATCH_VOLUME_ADJUST_STEPS) { |
| 2931 | throw new IllegalArgumentException("Bad volume adjust steps " + steps); |
| 2932 | } |
| 2933 | } |
| 2934 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | private void ensureValidStreamType(int streamType) { |
| 2936 | if (streamType < 0 || streamType >= mStreamStates.length) { |
| 2937 | throw new IllegalArgumentException("Bad stream type " + streamType); |
| 2938 | } |
| 2939 | } |
| 2940 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2941 | private boolean isInCommunication() { |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 2942 | boolean isInAPhoneCall = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2943 | |
Santos Cordon | 9eb4593 | 2014-06-27 12:28:43 -0700 | [diff] [blame] | 2944 | TelecommManager telecommManager = |
| 2945 | (TelecommManager) mContext.getSystemService(Context.TELECOMM_SERVICE); |
| 2946 | isInAPhoneCall = telecommManager.isInAPhoneCall(); |
| 2947 | |
| 2948 | return (isInAPhoneCall || getMode() == AudioManager.MODE_IN_COMMUNICATION); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2949 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2950 | |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 2951 | /** |
| 2952 | * For code clarity for getActiveStreamType(int) |
| 2953 | * @param delay_ms max time since last STREAM_MUSIC activity to consider |
| 2954 | * @return true if STREAM_MUSIC is active in streams handled by AudioFlinger now or |
| 2955 | * in the last "delay_ms" ms. |
| 2956 | */ |
| 2957 | private boolean isAfMusicActiveRecently(int delay_ms) { |
| 2958 | return AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, delay_ms) |
| 2959 | || AudioSystem.isStreamActiveRemotely(AudioSystem.STREAM_MUSIC, delay_ms); |
| 2960 | } |
| 2961 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2962 | private int getActiveStreamType(int suggestedStreamType) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2963 | switch (mPlatformType) { |
| 2964 | case PLATFORM_VOICE: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 2965 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2966 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 2967 | == AudioSystem.FORCE_BT_SCO) { |
| 2968 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO..."); |
| 2969 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 2970 | } else { |
| 2971 | // Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL..."); |
| 2972 | return AudioSystem.STREAM_VOICE_CALL; |
| 2973 | } |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2974 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 2975 | if (isAfMusicActiveRecently(DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 2976 | if (DEBUG_VOL) |
| 2977 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 2978 | return AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 2979 | } else |
| 2980 | if (mMediaFocusControl.checkUpdateRemoteStateIfActive(AudioSystem.STREAM_MUSIC)) |
| 2981 | { |
| 2982 | if (DEBUG_VOL) |
| 2983 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_REMOTE_MUSIC"); |
| 2984 | return STREAM_REMOTE_MUSIC; |
| 2985 | } else { |
| 2986 | if (DEBUG_VOL) |
| 2987 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_RING b/c default"); |
| 2988 | return AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 2989 | } |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 2990 | } else if (isAfMusicActiveRecently(0)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 2991 | if (DEBUG_VOL) |
| 2992 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_MUSIC stream active"); |
| 2993 | return AudioSystem.STREAM_MUSIC; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 2994 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 2995 | break; |
| 2996 | case PLATFORM_TELEVISION: |
| 2997 | if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 2998 | if (isAfMusicActiveRecently(DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { |
| 2999 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3000 | return AudioSystem.STREAM_MUSIC; |
| 3001 | } else if (mMediaFocusControl.checkUpdateRemoteStateIfActive( |
| 3002 | AudioSystem.STREAM_MUSIC)) { |
| 3003 | if (DEBUG_VOL) |
| 3004 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_REMOTE_MUSIC"); |
| 3005 | return STREAM_REMOTE_MUSIC; |
| 3006 | } else { |
| 3007 | if (DEBUG_VOL) Log.v(TAG, |
| 3008 | "getActiveStreamType: using STREAM_MUSIC as default"); |
| 3009 | return AudioSystem.STREAM_MUSIC; |
| 3010 | } |
| 3011 | } |
| 3012 | break; |
| 3013 | default: |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3014 | if (isInCommunication()) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3015 | if (AudioSystem.getForceUse(AudioSystem.FOR_COMMUNICATION) |
| 3016 | == AudioSystem.FORCE_BT_SCO) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3017 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_BLUETOOTH_SCO"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3018 | return AudioSystem.STREAM_BLUETOOTH_SCO; |
| 3019 | } else { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3020 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_VOICE_CALL"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3021 | return AudioSystem.STREAM_VOICE_CALL; |
| 3022 | } |
| 3023 | } else if (AudioSystem.isStreamActive(AudioSystem.STREAM_NOTIFICATION, |
Eric Laurent | 9903e26 | 2012-09-21 18:10:32 -0700 | [diff] [blame] | 3024 | DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS) || |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3025 | AudioSystem.isStreamActive(AudioSystem.STREAM_RING, |
Eric Laurent | 9903e26 | 2012-09-21 18:10:32 -0700 | [diff] [blame] | 3026 | DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3027 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Forcing STREAM_NOTIFICATION"); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3028 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3029 | } else if (suggestedStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
Jean-Michel Trivi | fca1e60 | 2013-10-10 18:12:59 -0700 | [diff] [blame] | 3030 | if (isAfMusicActiveRecently(DEFAULT_STREAM_TYPE_OVERRIDE_DELAY_MS)) { |
| 3031 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: forcing STREAM_MUSIC"); |
| 3032 | return AudioSystem.STREAM_MUSIC; |
| 3033 | } else |
| 3034 | if (mMediaFocusControl.checkUpdateRemoteStateIfActive(AudioSystem.STREAM_MUSIC)) |
| 3035 | { |
| 3036 | if (DEBUG_VOL) |
| 3037 | Log.v(TAG, "getActiveStreamType: Forcing STREAM_REMOTE_MUSIC"); |
| 3038 | return STREAM_REMOTE_MUSIC; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3039 | } else { |
John Spurlock | eb1d88d | 2014-07-19 14:49:19 -0400 | [diff] [blame] | 3040 | if (DEBUG_VOL) Log.v(TAG, |
| 3041 | "getActiveStreamType: using STREAM_NOTIFICATION as default"); |
| 3042 | return AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3043 | } |
Joe Onorato | c7fcba4 | 2011-01-05 16:53:11 -0800 | [diff] [blame] | 3044 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3045 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3047 | if (DEBUG_VOL) Log.v(TAG, "getActiveStreamType: Returning suggested type " |
| 3048 | + suggestedStreamType); |
| 3049 | return suggestedStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3050 | } |
| 3051 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3052 | private void broadcastRingerMode(int ringerMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | // Send sticky broadcast |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3054 | Intent broadcast = new Intent(AudioManager.RINGER_MODE_CHANGED_ACTION); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3055 | broadcast.putExtra(AudioManager.EXTRA_RINGER_MODE, ringerMode); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 3056 | broadcast.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 3057 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3058 | sendStickyBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | private void broadcastVibrateSetting(int vibrateType) { |
| 3062 | // Send broadcast |
| 3063 | if (ActivityManagerNative.isSystemReady()) { |
| 3064 | Intent broadcast = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION); |
| 3065 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_TYPE, vibrateType); |
| 3066 | broadcast.putExtra(AudioManager.EXTRA_VIBRATE_SETTING, getVibrateSetting(vibrateType)); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3067 | sendBroadcastToAll(broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | // Message helper methods |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3072 | /** |
| 3073 | * Queue a message on the given handler's message queue, after acquiring the service wake lock. |
| 3074 | * Note that the wake lock needs to be released after the message has been handled. |
| 3075 | */ |
| 3076 | private void queueMsgUnderWakeLock(Handler handler, int msg, |
| 3077 | int arg1, int arg2, Object obj, int delay) { |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3078 | final long ident = Binder.clearCallingIdentity(); |
| 3079 | // Always acquire the wake lock as AudioService because it is released by the |
| 3080 | // message handler. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 3081 | mAudioEventWakeLock.acquire(); |
Eric Laurent | a4dfbdc5 | 2013-10-01 11:53:51 -0700 | [diff] [blame] | 3082 | Binder.restoreCallingIdentity(ident); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3083 | sendMsg(handler, msg, SENDMSG_QUEUE, arg1, arg2, obj, delay); |
| 3084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3085 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3086 | private static void sendMsg(Handler handler, int msg, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3087 | 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] | 3088 | |
| 3089 | if (existingMsgPolicy == SENDMSG_REPLACE) { |
| 3090 | handler.removeMessages(msg); |
| 3091 | } else if (existingMsgPolicy == SENDMSG_NOOP && handler.hasMessages(msg)) { |
| 3092 | return; |
| 3093 | } |
| 3094 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3095 | handler.sendMessageDelayed(handler.obtainMessage(msg, arg1, arg2, obj), delay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | boolean checkAudioSettingsPermission(String method) { |
| 3099 | if (mContext.checkCallingOrSelfPermission("android.permission.MODIFY_AUDIO_SETTINGS") |
| 3100 | == PackageManager.PERMISSION_GRANTED) { |
| 3101 | return true; |
| 3102 | } |
| 3103 | String msg = "Audio Settings Permission Denial: " + method + " from pid=" |
| 3104 | + Binder.getCallingPid() |
| 3105 | + ", uid=" + Binder.getCallingUid(); |
| 3106 | Log.w(TAG, msg); |
| 3107 | return false; |
| 3108 | } |
| 3109 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3110 | private int getDeviceForStream(int stream) { |
| 3111 | int device = AudioSystem.getDevicesForStream(stream); |
| 3112 | if ((device & (device - 1)) != 0) { |
| 3113 | // Multiple device selection is either: |
| 3114 | // - speaker + one other device: give priority to speaker in this case. |
| 3115 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 3116 | // retain the device on the A2DP output as the other must not correspond to an active |
| 3117 | // selection if not the speaker. |
| 3118 | if ((device & AudioSystem.DEVICE_OUT_SPEAKER) != 0) { |
| 3119 | device = AudioSystem.DEVICE_OUT_SPEAKER; |
| 3120 | } else { |
| 3121 | device &= AudioSystem.DEVICE_OUT_ALL_A2DP; |
| 3122 | } |
| 3123 | } |
| 3124 | return device; |
| 3125 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3126 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3127 | public void setWiredDeviceConnectionState(int device, int state, String name) { |
| 3128 | synchronized (mConnectedDevices) { |
| 3129 | int delay = checkSendBecomingNoisyIntent(device, state); |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3130 | queueMsgUnderWakeLock(mAudioHandler, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3131 | MSG_SET_WIRED_DEVICE_CONNECTION_STATE, |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3132 | device, |
| 3133 | state, |
| 3134 | name, |
| 3135 | delay); |
| 3136 | } |
| 3137 | } |
| 3138 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3139 | public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state, int profile) |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3140 | { |
| 3141 | int delay; |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3142 | if (profile != BluetoothProfile.A2DP && profile != BluetoothProfile.A2DP_SINK) { |
| 3143 | throw new IllegalArgumentException("invalid profile " + profile); |
| 3144 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3145 | synchronized (mConnectedDevices) { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3146 | if (profile == BluetoothProfile.A2DP) { |
| 3147 | delay = checkSendBecomingNoisyIntent(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 3148 | (state == BluetoothA2dp.STATE_CONNECTED) ? 1 : 0); |
| 3149 | } else { |
| 3150 | delay = 0; |
| 3151 | } |
Jean-Michel Trivi | 2d8dab5 | 2012-05-30 18:13:59 -0700 | [diff] [blame] | 3152 | queueMsgUnderWakeLock(mAudioHandler, |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 3153 | (profile == BluetoothProfile.A2DP ? |
| 3154 | MSG_SET_A2DP_SINK_CONNECTION_STATE : MSG_SET_A2DP_SRC_CONNECTION_STATE), |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 3155 | state, |
| 3156 | 0, |
| 3157 | device, |
| 3158 | delay); |
| 3159 | } |
| 3160 | return delay; |
| 3161 | } |
| 3162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3163 | /////////////////////////////////////////////////////////////////////////// |
| 3164 | // Inner classes |
| 3165 | /////////////////////////////////////////////////////////////////////////// |
| 3166 | |
| 3167 | public class VolumeStreamState { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3168 | private final int mStreamType; |
| 3169 | |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3170 | private String mVolumeIndexSettingName; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3171 | private int mIndexMax; |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3172 | private final ConcurrentHashMap<Integer, Integer> mIndex = |
| 3173 | new ConcurrentHashMap<Integer, Integer>(8, 0.75f, 4); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3174 | private ArrayList<VolumeDeathHandler> mDeathHandlers; //handles mute/solo clients death |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3175 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3176 | private VolumeStreamState(String settingName, int streamType) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3178 | mVolumeIndexSettingName = settingName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | |
| 3180 | mStreamType = streamType; |
Jared Suttles | 5982013 | 2009-08-13 21:50:52 -0500 | [diff] [blame] | 3181 | mIndexMax = MAX_STREAM_VOLUME[streamType]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3182 | AudioSystem.initStreamVolume(streamType, 0, mIndexMax); |
| 3183 | mIndexMax *= 10; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3184 | |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3185 | // mDeathHandlers must be created before calling readSettings() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3186 | mDeathHandlers = new ArrayList<VolumeDeathHandler>(); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3187 | |
| 3188 | readSettings(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3189 | } |
| 3190 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3191 | public String getSettingNameForDevice(int device) { |
| 3192 | String name = mVolumeIndexSettingName; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 3193 | String suffix = AudioSystem.getOutputDeviceName(device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3194 | if (suffix.isEmpty()) { |
| 3195 | return name; |
| 3196 | } |
| 3197 | return name + "_" + suffix; |
Jean-Michel Trivi | 11a74a7 | 2009-10-27 17:39:30 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3200 | public void readSettings() { |
| 3201 | synchronized (VolumeStreamState.class) { |
| 3202 | // force maximum volume on all streams if fixed volume property is set |
| 3203 | if (mUseFixedVolume) { |
| 3204 | mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); |
| 3205 | return; |
| 3206 | } |
| 3207 | // do not read system stream volume from settings: this stream is always aliased |
| 3208 | // to another stream type and its volume is never persisted. Values in settings can |
| 3209 | // only be stale values |
| 3210 | if ((mStreamType == AudioSystem.STREAM_SYSTEM) || |
| 3211 | (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { |
| 3212 | int index = 10 * AudioManager.DEFAULT_STREAM_VOLUME[mStreamType]; |
| 3213 | synchronized (mCameraSoundForced) { |
| 3214 | if (mCameraSoundForced) { |
| 3215 | index = mIndexMax; |
| 3216 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3217 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3218 | mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, index); |
| 3219 | return; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3220 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3221 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3222 | int remainingDevices = AudioSystem.DEVICE_OUT_ALL; |
| 3223 | |
| 3224 | for (int i = 0; remainingDevices != 0; i++) { |
| 3225 | int device = (1 << i); |
| 3226 | if ((device & remainingDevices) == 0) { |
| 3227 | continue; |
| 3228 | } |
| 3229 | remainingDevices &= ~device; |
| 3230 | |
| 3231 | // retrieve current volume for device |
| 3232 | String name = getSettingNameForDevice(device); |
| 3233 | // if no volume stored for current stream and device, use default volume if default |
| 3234 | // device, continue otherwise |
| 3235 | int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? |
| 3236 | AudioManager.DEFAULT_STREAM_VOLUME[mStreamType] : -1; |
| 3237 | int index = Settings.System.getIntForUser( |
| 3238 | mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); |
| 3239 | if (index == -1) { |
| 3240 | continue; |
| 3241 | } |
| 3242 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3243 | mIndex.put(device, getValidIndex(10 * index)); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3244 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3246 | } |
| 3247 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3248 | public void applyDeviceVolume(int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3249 | int index; |
| 3250 | if (isMuted()) { |
| 3251 | index = 0; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3252 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 3253 | mAvrcpAbsVolSupported) { |
| 3254 | index = (mIndexMax + 5)/10; |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3255 | } else { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3256 | index = (getIndex(device) + 5)/10; |
| 3257 | } |
| 3258 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3260 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3261 | public void applyAllVolumes() { |
| 3262 | synchronized (VolumeStreamState.class) { |
| 3263 | // apply default volume first: by convention this will reset all |
| 3264 | // devices volumes in audio policy manager to the supplied value |
| 3265 | int index; |
| 3266 | if (isMuted()) { |
| 3267 | index = 0; |
| 3268 | } else { |
| 3269 | index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; |
| 3270 | } |
| 3271 | AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); |
| 3272 | // then apply device specific volumes |
| 3273 | Set set = mIndex.entrySet(); |
| 3274 | Iterator i = set.iterator(); |
| 3275 | while (i.hasNext()) { |
| 3276 | Map.Entry entry = (Map.Entry)i.next(); |
| 3277 | int device = ((Integer)entry.getKey()).intValue(); |
| 3278 | if (device != AudioSystem.DEVICE_OUT_DEFAULT) { |
| 3279 | if (isMuted()) { |
| 3280 | index = 0; |
| 3281 | } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && |
| 3282 | mAvrcpAbsVolSupported) { |
| 3283 | index = (mIndexMax + 5)/10; |
| 3284 | } else { |
| 3285 | index = ((Integer)entry.getValue() + 5)/10; |
| 3286 | } |
| 3287 | AudioSystem.setStreamVolumeIndex(mStreamType, index, device); |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3288 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | public boolean adjustIndex(int deltaIndex, int device) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3294 | return setIndex(getIndex(device) + deltaIndex, |
| 3295 | device); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3296 | } |
| 3297 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3298 | public boolean setIndex(int index, int device) { |
| 3299 | synchronized (VolumeStreamState.class) { |
| 3300 | int oldIndex = getIndex(device); |
| 3301 | index = getValidIndex(index); |
| 3302 | synchronized (mCameraSoundForced) { |
| 3303 | if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { |
| 3304 | index = mIndexMax; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3305 | } |
| 3306 | } |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3307 | mIndex.put(device, index); |
| 3308 | |
| 3309 | if (oldIndex != index) { |
| 3310 | // Apply change to all streams using this one as alias |
| 3311 | // if changing volume of current device, also change volume of current |
| 3312 | // device on aliased stream |
| 3313 | boolean currentDevice = (device == getDeviceForStream(mStreamType)); |
| 3314 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3315 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3316 | if (streamType != mStreamType && |
| 3317 | mStreamVolumeAlias[streamType] == mStreamType) { |
| 3318 | int scaledIndex = rescaleIndex(index, mStreamType, streamType); |
| 3319 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3320 | device); |
| 3321 | if (currentDevice) { |
| 3322 | mStreamStates[streamType].setIndex(scaledIndex, |
| 3323 | getDeviceForStream(streamType)); |
| 3324 | } |
| 3325 | } |
| 3326 | } |
| 3327 | return true; |
| 3328 | } else { |
| 3329 | return false; |
| 3330 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | } |
| 3332 | } |
| 3333 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3334 | public int getIndex(int device) { |
| 3335 | synchronized (VolumeStreamState.class) { |
| 3336 | Integer index = mIndex.get(device); |
| 3337 | if (index == null) { |
| 3338 | // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT |
| 3339 | index = mIndex.get(AudioSystem.DEVICE_OUT_DEFAULT); |
| 3340 | } |
| 3341 | return index.intValue(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3342 | } |
Eric Laurent | 5b4e654 | 2010-03-19 20:02:21 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3345 | public int getMaxIndex() { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3346 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3347 | } |
| 3348 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3349 | public void setAllIndexes(VolumeStreamState srcStream) { |
| 3350 | synchronized (VolumeStreamState.class) { |
| 3351 | int srcStreamType = srcStream.getStreamType(); |
| 3352 | // apply default device volume from source stream to all devices first in case |
| 3353 | // some devices are present in this stream state but not in source stream state |
| 3354 | int index = srcStream.getIndex(AudioSystem.DEVICE_OUT_DEFAULT); |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 3355 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3356 | Set set = mIndex.entrySet(); |
| 3357 | Iterator i = set.iterator(); |
| 3358 | while (i.hasNext()) { |
| 3359 | Map.Entry entry = (Map.Entry)i.next(); |
| 3360 | entry.setValue(index); |
| 3361 | } |
| 3362 | // Now apply actual volume for devices in source stream state |
| 3363 | set = srcStream.mIndex.entrySet(); |
| 3364 | i = set.iterator(); |
| 3365 | while (i.hasNext()) { |
| 3366 | Map.Entry entry = (Map.Entry)i.next(); |
| 3367 | int device = ((Integer)entry.getKey()).intValue(); |
| 3368 | index = ((Integer)entry.getValue()).intValue(); |
| 3369 | index = rescaleIndex(index, srcStreamType, mStreamType); |
Eric Laurent | 33902db | 2012-10-07 16:15:07 -0700 | [diff] [blame] | 3370 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3371 | setIndex(index, device); |
| 3372 | } |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3373 | } |
| 3374 | } |
| 3375 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3376 | public void setAllIndexesToMax() { |
| 3377 | synchronized (VolumeStreamState.class) { |
| 3378 | Set set = mIndex.entrySet(); |
| 3379 | Iterator i = set.iterator(); |
| 3380 | while (i.hasNext()) { |
| 3381 | Map.Entry entry = (Map.Entry)i.next(); |
| 3382 | entry.setValue(mIndexMax); |
| 3383 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3384 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Eric Laurent | fdbee86 | 2014-05-12 15:26:12 -0700 | [diff] [blame] | 3387 | public void mute(IBinder cb, boolean state) { |
| 3388 | synchronized (VolumeStreamState.class) { |
| 3389 | VolumeDeathHandler handler = getDeathHandler(cb, state); |
| 3390 | if (handler == null) { |
| 3391 | Log.e(TAG, "Could not get client death handler for stream: "+mStreamType); |
| 3392 | return; |
| 3393 | } |
| 3394 | handler.mute(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3398 | public int getStreamType() { |
| 3399 | return mStreamType; |
| 3400 | } |
| 3401 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3402 | public void checkFixedVolumeDevices() { |
| 3403 | synchronized (VolumeStreamState.class) { |
| 3404 | // ignore settings for fixed volume devices: volume should always be at max or 0 |
| 3405 | if (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) { |
| 3406 | Set set = mIndex.entrySet(); |
| 3407 | Iterator i = set.iterator(); |
| 3408 | while (i.hasNext()) { |
| 3409 | Map.Entry entry = (Map.Entry)i.next(); |
| 3410 | int device = ((Integer)entry.getKey()).intValue(); |
| 3411 | int index = ((Integer)entry.getValue()).intValue(); |
| 3412 | if (((device & mFixedVolumeDevices) != 0) && index != 0) { |
| 3413 | entry.setValue(mIndexMax); |
| 3414 | } |
| 3415 | applyDeviceVolume(device); |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3421 | private int getValidIndex(int index) { |
| 3422 | if (index < 0) { |
| 3423 | return 0; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3424 | } else if (mUseFixedVolume || index > mIndexMax) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3425 | return mIndexMax; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | return index; |
| 3429 | } |
| 3430 | |
| 3431 | private class VolumeDeathHandler implements IBinder.DeathRecipient { |
| 3432 | private IBinder mICallback; // To be notified of client's death |
| 3433 | private int mMuteCount; // Number of active mutes for this client |
| 3434 | |
| 3435 | VolumeDeathHandler(IBinder cb) { |
| 3436 | mICallback = cb; |
| 3437 | } |
| 3438 | |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3439 | // must be called while synchronized on parent VolumeStreamState |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3440 | public void mute(boolean state) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3441 | boolean updateVolume = false; |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3442 | if (state) { |
| 3443 | if (mMuteCount == 0) { |
| 3444 | // Register for client death notification |
| 3445 | try { |
| 3446 | // mICallback can be 0 if muted by AudioService |
| 3447 | if (mICallback != null) { |
| 3448 | mICallback.linkToDeath(this, 0); |
| 3449 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3450 | VolumeStreamState.this.mDeathHandlers.add(this); |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3451 | // If the stream is not yet muted by any client, set level to 0 |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3452 | if (!VolumeStreamState.this.isMuted()) { |
| 3453 | updateVolume = true; |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3454 | } |
| 3455 | } catch (RemoteException e) { |
| 3456 | // Client has died! |
| 3457 | binderDied(); |
| 3458 | return; |
| 3459 | } |
| 3460 | } else { |
| 3461 | Log.w(TAG, "stream: "+mStreamType+" was already muted by this client"); |
| 3462 | } |
| 3463 | mMuteCount++; |
| 3464 | } else { |
| 3465 | if (mMuteCount == 0) { |
| 3466 | Log.e(TAG, "unexpected unmute for stream: "+mStreamType); |
| 3467 | } else { |
| 3468 | mMuteCount--; |
| 3469 | if (mMuteCount == 0) { |
| 3470 | // Unregister from client death notification |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3471 | VolumeStreamState.this.mDeathHandlers.remove(this); |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3472 | // mICallback can be 0 if muted by AudioService |
| 3473 | if (mICallback != null) { |
| 3474 | mICallback.unlinkToDeath(this, 0); |
| 3475 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3476 | if (!VolumeStreamState.this.isMuted()) { |
| 3477 | updateVolume = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3478 | } |
| 3479 | } |
| 3480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3481 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3482 | if (updateVolume) { |
| 3483 | sendMsg(mAudioHandler, |
| 3484 | MSG_SET_ALL_VOLUMES, |
| 3485 | SENDMSG_QUEUE, |
| 3486 | 0, |
| 3487 | 0, |
| 3488 | VolumeStreamState.this, 0); |
| 3489 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | public void binderDied() { |
| 3493 | Log.w(TAG, "Volume service client died for stream: "+mStreamType); |
| 3494 | if (mMuteCount != 0) { |
| 3495 | // Reset all active mute requests from this client. |
| 3496 | mMuteCount = 1; |
| 3497 | mute(false); |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3502 | private synchronized int muteCount() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3503 | int count = 0; |
| 3504 | int size = mDeathHandlers.size(); |
| 3505 | for (int i = 0; i < size; i++) { |
| 3506 | count += mDeathHandlers.get(i).mMuteCount; |
| 3507 | } |
| 3508 | return count; |
| 3509 | } |
| 3510 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3511 | private synchronized boolean isMuted() { |
| 3512 | return muteCount() != 0; |
| 3513 | } |
| 3514 | |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3515 | // only called by mute() which is already synchronized |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3516 | private VolumeDeathHandler getDeathHandler(IBinder cb, boolean state) { |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3517 | VolumeDeathHandler handler; |
| 3518 | int size = mDeathHandlers.size(); |
| 3519 | for (int i = 0; i < size; i++) { |
| 3520 | handler = mDeathHandlers.get(i); |
| 3521 | if (cb == handler.mICallback) { |
| 3522 | return handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | } |
Eric Laurent | 3172d5e | 2012-05-09 11:38:16 -0700 | [diff] [blame] | 3525 | // If this is the first mute request for this client, create a new |
| 3526 | // client death handler. Otherwise, it is an out of sequence unmute request. |
| 3527 | if (state) { |
| 3528 | handler = new VolumeDeathHandler(cb); |
| 3529 | } else { |
| 3530 | Log.w(TAG, "stream was not muted by this client"); |
| 3531 | handler = null; |
| 3532 | } |
| 3533 | return handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3534 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3535 | |
| 3536 | private void dump(PrintWriter pw) { |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3537 | pw.print(" Mute count: "); |
| 3538 | pw.println(muteCount()); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3539 | pw.print(" Current: "); |
| 3540 | Set set = mIndex.entrySet(); |
| 3541 | Iterator i = set.iterator(); |
| 3542 | while (i.hasNext()) { |
| 3543 | Map.Entry entry = (Map.Entry)i.next(); |
| 3544 | pw.print(Integer.toHexString(((Integer)entry.getKey()).intValue()) |
| 3545 | + ": " + ((((Integer)entry.getValue()).intValue() + 5) / 10)+", "); |
| 3546 | } |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 3547 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | /** Thread that handles native AudioSystem control. */ |
| 3551 | private class AudioSystemThread extends Thread { |
| 3552 | AudioSystemThread() { |
| 3553 | super("AudioService"); |
| 3554 | } |
| 3555 | |
| 3556 | @Override |
| 3557 | public void run() { |
| 3558 | // Set this thread up so the handler will work on it |
| 3559 | Looper.prepare(); |
| 3560 | |
| 3561 | synchronized(AudioService.this) { |
| 3562 | mAudioHandler = new AudioHandler(); |
| 3563 | |
| 3564 | // Notify that the handler has been created |
| 3565 | AudioService.this.notify(); |
| 3566 | } |
| 3567 | |
| 3568 | // Listen for volume change requests that are set by VolumePanel |
| 3569 | Looper.loop(); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | /** Handles internal volume messages in separate volume thread. */ |
| 3574 | private class AudioHandler extends Handler { |
| 3575 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3576 | private void setDeviceVolume(VolumeStreamState streamState, int device) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3577 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3578 | // Apply volume |
| 3579 | streamState.applyDeviceVolume(device); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3580 | |
| 3581 | // Apply change to all streams using this one as alias |
| 3582 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3583 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3584 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3585 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 3586 | // Make sure volume is also maxed out on A2DP device for aliased stream |
| 3587 | // that may have a different device selected |
| 3588 | int streamDevice = getDeviceForStream(streamType); |
| 3589 | if ((device != streamDevice) && mAvrcpAbsVolSupported && |
| 3590 | ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) { |
| 3591 | mStreamStates[streamType].applyDeviceVolume(device); |
| 3592 | } |
| 3593 | mStreamStates[streamType].applyDeviceVolume(streamDevice); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3594 | } |
| 3595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3596 | |
| 3597 | // Post a persist volume msg |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3598 | sendMsg(mAudioHandler, |
| 3599 | MSG_PERSIST_VOLUME, |
Eric Laurent | 98ad9b9 | 2012-02-15 17:21:37 -0800 | [diff] [blame] | 3600 | SENDMSG_QUEUE, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3601 | device, |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3602 | 0, |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3603 | streamState, |
| 3604 | PERSIST_DELAY); |
| 3605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3608 | private void setAllVolumes(VolumeStreamState streamState) { |
| 3609 | |
| 3610 | // Apply volume |
| 3611 | streamState.applyAllVolumes(); |
| 3612 | |
| 3613 | // Apply change to all streams using this one as alias |
| 3614 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3615 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
| 3616 | if (streamType != streamState.mStreamType && |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 3617 | mStreamVolumeAlias[streamType] == streamState.mStreamType) { |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3618 | mStreamStates[streamType].applyAllVolumes(); |
| 3619 | } |
| 3620 | } |
| 3621 | } |
| 3622 | |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3623 | private void persistVolume(VolumeStreamState streamState, int device) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3624 | if (mUseFixedVolume) { |
| 3625 | return; |
| 3626 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3627 | if (isPlatformTelevision() && (streamState.mStreamType != AudioSystem.STREAM_MUSIC)) { |
| 3628 | return; |
| 3629 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3630 | System.putIntForUser(mContentResolver, |
| 3631 | streamState.getSettingNameForDevice(device), |
| 3632 | (streamState.getIndex(device) + 5)/ 10, |
| 3633 | UserHandle.USER_CURRENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3634 | } |
| 3635 | |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3636 | private void persistRingerMode(int ringerMode) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3637 | if (mUseFixedVolume) { |
| 3638 | return; |
| 3639 | } |
Jeff Sharkey | 8d9a1f6 | 2012-10-18 15:38:14 -0700 | [diff] [blame] | 3640 | Settings.Global.putInt(mContentResolver, Settings.Global.MODE_RINGER, ringerMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3641 | } |
| 3642 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3643 | private boolean onLoadSoundEffects() { |
| 3644 | int status; |
| 3645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3646 | synchronized (mSoundEffectsLock) { |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 3647 | if (!mSystemReady) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3648 | Log.w(TAG, "onLoadSoundEffects() called before boot complete"); |
| 3649 | return false; |
| 3650 | } |
| 3651 | |
| 3652 | if (mSoundPool != null) { |
| 3653 | return true; |
| 3654 | } |
| 3655 | |
| 3656 | loadTouchSoundAssets(); |
| 3657 | |
Jean-Michel Trivi | 55a30c4 | 2014-07-20 17:56:11 -0700 | [diff] [blame] | 3658 | mSoundPool = new SoundPool.Builder() |
| 3659 | .setMaxStreams(NUM_SOUNDPOOL_CHANNELS) |
| 3660 | .setAudioAttributes(new AudioAttributes.Builder() |
| 3661 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 3662 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 3663 | .build()) |
| 3664 | .build(); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3665 | mSoundPoolCallBack = null; |
| 3666 | mSoundPoolListenerThread = new SoundPoolListenerThread(); |
| 3667 | mSoundPoolListenerThread.start(); |
| 3668 | int attempts = 3; |
| 3669 | while ((mSoundPoolCallBack == null) && (attempts-- > 0)) { |
| 3670 | try { |
| 3671 | // Wait for mSoundPoolCallBack to be set by the other thread |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3672 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3673 | } catch (InterruptedException e) { |
| 3674 | Log.w(TAG, "Interrupted while waiting sound pool listener thread."); |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | if (mSoundPoolCallBack == null) { |
| 3679 | Log.w(TAG, "onLoadSoundEffects() SoundPool listener or thread creation error"); |
| 3680 | if (mSoundPoolLooper != null) { |
| 3681 | mSoundPoolLooper.quit(); |
| 3682 | mSoundPoolLooper = null; |
| 3683 | } |
| 3684 | mSoundPoolListenerThread = null; |
| 3685 | mSoundPool.release(); |
| 3686 | mSoundPool = null; |
| 3687 | return false; |
| 3688 | } |
| 3689 | /* |
| 3690 | * poolId table: The value -1 in this table indicates that corresponding |
| 3691 | * file (same index in SOUND_EFFECT_FILES[] has not been loaded. |
| 3692 | * Once loaded, the value in poolId is the sample ID and the same |
| 3693 | * sample can be reused for another effect using the same file. |
| 3694 | */ |
| 3695 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3696 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3697 | poolId[fileIdx] = -1; |
| 3698 | } |
| 3699 | /* |
| 3700 | * Effects whose value in SOUND_EFFECT_FILES_MAP[effect][1] is -1 must be loaded. |
| 3701 | * If load succeeds, value in SOUND_EFFECT_FILES_MAP[effect][1] is > 0: |
| 3702 | * this indicates we have a valid sample loaded for this effect. |
| 3703 | */ |
| 3704 | |
| 3705 | int numSamples = 0; |
| 3706 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3707 | // Do not load sample if this effect uses the MediaPlayer |
| 3708 | if (SOUND_EFFECT_FILES_MAP[effect][1] == 0) { |
| 3709 | continue; |
| 3710 | } |
| 3711 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == -1) { |
| 3712 | String filePath = Environment.getRootDirectory() |
| 3713 | + SOUND_EFFECTS_PATH |
| 3714 | + SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effect][0]); |
| 3715 | int sampleId = mSoundPool.load(filePath, 0); |
| 3716 | if (sampleId <= 0) { |
| 3717 | Log.w(TAG, "Soundpool could not load file: "+filePath); |
| 3718 | } else { |
| 3719 | SOUND_EFFECT_FILES_MAP[effect][1] = sampleId; |
| 3720 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = sampleId; |
| 3721 | numSamples++; |
| 3722 | } |
| 3723 | } else { |
| 3724 | SOUND_EFFECT_FILES_MAP[effect][1] = |
| 3725 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]]; |
| 3726 | } |
| 3727 | } |
| 3728 | // wait for all samples to be loaded |
| 3729 | if (numSamples > 0) { |
| 3730 | mSoundPoolCallBack.setSamples(poolId); |
| 3731 | |
| 3732 | attempts = 3; |
| 3733 | status = 1; |
| 3734 | while ((status == 1) && (attempts-- > 0)) { |
| 3735 | try { |
Glenn Kasten | 167d1a2 | 2013-07-23 16:24:41 -0700 | [diff] [blame] | 3736 | mSoundEffectsLock.wait(SOUND_EFFECTS_LOAD_TIMEOUT_MS); |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3737 | status = mSoundPoolCallBack.status(); |
| 3738 | } catch (InterruptedException e) { |
| 3739 | Log.w(TAG, "Interrupted while waiting sound pool callback."); |
| 3740 | } |
| 3741 | } |
| 3742 | } else { |
| 3743 | status = -1; |
| 3744 | } |
| 3745 | |
| 3746 | if (mSoundPoolLooper != null) { |
| 3747 | mSoundPoolLooper.quit(); |
| 3748 | mSoundPoolLooper = null; |
| 3749 | } |
| 3750 | mSoundPoolListenerThread = null; |
| 3751 | if (status != 0) { |
| 3752 | Log.w(TAG, |
| 3753 | "onLoadSoundEffects(), Error "+status+ " while loading samples"); |
| 3754 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3755 | if (SOUND_EFFECT_FILES_MAP[effect][1] > 0) { |
| 3756 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | mSoundPool.release(); |
| 3761 | mSoundPool = null; |
| 3762 | } |
| 3763 | } |
| 3764 | return (status == 0); |
| 3765 | } |
| 3766 | |
| 3767 | /** |
| 3768 | * Unloads samples from the sound pool. |
| 3769 | * This method can be called to free some memory when |
| 3770 | * sound effects are disabled. |
| 3771 | */ |
| 3772 | private void onUnloadSoundEffects() { |
| 3773 | synchronized (mSoundEffectsLock) { |
| 3774 | if (mSoundPool == null) { |
| 3775 | return; |
| 3776 | } |
| 3777 | |
| 3778 | int[] poolId = new int[SOUND_EFFECT_FILES.size()]; |
| 3779 | for (int fileIdx = 0; fileIdx < SOUND_EFFECT_FILES.size(); fileIdx++) { |
| 3780 | poolId[fileIdx] = 0; |
| 3781 | } |
| 3782 | |
| 3783 | for (int effect = 0; effect < AudioManager.NUM_SOUND_EFFECTS; effect++) { |
| 3784 | if (SOUND_EFFECT_FILES_MAP[effect][1] <= 0) { |
| 3785 | continue; |
| 3786 | } |
| 3787 | if (poolId[SOUND_EFFECT_FILES_MAP[effect][0]] == 0) { |
| 3788 | mSoundPool.unload(SOUND_EFFECT_FILES_MAP[effect][1]); |
| 3789 | SOUND_EFFECT_FILES_MAP[effect][1] = -1; |
| 3790 | poolId[SOUND_EFFECT_FILES_MAP[effect][0]] = -1; |
| 3791 | } |
| 3792 | } |
| 3793 | mSoundPool.release(); |
| 3794 | mSoundPool = null; |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | private void onPlaySoundEffect(int effectType, int volume) { |
| 3799 | synchronized (mSoundEffectsLock) { |
| 3800 | |
| 3801 | onLoadSoundEffects(); |
| 3802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3803 | if (mSoundPool == null) { |
| 3804 | return; |
| 3805 | } |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3806 | float volFloat; |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 3807 | // use default if volume is not specified by caller |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3808 | if (volume < 0) { |
Jean-Michel Trivi | f2b0c11 | 2012-07-09 11:59:11 -0700 | [diff] [blame] | 3809 | volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3810 | } else { |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 3811 | volFloat = volume / 1000.0f; |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3813 | |
| 3814 | if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3815 | mSoundPool.play(SOUND_EFFECT_FILES_MAP[effectType][1], |
| 3816 | volFloat, volFloat, 0, 0, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3817 | } else { |
| 3818 | MediaPlayer mediaPlayer = new MediaPlayer(); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3819 | try { |
Eric Laurent | e78fced | 2013-03-15 16:03:47 -0700 | [diff] [blame] | 3820 | String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + |
| 3821 | SOUND_EFFECT_FILES.get(SOUND_EFFECT_FILES_MAP[effectType][0]); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3822 | mediaPlayer.setDataSource(filePath); |
| 3823 | mediaPlayer.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 3824 | mediaPlayer.prepare(); |
Glenn Kasten | 068225d | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 3825 | mediaPlayer.setVolume(volFloat); |
Glenn Kasten | 62b9aec | 2011-11-07 11:10:16 -0800 | [diff] [blame] | 3826 | mediaPlayer.setOnCompletionListener(new OnCompletionListener() { |
| 3827 | public void onCompletion(MediaPlayer mp) { |
| 3828 | cleanupPlayer(mp); |
| 3829 | } |
| 3830 | }); |
| 3831 | mediaPlayer.setOnErrorListener(new OnErrorListener() { |
| 3832 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 3833 | cleanupPlayer(mp); |
| 3834 | return true; |
| 3835 | } |
| 3836 | }); |
| 3837 | mediaPlayer.start(); |
| 3838 | } catch (IOException ex) { |
| 3839 | Log.w(TAG, "MediaPlayer IOException: "+ex); |
| 3840 | } catch (IllegalArgumentException ex) { |
| 3841 | Log.w(TAG, "MediaPlayer IllegalArgumentException: "+ex); |
| 3842 | } catch (IllegalStateException ex) { |
| 3843 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | private void cleanupPlayer(MediaPlayer mp) { |
| 3850 | if (mp != null) { |
| 3851 | try { |
| 3852 | mp.stop(); |
| 3853 | mp.release(); |
| 3854 | } catch (IllegalStateException ex) { |
| 3855 | Log.w(TAG, "MediaPlayer IllegalStateException: "+ex); |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 3860 | private void setForceUse(int usage, int config) { |
| 3861 | AudioSystem.setForceUse(usage, config); |
| 3862 | } |
| 3863 | |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 3864 | private void onPersistSafeVolumeState(int state) { |
| 3865 | Settings.Global.putInt(mContentResolver, |
| 3866 | Settings.Global.AUDIO_SAFE_VOLUME_STATE, |
| 3867 | state); |
| 3868 | } |
| 3869 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3870 | @Override |
| 3871 | public void handleMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3873 | switch (msg.what) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3874 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3875 | case MSG_SET_DEVICE_VOLUME: |
| 3876 | setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1); |
| 3877 | break; |
| 3878 | |
| 3879 | case MSG_SET_ALL_VOLUMES: |
| 3880 | setAllVolumes((VolumeStreamState) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3881 | break; |
| 3882 | |
| 3883 | case MSG_PERSIST_VOLUME: |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 3884 | persistVolume((VolumeStreamState) msg.obj, msg.arg1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3885 | break; |
| 3886 | |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 3887 | case MSG_PERSIST_MASTER_VOLUME: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3888 | if (mUseFixedVolume) { |
| 3889 | return; |
| 3890 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3891 | Settings.System.putFloatForUser(mContentResolver, |
| 3892 | Settings.System.VOLUME_MASTER, |
RoboErik | 8a2cfc3 | 2014-05-16 11:19:38 -0700 | [diff] [blame] | 3893 | msg.arg1 / (float)1000.0, |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3894 | UserHandle.USER_CURRENT); |
Mike Lockwood | 5c55a05 | 2011-12-15 17:21:44 -0500 | [diff] [blame] | 3895 | break; |
| 3896 | |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 3897 | case MSG_PERSIST_MASTER_VOLUME_MUTE: |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 3898 | if (mUseFixedVolume) { |
| 3899 | return; |
| 3900 | } |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 3901 | Settings.System.putIntForUser(mContentResolver, |
| 3902 | Settings.System.VOLUME_MASTER_MUTE, |
| 3903 | msg.arg1, |
| 3904 | UserHandle.USER_CURRENT); |
Justin Koh | 57978ed | 2012-04-03 17:37:58 -0700 | [diff] [blame] | 3905 | break; |
| 3906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3907 | case MSG_PERSIST_RINGER_MODE: |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 3908 | // note that the value persisted is the current ringer mode, not the |
| 3909 | // value of ringer mode as of the time the request was made to persist |
| 3910 | persistRingerMode(getRingerMode()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3911 | break; |
| 3912 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3913 | case MSG_MEDIA_SERVER_DIED: |
Eric Laurent | eb4b8a2 | 2014-07-21 13:10:07 -0700 | [diff] [blame] | 3914 | if (!mSystemReady || |
| 3915 | (AudioSystem.checkAudioFlinger() != AudioSystem.AUDIO_STATUS_OK)) { |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3916 | Log.e(TAG, "Media server died."); |
Eric Laurent | afbb047 | 2011-12-15 09:04:23 -0800 | [diff] [blame] | 3917 | sendMsg(mAudioHandler, MSG_MEDIA_SERVER_DIED, SENDMSG_NOOP, 0, 0, |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3918 | null, 500); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3919 | break; |
Eric Laurent | 89e74ba | 2009-09-30 18:26:36 -0700 | [diff] [blame] | 3920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3921 | Log.e(TAG, "Media server started."); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3922 | |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3923 | // indicate to audio HAL that we start the reconfiguration phase after a media |
| 3924 | // server crash |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 3925 | // Note that we only execute this when the media server |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3926 | // process restarts after a crash, not the first time it is started. |
| 3927 | AudioSystem.setParameters("restarting=true"); |
| 3928 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 3929 | readAndSetLowRamDevice(); |
| 3930 | |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3931 | // Restore device connection states |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3932 | synchronized (mConnectedDevices) { |
| 3933 | Set set = mConnectedDevices.entrySet(); |
| 3934 | Iterator i = set.iterator(); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3935 | while (i.hasNext()) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 3936 | Map.Entry device = (Map.Entry)i.next(); |
| 3937 | AudioSystem.setDeviceConnectionState( |
| 3938 | ((Integer)device.getKey()).intValue(), |
| 3939 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 3940 | (String)device.getValue()); |
| 3941 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3942 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3943 | // Restore call state |
| 3944 | AudioSystem.setPhoneState(mMode); |
| 3945 | |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 3946 | // Restore forced usage for communcations and record |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3947 | AudioSystem.setForceUse(AudioSystem.FOR_COMMUNICATION, mForcedUseForComm); |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 3948 | AudioSystem.setForceUse(AudioSystem.FOR_RECORD, mForcedUseForComm); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 3949 | AudioSystem.setForceUse(AudioSystem.FOR_SYSTEM, mCameraSoundForced ? |
| 3950 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3951 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3952 | // Restore stream volumes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | int numStreamTypes = AudioSystem.getNumStreamTypes(); |
| 3954 | for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3955 | VolumeStreamState streamState = mStreamStates[streamType]; |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3956 | AudioSystem.initStreamVolume(streamType, 0, (streamState.mIndexMax + 5) / 10); |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 3957 | |
| 3958 | streamState.applyAllVolumes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3959 | } |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 3960 | |
| 3961 | // Restore ringer mode |
| 3962 | setRingerModeInt(getRingerMode(), false); |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3963 | |
Mike Lockwood | 9063154 | 2012-01-06 11:20:37 -0500 | [diff] [blame] | 3964 | // Restore master volume |
| 3965 | restoreMasterVolume(); |
| 3966 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 3967 | // Reset device orientation (if monitored for this device) |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 3968 | if (mMonitorOrientation) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 3969 | setOrientationForAudioSystem(); |
| 3970 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 3971 | if (mMonitorRotation) { |
| 3972 | setRotationForAudioSystem(); |
| 3973 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 3974 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 3975 | synchronized (mBluetoothA2dpEnabledLock) { |
| 3976 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 3977 | mBluetoothA2dpEnabled ? |
| 3978 | AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
| 3979 | } |
Eric Laurent | bff5ca5 | 2012-11-02 16:48:26 -0700 | [diff] [blame] | 3980 | |
| 3981 | synchronized (mSettingsLock) { |
| 3982 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, |
| 3983 | mDockAudioMediaEnabled ? |
| 3984 | AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE); |
| 3985 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 3986 | if (mHdmiManager != null) { |
| 3987 | synchronized (mHdmiManager) { |
| 3988 | if (mHdmiTvClient != null) { |
| 3989 | setHdmiSystemAudioSupported(mHdmiSystemAudioSupported); |
| 3990 | } |
| 3991 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 3992 | } |
Eric Laurent | 3c652ca | 2010-06-21 20:46:26 -0700 | [diff] [blame] | 3993 | // indicate the end of reconfiguration phase to audio HAL |
| 3994 | AudioSystem.setParameters("restarting=false"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3995 | break; |
| 3996 | |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 3997 | case MSG_UNLOAD_SOUND_EFFECTS: |
| 3998 | onUnloadSoundEffects(); |
| 3999 | break; |
| 4000 | |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4001 | case MSG_LOAD_SOUND_EFFECTS: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4002 | //FIXME: onLoadSoundEffects() should be executed in a separate thread as it |
| 4003 | // can take several dozens of milliseconds to complete |
| 4004 | boolean loaded = onLoadSoundEffects(); |
| 4005 | if (msg.obj != null) { |
| 4006 | LoadSoundEffectReply reply = (LoadSoundEffectReply)msg.obj; |
| 4007 | synchronized (reply) { |
| 4008 | reply.mStatus = loaded ? 0 : -1; |
| 4009 | reply.notify(); |
| 4010 | } |
| 4011 | } |
Eric Laurent | 117b7bb | 2011-01-16 17:07:27 -0800 | [diff] [blame] | 4012 | break; |
| 4013 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4014 | case MSG_PLAY_SOUND_EFFECT: |
Eric Laurent | 5d3eb44a | 2013-03-21 15:35:10 -0700 | [diff] [blame] | 4015 | onPlaySoundEffect(msg.arg1, msg.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4016 | break; |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4017 | |
| 4018 | case MSG_BTA2DP_DOCK_TIMEOUT: |
| 4019 | // msg.obj == address of BTA2DP device |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4020 | synchronized (mConnectedDevices) { |
| 4021 | makeA2dpDeviceUnavailableNow( (String) msg.obj ); |
| 4022 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4023 | break; |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4024 | |
| 4025 | case MSG_SET_FORCE_USE: |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4026 | case MSG_SET_FORCE_BT_A2DP_USE: |
Eric Laurent | fa64015 | 2011-03-12 15:59:51 -0800 | [diff] [blame] | 4027 | setForceUse(msg.arg1, msg.arg2); |
| 4028 | break; |
Jean-Michel Trivi | d589fea | 2011-04-15 11:28:10 -0700 | [diff] [blame] | 4029 | |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4030 | case MSG_BT_HEADSET_CNCT_FAILED: |
| 4031 | resetBluetoothSco(); |
| 4032 | break; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4033 | |
| 4034 | case MSG_SET_WIRED_DEVICE_CONNECTION_STATE: |
| 4035 | onSetWiredDeviceConnectionState(msg.arg1, msg.arg2, (String)msg.obj); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4036 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4037 | break; |
| 4038 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4039 | case MSG_SET_A2DP_SRC_CONNECTION_STATE: |
| 4040 | onSetA2dpSourceConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
| 4041 | mAudioEventWakeLock.release(); |
| 4042 | break; |
| 4043 | |
| 4044 | case MSG_SET_A2DP_SINK_CONNECTION_STATE: |
| 4045 | onSetA2dpSinkConnectionState((BluetoothDevice)msg.obj, msg.arg1); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4046 | mAudioEventWakeLock.release(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4047 | break; |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4048 | |
| 4049 | case MSG_REPORT_NEW_ROUTES: { |
| 4050 | int N = mRoutesObservers.beginBroadcast(); |
| 4051 | if (N > 0) { |
| 4052 | AudioRoutesInfo routes; |
| 4053 | synchronized (mCurAudioRoutes) { |
| 4054 | routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4055 | } |
| 4056 | while (N > 0) { |
| 4057 | N--; |
| 4058 | IAudioRoutesObserver obs = mRoutesObservers.getBroadcastItem(N); |
| 4059 | try { |
| 4060 | obs.dispatchAudioRoutesChanged(routes); |
| 4061 | } catch (RemoteException e) { |
| 4062 | } |
| 4063 | } |
| 4064 | } |
| 4065 | mRoutesObservers.finishBroadcast(); |
| 4066 | break; |
| 4067 | } |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4068 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4069 | case MSG_CHECK_MUSIC_ACTIVE: |
| 4070 | onCheckMusicActive(); |
| 4071 | break; |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4072 | |
| 4073 | case MSG_BROADCAST_AUDIO_BECOMING_NOISY: |
| 4074 | onSendBecomingNoisyIntent(); |
| 4075 | break; |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4076 | |
| 4077 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED: |
| 4078 | case MSG_CONFIGURE_SAFE_MEDIA_VOLUME: |
| 4079 | onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED)); |
| 4080 | break; |
Eric Laurent | 05274f3 | 2012-11-29 12:48:18 -0800 | [diff] [blame] | 4081 | case MSG_PERSIST_SAFE_VOLUME_STATE: |
| 4082 | onPersistSafeVolumeState(msg.arg1); |
| 4083 | break; |
Jean-Michel Trivi | a578c48 | 2012-12-28 11:19:49 -0800 | [diff] [blame] | 4084 | |
Eric Laurent | 2a57ca9 | 2013-03-07 17:29:27 -0800 | [diff] [blame] | 4085 | case MSG_BROADCAST_BT_CONNECTION_STATE: |
| 4086 | onBroadcastScoConnectionState(msg.arg1); |
| 4087 | break; |
Eric Laurent | 4a5eeb9 | 2014-05-06 10:49:04 -0700 | [diff] [blame] | 4088 | |
| 4089 | case MSG_SYSTEM_READY: |
| 4090 | onSystemReady(); |
| 4091 | break; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 4092 | |
| 4093 | case MSG_PERSIST_MUSIC_ACTIVE_MS: |
| 4094 | final int musicActiveMs = msg.arg1; |
| 4095 | Settings.Secure.putIntForUser(mContentResolver, |
| 4096 | Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs, |
| 4097 | UserHandle.USER_CURRENT); |
| 4098 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | } |
| 4102 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4103 | private class SettingsObserver extends ContentObserver { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4104 | |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4105 | SettingsObserver() { |
| 4106 | super(new Handler()); |
| 4107 | mContentResolver.registerContentObserver(Settings.System.getUriFor( |
| 4108 | Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4109 | mContentResolver.registerContentObserver(Settings.Global.getUriFor( |
| 4110 | Settings.Global.DOCK_AUDIO_MEDIA_ENABLED), false, this); |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | @Override |
| 4114 | public void onChange(boolean selfChange) { |
| 4115 | super.onChange(selfChange); |
Glenn Kasten | ba195eb | 2011-12-13 09:30:40 -0800 | [diff] [blame] | 4116 | // FIXME This synchronized is not necessary if mSettingsLock only protects mRingerMode. |
| 4117 | // However there appear to be some missing locks around mRingerModeMutedStreams |
| 4118 | // and mRingerModeAffectedStreams, so will leave this synchronized for now. |
| 4119 | // mRingerModeMutedStreams and mMuteAffectedStreams are safe (only accessed once). |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4120 | synchronized (mSettingsLock) { |
Eric Laurent | 24e0d9b | 2013-10-03 18:15:07 -0700 | [diff] [blame] | 4121 | if (updateRingerModeAffectedStreams()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4122 | /* |
| 4123 | * Ensure all stream types that should be affected by ringer mode |
| 4124 | * are in the proper state. |
| 4125 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4126 | setRingerModeInt(getRingerMode(), false); |
| 4127 | } |
Eric Laurent | 7ee1e4f | 2012-10-26 18:11:21 -0700 | [diff] [blame] | 4128 | readDockAudioSettings(mContentResolver); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4129 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4130 | } |
Jason Parekh | b109615 | 2009-03-24 17:48:25 -0700 | [diff] [blame] | 4131 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4132 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4133 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4134 | private void makeA2dpDeviceAvailable(String address) { |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4135 | // enable A2DP before notifying A2DP connection to avoid unecessary processing in |
| 4136 | // audio policy manager |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4137 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
| 4138 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4139 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, streamState, 0); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4140 | setBluetoothA2dpOnInt(true); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4141 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4142 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 4143 | address); |
| 4144 | // Reset A2DP suspend state each time a new sink is connected |
| 4145 | AudioSystem.setParameters("A2dpSuspended=false"); |
| 4146 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP), |
| 4147 | address); |
| 4148 | } |
| 4149 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4150 | private void onSendBecomingNoisyIntent() { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4151 | sendBroadcastToAll(new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4154 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4155 | private void makeA2dpDeviceUnavailableNow(String address) { |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4156 | synchronized (mA2dpAvrcpLock) { |
| 4157 | mAvrcpAbsVolSupported = false; |
| 4158 | } |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4159 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, |
| 4160 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
| 4161 | address); |
| 4162 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
| 4163 | } |
| 4164 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4165 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4166 | private void makeA2dpDeviceUnavailableLater(String address) { |
Eric Laurent | 3b59126 | 2010-04-20 07:01:00 -0700 | [diff] [blame] | 4167 | // prevent any activity on the A2DP audio output to avoid unwanted |
| 4168 | // reconnection of the sink. |
| 4169 | AudioSystem.setParameters("A2dpSuspended=true"); |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4170 | // the device will be made unavailable later, so consider it disconnected right away |
| 4171 | mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); |
| 4172 | // send the delayed message to make the device unavailable later |
| 4173 | Message msg = mAudioHandler.obtainMessage(MSG_BTA2DP_DOCK_TIMEOUT, address); |
| 4174 | mAudioHandler.sendMessageDelayed(msg, BTA2DP_DOCK_TIMEOUT_MILLIS); |
| 4175 | |
| 4176 | } |
| 4177 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4178 | // must be called synchronized on mConnectedDevices |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4179 | private void makeA2dpSrcAvailable(String address) { |
| 4180 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4181 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 4182 | address); |
| 4183 | mConnectedDevices.put( new Integer(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP), |
| 4184 | address); |
| 4185 | } |
| 4186 | |
| 4187 | // must be called synchronized on mConnectedDevices |
| 4188 | private void makeA2dpSrcUnavailable(String address) { |
| 4189 | AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, |
| 4190 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
| 4191 | address); |
| 4192 | mConnectedDevices.remove(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP); |
| 4193 | } |
| 4194 | |
| 4195 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4196 | private void cancelA2dpDeviceTimeout() { |
Jean-Michel Trivi | 4c637b9 | 2010-04-12 18:44:10 -0700 | [diff] [blame] | 4197 | mAudioHandler.removeMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4198 | } |
| 4199 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4200 | // must be called synchronized on mConnectedDevices |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4201 | private boolean hasScheduledA2dpDockTimeout() { |
| 4202 | return mAudioHandler.hasMessages(MSG_BTA2DP_DOCK_TIMEOUT); |
| 4203 | } |
| 4204 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4205 | private void onSetA2dpSinkConnectionState(BluetoothDevice btDevice, int state) |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4206 | { |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4207 | if (DEBUG_VOL) { |
| 4208 | Log.d(TAG, "onSetA2dpSinkConnectionState btDevice="+btDevice+"state="+state); |
| 4209 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4210 | if (btDevice == null) { |
| 4211 | return; |
| 4212 | } |
| 4213 | String address = btDevice.getAddress(); |
| 4214 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4215 | address = ""; |
| 4216 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4217 | |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4218 | synchronized (mConnectedDevices) { |
| 4219 | boolean isConnected = |
| 4220 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP) && |
| 4221 | mConnectedDevices.get(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP).equals(address)); |
| 4222 | |
| 4223 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4224 | if (btDevice.isBluetoothDock()) { |
| 4225 | if (state == BluetoothProfile.STATE_DISCONNECTED) { |
| 4226 | // introduction of a delay for transient disconnections of docks when |
| 4227 | // power is rapidly turned off/on, this message will be canceled if |
| 4228 | // we reconnect the dock under a preset delay |
| 4229 | makeA2dpDeviceUnavailableLater(address); |
| 4230 | // the next time isConnected is evaluated, it will be false for the dock |
| 4231 | } |
| 4232 | } else { |
| 4233 | makeA2dpDeviceUnavailableNow(address); |
| 4234 | } |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4235 | synchronized (mCurAudioRoutes) { |
| 4236 | if (mCurAudioRoutes.mBluetoothName != null) { |
| 4237 | mCurAudioRoutes.mBluetoothName = null; |
| 4238 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4239 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4240 | } |
| 4241 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4242 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4243 | if (btDevice.isBluetoothDock()) { |
| 4244 | // this could be a reconnection after a transient disconnection |
| 4245 | cancelA2dpDeviceTimeout(); |
| 4246 | mDockAddress = address; |
| 4247 | } else { |
| 4248 | // this could be a connection of another A2DP device before the timeout of |
| 4249 | // a dock: cancel the dock timeout, and make the dock unavailable now |
| 4250 | if(hasScheduledA2dpDockTimeout()) { |
| 4251 | cancelA2dpDeviceTimeout(); |
| 4252 | makeA2dpDeviceUnavailableNow(mDockAddress); |
| 4253 | } |
| 4254 | } |
| 4255 | makeA2dpDeviceAvailable(address); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4256 | synchronized (mCurAudioRoutes) { |
| 4257 | String name = btDevice.getAliasName(); |
| 4258 | if (!TextUtils.equals(mCurAudioRoutes.mBluetoothName, name)) { |
| 4259 | mCurAudioRoutes.mBluetoothName = name; |
| 4260 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4261 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4262 | } |
| 4263 | } |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4264 | } |
| 4265 | } |
| 4266 | } |
| 4267 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4268 | private void onSetA2dpSourceConnectionState(BluetoothDevice btDevice, int state) |
| 4269 | { |
| 4270 | if (DEBUG_VOL) { |
| 4271 | Log.d(TAG, "onSetA2dpSourceConnectionState btDevice="+btDevice+" state="+state); |
| 4272 | } |
| 4273 | if (btDevice == null) { |
| 4274 | return; |
| 4275 | } |
| 4276 | String address = btDevice.getAddress(); |
| 4277 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4278 | address = ""; |
| 4279 | } |
| 4280 | |
| 4281 | synchronized (mConnectedDevices) { |
| 4282 | boolean isConnected = |
| 4283 | (mConnectedDevices.containsKey(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP) && |
| 4284 | mConnectedDevices.get(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP).equals(address)); |
| 4285 | |
| 4286 | if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { |
| 4287 | makeA2dpSrcUnavailable(address); |
| 4288 | } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { |
| 4289 | makeA2dpSrcAvailable(address); |
| 4290 | } |
| 4291 | } |
| 4292 | } |
| 4293 | |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4294 | public void avrcpSupportsAbsoluteVolume(String address, boolean support) { |
| 4295 | // address is not used for now, but may be used when multiple a2dp devices are supported |
| 4296 | synchronized (mA2dpAvrcpLock) { |
| 4297 | mAvrcpAbsVolSupported = support; |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 4298 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
Eric Laurent | cd772d0 | 2013-10-30 18:31:07 -0700 | [diff] [blame] | 4299 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4300 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
| 4301 | sendMsg(mAudioHandler, MSG_SET_DEVICE_VOLUME, SENDMSG_QUEUE, |
| 4302 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, 0, |
| 4303 | mStreamStates[AudioSystem.STREAM_RING], 0); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 4304 | } |
| 4305 | } |
| 4306 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4307 | private boolean handleDeviceConnection(boolean connected, int device, String params) { |
| 4308 | synchronized (mConnectedDevices) { |
| 4309 | boolean isConnected = (mConnectedDevices.containsKey(device) && |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4310 | (params.isEmpty() || mConnectedDevices.get(device).equals(params))); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4311 | |
| 4312 | if (isConnected && !connected) { |
| 4313 | AudioSystem.setDeviceConnectionState(device, |
| 4314 | AudioSystem.DEVICE_STATE_UNAVAILABLE, |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4315 | mConnectedDevices.get(device)); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4316 | mConnectedDevices.remove(device); |
| 4317 | return true; |
| 4318 | } else if (!isConnected && connected) { |
| 4319 | AudioSystem.setDeviceConnectionState(device, |
| 4320 | AudioSystem.DEVICE_STATE_AVAILABLE, |
| 4321 | params); |
| 4322 | mConnectedDevices.put(new Integer(device), params); |
| 4323 | return true; |
| 4324 | } |
| 4325 | } |
| 4326 | return false; |
| 4327 | } |
| 4328 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4329 | // Devices which removal triggers intent ACTION_AUDIO_BECOMING_NOISY. The intent is only |
| 4330 | // sent if none of these devices is connected. |
| 4331 | int mBecomingNoisyIntentDevices = |
| 4332 | AudioSystem.DEVICE_OUT_WIRED_HEADSET | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4333 | AudioSystem.DEVICE_OUT_ALL_A2DP | AudioSystem.DEVICE_OUT_HDMI | |
Eric Laurent | 794da7a | 2012-08-30 11:30:16 -0700 | [diff] [blame] | 4334 | AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET | AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET | |
| 4335 | AudioSystem.DEVICE_OUT_ALL_USB; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4336 | |
| 4337 | // must be called before removing the device from mConnectedDevices |
| 4338 | private int checkSendBecomingNoisyIntent(int device, int state) { |
| 4339 | int delay = 0; |
| 4340 | if ((state == 0) && ((device & mBecomingNoisyIntentDevices) != 0)) { |
| 4341 | int devices = 0; |
| 4342 | for (int dev : mConnectedDevices.keySet()) { |
| 4343 | if ((dev & mBecomingNoisyIntentDevices) != 0) { |
| 4344 | devices |= dev; |
| 4345 | } |
| 4346 | } |
| 4347 | if (devices == device) { |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4348 | sendMsg(mAudioHandler, |
| 4349 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4350 | SENDMSG_REPLACE, |
| 4351 | 0, |
| 4352 | 0, |
| 4353 | null, |
| 4354 | 0); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4355 | delay = 1000; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4356 | } |
| 4357 | } |
| 4358 | |
Mike Lockwood | 0a40ec2 | 2014-05-21 10:08:50 -0700 | [diff] [blame] | 4359 | if (mAudioHandler.hasMessages(MSG_SET_A2DP_SRC_CONNECTION_STATE) || |
| 4360 | mAudioHandler.hasMessages(MSG_SET_A2DP_SINK_CONNECTION_STATE) || |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4361 | mAudioHandler.hasMessages(MSG_SET_WIRED_DEVICE_CONNECTION_STATE)) { |
| 4362 | delay = 1000; |
| 4363 | } |
| 4364 | return delay; |
| 4365 | } |
| 4366 | |
| 4367 | private void sendDeviceConnectionIntent(int device, int state, String name) |
| 4368 | { |
| 4369 | Intent intent = new Intent(); |
| 4370 | |
| 4371 | intent.putExtra("state", state); |
| 4372 | intent.putExtra("name", name); |
| 4373 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4374 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4375 | int connType = 0; |
| 4376 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4377 | if (device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4378 | connType = AudioRoutesInfo.MAIN_HEADSET; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4379 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4380 | intent.putExtra("microphone", 1); |
| 4381 | } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4382 | connType = AudioRoutesInfo.MAIN_HEADPHONES; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4383 | intent.setAction(Intent.ACTION_HEADSET_PLUG); |
| 4384 | intent.putExtra("microphone", 0); |
| 4385 | } else if (device == AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4386 | connType = AudioRoutesInfo.MAIN_DOCK_SPEAKERS; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4387 | intent.setAction(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG); |
| 4388 | } else if (device == AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4389 | connType = AudioRoutesInfo.MAIN_DOCK_SPEAKERS; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4390 | intent.setAction(Intent.ACTION_DIGITAL_AUDIO_DOCK_PLUG); |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 4391 | } else if (device == AudioSystem.DEVICE_OUT_HDMI) { |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4392 | connType = AudioRoutesInfo.MAIN_HDMI; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4393 | intent.setAction(Intent.ACTION_HDMI_AUDIO_PLUG); |
| 4394 | } |
| 4395 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4396 | synchronized (mCurAudioRoutes) { |
| 4397 | if (connType != 0) { |
| 4398 | int newConn = mCurAudioRoutes.mMainType; |
| 4399 | if (state != 0) { |
| 4400 | newConn |= connType; |
| 4401 | } else { |
| 4402 | newConn &= ~connType; |
| 4403 | } |
| 4404 | if (newConn != mCurAudioRoutes.mMainType) { |
| 4405 | mCurAudioRoutes.mMainType = newConn; |
| 4406 | sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES, |
| 4407 | SENDMSG_NOOP, 0, 0, null, 0); |
| 4408 | } |
| 4409 | } |
| 4410 | } |
| 4411 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4412 | final long ident = Binder.clearCallingIdentity(); |
| 4413 | try { |
| 4414 | ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.USER_ALL); |
| 4415 | } finally { |
| 4416 | Binder.restoreCallingIdentity(ident); |
| 4417 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | private void onSetWiredDeviceConnectionState(int device, int state, String name) |
| 4421 | { |
| 4422 | synchronized (mConnectedDevices) { |
| 4423 | if ((state == 0) && ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
| 4424 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE))) { |
| 4425 | setBluetoothA2dpOnInt(true); |
| 4426 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4427 | boolean isUsb = ((device & ~AudioSystem.DEVICE_OUT_ALL_USB) == 0) || |
| 4428 | (((device & AudioSystem.DEVICE_BIT_IN) != 0) && |
| 4429 | ((device & ~AudioSystem.DEVICE_IN_ALL_USB) == 0)); |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4430 | handleDeviceConnection((state == 1), device, (isUsb ? name : "")); |
Eric Laurent | f1a457d | 2012-09-20 16:27:23 -0700 | [diff] [blame] | 4431 | if (state != 0) { |
| 4432 | if ((device == AudioSystem.DEVICE_OUT_WIRED_HEADSET) || |
| 4433 | (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE)) { |
| 4434 | setBluetoothA2dpOnInt(false); |
| 4435 | } |
| 4436 | if ((device & mSafeMediaVolumeDevices) != 0) { |
| 4437 | sendMsg(mAudioHandler, |
| 4438 | MSG_CHECK_MUSIC_ACTIVE, |
| 4439 | SENDMSG_REPLACE, |
| 4440 | 0, |
| 4441 | 0, |
| 4442 | null, |
| 4443 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4444 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4445 | // Television devices without CEC service apply software volume on HDMI output |
| 4446 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4447 | mFixedVolumeDevices |= AudioSystem.DEVICE_OUT_HDMI; |
| 4448 | checkAllFixedVolumeDevices(); |
| 4449 | if (mHdmiManager != null) { |
| 4450 | synchronized (mHdmiManager) { |
| 4451 | if (mHdmiPlaybackClient != null) { |
| 4452 | mHdmiCecSink = false; |
| 4453 | mHdmiPlaybackClient.queryDisplayStatus(mHdmiDisplayStatusCallback); |
| 4454 | } |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | } else { |
| 4459 | if (isPlatformTelevision() && ((device & AudioSystem.DEVICE_OUT_HDMI) != 0)) { |
| 4460 | if (mHdmiManager != null) { |
| 4461 | synchronized (mHdmiManager) { |
| 4462 | mHdmiCecSink = false; |
| 4463 | } |
| 4464 | } |
| 4465 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4466 | } |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4467 | if (!isUsb && (device != AudioSystem.DEVICE_IN_WIRED_HEADSET)) { |
Mike Lockwood | db45484 | 2012-09-18 11:16:57 -0700 | [diff] [blame] | 4468 | sendDeviceConnectionIntent(device, state, name); |
| 4469 | } |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | |
Jean-Michel Trivi | a847ba4 | 2010-04-23 11:49:29 -0700 | [diff] [blame] | 4473 | /* cache of the address of the last dock the device was connected to */ |
| 4474 | private String mDockAddress; |
| 4475 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4476 | /** |
| 4477 | * Receiver for misc intent broadcasts the Phone app cares about. |
| 4478 | */ |
| 4479 | private class AudioServiceBroadcastReceiver extends BroadcastReceiver { |
| 4480 | @Override |
| 4481 | public void onReceive(Context context, Intent intent) { |
| 4482 | String action = intent.getAction(); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4483 | int outDevice; |
| 4484 | int inDevice; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4485 | int state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4486 | |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4487 | if (action.equals(Intent.ACTION_DOCK_EVENT)) { |
| 4488 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4489 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4490 | int config; |
| 4491 | switch (dockState) { |
| 4492 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 4493 | config = AudioSystem.FORCE_BT_DESK_DOCK; |
| 4494 | break; |
| 4495 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 4496 | config = AudioSystem.FORCE_BT_CAR_DOCK; |
| 4497 | break; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4498 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4499 | config = AudioSystem.FORCE_ANALOG_DOCK; |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 4500 | break; |
| 4501 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 4502 | config = AudioSystem.FORCE_DIGITAL_DOCK; |
| 4503 | break; |
Jean-Michel Trivi | 758559e | 2010-03-09 09:26:08 -0800 | [diff] [blame] | 4504 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 4505 | default: |
| 4506 | config = AudioSystem.FORCE_NONE; |
| 4507 | } |
Eric Laurent | 08ed1b9 | 2012-11-05 14:54:12 -0800 | [diff] [blame] | 4508 | // Low end docks have a menu to enable or disable audio |
| 4509 | // (see mDockAudioMediaEnabled) |
| 4510 | if (!((dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 4511 | ((dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) && |
| 4512 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK)))) { |
| 4513 | AudioSystem.setForceUse(AudioSystem.FOR_DOCK, config); |
| 4514 | } |
| 4515 | mDockState = dockState; |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4516 | } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4517 | state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, |
Jaikumar Ganesh | 82aa7f0 | 2010-09-27 17:05:10 -0700 | [diff] [blame] | 4518 | BluetoothProfile.STATE_DISCONNECTED); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4519 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
| 4520 | inDevice = AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Nick Pelly | a56d1c7 | 2009-08-19 14:49:29 -0700 | [diff] [blame] | 4521 | String address = null; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4522 | |
| 4523 | BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 4524 | if (btDevice == null) { |
| 4525 | return; |
| 4526 | } |
| 4527 | |
| 4528 | address = btDevice.getAddress(); |
| 4529 | BluetoothClass btClass = btDevice.getBluetoothClass(); |
| 4530 | if (btClass != null) { |
| 4531 | switch (btClass.getDeviceClass()) { |
| 4532 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 4533 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4534 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4535 | break; |
| 4536 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4537 | outDevice = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4538 | break; |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
Eric Laurent | dca56b9 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 4542 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
| 4543 | address = ""; |
| 4544 | } |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 4545 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4546 | boolean connected = (state == BluetoothProfile.STATE_CONNECTED); |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4547 | boolean success = handleDeviceConnection(connected, outDevice, address) && |
| 4548 | handleDeviceConnection(connected, inDevice, address); |
| 4549 | if (success) { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4550 | synchronized (mScoClients) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4551 | if (connected) { |
| 4552 | mBluetoothHeadsetDevice = btDevice; |
| 4553 | } else { |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4554 | mBluetoothHeadsetDevice = null; |
| 4555 | resetBluetoothSco(); |
Eric Laurent | 6bc7f2c | 2011-11-29 18:49:40 -0800 | [diff] [blame] | 4556 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4557 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4558 | } |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4559 | } else if (action.equals(Intent.ACTION_USB_AUDIO_ACCESSORY_PLUG)) { |
| 4560 | state = intent.getIntExtra("state", 0); |
| 4561 | |
| 4562 | int alsaCard = intent.getIntExtra("card", -1); |
| 4563 | int alsaDevice = intent.getIntExtra("device", -1); |
| 4564 | |
| 4565 | String params = (alsaCard == -1 && alsaDevice == -1 ? "" |
| 4566 | : "card=" + alsaCard + ";device=" + alsaDevice); |
| 4567 | |
| 4568 | // Playback Device |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4569 | outDevice = AudioSystem.DEVICE_OUT_USB_ACCESSORY; |
| 4570 | setWiredDeviceConnectionState(outDevice, state, params); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4571 | } else if (action.equals(Intent.ACTION_USB_AUDIO_DEVICE_PLUG)) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4572 | state = intent.getIntExtra("state", 0); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4573 | |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4574 | int alsaCard = intent.getIntExtra("card", -1); |
| 4575 | int alsaDevice = intent.getIntExtra("device", -1); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4576 | boolean hasPlayback = intent.getBooleanExtra("hasPlayback", false); |
| 4577 | boolean hasCapture = intent.getBooleanExtra("hasCapture", false); |
| 4578 | boolean hasMIDI = intent.getBooleanExtra("hasMIDI", false); |
| 4579 | |
Mike Lockwood | 9841818 | 2012-05-10 17:13:20 -0700 | [diff] [blame] | 4580 | String params = (alsaCard == -1 && alsaDevice == -1 ? "" |
| 4581 | : "card=" + alsaCard + ";device=" + alsaDevice); |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4582 | |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4583 | // Playback Device |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4584 | if (hasPlayback) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4585 | outDevice = AudioSystem.DEVICE_OUT_USB_DEVICE; |
| 4586 | setWiredDeviceConnectionState(outDevice, state, params); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4587 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4588 | |
| 4589 | // Capture Device |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4590 | if (hasCapture) { |
Eric Laurent | ae4506e | 2014-05-29 16:04:32 -0700 | [diff] [blame] | 4591 | inDevice = AudioSystem.DEVICE_IN_USB_DEVICE; |
| 4592 | setWiredDeviceConnectionState(inDevice, state, params); |
Paul McLean | df36146 | 2014-04-10 16:02:55 -0700 | [diff] [blame] | 4593 | } |
| 4594 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4595 | boolean broadcast = false; |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4596 | int scoAudioState = AudioManager.SCO_AUDIO_STATE_ERROR; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4597 | synchronized (mScoClients) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4598 | int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4599 | // broadcast intent if the connection was initated by AudioService |
| 4600 | if (!mScoClients.isEmpty() && |
| 4601 | (mScoAudioState == SCO_STATE_ACTIVE_INTERNAL || |
| 4602 | mScoAudioState == SCO_STATE_ACTIVATE_REQ || |
| 4603 | mScoAudioState == SCO_STATE_DEACTIVATE_REQ)) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4604 | broadcast = true; |
| 4605 | } |
| 4606 | switch (btState) { |
| 4607 | case BluetoothHeadset.STATE_AUDIO_CONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4608 | scoAudioState = AudioManager.SCO_AUDIO_STATE_CONNECTED; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4609 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4610 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4611 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4612 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4613 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4614 | break; |
| 4615 | case BluetoothHeadset.STATE_AUDIO_DISCONNECTED: |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4616 | scoAudioState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4617 | mScoAudioState = SCO_STATE_INACTIVE; |
Eric Laurent | d7454be | 2011-09-14 08:45:58 -0700 | [diff] [blame] | 4618 | clearAllScoClients(0, false); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4619 | break; |
| 4620 | case BluetoothHeadset.STATE_AUDIO_CONNECTING: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4621 | if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL && |
| 4622 | mScoAudioState != SCO_STATE_DEACTIVATE_REQ && |
| 4623 | mScoAudioState != SCO_STATE_DEACTIVATE_EXT_REQ) { |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4624 | mScoAudioState = SCO_STATE_ACTIVE_EXTERNAL; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4625 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4626 | default: |
| 4627 | // do not broadcast CONNECTING or invalid state |
| 4628 | broadcast = false; |
| 4629 | break; |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 4630 | } |
| 4631 | } |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4632 | if (broadcast) { |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4633 | broadcastScoConnectionState(scoAudioState); |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 4634 | //FIXME: this is to maintain compatibility with deprecated intent |
| 4635 | // AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED. Remove when appropriate. |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4636 | Intent newIntent = new Intent(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 4637 | newIntent.putExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, scoAudioState); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 4638 | sendStickyBroadcastToAll(newIntent); |
Eric Laurent | 62ef767 | 2010-11-24 10:58:32 -0800 | [diff] [blame] | 4639 | } |
Eric Laurent | 950e8cb | 2011-10-13 08:57:54 -0700 | [diff] [blame] | 4640 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 4641 | AudioSystem.setParameters("screen_state=on"); |
| 4642 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 4643 | AudioSystem.setParameters("screen_state=off"); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4644 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4645 | handleConfigurationChanged(context); |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4646 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4647 | // attempt to stop music playback for background user |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4648 | sendMsg(mAudioHandler, |
| 4649 | MSG_BROADCAST_AUDIO_BECOMING_NOISY, |
| 4650 | SENDMSG_REPLACE, |
| 4651 | 0, |
| 4652 | 0, |
| 4653 | null, |
| 4654 | 0); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4655 | // the current audio focus owner is no longer valid |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4656 | mMediaFocusControl.discardAudioFocusOwner(); |
Jean-Michel Trivi | f4a8eb2 | 2012-10-29 12:42:55 -0700 | [diff] [blame] | 4657 | |
Eric Laurent | 5bfaeae | 2012-09-21 18:44:48 -0700 | [diff] [blame] | 4658 | // load volume settings for new user |
Eric Laurent | bc0fab1f | 2012-09-19 11:24:41 -0700 | [diff] [blame] | 4659 | readAudioSettings(true /*userSwitch*/); |
| 4660 | // preserve STREAM_MUSIC volume from one user to the next. |
| 4661 | sendMsg(mAudioHandler, |
| 4662 | MSG_SET_ALL_VOLUMES, |
| 4663 | SENDMSG_QUEUE, |
| 4664 | 0, |
| 4665 | 0, |
| 4666 | mStreamStates[AudioSystem.STREAM_MUSIC], 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4667 | } |
| 4668 | } |
Paul McLean | c837a45 | 2014-04-09 09:04:43 -0700 | [diff] [blame] | 4669 | } // end class AudioServiceBroadcastReceiver |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4670 | |
| 4671 | //========================================================================================== |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4672 | // RemoteControlDisplay / RemoteControlClient / Remote info |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4673 | //========================================================================================== |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4674 | public boolean registerRemoteController(IRemoteControlDisplay rcd, int w, int h, |
| 4675 | ComponentName listenerComp) { |
| 4676 | return mMediaFocusControl.registerRemoteController(rcd, w, h, listenerComp); |
| 4677 | } |
| 4678 | |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4679 | public boolean registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4680 | return mMediaFocusControl.registerRemoteControlDisplay(rcd, w, h); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4681 | } |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4682 | |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4683 | public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4684 | mMediaFocusControl.unregisterRemoteControlDisplay(rcd); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4688 | mMediaFocusControl.remoteControlDisplayUsesBitmapSize(rcd, w, h); |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4689 | } |
| 4690 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 4691 | public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd, |
| 4692 | boolean wantsSync) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4693 | mMediaFocusControl.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync); |
| 4694 | } |
| 4695 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4696 | @Override |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4697 | public void setRemoteStreamVolume(int index) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4698 | enforceSelfOrSystemUI("set the remote stream volume"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4699 | mMediaFocusControl.setRemoteStreamVolume(index); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 4702 | //========================================================================================== |
| 4703 | // Audio Focus |
| 4704 | //========================================================================================== |
| 4705 | public int requestAudioFocus(int mainStreamType, int durationHint, IBinder cb, |
| 4706 | IAudioFocusDispatcher fd, String clientId, String callingPackageName) { |
| 4707 | return mMediaFocusControl.requestAudioFocus(mainStreamType, durationHint, cb, fd, |
| 4708 | clientId, callingPackageName); |
| 4709 | } |
| 4710 | |
| 4711 | public int abandonAudioFocus(IAudioFocusDispatcher fd, String clientId) { |
| 4712 | return mMediaFocusControl.abandonAudioFocus(fd, clientId); |
| 4713 | } |
| 4714 | |
| 4715 | public void unregisterAudioFocusClient(String clientId) { |
| 4716 | mMediaFocusControl.unregisterAudioFocusClient(clientId); |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 4717 | } |
| 4718 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 4719 | public int getCurrentAudioFocus() { |
| 4720 | return mMediaFocusControl.getCurrentAudioFocus(); |
| 4721 | } |
| 4722 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4723 | //========================================================================================== |
| 4724 | // Device orientation |
| 4725 | //========================================================================================== |
| 4726 | /** |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4727 | * Handles device configuration changes that may map to a change in the orientation |
| 4728 | * or orientation. |
| 4729 | * Monitoring orientation and rotation is optional, and is defined by the definition and value |
| 4730 | * of the "ro.audio.monitorOrientation" and "ro.audio.monitorRotation" system properties. |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4731 | */ |
| 4732 | private void handleConfigurationChanged(Context context) { |
| 4733 | try { |
| 4734 | // reading new orientation "safely" (i.e. under try catch) in case anything |
| 4735 | // goes wrong when obtaining resources and configuration |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4736 | Configuration config = context.getResources().getConfiguration(); |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4737 | // TODO merge rotation and orientation |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4738 | if (mMonitorOrientation) { |
| 4739 | int newOrientation = config.orientation; |
| 4740 | if (newOrientation != mDeviceOrientation) { |
| 4741 | mDeviceOrientation = newOrientation; |
| 4742 | setOrientationForAudioSystem(); |
| 4743 | } |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4744 | } |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4745 | if (mMonitorRotation) { |
| 4746 | int newRotation = ((WindowManager) context.getSystemService( |
| 4747 | Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); |
| 4748 | if (newRotation != mDeviceRotation) { |
| 4749 | mDeviceRotation = newRotation; |
| 4750 | setRotationForAudioSystem(); |
| 4751 | } |
| 4752 | } |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4753 | sendMsg(mAudioHandler, |
| 4754 | MSG_CONFIGURE_SAFE_MEDIA_VOLUME, |
| 4755 | SENDMSG_REPLACE, |
| 4756 | 0, |
| 4757 | 0, |
| 4758 | null, |
| 4759 | 0); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4760 | |
| 4761 | boolean cameraSoundForced = mContext.getResources().getBoolean( |
| 4762 | com.android.internal.R.bool.config_camera_sound_forced); |
| 4763 | synchronized (mSettingsLock) { |
| 4764 | synchronized (mCameraSoundForced) { |
| 4765 | if (cameraSoundForced != mCameraSoundForced) { |
| 4766 | mCameraSoundForced = cameraSoundForced; |
| 4767 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4768 | if (!isPlatformTelevision()) { |
| 4769 | VolumeStreamState s = mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED]; |
| 4770 | if (cameraSoundForced) { |
| 4771 | s.setAllIndexesToMax(); |
| 4772 | mRingerModeAffectedStreams &= |
| 4773 | ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4774 | } else { |
| 4775 | s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM]); |
| 4776 | mRingerModeAffectedStreams |= |
| 4777 | (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); |
| 4778 | } |
| 4779 | // take new state into account for streams muted by ringer mode |
| 4780 | setRingerModeInt(getRingerMode(), false); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4781 | } |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 4782 | |
| 4783 | sendMsg(mAudioHandler, |
| 4784 | MSG_SET_FORCE_USE, |
| 4785 | SENDMSG_QUEUE, |
| 4786 | AudioSystem.FOR_SYSTEM, |
| 4787 | cameraSoundForced ? |
| 4788 | AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE, |
| 4789 | null, |
| 4790 | 0); |
| 4791 | |
| 4792 | sendMsg(mAudioHandler, |
| 4793 | MSG_SET_ALL_VOLUMES, |
| 4794 | SENDMSG_QUEUE, |
| 4795 | 0, |
| 4796 | 0, |
| 4797 | mStreamStates[AudioSystem.STREAM_SYSTEM_ENFORCED], 0); |
| 4798 | } |
| 4799 | } |
| 4800 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4801 | mVolumeController.setLayoutDirection(config.getLayoutDirection()); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4802 | } catch (Exception e) { |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4803 | Log.e(TAG, "Error handling configuration change: ", e); |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | private void setOrientationForAudioSystem() { |
| 4808 | switch (mDeviceOrientation) { |
| 4809 | case Configuration.ORIENTATION_LANDSCAPE: |
| 4810 | //Log.i(TAG, "orientation is landscape"); |
| 4811 | AudioSystem.setParameters("orientation=landscape"); |
| 4812 | break; |
| 4813 | case Configuration.ORIENTATION_PORTRAIT: |
| 4814 | //Log.i(TAG, "orientation is portrait"); |
| 4815 | AudioSystem.setParameters("orientation=portrait"); |
| 4816 | break; |
| 4817 | case Configuration.ORIENTATION_SQUARE: |
| 4818 | //Log.i(TAG, "orientation is square"); |
| 4819 | AudioSystem.setParameters("orientation=square"); |
| 4820 | break; |
| 4821 | case Configuration.ORIENTATION_UNDEFINED: |
| 4822 | //Log.i(TAG, "orientation is undefined"); |
| 4823 | AudioSystem.setParameters("orientation=undefined"); |
| 4824 | break; |
| 4825 | default: |
| 4826 | Log.e(TAG, "Unknown orientation"); |
| 4827 | } |
| 4828 | } |
| 4829 | |
Jean-Michel Trivi | bb6f871 | 2013-05-23 15:02:41 -0700 | [diff] [blame] | 4830 | private void setRotationForAudioSystem() { |
| 4831 | switch (mDeviceRotation) { |
| 4832 | case Surface.ROTATION_0: |
| 4833 | AudioSystem.setParameters("rotation=0"); |
| 4834 | break; |
| 4835 | case Surface.ROTATION_90: |
| 4836 | AudioSystem.setParameters("rotation=90"); |
| 4837 | break; |
| 4838 | case Surface.ROTATION_180: |
| 4839 | AudioSystem.setParameters("rotation=180"); |
| 4840 | break; |
| 4841 | case Surface.ROTATION_270: |
| 4842 | AudioSystem.setParameters("rotation=270"); |
| 4843 | break; |
| 4844 | default: |
| 4845 | Log.e(TAG, "Unknown device rotation"); |
| 4846 | } |
| 4847 | } |
| 4848 | |
Jean-Michel Trivi | f26f017 | 2012-04-25 16:23:20 -0700 | [diff] [blame] | 4849 | |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4850 | // Handles request to override default use of A2DP for media. |
| 4851 | public void setBluetoothA2dpOnInt(boolean on) { |
| 4852 | synchronized (mBluetoothA2dpEnabledLock) { |
| 4853 | mBluetoothA2dpEnabled = on; |
Eric Laurent | c390bed | 2012-07-03 12:24:05 -0700 | [diff] [blame] | 4854 | mAudioHandler.removeMessages(MSG_SET_FORCE_BT_A2DP_USE); |
| 4855 | AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, |
| 4856 | mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP); |
Eric Laurent | 7847211 | 2012-05-21 08:57:21 -0700 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4860 | @Override |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4861 | public void setRingtonePlayer(IRingtonePlayer player) { |
| 4862 | mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null); |
| 4863 | mRingtonePlayer = player; |
| 4864 | } |
| 4865 | |
| 4866 | @Override |
| 4867 | public IRingtonePlayer getRingtonePlayer() { |
| 4868 | return mRingtonePlayer; |
| 4869 | } |
| 4870 | |
| 4871 | @Override |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 4872 | public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) { |
| 4873 | synchronized (mCurAudioRoutes) { |
| 4874 | AudioRoutesInfo routes = new AudioRoutesInfo(mCurAudioRoutes); |
| 4875 | mRoutesObservers.register(observer); |
| 4876 | return routes; |
| 4877 | } |
| 4878 | } |
| 4879 | |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4880 | |
| 4881 | //========================================================================================== |
| 4882 | // Safe media volume management. |
| 4883 | // MUSIC stream volume level is limited when headphones are connected according to safety |
| 4884 | // regulation. When the user attempts to raise the volume above the limit, a warning is |
| 4885 | // displayed and the user has to acknowlegde before the volume is actually changed. |
| 4886 | // The volume index corresponding to the limit is stored in config_safe_media_volume_index |
| 4887 | // property. Platforms with a different limit must set this property accordingly in their |
| 4888 | // overlay. |
| 4889 | //========================================================================================== |
| 4890 | |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4891 | // mSafeMediaVolumeState indicates whether the media volume is limited over headphones. |
| 4892 | // It is SAFE_MEDIA_VOLUME_NOT_CONFIGURED at boot time until a network service is connected |
| 4893 | // or the configure time is elapsed. It is then set to SAFE_MEDIA_VOLUME_ACTIVE or |
| 4894 | // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it |
| 4895 | // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume() |
| 4896 | // (when user opts out). |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 4897 | private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0; |
| 4898 | private static final int SAFE_MEDIA_VOLUME_DISABLED = 1; |
| 4899 | private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2; // confirmed |
| 4900 | private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3; // unconfirmed |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4901 | private Integer mSafeMediaVolumeState; |
| 4902 | |
| 4903 | private int mMcc = 0; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4904 | // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4905 | private int mSafeMediaVolumeIndex; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4906 | // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced, |
| 4907 | private final int mSafeMediaVolumeDevices = AudioSystem.DEVICE_OUT_WIRED_HEADSET | |
| 4908 | AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
| 4909 | // mMusicActiveMs is the cumulative time of music activity since safe volume was disabled. |
| 4910 | // When this time reaches UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX, the safe media volume is re-enabled |
| 4911 | // automatically. mMusicActiveMs is rounded to a multiple of MUSIC_ACTIVE_POLL_PERIOD_MS. |
| 4912 | private int mMusicActiveMs; |
| 4913 | private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours |
| 4914 | 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] | 4915 | 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] | 4916 | |
| 4917 | private void setSafeMediaVolumeEnabled(boolean on) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4918 | synchronized (mSafeMediaVolumeState) { |
| 4919 | if ((mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_NOT_CONFIGURED) && |
| 4920 | (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_DISABLED)) { |
| 4921 | if (on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_INACTIVE)) { |
| 4922 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE; |
| 4923 | enforceSafeMediaVolume(); |
| 4924 | } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) { |
| 4925 | mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE; |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 4926 | mMusicActiveMs = 1; // nonzero = confirmed |
| 4927 | saveMusicActiveMs(); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4928 | sendMsg(mAudioHandler, |
| 4929 | MSG_CHECK_MUSIC_ACTIVE, |
| 4930 | SENDMSG_REPLACE, |
| 4931 | 0, |
| 4932 | 0, |
| 4933 | null, |
| 4934 | MUSIC_ACTIVE_POLL_PERIOD_MS); |
| 4935 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4936 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | private void enforceSafeMediaVolume() { |
| 4941 | VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4942 | int devices = mSafeMediaVolumeDevices; |
| 4943 | int i = 0; |
| 4944 | |
| 4945 | while (devices != 0) { |
| 4946 | int device = 1 << i++; |
| 4947 | if ((device & devices) == 0) { |
| 4948 | continue; |
| 4949 | } |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4950 | int index = streamState.getIndex(device); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4951 | if (index > mSafeMediaVolumeIndex) { |
Eric Laurent | 42b041e | 2013-03-29 11:36:03 -0700 | [diff] [blame] | 4952 | streamState.setIndex(mSafeMediaVolumeIndex, device); |
| 4953 | sendMsg(mAudioHandler, |
| 4954 | MSG_SET_DEVICE_VOLUME, |
| 4955 | SENDMSG_QUEUE, |
| 4956 | device, |
| 4957 | 0, |
| 4958 | streamState, |
| 4959 | 0); |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4960 | } |
| 4961 | devices &= ~device; |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | private boolean checkSafeMediaVolume(int streamType, int index, int device) { |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4966 | synchronized (mSafeMediaVolumeState) { |
| 4967 | if ((mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) && |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4968 | (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && |
| 4969 | ((device & mSafeMediaVolumeDevices) != 0) && |
| 4970 | (index > mSafeMediaVolumeIndex)) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4971 | return false; |
| 4972 | } |
| 4973 | return true; |
| 4974 | } |
| 4975 | } |
| 4976 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4977 | @Override |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4978 | public void disableSafeMediaVolume() { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4979 | enforceSelfOrSystemUI("disable the safe media volume"); |
Eric Laurent | d640bd3 | 2012-09-28 18:01:48 -0700 | [diff] [blame] | 4980 | synchronized (mSafeMediaVolumeState) { |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4981 | setSafeMediaVolumeEnabled(false); |
Eric Laurent | fde16d5 | 2012-12-03 14:42:39 -0800 | [diff] [blame] | 4982 | if (mPendingVolumeCommand != null) { |
| 4983 | onSetStreamVolume(mPendingVolumeCommand.mStreamType, |
| 4984 | mPendingVolumeCommand.mIndex, |
| 4985 | mPendingVolumeCommand.mFlags, |
| 4986 | mPendingVolumeCommand.mDevice); |
| 4987 | mPendingVolumeCommand = null; |
| 4988 | } |
Eric Laurent | c34dcc1 | 2012-09-10 13:51:52 -0700 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 4992 | //========================================================================================== |
| 4993 | // Hdmi Cec system audio mode. |
| 4994 | // If Hdmi Cec's system audio mode is on, audio service should notify volume change |
| 4995 | // to HdmiControlService so that audio recevier can handle volume change. |
| 4996 | //========================================================================================== |
| 4997 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 4998 | private class MyDisplayStatusCallback implements HdmiPlaybackClient.DisplayStatusCallback { |
| 4999 | public void onComplete(int status) { |
| 5000 | if (mHdmiManager != null) { |
| 5001 | synchronized (mHdmiManager) { |
| 5002 | mHdmiCecSink = (status != HdmiControlManager.POWER_STATUS_UNKNOWN); |
| 5003 | // Television devices without CEC service apply software volume on HDMI output |
| 5004 | if (isPlatformTelevision() && !mHdmiCecSink) { |
| 5005 | mFixedVolumeDevices &= ~AudioSystem.DEVICE_OUT_HDMI; |
| 5006 | } |
| 5007 | checkAllFixedVolumeDevices(); |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | }; |
| 5012 | |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5013 | // If HDMI-CEC system audio is supported |
| 5014 | private boolean mHdmiSystemAudioSupported = false; |
| 5015 | // Set only when device is tv. |
| 5016 | private HdmiTvClient mHdmiTvClient; |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5017 | // true if the device has system feature PackageManager.FEATURE_TELEVISION. |
| 5018 | // cached HdmiControlManager interface |
| 5019 | private HdmiControlManager mHdmiManager; |
| 5020 | // Set only when device is a set-top box. |
| 5021 | private HdmiPlaybackClient mHdmiPlaybackClient; |
| 5022 | // true if we are a set-top box, an HDMI sink is connected and it supports CEC. |
| 5023 | private boolean mHdmiCecSink; |
| 5024 | |
| 5025 | private MyDisplayStatusCallback mHdmiDisplayStatusCallback = new MyDisplayStatusCallback(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5026 | |
| 5027 | @Override |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 5028 | public int setHdmiSystemAudioSupported(boolean on) { |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5029 | int device = AudioSystem.DEVICE_NONE; |
| 5030 | if (mHdmiManager != null) { |
| 5031 | synchronized (mHdmiManager) { |
| 5032 | if (mHdmiTvClient == null) { |
| 5033 | Log.w(TAG, "Only Hdmi-Cec enabled TV device supports system audio mode."); |
| 5034 | return device; |
| 5035 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5036 | |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5037 | synchronized (mHdmiTvClient) { |
| 5038 | if (mHdmiSystemAudioSupported != on) { |
| 5039 | mHdmiSystemAudioSupported = on; |
| 5040 | AudioSystem.setForceUse(AudioSystem.FOR_HDMI_SYSTEM_AUDIO, |
| 5041 | on ? AudioSystem.FORCE_HDMI_SYSTEM_AUDIO_ENFORCED : |
| 5042 | AudioSystem.FORCE_NONE); |
| 5043 | } |
| 5044 | device = AudioSystem.getDevicesForStream(AudioSystem.STREAM_MUSIC); |
| 5045 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5046 | } |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5047 | } |
Eric Laurent | 212532b | 2014-07-21 15:43:18 -0700 | [diff] [blame] | 5048 | return device; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 5049 | } |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 5050 | |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5051 | //========================================================================================== |
| 5052 | // Camera shutter sound policy. |
| 5053 | // config_camera_sound_forced configuration option in config.xml defines if the camera shutter |
| 5054 | // sound is forced (sound even if the device is in silent mode) or not. This option is false by |
| 5055 | // default and can be overridden by country specific overlay in values-mccXXX/config.xml. |
| 5056 | //========================================================================================== |
| 5057 | |
| 5058 | // cached value of com.android.internal.R.bool.config_camera_sound_forced |
| 5059 | private Boolean mCameraSoundForced; |
| 5060 | |
| 5061 | // called by android.hardware.Camera to populate CameraInfo.canDisableShutterSound |
| 5062 | public boolean isCameraSoundForced() { |
| 5063 | synchronized (mCameraSoundForced) { |
| 5064 | return mCameraSoundForced; |
| 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | private static final String[] RINGER_MODE_NAMES = new String[] { |
| 5069 | "SILENT", |
| 5070 | "VIBRATE", |
| 5071 | "NORMAL" |
| 5072 | }; |
| 5073 | |
| 5074 | private void dumpRingerMode(PrintWriter pw) { |
| 5075 | pw.println("\nRinger mode: "); |
| 5076 | pw.println("- mode: "+RINGER_MODE_NAMES[mRingerMode]); |
| 5077 | pw.print("- ringer mode affected streams = 0x"); |
| 5078 | pw.println(Integer.toHexString(mRingerModeAffectedStreams)); |
| 5079 | pw.print("- ringer mode muted streams = 0x"); |
| 5080 | pw.println(Integer.toHexString(mRingerModeMutedStreams)); |
| 5081 | } |
| 5082 | |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5083 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5084 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 5085 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 5086 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 5087 | mMediaFocusControl.dump(pw); |
Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 5088 | dumpStreamStates(pw); |
Eric Laurent | dd45d01 | 2012-10-08 09:04:34 -0700 | [diff] [blame] | 5089 | dumpRingerMode(pw); |
Dianne Hackborn | 632ca41 | 2012-06-14 19:34:10 -0700 | [diff] [blame] | 5090 | pw.println("\nAudio routes:"); |
| 5091 | pw.print(" mMainType=0x"); pw.println(Integer.toHexString(mCurAudioRoutes.mMainType)); |
| 5092 | pw.print(" mBluetoothName="); pw.println(mCurAudioRoutes.mBluetoothName); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5093 | |
| 5094 | pw.println("\nOther state:"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5095 | pw.print(" mVolumeController="); pw.println(mVolumeController); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5096 | pw.print(" mSafeMediaVolumeState="); |
| 5097 | pw.println(safeMediaVolumeStateToString(mSafeMediaVolumeState)); |
| 5098 | pw.print(" mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex); |
| 5099 | pw.print(" mPendingVolumeCommand="); pw.println(mPendingVolumeCommand); |
| 5100 | pw.print(" mMusicActiveMs="); pw.println(mMusicActiveMs); |
John Spurlock | aa5ee4d | 2014-07-25 13:05:12 -0400 | [diff] [blame^] | 5101 | pw.print(" mMcc="); pw.println(mMcc); |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | private static String safeMediaVolumeStateToString(Integer state) { |
| 5105 | switch(state) { |
| 5106 | case SAFE_MEDIA_VOLUME_NOT_CONFIGURED: return "SAFE_MEDIA_VOLUME_NOT_CONFIGURED"; |
| 5107 | case SAFE_MEDIA_VOLUME_DISABLED: return "SAFE_MEDIA_VOLUME_DISABLED"; |
| 5108 | case SAFE_MEDIA_VOLUME_INACTIVE: return "SAFE_MEDIA_VOLUME_INACTIVE"; |
| 5109 | case SAFE_MEDIA_VOLUME_ACTIVE: return "SAFE_MEDIA_VOLUME_ACTIVE"; |
| 5110 | } |
| 5111 | return null; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 5112 | } |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 5113 | |
| 5114 | // Inform AudioFlinger of our device's low RAM attribute |
| 5115 | private static void readAndSetLowRamDevice() |
| 5116 | { |
| 5117 | int status = AudioSystem.setLowRamDevice(ActivityManager.isLowRamDeviceStatic()); |
| 5118 | if (status != 0) { |
| 5119 | Log.w(TAG, "AudioFlinger informed of device's low RAM attribute; status " + status); |
| 5120 | } |
| 5121 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5122 | |
| 5123 | private void enforceSelfOrSystemUI(String action) { |
| 5124 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 5125 | "Only SystemUI can " + action); |
| 5126 | } |
| 5127 | |
| 5128 | @Override |
| 5129 | public void setVolumeController(final IVolumeController controller) { |
| 5130 | enforceSelfOrSystemUI("set the volume controller"); |
| 5131 | |
| 5132 | // return early if things are not actually changing |
| 5133 | if (mVolumeController.isSameBinder(controller)) { |
| 5134 | return; |
| 5135 | } |
| 5136 | |
| 5137 | // dismiss the old volume controller |
| 5138 | mVolumeController.postDismiss(); |
| 5139 | if (controller != null) { |
| 5140 | // we are about to register a new controller, listen for its death |
| 5141 | try { |
| 5142 | controller.asBinder().linkToDeath(new DeathRecipient() { |
| 5143 | @Override |
| 5144 | public void binderDied() { |
| 5145 | if (mVolumeController.isSameBinder(controller)) { |
| 5146 | Log.w(TAG, "Current remote volume controller died, unregistering"); |
| 5147 | setVolumeController(null); |
| 5148 | } |
| 5149 | } |
| 5150 | }, 0); |
| 5151 | } catch (RemoteException e) { |
| 5152 | // noop |
| 5153 | } |
| 5154 | } |
| 5155 | mVolumeController.setController(controller); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5156 | if (DEBUG_VOL) Log.d(TAG, "Volume controller: " + mVolumeController); |
| 5157 | } |
| 5158 | |
| 5159 | @Override |
| 5160 | public void notifyVolumeControllerVisible(final IVolumeController controller, boolean visible) { |
| 5161 | enforceSelfOrSystemUI("notify about volume controller visibility"); |
| 5162 | |
| 5163 | // return early if the controller is not current |
| 5164 | if (!mVolumeController.isSameBinder(controller)) { |
| 5165 | return; |
| 5166 | } |
| 5167 | |
| 5168 | mVolumeController.setVisible(visible); |
| 5169 | if (DEBUG_VOL) Log.d(TAG, "Volume controller visible: " + visible); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5170 | } |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5171 | |
| 5172 | public static class VolumeController { |
| 5173 | private static final String TAG = "VolumeController"; |
| 5174 | |
| 5175 | private IVolumeController mController; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5176 | private boolean mVisible; |
| 5177 | private long mNextLongPress; |
| 5178 | private int mLongPressTimeout; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5179 | |
| 5180 | public void setController(IVolumeController controller) { |
| 5181 | mController = controller; |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5182 | mVisible = false; |
| 5183 | } |
| 5184 | |
| 5185 | public void loadSettings(ContentResolver cr) { |
| 5186 | mLongPressTimeout = Settings.Secure.getIntForUser(cr, |
| 5187 | Settings.Secure.LONG_PRESS_TIMEOUT, 500, UserHandle.USER_CURRENT); |
| 5188 | } |
| 5189 | |
| 5190 | public boolean suppressAdjustment(int resolvedStream, int flags) { |
| 5191 | boolean suppress = false; |
| 5192 | if (resolvedStream == AudioSystem.STREAM_RING && mController != null) { |
| 5193 | final long now = SystemClock.uptimeMillis(); |
| 5194 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0 && !mVisible) { |
| 5195 | // ui will become visible |
| 5196 | if (mNextLongPress < now) { |
| 5197 | mNextLongPress = now + mLongPressTimeout; |
| 5198 | } |
| 5199 | suppress = true; |
| 5200 | } else if (mNextLongPress > 0) { // in a long-press |
| 5201 | if (now > mNextLongPress) { |
| 5202 | // long press triggered, no more suppression |
| 5203 | mNextLongPress = 0; |
| 5204 | } else { |
| 5205 | // keep suppressing until the long press triggers |
| 5206 | suppress = true; |
| 5207 | } |
| 5208 | } |
| 5209 | } |
| 5210 | return suppress; |
| 5211 | } |
| 5212 | |
| 5213 | public void setVisible(boolean visible) { |
| 5214 | mVisible = visible; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | public boolean isSameBinder(IVolumeController controller) { |
| 5218 | return Objects.equals(asBinder(), binder(controller)); |
| 5219 | } |
| 5220 | |
| 5221 | public IBinder asBinder() { |
| 5222 | return binder(mController); |
| 5223 | } |
| 5224 | |
| 5225 | private static IBinder binder(IVolumeController controller) { |
| 5226 | return controller == null ? null : controller.asBinder(); |
| 5227 | } |
| 5228 | |
| 5229 | @Override |
| 5230 | public String toString() { |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5231 | return "VolumeController(" + asBinder() + ",mVisible=" + mVisible + ")"; |
RoboErik | d09bd0c | 2014-06-24 17:45:19 -0700 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | public void postDisplaySafeVolumeWarning(int flags) { |
| 5235 | if (mController == null) |
| 5236 | return; |
| 5237 | try { |
| 5238 | mController.displaySafeVolumeWarning(flags); |
| 5239 | } catch (RemoteException e) { |
| 5240 | Log.w(TAG, "Error calling displaySafeVolumeWarning", e); |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | public void postVolumeChanged(int streamType, int flags) { |
| 5245 | if (mController == null) |
| 5246 | return; |
| 5247 | try { |
| 5248 | mController.volumeChanged(streamType, flags); |
| 5249 | } catch (RemoteException e) { |
| 5250 | Log.w(TAG, "Error calling volumeChanged", e); |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | public void postMasterVolumeChanged(int flags) { |
| 5255 | if (mController == null) |
| 5256 | return; |
| 5257 | try { |
| 5258 | mController.masterVolumeChanged(flags); |
| 5259 | } catch (RemoteException e) { |
| 5260 | Log.w(TAG, "Error calling masterVolumeChanged", e); |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | public void postMasterMuteChanged(int flags) { |
| 5265 | if (mController == null) |
| 5266 | return; |
| 5267 | try { |
| 5268 | mController.masterMuteChanged(flags); |
| 5269 | } catch (RemoteException e) { |
| 5270 | Log.w(TAG, "Error calling masterMuteChanged", e); |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | public void setLayoutDirection(int layoutDirection) { |
| 5275 | if (mController == null) |
| 5276 | return; |
| 5277 | try { |
| 5278 | mController.setLayoutDirection(layoutDirection); |
| 5279 | } catch (RemoteException e) { |
| 5280 | Log.w(TAG, "Error calling setLayoutDirection", e); |
| 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | public void postDismiss() { |
| 5285 | if (mController == null) |
| 5286 | return; |
| 5287 | try { |
| 5288 | mController.dismiss(); |
| 5289 | } catch (RemoteException e) { |
| 5290 | Log.w(TAG, "Error calling dismiss", e); |
| 5291 | } |
| 5292 | } |
| 5293 | } |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5294 | |
| 5295 | //========================================================================================== |
| 5296 | // Audio policy management |
| 5297 | //========================================================================================== |
| 5298 | public boolean registerAudioPolicy(AudioPolicyConfig policyConfig, IBinder cb) { |
| 5299 | //Log.v(TAG, "registerAudioPolicy for " + cb + " got policy:" + policyConfig); |
| 5300 | boolean hasPermissionForPolicy = |
| 5301 | (PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 5302 | android.Manifest.permission.MODIFY_AUDIO_ROUTING)); |
| 5303 | if (!hasPermissionForPolicy) { |
| 5304 | Slog.w(TAG, "Can't register audio policy for pid " + Binder.getCallingPid() + " / uid " |
| 5305 | + Binder.getCallingUid() + ", need MODIFY_AUDIO_ROUTING"); |
| 5306 | return false; |
| 5307 | } |
| 5308 | synchronized (mAudioPolicies) { |
| 5309 | AudioPolicyProxy app = new AudioPolicyProxy(policyConfig, cb); |
| 5310 | try { |
| 5311 | cb.linkToDeath(app, 0/*flags*/); |
| 5312 | mAudioPolicies.put(cb, app); |
| 5313 | } catch (RemoteException e) { |
| 5314 | // audio policy owner has already died! |
| 5315 | Slog.w(TAG, "Audio policy registration failed, could not link to " + cb + |
| 5316 | " binder death", e); |
| 5317 | return false; |
| 5318 | } |
| 5319 | } |
| 5320 | // TODO implement registration with native audio policy (including permission check) |
| 5321 | return true; |
| 5322 | } |
| 5323 | public void unregisterAudioPolicyAsync(IBinder cb) { |
| 5324 | synchronized (mAudioPolicies) { |
| 5325 | AudioPolicyProxy app = mAudioPolicies.remove(cb); |
| 5326 | if (app == null) { |
| 5327 | Slog.w(TAG, "Trying to unregister unknown audio policy for pid " |
| 5328 | + Binder.getCallingPid() + " / uid " + Binder.getCallingUid()); |
| 5329 | } else { |
| 5330 | cb.unlinkToDeath(app, 0/*flags*/); |
| 5331 | } |
| 5332 | } |
| 5333 | // TODO implement registration with native audio policy |
| 5334 | } |
| 5335 | |
| 5336 | public class AudioPolicyProxy implements IBinder.DeathRecipient { |
| 5337 | private static final String TAG = "AudioPolicyProxy"; |
| 5338 | AudioPolicyConfig mConfig; |
| 5339 | IBinder mToken; |
| 5340 | AudioPolicyProxy(AudioPolicyConfig config, IBinder token) { |
| 5341 | mConfig = config; |
| 5342 | mToken = token; |
| 5343 | } |
| 5344 | |
| 5345 | public void binderDied() { |
| 5346 | synchronized (mAudioPolicies) { |
| 5347 | Log.v(TAG, "audio policy " + mToken + " died"); |
| 5348 | mAudioPolicies.remove(mToken); |
| 5349 | } |
| 5350 | } |
| 5351 | }; |
| 5352 | |
| 5353 | private HashMap<IBinder, AudioPolicyProxy> mAudioPolicies = |
| 5354 | new HashMap<IBinder, AudioPolicyProxy>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5355 | } |