/*************************************************************************
Copyright 2014 MagicMod Project
This file is part of MagicMod Weather.
MagicMod Weather is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MagicMod Weather is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MagicMod Weather. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
package com.magicmod.mmweather;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import com.magicmod.mmweather.config.PreferenceUI;
import com.magicmod.mmweather.config.Preferences;
import com.magicmod.mmweather.engine.WeatherEngine;
import com.magicmod.mmweather.engine.WeatherInfo;
import com.magicmod.mmweather.engine.WeatherProvider;
import com.magicmod.mmweather.engine.WeatherResProvider;
import com.magicmod.mmweather.engine.WeatherInfo.DayForecast;
import com.magicmod.mmweather.engine.WeatherProvider.LocationResult;
import com.magicmod.mmweather.utils.Constants;
import com.magicmod.mmweather.utils.ImageUtils;
import com.magicmod.mmweather.utils.widget.RotateImageView;
import com.magicmod.mmweather.utils.widget.CirclePageIndicator;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends FragmentActivity implements OnClickListener {
private static final String TAG = "MainActivity";
private static final boolean DBG = Constants.DEBUG;
/*private static final int LOACTION_OK = 0;
private static final int UPDATE_EXISTS_CITY = 2;
public static final int GET_WEATHER_SCUESS = 3;
public static final int GET_WEATHER_FAIL = 4;*/
//Title
private ImageView mCityManagerBtn, mLocationBtn, mShareBtn;
private TextView mTitleCityName;
private RotateImageView mUpdateProgressBar;
//Today's view
private TextView mCountryTextView;
private TextView mCityTextView, mSyncTimeTextView, mWeatherSourceTextView, mPm25TextView, mAqiDataTextView;
private TextView mTodayWeekTextView, mTodayMonthTextView, mTempTextView, mHumidityTextView, mWeatherConditionTextView, mWindTextView;
private ImageView mWeatherImageView, mAqiImageView;
private View mAqiRootView;
private ViewPager mViewPager;
private List<Fragment> fragments;
private NextDaysWeatherPagerAdapter mWeatherPagerAdapter;
private WeatherEngine mWeatherEngine;
private Context mContext;
//private WeatherApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getApplicationContext();
setContentView(R.layout.activity_main);
final WeatherApplication mApplication = (WeatherApplication) this.getApplication();
mWeatherEngine = mApplication.getWeatherEngine();
initViews();
initWeatherData();
initFragments();
/*Intent intent = new Intent(mContext, WeatherUpdateServices.class);
mContext.startService(intent);*/
}
@Override
protected void onResume() {
super.onResume();
//initWeatherData();
}
private void initViews() {
mCityManagerBtn = (ImageView) findViewById(R.id.title_city_manager);
mCityManagerBtn.setOnClickListener(this);
mLocationBtn = (ImageView) findViewById(R.id.title_location);
mLocationBtn.setOnClickListener(this);
mShareBtn = (ImageView) findViewById(R.id.title_share);
mShareBtn.setOnClickListener(this);
mUpdateProgressBar = (RotateImageView) findViewById(R.id.title_update_progress);
mUpdateProgressBar.setOnClickListener(this);
mTitleCityName = (TextView) findViewById(R.id.title_city_name);
mTitleCityName.setOnClickListener(this);
mCountryTextView = (TextView) findViewById(R.id.country);
mCityTextView = (TextView) findViewById(R.id.city);
mWeatherSourceTextView = (TextView) findViewById(R.id.weather_source_view);
mSyncTimeTextView = (TextView) findViewById(R.id.sync_time);
mPm25TextView = (TextView) findViewById(R.id.pm_data);
mAqiDataTextView = (TextView) findViewById(R.id.pm2_5_quality);
mTodayWeekTextView = (TextView) findViewById(R.id.week_today);
mTodayMonthTextView = (TextView) findViewById(R.id.month_today);
mTempTextView = (TextView) findViewById(R.id.temperature);
mHumidityTextView = (TextView) findViewById(R.id.humidity);
mWeatherConditionTextView = (TextView) findViewById(R.id.weather_condition);
mWindTextView = (TextView) findViewById(R.id.wind);
mWeatherImageView = (ImageView) findViewById(R.id.weather_img);
mAqiImageView = (ImageView) findViewById(R.id.pm2_5_img);
mAqiImageView.setOnClickListener(this);
}
/**
* First data adapter when app opened
* 2014年1月19日
*/
private void initWeatherData() {
if (DBG)
Log.d(TAG, "init weather data");
WeatherInfo info = mWeatherEngine.getCache(); //get weather info from cache
if (info == null) { //open a dialog to let user select their city
if (DBG)
Log.d(TAG, "get cache fail, start the city input dialog");
showCityInputDialog(true);
}
updateWeatherView(info,false);
}
private void initFragments() {
fragments = new ArrayList<Fragment>();
fragments.add(new NextDaysFirstWeatherFragment(mContext, mWeatherEngine.getCache(),
mWeatherEngine.getWeatherProvider().getWeatherResProvider()));
fragments.add(new NextDaysSecondWeatherFragment(mContext, mWeatherEngine.getCache(),
mWeatherEngine.getWeatherProvider().getWeatherResProvider()));
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mWeatherPagerAdapter = new NextDaysWeatherPagerAdapter(getSupportFragmentManager(),
fragments);
mViewPager.setAdapter(mWeatherPagerAdapter);
((CirclePageIndicator) findViewById(R.id.indicator)).setViewPager(mViewPager);
}
private void updateWeatherView(WeatherInfo weatherInfo, boolean refreshFragment) {
if (weatherInfo == null) {
return;
}
WeatherProvider provider = mWeatherEngine.getWeatherProvider();
WeatherResProvider res = provider.getWeatherResProvider();
if (refreshFragment) {
if (fragments.size() > 0) {
((NextDaysFirstWeatherFragment) mWeatherPagerAdapter.getItem(0)).updateWeather(
weatherInfo, res, mContext);
((NextDaysSecondWeatherFragment) mWeatherPagerAdapter.getItem(1)).updateWeather(