Android Open Source - UK-Weather-repo Get Available Cities Task






From Project

Back to project page UK-Weather-repo.

License

The source code is released under:

Apache License

If you think the Android project UK-Weather-repo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.haringeymobile.ukweather;
//from   w w  w  . j av a  2s.co  m
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.gson.Gson;
import com.haringeymobile.ukweather.data.JsonFetcher;
import com.haringeymobile.ukweather.data.objects.CityCurrentWeather;
import com.haringeymobile.ukweather.data.objects.Coordinates;
import com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery;
import com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar;
import com.haringeymobile.ukweather.utils.MiscMethods;

/** A task to process the city search URL and deal with the obtained result. */
class GetAvailableCitiesTask extends
    AsyncTaskWithProgressBar<URL, Void, SearchResponseForFindQuery> {

  /** A listener for search response retrieval completion. */
  public interface OnCitySearchResponseRetrievedListener {

    /**
     * Reacts to the obtained city search result.
     * 
     * @param searchResponseForFindQuery
     *            an object corresponding to the JSON string provided by the
     *            Open Weather Map 'find cities' query
     */
    public void onSearchResponseForFindQueryRetrieved(
        SearchResponseForFindQuery searchResponseForFindQuery);

  }

  private static final String CITY_SEARCH_RESULTS_FRAGMENT_TAG = "search results";
  private static final String NO_CITIES_FOUND_DIALOG_FRAGMENT_TAG = "no cities fragment";

  private final FragmentActivity activity;

  /**
   * @param activity
   *            an activity from which this task is started
   */
  GetAvailableCitiesTask(FragmentActivity activity) {
    this.activity = activity;
    setContext(activity);
  }

  @Override
  protected SearchResponseForFindQuery doInBackground(URL... params) {
    String jsonString = null;
    try {
      jsonString = new JsonFetcher().getJsonString(params[0]);
    } catch (IOException e) {
      MiscMethods
          .log("IOException in SearchResponseForFindQuery doInBackground()");
      e.printStackTrace();
      return null;
    }
    return jsonString == null ? null : new Gson().fromJson(jsonString,
        SearchResponseForFindQuery.class);
  }

  @Override
  protected void onPostExecute(SearchResponseForFindQuery result) {
    super.onPostExecute(result);
    if (result == null
        || result.getCode() != JsonFetcher.HTTP_STATUS_CODE_OK) {
      displayErrorMessage();
    } else if (result.getCount() < 1) {
      showNoCitiesFoundAlertDialog();
    } else {
      dealWithSearchResponseForFindCitiesQuery(result);
    }
  }

  /**
   * Displays the network connection error message.
   */
  private void displayErrorMessage() {
    if (activity != null) {
      Toast.makeText(activity, R.string.error_message, Toast.LENGTH_SHORT)
          .show();
    }
  }

  /**
   * Shows an alert dialog informing that no cities were found for the query.
   */
  void showNoCitiesFoundAlertDialog() {
    DialogFragment dialogFragment = new DialogFragment() {

      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
            getActivity());
        OnClickListener onClickListener = getDialogOnClickListener();
        builder.setTitle(R.string.dialog_title_no_cities_found)
            .setMessage(R.string.message_no_cities_found)
            .setPositiveButton(android.R.string.ok, onClickListener);
        return builder.create();
      }

      private OnClickListener getDialogOnClickListener() {
        OnClickListener onClickListener = new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int id) {
            dismiss();
          }

        };
        return onClickListener;
      }

    };

    dialogFragment.show(activity.getSupportFragmentManager(),
        NO_CITIES_FOUND_DIALOG_FRAGMENT_TAG);
  }

  /**
   * Handles the city search response.
   * 
   * @param result
   *            a city search response, containing found cities and related
   *            data
   */
  private void dealWithSearchResponseForFindCitiesQuery(
      SearchResponseForFindQuery result) {
    informActivityAboutObtainedSearchResponse(result);
    showDialogWithSearchResults(result);
  }

  /**
   * Passes the city search response to the activity that started this task
   * for further processing.
   * 
   * @param result
   *            a city search response, containing found cities and related
   *            data
   */
  private void informActivityAboutObtainedSearchResponse(
      SearchResponseForFindQuery result) {
    try {
      OnCitySearchResponseRetrievedListener listener = (OnCitySearchResponseRetrievedListener) activity;
      listener.onSearchResponseForFindQueryRetrieved(result);
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
          + " must implement OnCitySearchResponseRetrievedListener");
    }
  }

  /**
   * Creates and shows a dialog with the list of found city names (so the user
   * can choose one of them).
   * 
   * @param result
   *            a city search response, containing found cities and related
   *            data
   */
  private void showDialogWithSearchResults(SearchResponseForFindQuery result) {
    ArrayList<String> foundCityNames = getFoundCityNames(result);
    CitySearchResultsDialog citySearchResultsDialog = CitySearchResultsDialog
        .newInstance(foundCityNames);
    citySearchResultsDialog.show(activity.getSupportFragmentManager(),
        CITY_SEARCH_RESULTS_FRAGMENT_TAG);
  }

  /**
   * Obtains a list of city names satisfying the user's search query.
   * 
   * @param result
   *            a city search response, containing found cities and related
   *            data
   * @return a list of city names (with coordinates)
   */
  private ArrayList<String> getFoundCityNames(
      SearchResponseForFindQuery result) {
    ArrayList<String> foundCityNames = new ArrayList<>();
    List<CityCurrentWeather> cities = result.getCities();
    for (CityCurrentWeather city : cities) {
      String cityName = getCityName(city);
      foundCityNames.add(cityName);
    }
    return foundCityNames;
  }

  /**
   * Obtains the city name to be displayed in the found city list.
   * 
   * @param cityCurrentWeather
   *            weather and other information about the city
   * @return a city name (with latitude and longitude)
   */
  private String getCityName(CityCurrentWeather cityCurrentWeather) {
    Coordinates cityCoordinates = cityCurrentWeather.getCoordinates();
    String cityName = cityCurrentWeather.getCityName() + ", "
        + cityCurrentWeather.getSystemParameters().getCountry() + "\n("
        + MiscMethods.formatDoubleValue(cityCoordinates.getLatitude())
        + ", "
        + MiscMethods.formatDoubleValue(cityCoordinates.getLongitude())
        + ")";
    return cityName;
  }

}




Java Source Code List

com.astuetz.PagerSlidingTabStrip.java
com.haringeymobile.ukweather.AboutActivity.java
com.haringeymobile.ukweather.BaseCityCursorAdapter.java
com.haringeymobile.ukweather.BaseCityListFragmentWithButtons.java
com.haringeymobile.ukweather.CityListFragmentWithUtilityButtons.java
com.haringeymobile.ukweather.CityListFragmentWithWeatherButtons.java
com.haringeymobile.ukweather.CityManagementActivity.java
com.haringeymobile.ukweather.CitySearchResultsDialog.java
com.haringeymobile.ukweather.CityUtilitiesCursorAdapter.java
com.haringeymobile.ukweather.CityWeatherCursorAdapter.java
com.haringeymobile.ukweather.DeleteCityDialog.java
com.haringeymobile.ukweather.GetAvailableCitiesTask.java
com.haringeymobile.ukweather.MainActivity.java
com.haringeymobile.ukweather.SettingsActivityPreHoneycomb.java
com.haringeymobile.ukweather.SettingsActivity.java
com.haringeymobile.ukweather.SettingsFragment.java
com.haringeymobile.ukweather.WeatherCurrentInfoFragment.java
com.haringeymobile.ukweather.WeatherDailyWeatherForecastChildFragment.java
com.haringeymobile.ukweather.WeatherForecastParentFragment.java
com.haringeymobile.ukweather.WeatherInfoActivity.java
com.haringeymobile.ukweather.WeatherInfoFragment.java
com.haringeymobile.ukweather.WeatherInfoType.java
com.haringeymobile.ukweather.WeatherThreeHourlyForecastChildFragment.java
com.haringeymobile.ukweather.WorkerFragmentToRetrieveJsonString.java
com.haringeymobile.ukweather.data.InitialCity.java
com.haringeymobile.ukweather.data.JsonFetcher.java
com.haringeymobile.ukweather.data.OpenWeatherMapUrl.java
com.haringeymobile.ukweather.data.objects.CityCurrentWeather.java
com.haringeymobile.ukweather.data.objects.CityDailyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.CityInfo.java
com.haringeymobile.ukweather.data.objects.CityThreeHourlyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.Clouds.java
com.haringeymobile.ukweather.data.objects.Coordinates.java
com.haringeymobile.ukweather.data.objects.NumericParameters.java
com.haringeymobile.ukweather.data.objects.Rain.java
com.haringeymobile.ukweather.data.objects.SearchResponseForDailyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForThreeHourlyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SystemParameters.java
com.haringeymobile.ukweather.data.objects.TemperatureScale.java
com.haringeymobile.ukweather.data.objects.Temperature.java
com.haringeymobile.ukweather.data.objects.WeatherInformation.java
com.haringeymobile.ukweather.data.objects.Weather.java
com.haringeymobile.ukweather.data.objects.WindSpeedMeasurementUnit.java
com.haringeymobile.ukweather.data.objects.Wind.java
com.haringeymobile.ukweather.database.CityTable.java
com.haringeymobile.ukweather.database.DatabaseHelper.java
com.haringeymobile.ukweather.database.GeneralDatabaseService.java
com.haringeymobile.ukweather.database.SqlOperation.java
com.haringeymobile.ukweather.database.WeatherContentProvider.java
com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar.java
com.haringeymobile.ukweather.utils.GlobalConstants.java
com.haringeymobile.ukweather.utils.MiscMethods.java
com.haringeymobile.ukweather.utils.SharedPrefsHelper.java