blob: 21a6b07ba6e50170ef11c2c9eec27d04927b39e3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* Copyright (C) 2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lineageos.weather;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import lineageos.os.Build;
import lineageos.os.Concierge;
import lineageos.os.Concierge.ParcelInfo;
import java.util.UUID;
/**
* A class representing a geographical location that a weather service provider can use to
* get weather data from. Each service provider will potentially populate objects of this class
* with different content, so make sure you don't preserve the values when a service provider
* is changed
*/
public final class WeatherLocation implements Parcelable{
private String mCityId;
private String mCity;
private String mState;
private String mPostal;
private String mCountryId;
private String mCountry;
private String mKey;
private WeatherLocation() {}
/**
* Builder class for {@link WeatherLocation}
*/
public static class Builder {
String mCityId = "";
String mCity = "";
String mState = "";
String mPostal = "";
String mCountryId = "";
String mCountry = "";
/**
* @param cityId An identifier for the city (for example WOEID - Where On Earth IDentifier)
* @param cityName The name of the city
*/
public Builder(String cityId, String cityName) {
if (cityId == null || cityName == null) {
throw new IllegalArgumentException("Illegal to set city id AND city to null");
}
this.mCityId = cityId;
this.mCity = cityName;
}
/**
* @param cityName The name of the city
*/
public Builder(String cityName) {
if (cityName == null) {
throw new IllegalArgumentException("City name can't be null");
}
this.mCity = cityName;
}
/**
* @param countryId An identifier for the country (for example ISO alpha-2, ISO alpha-3,
* ISO 3166-1 numeric-3, etc)
* @return The {@link Builder} instance
*/
public Builder setCountryId(String countryId) {
if (countryId == null) {
throw new IllegalArgumentException("Country ID can't be null");
}
this.mCountryId = countryId;
return this;
}
/**
* @param country The country name
* @return The {@link Builder} instance
*/
public Builder setCountry(String country) {
if (country == null) {
throw new IllegalArgumentException("Country can't be null");
}
this.mCountry = country;
return this;
}
/**
* @param postalCode The postal/ZIP code
* @return The {@link Builder} instance
*/
public Builder setPostalCode(String postalCode) {
if (postalCode == null) {
throw new IllegalArgumentException("Postal code/ZIP can't be null");
}
this.mPostal = postalCode;
return this;
}
/**
* @param state The state or territory where the city is located
* @return The {@link Builder} instance
*/
public Builder setState(String state) {
if (state == null) {
throw new IllegalArgumentException("State can't be null");
}
this.mState = state;
return this;
}
/**
* Combine all of the options that have been set and return a new {@link WeatherLocation}
* object
* @return {@link WeatherLocation}
*/
public WeatherLocation build() {
WeatherLocation weatherLocation = new WeatherLocation();
weatherLocation.mCityId = this.mCityId;
weatherLocation.mCity = this.mCity;
weatherLocation.mState = this.mState;
weatherLocation.mPostal = this.mPostal;
weatherLocation.mCountryId = this.mCountryId;
weatherLocation.mCountry = this.mCountry;
weatherLocation.mKey = UUID.randomUUID().toString();
return weatherLocation;
}
}
/**
* @return The city ID. This method will return an empty string if the city ID was not set
*/
public String getCityId() {
return mCityId;
}
/**
* @return The city name. This method will return an empty string if the city name was not set
*/
public String getCity() {
return mCity;
}
/**
* @return The state name. This method will return an empty string if the state was not set
*/
public String getState() {
return mState;
}
/**
* @return The postal/ZIP code. This method will return an empty string if the postal/ZIP code
* was not set
*/
public String getPostalCode() {
return mPostal;
}
/**
* @return The country ID. This method will return an empty string if the country ID was not set
*/
public String getCountryId() {
return mCountryId;
}
/**
* @return The country name. This method will return an empty string if the country ID was not
* set
*/
public String getCountry() {
return mCountry;
}
private WeatherLocation(Parcel in) {
// Read parcelable version via the Concierge
ParcelInfo parcelInfo = Concierge.receiveParcel(in);
int parcelableVersion = parcelInfo.getParcelVersion();
if (parcelableVersion >= Build.LINEAGE_VERSION_CODES.ELDERBERRY) {
mKey = in.readString();
mCityId = in.readString();
mCity = in.readString();
mState = in.readString();
mPostal = in.readString();
mCountryId = in.readString();
mCountry = in.readString();
}
// Complete parcel info for the concierge
parcelInfo.complete();
}
public static final Creator<WeatherLocation> CREATOR = new Creator<WeatherLocation>() {
@Override
public WeatherLocation createFromParcel(Parcel in) {
return new WeatherLocation(in);
}
@Override
public WeatherLocation[] newArray(int size) {
return new WeatherLocation[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// Tell the concierge to prepare the parcel
ParcelInfo parcelInfo = Concierge.prepareParcel(dest);
// ==== ELDERBERRY =====
dest.writeString(mKey);
dest.writeString(mCityId);
dest.writeString(mCity);
dest.writeString(mState);
dest.writeString(mPostal);
dest.writeString(mCountryId);
dest.writeString(mCountry);
// Complete parcel info for the concierge
parcelInfo.complete();
}
@Override
public String toString() {
return new StringBuilder()
.append("{ City ID: ").append(mCityId)
.append(" City: ").append(mCity)
.append(" State: ").append(mState)
.append(" Postal/ZIP Code: ").append(mPostal)
.append(" Country Id: ").append(mCountryId)
.append(" Country: ").append(mCountry).append("}")
.toString();
}
@Override
public int hashCode() {
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 == null) return false;
if (getClass() == obj.getClass()) {
WeatherLocation location = (WeatherLocation) obj;
return (TextUtils.equals(mKey, location.mKey));
}
return false;
}
}
|