Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 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 android.view;
     18 
     19 import com.android.internal.R;
     20 
     21 import android.app.Dialog;
     22 import android.content.DialogInterface.OnDismissListener;
     23 import android.content.BroadcastReceiver;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.content.res.Resources;
     29 import android.media.AudioManager;
     30 import android.media.AudioService;
     31 import android.media.AudioSystem;
     32 import android.media.RingtoneManager;
     33 import android.media.ToneGenerator;
     34 import android.net.Uri;
     35 import android.os.Handler;
     36 import android.os.Message;
     37 import android.os.RemoteException;
     38 import android.os.Vibrator;
     39 import android.provider.Settings;
     40 import android.provider.Settings.System;
     41 import android.util.Log;
     42 import android.view.WindowManager.LayoutParams;
     43 import android.widget.ImageView;
     44 import android.widget.SeekBar;
     45 import android.widget.SeekBar.OnSeekBarChangeListener;
     46 
     47 import java.util.HashMap;
     48 
     49 /**
     50  * Handle the volume up and down keys.
     51  *
     52  * This code really should be moved elsewhere.
     53  *
     54  * Seriously, it really really should be moved elsewhere.  This is used by
     55  * android.media.AudioService, which actually runs in the system process, to
     56  * show the volume dialog when the user changes the volume.  What a mess.
     57  *
     58  * @hide
     59  */
     60 public class VolumePanel extends Handler implements OnSeekBarChangeListener, View.OnClickListener
     61 {
     62     private static final String TAG = "VolumePanel";
     63     private static boolean LOGD = false;
     64 
     65     /**
     66      * The delay before playing a sound. This small period exists so the user
     67      * can press another key (non-volume keys, too) to have it NOT be audible.
     68      * <p>
     69      * PhoneWindow will implement this part.
     70      */
     71     public static final int PLAY_SOUND_DELAY = 300;
     72 
     73     /**
     74      * The delay before vibrating. This small period exists so if the user is
     75      * moving to silent mode, it will not emit a short vibrate (it normally
     76      * would since vibrate is between normal mode and silent mode using hardware
     77      * keys).
     78      */
     79     public static final int VIBRATE_DELAY = 300;
     80 
     81     private static final int VIBRATE_DURATION = 300;
     82     private static final int BEEP_DURATION = 150;
     83     private static final int MAX_VOLUME = 100;
     84     private static final int FREE_DELAY = 10000;
     85     private static final int TIMEOUT_DELAY = 3000;
     86 
     87     private static final int MSG_VOLUME_CHANGED = 0;
     88     private static final int MSG_FREE_RESOURCES = 1;
     89     private static final int MSG_PLAY_SOUND = 2;
     90     private static final int MSG_STOP_SOUNDS = 3;
     91     private static final int MSG_VIBRATE = 4;
     92     private static final int MSG_TIMEOUT = 5;
     93     private static final int MSG_RINGER_MODE_CHANGED = 6;
     94 
     95     protected Context mContext;
     96     private AudioManager mAudioManager;
     97     protected AudioService mAudioService;
     98     private boolean mRingIsSilent;
     99     private boolean mShowCombinedVolumes;
    100     private boolean mVoiceCapable;
    101 
    102     /** Dialog containing all the sliders */
    103     private final Dialog mDialog;
    104     /** Dialog's content view */
    105     private final View mView;
    106 
    107     /** The visible portion of the volume overlay */
    108     private final ViewGroup mPanel;
    109     /** Contains the sliders and their touchable icons */
    110     private final ViewGroup mSliderGroup;
    111     /** The button that expands the dialog to show all sliders */
    112     private final View mMoreButton;
    113     /** Dummy divider icon that needs to vanish with the more button */
    114     private final View mDivider;
    115 
    116     /** Currently active stream that shows up at the top of the list of sliders */
    117     private int mActiveStreamType = -1;
    118     /** All the slider controls mapped by stream type */
    119     private HashMap<Integer,StreamControl> mStreamControls;
    120 
    121     private enum StreamResources {
    122         BluetoothSCOStream(AudioManager.STREAM_BLUETOOTH_SCO,
    123                 R.string.volume_icon_description_bluetooth,
    124                 R.drawable.ic_audio_bt,
    125                 R.drawable.ic_audio_bt,
    126                 false),
    127         RingerStream(AudioManager.STREAM_RING,
    128                 R.string.volume_icon_description_ringer,
    129                 R.drawable.ic_audio_ring_notif,
    130                 R.drawable.ic_audio_ring_notif_mute,
    131                 false),
    132         VoiceStream(AudioManager.STREAM_VOICE_CALL,
    133                 R.string.volume_icon_description_incall,
    134                 R.drawable.ic_audio_phone,
    135                 R.drawable.ic_audio_phone,
    136                 false),
    137         AlarmStream(AudioManager.STREAM_ALARM,
    138                 R.string.volume_alarm,
    139                 R.drawable.ic_audio_alarm,
    140                 R.drawable.ic_audio_alarm_mute,
    141                 false),
    142         MediaStream(AudioManager.STREAM_MUSIC,
    143                 R.string.volume_icon_description_media,
    144                 R.drawable.ic_audio_vol,
    145                 R.drawable.ic_audio_vol_mute,
    146                 true),
    147         NotificationStream(AudioManager.STREAM_NOTIFICATION,
    148                 R.string.volume_icon_description_notification,
    149                 R.drawable.ic_audio_notification,
    150                 R.drawable.ic_audio_notification_mute,
    151                 true);
    152 
    153         int streamType;
    154         int descRes;
    155         int iconRes;
    156         int iconMuteRes;
    157         // RING, VOICE_CALL & BLUETOOTH_SCO are hidden unless explicitly requested
    158         boolean show;
    159 
    160         StreamResources(int streamType, int descRes, int iconRes, int iconMuteRes, boolean show) {
    161             this.streamType = streamType;
    162             this.descRes = descRes;
    163             this.iconRes = iconRes;
    164             this.iconMuteRes = iconMuteRes;
    165             this.show = show;
    166         }
    167     };
    168 
    169     // List of stream types and their order
    170     private static final StreamResources[] STREAMS = {
    171         StreamResources.BluetoothSCOStream,
    172         StreamResources.RingerStream,
    173         StreamResources.VoiceStream,
    174         StreamResources.MediaStream,
    175         StreamResources.NotificationStream,
    176         StreamResources.AlarmStream
    177     };
    178 
    179     /** Object that contains data for each slider */
    180     private class StreamControl {
    181         int streamType;
    182         ViewGroup group;
    183         ImageView icon;
    184         SeekBar seekbarView;
    185         int iconRes;
    186         int iconMuteRes;
    187     }
    188 
    189     // Synchronize when accessing this
    190     private ToneGenerator mToneGenerators[];
    191     private Vibrator mVibrator;
    192 
    193     public VolumePanel(final Context context, AudioService volumeService) {
    194         mContext = context;
    195         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    196         mAudioService = volumeService;
    197 
    198         LayoutInflater inflater = (LayoutInflater) context
    199                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    200         View view = mView = inflater.inflate(R.layout.volume_adjust, null);
    201         mView.setOnTouchListener(new View.OnTouchListener() {
    202             public boolean onTouch(View v, MotionEvent event) {
    203                 resetTimeout();
    204                 return false;
    205             }
    206         });
    207         mPanel = (ViewGroup) mView.findViewById(R.id.visible_panel);
    208         mSliderGroup = (ViewGroup) mView.findViewById(R.id.slider_group);
    209         mMoreButton = (ImageView) mView.findViewById(R.id.expand_button);
    210         mDivider = (ImageView) mView.findViewById(R.id.expand_button_divider);
    211 
    212         mDialog = new Dialog(context, R.style.Theme_Panel_Volume) {
    213             public boolean onTouchEvent(MotionEvent event) {
    214                 if (isShowing() && event.getAction() == MotionEvent.ACTION_OUTSIDE) {
    215                     forceTimeout();
    216                     return true;
    217                 }
    218                 return false;
    219             }
    220         };
    221         mDialog.setTitle("Volume control"); // No need to localize
    222         mDialog.setContentView(mView);
    223         mDialog.setOnDismissListener(new OnDismissListener() {
    224             public void onDismiss(DialogInterface dialog) {
    225                 mActiveStreamType = -1;
    226                 mAudioManager.forceVolumeControlStream(mActiveStreamType);
    227             }
    228         });
    229         // Change some window properties
    230         Window window = mDialog.getWindow();
    231         window.setGravity(Gravity.TOP);
    232         LayoutParams lp = window.getAttributes();
    233         lp.token = null;
    234         // Offset from the top
    235         lp.y = mContext.getResources().getDimensionPixelOffset(
    236                 com.android.internal.R.dimen.volume_panel_top);
    237         lp.type = LayoutParams.TYPE_VOLUME_OVERLAY;
    238         lp.width = LayoutParams.WRAP_CONTENT;
    239         lp.height = LayoutParams.WRAP_CONTENT;
    240         window.setAttributes(lp);
    241         window.addFlags(LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL
    242                 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    243 
    244         mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
    245         mVibrator = new Vibrator();
    246 
    247         mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
    248         mShowCombinedVolumes = !mVoiceCapable;
    249         // If we don't want to show multiple volumes, hide the settings button and divider
    250         if (!mShowCombinedVolumes) {
    251             mMoreButton.setVisibility(View.GONE);
    252             mDivider.setVisibility(View.GONE);
    253         } else {
    254             mMoreButton.setOnClickListener(this);
    255         }
    256 
    257         listenToRingerMode();
    258     }
    259 
    260     private void listenToRingerMode() {
    261         final IntentFilter filter = new IntentFilter();
    262         filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
    263         mContext.registerReceiver(new BroadcastReceiver() {
    264 
    265             public void onReceive(Context context, Intent intent) {
    266                 final String action = intent.getAction();
    267 
    268                 if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
    269                     removeMessages(MSG_RINGER_MODE_CHANGED);
    270                     sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
    271                 }
    272             }
    273         }, filter);
    274     }
    275 
    276     private boolean isMuted(int streamType) {
    277         return mAudioManager.isStreamMute(streamType);
    278     }
    279 
    280     private void createSliders() {
    281         LayoutInflater inflater = (LayoutInflater) mContext
    282                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    283         mStreamControls = new HashMap<Integer, StreamControl>(STREAMS.length);
    284         Resources res = mContext.getResources();
    285         for (int i = 0; i < STREAMS.length; i++) {
    286             StreamResources streamRes = STREAMS[i];
    287             int streamType = streamRes.streamType;
    288             if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
    289                 streamRes = StreamResources.RingerStream;
    290             }
    291             StreamControl sc = new StreamControl();
    292             sc.streamType = streamType;
    293             sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
    294             sc.group.setTag(sc);
    295             sc.icon = (ImageView) sc.group.findViewById(R.id.stream_icon);
    296             sc.icon.setTag(sc);
    297             sc.icon.setContentDescription(res.getString(streamRes.descRes));
    298             sc.iconRes = streamRes.iconRes;
    299             sc.iconMuteRes = streamRes.iconMuteRes;
    300             sc.icon.setImageResource(sc.iconRes);
    301             sc.seekbarView = (SeekBar) sc.group.findViewById(R.id.seekbar);
    302             int plusOne = (streamType == AudioSystem.STREAM_BLUETOOTH_SCO ||
    303                     streamType == AudioSystem.STREAM_VOICE_CALL) ? 1 : 0;
    304             sc.seekbarView.setMax(mAudioManager.getStreamMaxVolume(streamType) + plusOne);
    305             sc.seekbarView.setOnSeekBarChangeListener(this);
    306             sc.seekbarView.setTag(sc);
    307             mStreamControls.put(streamType, sc);
    308         }
    309     }
    310 
    311     private void reorderSliders(int activeStreamType) {
    312         mSliderGroup.removeAllViews();
    313 
    314         StreamControl active = mStreamControls.get(activeStreamType);
    315         if (active == null) {
    316             Log.e("VolumePanel", "Missing stream type! - " + activeStreamType);
    317             mActiveStreamType = -1;
    318         } else {
    319             mSliderGroup.addView(active.group);
    320             mActiveStreamType = activeStreamType;
    321             active.group.setVisibility(View.VISIBLE);
    322             updateSlider(active);
    323         }
    324 
    325         addOtherVolumes();
    326     }
    327 
    328     private void addOtherVolumes() {
    329         if (!mShowCombinedVolumes) return;
    330 
    331         for (int i = 0; i < STREAMS.length; i++) {
    332             // Skip the phone specific ones and the active one
    333             final int streamType = STREAMS[i].streamType;
    334             if (!STREAMS[i].show || streamType == mActiveStreamType) {
    335                 continue;
    336             }
    337             StreamControl sc = mStreamControls.get(streamType);
    338             mSliderGroup.addView(sc.group);
    339             updateSlider(sc);
    340         }
    341     }
    342 
    343     /** Update the mute and progress state of a slider */
    344     private void updateSlider(StreamControl sc) {
    345         sc.seekbarView.setProgress(mAudioManager.getLastAudibleStreamVolume(sc.streamType));
    346         final boolean muted = isMuted(sc.streamType);
    347         sc.icon.setImageResource(muted ? sc.iconMuteRes : sc.iconRes);
    348         if (sc.streamType == AudioManager.STREAM_RING && muted
    349                 && mAudioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
    350             sc.icon.setImageResource(R.drawable.ic_audio_ring_notif_vibrate);
    351         }
    352     }
    353 
    354     private boolean isExpanded() {
    355         return mMoreButton.getVisibility() != View.VISIBLE;
    356     }
    357 
    358     private void expand() {
    359         final int count = mSliderGroup.getChildCount();
    360         for (int i = 0; i < count; i++) {
    361             mSliderGroup.getChildAt(i).setVisibility(View.VISIBLE);
    362         }
    363         mMoreButton.setVisibility(View.INVISIBLE);
    364         mDivider.setVisibility(View.INVISIBLE);
    365     }
    366 
    367     private void collapse() {
    368         mMoreButton.setVisibility(View.VISIBLE);
    369         mDivider.setVisibility(View.VISIBLE);
    370         final int count = mSliderGroup.getChildCount();
    371         for (int i = 1; i < count; i++) {
    372             mSliderGroup.getChildAt(i).setVisibility(View.GONE);
    373         }
    374     }
    375 
    376     private void updateStates() {
    377         final int count = mSliderGroup.getChildCount();
    378         for (int i = 0; i < count; i++) {
    379             StreamControl sc = (StreamControl) mSliderGroup.getChildAt(i).getTag();
    380             updateSlider(sc);
    381         }
    382     }
    383 
    384     public void postVolumeChanged(int streamType, int flags) {
    385         if (hasMessages(MSG_VOLUME_CHANGED)) return;
    386         if (mStreamControls == null) {
    387             createSliders();
    388         }
    389         removeMessages(MSG_FREE_RESOURCES);
    390         obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
    391     }
    392 
    393     /**
    394      * Override this if you have other work to do when the volume changes (for
    395      * example, vibrating, playing a sound, etc.). Make sure to call through to
    396      * the superclass implementation.
    397      */
    398     protected void onVolumeChanged(int streamType, int flags) {
    399 
    400         if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")");
    401 
    402         if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
    403             if (mActiveStreamType == -1) {
    404                 reorderSliders(streamType);
    405             }
    406             onShowVolumeChanged(streamType, flags);
    407         }
    408 
    409         if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
    410             removeMessages(MSG_PLAY_SOUND);
    411             sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
    412         }
    413 
    414         if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
    415             removeMessages(MSG_PLAY_SOUND);
    416             removeMessages(MSG_VIBRATE);
    417             onStopSounds();
    418         }
    419 
    420         removeMessages(MSG_FREE_RESOURCES);
    421         sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
    422 
    423         resetTimeout();
    424     }
    425 
    426     protected void onShowVolumeChanged(int streamType, int flags) {
    427         int index = mAudioService.isStreamMute(streamType) ?
    428                 mAudioService.getLastAudibleStreamVolume(streamType)
    429                 : mAudioService.getStreamVolume(streamType);
    430 
    431         mRingIsSilent = false;
    432 
    433         if (LOGD) {
    434             Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType
    435                     + ", flags: " + flags + "), index: " + index);
    436         }
    437 
    438         // get max volume for progress bar
    439 
    440         int max = mAudioService.getStreamMaxVolume(streamType);
    441 
    442         switch (streamType) {
    443 
    444             case AudioManager.STREAM_RING: {
    445 //                setRingerIcon();
    446                 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
    447                         mContext, RingtoneManager.TYPE_RINGTONE);
    448                 if (ringuri == null) {
    449                     mRingIsSilent = true;
    450                 }
    451                 break;
    452             }
    453 
    454             case AudioManager.STREAM_MUSIC: {
    455                 // Special case for when Bluetooth is active for music
    456                 if ((mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC) &
    457                         (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
    458                         AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
    459                         AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) {
    460                     setMusicIcon(R.drawable.ic_audio_bt, R.drawable.ic_audio_bt_mute);
    461                 } else {
    462                     setMusicIcon(R.drawable.ic_audio_vol, R.drawable.ic_audio_vol_mute);
    463                 }
    464                 break;
    465             }
    466 
    467             case AudioManager.STREAM_VOICE_CALL: {
    468                 /*
    469                  * For in-call voice call volume, there is no inaudible volume.
    470                  * Rescale the UI control so the progress bar doesn't go all
    471                  * the way to zero and don't show the mute icon.
    472                  */
    473                 index++;
    474                 max++;
    475                 break;
    476             }
    477 
    478             case AudioManager.STREAM_ALARM: {
    479                 break;
    480             }
    481 
    482             case AudioManager.STREAM_NOTIFICATION: {
    483                 Uri ringuri = RingtoneManager.getActualDefaultRingtoneUri(
    484                         mContext, RingtoneManager.TYPE_NOTIFICATION);
    485                 if (ringuri == null) {
    486                     mRingIsSilent = true;
    487                 }
    488                 break;
    489             }
    490 
    491             case AudioManager.STREAM_BLUETOOTH_SCO: {
    492                 /*
    493                  * For in-call voice call volume, there is no inaudible volume.
    494                  * Rescale the UI control so the progress bar doesn't go all
    495                  * the way to zero and don't show the mute icon.
    496                  */
    497                 index++;
    498                 max++;
    499                 break;
    500             }
    501         }
    502 
    503         StreamControl sc = mStreamControls.get(streamType);
    504         if (sc != null) {
    505             if (sc.seekbarView.getMax() != max) {
    506                 sc.seekbarView.setMax(max);
    507             }
    508             sc.seekbarView.setProgress(index);
    509         }
    510 
    511         if (!mDialog.isShowing()) {
    512             mAudioManager.forceVolumeControlStream(streamType);
    513             mDialog.setContentView(mView);
    514             // Showing dialog - use collapsed state
    515             if (mShowCombinedVolumes) {
    516                 collapse();
    517             }
    518             mDialog.show();
    519         }
    520 
    521         // Do a little vibrate if applicable (only when going into vibrate mode)
    522         if ((flags & AudioManager.FLAG_VIBRATE) != 0 &&
    523                 mAudioService.isStreamAffectedByRingerMode(streamType) &&
    524                 mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
    525                 mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
    526             sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
    527         }
    528     }
    529 
    530     protected void onPlaySound(int streamType, int flags) {
    531 
    532         if (hasMessages(MSG_STOP_SOUNDS)) {
    533             removeMessages(MSG_STOP_SOUNDS);
    534             // Force stop right now
    535             onStopSounds();
    536         }
    537 
    538         synchronized (this) {
    539             ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
    540             if (toneGen != null) {
    541                 toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
    542                 sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
    543             }
    544         }
    545     }
    546 
    547     protected void onStopSounds() {
    548 
    549         synchronized (this) {
    550             int numStreamTypes = AudioSystem.getNumStreamTypes();
    551             for (int i = numStreamTypes - 1; i >= 0; i--) {
    552                 ToneGenerator toneGen = mToneGenerators[i];
    553                 if (toneGen != null) {
    554                     toneGen.stopTone();
    555                 }
    556             }
    557         }
    558     }
    559 
    560     protected void onVibrate() {
    561 
    562         // Make sure we ended up in vibrate ringer mode
    563         if (mAudioService.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
    564             return;
    565         }
    566 
    567         mVibrator.vibrate(VIBRATE_DURATION);
    568     }
    569 
    570     /**
    571      * Lock on this VolumePanel instance as long as you use the returned ToneGenerator.
    572      */
    573     private ToneGenerator getOrCreateToneGenerator(int streamType) {
    574         synchronized (this) {
    575             if (mToneGenerators[streamType] == null) {
    576                 try {
    577                     mToneGenerators[streamType] = new ToneGenerator(streamType, MAX_VOLUME);
    578                 } catch (RuntimeException e) {
    579                     if (LOGD) {
    580                         Log.d(TAG, "ToneGenerator constructor failed with "
    581                                 + "RuntimeException: " + e);
    582                     }
    583                 }
    584             }
    585             return mToneGenerators[streamType];
    586         }
    587     }
    588 
    589 
    590     /**
    591      * Switch between icons because Bluetooth music is same as music volume, but with
    592      * different icons.
    593      */
    594     private void setMusicIcon(int resId, int resMuteId) {
    595         StreamControl sc = mStreamControls.get(AudioManager.STREAM_MUSIC);
    596         if (sc != null) {
    597             sc.iconRes = resId;
    598             sc.iconMuteRes = resMuteId;
    599             sc.icon.setImageResource(isMuted(sc.streamType) ? sc.iconMuteRes : sc.iconRes);
    600         }
    601     }
    602 
    603     protected void onFreeResources() {
    604         synchronized (this) {
    605             for (int i = mToneGenerators.length - 1; i >= 0; i--) {
    606                 if (mToneGenerators[i] != null) {
    607                     mToneGenerators[i].release();
    608                 }
    609                 mToneGenerators[i] = null;
    610             }
    611         }
    612     }
    613 
    614     @Override
    615     public void handleMessage(Message msg) {
    616         switch (msg.what) {
    617 
    618             case MSG_VOLUME_CHANGED: {
    619                 onVolumeChanged(msg.arg1, msg.arg2);
    620                 break;
    621             }
    622 
    623             case MSG_FREE_RESOURCES: {
    624                 onFreeResources();
    625                 break;
    626             }
    627 
    628             case MSG_STOP_SOUNDS: {
    629                 onStopSounds();
    630                 break;
    631             }
    632 
    633             case MSG_PLAY_SOUND: {
    634                 onPlaySound(msg.arg1, msg.arg2);
    635                 break;
    636             }
    637 
    638             case MSG_VIBRATE: {
    639                 onVibrate();
    640                 break;
    641             }
    642 
    643             case MSG_TIMEOUT: {
    644                 if (mDialog.isShowing()) {
    645                     mDialog.dismiss();
    646                     mActiveStreamType = -1;
    647                 }
    648                 break;
    649             }
    650             case MSG_RINGER_MODE_CHANGED: {
    651                 if (mDialog.isShowing()) {
    652                     updateStates();
    653                 }
    654                 break;
    655             }
    656         }
    657     }
    658 
    659     private void resetTimeout() {
    660         removeMessages(MSG_TIMEOUT);
    661         sendMessageDelayed(obtainMessage(MSG_TIMEOUT), TIMEOUT_DELAY);
    662     }
    663 
    664     private void forceTimeout() {
    665         removeMessages(MSG_TIMEOUT);
    666         sendMessage(obtainMessage(MSG_TIMEOUT));
    667     }
    668 
    669     public void onProgressChanged(SeekBar seekBar, int progress,
    670             boolean fromUser) {
    671         final Object tag = seekBar.getTag();
    672         if (fromUser && tag instanceof StreamControl) {
    673             StreamControl sc = (StreamControl) tag;
    674             if (mAudioManager.getStreamVolume(sc.streamType) != progress) {
    675                 mAudioManager.setStreamVolume(sc.streamType, progress, 0);
    676             }
    677         }
    678         resetTimeout();
    679     }
    680 
    681     public void onStartTrackingTouch(SeekBar seekBar) {
    682     }
    683 
    684     public void onStopTrackingTouch(SeekBar seekBar) {
    685     }
    686 
    687     public void onClick(View v) {
    688         if (v == mMoreButton) {
    689             expand();
    690         }
    691         resetTimeout();
    692     }
    693 }
    694