Weather API: Delegate the responsability of rejecting back to back requests [1/2]

The Weather Manager Service should not impose rules regarding how
a weather provide service wants to react to back to back weather
update requests, nor impose the time a caller should wait before
a new request can be submitted. These constraints vary between
weather services, so it's up the each implementation to enforce
these constraints.

With this patch, the Weather manager service will pass the requests
as they come from the requester and it will be up to the active service
provider decide whether process or reject the request.

Changes to API:
- Moved the request statuses to new inner class RequestStatus in
 CMWeatherManager
- Pass status arg to onLookupCityRequestCompleted()
- Added reject(int) method to ServiceRequest

Change-Id: I3512490688255e25395e955d506fe42ed52f8fe0
TICKET: CYNGNOS-2430
This commit is contained in:
Luis Vidal
2016-04-13 01:08:11 -07:00
parent 377fffca69
commit fe1812fd38
6 changed files with 174 additions and 184 deletions

View File

@@ -53,40 +53,32 @@ public class CMWeatherManager {
private static final String TAG = CMWeatherManager.class.getSimpleName();
/**
* Weather update request state: Successfully completed
*/
public static final int WEATHER_REQUEST_COMPLETED = 1;
/**
* Weather update request state: You need to wait a bit longer before requesting an update
* again.
* <p>Please bear in mind that the weather does not change very often. A threshold of 10 minutes
* is enforced by the system</p>
* The different request statuses
*/
public static final int WEATHER_REQUEST_SUBMITTED_TOO_SOON = -1;
/**
* Weather update request state: An error occurred while trying to update the weather. You
* should wait before trying again, or your request will be rejected with
* {@link #WEATHER_REQUEST_SUBMITTED_TOO_SOON}
*/
public static final int WEATHER_REQUEST_FAILED = -2;
/**
* Weather update request state: Only one update request can be processed at a given time.
*/
public static final int WEATHER_REQUEST_ALREADY_IN_PROGRESS = -3;
/** @hide */
public static final int LOOKUP_REQUEST_COMPLETED = 100;
/** @hide */
public static final int LOOKUP_REQUEST_FAILED = -100;
/** @hide */
public static final int LOOKUP_REQUEST_NO_MATCH_FOUND = -101;
public static class RequestStatus {
/**
* Request Successfully completed
*/
public static final int COMPLETED = 1;
/**
* An error occurred while trying to honor the request.
*/
public static final int FAILED = -1;
/**
* The request can't be processed at this time
*/
public static final int SUBMITTED_TOO_SOON = -2;
/**
* Another request in already in progress
*/
public static final int ALREADY_IN_PROGRESS = -3;
/**
* No match found for the query
*/
public static final int NO_MATCH_FOUND = -4;
}
private CMWeatherManager(Context context) {
Context appContext = context.getApplicationContext();
@@ -330,7 +322,7 @@ public class CMWeatherManager {
private final IRequestInfoListener mRequestInfoListener = new IRequestInfoListener.Stub() {
@Override
public void onWeatherRequestCompleted(final RequestInfo requestInfo, final int state,
public void onWeatherRequestCompleted(final RequestInfo requestInfo, final int status,
final WeatherInfo weatherInfo) {
final WeatherUpdateRequestListener listener
= mWeatherUpdateRequestListeners.remove(requestInfo);
@@ -338,14 +330,14 @@ public class CMWeatherManager {
mHandler.post(new Runnable() {
@Override
public void run() {
listener.onWeatherRequestCompleted(state, weatherInfo);
listener.onWeatherRequestCompleted(status, weatherInfo);
}
});
}
}
@Override
public void onLookupCityRequestCompleted(RequestInfo requestInfo,
public void onLookupCityRequestCompleted(RequestInfo requestInfo, final int status,
final List<WeatherLocation> weatherLocations) {
final LookupCityRequestListener listener
@@ -354,7 +346,7 @@ public class CMWeatherManager {
mHandler.post(new Runnable() {
@Override
public void run() {
listener.onLookupCityRequestCompleted(weatherLocations);
listener.onLookupCityRequestCompleted(status, weatherLocations);
}
});
}
@@ -369,16 +361,12 @@ public class CMWeatherManager {
* This method will be called when the weather service provider has finished processing the
* request
*
* @param state Any of the following values
* {@link #WEATHER_REQUEST_COMPLETED}
* {@link #WEATHER_REQUEST_ALREADY_IN_PROGRESS}
* {@link #WEATHER_REQUEST_SUBMITTED_TOO_SOON}
* {@link #WEATHER_REQUEST_FAILED}
* @param status See {@link RequestStatus}
*
* @param weatherInfo A fully populated {@link WeatherInfo} if state is
* {@link #WEATHER_REQUEST_COMPLETED}, null otherwise
* {@link RequestStatus#COMPLETED}, null otherwise
*/
void onWeatherRequestCompleted(int state, WeatherInfo weatherInfo);
void onWeatherRequestCompleted(int status, WeatherInfo weatherInfo);
}
/**
@@ -387,11 +375,14 @@ public class CMWeatherManager {
public interface LookupCityRequestListener {
/**
* This method will be called when the weather service provider has finished processing the
* request. The argument can be null if the provider couldn't find a match
* request.
*
* @param locations
* @param status See {@link RequestStatus}
*
* @param locations A list of {@link WeatherLocation} if the status is
* {@link RequestStatus#COMPLETED}, null otherwise
*/
void onLookupCityRequestCompleted(List<WeatherLocation> locations);
void onLookupCityRequestCompleted(int status, List<WeatherLocation> locations);
}
/**