Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
153bf3598d |
@@ -11,8 +11,8 @@ android {
|
||||
applicationId = "com.expapp.trigger"
|
||||
minSdk = 24
|
||||
targetSdk = 34
|
||||
versionCode = 15
|
||||
versionName = "1.2.1"
|
||||
versionCode = 16
|
||||
versionName = "1.2.2"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
vectorDrawables {
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.expapp.trigger.ui.theme.DarkBackground
|
||||
import com.expapp.trigger.ui.theme.ExpAppTheme
|
||||
import com.expapp.trigger.widget.ServerWidgetProvider
|
||||
import com.expapp.trigger.worker.ActionTriggerWorker
|
||||
import com.expapp.trigger.worker.AppUpdateWorker
|
||||
import com.expapp.trigger.worker.NotificationHelper
|
||||
import com.expapp.trigger.worker.ServerMonitorWorker
|
||||
|
||||
@@ -62,6 +63,7 @@ class MainActivity : FragmentActivity() {
|
||||
NotificationHelper.createNotificationChannel(this)
|
||||
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||
ServerMonitorWorker.schedule(this)
|
||||
AppUpdateWorker.schedule(this)
|
||||
}
|
||||
onNotificationPermissionGrantedCallback?.invoke()
|
||||
} else {
|
||||
@@ -127,9 +129,10 @@ class MainActivity : FragmentActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize background server monitoring
|
||||
// Initialize background server monitoring & update checks
|
||||
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||
ServerMonitorWorker.schedule(this)
|
||||
AppUpdateWorker.schedule(this)
|
||||
}
|
||||
|
||||
// Handle initial launch intent (from NFC or Widget)
|
||||
@@ -195,10 +198,12 @@ class MainActivity : FragmentActivity() {
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
// When leaving the app, trigger an immediate one-time check & ensure worker is scheduled
|
||||
// When leaving the app, trigger an immediate one-time check & ensure workers are scheduled
|
||||
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||
ServerMonitorWorker.runOnce(this)
|
||||
ServerMonitorWorker.schedule(this)
|
||||
AppUpdateWorker.runOnce(this)
|
||||
AppUpdateWorker.schedule(this)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,6 +93,7 @@ import com.expapp.trigger.update.UpdateCheckResult
|
||||
import com.expapp.trigger.update.UpdateManager
|
||||
import com.expapp.trigger.widget.ServerWidgetProvider
|
||||
import com.expapp.trigger.worker.ActionTriggerWorker
|
||||
import com.expapp.trigger.worker.AppUpdateWorker
|
||||
import com.expapp.trigger.worker.ServerMonitorWorker
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
@@ -212,8 +213,14 @@ fun MainScreen(
|
||||
showUpdateDialog = true
|
||||
val newVer = result.info.versionName
|
||||
if (appSettings.lastNotifiedUpdateVersion != newVer) {
|
||||
appSettings.lastNotifiedUpdateVersion = newVer
|
||||
com.expapp.trigger.worker.NotificationHelper.sendUpdateAvailableNotification(context, newVer)
|
||||
val notified = com.expapp.trigger.worker.NotificationHelper.sendUpdateAvailableNotification(
|
||||
context = context,
|
||||
newVersion = newVer,
|
||||
releaseTitle = result.info.title
|
||||
)
|
||||
if (notified) {
|
||||
appSettings.lastNotifiedUpdateVersion = newVer
|
||||
}
|
||||
}
|
||||
if (isManual) {
|
||||
Toast.makeText(context, "New update available: v${result.info.versionName}!", Toast.LENGTH_SHORT).show()
|
||||
@@ -569,8 +576,10 @@ fun MainScreen(
|
||||
appSettings.isBackgroundMonitoringEnabled = enabled
|
||||
if (enabled) {
|
||||
ServerMonitorWorker.schedule(context)
|
||||
AppUpdateWorker.schedule(context)
|
||||
} else {
|
||||
ServerMonitorWorker.cancel(context)
|
||||
AppUpdateWorker.cancel(context)
|
||||
}
|
||||
},
|
||||
isBiometricAvailable = BiometricHelper.isBiometricAvailable(context),
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.expapp.trigger.worker
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.CoroutineWorker
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.NetworkType
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.WorkerParameters
|
||||
import com.expapp.trigger.BuildConfig
|
||||
import com.expapp.trigger.data.repository.AppSettings
|
||||
import com.expapp.trigger.update.UpdateCheckResult
|
||||
import com.expapp.trigger.update.UpdateManager
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* Dedicated background worker that checks for new app updates independently of server devices.
|
||||
* Ensures notifications are dispatched even if no servers are configured or if servers are offline.
|
||||
*/
|
||||
class AppUpdateWorker(
|
||||
private val appContext: Context,
|
||||
workerParams: WorkerParameters
|
||||
) : CoroutineWorker(appContext, workerParams) {
|
||||
|
||||
companion object {
|
||||
const val WORK_NAME = "app_update_periodic_check"
|
||||
const val ONE_TIME_WORK_NAME = "app_update_one_time_check"
|
||||
|
||||
fun schedule(context: Context, repeatIntervalMinutes: Long = 15) {
|
||||
NotificationHelper.createNotificationChannel(context)
|
||||
|
||||
val constraints = Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build()
|
||||
|
||||
val workRequest = PeriodicWorkRequestBuilder<AppUpdateWorker>(
|
||||
repeatIntervalMinutes,
|
||||
TimeUnit.MINUTES
|
||||
)
|
||||
.setConstraints(constraints)
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
|
||||
WORK_NAME,
|
||||
ExistingPeriodicWorkPolicy.UPDATE,
|
||||
workRequest
|
||||
)
|
||||
}
|
||||
|
||||
fun cancel(context: Context) {
|
||||
WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME)
|
||||
}
|
||||
|
||||
fun runOnce(context: Context) {
|
||||
NotificationHelper.createNotificationChannel(context)
|
||||
|
||||
val constraints = Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build()
|
||||
|
||||
val oneTimeRequest = OneTimeWorkRequestBuilder<AppUpdateWorker>()
|
||||
.setConstraints(constraints)
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context).enqueueUniqueWork(
|
||||
ONE_TIME_WORK_NAME,
|
||||
ExistingWorkPolicy.REPLACE,
|
||||
oneTimeRequest
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
val appSettings = AppSettings(appContext)
|
||||
|
||||
try {
|
||||
val updateManager = UpdateManager(appContext)
|
||||
val currentVersion = BuildConfig.VERSION_NAME
|
||||
val result = updateManager.checkForUpdateSync(currentVersion)
|
||||
|
||||
if (result is UpdateCheckResult.UpdateAvailable) {
|
||||
val newVer = result.info.versionName
|
||||
val lastNotified = appSettings.lastNotifiedUpdateVersion
|
||||
|
||||
if (lastNotified != newVer) {
|
||||
val notified = NotificationHelper.sendUpdateAvailableNotification(
|
||||
context = appContext,
|
||||
newVersion = newVer,
|
||||
releaseTitle = result.info.title
|
||||
)
|
||||
if (notified) {
|
||||
appSettings.lastNotifiedUpdateVersion = newVer
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// Ignore failure; will retry next interval
|
||||
}
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
}
|
||||
@@ -85,11 +85,20 @@ object NotificationHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun sendUpdateAvailableNotification(context: Context, newVersion: String): Boolean {
|
||||
fun sendUpdateAvailableNotification(
|
||||
context: Context,
|
||||
newVersion: String,
|
||||
releaseTitle: String? = null
|
||||
): Boolean {
|
||||
val title = if (!releaseTitle.isNullOrBlank() && releaseTitle != "v$newVersion") {
|
||||
"🚀 Update Available: $releaseTitle"
|
||||
} else {
|
||||
"🚀 Update Available: v$newVersion"
|
||||
}
|
||||
return sendNotification(
|
||||
context = context,
|
||||
id = 8888,
|
||||
title = "🚀 Update Available: v$newVersion",
|
||||
title = title,
|
||||
message = "A new version (v$newVersion) of Device Trigger is ready to install. Tap to update!",
|
||||
isSuccess = true,
|
||||
extraAction = "open_update"
|
||||
|
||||
@@ -142,8 +142,14 @@ class ServerMonitorWorker(
|
||||
val newVer = result.info.versionName
|
||||
val lastNotified = appSettings.lastNotifiedUpdateVersion
|
||||
if (lastNotified != newVer) {
|
||||
appSettings.lastNotifiedUpdateVersion = newVer
|
||||
NotificationHelper.sendUpdateAvailableNotification(appContext, newVer)
|
||||
val notified = NotificationHelper.sendUpdateAvailableNotification(
|
||||
context = appContext,
|
||||
newVersion = newVer,
|
||||
releaseTitle = result.info.title
|
||||
)
|
||||
if (notified) {
|
||||
appSettings.lastNotifiedUpdateVersion = newVer
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
|
||||
Reference in New Issue
Block a user