2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Böhler, Andreas Shimokawa, Carsten
|
|
|
|
Pfeiffer, Daniele Gobbetti, Johannes Tysiak, José Rebelo, mvn23, Normano64,
|
|
|
|
Taavi Eomäe
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-01-22 22:49:50 +01:00
|
|
|
|
2016-05-19 16:34:59 +02:00
|
|
|
import android.app.NotificationManager;
|
2016-05-19 23:58:13 +02:00
|
|
|
import android.app.NotificationManager.Policy;
|
2018-05-16 18:56:36 +02:00
|
|
|
import android.app.Service;
|
2015-01-22 22:49:50 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2019-05-25 15:56:21 +02:00
|
|
|
import android.media.AudioManager;
|
2022-06-06 01:38:34 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2015-01-22 22:49:50 +01:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
2020-07-27 18:15:33 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-02-07 12:58:18 +01:00
|
|
|
|
2015-01-22 22:49:50 +01:00
|
|
|
public class PhoneCallReceiver extends BroadcastReceiver {
|
2020-07-27 18:15:33 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(PhoneCallReceiver.class);
|
2015-01-22 22:49:50 +01:00
|
|
|
|
2015-02-07 12:58:18 +01:00
|
|
|
private static int mLastState = TelephonyManager.CALL_STATE_IDLE;
|
|
|
|
private static String mSavedNumber;
|
2019-05-25 15:56:21 +02:00
|
|
|
private boolean mRestoreMutedCall = false;
|
|
|
|
private int mLastRingerMode;
|
2015-02-07 12:58:18 +01:00
|
|
|
|
2022-06-06 01:38:34 +02:00
|
|
|
private final Handler delayHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
2015-01-22 22:49:50 +01:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2018-05-16 18:56:36 +02:00
|
|
|
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
|
2015-02-07 12:58:18 +01:00
|
|
|
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
|
|
|
|
mSavedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
|
2022-06-06 01:38:34 +02:00
|
|
|
} else if (intent.getAction().equals("nodomain.freeyourgadget.gadgetbridge.MUTE_CALL")) {
|
2019-05-25 15:56:21 +02:00
|
|
|
// Handle the mute request only if the phone is currently ringing
|
2020-07-27 18:15:33 +02:00
|
|
|
if (mLastState != TelephonyManager.CALL_STATE_RINGING)
|
2019-05-25 15:56:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
mLastRingerMode = audioManager.getRingerMode();
|
2020-07-27 18:15:33 +02:00
|
|
|
try {
|
|
|
|
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
LOG.error("SecurityException when trying to set ringer (no permission granted :/ ?), not setting it then.");
|
|
|
|
}
|
2019-05-25 15:56:21 +02:00
|
|
|
mRestoreMutedCall = true;
|
2015-02-07 12:58:18 +01:00
|
|
|
} else {
|
2018-09-05 20:41:01 +02:00
|
|
|
if (intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)) {
|
|
|
|
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
|
|
|
int state = tm.getCallState();
|
|
|
|
onCallStateChanged(context, state, number);
|
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onCallStateChanged(Context context, int state, String number) {
|
|
|
|
if (mLastState == state) {
|
|
|
|
return;
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 20:08:34 +02:00
|
|
|
int callCommand = CallSpec.CALL_UNDEFINED;
|
2015-02-07 12:58:18 +01:00
|
|
|
switch (state) {
|
|
|
|
case TelephonyManager.CALL_STATE_RINGING:
|
|
|
|
mSavedNumber = number;
|
2016-04-04 20:08:34 +02:00
|
|
|
callCommand = CallSpec.CALL_INCOMING;
|
2015-02-07 12:58:18 +01:00
|
|
|
break;
|
|
|
|
case TelephonyManager.CALL_STATE_OFFHOOK:
|
|
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
2016-04-04 20:08:34 +02:00
|
|
|
callCommand = CallSpec.CALL_START;
|
2015-02-07 12:58:18 +01:00
|
|
|
} else {
|
2016-04-04 20:08:34 +02:00
|
|
|
callCommand = CallSpec.CALL_OUTGOING;
|
2016-02-20 22:20:02 +01:00
|
|
|
mSavedNumber = number;
|
2015-02-07 12:58:18 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TelephonyManager.CALL_STATE_IDLE:
|
2015-02-07 13:20:38 +01:00
|
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
2015-02-12 16:00:45 +01:00
|
|
|
//missed call would be correct here
|
2016-04-04 20:08:34 +02:00
|
|
|
callCommand = CallSpec.CALL_END;
|
2015-02-08 23:53:40 +01:00
|
|
|
} else {
|
2016-04-04 20:08:34 +02:00
|
|
|
callCommand = CallSpec.CALL_END;
|
2015-02-07 13:20:38 +01:00
|
|
|
}
|
2022-06-06 01:38:34 +02:00
|
|
|
if (mRestoreMutedCall) {
|
2019-05-25 15:56:21 +02:00
|
|
|
mRestoreMutedCall = false;
|
|
|
|
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
audioManager.setRingerMode(mLastRingerMode);
|
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-04-04 20:08:34 +02:00
|
|
|
if (callCommand != CallSpec.CALL_UNDEFINED) {
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
if ("never".equals(prefs.getString("notification_mode_calls", "always"))) {
|
2015-09-20 22:04:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-05-15 16:51:41 +02:00
|
|
|
int dndSuppressed = 0;
|
2016-05-19 23:58:13 +02:00
|
|
|
switch (GBApplication.getGrantedInterruptionFilter()) {
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALL:
|
|
|
|
break;
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALARMS:
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_NONE:
|
2022-05-15 16:51:41 +02:00
|
|
|
dndSuppressed = 1;
|
|
|
|
break;
|
2016-05-19 23:58:13 +02:00
|
|
|
case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
|
|
|
|
if (GBApplication.isPriorityNumber(Policy.PRIORITY_CATEGORY_CALLS, mSavedNumber)) {
|
|
|
|
break;
|
2016-05-19 16:34:59 +02:00
|
|
|
}
|
2016-05-19 23:58:13 +02:00
|
|
|
// FIXME: Handle Repeat callers if it is enabled in Do Not Disturb
|
2022-05-15 16:51:41 +02:00
|
|
|
dndSuppressed = 1;
|
|
|
|
}
|
|
|
|
if (prefs.getBoolean("notification_filter", false) && dndSuppressed == 1) {
|
|
|
|
return;
|
2016-05-19 16:34:59 +02:00
|
|
|
}
|
2016-04-04 20:08:34 +02:00
|
|
|
CallSpec callSpec = new CallSpec();
|
|
|
|
callSpec.number = mSavedNumber;
|
|
|
|
callSpec.command = callCommand;
|
2022-05-15 16:51:41 +02:00
|
|
|
callSpec.dndSuppressed = dndSuppressed;
|
2022-06-06 01:38:34 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
mLastState = state;
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
2022-06-06 01:38:34 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|