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

Commit c868f703 authored by John Spurlock's avatar John Spurlock
Browse files

Settings: Improve inline volume sliders.

 - Track framework changes to show mute states for non-ringer streams
 - Update vibrate icon in sync with other system components.

Depends on:
  Ie2df52edb070055ef59c74039162a0e785d2fdbb

Bug: 17899613
Change-Id: Ib38c86600693e0f36fe72deb231ea751e0486af7
parent 7da7c222
Loading
Loading
Loading
Loading
+44 −26
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
    private PackageManager mPM;
    private boolean mVoiceCapable;
    private Vibrator mVibrator;
    private AudioManager mAudioManager;
    private VolumeSeekBarPreference mRingOrNotificationPreference;

    private Preference mPhoneRingtonePreference;
@@ -99,7 +100,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
    private boolean mSecure;
    private int mLockscreenSelectedValue;
    private ComponentName mSuppressor;
    private int mRingOrNotificationProgress;
    private int mRingerMode = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
@@ -109,6 +110,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
        mVoiceCapable = Utils.isVoiceCapable(mContext);
        mSecure = new LockPatternUtils(getActivity()).isSecure();

        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
        if (mVibrator != null && !mVibrator.hasVibrator()) {
            mVibrator = null;
@@ -117,15 +119,19 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
        addPreferencesFromResource(R.xml.notification_settings);

        final PreferenceCategory sound = (PreferenceCategory) findPreference(KEY_SOUND);
        initVolumePreference(KEY_MEDIA_VOLUME, AudioManager.STREAM_MUSIC);
        initVolumePreference(KEY_ALARM_VOLUME, AudioManager.STREAM_ALARM);
        initVolumePreference(KEY_MEDIA_VOLUME, AudioManager.STREAM_MUSIC,
                com.android.internal.R.drawable.ic_audio_vol_mute);
        initVolumePreference(KEY_ALARM_VOLUME, AudioManager.STREAM_ALARM,
                com.android.internal.R.drawable.ic_audio_alarm_mute);
        if (mVoiceCapable) {
            mRingOrNotificationPreference =
                    initVolumePreference(KEY_RING_VOLUME, AudioManager.STREAM_RING);
                    initVolumePreference(KEY_RING_VOLUME, AudioManager.STREAM_RING,
                            com.android.internal.R.drawable.ic_audio_ring_notif_mute);
            sound.removePreference(sound.findPreference(KEY_NOTIFICATION_VOLUME));
        } else {
            mRingOrNotificationPreference =
                    initVolumePreference(KEY_NOTIFICATION_VOLUME, AudioManager.STREAM_NOTIFICATION);
                    initVolumePreference(KEY_NOTIFICATION_VOLUME, AudioManager.STREAM_NOTIFICATION,
                            com.android.internal.R.drawable.ic_audio_ring_notif_mute);
            sound.removePreference(sound.findPreference(KEY_RING_VOLUME));
        }
        initRingtones(sound);
@@ -138,6 +144,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements

        mNotificationAccess = findPreference(KEY_NOTIFICATION_ACCESS);
        refreshNotificationListeners();
        updateRingerMode();
        updateEffectsSuppressor();
    }

@@ -148,6 +155,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
        lookupRingtoneNames();
        mSettingsObserver.register(true);
        mReceiver.register(true);
        updateRingOrNotificationPreference();
        updateEffectsSuppressor();
        for (VolumeSeekBarPreference volumePref : mVolumePrefs) {
            volumePref.onActivityResume();
@@ -163,22 +171,29 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
    }

    // === Volumes ===
    private VolumeSeekBarPreference initVolumePreference(String key, int stream) {

    private VolumeSeekBarPreference initVolumePreference(String key, int stream, int muteIcon) {
        final VolumeSeekBarPreference volumePref = (VolumeSeekBarPreference) findPreference(key);
        volumePref.setCallback(mVolumeCallback);
        volumePref.setStream(stream);
        mVolumePrefs.add(volumePref);
        volumePref.setMuteIcon(muteIcon);
        return volumePref;
    }

    private void updateRingOrNotificationIcon() {
    private void updateRingOrNotificationPreference() {
        mRingOrNotificationPreference.showIcon(mSuppressor != null
                ? com.android.internal.R.drawable.ic_audio_ring_notif_mute
                : mRingOrNotificationProgress > 0
                        ? com.android.internal.R.drawable.ic_audio_ring_notif
                        : (mVibrator == null
                                ? com.android.internal.R.drawable.ic_audio_ring_notif_mute
                                : com.android.internal.R.drawable.ic_audio_ring_notif_vibrate));
                : mRingerMode == AudioManager.RINGER_MODE_VIBRATE
                ? com.android.internal.R.drawable.ic_audio_ring_notif_vibrate
                : com.android.internal.R.drawable.ic_audio_ring_notif);
    }

    private void updateRingerMode() {
        final int ringerMode = mAudioManager.getRingerModeInternal();
        if (mRingerMode == ringerMode) return;
        mRingerMode = ringerMode;
        updateRingOrNotificationPreference();
    }

    private void updateEffectsSuppressor() {
@@ -191,7 +206,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
                            getSuppressorCaption(suppressor)) : null;
            mRingOrNotificationPreference.setSuppressionText(text);
        }
        updateRingOrNotificationIcon();
        updateRingOrNotificationPreference();
    }

    private String getSuppressorCaption(ComponentName suppressor) {
@@ -230,10 +245,7 @@ public class NotificationSettings extends SettingsPreferenceFragment implements

        @Override
        public void onStreamValueChanged(int stream, int progress) {
            if (stream == AudioManager.STREAM_RING) {
                mHandler.removeMessages(H.UPDATE_RINGER_ICON);
                mHandler.obtainMessage(H.UPDATE_RINGER_ICON, progress, 0).sendToTarget();
            }
            // noop
        }

        public void stopSample() {
@@ -514,8 +526,8 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
        private static final int UPDATE_PHONE_RINGTONE = 1;
        private static final int UPDATE_NOTIFICATION_RINGTONE = 2;
        private static final int STOP_SAMPLE = 3;
        private static final int UPDATE_RINGER_ICON = 4;
        private static final int UPDATE_EFFECTS_SUPPRESSOR = 5;
        private static final int UPDATE_EFFECTS_SUPPRESSOR = 4;
        private static final int UPDATE_RINGER_MODE = 5;

        private H() {
            super(Looper.getMainLooper());
@@ -533,13 +545,12 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
                case STOP_SAMPLE:
                    mVolumeCallback.stopSample();
                    break;
                case UPDATE_RINGER_ICON:
                    mRingOrNotificationProgress = msg.arg1;
                    updateRingOrNotificationIcon();
                    break;
                case UPDATE_EFFECTS_SUPPRESSOR:
                    updateEffectsSuppressor();
                    break;
                case UPDATE_RINGER_MODE:
                    updateRingerMode();
                    break;
            }
        }
    }
@@ -550,8 +561,10 @@ public class NotificationSettings extends SettingsPreferenceFragment implements
        public void register(boolean register) {
            if (mRegistered == register) return;
            if (register) {
                mContext.registerReceiver(this,
                        new IntentFilter(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED));
                final IntentFilter filter = new IntentFilter();
                filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED);
                filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
                mContext.registerReceiver(this, filter);
            } else {
                mContext.unregisterReceiver(this);
            }
@@ -560,7 +573,12 @@ public class NotificationSettings extends SettingsPreferenceFragment implements

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) {
                mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR);
            } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) {
                mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE);
            }
        }
    }

+37 −12
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ public class VolumeSeekBarPreference extends SeekBarPreference
    private ImageView mIconView;
    private TextView mSuppressionTextView;
    private String mSuppressionText;
    private boolean mMuted;
    private int mIconResId;
    private int mMuteIconResId;

    public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
@@ -99,22 +102,27 @@ public class VolumeSeekBarPreference extends SeekBarPreference
                    mCallback.onSampleStarting(sbv);
                }
            }
        };
        final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
        if (mVolumizer == null) {
            mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc) {
                // we need to piggyback on SBV's SeekBar listener to update our icon
            @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromTouch) {
                    super.onProgressChanged(seekBar, progress, fromTouch);
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
                if (mCallback != null) {
                    mCallback.onStreamValueChanged(mStream, progress);
                }
            }
            @Override
            public void onMuted(boolean muted) {
                if (mMuted == muted) return;
                mMuted = muted;
                updateIconView();
            }
        };
        final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
        if (mVolumizer == null) {
            mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
        }
        mVolumizer.start();
        mVolumizer.setSeekBar(mSeekBar);
        mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
        updateIconView();
        mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text);
        mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress());
        updateSuppressionText();
@@ -133,12 +141,29 @@ public class VolumeSeekBarPreference extends SeekBarPreference
        mCallback.onStreamValueChanged(mStream, progress);
    }

    private void updateIconView() {
        if (mIconView == null) return;
        if (mIconResId != 0) {
            mIconView.setImageResource(mIconResId);
        } else if (mMuteIconResId != 0 && mMuted) {
            mIconView.setImageResource(mMuteIconResId);
        } else {
            mIconView.setImageDrawable(getIcon());
        }
    }

    public void showIcon(int resId) {
        // Instead of using setIcon, which will trigger listeners, this just decorates the
        // preference temporarily with a new icon.
        if (mIconView != null) {
            mIconView.setImageResource(resId);
        if (mIconResId == resId) return;
        mIconResId = resId;
        updateIconView();
    }

    public void setMuteIcon(int resId) {
        if (mMuteIconResId == resId) return;
        mMuteIconResId = resId;
        updateIconView();
    }

    private Uri getMediaVolumeUri() {