|
1 | 1 | package com.example.on_time_front |
2 | 2 |
|
| 3 | +import android.app.AlarmManager |
| 4 | +import android.app.PendingIntent |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.os.Bundle |
3 | 8 | import io.flutter.embedding.android.FlutterActivity |
| 9 | +import io.flutter.embedding.engine.FlutterEngine |
| 10 | +import io.flutter.plugin.common.MethodCall |
| 11 | +import io.flutter.plugin.common.MethodChannel |
4 | 12 |
|
5 | | -class MainActivity: FlutterActivity() |
| 13 | +class MainActivity : FlutterActivity() { |
| 14 | + private var methodChannel: MethodChannel? = null |
| 15 | + |
| 16 | + companion object { |
| 17 | + private const val CHANNEL_NAME = "on_time_front/native_alarm" |
| 18 | + const val ACTION_SCHEDULE_ALARM = "on_time_front.SCHEDULE_ALARM" |
| 19 | + private var launchPayload: Map<String, String>? = null |
| 20 | + } |
| 21 | + |
| 22 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 23 | + super.onCreate(savedInstanceState) |
| 24 | + payloadFromIntent(intent)?.let { launchPayload = it } |
| 25 | + } |
| 26 | + |
| 27 | + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { |
| 28 | + super.configureFlutterEngine(flutterEngine) |
| 29 | + methodChannel = MethodChannel( |
| 30 | + flutterEngine.dartExecutor.binaryMessenger, |
| 31 | + CHANNEL_NAME, |
| 32 | + ) |
| 33 | + methodChannel?.setMethodCallHandler { call, result -> |
| 34 | + when (call.method) { |
| 35 | + "getCapabilities" -> result.success( |
| 36 | + mapOf( |
| 37 | + "supportsNativeAlarm" to true, |
| 38 | + "nativeAlarmProvider" to "androidAlarmManager", |
| 39 | + "fallbackProvider" to "localNotification", |
| 40 | + ), |
| 41 | + ) |
| 42 | + "checkPermission" -> result.success("granted") |
| 43 | + "requestPermission" -> result.success("granted") |
| 44 | + "scheduleNativeAlarm" -> scheduleNativeAlarm(call, result) |
| 45 | + "cancelNativeAlarm" -> cancelNativeAlarm(call, result) |
| 46 | + "getLaunchPayload" -> { |
| 47 | + result.success(launchPayload) |
| 48 | + launchPayload = null |
| 49 | + } |
| 50 | + else -> result.notImplemented() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + override fun onNewIntent(intent: Intent) { |
| 56 | + super.onNewIntent(intent) |
| 57 | + setIntent(intent) |
| 58 | + payloadFromIntent(intent)?.let { |
| 59 | + launchPayload = it |
| 60 | + methodChannel?.invokeMethod("alarmLaunch", it) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun scheduleNativeAlarm(call: MethodCall, result: MethodChannel.Result) { |
| 65 | + val args = call.arguments as? Map<*, *> |
| 66 | + if (args == null) { |
| 67 | + result.error("invalidArguments", "Missing alarm arguments", null) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + val triggerAtMillis = (args["alarmTime"] as? Number)?.toLong() |
| 72 | + val scheduleId = args["scheduleId"]?.toString() |
| 73 | + if (triggerAtMillis == null || scheduleId.isNullOrEmpty()) { |
| 74 | + result.error("invalidArguments", "Missing scheduleId or alarmTime", null) |
| 75 | + return |
| 76 | + } |
| 77 | + if (triggerAtMillis <= System.currentTimeMillis()) { |
| 78 | + result.error("invalidArguments", "Cannot schedule a past alarm", null) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + val alarmManager = getSystemService(Context.ALARM_SERVICE) as? AlarmManager |
| 83 | + if (alarmManager == null) { |
| 84 | + result.error("unsupported", "AlarmManager is unavailable", null) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + val pendingIntent = pendingIntentFor(args, PendingIntent.FLAG_UPDATE_CURRENT) |
| 89 | + val alarmClockInfo = AlarmManager.AlarmClockInfo(triggerAtMillis, pendingIntent) |
| 90 | + alarmManager.setAlarmClock(alarmClockInfo, pendingIntent) |
| 91 | + result.success(null) |
| 92 | + } |
| 93 | + |
| 94 | + private fun cancelNativeAlarm(call: MethodCall, result: MethodChannel.Result) { |
| 95 | + val args = call.arguments as? Map<*, *> |
| 96 | + if (args == null) { |
| 97 | + result.success(null) |
| 98 | + return |
| 99 | + } |
| 100 | + val alarmManager = getSystemService(Context.ALARM_SERVICE) as? AlarmManager |
| 101 | + val pendingIntent = pendingIntentFor(args, PendingIntent.FLAG_NO_CREATE) |
| 102 | + if (alarmManager != null && pendingIntent != null) { |
| 103 | + alarmManager.cancel(pendingIntent) |
| 104 | + pendingIntent.cancel() |
| 105 | + } |
| 106 | + result.success(null) |
| 107 | + } |
| 108 | + |
| 109 | + private fun pendingIntentFor(args: Map<*, *>, lookupFlag: Int): PendingIntent? { |
| 110 | + val requestCode = (args["nativeAlarmId"] as? Number)?.toInt() |
| 111 | + ?: args["scheduleId"].toString().hashCode() |
| 112 | + val intent = Intent(this, MainActivity::class.java).apply { |
| 113 | + action = ACTION_SCHEDULE_ALARM |
| 114 | + flags = Intent.FLAG_ACTIVITY_NEW_TASK or |
| 115 | + Intent.FLAG_ACTIVITY_CLEAR_TOP or |
| 116 | + Intent.FLAG_ACTIVITY_SINGLE_TOP |
| 117 | + putExtra("type", "schedule_alarm") |
| 118 | + putExtra("scheduleId", args["scheduleId"]?.toString()) |
| 119 | + putExtra("promptVariant", "alarm") |
| 120 | + putExtra("alarmTime", args["alarmTime"]?.toString()) |
| 121 | + putExtra("preparationStartTime", args["preparationStartTime"]?.toString()) |
| 122 | + |
| 123 | + val payload = args["payload"] as? Map<*, *> |
| 124 | + payload?.forEach { (key, value) -> |
| 125 | + if (key != null && value != null) { |
| 126 | + putExtra(key.toString(), value.toString()) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + val flags = lookupFlag or PendingIntent.FLAG_IMMUTABLE |
| 131 | + return PendingIntent.getActivity(this, requestCode, intent, flags) |
| 132 | + } |
| 133 | + |
| 134 | + private fun payloadFromIntent(intent: Intent?): Map<String, String>? { |
| 135 | + if (intent?.action != ACTION_SCHEDULE_ALARM) return null |
| 136 | + val extras = intent.extras ?: return null |
| 137 | + val payload = mutableMapOf<String, String>() |
| 138 | + for (key in extras.keySet()) { |
| 139 | + extras.get(key)?.let { payload[key] = it.toString() } |
| 140 | + } |
| 141 | + payload["type"] = "schedule_alarm" |
| 142 | + payload["promptVariant"] = "alarm" |
| 143 | + return payload |
| 144 | + } |
| 145 | +} |
0 commit comments