version:2.1.4
bugfixes: update:优化天气,自动接听和联系人修改增加弹窗
This commit is contained in:
215
app/src/main/java/com/vscool/os/manager/WeatherManager.java
Normal file
215
app/src/main/java/com/vscool/os/manager/WeatherManager.java
Normal file
@@ -0,0 +1,215 @@
|
||||
package com.vscool.os.manager;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.qweather.sdk.bean.base.Code;
|
||||
import com.qweather.sdk.bean.base.Lang;
|
||||
import com.qweather.sdk.bean.base.Unit;
|
||||
import com.qweather.sdk.bean.weather.WeatherDailyBean;
|
||||
import com.qweather.sdk.bean.weather.WeatherHourlyBean;
|
||||
import com.qweather.sdk.bean.weather.WeatherNowBean;
|
||||
import com.qweather.sdk.view.QWeather;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.vscool.os.config.CommonConfig;
|
||||
import com.vscool.os.gson.GsonUtils;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
public class WeatherManager {
|
||||
private static final String TAG = "WeatherManager";
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
private Context mContext;
|
||||
private Gson mGson;
|
||||
|
||||
|
||||
/**
|
||||
* 上次获取实时天气时间戳 3小时更新
|
||||
*/
|
||||
private static final String GET_WEATHER_NOW_LAST_TIME_KEY = "get_weather_now_last_time_key";
|
||||
|
||||
/**
|
||||
* 上次获取24小时天气时间戳 8小时更新
|
||||
*/
|
||||
private static final String GET_WEATHER_24_HOURLY_LAST_TIME_KEY = "get_weather_24_hourly_last_time_key";
|
||||
|
||||
/**
|
||||
* 上次获取7天天气时间戳 24小时更新
|
||||
*/
|
||||
private static final String GET_WEATHER_7_DAY_LAST_TIME_KEY = "get_weather_7_day_last_time_key";
|
||||
|
||||
/*实时天气缓存*/
|
||||
private static final String CACHE_WEATHER_NOW_BEAN_KEY = "cache_weather_now_bean_key";
|
||||
/*24小时天气缓存*/
|
||||
private static final String CACHE_WEATHER_HOURLY_BEAN_KEY = "cache_weather_hourly_bean_key";
|
||||
/*7天天气缓存*/
|
||||
private static final String CACHE_WEATHER_DAILY_BEAN_KEY = "cache_weather_daily_bean_key";
|
||||
|
||||
private static final long MINUTE_1_INTERVAL_MS = 60 * 1000; // 1分钟毫秒数
|
||||
private static final long HOURLY_3_INTERVAL_MS = 3 * 60 * 60 * 1000; // 3小时毫秒数
|
||||
private static final long HOURLY_8_INTERVAL_MS = 8 * 60 * 60 * 1000; // 8小时毫秒数
|
||||
private static final long HOURLY_24_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24小时毫秒数
|
||||
|
||||
public WeatherManager(Context context) {
|
||||
this.mContext = context;
|
||||
mGson = new Gson();
|
||||
}
|
||||
|
||||
public interface WeatherNowCallback {
|
||||
void onWeatherNow(WeatherNowBean.NowBaseBean nowBaseBean);
|
||||
}
|
||||
|
||||
public synchronized void getWeatherNow(String location, WeatherNowCallback weatherNowCallback) {
|
||||
long lastTime = mMMKV.decodeLong(GET_WEATHER_NOW_LAST_TIME_KEY, 0);
|
||||
Log.e(TAG, "getWeatherNow: lastTime = " + lastTime);
|
||||
String jsonString = mMMKV.decodeString(CACHE_WEATHER_NOW_BEAN_KEY, "");
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
if (lastTime == 0 || TextUtils.isEmpty(jsonString) || time - lastTime > HOURLY_3_INTERVAL_MS) {
|
||||
Log.e(TAG, "getWeatherNow: get data");
|
||||
QWeather.getWeatherNow(mContext, location, Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
mMMKV.encode(GET_WEATHER_NOW_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeatherNow", "onError: " + e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(WeatherNowBean weatherBean) {
|
||||
mMMKV.encode(GET_WEATHER_NOW_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeatherNow", "onSuccess: ");
|
||||
|
||||
//先判断返回的status是否正确,当status正确时获取数据,若status不正确,可查看status对应的Code值找到原因
|
||||
if (Code.OK == weatherBean.getCode()) {
|
||||
WeatherNowBean.NowBaseBean now = weatherBean.getNow();
|
||||
mMMKV.encode(CACHE_WEATHER_NOW_BEAN_KEY, GsonUtils.toJSONString(now));
|
||||
if (weatherNowCallback != null) weatherNowCallback.onWeatherNow(now);
|
||||
} else {
|
||||
//在此查看返回数据失败的原因
|
||||
Code code = weatherBean.getCode();
|
||||
Log.d("getWeatherNow", "onSuccess: failed code: " + code);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Log.e(TAG, "getWeatherNow: get cache");
|
||||
try {
|
||||
WeatherNowBean.NowBaseBean now = GsonUtils.toJavaObject(jsonString, WeatherNowBean.NowBaseBean.class);
|
||||
if (weatherNowCallback != null) weatherNowCallback.onWeatherNow(now);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getWeatherNow: " + e.getMessage());
|
||||
mMMKV.remove(CACHE_WEATHER_NOW_BEAN_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface WeatherHourlyCallback {
|
||||
void onWeatherHourly(List<WeatherHourlyBean.HourlyBean> hourlyBeanList);
|
||||
}
|
||||
|
||||
public synchronized void getWeather24Hourly(String location, WeatherHourlyCallback weatherHourlyCallback) {
|
||||
long lastTime = mMMKV.decodeLong(GET_WEATHER_24_HOURLY_LAST_TIME_KEY, 0);
|
||||
Log.e(TAG, "getWeather24Hourly: lastTime = " + lastTime);
|
||||
String jsonString = mMMKV.decodeString(CACHE_WEATHER_HOURLY_BEAN_KEY, "");
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
if (lastTime == 0 || TextUtils.isEmpty(jsonString) || time - lastTime > HOURLY_8_INTERVAL_MS) {
|
||||
Log.e(TAG, "getWeather24Hourly: get data");
|
||||
QWeather.getWeather24Hourly(mContext, location, new QWeather.OnResultWeatherHourlyListener() {
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
mMMKV.encode(GET_WEATHER_24_HOURLY_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeather24Hourly", "onError: " + throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(WeatherHourlyBean weatherHourlyBean) {
|
||||
mMMKV.encode(GET_WEATHER_24_HOURLY_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeather24Hourly", "onSuccess: ");
|
||||
|
||||
if (Code.OK == weatherHourlyBean.getCode()) {
|
||||
List<WeatherHourlyBean.HourlyBean> hourly = weatherHourlyBean.getHourly();
|
||||
mMMKV.encode(CACHE_WEATHER_HOURLY_BEAN_KEY, GsonUtils.toJSONString(hourly));
|
||||
if (weatherHourlyCallback != null)
|
||||
weatherHourlyCallback.onWeatherHourly(hourly);
|
||||
} else {
|
||||
//在此查看返回数据失败的原因
|
||||
Code code = weatherHourlyBean.getCode();
|
||||
Log.e("getWeather24Hourly", "failed code: " + code);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
Log.e(TAG, "getWeather24Hourly: get cache");
|
||||
try {
|
||||
Type type = new TypeToken<List<WeatherHourlyBean.HourlyBean>>() {
|
||||
}.getType();
|
||||
List<WeatherHourlyBean.HourlyBean> hourly = mGson.fromJson(jsonString, type);
|
||||
if (weatherHourlyCallback != null) weatherHourlyCallback.onWeatherHourly(hourly);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getWeather24Hourly: " + e.getMessage());
|
||||
mMMKV.remove(CACHE_WEATHER_HOURLY_BEAN_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface WeatherDailyCallback {
|
||||
void onWeatherDaily(List<WeatherDailyBean.DailyBean> dailyBeanList);
|
||||
}
|
||||
|
||||
public synchronized void getWeather7Day(boolean refresh, String location, WeatherDailyCallback weatherDailyCallback) {
|
||||
Log.e(TAG, "getWeather7Day: refresh = " + refresh);
|
||||
long lastTime = mMMKV.decodeLong(GET_WEATHER_7_DAY_LAST_TIME_KEY, 0);
|
||||
Log.e(TAG, "getWeather7Day: lastTime = " + lastTime);
|
||||
String jsonString = mMMKV.decodeString(CACHE_WEATHER_DAILY_BEAN_KEY, "");
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
if (lastTime == 0 || TextUtils.isEmpty(jsonString) || refresh ? time - lastTime > MINUTE_1_INTERVAL_MS : time - lastTime > HOURLY_24_INTERVAL_MS) {
|
||||
Log.e(TAG, "getWeather7Day: get data");
|
||||
QWeather.getWeather7D(mContext, location, new QWeather.OnResultWeatherDailyListener() {
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
mMMKV.encode(GET_WEATHER_7_DAY_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeather7Day", "onError: " + throwable.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(WeatherDailyBean weatherDailyBean) {
|
||||
mMMKV.encode(GET_WEATHER_7_DAY_LAST_TIME_KEY, System.currentTimeMillis());
|
||||
Log.e("getWeather7Day", "onSuccess: ");
|
||||
|
||||
if (Code.OK == weatherDailyBean.getCode()) {
|
||||
List<WeatherDailyBean.DailyBean> dailyBeans = weatherDailyBean.getDaily();
|
||||
mMMKV.encode(CACHE_WEATHER_DAILY_BEAN_KEY, GsonUtils.toJSONString(dailyBeans));
|
||||
if (weatherDailyCallback != null)
|
||||
weatherDailyCallback.onWeatherDaily(dailyBeans);
|
||||
} else {
|
||||
//在此查看返回数据失败的原因
|
||||
Code code = weatherDailyBean.getCode();
|
||||
Log.e("getWeather7Day", "failed code: " + code);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
Log.e(TAG, "getWeather7Day: get cache");
|
||||
try {
|
||||
Type type = new TypeToken<List<WeatherDailyBean.DailyBean>>() {
|
||||
}.getType();
|
||||
List<WeatherDailyBean.DailyBean> dailyBeans = mGson.fromJson(jsonString, type);
|
||||
if (weatherDailyCallback != null) weatherDailyCallback.onWeatherDaily(dailyBeans);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getWeather7Day: " + e.getMessage());
|
||||
mMMKV.remove(CACHE_WEATHER_DAILY_BEAN_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user