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

Commit 09592c64 authored by DvTonder's avatar DvTonder
Browse files

Forward port Volume panel style and Linked volumes

Original author ptunstal

Change-Id: I4359550fabd110ced6359d2bdcd321561b08fae0
parent 5ae717b3
Loading
Loading
Loading
Loading
+65 −85
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.media.AudioService;
import android.media.AudioSystem;
@@ -38,7 +39,6 @@ import android.os.Message;
import android.os.RemoteException;
import android.os.Vibrator;
import android.provider.Settings;
import android.provider.Settings.System;
import android.util.Log;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
@@ -102,8 +102,10 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private static final int STREAM_MASTER = -100;
    // Pseudo stream type for remote volume is defined in AudioService.STREAM_REMOTE_MUSIC

    public static final String ACTION_VOLUME_OVERLAY_CHANGED
        = "android.intent.action.VOLUME_OVERLAY_CHANGED";
    public static final int VOLUME_OVERLAY_SINGLE = 0;
    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;
    private AudioManager mAudioManager;
@@ -111,6 +113,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private boolean mRingIsSilent;
    private boolean mShowCombinedVolumes;
    private boolean mVoiceCapable;
    private boolean mVolumeLinkNotification;
    private int mCurrentOverlayStyle = -1;

    // True if we want to play tones on the system stream when the master stream is specified.
@@ -145,7 +148,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
                R.string.volume_icon_description_ringer,
                R.drawable.ic_audio_ring_notif,
                R.drawable.ic_audio_ring_notif_mute,
                false),
                true),
        VoiceStream(AudioManager.STREAM_VOICE_CALL,
                R.string.volume_icon_description_incall,
                R.drawable.ic_audio_phone,
@@ -155,7 +158,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
                R.string.volume_alarm,
                R.drawable.ic_audio_alarm,
                R.drawable.ic_audio_alarm_mute,
                false),
                true),
        MediaStream(AudioManager.STREAM_MUSIC,
                R.string.volume_icon_description_media,
                R.drawable.ic_audio_vol,
@@ -220,6 +223,17 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private ToneGenerator mToneGenerators[];
    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);
        }
    };

    private static AlertDialog sConfirmSafeVolumeDialog;
    private static Object sConfirmSafeVolumeLock = new Object();

@@ -269,7 +283,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie

        LayoutInflater inflater = (LayoutInflater) context
                .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() {
            public boolean onTouch(View v, MotionEvent event) {
                resetTimeout();
@@ -315,44 +329,38 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie

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

        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 user's preferences
        mVolumeLinkNotification = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
        final int chosenStyle = Settings.System.getInt(context.getContentResolver(),
                Settings.System.MODE_VOLUME_OVERLAY, VOLUME_OVERLAY_EXPANDABLE);
        changeOverlayStyle(chosenStyle);

        context.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.VOLUME_LINK_NOTIFICATION), false,
                mSettingsObserver);
        context.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.MODE_VOLUME_OVERLAY), false,
                mSettingsObserver);

        // This is new with 4.2 it seems
        boolean masterVolumeOnly = context.getResources().getBoolean(
                com.android.internal.R.bool.config_useMasterVolume);
        boolean masterVolumeKeySounds = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_useVolumeKeySounds);

        mPlayMasterStreamTones = masterVolumeOnly && masterVolumeKeySounds;
        // End this is new

        // get the users preference
        int choosenStyle = Settings.System.getInt(context.getContentResolver(),Settings.System.MODE_VOLUME_OVERLAY, -1);
        // by default -1 is expected - deal with choosing the right default
        if (choosenStyle == -1) {
            if (mVoiceCapable) {
                choosenStyle = Settings.System.VOLUME_OVERLAY_SINGLE;
            } else {
                choosenStyle = Settings.System.VOLUME_OVERLAY_EXPANDABLE;
            }
        }
        changeOverlayStyle(choosenStyle);
        mMoreButton.setOnClickListener(this);

        listenToRingerMode();
    }

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

            public void onReceive(Context context, Intent intent) {
@@ -361,9 +369,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
                if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
                    removeMessages(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);
@@ -374,31 +379,31 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
        // Don't change to the same style
        if (newStyle == mCurrentOverlayStyle) return;
        switch (newStyle) {
            case Settings.System.VOLUME_OVERLAY_SINGLE :
            case VOLUME_OVERLAY_SINGLE :
                mMoreButton.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mShowCombinedVolumes = false;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_SINGLE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_SINGLE;
                break;
            case Settings.System.VOLUME_OVERLAY_EXPANDABLE :
            case VOLUME_OVERLAY_EXPANDABLE :
                mMoreButton.setVisibility(View.VISIBLE);
                mDivider.setVisibility(View.VISIBLE);
                mShowCombinedVolumes = true;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_EXPANDABLE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_EXPANDABLE;
                break;
            case Settings.System.VOLUME_OVERLAY_EXPANDED :
            case VOLUME_OVERLAY_EXPANDED :
                mMoreButton.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mShowCombinedVolumes = true;
                if (mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_NONE) {
                if (mCurrentOverlayStyle == VOLUME_OVERLAY_NONE) {
                    addOtherVolumes();
                    expand();
                }
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_EXPANDED;
                mCurrentOverlayStyle = VOLUME_OVERLAY_EXPANDED;
                break;
            case Settings.System.VOLUME_OVERLAY_NONE :
            case VOLUME_OVERLAY_NONE :
                mShowCombinedVolumes = false;
                mCurrentOverlayStyle = Settings.System.VOLUME_OVERLAY_NONE;
                mCurrentOverlayStyle = VOLUME_OVERLAY_NONE;
                break;
        }
    }
@@ -434,8 +439,14 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    }

    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);
        }
    }

    private void createSliders() {
        LayoutInflater inflater = (LayoutInflater) mContext
@@ -445,9 +456,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
        for (int i = 0; i < STREAMS.length; i++) {
            StreamResources streamRes = STREAMS[i];
            int streamType = streamRes.streamType;
            if (mVoiceCapable && streamRes == StreamResources.NotificationStream) {
                streamRes = StreamResources.RingerStream;
            }
            StreamControl sc = new StreamControl();
            sc.streamType = streamType;
            sc.group = (ViewGroup) inflater.inflate(R.layout.volume_adjust_item, null);
@@ -499,6 +507,11 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
            if (!mVoiceCapable && streamType == AudioManager.STREAM_RING) {
                continue;
            }
            // Skip notification volume if linked with ring volume
            if (mVoiceCapable && mVolumeLinkNotification &&
                    streamType == AudioManager.STREAM_NOTIFICATION) {
                continue;
            }
            StreamControl sc = mStreamControls.get(streamType);
            mSliderGroup.addView(sc.group);
            updateSlider(sc);
@@ -647,9 +660,8 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie

        if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
            synchronized (this) {
	            if (mActiveStreamType == -1 || streamType != mActiveStreamType) {
	                if (streamType != mActiveStreamType && 
	                       mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_EXPANDABLE) {
                if (streamType != mActiveStreamType) {
                    if (mCurrentOverlayStyle == VOLUME_OVERLAY_EXPANDABLE) {
                        hideSlider(mActiveStreamType);
                    }
                    reorderSliders(streamType);
@@ -782,30 +794,20 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
            } else {
                sc.seekbarView.setEnabled(true);
            }
            // 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);
                }
            }
        }

        // 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;
            // when the stream is for remote playback, use -1 to reset the stream type evaluation
            mAudioManager.forceVolumeControlStream(stream);
            mDialog.setContentView(mView);
            // Showing dialog - use collapsed state
            if (mShowCombinedVolumes && mCurrentOverlayStyle != Settings.System.VOLUME_OVERLAY_EXPANDED) {
            if (mShowCombinedVolumes && mCurrentOverlayStyle != VOLUME_OVERLAY_EXPANDED) {
                collapse();
            }
            // If just changed the style and we need to expand
            if (mCurrentOverlayStyle == Settings.System.VOLUME_OVERLAY_EXPANDED) {
            if (mCurrentOverlayStyle == VOLUME_OVERLAY_EXPANDED) {
                expand();
            }
            mDialog.show();
@@ -1089,28 +1091,6 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
            StreamControl sc = (StreamControl) tag;
            if (getStreamVolume(sc.streamType) != progress) {
                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();
+24 −1
Original line number Diff line number Diff line
@@ -236,7 +236,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
     * NOTE: do not create loops in aliases!
     * Some streams alias to different streams according to device category (phone or tablet) or
     * use case (in call s off call...).See updateStreamVolumeAlias() for more details
     *  mStreamVolumeAlias contains the default aliases for a voice capable device (phone) and
     *  STREAM_VOLUME_ALIAS contains the default aliases for a voice capable device (phone) and
     *  STREAM_VOLUME_ALIAS_NON_VOICE for a non voice capable device (tablet).*/
    private final int[] STREAM_VOLUME_ALIAS = new int[] {
        AudioSystem.STREAM_VOICE_CALL,      // STREAM_VOICE_CALL
@@ -278,6 +278,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            "STREAM_TTS"
    };

    private boolean mLinkNotificationWithVolume;

    private final AudioSystem.ErrorCallback mAudioSystemCallback = new AudioSystem.ErrorCallback() {
        public void onError(int error) {
            switch (error) {
@@ -619,6 +621,13 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL;
        }
        mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias;

        if (mLinkNotificationWithVolume) {
            mStreamVolumeAlias[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;
        } else {
            mStreamVolumeAlias[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
        }

        if (updateVolumes) {
            mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias],
                                                                 false /*lastAudible*/);
@@ -721,6 +730,12 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            readDockAudioSettings(cr);
        }

        //******************************************************************
        //TODO: should this be a user specific setting or device (see below)
        mLinkNotificationWithVolume = Settings.System.getInt(cr,
                Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
        //******************************************************************

        mMuteAffectedStreams = System.getIntForUser(cr,
                System.MUTE_STREAMS_AFFECTED,
                ((1 << AudioSystem.STREAM_MUSIC)|
@@ -3472,6 +3487,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    setRingerModeInt(getRingerMode(), false);
                }
                readDockAudioSettings(mContentResolver);

                mLinkNotificationWithVolume = Settings.System.getInt(mContentResolver,
                        Settings.System.VOLUME_LINK_NOTIFICATION, 1) == 1;
                if (mLinkNotificationWithVolume) {
                    mStreamVolumeAlias[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_RING;
                } else {
                    mStreamVolumeAlias[AudioSystem.STREAM_NOTIFICATION] = AudioSystem.STREAM_NOTIFICATION;
                }
            }
        }
    }