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

Commit 5cfe2afa authored by Hall Liu's avatar Hall Liu
Browse files

Suppress ringtone when the device is on ear

Don't play a (potentially loud) ringtone over a held call when the user
is holding the phone up to the ear (as detected by position sensors).

Also move Telecom tests to the new mockito so that we can mock final
methods/classes.

Bug: 64438244
Test: manual, unit

Change-Id: I685069bb63c6fd47d6a5e62a8c0896c02cd95056
parent fea1c0db
Loading
Loading
Loading
Loading
+9 −0
Original line number Original line Diff line number Diff line
@@ -60,4 +60,13 @@
         between repeats of the ringtone.
         between repeats of the ringtone.
         When false, the ringtone will be looping with no pause. -->
         When false, the ringtone will be looping with no pause. -->
    <bool name="should_pause_between_ringtone_repeats">true</bool>
    <bool name="should_pause_between_ringtone_repeats">true</bool>

    <!-- Threshold for the X+Y component of gravity needed for the device orientation to be
         classified as being on a user's ear. -->
    <item name="device_on_ear_xy_gravity_threshold" format="float" type="dimen">5.5</item>

    <!-- Lower threshold for the Y-component of gravity needed for the device orientation to be
         classified as being on a user's ear. If the Y-component is less than this negative value,
         the device is probably upside-down and therefore not on a ear -->
    <item name="device_on_ear_y_gravity_negative_threshold" format="float" type="dimen">-1</item>
</resources>
</resources>
+2 −2
Original line number Original line Diff line number Diff line
@@ -446,9 +446,9 @@ public class CallAudioManager extends CallsManagerListenerBase {
    }
    }


    @VisibleForTesting
    @VisibleForTesting
    public void startCallWaiting() {
    public void startCallWaiting(String reason) {
        if (mRingingCalls.size() == 1) {
        if (mRingingCalls.size() == 1) {
            mRinger.startCallWaiting(mRingingCalls.iterator().next());
            mRinger.startCallWaiting(mRingingCalls.iterator().next(), reason);
        }
        }
    }
    }


+17 −7
Original line number Original line Diff line number Diff line
@@ -30,8 +30,9 @@ import com.android.internal.util.StateMachine;


public class CallAudioModeStateMachine extends StateMachine {
public class CallAudioModeStateMachine extends StateMachine {
    public static class Factory {
    public static class Factory {
        public CallAudioModeStateMachine create(AudioManager am) {
        public CallAudioModeStateMachine create(SystemStateHelper systemStateHelper,
            return new CallAudioModeStateMachine(am);
                AudioManager am) {
            return new CallAudioModeStateMachine(systemStateHelper, am);
        }
        }
    }
    }


@@ -333,7 +334,7 @@ public class CallAudioModeStateMachine extends StateMachine {
                    return HANDLED;
                    return HANDLED;
                case NEW_RINGING_CALL:
                case NEW_RINGING_CALL:
                    // Don't make a call ring over an active call, but do play a call waiting tone.
                    // Don't make a call ring over an active call, but do play a call waiting tone.
                    mCallAudioManager.startCallWaiting();
                    mCallAudioManager.startCallWaiting("call already active");
                    return HANDLED;
                    return HANDLED;
                case NEW_HOLDING_CALL:
                case NEW_HOLDING_CALL:
                    // Don't do anything now. Putting an active call on hold will be handled when
                    // Don't do anything now. Putting an active call on hold will be handled when
@@ -388,7 +389,7 @@ public class CallAudioModeStateMachine extends StateMachine {
                    return HANDLED;
                    return HANDLED;
                case NEW_RINGING_CALL:
                case NEW_RINGING_CALL:
                    // Don't make a call ring over an active call, but do play a call waiting tone.
                    // Don't make a call ring over an active call, but do play a call waiting tone.
                    mCallAudioManager.startCallWaiting();
                    mCallAudioManager.startCallWaiting("call already active");
                    return HANDLED;
                    return HANDLED;
                case NEW_HOLDING_CALL:
                case NEW_HOLDING_CALL:
                    // Don't do anything now. Putting an active call on hold will be handled when
                    // Don't do anything now. Putting an active call on hold will be handled when
@@ -442,8 +443,14 @@ public class CallAudioModeStateMachine extends StateMachine {
                            ? mVoipCallFocusState : mSimCallFocusState);
                            ? mVoipCallFocusState : mSimCallFocusState);
                    return HANDLED;
                    return HANDLED;
                case NEW_RINGING_CALL:
                case NEW_RINGING_CALL:
                    // Apparently this is current behavior. Should this be the case?
                    // TODO: consider whether to move this into MessageArgs if more things start
                    // to use it.
                    if (args.hasHoldingCalls && mSystemStateHelper.isDeviceAtEar()) {
                        mCallAudioManager.startCallWaiting(
                                "Device is at ear with held call");
                    } else {
                        transitionTo(mRingingFocusState);
                        transitionTo(mRingingFocusState);
                    }
                    return HANDLED;
                    return HANDLED;
                case NEW_HOLDING_CALL:
                case NEW_HOLDING_CALL:
                    // Do nothing.
                    // Do nothing.
@@ -470,14 +477,17 @@ public class CallAudioModeStateMachine extends StateMachine {
    private final BaseState mOtherFocusState = new OtherFocusState();
    private final BaseState mOtherFocusState = new OtherFocusState();


    private final AudioManager mAudioManager;
    private final AudioManager mAudioManager;
    private final SystemStateHelper mSystemStateHelper;
    private CallAudioManager mCallAudioManager;
    private CallAudioManager mCallAudioManager;


    private int mMostRecentMode;
    private int mMostRecentMode;
    private boolean mIsInitialized = false;
    private boolean mIsInitialized = false;


    public CallAudioModeStateMachine(AudioManager audioManager) {
    public CallAudioModeStateMachine(SystemStateHelper systemStateHelper,
            AudioManager audioManager) {
        super(CallAudioModeStateMachine.class.getSimpleName());
        super(CallAudioModeStateMachine.class.getSimpleName());
        mAudioManager = audioManager;
        mAudioManager = audioManager;
        mSystemStateHelper = systemStateHelper;
        mMostRecentMode = AudioManager.MODE_NORMAL;
        mMostRecentMode = AudioManager.MODE_NORMAL;


        addState(mUnfocusedState);
        addState(mUnfocusedState);
+4 −4
Original line number Original line Diff line number Diff line
@@ -360,7 +360,7 @@ public class CallsManager extends Call.ListenerBase
            CallAudioManager.AudioServiceFactory audioServiceFactory,
            CallAudioManager.AudioServiceFactory audioServiceFactory,
            BluetoothRouteManager bluetoothManager,
            BluetoothRouteManager bluetoothManager,
            WiredHeadsetManager wiredHeadsetManager,
            WiredHeadsetManager wiredHeadsetManager,
            SystemStateProvider systemStateProvider,
            SystemStateHelper systemStateHelper,
            DefaultDialerCache defaultDialerCache,
            DefaultDialerCache defaultDialerCache,
            Timeouts.Adapter timeoutsAdapter,
            Timeouts.Adapter timeoutsAdapter,
            AsyncRingtonePlayer asyncRingtonePlayer,
            AsyncRingtonePlayer asyncRingtonePlayer,
@@ -422,7 +422,7 @@ public class CallsManager extends Call.ListenerBase
        RingtoneFactory ringtoneFactory = new RingtoneFactory(this, context);
        RingtoneFactory ringtoneFactory = new RingtoneFactory(this, context);
        SystemVibrator systemVibrator = new SystemVibrator(context);
        SystemVibrator systemVibrator = new SystemVibrator(context);
        mInCallController = inCallControllerFactory.create(context, mLock, this,
        mInCallController = inCallControllerFactory.create(context, mLock, this,
                systemStateProvider, defaultDialerCache, mTimeoutsAdapter,
                systemStateHelper, defaultDialerCache, mTimeoutsAdapter,
                emergencyCallHelper);
                emergencyCallHelper);
        mRinger = new Ringer(playerFactory, context, systemSettingsUtil, asyncRingtonePlayer,
        mRinger = new Ringer(playerFactory, context, systemSettingsUtil, asyncRingtonePlayer,
                ringtoneFactory, systemVibrator,
                ringtoneFactory, systemVibrator,
@@ -430,8 +430,8 @@ public class CallsManager extends Call.ListenerBase
        mCallRecordingTonePlayer = new CallRecordingTonePlayer(mContext,
        mCallRecordingTonePlayer = new CallRecordingTonePlayer(mContext,
                (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE), mLock);
                (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE), mLock);
        mCallAudioManager = new CallAudioManager(callAudioRouteStateMachine,
        mCallAudioManager = new CallAudioManager(callAudioRouteStateMachine,
                this, callAudioModeStateMachineFactory.create((AudioManager)
                this, callAudioModeStateMachineFactory.create(systemStateHelper,
                        mContext.getSystemService(Context.AUDIO_SERVICE)),
                (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)),
                playerFactory, mRinger, new RingbackPlayer(playerFactory),
                playerFactory, mRinger, new RingbackPlayer(playerFactory),
                bluetoothStateReceiver, mDtmfLocalTonePlayer);
                bluetoothStateReceiver, mDtmfLocalTonePlayer);


+6 −6
Original line number Original line Diff line number Diff line
@@ -47,7 +47,7 @@ import com.android.internal.annotations.VisibleForTesting;
// TODO: Needed for move to system service: import com.android.internal.R;
// TODO: Needed for move to system service: import com.android.internal.R;
import com.android.internal.telecom.IInCallService;
import com.android.internal.telecom.IInCallService;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.IndentingPrintWriter;
import com.android.server.telecom.SystemStateProvider.SystemStateListener;
import com.android.server.telecom.SystemStateHelper.SystemStateListener;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collection;
@@ -729,7 +729,7 @@ public class InCallController extends CallsManagerListenerBase {
    private final Context mContext;
    private final Context mContext;
    private final TelecomSystem.SyncRoot mLock;
    private final TelecomSystem.SyncRoot mLock;
    private final CallsManager mCallsManager;
    private final CallsManager mCallsManager;
    private final SystemStateProvider mSystemStateProvider;
    private final SystemStateHelper mSystemStateHelper;
    private final Timeouts.Adapter mTimeoutsAdapter;
    private final Timeouts.Adapter mTimeoutsAdapter;
    private final DefaultDialerCache mDefaultDialerCache;
    private final DefaultDialerCache mDefaultDialerCache;
    private final EmergencyCallHelper mEmergencyCallHelper;
    private final EmergencyCallHelper mEmergencyCallHelper;
@@ -737,13 +737,13 @@ public class InCallController extends CallsManagerListenerBase {
    private NonUIInCallServiceConnectionCollection mNonUIInCallServiceConnections;
    private NonUIInCallServiceConnectionCollection mNonUIInCallServiceConnections;


    public InCallController(Context context, TelecomSystem.SyncRoot lock, CallsManager callsManager,
    public InCallController(Context context, TelecomSystem.SyncRoot lock, CallsManager callsManager,
            SystemStateProvider systemStateProvider,
            SystemStateHelper systemStateHelper,
            DefaultDialerCache defaultDialerCache, Timeouts.Adapter timeoutsAdapter,
            DefaultDialerCache defaultDialerCache, Timeouts.Adapter timeoutsAdapter,
            EmergencyCallHelper emergencyCallHelper) {
            EmergencyCallHelper emergencyCallHelper) {
        mContext = context;
        mContext = context;
        mLock = lock;
        mLock = lock;
        mCallsManager = callsManager;
        mCallsManager = callsManager;
        mSystemStateProvider = systemStateProvider;
        mSystemStateHelper = systemStateHelper;
        mTimeoutsAdapter = timeoutsAdapter;
        mTimeoutsAdapter = timeoutsAdapter;
        mDefaultDialerCache = defaultDialerCache;
        mDefaultDialerCache = defaultDialerCache;
        mEmergencyCallHelper = emergencyCallHelper;
        mEmergencyCallHelper = emergencyCallHelper;
@@ -753,7 +753,7 @@ public class InCallController extends CallsManagerListenerBase {
                resources.getString(R.string.ui_default_package),
                resources.getString(R.string.ui_default_package),
                resources.getString(R.string.incall_default_class));
                resources.getString(R.string.incall_default_class));


        mSystemStateProvider.addListener(mSystemStateListener);
        mSystemStateHelper.addListener(mSystemStateListener);
    }
    }


    @Override
    @Override
@@ -1227,7 +1227,7 @@ public class InCallController extends CallsManagerListenerBase {
    }
    }


    private boolean shouldUseCarModeUI() {
    private boolean shouldUseCarModeUI() {
        return mSystemStateProvider.isCarMode();
        return mSystemStateHelper.isCarMode();
    }
    }


    /**
    /**
Loading