From 38ebf30f6a423dd087f5ee7851fe8ab551adf3e1 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:09:26 +0800 Subject: [PATCH] Import apps/android/plugins/remote-foreground-service/RemoteForegroundServiceModule.kt --- .../RemoteForegroundServiceModule.kt | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 apps/android/plugins/remote-foreground-service/RemoteForegroundServiceModule.kt diff --git a/apps/android/plugins/remote-foreground-service/RemoteForegroundServiceModule.kt b/apps/android/plugins/remote-foreground-service/RemoteForegroundServiceModule.kt new file mode 100644 index 0000000..5367e85 --- /dev/null +++ b/apps/android/plugins/remote-foreground-service/RemoteForegroundServiceModule.kt @@ -0,0 +1,146 @@ +package io.github.liguobao.dshremote + +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Build +import android.os.PowerManager +import android.provider.Settings +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContextBaseJavaModule +import com.facebook.react.bridge.ReactMethod +import com.facebook.react.bridge.Promise + +class RemoteForegroundServiceModule( + private val context: ReactApplicationContext, +) : ReactContextBaseJavaModule(context) { + override fun getName(): String = "DshRemoteForegroundService" + + @ReactMethod + fun start(promise: Promise) { + context.runOnUiQueueThread { RemoteForegroundService.start(context, promise) } + } + + @ReactMethod + fun notifyTask(hostId: String, sessionId: String, title: String, body: String) { + RemoteForegroundService.notifyTask(context, hostId, sessionId, title, body) + } + + @ReactMethod + fun stop(promise: Promise) { + context.runOnUiQueueThread { + RemoteForegroundService.stop(context) + promise.resolve(null) + } + } + + @ReactMethod + fun setStatus(status: String, promise: Promise) { + context.runOnUiQueueThread { + RemoteForegroundService.setStatus(status) + promise.resolve(null) + } + } + + @ReactMethod + fun updateLiveTask( + hostId: String, + sessionId: String, + badge: String, + title: String, + statusText: String, + isWaitingInput: Boolean, + updatedAtMs: Double, + summary: String, + promise: Promise, + ) { + context.runOnUiQueueThread { + try { + val boundedTitle = title.take(150) + val boundedBadge = badge.take(10) + val boundedStatus = statusText.take(50) + val safeTime = if (updatedAtMs.isFinite() && updatedAtMs > 0) updatedAtMs.toLong() else 0L + val boundedSummary = summary.take(400) + RemoteForegroundService.updateLiveTask( + hostId, sessionId, boundedBadge, boundedTitle, boundedStatus, isWaitingInput, safeTime, boundedSummary + ) + promise.resolve(null) + } catch (e: Exception) { + promise.reject("UPDATE_LIVE_TASK_FAILED", e.message, e) + } + } + } + + @ReactMethod + fun clearLiveTask(promise: Promise) { + context.runOnUiQueueThread { + try { + RemoteForegroundService.clearLiveTask() + promise.resolve(null) + } catch (e: Exception) { + promise.reject("CLEAR_LIVE_TASK_FAILED", e.message, e) + } + } + } + + @ReactMethod + fun waitUntilStopped(lifetimeId: String, promise: Promise) { + context.runOnUiQueueThread { RemoteForegroundService.waitUntilStopped(lifetimeId, promise) } + } + + @ReactMethod + fun isIgnoringBatteryOptimizations(promise: Promise) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager + promise.resolve(pm?.isIgnoringBatteryOptimizations(context.packageName) ?: false) + } else { + promise.resolve(true) + } + } + + @ReactMethod + fun requestIgnoreBatteryOptimizations(promise: Promise) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager + if (pm != null && pm.isIgnoringBatteryOptimizations(context.packageName)) { + promise.resolve(true) + return + } + try { + val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply { + data = Uri.parse("package:${context.packageName}") + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + context.startActivity(intent) + promise.resolve(true) + } catch (_: Exception) { + try { + val fallback = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS).apply { + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + context.startActivity(fallback) + promise.resolve(true) + } catch (_: Exception) { + try { + val appDetails = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { + data = Uri.parse("package:${context.packageName}") + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + context.startActivity(appDetails) + promise.resolve(true) + } catch (e: Exception) { + promise.reject("BATTERY_SETTING_FAILED", e.message) + } + } + } + } else { + promise.resolve(true) + } + } + + override fun invalidate() { + context.runOnUiQueueThread { RemoteForegroundService.stop(context) } + super.invalidate() + } +} +