tor-android icon indicating copy to clipboard operation
tor-android copied to clipboard

document how to use TorService with foreground priority via binding

Open eighthave opened this issue 4 years ago • 7 comments

Android gives "foreground" priority to the activity that is currently visible. Things that are at the foreground priority are the last to be killed. With Service instances, it is possible to set the service to be permanently at the "foreground" priority as long as there is a permanent notification. It is also possible for a Service to get foreground priority if the currently active screen is bound to the Service using BIND_IMPORTANT.

eighthave avatar Dec 16 '21 15:12 eighthave

Android's Binder model means that all the things that need Tor bind to it with BIND_IMPORTANT. That tells Android to keep it alive. Then for letting TorService quit when idle, there is a built-in mechanism to shutdown a Service when nothing is bound to it. The Android docs explicitly recommend bindService for IPC: https://developer.android.com/guide/components/processes-and-threads

More on bound services: https://developer.android.com/guide/components/bound-services

eighthave avatar Dec 16 '21 15:12 eighthave

Orbot does this today. OrbotService is the application specific foreground service that then binds to TorService: https://github.com/guardianproject/orbot/blob/master/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java#L759

n8fr8 avatar Dec 16 '21 15:12 n8fr8

The Chromium docs outline some nice security properties of bound services: https://chromium.googlesource.com/chromium/src.git/+/refs/heads/main/docs/security/android-ipc.md#binder

eighthave avatar Dec 16 '21 15:12 eighthave

It seems to me that we should be recommending WorkManager here also. Google is putting a lot of work into it to make it the complete background task runner. This is based on this discussion https://github.com/guardianproject/tor-android/pull/61#discussion_r773730904

WorkManager started out for short lived background tasks, but has been extended to handle foreground and long-lived tasks:

  • https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running
  • https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#quotas

eighthave avatar Feb 17 '22 15:02 eighthave

heya I was wondering is there any documentation on implementing torservice on android? I tried the sampleapp, which doesn't work for me as well.

holywend avatar Oct 10 '22 12:10 holywend

There isn't really documentation outside of the example code and apps at this point. We welcome contributions. You can see it in production code in the TorServices app, OnionShare, Orbot, and some others.

eighthave avatar Oct 11 '22 07:10 eighthave

@eighthave atm I couldn't even start the sampleapp. even tho I just clone it from the repo.

holywend avatar Oct 11 '22 10:10 holywend