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

Commit cf6549f2 authored by Tyler Gunn's avatar Tyler Gunn
Browse files

Refactor dependency on AccessibilityManager.

The class is final, so the attempt to mock it in the Telecom unit tests
was resulting in Mockito just using the actual class.  This was causing
test flakes.

To fix this, added a new interface that abstracts out the few methods
from AM that our code actually uses.  We mock these in the unit tests and
rely on the real class in production.

Test: Ran all Telecom unit tests to verify no regressions.
Fixes: 270428514
Change-Id: I0e58896640e5ff5c9d7446665ff0ca42cf15597c
parent cfaab432
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -558,7 +558,8 @@ public class CallsManager extends Call.ListenerBase
            ToastFactory toastFactory,
            CallEndpointControllerFactory callEndpointControllerFactory,
            CallAnomalyWatchdog callAnomalyWatchdog,
            Executor asyncTaskExecutor) {
            Executor asyncTaskExecutor,
            Ringer.AccessibilityManagerAdapter accessibilityManagerAdapter) {
        mContext = context;
        mLock = lock;
        mPhoneNumberUtilsAdapter = phoneNumberUtilsAdapter;
@@ -619,7 +620,7 @@ public class CallsManager extends Call.ListenerBase
                ringtoneFactory, systemVibrator,
                new Ringer.VibrationEffectProxy(), mInCallController,
                mContext.getSystemService(NotificationManager.class),
                mContext.getSystemService(AccessibilityManager.class));
                accessibilityManagerAdapter);
        mCallRecordingTonePlayer = new CallRecordingTonePlayer(mContext, audioManager,
                mTimeoutsAdapter, mLock);
        mCallAudioManager = new CallAudioManager(callAudioRouteStateMachine,
+27 −19
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.provider.CallLog.Calls.USER_MISSED_LOW_RING_VOLUME;
import static android.provider.CallLog.Calls.USER_MISSED_NO_VIBRATE;
import static android.provider.Settings.Global.ZEN_MODE_OFF;

import android.annotation.NonNull;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Person;
@@ -54,6 +55,11 @@ import java.util.concurrent.TimeoutException;
 */
@VisibleForTesting
public class Ringer {
    public interface AccessibilityManagerAdapter {
        boolean startFlashNotificationSequence(@NonNull Context context,
                @AccessibilityManager.FlashNotificationReason int reason);
        boolean stopFlashNotificationSequence(@NonNull Context context);
    }
    /**
     * Flag only for local debugging. Do not submit enabled.
     */
@@ -162,7 +168,7 @@ public class Ringer {
    private RingtoneFactory mRingtoneFactory;
    private AudioManager mAudioManager;
    private NotificationManager mNotificationManager;
    private AccessibilityManager mAccessibilityManager;
    private AccessibilityManagerAdapter mAccessibilityManagerAdapter;

    /**
     * Call objects that are ringing, vibrating or call-waiting. These are used only for logging
@@ -197,7 +203,7 @@ public class Ringer {
            VibrationEffectProxy vibrationEffectProxy,
            InCallController inCallController,
            NotificationManager notificationManager,
            AccessibilityManager accessibilityManager) {
            AccessibilityManagerAdapter accessibilityManagerAdapter) {

        mLock = new Object();
        mSystemSettingsUtil = systemSettingsUtil;
@@ -211,7 +217,7 @@ public class Ringer {
        mInCallController = inCallController;
        mVibrationEffectProxy = vibrationEffectProxy;
        mNotificationManager = notificationManager;
        mAccessibilityManager = accessibilityManager;
        mAccessibilityManagerAdapter = accessibilityManagerAdapter;

        if (mContext.getResources().getBoolean(R.bool.use_simple_vibration_pattern)) {
            mDefaultVibrationEffect = mVibrationEffectProxy.createWaveform(SIMPLE_VIBRATION_PATTERN,
@@ -295,9 +301,10 @@ public class Ringer {
        stopCallWaiting();

        final boolean shouldFlash = attributes.shouldRingForContact();
        if (mAccessibilityManager != null && shouldFlash) {
        if (mAccessibilityManagerAdapter != null && shouldFlash) {
            Log.addEvent(foregroundCall, LogUtils.Events.FLASH_NOTIFICATION_START);
            getHandler().post(() -> mAccessibilityManager.startFlashNotificationSequence(mContext,
            getHandler().post(() ->
                    mAccessibilityManagerAdapter.startFlashNotificationSequence(mContext,
                        AccessibilityManager.FLASH_REASON_CALL));
        }

@@ -512,9 +519,10 @@ public class Ringer {

    public void stopRinging() {
        final Call foregroundCall = mRingingCall != null ? mRingingCall : mVibratingCall;
        if (mAccessibilityManager != null) {
        if (mAccessibilityManagerAdapter != null) {
            Log.addEvent(foregroundCall, LogUtils.Events.FLASH_NOTIFICATION_STOP);
            getHandler().post(() -> mAccessibilityManager.stopFlashNotificationSequence(mContext));
            getHandler().post(() ->
                    mAccessibilityManagerAdapter.stopFlashNotificationSequence(mContext));
        }

        synchronized (mLock) {
+6 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.os.UserManager;
import android.telecom.Log;
import android.telecom.PhoneAccountHandle;
import android.telephony.AnomalyReporter;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
@@ -208,7 +209,8 @@ public class TelecomSystem {
            ClockProxy clockProxy,
            RoleManagerAdapter roleManagerAdapter,
            ContactsAsyncHelper.Factory contactsAsyncHelperFactory,
            DeviceIdleControllerAdapter deviceIdleControllerAdapter) {
            DeviceIdleControllerAdapter deviceIdleControllerAdapter,
            Ringer.AccessibilityManagerAdapter accessibilityManagerAdapter) {
        mContext = context.getApplicationContext();
        LogUtils.initLogging(mContext);
        AnomalyReporter.initialize(mContext);
@@ -332,6 +334,7 @@ public class TelecomSystem {
            CallAnomalyWatchdog callAnomalyWatchdog = new CallAnomalyWatchdog(
                    Executors.newSingleThreadScheduledExecutor(),
                    mLock, timeoutsAdapter, clockProxy);

            mCallsManager = new CallsManager(
                    mContext,
                    mLock,
@@ -364,7 +367,8 @@ public class TelecomSystem {
                    toastFactory,
                    callEndpointControllerFactory,
                    callAnomalyWatchdog,
                    Executors.newSingleThreadExecutor());
                    Executors.newSingleThreadExecutor(),
                    accessibilityManagerAdapter);

            mIncomingCallNotifier = incomingCallNotifier;
            incomingCallNotifier.setCallsManagerProxy(new IncomingCallNotifier.CallsManagerProxy() {
+18 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.os.SystemClock;
import android.telecom.Log;

import android.telecom.CallerInfoAsyncQuery;
import android.view.accessibility.AccessibilityManager;

import com.android.internal.telecom.IInternalServiceRetriever;
import com.android.internal.telecom.ITelecomLoader;
@@ -53,6 +54,7 @@ import com.android.server.telecom.PhoneNumberUtilsAdapterImpl;
import com.android.server.telecom.ProximitySensorManagerFactory;
import com.android.server.telecom.InCallWakeLockController;
import com.android.server.telecom.ProximitySensorManager;
import com.android.server.telecom.Ringer;
import com.android.server.telecom.RoleManagerAdapterImpl;
import com.android.server.telecom.TelecomSystem;
import com.android.server.telecom.TelecomWakeLock;
@@ -191,7 +193,22 @@ public class TelecomService extends Service implements TelecomSystem.Component {
                            new RoleManagerAdapterImpl(context,
                                    (RoleManager) context.getSystemService(Context.ROLE_SERVICE)),
                            new ContactsAsyncHelper.Factory(),
                            internalServiceRetriever.getDeviceIdleController()));
                            internalServiceRetriever.getDeviceIdleController(),
                            new Ringer.AccessibilityManagerAdapter() {
                                @Override
                                public boolean startFlashNotificationSequence(
                                        @androidx.annotation.NonNull Context context, int reason) {
                                    return context.getSystemService(AccessibilityManager.class)
                                            .startFlashNotificationSequence(context, reason);
                                }

                                @Override
                                public boolean stopFlashNotificationSequence(
                                        @androidx.annotation.NonNull Context context) {
                                    return context.getSystemService(AccessibilityManager.class)
                                            .stopFlashNotificationSequence(context);
                                }
                            }));
        }
    }

+4 −1
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ import com.android.server.telecom.PhoneAccountRegistrar;
import com.android.server.telecom.PhoneNumberUtilsAdapter;
import com.android.server.telecom.ProximitySensorManager;
import com.android.server.telecom.ProximitySensorManagerFactory;
import com.android.server.telecom.Ringer;
import com.android.server.telecom.RoleManagerAdapter;
import com.android.server.telecom.SystemStateHelper;
import com.android.server.telecom.TelecomSystem;
@@ -235,6 +236,7 @@ public class CallsManagerTest extends TelecomTestCase {
    @Mock private Toast mToast;
    @Mock private CallAnomalyWatchdog mCallAnomalyWatchdog;
    @Mock private AnomalyReporterAdapter mAnomalyReporterAdapter;
    @Mock private Ringer.AccessibilityManagerAdapter mAccessibilityManagerAdapter;

    private CallsManager mCallsManager;

@@ -298,7 +300,8 @@ public class CallsManagerTest extends TelecomTestCase {
                mCallEndpointControllerFactory,
                mCallAnomalyWatchdog,
                // Just do async tasks synchronously to support testing.
                command -> command.run());
                command -> command.run(),
                mAccessibilityManagerAdapter);

        when(mPhoneAccountRegistrar.getPhoneAccount(
                eq(SELF_MANAGED_HANDLE), any())).thenReturn(SELF_MANAGED_ACCOUNT);
Loading