Specify vibrate attributes to resolve vibrate from background.
This commit is contained in:
parent
9a0bb243cd
commit
52fb873b1b
1 changed files with 18 additions and 1 deletions
|
@ -8,6 +8,8 @@ import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.VibrationAttributes;
|
||||||
|
import android.os.VibrationEffect;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
@ -30,6 +32,10 @@ public class IncomingRinger {
|
||||||
|
|
||||||
private MediaPlayer player;
|
private MediaPlayer player;
|
||||||
|
|
||||||
|
private final AudioAttributes audioAttributes = new AudioAttributes.Builder()
|
||||||
|
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
|
||||||
|
.build();
|
||||||
|
|
||||||
IncomingRinger(Context context) {
|
IncomingRinger(Context context) {
|
||||||
this.context = context.getApplicationContext();
|
this.context = context.getApplicationContext();
|
||||||
this.vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
this.vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||||
|
@ -49,7 +55,7 @@ public class IncomingRinger {
|
||||||
|
|
||||||
if (shouldVibrate(context, player, ringerMode, vibrate)) {
|
if (shouldVibrate(context, player, ringerMode, vibrate)) {
|
||||||
Log.i(TAG, "Starting vibration");
|
Log.i(TAG, "Starting vibration");
|
||||||
vibrator.vibrate(VIBRATE_PATTERN, 1);
|
startVibrate();
|
||||||
} else {
|
} else {
|
||||||
Log.i(TAG, "Skipping vibration");
|
Log.i(TAG, "Skipping vibration");
|
||||||
}
|
}
|
||||||
|
@ -83,6 +89,17 @@ public class IncomingRinger {
|
||||||
vibrator.cancel();
|
vibrator.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void startVibrate() {
|
||||||
|
if (Build.VERSION.SDK_INT >= 33) {
|
||||||
|
vibrator.vibrate(
|
||||||
|
VibrationEffect.createWaveform(VIBRATE_PATTERN, 1),
|
||||||
|
VibrationAttributes.createForUsage(VibrationAttributes.USAGE_RINGTONE)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
vibrator.vibrate(VIBRATE_PATTERN, 1, audioAttributes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overrides the ringer mode if we are on the right API level and have the right policy access.
|
* Overrides the ringer mode if we are on the right API level and have the right policy access.
|
||||||
* Checks the ringer volume to make sure we're not going to blast someone with their ringtone inadvertently.
|
* Checks the ringer volume to make sure we're not going to blast someone with their ringtone inadvertently.
|
||||||
|
|
Loading…
Add table
Reference in a new issue