Create RemoveNodeActivity in the sample project that shows an updated list of nodes sorted by latency. This whole activity while be used to add and test the functionallity of removing a node from the nodes list when the app that uses graphenej decides so, and then reconnects to the next best node.
This commit is contained in:
parent
94511daea9
commit
21311ea5a3
6 changed files with 345 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="cy.agorise.labs.sample">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
@ -11,7 +12,8 @@
|
|||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
android:theme="@style/AppTheme"
|
||||
tools:ignore="GoogleAppIndexingWarning">
|
||||
<activity android:name=".SubscriptionActivity" />
|
||||
<activity android:name=".CallsActivity">
|
||||
<intent-filter>
|
||||
|
@ -19,7 +21,8 @@
|
|||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".PerformCallActivity"></activity>
|
||||
<activity android:name=".PerformCallActivity" />
|
||||
<activity android:name=".RemoveNodeActivity" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -17,6 +17,8 @@ import cy.agorise.graphenej.RPC;
|
|||
|
||||
public class CallsActivity extends AppCompatActivity {
|
||||
|
||||
private static final String REMOVE_CURRENT_NODE = "remove_current_node";
|
||||
|
||||
@BindView(R.id.call_list)
|
||||
RecyclerView mRecyclerView;
|
||||
|
||||
|
@ -50,7 +52,8 @@ public class CallsActivity extends AppCompatActivity {
|
|||
RPC.CALL_SET_SUBSCRIBE_CALLBACK,
|
||||
RPC.CALL_GET_DYNAMIC_GLOBAL_PROPERTIES,
|
||||
RPC.CALL_GET_KEY_REFERENCES,
|
||||
RPC.CALL_GET_ACCOUNT_BALANCES
|
||||
RPC.CALL_GET_ACCOUNT_BALANCES,
|
||||
REMOVE_CURRENT_NODE
|
||||
};
|
||||
|
||||
@NonNull
|
||||
|
@ -71,6 +74,8 @@ public class CallsActivity extends AppCompatActivity {
|
|||
Intent intent;
|
||||
if(selectedCall.equals(RPC.CALL_SET_SUBSCRIBE_CALLBACK)){
|
||||
intent = new Intent(CallsActivity.this, SubscriptionActivity.class);
|
||||
} else if (selectedCall.equals(REMOVE_CURRENT_NODE)){
|
||||
intent = new Intent(CallsActivity.this, RemoveNodeActivity.class);
|
||||
}else{
|
||||
intent = new Intent(CallsActivity.this, PerformCallActivity.class);
|
||||
intent.putExtra(Constants.KEY_SELECTED_CALL, selectedCall);
|
||||
|
|
|
@ -0,0 +1,274 @@
|
|||
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 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);
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
nodesAdapter.add(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);
|
||||
}
|
||||
|
||||
|
||||
@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>
|
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>
|
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