diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index 88f6c45..cecaca0 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import cy.agorise.crystalwallet.network.WebSocketThread; import cy.agorise.graphenej.Address; import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.api.GetAccountByName; @@ -22,12 +23,14 @@ import cy.agorise.graphenej.models.WitnessResponse; /** + * This class manage all the api request directly to the Graphene Servers * Created by henry on 26/9/2017. */ public class GrapheneApiGenerator { //TODO network connections + //TODO make to work with all Graphene stype, not only bitshares private static int connectionTimeout = 5000; private static String url = "http://128.0.69.157:8090"; @@ -38,46 +41,28 @@ public class GrapheneApiGenerator { * @param request The Api request object, to answer this petition */ public static void getAccountById(String accountId, final ApiRequest request){ - WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(connectionTimeout); - try { - final WebSocket webSocket = factory.createSocket(url); - webSocket.addListener(new GetAccounts(accountId, new WitnessResponseListener() { - @Override - public void onSuccess(WitnessResponse response) { - if (response.result.getClass() == ArrayList.class) { - List list = (List) response.result; - if (list.size() > 0) { - if (list.get(0).getClass() == AccountProperties.class) { - request.getListener().success(list.get(0),request.getId()); - return; - //TODO answer a crystal model - } + WebSocketThread thread = new WebSocketThread(new GetAccounts(accountId, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + if (response.result.getClass() == ArrayList.class) { + List list = (List) response.result; + if (list.size() > 0) { + if (list.get(0).getClass() == AccountProperties.class) { + request.getListener().success(list.get(0),request.getId()); + return; + //TODO answer a crystal model } } - request.getListener().fail(request.getId()); } + request.getListener().fail(request.getId()); + } - @Override - public void onError(BaseResponse.Error error) { - request.getListener().fail(request.getId()); - } - })); - Thread thread = new Thread(){ - public void run(){ - android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); - try { - webSocket.connect(); - } catch (WebSocketException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } - } - }; - thread.start(); - } catch (IOException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } + @Override + public void onError(BaseResponse.Error error) { + request.getListener().fail(request.getId()); + } + }),url); + thread.start(); } /** @@ -87,11 +72,7 @@ public class GrapheneApiGenerator { * @param request The Api request object, to answer this petition */ public static void getAccountByOwnerOrActiveAddress(Address address, final ApiRequest request){ - //TODO change address - WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(connectionTimeout); - try { - final WebSocket webSocket = factory.createSocket(url); - webSocket.addListener(new GetKeyReferences(address, true, new WitnessResponseListener() { + WebSocketThread thread = new WebSocketThread(new GetKeyReferences(address, true, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { final List> resp = (List>) response.result; @@ -107,23 +88,9 @@ public class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - })); - Thread thread = new Thread(){ - public void run(){ - android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); - try { - webSocket.connect(); - } catch (WebSocketException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } - } - }; + }),url); + thread.start(); - } catch (IOException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } } /** @@ -136,10 +103,7 @@ public class GrapheneApiGenerator { * @param request The Api request object, to answer this petition */ public static void getAccountTransaction(String accountId, int start, int stop, int limit, final ApiRequest request){ - WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(connectionTimeout); - try { - final WebSocket webSocket = factory.createSocket(url); - webSocket.addListener(new GetRelativeAccountHistory(new UserAccount(accountId), stop, limit, start, new WitnessResponseListener() { + WebSocketThread thread = new WebSocketThread(new GetRelativeAccountHistory(new UserAccount(accountId), stop, limit, start, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { WitnessResponse> resp = response; @@ -150,23 +114,8 @@ public class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - })); - Thread thread = new Thread(){ - public void run(){ - android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); - try { - webSocket.connect(); - } catch (WebSocketException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } - } - }; + }),url); thread.start(); - } catch (IOException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } } /** @@ -176,10 +125,7 @@ public class GrapheneApiGenerator { * @param request The Api request object, to answer this petition */ public static void getAccountIdByName(String accountName, final ApiRequest request){ - WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(connectionTimeout); - try { - final WebSocket webSocket = factory.createSocket(url); - webSocket.addListener(new GetAccountByName(accountName, new WitnessResponseListener() { + WebSocketThread thread = new WebSocketThread(new GetAccountByName(accountName, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { AccountProperties accountProperties = ((WitnessResponse) response).result; @@ -194,22 +140,7 @@ public class GrapheneApiGenerator { public void onError(BaseResponse.Error error) { request.getListener().fail(request.getId()); } - })); - Thread thread = new Thread(){ - public void run(){ - android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); - try { - webSocket.connect(); - } catch (WebSocketException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } - } - }; - thread.start(); - } catch (IOException e) { - e.printStackTrace(); - request.getListener().fail(request.getId()); - } + }),url); + thread.start(); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateExistBitsharesAccountRequest.java b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateExistBitsharesAccountRequest.java new file mode 100644 index 0000000..ace3061 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateExistBitsharesAccountRequest.java @@ -0,0 +1,39 @@ +package cy.agorise.crystalwallet.cryptonetinforequests; + +import cy.agorise.crystalwallet.enums.CryptoCoin; + +/** + * This class validates that an account name exist, this can be used to verified the existing accounts + * or to verified if the name is available to create an Account + * + * Created by henry on 8/10/2017. + */ + +public class ValidateExistBitsharesAccountRequest extends CryptoNetInfoRequest { + // The account name to validate + private String accountName; + // The result of the validation, or null if there isn't a response + private Boolean accountExists; + + public ValidateExistBitsharesAccountRequest(CryptoCoin coin, String accountName) { + super(coin); + this.accountName = accountName; + } + + + public void setAccountExists(boolean value){ + this.accountExists = value; + this.validate(); + } + + public void validate(){ + if ((this.accountExists != null)){ + this._fireOnCarryOutEvent(); + } + } + + public String getAccountName() { + return accountName; + } + +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index 595f851..a5ba668 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -5,6 +5,7 @@ import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequest; import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequestsListener; +import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest; import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest; import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.graphenej.Address; @@ -74,6 +75,21 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } }); + GrapheneApiGenerator.getAccountIdByName(importRequest.getAccountName(),checkAccountName); + } else if (request instanceof ValidateExistBitsharesAccountRequest){ + final ValidateExistBitsharesAccountRequest importRequest = (ValidateExistBitsharesAccountRequest) request; + ApiRequest checkAccountName = new ApiRequest(0, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + importRequest.setAccountExists(true); + } + + @Override + public void fail(int idPetition) { + //TODO verified + importRequest.setAccountExists(false); + } + }); GrapheneApiGenerator.getAccountIdByName(importRequest.getAccountName(),checkAccountName); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/WebSocketThread.java b/app/src/main/java/cy/agorise/crystalwallet/network/WebSocketThread.java new file mode 100644 index 0000000..11d6de2 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/WebSocketThread.java @@ -0,0 +1,126 @@ +package cy.agorise.crystalwallet.network; + +import android.util.Log; + +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFactory; +import com.neovisionaries.ws.client.WebSocketListener; + +import java.io.IOException; +import java.util.HashMap; + +/** + * Created by henry on 8/10/2017. + */ + +public class WebSocketThread extends Thread { + + // The tag of this class for the log + private final String TAG = this.getClass().getName(); + + //This is to manage the differents threads of this app + private static HashMap currentThreads = new HashMap<>(); + // The connection tiemout + private static int connectionTimeout = 5000; + + // The websocket to be used + private WebSocket mWebSocket; + // The socketListener for the websocket to reponse + private WebSocketListener mWebSocketListener; + // The url to connect + private String mUrl; + // If the parameters of this class can be change + private boolean canChange = true; + + + /** + * Basic constructor, + * + * TODO make it throw exception is problem creating the socket + * + * @param webSocketListener The socket listener for the wbesocket to response + * @param url The url to connect + */ + public WebSocketThread(WebSocketListener webSocketListener, String url) { + try { + WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000); + this.mUrl = url; + this.mWebSocketListener = webSocketListener; + this.mWebSocket = factory.createSocket(this.mUrl); + this.mWebSocket.addListener(this.mWebSocketListener); + } catch (IOException e) { + Log.e(TAG, "IOException. Msg: "+e.getMessage()); + } catch(NullPointerException e){ + Log.e(TAG, "NullPointerException at WebsocketWorkerThreas. Msg: "+e.getMessage()); + } + } + + /** + * Gets the current url where the websocket will connect + * @return the full url + */ + public String getUrl() { + return mUrl; + } + + /** + * Sets the url of the websocket to connects, it would not change if this thread was already started + * @param url The full url with the protocol and the ports + */ + public void setUrl(String url) { + if(canChange) { + try { + WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000); + this.mUrl = url; + mWebSocket = factory.createSocket(mUrl); + mWebSocket.addListener(this.mWebSocketListener); + } catch (IOException e) { + Log.e(TAG, "IOException. Msg: " + e.getMessage()); + } catch (NullPointerException e) { + Log.e(TAG, "NullPointerException at WebsocketWorkerThreas. Msg: " + e.getMessage()); + } + } + } + + /** + * Return the class listening for the websocket response + */ + public WebSocketListener getWebSocketListener() { + return mWebSocketListener; + } + + /** + * Sets the class listenening the websocket response, it will not change if this thread was already started + */ + public void setWebSocketListener(WebSocketListener webSocketListener) { + if(canChange) { + try { + WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000); + this.mWebSocketListener = webSocketListener; + mWebSocket = factory.createSocket(mUrl); + mWebSocket.addListener(this.mWebSocketListener); + } catch (IOException e) { + Log.e(TAG, "IOException. Msg: " + e.getMessage()); + } catch (NullPointerException e) { + Log.e(TAG, "NullPointerException at WebsocketWorkerThreas. Msg: " + e.getMessage()); + } + } + } + + @Override + public void run() { + canChange = false; + // Moves the current Thread into the background + android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); + try { + WebSocketThread.currentThreads.put(this.getId(),this); + mWebSocket.connect(); + } catch (WebSocketException e) { + Log.e(TAG, "WebSocketException. Msg: "+e.getMessage()); + } catch(NullPointerException e){ + Log.e(TAG, "NullPointerException. Msg: "+e.getMessage()); + } + WebSocketThread.currentThreads.remove(this.getId()); + } +}