From e54c911b102205ea5b8e4eb81ff0b22dc972cd95 Mon Sep 17 00:00:00 2001 From: Nikolas Havrikov Date: Thu, 30 Mar 2023 18:07:34 +0200 Subject: [PATCH] Only wait for successful vibration Currently, the device sleeps for 500ms even if no vibrator is available, such as on a TV device. This CL ensures the sleep happens only if the vibration is happening successfully. MUST_SLEEP because this is just moving already exiting code. Test: atest FrameworksServicesTests:VibratorManagerServiceTest Bug: 276697207 Change-Id: I14182e43ad1fe08c2276be7988f2ac1fb3d79634 Merged-In: I14182e43ad1fe08c2276be7988f2ac1fb3d79634 --- .../com/android/server/power/ShutdownThread.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/power/ShutdownThread.java b/services/core/java/com/android/server/power/ShutdownThread.java index c2d4ac694c397..b1430e7138e79 100644 --- a/services/core/java/com/android/server/power/ShutdownThread.java +++ b/services/core/java/com/android/server/power/ShutdownThread.java @@ -703,17 +703,20 @@ public final class ShutdownThread extends Thread { // vibrate before shutting down Vibrator vibrator = new SystemVibrator(context); try { - vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES); + if (vibrator.hasVibrator()) { + vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES); + // vibrator is asynchronous so we need to wait to avoid shutting down too soon. + try { + Thread.sleep(SHUTDOWN_VIBRATE_MS); + } catch (InterruptedException unused) { + // this is not critical and does not require logging + } + } } catch (Exception e) { // Failure to vibrate shouldn't interrupt shutdown. Just log it. Log.w(TAG, "Failed to vibrate during shutdown.", e); } - // vibrator is asynchronous so we need to wait to avoid shutting down too soon. - try { - Thread.sleep(SHUTDOWN_VIBRATE_MS); - } catch (InterruptedException unused) { - } } // Shutdown power Log.i(TAG, "Performing low-level shutdown...");