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 17 package com.android.systemui.statusbar.policy; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.os.RemoteException; 22 import android.os.ServiceManager; 23 import android.os.Vibrator; 24 import android.media.AudioManager; 25 import android.provider.Settings; 26 import android.util.Slog; 27 import android.view.IWindowManager; 28 import android.widget.CompoundButton; 29 30 import com.android.systemui.settings.ToggleSlider; 31 32 public 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; 42 // Is there a vibrator 43 private final boolean mHasVibrator; 44 45 public VolumeController(Context context, ToggleSlider control) { 46 mContext = context; 47 mControl = control; 48 49 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 50 mHasVibrator = vibrator == null ? false : vibrator.hasVibrator(); 51 52 mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); 53 54 mMute = mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; 55 mVolume = mAudioManager.getStreamVolume(STREAM); 56 57 control.setOnChangedListener(this); 58 } 59 60 @Override 61 public void onInit(ToggleSlider control) { 62 control.setMax(mAudioManager.getStreamMaxVolume(STREAM)); 63 control.setValue(mVolume); 64 control.setChecked(mMute); 65 } 66 67 public void onChanged(ToggleSlider view, boolean tracking, boolean mute, int level) { 68 if (!tracking) { 69 if (mute) { 70 mAudioManager.setRingerMode( 71 mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE 72 : 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 } 80