From 6c7958ca9aa39dcec2ba2d44a9abc22ca7f65221 Mon Sep 17 00:00:00 2001 From: Lucas Dupin Date: Mon, 14 Nov 2022 11:21:01 -0800 Subject: [PATCH] Move VibratorHelper to a single threaded executor VibratorHelper has been using the @Background Executor, but the background thread has often been saturated. This causes vibrations to be delayed. Ideally we need a oneway call to Vibrator, that doesn't block our main thread, but until then we'll use a single thread Executor. Bug: 245528624 Test: manual Test: atest VibratorHelperTest Change-Id: I7349c24c1c6ed5cfa658e7055126103f4d16dee6 --- .../systemui/statusbar/VibratorHelper.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java index c070fccf98086..324e97294f4ea 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/VibratorHelper.java @@ -24,17 +24,21 @@ import android.os.VibrationAttributes; import android.os.VibrationEffect; import android.os.Vibrator; +import androidx.annotation.VisibleForTesting; + import com.android.systemui.dagger.SysUISingleton; -import com.android.systemui.dagger.qualifiers.Background; import org.jetbrains.annotations.NotNull; import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import javax.inject.Inject; /** - * + * A Helper class that offloads {@link Vibrator} calls to a different thread. + * {@link Vibrator} makes blocking calls that may cause SysUI to ANR. + * TODO(b/245528624): Use regular Vibrator instance once new APIs are available. */ @SysUISingleton public class VibratorHelper { @@ -53,10 +57,18 @@ public class VibratorHelper { private final Executor mExecutor; /** - * + * Creates a vibrator helper on a new single threaded {@link Executor}. */ @Inject - public VibratorHelper(@Nullable Vibrator vibrator, @Background Executor executor) { + public VibratorHelper(@Nullable Vibrator vibrator) { + this(vibrator, Executors.newSingleThreadExecutor()); + } + + /** + * Creates new vibrator helper on a specific {@link Executor}. + */ + @VisibleForTesting + public VibratorHelper(@Nullable Vibrator vibrator, Executor executor) { mExecutor = executor; mVibrator = vibrator; }