John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 1 | package com.android.systemui.volume; |
| 2 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 3 | import android.app.ActivityManagerNative; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 4 | import android.content.Context; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 5 | import android.content.Intent; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6 | import android.database.ContentObserver; |
| 7 | import android.media.AudioManager; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 8 | import android.media.IRemoteVolumeController; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 9 | import android.media.IVolumeController; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 10 | import android.media.session.ISessionController; |
| 11 | import android.media.session.MediaController; |
| 12 | import android.media.session.MediaSessionManager; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 13 | import android.net.Uri; |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 14 | import android.os.AsyncTask; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 15 | import android.os.Handler; |
| 16 | import android.os.RemoteException; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 17 | import android.os.UserHandle; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 18 | import android.provider.Settings; |
| 19 | import android.util.Log; |
| 20 | |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 21 | import com.android.systemui.R; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 22 | import com.android.systemui.SystemUI; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 23 | import com.android.systemui.keyguard.KeyguardViewMediator; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 24 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 25 | import com.android.systemui.statusbar.policy.ZenModeControllerImpl; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Copyright (C) 2014 The Android Open Source Project |
| 29 | * |
| 30 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | * you may not use this file except in compliance with the License. |
| 32 | * You may obtain a copy of the License at |
| 33 | * |
| 34 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | * |
| 36 | * Unless required by applicable law or agreed to in writing, software |
| 37 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 38 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | * See the License for the specific language governing permissions and |
| 40 | * limitations under the License. |
| 41 | */ |
| 42 | |
| 43 | public class VolumeUI extends SystemUI { |
| 44 | private static final String TAG = "VolumeUI"; |
| 45 | private static final String SETTING = "systemui_volume_controller"; // for testing |
| 46 | private static final Uri SETTING_URI = Settings.Global.getUriFor(SETTING); |
| 47 | private static final int DEFAULT = 1; // enabled by default |
| 48 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 49 | private final Handler mHandler = new Handler(); |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 50 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 51 | private AudioManager mAudioManager; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 52 | private MediaSessionManager mMediaSessionManager; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 53 | private VolumeController mVolumeController; |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 54 | private RemoteVolumeController mRemoteVolumeController; |
| 55 | |
| 56 | private VolumePanel mDialogPanel; |
| 57 | private VolumePanel mPanel; |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 58 | private int mDismissDelay; |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 59 | |
| 60 | @Override |
| 61 | public void start() { |
| 62 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 63 | mMediaSessionManager = (MediaSessionManager) mContext |
| 64 | .getSystemService(Context.MEDIA_SESSION_SERVICE); |
| 65 | initPanel(); |
| 66 | mVolumeController = new VolumeController(); |
| 67 | mRemoteVolumeController = new RemoteVolumeController(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 68 | putComponent(VolumeComponent.class, mVolumeController); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 69 | updateController(); |
| 70 | mContext.getContentResolver().registerContentObserver(SETTING_URI, false, mObserver); |
| 71 | } |
| 72 | |
| 73 | private void updateController() { |
| 74 | if (Settings.Global.getInt(mContext.getContentResolver(), SETTING, DEFAULT) != 0) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 75 | Log.d(TAG, "Registering volume controller"); |
| 76 | mAudioManager.setVolumeController(mVolumeController); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 77 | mMediaSessionManager.setRemoteVolumeController(mRemoteVolumeController); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 78 | } else { |
| 79 | Log.d(TAG, "Unregistering volume controller"); |
| 80 | mAudioManager.setVolumeController(null); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 81 | mMediaSessionManager.setRemoteVolumeController(null); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 85 | private void initPanel() { |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 86 | mDismissDelay = mContext.getResources().getInteger(R.integer.volume_panel_dismiss_delay); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 87 | mPanel = new VolumePanel(mContext, null, new ZenModeControllerImpl(mContext, mHandler)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 88 | mPanel.setCallback(new VolumePanel.Callback() { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 89 | @Override |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 90 | public void onZenSettings() { |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 91 | mHandler.removeCallbacks(mStartZenSettings); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 92 | mHandler.post(mStartZenSettings); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onInteraction() { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 97 | final KeyguardViewMediator kvm = getComponent(KeyguardViewMediator.class); |
| 98 | if (kvm != null) { |
| 99 | kvm.userActivity(); |
| 100 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 101 | } |
| 102 | }); |
| 103 | mDialogPanel = mPanel; |
| 104 | } |
| 105 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 106 | private final ContentObserver mObserver = new ContentObserver(mHandler) { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 107 | public void onChange(boolean selfChange, Uri uri) { |
| 108 | if (SETTING_URI.equals(uri)) { |
| 109 | updateController(); |
| 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 114 | private final Runnable mStartZenSettings = new Runnable() { |
| 115 | @Override |
| 116 | public void run() { |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 117 | AsyncTask.execute(new Runnable() { |
| 118 | @Override |
| 119 | public void run() { |
| 120 | try { |
| 121 | // Dismiss the lock screen when Settings starts. |
| 122 | ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity(); |
| 123 | } catch (RemoteException e) { |
| 124 | } |
| 125 | final Intent intent = ZenModePanel.ZEN_SETTINGS; |
| 126 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 127 | mContext.startActivityAsUser(intent, new UserHandle(UserHandle.USER_CURRENT)); |
| 128 | } |
| 129 | }); |
| 130 | mDialogPanel.postDismiss(mDismissDelay); |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 131 | } |
| 132 | }; |
| 133 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 134 | /** For now, simply host an unmodified base volume panel in this process. */ |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 135 | private final class VolumeController extends IVolumeController.Stub implements VolumeComponent { |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public void displaySafeVolumeWarning(int flags) throws RemoteException { |
| 139 | mPanel.postDisplaySafeVolumeWarning(flags); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void volumeChanged(int streamType, int flags) |
| 144 | throws RemoteException { |
| 145 | mPanel.postVolumeChanged(streamType, flags); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void masterVolumeChanged(int flags) throws RemoteException { |
| 150 | mPanel.postMasterVolumeChanged(flags); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void masterMuteChanged(int flags) throws RemoteException { |
| 155 | mPanel.postMasterMuteChanged(flags); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void setLayoutDirection(int layoutDirection) |
| 160 | throws RemoteException { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 161 | mPanel.postLayoutDirection(layoutDirection); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void dismiss() throws RemoteException { |
John Spurlock | 1dad272 | 2014-07-11 11:07:53 -0400 | [diff] [blame^] | 166 | mPanel.postDismiss(0); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 167 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 168 | |
| 169 | @Override |
| 170 | public ZenModeController getZenController() { |
| 171 | return mDialogPanel.getZenController(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void setVolumePanel(VolumePanel panel) { |
| 176 | mPanel = panel == null ? mDialogPanel : panel; |
| 177 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 178 | } |
RoboErik | 19c9518 | 2014-06-23 15:38:48 -0700 | [diff] [blame] | 179 | |
| 180 | private final class RemoteVolumeController extends IRemoteVolumeController.Stub { |
| 181 | |
| 182 | @Override |
| 183 | public void remoteVolumeChanged(ISessionController binder, int flags) |
| 184 | throws RemoteException { |
| 185 | MediaController controller = MediaController.fromBinder(binder); |
| 186 | mPanel.postRemoteVolumeChanged(controller, flags); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void updateRemoteController(ISessionController session) throws RemoteException { |
| 191 | mPanel.postRemoteSliderVisibility(session != null); |
| 192 | // TODO stash default session in case the slider can be opened other |
| 193 | // than by remoteVolumeChanged. |
| 194 | } |
| 195 | } |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 196 | } |