opt.: dismiss notification if no ssh conn (#592)

This commit is contained in:
lollipopkit🏳️‍⚧️
2024-09-24 22:01:35 +08:00
committed by GitHub
parent 47aedb2f2e
commit aef317a140
5 changed files with 59 additions and 16 deletions

View File

@@ -14,12 +14,17 @@ class ForegroundService : Service() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = createNotification() when (intent?.action) {
startForeground(1, notification) "ACTION_STOP_FOREGROUND" -> {
stopForegroundService()
// Exec your code here return START_NOT_STICKY
}
return START_STICKY else -> {
val notification = createNotification()
startForeground(1, notification)
return START_STICKY
}
}
} }
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
@@ -47,20 +52,37 @@ class ForegroundService : Service() {
PendingIntent.FLAG_IMMUTABLE PendingIntent.FLAG_IMMUTABLE
) )
val deleteIntent = Intent(this, ForegroundService::class.java).apply {
action = "ACTION_STOP_FOREGROUND"
}
val deletePendingIntent = PendingIntent.getService(
this,
0,
deleteIntent,
PendingIntent.FLAG_IMMUTABLE
)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(this, chanId) Notification.Builder(this, chanId)
.setContentTitle("App is running") .setContentTitle("Server Box")
.setContentText("Click to open the app") .setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build() .build()
} else { } else {
Notification.Builder(this) Notification.Builder(this)
.setContentTitle("App is running") .setContentTitle("Server Box")
.setContentText("Click to open the app") .setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build() .build()
} }
} }
fun stopForegroundService() {
stopForeground(true)
stopSelf()
}
} }

View File

@@ -31,6 +31,11 @@ class MainActivity: FlutterFragmentActivity() {
startService(serviceIntent) startService(serviceIntent)
} }
} }
"stopService" -> {
val serviceIntent = Intent(this@MainActivity, ForegroundService::class.java)
stopService(serviceIntent)
result.success(null)
}
else -> { else -> {
result.notImplemented() result.notImplemented()
} }

View File

@@ -11,4 +11,8 @@ abstract final class BgRunMC {
static void startService() { static void startService() {
_channel.invokeMethod('startService'); _channel.invokeMethod('startService');
} }
static void stopService() {
_channel.invokeMethod('stopService');
}
} }

View File

@@ -9,7 +9,6 @@ import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:hive_flutter/hive_flutter.dart'; import 'package:hive_flutter/hive_flutter.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:server_box/app.dart'; import 'package:server_box/app.dart';
import 'package:server_box/core/channel/bg_run.dart';
import 'package:server_box/core/sync.dart'; import 'package:server_box/core/sync.dart';
import 'package:server_box/data/model/app/menu/server_func.dart'; import 'package:server_box/data/model/app/menu/server_func.dart';
import 'package:server_box/data/model/app/net_view.dart'; import 'package:server_box/data/model/app/net_view.dart';
@@ -111,10 +110,6 @@ void _doPlatformRelated() async {
if (isAndroid) { if (isAndroid) {
// try switch to highest refresh rate // try switch to highest refresh rate
FlutterDisplayMode.setHighRefreshRate(); FlutterDisplayMode.setHighRefreshRate();
if (Stores.setting.bgRun.fetch()) {
Loggers.app.info('Start foreground service');
BgRunMC.startService();
}
} }
final serversCount = Stores.server.box.keys.length; final serversCount = Stores.server.box.keys.length;

View File

@@ -6,6 +6,7 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:server_box/core/channel/bg_run.dart';
import 'package:server_box/core/extension/context/locale.dart'; import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/core/utils/ssh_auth.dart'; import 'package:server_box/core/utils/ssh_auth.dart';
import 'package:server_box/core/utils/server.dart'; import 'package:server_box/core/utils/server.dart';
@@ -70,13 +71,22 @@ class SSHPageState extends State<SSHPage>
late SSHClient? _client = widget.spi.server?.value.client; late SSHClient? _client = widget.spi.server?.value.client;
Timer? _discontinuityTimer; Timer? _discontinuityTimer;
/// Used for (de)activate the wake lock and forground service
static var _sshConnCount = 0;
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
_virtKeyLongPressTimer?.cancel(); _virtKeyLongPressTimer?.cancel();
_terminalController.dispose(); _terminalController.dispose();
_discontinuityTimer?.cancel(); _discontinuityTimer?.cancel();
WakelockPlus.disable();
if (--_sshConnCount <= 0) {
WakelockPlus.disable();
if (isAndroid) {
BgRunMC.stopService();
}
}
} }
@override @override
@@ -85,6 +95,13 @@ class SSHPageState extends State<SSHPage>
_initStoredCfg(); _initStoredCfg();
_initVirtKeys(); _initVirtKeys();
_setupDiscontinuityTimer(); _setupDiscontinuityTimer();
if (++_sshConnCount == 1) {
WakelockPlus.enable();
if (isAndroid) {
BgRunMC.startService();
}
}
} }
@override @override