Removed Handler and Looper android classes dependencies

This commit is contained in:
Nelson R. Perez 2019-09-19 16:57:41 -05:00
parent d019acca6a
commit d30aa251a1

View file

@ -1,8 +1,5 @@
package cy.agorise.graphenej.api.android; package cy.agorise.graphenej.api.android;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -12,6 +9,7 @@ import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -56,9 +54,12 @@ import cy.agorise.graphenej.network.NodeProvider;
import cy.agorise.graphenej.operations.CustomOperation; import cy.agorise.graphenej.operations.CustomOperation;
import cy.agorise.graphenej.operations.LimitOrderCreateOperation; import cy.agorise.graphenej.operations.LimitOrderCreateOperation;
import cy.agorise.graphenej.operations.TransferOperation; import cy.agorise.graphenej.operations.TransferOperation;
import io.reactivex.Observable;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.PublishSubject; import io.reactivex.subjects.PublishSubject;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -114,7 +115,7 @@ public class NetworkService {
// Property used to keep track of the currently active node // Property used to keep track of the currently active node
private FullNode mSelectedNode; private FullNode mSelectedNode;
private Handler mHandler = new Handler(Looper.getMainLooper()); private CompositeDisposable mCompositeDisposable;
private Gson gson = new GsonBuilder() private Gson gson = new GsonBuilder()
.registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer()) .registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer())
@ -185,7 +186,8 @@ public class NetworkService {
}else{ }else{
System.out.println("Could not find best node, reescheduling"); System.out.println("Could not find best node, reescheduling");
// If no node could be found yet, schedule a new attempt in DEFAULT_INITIAL_DELAY ms // If no node could be found yet, schedule a new attempt in DEFAULT_INITIAL_DELAY ms
mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY); Disposable d = Observable.timer(DEFAULT_INITIAL_DELAY, TimeUnit.MILLISECONDS).subscribe(mConnectAttempt);
mCompositeDisposable.add(d);
} }
} }
} }
@ -243,12 +245,16 @@ public class NetworkService {
if(nodeLatencyVerifier != null) if(nodeLatencyVerifier != null)
nodeLatencyVerifier.stop(); nodeLatencyVerifier.stop();
mCompositeDisposable.dispose();
} }
/** /**
* Starts the connection * Starts the connection
*/ */
public void start(String[] urls, double alpha) { public void start(String[] urls, double alpha) {
mCompositeDisposable = new CompositeDisposable();
// Retrieving credentials and requested API data from the shared preferences // Retrieving credentials and requested API data from the shared preferences
mUsername = ""; mUsername = "";
mPassword = ""; mPassword = "";
@ -270,7 +276,8 @@ public class NetworkService {
fullNodePublishSubject = nodeLatencyVerifier.start(); fullNodePublishSubject = nodeLatencyVerifier.start();
fullNodePublishSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(nodeLatencyObserver); fullNodePublishSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(nodeLatencyObserver);
mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY); Disposable d = Observable.timer(DEFAULT_INITIAL_DELAY, TimeUnit.MILLISECONDS).subscribe(mConnectAttempt);
mCompositeDisposable.add(d);
} }
/** /**
@ -281,7 +288,7 @@ public class NetworkService {
} }
/** /**
* Runnable that will perform a connection attempt with the best node after DEFAULT_INITIAL_DELAY * Consumer that will perform a connection attempt with the best node after DEFAULT_INITIAL_DELAY
* milliseconds. This is used only if the node latency verification is activated. * milliseconds. This is used only if the node latency verification is activated.
* *
* The reason to delay the initial connection is that we want to ideally connect to the best node, * The reason to delay the initial connection is that we want to ideally connect to the best node,
@ -289,15 +296,17 @@ public class NetworkService {
* first node latency measurement round to finish in order to have at least a partial result set * first node latency measurement round to finish in order to have at least a partial result set
* that could be used. * that could be used.
*/ */
private Runnable mConnectAttempt = new Runnable() { private Consumer mConnectAttempt = new Consumer() {
@Override @Override
public void run() { public void accept(Object o) throws Exception {
FullNode fullNode = nodeProvider.getBestNode(); FullNode fullNode = nodeProvider.getBestNode();
if(fullNode != null){ if(fullNode != null){
System.out.println( String.format("Connected with %d latency results", latencyUpdateCounter)); System.out.println(String.format(Locale.ROOT, "Connected with %d latency results", latencyUpdateCounter));
connect(); connect();
}else{ }else{
mHandler.postDelayed(this, DEFAULT_INITIAL_DELAY); Disposable d = Observable.timer(DEFAULT_INITIAL_DELAY, TimeUnit.MILLISECONDS).subscribe(this);
mCompositeDisposable.add(d);
} }
} }
}; };
@ -644,12 +653,13 @@ public class NetworkService {
if (nodeProvider.getBestNode() == null) { if (nodeProvider.getBestNode() == null) {
System.out.println( "Giving up on connections"); System.out.println( "Giving up on connections");
} else { } else {
mHandler.postDelayed(new Runnable() { Disposable d = Observable.timer(DEFAULT_RETRY_DELAY, TimeUnit.MILLISECONDS).subscribe(new Consumer<Long>() {
@Override @Override
public void run() { public void accept(Long aLong) throws Exception {
connect(); connect();
} }
}, DEFAULT_RETRY_DELAY); });
mCompositeDisposable.add(d);
} }
} }