Home | History | Annotate | Download | only in policy
      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 public 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         control.setMax(mAudioManager.getStreamMaxVolume(STREAM));
     55         control.setValue(mVolume);
     56         control.setChecked(mMute);
     57 
     58         control.setOnChangedListener(this);
     59     }
     60 
     61     public void onChanged(ToggleSlider view, boolean tracking, boolean mute, int level) {
     62         if (!tracking) {
     63             if (mute) {
     64                 mAudioManager.setRingerMode(
     65                         mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE
     66                                      : AudioManager.RINGER_MODE_SILENT);
     67             } else {
     68                 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
     69                 mAudioManager.setStreamVolume(STREAM, level, AudioManager.FLAG_PLAY_SOUND);
     70             }
     71         }
     72     }
     73 }
     74