Skip to content

Commit

Permalink
Updating the syncadapter to use the latlng variable stored in sharedp…
Browse files Browse the repository at this point in the history
…references
  • Loading branch information
joannasmith authored and DAGalpin committed Aug 27, 2015
1 parent cc998e7 commit fac9721
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
Expand Up @@ -24,12 +24,13 @@
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.text.TextUtils;

import com.example.android.sunshine.app.data.WeatherContract;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;

/**
* A {@link PreferenceActivity} that presents a set of application settings.
Expand Down Expand Up @@ -137,7 +138,12 @@ public boolean onPreferenceChange(Preference preference, Object value) {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if ( key.equals(getString(R.string.pref_location_key)) ) {
// we've changed the location
// first clear locationStatus
// Wipe out any potential PlacePicker latlng values so that we can use this text entry.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(getString(R.string.pref_location_latitude));
editor.remove(getString(R.string.pref_location_longitude));
editor.commit();

Utility.resetLocationStatus(this);
SunshineSyncAdapter.syncImmediately(this);
} else if ( key.equals(getString(R.string.pref_units_key)) ) {
Expand Down Expand Up @@ -167,12 +173,38 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String address = place.getAddress().toString();
// TODO(student): Get the LatLng object from the place -- this is a placeholder.
LatLng latLong = new LatLng(37.422503f, -122.083939f);

// If the provided place doesn't have an address, we'll form a display-friendly
// string from the latlng values.
if (TextUtils.isEmpty(address)) {
address = String.format("(%.2f, %.2f)",latLong.latitude, latLong.longitude);
}

SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.pref_location_key), address);

// Also store the latitude and longitude so that we can use these to get a precise
// result from our weather service. We cannot expect the weather service to
// understand addresses that Google formats.
// TODO(student) Store the latitude and longitude as float values according to the
// keys defined in strings.xml.



editor.commit();

// Tell the SyncAdapter that we've changed the location, so that we can update
// our UI with new values. We need to do this manually because we are responding
// to the PlacePicker widget result here instead of allowing the
// LocationEditTextPreference to handle these changes and invoke our callbacks.
Preference locationPreference = findPreference(getString(R.string.pref_location_key));
setPreferenceSummary(locationPreference, address);
Utility.resetLocationStatus(this);
SunshineSyncAdapter.syncImmediately(this);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/example/android/sunshine/app/Utility.java
Expand Up @@ -30,6 +30,30 @@
import java.util.Locale;

public class Utility {
// We'll default our latlong to 0. Yay, "Earth!"
public static float DEFAULT_LATLONG = 0F;

public static boolean isLocationLatLonAvailable(Context context) {
SharedPreferences prefs
= PreferenceManager.getDefaultSharedPreferences(context);
return prefs.contains(context.getString(R.string.pref_location_latitude))
&& prefs.contains(context.getString(R.string.pref_location_longitude));
}

public static float getLocationLatitude(Context context) {
SharedPreferences prefs
= PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getFloat(context.getString(R.string.pref_location_latitude),
DEFAULT_LATLONG);
}

public static float getLocationLongitude(Context context) {
SharedPreferences prefs
= PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getFloat(context.getString(R.string.pref_location_longitude),
DEFAULT_LATLONG);
}

public static String getPreferredLocation(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString(context.getString(R.string.pref_location_key),
Expand Down
Expand Up @@ -93,7 +93,13 @@ public SunshineSyncAdapter(Context context, boolean autoInitialize) {
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(LOG_TAG, "Starting sync");
String locationQuery = Utility.getPreferredLocation(getContext());

// We no longer need just the location String, but also potentially the latitude and
// longitude, in case we are syncing based on a new Place Picker API result.
Context context = getContext();
String locationQuery = Utility.getPreferredLocation(context);
String locationLatitude = String.valueOf(Utility.getLocationLatitude(context));
String locationLongitude = String.valueOf(Utility.getLocationLongitude(context));

// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
Expand All @@ -114,13 +120,28 @@ public void onPerformSync(Account account, Bundle extras, String authority, Cont
final String FORECAST_BASE_URL =
"http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String LAT_PARAM = "lat";
final String LON_PARAM = "lon";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";

Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, locationQuery)
.appendQueryParameter(FORMAT_PARAM, format)
Uri.Builder uriBuilder = Uri.parse(FORECAST_BASE_URL).buildUpon();

// Instead of always building the query based off of the location string, we want to
// potentially build a query using a lat/lon value. This will be the case when we are
// syncing based off of a new location from the Place Picker API. So we need to check
// if we have a lat/lon to work with, and use those when we do. Otherwise, the weather
// service may not understand the location address provided by the Place Picker API
// and the user could end up with no weather! The horror!
if (Utility.isLocationLatLonAvailable(context)) {
uriBuilder.appendQueryParameter(LAT_PARAM, locationLatitude)
.appendQueryParameter(LON_PARAM, locationLongitude);
} else {
uriBuilder.appendQueryParameter(QUERY_PARAM, locationQuery);
}

Uri builtUri = uriBuilder.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.build();
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -45,6 +45,10 @@
<!-- Key name for storing location status in SharedPreferences -->
<string name="pref_location_status_key" translatable="false">loc-status</string>

<!-- Key name for storing location latlong in SharedPreferences -->
<string name="pref_location_latitude" translatable="false">loc-latitude</string>
<string name="pref_location_longitude" translatable="false">loc-longitude</string>

<!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
<string name="pref_location_default" translatable="false">94043</string>

Expand Down

0 comments on commit fac9721

Please sign in to comment.