aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java
blob: 88b28b88aecaa1deb2d494dc9cf62e0a6c540e4c (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
 * 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 org.cyanogenmod.wundergroundcmweatherprovider;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.ConverterUtils;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.Feature;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.WundergroundServiceManager;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.CurrentObservationResponse;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.DisplayLocationResponse;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.ForecastResponse;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.WundergroundReponse;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.citylookup.CityDisambiguationResponse;
import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.forecast.SimpleForecastResponse;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import cyanogenmod.providers.WeatherContract;
import cyanogenmod.weather.CMWeatherManager;
import cyanogenmod.weather.WeatherInfo;
import cyanogenmod.weather.WeatherLocation;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DebugActivity extends WUBaseActivity implements
        CMWeatherManager.WeatherServiceProviderChangeListener,
        CMWeatherManager.WeatherUpdateRequestListener,
        CMWeatherManager.LookupCityRequestListener,
        LocationListener {

    private static final String TAG = DebugActivity.class.getSimpleName();

    private static final int TYPE_CITY_STATE = 0;
    private static final int TYPE_POSTAL_CODE = 1;

    @Inject
    WundergroundServiceManager mWundergroundServiceManager;

    private CMWeatherManager mWeatherManager;
    private LocationManager mLocationManager;

    private boolean mDirectRequest = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWeatherManager = CMWeatherManager.getInstance(this);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mWeatherManager.registerWeatherServiceProviderChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void requestWeatherInfoByGeoLocation(View v) {
        mDirectRequest = false;
        requestWeatherInfoByGeoLocation();
    }

    public void requestWeatherInfoByGeoLocationDirectly(View v) {
        mDirectRequest = true;
        requestWeatherInfoByGeoLocation();
    }

    private void requestWeatherInfoByGeoLocation() {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        mLocationManager.requestSingleUpdate(criteria, this, Looper.getMainLooper());
    }

    public void requestWeatherInfoByWeatherLocationCityState(View v) {
        mDirectRequest = false;
        requestWeatherInfoByWeatherLocation(TYPE_CITY_STATE);
    }

    public void requestWeatherInfoByWeatherLocationCityStateDirectly(View v) {
        mDirectRequest = true;
        requestWeatherInfoByWeatherLocation(TYPE_CITY_STATE);
    }

    public void requestWeatherInfoByWeatherLocationPostalcode(View v) {
        mDirectRequest = false;
        requestWeatherInfoByWeatherLocation(TYPE_POSTAL_CODE);
    }

    public void requestWeatherInfoByWeatherLocationPostalcodeDirectly(View v) {
        mDirectRequest = true;
        requestWeatherInfoByWeatherLocation(TYPE_POSTAL_CODE);
    }

    public void requestCityDisambiguation(View v) {
        mDirectRequest = false;
        requestCityDisambiguation();
    }

    public void requestCityDisambiguationDirectly(View v) {
        mDirectRequest = true;
        requestCityDisambiguation();
    }

    private static final String HARDCODED_CITY = "DALLAS";
    private void requestCityDisambiguation() {
        if (!mDirectRequest) {
            mWeatherManager.lookupCity(HARDCODED_CITY, this);
        } else {
            Call<WundergroundReponse> wundergroundCall =
                    mWundergroundServiceManager.lookupCity(HARDCODED_CITY);
            wundergroundCall.enqueue(new Callback<WundergroundReponse>() {
                @Override
                public void onResponse(Call<WundergroundReponse> call, Response<WundergroundReponse> response) {
                    List<CityDisambiguationResponse> cityDisambiguationResponses =
                            response.body().getCityDisambiguation();

                    ArrayList<WeatherLocation> weatherLocations =
                            ConverterUtils.convertDisambiguationsToWeatherLocations(
                                    cityDisambiguationResponses);

                    Log.d(TAG, "Received disambiguation:");
                    for (WeatherLocation weatherLocation : weatherLocations) {
                        Log.d(TAG, "Weather location: " + weatherLocation);
                    }
                }

                @Override
                public void onFailure(Call<WundergroundReponse> call, Throwable t) {

                }
            });
        }
    }

    private void requestWeatherInfoByWeatherLocation(int type) {
        WeatherLocation weatherLocation = new WeatherLocation.Builder("Seattle", "Seattle")
                .setPostalCode("98121")
                .setCountry("US", "US")
                .setState("WA")
                .build();

        Log.d(TAG, "Requesting weather by weather location " + weatherLocation);
        Call<WundergroundReponse> wundergroundCall = null;
        if (!mDirectRequest) {
            mWeatherManager.requestWeatherUpdate(weatherLocation, this);
            return;
        } else  {
            if (type == TYPE_CITY_STATE) {
                wundergroundCall =
                        mWundergroundServiceManager.query(weatherLocation.getState(),
                                weatherLocation.getCity(), Feature.conditions, Feature.forecast);
            } else if (type == TYPE_POSTAL_CODE) {
                wundergroundCall =
                        mWundergroundServiceManager.query(weatherLocation.getPostalCode(),
                                Feature.conditions, Feature.forecast);
            }
        }

        wundergroundCall.enqueue(new Callback<WundergroundReponse>() {
            @Override
            public void onResponse(Call<WundergroundReponse> call, Response<WundergroundReponse> response) {
                if (response.isSuccessful()) {
                    Log.d(TAG, "Received response:\n" + response.body().toString());
                    WundergroundReponse wundergroundReponse = response.body();

                    if (wundergroundReponse == null) {
                        Log.d(TAG, "Null wu reponse, return");
                        return;
                    }

                    CurrentObservationResponse currentObservationResponse =
                            wundergroundReponse.getCurrentObservation();

                    if (currentObservationResponse == null) {
                        Log.d(TAG, "Null co reponse, return");
                        return;
                    }

                    WeatherInfo.Builder weatherInfoBuilder =
                            new WeatherInfo.Builder(
                                    currentObservationResponse.getDisplayLocation().getCity(),
                                    currentObservationResponse.getTempF(),
                                    WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT);

                    weatherInfoBuilder.setWeatherCondition(
                            WeatherContract.WeatherColumns.WeatherCode.CLOUDY);

                    DisplayLocationResponse displayLocationResponse =
                            currentObservationResponse.getDisplayLocation();

                    if (displayLocationResponse == null) {
                        Log.d(TAG, "Null dl reponse, return");
                        return;
                    }

                    // Set humidity
                    weatherInfoBuilder.setHumidity(currentObservationResponse.getHumidity()
                            .floatValue());

                    // Set wind arguments
                    weatherInfoBuilder.setWind(
                            currentObservationResponse.getWindMph().floatValue(),
                            currentObservationResponse.getWindDegrees().floatValue(),
                            WeatherContract.WeatherColumns.WindSpeedUnit.MPH);

                    ForecastResponse forecastResponse =
                            wundergroundReponse.getForecast();

                    if (forecastResponse == null) {
                        Log.d(TAG, "Null fc reponse, return");
                        return;
                    }

                    SimpleForecastResponse simpleForecastResponse =
                            forecastResponse.getSimpleForecast();

                    if (simpleForecastResponse == null) {
                        Log.d(TAG, "Null sf reponse, return");
                        return;
                    }

                    ArrayList<WeatherInfo.DayForecast> dayForecasts =
                            ConverterUtils.convertSimpleFCToDayForcast(
                                    simpleForecastResponse.getForecastDay());
                    weatherInfoBuilder.setForecast(dayForecasts);

                    Log.d(TAG, "Weather info " + weatherInfoBuilder.build().toString());
                }
            }

            @Override
            public void onFailure(Call<WundergroundReponse> call, Throwable t) {
                Log.d(TAG, "Failure " + t.toString());
            }
        });
    }

    @Override
    public void onWeatherServiceProviderChanged(String s) {

    }

    @Override
    public void onWeatherRequestCompleted(int i, WeatherInfo weatherInfo) {
        switch (i) {
            case CMWeatherManager.WEATHER_REQUEST_COMPLETED:
                Log.d(TAG, "Weather request completed: " + weatherInfo.toString());
                break;
            case CMWeatherManager.WEATHER_REQUEST_FAILED:
                Log.d(TAG, "Weather request failed!");
                break;
            case CMWeatherManager.WEATHER_REQUEST_ALREADY_IN_PROGRESS:
                Log.d(TAG, "Weather request already in progress");
                break;
            case CMWeatherManager.WEATHER_REQUEST_SUBMITTED_TOO_SOON:
                Log.d(TAG, "Weather request submitted too soon");
                break;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "Requesting weather by location " + location);
        if (mDirectRequest) {
            Call<WundergroundReponse> wundergroundCall =
                    mWundergroundServiceManager.query(location.getLatitude(),
                    location.getLongitude(), Feature.conditions, Feature.forecast);

            wundergroundCall.enqueue(new Callback<WundergroundReponse>() {
                @Override
                public void onResponse(Call<WundergroundReponse> call, Response<WundergroundReponse> response) {
                    if (response.isSuccessful()) {
                        Log.d(TAG, "Received response:\n" + response.body().toString());
                        WundergroundReponse wundergroundReponse = response.body();

                        if (wundergroundReponse == null) {
                            Log.d(TAG, "Null wu reponse, return");
                            return;
                        }

                        CurrentObservationResponse currentObservationResponse =
                                wundergroundReponse.getCurrentObservation();

                        if (currentObservationResponse == null) {
                            Log.d(TAG, "Null co reponse, return");
                            return;
                        }

                        WeatherInfo.Builder weatherInfoBuilder =
                                new WeatherInfo.Builder(
                                        currentObservationResponse.getDisplayLocation().getCity(),
                                        currentObservationResponse.getTempF(),
                                        WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT);

                        weatherInfoBuilder.setWeatherCondition(
                                WeatherContract.WeatherColumns.WeatherCode.CLOUDY);

                        DisplayLocationResponse displayLocationResponse =
                                currentObservationResponse.getDisplayLocation();

                        if (displayLocationResponse == null) {
                            Log.d(TAG, "Null dl reponse, return");
                            return;
                        }

                        // Set humidity
                        weatherInfoBuilder.setHumidity(currentObservationResponse.getHumidity()
                                .floatValue());

                        // Set wind arguments
                        weatherInfoBuilder.setWind(
                                currentObservationResponse.getWindMph().floatValue(),
                                currentObservationResponse.getWindDegrees().floatValue(),
                                WeatherContract.WeatherColumns.WindSpeedUnit.MPH);

                        ForecastResponse forecastResponse =
                                wundergroundReponse.getForecast();

                        if (forecastResponse == null) {
                            Log.d(TAG, "Null fc reponse, return");
                            return;
                        }

                        SimpleForecastResponse simpleForecastResponse =
                                forecastResponse.getSimpleForecast();

                        if (simpleForecastResponse == null) {
                            Log.d(TAG, "Null sf reponse, return");
                            return;
                        }

                        ArrayList<WeatherInfo.DayForecast> dayForecasts =
                                ConverterUtils.convertSimpleFCToDayForcast(
                                simpleForecastResponse.getForecastDay());
                        weatherInfoBuilder.setForecast(dayForecasts);

                        Log.d(TAG, "Weather info " + weatherInfoBuilder.build().toString());
                    } else {
                        Log.d(TAG, "Response " + response.toString());
                    }
                }

                @Override
                public void onFailure(Call<WundergroundReponse> call, Throwable t) {
                    Log.d(TAG, "Failure " + t.toString());
                }
            });
        } else {
            mWeatherManager.requestWeatherUpdate(location, this);
        }
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    public void onLookupCityRequestCompleted(ArrayList<WeatherLocation> arrayList) {
        Log.d(TAG, "Received disambiguation:");
        for (WeatherLocation weatherLocation : arrayList) {
            Log.d(TAG, "Weather location: " + weatherLocation);
        }
    }
}