diff options
author | Adnan Begovic <adnan@cyngn.com> | 2016-04-07 17:50:36 -0700 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2016-04-07 17:52:14 -0700 |
commit | bdb76416e6375ac4d3df754eda4153bfb247e30c (patch) | |
tree | 89015cdc6eb65f2f3190bb913a62bb4d8d5ab284 | |
parent | e3212dca58037083ba4a70108f937756c3a45052 (diff) | |
download | android_packages_apps_WundergroundWeatherProvider-bdb76416e6375ac4d3df754eda4153bfb247e30c.tar.gz android_packages_apps_WundergroundWeatherProvider-bdb76416e6375ac4d3df754eda4153bfb247e30c.tar.bz2 android_packages_apps_WundergroundWeatherProvider-bdb76416e6375ac4d3df754eda4153bfb247e30c.zip |
WundergroundCM: Add support for city lookup.
Change-Id: Ic60ba415006b0f3394c33851dc46d6bcb14d608f
9 files changed, 223 insertions, 13 deletions
diff --git a/app/build.gradle b/app/build.gradle index d28efab..8b919f1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,4 +31,5 @@ dependencies { compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.dagger:dagger-compiler:1.2.2' compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' + compile 'com.google.code.gson:gson:2.6.1' } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java index 789e6ac..99c9a1c 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/DebugActivity.java @@ -35,9 +35,11 @@ import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.Curr 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; @@ -53,6 +55,7 @@ import retrofit2.Response; public class DebugActivity extends WUBaseActivity implements CMWeatherManager.WeatherServiceProviderChangeListener, CMWeatherManager.WeatherUpdateRequestListener, + CMWeatherManager.LookupCityRequestListener, LocationListener { private static final String TAG = DebugActivity.class.getSimpleName(); @@ -135,6 +138,47 @@ public class DebugActivity extends WUBaseActivity implements 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") @@ -376,4 +420,12 @@ public class DebugActivity extends WUBaseActivity implements 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); + } + } } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java index 0e01e1d..02a7c08 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/WundergroundWeatherProviderService.java @@ -21,6 +21,7 @@ import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.os.Message; +import android.text.TextUtils; import android.util.Log; import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.ConverterUtils; @@ -30,6 +31,7 @@ import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.Curr 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 cyanogenmod.providers.WeatherContract; @@ -43,6 +45,7 @@ import cyanogenmod.weatherservice.WeatherProviderService; import retrofit2.Call; import java.util.ArrayList; +import java.util.List; import javax.inject.Inject; @@ -96,6 +99,9 @@ public class WundergroundWeatherProviderService extends WeatherProviderService case RequestInfo.TYPE_GEO_LOCATION_REQ: reference.handleWeatherRequest(serviceRequest); break; + case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: + reference.handleLookupRequest(serviceRequest); + break; default: //Don't support anything else, fail. serviceRequest.fail(); @@ -114,6 +120,22 @@ public class WundergroundWeatherProviderService extends WeatherProviderService } } + private void handleLookupRequest(ServiceRequest serviceRequest) { + final RequestInfo requestInfo = serviceRequest.getRequestInfo(); + + String cityName = requestInfo.getCityName(); + + if (TextUtils.isEmpty(cityName)) { + Log.d(TAG, "Null citname return"); + serviceRequest.fail(); + return; + } + + Call<WundergroundReponse> wundergroundCall = + mWundergroundServiceManager.query(cityName, Feature.geolookup); + wundergroundCall.enqueue(new WundergroundRequestCallback(serviceRequest, this)); + } + private void handleWeatherRequest(final ServiceRequest serviceRequest) { final RequestInfo requestInfo = serviceRequest.getRequestInfo(); Log.d(TAG, "Received weather request info: " + requestInfo.toString()); @@ -159,7 +181,8 @@ public class WundergroundWeatherProviderService extends WeatherProviderService weatherLocation.getCity(), Feature.conditions, Feature.forecast); } else if (weatherLocation.getPostalCode() != null) { wundergroundCall = - mWundergroundServiceManager.query(weatherLocation.getPostalCode(), Feature.conditions, Feature.forecast); + mWundergroundServiceManager.query(weatherLocation.getPostalCode(), + Feature.conditions, Feature.forecast); } else { Log.e(TAG, "Unable to handle service request"); serviceRequest.fail(); @@ -172,7 +195,22 @@ public class WundergroundWeatherProviderService extends WeatherProviderService @Override public void processWundergroundResponse(WundergroundReponse wundergroundReponse, final ServiceRequest serviceRequest) { + switch (serviceRequest.getRequestInfo().getRequestType()) { + case RequestInfo.TYPE_WEATHER_LOCATION_REQ: + case RequestInfo.TYPE_GEO_LOCATION_REQ: + processWeatherRequest(wundergroundReponse, serviceRequest); + break; + case RequestInfo.TYPE_LOOKUP_CITY_NAME_REQ: + processCityLookupRequest(wundergroundReponse, serviceRequest); + break; + default: + //Don't support anything else, fail. + serviceRequest.fail(); + } + } + private void processWeatherRequest(WundergroundReponse wundergroundReponse, + ServiceRequest serviceRequest) { CurrentObservationResponse currentObservationResponse = wundergroundReponse.getCurrentObservation(); @@ -238,7 +276,24 @@ public class WundergroundWeatherProviderService extends WeatherProviderService ServiceRequestResult serviceRequestResult = new ServiceRequestResult.Builder() - .setWeatherInfo(weatherInfoBuilder.build()).build(); + .setWeatherInfo(weatherInfoBuilder.build()) + .build(); + serviceRequest.complete(serviceRequestResult); + } + + private void processCityLookupRequest(WundergroundReponse wundergroundReponse, + ServiceRequest serviceRequest) { + List<CityDisambiguationResponse> cityDisambiguationResponses = + wundergroundReponse.getCityDisambiguation(); + + ArrayList<WeatherLocation> weatherLocations = + ConverterUtils.convertDisambiguationsToWeatherLocations( + cityDisambiguationResponses); + + ServiceRequestResult serviceRequestResult = + new ServiceRequestResult.Builder() + .setLocationLookupList(weatherLocations) + .build(); serviceRequest.complete(serviceRequestResult); } } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/ConverterUtils.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/ConverterUtils.java index 41ea613..cb13588 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/ConverterUtils.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/ConverterUtils.java @@ -16,6 +16,7 @@ package org.cyanogenmod.wundergroundcmweatherprovider.wunderground; +import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.citylookup.CityDisambiguationResponse; import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.forecast.ForecastDayResponse; import java.util.ArrayList; @@ -23,6 +24,7 @@ import java.util.List; import cyanogenmod.providers.WeatherContract; import cyanogenmod.weather.WeatherInfo; +import cyanogenmod.weather.WeatherLocation; public class ConverterUtils { @@ -42,4 +44,18 @@ public class ConverterUtils { } return dayForecasts; } + + public static ArrayList<WeatherLocation> convertDisambiguationsToWeatherLocations( + List<CityDisambiguationResponse> cityDisambiguationResponses) { + ArrayList<WeatherLocation> weatherLocations = new ArrayList<>(); + for (CityDisambiguationResponse cityDisambiguationResponse : cityDisambiguationResponses) { + WeatherLocation weatherLocation = new WeatherLocation.Builder( + cityDisambiguationResponse.getCity(), cityDisambiguationResponse.getCity()) + .setCountry(cityDisambiguationResponse.getCountry(), + cityDisambiguationResponse.getCountry()) + .build(); + weatherLocations.add(weatherLocation); + } + return weatherLocations; + } } 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 5bbc8ad..12d2835 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceInterface.java @@ -36,4 +36,7 @@ public interface WundergroundServiceInterface { @GET("{features}/q/{postalCode}.json") public Call<WundergroundReponse> query(@Path("postalCode") String postalCode, @Path(value ="features", encoded = true) String features); + + @GET("q/{city}.json") + public Call<WundergroundReponse> lookupCity(@Path("city") String city); }
\ 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 3645740..359730a 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/WundergroundServiceManager.java @@ -54,19 +54,23 @@ public class WundergroundServiceManager { coerceVarArgFeaturesToDelimitedString(features)); } + public Call<WundergroundReponse> lookupCity(String city) { + return mWundergroundServiceInterface.lookupCity(city); + } + private String coerceVarArgFeaturesToDelimitedString(FeatureParam... featureParams) { return Joiner.on('/').join(featureParams); } private Retrofit buildRestAdapter(String apiKey) { //TODO: Wrap this in debug flag - //HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); - //interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); - //OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); + HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); + interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); + OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); return new Retrofit.Builder() .baseUrl("http://api.wunderground.com/api/" + apiKey + "/") - //.client(client) + .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/WundergroundReponse.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/WundergroundReponse.java index af4c09c..79f49bd 100644 --- a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/WundergroundReponse.java +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/WundergroundReponse.java @@ -18,7 +18,11 @@ package org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses; import com.google.gson.annotations.SerializedName; +import org.cyanogenmod.wundergroundcmweatherprovider.wunderground.responses.citylookup.CityDisambiguationResponse; + import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; import cyanogenmod.weatherservice.ServiceRequest; @@ -29,6 +33,9 @@ public class WundergroundReponse implements Serializable { @SerializedName("forecast") private ForecastResponse forecastResponse; + @SerializedName("results") + private List<CityDisambiguationResponse> cityDisambiguationResponseList = new ArrayList<>(); + private ServiceRequest serviceRequest; public CurrentObservationResponse getCurrentObservation() { @@ -52,6 +59,15 @@ public class WundergroundReponse implements Serializable { this.serviceRequest = serviceRequest; } + public List<CityDisambiguationResponse> getCityDisambiguation() { + return cityDisambiguationResponseList; + } + + public void setCityDisambiguationResponseList( + List<CityDisambiguationResponse> cityDisambiguationResponseList) { + this.cityDisambiguationResponseList = cityDisambiguationResponseList; + } + public ServiceRequest getServiceRequest() { return serviceRequest; } diff --git a/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/citylookup/CityDisambiguationResponse.java b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/citylookup/CityDisambiguationResponse.java new file mode 100644 index 0000000..45b8e86 --- /dev/null +++ b/app/src/main/java/org/cyanogenmod/wundergroundcmweatherprovider/wunderground/responses/citylookup/CityDisambiguationResponse.java @@ -0,0 +1,58 @@ +/** + * 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.wunderground.responses.citylookup; + +import java.io.Serializable; + +public class CityDisambiguationResponse implements Serializable { + private String name; + private String city; + private String state; + private String country; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 90ec06c..d67c7c9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,27 +30,29 @@ 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"/> + <Button + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request City Disambiguation (DALLAS)" + android:onClick="requestCityDisambiguation"/> + <TextView android:text="Direct (bypass service)" android:layout_width="wrap_content" @@ -62,24 +64,27 @@ 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 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 By WeatherLocation (ZIP)" android:onClick="requestWeatherInfoByWeatherLocationPostalcodeDirectly"/> + <Button + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Request City Disambiguation (DALLAS)" + android:onClick="requestCityDisambiguationDirectly"/> + </LinearLayout> |