aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/weather/WeatherInfo.java
blob: a8577346bafd7bfbc09d3335420f6f49956efb2a (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
/*
 * Copyright (C) 2012 The AOKP 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 com.cyanogenmod.lockclock.weather;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;

import com.cyanogenmod.lockclock.R;
import com.cyanogenmod.lockclock.misc.WidgetUtils;

import java.text.DecimalFormat;
import java.util.Date;

public class WeatherInfo {
    private static final DecimalFormat sNoDigitsFormat = new DecimalFormat("0");

    private Context mContext;

    private String id;
    private String city;
    private String forecastDate;
    private String condition;
    private int conditionCode;
    private float temperature;
    private float lowTemperature;
    private float highTemperature;
    private String tempUnit;
    private float humidity;
    private float wind;
    private int windDirection;
    private String speedUnit;
    private long timestamp;

    public WeatherInfo(Context context, String id,
            String city, String fdate, String condition, int conditionCode,
            float temp, float low, float high, String tempUnit, float humidity,
            float wind, int windDir, String speedUnit, long timestamp) {
        this.mContext = context.getApplicationContext();
        this.id = id;
        this.city = city;
        this.forecastDate = fdate;
        this.condition = condition;
        this.conditionCode = conditionCode;
        this.humidity = humidity;
        this.wind = wind;
        this.windDirection = windDir;
        this.speedUnit = speedUnit;
        this.timestamp = timestamp;
        this.temperature = temp;
        this.lowTemperature = low;
        this.highTemperature = high;
        this.tempUnit = tempUnit;
    }

    public int getConditionResource() {
        final Resources res = mContext.getResources();
        final int resId = res.getIdentifier("weather2_" + conditionCode, "drawable", mContext.getPackageName());
        if (resId != 0) {
            return resId;
        }
        return R.drawable.weather2_na;
    }

    public Bitmap getConditionBitmap(int color) {
        final Resources res = mContext.getResources();
        int resId = res.getIdentifier("weather_" + conditionCode, "drawable", mContext.getPackageName());
        if (resId == 0) {
            resId = R.drawable.weather_na;
        }
        return WidgetUtils.getOverlaidBitmap(mContext, resId, color);
    }

    public String getId() {
        return id;
    }

    public String getCity() {
        return city;
    }

    public String getCondition() {
        final Resources res = mContext.getResources();
        final int resId = res.getIdentifier("weather_" + conditionCode, "string", mContext.getPackageName());
        if (resId != 0) {
            return res.getString(resId);
        }
        return condition;
    }

    public Date getTimestamp() {
        return new Date(timestamp);
    }

    private String getFormattedValue(float value, String unit) {
        if (Float.isNaN(highTemperature)) {
            return "-";
        }
        return sNoDigitsFormat.format(value) + unit;
    }

    public String getFormattedTemperature() {
        return getFormattedValue(temperature, "°" + tempUnit);
    }

    public String getFormattedLow() {
        return getFormattedValue(lowTemperature, "°");
    }

    public String getFormattedHigh() {
        return getFormattedValue(highTemperature, "°");
    }

    public String getFormattedHumidity() {
        return getFormattedValue(humidity, "%");
    }

    public String getFormattedWindSpeed() {
        if (wind < 0) {
            return mContext.getString(R.string.unknown);
        }
        return getFormattedValue(wind, speedUnit);
    }

    public String getWindDirection() {
        int resId;

        if (windDirection < 0) resId = R.string.unknown;
        else if (windDirection < 23) resId = R.string.weather_N;
        else if (windDirection < 68) resId = R.string.weather_NE;
        else if (windDirection < 113) resId = R.string.weather_E;
        else if (windDirection < 158) resId = R.string.weather_SE;
        else if (windDirection < 203) resId = R.string.weather_S;
        else if (windDirection < 248) resId = R.string.weather_SW;
        else if (windDirection < 293) resId = R.string.weather_W;
        else if (windDirection < 338) resId = R.string.weather_NW;
        else resId = R.string.weather_N;

        return mContext.getString(resId);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("WeatherInfo for ");
        builder.append(city);
        builder.append(" (");
        builder.append(id);
        builder.append(") @ ");
        builder.append(getTimestamp());
        builder.append(": ");
        builder.append(getCondition());
        builder.append("(");
        builder.append(conditionCode);
        builder.append("), temperature ");
        builder.append(getFormattedTemperature());
        builder.append(", low ");
        builder.append(getFormattedLow());
        builder.append(", high ");
        builder.append(getFormattedHigh());
        builder.append(", humidity ");
        builder.append(getFormattedHumidity());
        builder.append(", wind ");
        builder.append(getFormattedWindSpeed());
        builder.append(" at ");
        builder.append(getWindDirection());
        return builder.toString();
    }

    public String toSerializedString() {
        StringBuilder builder = new StringBuilder();
        builder.append(id).append('|');
        builder.append(city).append('|');
        builder.append(forecastDate).append('|');
        builder.append(condition).append('|');
        builder.append(conditionCode).append('|');
        builder.append(temperature).append('|');
        builder.append(lowTemperature).append('|');
        builder.append(highTemperature).append('|');
        builder.append(tempUnit).append('|');
        builder.append(humidity).append('|');
        builder.append(wind).append('|');
        builder.append(windDirection).append('|');
        builder.append(speedUnit).append('|');
        builder.append(timestamp);
        return builder.toString();
    }

    public static WeatherInfo fromSerializedString(Context context, String input) {
        if (input == null) {
            return null;
        }

        String[] parts = input.split("\\|");
        if (parts == null || parts.length != 14) {
            return null;
        }

        int conditionCode, windDirection;
        long timestamp;
        float temperature, low, high, humidity, wind;

        try {
            conditionCode = Integer.parseInt(parts[4]);
            temperature = Float.parseFloat(parts[5]);
            low = Float.parseFloat(parts[6]);
            high = Float.parseFloat(parts[7]);
            humidity = Float.parseFloat(parts[9]);
            wind = Float.parseFloat(parts[10]);
            windDirection = Integer.parseInt(parts[11]);
            timestamp = Long.parseLong(parts[13]);
        } catch (NumberFormatException e) {
            return null;
        }

        return new WeatherInfo(context,
                /* id */ parts[0], /* city */ parts[1], /* date */ parts[2],
                /* condition */ parts[3], conditionCode, temperature, low, high,
                /* tempUnit */ parts[8], humidity, wind, windDirection,
                /* speedUnit */ parts[12], timestamp);
    }
}