2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2015-2021 Andreas Shimokawa, Andrzej Surowiec, Carsten
|
|
|
|
Pfeiffer, Daniel Dakhno
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-02-22 10:12:33 +01:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
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
|
|
|
|
2020-02-22 10:12:33 +01:00
|
|
|
if (GBApplication.isRunningPieOrLater()) {
|
2020-02-21 21:57:18 +01:00
|
|
|
handleCallCmdTelecomManager(callCmd);
|
2020-02-22 10:12:33 +01:00
|
|
|
} else {
|
2020-02-21 21:57:18 +01:00
|
|
|
switch (callCmd) {
|
|
|
|
case END:
|
|
|
|
case REJECT:
|
|
|
|
case START:
|
|
|
|
try {
|
2020-02-22 10:12:33 +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);
|
|
|
|
if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) {
|
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 10:12:33 +01:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.P)
|
|
|
|
public void handleCallCmdTelecomManager(GBDeviceEventCallControl.Event callCmd) {
|
2020-02-21 21:57:18 +01:00
|
|
|
try {
|
|
|
|
TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
|
|
|
|
|
|
|
|
if (callCmd == GBDeviceEventCallControl.Event.END || callCmd == GBDeviceEventCallControl.Event.REJECT) {
|
|
|
|
tm.endCall();
|
2020-02-22 10:12:33 +01:00
|
|
|
} else if (callCmd == GBDeviceEventCallControl.Event.START || callCmd == GBDeviceEventCallControl.Event.ACCEPT) {
|
2020-02-21 21:57:18 +01:00
|
|
|
tm.acceptRingingCall();
|
|
|
|
}
|
|
|
|
|
2020-02-22 10:12:33 +01:00
|
|
|
} catch (SecurityException e) {
|
2020-02-21 21:57:18 +01:00
|
|
|
LOG.warn("no permission to start or hangup call");
|
2020-02-22 10:12:33 +01:00
|
|
|
} catch (Exception e) {
|
2020-02-21 21:57:18 +01:00
|
|
|
LOG.warn("could not start or hangup call");
|
2015-03-07 17:44:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|