Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c66f6c4dc | ||
|
|
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 = 17
|
||||||
versionName = "1.2.1"
|
versionName = "1.2.3"
|
||||||
|
|
||||||
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),
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ class ServerWidgetProvider : AppWidgetProvider() {
|
|||||||
appWidgetManager: AppWidgetManager,
|
appWidgetManager: AppWidgetManager,
|
||||||
appWidgetIds: IntArray
|
appWidgetIds: IntArray
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
val repository = DeviceRepository(SharedPrefsStorage(context))
|
val repository = DeviceRepository(SharedPrefsStorage(context))
|
||||||
val devices = repository.getDevices()
|
val devices = repository.getDevices()
|
||||||
|
|
||||||
@@ -67,13 +68,24 @@ class ServerWidgetProvider : AppWidgetProvider() {
|
|||||||
// If device exists, fetch live status asynchronously
|
// If device exists, fetch live status asynchronously
|
||||||
if (device != null && device.statusUrl.isNotBlank()) {
|
if (device != null && device.statusUrl.isNotBlank()) {
|
||||||
executor.execute {
|
executor.execute {
|
||||||
|
try {
|
||||||
val status = TriggerClient().checkStatusSync(device)
|
val status = TriggerClient().checkStatusSync(device)
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
|
try {
|
||||||
renderWidget(context, appWidgetManager, appWidgetId, device, status)
|
renderWidget(context, appWidgetManager, appWidgetId, device, status)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
@@ -131,6 +143,7 @@ class ServerWidgetProvider : AppWidgetProvider() {
|
|||||||
status: NodeStatus?,
|
status: NodeStatus?,
|
||||||
footerOverride: String? = null
|
footerOverride: String? = null
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
val views = RemoteViews(context.packageName, R.layout.widget_server_control)
|
val views = RemoteViews(context.packageName, R.layout.widget_server_control)
|
||||||
|
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
@@ -217,6 +230,9 @@ class ServerWidgetProvider : AppWidgetProvider() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createTriggerPendingIntent(
|
private fun createTriggerPendingIntent(
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 324 B |
Binary file not shown.
|
After Width: | Height: | Size: 314 B |
@@ -17,7 +17,7 @@
|
|||||||
<ImageView
|
<ImageView
|
||||||
android:layout_width="18dp"
|
android:layout_width="18dp"
|
||||||
android:layout_height="18dp"
|
android:layout_height="18dp"
|
||||||
android:src="@drawable/ic_terminal"
|
android:src="@drawable/widget_ic_terminal"
|
||||||
android:contentDescription="Server" />
|
android:contentDescription="Server" />
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
@@ -82,14 +82,13 @@
|
|||||||
android:id="@+id/widget_btn_refresh"
|
android:id="@+id/widget_btn_refresh"
|
||||||
android:layout_width="26dp"
|
android:layout_width="26dp"
|
||||||
android:layout_height="26dp"
|
android:layout_height="26dp"
|
||||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
|
||||||
android:contentDescription="Refresh"
|
android:contentDescription="Refresh"
|
||||||
android:padding="3dp"
|
android:padding="3dp"
|
||||||
android:src="@drawable/ic_refresh" />
|
android:src="@drawable/widget_ic_refresh" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- Spacer / Divider -->
|
<!-- Spacer / Divider (FrameLayout is explicitly whitelisted in RemoteViews) -->
|
||||||
<View
|
<FrameLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="1dp"
|
android:layout_height="1dp"
|
||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:minWidth="250dp"
|
android:minWidth="220dp"
|
||||||
android:minHeight="100dp"
|
android:minHeight="90dp"
|
||||||
|
android:targetCellWidth="3"
|
||||||
|
android:targetCellHeight="2"
|
||||||
|
android:minResizeWidth="180dp"
|
||||||
|
android:minResizeHeight="70dp"
|
||||||
android:updatePeriodMillis="1800000"
|
android:updatePeriodMillis="1800000"
|
||||||
android:initialLayout="@layout/widget_server_control"
|
android:initialLayout="@layout/widget_server_control"
|
||||||
android:resizeMode="horizontal|vertical"
|
android:resizeMode="horizontal|vertical"
|
||||||
|
|||||||
Reference in New Issue
Block a user