From 8c1c7fbe6329898fd28604d7de38d62f96da47a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Mon, 6 Jun 2022 00:38:34 +0100 Subject: [PATCH] Add support for incoming call notification delay --- .../NotificationManagementActivity.java | 4 ++ .../externalevents/PhoneCallReceiver.java | 41 +++++++++++++++++-- app/src/main/res/values/strings.xml | 2 + .../res/xml/notifications_preferences.xml | 8 ++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/NotificationManagementActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/NotificationManagementActivity.java index a585eb0f9..8ac78bff3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/NotificationManagementActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/NotificationManagementActivity.java @@ -39,10 +39,14 @@ import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; +import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceManager; import android.provider.Settings; +import android.widget.EditText; + +import androidx.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java index d4922d543..a912cea18 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java @@ -24,6 +24,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.AudioManager; +import android.os.Handler; +import android.os.Looper; import android.telephony.TelephonyManager; import org.slf4j.Logger; @@ -42,12 +44,14 @@ public class PhoneCallReceiver extends BroadcastReceiver { private boolean mRestoreMutedCall = false; private int mLastRingerMode; + private final Handler delayHandler = new Handler(Looper.getMainLooper()); + @Override public void onReceive(Context context, Intent intent) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { mSavedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); - } else if(intent.getAction().equals("nodomain.freeyourgadget.gadgetbridge.MUTE_CALL")) { + } else if (intent.getAction().equals("nodomain.freeyourgadget.gadgetbridge.MUTE_CALL")) { // Handle the mute request only if the phone is currently ringing if (mLastState != TelephonyManager.CALL_STATE_RINGING) return; @@ -95,7 +99,7 @@ public class PhoneCallReceiver extends BroadcastReceiver { } else { callCommand = CallSpec.CALL_END; } - if(mRestoreMutedCall) { + if (mRestoreMutedCall) { mRestoreMutedCall = false; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.setRingerMode(mLastRingerMode); @@ -129,8 +133,39 @@ public class PhoneCallReceiver extends BroadcastReceiver { callSpec.number = mSavedNumber; callSpec.command = callCommand; callSpec.dndSuppressed = dndSuppressed; - GBApplication.deviceService().onSetCallState(callSpec); + + int callDelay = prefs.getInt("notification_delay_calls", 0); + if (callCommand == CallSpec.CALL_INCOMING) { + // Delay incoming call notifications by a configurable number of seconds + if (callDelay <= 0) { + GBApplication.deviceService().onSetCallState(callSpec); + } else { + scheduleOnSetCallState(callSpec, callDelay); + } + } else { + if (callCommand == CallSpec.CALL_START || callCommand == CallSpec.CALL_END) { + // Call started or ended, unschedule any outstanding notifications + unscheduleAllOnSetCallState(); + } + + // propagate the event to the device + GBApplication.deviceService().onSetCallState(callSpec); + } } mLastState = state; } + + private void scheduleOnSetCallState(final CallSpec callSpec, final int delaySeconds) { + final Runnable runnable = new Runnable() { + @Override public void run() { + GBApplication.deviceService().onSetCallState(callSpec); + } + }; + + delayHandler.postDelayed(runnable, delaySeconds * 1000); + } + + private void unscheduleAllOnSetCallState() { + delayHandler.removeCallbacksAndMessages(null); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3805107fe..e41050a0a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -163,6 +163,8 @@ Notifications Repetitions Phone Calls + Call notification delay + Delay before sending incoming call notifications to the device, in seconds. Enable VoIP app calls Ping tone SMS diff --git a/app/src/main/res/xml/notifications_preferences.xml b/app/src/main/res/xml/notifications_preferences.xml index 33f4ac10f..7f732a5ea 100644 --- a/app/src/main/res/xml/notifications_preferences.xml +++ b/app/src/main/res/xml/notifications_preferences.xml @@ -42,6 +42,14 @@ android:key="notification_support_voip_calls" android:layout="@layout/preference_checkbox" android:title="@string/pref_title_support_voip_calls" /> +