aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/weather/RequestInfo.java
blob: 353b5fd6c1ed4522442ca9d4c313d2a2dbd341c9 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * 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 cyanogenmod.weather;

import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;

import cyanogenmod.os.Build;
import cyanogenmod.os.Concierge;
import cyanogenmod.os.Concierge.ParcelInfo;
import cyanogenmod.providers.WeatherContract;

/**
 * This class holds the information of a request submitted to the active weather provider service
 */
public final class RequestInfo implements Parcelable {

    private Location mLocation;
    private String mCityName;
    private WeatherLocation mWeatherLocation;
    private int mRequestType;
    private IRequestInfoListener mListener;
    private int mTempUnit;
    private int mKey;
    private boolean mIsQueryOnly;

    /**
     * A request to update the weather data using a geographical {@link android.location.Location}
     */
    public static final int TYPE_GEO_LOCATION_REQ = 1;
    /**
     * A request to update the weather data using a {@link WeatherLocation}
     */
    public static final int TYPE_WEATHER_LOCATION_REQ = 2;

    /**
     * A request to look up a city name
     */
    public static final int TYPE_LOOKUP_CITY_NAME_REQ = 3;

    private RequestInfo() {}

    /* package */ static class Builder {
        private Location mLocation;
        private String mCityName;
        private WeatherLocation mWeatherLocation;
        private int mRequestType;
        private IRequestInfoListener mListener;
        private int mTempUnit = WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT;
        private boolean mIsQueryOnly = false;

        public Builder(IRequestInfoListener listener) {
            this.mListener = listener;
        }

        /**
         * Sets the city name and identifies this request as a {@link #TYPE_LOOKUP_CITY_NAME_REQ}
         * request. If set, will null out the location and weather location.
         */
        public Builder setCityName(String cityName) {
            this.mCityName = cityName;
            this.mRequestType = TYPE_LOOKUP_CITY_NAME_REQ;
            this.mLocation = null;
            this.mWeatherLocation = null;
            return this;
        }

        /**
         * Sets the Location and identifies this request as a {@link #TYPE_GEO_LOCATION_REQ}. If
         * set, will null out the city name and weather location.
         */
        public Builder setLocation(Location location) {
            this.mLocation = location;
            this.mCityName = null;
            this.mWeatherLocation = null;
            this.mRequestType = TYPE_GEO_LOCATION_REQ;
            return this;
        }

        /**
         * Sets the weather location and identifies this request as a
         * {@link #TYPE_WEATHER_LOCATION_REQ}. If set, will null out the location and city name
         */
        public Builder setWeatherLocation(WeatherLocation weatherLocation) {
            this.mWeatherLocation = weatherLocation;
            this.mLocation = null;
            this.mCityName = null;
            this.mRequestType = TYPE_WEATHER_LOCATION_REQ;
            return this;
        }

        /**
         * Sets the unit in which the temperature will be reported if the request is honored.
         * Valid values are:
         * <ul>
         * {@link cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit#CELSIUS}
         * {@link cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit#FAHRENHEIT}
         * </ul>
         * Any other value will generate an IllegalArgumentException. If the temperature unit is not
         * set, the default will be degrees Fahrenheit
         * @param unit A valid temperature unit
         */
        public Builder setTemperatureUnit(int unit) {
            if (!isValidTempUnit(unit)) {
                throw new IllegalArgumentException("Invalid temperature unit");
            }
            this.mTempUnit = unit;
            return this;
        }

        /**
         * If this is a weather request, marks the request as a query only, meaning that the
         * content provider won't be updated after the active weather service has finished
         * processing the request.
         */
        public Builder queryOnly() {
            switch (mRequestType) {
                case TYPE_GEO_LOCATION_REQ:
                case TYPE_WEATHER_LOCATION_REQ:
                    this.mIsQueryOnly = true;
                    break;
                default:
                    this.mIsQueryOnly = false;
                    break;
            }
            return this;
        }

        public RequestInfo build() {
            RequestInfo info = new RequestInfo();
            info.mListener = this.mListener;
            info.mRequestType = this.mRequestType;
            info.mCityName = this.mCityName;
            info.mWeatherLocation = this.mWeatherLocation;
            info.mLocation = this.mLocation;
            info.mTempUnit = this.mTempUnit;
            info.mIsQueryOnly = this.mIsQueryOnly;
            info.mKey = this.hashCode();
            return info;
        }

        private boolean isValidTempUnit(int unit) {
            switch (unit) {
                case WeatherContract.WeatherColumns.TempUnit.CELSIUS:
                case WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT:
                    return true;
                default:
                    return false;
            }
        }

    }

    private RequestInfo(Parcel parcel) {
        // Read parcelable version via the Concierge
        ParcelInfo parcelInfo = Concierge.receiveParcel(parcel);
        int parcelableVersion = parcelInfo.getParcelVersion();

        if (parcelableVersion >= Build.CM_VERSION_CODES.ELDERBERRY) {
            mKey = parcel.readInt();
            mRequestType = parcel.readInt();
            switch (mRequestType) {
                case TYPE_GEO_LOCATION_REQ:
                    mLocation = Location.CREATOR.createFromParcel(parcel);
                    mTempUnit = parcel.readInt();
                    break;
                case TYPE_WEATHER_LOCATION_REQ:
                    mWeatherLocation = WeatherLocation.CREATOR.createFromParcel(parcel);
                    mTempUnit = parcel.readInt();
                    break;
                case TYPE_LOOKUP_CITY_NAME_REQ:
                    mCityName = parcel.readString();
                    break;
            }
            mIsQueryOnly = (parcel.readInt() == 1);
            mListener = IRequestInfoListener.Stub.asInterface(parcel.readStrongBinder());
        }

        // Complete parcel info for the concierge
        parcelInfo.complete();
    }


    /**
     * @return The request type
     */
    public int getRequestType() {
        return mRequestType;
    }

    /**
     * @return the {@link android.location.Location} if this is a request by location, null
     * otherwise
     */
    public Location getLocation() {
        return mLocation;
    }

    /**
     * @return the {@link cyanogenmod.weather.WeatherLocation} if this is a request by weather
     * location, null otherwise
     */
    public WeatherLocation getWeatherLocation() {
        return mWeatherLocation;
    }

    /**
     * @hide
     */
    public IRequestInfoListener getRequestListener() {
        return mListener;
    }

    /**
     * @return the city name if this is a lookup request, null otherwise
     */
    public String getCityName() {
        return mCityName;
    }

    /**
     * @return the temperature unit if this is a weather request, -1 otherwise
     */
    public int getTemperatureUnit() {
        switch (mRequestType) {
            case TYPE_GEO_LOCATION_REQ:
            case TYPE_WEATHER_LOCATION_REQ:
                return mTempUnit;
            default:
                return -1;
        }
    }

    /**
     * @return if this is a weather request, whether the request will update the content provider.
     * False for other kind of requests
     * @hide
     */
    public boolean isQueryOnlyWeatherRequest() {
        switch (mRequestType) {
            case TYPE_GEO_LOCATION_REQ:
            case TYPE_WEATHER_LOCATION_REQ:
                return mIsQueryOnly;
            default:
                return false;
        }
    }

    public static final Creator<RequestInfo> CREATOR = new Creator<RequestInfo>() {
        @Override
        public RequestInfo createFromParcel(Parcel in) {
            return new RequestInfo(in);
        }

        @Override
        public RequestInfo[] newArray(int size) {
            return new RequestInfo[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.writeInt(mKey);
        dest.writeInt(mRequestType);
        switch (mRequestType) {
            case TYPE_GEO_LOCATION_REQ:
                mLocation.writeToParcel(dest, 0);
                dest.writeInt(mTempUnit);
                break;
            case TYPE_WEATHER_LOCATION_REQ:
                mWeatherLocation.writeToParcel(dest, 0);
                dest.writeInt(mTempUnit);
                break;
            case TYPE_LOOKUP_CITY_NAME_REQ:
                dest.writeString(mCityName);
                break;
        }
        dest.writeInt(mIsQueryOnly == true ? 1 : 0);
        dest.writeStrongBinder(mListener.asBinder());

        // Complete parcel info for the concierge
        parcelInfo.complete();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{ Request for ");
        switch (mRequestType) {
            case TYPE_GEO_LOCATION_REQ:
                builder.append("Location: ").append(mLocation);
                builder.append(" Temp Unit: ");
                if (mTempUnit == WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT) {
                    builder.append("Fahrenheit");
                } else {
                    builder.append(" Celsius");
                }
                break;
            case TYPE_WEATHER_LOCATION_REQ:
                builder.append("WeatherLocation: ").append(mWeatherLocation);
                builder.append(" Temp Unit: ");
                if (mTempUnit == WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT) {
                    builder.append("Fahrenheit");
                } else {
                    builder.append(" Celsius");
                }
                break;
            case TYPE_LOOKUP_CITY_NAME_REQ:
                builder.append("Lookup City: ").append(mCityName);
                break;
        }
        return builder.append(" }").toString();
    }

    @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;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof RequestInfo) {
            RequestInfo info = (RequestInfo) obj;
            return (info.hashCode() == this.mKey);
        }
        return false;
    }
}