[tor-commits] [orbot/master] handle service restarts with null intents

n8fr8 at torproject.org n8fr8 at torproject.org
Tue Apr 28 21:05:01 UTC 2020


commit 1d2fa53f8e14bbeead395ea4fbb9f3dfd8c29eb3
Author: n8fr8 <nathan at guardianproject.info>
Date:   Tue Jan 28 16:23:40 2020 -0500

    handle service restarts with null intents
---
 .../android/service/vpn/TorVpnService.java         | 53 ++++++++++++----------
 1 file changed, 29 insertions(+), 24 deletions(-)

diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
index a6666474..688a8610 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
@@ -58,19 +58,38 @@ public class TorVpnService extends VpnService {
      */
     public int onStartCommand(Intent intent, int flags, int startId) {
 
-        if (ACTION_START.equals(intent.getAction()))
-        {
-            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
-            lbm.registerReceiver(mLocalBroadcastReceiver,
-                    new IntentFilter(TorServiceConstants.LOCAL_ACTION_PORTS));
-        }
-        else if (ACTION_STOP.equals(intent.getAction()))
-        {
+        String action = ACTION_START;
+        if (intent != null && intent.getAction() != null)
+            action = intent.getAction();
+
+        if (ACTION_START.equals(action)) {
+
+            if (mLocalBroadcastReceiver == null) {
+                mLocalBroadcastReceiver = new BroadcastReceiver() {
+
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        String action = intent.getAction();
+                        if (action == null)
+                            return;
+
+                        if (action.equals(TorServiceConstants.LOCAL_ACTION_PORTS)) {
+
+                            mVpnManager.handleIntent(new Builder(), intent);
+                        }
+                    }
+                };
+                LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
+                lbm.registerReceiver(mLocalBroadcastReceiver,
+                        new IntentFilter(TorServiceConstants.LOCAL_ACTION_PORTS));
+            }
+
+        } else if (ACTION_STOP.equals(action)) {
             Log.d(TAG, "clearing VPN Proxy");
             Prefs.putUseVpn(false);
-
             LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
             lbm.unregisterReceiver(mLocalBroadcastReceiver);
+            mLocalBroadcastReceiver = null;
         }
 
         mVpnManager.handleIntent(new Builder(), intent);
@@ -82,7 +101,6 @@ public class TorVpnService extends VpnService {
     public void onDestroy() {
         super.onDestroy();
 
-
     }
 
     /**
@@ -91,20 +109,7 @@ public class TorVpnService extends VpnService {
      * so local ones are used here so other apps cannot interfere with Orbot's
      * operation.
      */
-    private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {
-
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            String action = intent.getAction();
-            if (action == null)
-                return;
-
-            if (action.equals(TorServiceConstants.LOCAL_ACTION_PORTS)) {
-
-                mVpnManager.handleIntent(new Builder(),intent);
-            }
-        }
-    };
+    private BroadcastReceiver mLocalBroadcastReceiver = null;
 
 
 }





More information about the tor-commits mailing list