diff options
Diffstat (limited to 'sdk')
3 files changed, 120 insertions, 90 deletions
diff --git a/sdk/src/java/cyanogenmod/weather/CMWeatherManager.java b/sdk/src/java/cyanogenmod/weather/CMWeatherManager.java index 545c3ce..8292b58 100644 --- a/sdk/src/java/cyanogenmod/weather/CMWeatherManager.java +++ b/sdk/src/java/cyanogenmod/weather/CMWeatherManager.java @@ -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> - */ - 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. + * The different request statuses */ - 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 status See {@link RequestStatus} * - * @param locations + * @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); } /** diff --git a/sdk/src/java/cyanogenmod/weather/IRequestInfoListener.aidl b/sdk/src/java/cyanogenmod/weather/IRequestInfoListener.aidl index 553da71..d35db44 100644 --- a/sdk/src/java/cyanogenmod/weather/IRequestInfoListener.aidl +++ b/sdk/src/java/cyanogenmod/weather/IRequestInfoListener.aidl @@ -24,8 +24,8 @@ import java.util.List; /** @hide */ oneway interface IRequestInfoListener { - void onWeatherRequestCompleted(in RequestInfo requestInfo, int state, + void onWeatherRequestCompleted(in RequestInfo requestInfo, int status, in WeatherInfo weatherInfo); - void onLookupCityRequestCompleted(in RequestInfo requestInfo, + void onLookupCityRequestCompleted(in RequestInfo requestInfo, int status, in List<WeatherLocation> weatherLocation); }
\ No newline at end of file diff --git a/sdk/src/java/cyanogenmod/weatherservice/ServiceRequest.java b/sdk/src/java/cyanogenmod/weatherservice/ServiceRequest.java index bc2f38d..f503a2f 100644 --- a/sdk/src/java/cyanogenmod/weatherservice/ServiceRequest.java +++ b/sdk/src/java/cyanogenmod/weatherservice/ServiceRequest.java @@ -29,15 +29,15 @@ public final class ServiceRequest { private final RequestInfo mInfo; private final IWeatherProviderServiceClient mClient; - /** - * If a request is marked as cancelled, it means the client does not want to know anything about - * this request anymore - */ - private volatile boolean mCancelled; + private enum Status { + IN_PROGRESS, COMPLETED, CANCELLED, FAILED, REJECTED + } + private Status mStatus; /* package */ ServiceRequest(RequestInfo info, IWeatherProviderServiceClient client) { mInfo = info; mClient = client; + mStatus = Status.IN_PROGRESS; } /** @@ -52,32 +52,36 @@ public final class ServiceRequest { * This method should be called once the request has been completed */ public void complete(@NonNull ServiceRequestResult result) { - if (!mCancelled) { - try { - final int requestType = mInfo.getRequestType(); - switch (requestType) { - case RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ: - case RequestInfo.TYPE_WEATHER_BY_WEATHER_LOCATION_REQ: - if (result.getWeatherInfo() == null) { - throw new IllegalStateException("The service request result does not" - + " contain a valid WeatherInfo object"); - } - mClient.setServiceRequestState(mInfo, result, - CMWeatherManager.WEATHER_REQUEST_COMPLETED); - break; - case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: - if (result.getLocationLookupList() == null) { - //In case the user decided to mark this request as completed with an - //empty list. It's not necessarily a failure - mClient.setServiceRequestState(mInfo, null, - CMWeatherManager.LOOKUP_REQUEST_NO_MATCH_FOUND); - } else { + synchronized (this) { + if (mStatus.equals(Status.IN_PROGRESS)) { + try { + final int requestType = mInfo.getRequestType(); + switch (requestType) { + case RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ: + case RequestInfo.TYPE_WEATHER_BY_WEATHER_LOCATION_REQ: + if (result.getWeatherInfo() == null) { + throw new IllegalStateException("The service request result doesn't" + + " contain a valid WeatherInfo object"); + } mClient.setServiceRequestState(mInfo, result, - CMWeatherManager.LOOKUP_REQUEST_COMPLETED); - } - break; + CMWeatherManager.RequestStatus.COMPLETED); + break; + case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: + if (result.getLocationLookupList() == null + || result.getLocationLookupList().size() <= 0) { + //In case the user decided to mark this request as completed with + //null or empty list. It's not necessarily a failure + mClient.setServiceRequestState(mInfo, null, + CMWeatherManager.RequestStatus.NO_MATCH_FOUND); + } else { + mClient.setServiceRequestState(mInfo, result, + CMWeatherManager.RequestStatus.COMPLETED); + } + break; + } + } catch (RemoteException e) { } - } catch (RemoteException e) { + mStatus = Status.COMPLETED; } } } @@ -87,21 +91,54 @@ public final class ServiceRequest { * (no internet connection, time out, etc.) */ public void fail() { - if (!mCancelled) { - try { - final int requestType = mInfo.getRequestType(); - switch (requestType) { - case RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ: - case RequestInfo.TYPE_WEATHER_BY_WEATHER_LOCATION_REQ: - mClient.setServiceRequestState(mInfo, null, - CMWeatherManager.WEATHER_REQUEST_FAILED); - break; - case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: - mClient.setServiceRequestState(mInfo, null, - CMWeatherManager.LOOKUP_REQUEST_FAILED); + synchronized (this) { + if (mStatus.equals(Status.IN_PROGRESS)) { + try { + final int requestType = mInfo.getRequestType(); + switch (requestType) { + case RequestInfo.TYPE_WEATHER_BY_GEO_LOCATION_REQ: + case RequestInfo.TYPE_WEATHER_BY_WEATHER_LOCATION_REQ: + mClient.setServiceRequestState(mInfo, null, + CMWeatherManager.RequestStatus.FAILED); + break; + case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: + mClient.setServiceRequestState(mInfo, null, + CMWeatherManager.RequestStatus.FAILED); + break; + } + } catch (RemoteException e) { + } + mStatus = Status.FAILED; + } + } + } + + /** + * This method should be called if the service decides not to honor the request. Note this + * method will accept only the following values. + * <ul> + * <li>{@link cyanogenmod.weather.CMWeatherManager.RequestStatus#SUBMITTED_TOO_SOON}</li> + * <li>{@link cyanogenmod.weather.CMWeatherManager.RequestStatus#ALREADY_IN_PROGRESS}</li> + * </ul> + * Attempting to pass any other value will get you an IllegalArgumentException + * @param status + */ + public void reject(int status) { + synchronized (this) { + if (mStatus.equals(Status.IN_PROGRESS)) { + switch (status) { + case CMWeatherManager.RequestStatus.ALREADY_IN_PROGRESS: + case CMWeatherManager.RequestStatus.SUBMITTED_TOO_SOON: + try { + mClient.setServiceRequestState(mInfo, null, status); + } catch (RemoteException e) { + e.printStackTrace(); + } break; + default: + throw new IllegalArgumentException("Can't reject with status " + status); } - } catch (RemoteException e) { + mStatus = Status.REJECTED; } } } @@ -113,6 +150,8 @@ public final class ServiceRequest { * @hide */ public void cancel() { - mCancelled = true; + synchronized (this) { + mStatus = Status.CANCELLED; + } } } |