From: kfl_614 Date: Mon, 30 Mar 2020 11:16:44 +0000 (+0800) Subject: 1.增加对时区和时间变化处理,时区改变强制去加载天气,每隔1天加载一次。 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=18f90285a2150e014fbff28f147d8de986c1f833;p=icworkservice.git 1.增加对时区和时间变化处理,时区改变强制去加载天气,每隔1天加载一次。 2.获取三天的天气 --- diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 7ca457d..d8ffe0b 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -152,6 +152,15 @@ public protected ; -keep public class com.android.support.b -keep public class rx.internal.util +#androidx 混淆 +-keep class com.google.android.material.** {*;} +-keep class androidx.** {*;} +-keep public class * extends androidx.** +-keep interface androidx.** {*;} +-dontwarn com.google.android.material.** +-dontnote com.google.android.material.** +-dontwarn androidx.** + #event bus -keepattributes *Annotation* -keepclassmembers class * { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 41cf9f9..d52c6fa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,11 +40,12 @@ android:name="com.amap.api.v2.apikey" android:value="80c129088f7bbe251a846d645ee8360a" /> + + > diff --git a/app/src/main/aidl/com/ic/aidl/weather/IWeatherAidlInterface.aidl b/app/src/main/aidl/com/ic/aidl/weather/IWeatherAidlInterface.aidl index 219c69e..284d38e 100644 --- a/app/src/main/aidl/com/ic/aidl/weather/IWeatherAidlInterface.aidl +++ b/app/src/main/aidl/com/ic/aidl/weather/IWeatherAidlInterface.aidl @@ -9,6 +9,6 @@ interface IWeatherAidlInterface { * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ - void updateCurrWeather(in WeatherParcelable weather); + void updateCurrWeather(in List weather); } diff --git a/app/src/main/java/com/ic/work/WorkService.java b/app/src/main/java/com/ic/work/WorkService.java index d51da3b..75f5ca4 100644 --- a/app/src/main/java/com/ic/work/WorkService.java +++ b/app/src/main/java/com/ic/work/WorkService.java @@ -5,9 +5,17 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; +import android.os.Build; import android.os.IBinder; import android.text.TextUtils; +import androidx.annotation.RequiresApi; + import com.ic.work.event.BloodCheckResultEvent; import com.ic.work.event.BloodCheckTipsEvent; import com.ic.work.event.LocationResultEvent; @@ -22,11 +30,14 @@ import com.ic.work.process.PedProcess; import com.ic.work.process.WeatherProcess; import com.ic.work.utils.IcLog; import com.ic.work.utils.SPUtils; +import com.ic.work.weather.LocationState; import com.ic.work.weather.WeatherHelper; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import org.threeten.bp.LocalDateTime; +import org.threeten.bp.ZoneId; public class WorkService extends Service { @@ -45,12 +56,12 @@ public class WorkService extends Service { private WorkServiceApp mApp; - private MessengerCoreService coreService; + private MessengerCoreService coreService; @Override public IBinder onBind(Intent intent) { if (intent != null) { - IcLog.i(TAG,intent.getPackage()); + IcLog.i(TAG, intent.getPackage()); switch (intent.getPackage()) { case "com.ic.weather": //天气 @@ -68,6 +79,7 @@ public class WorkService extends Service { return null; } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onCreate() { super.onCreate(); @@ -85,13 +97,13 @@ public class WorkService extends Service { EventBus.getDefault().register(this); - mWeatherHelper.startLocation(getApplicationContext()); - registerBroadcast(); coreService = new MessengerCoreService(); coreService.bindCoreService(getApplicationContext()); + mWeatherHelper.startLocation(getApplicationContext(),false); + IcLog.i(TAG, "work service is start"); } @@ -112,15 +124,15 @@ public class WorkService extends Service { unregisterReceiver(mIntentReceiver); - if(coreService != null){ + if (coreService != null) { coreService.onUnBindCoreService(getApplicationContext()); - coreService=null; + coreService = null; } } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(LocationResultEvent event) { - mWeatherHelper.queryWeatherServerInfo(mApp.getWeatherRepository(), event.getmCity()); + mWeatherHelper.queryWeatherServerInfo(mApp.getWeatherRepository(), event.getLocation()); } @Subscribe(threadMode = ThreadMode.MAIN) @@ -138,8 +150,8 @@ public class WorkService extends Service { event.getLow(), event.getHeart()); } - if(coreService != null){ - coreService.sendBloodValueMessage(event.getHigh(),event.getLow(),event.getHeart()); + if (coreService != null) { + coreService.sendBloodValueMessage(event.getHigh(), event.getLow(), event.getHeart()); } //发送一次广播给 心率 launcher @@ -194,13 +206,16 @@ public class WorkService extends Service { @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(WeatherAgainEvent event) { if (mWeatherHelper != null) { - mWeatherHelper.startLocation(getApplicationContext()); + mWeatherHelper.startLocation(getApplicationContext(),false); } } public void registerBroadcast() { IntentFilter filter = new IntentFilter(); filter.addAction(GET_PED_ACTION); + filter.addAction(Intent.ACTION_TIME_TICK); + filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); + filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mIntentReceiver, filter); } @@ -218,7 +233,7 @@ public class WorkService extends Service { sendBroadcast(pedIntent); } - private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { + private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { @@ -230,6 +245,12 @@ public class WorkService extends Service { case GET_PED_ACTION: pedProcess.startPedRunGetCount(); break; + case Intent.ACTION_TIMEZONE_CHANGED: + mWeatherHelper.startLocation(getApplicationContext(),true); + break; + case Intent.ACTION_TIME_TICK: + mWeatherHelper.startLocation(getApplicationContext(),false); + break; } } } diff --git a/app/src/main/java/com/ic/work/db/AppDatabase.java b/app/src/main/java/com/ic/work/db/AppDatabase.java index 3ada192..6b0a320 100644 --- a/app/src/main/java/com/ic/work/db/AppDatabase.java +++ b/app/src/main/java/com/ic/work/db/AppDatabase.java @@ -12,7 +12,7 @@ import com.ic.work.db.dao.WeatherDao; import com.ic.work.db.entity.PedEntity; import com.ic.work.db.entity.WeatherEntity; -@Database(entities = {PedEntity.class, WeatherEntity.class}, version = 2,exportSchema = false) +@Database(entities = {PedEntity.class, WeatherEntity.class}, version = 3,exportSchema = false) @TypeConverters(TiviTypeConverters.class) public abstract class AppDatabase extends RoomDatabase { @@ -35,6 +35,6 @@ public abstract class AppDatabase extends RoomDatabase { } private static AppDatabase buildDatabase(final Context appContext) { - return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME).build(); + return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME).fallbackToDestructiveMigration().build(); } } diff --git a/app/src/main/java/com/ic/work/db/WeatherInjection.java b/app/src/main/java/com/ic/work/db/WeatherInjection.java index a990d86..5447e42 100644 --- a/app/src/main/java/com/ic/work/db/WeatherInjection.java +++ b/app/src/main/java/com/ic/work/db/WeatherInjection.java @@ -33,15 +33,15 @@ public class WeatherInjection { return mDatabase.loadWeatherDao(); } - public List queryDBWeatherDate(LocalDate date) { - return getWeatherDao().loadAllDateWeatherList(date); + public List queryDBWeatherDate() { + return getWeatherDao().loadAllDateWeatherList(); } public void deleteDBWeatherDate() { getWeatherDao().deleteWeatherList(); } - public void insertWeatherEntity(WeatherEntity entity){ + public void insertWeatherEntity(List entity){ getWeatherDao().insertWeatherEntity(entity); } } diff --git a/app/src/main/java/com/ic/work/db/dao/WeatherDao.java b/app/src/main/java/com/ic/work/db/dao/WeatherDao.java index 7be1051..6e2968b 100644 --- a/app/src/main/java/com/ic/work/db/dao/WeatherDao.java +++ b/app/src/main/java/com/ic/work/db/dao/WeatherDao.java @@ -2,24 +2,22 @@ package com.ic.work.db.dao; import androidx.room.Dao; import androidx.room.Insert; +import androidx.room.OnConflictStrategy; import androidx.room.Query; import com.ic.work.db.entity.WeatherEntity; -import org.threeten.bp.LocalDate; -import org.threeten.bp.OffsetDateTime; - import java.util.List; @Dao public interface WeatherDao { - @Query("SELECT * FROM icWeatherDb WHERE updateTime= :currDate") - List loadAllDateWeatherList(LocalDate currDate); + @Query("SELECT * FROM icWeatherDb") + List loadAllDateWeatherList(); @Query("DELETE from icWeatherDb") void deleteWeatherList(); - @Insert - void insertWeatherEntity(WeatherEntity entity); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertWeatherEntity(List entity); } diff --git a/app/src/main/java/com/ic/work/db/entity/WeatherEntity.java b/app/src/main/java/com/ic/work/db/entity/WeatherEntity.java index fbb1993..4ebdb1a 100644 --- a/app/src/main/java/com/ic/work/db/entity/WeatherEntity.java +++ b/app/src/main/java/com/ic/work/db/entity/WeatherEntity.java @@ -4,7 +4,6 @@ import androidx.room.Entity; import androidx.room.Index; import androidx.room.PrimaryKey; -import org.threeten.bp.LocalDate; @Entity(tableName = "icWeatherDb", indices = {@Index("id")}) @@ -18,7 +17,7 @@ public class WeatherEntity { private String figureNight; private String temperatureDay; // 白天温度 或 最低温度 private String temperatureNight; // 晚上温度 或 最低温度 - private LocalDate updateTime; // 更新时间 + private String updateTime; // 更新时间 public int getId() { return id; @@ -84,11 +83,11 @@ public class WeatherEntity { this.temperatureNight = temperatureNight; } - public LocalDate getUpdateTime() { + public String getUpdateTime() { return updateTime; } - public void setUpdateTime(LocalDate updateTime) { + public void setUpdateTime(String updateTime) { this.updateTime = updateTime; } } diff --git a/app/src/main/java/com/ic/work/event/LocationResultEvent.java b/app/src/main/java/com/ic/work/event/LocationResultEvent.java index 9b61e33..ea313fc 100644 --- a/app/src/main/java/com/ic/work/event/LocationResultEvent.java +++ b/app/src/main/java/com/ic/work/event/LocationResultEvent.java @@ -1,14 +1,16 @@ package com.ic.work.event; +import com.ic.work.weather.LocationState; + public class LocationResultEvent { - private String mCity; + private LocationState location; - public LocationResultEvent(String city) { - this.mCity = city; + public LocationResultEvent(LocationState location) { + this.location = location; } - public String getmCity() { - return mCity; + public LocationState getLocation() { + return location; } } diff --git a/app/src/main/java/com/ic/work/event/WeatherResultEvent.java b/app/src/main/java/com/ic/work/event/WeatherResultEvent.java index b00277d..03df6aa 100644 --- a/app/src/main/java/com/ic/work/event/WeatherResultEvent.java +++ b/app/src/main/java/com/ic/work/event/WeatherResultEvent.java @@ -2,14 +2,16 @@ package com.ic.work.event; import com.ic.aidl.weather.WeatherParcelable; +import java.util.List; + public class WeatherResultEvent { - private WeatherParcelable parcelable; + private List parcelable; - public WeatherResultEvent(WeatherParcelable parcelable) { + public WeatherResultEvent(List parcelable) { this.parcelable = parcelable; } - public WeatherParcelable getParcelable() { + public List getParcelable() { return parcelable; } } diff --git a/app/src/main/java/com/ic/work/process/WeatherProcess.java b/app/src/main/java/com/ic/work/process/WeatherProcess.java index c05e276..3931720 100644 --- a/app/src/main/java/com/ic/work/process/WeatherProcess.java +++ b/app/src/main/java/com/ic/work/process/WeatherProcess.java @@ -12,6 +12,8 @@ import com.ic.work.WorkServiceApp; import com.ic.work.utils.IcLog; import com.ic.work.weather.WeatherDBRun; +import java.util.List; + public class WeatherProcess { private static final String TAG = "WeatherProcess"; @@ -49,7 +51,7 @@ public class WeatherProcess { } }; - public void sendCurrWeather(WeatherParcelable parcelable) { + public void sendCurrWeather(List parcelable) { int N = 0; try { N = mCallbacks.beginBroadcast(); diff --git a/app/src/main/java/com/ic/work/weather/LocationHelper.java b/app/src/main/java/com/ic/work/weather/LocationHelper.java index 3d5765a..91394e4 100644 --- a/app/src/main/java/com/ic/work/weather/LocationHelper.java +++ b/app/src/main/java/com/ic/work/weather/LocationHelper.java @@ -9,10 +9,12 @@ import com.ic.work.event.LocationResultEvent; import com.ic.work.utils.IcLog; import org.greenrobot.eventbus.EventBus; +import org.threeten.bp.LocalDateTime; +import org.threeten.bp.ZoneId; public class LocationHelper { - private static final String TAG ="LocationHelper"; + private static final String TAG = "LocationHelper"; public AMapLocationClient mLocationClient; @@ -20,16 +22,18 @@ public class LocationHelper { mLocationClient = new AMapLocationClient(context); //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 AMapLocationListener mLocationListener = aMapLocation -> { + LocalDateTime time = LocalDateTime.now(ZoneId.systemDefault()); if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { - IcLog.i(TAG,"Loaction City:"+aMapLocation.getCity()); - EventBus.getDefault().post(new LocationResultEvent(aMapLocation.getCity())); + IcLog.i(TAG, "Loaction City:" + aMapLocation.getCity()); + LocationState state = new LocationState(time, true, aMapLocation.getCity()); + EventBus.getDefault().post(new LocationResultEvent(state)); } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 IcLog.e(TAG, "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" - + aMapLocation.getErrorInfo(),null); - EventBus.getDefault().post(new LocationResultEvent("")); + + aMapLocation.getErrorInfo(), null); + EventBus.getDefault().post(new LocationResultEvent(new LocationState(time, false, null))); } } }; @@ -37,14 +41,14 @@ public class LocationHelper { mLocationClient.setLocationOption(getDefaultOption()); } - public void startLocationService(){ - if(mLocationClient != null){ + public void startLocationService() { + if (mLocationClient != null) { mLocationClient.startLocation(); } } - public void stopLocationService(){ - if(mLocationClient != null){ + public void stopLocationService() { + if (mLocationClient != null) { mLocationClient.stopLocation(); mLocationClient.onDestroy(); diff --git a/app/src/main/java/com/ic/work/weather/LocationState.java b/app/src/main/java/com/ic/work/weather/LocationState.java new file mode 100644 index 0000000..88bd930 --- /dev/null +++ b/app/src/main/java/com/ic/work/weather/LocationState.java @@ -0,0 +1,40 @@ +package com.ic.work.weather; + + +import org.threeten.bp.LocalDateTime; + +public class LocationState { + private LocalDateTime time; + private boolean state; + private String city; + + public LocationState(LocalDateTime oldTime, boolean state, String city) { + this.time = oldTime; + this.state = state; + this.city = city; + } + + public LocalDateTime getTime() { + return time; + } + + public void setTime(LocalDateTime oldTime) { + this.time = oldTime; + } + + public boolean isState() { + return state; + } + + public void setState(boolean state) { + this.state = state; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/app/src/main/java/com/ic/work/weather/WeatherDBRun.java b/app/src/main/java/com/ic/work/weather/WeatherDBRun.java index 3e882b9..5db44d5 100644 --- a/app/src/main/java/com/ic/work/weather/WeatherDBRun.java +++ b/app/src/main/java/com/ic/work/weather/WeatherDBRun.java @@ -11,6 +11,7 @@ import org.threeten.bp.LocalDate; import org.threeten.bp.OffsetDateTime; import org.threeten.bp.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; public class WeatherDBRun implements Runnable { @@ -26,27 +27,29 @@ public class WeatherDBRun implements Runnable { @Override public void run() { - List weatherList = weatherDb.queryDBWeatherDate(LocalDate.now()); + List weatherList = weatherDb.queryDBWeatherDate(); if(weatherList == null || weatherList.size() ==0){ //通知主线程 开始定位 并通过Socket 获取天气 EventBus.getDefault().post(new WeatherAgainEvent()); - //删除表中所有数据 - weatherDb.deleteDBWeatherDate(); }else { - WeatherEntity entity = weatherList.get(0); - - WeatherParcelable parcelable = new WeatherParcelable(); - parcelable.setCity(entity.getCity()); - parcelable.setFigureDay(entity.getFigureDay()); - parcelable.setFigureNight(entity.getFigureNight()); - parcelable.setStatusDay(entity.getStatusDay()); - parcelable.setStatusNight(entity.getStatusNight()); - parcelable.setTemperatureDay(entity.getTemperatureDay()); - parcelable.setTemperatureNight(entity.getTemperatureNight()); - parcelable.setUpdateTime(entity.getUpdateTime().format(formatter)); + List wpList = new ArrayList<>(weatherList.size()); + for (int i=0; i wpList = new ArrayList<>(size); + List dbList = new ArrayList<>(size); + + for (int i=0; i< jsonObjectForecast.size(); i++){ + JSONObject fJson = jsonObjectForecast.getJSONObject(i); + + JSONObject jObjectCond = fJson.getJSONObject("cond"); + JSONObject jObjectTemp = fJson.getJSONObject("tmp"); + + WeatherParcelable weather = new WeatherParcelable(); + WeatherEntity db = new WeatherEntity(); + weather.setCity(mCity); + db.setCity(mCity); + + weather.setStatusDay(jObjectCond.getString("txt_d")); // 白天天气描述 + db.setStatusDay(weather.getStatusDay()); + weather.setStatusNight(jObjectCond.getString("txt_n")); // 夜间天气描述 + db.setStatusNight(weather.getStatusNight()); + + weather.setFigureDay(jObjectCond.getString("code_d")); // 天气索引 + db.setFigureDay(weather.getFigureDay()); + weather.setFigureNight(jObjectCond.getString("code_n")); + db.setFigureNight(weather.getFigureNight()); + + weather.setTemperatureDay(jObjectTemp.getString("min")); // 白天温度-对应“和风天气”的最低温度 + db.setTemperatureDay(weather.getTemperatureDay()); + weather.setTemperatureNight(jObjectTemp.getString("max")); // 夜间温度-对应“和风天气”的最高温度 + db.setTemperatureNight(weather.getTemperatureNight()); + + weather.setUpdateTime(fJson.getString("date")); + db.setUpdateTime(fJson.getString("date")); + + if (BuildConfig.DEBUG) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(weather.getStatusDay()).append("-").append(weather.getStatusNight()) + .append("|") + .append(weather.getTemperatureDay()).append("-").append(weather.getTemperatureNight()) + .append("|") + .append(weather.getUpdateTime()); + IcLog.i(TAG, "Response is weather:" + stringBuffer.toString()); + } + + wpList.add(weather); + dbList.add(db); } - EventBus.getDefault().post(new WeatherResultEvent(weather)); + EventBus.getDefault().post(new WeatherResultEvent(wpList)); //插入数据库 weatherInjection.deleteDBWeatherDate(); - weatherInjection.insertWeatherEntity(db); + weatherInjection.insertWeatherEntity(dbList); } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6e79ec6..e443d47 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -27,5 +27,5 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/start_service_bt" /> + app:layout_constraintTop_toBottomOf="@id/start_service_bt" /> \ No newline at end of file diff --git a/app/version_code.properties b/app/version_code.properties index 8ee6a5b..b30dd98 100644 --- a/app/version_code.properties +++ b/app/version_code.properties @@ -1,2 +1,2 @@ -#Tue Mar 24 18:46:07 CST 2020 -VERSION_CODE=13 +#Mon Mar 30 12:03:25 CST 2020 +VERSION_CODE=16