package com.neuedu.coolweather;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.neuedu.coolweather.gson.Forecast;
import com.neuedu.coolweather.gson.Weather;
import com.neuedu.coolweather.service.AutoUpdateService;
import com.neuedu.coolweather.util.HttpUtil;
import com.neuedu.coolweather.util.Utility;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class WeatherActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public SwipeRefreshLayout swipeRefresh;
private ScrollView weatherLayout;
private Button navButton;
private TextView titleCity;
private TextView titleUpdateTime;
private TextView degreeText;
private TextView weatherInfoText;
private LinearLayout forecastLayout;
private TextView aqiText;
private TextView pm25Text;
private TextView comfortText;
private TextView carWashText;
private TextView sportText;
private ImageView bingPicImg;
private String mWeatherId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_weather);
// 初始化各控件
bingPicImg = (ImageView) findViewById(R.id.bing_pic_img);
weatherLayout = (ScrollView) findViewById(R.id.weather_layout);
titleCity = (TextView) findViewById(R.id.title_city);
titleUpdateTime = (TextView) findViewById(R.id.title_update_time);
degreeText = (TextView) findViewById(R.id.degree_text);
weatherInfoText = (TextView) findViewById(R.id.weather_info_text);
forecastLayout = (LinearLayout) findViewById(R.id.forecast_layout);
aqiText = (TextView) findViewById(R.id.aqi_text);
pm25Text = (TextView) findViewById(R.id.pm25_text);
comfortText = (TextView) findViewById(R.id.comfort_text);
carWashText = (TextView) findViewById(R.id.car_wash_text);
sportText = (TextView) findViewById(R.id.sport_text);
swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navButton = (Button) findViewById(R.id.nav_button);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String weatherString = prefs.getString("weather", null);
if (weatherString != null) {
// 有缓存时直接解析天气数据
Weather weather = Utility.handleWeatherResponse(weatherString);
mWeatherId = weather.basic.weatherId;
showWeatherInfo(weather);
} else {
// 无缓存时去服务器查询天气
mWeatherId = getIntent().getStringExtra("weather_id");
weatherLayout.setVisibility(View.INVISIBLE);
requestWeather(mWeatherId);
}
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
requestWeather(mWeatherId);
}
});
navButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
String bingPic = prefs.getString("bing_pic", null);
if (bingPic != null) {
Glide.with(this).load(bingPic).into(bingPicImg);
} else {
loadBingPic();
}
}
/**
* 根据天气id请求城市天气信息。
*/
public void requestWeather(final String weatherId) {
String weatherUrl = "http://guolin.tech/api/weather?cityid=" weatherId "&key=28fff3e61be5402eb736dc6fef9e92f7";
HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseText = response.body().string();
final Weather weather = Utility.handleWeatherResponse(responseText);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (weather != null && "ok".equals(weather.status)) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
editor.putString("weather", responseText);
editor.apply();
mWeatherId = weather.basic.weatherId;
showWeatherInfo(weather);
} else {
Toast.makeText(WeatherActivity.this, "获取天气信息失败", Toast.LENGTH_SHORT).show();
}
swipeRefresh.setRefreshing(false);
}
});
}
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(WeatherActivity.this, "获取天气信息失败", Toast.LENGTH_SHORT).show();
swipeRefresh.setRefreshing(false);
}
});
}
});
loadBingPic();
}
/**
* 加载必应每日一图
*/
private void loadBingPic() {
String requestBingPic = "http://guolin.tech/api/bing_pic";
HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
final String bingPic = response.body().string();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
editor.putString("bing_pic", bingPic);
editor.apply();
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(WeatherActivity.this).load(bingPic).into(bingPicImg);
}
});
}
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
});
}
/**
* 处理并展示Weather实体类中的数据。
*/
private void showWeatherInfo(Weather weather) {
String cityName = weather.basic.cityName;
String updateTime = weather.basic.update.updateTime.split(" ")[1];
String degree = weather.now.temperature "℃";
String weatherInfo = weather.now.more.info;
titleCity.setText(cityName);
titleUpdateTime.setText(updateTime);
degreeText.setText(degree);
weatherInfoText.setText(weatherInfo);
forecastLayout.removeAllViews();
for (Forecast forecast : weather.forecastList) {
View view = LayoutInflater.from(this).inflate(R.layout.forecast_item, forecastLayout, false);
TextView dateText = (TextView) view.findViewById(R.id.date_text);
TextView infoText = (TextView) view.findViewById(R.id.info_text);
TextView maxText = (TextView) view.findViewById(R.id.max_text);
TextView minText = (TextView) view.findViewById(R.id.min_text);
dateText.setText(forecast.date);
infoText.setText(forecast.more.info);
maxText.setText(forecast.temperature.max);
minText.setText(forecast.temperature.min);
forecastLayout.addView(view);
}
if (weather.aqi != null) {
aqiText.setText(weather.aqi.city.aqi);
pm25Text.setText(weather.aqi.city.pm25);
}
String comfort = "舒适度:" weather.suggestion.comfort.info;
String carWash = "洗车指数:" weather.suggestion.carWash.info;
String sport = "运行建议:" weather.suggestion.sport.info;
comfortText.setText(comfort);
carWashText.setText(carWash);
sportText.setText(sport);
weatherLayout.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, AutoUpdateService.class);
startService(intent);
}
}
资源文件列表
CoolWeather/.git/HEAD , 23
CoolWeather/.git/config , 130
CoolWeather/.git/description , 73
CoolWeather/.git/hooks/applypatch-msg.sample , 478
CoolWeather/.git/hooks/commit-msg.sample , 896
CoolWeather/.git/hooks/fsmonitor-watchman.sample , 3327
CoolWeather/.git/hooks/post-update.sample , 189
CoolWeather/.git/hooks/pre-applypatch.sample , 424
CoolWeather/.git/hooks/pre-commit.sample , 1642
CoolWeather/.git/hooks/pre-push.sample , 1348
CoolWeather/.git/hooks/pre-rebase.sample , 4898
CoolWeather/.git/hooks/pre-receive.sample , 544
CoolWeather/.git/hooks/prepare-commit-msg.sample , 1492
CoolWeather/.git/hooks/update.sample , 3610
CoolWeather/.git/index , 112
CoolWeather/.git/info/exclude , 240
CoolWeather/.git/objects/94/a25f7f4cb416c083d265558da75d457237d671 , 155
CoolWeather/.gitignore , 127
CoolWeather/.gradle/2.14.1/taskArtifacts/cache.properties , 31
CoolWeather/.gradle/2.14.1/taskArtifacts/cache.properties.lock , 17
CoolWeather/.gradle/2.14.1/taskArtifacts/fileHashes.bin , 149735
CoolWeather/.gradle/2.14.1/taskArtifacts/fileSnapshots.bin , 3430973
CoolWeather/.gradle/2.14.1/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin , 28184
CoolWeather/.gradle/2.14.1/taskArtifacts/taskArtifacts.bin , 107504
CoolWeather/.gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.bin , 33555
CoolWeather/.gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock , 17
CoolWeather/.gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.bin , 23241
CoolWeather/.gradle/2.14.1/tasks/_app_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock , 17
CoolWeather/.idea/compiler.xml , 686
CoolWeather/.idea/copyright/profiles_settings.xml , 76
CoolWeather/.idea/gradle.xml , 722
CoolWeather/.idea/libraries/animated_vector_drawable_26_0_0_alpha1.xml , 688
CoolWeather/.idea/libraries/appcompat_v7_26_0_0_alpha1.xml , 811
CoolWeather/.idea/libraries/core_1_5_1.xml , 585
CoolWeather/.idea/libraries/espresso_core_2_2_2.xml , 635
CoolWeather/.idea/libraries/espresso_idling_resource_2_2_2.xml , 690
CoolWeather/.idea/libraries/exposed_instrumentation_api_publish_0_5.xml , 708
CoolWeather/.idea/libraries/glide_3_7_0.xml , 522
CoolWeather/.idea/libraries/gson_2_7.xml , 497
CoolWeather/.idea/libraries/hamcrest_core_1_3.xml , 526
CoolWeather/.idea/libraries/hamcrest_integration_1_3.xml , 354
CoolWeather/.idea/libraries/hamcrest_library_1_3.xml , 342
CoolWeather/.idea/libraries/javawriter_2_1_1.xml , 330
CoolWeather/.idea/libraries/javax_annotation_api_1_2.xml , 358
CoolWeather/.idea/libraries/javax_inject_1.xml , 324
CoolWeather/.idea/libraries/jsr305_2_0_1.xml , 330
CoolWeather/.idea/libraries/junit_4_12.xml , 477
CoolWeather/.idea/libraries/okhttp_3_4_1.xml , 517
CoolWeather/.idea/libraries/okio_1_9_0.xml , 501
CoolWeather/.idea/libraries/rules_0_5.xml , 729
CoolWeather/.idea/libraries/runner_0_5.xml , 735
CoolWeather/.idea/libraries/support_annotations_26_0_0_alpha1.xml , 546
CoolWeather/.idea/libraries/support_compat_26_0_0_alpha1.xml , 823
CoolWeather/.idea/libraries/support_core_ui_26_0_0_alpha1.xml , 829
CoolWeather/.idea/libraries/support_core_utils_26_0_0_alpha1.xml , 847
CoolWeather/.idea/libraries/support_fragment_26_0_0_alpha1.xml , 835
CoolWeather/.idea/libraries/support_media_compat_26_0_0_alpha1.xml , 859
CoolWeather/.idea/libraries/support_v4_26_0_0_alpha1.xml , 618
CoolWeather/.idea/libraries/support_vector_drawable_26_0_0_alpha1.xml , 683
CoolWeather/.idea/misc.xml , 2644
CoolWeather/.idea/modules.xml , 359
CoolWeather/.idea/runConfigurations.xml , 564
CoolWeather/.idea/vcs.xml , 180
CoolWeather/.idea/workspace.xml , 172529
CoolWeather/CoolWeather.iml , 865
CoolWeather/app/.gitignore , 8
CoolWeather/app/app.iml , 13112
CoolWeather/app/build/generated/source/buildConfig/androidTest/debug/com/neuedu/coolweather/test/BuildConfig.java , 461
CoolWeather/app/build/generated/source/buildConfig/debug/com/neuedu/coolweather/BuildConfig.java , 451
CoolWeather/app/build/generated/source/r/androidTest/debug/android/support/test/espresso/R.java , 334
CoolWeather/app/build/generated/source/r/androidTest/debug/android/support/test/rule/R.java , 330
CoolWeather/app/build/generated/source/r/androidTest/debug/com/neuedu/coolweather/test/R.java , 402
CoolWeather/app/build/generated/source/r/debug/android/support/v7/appcompat/R.java , 100721
CoolWeather/app/build/generated/source/r/debug/com/neuedu/coolweather/R.java , 436302
CoolWeather/app/build/generated/source/r/debug/org/litepal/R.java , 316
CoolWeather/app/build/intermediates/assets/debug/litepal.xml , 337
CoolWeather/app/build/intermediates/blame/res/androidTest/debug/multi/values.json , 933
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-af.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-am.json , 16078
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ar.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-az.json , 15324
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-b+sr+Latn.json , 16396
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-be.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-bg.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-bn.json , 16094
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-bs.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ca.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-cs.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-da.json , 16082
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-de.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-el.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-en-rAU.json , 16258
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-en-rGB.json , 16258
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-en-rIN.json , 16258
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-es-rUS.json , 16266
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-es.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-et.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-eu.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-fa.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-fi.json , 16082
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-fr-rCA.json , 16268
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-fr.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-gl.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-gu.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-h720dp-v13.json , 978
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-hdpi-v4.json , 1038
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-hi.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-hr.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-hu.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-hy.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-in.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-is.json , 16080
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-it.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-iw.json , 16080
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ja.json , 16074
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ka.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-kk.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-km.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-kn.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ko.json , 16070
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ky.json , 16086
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-land.json , 3222
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-large-v4.json , 7088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ldltr-v21.json , 976
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-lo.json , 16082
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-lt.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-lv.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-mk.json , 14568
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ml.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-mn.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-mr.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ms.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-my.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-nb.json , 16078
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ne.json , 16094
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-night-v8.json , 5558
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-nl.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-pa.json , 16082
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-pl.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-port.json , 954
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-pt-rBR.json , 16268
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-pt-rPT.json , 16268
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-pt.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ro.json , 16092
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-ru.json , 16090
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-si.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-sk.json , 16084
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-sl.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-sq.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-sr.json , 16088
CoolWeather/app/build/intermediates/blame/res/debug/multi/values-sv.json , 16086
最多只能显示150条信息!
