From ab71eacd8cc48cdf8f41d35302997591f35499e5 Mon Sep 17 00:00:00 2001
From: n8fr8 <nathan@freitas.net>
Date: Fri, 29 Apr 2022 10:43:49 -0400
Subject: [PATCH] update sample app to actually get the tor control connection
 (as a test)

---
 .../android/sample/MainActivity.java          | 38 ++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/sampletorapp/src/main/java/org/torproject/android/sample/MainActivity.java b/sampletorapp/src/main/java/org/torproject/android/sample/MainActivity.java
index 16df512a..479f914d 100644
--- a/sampletorapp/src/main/java/org/torproject/android/sample/MainActivity.java
+++ b/sampletorapp/src/main/java/org/torproject/android/sample/MainActivity.java
@@ -18,13 +18,19 @@ package org.torproject.android.sample;
 
 import android.app.Activity;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.ServiceConnection;
 import android.os.Bundle;
+import android.os.IBinder;
 import android.webkit.WebView;
 import android.widget.TextView;
 import android.widget.Toast;
+
+import net.freehaven.tor.control.TorControlConnection;
+
 import org.torproject.jni.TorService;
 
 public class MainActivity extends Activity {
@@ -51,6 +57,36 @@ public class MainActivity extends Activity {
 
             }
         }, new IntentFilter(TorService.ACTION_STATUS));
-        startService(new Intent(this, TorService.class));
+
+
+        bindService(new Intent(this, TorService.class), new ServiceConnection() {
+            @Override
+            public void onServiceConnected(ComponentName name, IBinder service) {
+
+                //moved torService to a local variable, since we only need it once
+                TorService torService = ((TorService.LocalBinder) service).getService();
+                TorControlConnection conn = torService.getTorControlConnection();
+
+                while ((conn = torService.getTorControlConnection())==null)
+                {
+                    try {
+                        Thread.sleep(500);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+
+                if (conn != null)
+                {
+                    Toast.makeText(MainActivity.this, "Got Tor control connection", Toast.LENGTH_LONG).show();
+                }
+            }
+
+            @Override
+            public void onServiceDisconnected(ComponentName name) {
+
+            }
+        },BIND_AUTO_CREATE);
+
     }
 }
-- 
GitLab