-
Notifications
You must be signed in to change notification settings - Fork 96
/
RecorderInCallService.kt
286 lines (244 loc) · 10 KB
/
RecorderInCallService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.chiller3.bcr
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.telecom.Call
import android.telecom.InCallService
import android.util.Log
class RecorderInCallService : InCallService(), RecorderThread.OnRecordingCompletedListener {
companion object {
private val TAG = RecorderInCallService::class.java.simpleName
}
private lateinit var prefs: Preferences
private val handler = Handler(Looper.getMainLooper())
/**
* Recording threads for each active call. When a call is disconnected, it is immediately
* removed from this map and [pendingExit] is incremented.
*/
private val recorders = HashMap<Call, RecorderThread>()
/**
* Number of threads pending exit after the call has been disconnected. This can be negative if
* the recording thread fails before the call is disconnected.
*/
private var pendingExit = 0
private var failedNotificationId = 2
private val callback = object : Call.Callback() {
override fun onStateChanged(call: Call, state: Int) {
super.onStateChanged(call, state)
Log.d(TAG, "onStateChanged: $call, $state")
handleStateChange(call, state)
}
override fun onDetailsChanged(call: Call, details: Call.Details) {
super.onDetailsChanged(call, details)
Log.d(TAG, "onDetailsChanged: $call, $details")
handleDetailsChange(call, details)
// Due to firmware bugs, on older Samsung firmware, this callback (with the DISCONNECTED
// state) is the only notification we receive that a call ended
handleStateChange(call, null)
}
override fun onCallDestroyed(call: Call) {
super.onCallDestroyed(call)
Log.d(TAG, "onCallDestroyed: $call")
requestStopRecording(call)
}
}
override fun onCreate() {
super.onCreate()
prefs = Preferences(this)
}
/**
* Always called when the telephony framework becomes aware of a new call.
*
* This is the entry point for a new call. [callback] is always registered to keep track of
* state changes.
*/
override fun onCallAdded(call: Call) {
super.onCallAdded(call)
Log.d(TAG, "onCallAdded: $call")
// The callback is unregistered in requestStopRecording()
call.registerCallback(callback)
// In case the call is already in the active state
handleStateChange(call, null)
}
/**
* Called when the telephony framework destroys a call.
*
* This will request the cancellation of the recording, even if [call] happens to not be in one
* of the disconnecting states.
*
* This is NOT guaranteed to be called, notably on older Samsung firmware, due to bugs in the
* telephony framework. As a result, [handleStateChange] stop the recording if the call enters a
* disconnecting state.
*/
override fun onCallRemoved(call: Call) {
super.onCallRemoved(call)
Log.d(TAG, "onCallRemoved: $call")
// Unconditionally request the recording to stop, even if it's not in a disconnecting state
// since no further events will be received for the call.
requestStopRecording(call)
}
/**
* Start or stop recording based on the [call] state.
*
* If the state is [Call.STATE_ACTIVE], then recording will begin. If the state is either
* [Call.STATE_DISCONNECTING] or [Call.STATE_DISCONNECTED], then the cancellation of the active
* recording will be requested. If [state] is null, then the call state is queried from [call].
*/
private fun handleStateChange(call: Call, state: Int?) {
val callState = state ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
call.details.state
} else {
@Suppress("DEPRECATION")
call.state
}
Log.d(TAG, "handleStateChange: $call, $state, $callState")
if (callState == Call.STATE_ACTIVE) {
startRecording(call)
} else if (callState == Call.STATE_DISCONNECTING || callState == Call.STATE_DISCONNECTED) {
// This is necessary because onCallRemoved() might not be called due to firmware bugs
requestStopRecording(call)
}
}
/**
* Start a [RecorderThread] for [call].
*
* If call recording is disabled or the required permissions aren't granted, then no
* [RecorderThread] will be created.
*
* This function is idempotent.
*/
private fun startRecording(call: Call) {
if (!prefs.isCallRecordingEnabled) {
Log.v(TAG, "Call recording is disabled")
} else if (!Permissions.haveRequired(this)) {
Log.v(TAG, "Required permissions have not been granted")
} else if (!recorders.containsKey(call)) {
val recorder = try {
RecorderThread(this, this, call)
} catch (e: Exception) {
notifyError(e.message, null)
throw e
}
recorders[call] = recorder
updateForegroundState()
recorder.start()
}
}
/**
* Request the cancellation of the [RecorderThread].
*
* The [RecorderThread] is immediately removed from [recorders], but [pendingExit] will be
* incremented to keep the foreground notification alive until the [RecorderThread] exits and
* reports its status. The thread may exit, decrementing [pendingExit], before this function is
* called if an error occurs during recording.
*
* This function will also unregister [callback] from the call since it's no longer necessary to
* track further state changes.
*
* This function is idempotent.
*/
private fun requestStopRecording(call: Call) {
// This is safe to call multiple times in the AOSP implementation and also in heavily
// modified builds, like Samsung's firmware. If this ever becomes a problem, we can keep
// track of which calls have callbacks registered.
call.unregisterCallback(callback)
val recorder = recorders[call]
if (recorder != null) {
recorder.cancel()
recorders.remove(call)
// Don't change the foreground state until the thread has exited
++pendingExit
}
}
/**
* Notify the recording thread of call details changes.
*
* The recording thread uses call details for generating filenames.
*/
private fun handleDetailsChange(call: Call, details: Call.Details) {
recorders[call]?.onCallDetailsChanged(details)
}
/**
* Move to foreground, creating a persistent notification, when there are active calls or
* recording threads that haven't finished exiting yet.
*/
private fun updateForegroundState() {
if (recorders.isEmpty() && pendingExit == 0) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
startForeground(1, createPersistentNotification())
}
}
/**
* Create a persistent notification for use during recording. The notification appearance is
* fully static and in progress call recording is represented by the presence or absence of the
* notification.
*/
private fun createPersistentNotification(): Notification {
val notificationIntent = Intent(this, SettingsActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
)
return Notification.Builder(this, RecorderApplication.CHANNEL_ID_PERSISTENT).run {
setContentTitle(getText(R.string.notification_recording_in_progress))
setSmallIcon(R.drawable.ic_launcher_quick_settings)
setContentIntent(pendingIntent)
setOngoing(true)
// Inhibit 10-second delay when showing persistent notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
}
build()
}
}
private fun createRecordingFailedNotification(errorMsg: String?, uri: Uri?): Notification =
Notification.Builder(this, RecorderApplication.CHANNEL_ID_ALERTS).run {
val text = buildString {
val errorMsgTrimmed = errorMsg?.trim()
if (!errorMsgTrimmed.isNullOrBlank()) {
append(errorMsgTrimmed)
}
if (uri != null) {
if (!isEmpty()) {
append("\n\n")
}
append(uri)
}
}
setContentTitle(getString(R.string.notification_recording_failed))
if (text.isNotBlank()) {
setContentText(text)
style = Notification.BigTextStyle()
}
setSmallIcon(R.drawable.ic_launcher_quick_settings)
build()
}
private fun notifyError(errorMsg: String?, uri: Uri?) {
val notification = createRecordingFailedNotification(errorMsg, uri)
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.notify(failedNotificationId, notification)
++failedNotificationId
}
private fun onThreadExited() {
--pendingExit
updateForegroundState()
}
override fun onRecordingCompleted(thread: RecorderThread, uri: Uri) {
Log.i(TAG, "Recording completed: ${thread.id}: ${thread.redact(uri)}")
handler.post {
onThreadExited()
}
}
override fun onRecordingFailed(thread: RecorderThread, errorMsg: String?, uri: Uri?) {
Log.w(TAG, "Recording failed: ${thread.id}: ${uri?.let { thread.redact(it) }}")
handler.post {
onThreadExited()
notifyError(errorMsg, uri)
}
}
}