Skip to content

Commit

Permalink
opt.: dismiss notification if no ssh conn (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit authored Sep 24, 2024
1 parent 47aedb2 commit aef317a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
42 changes: 32 additions & 10 deletions android/app/src/main/kotlin/tech/lolli/toolbox/ForegroundService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ class ForegroundService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = createNotification()
startForeground(1, notification)

// Exec your code here

return START_STICKY
when (intent?.action) {
"ACTION_STOP_FOREGROUND" -> {
stopForegroundService()
return START_NOT_STICKY
}
else -> {
val notification = createNotification()
startForeground(1, notification)
return START_STICKY
}
}
}

override fun onBind(intent: Intent): IBinder? {
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 4 additions & 0 deletions lib/core/channel/bg_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ abstract final class BgRunMC {
static void startService() {
_channel.invokeMethod('startService');
}

static void stopService() {
_channel.invokeMethod('stopService');
}
}
5 changes: 0 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion lib/view/page/ssh/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
WakelockPlus.disable();

if (--_sshConnCount <= 0) {
WakelockPlus.disable();
if (isAndroid) {
BgRunMC.stopService();
}
}
}

@override
Expand All @@ -85,6 +95,13 @@ class SSHPageState extends State<SSHPage>
_initStoredCfg();
_initVirtKeys();
_setupDiscontinuityTimer();

if (++_sshConnCount == 1) {
WakelockPlus.enable();
if (isAndroid) {
BgRunMC.startService();
}
}
}

@override
Expand Down

0 comments on commit aef317a

Please sign in to comment.