package org.pictalk.plugin.alarm import android.annotation.SuppressLint import android.content.Intent import android.content.pm.ActivityInfo import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri import android.os.Build import android.os.Bundle import android.view.WindowManager import android.view.View import android.widget.Button import android.widget.ImageView import android.widget.TextView import androidx.fragment.app.FragmentActivity import com.getcapacitor.Logger import kotlinx.serialization.json.Json import org.pictalk.plugin.alarm.alarm.AlarmService import org.pictalk.plugin.alarm.models.AlarmSettings import java.io.InputStream import java.text.SimpleDateFormat import java.util.* /** * Alarm Clock alarm alert: pops visible indicator and plays alarm tone. This activity is the full * screen version which shows over the lock screen with the wallpaper as the background. * * Adapted from the AlarmClock reference implementation for the Capacitor alarm plugin. */ class AlarmAlertFullScreen : FragmentActivity() { companion object { private const val TAG = "AlarmAlertFullScreen" const val EXTRA_ALARM_ID = "alarm_id" const val EXTRA_ALARM_SETTINGS = "alarm_settings" } private var alarmId: Int = -1 private var alarmSettings: AlarmSettings? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Set orientation based on device type requestedOrientation = if (isTablet()) { // Preserve initial rotation and disable rotation change on tablets if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) { ActivityInfo.SCREEN_ORIENTATION_PORTRAIT } else { ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE } } else { // Portrait on smartphone ActivityInfo.SCREEN_ORIENTATION_PORTRAIT } // Get alarm data from intent alarmId = intent.getIntExtra(EXTRA_ALARM_ID, -1) val alarmSettingsJson = intent.getStringExtra(EXTRA_ALARM_SETTINGS) if (alarmId == -1 || alarmSettingsJson == null) { Logger.info(TAG, "Missing alarm ID or settings") finish() return } try { alarmSettings = Json.decodeFromString(alarmSettingsJson) } catch (e: Exception) { Logger.error(TAG, "Failed to parse alarm settings", e) finish() return } turnScreenOn() updateLayout() } private fun isTablet(): Boolean { return resources.getBoolean(R.bool.isTablet) } /** * Turns the screen on and shows over the lock screen. * * Based on the reference implementation, handling different API levels properly. */ private fun turnScreenOn() { if (Build.VERSION.SDK_INT >= 27) { setShowWhenLocked(true) setTurnScreenOn(true) } // Deprecated flags are required on some devices, even with API>=27 window.addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON ) } private fun updateLayout() { setContentView(R.layout.activity_alarm_alert_fullscreen) // Set up dismiss button findViewById