Weather API: Return ID rather than RequestInfo [1/2]
Instead of exposing the RequestInfo object created by the WeatherMgr return an ID to identify the request. This ID can be later used to cancel the request if needed. The WeatherProviderService base class keeps track of the ongoing requests and can map this ID to the corresponding request This patch also include the following minor changes: - Use List instead of ArrayList in API - Update javadoc to public methods to reflect API changes - Use UUID random generator in immutable classes to generate the hashcode rather than relying solely in the hashcode of the builder object. Change-Id: Ib88dd0ecddd6fdb016b77ac29709fbae092dea29 TICKET: CYNGNOS-2425 TICKET: CYNGNOS-2423
This commit is contained in:
@@ -19,6 +19,7 @@ package cyanogenmod.weather;
|
||||
import android.annotation.NonNull;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
import cyanogenmod.os.Build;
|
||||
import cyanogenmod.os.Concierge;
|
||||
import cyanogenmod.os.Concierge.ParcelInfo;
|
||||
@@ -27,6 +28,8 @@ import cyanogenmod.weatherservice.ServiceRequest;
|
||||
import cyanogenmod.weatherservice.ServiceRequestResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* This class represents the weather information that a
|
||||
@@ -51,8 +54,8 @@ public final class WeatherInfo implements Parcelable {
|
||||
private double mWindDirection;
|
||||
private int mWindSpeedUnit;
|
||||
private long mTimestamp;
|
||||
private ArrayList<DayForecast> mForecastList;
|
||||
private int mKey;
|
||||
private List<DayForecast> mForecastList;
|
||||
private String mKey;
|
||||
|
||||
private WeatherInfo() {}
|
||||
|
||||
@@ -71,7 +74,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
private double mWindDirection = Double.NaN;
|
||||
private int mWindSpeedUnit = WeatherContract.WeatherColumns.WindSpeedUnit.MPH;
|
||||
private long mTimestamp = -1;
|
||||
private ArrayList<DayForecast> mForecastList = new ArrayList<>(0);
|
||||
private List<DayForecast> mForecastList = new ArrayList<>(0);
|
||||
|
||||
/**
|
||||
* @param cityName A valid city name. Attempting to pass null will get you an
|
||||
@@ -169,7 +172,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
* null will get you an IllegalArgumentException'
|
||||
* @return The {@link Builder} instance
|
||||
*/
|
||||
public Builder setForecast(@NonNull ArrayList<DayForecast> forecasts) {
|
||||
public Builder setForecast(@NonNull List<DayForecast> forecasts) {
|
||||
if (forecasts == null) {
|
||||
throw new IllegalArgumentException("Forecast list can't be null");
|
||||
}
|
||||
@@ -220,7 +223,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
info.mWindSpeedUnit = this.mWindSpeedUnit;
|
||||
info.mTimestamp = this.mTimestamp == -1 ? System.currentTimeMillis() : this.mTimestamp;
|
||||
info.mForecastList = this.mForecastList;
|
||||
info.mKey = this.hashCode();
|
||||
info.mKey = UUID.randomUUID().toString();
|
||||
return info;
|
||||
}
|
||||
|
||||
@@ -338,7 +341,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
* the forecast weather for the upcoming days. If you want to know today's high and low
|
||||
* temperatures, use {@link WeatherInfo#getTodaysHigh()} and {@link WeatherInfo#getTodaysLow()}
|
||||
*/
|
||||
public ArrayList<DayForecast> getForecasts() {
|
||||
public List<DayForecast> getForecasts() {
|
||||
return new ArrayList<>(mForecastList);
|
||||
}
|
||||
|
||||
@@ -348,7 +351,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
int parcelableVersion = parcelInfo.getParcelVersion();
|
||||
|
||||
if (parcelableVersion >= Build.CM_VERSION_CODES.ELDERBERRY) {
|
||||
mKey = parcel.readInt();
|
||||
mKey = parcel.readString();
|
||||
mCity = parcel.readString();
|
||||
mConditionCode = parcel.readInt();
|
||||
mTemperature = parcel.readDouble();
|
||||
@@ -383,7 +386,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
ParcelInfo parcelInfo = Concierge.prepareParcel(dest);
|
||||
|
||||
// ==== ELDERBERRY =====
|
||||
dest.writeInt(mKey);
|
||||
dest.writeString(mKey);
|
||||
dest.writeString(mCity);
|
||||
dest.writeInt(mConditionCode);
|
||||
dest.writeDouble(mTemperature);
|
||||
@@ -428,7 +431,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
double mLow;
|
||||
double mHigh;
|
||||
int mConditionCode;
|
||||
int mKey;
|
||||
String mKey;
|
||||
|
||||
private DayForecast() {}
|
||||
|
||||
@@ -490,7 +493,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
forecast.mLow = this.mLow;
|
||||
forecast.mHigh = this.mHigh;
|
||||
forecast.mConditionCode = this.mConditionCode;
|
||||
forecast.mKey = this.hashCode();
|
||||
forecast.mKey = UUID.randomUUID().toString();
|
||||
return forecast;
|
||||
}
|
||||
}
|
||||
@@ -527,7 +530,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
ParcelInfo parcelInfo = Concierge.prepareParcel(dest);
|
||||
|
||||
// ==== ELDERBERRY =====
|
||||
dest.writeInt(mKey);
|
||||
dest.writeString(mKey);
|
||||
dest.writeDouble(mLow);
|
||||
dest.writeDouble(mHigh);
|
||||
dest.writeInt(mConditionCode);
|
||||
@@ -555,7 +558,7 @@ public final class WeatherInfo implements Parcelable {
|
||||
int parcelableVersion = parcelInfo.getParcelVersion();
|
||||
|
||||
if (parcelableVersion >= Build.CM_VERSION_CODES.ELDERBERRY) {
|
||||
mKey = parcel.readInt();
|
||||
mKey = parcel.readString();
|
||||
mLow = parcel.readDouble();
|
||||
mHigh = parcel.readDouble();
|
||||
mConditionCode = parcel.readInt();
|
||||
@@ -576,18 +579,19 @@ public final class WeatherInfo implements Parcelable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
//The hashcode of this object was stored when it was built. This is an
|
||||
//immutable object but we need to preserve the hashcode since this object is parcelable
|
||||
//and it's reconstructed over IPC, and clients of this object might want to store it in
|
||||
//a collection that relies on this code to identify the object
|
||||
return mKey;
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((mKey != null) ? mKey.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof DayForecast) {
|
||||
if (obj == null) return false;
|
||||
|
||||
if (getClass() == obj.getClass()) {
|
||||
DayForecast forecast = (DayForecast) obj;
|
||||
return (forecast.hashCode() == this.mKey);
|
||||
return (TextUtils.equals(mKey, forecast.mKey));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -615,18 +619,19 @@ public final class WeatherInfo implements Parcelable {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
//The hashcode of this object was stored when it was built. This is an
|
||||
//immutable object but we need to preserve the hashcode since this object is parcelable and
|
||||
//it's reconstructed over IPC, and clients of this object might want to store it in a
|
||||
//collection that relies on this code to identify the object
|
||||
return mKey;
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((mKey != null) ? mKey.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof WeatherInfo) {
|
||||
if (obj == null) return false;
|
||||
|
||||
if (getClass() == obj.getClass()) {
|
||||
WeatherInfo info = (WeatherInfo) obj;
|
||||
return (info.hashCode() == this.mKey);
|
||||
return (TextUtils.equals(mKey, info.mKey));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user