[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake-mobile/master] Add relay check - only handled success



commit fed179a61aefef8c4c125fe602701b6f24adbc52
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Wed Nov 11 13:57:22 2020 +0530

    Add relay check - only handled success
---
 .../org/torproject/snowflake/MainActivity.java     | 31 +++++++++++++---
 .../snowflake/fragments/MainFragment.java          | 11 +++++-
 .../snowflake/interfaces/MainFragmentCallback.java |  1 +
 .../snowflake/models/MainActivityModel.java        | 41 ++++++++++++++++++++--
 .../snowflake/mvp/MainActivityMVPContract.java     | 12 +++++++
 .../presenters/MainActivityPresenter.java          | 19 ++++++++++
 6 files changed, 108 insertions(+), 7 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index aa1692c..43f3697 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -7,16 +7,13 @@ import android.content.Intent;
 import android.graphics.Color;
 import android.os.Build;
 import android.os.Bundle;
-import android.transition.Slide;
 import android.util.Log;
-import android.view.Gravity;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;
 
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.AppCompatActivity;
-import androidx.core.view.GravityCompat;
 import androidx.fragment.app.Fragment;
 
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
@@ -37,7 +34,6 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
     //Indicates if model finished checking the date and reset served count if need be.
     boolean isCheckDateFinished;
     private Button settingsButton;
-    boolean transitionToggle = false;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -183,6 +179,14 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
             return 0;
     }
 
+    @Override
+    public void relayCheck() {
+        // Deactivate settings button
+        settingsButton.setEnabled(false);
+        settingsButton.setClickable(false);
+        presenter.relayCheck();
+    }
+
     @Override
     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
 
@@ -193,4 +197,23 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
 
         return super.onOptionsItemSelected(item);
     }
+
+
+    @Override
+    public void relayFailed(String message) {
+        runOnUiThread(() -> {
+            settingsButton.setEnabled(true);
+            settingsButton.setClickable(true);
+        });
+    }
+
+    @Override
+    public void relaySuccess() {
+        runOnUiThread(() -> {
+            settingsButton.setEnabled(true);
+            settingsButton.setClickable(true);
+            Fragment mainFragment = getSupportFragmentManager().findFragmentByTag(Integer.toString(FragmentConstants.MAIN_FRAGMENT));
+            ((MainFragment) mainFragment).startProxy();
+        });
+    }
 }
diff --git a/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java b/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
index 25b0ba2..64e0ef4 100644
--- a/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
+++ b/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
@@ -79,7 +79,7 @@ public class MainFragment extends Fragment {
                 setSnowflakeStatus(false);
                 callback.serviceToggle(ForegroundServiceConstants.ACTION_STOP);
             } else {
-                startProxy();
+                startRelayCheck();
             }
         });
         showServed(callback.getServed());
@@ -88,6 +88,13 @@ public class MainFragment extends Fragment {
         return rootView;
     }
 
+    private void startRelayCheck() {
+        startButton.setEnabled(false);
+        startButton.setClickable(false);
+        noteTV.setText("Checking relay reachability");
+        callback.relayCheck();
+    }
+
     @Override
     public void onAttach(@NonNull Context context) {
         super.onAttach(context);
@@ -108,6 +115,8 @@ public class MainFragment extends Fragment {
      * Starts the proxy.
      */
     public void startProxy() {
+        startButton.setEnabled(true);
+        startButton.setClickable(true);
         setSnowflakeStatus(true);
         callback.serviceToggle(ForegroundServiceConstants.ACTION_START);
     }
diff --git a/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
index 40da367..32df1c9 100644
--- a/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
+++ b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
@@ -4,4 +4,5 @@ public interface MainFragmentCallback {
     boolean isServiceRunning();
     void serviceToggle(String action);
     int getServed();
+    void relayCheck();
 }
diff --git a/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java b/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
index 66c5f4c..15572a0 100644
--- a/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
+++ b/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
@@ -3,6 +3,7 @@ package org.torproject.snowflake.models;
 import android.content.SharedPreferences;
 import android.util.Log;
 
+import org.jetbrains.annotations.NotNull;
 import org.torproject.snowflake.GlobalApplication;
 import org.torproject.snowflake.constants.AppPreferenceKeys;
 import org.torproject.snowflake.mvp.MainActivityMVPContract;
@@ -15,6 +16,11 @@ import java.util.Date;
 import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
 import io.reactivex.rxjava3.core.Single;
 import io.reactivex.rxjava3.schedulers.Schedulers;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import okhttp3.WebSocket;
+import okhttp3.WebSocketListener;
 
 /**
  * Model for MainActivity to handle network calls, Shared preferences.
@@ -22,8 +28,8 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
 public class MainActivityModel implements MainActivityMVPContract.Model {
     private static final String TAG = "MainActivityModel";
     private static MainActivityModel instance = null;
-    private SharedPreferences sharedPreferences;
-    private MainActivityMVPContract.Presenter presenter;
+    private final SharedPreferences sharedPreferences;
+    private final MainActivityMVPContract.Presenter presenter;
     private int servedCount;
     private SharedPreferences.OnSharedPreferenceChangeListener listener;
 
@@ -151,4 +157,35 @@ public class MainActivityModel implements MainActivityMVPContract.Model {
                     });
         }
     }
+
+    @Override
+    public void relayCheck() {
+        Request req;
+        try {
+            req = new Request.Builder().url(GlobalApplication.getWebSocketUrl()).build();
+        } catch (IllegalArgumentException e) {
+            presenter.relayFailure("Malformed URL");
+            return;
+        }
+
+        OkHttpClient client = new OkHttpClient();
+        client.newWebSocket(req,
+                new WebSocketListener() {
+                    @Override
+                    public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @org.jetbrains.annotations.Nullable Response response) {
+                        Log.d(TAG, "WebSocketListener: onFailure: ");
+                        webSocket.close(1000, "Normal Closure"); //TODO: Change Failure code.
+                        presenter.relayFailure("Web socket connection failed");
+                    }
+
+                    @Override
+                    public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
+                        Log.d(TAG, "WebSocketListener: onOpen: ");
+                        webSocket.close(1000, "Normal Closure");
+                        presenter.relaySuccess();
+                    }
+                });
+
+        client.dispatcher().executorService().shutdown();
+    }
 }
diff --git a/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVPContract.java b/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVPContract.java
index 1bda683..6fb9a35 100644
--- a/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVPContract.java
+++ b/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVPContract.java
@@ -6,6 +6,10 @@ package org.torproject.snowflake.mvp;
 public interface MainActivityMVPContract {
     interface View {
         void updateCountInFragment(int i);
+
+        void relayFailed(String message);
+
+        void relaySuccess();
     }
 
     interface Model {
@@ -18,6 +22,8 @@ public interface MainActivityMVPContract {
         void checkDateAsync();
 
         int getServedCount();
+
+        void relayCheck();
     }
 
     interface Presenter {
@@ -34,5 +40,11 @@ public interface MainActivityMVPContract {
         void updateServedCount(int count);
 
         void checkDate();
+
+        void relayCheck();
+
+        void relayFailure(String message);
+
+        void relaySuccess();
     }
 }
diff --git a/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java b/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
index fd91839..4046f8b 100644
--- a/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
+++ b/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
@@ -80,4 +80,23 @@ public class MainActivityPresenter implements MainActivityMVPContract.Presenter
     public void checkDate() {
         model.checkDateAsync();
     }
+
+    @Override
+    public void relayCheck() {
+        model.relayCheck();
+    }
+
+    @Override
+    public void relayFailure(String message) {
+        if (view != null) {
+            view.relayFailed(message);
+        }
+    }
+
+    @Override
+    public void relaySuccess() {
+        if (view != null) {
+            view.relaySuccess();
+        }
+    }
 }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits