From dd1c872287287afc6fb8a40405091224bef18270 Mon Sep 17 00:00:00 2001 From: Efra Espada Date: Wed, 8 Sep 2021 12:23:41 +0200 Subject: [PATCH] [FIX] Duplicated call onAnswer. New open method. (#111) * feature: open method openPhoneAccounts * feature: fixed duplicated call onAnswer on VoiceConnection * feature: added support for drawables in notification icon in foreground service --- README.md | 2 +- .../flutter_callkeep/FlutterCallkeepPlugin.java | 2 +- .../main/java/io/wazo/callkeep/CallKeepModule.java | 8 ++++++-- .../main/java/io/wazo/callkeep/VoiceConnection.java | 13 +++---------- .../io/wazo/callkeep/VoiceConnectionService.java | 12 +++++++++++- lib/src/api.dart | 4 ++++ 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 216ec3e..30137f1 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ final callSetup = { 'channelId': 'com.company.my', 'channelName': 'Foreground service for my app', 'notificationTitle': 'My app is running on background', - 'notificationIcon': 'Path to the resource icon of the notification', + 'notificationIcon': 'mipmap/ic_notification_launcher', }, }, }; diff --git a/android/src/main/java/com/github/cloudwebrtc/flutter_callkeep/FlutterCallkeepPlugin.java b/android/src/main/java/com/github/cloudwebrtc/flutter_callkeep/FlutterCallkeepPlugin.java index d9d5c2d..9402ed1 100644 --- a/android/src/main/java/com/github/cloudwebrtc/flutter_callkeep/FlutterCallkeepPlugin.java +++ b/android/src/main/java/com/github/cloudwebrtc/flutter_callkeep/FlutterCallkeepPlugin.java @@ -66,7 +66,7 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin @Override public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { - if (!callKeep.HandleMethodCall(call, result)) { + if (!callKeep.handleMethodCall(call, result)) { result.notImplemented(); } } diff --git a/android/src/main/java/io/wazo/callkeep/CallKeepModule.java b/android/src/main/java/io/wazo/callkeep/CallKeepModule.java index 081e559..67d323e 100644 --- a/android/src/main/java/io/wazo/callkeep/CallKeepModule.java +++ b/android/src/main/java/io/wazo/callkeep/CallKeepModule.java @@ -97,11 +97,13 @@ public void setActivity(Activity activity) { } public void dispose(){ + if (voiceBroadcastReceiver == null || this._context == null) return; LocalBroadcastManager.getInstance(this._context).unregisterReceiver(voiceBroadcastReceiver); VoiceConnectionService.setPhoneAccountHandle(null); + isReceiverRegistered = false; } - public boolean HandleMethodCall(@NonNull MethodCall call, @NonNull Result result) { + public boolean handleMethodCall(@NonNull MethodCall call, @NonNull Result result) { switch(call.method) { case "setup": { setup(new ConstraintsMap((Map)call.argument("options"))); @@ -219,6 +221,9 @@ public boolean HandleMethodCall(@NonNull MethodCall call, @NonNull Result result } public void setup(ConstraintsMap options) { + if (isReceiverRegistered) { + return; + } VoiceConnectionService.setAvailable(false); this._settings = options; if (isConnectionServiceAvailable()) { @@ -243,7 +248,6 @@ public void registerEvents() { if (!isConnectionServiceAvailable()) { return; } - voiceBroadcastReceiver = new VoiceBroadcastReceiver(); registerReceiver(); VoiceConnectionService.setPhoneAccountHandle(handle); diff --git a/android/src/main/java/io/wazo/callkeep/VoiceConnection.java b/android/src/main/java/io/wazo/callkeep/VoiceConnection.java index 58a3044..f58919f 100644 --- a/android/src/main/java/io/wazo/callkeep/VoiceConnection.java +++ b/android/src/main/java/io/wazo/callkeep/VoiceConnection.java @@ -83,27 +83,20 @@ public void onCallAudioStateChanged(CallAudioState state) { public void onAnswer() { super.onAnswer(); Log.d(TAG, "onAnswer called"); - - setConnectionCapabilities(getConnectionCapabilities() | Connection.CAPABILITY_HOLD); - setAudioModeIsVoip(true); - - sendCallRequestToActivity(ACTION_ANSWER_CALL, handle); - sendCallRequestToActivity(ACTION_AUDIO_SESSION, handle); - Log.d(TAG, "onAnswer executed"); + Log.d(TAG, "onAnswer ignored"); } - @Override public void onAnswer(int videoState) { super.onAnswer(videoState); - Log.d(TAG, "onAnswer called"); + Log.d(TAG, "onAnswer videoState called: " + videoState); setConnectionCapabilities(getConnectionCapabilities() | Connection.CAPABILITY_HOLD); setAudioModeIsVoip(true); sendCallRequestToActivity(ACTION_ANSWER_CALL, handle); sendCallRequestToActivity(ACTION_AUDIO_SESSION, handle); - Log.d(TAG, "onAnswer executed"); + Log.d(TAG, "onAnswer videoState executed"); } @Override diff --git a/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java b/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java index aef7b57..f140fb7 100644 --- a/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java +++ b/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java @@ -229,7 +229,17 @@ private void startForegroundService() { Context context = this.getApplicationContext(); Resources res = context.getResources(); String smallIcon = foregroundSettings.getString("notificationIcon"); - notificationBuilder.setSmallIcon(res.getIdentifier(smallIcon, "mipmap", context.getPackageName())); + String mipmap = "mipmap/"; + String drawable = "drawable/"; + if (smallIcon.contains(mipmap)) { + notificationBuilder.setSmallIcon( + res.getIdentifier(smallIcon.replace(mipmap, ""), + "mipmap", context.getPackageName())); + } else if (smallIcon.contains(drawable)) { + notificationBuilder.setSmallIcon( + res.getIdentifier(smallIcon.replace(drawable, ""), + "drawable", context.getPackageName())); + } } Log.d(TAG, "[VoiceConnectionService] Starting foreground service"); diff --git a/lib/src/api.dart b/lib/src/api.dart index 75aaec8..58d9600 100644 --- a/lib/src/api.dart +++ b/lib/src/api.dart @@ -310,6 +310,10 @@ class FlutterCallkeep extends EventManager { return false; } + Future openPhoneAccounts() async { + _openPhoneAccounts(); + } + Future _openPhoneAccounts() async { if (!Platform.isAndroid) { return;