diff --git a/android/src/main/java/com/incomingcall/AnswerCallActivity.kt b/android/src/main/java/com/incomingcall/AnswerCallActivity.kt index 7fbd092..4ede877 100644 --- a/android/src/main/java/com/incomingcall/AnswerCallActivity.kt +++ b/android/src/main/java/com/incomingcall/AnswerCallActivity.kt @@ -48,6 +48,7 @@ class AnswerCallActivity : ReactActivity() { or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON ) + IncomingCallModule.sendIntercomBroadcast(this, "Call Answered") if (CallingActivity.active) { sendBroadcast(Intent(Constants.ACTION_END_CALL)) @@ -80,6 +81,9 @@ class AnswerCallActivity : ReactActivity() { private val mBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent) { if (intent.action == Constants.ACTION_END_CALL) { + context?.let { + IncomingCallModule.sendIntercomBroadcast(it, "Call Hangup") + } finishAndRemoveTask() } } diff --git a/android/src/main/java/com/incomingcall/CallService.kt b/android/src/main/java/com/incomingcall/CallService.kt index 687cd94..d34ab75 100644 --- a/android/src/main/java/com/incomingcall/CallService.kt +++ b/android/src/main/java/com/incomingcall/CallService.kt @@ -20,6 +20,7 @@ import android.os.VibratorManager import android.widget.RemoteViews import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat +import java.lang.Error class CallService : Service() { override fun onBind(intent: Intent?): IBinder? { @@ -29,13 +30,19 @@ class CallService : Service() { @RequiresApi(Build.VERSION_CODES.O) override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val bundle = intent?.extras + try { + val bundle = intent?.extras - val notification: Notification = buildNotification(bundle) - startForeground(Constants.FOREGROUND_SERVICE_ID, notification) - playRingtone() - startVibration() - startTimer(Constants.TIME_OUT) + val notification: Notification = buildNotification(bundle) + startForeground(Constants.FOREGROUND_SERVICE_ID, notification) + playRingtone() + startVibration() + startTimer(Constants.TIME_OUT) + IncomingCallModule.sendIntercomBroadcast(this, "Notification showed") + + } catch (error: Error) { + IncomingCallModule.sendIntercomBroadcast(this, "Failed to show notification") + } return START_NOT_STICKY } @@ -68,6 +75,8 @@ class CallService : Service() { notificationChannel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC notificationManager.createNotificationChannel(notificationChannel) + } else { + IncomingCallModule.sendIntercomBroadcast(this, "Android OS less than API 26") } val notification = NotificationCompat.Builder(this, Constants.CHANNEL) notification.setContentTitle(Constants.INCOMING_CALL) diff --git a/android/src/main/java/com/incomingcall/CallingActivity.kt b/android/src/main/java/com/incomingcall/CallingActivity.kt index e079867..d64df71 100644 --- a/android/src/main/java/com/incomingcall/CallingActivity.kt +++ b/android/src/main/java/com/incomingcall/CallingActivity.kt @@ -68,8 +68,11 @@ class CallingActivity : ReactActivity() { registerReceiver(mBroadcastReceiver, mIntentFilter) } + IncomingCallModule.sendIntercomBroadcast(this, "Incoming call full screen showed") + acceptButton.setOnClickListener { stopService(Intent(this, CallService::class.java)) + IncomingCallModule.sendIntercomBroadcast(this, "Call accepted from full screen") val answerIntent = Intent(this, AnswerCallActivity::class.java) startActivity(answerIntent) finishAndRemoveTask() @@ -77,8 +80,10 @@ class CallingActivity : ReactActivity() { declineButton.setOnClickListener { stopService(Intent(this, CallService::class.java)) + IncomingCallModule.sendIntercomBroadcast(this, "Call declined from full screen") finishAndRemoveTask() } + } private val mBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() { diff --git a/android/src/main/java/com/incomingcall/HungUpBroadcast.kt b/android/src/main/java/com/incomingcall/HungUpBroadcast.kt index 63c8897..209eaf8 100644 --- a/android/src/main/java/com/incomingcall/HungUpBroadcast.kt +++ b/android/src/main/java/com/incomingcall/HungUpBroadcast.kt @@ -14,5 +14,8 @@ class HungUpBroadcast : BroadcastReceiver() { val stopIntent = Intent(context, CallService::class.java) context?.stopService(stopIntent) + + IncomingCallModule.sendIntercomBroadcast(context!!, "Call Declined") + } } diff --git a/android/src/main/java/com/incomingcall/IncomingCallModule.kt b/android/src/main/java/com/incomingcall/IncomingCallModule.kt index 6ac5e34..8052087 100644 --- a/android/src/main/java/com/incomingcall/IncomingCallModule.kt +++ b/android/src/main/java/com/incomingcall/IncomingCallModule.kt @@ -1,11 +1,15 @@ package com.incomingcall +import android.annotation.SuppressLint import android.app.NotificationManager +import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.os.Build import androidx.annotation.RequiresApi import androidx.core.app.NotificationManagerCompat +import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule @@ -21,10 +25,42 @@ class IncomingCallModule(reactContext: ReactApplicationContext) : return Constants.INCOMING_CALL } + private val broadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + val action = intent?.extras?.getString("action") + if(!action.isNullOrEmpty()){ + val params: WritableMap = Arguments.createMap() + params.putString("action", action) + sendEventToJs("intercom_broadcast", params) + } + } + } + + @SuppressLint("UnspecifiedRegisterReceiverFlag") + @ReactMethod + fun registerReceiver() { + try { + unregisterReceiver() + val intentFilter = IntentFilter("android.intercom.broadcast") + reactApplicationContext.registerReceiver(broadcastReceiver, intentFilter) + }catch (error: Exception){ + error.printStackTrace() + } + } + + @ReactMethod + fun unregisterReceiver() { + try { + reactApplicationContext.unregisterReceiver(broadcastReceiver) + }catch (error: Exception){ + error.printStackTrace() + } + } + @RequiresApi(Build.VERSION_CODES.O) @ReactMethod fun showIncomingCall(options: ReadableMap?) { - if(AnswerCallActivity.active){ + if (AnswerCallActivity.active) { return } reactApplicationContext.stopService( @@ -36,6 +72,8 @@ class IncomingCallModule(reactContext: ReactApplicationContext) : val intent = Intent(reactApplicationContext, CallService::class.java) reactApplicationContext.startForegroundService(intent) + + sendIntercomBroadcast(reactApplicationContext, "Invoked call notification") } @ReactMethod @@ -73,4 +111,13 @@ class IncomingCallModule(reactContext: ReactApplicationContext) : reactApplicationContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) ?.emit(eventName, params) } + + + companion object { + fun sendIntercomBroadcast(context: Context, action: String){ + val intent = Intent("android.intercom.broadcast") + intent.putExtra("action", action) + context.sendBroadcast(intent) + } + } } diff --git a/package.json b/package.json index 1b90628..a10df7e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@harsha1642/react-native-incomingcall", - "version": "0.1.0", + "version": "2.0.0", "description": "test", "main": "src/index", "module": "lib/module/index", diff --git a/src/index.ts b/src/index.ts index e3fd23a..68ee49c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,18 @@ import { NativeModules, Platform } from 'react-native'; const IncomingCall = NativeModules.IncomingCall; +const registerReceiver = (): void => { + if (Platform.OS === 'android') { + IncomingCall.registerReceiver(); + } +}; + +const unregisterReceiver = (): void => { + if (Platform.OS === 'android') { + IncomingCall.unregisterReceiver(); + } +}; + const showIncomingCall = (options = {}): void => { if (Platform.OS === 'android') { IncomingCall.showIncomingCall(options); @@ -19,4 +31,10 @@ const areNotificationsEnabled = async () => { return granted; }; -export { showIncomingCall, endCall, areNotificationsEnabled }; +export { + showIncomingCall, + endCall, + areNotificationsEnabled, + registerReceiver, + unregisterReceiver, +};