From 0ae397a5fcee18307d86f0dd4c7eccb591a0a754 Mon Sep 17 00:00:00 2001 From: n8fr8 Date: Tue, 13 Feb 2018 22:16:16 -0500 Subject: [PATCH] respect system volumes for notification and vibrate --- .../messenger/service/StatusBarNotifier.java | 31 ++++++++++++++++++- .../messenger/util/SoundService.java | 4 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/awesomeapp/messenger/service/StatusBarNotifier.java b/app/src/main/java/org/awesomeapp/messenger/service/StatusBarNotifier.java index e3618be0a..549679a79 100644 --- a/app/src/main/java/org/awesomeapp/messenger/service/StatusBarNotifier.java +++ b/app/src/main/java/org/awesomeapp/messenger/service/StatusBarNotifier.java @@ -44,14 +44,17 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.media.AudioManager; import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.SystemClock; import android.os.VibrationEffect; import android.os.Vibrator; +import android.provider.Settings; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; +import android.widget.Toast; import static org.awesomeapp.messenger.ImApp.NOTIFICATION_CHANNEL_ID_MESSAGE; @@ -391,7 +394,7 @@ public Notification createNotification(String tickerText, boolean lightWeightNot } - if (Preferences.getNotificationVibrate()) + if (Preferences.getNotificationVibrate() && isVibrateOn()) { // play vibration @@ -482,4 +485,30 @@ private boolean shouldSuppressSoundNotification() { public static String html2text(String html) { return Jsoup.parse(html).text(); }**/ + AudioManager mAudioManager; + + private boolean isVibrateOn () + { + if (mAudioManager == null) + mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + + int mode = mAudioManager.getRingerMode(); + + switch (mode) { + case AudioManager.RINGER_MODE_NORMAL: + if ((1 == Settings.System.getInt(mContext.getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0))) { + return true; + } else { + return false; + } + + case AudioManager.RINGER_MODE_SILENT: + return false; + + case AudioManager.RINGER_MODE_VIBRATE: + return true; + } + + return false; + } } diff --git a/app/src/main/java/org/awesomeapp/messenger/util/SoundService.java b/app/src/main/java/org/awesomeapp/messenger/util/SoundService.java index c6407d7d4..0f4c06f34 100644 --- a/app/src/main/java/org/awesomeapp/messenger/util/SoundService.java +++ b/app/src/main/java/org/awesomeapp/messenger/util/SoundService.java @@ -2,6 +2,7 @@ import android.app.Service; import android.content.Intent; +import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Build; @@ -49,9 +50,10 @@ private void startSound(String uriString) { return; } - // play sound + // play ringer sound if (mMediaPlayer == null) { mMediaPlayer = new MediaPlayer(); + mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) {