panicAlarm

This module provides functionality to send panic alarms to the Smart1 SDK API.

Overview

The panicAlarm module allows you to:

  • Send panic alarms with device location

  • Automatically capture device information (GPS, battery, OS, etc.)

  • Handle emergency situations with real-time alerts

  • Support offline detection

Main Components

SendPanicAlarm

Main use case for sending panic alarms to the API with automatic device information capture.

Usage Examples

Basic Panic Alarm

import com.servinformacion.smart1sdk.android.panicAlarm.SendPanicAlarm
import com.servinformacion.smart1sdk.android.core.model.CoordinatesData
import com.servinformacion.smart1sdk.android.core.ResultS1SDK

class EmergencyViewModel : ViewModel() {

private val sendPanicAlarm = SendPanicAlarm()

fun triggerPanicAlarm(latitude: Double, longitude: Double) {
viewModelScope.launch {
val coordinates = CoordinatesData(
latitude = latitude,
longitude = longitude
)

val result = sendPanicAlarm(
coordinates = coordinates
)

when (result) {
is ResultS1SDK.Success -> {
// Panic alarm sent successfully
showSuccessMessage()
}
is ResultS1SDK.Error -> {
// Handle error
showErrorMessage(result.error)
}
}
}
}
}

Panic Alarm from User Action

suspend fun handlePanicButtonPress(latitude: Double, longitude: Double) {
val sendPanicAlarm = SendPanicAlarm()

val coordinates = CoordinatesData(
latitude = latitude,
longitude = longitude
)

val result = sendPanicAlarm(coordinates = coordinates)

when (result) {
is ResultS1SDK.Success -> {
println("Panic alarm sent successfully")
notifyUser("Emergency alert sent")
}
is ResultS1SDK.Error -> {
println("Failed to send panic alarm: ${result.error}")
showRetryOption()
}
}
}

Panic Button Implementation

@Composable
fun PanicButton(
onPanicSent: () -> Unit,
onError: (ErrorS1SDK) -> Unit
) {
val context = LocalContext.current
val sendPanicAlarm = remember { SendPanicAlarm() }
val scope = rememberCoroutineScope()
var isLoading by remember { mutableStateOf(false) }

Button(
onClick = {
scope.launch {
isLoading = true

// Get current location (simplified)
val coordinates = getCurrentLocation(context)

val result = sendPanicAlarm(coordinates = coordinates)

isLoading = false

when (result) {
is ResultS1SDK.Success -> onPanicSent()
is ResultS1SDK.Error -> onError(result.error)
}
}
},
enabled = !isLoading,
colors = ButtonDefaults.buttonColors(
containerColor = Color.Red
),
modifier = Modifier
.size(120.dp)
.clip(CircleShape)
) {
if (isLoading) {
CircularProgressIndicator(color = Color.White)
} else {
Text(
text = "PANIC",
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
}
}

With Custom Session Token

suspend fun sendPanicAlarmWithCustomSession(
coordinates: CoordinatesData,
customToken: String
) {
val sendPanicAlarm = SendPanicAlarm()

val result = sendPanicAlarm(
coordinates = coordinates,
sessionToken = customToken
)

// Handle result
}

Complete Emergency Handler

class EmergencyService(
private val locationProvider: LocationProvider,
private val notificationManager: NotificationManager
) {
private val sendPanicAlarm = SendPanicAlarm()

suspend fun handleEmergency(companyId: Int? = null) {
// Show immediate notification
showEmergencyNotification()

// Get current location
val location = locationProvider.getCurrentLocation()

if (location == null) {
// Use last known location or default
handleLocationUnavailable()
return
}

val coordinates = CoordinatesData(
latitude = location.latitude,
longitude = location.longitude
)

// Send panic alarm
val result = sendPanicAlarm(
coordinates = coordinates,
companyId = companyId
)

when (result) {
is ResultS1SDK.Success -> {
// Alarm sent successfully
updateNotification("Emergency alert sent")
logEmergencyEvent(coordinates)
notifyLocalContacts()
}
is ResultS1SDK.Error -> {
// Handle error - maybe queue for retry
handleSendError(result.error, coordinates)
}
}
}

private fun showEmergencyNotification() {
// Show high-priority notification
}

private fun handleLocationUnavailable() {
// Handle case when location is not available
}

private fun handleSendError(error: ErrorS1SDK, coordinates: CoordinatesData) {
// Queue for retry or show error to user
}
}

Important Notes

  1. Location Permissions: Ensure your app has location permissions before calling this use case.

  2. Coordinates Validation: The SDK automatically validates:

    • Latitude must be between -90 and 90

    • Longitude must be between -180 and 180

  3. Company ID:

    • If not provided, it will be obtained from the current session

    • Must be greater than 0

  4. Automatic Data Capture: The SDK automatically captures:

    • GPS status

    • Battery level

    • App version

    • OS information

    • Timestamps (local and UTC)

    • Timezone

  5. Session Handling:

    • Automatically uses session from SDK setup

    • Can provide custom session token if needed

  6. Error Handling: Always handle potential errors:

    • Invalid coordinates

    • Missing company ID

    • Network errors

    • Session errors

  7. Emergency Response: This is a critical feature - ensure:

    • Fast response time

    • Proper error handling

    • User feedback

    • Retry mechanism for failures

Response

The use case returns ResultS1SDK<Boolean, ErrorS1SDK>:

  • Success: true when panic alarm is sent successfully

  • Error: Contains error information

Common Errors

  • CommonError.InvalidInputData - Invalid coordinates, session token, or company ID

  • ApiError.ExpiredToken - Session token expired (handled automatically)

  • ApiError.Unauthorized - Invalid authentication

  • ApiError.NetworkError - Network connectivity issues

Packages