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

Commit 708c44f8 authored by Danny Baumann's avatar Danny Baumann
Browse files

Add back increasing ring feature (3/3).

Change-Id: I6c0582ff92fea06ee18df6d084790b420b1b58f6
parent 781e73bc
Loading
Loading
Loading
Loading
+36 −7
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ class AsyncRingtonePlayer {
    private static final int EVENT_PLAY = 1;
    private static final int EVENT_STOP = 2;
    private static final int EVENT_REPEAT = 3;
    private static final int EVENT_INCREASE_VOLUME = 4;

    // The interval in which to restart the ringer.
    private static final int RESTART_RINGER_MILLIS = 3000;
@@ -47,6 +48,9 @@ class AsyncRingtonePlayer {
    /** The current ringtone. Only used by the ringtone thread. */
    private Ringtone mRingtone;

    private float mIncrementAmount;
    private float mCurrentIncrementVolume;

    /**
     * The context.
     */
@@ -57,15 +61,17 @@ class AsyncRingtonePlayer {
    }

    /** Plays the ringtone. */
    void play(Uri ringtone) {
    void play(Uri ringtone, float incStartVolume, int incRampUpTime) {
        Log.d(this, "Posting play.");
        postMessage(EVENT_PLAY, true /* shouldCreateHandler */, ringtone);

        postMessage(EVENT_PLAY, true /* shouldCreateHandler */, ringtone,
                Math.round(incStartVolume * 100F), incRampUpTime);
    }

    /** Stops playing the ringtone. */
    void stop() {
        Log.d(this, "Posting stop.");
        postMessage(EVENT_STOP, false /* shouldCreateHandler */, null);
        postMessage(EVENT_STOP, false /* shouldCreateHandler */, null, 0, 0);
    }

    /**
@@ -75,7 +81,8 @@ class AsyncRingtonePlayer {
     * @param messageCode The message to post.
     * @param shouldCreateHandler True when a handler should be created to handle this message.
     */
    private void postMessage(int messageCode, boolean shouldCreateHandler, Uri ringtone) {
    private void postMessage(int messageCode, boolean shouldCreateHandler,
            Uri ringtone, int arg1, int arg2) {
        synchronized(this) {
            if (mHandler == null && shouldCreateHandler) {
                mHandler = getNewHandler();
@@ -84,7 +91,7 @@ class AsyncRingtonePlayer {
            if (mHandler == null) {
                Log.d(this, "Message %d skipped because there is no handler.", messageCode);
            } else {
                mHandler.obtainMessage(messageCode, ringtone).sendToTarget();
                mHandler.obtainMessage(messageCode, arg1, arg2, ringtone).sendToTarget();
            }
        }
    }
@@ -103,7 +110,7 @@ class AsyncRingtonePlayer {
            public void handleMessage(Message msg) {
                switch(msg.what) {
                    case EVENT_PLAY:
                        handlePlay((Uri) msg.obj);
                        handlePlay((Uri) msg.obj, (float) msg.arg1 / 100F, msg.arg2);
                        break;
                    case EVENT_REPEAT:
                        handleRepeat();
@@ -111,6 +118,15 @@ class AsyncRingtonePlayer {
                    case EVENT_STOP:
                        handleStop();
                        break;
                    case EVENT_INCREASE_VOLUME:
                        mCurrentIncrementVolume += mIncrementAmount;
                        Log.d(AsyncRingtonePlayer.this, "Increasing ringtone volume to "
                                + Math.round(mCurrentIncrementVolume * 100F) + "%");
                        mRingtone.setVolume(mCurrentIncrementVolume);
                        if (mCurrentIncrementVolume < 1F) {
                            sendEmptyMessageDelayed(EVENT_INCREASE_VOLUME, 1000);
                        }
                        break;
                }
            }
        };
@@ -119,7 +135,7 @@ class AsyncRingtonePlayer {
    /**
     * Starts the actual playback of the ringtone. Executes on ringtone-thread.
     */
    private void handlePlay(Uri ringtoneUri) {
    private void handlePlay(Uri ringtoneUri, float incStartVolume, int incRampUpTime) {
        // don't bother with any of this if there is an EVENT_STOP waiting.
        if (mHandler.hasMessages(EVENT_STOP)) {
            return;
@@ -138,6 +154,18 @@ class AsyncRingtonePlayer {
            }
        }

        if (incRampUpTime > 0) {
            Log.d(this, "Starting ringtone volume at " + Math.round(incStartVolume * 100F) + "%");
            mRingtone.setVolume(incStartVolume);

            mIncrementAmount = (1F - incStartVolume) / (float) incRampUpTime;
            mCurrentIncrementVolume = incStartVolume;

            mHandler.sendEmptyMessageDelayed(EVENT_INCREASE_VOLUME, 1000);
        } else {
            mRingtone.setVolume(1F);
        }

        handleRepeat();
    }

@@ -178,6 +206,7 @@ class AsyncRingtonePlayer {
            // At the time that STOP is handled, there should be no need for repeat messages in the
            // queue.
            mHandler.removeMessages(EVENT_REPEAT);
            mHandler.removeMessages(EVENT_INCREASE_VOLUME);

            if (mHandler.hasMessages(EVENT_PLAY)) {
                Log.v(this, "Keeping alive ringtone thread for subsequent play request.");
+14 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.telecom;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
@@ -183,13 +184,25 @@ final class Ringer extends CallsManagerListenerBase {
                    (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_RING) >= 0) {
                Log.v(this, "startRingingOrCallWaiting");

                float startVolume = 0;
                int rampUpTime = 0;

                final ContentResolver cr = mContext.getContentResolver();
                if (Settings.System.getInt(cr, Settings.System.INCREASING_RING, 0) != 0) {
                    startVolume = Settings.System.getFloat(cr,
                            Settings.System.INCREASING_RING_START_VOLUME, 0.1f);
                    rampUpTime = Settings.System.getInt(cr,
                            Settings.System.INCREASING_RING_RAMP_UP_TIME, 20);
                }

                mCallAudioManager.setIsRinging(true);

                // Because we wait until a contact info query to complete before processing a
                // call (for the purposes of direct-to-voicemail), the information about custom
                // ringtones should be available by the time this code executes. We can safely
                // request the custom ringtone from the call and expect it to be current.
                mRingtonePlayer.play(foregroundCall.getRingtone());
                mRingtonePlayer.play(foregroundCall.getRingtone(), startVolume, rampUpTime);
            } else {
                Log.v(this, "startRingingOrCallWaiting, skipping because volume is 0");
            }