From 77f86521778ef28149f1806a5782ce37fc2f5314 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 3 Aug 2018 11:33:02 +0200 Subject: [PATCH] Fix #210 Signed-off-by: Mario Danic --- app/src/main/AndroidManifest.xml | 1 + .../CallNotificationController.java | 47 ++++++++++++++++++- .../talk/controllers/SettingsController.java | 11 ++++- .../nextcloud/talk/jobs/NotificationJob.java | 16 +++++++ .../talk/utils/DoNotDisturbUtils.java | 26 ++++++++++ .../utils/preferences/AppPreferences.java | 12 +++++ .../main/res/layout/controller_settings.xml | 9 ++++ app/src/main/res/values/strings.xml | 4 ++ 8 files changed, 123 insertions(+), 3 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7fbe05f5b..7f48e8b83 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,6 +26,7 @@ + = Build.VERSION_CODES.O) { + if (vibrator.hasAmplitudeControl()) { + vibrationEffect = VibrationEffect.createWaveform(vibratePattern, amplitudes, 0); + vibrator.vibrate(vibrationEffect); + } else { + vibrationEffect = VibrationEffect.createWaveform(vibratePattern, 0); + vibrator.vibrate(vibrationEffect); + } + } else { + vibrator.vibrate(vibratePattern, 0); + } + } + + handler.postDelayed(() -> { + if (vibrator != null) { + vibrator.cancel(); + } + }, 10000); + } } @@ -429,7 +466,7 @@ public class CallNotificationController extends BaseController { } } - private void endMediaPlayer() { + private void endMediaAndVibratorNotifications() { if (mediaPlayer != null) { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); @@ -438,14 +475,20 @@ public class CallNotificationController extends BaseController { mediaPlayer.release(); mediaPlayer = null; } + + if (vibrator != null) { + vibrator.cancel(); + } } @Override public void onDestroy() { AvatarStatusCodeHolder.getInstance().setStatusCode(0); leavingScreen = true; + handler.removeCallbacksAndMessages(null); + handler = null; dispose(); - endMediaPlayer(); + endMediaAndVibratorNotifications(); super.onDestroy(); } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java b/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java index 584193243..c37292d21 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java @@ -54,17 +54,19 @@ import com.nextcloud.talk.jobs.AccountRemovalJob; import com.nextcloud.talk.models.RingtoneSettings; import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.utils.ApiUtils; -import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder; +import com.nextcloud.talk.utils.DoNotDisturbUtils; import com.nextcloud.talk.utils.bundle.BundleKeys; import com.nextcloud.talk.utils.database.user.UserUtils; import com.nextcloud.talk.utils.glide.GlideApp; import com.nextcloud.talk.utils.preferences.AppPreferences; import com.nextcloud.talk.utils.preferences.MagicUserInputModule; +import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder; import com.yarolegovich.mp.MaterialChoicePreference; import com.yarolegovich.mp.MaterialEditTextPreference; import com.yarolegovich.mp.MaterialPreferenceCategory; import com.yarolegovich.mp.MaterialPreferenceScreen; import com.yarolegovich.mp.MaterialStandardPreference; +import com.yarolegovich.mp.MaterialSwitchPreference; import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; @@ -146,6 +148,9 @@ public class SettingsController extends BaseController { @BindView(R.id.settings_client_cert) MaterialStandardPreference certificateSetup; + @BindView(R.id.settings_always_vibrate) + MaterialSwitchPreference shouldVibrateSwitchPreference; + @BindView(R.id.message_text) TextView messageText; @@ -207,6 +212,10 @@ public class SettingsController extends BaseController { licenceButton.setVisibility(View.GONE); } + if (!DoNotDisturbUtils.hasVibrator()) { + shouldVibrateSwitchPreference.setVisibility(View.GONE); + } + if (!TextUtils.isEmpty(getResources().getString(R.string.nc_privacy_url))) { privacyButton.addPreferenceClickListener(view12 -> { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources(). diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java index 7cd1f87b9..f82da5ef7 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java @@ -27,10 +27,13 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.VibrationEffect; +import android.os.Vibrator; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Base64; @@ -349,9 +352,22 @@ public class NotificationJob extends Job { if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && DoNotDisturbUtils.shouldPlaySound()) { MediaPlayer mediaPlayer = MediaPlayer.create(context, soundUri); + mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(MediaPlayer::release); + } + + if (DoNotDisturbUtils.shouldVibrate(appPreferences.getShouldVibrateSetting())) { + Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + + if (vibrator != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); + } else { + vibrator.vibrate(500); + } + } } } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/DoNotDisturbUtils.java b/app/src/main/java/com/nextcloud/talk/utils/DoNotDisturbUtils.java index 0d793f4a6..6d875e0d1 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/DoNotDisturbUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/DoNotDisturbUtils.java @@ -24,6 +24,7 @@ import android.app.NotificationManager; import android.content.Context; import android.media.AudioManager; import android.os.Build; +import android.os.Vibrator; import com.nextcloud.talk.application.NextcloudTalkApplication; @@ -49,6 +50,31 @@ public class DoNotDisturbUtils { shouldPlaySound = false; } } + return shouldPlaySound; } + + public static boolean hasVibrator() { + Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext(); + Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + return (vibrator != null && vibrator.hasVibrator()); + } + + public static boolean shouldVibrate(boolean vibrate) { + + if (hasVibrator()) { + Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext(); + AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + + if (audioManager != null) { + if (vibrate) { + return (audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT); + } else { + return (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE); + } + } + } + + return false; + } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java index d8dae8e99..4a860c544 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -21,6 +21,7 @@ package com.nextcloud.talk.utils.preferences; import net.orange_box.storebox.annotations.method.ClearMethod; +import net.orange_box.storebox.annotations.method.DefaultValue; import net.orange_box.storebox.annotations.method.KeyByString; import net.orange_box.storebox.annotations.method.RegisterChangeListenerMethod; import net.orange_box.storebox.annotations.method.RemoveMethod; @@ -178,6 +179,17 @@ public interface AppPreferences { @RemoveMethod void removeMessagesNotificationChannelUpgradeToV3(); + @KeyByString("notifications_vibrate") + @DefaultValue(1) + boolean getShouldVibrateSetting(); + + @KeyByString("notifications_vibrate") + void setVibrateSetting(boolean value); + + @KeyByString("notifications_vibrate") + @RemoveMethod + void removeVibrateSetting(); + @ClearMethod void clear(); } diff --git a/app/src/main/res/layout/controller_settings.xml b/app/src/main/res/layout/controller_settings.xml index a5bb33273..53ef4e068 100644 --- a/app/src/main/res/layout/controller_settings.xml +++ b/app/src/main/res/layout/controller_settings.xml @@ -132,6 +132,15 @@ apc:mp_key="@string/nc_settings_message_ringtone_key" apc:mp_title="@string/nc_settings_other_notifications_ringtone"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 22e5e34cd..a2245204a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -67,6 +67,10 @@ message_ringtone Librem by feandesign No sound + Vibrate + When enabled, phone will vibrate unless it\'s silenced + notifications_vibrate + No proxy Username