aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/weather/WeatherContentProvider.java
blob: a2ab385e3e59d30ff3ef7fe6ac23856fa7a1318f (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

package com.cyanogenmod.lockclock.weather;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;

import com.cyanogenmod.lockclock.misc.Preferences;
import com.cyanogenmod.lockclock.weather.WeatherInfo.DayForecast;

public class WeatherContentProvider extends ContentProvider {

    public static final String TAG = WeatherContentProvider.class.getSimpleName();
    private static final boolean DEBUG = false;

    static WeatherInfo sCachedWeatherInfo;

    private static final int URI_TYPE_EVERYTHING = 1;
    private static final int URI_TYPE_CURRENT = 2;
    private static final int URI_TYPE_FORECAST = 3;

    private static final String COLUMN_CURRENT_CITY_ID = "city_id";
    private static final String COLUMN_CURRENT_CITY = "city";
    private static final String COLUMN_CURRENT_CONDITION = "condition";
    private static final String COLUMN_CURRENT_TEMPERATURE = "temperature";
    private static final String COLUMN_CURRENT_HUMIDITY = "humidity";
    private static final String COLUMN_CURRENT_WIND = "wind";
    private static final String COLUMN_CURRENT_TIME_STAMP = "time_stamp";
    private static final String COLUMN_CURRENT_CONDITION_CODE = "condition_code";

    private static final String COLUMN_FORECAST_LOW = "forecast_low";
    private static final String COLUMN_FORECAST_HIGH = "forecast_high";
    private static final String COLUMN_FORECAST_CONDITION = "forecast_condition";
    private static final String COLUMN_FORECAST_CONDITION_CODE = "forecast_condition_code";

    private static final String[] PROJECTION_DEFAULT_CURRENT = new String[] {
            COLUMN_CURRENT_CITY_ID,
            COLUMN_CURRENT_CITY,
            COLUMN_CURRENT_CONDITION,
            COLUMN_CURRENT_TEMPERATURE,
            COLUMN_CURRENT_HUMIDITY,
            COLUMN_CURRENT_WIND,
            COLUMN_CURRENT_TIME_STAMP,
            COLUMN_CURRENT_CONDITION_CODE
    };

    private static final String[] PROJECTION_DEFAULT_FORECAST = new String[] {
            COLUMN_FORECAST_LOW,
            COLUMN_FORECAST_HIGH,
            COLUMN_FORECAST_CONDITION,
            COLUMN_FORECAST_CONDITION_CODE
    };

    private static final String[] PROJECTION_DEFAULT_EVERYTHING = new String[] {
            COLUMN_CURRENT_CITY_ID,
            COLUMN_CURRENT_CITY,
            COLUMN_CURRENT_CONDITION,
            COLUMN_CURRENT_TEMPERATURE,
            COLUMN_CURRENT_HUMIDITY,
            COLUMN_CURRENT_WIND,
            COLUMN_CURRENT_TIME_STAMP,
            COLUMN_CURRENT_CONDITION_CODE,

            COLUMN_FORECAST_LOW,
            COLUMN_FORECAST_HIGH,
            COLUMN_FORECAST_CONDITION,
            COLUMN_FORECAST_CONDITION_CODE
    };

    public static final String AUTHORITY = "com.cyanogenmod.lockclock.weather.provider";

    private static final UriMatcher sUriMatcher;
    static {
        sUriMatcher = new UriMatcher(URI_TYPE_EVERYTHING);
        sUriMatcher.addURI(AUTHORITY, "weather", URI_TYPE_EVERYTHING);
        sUriMatcher.addURI(AUTHORITY, "weather/current", URI_TYPE_CURRENT);
        sUriMatcher.addURI(AUTHORITY, "weather/forecast", URI_TYPE_FORECAST);
    }

    private Context mContext;

    @Override
    public boolean onCreate() {
        mContext = getContext();
        sCachedWeatherInfo = Preferences.getCachedWeatherInfo(mContext);
        return true;
    }

    @Override
    public Cursor query(
            Uri uri,
            String[] projection,
            String selection,
            String[] selectionArgs,
            String sortOrder) {

        final int projectionType = sUriMatcher.match(uri);
        final MatrixCursor result = new MatrixCursor(resolveProjection(projection, projectionType));

        WeatherInfo weather = sCachedWeatherInfo;
        if (weather != null) {
            // current
            result.newRow()
                    .add(COLUMN_CURRENT_CITY, weather.getCity())
                    .add(COLUMN_CURRENT_CITY_ID, weather.getId())
                    .add(COLUMN_CURRENT_CONDITION, weather.getCondition())
                    .add(COLUMN_CURRENT_HUMIDITY, weather.getFormattedHumidity())
                    .add(COLUMN_CURRENT_WIND, weather.getFormattedWindSpeed()
                            + " " + weather.getWindDirection())
                    .add(COLUMN_CURRENT_TEMPERATURE, weather.getFormattedTemperature())
                    .add(COLUMN_CURRENT_TIME_STAMP, weather.getTimestamp().toString())
                    .add(COLUMN_CURRENT_CONDITION_CODE, weather.getConditionCode());

            // forecast
            for (DayForecast day : weather.getForecasts()) {
                result.newRow()
                        .add(COLUMN_FORECAST_CONDITION, day.getCondition(mContext))
                        .add(COLUMN_FORECAST_LOW, day.getFormattedLow())
                        .add(COLUMN_FORECAST_HIGH, day.getFormattedHigh())
                        .add(COLUMN_FORECAST_CONDITION_CODE, day.getConditionCode());
            }
            return result;
        } else {
            if (DEBUG) Log.e(TAG, "sCachedWeatherInfo is null");
            Intent updateWeather = new Intent(WeatherUpdateService.ACTION_FORCE_UPDATE);
            updateWeather.setClass(mContext, WeatherUpdateService.class);
            mContext.startService(updateWeather);
        }
        return null;
    }

    private String[] resolveProjection(String[] projection, int uriType) {
        if (projection != null)
            return projection;
        switch (uriType) {
            default:
            case URI_TYPE_EVERYTHING:
                return PROJECTION_DEFAULT_EVERYTHING;

            case URI_TYPE_CURRENT:
                return PROJECTION_DEFAULT_CURRENT;

            case URI_TYPE_FORECAST:
                return PROJECTION_DEFAULT_FORECAST;
        }
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    public static void updateCachedWeatherInfo(Context context, WeatherInfo info) {
        if (DEBUG) Log.e(TAG, "updateCachedWeatherInfo()");
        if(info != null) {
            if (DEBUG) Log.e(TAG, "set new weather info");
            sCachedWeatherInfo = WeatherInfo.fromSerializedString(context, info.toSerializedString());
        } else {
            if(DEBUG) Log.e(TAG, "nulled out cached weather info");
            sCachedWeatherInfo = null;
        }
        context.getContentResolver().notifyChange(
                Uri.parse("content://" + WeatherContentProvider.AUTHORITY + "/weather"), null);
    }

}