diff options
author | Adnan Begovic <adnan@cyngn.com> | 2016-04-07 15:59:44 -0700 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2016-04-07 16:03:29 -0700 |
commit | e3212dca58037083ba4a70108f937756c3a45052 (patch) | |
tree | 6a02109e8abf0f3a1ff7a8bc3a7e2f792cd16558 | |
parent | 44732a0846764a66456daf5b63356daf5c339a68 (diff) | |
download | android_packages_apps_WundergroundWeatherProvider-e3212dca58037083ba4a70108f937756c3a45052.tar.gz android_packages_apps_WundergroundWeatherProvider-e3212dca58037083ba4a70108f937756c3a45052.tar.bz2 android_packages_apps_WundergroundWeatherProvider-e3212dca58037083ba4a70108f937756c3a45052.zip |
WundergroundCM: Support weatherlocation lookup by postal and city/state.
Change-Id: Ida19d9ae5eaead1c72d2bac41669bb4cf4869686
5 files changed, 215 insertions, 14 deletions
diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java index 117697d..789e6ac 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java @@ -44,6 +44,7 @@ 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; @@ -56,6 +57,9 @@ public class DebugActivity extends WUBaseActivity implements 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; @@ -95,23 +99,149 @@ public class DebugActivity extends WUBaseActivity implements return super.onOptionsItemSelected(item); } - public void requestWeatherInfo(View v) { + public void requestWeatherInfoByGeoLocation(View v) { mDirectRequest = false; - requestWeatherInfo(); + requestWeatherInfoByGeoLocation(); } - public void requestWeatherInfoDirectly(View v) { + public void requestWeatherInfoByGeoLocationDirectly(View v) { mDirectRequest = true; - requestWeatherInfo(); + requestWeatherInfoByGeoLocation(); } - private void requestWeatherInfo() { - Log.d(TAG, "Requesting weather!"); + 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); + } + + private void requestWeatherInfoByWeatherLocation(int type) { + WeatherLocation weatherLocation = new WeatherLocation.Builder("Seattle", "Seattle") + .setPostalCode("98121") + .setCountry("US", "US") + .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("WA", + 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(System.currentTimeMillis()); + + weatherInfoBuilder.setTemperature(currentObservationResponse.getTempF() + .floatValue(), + 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 city + weatherInfoBuilder.setCity(displayLocationResponse.getCity(), + displayLocationResponse.getCity()); + + // 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) { @@ -137,6 +267,7 @@ public class DebugActivity extends WUBaseActivity implements @Override public void onLocationChanged(Location location) { + Log.d(TAG, "Requesting weather by location " + location); if (mDirectRequest) { Call<WundergroundReponse> wundergroundCall = mWundergroundServiceManager.query(location.getLatitude(), diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java index 142872e..0e01e1d 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java @@ -150,10 +150,22 @@ public class WundergroundWeatherProviderService extends WeatherProviderService */ private void handleRequestByWeatherLocation(WeatherLocation weatherLocation, final ServiceRequest serviceRequest) { - Call<WundergroundReponse> wundergroundCall = - mWundergroundServiceManager.query( - /** todo: ADD STATE TO WEATHER LOCATION API */ null, - weatherLocation.getCity(), Feature.conditions, Feature.forecast); + + Call<WundergroundReponse> wundergroundCall = null; + if (weatherLocation.getCity() != null) { + wundergroundCall = + mWundergroundServiceManager.query( + /** todo: ADD STATE TO WEATHER LOCATION API */ "WA", + weatherLocation.getCity(), Feature.conditions, Feature.forecast); + } else if (weatherLocation.getPostalCode() != null) { + wundergroundCall = + mWundergroundServiceManager.query(weatherLocation.getPostalCode(), Feature.conditions, Feature.forecast); + } else { + Log.e(TAG, "Unable to handle service request"); + serviceRequest.fail(); + return; + } + wundergroundCall.enqueue(new WundergroundRequestCallback(serviceRequest, this)); } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java index 1d544e5..5bbc8ad 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java @@ -32,4 +32,8 @@ public interface WundergroundServiceInterface { public Call<WundergroundReponse> query(@Path("latitude") double latitude, @Path("longitude") double longitude, @Path(value ="features", encoded = true) String features); + + @GET("{features}/q/{postalCode}.json") + public Call<WundergroundReponse> query(@Path("postalCode") String postalCode, + @Path(value ="features", encoded = true) String features); }
\ No newline at end of file diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java index 5dbe3f4..3645740 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java @@ -49,6 +49,11 @@ public class WundergroundServiceManager { coerceVarArgFeaturesToDelimitedString(features)); } + public Call<WundergroundReponse> query(String postalCode, FeatureParam... features) { + return mWundergroundServiceInterface.query(postalCode, + coerceVarArgFeaturesToDelimitedString(features)); + } + private String coerceVarArgFeaturesToDelimitedString(FeatureParam... featureParams) { return Joiner.on('/').join(featureParams); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6729e8c..90ec06c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -19,18 +19,67 @@ android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> + <TextView + android:text="Through CM" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <View + android:layout_width="match_parent" + android:layout_height="1dp" + android:background="@android:color/black" /> + + <Button + android:layout_marginTop="5dp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request WeatherInfo By Geo" + android:onClick="requestWeatherInfoByGeoLocation"/> + + <Button + android:layout_marginTop="5dp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request WeatherInfo By WeatherLocation (City/State)" + android:onClick="requestWeatherInfoByWeatherLocationCityState"/> + + <Button + android:layout_marginTop="5dp" + android:layout_marginBottom="5dp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request WeatherInfo By WeatherLocation (ZIP)" + android:onClick="requestWeatherInfoByWeatherLocationPostalcode"/> + + <TextView + android:text="Direct (bypass service)" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <View + android:layout_width="match_parent" + android:layout_height="1dp" + android:background="@android:color/black" /> + + <Button + android:layout_marginTop="5dp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request WeatherInfo By Geo" + android:onClick="requestWeatherInfoByGeoLocationDirectly"/> + <Button android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Request WeatherInfo" - android:onClick="requestWeatherInfo"/> + android:text="Request WeatherInfo By WeatherLocation (City/State)" + android:onClick="requestWeatherInfoByWeatherLocationCityStateDirectly"/> <Button android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Request WeatherInfo Directly" - android:onClick="requestWeatherInfoDirectly"/> + android:text="Request WeatherInfo By WeatherLocation (ZIP)" + android:onClick="requestWeatherInfoByWeatherLocationPostalcodeDirectly"/> </LinearLayout> |