diff --git a/graphenej/src/main/AndroidManifest.xml b/graphenej/src/main/AndroidManifest.xml index 7e71960..245e180 100644 --- a/graphenej/src/main/AndroidManifest.xml +++ b/graphenej/src/main/AndroidManifest.xml @@ -1,6 +1,14 @@ + - + + + + + \ No newline at end of file diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java similarity index 96% rename from sample/src/main/java/com/luminiasoft/labs/sample/NetworkService.java rename to graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index 473c861..e2b84c5 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -1,4 +1,4 @@ -package com.luminiasoft.labs.sample; +package cy.agorise.graphenej.api.android; import android.app.Service; import android.content.Intent; @@ -6,7 +6,6 @@ import android.content.SharedPreferences; import android.os.Binder; import android.os.IBinder; import android.preference.PreferenceManager; -import android.support.annotation.Nullable; import android.util.Log; import com.google.gson.Gson; @@ -20,11 +19,11 @@ import java.util.HashMap; import cy.agorise.graphenej.RPC; import cy.agorise.graphenej.api.ApiAccess; import cy.agorise.graphenej.api.ConnectionStatusUpdate; -import cy.agorise.graphenej.api.android.RxBus; import cy.agorise.graphenej.api.bitshares.Nodes; import cy.agorise.graphenej.api.calls.ApiCallable; import cy.agorise.graphenej.models.ApiCall; import cy.agorise.graphenej.models.JsonRpcResponse; +import io.reactivex.annotations.Nullable; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -32,7 +31,7 @@ import okhttp3.WebSocket; import okhttp3.WebSocketListener; /** - * Service in charge of mantaining a connection to the full node. + * Service in charge of maintaining a connection to the full node. */ public class NetworkService extends Service { @@ -69,127 +68,6 @@ public class NetworkService extends Service { private Gson gson = new Gson(); - private WebSocketListener mWebSocketListener = new WebSocketListener() { - - @Override - public void onOpen(WebSocket webSocket, Response response) { - super.onOpen(webSocket, response); - mWebSocket = webSocket; - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED)); - - if(!isLoggedIn){ - Log.d(TAG,"About to send login request"); - ArrayList loginParams = new ArrayList<>(); - loginParams.add(mUsername); - loginParams.add(mPassword); - ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, ++mCurrentId); - mLastCall = RPC.CALL_LOGIN; - sendMessage(loginCall.toJsonString()); - }else{ - Log.d(TAG,"Already logged in"); - } - } - - @Override - public void onMessage(WebSocket webSocket, String text) { - super.onMessage(webSocket, text); - Log.v(TAG,"< "+text); - JsonRpcResponse response = gson.fromJson(text, JsonRpcResponse.class); - - // We will only handle messages that relate to the login and API accesses here. - if(response.result != null){ - if(mLastCall == RPC.CALL_LOGIN){ - isLoggedIn = true; - - checkNextRequestedApiAccess(); - }else if(mLastCall == RPC.CALL_DATABASE){ - // Deserializing integer response - Type IntegerJsonResponse = new TypeToken>(){}.getType(); - JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); - - // Storing the "database" api id - mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result); - - checkNextRequestedApiAccess(); - }else if(mLastCall == RPC.CALL_HISTORY){ - // Deserializing integer response - Type IntegerJsonResponse = new TypeToken>(){}.getType(); - JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); - - // Storing the "history" api id - mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result); - - checkNextRequestedApiAccess(); - }else if(mLastCall == RPC.CALL_NETWORK_BROADCAST){ - // Deserializing integer response - Type IntegerJsonResponse = new TypeToken>(){}.getType(); - JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); - - // Storing the "network_broadcast" api access - mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result); - - // All calls have been handled at this point - mLastCall = ""; - }else{ - Log.d(TAG,"New unhandled message"); - } - }else{ - Log.w(TAG,"Error.Msg: "+response.error.message); - } - RxBus.getBusInstance().send(response); - } - - private void checkNextRequestedApiAccess(){ - if( (mRequestedApis & ApiAccess.API_DATABASE) == ApiAccess.API_DATABASE && - mApiIds.get(ApiAccess.API_DATABASE) == null){ - // If we need the "database" api access and we don't yet have it - - ApiCall apiCall = new ApiCall(1, RPC.CALL_DATABASE, null, RPC.VERSION, ++mCurrentId); - mLastCall = RPC.CALL_DATABASE; - sendMessage(apiCall.toJsonString()); - } else if( (mRequestedApis & ApiAccess.API_HISTORY) == ApiAccess.API_HISTORY && - mApiIds.get(ApiAccess.API_HISTORY) == null){ - // If we need the "history" api access and we don't yet have it - - ApiCall apiCall = new ApiCall(1, RPC.CALL_HISTORY, null, RPC.VERSION, ++mCurrentId); - mLastCall = RPC.CALL_HISTORY; - sendMessage(apiCall.toJsonString()); - }else if( (mRequestedApis & ApiAccess.API_NETWORK_BROADCAST) == ApiAccess.API_NETWORK_BROADCAST && - mApiIds.get(ApiAccess.API_NETWORK_BROADCAST) == null){ - // If we need the "network_broadcast" api access and we don't yet have it - - ApiCall apiCall = new ApiCall(1, RPC.CALL_NETWORK_BROADCAST, null, RPC.VERSION, ++mCurrentId); - mLastCall = RPC.CALL_NETWORK_BROADCAST; - sendMessage(apiCall.toJsonString()); - } - } - - @Override - public void onClosed(WebSocket webSocket, int code, String reason) { - super.onClosed(webSocket, code, reason); - Log.d(TAG,"onClosed"); - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); - - isLoggedIn = false; - } - - @Override - public void onFailure(WebSocket webSocket, Throwable t, Response response) { - super.onFailure(webSocket, t, response); - Log.e(TAG,"onFailure. Msg: "+t.getMessage()); - isLoggedIn = false; - if(response != null){ - Log.e(TAG,"Response: "+response.message()); - } - for(StackTraceElement element : t.getStackTrace()){ - Log.v(TAG,String.format("%s#%s:%d", element.getClassName(), element.getMethodName(), element.getLineNumber())); - } - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); - mSocketIndex++; - connect(); - } - }; - @Override public void onCreate() { super.onCreate(); @@ -260,4 +138,125 @@ public class NetworkService extends Service { return NetworkService.this; } } + + private WebSocketListener mWebSocketListener = new WebSocketListener() { + + @Override + public void onOpen(WebSocket webSocket, Response response) { + super.onOpen(webSocket, response); + mWebSocket = webSocket; + + // Notifying all listeners about the new connection status + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED)); + + // If we're not yet logged in, we should do it now + if(!isLoggedIn){ + ArrayList loginParams = new ArrayList<>(); + loginParams.add(mUsername); + loginParams.add(mPassword); + ApiCall loginCall = new ApiCall(1, RPC.CALL_LOGIN, loginParams, RPC.VERSION, ++mCurrentId); + mLastCall = RPC.CALL_LOGIN; + sendMessage(loginCall.toJsonString()); + } + } + + @Override + public void onMessage(WebSocket webSocket, String text) { + super.onMessage(webSocket, text); + Log.v(TAG,"< "+text); + JsonRpcResponse response = gson.fromJson(text, JsonRpcResponse.class); + + // We will only handle messages that relate to the login and API accesses here. + if(response.result != null){ + if(mLastCall == RPC.CALL_LOGIN){ + isLoggedIn = true; + + checkNextRequestedApiAccess(); + }else if(mLastCall == RPC.CALL_DATABASE){ + // Deserializing integer response + Type IntegerJsonResponse = new TypeToken>(){}.getType(); + JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); + + // Storing the "database" api id + mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result); + + checkNextRequestedApiAccess(); + }else if(mLastCall == RPC.CALL_HISTORY){ + // Deserializing integer response + Type IntegerJsonResponse = new TypeToken>(){}.getType(); + JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); + + // Storing the "history" api id + mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result); + + checkNextRequestedApiAccess(); + }else if(mLastCall == RPC.CALL_NETWORK_BROADCAST){ + // Deserializing integer response + Type IntegerJsonResponse = new TypeToken>(){}.getType(); + JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); + + // Storing the "network_broadcast" api access + mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result); + + // All calls have been handled at this point + mLastCall = ""; + }else{ + Log.d(TAG,"New unhandled message"); + } + }else{ + Log.w(TAG,"Error.Msg: "+response.error.message); + } + + RxBus.getBusInstance().send(response); + } + + private void checkNextRequestedApiAccess(){ + if( (mRequestedApis & ApiAccess.API_DATABASE) == ApiAccess.API_DATABASE && + mApiIds.get(ApiAccess.API_DATABASE) == null){ + // If we need the "database" api access and we don't yet have it + + ApiCall apiCall = new ApiCall(1, RPC.CALL_DATABASE, null, RPC.VERSION, ++mCurrentId); + mLastCall = RPC.CALL_DATABASE; + sendMessage(apiCall.toJsonString()); + } else if( (mRequestedApis & ApiAccess.API_HISTORY) == ApiAccess.API_HISTORY && + mApiIds.get(ApiAccess.API_HISTORY) == null){ + // If we need the "history" api access and we don't yet have it + + ApiCall apiCall = new ApiCall(1, RPC.CALL_HISTORY, null, RPC.VERSION, ++mCurrentId); + mLastCall = RPC.CALL_HISTORY; + sendMessage(apiCall.toJsonString()); + }else if( (mRequestedApis & ApiAccess.API_NETWORK_BROADCAST) == ApiAccess.API_NETWORK_BROADCAST && + mApiIds.get(ApiAccess.API_NETWORK_BROADCAST) == null){ + // If we need the "network_broadcast" api access and we don't yet have it + + ApiCall apiCall = new ApiCall(1, RPC.CALL_NETWORK_BROADCAST, null, RPC.VERSION, ++mCurrentId); + mLastCall = RPC.CALL_NETWORK_BROADCAST; + sendMessage(apiCall.toJsonString()); + } + } + + @Override + public void onClosed(WebSocket webSocket, int code, String reason) { + super.onClosed(webSocket, code, reason); + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); + + isLoggedIn = false; + } + + @Override + public void onFailure(WebSocket webSocket, Throwable t, Response response) { + super.onFailure(webSocket, t, response); + Log.e(TAG,"onFailure. Msg: "+t.getMessage()); + isLoggedIn = false; + if(response != null){ + Log.e(TAG,"Response: "+response.message()); + } + for(StackTraceElement element : t.getStackTrace()){ + Log.v(TAG,String.format("%s#%s:%d", element.getClassName(), element.getMethodName(), element.getLineNumber())); + } + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); + mSocketIndex++; + connect(); + } + }; } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 8762327..6c72283 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -18,8 +18,6 @@ - - diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java b/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java index 464b1f0..3cb2487 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java +++ b/sample/src/main/java/com/luminiasoft/labs/sample/MainActivity.java @@ -19,6 +19,7 @@ import butterknife.ButterKnife; import butterknife.OnClick; import cy.agorise.graphenej.api.ApiAccess; import cy.agorise.graphenej.api.ConnectionStatusUpdate; +import cy.agorise.graphenej.api.android.NetworkService; import cy.agorise.graphenej.api.android.RxBus; import cy.agorise.graphenej.api.calls.GetBlock; import cy.agorise.graphenej.models.JsonRpcResponse; diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/SampleApplication.java b/sample/src/main/java/com/luminiasoft/labs/sample/SampleApplication.java index 124c7cd..495e6ef 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/SampleApplication.java +++ b/sample/src/main/java/com/luminiasoft/labs/sample/SampleApplication.java @@ -7,7 +7,7 @@ import android.os.Bundle; import android.os.Handler; import android.util.Log; -import cy.agorise.graphenej.api.ApiAccess; +import cy.agorise.graphenej.api.android.NetworkService; /** * Sample application class @@ -45,8 +45,6 @@ public class SampleApplication extends Application implements Application.Activi public void onCreate() { super.onCreate(); Intent intent = new Intent(this, NetworkService.class); - int requestedApis = ApiAccess.API_DATABASE | ApiAccess.API_HISTORY | ApiAccess.API_NETWORK_BROADCAST; - intent.putExtra(NetworkService.KEY_REQUESTED_APIS, requestedApis); startService(intent); /* diff --git a/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java b/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java index 0168653..56c529c 100644 --- a/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java +++ b/sample/src/main/java/com/luminiasoft/labs/sample/SecondActivity.java @@ -4,12 +4,13 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.os.Bundle; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; import android.util.Log; import cy.agorise.graphenej.api.ApiAccess; +import cy.agorise.graphenej.api.android.NetworkService; public class SecondActivity extends AppCompatActivity {