2026-11-18 13:05:07 +05:30
|
|
|
package com.expapp.trigger
|
|
|
|
|
|
2026-11-19 00:27:15 +05:30
|
|
|
import android.Manifest
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
import android.nfc.NfcAdapter
|
|
|
|
|
import android.nfc.Tag
|
|
|
|
|
import android.os.Build
|
2026-11-18 13:05:07 +05:30
|
|
|
import android.os.Bundle
|
2026-11-19 00:27:15 +05:30
|
|
|
import android.os.VibrationEffect
|
|
|
|
|
import android.os.Vibrator
|
|
|
|
|
import android.os.VibratorManager
|
|
|
|
|
import android.widget.Toast
|
2026-11-18 13:05:07 +05:30
|
|
|
import androidx.activity.compose.setContent
|
|
|
|
|
import androidx.activity.enableEdgeToEdge
|
2026-11-19 00:27:15 +05:30
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
2026-11-18 13:05:07 +05:30
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
|
|
import androidx.compose.material3.Surface
|
2026-11-19 00:27:15 +05:30
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
|
import androidx.compose.runtime.setValue
|
2026-11-18 13:05:07 +05:30
|
|
|
import androidx.compose.ui.Modifier
|
2026-11-19 00:27:15 +05:30
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import androidx.fragment.app.FragmentActivity
|
|
|
|
|
import com.expapp.trigger.data.model.DeviceConfig
|
|
|
|
|
import com.expapp.trigger.data.model.TriggerLog
|
|
|
|
|
import com.expapp.trigger.data.repository.AppSettings
|
2026-11-18 13:05:07 +05:30
|
|
|
import com.expapp.trigger.data.repository.DeviceRepository
|
|
|
|
|
import com.expapp.trigger.data.repository.SharedPrefsStorage
|
2026-11-19 00:27:15 +05:30
|
|
|
import com.expapp.trigger.data.repository.TriggerHistoryRepository
|
|
|
|
|
import com.expapp.trigger.network.TriggerAction
|
2026-11-18 13:05:07 +05:30
|
|
|
import com.expapp.trigger.network.TriggerClient
|
2026-11-19 00:27:15 +05:30
|
|
|
import com.expapp.trigger.nfc.NfcHelper
|
|
|
|
|
import com.expapp.trigger.nfc.NfcTriggerPayload
|
|
|
|
|
import com.expapp.trigger.security.BiometricHelper
|
2026-11-18 13:05:07 +05:30
|
|
|
import com.expapp.trigger.ui.screens.MainScreen
|
|
|
|
|
import com.expapp.trigger.ui.theme.DarkBackground
|
|
|
|
|
import com.expapp.trigger.ui.theme.ExpAppTheme
|
2026-11-19 00:27:15 +05:30
|
|
|
import com.expapp.trigger.widget.ServerWidgetProvider
|
2026-11-19 00:43:16 +05:30
|
|
|
import com.expapp.trigger.worker.NotificationHelper
|
2026-11-19 00:27:15 +05:30
|
|
|
import com.expapp.trigger.worker.ServerMonitorWorker
|
|
|
|
|
|
|
|
|
|
class MainActivity : FragmentActivity() {
|
|
|
|
|
|
|
|
|
|
private lateinit var repository: DeviceRepository
|
|
|
|
|
private lateinit var triggerClient: TriggerClient
|
|
|
|
|
private lateinit var appSettings: AppSettings
|
|
|
|
|
private lateinit var historyRepository: TriggerHistoryRepository
|
|
|
|
|
|
|
|
|
|
private var activeNfcWritePayload by mutableStateOf<NfcTriggerPayload?>(null)
|
|
|
|
|
private var nfcWriteResult by mutableStateOf<Result<Unit>?>(null)
|
|
|
|
|
|
2026-11-19 00:43:16 +05:30
|
|
|
private var isNotificationPermissionGranted by mutableStateOf(false)
|
|
|
|
|
private var onNotificationPermissionGrantedCallback: (() -> Unit)? = null
|
|
|
|
|
|
2026-11-19 00:27:15 +05:30
|
|
|
private val notificationPermissionLauncher =
|
|
|
|
|
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
|
2026-11-19 00:43:16 +05:30
|
|
|
isNotificationPermissionGranted = isGranted || NotificationHelper.areNotificationsEnabled(this)
|
|
|
|
|
if (isGranted) {
|
|
|
|
|
NotificationHelper.createNotificationChannel(this)
|
|
|
|
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
|
|
|
|
ServerMonitorWorker.schedule(this)
|
|
|
|
|
}
|
|
|
|
|
onNotificationPermissionGrantedCallback?.invoke()
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "Notification permission is required for alerts", Toast.LENGTH_SHORT).show()
|
2026-11-19 00:27:15 +05:30
|
|
|
}
|
2026-11-19 00:43:16 +05:30
|
|
|
onNotificationPermissionGrantedCallback = null
|
2026-11-19 00:27:15 +05:30
|
|
|
}
|
2026-11-18 13:05:07 +05:30
|
|
|
|
2026-11-19 00:43:16 +05:30
|
|
|
fun requestNotificationPermission(onGranted: () -> Unit = {}) {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
|
|
|
|
|
== PackageManager.PERMISSION_GRANTED
|
|
|
|
|
) {
|
|
|
|
|
isNotificationPermissionGranted = true
|
|
|
|
|
onGranted()
|
|
|
|
|
} else {
|
|
|
|
|
onNotificationPermissionGrantedCallback = onGranted
|
|
|
|
|
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
isNotificationPermissionGranted = NotificationHelper.areNotificationsEnabled(this)
|
|
|
|
|
onGranted()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:05:07 +05:30
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
enableEdgeToEdge()
|
|
|
|
|
|
2026-11-19 00:27:15 +05:30
|
|
|
repository = DeviceRepository(SharedPrefsStorage(applicationContext))
|
2026-11-19 01:05:42 +05:30
|
|
|
val settingsPrefs = getSharedPreferences("app_settings_prefs", Context.MODE_PRIVATE)
|
|
|
|
|
val hasSeeded = settingsPrefs.getBoolean("has_seeded_default_device", false)
|
|
|
|
|
if (!hasSeeded) {
|
|
|
|
|
settingsPrefs.edit().putBoolean("has_seeded_default_device", true).apply()
|
|
|
|
|
if (repository.getDevices().isEmpty()) {
|
|
|
|
|
val defaultDevice = DeviceConfig(
|
|
|
|
|
id = "2dc98ae9-9f71-4f29-95a6-a59ac94d7bf6",
|
|
|
|
|
name = "Server",
|
|
|
|
|
node = "debian",
|
|
|
|
|
powerOffUrl = "https://cosmos.linuxguy.space/poweroff?node=debian",
|
|
|
|
|
powerOnUrl = "https://trigger.macrodroid.com/87f2f62c-09ee-43cb-8796-fd1a6465754b/wakeserver",
|
|
|
|
|
r2Url = "https://bucket.linuxguy.space/home-server-app/endpoints.json",
|
|
|
|
|
rebootUrl = "https://cosmos.linuxguy.space/reboot?node=debian",
|
|
|
|
|
secretKey = "Carbon@12",
|
|
|
|
|
statusUrl = "https://cosmos.linuxguy.space/status?node=debian"
|
|
|
|
|
)
|
|
|
|
|
repository.addDevice(defaultDevice)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-19 00:27:15 +05:30
|
|
|
triggerClient = TriggerClient()
|
|
|
|
|
appSettings = AppSettings(applicationContext)
|
|
|
|
|
historyRepository = TriggerHistoryRepository(applicationContext)
|
|
|
|
|
|
2026-11-19 00:43:16 +05:30
|
|
|
NotificationHelper.createNotificationChannel(this)
|
|
|
|
|
isNotificationPermissionGranted = NotificationHelper.areNotificationsEnabled(this)
|
|
|
|
|
|
2026-11-19 00:27:15 +05:30
|
|
|
// Request notification permission on Android 13+
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
|
|
|
|
|
!= PackageManager.PERMISSION_GRANTED
|
|
|
|
|
) {
|
|
|
|
|
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize background server monitoring
|
|
|
|
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
|
|
|
|
ServerMonitorWorker.schedule(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle initial launch intent (from NFC or Widget)
|
|
|
|
|
handleIntent(intent)
|
2026-11-18 13:05:07 +05:30
|
|
|
|
|
|
|
|
setContent {
|
|
|
|
|
ExpAppTheme {
|
|
|
|
|
Surface(
|
|
|
|
|
modifier = Modifier.fillMaxSize(),
|
|
|
|
|
color = DarkBackground
|
|
|
|
|
) {
|
|
|
|
|
MainScreen(
|
|
|
|
|
repository = repository,
|
2026-11-19 00:27:15 +05:30
|
|
|
triggerClient = triggerClient,
|
|
|
|
|
appSettings = appSettings,
|
|
|
|
|
historyRepository = historyRepository,
|
|
|
|
|
requestBiometricAuth = { title, subtitle, onResult ->
|
|
|
|
|
BiometricHelper.authenticate(this, title, subtitle) { success, _ ->
|
|
|
|
|
onResult(success)
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-11-19 00:43:16 +05:30
|
|
|
isNotificationPermissionGranted = isNotificationPermissionGranted,
|
|
|
|
|
onRequestNotificationPermission = {
|
|
|
|
|
requestNotificationPermission()
|
|
|
|
|
},
|
|
|
|
|
onSendTestNotification = {
|
|
|
|
|
val ok = NotificationHelper.sendTestNotification(this)
|
|
|
|
|
if (ok) {
|
|
|
|
|
Toast.makeText(this, "Test notification sent!", Toast.LENGTH_SHORT).show()
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "Notification failed to send. Check system permissions.", Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-11-19 00:27:15 +05:30
|
|
|
nfcWriteResult = nfcWriteResult,
|
|
|
|
|
onStartNfcWriting = { payload ->
|
|
|
|
|
nfcWriteResult = null
|
|
|
|
|
activeNfcWritePayload = payload
|
|
|
|
|
},
|
|
|
|
|
onStopNfcWriting = {
|
|
|
|
|
activeNfcWritePayload = null
|
|
|
|
|
nfcWriteResult = null
|
|
|
|
|
}
|
2026-11-18 13:05:07 +05:30
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-19 00:27:15 +05:30
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
|
super.onResume()
|
2026-11-19 00:43:16 +05:30
|
|
|
isNotificationPermissionGranted = NotificationHelper.areNotificationsEnabled(this)
|
2026-11-19 00:27:15 +05:30
|
|
|
if (NfcHelper.isNfcSupported(this)) {
|
|
|
|
|
NfcHelper.enableForegroundDispatch(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
|
super.onPause()
|
|
|
|
|
if (NfcHelper.isNfcSupported(this)) {
|
|
|
|
|
NfcHelper.disableForegroundDispatch(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 00:43:16 +05:30
|
|
|
override fun onStop() {
|
|
|
|
|
super.onStop()
|
|
|
|
|
// When leaving the app, trigger an immediate one-time check & ensure worker is scheduled
|
|
|
|
|
if (appSettings.isBackgroundMonitoringEnabled) {
|
|
|
|
|
ServerMonitorWorker.runOnce(this)
|
|
|
|
|
ServerMonitorWorker.schedule(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 00:27:15 +05:30
|
|
|
override fun onNewIntent(intent: Intent) {
|
|
|
|
|
super.onNewIntent(intent)
|
|
|
|
|
setIntent(intent)
|
|
|
|
|
handleIntent(intent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun handleIntent(intent: Intent?) {
|
|
|
|
|
if (intent == null) return
|
|
|
|
|
|
|
|
|
|
// 1. NFC Tag write handling
|
|
|
|
|
val writePayload = activeNfcWritePayload
|
|
|
|
|
if (writePayload != null) {
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
|
val tag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
|
|
|
|
|
} else {
|
|
|
|
|
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
|
|
|
|
}
|
|
|
|
|
if (tag != null) {
|
|
|
|
|
val message = NfcHelper.createNdefMessage(writePayload.nodeOrDeviceId, writePayload.action)
|
|
|
|
|
val result = NfcHelper.writeTag(tag, message)
|
|
|
|
|
nfcWriteResult = result
|
|
|
|
|
vibrateFeedback(result.isSuccess)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. NFC Tag trigger execution
|
|
|
|
|
val nfcPayload = NfcHelper.parseTriggerPayload(intent)
|
|
|
|
|
if (nfcPayload != null) {
|
|
|
|
|
val devices = repository.getDevices()
|
|
|
|
|
val device = devices.find { it.node.equals(nfcPayload.nodeOrDeviceId, ignoreCase = true) || it.id == nfcPayload.nodeOrDeviceId }
|
|
|
|
|
if (device != null) {
|
|
|
|
|
executeExternalTrigger(device, nfcPayload.action, "NFC")
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "NFC Tag: Unknown server (${nfcPayload.nodeOrDeviceId})", Toast.LENGTH_LONG).show()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Widget biometric trigger launch
|
|
|
|
|
if (intent.action == ServerWidgetProvider.ACTION_TRIGGER) {
|
|
|
|
|
val deviceId = intent.getStringExtra(ServerWidgetProvider.EXTRA_DEVICE_ID)
|
|
|
|
|
val actionName = intent.getStringExtra(ServerWidgetProvider.EXTRA_ACTION)
|
|
|
|
|
if (deviceId != null && actionName != null) {
|
|
|
|
|
val action = try { TriggerAction.valueOf(actionName) } catch (e: Exception) { null }
|
|
|
|
|
val device = repository.getDevices().find { it.id == deviceId }
|
|
|
|
|
if (device != null && action != null) {
|
|
|
|
|
executeExternalTrigger(device, action, "Widget")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun executeExternalTrigger(device: DeviceConfig, action: TriggerAction, source: String) {
|
|
|
|
|
val actionName = when (action) {
|
|
|
|
|
TriggerAction.POWER_ON -> "Power On"
|
|
|
|
|
TriggerAction.REBOOT -> "Reboot"
|
|
|
|
|
TriggerAction.POWER_OFF -> "Shutdown"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val onAuthorized = {
|
|
|
|
|
vibrateFeedback(true)
|
|
|
|
|
Toast.makeText(this, "Sending $actionName signal to ${device.name}...", Toast.LENGTH_SHORT).show()
|
|
|
|
|
triggerClient.sendTrigger(device, action) { response ->
|
|
|
|
|
val msg = if (response.isSuccess) {
|
|
|
|
|
"$actionName signal sent successfully"
|
|
|
|
|
} else {
|
|
|
|
|
"Failed: ${response.message.take(50)}"
|
|
|
|
|
}
|
|
|
|
|
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
|
|
|
|
historyRepository.addLog(
|
|
|
|
|
TriggerLog(
|
|
|
|
|
deviceId = device.id,
|
|
|
|
|
deviceName = device.name,
|
|
|
|
|
node = device.node,
|
|
|
|
|
action = action,
|
|
|
|
|
isSuccess = response.isSuccess,
|
|
|
|
|
statusCode = response.statusCode,
|
|
|
|
|
message = response.message,
|
|
|
|
|
source = source
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
ServerWidgetProvider.updateAllWidgets(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (appSettings.isBiometricEnabled && BiometricHelper.isBiometricAvailable(this)) {
|
|
|
|
|
BiometricHelper.authenticate(
|
|
|
|
|
this,
|
|
|
|
|
title = "Confirm $actionName",
|
|
|
|
|
subtitle = "Authenticate with fingerprint to trigger ${device.name}"
|
|
|
|
|
) { success, errorMsg ->
|
|
|
|
|
if (success) {
|
|
|
|
|
onAuthorized()
|
|
|
|
|
} else if (errorMsg != null) {
|
|
|
|
|
Toast.makeText(this, "Authentication cancelled", Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
onAuthorized()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun vibrateFeedback(success: Boolean) {
|
|
|
|
|
try {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
|
|
|
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as? VibratorManager
|
|
|
|
|
val vibrator = vibratorManager?.defaultVibrator
|
|
|
|
|
vibrator?.vibrate(
|
|
|
|
|
VibrationEffect.createOneShot(
|
|
|
|
|
if (success) 80 else 200,
|
|
|
|
|
VibrationEffect.DEFAULT_AMPLITUDE
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
|
val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
vibrator?.vibrate(
|
|
|
|
|
VibrationEffect.createOneShot(
|
|
|
|
|
if (success) 80 else 200,
|
|
|
|
|
VibrationEffect.DEFAULT_AMPLITUDE
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
|
vibrator?.vibrate(if (success) 80 else 200)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
// Ignore if vibration is not supported
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-18 13:05:07 +05:30
|
|
|
}
|