Skip to content

Commit 9b3edbb

Browse files
committed
Add ability to store multiple heartbeats in 1 request
1 parent 98fd2d7 commit 9b3edbb

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.httpsms
22

3-
import android.app.Application
43
import android.app.PendingIntent
54
import android.content.Context
65
import android.content.Intent
@@ -56,7 +55,13 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
5655
}
5756
Thread {
5857
try {
59-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
58+
val phoneNumbers = mutableListOf<String>()
59+
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
60+
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
61+
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
62+
}
63+
64+
HttpSmsApiService.create(applicationContext).storeHeartbeat(phoneNumbers.toTypedArray(), Settings.isCharging(applicationContext))
6065
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
6166
} catch (exception: Exception) {
6267
Timber.e(exception)

android/app/src/main/java/com/httpsms/HttpSmsApiService.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
129129
return true
130130
}
131131

132-
fun storeHeartbeat(phoneNumber: String, charging: Boolean) {
132+
fun storeHeartbeat(phoneNumbers: Array<String>, charging: Boolean) {
133133
val body = """
134134
{
135135
"charging": $charging,
136-
"owner": "$phoneNumber"
136+
"phone_numbers": ${phoneNumbers.joinToString(prefix = "[", postfix = "]") { "\"$it\"" }}
137137
}
138138
""".trimIndent()
139139

@@ -146,13 +146,13 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
146146

147147
val response = client.newCall(request).execute()
148148
if (!response.isSuccessful) {
149-
Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending heartbeat [$body] for owner [$phoneNumber]")
149+
Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending heartbeat [$body] for phone numbers [${phoneNumbers.joinToString()}]")
150150
response.close()
151151
return
152152
}
153153

154154
response.close()
155-
Timber.i( "heartbeat stored successfully for owner [$phoneNumber]" )
155+
Timber.i( "heartbeat stored successfully for phone numbers [${phoneNumbers.joinToString()}]" )
156156
}
157157

158158

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatActivity
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.work.Constraints
2222
import androidx.work.ExistingPeriodicWorkPolicy
23+
import androidx.work.ListenableWorker.Result
2324
import androidx.work.NetworkType
2425
import androidx.work.PeriodicWorkRequestBuilder
2526
import androidx.work.WorkManager
@@ -309,7 +310,6 @@ class MainActivity : AppCompatActivity() {
309310
val progressBar = findViewById<LinearProgressIndicator>(R.id.mainProgressIndicator)
310311
progressBar.visibility = View.VISIBLE
311312

312-
313313
val liveData = MutableLiveData<String?>()
314314
liveData.observe(this) { exception ->
315315
run {
@@ -331,21 +331,18 @@ class MainActivity : AppCompatActivity() {
331331
val charging = Settings.isCharging(applicationContext)
332332
var error: String? = null
333333
try {
334-
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM1PhoneNumber(context), charging)
334+
val phoneNumbers = mutableListOf<String>()
335+
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
336+
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
337+
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
338+
}
339+
Timber.w("numbers = [${phoneNumbers.joinToString()}]")
340+
HttpSmsApiService.create(context).storeHeartbeat(phoneNumbers.toTypedArray(), charging)
335341
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
336342
} catch (exception: Exception) {
337343
Timber.e(exception)
338344
error = exception.javaClass.simpleName
339345
}
340-
if (Settings.isDualSIM(context)) {
341-
try {
342-
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM2PhoneNumber(context), charging)
343-
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
344-
} catch (exception: Exception) {
345-
Timber.e(exception)
346-
error = exception.javaClass.simpleName
347-
}
348-
}
349346
liveData.postValue(error)
350347
Timber.d("finished sending pulse")
351348
}.start()

android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,25 @@ class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Wor
1616
return Result.failure()
1717
}
1818

19-
sendSIM1Heartbeat()
20-
if (Settings.isDualSIM(applicationContext)) {
21-
sendSIM2Heartbeat()
19+
val phoneNumbers = mutableListOf<String>()
20+
if (Settings.getActiveStatus(applicationContext, Constants.SIM1)) {
21+
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
2222
}
23-
24-
return Result.success()
25-
}
26-
27-
private fun sendSIM1Heartbeat() {
28-
if (!Settings.getActiveStatus(applicationContext, Constants.SIM1)) {
29-
Timber.w("[SIM1] user is not active, stopping processing")
30-
return
23+
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
24+
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
3125
}
3226

33-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
34-
Timber.d("[SIM1] finished sending heartbeat to server")
35-
36-
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
37-
Timber.d("[SIM1] set the heartbeat timestamp")
38-
}
39-
40-
private fun sendSIM2Heartbeat() {
41-
if (!Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
42-
Timber.w("[SIM2] user is not active, stopping processing")
43-
return
27+
if (phoneNumbers.isEmpty()) {
28+
Timber.w("both [SIM1] and [SIM2] are inactive stopping processing.")
29+
return Result.success()
4430
}
4531

46-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM2PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
47-
Timber.d("[SIM2] finished sending heartbeat to server")
32+
HttpSmsApiService.create(applicationContext).storeHeartbeat(phoneNumbers.toTypedArray(), Settings.isCharging(applicationContext))
33+
Timber.d("finished sending heartbeats to server")
4834

4935
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
50-
Timber.d("[SIM2] set the heartbeat timestamp")
36+
Timber.d("Set the heartbeat timestamp")
37+
38+
return Result.success()
5139
}
5240
}

api/pkg/validators/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
for index, number := range phoneNumbers {
5151
_, err := phonenumbers.Parse(number, phonenumbers.UNKNOWN_REGION)
5252
if err != nil {
53-
return fmt.Errorf("The %s field in index [%s] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field, index)
53+
return fmt.Errorf("The %s field in index [%d] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field, index)
5454
}
5555
}
5656

0 commit comments

Comments
 (0)