From ea015f941518d891b0ae1e15b3ffe4db0ca23e21 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Thu, 8 Jan 2026 21:49:37 +0100 Subject: [PATCH] improve tor notification --- .../weblibre/flutter_tor/FlutterTorPlugin.kt | 4 +-- .../eu/weblibre/flutter_tor/TorService.kt | 29 +++++++++++-------- .../src/main/res/drawable/ic_onion.xml | 16 ++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 packages/flutter_tor/android/src/main/res/drawable/ic_onion.xml diff --git a/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/FlutterTorPlugin.kt b/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/FlutterTorPlugin.kt index f5cfe780..db9dfad3 100644 --- a/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/FlutterTorPlugin.kt +++ b/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/FlutterTorPlugin.kt @@ -91,8 +91,8 @@ class FlutterTorPlugin : FlutterPlugin, TorApi { serviceConnection = connection val intent = Intent(ctx, TorService::class.java) - intent.action = TorService.ACTION_START - ctx.startService(intent) + // Only bind to service, don't start it yet + // Service will be started when startTor() is called ctx.bindService(intent, connection, Context.BIND_AUTO_CREATE) } diff --git a/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/TorService.kt b/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/TorService.kt index e4aceed6..3f43bbc2 100644 --- a/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/TorService.kt +++ b/packages/flutter_tor/android/src/main/kotlin/eu/weblibre/flutter_tor/TorService.kt @@ -55,18 +55,15 @@ class TorService : Service() { Log.d(TAG, "onStartCommand: ${intent?.action}") when (intent?.action) { - ACTION_START -> { - // Start in foreground immediately - startForeground(NOTIFICATION_ID, createNotification("Starting Tor...")) - // Actual start will be handled via binder methods - } - ACTION_STOP -> { scope.launch { stopTor() stopSelf() } } + else -> { + Log.d(TAG, "Service started via binding, waiting for startTor() call") + } } return START_STICKY @@ -94,17 +91,23 @@ class TorService : Service() { */ suspend fun startTor(config: TorConfiguration): Int { Log.d(TAG, "Starting Tor...") - updateNotification("Starting Tor...") + + // Start foreground service with notification + // This keeps the service alive even when the app is backgrounded + startForeground(NOTIFICATION_ID, createNotification("Tor is connecting...")) + Log.d(TAG, "Started foreground service") val manager = torManager ?: throw IllegalStateException("Service not initialized") try { val socksPort = manager.start(config) - updateNotification("Tor is running (SOCKS: $socksPort)") + updateNotification("Tor connected") return socksPort } catch (e: Exception) { Log.e(TAG, "Failed to start Tor", e) updateNotification("Failed to start Tor") + // Stop foreground on failure + stopForeground(STOP_FOREGROUND_REMOVE) throw e } } @@ -114,10 +117,12 @@ class TorService : Service() { */ suspend fun stopTor() { Log.d(TAG, "Stopping Tor...") - updateNotification("Stopping Tor...") torManager?.stop() - updateNotification("Tor stopped") + + // Stop foreground service and remove notification + stopForeground(STOP_FOREGROUND_REMOVE) + Log.d(TAG, "Stopped foreground service") } /** @@ -162,9 +167,9 @@ class TorService : Service() { ) return NotificationCompat.Builder(this, CHANNEL_ID) - .setContentTitle("Tor Service") + .setContentTitle("WebLibre Tor") .setContentText(text) - .setSmallIcon(android.R.drawable.ic_dialog_info) // TODO: Use custom icon + .setSmallIcon(R.drawable.ic_onion) .setContentIntent(pendingIntent) .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_LOW) diff --git a/packages/flutter_tor/android/src/main/res/drawable/ic_onion.xml b/packages/flutter_tor/android/src/main/res/drawable/ic_onion.xml new file mode 100644 index 00000000..b0722563 --- /dev/null +++ b/packages/flutter_tor/android/src/main/res/drawable/ic_onion.xml @@ -0,0 +1,16 @@ + + + + +