Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
153bf3598d |
@@ -11,8 +11,8 @@ android {
|
|||||||
applicationId = "com.expapp.trigger"
|
applicationId = "com.expapp.trigger"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 15
|
versionCode = 16
|
||||||
versionName = "1.2.1"
|
versionName = "1.2.2"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import com.expapp.trigger.ui.theme.DarkBackground
|
|||||||
import com.expapp.trigger.ui.theme.ExpAppTheme
|
import com.expapp.trigger.ui.theme.ExpAppTheme
|
||||||
import com.expapp.trigger.widget.ServerWidgetProvider
|
import com.expapp.trigger.widget.ServerWidgetProvider
|
||||||
import com.expapp.trigger.worker.ActionTriggerWorker
|
import com.expapp.trigger.worker.ActionTriggerWorker
|
||||||
|
import com.expapp.trigger.worker.AppUpdateWorker
|
||||||
import com.expapp.trigger.worker.NotificationHelper
|
import com.expapp.trigger.worker.NotificationHelper
|
||||||
import com.expapp.trigger.worker.ServerMonitorWorker
|
import com.expapp.trigger.worker.ServerMonitorWorker
|
||||||
|
|
||||||
@@ -62,6 +63,7 @@ class MainActivity : FragmentActivity() {
|
|||||||
NotificationHelper.createNotificationChannel(this)
|
NotificationHelper.createNotificationChannel(this)
|
||||||
if (appSettings.isBackgroundMonitoringEnabled) {
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||||
ServerMonitorWorker.schedule(this)
|
ServerMonitorWorker.schedule(this)
|
||||||
|
AppUpdateWorker.schedule(this)
|
||||||
}
|
}
|
||||||
onNotificationPermissionGrantedCallback?.invoke()
|
onNotificationPermissionGrantedCallback?.invoke()
|
||||||
} else {
|
} else {
|
||||||
@@ -127,9 +129,10 @@ class MainActivity : FragmentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize background server monitoring
|
// Initialize background server monitoring & update checks
|
||||||
if (appSettings.isBackgroundMonitoringEnabled) {
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||||
ServerMonitorWorker.schedule(this)
|
ServerMonitorWorker.schedule(this)
|
||||||
|
AppUpdateWorker.schedule(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle initial launch intent (from NFC or Widget)
|
// Handle initial launch intent (from NFC or Widget)
|
||||||
@@ -195,10 +198,12 @@ class MainActivity : FragmentActivity() {
|
|||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
super.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) {
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
||||||
ServerMonitorWorker.runOnce(this)
|
ServerMonitorWorker.runOnce(this)
|
||||||
ServerMonitorWorker.schedule(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.update.UpdateManager
|
||||||
import com.expapp.trigger.widget.ServerWidgetProvider
|
import com.expapp.trigger.widget.ServerWidgetProvider
|
||||||
import com.expapp.trigger.worker.ActionTriggerWorker
|
import com.expapp.trigger.worker.ActionTriggerWorker
|
||||||
|
import com.expapp.trigger.worker.AppUpdateWorker
|
||||||
import com.expapp.trigger.worker.ServerMonitorWorker
|
import com.expapp.trigger.worker.ServerMonitorWorker
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
@@ -212,8 +213,14 @@ fun MainScreen(
|
|||||||
showUpdateDialog = true
|
showUpdateDialog = true
|
||||||
val newVer = result.info.versionName
|
val newVer = result.info.versionName
|
||||||
if (appSettings.lastNotifiedUpdateVersion != newVer) {
|
if (appSettings.lastNotifiedUpdateVersion != newVer) {
|
||||||
|
val notified = com.expapp.trigger.worker.NotificationHelper.sendUpdateAvailableNotification(
|
||||||
|
context = context,
|
||||||
|
newVersion = newVer,
|
||||||
|
releaseTitle = result.info.title
|
||||||
|
)
|
||||||
|
if (notified) {
|
||||||
appSettings.lastNotifiedUpdateVersion = newVer
|
appSettings.lastNotifiedUpdateVersion = newVer
|
||||||
com.expapp.trigger.worker.NotificationHelper.sendUpdateAvailableNotification(context, newVer)
|
}
|
||||||
}
|
}
|
||||||
if (isManual) {
|
if (isManual) {
|
||||||
Toast.makeText(context, "New update available: v${result.info.versionName}!", Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, "New update available: v${result.info.versionName}!", Toast.LENGTH_SHORT).show()
|
||||||
@@ -569,8 +576,10 @@ fun MainScreen(
|
|||||||
appSettings.isBackgroundMonitoringEnabled = enabled
|
appSettings.isBackgroundMonitoringEnabled = enabled
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
ServerMonitorWorker.schedule(context)
|
ServerMonitorWorker.schedule(context)
|
||||||
|
AppUpdateWorker.schedule(context)
|
||||||
} else {
|
} else {
|
||||||
ServerMonitorWorker.cancel(context)
|
ServerMonitorWorker.cancel(context)
|
||||||
|
AppUpdateWorker.cancel(context)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isBiometricAvailable = BiometricHelper.isBiometricAvailable(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(
|
return sendNotification(
|
||||||
context = context,
|
context = context,
|
||||||
id = 8888,
|
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!",
|
message = "A new version (v$newVersion) of Device Trigger is ready to install. Tap to update!",
|
||||||
isSuccess = true,
|
isSuccess = true,
|
||||||
extraAction = "open_update"
|
extraAction = "open_update"
|
||||||
|
|||||||
@@ -142,8 +142,14 @@ class ServerMonitorWorker(
|
|||||||
val newVer = result.info.versionName
|
val newVer = result.info.versionName
|
||||||
val lastNotified = appSettings.lastNotifiedUpdateVersion
|
val lastNotified = appSettings.lastNotifiedUpdateVersion
|
||||||
if (lastNotified != newVer) {
|
if (lastNotified != newVer) {
|
||||||
|
val notified = NotificationHelper.sendUpdateAvailableNotification(
|
||||||
|
context = appContext,
|
||||||
|
newVersion = newVer,
|
||||||
|
releaseTitle = result.info.title
|
||||||
|
)
|
||||||
|
if (notified) {
|
||||||
appSettings.lastNotifiedUpdateVersion = newVer
|
appSettings.lastNotifiedUpdateVersion = newVer
|
||||||
NotificationHelper.sendUpdateAvailableNotification(appContext, newVer)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
|
|||||||
Reference in New Issue
Block a user