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