From dc22aabb1cdc0cbd73b572b495b6452819a4ca5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hler?= Date: Sat, 25 May 2019 15:56:21 +0200 Subject: [PATCH] Add support for muting an incoming call --- .../deviceevents/GBDeviceEventCallControl.java | 1 + .../externalevents/PhoneCallReceiver.java | 17 +++++++++++++++++ .../service/AbstractDeviceSupport.java | 7 +++++++ .../service/DeviceCommunicationService.java | 1 + .../casiogb6900/CasioGB6900DeviceSupport.java | 5 ++++- 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventCallControl.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventCallControl.java index 97e4f9d3d..e41887d2f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventCallControl.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/deviceevents/GBDeviceEventCallControl.java @@ -28,5 +28,6 @@ public class GBDeviceEventCallControl extends GBDeviceEvent { OUTGOING, REJECT, START, + IGNORE, } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java index dfedbbf16..e1d098bf0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java @@ -23,6 +23,7 @@ import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.media.AudioManager; import android.telephony.TelephonyManager; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -34,12 +35,23 @@ public class PhoneCallReceiver extends BroadcastReceiver { private static int mLastState = TelephonyManager.CALL_STATE_IDLE; private static String mSavedNumber; + private boolean mRestoreMutedCall = false; + private int mLastRingerMode; @Override public void onReceive(Context context, Intent intent) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { mSavedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); + } 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; } else { if (intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)) { String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); @@ -75,6 +87,11 @@ public class PhoneCallReceiver extends BroadcastReceiver { } else { callCommand = CallSpec.CALL_END; } + if(mRestoreMutedCall) { + mRestoreMutedCall = false; + AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + audioManager.setRingerMode(mLastRingerMode); + } break; } if (callCommand != CallSpec.CALL_UNDEFINED) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index 69d6ed0f7..859fb8271 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -198,6 +198,13 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { private void handleGBDeviceEvent(GBDeviceEventCallControl callEvent) { Context context = getContext(); LOG.info("Got event for CALL_CONTROL"); + if(callEvent.event == GBDeviceEventCallControl.Event.IGNORE) { + LOG.info("Sending intent for mute"); + Intent broadcastIntent = new Intent("nodomain.freeyourgadget.gadgetbridge.MUTE_CALL"); + broadcastIntent.setPackage(context.getPackageName()); + context.sendBroadcast(broadcastIntent); + return; + } Intent callIntent = new Intent(GBCallControlReceiver.ACTION_CALLCONTROL); callIntent.putExtra("event", callEvent.event.ordinal()); callIntent.setPackage(context.getPackageName()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 68ab0c5f7..1a7876332 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -686,6 +686,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.PHONE_STATE"); filter.addAction("android.intent.action.NEW_OUTGOING_CALL"); + filter.addAction("nodomain.freeyourgadget.gadgetbridge.MUTE_CALL"); registerReceiver(mPhoneCallReceiver, filter); } if (mSMSReceiver == null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java index 877f7c170..d42233a0f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casiogb6900/CasioGB6900DeviceSupport.java @@ -35,6 +35,7 @@ import java.util.Calendar; import java.util.UUID; import java.util.concurrent.TimeUnit; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.devices.casiogb6900.CasioGB6900Constants; @@ -437,7 +438,9 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport { if(characteristicUUID.equals(CasioGB6900Constants.RINGER_CONTROL_POINT)) { if(data[0] == 0x02) { - LOG.info("Mute/ignore call event not yet supported by GB"); + GBDeviceEventCallControl callControlEvent = new GBDeviceEventCallControl(); + callControlEvent.event = GBDeviceEventCallControl.Event.IGNORE; + evaluateGBDeviceEvent(callControlEvent); } handled = true; }