diff --git a/app/src/main/java/com/fuying/sn/core/common/device/PlatformUtils.java b/app/src/main/java/com/fuying/sn/core/common/device/PlatformUtils.java new file mode 100644 index 0000000..4fc746f --- /dev/null +++ b/app/src/main/java/com/fuying/sn/core/common/device/PlatformUtils.java @@ -0,0 +1,92 @@ +package com.fuying.sn.core.common.device; + +import android.util.Log; + +import com.fuying.sn.BuildConfig; + +/** + * 机型平台识别。 + * + *

平台标识来自各 buildType 注入的 {@link BuildConfig#platform},属于设备身份信息, + * 不依赖任何系统隐藏接口,因此放在 core 层,供数据层与平台层共用。 + * + *

该映射原先只在 platform 层的 JgyUtils 中提供,导致 repository 为取一个平台编号 + * 反向依赖 platform 层;JgyUtils 现改为委托本类,对外常量保持不变。 + */ +public final class PlatformUtils { + + private static final String TAG = "PlatformUtils"; + + public static final int UNKNOWN_PLATFORM = 0; + public static final int MTK_PLATFORM = 1; + public static final int ZHANRUI_PLATFORM = 2; + public static final int M40SE_PLATFORM = 4; + public static final int T30PRO_PLATFORM = 5; + public static final int MTK8515_PLATFORM = 6; + public static final int G13_PLATFORM = 7; + public static final int IPLAY50SE_PLATFORM = 8; + public static final int G6_PLATFORM = 10; + public static final int G11J_PLATFORM = 11; + public static final int G12NL_PLATFORM = 12; + + private static final String MTK8515_TAG = "8515"; + private static final String G13_TAG = "G13"; + private static final String IPLAY50SE_TAG = "ipaly50SE"; + private static final String G6_TAG = "G6"; + private static final String G11J_TAG = "G10J"; + private static final String G12NL_TAG = "G12NL"; + + private PlatformUtils() { + } + + /** + * 读取当前 buildType 注入的平台标识并转换为平台编号。 + * + * @return 平台编号,无法识别时返回 {@link #UNKNOWN_PLATFORM} + */ + public static int getPlatformCode() { + String platform = BuildConfig.platform; + if ("MTK".equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: MTK平台"); + return MTK_PLATFORM; + } + if ("ZhanRui".equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: 展锐平台"); + return ZHANRUI_PLATFORM; + } + if ("M40SE".equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: M40SE"); + return M40SE_PLATFORM; + } + if ("T30PRO".equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: T30PRO"); + return T30PRO_PLATFORM; + } + if (MTK8515_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: 8515"); + return MTK8515_PLATFORM; + } + if (G13_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: G13"); + return G13_PLATFORM; + } + if (IPLAY50SE_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: ipaly50SE"); + return IPLAY50SE_PLATFORM; + } + if (G6_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: G6"); + return G6_PLATFORM; + } + if (G11J_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: G10J"); + return G11J_PLATFORM; + } + if (G12NL_TAG.equalsIgnoreCase(platform)) { + Log.e(TAG, "getPlatformCode: G12NL"); + return G12NL_PLATFORM; + } + Log.e(TAG, "getPlatformCode: 没有数据"); + return UNKNOWN_PLATFORM; + } +} diff --git a/app/src/main/java/com/fuying/sn/core/common/util/RandomUtils.java b/app/src/main/java/com/fuying/sn/core/common/util/RandomUtils.java new file mode 100644 index 0000000..301fd08 --- /dev/null +++ b/app/src/main/java/com/fuying/sn/core/common/util/RandomUtils.java @@ -0,0 +1,28 @@ +package com.fuying.sn.core.common.util; + +/** + * 随机延时工具。 + * + *

服务端扛不住全量设备同时请求,错峰延时是硬性要求,需要被数据层与运行时层共用, + * 因此实现放在 core 层;{@code platform.time.TimeUtils#randomDelay} 保留为委托入口, + * 既有调用方与仓库约定(AGENTS.md 中记的 TimeUtils.randomDelay)都不受影响。 + */ +public final class RandomUtils { + + private RandomUtils() { + } + + /** + * 生成 [minMillis, maxMillis) 区间内的随机延时。 + * + * @param minMillis 最小延时(含) + * @param maxMillis 最大延时(不含) + * @return 随机延时毫秒数,入参非法时退化为 minMillis + */ + public static long randomDelay(long minMillis, long maxMillis) { + if (maxMillis <= minMillis) { + return Math.max(minMillis, 0); + } + return minMillis + (long) (Math.random() * (maxMillis - minMillis)); + } +} diff --git a/app/src/main/java/com/fuying/sn/core/data/repository/DeviceRepository.java b/app/src/main/java/com/fuying/sn/core/data/repository/DeviceRepository.java index 46132b3..153d24e 100644 --- a/app/src/main/java/com/fuying/sn/core/data/repository/DeviceRepository.java +++ b/app/src/main/java/com/fuying/sn/core/data/repository/DeviceRepository.java @@ -43,9 +43,9 @@ import com.fuying.sn.core.data.remote.api.ControlApi; import com.fuying.sn.core.data.remote.api.DeviceInfoApi; import com.fuying.sn.core.data.remote.api.StatisticsApi; import com.fuying.sn.core.data.local.cache.RxCacheManager; -import com.fuying.sn.platform.system.JgyUtils; -import com.fuying.sn.platform.time.TimeUtils; import com.fuying.sn.core.common.device.DeviceIdUtils; +import com.fuying.sn.core.common.device.PlatformUtils; +import com.fuying.sn.core.common.util.RandomUtils; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; @@ -263,7 +263,7 @@ public final class DeviceRepository { } // 延时并随机错峰后重订阅,让常规推送刷新有机会先完成。 // 缓存请求会重新检查内存;仅仍在进行的同键请求能够合并,再次作废则继续重试。 - long delay = INVALIDATED_RETRY_BASE_MS + TimeUtils.randomDelay(0, INVALIDATED_RETRY_SPREAD_MS); + long delay = INVALIDATED_RETRY_BASE_MS + RandomUtils.randomDelay(0, INVALIDATED_RETRY_SPREAD_MS); return Observable.timer(delay, TimeUnit.MILLISECONDS); })); } @@ -427,7 +427,7 @@ public final class DeviceRepository { } public Observable>> getOverallAppObservable() { - int platformCode = JgyUtils.getInstance().checkAppPlatform(); + int platformCode = PlatformUtils.getPlatformCode(); return schedule(getAppApi().getOverallApp(platformCode)); } @@ -538,7 +538,7 @@ public final class DeviceRepository { public Observable> checkAppUpdate(String packageName, String versionCode) { return schedule(getAppApi().getAppUpdate(packageName, versionCode, - JgyUtils.getInstance().checkAppPlatform())); + PlatformUtils.getPlatformCode())); } public Observable reportDeviceInfo(String address, String machine, diff --git a/app/src/main/java/com/fuying/sn/core/data/repository/TimeConfigRepository.java b/app/src/main/java/com/fuying/sn/core/data/repository/TimeConfigRepository.java index a02853c..74fad5d 100644 --- a/app/src/main/java/com/fuying/sn/core/data/repository/TimeConfigRepository.java +++ b/app/src/main/java/com/fuying/sn/core/data/repository/TimeConfigRepository.java @@ -26,8 +26,6 @@ import java.util.concurrent.TimeUnit; import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.schedulers.Schedulers; -import com.fuying.sn.platform.app.RunningAppManager; -import com.fuying.sn.platform.time.TimeManager; /** * 时间管控配置仓库:统一负责服务端配置的拉取、本地缓存与失败重试。 diff --git a/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java b/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java index f7aafa8..4805e18 100644 --- a/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java +++ b/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java @@ -31,6 +31,7 @@ import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.fuying.sn.BuildConfig; import com.fuying.sn.core.common.config.CommonConfig; +import com.fuying.sn.core.common.device.PlatformUtils; import com.fuying.sn.core.common.gson.GsonUtils; import com.fuying.sn.runtime.receiver.BootReceiver; import com.fuying.sn.runtime.sync.AppInventorySync; @@ -75,18 +76,19 @@ public class JgyUtils { private ContentResolver resolver; private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE); - public static final int UnknowPlatform = 0; + // 平台编号已迁到 core 层,这里保留同名常量做别名,避免全仓调用点改动。 + public static final int UnknowPlatform = PlatformUtils.UNKNOWN_PLATFORM; - public static final int MTKPlatform = 1; - public static final int ZhanruiPlatform = 2; - public static final int M40sePlatform = 4; - public static final int T30ProPlatform = 5; - public static final int MTK8515Platform = 6; - public static final int G13Platform = 7; - public static final int iPlay50SEPlatform = 8; - public static final int G6Platform = 10; - public static final int G11JPlatform = 11; - public static final int G12NLPlatform = 12; + public static final int MTKPlatform = PlatformUtils.MTK_PLATFORM; + public static final int ZhanruiPlatform = PlatformUtils.ZHANRUI_PLATFORM; + public static final int M40sePlatform = PlatformUtils.M40SE_PLATFORM; + public static final int T30ProPlatform = PlatformUtils.T30PRO_PLATFORM; + public static final int MTK8515Platform = PlatformUtils.MTK8515_PLATFORM; + public static final int G13Platform = PlatformUtils.G13_PLATFORM; + public static final int iPlay50SEPlatform = PlatformUtils.IPLAY50SE_PLATFORM; + public static final int G6Platform = PlatformUtils.G6_PLATFORM; + public static final int G11JPlatform = PlatformUtils.G11J_PLATFORM; + public static final int G12NLPlatform = PlatformUtils.G12NL_PLATFORM; public static final String MTKTag = "MTK"; @@ -372,42 +374,13 @@ public class JgyUtils { }); } + /** + * 读取当前机型平台编号。 + * + *

映射逻辑已下沉到 {@link PlatformUtils},本方法只做转发,保持既有调用方不变。 + */ public int checkAppPlatform() { - String platform = BuildConfig.platform; - if ("MTK".equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "MTK平台"); - return MTKPlatform; - } else if ("ZhanRui".equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "展锐平台"); - return ZhanruiPlatform; - } else if ("M40SE".equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "M40SE"); - return M40sePlatform; - } else if ("T30PRO".equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "T30PRO"); - return T30ProPlatform; - } else if (MTK8515Tag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "8515"); - return MTK8515Platform; - } else if (G13Tag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "G13"); - return G13Platform; - } else if (iPlay50SETag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "ipaly50SE"); - return iPlay50SEPlatform; - } else if (G6Tag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "G6"); - return G6Platform; - } else if (G11JTag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "G10J"); - return G11JPlatform; - } else if (G12NLTag.equalsIgnoreCase(platform)) { - Log.i(TAG, "checkAppPlatform: " + "G12NL"); - return G12NLPlatform; - } else { - Log.i(TAG, "checkAppPlatform: " + "没有数据"); - return UnknowPlatform; - } + return PlatformUtils.getPlatformCode(); } public interface GetAppPlatformCallback { diff --git a/app/src/main/java/com/fuying/sn/platform/time/TimeUtils.java b/app/src/main/java/com/fuying/sn/platform/time/TimeUtils.java index 86c2962..7c5fda9 100644 --- a/app/src/main/java/com/fuying/sn/platform/time/TimeUtils.java +++ b/app/src/main/java/com/fuying/sn/platform/time/TimeUtils.java @@ -4,6 +4,7 @@ import android.os.SystemClock; import android.text.TextUtils; import android.util.Log; +import com.fuying.sn.core.common.util.RandomUtils; import com.fuying.sn.core.data.dto.PartTime; import com.fuying.sn.core.data.dto.statistics.AppControlDetail; @@ -484,14 +485,13 @@ public class TimeUtils { * 用于多设备错峰:静默升级是全量下发,若所有设备在同一时刻下载/请求, * 会把服务端打挂,这里用随机延时把请求摊开。 * + *

实现已下沉到 {@link RandomUtils},本方法保留为委托入口,避免既有调用方与仓库约定失效。 + * * @param minMillis 最小延时(含) * @param maxMillis 最大延时(不含) */ public static long randomDelay(long minMillis, long maxMillis) { - if (maxMillis <= minMillis) { - return Math.max(minMillis, 0); - } - return minMillis + (long) (Math.random() * (maxMillis - minMillis)); + return RandomUtils.randomDelay(minMillis, maxMillis); } }