/** * Returns the elapsed time in milliseconds since the last notification was shown. This is used * as part of our check to see if we should show another notification when the weather is * updated. * * @param context Used to access SharedPreferences as well as use other utility methods * @return Elapsed time in milliseconds since the last notification was shown */ public static long getEllapsedTimeSinceLastNotification(Context context) { long lastNotificationTimeMillis = SunshinePreferences.getLastNotificationTimeInMillis(context); long timeSinceLastNotification = System.currentTimeMillis() - lastNotificationTimeMillis; return timeSinceLastNotification; }
/** * Returns the location coordinates associated with the location. Note that these coordinates * may not be set, which results in (0,0) being returned. (conveniently, 0,0 is in the middle * of the ocean off the west coast of Africa) * * @param context Used to get the SharedPreferences * @return An array containing the two coordinate values. */ public static double[] getLocationCoordinates(Context context) { return getDefaultWeatherCoordinates(); }
/** * Returns the location currently set in Preferences. The default location this method * will return is "94043,USA", which is Mountain View, California. Mountain View is the * home of the headquarters of the Googleplex! * * @param context Context used to get the SharedPreferences * @return Location The current user has set in SharedPreferences. Will default to * "94043,USA" if SharedPreferences have not been implemented yet. */ public static String getPreferredWeatherLocation(Context context) { /** This will be implemented in a future lesson **/ return getDefaultWeatherLocation(); }
/** * Retrieves the proper URL to query for the weather data. The reason for both this method as * well as {@link #buildUrlWithLocationQuery(String)} is two fold. * <p> * 1) You should be able to just use one method when you need to create the URL within the * app instead of calling both methods. * 2) Later in Sunshine, you are going to add an alternate method of allowing the user * to select their preferred location. Once you do so, there will be another way to form * the URL using a latitude and longitude rather than just a location String. This method * will "decide" which URL to build and return it. * * @param context used to access other Utility methods * @return URL to query weather service */ public static URL getUrl(Context context) { if (SunshinePreferences.isLocationLatLonAvailable(context)) { double[] preferredCoordinates = SunshinePreferences.getLocationCoordinates(context); double latitude = preferredCoordinates[0]; double longitude = preferredCoordinates[1]; return buildUrlWithLatitudeLongitude(latitude, longitude); } else { String locationQuery = SunshinePreferences.getPreferredWeatherLocation(context); return buildUrlWithLocationQuery(locationQuery); } }
/** * Temperature data is stored in Celsius by our app. Depending on the user's preference, * the app may need to display the temperature in Fahrenheit. This method will perform that * temperature conversion if necessary. It will also format the temperature so that no * decimal points show. Temperatures will be formatted to the following form: "21°" * * @param context Android Context to access preferences and resources * @param temperature Temperature in degrees Celsius (°C) * * @return Formatted temperature String in the following form: * "21°" */ public static String formatTemperature(Context context, double temperature) { if (!SunshinePreferences.isMetric(context)) { temperature = celsiusToFahrenheit(temperature); } int temperatureFormatResourceId = R.string.format_temperature; /* For presentation, assume the user doesn't care about tenths of a degree. */ return String.format(context.getString(temperatureFormatResourceId), temperature); }
/** * This method uses the URI scheme for showing a location found on a map in conjunction with * an implicit Intent. This super-handy intent is detailed in the "Common Intents" page of * Android's developer site: * * @see "http://developer.android.com/guide/components/intents-common.html#Maps" * <p> * Protip: Hold Command on Mac or Control on Windows and click that link to automagically * open the Common Intents page */ private void openLocationInMap() { String addressString = SunshinePreferences.getPreferredWeatherLocation(this); Uri geoLocation = Uri.parse("geo:0,0?q=" + addressString); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d(TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } }
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity activity = getActivity(); if (key.equals(getString(R.string.pref_location_key))) { // we've changed the location // Wipe out any potential PlacePicker latlng values so that we can use this text entry. SunshinePreferences.resetLocationCoordinates(activity); } Preference preference = findPreference(key); if (null != preference) { if (!(preference instanceof CheckBoxPreference)) { setPreferenceSummary(preference, sharedPreferences.getString(key, "")); } } } }
double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); SunshinePreferences.setLocationDetails(context, cityLatitude, cityLongitude);
/** * Uses the URI scheme for showing a location found on a map in conjunction with * an implicit Intent. This super-handy Intent is detailed in the "Common Intents" page of * Android's developer site: * * @see "http://developer.android.com/guide/components/intents-common.html#Maps" * <p> * Protip: Hold Command on Mac or Control on Windows and click that link to automagically * open the Common Intents page */ private void openPreferredLocationInMap() { double[] coords = SunshinePreferences.getLocationCoordinates(this); String posLat = Double.toString(coords[0]); String posLong = Double.toString(coords[1]); Uri geoLocation = Uri.parse("geo:" + posLat + "," + posLong); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d(TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } }
/** * Retrieves the proper URL to query for the weather data. The reason for both this method as * well as {@link #buildUrlWithLocationQuery(String)} is two fold. * <p> * 1) You should be able to just use one method when you need to create the URL within the * app instead of calling both methods. * 2) Later in Sunshine, you are going to add an alternate method of allowing the user * to select their preferred location. Once you do so, there will be another way to form * the URL using a latitude and longitude rather than just a location String. This method * will "decide" which URL to build and return it. * * @param context used to access other Utility methods * @return URL to query weather service */ public static URL getUrl(Context context) { if (SunshinePreferences.isLocationLatLonAvailable(context)) { double[] preferredCoordinates = SunshinePreferences.getLocationCoordinates(context); double latitude = preferredCoordinates[0]; double longitude = preferredCoordinates[1]; return buildUrlWithLatitudeLongitude(latitude, longitude); } else { String locationQuery = SunshinePreferences.getPreferredWeatherLocation(context); return buildUrlWithLocationQuery(locationQuery); } }
/** * Temperature data is stored in Celsius by our app. Depending on the user's preference, * the app may need to display the temperature in Fahrenheit. This method will perform that * temperature conversion if necessary. It will also format the temperature so that no * decimal points show. Temperatures will be formatted to the following form: "21°" * * @param context Android Context to access preferences and resources * @param temperature Temperature in degrees Celsius (°C) * * @return Formatted temperature String in the following form: * "21°" */ public static String formatTemperature(Context context, double temperature) { if (!SunshinePreferences.isMetric(context)) { temperature = celsiusToFahrenheit(temperature); } int temperatureFormatResourceId = R.string.format_temperature; /* For presentation, assume the user doesn't care about tenths of a degree. */ return String.format(context.getString(temperatureFormatResourceId), temperature); }
/** * This method uses the URI scheme for showing a location found on a map in conjunction with * an implicit Intent. This super-handy intent is detailed in the "Common Intents" page of * Android's developer site: * * @see "http://developer.android.com/guide/components/intents-common.html#Maps" * <p> * Protip: Hold Command on Mac or Control on Windows and click that link to automagically * open the Common Intents page */ private void openLocationInMap() { String addressString = SunshinePreferences.getPreferredWeatherLocation(this); Uri geoLocation = Uri.parse("geo:0,0?q=" + addressString); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d(TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } }
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity activity = getActivity(); if (key.equals(getString(R.string.pref_location_key))) { // we've changed the location // Wipe out any potential PlacePicker latlng values so that we can use this text entry. SunshinePreferences.resetLocationCoordinates(activity); } Preference preference = findPreference(key); if (null != preference) { if (!(preference instanceof CheckBoxPreference)) { setPreferenceSummary(preference, sharedPreferences.getString(key, "")); } } } }
double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); SunshinePreferences.setLocationDetails(context, cityLatitude, cityLongitude);
/** * Uses the URI scheme for showing a location found on a map in conjunction with * an implicit Intent. This super-handy Intent is detailed in the "Common Intents" page of * Android's developer site: * * @see "http://developer.android.com/guide/components/intents-common.html#Maps" * <p> * Protip: Hold Command on Mac or Control on Windows and click that link to automagically * open the Common Intents page */ private void openPreferredLocationInMap() { double[] coords = SunshinePreferences.getLocationCoordinates(this); String posLat = Double.toString(coords[0]); String posLong = Double.toString(coords[1]); Uri geoLocation = Uri.parse("geo:" + posLat + "," + posLong); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d(TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } }
/** * Retrieves the proper URL to query for the weather data. The reason for both this method as * well as {@link #buildUrlWithLocationQuery(String)} is two fold. * <p> * 1) You should be able to just use one method when you need to create the URL within the * app instead of calling both methods. * 2) Later in Sunshine, you are going to add an alternate method of allowing the user * to select their preferred location. Once you do so, there will be another way to form * the URL using a latitude and longitude rather than just a location String. This method * will "decide" which URL to build and return it. * * @param context used to access other Utility methods * @return URL to query weather service */ public static URL getUrl(Context context) { if (SunshinePreferences.isLocationLatLonAvailable(context)) { double[] preferredCoordinates = SunshinePreferences.getLocationCoordinates(context); double latitude = preferredCoordinates[0]; double longitude = preferredCoordinates[1]; return buildUrlWithLatitudeLongitude(latitude, longitude); } else { String locationQuery = SunshinePreferences.getPreferredWeatherLocation(context); return buildUrlWithLocationQuery(locationQuery); } }
/** * Temperature data is stored in Celsius by our app. Depending on the user's preference, * the app may need to display the temperature in Fahrenheit. This method will perform that * temperature conversion if necessary. It will also format the temperature so that no * decimal points show. Temperatures will be formatted to the following form: "21°" * * @param context Android Context to access preferences and resources * @param temperature Temperature in degrees Celsius (°C) * * @return Formatted temperature String in the following form: * "21°" */ public static String formatTemperature(Context context, double temperature) { if (!SunshinePreferences.isMetric(context)) { temperature = celsiusToFahrenheit(temperature); } int temperatureFormatResourceId = R.string.format_temperature; /* For presentation, assume the user doesn't care about tenths of a degree. */ return String.format(context.getString(temperatureFormatResourceId), temperature); }
/** * This method uses the URI scheme for showing a location found on a map in conjunction with * an implicit Intent. This super-handy intent is detailed in the "Common Intents" page of * Android's developer site: * * @see "http://developer.android.com/guide/components/intents-common.html#Maps" * <p> * Protip: Hold Command on Mac or Control on Windows and click that link to automagically * open the Common Intents page */ private void openLocationInMap() { String addressString = SunshinePreferences.getPreferredWeatherLocation(this); Uri geoLocation = Uri.parse("geo:0,0?q=" + addressString); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Log.d(TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); } }
/** * Returns the elapsed time in milliseconds since the last notification was shown. This is used * as part of our check to see if we should show another notification when the weather is * updated. * * @param context Used to access SharedPreferences as well as use other utility methods * @return Elapsed time in milliseconds since the last notification was shown */ public static long getEllapsedTimeSinceLastNotification(Context context) { long lastNotificationTimeMillis = SunshinePreferences.getLastNotificationTimeInMillis(context); long timeSinceLastNotification = System.currentTimeMillis() - lastNotificationTimeMillis; return timeSinceLastNotification; }
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Activity activity = getActivity(); if (key.equals(getString(R.string.pref_location_key))) { // we've changed the location // Wipe out any potential PlacePicker latlng values so that we can use this text entry. SunshinePreferences.resetLocationCoordinates(activity); } Preference preference = findPreference(key); if (null != preference) { if (!(preference instanceof CheckBoxPreference)) { setPreferenceSummary(preference, sharedPreferences.getString(key, "")); } } } }