mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-15 03:21:13 +01:00
a1cb246e27
Previously, the DeviceCommunicationService was invoked directly, via Intent intent = new Intent(foo, bar); intent.setExtra(EXTRA_BAZ, baz); startService(...); and this was scattered throughout GadgetBridge. Now there is a "frontend" available, so that you can call the service more easily, like GBApplication.deviceService().connect(); For a start, this client interface (DeviceService) actually implements the same interface (EventHandler) as the receiving side (DeviceSupport). This may change in the future. This will also make testing much easier, because we can use this client interface to invoke the test service as well.
70 lines
2.7 KiB
Java
70 lines
2.7 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.telephony.TelephonyManager;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
|
|
|
|
|
|
public class PhoneCallReceiver extends BroadcastReceiver {
|
|
|
|
private static int mLastState = TelephonyManager.CALL_STATE_IDLE;
|
|
private static String mSavedNumber;
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
|
|
mSavedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
|
|
} else {
|
|
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
|
|
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
|
int state = 0;
|
|
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
|
|
state = TelephonyManager.CALL_STATE_IDLE;
|
|
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
|
|
state = TelephonyManager.CALL_STATE_OFFHOOK;
|
|
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
|
|
state = TelephonyManager.CALL_STATE_RINGING;
|
|
}
|
|
|
|
onCallStateChanged(context, state, number);
|
|
}
|
|
}
|
|
|
|
public void onCallStateChanged(Context context, int state, String number) {
|
|
if (mLastState == state) {
|
|
return;
|
|
}
|
|
|
|
ServiceCommand callCommand = null;
|
|
switch (state) {
|
|
case TelephonyManager.CALL_STATE_RINGING:
|
|
mSavedNumber = number;
|
|
callCommand = ServiceCommand.CALL_INCOMING;
|
|
break;
|
|
case TelephonyManager.CALL_STATE_OFFHOOK:
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
|
callCommand = ServiceCommand.CALL_START;
|
|
} else {
|
|
callCommand = ServiceCommand.CALL_OUTGOING;
|
|
}
|
|
break;
|
|
case TelephonyManager.CALL_STATE_IDLE:
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
|
//missed call would be correct here
|
|
callCommand = ServiceCommand.CALL_END;
|
|
} else {
|
|
callCommand = ServiceCommand.CALL_END;
|
|
}
|
|
break;
|
|
}
|
|
if (callCommand != null) {
|
|
GBApplication.deviceService().onSetCallState(mSavedNumber, null, callCommand);
|
|
}
|
|
mLastState = state;
|
|
}
|
|
}
|