diff --git a/services/core/java/com/android/server/vibrator/InputDeviceDelegate.java b/services/core/java/com/android/server/vibrator/InputDeviceDelegate.java index 685dce4683d7c..96f84dc65e1dd 100644 --- a/services/core/java/com/android/server/vibrator/InputDeviceDelegate.java +++ b/services/core/java/com/android/server/vibrator/InputDeviceDelegate.java @@ -16,6 +16,7 @@ package com.android.server.vibrator; +import android.annotation.Nullable; import android.content.Context; import android.hardware.input.InputManager; import android.os.CombinedVibrationEffect; @@ -33,7 +34,11 @@ final class InputDeviceDelegate implements InputManager.InputDeviceListener { private final Object mLock = new Object(); private final Handler mHandler; - private final InputManager mInputManager; + private final Context mContext; + + @GuardedBy("mLock") + @Nullable + private InputManager mInputManager; @GuardedBy("mLock") private final SparseArray mInputDeviceVibrators = new SparseArray<>(); @@ -47,7 +52,13 @@ final class InputDeviceDelegate implements InputManager.InputDeviceListener { InputDeviceDelegate(Context context, Handler handler) { mHandler = handler; - mInputManager = context.getSystemService(InputManager.class); + mContext = context; + } + + public void onSystemReady() { + synchronized (mLock) { + mInputManager = mContext.getSystemService(InputManager.class); + } } @Override @@ -116,6 +127,10 @@ final class InputDeviceDelegate implements InputManager.InputDeviceListener { */ public boolean updateInputDeviceVibrators(boolean vibrateInputDevices) { synchronized (mLock) { + if (mInputManager == null) { + // Ignore update, service not loaded yet so change cannot be applied. + return false; + } if (vibrateInputDevices == mShouldVibrateInputDevices) { // No need to update if settings haven't changed. return false; @@ -150,6 +165,10 @@ final class InputDeviceDelegate implements InputManager.InputDeviceListener { private void updateInputDevice(int deviceId) { synchronized (mLock) { + if (mInputManager == null) { + // Ignore update, service not loaded yet so change cannot be applied. + return; + } if (!mShouldVibrateInputDevices) { // No need to keep this device vibrator if setting is off. return; diff --git a/services/core/java/com/android/server/vibrator/VibrationSettings.java b/services/core/java/com/android/server/vibrator/VibrationSettings.java index 334129d6bde96..4a07c1ac1e39d 100644 --- a/services/core/java/com/android/server/vibrator/VibrationSettings.java +++ b/services/core/java/com/android/server/vibrator/VibrationSettings.java @@ -16,6 +16,7 @@ package com.android.server.vibrator; +import android.annotation.Nullable; import android.app.ActivityManager; import android.app.IUidObserver; import android.content.Context; @@ -57,8 +58,6 @@ final class VibrationSettings { private final Object mLock = new Object(); private final Context mContext; - private final Vibrator mVibrator; - private final AudioManager mAudioManager; private final SettingsObserver mSettingObserver; @VisibleForTesting final UidObserver mUidObserver; @@ -67,6 +66,13 @@ final class VibrationSettings { private final List mListeners = new ArrayList<>(); private final SparseArray mFallbackEffects; + @GuardedBy("mLock") + @Nullable + private Vibrator mVibrator; + @GuardedBy("mLock") + @Nullable + private AudioManager mAudioManager; + @GuardedBy("mLock") private boolean mVibrateInputDevices; @GuardedBy("mLock") @@ -86,22 +92,9 @@ final class VibrationSettings { VibrationSettings(Context context, Handler handler) { mContext = context; - mVibrator = context.getSystemService(Vibrator.class); - mAudioManager = context.getSystemService(AudioManager.class); mSettingObserver = new SettingsObserver(handler); mUidObserver = new UidObserver(); - registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES)); - registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_WHEN_RINGING)); - registerSettingsObserver(Settings.Global.getUriFor(Settings.Global.APPLY_RAMPING_RINGER)); - registerSettingsObserver(Settings.Global.getUriFor(Settings.Global.ZEN_MODE)); - registerSettingsObserver( - Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_INTENSITY)); - registerSettingsObserver( - Settings.System.getUriFor(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)); - registerSettingsObserver( - Settings.System.getUriFor(Settings.System.RING_VIBRATION_INTENSITY)); - VibrationEffect clickEffect = createEffectFromResource( com.android.internal.R.array.config_virtualKeyVibePattern); VibrationEffect doubleClickEffect = VibrationEffect.createWaveform( @@ -119,6 +112,15 @@ final class VibrationSettings { mFallbackEffects.put(VibrationEffect.EFFECT_TEXTURE_TICK, VibrationEffect.get(VibrationEffect.EFFECT_TICK, false)); + // Update with current values from settings. + updateSettings(); + } + + public void onSystemReady() { + synchronized (mLock) { + mVibrator = mContext.getSystemService(Vibrator.class); + mAudioManager = mContext.getSystemService(AudioManager.class); + } try { ActivityManager.getService().registerUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE, @@ -148,7 +150,18 @@ final class VibrationSettings { } }); - // Update with current values from settings. + registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_INPUT_DEVICES)); + registerSettingsObserver(Settings.System.getUriFor(Settings.System.VIBRATE_WHEN_RINGING)); + registerSettingsObserver(Settings.Global.getUriFor(Settings.Global.APPLY_RAMPING_RINGER)); + registerSettingsObserver(Settings.Global.getUriFor(Settings.Global.ZEN_MODE)); + registerSettingsObserver( + Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_INTENSITY)); + registerSettingsObserver( + Settings.System.getUriFor(Settings.System.NOTIFICATION_VIBRATION_INTENSITY)); + registerSettingsObserver( + Settings.System.getUriFor(Settings.System.RING_VIBRATION_INTENSITY)); + + // Update with newly loaded services. updateSettings(); } @@ -178,17 +191,21 @@ final class VibrationSettings { * @return The vibration intensity, one of Vibrator.VIBRATION_INTENSITY_* */ public int getDefaultIntensity(int usageHint) { - if (isRingtone(usageHint)) { - return mVibrator.getDefaultRingVibrationIntensity(); - } else if (isNotification(usageHint)) { - return mVibrator.getDefaultNotificationVibrationIntensity(); - } else if (isHapticFeedback(usageHint)) { - return mVibrator.getDefaultHapticFeedbackIntensity(); - } else if (isAlarm(usageHint)) { + if (isAlarm(usageHint)) { return Vibrator.VIBRATION_INTENSITY_HIGH; - } else { - return Vibrator.VIBRATION_INTENSITY_MEDIUM; } + synchronized (mLock) { + if (mVibrator != null) { + if (isRingtone(usageHint)) { + return mVibrator.getDefaultRingVibrationIntensity(); + } else if (isNotification(usageHint)) { + return mVibrator.getDefaultNotificationVibrationIntensity(); + } else if (isHapticFeedback(usageHint)) { + return mVibrator.getDefaultHapticFeedbackIntensity(); + } + } + } + return Vibrator.VIBRATION_INTENSITY_MEDIUM; } /** @@ -234,8 +251,11 @@ final class VibrationSettings { if (!isRingtone(usageHint)) { return true; } - int ringerMode = mAudioManager.getRingerModeInternal(); synchronized (mLock) { + if (mAudioManager == null) { + return false; + } + int ringerMode = mAudioManager.getRingerModeInternal(); if (mVibrateWhenRinging) { return ringerMode != AudioManager.RINGER_MODE_SILENT; } else if (mApplyRampingRinger) { @@ -304,12 +324,12 @@ final class VibrationSettings { mVibrateWhenRinging = getSystemSetting(Settings.System.VIBRATE_WHEN_RINGING, 0) != 0; mApplyRampingRinger = getGlobalSetting(Settings.Global.APPLY_RAMPING_RINGER, 0) != 0; mHapticFeedbackIntensity = getSystemSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, - mVibrator.getDefaultHapticFeedbackIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_TOUCH)); mNotificationIntensity = getSystemSetting( Settings.System.NOTIFICATION_VIBRATION_INTENSITY, - mVibrator.getDefaultNotificationVibrationIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_NOTIFICATION)); mRingIntensity = getSystemSetting(Settings.System.RING_VIBRATION_INTENSITY, - mVibrator.getDefaultRingVibrationIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_RINGTONE)); mVibrateInputDevices = getSystemSetting(Settings.System.VIBRATE_INPUT_DEVICES, 0) > 0; mZenMode = getGlobalSetting(Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF); } @@ -346,15 +366,15 @@ final class VibrationSettings { proto.write(VibratorManagerServiceDumpProto.HAPTIC_FEEDBACK_INTENSITY, mHapticFeedbackIntensity); proto.write(VibratorManagerServiceDumpProto.HAPTIC_FEEDBACK_DEFAULT_INTENSITY, - mVibrator.getDefaultHapticFeedbackIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_TOUCH)); proto.write(VibratorManagerServiceDumpProto.NOTIFICATION_INTENSITY, mNotificationIntensity); proto.write(VibratorManagerServiceDumpProto.NOTIFICATION_DEFAULT_INTENSITY, - mVibrator.getDefaultNotificationVibrationIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_NOTIFICATION)); proto.write(VibratorManagerServiceDumpProto.RING_INTENSITY, mRingIntensity); proto.write(VibratorManagerServiceDumpProto.RING_DEFAULT_INTENSITY, - mVibrator.getDefaultRingVibrationIntensity()); + getDefaultIntensity(VibrationAttributes.USAGE_RINGTONE)); } } diff --git a/services/core/java/com/android/server/vibrator/VibratorManagerService.java b/services/core/java/com/android/server/vibrator/VibratorManagerService.java index 175085475b6cd..90a763c260f6d 100644 --- a/services/core/java/com/android/server/vibrator/VibratorManagerService.java +++ b/services/core/java/com/android/server/vibrator/VibratorManagerService.java @@ -127,9 +127,9 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { @GuardedBy("mLock") private ExternalVibrationHolder mCurrentExternalVibration; - private VibrationSettings mVibrationSettings; - private VibrationScaler mVibrationScaler; - private InputDeviceDelegate mInputDeviceDelegate; + private final VibrationSettings mVibrationSettings; + private final VibrationScaler mVibrationScaler; + private final InputDeviceDelegate mInputDeviceDelegate; private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override @@ -170,6 +170,10 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { mContext = context; mHandler = injector.createHandler(Looper.myLooper()); + mVibrationSettings = new VibrationSettings(mContext, mHandler); + mVibrationScaler = new VibrationScaler(mContext, mVibrationSettings); + mInputDeviceDelegate = new InputDeviceDelegate(mContext, mHandler); + VibrationCompleteListener listener = new VibrationCompleteListener(this); mNativeWrapper = injector.getNativeWrapper(); mNativeWrapper.init(listener); @@ -224,12 +228,12 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { Slog.v(TAG, "Initializing VibratorManager service..."); Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "systemReady"); try { - mVibrationSettings = new VibrationSettings(mContext, mHandler); - mVibrationScaler = new VibrationScaler(mContext, mVibrationSettings); - mInputDeviceDelegate = new InputDeviceDelegate(mContext, mHandler); + mVibrationSettings.onSystemReady(); + mInputDeviceDelegate.onSystemReady(); mVibrationSettings.addListener(this::updateServiceState); + // Will update settings and input devices. updateServiceState(); } finally { Slog.v(TAG, "VibratorManager service initialized"); diff --git a/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java b/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java index 8c62b7fe235ee..3ca90603e9d26 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/InputDeviceDelegateTest.java @@ -91,6 +91,7 @@ public class InputDeviceDelegateTest { mInputDeviceDelegate = new InputDeviceDelegate( mContextSpy, new Handler(mTestLooper.getLooper())); + mInputDeviceDelegate.onSystemReady(); } @After @@ -98,6 +99,24 @@ public class InputDeviceDelegateTest { InputManager.clearInstance(); } + @Test + public void beforeSystemReady_ignoresAnyUpdate() throws Exception { + when(mIInputManagerMock.getInputDeviceIds()).thenReturn(new int[0]); + InputDeviceDelegate inputDeviceDelegate = new InputDeviceDelegate( + mContextSpy, new Handler(mTestLooper.getLooper())); + + inputDeviceDelegate.updateInputDeviceVibrators(/* vibrateInputDevices= */ true); + assertFalse(inputDeviceDelegate.isAvailable()); + + inputDeviceDelegate.onInputDeviceAdded(1); + assertFalse(inputDeviceDelegate.isAvailable()); + + updateInputDevices(new int[]{1}); + assertFalse(inputDeviceDelegate.isAvailable()); + + verify(mIInputManagerMock, never()).getInputDevice(anyInt()); + } + @Test public void onInputDeviceAdded_withSettingsDisabled_ignoresNewDevice() throws Exception { when(mIInputManagerMock.getInputDeviceIds()).thenReturn(new int[0]); diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibrationScalerTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibrationScalerTest.java index 1e6ef91376863..b6c11fe62ff60 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibrationScalerTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibrationScalerTest.java @@ -88,6 +88,7 @@ public class VibrationScalerTest { mVibrationSettings = new VibrationSettings( mContextSpy, new Handler(mTestLooper.getLooper())); mVibrationScaler = new VibrationScaler(mContextSpy, mVibrationSettings); + mVibrationSettings.onSystemReady(); } @After diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibrationSettingsTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibrationSettingsTest.java index d8679876965cb..855012459bd68 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibrationSettingsTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibrationSettingsTest.java @@ -106,6 +106,7 @@ public class VibrationSettingsTest { mAudioManager = mContextSpy.getSystemService(AudioManager.class); mVibrationSettings = new VibrationSettings(mContextSpy, new Handler(mTestLooper.getLooper())); + mVibrationSettings.onSystemReady(); setUserSetting(Settings.System.VIBRATE_INPUT_DEVICES, 0); setUserSetting(Settings.System.VIBRATE_WHEN_RINGING, 0); @@ -161,6 +162,23 @@ public class VibrationSettingsTest { setGlobalSetting(Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_ALARMS); } + @Test + public void shouldVibrateForRingerMode_beforeSystemReady_returnsFalseOnlyForRingtone() { + setUserSetting(Settings.System.VIBRATE_WHEN_RINGING, 1); + setRingerMode(AudioManager.RINGER_MODE_MAX); + VibrationSettings vibrationSettings = new VibrationSettings(mContextSpy, + new Handler(mTestLooper.getLooper())); + + assertFalse(vibrationSettings.shouldVibrateForRingerMode( + VibrationAttributes.USAGE_RINGTONE)); + assertTrue(mVibrationSettings.shouldVibrateForRingerMode(VibrationAttributes.USAGE_ALARM)); + assertTrue(mVibrationSettings.shouldVibrateForRingerMode(VibrationAttributes.USAGE_TOUCH)); + assertTrue(mVibrationSettings.shouldVibrateForRingerMode( + VibrationAttributes.USAGE_NOTIFICATION)); + assertTrue(mVibrationSettings.shouldVibrateForRingerMode( + VibrationAttributes.USAGE_COMMUNICATION_REQUEST)); + } + @Test public void shouldVibrateForRingerMode_withoutRingtoneUsage_returnsTrue() { assertTrue(mVibrationSettings.shouldVibrateForRingerMode(VibrationAttributes.USAGE_ALARM)); @@ -302,6 +320,37 @@ public class VibrationSettingsTest { assertTrue(mVibrationSettings.isInZenMode()); } + @Test + public void getDefaultIntensity_beforeSystemReady_returnsMediumToAllExceptAlarm() { + mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_HIGH); + mFakeVibrator.setDefaultNotificationVibrationIntensity(Vibrator.VIBRATION_INTENSITY_HIGH); + mFakeVibrator.setDefaultRingVibrationIntensity(Vibrator.VIBRATION_INTENSITY_HIGH); + + setUserSetting(Settings.System.NOTIFICATION_VIBRATION_INTENSITY, + Vibrator.VIBRATION_INTENSITY_OFF); + setUserSetting(Settings.System.RING_VIBRATION_INTENSITY, + Vibrator.VIBRATION_INTENSITY_OFF); + setUserSetting(Settings.System.HAPTIC_FEEDBACK_INTENSITY, + Vibrator.VIBRATION_INTENSITY_OFF); + + VibrationSettings vibrationSettings = new VibrationSettings(mContextSpy, + new Handler(mTestLooper.getLooper())); + + assertEquals(Vibrator.VIBRATION_INTENSITY_HIGH, + vibrationSettings.getDefaultIntensity(VibrationAttributes.USAGE_ALARM)); + assertEquals(Vibrator.VIBRATION_INTENSITY_MEDIUM, + vibrationSettings.getDefaultIntensity(VibrationAttributes.USAGE_TOUCH)); + assertEquals(Vibrator.VIBRATION_INTENSITY_MEDIUM, + vibrationSettings.getDefaultIntensity(VibrationAttributes.USAGE_NOTIFICATION)); + assertEquals(Vibrator.VIBRATION_INTENSITY_MEDIUM, + vibrationSettings.getDefaultIntensity(VibrationAttributes.USAGE_UNKNOWN)); + assertEquals(Vibrator.VIBRATION_INTENSITY_MEDIUM, + vibrationSettings.getDefaultIntensity( + VibrationAttributes.USAGE_PHYSICAL_EMULATION)); + assertEquals(Vibrator.VIBRATION_INTENSITY_MEDIUM, + vibrationSettings.getDefaultIntensity(VibrationAttributes.USAGE_RINGTONE)); + } + @Test public void getDefaultIntensity_returnsIntensityFromVibratorService() { mFakeVibrator.setDefaultHapticFeedbackIntensity(Vibrator.VIBRATION_INTENSITY_HIGH); diff --git a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java index ba0a472c80ddc..a28d18fb74d3e 100644 --- a/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/vibrator/VibratorManagerServiceTest.java @@ -176,8 +176,14 @@ public class VibratorManagerServiceTest { LocalServices.removeServiceForTest(PowerManagerInternal.class); } + private VibratorManagerService createSystemReadyService() { + VibratorManagerService service = createService(); + service.systemReady(); + return service; + } + private VibratorManagerService createService() { - VibratorManagerService service = new VibratorManagerService( + return new VibratorManagerService( mContextSpy, new VibratorManagerService.Injector() { @Override @@ -201,8 +207,6 @@ public class VibratorManagerServiceTest { void addService(String name, IBinder service) { } }); - service.systemReady(); - return service; } @Test @@ -214,22 +218,45 @@ public class VibratorManagerServiceTest { assertTrue(mVibratorProviders.get(2).isInitialized()); } + @Test + public void createService_doNotCrashIfUsedBeforeSystemReady() { + mockVibrators(1, 2); + mVibratorProviders.get(1).setCapabilities(IVibrator.CAP_ALWAYS_ON_CONTROL); + mVibratorProviders.get(2).setCapabilities(IVibrator.CAP_ALWAYS_ON_CONTROL); + VibratorManagerService service = createService(); + + assertNotNull(service.getVibratorIds()); + assertNotNull(service.getVibratorInfo(1)); + assertFalse(service.isVibrating(1)); + + CombinedVibrationEffect effect = CombinedVibrationEffect.createSynced( + VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); + vibrate(service, effect, HAPTIC_FEEDBACK_ATTRS); + service.cancelVibrate(service); + + assertTrue(service.setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + + IVibratorStateListener listener = mockVibratorStateListener(); + assertTrue(service.registerVibratorStateListener(1, listener)); + assertTrue(service.unregisterVibratorStateListener(1, listener)); + } + @Test public void getVibratorIds_withNullResultFromNative_returnsEmptyArray() { when(mNativeWrapperMock.getVibratorIds()).thenReturn(null); - assertArrayEquals(new int[0], createService().getVibratorIds()); + assertArrayEquals(new int[0], createSystemReadyService().getVibratorIds()); } @Test public void getVibratorIds_withNonEmptyResultFromNative_returnsSameArray() { mockVibrators(2, 1); - assertArrayEquals(new int[]{2, 1}, createService().getVibratorIds()); + assertArrayEquals(new int[]{2, 1}, createSystemReadyService().getVibratorIds()); } @Test public void getVibratorInfo_withMissingVibratorId_returnsNull() { mockVibrators(1); - assertNull(createService().getVibratorInfo(2)); + assertNull(createSystemReadyService().getVibratorInfo(2)); } @Test @@ -239,7 +266,7 @@ public class VibratorManagerServiceTest { vibrator.setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS, IVibrator.CAP_AMPLITUDE_CONTROL); vibrator.setSupportedEffects(VibrationEffect.EFFECT_CLICK); vibrator.setSupportedPrimitives(VibrationEffect.Composition.PRIMITIVE_CLICK); - VibratorInfo info = createService().getVibratorInfo(1); + VibratorInfo info = createSystemReadyService().getVibratorInfo(1); assertNotNull(info); assertEquals(1, info.getId()); @@ -257,7 +284,7 @@ public class VibratorManagerServiceTest { @Test public void registerVibratorStateListener_callbacksAreTriggered() throws Exception { mockVibrators(1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); IVibratorStateListener listenerMock = mockVibratorStateListener(); service.registerVibratorStateListener(1, listenerMock); @@ -278,7 +305,7 @@ public class VibratorManagerServiceTest { @Test public void unregisterVibratorStateListener_callbackNotTriggeredAfter() throws Exception { mockVibrators(1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); IVibratorStateListener listenerMock = mockVibratorStateListener(); service.registerVibratorStateListener(1, listenerMock); @@ -303,7 +330,7 @@ public class VibratorManagerServiceTest { @Test public void registerVibratorStateListener_multipleVibratorsAreTriggered() throws Exception { mockVibrators(0, 1, 2); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); IVibratorStateListener[] listeners = new IVibratorStateListener[3]; for (int i = 0; i < 3; i++) { listeners[i] = mockVibratorStateListener(); @@ -330,7 +357,8 @@ public class VibratorManagerServiceTest { CombinedVibrationEffect effect = CombinedVibrationEffect.createSynced( VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); - assertTrue(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + assertTrue(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); VibrationEffect.Prebaked expectedEffect = new VibrationEffect.Prebaked( VibrationEffect.EFFECT_CLICK, false, VibrationEffect.EFFECT_STRENGTH_STRONG); @@ -353,7 +381,8 @@ public class VibratorManagerServiceTest { .addVibrator(2, VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)) .addVibrator(3, VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)) .combine(); - assertTrue(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + assertTrue(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); VibrationEffect.Prebaked expectedClick = new VibrationEffect.Prebaked( VibrationEffect.EFFECT_CLICK, false, VibrationEffect.EFFECT_STRENGTH_STRONG); @@ -376,9 +405,11 @@ public class VibratorManagerServiceTest { CombinedVibrationEffect effect = CombinedVibrationEffect.createSynced( VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); - assertTrue(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + assertTrue(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); - assertTrue(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, null, ALARM_ATTRS)); + assertTrue(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, null, ALARM_ATTRS)); assertNull(mVibratorProviders.get(1).getAlwaysOnEffect(1)); assertNull(mVibratorProviders.get(2).getAlwaysOnEffect(1)); @@ -392,7 +423,8 @@ public class VibratorManagerServiceTest { CombinedVibrationEffect effect = CombinedVibrationEffect.createSynced( VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE)); - assertFalse(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + assertFalse(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); assertNull(mVibratorProviders.get(1).getAlwaysOnEffect(1)); } @@ -405,7 +437,8 @@ public class VibratorManagerServiceTest { CombinedVibrationEffect effect = CombinedVibrationEffect.startSequential() .addNext(0, VibrationEffect.get(VibrationEffect.EFFECT_CLICK)) .combine(); - assertFalse(createService().setAlwaysOnEffect(UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); + assertFalse(createSystemReadyService().setAlwaysOnEffect( + UID, PACKAGE_NAME, 1, effect, ALARM_ATTRS)); assertNull(mVibratorProviders.get(1).getAlwaysOnEffect(1)); } @@ -413,7 +446,7 @@ public class VibratorManagerServiceTest { @Test public void setAlwaysOnEffect_withNoVibratorWithCapability_ignoresEffect() { mockVibrators(1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect mono = CombinedVibrationEffect.createSynced( VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)); @@ -435,18 +468,18 @@ public class VibratorManagerServiceTest { setRingerMode(AudioManager.RINGER_MODE_NORMAL); setUserSetting(Settings.System.VIBRATE_WHEN_RINGING, 0); setGlobalSetting(Settings.Global.APPLY_RAMPING_RINGER, 0); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); vibrate(service, VibrationEffect.createOneShot(40, 1), RINGTONE_ATTRS); setUserSetting(Settings.System.VIBRATE_WHEN_RINGING, 0); setGlobalSetting(Settings.Global.APPLY_RAMPING_RINGER, 1); - service = createService(); + service = createSystemReadyService(); vibrate(service, VibrationEffect.createOneShot(40, 10), RINGTONE_ATTRS); assertTrue(waitUntil(s -> s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); setUserSetting(Settings.System.VIBRATE_WHEN_RINGING, 1); setGlobalSetting(Settings.Global.APPLY_RAMPING_RINGER, 0); - service = createService(); + service = createSystemReadyService(); vibrate(service, VibrationEffect.createOneShot(40, 100), RINGTONE_ATTRS); assertTrue(waitUntil(s -> s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); @@ -459,7 +492,7 @@ public class VibratorManagerServiceTest { mockVibrators(1); FakeVibratorControllerProvider fakeVibrator = mVibratorProviders.get(1); fakeVibrator.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); mRegisteredPowerModeListener.onLowPowerModeChanged(LOW_POWER_STATE); vibrate(service, VibrationEffect.createOneShot(1, 1), HAPTIC_FEEDBACK_ATTRS); vibrate(service, VibrationEffect.createOneShot(2, 2), RINGTONE_ATTRS); @@ -480,7 +513,7 @@ public class VibratorManagerServiceTest { @Test public void vibrate_withAudioAttributes_usesOriginalAudioUsageInAppOpsManager() { - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); VibrationEffect effect = VibrationEffect.get(VibrationEffect.EFFECT_CLICK); AudioAttributes audioAttributes = new AudioAttributes.Builder() @@ -496,7 +529,7 @@ public class VibratorManagerServiceTest { @Test public void vibrate_withVibrationAttributes_usesCorrespondingAudioUsageInAppOpsManager() { - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); vibrate(service, VibrationEffect.get(VibrationEffect.EFFECT_CLICK), ALARM_ATTRS); vibrate(service, VibrationEffect.get(VibrationEffect.EFFECT_TICK), NOTIFICATION_ATTRS); @@ -534,7 +567,7 @@ public class VibratorManagerServiceTest { when(mIInputManagerMock.getVibratorIds(eq(1))).thenReturn(new int[]{1}); when(mIInputManagerMock.getInputDevice(eq(1))).thenReturn(createInputDeviceWithVibrator(1)); setUserSetting(Settings.System.VIBRATE_INPUT_DEVICES, 1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect effect = CombinedVibrationEffect.createSynced( VibrationEffect.createOneShot(10, 10)); @@ -550,7 +583,7 @@ public class VibratorManagerServiceTest { public void vibrate_withNativeCallbackTriggered_finishesVibration() throws Exception { mockVibrators(1); mVibratorProviders.get(1).setSupportedEffects(VibrationEffect.EFFECT_CLICK); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); // The native callback will be dispatched manually in this test. mTestLooper.stopAutoDispatchAndIgnoreExceptions(); @@ -573,7 +606,7 @@ public class VibratorManagerServiceTest { mockVibrators(1, 2); mVibratorProviders.get(1).setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); mVibratorProviders.get(2).setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); // The native callback will be dispatched manually in this test. mTestLooper.stopAutoDispatchAndIgnoreExceptions(); @@ -619,7 +652,7 @@ public class VibratorManagerServiceTest { FakeVibratorControllerProvider fakeVibrator1 = mVibratorProviders.get(1); fakeVibrator1.setSupportedEffects(VibrationEffect.EFFECT_CLICK); mVibratorProviders.get(2).setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect effect = CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.get(VibrationEffect.EFFECT_CLICK)) @@ -645,7 +678,7 @@ public class VibratorManagerServiceTest { mockVibrators(1, 2); FakeVibratorControllerProvider fakeVibrator1 = mVibratorProviders.get(1); fakeVibrator1.setSupportedEffects(VibrationEffect.EFFECT_CLICK); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect effect = CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.get(VibrationEffect.EFFECT_CLICK)) @@ -665,7 +698,7 @@ public class VibratorManagerServiceTest { mockCapabilities(IVibratorManager.CAP_SYNC, IVibratorManager.CAP_PREPARE_ON); mockVibrators(1, 2); when(mNativeWrapperMock.prepareSynced(any())).thenReturn(false); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect effect = CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.createOneShot(10, 50)) @@ -686,7 +719,7 @@ public class VibratorManagerServiceTest { mockVibrators(1, 2); when(mNativeWrapperMock.prepareSynced(eq(new int[]{1, 2}))).thenReturn(true); when(mNativeWrapperMock.triggerSynced(anyLong())).thenReturn(false); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); CombinedVibrationEffect effect = CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.createOneShot(10, 50)) @@ -716,7 +749,7 @@ public class VibratorManagerServiceTest { fakeVibrator.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL, IVibrator.CAP_COMPOSE_EFFECTS); fakeVibrator.setSupportedEffects(VibrationEffect.EFFECT_CLICK); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); vibrate(service, CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.get(VibrationEffect.EFFECT_CLICK)) @@ -762,7 +795,7 @@ public class VibratorManagerServiceTest { @Test public void vibrate_withPowerModeChange_cancelVibrationIfNotAllowed() throws Exception { mockVibrators(1, 2); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); vibrate(service, CombinedVibrationEffect.startSynced() .addVibrator(1, VibrationEffect.createOneShot(1000, 100)) @@ -780,7 +813,7 @@ public class VibratorManagerServiceTest { @Test public void vibrate_withSettingsChange_doNotCancelVibration() throws Exception { mockVibrators(1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); vibrate(service, VibrationEffect.createOneShot(1000, 100), HAPTIC_FEEDBACK_ATTRS); assertTrue(waitUntil(s -> s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); @@ -793,7 +826,7 @@ public class VibratorManagerServiceTest { @Test public void cancelVibrate_stopsVibrating() throws Exception { mockVibrators(1); - VibratorManagerService service = createService(); + VibratorManagerService service = createSystemReadyService(); service.cancelVibrate(service); assertFalse(service.isVibrating(1));