Skip to content

Commit 3320f79

Browse files
committed
Add button to send heartbeat
1 parent de5dbc4 commit 3320f79

4 files changed

Lines changed: 95 additions & 1 deletion

File tree

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ import android.content.pm.PackageManager
1111
import android.os.Bundle
1212
import android.telephony.PhoneNumberUtils
1313
import android.telephony.TelephonyManager
14+
import android.view.View
1415
import android.widget.TextView
1516
import android.widget.Toast
1617
import androidx.activity.result.contract.ActivityResultContracts
1718
import androidx.appcompat.app.AppCompatActivity
1819
import androidx.core.app.ActivityCompat
20+
import androidx.lifecycle.MutableLiveData
1921
import androidx.work.ExistingPeriodicWorkPolicy
2022
import androidx.work.PeriodicWorkRequestBuilder
2123
import androidx.work.WorkManager
2224
import com.google.android.material.button.MaterialButton
25+
import com.google.android.material.progressindicator.LinearProgressIndicator
2326
import com.google.android.material.switchmaterial.SwitchMaterial
2427
import com.httpsms.services.StickyNotificationService
2528
import com.httpsms.worker.HeartbeatWorker
@@ -58,6 +61,7 @@ class MainActivity : AppCompatActivity() {
5861
startStickyNotification(this)
5962
scheduleHeartbeatWorker(this)
6063
setLastHeartbeatTimestamp(this)
64+
setHeartbeatListener(this)
6165
}
6266

6367
override fun onResume() {
@@ -82,7 +86,7 @@ class MainActivity : AppCompatActivity() {
8286
val localTime = timestampZdt.withZoneSameInstant(ZoneId.systemDefault())
8387
Timber.d("heartbeat timestamp in UTC is [${timestampZdt}] and local is [$localTime]")
8488

85-
refreshTimestampView.text = localTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
89+
refreshTimestampView.text = localTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
8690
}
8791

8892
private fun scheduleHeartbeatWorker(context: Context) {
@@ -283,4 +287,47 @@ class MainActivity : AppCompatActivity() {
283287

284288
Timber.d("creating permissions launcher")
285289
}
290+
291+
private fun setHeartbeatListener(context: Context) {
292+
findViewById<MaterialButton>(R.id.mainHeartbeatButton).setOnClickListener{onHeartbeatClick(context)}
293+
}
294+
295+
private fun onHeartbeatClick(context: Context) {
296+
Timber.d("heartbeat button clicked")
297+
val heartbeatButton = findViewById<MaterialButton>(R.id.mainHeartbeatButton)
298+
heartbeatButton.isEnabled = false
299+
300+
val progressBar = findViewById<LinearProgressIndicator>(R.id.mainProgressIndicator)
301+
progressBar.visibility = View.VISIBLE
302+
303+
304+
val liveData = MutableLiveData<String?>()
305+
liveData.observe(this) { exception ->
306+
run {
307+
progressBar.visibility = View.INVISIBLE
308+
heartbeatButton.isEnabled = true
309+
310+
if (exception != null) {
311+
Timber.w("heartbeat sending failed with [$exception]")
312+
Toast.makeText(context, exception, Toast.LENGTH_SHORT).show()
313+
return@run
314+
}
315+
Toast.makeText(context, "Heartbeat Sent", Toast.LENGTH_SHORT).show()
316+
setLastHeartbeatTimestamp(this)
317+
}
318+
}
319+
320+
Thread {
321+
var error: String? = null
322+
try {
323+
HttpSmsApiService(Settings.getApiKeyOrDefault(context)).storeHeartbeat(Settings.getOwnerOrDefault(context))
324+
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
325+
} catch (exception: Exception) {
326+
Timber.e(exception)
327+
error = exception.javaClass.simpleName
328+
}
329+
liveData.postValue(error)
330+
Timber.d("finished sending pulse")
331+
}.start()
332+
}
286333
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="18dp"
3+
android:height="14dp"
4+
android:viewportWidth="18"
5+
android:viewportHeight="14">
6+
<path
7+
android:pathData="M0,8.21H2.79L7.1,0L8.28,8.96L11.5,4.87L14.83,8.21H18V10.21H14L11.67,7.88L6.92,13.94L5.94,6.52L4,10.21H0V8.21z"
8+
android:fillColor="#ffffff"/>
9+
</vector>

android/app/src/main/res/layout/activity_main.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@
7373
</LinearLayout>
7474
</com.google.android.material.card.MaterialCardView>
7575

76+
<LinearLayout
77+
android:id="@+id/linearLayout"
78+
android:layout_width="match_parent"
79+
android:layout_height="wrap_content"
80+
android:layout_marginTop="16dp"
81+
android:layout_weight="50"
82+
android:orientation="vertical"
83+
app:layout_constraintEnd_toEndOf="parent"
84+
app:layout_constraintStart_toStartOf="parent"
85+
app:layout_constraintTop_toBottomOf="@+id/mainPhoneCard">
86+
87+
<com.google.android.material.button.MaterialButton
88+
android:id="@+id/mainHeartbeatButton"
89+
style="@style/Widget.MaterialComponents.Button.Icon"
90+
android:layout_width="match_parent"
91+
android:layout_height="wrap_content"
92+
android:backgroundTint="#2196f3"
93+
android:drawableTint="@color/white"
94+
android:padding="10dp"
95+
android:text="@string/send_heartbeat"
96+
android:textColor="@color/white"
97+
android:textSize="16sp"
98+
app:iconTint="@color/white"
99+
tools:ignore="TextContrastCheck" />
100+
101+
<com.google.android.material.progressindicator.LinearProgressIndicator
102+
android:id="@+id/mainProgressIndicator"
103+
android:layout_width="match_parent"
104+
android:layout_height="wrap_content"
105+
android:layout_marginTop="4dp"
106+
android:indeterminate="true"
107+
android:visibility="invisible"
108+
app:indicatorColor="@color/pink_500"
109+
app:layout_constraintTop_toBottomOf="@+id/mainHeartbeatButton"
110+
tools:layout_editor_absoluteX="16dp" />
111+
</LinearLayout>
112+
76113
<com.google.android.material.button.MaterialButton
77114
android:id="@+id/mainLogoutButton"
78115
style="@style/Widget.MaterialComponents.Button.Icon"

android/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
<string name="main_log_out">Log Out</string>
1212
<string name="login_phone_number_hint">International phone number in E.164 format</string>
1313
<string name="phone_number">Phone Number</string>
14+
<string name="send_heartbeat">Send Heartbeat</string>
1415
</resources>

0 commit comments

Comments
 (0)