graphenej/sample/src/main/java/cy/agorise/labs/sample/PerformCallActivity.java

323 lines
12 KiB
Java
Raw Normal View History

package cy.agorise.labs.sample;
import android.content.ComponentName;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.ConnectionStatusUpdate;
import cy.agorise.graphenej.api.android.DeserializationMap;
import cy.agorise.graphenej.api.android.RxBus;
import cy.agorise.graphenej.api.calls.GetAccounts;
import cy.agorise.graphenej.api.calls.GetBlock;
import cy.agorise.graphenej.api.calls.GetObjects;
import cy.agorise.graphenej.models.JsonRpcResponse;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperation;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
public class PerformCallActivity extends ConnectedActivity {
private final String TAG = this.getClass().getName();
@BindView(R.id.response)
TextView mResponseView;
@BindView(R.id.container_param1)
TextInputLayout mParam1View;
@BindView(R.id.container_param2)
TextInputLayout mParam2View;
@BindView(R.id.container_param3)
TextInputLayout mParam3View;
@BindView(R.id.container_param4)
TextInputLayout mParam4View;
@BindView(R.id.param1)
TextInputEditText param1;
@BindView(R.id.param2)
TextInputEditText param2;
@BindView(R.id.param3)
TextInputEditText param3;
@BindView(R.id.param4)
TextInputEditText param4;
@BindView(R.id.button_send)
Button mButtonSend;
// Field used to map a request id to its type
private HashMap<Long, String> responseMap = new HashMap<>();
// Current request type. Ex: 'get_objects', 'get_accounts', etc
private String mRPC;
private Disposable mDisposable;
private Gson gson = new GsonBuilder()
.setExclusionStrategies(new DeserializationMap.SkipAccountOptionsStrategy(), new DeserializationMap.SkipAssetOptionsStrategy())
.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer())
.registerTypeAdapter(Memo.class, new Memo.MemoSerializer())
.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perform_call);
ButterKnife.bind(this);
mRPC = getIntent().getStringExtra(Constants.KEY_SELECTED_CALL);
Log.d(TAG,"Selected call: "+mRPC);
switch (mRPC){
case RPC.CALL_GET_OBJECTS:
setupGetObjects();
break;
case RPC.CALL_GET_ACCOUNTS:
setupGetAccounts();
break;
case RPC.CALL_GET_BLOCK:
setupGetBlock();
break;
case RPC.CALL_GET_BLOCK_HEADER:
setupGetBlockHeader();
break;
case RPC.CALL_GET_MARKET_HISTORY:
setupGetMarketHistory();
break;
case RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY:
setupGetRelativeAccountHistory();
break;
case RPC.CALL_GET_REQUIRED_FEES:
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
setupLookupAssetSymbols();
break;
default:
Log.d(TAG,"Default called");
}
mDisposable = RxBus.getBusInstance()
.asFlowable()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object message) throws Exception {
Log.d(TAG,"accept. Msg class: "+message.getClass());
if(message instanceof ConnectionStatusUpdate){
// TODO: Update UI ?
}else if(message instanceof JsonRpcResponse){
handleJsonRpcResponse((JsonRpcResponse) message);
}
}
});
}
private void setupGetObjects(){
requiredInput(1);
mParam1View.setHint(getResources().getString(R.string.get_objects_arg1));
}
private void setupGetAccounts(){
requiredInput(1);
mParam1View.setHint(getResources().getString(R.string.get_accounts_arg1));
}
private void setupGetBlock(){
requiredInput(1);
mParam1View.setHint(getResources().getString(R.string.get_block_arg1));
}
private void setupGetBlockHeader(){
requiredInput(1);
mParam1View.setHint(getResources().getString(R.string.get_block_arg1));
}
private void setupGetMarketHistory(){
requiredInput(4);
Resources resources = getResources();
mParam1View.setHint(resources.getString(R.string.get_market_history_arg1));
mParam2View.setHint(resources.getString(R.string.get_market_history_arg2));
mParam3View.setHint(resources.getString(R.string.get_market_history_arg3));
mParam4View.setHint(resources.getString(R.string.get_market_history_arg4));
}
private void setupGetRelativeAccountHistory(){
requiredInput(4);
Resources resources = getResources();
mParam1View.setHint(resources.getString(R.string.get_relative_account_history_arg1));
mParam2View.setHint(resources.getString(R.string.get_relative_account_history_arg2));
mParam3View.setHint(resources.getString(R.string.get_relative_account_history_arg3));
mParam4View.setHint(resources.getString(R.string.get_relative_account_history_arg4));
}
private void setupLookupAssetSymbols(){
requiredInput(4);
Resources resources = getResources();
mParam1View.setHint(resources.getString(R.string.lookup_asset_symbols_arg1));
mParam2View.setHint(resources.getString(R.string.lookup_asset_symbols_arg2));
mParam3View.setHint(resources.getString(R.string.lookup_asset_symbols_arg3));
mParam4View.setHint(resources.getString(R.string.lookup_asset_symbols_arg4));
}
private void requiredInput(int inputCount){
if(inputCount == 1){
mParam1View.setVisibility(View.VISIBLE);
mParam2View.setVisibility(View.GONE);
mParam3View.setVisibility(View.GONE);
mParam4View.setVisibility(View.GONE);
}else if(inputCount == 2){
mParam1View.setVisibility(View.VISIBLE);
mParam2View.setVisibility(View.VISIBLE);
mParam3View.setVisibility(View.GONE);
mParam4View.setVisibility(View.GONE);
}else if(inputCount == 3){
mParam1View.setVisibility(View.VISIBLE);
mParam2View.setVisibility(View.VISIBLE);
mParam3View.setVisibility(View.VISIBLE);
mParam4View.setVisibility(View.GONE);
}else if(inputCount == 4){
mParam1View.setVisibility(View.VISIBLE);
mParam2View.setVisibility(View.VISIBLE);
mParam3View.setVisibility(View.VISIBLE);
mParam4View.setVisibility(View.VISIBLE);
}
}
@OnClick(R.id.button_send)
public void onSendClicked(Button v){
switch (mRPC){
case RPC.CALL_GET_OBJECTS:
sendGetObjectsRequest();
break;
case RPC.CALL_GET_ACCOUNTS:
sendGetAccountsRequest();
break;
case RPC.CALL_GET_BLOCK:
break;
case RPC.CALL_GET_BLOCK_HEADER:
break;
case RPC.CALL_GET_MARKET_HISTORY:
break;
case RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY:
break;
case RPC.CALL_GET_REQUIRED_FEES:
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
break;
default:
Log.d(TAG,"Default called");
}
}
private void sendGetObjectsRequest(){
String objectId = param1.getText().toString();
if(objectId.matches("\\d\\.\\d{1,3}\\.\\d{1,10}")){
ArrayList<String> array = new ArrayList<>();
array.add(objectId);
GetObjects getObjects = new GetObjects(array);
long id = mNetworkService.sendMessage(getObjects, GetObjects.REQUIRED_API);
responseMap.put(id, mRPC);
}else{
param1.setError(getResources().getString(R.string.error_input_id));
}
}
private void sendGetAccountsRequest(){
String userId = param1.getText().toString();
if(userId.matches("\\d\\.\\d{1,3}\\.\\d{1,10}")){
GetAccounts getAccounts = new GetAccounts(new UserAccount(userId));
long id = mNetworkService.sendMessage(getAccounts, GetBlock.REQUIRED_API);
responseMap.put(id, mRPC);
}else{
param1.setError(getResources().getString(R.string.error_input_id));
}
}
/**
* Internal method that will decide what to do with each JSON-RPC response
*
* @param response The JSON-RPC api call response
*/
private void handleJsonRpcResponse(JsonRpcResponse response){
long id = response.id;
if(responseMap.get(id) != null){
String request = responseMap.get(id);
switch(request){
case RPC.CALL_GET_ACCOUNTS:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_BLOCK:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_BLOCK_HEADER:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_MARKET_HISTORY:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_ACCOUNT_HISTORY:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_RELATIVE_ACCOUNT_HISTORY:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_GET_REQUIRED_FEES:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
case RPC.CALL_LOOKUP_ASSET_SYMBOLS:
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
break;
default:
Log.w(TAG,"Case not handled");
mResponseView.setText(mResponseView.getText() + response.result.toString());
}
// Remember to remove the used id entry from the map, as it would
// otherwise just increase the app's memory usage
responseMap.remove(id);
}else{
Log.d(TAG,"No entry");
mResponseView.setText(mResponseView.getText() + gson.toJson(response, JsonRpcResponse.class) + "\n");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(!mDisposable.isDisposed())
mDisposable.dispose();
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// Called upon NetworkService connection
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
// Called upon NetworkService disconnection
}
}