Android Open Source - UK-Weather-repo City Management Activity






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 ww  w  .  jav a 2s  . c  o m*/
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.haringeymobile.ukweather.database.CityTable;
import com.haringeymobile.ukweather.database.GeneralDatabaseService;
import com.haringeymobile.ukweather.utils.SharedPrefsHelper;

/** An activity to manage city deletion and renaming. */
public class CityManagementActivity extends ActionBarActivity implements
    CityListFragmentWithUtilityButtons.OnUtilityButtonClickedListener,
    DeleteCityDialog.OnDialogButtonClickedListener {

  public static final String CITY_ID = "city id";
  public static final String CITY_NEW_NAME = "city new name";
  static final String CITY_DELETE_DIALOG_FRAGMENT_TAG = "delete city dialog";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_AppCompat);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_city_management);
  }

  @Override
  public void onCityNameChangeRequested(final int cityId,
      final String originalName) {
    AlertDialog.Builder cityNameChangeDialog = new AlertDialog.Builder(this);

    String dialogTitle = getDialogTitle(originalName);
    cityNameChangeDialog.setTitle(dialogTitle);

    final EditText cityNameEditText = getNewCityNameEditText();
    cityNameChangeDialog.setView(cityNameEditText);

    DialogInterface.OnClickListener dialogOnClickListener = getDialogOnClickListener(
        cityId, originalName, cityNameEditText);
    cityNameChangeDialog.setPositiveButton(android.R.string.ok,
        dialogOnClickListener);

    cityNameChangeDialog.show();
  }

  /**
   * Creates the dialog's title.
   * 
   * @param originalName
   *            current city name
   * @return the dialog title, asking to enter a new name for the city
   */
  private String getDialogTitle(final String originalName) {
    Resources res = getResources();
    String title = res.getString(R.string.dialog_title_rename_city_part_1)
        + originalName
        + res.getString(R.string.dialog_title_rename_city_part_2);
    return title;
  }

  /**
   * Obtains an editable view were the new city name should be typed in.
   * 
   * @return an editable view for the new city name
   */
  private EditText getNewCityNameEditText() {
    final EditText cityNameEditText = new EditText(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
    cityNameEditText.setLayoutParams(lp);
    return cityNameEditText;
  }

  /**
   * Obtain a listener for dialog button clicks.
   * 
   * @param cityId
   *            OpenWeatherMap city ID
   * @param originalName
   *            current city name
   * @param cityNameEditText
   *            an editable view for the new city name
   * @return a dialog button clicks listener
   */
  private DialogInterface.OnClickListener getDialogOnClickListener(
      final int cityId, final String originalName,
      final EditText cityNameEditText) {
    DialogInterface.OnClickListener dialogOnClickListener = new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int whichButton) {
        String newName = cityNameEditText.getText().toString();
        if (newName.length() == 0) {
          showEmptyNameErrorMessage();
          return;
        } else {
          boolean userNameHasBeenChanged = !newName
              .equals(originalName);
          if (userNameHasBeenChanged) {
            renameCity(cityId, newName);
          }
        }
      }

    };
    return dialogOnClickListener;
  }

  /**
   * Displays a message informing that the new name cannot be an empty string.
   */
  private void showEmptyNameErrorMessage() {
    Toast.makeText(this, R.string.message_enter_city_name,
        Toast.LENGTH_SHORT).show();
  }

  /**
   * Renames the specified city.
   * 
   * @param cityId
   *            OpenWeatherMap city ID
   * @param newName
   *            a new name for the city
   */
  private void renameCity(int cityId, String newName) {
    Intent intent = new Intent(this, GeneralDatabaseService.class);
    intent.setAction(GeneralDatabaseService.ACTION_RENAME_CITY);
    intent.putExtra(CITY_ID, cityId);
    intent.putExtra(CITY_NEW_NAME, newName);
    startService(intent);
  }

  @Override
  public void onCityRecordDeletionRequested(int cityId, String cityName) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    DeleteCityDialog dialogFragment = (DeleteCityDialog) fragmentManager
        .findFragmentByTag(CITY_DELETE_DIALOG_FRAGMENT_TAG);
    if (dialogFragment == null) {
      dialogFragment = DeleteCityDialog.newInstance(cityId, cityName);
      dialogFragment.show(fragmentManager,
          CITY_DELETE_DIALOG_FRAGMENT_TAG);
    }
  }

  @Override
  public void onCityRecordDeletionConfirmed(int cityId) {
    updateLastRequestedCityInfo(cityId);
    removeCity(cityId);
  }

  /**
   * Makes note in the SharedPreferences if the city to be deleted is also the
   * last city to be queried for weather information (so the next time an
   * automatic weather information request is made, it won't be necessary to
   * go to the database to check if the city still exists).
   * 
   * @param cityId
   *            OpenWeatherMap city ID
   */
  private void updateLastRequestedCityInfo(int cityId) {
    int lastCityId = SharedPrefsHelper.getCityIdFromSharedPrefs(this);
    if (cityId == lastCityId) {
      SharedPrefsHelper.putCityIdIntoSharedPrefs(this,
          CityTable.CITY_ID_DOES_NOT_EXIST);
    }
  }

  /**
   * Removes the specified city from the database.
   * 
   * @param cityId
   *            OpenWeatherMap ID for the city to be deleted
   */
  private void removeCity(int cityId) {
    Intent intent = new Intent(this, GeneralDatabaseService.class);
    intent.setAction(GeneralDatabaseService.ACTION_DELETE_CITY_RECORDS);
    intent.putExtra(CITY_ID, cityId);
    startService(intent);
  }

}




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