blob: 70f9ac88d64bfe0d6555495bbf652b7cbd387a8e [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
Michael Wright0087a142013-02-05 16:29:39 -080030import com.android.systemui.settings.ToggleSlider;
31
Joe Onorato1e28f412010-12-01 09:33:36 -080032public class VolumeController implements ToggleSlider.Listener {
33 private static final String TAG = "StatusBar.VolumeController";
34 private static final int STREAM = AudioManager.STREAM_NOTIFICATION;
35
36 private Context mContext;
37 private ToggleSlider mControl;
38 private AudioManager mAudioManager;
39
40 private boolean mMute;
41 private int mVolume;
Eric Laurentbffc3d12012-05-07 17:43:49 -070042 // Is there a vibrator
43 private final boolean mHasVibrator;
Joe Onorato1e28f412010-12-01 09:33:36 -080044
45 public VolumeController(Context context, ToggleSlider control) {
46 mContext = context;
47 mControl = control;
Eric Laurentbffc3d12012-05-07 17:43:49 -070048
49 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
50 mHasVibrator = vibrator == null ? false : vibrator.hasVibrator();
51
Joe Onorato1e28f412010-12-01 09:33:36 -080052 mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
53
54 mMute = mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
55 mVolume = mAudioManager.getStreamVolume(STREAM);
John Spurlock48f37ec2012-10-05 16:32:51 -040056
57 control.setOnChangedListener(this);
58 }
59
60 @Override
61 public void onInit(ToggleSlider control) {
Joe Onorato1e28f412010-12-01 09:33:36 -080062 control.setMax(mAudioManager.getStreamMaxVolume(STREAM));
63 control.setValue(mVolume);
64 control.setChecked(mMute);
Joe Onorato1e28f412010-12-01 09:33:36 -080065 }
66
67 public void onChanged(ToggleSlider view, boolean tracking, boolean mute, int level) {
68 if (!tracking) {
69 if (mute) {
Joe Onorato1e28f412010-12-01 09:33:36 -080070 mAudioManager.setRingerMode(
Eric Laurentbffc3d12012-05-07 17:43:49 -070071 mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE
Joe Onorato1e28f412010-12-01 09:33:36 -080072 : AudioManager.RINGER_MODE_SILENT);
73 } else {
74 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
75 mAudioManager.setStreamVolume(STREAM, level, AudioManager.FLAG_PLAY_SOUND);
76 }
77 }
78 }
79}