Skip to content

Commit

Permalink
Broadcasts are now managed by LocalBroadcastManager.
Browse files Browse the repository at this point in the history
Simple requests now don't send broadcasts by default (but can be enabled).
RequestService now allows the user to provide an own implementation of the RequestResponseProcessor.
  • Loading branch information
SpartanJ committed Mar 4, 2020
1 parent f1d647a commit 12b8493
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
5 changes: 3 additions & 2 deletions restafari/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.UnsupportedEncodingException;
import java.util.Map;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class RequestResponseProcessor<T>
{
public static final String TAG = RequestResponseProcessor.class.getCanonicalName();
Expand Down Expand Up @@ -153,7 +155,7 @@ else if ( request.getResponseClass() != null )
} ).start();
}

public <ParamType> Response.ErrorListener getErrorListener( final ResponseListener<T> processor, final ParamType parameters, final long requestId )
public <ParamType> Response.ErrorListener getErrorListener( final ResponseListener<T> processor, final ParamType parameters, final long requestId, final boolean sendBroadcast )
{
return error -> new Thread( () -> {
String errorMsg = error.getMessage();
Expand Down Expand Up @@ -181,12 +183,13 @@ public <ParamType> 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 <ParamType> Response.Listener<T> getResponseListener( final ResponseListener<T> processor, final ParamType parameters, final long requestId )
public <ParamType> Response.Listener<T> getResponseListener( final ResponseListener<T> processor, final ParamType parameters, final long requestId, final boolean sendBroadcast )
{
return response -> new Thread( () -> {
String responseString = response.toString();
Expand Down Expand Up @@ -224,15 +227,17 @@ public <ParamType> Response.Listener<T> 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;
}
}
}
}

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();
}

Expand All @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -219,7 +238,7 @@ public ResponseStatusManager getResponseStatusManager()

/** Simple JSON requests */
@SuppressWarnings( "unchecked" )
public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map<String, String> headers, RetryPolicy retryPolicy )
public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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<String, String> headers, RetryPolicy retryPolicy )
public long makeJsonRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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<String, String> headers, RetryPolicy retryPolicy )
public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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<String, String> headers, RetryPolicy retryPolicy )
public long makeJsonArrayRequest( int method, String url, ResponseListener responseListener, JSONArray parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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<String, String> headers, RetryPolicy retryPolicy )
public long makeJsonMultipartRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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<String, String> headers, RetryPolicy retryPolicy )
public long makeStringRequest( int method, String url, ResponseListener responseListener, JSONObject parameters, Map<String, String> headers, RetryPolicy retryPolicy, boolean sendBroadcast )
{
long requestId = generateRequestID();

Expand All @@ -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 );
Expand All @@ -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<String, String> 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 );
}
}
Loading

0 comments on commit 12b8493

Please sign in to comment.