From 003e085aa818bdf6bf95585e8f49cb271339087f Mon Sep 17 00:00:00 2001 From: tongchenfei Date: Mon, 28 Dec 2020 15:36:10 +0800 Subject: [PATCH] opt Block Strategy --- .idea/gradle.xml | 1 + .../share/strategyreceiver/BlockStrategy.kt | 39 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index b892d6479b..cfc605c98e 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -84,6 +84,7 @@ diff --git a/modules/mogo-module-share/src/main/java/com/mogo/module/share/strategyreceiver/BlockStrategy.kt b/modules/mogo-module-share/src/main/java/com/mogo/module/share/strategyreceiver/BlockStrategy.kt index 6bb1360d4f..fc431dc621 100644 --- a/modules/mogo-module-share/src/main/java/com/mogo/module/share/strategyreceiver/BlockStrategy.kt +++ b/modules/mogo-module-share/src/main/java/com/mogo/module/share/strategyreceiver/BlockStrategy.kt @@ -3,24 +3,14 @@ package com.mogo.module.share.strategyreceiver import android.content.Context import android.os.Handler import android.os.SystemClock -import android.util.ArrayMap -import com.mogo.commons.network.SubscribeImpl -import com.mogo.commons.network.Utils import com.mogo.map.MogoLatLng -import com.mogo.module.share.bean.AverateSpeedResponse -import com.mogo.module.share.constant.HttpConstant import com.mogo.module.share.manager.ServiceApisManager -import com.mogo.module.share.net.ShareApiService import com.mogo.service.IMogoServiceApis import com.mogo.service.share.IMogoTanluProvider import com.mogo.service.share.TanluUploadParams import com.mogo.utils.logger.Logger -import com.mogo.utils.network.RequestOptions -import com.mogo.utils.network.utils.GsonUtil import com.mogo.utils.storage.SharedPrefsMgr -import io.reactivex.schedulers.Schedulers -import okhttp3.MediaType -import okhttp3.RequestBody +import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList @@ -35,7 +25,7 @@ private const val SPEED_RECORD_TIME_INTERVAL = 2 * 60 * 1000L */ private const val STOP_FLAG_THRESHOLD = 30 private const val TAG = "BlockUploadStrategy" - +private const val TIME_FAULT_TOLERANT = 60 * 60 * 1000L private const val DISTANCE_RECORD_TIME = 3 * 60 * 1000L private const val MORNING_PEAK_START_MINUTE = 7 * 60 + 30 @@ -48,6 +38,8 @@ private const val DISTANCE_MORNING_PEAK_COUNT = "DISTANCE_MORNING_PEAK_COUNT" private const val DISTANCE_NIGHT_PEAK_COUNT = "DISTANCE_NIGHT_PEAK_COUNT" private const val DISTANCE_NORMAL_COUNT = "DISTANCE_NORMAL_COUNT" +private const val START_BLOCK_COUNT_DATE = "START_BLOCK_COUNT_DATE" + private const val SPEED_MORNING_PEAK_COUNT = "SPEED_MORNING_PEAK_COUNT" private const val SPEED_NIGHT_PEAK_COUNT = "SPEED_NIGHT_PEAK_COUNT" @@ -118,8 +110,20 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService lastLon = lon lastLat = lat } - - if (current - startRecordTime >= DISTANCE_RECORD_TIME) { + val diff = current - startRecordTime + if (diff in DISTANCE_RECORD_TIME until TIME_FAULT_TOLERANT) { + // 重置count data + val countDate = SharedPrefsMgr.getInstance(context).getLong(START_BLOCK_COUNT_DATE, 0) + if (countDate == 0L || isDifferentDay(countDate, System.currentTimeMillis())) { + // 不是同一天,重置各种计数,重新开始计数 + SharedPrefsMgr.getInstance(context).putLong(START_BLOCK_COUNT_DATE, System.currentTimeMillis()) + SharedPrefsMgr.getInstance(context).putInt(DISTANCE_MORNING_PEAK_COUNT, 0) + SharedPrefsMgr.getInstance(context).putInt(DISTANCE_NIGHT_PEAK_COUNT, 0) + SharedPrefsMgr.getInstance(context).putInt(DISTANCE_NORMAL_COUNT, 0) + SharedPrefsMgr.getInstance(context).putInt(SPEED_NORMAL_COUNT, 0) + SharedPrefsMgr.getInstance(context).putInt(SPEED_NIGHT_PEAK_COUNT, 0) + SharedPrefsMgr.getInstance(context).putInt(SPEED_MORNING_PEAK_COUNT, 0) + } var blockFromSpeed = false var blockFromDistance = false @@ -299,4 +303,11 @@ class BlockStrategy(private val context: Context, private val apis: IMogoService val minute = Calendar.getInstance().get(Calendar.MINUTE) + hour * 60 return minute in NIGHT_PEAK_START_MINUTE..NIGHT_PEAK_STOP_MINUTE } + + private fun isDifferentDay(a: Long, b: Long): Boolean { + val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA) + val aDate = dateFormat.format(a) + val bDate = dateFormat.format(b) + return aDate != bDate + } } \ No newline at end of file