summaryrefslogtreecommitdiffstats
path: root/src/java/cyanogenmod/weatherservice/ServiceRequestResult.java
blob: 07f4cd903d62f3076f534346b6479a13f0ecea65 (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
/*
 * 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.weatherservice;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

import cyanogenmod.os.Build;
import cyanogenmod.os.Concierge;
import cyanogenmod.os.Concierge.ParcelInfo;
import cyanogenmod.weather.WeatherLocation;
import cyanogenmod.weather.WeatherInfo;

import java.util.ArrayList;

/**
 * Use this class to build a request result.
 */
public final class ServiceRequestResult implements Parcelable {

    private WeatherInfo mWeatherInfo;
    private ArrayList<WeatherLocation> mLocationLookupList;
    private int mKey;

    private ServiceRequestResult() {}

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

        if (parcelableVersion >= Build.CM_VERSION_CODES.ELDERBERRY) {
            mKey = in.readInt();
            int hasWeatherInfo = in.readInt();
            if (hasWeatherInfo == 1) {
                mWeatherInfo = WeatherInfo.CREATOR.createFromParcel(in);
            }
            int hasLocationLookupList = in.readInt();
            if (hasLocationLookupList == 1) {
                mLocationLookupList = new ArrayList<>();
                int listSize = in.readInt();
                while (listSize > 0) {
                    mLocationLookupList.add(WeatherLocation.CREATOR.createFromParcel(in));
                    listSize--;
                }
            }
        }

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

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

        @Override
        public ServiceRequestResult[] newArray(int size) {
            return new ServiceRequestResult[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);
        if (mWeatherInfo != null) {
            dest.writeInt(1);
            mWeatherInfo.writeToParcel(dest, 0);
        } else {
            dest.writeInt(0);
        }
        if (mLocationLookupList != null) {
            dest.writeInt(1);
            dest.writeInt(mLocationLookupList.size());
            for (WeatherLocation lookup : mLocationLookupList) {
                lookup.writeToParcel(dest, 0);
            }
        } else {
            dest.writeInt(0);
        }

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

    public static class Builder {
        private WeatherInfo mBuilderWeatherInfo;
        private ArrayList<WeatherLocation> mBuilderLocationLookupList;
        public Builder() {
            this.mBuilderWeatherInfo = null;
            this.mBuilderLocationLookupList = null;
        }

        /**
         * Add the supplied weather information to the result. Attempting to add a WeatherLocation
         * list to the same builder will cause the system to throw IllegalArgumentException
         *
         * @param weatherInfo The WeatherInfo object holding the data that will be used to update
         *                    the weather content provider
         */
        public Builder setWeatherInfo(@NonNull WeatherInfo weatherInfo) {
            if (mBuilderLocationLookupList != null) {
                throw new IllegalArgumentException("Can't add weather information when you have"
                        + " already added a WeatherLocation list");
            }

            if (weatherInfo == null) {
                throw new IllegalArgumentException("WeatherInfo can't be null");
            }

            mBuilderWeatherInfo = weatherInfo;
            return this;
        }

        /**
         * Add the supplied list of WeatherLocation objects to the result. Attempting to add a
         * WeatherInfo object to the same builder will cause the system to throw
         * IllegalArgumentException
         *
         * @param locations The list of WeatherLocation objects. The list should not be null
         */
        public Builder setLocationLookupList(@NonNull ArrayList<WeatherLocation> locations) {
            if (mBuilderWeatherInfo != null) {
                throw new IllegalArgumentException("Can't add a WeatherLocation list when you have"
                        + " already added weather information");
            }

            mBuilderLocationLookupList = locations;
            return this;
        }

        /**
         * Creates a {@link ServiceRequest} with the arguments
         * supplied to this builder
         * @return {@link ServiceRequestResult}
         */
        public ServiceRequestResult build() {
            ServiceRequestResult result = new ServiceRequestResult();
            result.mWeatherInfo = this.mBuilderWeatherInfo;
            result.mLocationLookupList = this.mBuilderLocationLookupList;
            result.mKey = this.hashCode();
            return result;
        }
    }

    /**
     * @return The WeatherInfo object supplied by the weather provider service
     */
    public WeatherInfo getWeatherInfo() {
        return mWeatherInfo;
    }

    /**
     * @return The list of WeatherLocation objects supplied by the weather provider service
     */
    public ArrayList<WeatherLocation> getLocationLookupList() {
        return mLocationLookupList;
    }

    @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 ServiceRequestResult) {
            ServiceRequestResult request = (ServiceRequestResult) obj;
            return (request.hashCode() == this.mKey);
        }
        return false;
    }
}