2019-11-23 21:52:46 +01:00
|
|
|
/* Copyright (C) 2015-2019 Andreas Böhler, Andreas Shimokawa, Carsten
|
|
|
|
Pfeiffer, Daniele Gobbetti, Johannes Tysiak, Normano64
|
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
|
|
|
|
along with this program. If not, see <http://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;
|
2015-01-22 22:49:50 +01:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
|
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");
|
2019-05-25 15:56:21 +02:00
|
|
|
} 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;
|
|
|
|
|
|
|
|
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
mLastRingerMode = audioManager.getRingerMode();
|
|
|
|
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
|
|
|
|
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
|
|
|
}
|
2019-05-25 15:56:21 +02:00
|
|
|
if(mRestoreMutedCall) {
|
|
|
|
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;
|
|
|
|
}
|
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:
|
|
|
|
return;
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|