VolumeUI.java revision 3346a802087f621c6441bc512dfcc17b07143fc6
1package com.android.systemui.volume;
2
3import android.content.Context;
4import android.database.ContentObserver;
5import android.media.AudioManager;
6import android.media.IVolumeController;
7import android.net.Uri;
8import android.os.Handler;
9import android.os.RemoteException;
10import android.provider.Settings;
11import android.util.Log;
12
13import com.android.systemui.SystemUI;
14
15/*
16 * Copyright (C) 2014 The Android Open Source Project
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 *      http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */
30
31public class VolumeUI extends SystemUI {
32    private static final String TAG = "VolumeUI";
33    private static final String SETTING = "systemui_volume_controller";  // for testing
34    private static final Uri SETTING_URI = Settings.Global.getUriFor(SETTING);
35    private static final int DEFAULT = 1;  // enabled by default
36
37    private AudioManager mAudioManager;
38    private VolumeController mVolumeController;
39
40    @Override
41    public void start() {
42        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
43        updateController();
44        mContext.getContentResolver().registerContentObserver(SETTING_URI, false, mObserver);
45    }
46
47    private void updateController() {
48        if (Settings.Global.getInt(mContext.getContentResolver(), SETTING, DEFAULT) != 0) {
49            if (mVolumeController == null) {
50                mVolumeController = new VolumeController(mContext);
51            }
52            Log.d(TAG, "Registering volume controller");
53            mAudioManager.setVolumeController(mVolumeController);
54        } else {
55            Log.d(TAG, "Unregistering volume controller");
56            mAudioManager.setVolumeController(null);
57        }
58    }
59
60    private final ContentObserver mObserver = new ContentObserver(new Handler()) {
61        public void onChange(boolean selfChange, Uri uri) {
62            if (SETTING_URI.equals(uri)) {
63                updateController();
64            }
65        }
66    };
67
68    /** For now, simply host an unmodified base volume panel in this process. */
69    private final class VolumeController extends IVolumeController.Stub {
70        private final VolumePanel mPanel;
71
72        public VolumeController(Context context) {
73            mPanel = new VolumePanel(context);
74        }
75
76        @Override
77        public void hasNewRemotePlaybackInfo() throws RemoteException {
78            mPanel.postHasNewRemotePlaybackInfo();
79        }
80
81        @Override
82        public void remoteVolumeChanged(int streamType, int flags)
83                throws RemoteException {
84            mPanel.postRemoteVolumeChanged(streamType, flags);
85        }
86
87        @Override
88        public void remoteSliderVisibility(boolean visible)
89                throws RemoteException {
90            mPanel.postRemoteSliderVisibility(visible);
91        }
92
93        @Override
94        public void displaySafeVolumeWarning(int flags) throws RemoteException {
95            mPanel.postDisplaySafeVolumeWarning(flags);
96        }
97
98        @Override
99        public void volumeChanged(int streamType, int flags)
100                throws RemoteException {
101            mPanel.postVolumeChanged(streamType, flags);
102        }
103
104        @Override
105        public void masterVolumeChanged(int flags) throws RemoteException {
106            mPanel.postMasterVolumeChanged(flags);
107        }
108
109        @Override
110        public void masterMuteChanged(int flags) throws RemoteException {
111            mPanel.postMasterMuteChanged(flags);
112        }
113
114        @Override
115        public void setLayoutDirection(int layoutDirection)
116                throws RemoteException {
117            mPanel.setLayoutDirection(layoutDirection);
118        }
119
120        @Override
121        public void dismiss() throws RemoteException {
122            mPanel.postDismiss();
123        }
124    }
125}
126