mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
opt.: dismiss notification if no ssh conn (#592)
This commit is contained in:
@@ -14,13 +14,18 @@ class ForegroundService : Service() {
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
"ACTION_STOP_FOREGROUND" -> {
|
||||
stopForegroundService()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
else -> {
|
||||
val notification = createNotification()
|
||||
startForeground(1, notification)
|
||||
|
||||
// Exec your code here
|
||||
|
||||
return START_STICKY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
return null
|
||||
@@ -47,20 +52,37 @@ class ForegroundService : Service() {
|
||||
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) {
|
||||
Notification.Builder(this, chanId)
|
||||
.setContentTitle("App is running")
|
||||
.setContentText("Click to open the app")
|
||||
.setContentTitle("Server Box")
|
||||
.setContentText("Open the app")
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(pendingIntent)
|
||||
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
|
||||
.build()
|
||||
} else {
|
||||
Notification.Builder(this)
|
||||
.setContentTitle("App is running")
|
||||
.setContentText("Click to open the app")
|
||||
.setContentTitle("Server Box")
|
||||
.setContentText("Open the app")
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(pendingIntent)
|
||||
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
fun stopForegroundService() {
|
||||
stopForeground(true)
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,11 @@ class MainActivity: FlutterFragmentActivity() {
|
||||
startService(serviceIntent)
|
||||
}
|
||||
}
|
||||
"stopService" -> {
|
||||
val serviceIntent = Intent(this@MainActivity, ForegroundService::class.java)
|
||||
stopService(serviceIntent)
|
||||
result.success(null)
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
|
||||
@@ -11,4 +11,8 @@ abstract final class BgRunMC {
|
||||
static void startService() {
|
||||
_channel.invokeMethod('startService');
|
||||
}
|
||||
|
||||
static void stopService() {
|
||||
_channel.invokeMethod('stopService');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import 'package:flutter_displaymode/flutter_displaymode.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:logging/logging.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/data/model/app/menu/server_func.dart';
|
||||
import 'package:server_box/data/model/app/net_view.dart';
|
||||
@@ -111,10 +110,6 @@ void _doPlatformRelated() async {
|
||||
if (isAndroid) {
|
||||
// try switch to highest refresh rate
|
||||
FlutterDisplayMode.setHighRefreshRate();
|
||||
if (Stores.setting.bgRun.fetch()) {
|
||||
Loggers.app.info('Start foreground service');
|
||||
BgRunMC.startService();
|
||||
}
|
||||
}
|
||||
|
||||
final serversCount = Stores.server.box.keys.length;
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.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/utils/ssh_auth.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;
|
||||
Timer? _discontinuityTimer;
|
||||
|
||||
/// Used for (de)activate the wake lock and forground service
|
||||
static var _sshConnCount = 0;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
_virtKeyLongPressTimer?.cancel();
|
||||
_terminalController.dispose();
|
||||
_discontinuityTimer?.cancel();
|
||||
|
||||
if (--_sshConnCount <= 0) {
|
||||
WakelockPlus.disable();
|
||||
if (isAndroid) {
|
||||
BgRunMC.stopService();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -85,6 +95,13 @@ class SSHPageState extends State<SSHPage>
|
||||
_initStoredCfg();
|
||||
_initVirtKeys();
|
||||
_setupDiscontinuityTimer();
|
||||
|
||||
if (++_sshConnCount == 1) {
|
||||
WakelockPlus.enable();
|
||||
if (isAndroid) {
|
||||
BgRunMC.startService();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user