Skip to content

Commit

Permalink
Fixed: Fix NullPointerException when running bell/vibrate on Samsung …
Browse files Browse the repository at this point in the history
…devices on android 8 and handled deprecated code

Apparently occurs on only Samsung android 8 devices and there is no fix for vibrator except catching the exception so that app doesn't crash.

https://gitlab.com/juanitobananas/wave-up/-/issues/131
overbound/SonicTimeTwisted#131
https://web.archive.org/web/20201114040257/https://www.badlogicgames.com/forum/viewtopic.php?t=28507

```
java.lang.NullPointerException: Attempt to read from field 'android.os.VibrationEffect com.android.server.VibratorService$Vibration.mEffect' on a null object reference
at android.os.Parcel.readException(Parcel.java:2035)
at android.os.Parcel.readException(Parcel.java:1975)
at android.os.IVibratorService$Stub$Proxy.vibrate(IVibratorService.java:292)
at android.os.SystemVibrator.vibrate(SystemVibrator.java:81)
at android.os.Vibrator.vibrate(Vibrator.java:191)
at android.os.Vibrator.vibrate(Vibrator.java:110)
at android.os.Vibrator.vibrate(Vibrator.java:89)
at com.termux.app.terminal.io.BellHandler$1.run(BellHandler.java:37)
at com.termux.app.terminal.io.BellHandler.doBell(BellHandler.java:55)
at com.termux.app.terminal.TermuxTerminalSessionClient.onBell(TermuxTerminalSessionClient.java:178)
at com.termux.terminal.TerminalSession.onBell(TerminalSession.java:278)
```
  • Loading branch information
agnostic-apollo committed Aug 20, 2021
1 parent 10704b1 commit 956e20e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/src/main/java/com/termux/app/terminal/io/BellHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.termux.app.terminal.io;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.os.VibrationEffect;
import android.os.Vibrator;

import com.termux.shared.logger.Logger;

public class BellHandler {
private static BellHandler instance = null;
private static final Object lock = new Object();

private static final String LOG_TAG = "BellHandler";

public static BellHandler getInstance(Context context) {
if (instance == null) {
synchronized (lock) {
Expand All @@ -34,7 +40,17 @@ private BellHandler(final Vibrator vibrator) {
@Override
public void run() {
if (vibrator != null) {
vibrator.vibrate(DURATION);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(DURATION);
}
} catch (Exception e) {
// Issue on samsung devices on android 8
// java.lang.NullPointerException: Attempt to read from field 'android.os.VibrationEffect com.android.server.VibratorService$Vibration.mEffect' on a null object reference
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to run vibrator", e);
}
}
}
};
Expand Down

0 comments on commit 956e20e

Please sign in to comment.