Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fd79c16 authored by Phil Tunstall's avatar Phil Tunstall
Browse files

Framework: Volume panel cleanup (1/2)

- Hide notifications volume slider when linked with ringtone volume.
- Default to expandable panel style. This was made default on the settings side
in commit Ibd25bd1f058d49ab0560ed073c3b45782d165cbb
- Use a ContentObserver to monitor system settings.
- Moved static final variables out of Settings.java.
- Cleanup of a couple of untidy bits from initial jellybean merge.

Patch Set 2: Fix whitespace.

Change-Id: I738da63a4d59ad5ee52b1aa9e0e4f4c9c93a7576
parent 202b17be
Loading
Loading
Loading
Loading
+5 −11
Original line number Original line Diff line number Diff line
@@ -1583,21 +1583,15 @@ public final class Settings {




        /**
        /**
         * Volume Overlay Mode, This is behaviour of the volume overlay panel
         * Volume Overlay Mode. This is the style of the volume overlay panel.
         * Defaults to 0 - which is simple
         *      0 - Single
         *      1 - Expandable (default)
         *      2 - Expanded
         *      3 - None
         * @hide
         * @hide
         */
         */
        public static final String MODE_VOLUME_OVERLAY = "mode_volume_overlay";
        public static final String MODE_VOLUME_OVERLAY = "mode_volume_overlay";


        /** @hide */
        public static final int VOLUME_OVERLAY_SINGLE = 0;
        /** @hide */
        public static final int VOLUME_OVERLAY_EXPANDABLE = 1;
        /** @hide */
        public static final int VOLUME_OVERLAY_EXPANDED = 2;
        /** @hide */
        public static final int VOLUME_OVERLAY_NONE = 3;

        /**
        /**
         * Ringer mode. This is used internally, changing this value will not
         * Ringer mode. This is used internally, changing this value will not
         * change the ringer mode. See AudioManager.
         * change the ringer mode. See AudioManager.
+60 −82
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.media.AudioManager;
import android.media.AudioService;
import android.media.AudioService;
import android.media.AudioSystem;
import android.media.AudioSystem;
@@ -34,10 +35,8 @@ import android.media.ToneGenerator;
import android.net.Uri;
import android.net.Uri;
import android.os.Handler;
import android.os.Handler;
import android.os.Message;
import android.os.Message;
import android.os.RemoteException;
import android.os.Vibrator;
import android.os.Vibrator;
import android.provider.Settings;
import android.provider.Settings;
import android.provider.Settings.System;
import android.util.Log;
import android.util.Log;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView;
@@ -100,8 +99,10 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private static final int STREAM_MASTER = -100;
    private static final int STREAM_MASTER = -100;
    // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC
    // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC


    public static final String ACTION_VOLUME_OVERLAY_CHANGED
    public static final int VOLUME_OVERLAY_SINGLE = 0;
        = "android.intent.action.VOLUME_OVERLAY_CHANGED";
    public static final int VOLUME_OVERLAY_EXPANDABLE = 1;
    public static final int VOLUME_OVERLAY_EXPANDED = 2;
    public static final int VOLUME_OVERLAY_NONE = 3;


    protected Context mContext;
    protected Context mContext;
    private AudioManager mAudioManager;
    private AudioManager mAudioManager;
@@ -109,6 +110,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private boolean mRingIsSilent;
    private boolean mRingIsSilent;
    private boolean mShowCombinedVolumes;
    private boolean mShowCombinedVolumes;
    private boolean mVoiceCapable;
    private boolean mVoiceCapable;
    private boolean mVolumeLinkNotification;
    private int mCurrentOverlayStyle = -1;
    private int mCurrentOverlayStyle = -1;


    /** Dialog containing all the sliders */
    /** Dialog containing all the sliders */
@@ -215,6 +217,17 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private ToneGenerator mToneGenerators[];
    private ToneGenerator mToneGenerators[];
    private Vibrator mVibrator;
    private Vibrator mVibrator;


    private ContentObserver mSettingsObserver = new ContentObserver(this) {
        @Override
        public void onChange(boolean selfChange) {
            mVolumeLinkNotification = Settings.System.getInt(mContext.getContentResolver(),
                    Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
            final int overlayStyle = Settings.System.getInt(mContext.getContentResolver(),
                    Settings.System.MODE_VOLUME_OVERLAY, VOLUME_OVERLAY_EXPANDABLE);
            changeOverlayStyle(overlayStyle);
        }
    };

    public VolumePanel(final Context context, AudioService volumeService) {
    public VolumePanel(final Context context, AudioService volumeService) {
        mContext = context;
        mContext = context;
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
@@ -232,7 +245,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie


        LayoutInflater inflater = (LayoutInflater) context
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mView = inflater.inflate(R.layout.volume_adjust, null);
        mView = inflater.inflate(R.layout.volume_adjust, null);
        mView.setOnTouchListener(new View.OnTouchListener() {
        mView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
            public boolean onTouch(View v, MotionEvent event) {
                resetTimeout();
                resetTimeout();
@@ -278,37 +291,28 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie


        mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
        mToneGenerators = new ToneGenerator[AudioSystem.getNumStreamTypes()];
        mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
        mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);

        mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
        mVoiceCapable = context.getResources().getBoolean(R.bool.config_voice_capable);
        mShowCombinedVolumes = !mVoiceCapable && !useMasterVolume;
        // If we don't want to show multiple volumes, hide the settings button and divider
        if (!mShowCombinedVolumes) {
            mMoreButton.setVisibility(View.GONE);
            mDivider.setVisibility(View.GONE);
        } else {
            mMoreButton.setOnClickListener(this);
        }


        // get the users preference
        // Get the user's preferences
        int choosenStyle = Settings.System.getInt(context.getContentResolver(),Settings.System.MODE_VOLUME_OVERLAY, -1);
        mVolumeLinkNotification = Settings.System.getInt(mContext.getContentResolver(),
        // by default -1 is expected - deal with choosing the right default
                Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
        if (choosenStyle == -1) {
        final int chosenStyle = Settings.System.getInt(context.getContentResolver(),
            if (mVoiceCapable) {
                Settings.System.MODE_VOLUME_OVERLAY, VOLUME_OVERLAY_EXPANDABLE);
                choosenStyle = Settings.System.VOLUME_OVERLAY_SINGLE;
        changeOverlayStyle(chosenStyle);
            } else {

                choosenStyle = Settings.System.VOLUME_OVERLAY_EXPANDABLE;
        context.getContentResolver().registerContentObserver(
            }
                Settings.System.getUriFor(Settings.System.VOLUME_LINK_NOTIFICATION), false,
        }
                mSettingsObserver);
        changeOverlayStyle(choosenStyle);
        context.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.MODE_VOLUME_OVERLAY), false,
                mSettingsObserver);
        mMoreButton.setOnClickListener(this);
        mMoreButton.setOnClickListener(this);

        listenToRingerMode();
        listenToRingerMode();
    }
    }


    private void listenToRingerMode() {
    private void listenToRingerMode() {
        final IntentFilter filter = new IntentFilter();
        final IntentFilter filter = new IntentFilter();
        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
        filter.addAction(ACTION_VOLUME_OVERLAY_CHANGED);
        mContext.registerReceiver(new BroadcastReceiver() {
        mContext.registerReceiver(new BroadcastReceiver() {


            public void onReceive(Context context, Intent intent) {
            public void onReceive(Context context, Intent intent) {
@@ -317,9 +321,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
                if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
                if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
                    removeMessages(MSG_RINGER_MODE_CHANGED);
                    removeMessages(MSG_RINGER_MODE_CHANGED);
                    sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
                    sendMessage(obtainMessage(MSG_RINGER_MODE_CHANGED));
                } else if (ACTION_VOLUME_OVERLAY_CHANGED.equals(action)) {
                    int state = (Integer) intent.getExtra("state");
                    changeOverlayStyle(state);
                }
                }
            }
            }
        }, filter);
        }, filter);
@@ -330,31 +331,31 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
        // Don't change to the same style
        // Don't change to the same style
        if (newStyle == mCurrentOverlayStyle) return;
        if (newStyle == mCurrentOverlayStyle) return;
        switch (newStyle) {
        switch (newStyle) {
            case Settings.System.VOLUME_OVERLAY_SINGLE :
            case VOLUME_OVERLAY_SINGLE :
                mMoreButton.setVisibility(View.GONE);
                mMoreButton.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mShowCombinedVolumes = false;
                mShowCombinedVolumes = false;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_SINGLE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_SINGLE;
                break;
                break;
            case Settings.System.VOLUME_OVERLAY_EXPANDABLE :
            case VOLUME_OVERLAY_EXPANDABLE :
                mMoreButton.setVisibility(View.VISIBLE);
                mMoreButton.setVisibility(View.VISIBLE);
                mDivider.setVisibility(View.VISIBLE);
                mDivider.setVisibility(View.VISIBLE);
                mShowCombinedVolumes = true;
                mShowCombinedVolumes = true;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_EXPANDABLE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_EXPANDABLE;
                break;
                break;
            case Settings.System.VOLUME_OVERLAY_EXPANDED :
            case VOLUME_OVERLAY_EXPANDED :
                mMoreButton.setVisibility(View.GONE);
                mMoreButton.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mShowCombinedVolumes = true;
                mShowCombinedVolumes = true;
                if (mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_NONE) {
                if (mCurrentOverlayStyle == VOLUME_OVERLAY_NONE) {
                    addOtherVolumes();
                    addOtherVolumes();
                    expand();
                    expand();
                }
                }
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_EXPANDED;
                mCurrentOverlayStyle = VOLUME_OVERLAY_EXPANDED;
                break;
                break;
            case Settings.System.VOLUME_OVERLAY_NONE :
            case VOLUME_OVERLAY_NONE :
                mShowCombinedVolumes = false;
                mShowCombinedVolumes = false;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_NONE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_NONE;
                break;
                break;
        }
        }
    }
    }
@@ -390,8 +391,14 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    }
    }


    private boolean isMuted(int streamType) {
    private boolean isMuted(int streamType) {
        if (streamType == STREAM_MASTER) {
            return mAudioManager.isMasterMute();
        } else if (streamType == AudioService.STREAM_REMOTE_MUSIC) {
            return (mAudioService.getRemoteStreamVolume() <= 0);
        } else {
            return mAudioManager.isStreamMute(streamType);
            return mAudioManager.isStreamMute(streamType);
        }
        }
    }


    private void createSliders() {
    private void createSliders() {
        LayoutInflater inflater = (LayoutInflater) mContext
        LayoutInflater inflater = (LayoutInflater) mContext
@@ -452,6 +459,11 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
            if (!mVoiceCapable && streamType == AudioManager.STREAM_RING) {
            if (!mVoiceCapable && streamType == AudioManager.STREAM_RING) {
                continue;
                continue;
            }
            }
            // Skip notification volume if linked with ring volume
            if (mVoiceCapable && mVolumeLinkNotification &&
                    streamType == AudioManager.STREAM_NOTIFICATION) {
                continue;
            }
            StreamControl sc = mStreamControls.get(streamType);
            StreamControl sc = mStreamControls.get(streamType);
            mSliderGroup.addView(sc.group);
            mSliderGroup.addView(sc.group);
            updateSlider(sc);
            updateSlider(sc);
@@ -595,9 +607,8 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie


        if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
        if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
            synchronized (this) {
            synchronized (this) {
	            if (mActiveStreamType == -1 || streamType != mActiveStreamType) {
                if (streamType != mActiveStreamType) {
	                if (streamType != mActiveStreamType && 
                    if (mCurrentOverlayStyle == VOLUME_OVERLAY_EXPANDABLE) {
	                       mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_EXPANDABLE) {
                        hideSlider(mActiveStreamType);
                        hideSlider(mActiveStreamType);
                    }
                    }
                    reorderSliders(streamType);
                    reorderSliders(streamType);
@@ -721,17 +732,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
                sc.seekbarView.setMax(max);
                sc.seekbarView.setMax(max);
            }
            }
            sc.seekbarView.setProgress(index);
            sc.seekbarView.setProgress(index);
            // If adjusting Ring volume and preference is to link it to Notification
            if (streamType == mAudioManager.STREAM_RING &&
                    System.getInt(mContext.getContentResolver(),System.VOLUME_LINK_NOTIFICATION, 1) == 1) {
                StreamControl notifySc = mStreamControls.get(mAudioManager.STREAM_NOTIFICATION);
                if (index > notifySc.seekbarView.getMax()) {
                    notifySc.seekbarView.setProgress(notifySc.seekbarView.getMax());
                } else {
                    notifySc.seekbarView.setProgress(index);
                }
            }

            if (streamType != mAudioManager.getMasterStreamType()
            if (streamType != mAudioManager.getMasterStreamType()
                    && streamType != AudioService.STREAM_REMOTE_MUSIC && isMuted(streamType)) {
                    && streamType != AudioService.STREAM_REMOTE_MUSIC && isMuted(streamType)) {
                sc.seekbarView.setEnabled(false);
                sc.seekbarView.setEnabled(false);
@@ -741,17 +741,17 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
        }
        }


        // Only Show if style needs it
        // Only Show if style needs it
        if (!mDialog.isShowing() && mCurrentOverlayStyle != Settings.System.VOLUME_OVERLAY_NONE) {
        if (!mDialog.isShowing() && mCurrentOverlayStyle != VOLUME_OVERLAY_NONE) {
            int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
            int stream = (streamType == AudioService.STREAM_REMOTE_MUSIC) ? -1 : streamType;
            // when the stream is for remote playback, use -1 to reset the stream type evaluation
            // when the stream is for remote playback, use -1 to reset the stream type evaluation
            mAudioManager.forceVolumeControlStream(stream);
            mAudioManager.forceVolumeControlStream(stream);
            mDialog.setContentView(mView);
            mDialog.setContentView(mView);
            // Showing dialog - use collapsed state
            // Showing dialog - use collapsed state
            if (mShowCombinedVolumes && mCurrentOverlayStyle != Settings.System.VOLUME_OVERLAY_EXPANDED) {
            if (mShowCombinedVolumes && mCurrentOverlayStyle != VOLUME_OVERLAY_EXPANDED) {
                collapse();
                collapse();
            }
            }
            // If just changed the style and we need to expand
            // If just changed the style and we need to expand
            if (mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_EXPANDED) {
            if (mCurrentOverlayStyle == VOLUME_OVERLAY_EXPANDED) {
                expand();
                expand();
            }
            }
            mDialog.show();
            mDialog.show();
@@ -996,28 +996,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
            StreamControl sc = (StreamControl) tag;
            StreamControl sc = (StreamControl) tag;
            if (getStreamVolume(sc.streamType) != progress) {
            if (getStreamVolume(sc.streamType) != progress) {
                setStreamVolume(sc.streamType, progress, 0);
                setStreamVolume(sc.streamType, progress, 0);
                // if audio is linked then adjust other one if change made by user
                if (fromUser && System.getInt(mContext.getContentResolver(),System.VOLUME_LINK_NOTIFICATION, 1) == 1) {
                    if (sc.streamType == AudioManager.STREAM_RING) {
                        StreamControl notifySc = mStreamControls.get(AudioManager.STREAM_NOTIFICATION);
                        if (notifySc != null) {
                            if (progress > notifySc.seekbarView.getMax()) {
                                notifySc.seekbarView.setProgress(notifySc.seekbarView.getMax());
                            } else {
                                notifySc.seekbarView.setProgress(progress);
                            }
                        }
                    } else if (sc.streamType == AudioManager.STREAM_NOTIFICATION) {
                        StreamControl phoneSc = mStreamControls.get(AudioManager.STREAM_RING);
                        if (phoneSc != null) {
                            if (progress > phoneSc.seekbarView.getMax()) {
                                phoneSc.seekbarView.setProgress(phoneSc.seekbarView.getMax());
                            } else {
                                phoneSc.seekbarView.setProgress(progress);
                            }
                        }
                    }
                }
            }
            }
        }
        }
        resetTimeout();
        resetTimeout();