Android Open Source - android-weather-demo-application Main Activity






From Project

Back to project page android-weather-demo-application.

License

The source code is released under:

GNU General Public License

If you think the Android project android-weather-demo-application 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 org.openweathermap.activity;
/*from  w  ww .  j a  va  2  s  . c  om*/
import org.openweathermap.activity.base.BaseActivity;
import org.openweathermap.adapter.ForecastAdapter;
import org.openweathermap.asynctask.GetLastCityAsyncTask;
import org.openweathermap.asynctask.SaveForecastAsyncTask;
import org.openweathermap.asynctask.base.BaseAsyncTask;
import org.openweathermap.asynctask.base.BaseAsyncTask.AsyncCallback;
import org.openweathermap.asynctask.base.BaseResponse;
import org.openweathermap.asynctask.param.SaveForecastParam;
import org.openweathermap.asynctask.response.GetLastCityResponse;
import org.openweathermap.asynctask.response.SaveForecastResponse;
import org.openweathermap.dto.ResultDTO;
import org.openweathermap.dto.base.IDTO;
import org.openweathermap.fragment.ModalDialogFragment;
import org.openweathermap.sql.model.CityModel;
import org.openweathermap.sql.model.WeatherModel;
import org.openweathermap.utils.RESTProvider;
import org.openweathermap.view.ViewPagerIndicatorView;
import org.openweathermap.view.ViewPagerIndicatorView.PagerPositionCallback;
import org.openweathermap.volley.callback.VolleyResponseCallback;
import org.openweathermap.volley.provider.VolleyRequest;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.ink.weather.R;

/**
 * An activity that loads the current weather details from openweathermap.org 
 * when it starts. After the details are available a WeatherDataView is 
 * populated with the information. Swiping left or clicking "Select new city"
 * opens the navigation drawer.
 * @author samkirton
 */
public class MainActivity extends BaseActivity implements OnClickListener, VolleyResponseCallback, AsyncCallback, PagerPositionCallback {
  private TextView uiSelectNewCityTextView;
  private ViewPager uiViewPager;
  private ViewPagerIndicatorView uiViewPagerIndicatorView;
  private TextView uiNoResultsTextView;
  private LinearLayout uiMainHeaderLayout;
  
  private CityModel mCityModel;
  private WeatherModel[] mWeatherModelArray;
  private ForecastAdapter mForecastAdapter;
  private boolean mHasNoResults;
  private GetLastCityAsyncTask mGetLastCityAsyncTask;
  private SaveForecastAsyncTask mSaveForecastAsyncTask;
  
  public CityModel getCityModel() {
    return mCityModel;
  }
  
  public WeatherModel[] getWeatherModelArray() {
    return mWeatherModelArray;
  }
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.useNavigationDrawer();

        uiSelectNewCityTextView = (TextView)findViewById(R.id.activity_main_select_new_city);   
        uiViewPager = (ViewPager)findViewById(R.id.activity_main_forecast_pager_viewpager);
        uiViewPagerIndicatorView = (ViewPagerIndicatorView)findViewById(R.id.activity_main_forecast_view_pager_indicator_view);
        uiNoResultsTextView = (TextView)findViewById(R.id.activity_main_forecast_no_results);
        uiMainHeaderLayout = (LinearLayout)findViewById(R.id.activity_main_header_layout);
        
        // button is not available on tablet resolutions
        if (uiSelectNewCityTextView != null) 
          uiSelectNewCityTextView.setOnClickListener(this);
        
    requestWeatherInformation_Start(null);
    }

  /**
   * Request weather information for the starting city
   */
  public void requestWeatherInformation_Start(String city) {    
    // close the navigation drawer if it is open
    if (getNavigationDrawer() != null && getNavigationDrawer().isDrawerOpen(Gravity.LEFT)) {
      getNavigationDrawer().closeDrawer(Gravity.LEFT);
    }
    
    showDialog(getResources().getString(R.string.activity_loading_weather),ModalDialogFragment.BUTTON_TYPE_BLOCKING);
    
    if (city != null) {
      requestWeatherInformationApi(city);
    } else {
      // get the last viewed city and query 
      mGetLastCityAsyncTask = new GetLastCityAsyncTask(this);
      mGetLastCityAsyncTask.setAsyncCallback(this);
      mGetLastCityAsyncTask.runTask(null);
    }
  }
  
  /**
   * Request weather information from the API using the CityModel to 
   * build the request
   * @param  cityModel  The city to request weather information for
   */
  private void requestWeatherInformationApi(String city) {
    String cityQuery = "London, GB";
    if (city != null) 
      cityQuery = city;  
    
    VolleyRequest volleyRequest = new VolleyRequest(); 
    volleyRequest.setVollyResponseCallback(this);
    volleyRequest.execute(Request.Method.GET, 
      RESTProvider.getWeatherForecastUrl(cityQuery, getBaseContext()),
      null,
      ResultDTO.class
    );
  }
  
  /**
   * Close the loading dialog and initialise the WeatherDataView
   * @param  resultDTO  The weather results json as an easy to use DTO
   */
  private void requestWeatherInformation_Finished(SaveForecastResponse saveForecastResponse) {
    closeDialog();

    mCityModel = saveForecastResponse.getCityModel();
    mWeatherModelArray = saveForecastResponse.getWeatherModelArray();
    
    mForecastAdapter = new ForecastAdapter(getSupportFragmentManager(), mWeatherModelArray.length);
    uiViewPager.setAdapter(mForecastAdapter);
    
    uiViewPagerIndicatorView.init(mWeatherModelArray);
    uiViewPagerIndicatorView.setPagerPositionCallback(this);
    uiViewPager.setOnPageChangeListener(uiViewPagerIndicatorView);
    
    if (mHasNoResults)
      uiNoResultsTextView.setVisibility(View.GONE);
  }
  
  /**
   * Open the navigation drawer
   */
  private void selectNewCity_Click() {
    getNavigationDrawer().openDrawer(Gravity.LEFT);
  }
  
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    
    // remove the "select new city" button when the device changes to landscape orientation
    if (uiMainHeaderLayout != null) {
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        uiMainHeaderLayout.setVisibility(View.GONE);
      } else {
        uiMainHeaderLayout.setVisibility(View.VISIBLE);
      }
    }
  }
  
  @Override
  public void onClick(View v) {
    if (v == uiSelectNewCityTextView) {
      selectNewCity_Click();
    }
  }
  
  @Override
  public void onSetPosition(int position) {
    uiViewPager.setCurrentItem(position);
  }
  
  @Override
  public void onVolleyResponse(IDTO dto) {
    if (dto instanceof ResultDTO) {
      ResultDTO resultDTO = (ResultDTO)dto;
      
      if (resultDTO.getDay() != null && resultDTO.getDay().length > 0) {
        SaveForecastParam saveForecastParam = new SaveForecastParam();
        saveForecastParam.setCityDTO(resultDTO.getCity());
        saveForecastParam.setDayDTOArray(resultDTO.getDay());
        
        mSaveForecastAsyncTask = new SaveForecastAsyncTask(this);
        mSaveForecastAsyncTask.setAsyncCallback(this);
        mSaveForecastAsyncTask.runTask(saveForecastParam);
      } else {
        showDialog(
          getResources().getString(R.string.activity_main_could_not_find_city),
          ModalDialogFragment.BUTTON_TYPE_OK
        );
      }
    }
  }

  @Override
  public void onVolleyError(VolleyError error) {
    if (error.networkResponse == null) {
      showDialog(
        getResources().getString(R.string.activity_main_could_not_connect),
        ModalDialogFragment.BUTTON_TYPE_OK
      );
    } else {
      showDialog(
        getResources().getString(R.string.activity_main_could_not_find_city),
        ModalDialogFragment.BUTTON_TYPE_OK
      );
    }
    
    if (!mHasNoResults) {
      mHasNoResults = true;
      uiNoResultsTextView.setVisibility(View.VISIBLE);
    }
  }
  
  @Override
  public void onAsyncTaskFinished(BaseResponse response, BaseAsyncTask asyncTask) {
    if (asyncTask == mSaveForecastAsyncTask) {
      SaveForecastResponse saveForecastResponse = (SaveForecastResponse)response;
      requestWeatherInformation_Finished(saveForecastResponse);
    } else if (asyncTask == mGetLastCityAsyncTask) {
      GetLastCityResponse getLastCityResponse = (GetLastCityResponse)response;
      
      CityModel cityModel = getLastCityResponse.getCityModel();
      String cityQuery = null;
      if (cityModel != null) 
        cityQuery = cityModel.getName() + ", " + cityModel.getCountry();
      
      requestWeatherInformationApi(cityQuery);
    }
  }
  
  @Override
  protected View getContentLayout() {
    return findViewById(R.id.activity_main_content_layout);
  }
    
  @Override
  protected FrameLayout getNavigationContentLayout() {
    return (FrameLayout)findViewById(R.id.activity_main_navigation_fragment);
  }
  
  @Override
  public DrawerLayout getNavigationDrawer() {
    return (DrawerLayout)findViewById(R.id.main_activity_navigation_drawer);
  }
}




Java Source Code List

org.openweathermap.WeatherApplication.java
org.openweathermap.activity.MainActivity.java
org.openweathermap.activity.base.BaseActivity.java
org.openweathermap.adapter.CityAdapter.java
org.openweathermap.adapter.ForecastAdapter.java
org.openweathermap.asynctask.GetCitiesAsyncTask.java
org.openweathermap.asynctask.GetLastCityAsyncTask.java
org.openweathermap.asynctask.SaveForecastAsyncTask.java
org.openweathermap.asynctask.SearchCitiesAsyncTask.java
org.openweathermap.asynctask.base.BaseAsyncTask.java
org.openweathermap.asynctask.base.BaseResponse.java
org.openweathermap.asynctask.base.IParam.java
org.openweathermap.asynctask.param.SaveForecastParam.java
org.openweathermap.asynctask.param.SearchCitiesParam.java
org.openweathermap.asynctask.response.GetCitiesResponse.java
org.openweathermap.asynctask.response.GetLastCityResponse.java
org.openweathermap.asynctask.response.SaveForecastResponse.java
org.openweathermap.dto.CityDTO.java
org.openweathermap.dto.CoordDTO.java
org.openweathermap.dto.DayDTO.java
org.openweathermap.dto.ResultDTO.java
org.openweathermap.dto.TempDTO.java
org.openweathermap.dto.WeatherDTO.java
org.openweathermap.dto.base.IDTO.java
org.openweathermap.fragment.ForecastFragment.java
org.openweathermap.fragment.ModalDialogFragment.java
org.openweathermap.fragment.SelectCountryFragment.java
org.openweathermap.sql.SQLInitProvider.java
org.openweathermap.sql.model.CityModel.java
org.openweathermap.sql.model.IconModel.java
org.openweathermap.sql.model.WeatherModel.java
org.openweathermap.utils.DateHelper.java
org.openweathermap.utils.RESTProvider.java
org.openweathermap.utils.ReflectionHelper.java
org.openweathermap.utils.ResourceHelper.java
org.openweathermap.utils.StringHelper.java
org.openweathermap.view.CityListItemView.java
org.openweathermap.view.DateDisplayView.java
org.openweathermap.view.ViewPagerFade.java
org.openweathermap.view.ViewPagerIndicatorView.java
org.openweathermap.view.WeatherDataRowView.java
org.openweathermap.volley.callback.VolleyResponseCallback.java
org.openweathermap.volley.provider.VolleyImageLoader.java
org.openweathermap.volley.provider.VolleyRequest.java