summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2021-11-11 22:16:15 +0100
committercyBerta <cyberta@riseup.net>2021-11-11 22:21:05 +0100
commita917cbe977f640345677b97dc9b00900d78c46b3 (patch)
tree0e2745401674ae0d83b52883474097e9ff851994 /app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java
parent0b687502d047253ca50b691c29336bc3e53a29d2 (diff)
use command pattern to start/stop tor service, similar to EIP and ProviderAPI service
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java
new file mode 100644
index 00000000..0154983a
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorServiceConnection.java
@@ -0,0 +1,73 @@
+package se.leap.bitmaskclient.tor;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.util.Log;
+
+import androidx.annotation.WorkerThread;
+
+import org.torproject.jni.TorService;
+
+import java.io.Closeable;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import se.leap.bitmaskclient.providersetup.ProviderAPI;
+
+import static se.leap.bitmaskclient.base.utils.ConfigHelper.ensureNotOnMainThread;
+
+public class TorServiceConnection implements Closeable {
+ private static final String TAG = TorServiceConnection.class.getSimpleName();
+ private final Context context;
+ private ServiceConnection serviceConnection;
+ private TorService torService;
+
+ @WorkerThread
+ public TorServiceConnection(Context context) throws InterruptedException, IllegalStateException {
+ this.context = context;
+ ensureNotOnMainThread(context);
+ initSynchronizedServiceConnection(context);
+ }
+
+ @Override
+ public void close() {
+ context.unbindService(serviceConnection);
+ }
+
+ private void initSynchronizedServiceConnection(final Context context) throws InterruptedException {
+ Log.d(TAG, "initSynchronizedServiceConnection");
+ final BlockingQueue<TorService> blockingQueue = new LinkedBlockingQueue<>(1);
+ this.serviceConnection = new ServiceConnection() {
+ volatile boolean mConnectedAtLeastOnce = false;
+
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ if (!mConnectedAtLeastOnce) {
+ mConnectedAtLeastOnce = true;
+ Log.d(TAG, "onServiceConnected");
+ try {
+ TorService.LocalBinder binder = (TorService.LocalBinder) service;
+ blockingQueue.put(binder.getService());
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ torService = null;
+ }
+ };
+ Intent intent = new Intent(context, TorService.class);
+ context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
+ torService = blockingQueue.take();
+ }
+
+ public TorService getService() {
+ return torService;
+ }
+}