John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 17 | package com.android.systemui.volume; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.DialogInterface.OnClickListener; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| 29 | import android.content.pm.ApplicationInfo; |
| 30 | import android.content.res.Configuration; |
| 31 | import android.media.AudioManager; |
| 32 | import android.media.IRemoteVolumeController; |
| 33 | import android.media.IVolumeController; |
| 34 | import android.media.session.ISessionController; |
| 35 | import android.media.session.MediaController; |
| 36 | import android.media.session.MediaSessionManager; |
| 37 | import android.os.Bundle; |
| 38 | import android.os.Handler; |
| 39 | import android.os.RemoteException; |
| 40 | import android.provider.Settings; |
| 41 | import android.text.TextUtils; |
| 42 | import android.util.Log; |
| 43 | |
| 44 | import com.android.systemui.R; |
| 45 | import com.android.systemui.SystemUI; |
| 46 | import com.android.systemui.keyguard.KeyguardViewMediator; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame^] | 47 | import com.android.systemui.qs.tiles.DndTile; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 48 | import com.android.systemui.statusbar.ServiceMonitor; |
| 49 | import com.android.systemui.statusbar.phone.PhoneStatusBar; |
| 50 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
| 51 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 52 | import com.android.systemui.statusbar.policy.ZenModeControllerImpl; |
| 53 | |
| 54 | import java.io.FileDescriptor; |
| 55 | import java.io.PrintWriter; |
| 56 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 57 | public class VolumeUI extends SystemUI { |
| 58 | private static final String TAG = "VolumeUI"; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 59 | private static boolean LOGD = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 60 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 61 | private final Handler mHandler = new Handler(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 62 | private final Receiver mReceiver = new Receiver(); |
| 63 | private final RestorationNotification mRestorationNotification = new RestorationNotification(); |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame] | 64 | |
John Spurlock | f2565a8 | 2014-10-23 20:16:22 -0400 | [diff] [blame] | 65 | private boolean mEnabled; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 66 | private AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 67 | private NotificationManager mNotificationManager; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 68 | private MediaSessionManager mMediaSessionManager; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 69 | private VolumeController mVolumeController; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 70 | private RemoteVolumeController mRemoteVolumeController; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 71 | private ServiceMonitor mVolumeControllerService; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 72 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 73 | private VolumePanel mPanel; |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame] | 74 | private int mDismissDelay; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public void start() { |
John Spurlock | f2565a8 | 2014-10-23 20:16:22 -0400 | [diff] [blame] | 78 | mEnabled = mContext.getResources().getBoolean(R.bool.enable_volume_ui); |
| 79 | if (!mEnabled) return; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 80 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 81 | mNotificationManager = |
| 82 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 83 | mMediaSessionManager = (MediaSessionManager) mContext |
| 84 | .getSystemService(Context.MEDIA_SESSION_SERVICE); |
| 85 | initPanel(); |
| 86 | mVolumeController = new VolumeController(); |
| 87 | mRemoteVolumeController = new RemoteVolumeController(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 88 | putComponent(VolumeComponent.class, mVolumeController); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 89 | mReceiver.start(); |
| 90 | mVolumeControllerService = new ServiceMonitor(TAG, LOGD, |
| 91 | mContext, Settings.Secure.VOLUME_CONTROLLER_SERVICE_COMPONENT, |
| 92 | new ServiceMonitorCallbacks()); |
| 93 | mVolumeControllerService.start(); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 94 | } |
| 95 | |
John Spurlock | ad494bc | 2014-07-19 15:56:19 -0400 | [diff] [blame] | 96 | @Override |
John Spurlock | 7e6809a | 2014-08-06 16:03:14 -0400 | [diff] [blame] | 97 | protected void onConfigurationChanged(Configuration newConfig) { |
| 98 | super.onConfigurationChanged(newConfig); |
| 99 | if (mPanel != null) { |
| 100 | mPanel.onConfigurationChanged(newConfig); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
John Spurlock | ad494bc | 2014-07-19 15:56:19 -0400 | [diff] [blame] | 105 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
John Spurlock | f2565a8 | 2014-10-23 20:16:22 -0400 | [diff] [blame] | 106 | pw.print("mEnabled="); pw.println(mEnabled); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 107 | pw.print("mVolumeControllerService="); pw.println(mVolumeControllerService.getComponent()); |
John Spurlock | ad494bc | 2014-07-19 15:56:19 -0400 | [diff] [blame] | 108 | if (mPanel != null) { |
| 109 | mPanel.dump(fd, pw, args); |
| 110 | } |
| 111 | } |
| 112 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 113 | private void setVolumeController(boolean register) { |
| 114 | if (register) { |
| 115 | if (LOGD) Log.d(TAG, "Registering volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 116 | mAudioManager.setVolumeController(mVolumeController); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 117 | mMediaSessionManager.setRemoteVolumeController(mRemoteVolumeController); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame^] | 118 | DndTile.setVisible(mContext, false); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 119 | } else { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 120 | if (LOGD) Log.d(TAG, "Unregistering volume controller"); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 121 | mAudioManager.setVolumeController(null); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 122 | mMediaSessionManager.setRemoteVolumeController(null); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 126 | private void initPanel() { |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame] | 127 | mDismissDelay = mContext.getResources().getInteger(R.integer.volume_panel_dismiss_delay); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 128 | mPanel = new VolumePanel(mContext, new ZenModeControllerImpl(mContext, mHandler)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 129 | mPanel.setCallback(new VolumePanel.Callback() { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 130 | @Override |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 131 | public void onZenSettings() { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 132 | mHandler.removeCallbacks(mStartZenSettings); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 133 | mHandler.post(mStartZenSettings); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onInteraction() { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 138 | final KeyguardViewMediator kvm = getComponent(KeyguardViewMediator.class); |
| 139 | if (kvm != null) { |
| 140 | kvm.userActivity(); |
| 141 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 142 | } |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public void onVisible(boolean visible) { |
| 146 | if (mAudioManager != null && mVolumeController != null) { |
| 147 | mAudioManager.notifyVolumeControllerVisible(mVolumeController, visible); |
| 148 | } |
| 149 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 150 | }); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 153 | private String getAppLabel(ComponentName component) { |
| 154 | final String pkg = component.getPackageName(); |
| 155 | try { |
| 156 | final ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(pkg, 0); |
| 157 | final String rt = mContext.getPackageManager().getApplicationLabel(ai).toString(); |
| 158 | if (!TextUtils.isEmpty(rt)) { |
| 159 | return rt; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 160 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 161 | } catch (Exception e) { |
| 162 | Log.w(TAG, "Error loading app label", e); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 163 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 164 | return pkg; |
| 165 | } |
| 166 | |
| 167 | private void showServiceActivationDialog(final ComponentName component) { |
| 168 | final SystemUIDialog d = new SystemUIDialog(mContext); |
| 169 | d.setMessage(mContext.getString(R.string.volumeui_prompt_message, getAppLabel(component))); |
| 170 | d.setPositiveButton(R.string.volumeui_prompt_allow, new OnClickListener() { |
| 171 | @Override |
| 172 | public void onClick(DialogInterface dialog, int which) { |
| 173 | mVolumeControllerService.setComponent(component); |
| 174 | } |
| 175 | }); |
| 176 | d.setNegativeButton(R.string.volumeui_prompt_deny, null); |
| 177 | d.show(); |
| 178 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 179 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 180 | private final Runnable mStartZenSettings = new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
Jorim Jaggi | 8de4311c | 2014-08-11 22:36:20 +0200 | [diff] [blame] | 183 | getComponent(PhoneStatusBar.class).startActivityDismissingKeyguard( |
Jorim Jaggi | 85dc23c | 2014-09-08 14:42:29 +0200 | [diff] [blame] | 184 | ZenModePanel.ZEN_SETTINGS, true /* onlyProvisioned */, true /* dismissShade */); |
John Spurlock | ad494bc | 2014-07-19 15:56:19 -0400 | [diff] [blame] | 185 | mPanel.postDismiss(mDismissDelay); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 186 | } |
| 187 | }; |
| 188 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 189 | /** For now, simply host an unmodified base volume panel in this process. */ |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 190 | private final class VolumeController extends IVolumeController.Stub implements VolumeComponent { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 191 | |
| 192 | @Override |
| 193 | public void displaySafeVolumeWarning(int flags) throws RemoteException { |
| 194 | mPanel.postDisplaySafeVolumeWarning(flags); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void volumeChanged(int streamType, int flags) |
| 199 | throws RemoteException { |
| 200 | mPanel.postVolumeChanged(streamType, flags); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void masterVolumeChanged(int flags) throws RemoteException { |
| 205 | mPanel.postMasterVolumeChanged(flags); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void masterMuteChanged(int flags) throws RemoteException { |
| 210 | mPanel.postMasterMuteChanged(flags); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void setLayoutDirection(int layoutDirection) |
| 215 | throws RemoteException { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 216 | mPanel.postLayoutDirection(layoutDirection); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void dismiss() throws RemoteException { |
John Spurlock | 7fbf573 | 2014-11-18 11:40:22 -0500 | [diff] [blame] | 221 | dismissNow(); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 222 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 223 | |
| 224 | @Override |
| 225 | public ZenModeController getZenController() { |
John Spurlock | ad494bc | 2014-07-19 15:56:19 -0400 | [diff] [blame] | 226 | return mPanel.getZenController(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 227 | } |
John Spurlock | bb4a702 | 2014-11-08 12:40:19 -0500 | [diff] [blame] | 228 | |
| 229 | @Override |
| 230 | public void dispatchDemoCommand(String command, Bundle args) { |
| 231 | mPanel.dispatchDemoCommand(command, args); |
| 232 | } |
John Spurlock | 7fbf573 | 2014-11-18 11:40:22 -0500 | [diff] [blame] | 233 | |
| 234 | @Override |
| 235 | public void dismissNow() { |
| 236 | mPanel.postDismiss(0); |
| 237 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 238 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 239 | |
| 240 | private final class RemoteVolumeController extends IRemoteVolumeController.Stub { |
| 241 | |
| 242 | @Override |
| 243 | public void remoteVolumeChanged(ISessionController binder, int flags) |
| 244 | throws RemoteException { |
RoboErik | 031149c | 2014-07-25 16:00:52 -0700 | [diff] [blame] | 245 | MediaController controller = new MediaController(mContext, binder); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 246 | mPanel.postRemoteVolumeChanged(controller, flags); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void updateRemoteController(ISessionController session) throws RemoteException { |
| 251 | mPanel.postRemoteSliderVisibility(session != null); |
| 252 | // TODO stash default session in case the slider can be opened other |
| 253 | // than by remoteVolumeChanged. |
| 254 | } |
| 255 | } |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 256 | |
| 257 | private final class ServiceMonitorCallbacks implements ServiceMonitor.Callbacks { |
| 258 | @Override |
| 259 | public void onNoService() { |
| 260 | if (LOGD) Log.d(TAG, "onNoService"); |
| 261 | setVolumeController(true); |
| 262 | mRestorationNotification.hide(); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public long onServiceStartAttempt() { |
| 267 | if (LOGD) Log.d(TAG, "onServiceStartAttempt"); |
| 268 | setVolumeController(false); |
| 269 | mVolumeController.dismissNow(); |
| 270 | mRestorationNotification.show(); |
| 271 | return 0; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private final class Receiver extends BroadcastReceiver { |
| 276 | private static final String ENABLE = "com.android.systemui.vui.ENABLE"; |
| 277 | private static final String DISABLE = "com.android.systemui.vui.DISABLE"; |
| 278 | private static final String EXTRA_COMPONENT = "component"; |
| 279 | |
| 280 | public void start() { |
| 281 | final IntentFilter filter = new IntentFilter(); |
| 282 | filter.addAction(ENABLE); |
| 283 | filter.addAction(DISABLE); |
| 284 | mContext.registerReceiver(this, filter, null, mHandler); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void onReceive(Context context, Intent intent) { |
| 289 | final String action = intent.getAction(); |
| 290 | final ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT); |
| 291 | final boolean current = component.equals(mVolumeControllerService.getComponent()); |
| 292 | if (ENABLE.equals(action) && component != null) { |
| 293 | if (!current) { |
| 294 | showServiceActivationDialog(component); |
| 295 | } |
| 296 | } |
| 297 | if (DISABLE.equals(action) && component != null) { |
| 298 | if (current) { |
| 299 | mVolumeControllerService.setComponent(null); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private final class RestorationNotification { |
| 306 | public void hide() { |
| 307 | mNotificationManager.cancel(R.id.notification_volumeui); |
| 308 | } |
| 309 | |
| 310 | public void show() { |
| 311 | final ComponentName component = mVolumeControllerService.getComponent(); |
| 312 | if (component == null) { |
| 313 | Log.w(TAG, "Not showing restoration notification, component not active"); |
| 314 | return; |
| 315 | } |
| 316 | final Intent intent = new Intent(Receiver.DISABLE) |
| 317 | .putExtra(Receiver.EXTRA_COMPONENT, component); |
| 318 | mNotificationManager.notify(R.id.notification_volumeui, |
| 319 | new Notification.Builder(mContext) |
| 320 | .setSmallIcon(R.drawable.ic_ringer_audible) |
| 321 | .setWhen(0) |
| 322 | .setShowWhen(false) |
| 323 | .setOngoing(true) |
| 324 | .setContentTitle(mContext.getString( |
| 325 | R.string.volumeui_notification_title, getAppLabel(component))) |
| 326 | .setContentText(mContext.getString(R.string.volumeui_notification_text)) |
| 327 | .setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0)) |
| 328 | .setPriority(Notification.PRIORITY_MIN) |
| 329 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 330 | .setColor(mContext.getResources().getColor( |
| 331 | com.android.internal.R.color.system_notification_accent_color)) |
| 332 | .build()); |
| 333 | } |
| 334 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 335 | } |