From 8cfbb63f1c227b06e2b58382d59f27842e7ed351 Mon Sep 17 00:00:00 2001 From: hvarona Date: Wed, 5 Sep 2018 23:43:37 -0400 Subject: [PATCH] Change the insight serverUrl, to make it not a constant but selected by the app --- .../insightapi/AccountActivityWatcher.java | 7 +--- .../insightapi/BroadcastTransaction.java | 3 +- .../insightapi/GetEstimateFee.java | 40 ++++--------------- .../insightapi/GetTransactionByAddress.java | 3 +- .../insightapi/GetTransactionData.java | 12 +++--- 5 files changed, 18 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java index aa8624e..8bbcdf8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/AccountActivityWatcher.java @@ -106,15 +106,10 @@ public class AccountActivityWatcher { * @param mAccount The mAccount to be monitor * @param mContext This app mContext */ - public AccountActivityWatcher(GeneralCoinAccount mAccount, Context mContext) { - //String serverUrl = InsightApiConstants.protocol + "://" + InsightApiConstants.getAddress(mAccount.getCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCoin()) + "/"+InsightApiConstants.getRawPath(mAccount.getCoin())+"/mSocket.io/"; - String serverUrl = InsightApiConstants.sProtocolSocketIO + "://" + InsightApiConstants.getAddress(mAccount.getCryptoCoin()) + ":" + InsightApiConstants.getPort(mAccount.getCryptoCoin()) + "/"; + public AccountActivityWatcher(String serverUrl, GeneralCoinAccount mAccount, Context mContext) { this.mAccount = mAccount; this.mContext = mContext; - System.out.println("accountActivityWatcher " + serverUrl); try { - IO.Options opts = new IO.Options(); - System.out.println("accountActivityWatcher default path " + opts.path); this.mSocket = IO.socket(serverUrl); this.mSocket.on(Socket.EVENT_CONNECT, onConnect); this.mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/BroadcastTransaction.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/BroadcastTransaction.java index ae91afb..d2b44a9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/BroadcastTransaction.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/BroadcastTransaction.java @@ -37,8 +37,7 @@ public class BroadcastTransaction extends Thread implements Callback { * @param account The account who signs the transaction * @param context This app context */ - public BroadcastTransaction(String RawTx, GeneralCoinAccount account, Context context){ - String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/"; + public BroadcastTransaction(String RawTx, GeneralCoinAccount account, String serverUrl, Context context){ this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl); this.mContext = context; this.mRawTx = RawTx; diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetEstimateFee.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetEstimateFee.java index 933d2b4..c175238 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetEstimateFee.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetEstimateFee.java @@ -20,55 +20,31 @@ import retrofit2.Response; public abstract class GetEstimateFee { - //TODO add a funciton to get the rate of a specific port - /** * The funciton to get the rate for the transaction be included in the next 2 blocks * @param coin The coin to get the rate - * @return The rate number (coin/kbytes) - * @throws IOException If the server answer null, or the rate couldn't be calculated */ - public static long getEstimateFee(final CryptoCoin coin) throws IOException { - String serverUrl = InsightApiConstants.sProtocol + "://" - + InsightApiConstants.getAddress(coin) + "/"; + public static void getEstimateFee(final CryptoCoin coin, String serverUrl, final estimateFeeListener listener) { InsightApiServiceGenerator serviceGenerator = new InsightApiServiceGenerator(serverUrl); InsightApiService service = serviceGenerator.getService(InsightApiService.class); Call call = service.estimateFee(InsightApiConstants.getPath(coin)); - final Object SYNC = new Object(); final JsonObject answer = new JsonObject(); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { - synchronized (SYNC) { - answer.addProperty("answer", - (long) (response.body().get("2").getAsDouble()* Math.pow(10, coin.getPrecision()))); - SYNC.notifyAll(); - } + listener.estimateFee((long) (answer.get("answer").getAsDouble())); + } @Override public void onFailure(Call call, Throwable t) { - synchronized (SYNC) { - SYNC.notifyAll(); - } + listener.estimateFee(-1); } }); - synchronized (SYNC){ - for(int i = 0; i < 6; i++) { - try { - SYNC.wait(5000); - } catch (InterruptedException e) { - // this interruption never rises - } - if(answer.get("answer")!=null){ - break; - } - } - } - if(answer.get("answer")==null){ - throw new IOException(""); - } - return (long) (answer.get("answer").getAsDouble()); + } + + public static interface estimateFeeListener{ + public void estimateFee(long value); } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java index d8052ff..71ee3a4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/insightapi/GetTransactionByAddress.java @@ -48,8 +48,7 @@ public class GetTransactionByAddress extends Thread implements Callback { * This app context, used to save on the DB */ private Context mContext; + + private String mServerUrl; /** * If has to wait for another confirmation */ @@ -47,8 +49,8 @@ public class GetTransactionData extends Thread implements Callback { * @param account The account to be query * @param context This app Context */ - public GetTransactionData(String txid, GeneralCoinAccount account, Context context) { - this(txid, account, context, false); + public GetTransactionData(String txid, GeneralCoinAccount account,String serverUrl, Context context) { + this(txid, account, serverUrl, context, false); } /** @@ -58,8 +60,8 @@ public class GetTransactionData extends Thread implements Callback { * @param context This app Context * @param mustWait If there is less confirmation that needed */ - public GetTransactionData(String txid, GeneralCoinAccount account, Context context, boolean mustWait) { - String serverUrl = InsightApiConstants.sProtocol + "://" + InsightApiConstants.getAddress(account.getCryptoCoin()) +"/"; + public GetTransactionData(String txid, GeneralCoinAccount account,String serverUrl, Context context, boolean mustWait) { + this.mServerUrl = serverUrl; this.mAccount = account; this.mTxId= txid; this.mServiceGenerator = new InsightApiServiceGenerator(serverUrl); @@ -179,7 +181,7 @@ public class GetTransactionData extends Thread implements Callback { if (transaction.getConfirm() < this.mAccount.getCryptoNet().getConfirmationsNeeded()) { //If transaction weren't confirmed, add the transaction to watch for change on the confirmations - new GetTransactionData(this.mTxId, this.mAccount, this.mContext, true).start(); + new GetTransactionData(this.mTxId, this.mAccount, this.mServerUrl, this.mContext, true).start(); } } }