2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer
|
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-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.receivers;
|
2015-03-07 17:44:39 +01:00
|
|
|
|
2020-02-21 21:57:18 +01:00
|
|
|
import android.annotation.TargetApi;
|
2015-03-07 17:44:39 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-02-21 21:57:18 +01:00
|
|
|
import android.os.Build;
|
|
|
|
import android.telecom.TelecomManager;
|
2015-03-07 17:44:39 +01:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import com.android.internal.telephony.ITelephony;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.lang.reflect.Method;
|
2015-03-07 17:44:39 +01:00
|
|
|
|
2020-02-21 21:57:18 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-06-23 11:54:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
|
2015-03-07 17:44:39 +01:00
|
|
|
|
2015-03-26 12:06:26 +01:00
|
|
|
public class GBCallControlReceiver extends BroadcastReceiver {
|
2015-03-07 17:44:39 +01:00
|
|
|
public static final String ACTION_CALLCONTROL = "nodomain.freeyourgadget.gadgetbridge.callcontrol";
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GBCallControlReceiver.class);
|
2020-02-21 21:57:18 +01:00
|
|
|
private Context mContext = GBApplication.getContext();
|
2015-03-07 17:44:39 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-06-23 11:54:33 +02:00
|
|
|
GBDeviceEventCallControl.Event callCmd = GBDeviceEventCallControl.Event.values()[intent.getIntExtra("event", 0)];
|
2020-02-21 21:57:18 +01:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 28){
|
|
|
|
handleCallCmdTelecomManager(callCmd);
|
|
|
|
}else {
|
|
|
|
switch (callCmd) {
|
|
|
|
case END:
|
|
|
|
case REJECT:
|
|
|
|
case START:
|
|
|
|
try {
|
2015-03-07 17:44:39 +01:00
|
|
|
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
Class clazz = Class.forName(telephonyManager.getClass().getName());
|
|
|
|
Method method = clazz.getDeclaredMethod("getITelephony");
|
|
|
|
method.setAccessible(true);
|
|
|
|
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
|
2017-08-15 17:23:12 +02:00
|
|
|
if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) {
|
2015-03-17 21:42:31 +01:00
|
|
|
telephonyService.endCall();
|
|
|
|
} else {
|
|
|
|
telephonyService.answerRingingCall();
|
|
|
|
}
|
2020-02-21 21:57:18 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("could not start or hangup call");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(28)
|
|
|
|
public void handleCallCmdTelecomManager(GBDeviceEventCallControl.Event callCmd){
|
|
|
|
try {
|
|
|
|
TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
|
|
|
|
|
|
|
|
if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) {
|
|
|
|
tm.endCall();
|
|
|
|
}
|
|
|
|
else if (callCmd == GBDeviceEventCallControl.Event.START || callCmd == GBDeviceEventCallControl.Event.ACCEPT) {
|
|
|
|
tm.acceptRingingCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (SecurityException e){
|
|
|
|
LOG.warn("no permission to start or hangup call");
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOG.warn("could not start or hangup call");
|
2015-03-07 17:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|