diff --git a/README.md b/README.md index b3d7920..78a3377 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ It's designed to be a robust library to handle all the cycle of an REST API call ``` dependencies { - implementation 'com.ensoft-dev.restafari:restafari:0.4.0' + implementation 'com.ensoft-dev.restafari:restafari:0.4.1' } ``` diff --git a/build.gradle b/build.gradle index 004b5c7..c418678 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.android.tools.build:gradle:3.6.0' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' } diff --git a/restafari/build.gradle b/restafari/build.gradle index 4c7308c..17682d7 100644 --- a/restafari/build.gradle +++ b/restafari/build.gradle @@ -2,11 +2,11 @@ apply plugin: 'com.android.library' def versionMajor = 0 def versionMinor = 4 -def versionPatch = 0 +def versionPatch = 1 android { compileSdkVersion 29 - buildToolsVersion '29.0.0' + buildToolsVersion '29.0.3' defaultConfig { minSdkVersion 14 @@ -30,6 +30,7 @@ dependencies { api 'androidx.appcompat:appcompat:1.1.0' api 'com.google.code.gson:gson:2.8.5' api 'com.android.volley:volley:1.1.1' + implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' } ext { diff --git a/restafari/src/main/java/com/ensoft/restafari/network/rest/response/RequestResponseProcessor.java b/restafari/src/main/java/com/ensoft/restafari/network/rest/response/RequestResponseProcessor.java index 58696a8..0e64b41 100644 --- a/restafari/src/main/java/com/ensoft/restafari/network/rest/response/RequestResponseProcessor.java +++ b/restafari/src/main/java/com/ensoft/restafari/network/rest/response/RequestResponseProcessor.java @@ -21,6 +21,8 @@ import java.io.UnsupportedEncodingException; import java.util.Map; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + public class RequestResponseProcessor { public static final String TAG = RequestResponseProcessor.class.getCanonicalName(); @@ -153,7 +155,7 @@ else if ( request.getResponseClass() != null ) } ).start(); } - public Response.ErrorListener getErrorListener( final ResponseListener processor, final ParamType parameters, final long requestId ) + public Response.ErrorListener getErrorListener( final ResponseListener processor, final ParamType parameters, final long requestId, final boolean sendBroadcast ) { return error -> new Thread( () -> { String errorMsg = error.getMessage(); @@ -181,12 +183,13 @@ public Response.ErrorListener getErrorListener( final ResponseListen ThreadRunner.run( processor.getThreadMode(), () -> processor.onRequestError( context, statusCode, message ) ); } - broadcastRequestResponse( REQUEST_RESPONSE_FAIL, parameters, statusCode, errorMsg, requestId, networkResponse ); + if ( sendBroadcast ) + broadcastRequestResponse( REQUEST_RESPONSE_FAIL, parameters, statusCode, errorMsg, requestId, networkResponse ); } ).start(); } @SuppressWarnings( "unchecked" ) - public Response.Listener getResponseListener( final ResponseListener processor, final ParamType parameters, final long requestId ) + public Response.Listener getResponseListener( final ResponseListener processor, final ParamType parameters, final long requestId, final boolean sendBroadcast ) { return response -> new Thread( () -> { String responseString = response.toString(); @@ -224,7 +227,8 @@ public Response.Listener getResponseListener( final ResponseListe { ThreadRunner.run( processor.getThreadMode(), () -> processor.onRequestError( context, HttpStatus.UNKNOWN_ERROR.getCode(), exception.toString() ) ); - broadcastRequestResponse( REQUEST_RESPONSE_FAIL, parameters, HttpStatus.UNKNOWN_ERROR.getCode(), exception.toString(), requestId, networkResponse ); + if ( sendBroadcast ) + broadcastRequestResponse( REQUEST_RESPONSE_FAIL, parameters, HttpStatus.UNKNOWN_ERROR.getCode(), exception.toString(), requestId, networkResponse ); return; } @@ -232,7 +236,8 @@ public Response.Listener getResponseListener( final ResponseListe } } - broadcastRequestResponse( REQUEST_RESPONSE_SUCCESS, parameters, HttpStatus.OK_200.getCode(), responseString, requestId, networkResponse ); + if ( sendBroadcast ) + broadcastRequestResponse( REQUEST_RESPONSE_SUCCESS, parameters, HttpStatus.OK_200.getCode(), responseString, requestId, networkResponse ); } ).start(); } @@ -254,6 +259,6 @@ protected void broadcastRequestResponse( int resultCode, Object requestParams, i RequestService.getInstance().getRequestDelayedBroadcast().queueBroadcast( resultBroadcast ); - context.sendBroadcast( resultBroadcast ); + LocalBroadcastManager.getInstance(context).sendBroadcast( resultBroadcast ); } } diff --git a/restafari/src/main/java/com/ensoft/restafari/network/rest/response/ResponseReceiver.java b/restafari/src/main/java/com/ensoft/restafari/network/rest/response/ResponseReceiver.java index ea4ef34..39dbdb3 100644 --- a/restafari/src/main/java/com/ensoft/restafari/network/rest/response/ResponseReceiver.java +++ b/restafari/src/main/java/com/ensoft/restafari/network/rest/response/ResponseReceiver.java @@ -6,6 +6,8 @@ import android.content.IntentFilter; import android.util.Log; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + public class ResponseReceiver extends BroadcastReceiver { public static final String TAG = ResponseReceiver.class.getCanonicalName(); @@ -36,11 +38,11 @@ public void setReceiver( Receiver receiver ) if ( receiver == null ) { if ( this.receiver != null ) - context.unregisterReceiver( this ); + LocalBroadcastManager.getInstance(context).unregisterReceiver( this ); } else { - context.registerReceiver( this, filter ); + LocalBroadcastManager.getInstance(context).registerReceiver( this, filter ); } } catch ( Exception ignored ) diff --git a/restafari/src/main/java/com/ensoft/restafari/network/service/RequestService.java b/restafari/src/main/java/com/ensoft/restafari/network/service/RequestService.java index 95cb190..2179aa6 100644 --- a/restafari/src/main/java/com/ensoft/restafari/network/service/RequestService.java +++ b/restafari/src/main/java/com/ensoft/restafari/network/service/RequestService.java @@ -56,18 +56,28 @@ public static synchronized RequestService init( Context context, RequestServiceO { if ( instance == null ) { - instance = new RequestService( context, requestServiceOptions ); + instance = new RequestService( context, requestServiceOptions, null ); } return instance; } - + + public static synchronized RequestService init( Context context, RequestServiceOptions requestServiceOptions, RequestResponseProcessor requestResponseProcessor ) + { + if ( instance == null ) + { + instance = new RequestService( context, requestServiceOptions, requestResponseProcessor ); + } + + return instance; + } + public static synchronized RequestService getInstance() { return instance; } - private RequestService( Context context, RequestServiceOptions requestServiceOptions ) + private RequestService( Context context, RequestServiceOptions requestServiceOptions, RequestResponseProcessor requestResponseProcessor ) { this.context = context; @@ -82,7 +92,16 @@ private RequestService( Context context, RequestServiceOptions requestServiceOpt CookieHandler.setDefault( cookieManager ); requestDelayedBroadcast = new RequestDelayedBroadcast(); - requestResponseProcessor = new RequestResponseProcessor( context ); + + if ( requestResponseProcessor != null ) + { + this.requestResponseProcessor = requestResponseProcessor; + } + else + { + this.requestResponseProcessor = new RequestResponseProcessor( context ); + } + responseStatusManager = new ResponseStatusManager(); } @@ -219,7 +238,7 @@ public ResponseStatusManager getResponseStatusManager() /** Simple JSON requests */ @SuppressWarnings( "unchecked" ) - public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy ) + public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -231,8 +250,8 @@ public long makeJsonRequest( int method, String url, ResponseListener responseLi url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -242,21 +261,21 @@ public long makeJsonRequest( int method, String url, ResponseListener responseLi public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers ) { - return makeJsonRequest( method, url, responseListener, parameters, headers, null ); + return makeJsonRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters ) { - return makeJsonRequest( method, url, responseListener, parameters, null, null ); + return makeJsonRequest( method, url, responseListener, parameters, null, null, false ); } public long makeJsonRequest( int method, String url, ResponseListener responseListener ) { - return makeJsonRequest( method, url, responseListener, new JSONObject(), null, null ); + return makeJsonRequest( method, url, responseListener, new JSONObject(), null, null, false ); } @SuppressWarnings( "unchecked" ) - public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers, RetryPolicy retryPolicy ) + public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -268,8 +287,8 @@ public long makeJsonRequest( int method, String url, ResponseListener responseLi url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -279,17 +298,17 @@ public long makeJsonRequest( int method, String url, ResponseListener responseLi public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers ) { - return makeJsonRequest( method, url, responseListener, parameters, headers, null ); + return makeJsonRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONArray parameters ) { - return makeJsonRequest( method, url, responseListener, parameters, null, null ); + return makeJsonRequest( method, url, responseListener, parameters, null, null, false ); } /** Simple JSON Array requests */ @SuppressWarnings( "unchecked" ) - public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy ) + public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -301,8 +320,8 @@ public long makeJsonArrayRequest( int method, String url, ResponseListener respo url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -312,21 +331,21 @@ public long makeJsonArrayRequest( int method, String url, ResponseListener respo public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers ) { - return makeJsonArrayRequest( method, url, responseListener, parameters, headers, null ); + return makeJsonArrayRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONObject parameters ) { - return makeJsonArrayRequest( method, url, responseListener, parameters, null, null ); + return makeJsonArrayRequest( method, url, responseListener, parameters, null, null, false ); } public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener ) { - return makeJsonArrayRequest( method, url, responseListener, new JSONObject(), null, null ); + return makeJsonArrayRequest( method, url, responseListener, new JSONObject(), null, null, false ); } @SuppressWarnings( "unchecked" ) - public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers, RetryPolicy retryPolicy ) + public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -338,8 +357,8 @@ public long makeJsonArrayRequest( int method, String url, ResponseListener respo url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -349,17 +368,17 @@ public long makeJsonArrayRequest( int method, String url, ResponseListener respo public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map headers ) { - return makeJsonArrayRequest( method, url, responseListener, parameters, headers, null ); + return makeJsonArrayRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONArray parameters ) { - return makeJsonArrayRequest( method, url, responseListener, parameters, null, null ); + return makeJsonArrayRequest( method, url, responseListener, parameters, null, null, false ); } /** Simple JSON Multipart requests */ @SuppressWarnings( "unchecked" ) - public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy ) + public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -371,8 +390,8 @@ public long makeJsonMultipartRequest( int method, String url, ResponseListener r url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -382,22 +401,22 @@ public long makeJsonMultipartRequest( int method, String url, ResponseListener r public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers ) { - return makeJsonMultipartRequest( method, url, responseListener, parameters, headers, null ); + return makeJsonMultipartRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener, JSONObject parameters ) { - return makeJsonMultipartRequest( method, url, responseListener, parameters, null, null ); + return makeJsonMultipartRequest( method, url, responseListener, parameters, null, null, false ); } public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener ) { - return makeJsonArrayRequest( method, url, responseListener, new JSONObject(), null, null ); + return makeJsonArrayRequest( method, url, responseListener, new JSONObject(), null, null, false ); } /** Simple String requests */ @SuppressWarnings( "unchecked" ) - public long makeStringRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy ) + public long makeStringRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers, RetryPolicy retryPolicy, boolean sendBroadcast ) { long requestId = generateRequestID(); @@ -409,8 +428,8 @@ public long makeStringRequest( int method, String url, ResponseListener response url, parameters, headers, - requestResponseProcessor.getResponseListener( responseListener, parameters, requestId ), - requestResponseProcessor.getErrorListener( responseListener, parameters, requestId ) + requestResponseProcessor.getResponseListener( responseListener, parameters, requestId, sendBroadcast ), + requestResponseProcessor.getErrorListener( responseListener, parameters, requestId, sendBroadcast ) ){}; requestResponseProcessor.queueRequest( request, retryPolicy, requestId ); @@ -420,16 +439,16 @@ public long makeStringRequest( int method, String url, ResponseListener response public long makeStringRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map headers ) { - return makeStringRequest( method, url, responseListener, parameters, headers, null ); + return makeStringRequest( method, url, responseListener, parameters, headers, null, false ); } public long makeStringRequest( int method, String url, ResponseListener responseListener, JSONObject parameters ) { - return makeStringRequest( method, url, responseListener, parameters, null, null ); + return makeStringRequest( method, url, responseListener, parameters, null, null, false ); } public long makeStringRequest( int method, String url, ResponseListener responseListener ) { - return makeStringRequest( method, url, responseListener, new JSONObject(), null, null ); + return makeStringRequest( method, url, responseListener, new JSONObject(), null, null, false ); } } diff --git a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseActivity.java b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseActivity.java index 5019e38..1b81021 100644 --- a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseActivity.java +++ b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseActivity.java @@ -13,7 +13,12 @@ public class RequestResponseActivity extends Activity implements ResponseReceive protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - responseReceiverService = new ResponseReceiverService( this, this ); + createResponseReceiverService(); + } + + protected void createResponseReceiverService() + { + responseReceiverService = new ResponseReceiverService( getApplicationContext(), this ); } @Override diff --git a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseAppCompatActivity.java b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseAppCompatActivity.java index 18f8f4f..9978915 100644 --- a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseAppCompatActivity.java +++ b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseAppCompatActivity.java @@ -14,9 +14,14 @@ public class RequestResponseAppCompatActivity extends AppCompatActivity implemen protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - responseReceiverService = new ResponseReceiverService( this, this ); + createResponseReceiverService(); } - + + protected void createResponseReceiverService() + { + responseReceiverService = new ResponseReceiverService( getApplicationContext(), this ); + } + @Override protected void onPause() { diff --git a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseFragment.java b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseFragment.java index 64fd5d2..d4783df 100644 --- a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseFragment.java +++ b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseFragment.java @@ -13,9 +13,14 @@ public class RequestResponseFragment extends Fragment implements ResponseReceive public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - responseReceiverService = new ResponseReceiverService( getActivity().getBaseContext(), this ); + createResponseReceiverService(); } - + + protected void createResponseReceiverService() + { + responseReceiverService = new ResponseReceiverService( getActivity().getApplicationContext(), this ); + } + @Override public void onPause() { diff --git a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseSupportFragment.java b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseSupportFragment.java index ab536dc..8e3dcd5 100644 --- a/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseSupportFragment.java +++ b/restafari/src/main/java/com/ensoft/restafari/ui/view/RequestResponseSupportFragment.java @@ -9,14 +9,19 @@ public class RequestResponseSupportFragment extends Fragment implements ResponseReceiverService.RequestResponse { protected ResponseReceiverService responseReceiverService; - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); - responseReceiverService = new ResponseReceiverService( getActivity().getBaseContext(), this ); + createResponseReceiverService(); } - + + protected void createResponseReceiverService() + { + responseReceiverService = new ResponseReceiverService( getActivity().getApplicationContext(), this ); + } + @Override public void onPause() {