@qusaieilouti99/call-manager 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
package com.qusaieilouti99.callmanager
|
|
2
2
|
|
|
3
3
|
import android.app.ActivityManager
|
|
4
|
-
import android.content.ComponentName
|
|
5
4
|
import android.content.Context
|
|
6
5
|
import android.content.Intent
|
|
7
6
|
import android.os.Build
|
|
8
|
-
import android.os.Bundle
|
|
9
|
-
import android.telecom.PhoneAccount
|
|
10
|
-
import android.telecom.PhoneAccountHandle
|
|
11
|
-
import android.telecom.TelecomManager
|
|
12
|
-
import android.util.Log
|
|
13
7
|
import com.facebook.react.bridge.*
|
|
14
8
|
import com.facebook.react.turbomodule.core.interfaces.TurboModule
|
|
15
|
-
import com.facebook.react.bridge.ReactApplicationContext
|
|
16
9
|
import com.facebook.react.module.annotations.ReactModule
|
|
17
10
|
|
|
18
11
|
@ReactModule(name = CallManagerModule.NAME)
|
|
@@ -22,12 +15,10 @@ class CallManagerModule(reactContext: ReactApplicationContext) :
|
|
|
22
15
|
companion object {
|
|
23
16
|
const val NAME = "CallManager"
|
|
24
17
|
const val TAG = "CallManagerModule"
|
|
25
|
-
const val PHONE_ACCOUNT_ID = "com.qusaieilouti99.callmanager.SELF_MANAGED"
|
|
26
18
|
}
|
|
27
19
|
|
|
28
20
|
private var currentCallId: String? = null
|
|
29
21
|
private var eventHandler: Callback? = null
|
|
30
|
-
private val notificationHelper by lazy { CallNotificationHelper(reactApplicationContext) }
|
|
31
22
|
|
|
32
23
|
override fun getName(): String = NAME
|
|
33
24
|
|
|
@@ -41,52 +32,18 @@ class CallManagerModule(reactContext: ReactApplicationContext) :
|
|
|
41
32
|
eventHandler?.invoke(event, callData)
|
|
42
33
|
}
|
|
43
34
|
|
|
44
|
-
// 1. Report incoming call
|
|
35
|
+
// 1. Report incoming call (for JS, not used by NotificationService anymore)
|
|
45
36
|
override fun reportIncomingCall(callId: String, callData: String) {
|
|
46
|
-
Log.d(TAG, "reportIncomingCall called with callId=$callId, callData=$callData")
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
val json = org.json.JSONObject(callData)
|
|
50
|
-
json.optString("name", "Unknown")
|
|
51
|
-
} catch (e: Exception) {
|
|
52
|
-
"Unknown"
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Show incoming call notification and play ringtone
|
|
56
|
-
notificationHelper.showIncomingCallNotification(callId, callerName)
|
|
57
|
-
|
|
58
|
-
val context = reactApplicationContext
|
|
59
|
-
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
|
|
60
|
-
|
|
61
|
-
// Register self-managed PhoneAccount if not already registered
|
|
62
|
-
val phoneAccountHandle = PhoneAccountHandle(
|
|
63
|
-
ComponentName(context, MyConnectionService::class.java),
|
|
64
|
-
PHONE_ACCOUNT_ID
|
|
65
|
-
)
|
|
66
|
-
if (telecomManager.getPhoneAccount(phoneAccountHandle) == null) {
|
|
67
|
-
val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "PingMe Call")
|
|
68
|
-
.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
|
|
69
|
-
.build()
|
|
70
|
-
telecomManager.registerPhoneAccount(phoneAccount)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Prepare extras
|
|
74
|
-
val extras = Bundle()
|
|
75
|
-
extras.putString(MyConnectionService.EXTRA_CALL_DATA, callData)
|
|
76
|
-
|
|
77
|
-
// Report the call
|
|
78
|
-
try {
|
|
79
|
-
telecomManager.addNewIncomingCall(phoneAccountHandle, extras)
|
|
80
|
-
currentCallId = callId
|
|
81
|
-
startCallForegroundService()
|
|
82
|
-
} catch (e: Exception) {
|
|
83
|
-
Log.e(TAG, "Failed to report incoming call: ${e.message}")
|
|
84
|
-
}
|
|
37
|
+
Log.d(TAG, "reportIncomingCall called from JS with callId=$callId, callData=$callData")
|
|
38
|
+
// Optionally, you can call CallNativeHelper here if you want to trigger from JS
|
|
39
|
+
CallNativeHelper.reportIncomingCall(reactApplicationContext, callId, callData)
|
|
85
40
|
}
|
|
86
41
|
|
|
87
42
|
// 2. End call
|
|
88
43
|
override fun endCall(callId: String) {
|
|
89
|
-
|
|
44
|
+
val context = reactApplicationContext
|
|
45
|
+
val intent = Intent(context, CallForegroundService::class.java)
|
|
46
|
+
context.stopService(intent)
|
|
90
47
|
}
|
|
91
48
|
|
|
92
49
|
// 3. Answer call
|
|
@@ -102,25 +59,7 @@ class CallManagerModule(reactContext: ReactApplicationContext) :
|
|
|
102
59
|
// 5. Reject current and answer new
|
|
103
60
|
override fun rejectCurrentAndAnswerNew(callId: String, callData: String) {
|
|
104
61
|
endCall(currentCallId ?: "")
|
|
105
|
-
reportIncomingCall(callId, callData)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// --- Foreground Service Helpers ---
|
|
109
|
-
|
|
110
|
-
private fun startCallForegroundService() {
|
|
111
|
-
val context = reactApplicationContext
|
|
112
|
-
val intent = Intent(context, CallForegroundService::class.java)
|
|
113
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
114
|
-
context.startForegroundService(intent)
|
|
115
|
-
} else {
|
|
116
|
-
context.startService(intent)
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private fun stopCallForegroundService() {
|
|
121
|
-
val context = reactApplicationContext
|
|
122
|
-
val intent = Intent(context, CallForegroundService::class.java)
|
|
123
|
-
context.stopService(intent)
|
|
62
|
+
CallNativeHelper.reportIncomingCall(reactApplicationContext, callId, callData)
|
|
124
63
|
}
|
|
125
64
|
|
|
126
65
|
// --- Bring App to Foreground ---
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
package com.qusaieilouti99.callmanager
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.os.Build
|
|
6
|
+
import android.os.Bundle
|
|
7
|
+
import android.telecom.PhoneAccount
|
|
8
|
+
import android.telecom.PhoneAccountHandle
|
|
9
|
+
import android.telecom.TelecomManager
|
|
10
|
+
import android.util.Log
|
|
11
|
+
|
|
12
|
+
object CallNativeHelper {
|
|
13
|
+
private const val TAG = "CallNativeHelper"
|
|
14
|
+
private const val PHONE_ACCOUNT_ID = "com.qusaieilouti99.callmanager.SELF_MANAGED"
|
|
15
|
+
|
|
16
|
+
fun reportIncomingCall(context: Context, callId: String, callData: String) {
|
|
17
|
+
Log.d(TAG, "reportIncomingCall called with callId=$callId, callData=$callData")
|
|
18
|
+
// Parse caller name from callData (or use a default)
|
|
19
|
+
val callerName = try {
|
|
20
|
+
val json = org.json.JSONObject(callData)
|
|
21
|
+
json.optString("name", "Unknown")
|
|
22
|
+
} catch (e: Exception) {
|
|
23
|
+
"Unknown"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Show incoming call notification and play ringtone
|
|
27
|
+
CallNotificationHelper(context).showIncomingCallNotification(callId, callerName)
|
|
28
|
+
|
|
29
|
+
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
|
|
30
|
+
|
|
31
|
+
// Register self-managed PhoneAccount if not already registered
|
|
32
|
+
val phoneAccountHandle = PhoneAccountHandle(
|
|
33
|
+
ComponentName(context, MyConnectionService::class.java),
|
|
34
|
+
PHONE_ACCOUNT_ID
|
|
35
|
+
)
|
|
36
|
+
if (telecomManager.getPhoneAccount(phoneAccountHandle) == null) {
|
|
37
|
+
val phoneAccount = PhoneAccount.builder(phoneAccountHandle, "PingMe Call")
|
|
38
|
+
.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
|
|
39
|
+
.build()
|
|
40
|
+
telecomManager.registerPhoneAccount(phoneAccount)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Prepare extras
|
|
44
|
+
val extras = Bundle()
|
|
45
|
+
extras.putString(MyConnectionService.EXTRA_CALL_DATA, callData)
|
|
46
|
+
|
|
47
|
+
// Report the call
|
|
48
|
+
try {
|
|
49
|
+
telecomManager.addNewIncomingCall(phoneAccountHandle, extras)
|
|
50
|
+
// Optionally, start foreground service for ongoing call
|
|
51
|
+
val intent = android.content.Intent(context, CallForegroundService::class.java)
|
|
52
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
53
|
+
context.startForegroundService(intent)
|
|
54
|
+
} else {
|
|
55
|
+
context.startService(intent)
|
|
56
|
+
}
|
|
57
|
+
} catch (e: Exception) {
|
|
58
|
+
Log.e(TAG, "Failed to report incoming call: ${e.message}")
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|