Exemplo n.º 1
0
  @Override
  public boolean onPreferenceChange(Preference preference, Object value) {
    String stringValue = value.toString();

    // are we starting the preference activity?
    if (!mBindingPreference) {
      if (preference.getKey().equals(getString(R.string.pref_location_key))) {
        FetchWeatherTask weatherTask = new FetchWeatherTask(this);
        String location = value.toString();
        weatherTask.execute(location);
      } else {
        // notify code that weather may be impacted
        getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
      }
    }

    if (preference instanceof ListPreference) {
      // For list preferences, look up the correct display value in
      // the preference's 'entries' list (since they have separate labels/values).
      ListPreference listPreference = (ListPreference) preference;
      int prefIndex = listPreference.findIndexOfValue(stringValue);
      if (prefIndex >= 0) {
        preference.setSummary(listPreference.getEntries()[prefIndex]);
      }
    } else {
      // For other preferences, set the summary to the value's simple string representation.
      preference.setSummary(stringValue);
    }
    return true;
  }
Exemplo n.º 2
0
 private void updateWeather() {
   FetchWeatherTask weatherTask = new FetchWeatherTask();
   String location =
       PreferenceManager.getDefaultSharedPreferences(getActivity())
           .getString(
               getString(R.string.pref_location_key), getString(R.string.pref_location_default));
   weatherTask.execute(location);
 }
Exemplo n.º 3
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_refresh) {
      FetchWeatherTask fetch_weather;
      Log.i("options_menu", "Creating task");
      fetch_weather = new FetchWeatherTask();
      fetch_weather.execute("94043");
      return false;
    }
    return super.onOptionsItemSelected(item);
  }
Exemplo n.º 4
0
 private void updateWeather() {
   FetchWeatherTask weatherTask = new FetchWeatherTask();
   /**
    * SharedPreferences can access Preferences everywhere Notice 2x getString(getString(key),
    * getString(value))
    */
   SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
   // Location Setting
   String location =
       sharedPref.getString(
           getString(R.string.pref_location_key), getString(R.string.pref_location_default));
   weatherTask.execute(location);
 }
Exemplo n.º 5
0
 @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();
   if (id == R.id.action_refresh) {
     FetchWeatherTask fetchWeatherTask = new FetchWeatherTask();
     fetchWeatherTask.execute("Zagreb", "CRO");
     return true;
   }
   return super.onOptionsItemSelected(item);
 }
Exemplo n.º 6
0
  private void updateWeather() {
    FetchWeatherTask weatherTask = new FetchWeatherTask();

    // get settings
    SharedPreferences sharedPrefs =
        PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
    // get city / zip code from settings
    String location = sharedPrefs.getString("location", "94043");
    // get unit type
    String unit = sharedPrefs.getString("unit", "metric");

    // run task and get weather for specified location
    weatherTask.execute(location, unit);
  }
  /*
     Students: uncomment testAddLocation after you have written the AddLocation function.
     This test will only run on API level 11 and higher because of a requirement in the
     content provider.
  */
  @TargetApi(11)
  public void testAddLocation() {
    // start from a clean state
    getContext()
        .getContentResolver()
        .delete(
            WeatherContract.LocationEntry.CONTENT_URI,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] {ADD_LOCATION_SETTING});

    FetchWeatherTask fwt = new FetchWeatherTask(getContext());
    long locationId =
        fwt.addLocation(
            ADD_LOCATION_SETTING, ADD_LOCATION_CITY, ADD_LOCATION_LAT, ADD_LOCATION_LON);

    // does addLocation return a valid record ID?
    assertFalse("Error: addLocation returned an invalid ID on insert", locationId == -1);

    // test all this twice
    for (int i = 0; i < 2; i++) {

      // does the ID point to our location?
      Cursor locationCursor =
          getContext()
              .getContentResolver()
              .query(
                  WeatherContract.LocationEntry.CONTENT_URI,
                  new String[] {
                    WeatherContract.LocationEntry._ID,
                    WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING,
                    WeatherContract.LocationEntry.COLUMN_CITY_NAME,
                    WeatherContract.LocationEntry.COLUMN_COORD_LAT,
                    WeatherContract.LocationEntry.COLUMN_COORD_LONG
                  },
                  WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
                  new String[] {ADD_LOCATION_SETTING},
                  null);

      // these match the indices of the projection
      if (locationCursor.moveToFirst()) {
        assertEquals(
            "Error: the queried value of locationId does not match the returned value"
                + "from addLocation",
            locationCursor.getLong(0),
            locationId);
        assertEquals(
            "Error: the queried value of location setting is incorrect",
            locationCursor.getString(1),
            ADD_LOCATION_SETTING);
        assertEquals(
            "Error: the queried value of location city is incorrect",
            locationCursor.getString(2),
            ADD_LOCATION_CITY);
        assertEquals(
            "Error: the queried value of latitude is incorrect",
            locationCursor.getDouble(3),
            ADD_LOCATION_LAT);
        assertEquals(
            "Error: the queried value of longitude is incorrect",
            locationCursor.getDouble(4),
            ADD_LOCATION_LON);
      } else {
        fail("Error: the id you used to query returned an empty cursor");
      }

      // there should be no more records
      assertFalse(
          "Error: there should be only one record returned from a location query",
          locationCursor.moveToNext());

      // add the location again
      long newLocationId =
          fwt.addLocation(
              ADD_LOCATION_SETTING, ADD_LOCATION_CITY, ADD_LOCATION_LAT, ADD_LOCATION_LON);

      assertEquals(
          "Error: inserting a location again should return the same ID", locationId, newLocationId);
    }
    // reset our state back to normal
    getContext()
        .getContentResolver()
        .delete(
            WeatherContract.LocationEntry.CONTENT_URI,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] {ADD_LOCATION_SETTING});

    // clean up the test so that other tests can use the content provider
    getContext()
        .getContentResolver()
        .acquireContentProviderClient(WeatherContract.LocationEntry.CONTENT_URI)
        .getLocalContentProvider()
        .shutdown();
  }