blob: 6fee4323dc050cac9745146b48aa277364d4736b [file] [log] [blame]
Joe Onorato1e28f412010-12-01 09:33:36 -08001/*
2 * Copyright (C) 2010 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
17package com.android.systemui.statusbar.policy;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.os.RemoteException;
22import android.os.ServiceManager;
Eric Laurentbffc3d12012-05-07 17:43:49 -070023import android.os.Vibrator;
Joe Onorato1e28f412010-12-01 09:33:36 -080024import android.media.AudioManager;
25import android.provider.Settings;
26import android.util.Slog;
27import android.view.IWindowManager;
28import android.widget.CompoundButton;
29
30public class VolumeController implements ToggleSlider.Listener {
31 private static final String TAG = "StatusBar.VolumeController";
32 private static final int STREAM = AudioManager.STREAM_NOTIFICATION;
33
34 private Context mContext;
35 private ToggleSlider mControl;
36 private AudioManager mAudioManager;
37
38 private boolean mMute;
39 private int mVolume;
Eric Laurentbffc3d12012-05-07 17:43:49 -070040 // Is there a vibrator
41 private final boolean mHasVibrator;
Joe Onorato1e28f412010-12-01 09:33:36 -080042
43 public VolumeController(Context context, ToggleSlider control) {
44 mContext = context;
45 mControl = control;
Eric Laurentbffc3d12012-05-07 17:43:49 -070046
47 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
48 mHasVibrator = vibrator == null ? false : vibrator.hasVibrator();
49
Joe Onorato1e28f412010-12-01 09:33:36 -080050 mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
51
52 mMute = mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
53 mVolume = mAudioManager.getStreamVolume(STREAM);
John Spurlock48f37ec2012-10-05 16:32:51 -040054
55 control.setOnChangedListener(this);
56 }
57
58 @Override
59 public void onInit(ToggleSlider control) {
Joe Onorato1e28f412010-12-01 09:33:36 -080060 control.setMax(mAudioManager.getStreamMaxVolume(STREAM));
61 control.setValue(mVolume);
62 control.setChecked(mMute);
Joe Onorato1e28f412010-12-01 09:33:36 -080063 }
64
65 public void onChanged(ToggleSlider view, boolean tracking, boolean mute, int level) {
66 if (!tracking) {
67 if (mute) {
Joe Onorato1e28f412010-12-01 09:33:36 -080068 mAudioManager.setRingerMode(
Eric Laurentbffc3d12012-05-07 17:43:49 -070069 mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE
Joe Onorato1e28f412010-12-01 09:33:36 -080070 : AudioManager.RINGER_MODE_SILENT);
71 } else {
72 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
73 mAudioManager.setStreamVolume(STREAM, level, AudioManager.FLAG_PLAY_SOUND);
74 }
75 }
76 }
77}