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 Mar 14, 2022
1 parent 916f321 commit 99080ba
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions app/src/main/java/com/termux/api/apis/VibrateAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

import com.termux.api.TermuxApiReceiver;
Expand All @@ -16,16 +18,34 @@ public class VibrateAPI {
public static void onReceive(TermuxApiReceiver apiReceiver, Context context, Intent intent) {
Logger.logDebug(LOG_TAG, "onReceive");

Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
int milliseconds = intent.getIntExtra("duration_ms", 1000);
boolean force = intent.getBooleanExtra("force", false);

AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT && !force) {
// Not vibrating since in silent mode and -f/--force option not used.
} else {
vibrator.vibrate(milliseconds);
}
new Thread() {
@Override
public void run() {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
int milliseconds = intent.getIntExtra("duration_ms", 1000);
boolean force = intent.getBooleanExtra("force", false);

AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am == null) {
Logger.logError(LOG_TAG, "Audio service null");
return;
}
// Do not vibrate if in silent mode and -f/--force option is not used.
if (am.getRingerMode() != AudioManager.RINGER_MODE_SILENT || force) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(milliseconds);
}
} 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);
}
}
}
}.start();

ResultReturner.noteDone(apiReceiver, intent);
}
Expand Down

0 comments on commit 99080ba

Please sign in to comment.