Skip to content

Commit f2ee2c1

Browse files
committed
Send charging state with heartbeat event
1 parent 6f9b8ef commit f2ee2c1

9 files changed

Lines changed: 23 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
5757
}
5858
Thread {
5959
try {
60-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext))
60+
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
6161
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
6262
} catch (exception: Exception) {
6363
Timber.e(exception)

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

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

33
import android.content.Context
4+
import android.os.BatteryManager
45
import okhttp3.MediaType.Companion.toMediaType
56
import okhttp3.OkHttpClient
67
import okhttp3.Request
@@ -104,9 +105,10 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
104105
Timber.i("received message stored successfully for message with ID [${message?.data?.id}]" )
105106
}
106107

107-
fun storeHeartbeat(phoneNumber: String) {
108+
fun storeHeartbeat(phoneNumber: String, charging: Boolean) {
108109
val body = """
109110
{
111+
"charging": "$charging",
110112
"owner": "$phoneNumber"
111113
}
112114
""".trimIndent()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,18 @@ class MainActivity : AppCompatActivity() {
326326
}
327327

328328
Thread {
329+
var charging = Settings.isCharging(applicationContext)
329330
var error: String? = null
330331
try {
331-
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM1PhoneNumber(context))
332+
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM1PhoneNumber(context), charging)
332333
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
333334
} catch (exception: Exception) {
334335
Timber.e(exception)
335336
error = exception.javaClass.simpleName
336337
}
337338
if (Settings.isDualSIM(context)) {
338339
try {
339-
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM2PhoneNumber(context))
340+
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM2PhoneNumber(context), charging)
340341
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
341342
} catch (exception: Exception) {
342343
Timber.e(exception)

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

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

33
import android.content.Context
4+
import android.os.BatteryManager
45
import androidx.preference.PreferenceManager
56
import timber.log.Timber
67
import java.net.URI
@@ -180,6 +181,11 @@ object Settings {
180181
return getApiKey(context) ?: ""
181182
}
182183

184+
fun isCharging(context: Context): Boolean {
185+
val myBatteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
186+
return myBatteryManager.isCharging
187+
}
188+
183189
fun setUserID(context:Context, userID: String?) {
184190
Timber.d(Settings::setUserID.name)
185191
PreferenceManager.getDefaultSharedPreferences(context)

android/app/src/main/java/com/httpsms/services/StickyNotificationService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.httpsms.services
33
import android.app.*
44
import android.content.Context
55
import android.content.Intent
6-
import android.graphics.Color
76
import android.os.IBinder
87
import android.widget.Toast
98
import com.httpsms.MainActivity

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.httpsms.worker
22

33
import android.content.Context
4+
import android.os.BatteryManager
45
import androidx.work.Worker
56
import androidx.work.WorkerParameters
67
import com.httpsms.Constants
78
import com.httpsms.HttpSmsApiService
89
import com.httpsms.Settings
10+
import com.httpsms.utils.Heartbeat
911
import timber.log.Timber
1012

1113
class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
@@ -30,7 +32,7 @@ class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Wor
3032
return
3133
}
3234

33-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext))
35+
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
3436
Timber.d("[SIM1] finished sending heartbeat to server")
3537

3638
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
@@ -43,7 +45,7 @@ class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Wor
4345
return
4446
}
4547

46-
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM2PhoneNumber(applicationContext))
48+
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM2PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
4749
Timber.d("[SIM2] finished sending heartbeat to server")
4850

4951
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())

api/pkg/entities/heartbeat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Heartbeat struct {
1111
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
1212
Owner string `json:"owner" gorm:"index:idx_heartbeats_owner_timestamp" example:"+18005550199"`
1313
Version string `json:"version" example:"344c10f"`
14+
Charging bool `json:"charging" example:"true"`
1415
UserID UserID `json:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
1516
Timestamp time.Time `json:"timestamp" gorm:"index:idx_heartbeats_owner_timestamp" example:"2022-06-05T14:26:01.520828+03:00"`
1617
}

api/pkg/requests/heartbeat_store_request.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
// HeartbeatStore is the payload for fetching entities.Heartbeat of a phone number
1111
type HeartbeatStore struct {
1212
request
13-
Owner string `json:"owner"`
13+
Charging bool `json:"charging"`
14+
Owner string `json:"owner"`
1415
}
1516

1617
// Sanitize sets defaults to MessageOutstanding
@@ -24,6 +25,7 @@ func (input *HeartbeatStore) ToStoreParams(user entities.AuthUser, version strin
2425
return services.HeartbeatStoreParams{
2526
Owner: input.Owner,
2627
Version: version,
28+
Charging: input.Charging,
2729
Timestamp: time.Now().UTC(),
2830
UserID: user.ID,
2931
}

api/pkg/services/heartbeat_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (service *HeartbeatService) Index(ctx context.Context, userID entities.User
7070
type HeartbeatStoreParams struct {
7171
Owner string
7272
Version string
73+
Charging bool
7374
Timestamp time.Time
7475
UserID entities.UserID
7576
}
@@ -84,6 +85,7 @@ func (service *HeartbeatService) Store(ctx context.Context, params HeartbeatStor
8485
heartbeat := &entities.Heartbeat{
8586
ID: uuid.New(),
8687
Owner: params.Owner,
88+
Charging: params.Charging,
8789
Timestamp: params.Timestamp,
8890
Version: params.Version,
8991
UserID: params.UserID,

0 commit comments

Comments
 (0)