Merge branch 'develop' of github.com:Agorise/graphenej into develop
This commit is contained in:
commit
bcb4f8a964
15 changed files with 445 additions and 69 deletions
|
@ -80,7 +80,7 @@ public class NetworkService extends Service {
|
||||||
private final String TAG = this.getClass().getName();
|
private final String TAG = this.getClass().getName();
|
||||||
|
|
||||||
public static final int NORMAL_CLOSURE_STATUS = 1000;
|
public static final int NORMAL_CLOSURE_STATUS = 1000;
|
||||||
private static final int NO_HISTORY_CLOSURE_STATUS = 1001;
|
private static final int GOING_AWAY_STATUS = 1001;
|
||||||
|
|
||||||
// Time to wait before retrying a connection attempt
|
// Time to wait before retrying a connection attempt
|
||||||
private static final int DEFAULT_RETRY_DELAY = 500;
|
private static final int DEFAULT_RETRY_DELAY = 500;
|
||||||
|
@ -341,6 +341,14 @@ public class NetworkService extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public method that can be called from classes that bind to the service and find out that
|
||||||
|
* for any reason want the service to connect to a different node.
|
||||||
|
*/
|
||||||
|
public void removeCurrentNodeAndReconnect() {
|
||||||
|
mWebSocket.close(GOING_AWAY_STATUS, null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runnable that will perform a connection attempt with the best node after DEFAULT_INITIAL_DELAY
|
* Runnable 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.
|
||||||
|
@ -566,9 +574,6 @@ public class NetworkService extends Service {
|
||||||
} else if (requestClass == GetFullAccounts.class) {
|
} else if (requestClass == GetFullAccounts.class) {
|
||||||
Type GetFullAccountsResponse = new TypeToken<JsonRpcResponse<List<FullAccountDetails>>>(){}.getType();
|
Type GetFullAccountsResponse = new TypeToken<JsonRpcResponse<List<FullAccountDetails>>>(){}.getType();
|
||||||
parsedResponse = gson.fromJson(text, GetFullAccountsResponse);
|
parsedResponse = gson.fromJson(text, GetFullAccountsResponse);
|
||||||
|
|
||||||
if(parsedResponse != null)
|
|
||||||
verifyNodeHasHistoryApi(parsedResponse);
|
|
||||||
} else if(requestClass == GetKeyReferences.class){
|
} else if(requestClass == GetKeyReferences.class){
|
||||||
Type GetKeyReferencesResponse = new TypeToken<JsonRpcResponse<List<List<UserAccount>>>>(){}.getType();
|
Type GetKeyReferencesResponse = new TypeToken<JsonRpcResponse<List<List<UserAccount>>>>(){}.getType();
|
||||||
parsedResponse = gson.fromJson(text, GetKeyReferencesResponse);
|
parsedResponse = gson.fromJson(text, GetKeyReferencesResponse);
|
||||||
|
@ -591,30 +596,6 @@ public class NetworkService extends Service {
|
||||||
RxBus.getBusInstance().send(parsedResponse);
|
RxBus.getBusInstance().send(parsedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method inspects the node response to find out if the totalOps is equal to zero,
|
|
||||||
* in that case the current connected node may not have the history plugin so we would need
|
|
||||||
* to close the connection and choose a different node.
|
|
||||||
*
|
|
||||||
* @param parsedResponse A JSONRpcResponse from a GetFullAccounts API call
|
|
||||||
*/
|
|
||||||
private void verifyNodeHasHistoryApi(JsonRpcResponse parsedResponse) {
|
|
||||||
if(parsedResponse.result instanceof List &&
|
|
||||||
((List) parsedResponse.result).size() > 0 &&
|
|
||||||
((List) parsedResponse.result).get(0) instanceof FullAccountDetails) {
|
|
||||||
|
|
||||||
FullAccountDetails fullAccountDetails = (FullAccountDetails) ((List) parsedResponse.result).get(0);
|
|
||||||
long totalOps = fullAccountDetails.getStatistics().total_ops;
|
|
||||||
|
|
||||||
if (totalOps == 0) {
|
|
||||||
Log.d(TAG, "The node returned 0 total_ops for current account and may not have installed the history plugin. " +
|
|
||||||
"Trying to connect to a different node.");
|
|
||||||
|
|
||||||
mWebSocket.close(NO_HISTORY_CLOSURE_STATUS, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private method that will just broadcast a de-serialized notification to all interested parties
|
* Private method that will just broadcast a de-serialized notification to all interested parties
|
||||||
* @param notification De-serialized notification
|
* @param notification De-serialized notification
|
||||||
|
@ -674,10 +655,10 @@ public class NetworkService extends Service {
|
||||||
super.onClosed(webSocket, code, reason);
|
super.onClosed(webSocket, code, reason);
|
||||||
Log.d(TAG,"onClosed");
|
Log.d(TAG,"onClosed");
|
||||||
|
|
||||||
if (code == NO_HISTORY_CLOSURE_STATUS)
|
if (code == GOING_AWAY_STATUS)
|
||||||
handleWebSocketDisconnection(true);
|
handleWebSocketDisconnection(true, true);
|
||||||
else
|
else
|
||||||
handleWebSocketDisconnection(false);
|
handleWebSocketDisconnection(false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -694,16 +675,17 @@ public class NetworkService extends Service {
|
||||||
Log.e(TAG,"Response: "+response.message());
|
Log.e(TAG,"Response: "+response.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
handleWebSocketDisconnection(true);
|
handleWebSocketDisconnection(true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that encapsulates the behavior of handling a disconnection to the current node, and
|
* Method that encapsulates the behavior of handling a disconnection to the current node, and
|
||||||
* potentially tries to reconnect to another one.
|
* potentially tries to reconnect to another one.
|
||||||
*
|
*
|
||||||
* @param tryReconnection Variable that states if a reconnection to other node should be tried.
|
* @param tryReconnection States if a reconnection to other node should be tried.
|
||||||
|
* @param removeSelectedNode States if the current node should be removed from the nodes list.
|
||||||
*/
|
*/
|
||||||
private void handleWebSocketDisconnection(boolean tryReconnection) {
|
private void handleWebSocketDisconnection(boolean tryReconnection, boolean removeSelectedNode) {
|
||||||
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
|
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
|
||||||
|
|
||||||
isLoggedIn = false;
|
isLoggedIn = false;
|
||||||
|
@ -720,10 +702,18 @@ public class NetworkService extends Service {
|
||||||
mCurrentId = 0;
|
mCurrentId = 0;
|
||||||
mApiIds.clear();
|
mApiIds.clear();
|
||||||
|
|
||||||
|
if (removeSelectedNode) {
|
||||||
|
// Remove node from node provider so that it is not returned for following connections
|
||||||
|
nodeProvider.removeNode(mSelectedNode);
|
||||||
|
|
||||||
|
// Remove node from nodeLatencyVerifier, so that it publishes its removal
|
||||||
|
nodeLatencyVerifier.removeNode(mSelectedNode);
|
||||||
|
} else {
|
||||||
// Adding a very high latency value to this node in order to prevent
|
// Adding a very high latency value to this node in order to prevent
|
||||||
// us from getting it again
|
// us from getting it again
|
||||||
mSelectedNode.addLatencyValue(Long.MAX_VALUE);
|
mSelectedNode.addLatencyValue(Long.MAX_VALUE);
|
||||||
nodeProvider.updateNode(mSelectedNode);
|
nodeProvider.updateNode(mSelectedNode);
|
||||||
|
}
|
||||||
|
|
||||||
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
|
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ public class FullNode implements Comparable {
|
||||||
private String mUrl;
|
private String mUrl;
|
||||||
private ExponentialMovingAverage mLatency;
|
private ExponentialMovingAverage mLatency;
|
||||||
private boolean isConnected;
|
private boolean isConnected;
|
||||||
|
private boolean isRemoved;
|
||||||
|
|
||||||
private FullNode(){}
|
private FullNode(){}
|
||||||
|
|
||||||
|
@ -77,6 +78,14 @@ public class FullNode implements Comparable {
|
||||||
isConnected = connected;
|
isConnected = connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRemoved() {
|
||||||
|
return isRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRemoved(boolean removed) {
|
||||||
|
isRemoved = removed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that updates the mLatency average with a new value.
|
* Method that updates the mLatency average with a new value.
|
||||||
* @param latency Most recent mLatency sample to be added to the exponential average
|
* @param latency Most recent mLatency sample to be added to the exponential average
|
||||||
|
|
|
@ -3,13 +3,14 @@ package cy.agorise.graphenej.network;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.PriorityQueue;
|
import java.util.concurrent.PriorityBlockingQueue;
|
||||||
|
|
||||||
public class LatencyNodeProvider implements NodeProvider {
|
public class LatencyNodeProvider implements NodeProvider {
|
||||||
private PriorityQueue<FullNode> mFullNodeHeap;
|
|
||||||
|
private PriorityBlockingQueue<FullNode> mFullNodeHeap;
|
||||||
|
|
||||||
public LatencyNodeProvider(){
|
public LatencyNodeProvider(){
|
||||||
mFullNodeHeap = new PriorityQueue<>();
|
mFullNodeHeap = new PriorityBlockingQueue<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -24,9 +25,12 @@ public class LatencyNodeProvider implements NodeProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean updateNode(FullNode fullNode) {
|
public boolean updateNode(FullNode fullNode) {
|
||||||
mFullNodeHeap.remove(fullNode);
|
boolean existed = mFullNodeHeap.remove(fullNode);
|
||||||
|
if(existed){
|
||||||
return mFullNodeHeap.offer(fullNode);
|
return mFullNodeHeap.offer(fullNode);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates an existing node with the new latency value.
|
* Updates an existing node with the new latency value.
|
||||||
|
@ -36,12 +40,16 @@ public class LatencyNodeProvider implements NodeProvider {
|
||||||
* @return True if the node priority was updated successfully
|
* @return True if the node priority was updated successfully
|
||||||
*/
|
*/
|
||||||
public boolean updateNode(FullNode fullNode, int latency){
|
public boolean updateNode(FullNode fullNode, int latency){
|
||||||
if(mFullNodeHeap.remove(fullNode)){
|
boolean existed = mFullNodeHeap.remove(fullNode);
|
||||||
fullNode.addLatencyValue(latency);
|
if(existed){
|
||||||
return mFullNodeHeap.add(fullNode);
|
return mFullNodeHeap.add(fullNode);
|
||||||
}else{
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeNode(FullNode fullNode) {
|
||||||
|
mFullNodeHeap.remove(fullNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -183,4 +183,24 @@ public class NodeLatencyVerifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given node from the nodes list
|
||||||
|
* @param fullNode The node to remove
|
||||||
|
*/
|
||||||
|
public void removeNode(FullNode fullNode){
|
||||||
|
for(FullNode node : mNodeList){
|
||||||
|
if(node.equals(fullNode)){
|
||||||
|
mNodeList.remove(node);
|
||||||
|
|
||||||
|
String normalURL = node.getUrl().replace("wss://", "https://");
|
||||||
|
HttpUrl key = HttpUrl.parse(normalURL);
|
||||||
|
nodeURLMap.remove(key);
|
||||||
|
|
||||||
|
node.setRemoved(true);
|
||||||
|
subject.onNext(node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,19 @@ public interface NodeProvider {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the rating of a specific node that is already in the NodeProvider
|
* Updates the rating of a specific node that is already in the NodeProvider
|
||||||
* @param fullNode
|
* @param fullNode The node tu update
|
||||||
*/
|
*/
|
||||||
boolean updateNode(FullNode fullNode);
|
boolean updateNode(FullNode fullNode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given node from the nodes list
|
||||||
|
* @param fullNode The node to remove
|
||||||
|
*/
|
||||||
|
void removeNode(FullNode fullNode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an ordered list of {@link FullNode} instances.
|
* Returns an ordered list of {@link FullNode} instances.
|
||||||
* @return
|
* @return The sorted list of nodes.
|
||||||
*/
|
*/
|
||||||
List<FullNode> getSortedNodes();
|
List<FullNode> getSortedNodes();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="cy.agorise.labs.sample">
|
package="cy.agorise.labs.sample">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
@ -11,7 +12,8 @@
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme"
|
||||||
|
tools:ignore="GoogleAppIndexingWarning">
|
||||||
<activity android:name=".SubscriptionActivity" />
|
<activity android:name=".SubscriptionActivity" />
|
||||||
<activity android:name=".CallsActivity">
|
<activity android:name=".CallsActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
@ -19,7 +21,8 @@
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
<activity android:name=".PerformCallActivity"></activity>
|
<activity android:name=".PerformCallActivity" />
|
||||||
|
<activity android:name=".RemoveNodeActivity" />
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
|
@ -25,6 +25,8 @@ import io.reactivex.functions.Consumer;
|
||||||
public class CallsActivity extends AppCompatActivity {
|
public class CallsActivity extends AppCompatActivity {
|
||||||
private final String TAG = this.getClass().getName();
|
private final String TAG = this.getClass().getName();
|
||||||
|
|
||||||
|
private static final String REMOVE_CURRENT_NODE = "remove_current_node";
|
||||||
|
|
||||||
@BindView(R.id.call_list)
|
@BindView(R.id.call_list)
|
||||||
RecyclerView mRecyclerView;
|
RecyclerView mRecyclerView;
|
||||||
|
|
||||||
|
@ -75,7 +77,8 @@ public class CallsActivity extends AppCompatActivity {
|
||||||
RPC.CALL_SET_SUBSCRIBE_CALLBACK,
|
RPC.CALL_SET_SUBSCRIBE_CALLBACK,
|
||||||
RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES,
|
RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES,
|
||||||
RPC.CALL_GET_KEY_REFERENCES,
|
RPC.CALL_GET_KEY_REFERENCES,
|
||||||
RPC.CALL_GET_ACCOUNT_BALANCES
|
RPC.CALL_GET_ACCOUNT_BALANCES,
|
||||||
|
REMOVE_CURRENT_NODE
|
||||||
};
|
};
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@ -96,6 +99,8 @@ public class CallsActivity extends AppCompatActivity {
|
||||||
Intent intent;
|
Intent intent;
|
||||||
if(selectedCall.equals(RPC.CALL_SET_SUBSCRIBE_CALLBACK)){
|
if(selectedCall.equals(RPC.CALL_SET_SUBSCRIBE_CALLBACK)){
|
||||||
intent = new Intent(CallsActivity.this, SubscriptionActivity.class);
|
intent = new Intent(CallsActivity.this, SubscriptionActivity.class);
|
||||||
|
} else if (selectedCall.equals(REMOVE_CURRENT_NODE)){
|
||||||
|
intent = new Intent(CallsActivity.this, RemoveNodeActivity.class);
|
||||||
}else{
|
}else{
|
||||||
intent = new Intent(CallsActivity.this, PerformCallActivity.class);
|
intent = new Intent(CallsActivity.this, PerformCallActivity.class);
|
||||||
intent.putExtra(Constants.KEY_SELECTED_CALL, selectedCall);
|
intent.putExtra(Constants.KEY_SELECTED_CALL, selectedCall);
|
||||||
|
|
|
@ -0,0 +1,286 @@
|
||||||
|
package cy.agorise.labs.sample;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Typeface;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
import android.support.v7.util.SortedList;
|
||||||
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.text.SpannableStringBuilder;
|
||||||
|
import android.text.Spanned;
|
||||||
|
import android.text.style.ForegroundColorSpan;
|
||||||
|
import android.text.style.StyleSpan;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import butterknife.BindView;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
import butterknife.OnClick;
|
||||||
|
import cy.agorise.graphenej.api.android.NetworkService;
|
||||||
|
import cy.agorise.graphenej.network.FullNode;
|
||||||
|
import io.reactivex.Observer;
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.disposables.Disposable;
|
||||||
|
import io.reactivex.subjects.PublishSubject;
|
||||||
|
|
||||||
|
public class RemoveNodeActivity extends AppCompatActivity implements ServiceConnection {
|
||||||
|
|
||||||
|
private final String TAG = this.getClass().getName();
|
||||||
|
|
||||||
|
@BindView(R.id.rvNodes)
|
||||||
|
RecyclerView rvNodes;
|
||||||
|
|
||||||
|
FullNodesAdapter nodesAdapter;
|
||||||
|
|
||||||
|
// Comparator used to sort the nodes in ascending order
|
||||||
|
private final Comparator<FullNode> LATENCY_COMPARATOR = (a, b) ->
|
||||||
|
Double.compare(a.getLatencyValue(), b.getLatencyValue());
|
||||||
|
|
||||||
|
/* Network service connection */
|
||||||
|
private NetworkService mNetworkService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_remove_node);
|
||||||
|
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
|
rvNodes.setLayoutManager(new LinearLayoutManager(this));
|
||||||
|
nodesAdapter = new FullNodesAdapter(this, LATENCY_COMPARATOR);
|
||||||
|
rvNodes.setAdapter(nodesAdapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.btnRemoveCurrentNode)
|
||||||
|
public void removeCurrentNode() {
|
||||||
|
mNetworkService.removeCurrentNodeAndReconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||||
|
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||||
|
NetworkService.LocalBinder binder = (NetworkService.LocalBinder) iBinder;
|
||||||
|
mNetworkService = binder.getService();
|
||||||
|
|
||||||
|
if(mNetworkService != null){
|
||||||
|
// PublishSubject used to announce full node latencies updates
|
||||||
|
PublishSubject<FullNode> fullNodePublishSubject = mNetworkService.getNodeLatencyObservable();
|
||||||
|
if(fullNodePublishSubject != null)
|
||||||
|
fullNodePublishSubject.observeOn(AndroidSchedulers.mainThread()).subscribe(nodeLatencyObserver);
|
||||||
|
|
||||||
|
List<FullNode> fullNodes = mNetworkService.getNodes();
|
||||||
|
nodesAdapter.add(fullNodes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceDisconnected(ComponentName componentName) {
|
||||||
|
mNetworkService = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Observer used to be notified about node latency measurement updates.
|
||||||
|
*/
|
||||||
|
private Observer<FullNode> nodeLatencyObserver = new Observer<FullNode>() {
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(Disposable d) { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(FullNode fullNode) {
|
||||||
|
if (!fullNode.isRemoved())
|
||||||
|
nodesAdapter.add(fullNode);
|
||||||
|
else
|
||||||
|
nodesAdapter.remove(fullNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable e) {
|
||||||
|
Log.e(TAG,"nodeLatencyObserver.onError.Msg: "+e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() { }
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
// Bind to LocalService
|
||||||
|
Intent intent = new Intent(this, NetworkService.class);
|
||||||
|
bindService(intent, this, Context.BIND_AUTO_CREATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
unbindService(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
class FullNodesAdapter extends RecyclerView.Adapter<FullNodesAdapter.ViewHolder> {
|
||||||
|
|
||||||
|
class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
|
ImageView ivNodeStatus;
|
||||||
|
TextView tvNodeName;
|
||||||
|
|
||||||
|
ViewHolder(View itemView) {
|
||||||
|
super(itemView);
|
||||||
|
|
||||||
|
ivNodeStatus = itemView.findViewById(R.id.ivNodeStatus);
|
||||||
|
tvNodeName = itemView.findViewById(R.id.tvNodeName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final SortedList<FullNode> mSortedList = new SortedList<>(FullNode.class, new SortedList.Callback<FullNode>() {
|
||||||
|
@Override
|
||||||
|
public void onInserted(int position, int count) {
|
||||||
|
notifyItemRangeInserted(position, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemoved(int position, int count) {
|
||||||
|
notifyItemRangeRemoved(position, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMoved(int fromPosition, int toPosition) {
|
||||||
|
notifyItemMoved(fromPosition, toPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged(int position, int count) {
|
||||||
|
notifyItemRangeChanged(position, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(FullNode a, FullNode b) {
|
||||||
|
return mComparator.compare(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areContentsTheSame(FullNode oldItem, FullNode newItem) {
|
||||||
|
return oldItem.getLatencyValue() == newItem.getLatencyValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean areItemsTheSame(FullNode item1, FullNode item2) {
|
||||||
|
return item1.getUrl().equals(item2.getUrl());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
private final Comparator<FullNode> mComparator;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
FullNodesAdapter(Context context, Comparator<FullNode> comparator) {
|
||||||
|
mContext = context;
|
||||||
|
mComparator = comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Context getContext() {
|
||||||
|
return mContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public FullNodesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
Context context = parent.getContext();
|
||||||
|
LayoutInflater inflater = LayoutInflater.from(context);
|
||||||
|
|
||||||
|
View transactionView = inflater.inflate(R.layout.item_node, parent, false);
|
||||||
|
|
||||||
|
return new ViewHolder(transactionView);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
|
||||||
|
final FullNode fullNode = mSortedList.get(position);
|
||||||
|
|
||||||
|
// Show the green check mark before the node name if that node is the one being used
|
||||||
|
if (fullNode.isConnected())
|
||||||
|
viewHolder.ivNodeStatus.setImageResource(R.drawable.ic_connected);
|
||||||
|
else
|
||||||
|
viewHolder.ivNodeStatus.setImageDrawable(null);
|
||||||
|
|
||||||
|
double latency = fullNode.getLatencyValue();
|
||||||
|
|
||||||
|
// Select correct color span according to the latency value
|
||||||
|
ForegroundColorSpan colorSpan;
|
||||||
|
|
||||||
|
if (latency < 400)
|
||||||
|
colorSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary));
|
||||||
|
else if (latency < 800)
|
||||||
|
colorSpan = new ForegroundColorSpan(Color.rgb(255,136,0)); // Holo orange
|
||||||
|
else
|
||||||
|
colorSpan = new ForegroundColorSpan(Color.rgb(204,0,0)); // Holo red
|
||||||
|
|
||||||
|
// Create a string with the latency number colored according to their amount
|
||||||
|
SpannableStringBuilder ssb = new SpannableStringBuilder();
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
ssb.append(fullNode.getUrl().replace("wss://", ""), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
}
|
||||||
|
ssb.append(" (");
|
||||||
|
|
||||||
|
// 2000 ms is the timeout of the websocket used to calculate the latency, therefore if the
|
||||||
|
// received latency is greater than such value we can assume the node was not reachable.
|
||||||
|
String ms = latency < 2000 ? String.format(Locale.US, "%.0f ms", latency) : "??";
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
ssb.append(ms, colorSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
}
|
||||||
|
ssb.append(")");
|
||||||
|
|
||||||
|
viewHolder.tvNodeName.setText(ssb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functions that adds/updates a FullNode to the SortedList
|
||||||
|
*/
|
||||||
|
public void add(FullNode fullNode) {
|
||||||
|
// Remove the old instance of the FullNode before adding a new one. My understanding is that
|
||||||
|
// the sorted list should be able to automatically find repeated elements and update them
|
||||||
|
// instead of adding duplicates but it wasn't working so I opted for manually removing old
|
||||||
|
// instances of FullNodes before adding the updated ones.
|
||||||
|
int removed = 0;
|
||||||
|
for (int i=0; i<mSortedList.size(); i++)
|
||||||
|
if (mSortedList.get(i - removed).getUrl().equals(fullNode.getUrl()))
|
||||||
|
mSortedList.removeItemAt(i-removed++);
|
||||||
|
|
||||||
|
mSortedList.add(fullNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function that adds a whole list of nodes to the SortedList. It should only be used at the
|
||||||
|
* moment of populating the SortedList for the first time.
|
||||||
|
*/
|
||||||
|
public void add(List<FullNode> fullNodes) {
|
||||||
|
mSortedList.addAll(fullNodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(FullNode fullNode) {
|
||||||
|
mSortedList.remove(fullNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return mSortedList.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
sample/src/main/res/drawable/ic_connected.xml
Normal file
5
sample/src/main/res/drawable/ic_connected.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<vector android:height="24dp" android:tint="#32C832"
|
||||||
|
android:viewportHeight="24.0" android:viewportWidth="24.0"
|
||||||
|
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<path android:fillColor="#FF000000" android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
|
||||||
|
</vector>
|
|
@ -4,6 +4,6 @@
|
||||||
android:id="@+id/call_list"
|
android:id="@+id/call_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".CallsActivity">
|
tools:context=".CallsActivity"
|
||||||
|
tools:listitem="@layout/item_call"
|
||||||
</android.support.v7.widget.RecyclerView>
|
tools:itemCount="5"/>
|
|
@ -11,8 +11,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/container_param1"
|
app:layout_constraintBottom_toTopOf="@+id/container_param1"
|
||||||
|
@ -32,8 +30,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/container_param2"
|
app:layout_constraintBottom_toTopOf="@+id/container_param2"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
@ -51,8 +47,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/container_param3"
|
app:layout_constraintBottom_toTopOf="@+id/container_param3"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
@ -69,8 +63,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/container_param4"
|
app:layout_constraintBottom_toTopOf="@+id/container_param4"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
@ -87,8 +79,6 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
app:layout_constraintBottom_toTopOf="@+id/button_send"
|
app:layout_constraintBottom_toTopOf="@+id/button_send"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
@ -106,8 +96,6 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="8dp"
|
android:layout_marginBottom="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginLeft="8dp"
|
|
||||||
android:layout_marginRight="8dp"
|
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:text="@string/action_send"
|
android:text="@string/action_send"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
|
25
sample/src/main/res/layout/activity_remove_node.xml
Normal file
25
sample/src/main/res/layout/activity_remove_node.xml
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<android.support.v7.widget.RecyclerView
|
||||||
|
android:id="@+id/rvNodes"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:layout_constraintBottom_toTopOf="@+id/btnRemoveCurrentNode"
|
||||||
|
tools:listitem="@layout/item_node"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnRemoveCurrentNode"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Remove current node"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"/>
|
||||||
|
|
||||||
|
</android.support.constraint.ConstraintLayout>
|
|
@ -25,14 +25,14 @@
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:textSize="11sp"
|
android:textSize="12sp"
|
||||||
android:text="Subscribe"/>
|
android:text="Subscribe"/>
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/unsubscribe"
|
android:id="@+id/unsubscribe"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:textSize="11sp"
|
android:textSize="12sp"
|
||||||
android:text="Unsubscribe"/>
|
android:text="Unsubscribe"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</android.support.constraint.ConstraintLayout>
|
</android.support.constraint.ConstraintLayout>
|
||||||
|
|
|
@ -9,5 +9,6 @@
|
||||||
android:padding="16dp"
|
android:padding="16dp"
|
||||||
android:background="?attr/selectableItemBackground"
|
android:background="?attr/selectableItemBackground"
|
||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
|
android:focusable="true"
|
||||||
tools:text="Sample">
|
tools:text="Sample">
|
||||||
</TextView>
|
</TextView>
|
30
sample/src/main/res/layout/item_node.xml
Normal file
30
sample/src/main/res/layout/item_node.xml
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="24dp"
|
||||||
|
android:paddingEnd="24dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="2dp">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivNodeStatus"
|
||||||
|
android:layout_width="16dp"
|
||||||
|
android:layout_height="16dp"
|
||||||
|
tools:src="@drawable/ic_connected"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvNodeName"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="2dp"
|
||||||
|
android:layout_marginLeft="2dp"
|
||||||
|
tools:text="de.palmpayisthebestappinthefreakingworld.io/ws (123ms)"
|
||||||
|
android:ellipsize="middle"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textColor="#222"
|
||||||
|
android:textSize="16sp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in a new issue