Commit fed179a6 authored by HashikD's avatar HashikD
Browse files

Add relay check - only handled success

parent 1a47d2c1
......@@ -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();
});
}
}
......@@ -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);
}
......
......@@ -4,4 +4,5 @@ public interface MainFragmentCallback {
boolean isServiceRunning();
void serviceToggle(String action);
int getServed();
void relayCheck();
}
......@@ -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();
}
}
......@@ -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();
}
}
......@@ -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();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment