1/*
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;
23import android.os.Vibrator;
24import 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;
40    // Is there a vibrator
41    private final boolean mHasVibrator;
42
43    public VolumeController(Context context, ToggleSlider control) {
44        mContext = context;
45        mControl = control;
46
47        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
48        mHasVibrator = vibrator == null ? false : vibrator.hasVibrator();
49
50        mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
51
52        mMute = mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
53        mVolume = mAudioManager.getStreamVolume(STREAM);
54
55        control.setOnChangedListener(this);
56    }
57
58    @Override
59    public void onInit(ToggleSlider control) {
60        control.setMax(mAudioManager.getStreamMaxVolume(STREAM));
61        control.setValue(mVolume);
62        control.setChecked(mMute);
63    }
64
65    public void onChanged(ToggleSlider view, boolean tracking, boolean mute, int level) {
66        if (!tracking) {
67            if (mute) {
68                mAudioManager.setRingerMode(
69                        mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE
70                                     : 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}
78