Compare commits

...
2 Commits
Author SHA1 Message Date
Vaishno Singh 5c66f6c4dc fix(widget): fix 'Failed to load widget' by replacing View with FrameLayout, removing theme attrs & using PNG drawables (v1.2.3)
Build & Release Android APK / Build & Publish Release APK (push) Failing after 1m28s
2026-11-19 21:52:09 +05:30
Vaishno Singh 153bf3598d fix(update): reliable background update notifications, dedicated AppUpdateWorker, intent handling (v1.2.2)
Build & Release Android APK / Build & Publish Release APK (push) Failing after 3m26s
2026-11-19 18:36:06 +05:30
11 changed files with 261 additions and 108 deletions
+2 -2
View File
@@ -11,8 +11,8 @@ android {
applicationId = "com.expapp.trigger"
minSdk = 24
targetSdk = 34
versionCode = 15
versionName = "1.2.1"
versionCode = 17
versionName = "1.2.3"
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),
@@ -57,22 +57,34 @@ class ServerWidgetProvider : AppWidgetProvider() {
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
val repository = DeviceRepository(SharedPrefsStorage(context))
val devices = repository.getDevices()
try {
val repository = DeviceRepository(SharedPrefsStorage(context))
val devices = repository.getDevices()
for (appWidgetId in appWidgetIds) {
val device = devices.firstOrNull()
renderWidget(context, appWidgetManager, appWidgetId, device, null)
for (appWidgetId in appWidgetIds) {
val device = devices.firstOrNull()
renderWidget(context, appWidgetManager, appWidgetId, device, null)
// If device exists, fetch live status asynchronously
if (device != null && device.statusUrl.isNotBlank()) {
executor.execute {
val status = TriggerClient().checkStatusSync(device)
mainHandler.post {
renderWidget(context, appWidgetManager, appWidgetId, device, status)
// If device exists, fetch live status asynchronously
if (device != null && device.statusUrl.isNotBlank()) {
executor.execute {
try {
val status = TriggerClient().checkStatusSync(device)
mainHandler.post {
try {
renderWidget(context, appWidgetManager, appWidgetId, device, status)
} catch (e: Exception) {
e.printStackTrace()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
@@ -131,92 +143,96 @@ class ServerWidgetProvider : AppWidgetProvider() {
status: NodeStatus?,
footerOverride: String? = null
) {
val views = RemoteViews(context.packageName, R.layout.widget_server_control)
try {
val views = RemoteViews(context.packageName, R.layout.widget_server_control)
if (device == null) {
views.setTextViewText(R.id.widget_device_name, "No Device")
views.setTextViewText(R.id.widget_node_name, "Configure in app")
views.setTextViewText(R.id.widget_status_text, "OFFLINE")
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_gray)
views.setTextViewText(R.id.widget_footer_text, "Open app to add server")
if (device == null) {
views.setTextViewText(R.id.widget_device_name, "No Device")
views.setTextViewText(R.id.widget_node_name, "Configure in app")
views.setTextViewText(R.id.widget_status_text, "OFFLINE")
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_gray)
views.setTextViewText(R.id.widget_footer_text, "Open app to add server")
val appIntent = Intent(context, MainActivity::class.java)
val pendingApp = PendingIntent.getActivity(
context, 0, appIntent,
val appIntent = Intent(context, MainActivity::class.java)
val pendingApp = PendingIntent.getActivity(
context, 0, appIntent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
views.setOnClickPendingIntent(R.id.widget_header_clickable, pendingApp)
views.setOnClickPendingIntent(R.id.widget_root, pendingApp)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}
views.setTextViewText(R.id.widget_device_name, device.name)
views.setTextViewText(R.id.widget_node_name, device.node)
// Status display
if (status == null) {
views.setTextViewText(R.id.widget_status_text, "SYNCING")
views.setTextColor(R.id.widget_status_text, 0xFF71717A.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_gray)
} else if (status.isOnline) {
views.setTextViewText(R.id.widget_status_text, "ONLINE")
views.setTextColor(R.id.widget_status_text, 0xFF22C55E.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_green)
} else {
views.setTextViewText(R.id.widget_status_text, "OFFLINE")
views.setTextColor(R.id.widget_status_text, 0xFFEF4444.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_red)
}
views.setTextViewText(
R.id.widget_footer_text,
footerOverride ?: (if (status?.uptime != null) "Up: ${status.uptime}" else "Tap header to open • Tap button to trigger")
)
// Header click -> Open app
val openAppIntent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingOpenApp = PendingIntent.getActivity(
context,
appWidgetId * 10,
openAppIntent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
views.setOnClickPendingIntent(R.id.widget_header_clickable, pendingApp)
views.setOnClickPendingIntent(R.id.widget_root, pendingApp)
views.setOnClickPendingIntent(R.id.widget_header_clickable, pendingOpenApp)
// Refresh click
val refreshIntent = Intent(context, ServerWidgetProvider::class.java).apply {
action = ACTION_REFRESH
}
val pendingRefresh = PendingIntent.getBroadcast(
context,
appWidgetId * 10 + 1,
refreshIntent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
views.setOnClickPendingIntent(R.id.widget_btn_refresh, pendingRefresh)
// Power On click
views.setOnClickPendingIntent(
R.id.widget_btn_power_on,
createTriggerPendingIntent(context, appWidgetId * 10 + 2, device.id, TriggerAction.POWER_ON)
)
// Reboot click
views.setOnClickPendingIntent(
R.id.widget_btn_reboot,
createTriggerPendingIntent(context, appWidgetId * 10 + 3, device.id, TriggerAction.REBOOT)
)
// Power Off click
views.setOnClickPendingIntent(
R.id.widget_btn_power_off,
createTriggerPendingIntent(context, appWidgetId * 10 + 4, device.id, TriggerAction.POWER_OFF)
)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
} catch (e: Exception) {
e.printStackTrace()
}
views.setTextViewText(R.id.widget_device_name, device.name)
views.setTextViewText(R.id.widget_node_name, device.node)
// Status display
if (status == null) {
views.setTextViewText(R.id.widget_status_text, "SYNCING")
views.setTextColor(R.id.widget_status_text, 0xFF71717A.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_gray)
} else if (status.isOnline) {
views.setTextViewText(R.id.widget_status_text, "ONLINE")
views.setTextColor(R.id.widget_status_text, 0xFF22C55E.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_green)
} else {
views.setTextViewText(R.id.widget_status_text, "OFFLINE")
views.setTextColor(R.id.widget_status_text, 0xFFEF4444.toInt())
views.setImageViewResource(R.id.widget_status_dot, R.drawable.widget_dot_red)
}
views.setTextViewText(
R.id.widget_footer_text,
footerOverride ?: (if (status?.uptime != null) "Up: ${status.uptime}" else "Tap header to open • Tap button to trigger")
)
// Header click -> Open app
val openAppIntent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingOpenApp = PendingIntent.getActivity(
context,
appWidgetId * 10,
openAppIntent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
views.setOnClickPendingIntent(R.id.widget_header_clickable, pendingOpenApp)
// Refresh click
val refreshIntent = Intent(context, ServerWidgetProvider::class.java).apply {
action = ACTION_REFRESH
}
val pendingRefresh = PendingIntent.getBroadcast(
context,
appWidgetId * 10 + 1,
refreshIntent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
views.setOnClickPendingIntent(R.id.widget_btn_refresh, pendingRefresh)
// Power On click
views.setOnClickPendingIntent(
R.id.widget_btn_power_on,
createTriggerPendingIntent(context, appWidgetId * 10 + 2, device.id, TriggerAction.POWER_ON)
)
// Reboot click
views.setOnClickPendingIntent(
R.id.widget_btn_reboot,
createTriggerPendingIntent(context, appWidgetId * 10 + 3, device.id, TriggerAction.REBOOT)
)
// Power Off click
views.setOnClickPendingIntent(
R.id.widget_btn_power_off,
createTriggerPendingIntent(context, appWidgetId * 10 + 4, device.id, TriggerAction.POWER_OFF)
)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
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(
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) {
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
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/ic_terminal"
android:src="@drawable/widget_ic_terminal"
android:contentDescription="Server" />
<LinearLayout
@@ -82,14 +82,13 @@
android:id="@+id/widget_btn_refresh"
android:layout_width="26dp"
android:layout_height="26dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="Refresh"
android:padding="3dp"
android:src="@drawable/ic_refresh" />
android:src="@drawable/widget_ic_refresh" />
</LinearLayout>
<!-- Spacer / Divider -->
<View
<!-- Spacer / Divider (FrameLayout is explicitly whitelisted in RemoteViews) -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
+6 -2
View File
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="100dp"
android:minWidth="220dp"
android:minHeight="90dp"
android:targetCellWidth="3"
android:targetCellHeight="2"
android:minResizeWidth="180dp"
android:minResizeHeight="70dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_server_control"
android:resizeMode="horizontal|vertical"