From 3d8ad8a0e374df4aeb74860e6a7ff4c56684ab63 Mon Sep 17 00:00:00 2001 From: Mihai Alexandru <77043862+MAJigsaw77@users.noreply.github.com> Date: Mon, 17 Jun 2024 09:48:22 +0300 Subject: [PATCH] Cancel the vibration when the app pauses or when is being destroyed. --- .../main/java/org/haxe/lime/GameActivity.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java b/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java index 3edf875cd..a1e855a66 100644 --- a/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java +++ b/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java @@ -33,6 +33,8 @@ public class GameActivity extends SDLActivity { public Handler handler; + protected Vibrator vibrator; + public static double getDisplayXDPI () { @@ -107,6 +109,8 @@ public class GameActivity extends SDLActivity { protected void onCreate (Bundle state) { + vibrator = (Vibrator)mSingleton.getSystemService (Context.VIBRATOR_SERVICE); + super.onCreate (state); assetManager = getAssets (); @@ -138,6 +142,14 @@ public class GameActivity extends SDLActivity { @Override protected void onDestroy () { + if (vibrator != null) { + + Log.d ("GameActivity", "Cancelling vibration"); + + vibrator.cancel (); + + } + for (Extension extension : extensions) { extension.onDestroy (); @@ -177,6 +189,14 @@ public class GameActivity extends SDLActivity { @Override protected void onPause () { + if (vibrator != null) { + + Log.d ("GameActivity", "Cancelling vibration"); + + vibrator.cancel (); + + } + super.onPause (); for (Extension extension : extensions) { @@ -371,9 +391,7 @@ public class GameActivity extends SDLActivity { public static void vibrate (int period, int duration) { - Vibrator v = (Vibrator)mSingleton.getSystemService (Context.VIBRATOR_SERVICE); - - if (v == null || !v.hasVibrator()) { + if (vibrator == null || !vibrator.hasVibrator ()) { return; @@ -383,7 +401,7 @@ public class GameActivity extends SDLActivity { if (period == 0) { - v.vibrate (VibrationEffect.createOneShot (duration, VibrationEffect.DEFAULT_AMPLITUDE)); + vibrator.vibrate (VibrationEffect.createOneShot (duration, VibrationEffect.DEFAULT_AMPLITUDE)); } else { @@ -397,7 +415,7 @@ public class GameActivity extends SDLActivity { } - v.vibrate (VibrationEffect.createWaveform (pattern, -1)); + vibrator.vibrate (VibrationEffect.createWaveform (pattern, -1)); } @@ -405,7 +423,7 @@ public class GameActivity extends SDLActivity { if (period == 0) { - v.vibrate (duration); + vibrator.vibrate (duration); } else { @@ -419,7 +437,7 @@ public class GameActivity extends SDLActivity { } - v.vibrate (pattern, -1); + vibrator.vibrate (pattern, -1); }