1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-08 22:28:20 +02:00

Add support for incoming call notification delay

This commit is contained in:
José Rebelo 2022-06-06 00:38:34 +01:00 committed by Gitea
parent 9cb6403c04
commit 8c1c7fbe63
4 changed files with 52 additions and 3 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -163,6 +163,8 @@
<string name="pref_header_notifications">Notifications</string>
<string name="pref_title_notifications_repetitions">Repetitions</string>
<string name="pref_title_notifications_call">Phone Calls</string>
<string name="pref_title_notification_delay_calls">Call notification delay</string>
<string name="pref_summary_notification_delay_calls">Delay before sending incoming call notifications to the device, in seconds.</string>
<string name="pref_title_support_voip_calls">Enable VoIP app calls</string>
<string name="pref_title_ping_tone">Ping tone</string>
<string name="pref_title_notifications_sms">SMS</string>

View File

@ -42,6 +42,14 @@
android:key="notification_support_voip_calls"
android:layout="@layout/preference_checkbox"
android:title="@string/pref_title_support_voip_calls" />
<EditTextPreference
android:defaultValue="0"
android:inputType="number"
android:digits="0123456789"
android:key="notification_delay_calls"
android:maxLength="2"
android:title="@string/pref_title_notification_delay_calls"
android:summary="@string/pref_summary_notification_delay_calls" />
<ListPreference
android:defaultValue="when_screen_off"