FileDocCategorySizeDatePackage
SeekBarVolumizer.javaAPI DocAndroid 5.1 API13473Thu Mar 12 22:22:10 GMT 2015android.preference

SeekBarVolumizer

public class SeekBarVolumizer extends Object implements android.widget.SeekBar.OnSeekBarChangeListener, Handler.Callback
Turns a {@link SeekBar} into a volume control.
hide

Fields Summary
private static final String
TAG
private final android.content.Context
mContext
private final H
mUiHandler
private final Callback
mCallback
private final android.net.Uri
mDefaultUri
private final android.media.AudioManager
mAudioManager
private final int
mStreamType
private final int
mMaxStreamVolume
private boolean
mAffectedByRingerMode
private boolean
mNotificationOrRing
private final Receiver
mReceiver
private android.os.Handler
mHandler
private Observer
mVolumeObserver
private int
mOriginalStreamVolume
private android.media.Ringtone
mRingtone
private int
mLastProgress
private boolean
mMuted
private android.widget.SeekBar
mSeekBar
private int
mVolumeBeforeMute
private int
mRingerMode
private static final int
MSG_SET_STREAM_VOLUME
private static final int
MSG_START_SAMPLE
private static final int
MSG_STOP_SAMPLE
private static final int
MSG_INIT_SAMPLE
private static final int
CHECK_RINGTONE_PLAYBACK_DELAY_MS
Constructors Summary
public SeekBarVolumizer(android.content.Context context, int streamType, android.net.Uri defaultUri, Callback callback)


             
        mContext = context;
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mStreamType = streamType;
        mAffectedByRingerMode = mAudioManager.isStreamAffectedByRingerMode(mStreamType);
        mNotificationOrRing = isNotificationOrRing(mStreamType);
        if (mNotificationOrRing) {
            mRingerMode = mAudioManager.getRingerModeInternal();
        }
        mMaxStreamVolume = mAudioManager.getStreamMaxVolume(mStreamType);
        mCallback = callback;
        mOriginalStreamVolume = mAudioManager.getStreamVolume(mStreamType);
        mMuted = mAudioManager.isStreamMute(mStreamType);
        if (mCallback != null) {
            mCallback.onMuted(mMuted);
        }
        if (defaultUri == null) {
            if (mStreamType == AudioManager.STREAM_RING) {
                defaultUri = Settings.System.DEFAULT_RINGTONE_URI;
            } else if (mStreamType == AudioManager.STREAM_NOTIFICATION) {
                defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
            } else {
                defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
            }
        }
        mDefaultUri = defaultUri;
    
Methods Summary
public voidchangeVolumeBy(int amount)

        mSeekBar.incrementProgressBy(amount);
        postSetVolume(mSeekBar.getProgress());
        postStartSample();
        mVolumeBeforeMute = -1;
    
public android.widget.SeekBargetSeekBar()

        return mSeekBar;
    
public booleanhandleMessage(android.os.Message msg)

        switch (msg.what) {
            case MSG_SET_STREAM_VOLUME:
                mAudioManager.setStreamVolume(mStreamType, mLastProgress,
                        AudioManager.FLAG_SHOW_UI_WARNINGS);
                break;
            case MSG_START_SAMPLE:
                onStartSample();
                break;
            case MSG_STOP_SAMPLE:
                onStopSample();
                break;
            case MSG_INIT_SAMPLE:
                onInitSample();
                break;
            default:
                Log.e(TAG, "invalid SeekBarVolumizer message: "+msg.what);
        }
        return true;
    
private static booleanisNotificationOrRing(int stream)

        return stream == AudioManager.STREAM_RING || stream == AudioManager.STREAM_NOTIFICATION;
    
public booleanisSamplePlaying()

        return mRingtone != null && mRingtone.isPlaying();
    
public voidmuteVolume()

        if (mVolumeBeforeMute != -1) {
            mSeekBar.setProgress(mVolumeBeforeMute);
            postSetVolume(mVolumeBeforeMute);
            postStartSample();
            mVolumeBeforeMute = -1;
        } else {
            mVolumeBeforeMute = mSeekBar.getProgress();
            mSeekBar.setProgress(0);
            postStopSample();
            postSetVolume(0);
        }
    
private voidonInitSample()

        mRingtone = RingtoneManager.getRingtone(mContext, mDefaultUri);
        if (mRingtone != null) {
            mRingtone.setStreamType(mStreamType);
        }
    
public voidonProgressChanged(android.widget.SeekBar seekBar, int progress, boolean fromTouch)

        if (fromTouch) {
            postSetVolume(progress);
        }
        if (mCallback != null) {
            mCallback.onProgressChanged(seekBar, progress, fromTouch);
        }
    
public voidonRestoreInstanceState(android.preference.VolumePreference.VolumeStore volumeStore)

        if (volumeStore.volume != -1) {
            mOriginalStreamVolume = volumeStore.originalVolume;
            mLastProgress = volumeStore.volume;
            postSetVolume(mLastProgress);
        }
    
public voidonSaveInstanceState(android.preference.VolumePreference.VolumeStore volumeStore)

        if (mLastProgress >= 0) {
            volumeStore.volume = mLastProgress;
            volumeStore.originalVolume = mOriginalStreamVolume;
        }
    
private voidonStartSample()

        if (!isSamplePlaying()) {
            if (mCallback != null) {
                mCallback.onSampleStarting(this);
            }
            if (mRingtone != null) {
                try {
                    mRingtone.play();
                } catch (Throwable e) {
                    Log.w(TAG, "Error playing ringtone, stream " + mStreamType, e);
                }
            }
        }
    
public voidonStartTrackingTouch(android.widget.SeekBar seekBar)

    
private voidonStopSample()

        if (mRingtone != null) {
            mRingtone.stop();
        }
    
public voidonStopTrackingTouch(android.widget.SeekBar seekBar)

        postStartSample();
    
private voidpostSetVolume(int progress)

        if (mHandler == null) return;
        // Do the volume changing separately to give responsive UI
        mLastProgress = progress;
        mHandler.removeMessages(MSG_SET_STREAM_VOLUME);
        mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_STREAM_VOLUME));
    
private voidpostStartSample()

        if (mHandler == null) return;
        mHandler.removeMessages(MSG_START_SAMPLE);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE),
                isSamplePlaying() ? CHECK_RINGTONE_PLAYBACK_DELAY_MS : 0);
    
private voidpostStopSample()

        if (mHandler == null) return;
        // remove pending delayed start messages
        mHandler.removeMessages(MSG_START_SAMPLE);
        mHandler.removeMessages(MSG_STOP_SAMPLE);
        mHandler.sendMessage(mHandler.obtainMessage(MSG_STOP_SAMPLE));
    
public voidrevertVolume()

        mAudioManager.setStreamVolume(mStreamType, mOriginalStreamVolume, 0);
    
public voidsetSeekBar(android.widget.SeekBar seekBar)

        if (mSeekBar != null) {
            mSeekBar.setOnSeekBarChangeListener(null);
        }
        mSeekBar = seekBar;
        mSeekBar.setOnSeekBarChangeListener(null);
        mSeekBar.setMax(mMaxStreamVolume);
        updateSeekBar();
        mSeekBar.setOnSeekBarChangeListener(this);
    
public voidstart()

        if (mHandler != null) return;  // already started
        HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler");
        thread.start();
        mHandler = new Handler(thread.getLooper(), this);
        mHandler.sendEmptyMessage(MSG_INIT_SAMPLE);
        mVolumeObserver = new Observer(mHandler);
        mContext.getContentResolver().registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS[mStreamType]),
                false, mVolumeObserver);
        mReceiver.setListening(true);
    
public voidstartSample()

        postStartSample();
    
public voidstop()

        if (mHandler == null) return;  // already stopped
        postStopSample();
        mContext.getContentResolver().unregisterContentObserver(mVolumeObserver);
        mReceiver.setListening(false);
        mSeekBar.setOnSeekBarChangeListener(null);
        mHandler.getLooper().quitSafely();
        mHandler = null;
        mVolumeObserver = null;
    
public voidstopSample()

        postStopSample();
    
protected voidupdateSeekBar()

        if (mNotificationOrRing && mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
            mSeekBar.setEnabled(true);
            mSeekBar.setProgress(0);
        } else if (mMuted) {
            mSeekBar.setEnabled(false);
            mSeekBar.setProgress(0);
        } else {
            mSeekBar.setEnabled(true);
            mSeekBar.setProgress(mLastProgress > -1 ? mLastProgress : mOriginalStreamVolume);
        }
    
private voidupdateSlider()

        if (mSeekBar != null && mAudioManager != null) {
            final int volume = mAudioManager.getStreamVolume(mStreamType);
            final boolean mute = mAudioManager.isStreamMute(mStreamType);
            mUiHandler.postUpdateSlider(volume, mute);
        }