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

Commit 2238e857 authored by Danny Baumann's avatar Danny Baumann Committed by Gerrit Code Review
Browse files

Fix up increasing ring tone volume preview behaviour.

Change-Id: Ibe7fdf1ee7f0aa1c7c76052df69fa92e8b6a2ffd
parent 7c09a30e
Loading
Loading
Loading
Loading
+41 −13
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.notification;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
@@ -59,6 +60,7 @@ public class IncreasingRingVolumePreference extends Preference implements
    private static final int MSG_START_SAMPLE = 1;
    private static final int MSG_STOP_SAMPLE = 2;
    private static final int MSG_INIT_SAMPLE = 3;
    private static final int MSG_SET_VOLUME = 4;
    private static final int CHECK_RINGTONE_PLAYBACK_DELAY_MS = 1000;

    public IncreasingRingVolumePreference(Context context) {
@@ -85,6 +87,10 @@ public class IncreasingRingVolumePreference extends Preference implements
        mCallback = callback;
    }

    public void onActivityResume() {
        initHandler();
    }

    @Override
    public void onActivityStop() {
        if (mHandler != null) {
@@ -98,7 +104,7 @@ public class IncreasingRingVolumePreference extends Preference implements
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_START_SAMPLE:
                onStartSample();
                onStartSample((float) msg.arg1 / 1000F);
                break;
            case MSG_STOP_SAMPLE:
                onStopSample();
@@ -106,6 +112,9 @@ public class IncreasingRingVolumePreference extends Preference implements
            case MSG_INIT_SAMPLE:
                onInitSample();
                break;
            case MSG_SET_VOLUME:
                onSetVolume((float) msg.arg1 / 1000F);
                break;
        }
        return true;
    }
@@ -138,15 +147,12 @@ public class IncreasingRingVolumePreference extends Preference implements

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if (seekBar == mStartVolumeSeekBar && mCallback != null) {
            mCallback.onStartingSample();
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (seekBar == mStartVolumeSeekBar) {
            postStartSample();
            postStartSample(seekBar.getProgress());
        }
    }

@@ -161,7 +167,8 @@ public class IncreasingRingVolumePreference extends Preference implements
            mRampUpTimeValue.setText(
                    Formatter.formatShortElapsedTime(getContext(), seconds * 1000));
            if (fromTouch) {
                CMSettings.System.putInt(cr, CMSettings.System.INCREASING_RING_RAMP_UP_TIME, seconds);
                CMSettings.System.putInt(cr,
                        CMSettings.System.INCREASING_RING_RAMP_UP_TIME, seconds);
            }
        }
    }
@@ -181,24 +188,45 @@ public class IncreasingRingVolumePreference extends Preference implements
                Settings.System.DEFAULT_RINGTONE_URI);
        if (mRingtone != null) {
            mRingtone.setStreamType(AudioManager.STREAM_RING);
            mRingtone.setAudioAttributes(
                    new AudioAttributes.Builder(mRingtone.getAudioAttributes())
                            .setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY |
                                    AudioAttributes.FLAG_BYPASS_MUTE)
                            .build());
        }
    }

    private void postStartSample() {
    private void postStartSample(int progress) {
        boolean playing = isSamplePlaying();
        mHandler.removeMessages(MSG_START_SAMPLE);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE),
                isSamplePlaying() ? CHECK_RINGTONE_PLAYBACK_DELAY_MS : 0);
        mHandler.removeMessages(MSG_SET_VOLUME);
        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_SAMPLE, progress, 0),
                playing ? CHECK_RINGTONE_PLAYBACK_DELAY_MS : 0);
        if (playing) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_VOLUME, progress, 0));
        }
    }

    private void onStartSample() {
        if (!isSamplePlaying() && mRingtone != null) {
    private void onStartSample(float volume) {
        if (mRingtone == null) {
            return;
        }
        if (!isSamplePlaying()) {
            if (mCallback != null) {
                mCallback.onStartingSample();
            }
            try {
                mRingtone.play();
            } catch (Throwable e) {
                Log.w(TAG, "Error playing ringtone", e);
            }
            mHandler.removeMessages(MSG_STOP_SAMPLE);
            mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_STOP_SAMPLE), 2000);
        }
        mRingtone.setVolume(volume);
    }

    private void onSetVolume(float volume) {
        if (mRingtone != null) {
            mRingtone.setVolume(volume);
        }
    }

+11 −0
Original line number Diff line number Diff line
@@ -112,6 +112,8 @@ public class SoundSettings extends SettingsPreferenceFragment implements Indexab
        @Override
        public void onStartingSample() {
            mVolumeCallback.stopSample();
            mHandler.removeMessages(H.STOP_SAMPLE);
            mHandler.sendEmptyMessageDelayed(H.STOP_SAMPLE, SAMPLE_CUTOFF);
        }
    };

@@ -210,6 +212,9 @@ public class SoundSettings extends SettingsPreferenceFragment implements Indexab
        for (VolumeSeekBarPreference volumePref : mVolumePrefs) {
            volumePref.onActivityResume();
        }
        if (mIncreasingRingVolume != null) {
            mIncreasingRingVolume.onActivityResume();
        }
        boolean isRestricted = mUserManager.hasUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME);
        for (String key : RESTRICTED_KEYS) {
            Preference pref = findPreference(key);
@@ -223,6 +228,9 @@ public class SoundSettings extends SettingsPreferenceFragment implements Indexab
    public void onPause() {
        super.onPause();
        mVolumeCallback.stopSample();
        if (mIncreasingRingVolume != null) {
            mIncreasingRingVolume.stopSample();
        }
        mSettingsObserver.register(false);
        mReceiver.register(false);
    }
@@ -611,6 +619,9 @@ public class SoundSettings extends SettingsPreferenceFragment implements Indexab
                    break;
                case STOP_SAMPLE:
                    mVolumeCallback.stopSample();
                    if (mIncreasingRingVolume != null) {
                        mIncreasingRingVolume.stopSample();
                    }
                    break;
                case UPDATE_EFFECTS_SUPPRESSOR:
                    updateEffectsSuppressor();