improve tor notification

This commit is contained in:
Fabian Freund
2026-01-08 21:49:37 +01:00
parent 2741876ed5
commit ea015f9415
3 changed files with 35 additions and 14 deletions
@@ -91,8 +91,8 @@ class FlutterTorPlugin : FlutterPlugin, TorApi {
serviceConnection = connection serviceConnection = connection
val intent = Intent(ctx, TorService::class.java) val intent = Intent(ctx, TorService::class.java)
intent.action = TorService.ACTION_START // Only bind to service, don't start it yet
ctx.startService(intent) // Service will be started when startTor() is called
ctx.bindService(intent, connection, Context.BIND_AUTO_CREATE) ctx.bindService(intent, connection, Context.BIND_AUTO_CREATE)
} }
@@ -55,18 +55,15 @@ class TorService : Service() {
Log.d(TAG, "onStartCommand: ${intent?.action}") Log.d(TAG, "onStartCommand: ${intent?.action}")
when (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 -> { ACTION_STOP -> {
scope.launch { scope.launch {
stopTor() stopTor()
stopSelf() stopSelf()
} }
} }
else -> {
Log.d(TAG, "Service started via binding, waiting for startTor() call")
}
} }
return START_STICKY return START_STICKY
@@ -94,17 +91,23 @@ class TorService : Service() {
*/ */
suspend fun startTor(config: TorConfiguration): Int { suspend fun startTor(config: TorConfiguration): Int {
Log.d(TAG, "Starting Tor...") 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") val manager = torManager ?: throw IllegalStateException("Service not initialized")
try { try {
val socksPort = manager.start(config) val socksPort = manager.start(config)
updateNotification("Tor is running (SOCKS: $socksPort)") updateNotification("Tor connected")
return socksPort return socksPort
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to start Tor", e) Log.e(TAG, "Failed to start Tor", e)
updateNotification("Failed to start Tor") updateNotification("Failed to start Tor")
// Stop foreground on failure
stopForeground(STOP_FOREGROUND_REMOVE)
throw e throw e
} }
} }
@@ -114,10 +117,12 @@ class TorService : Service() {
*/ */
suspend fun stopTor() { suspend fun stopTor() {
Log.d(TAG, "Stopping Tor...") Log.d(TAG, "Stopping Tor...")
updateNotification("Stopping Tor...")
torManager?.stop() 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) return NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Tor Service") .setContentTitle("WebLibre Tor")
.setContentText(text) .setContentText(text)
.setSmallIcon(android.R.drawable.ic_dialog_info) // TODO: Use custom icon .setSmallIcon(R.drawable.ic_onion)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setOngoing(true) .setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW) .setPriority(NotificationCompat.PRIORITY_LOW)
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8 0,-4.41 3.59,-8 8,-8s8,3.59 8,8c0,4.41 -3.59,8 -8,8z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,6c-3.31,0 -6,2.69 -6,6s2.69,6 6,6 6,-2.69 6,-6 -2.69,-6 -6,-6zM12,16c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4 4,1.79 4,4 -1.79,4 -4,4z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3z"/>
</vector>