-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
SendMessageWorker.kt
356 lines (338 loc) · 13.2 KB
/
SendMessageWorker.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* VoIP.ms SMS
* Copyright (C) 2017-2021 Michael Kourlas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.kourlas.voipms_sms.sms.workers
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.database.Database
import net.kourlas.voipms_sms.network.NetworkManager
import net.kourlas.voipms_sms.notifications.Notifications
import net.kourlas.voipms_sms.preferences.accountConfigured
import net.kourlas.voipms_sms.preferences.getEmail
import net.kourlas.voipms_sms.preferences.getPassword
import net.kourlas.voipms_sms.sms.ConversationId
import net.kourlas.voipms_sms.sms.Message
import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import java.io.IOException
/**
* Worker used to send an SMS message using the VoIP.ms API.
*/
class SendMessageWorker(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params) {
private var conversationId: ConversationId? = null
private var error: String? = null
private var markedAsSent: Boolean = false
private var markedAsNotSent: Boolean = false
override suspend fun doWork(): Result {
// Terminate quietly if we cannot find the message we are supposed
// to send.
val databaseId =
inputData.getLong(
applicationContext.getString(
R.string.send_message_database_id
), -1
)
if (databaseId == -1L) {
return Result.failure()
}
try {
// Send the message we were asked to send.
sendMessage(databaseId)
// Send a broadcast indicating that the message has been sent, or that
// an attempt to send it was made.
conversationId?.let {
val sentMessageBroadcastIntent = Intent(
applicationContext.getString(
R.string.sent_message_action, it.did, it.contact
)
)
if (error != null) {
sentMessageBroadcastIntent.putExtra(
applicationContext.getString(
R.string.sent_message_error
), error
)
}
sentMessageBroadcastIntent.setPackage(
applicationContext.packageName
)
applicationContext.sendBroadcast(sentMessageBroadcastIntent)
}
return if (error == null) {
Result.success()
} else {
Result.failure()
}
} finally {
// We are not going to try sending this message again unless the
// user triggers it by clicking on the message, so ensure the
// database is correctly updated even on failure.
if (!markedAsSent && !markedAsNotSent) {
try {
Database.getInstance(
applicationContext
).markMessageNotSent(
databaseId
)
} catch (e: Exception) {
}
}
}
}
override suspend fun getForegroundInfo(): ForegroundInfo {
val notification = Notifications.getInstance(applicationContext)
.getSyncMessageSendNotification()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ForegroundInfo(
Notifications.SYNC_SEND_MESSAGE_NOTIFICATION_ID,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
} else {
ForegroundInfo(
Notifications.SYNC_SEND_MESSAGE_NOTIFICATION_ID,
notification
)
}
}
/**
* Sends the message we were asked to send using the VoIP.ms API and
* updates the database accordingly.
*/
private suspend fun sendMessage(databaseId: Long) {
try {
// Terminate quietly if it is impossible to send messages due to
// account configuration.
if (!accountConfigured(applicationContext)) {
return
}
// Retrieve the message we are supposed to send. Terminate quietly
// if it is not in a valid state.
val message = Database.getInstance(applicationContext)
.getMessageDatabaseId(databaseId)
conversationId = message?.conversationId
if (message == null || !message.isDeliveryInProgress) {
return
}
// Terminate if no network connection is available.
if (!NetworkManager.getInstance().isNetworkConnectionAvailable(
applicationContext
)
) {
error = applicationContext.getString(
R.string.send_message_error_network
)
Database.getInstance(
applicationContext
).markMessageNotSent(
message.databaseId
)
return
}
// Send the message using the VoIP.ms API. If we succeed, mark the
// the message was sent. If not, mark it as not having been sent.
val voipId = sendMessageWithVoipMsApi(message)
if (voipId != null) {
Database.getInstance(
applicationContext
).markMessageSent(
message.databaseId, voipId
)
markedAsSent = true
} else {
Database.getInstance(
applicationContext
).markMessageNotSent(
message.databaseId
)
markedAsNotSent = true
}
// If this request came from an inline reply, refresh the
// notification.
val inlineReply = inputData.getBoolean(
applicationContext.getString(
R.string.send_message_inline_reply
), false
)
if (inlineReply) {
val inlineReplyDid = inputData.getString(
applicationContext.getString(
R.string.send_message_inline_reply_did
)
) ?: return
val inlineReplyContact = inputData.getString(
applicationContext.getString(
R.string.send_message_inline_reply_contact
)
) ?: return
Notifications.getInstance(applicationContext).showNotifications(
setOf(ConversationId(inlineReplyDid, inlineReplyContact)),
inlineReplyMessages = listOf(message)
)
}
} catch (e: Exception) {
logException(e)
error = applicationContext.getString(
R.string.send_message_error_unknown
)
}
}
@JsonClass(generateAdapter = true)
data class MessageResponse(val status: String, val sms: Long?)
/**
* Sends the specified message using the VoIP.ms API
*
* @return Null if the message could not be sent.
*/
private suspend fun sendMessageWithVoipMsApi(message: Message): Long? {
// Get JSON response from API
val response: MessageResponse? = getMessageResponse(message)
// Get VoIP.ms ID from response
if (response?.status == "") {
error = applicationContext.getString(
R.string.send_message_error_api_parse
)
return null
}
if (response?.status != "success") {
error = when (response?.status) {
"invalid_credentials" -> applicationContext.getString(
R.string.send_message_error_api_error_invalid_credentials
)
"invalid_dst" -> applicationContext.getString(
R.string.send_message_error_api_error_invalid_dst
)
"invalid_sms" -> applicationContext.getString(
R.string.send_message_error_api_error_invalid_sms
)
"limit_reached" -> applicationContext.getString(
R.string.send_message_error_api_error_limit_reached
)
"message_empty" -> applicationContext.getString(
R.string.send_message_error_api_error_message_empty
)
"missing_sms" -> applicationContext.getString(
R.string.send_message_error_api_error_missing_sms
)
"sms_failed" -> applicationContext.getString(
R.string.send_message_error_api_error_sms_failed
)
"sms_toolong" -> applicationContext.getString(
R.string.send_message_error_api_error_sms_toolong
)
else -> applicationContext.getString(
R.string.send_message_error_api_error, response?.status
)
}
return null
}
if (response.sms == 0L) {
error = applicationContext.getString(
R.string.send_message_error_api_parse
)
return null
}
return response.sms
}
private suspend fun getMessageResponse(message: Message): MessageResponse? {
try {
repeat(3) {
try {
return httpPostWithMultipartFormData(
applicationContext,
"https://voip.ms/api/v1/rest.php",
mapOf(
"api_username" to getEmail(applicationContext),
"api_password" to getPassword(applicationContext),
"method" to "sendSMS",
"did" to message.did,
"dst" to message.contact,
"message" to message.text
)
)
} catch (e: IOException) {
// Try again...
}
}
error = applicationContext.getString(
R.string.send_message_error_api_request
)
return null
} catch (e: JsonDataException) {
logException(e)
error = applicationContext.getString(
R.string.send_message_error_api_parse
)
return null
} catch (e: Exception) {
logException(e)
error = applicationContext.getString(
R.string.send_message_error_unknown
)
return null
}
}
companion object {
/**
* Sends the message associated with the specified database ID.
*/
fun sendMessage(
context: Context, databaseId: Long,
inlineReplyConversationId: ConversationId? = null
) {
val work = OneTimeWorkRequestBuilder<SendMessageWorker>()
.setInputData(
workDataOf(
context.getString(
R.string.send_message_database_id
) to databaseId,
context.getString(
R.string.send_message_inline_reply
)
to (inlineReplyConversationId != null),
context.getString(
R.string.send_message_inline_reply_did
)
to inlineReplyConversationId?.did,
context.getString(
R.string.send_message_inline_reply_contact
)
to inlineReplyConversationId?.contact
)
)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
WorkManager.getInstance(context).enqueueUniqueWork(
context.getString(R.string.send_message_work_id, databaseId),
ExistingWorkPolicy.KEEP, work
)
}
}
}