refactor(layer): 新增同步网关,消除 platform 对 runtime 的反向依赖
平台层需要触发的清单上报、使用时长上报与策略刷新改为经 core 层 DeviceSyncGateway 声明,runtime 实现并在 Application 启动时注册,调用时机与回调顺序保持不变。作业系统开机广播常量下沉 CommonConfig;InstallResultReceiver 随 ApkUtils 迁入 platform.app,Manifest 组件名同步更新。
This commit is contained in:
1 parent
23c5c99332
commit
5dbee71a90
13 files changed
+247
-35
No files matched your search
@@ -195,7 +195,7 @@
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".runtime.receiver.InstallResultReceiver"
|
||||
android:name=".platform.app.InstallResultReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true" />
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.alibaba.sdk.android.push.CommonCallback;
|
||||
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.fuying.sn.core.data.repository.DeviceRepository;
|
||||
import com.fuying.sn.platform.app.RunningAppManager;
|
||||
import com.fuying.sn.platform.time.TimeControlManager;
|
||||
@@ -35,6 +36,7 @@ import com.fuying.sn.runtime.sync.AppUsageReporter;
|
||||
import com.fuying.sn.runtime.sync.BrowserPolicySync;
|
||||
import com.fuying.sn.runtime.sync.DeviceProfileSync;
|
||||
import com.fuying.sn.runtime.sync.DeviceSettingsSync;
|
||||
import com.fuying.sn.runtime.sync.DeviceSyncGatewayImpl;
|
||||
import com.fuying.sn.runtime.sync.PushTagSync;
|
||||
import com.fuying.sn.runtime.sync.RemoteDiagnostics;
|
||||
import com.fuying.sn.runtime.sync.TimePolicySync;
|
||||
@@ -125,6 +127,8 @@ public class BaseApplication extends Application {
|
||||
TimePolicySync.init(this, syncCache);
|
||||
AppUpdateCoordinator.init(this, syncCache);
|
||||
AppUsageReporter.init(this);
|
||||
// 平台层通过网关触发同步动作,避免 platform 反向依赖 runtime;需在同步类初始化之后注册。
|
||||
DeviceSyncGateways.register(new DeviceSyncGatewayImpl());
|
||||
|
||||
if (BuildConfig.VERSION_CODE < 88) {
|
||||
RunningAppManager.init(this);
|
||||
|
||||
@@ -58,6 +58,9 @@ public class CommonConfig {
|
||||
* 管控系统指令
|
||||
*/
|
||||
|
||||
/*作业系统开机广播,与整机固件约定*/
|
||||
public static final String ACTION_ZUOYEOS_BOOT_COMPLETED = "zuoyeos.action.BOOT_COMPLETED";
|
||||
|
||||
/*USB模式-充电*/
|
||||
public static final String AOLE_ACTION_USB_USB_CHARGE = "aole_action_usb_usb_charge";
|
||||
/*USB模式-MTP*/
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.fuying.sn.core.common.sync;
|
||||
|
||||
import com.fuying.sn.core.data.dto.MyAppUsageBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 运行时同步网关。
|
||||
*
|
||||
* <p>平台层的时间管控、前台应用管控、定位在运行过程中需要触发安装清单上报、
|
||||
* 使用时长上报与管控策略刷新,这些能力实际由 runtime 层的同步类提供。
|
||||
* 为避免 platform 反向依赖 runtime,这里只声明平台层所需的动作,
|
||||
* 由 runtime 层实现并在 Application 启动时注册到 {@link DeviceSyncGateways}。
|
||||
*
|
||||
* <p>网关只做依赖解耦,调用时机与回调顺序必须与原先的直接调用保持一致。
|
||||
*/
|
||||
public interface DeviceSyncGateway {
|
||||
|
||||
/**
|
||||
* 设备资料变化(例如定位地址更新)后触发一次资料上报。
|
||||
*/
|
||||
void requestProfileSync();
|
||||
|
||||
/**
|
||||
* 主动上报全量安装清单,结果通过回调返回。
|
||||
*
|
||||
* @param callback 上报结果回调
|
||||
*/
|
||||
void requestInstalledAppReport(InstalledAppReportCallback callback);
|
||||
|
||||
/**
|
||||
* 重新拉取应用名单与白名单配置,不触发全量清单上报。
|
||||
*/
|
||||
void requestAppListRefresh();
|
||||
|
||||
/**
|
||||
* 上报单条应用使用时长。
|
||||
*
|
||||
* @param packageName 应用包名
|
||||
* @param openTime 打开时间(秒)
|
||||
* @param closeTime 关闭时间(秒)
|
||||
* @param onComplete 上报流程结束后的回调
|
||||
*/
|
||||
void reportAppUsage(String packageName, long openTime, long closeTime, Runnable onComplete);
|
||||
|
||||
/**
|
||||
* 上报应用关闭事件。
|
||||
*
|
||||
* @param packageName 应用包名
|
||||
* @param onComplete 上报流程结束后的回调
|
||||
*/
|
||||
void reportAppClose(String packageName, Runnable onComplete);
|
||||
|
||||
/**
|
||||
* 重新拉取应用管控策略。
|
||||
*/
|
||||
void refreshAppTimeControl();
|
||||
|
||||
/**
|
||||
* 重新拉取整机管控策略。
|
||||
*/
|
||||
void refreshMachineTimeControl();
|
||||
|
||||
/**
|
||||
* 拉取我的应用列表。
|
||||
*
|
||||
* @param callback 列表回调
|
||||
*/
|
||||
void requestMyAppList(MyAppListCallback callback);
|
||||
|
||||
/**
|
||||
* 安装清单上报结果。
|
||||
*/
|
||||
interface InstalledAppReportCallback {
|
||||
|
||||
/**
|
||||
* 上报成功(服务端返回 code == 200)。
|
||||
*/
|
||||
void onSuccess();
|
||||
|
||||
/**
|
||||
* 上报失败(网络异常或服务端返回非 200)。
|
||||
*
|
||||
* @param code 服务端返回的 code;网络异常时为 -1
|
||||
* @param msg 失败信息
|
||||
*/
|
||||
void onFail(int code, String msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的应用列表回调。
|
||||
*/
|
||||
interface MyAppListCallback {
|
||||
|
||||
/**
|
||||
* 列表拉取完成。
|
||||
*
|
||||
* @param myAppList 服务端返回的应用列表,可能为空
|
||||
*/
|
||||
void onResult(List<MyAppUsageBean> myAppList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.fuying.sn.core.common.sync;
|
||||
|
||||
/**
|
||||
* 同步网关注册表。
|
||||
*
|
||||
* <p>runtime 层在 Application 启动时注册实现,platform 层通过 {@link #get()} 取用。
|
||||
* 未注册就调用属于启动顺序错误,按仓库单例约定直接抛出异常,避免静默失效。
|
||||
*/
|
||||
public final class DeviceSyncGateways {
|
||||
|
||||
private static volatile DeviceSyncGateway sGateway;
|
||||
|
||||
private DeviceSyncGateways() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册同步网关实现,仅允许在 Application 启动阶段调用一次。
|
||||
*
|
||||
* @param gateway 由 runtime 层提供的实现
|
||||
*/
|
||||
public static void register(DeviceSyncGateway gateway) {
|
||||
sGateway = gateway;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取同步网关实现。
|
||||
*
|
||||
* @return 已注册的实现
|
||||
*/
|
||||
public static DeviceSyncGateway get() {
|
||||
DeviceSyncGateway gateway = sGateway;
|
||||
if (gateway == null) {
|
||||
throw new IllegalStateException("DeviceSyncGateway must be registered first");
|
||||
}
|
||||
return gateway;
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,6 @@ import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.core.data.dto.AppInfo;
|
||||
import com.fuying.sn.core.common.config.CommonConfig;
|
||||
import com.fuying.sn.core.common.gson.GsonUtils;
|
||||
import com.fuying.sn.runtime.receiver.InstallResultReceiver;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.fuying.sn.runtime.receiver;
|
||||
package com.fuying.sn.platform.app;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
@@ -40,9 +40,8 @@ import com.fuying.sn.core.data.remote.UrlAddress;
|
||||
import com.fuying.sn.core.data.dto.statistics.AppRunTimeBean;
|
||||
import com.fuying.sn.core.data.dto.time.AppTimeControl;
|
||||
import com.fuying.sn.core.data.dto.time.MachineControl;
|
||||
import com.fuying.sn.runtime.sync.AppUsageReporter;
|
||||
import com.fuying.sn.runtime.sync.RequestCallbacks;
|
||||
import com.fuying.sn.runtime.sync.TimePolicySync;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateway;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.fuying.sn.platform.app.AppUsedTimeUtils;
|
||||
import com.fuying.sn.platform.system.JgyUtils;
|
||||
import com.fuying.sn.core.common.system.ReceiverCompat;
|
||||
@@ -218,17 +217,17 @@ public class RunningAppManager {
|
||||
if (!topPackage.equals(appPackageName)) {
|
||||
//切换出去了
|
||||
Log.i(TAG, "checkForegroundAppName: 切换应用1:" + topPackage);
|
||||
AppUsageReporter.getInstance().sendCloseApp(appPackageName, new RequestCallbacks.CompleteCallback() {
|
||||
DeviceSyncGateways.get().reportAppClose(appPackageName, new Runnable() {
|
||||
@Override
|
||||
public void onComplete() {
|
||||
public void run() {
|
||||
Log.i(TAG, "onComplete: ");
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
DeviceSyncGateways.get().refreshAppTimeControl();
|
||||
DeviceSyncGateways.get().refreshMachineTimeControl();
|
||||
}
|
||||
});
|
||||
TimePolicySync.getInstance().getMyAppList(new TimePolicySync.MyAppListCallback() {
|
||||
DeviceSyncGateways.get().requestMyAppList(new DeviceSyncGateway.MyAppListCallback() {
|
||||
@Override
|
||||
public void setMyAppList(List<MyAppUsageBean> myAppList) {
|
||||
public void onResult(List<MyAppUsageBean> myAppList) {
|
||||
syncAllAppUsageTime(myAppList);
|
||||
}
|
||||
});
|
||||
@@ -255,8 +254,8 @@ public class RunningAppManager {
|
||||
if (inControlTime(appPackageName)) {
|
||||
Log.i(TAG, "checkForegroundAppName: 没有剩余时间1");
|
||||
// removeTask(topPackage);
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
DeviceSyncGateways.get().refreshAppTimeControl();
|
||||
DeviceSyncGateways.get().refreshMachineTimeControl();
|
||||
JgyUtils.getInstance().killPackage(appPackageName);
|
||||
killApp();
|
||||
gotoLauncher();
|
||||
@@ -266,11 +265,11 @@ public class RunningAppManager {
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, "checkForegroundAppName: 切换应用2:" + topPackage);
|
||||
AppUsageReporter.getInstance().sendCloseApp(appPackageName, new RequestCallbacks.CompleteCallback() {
|
||||
DeviceSyncGateways.get().reportAppClose(appPackageName, new Runnable() {
|
||||
@Override
|
||||
public void onComplete() {
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
public void run() {
|
||||
DeviceSyncGateways.get().refreshAppTimeControl();
|
||||
DeviceSyncGateways.get().refreshMachineTimeControl();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -286,8 +285,8 @@ public class RunningAppManager {
|
||||
killApp();
|
||||
JgyUtils.getInstance().killPackage(appPackageName);
|
||||
gotoLauncher();
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
DeviceSyncGateways.get().refreshAppTimeControl();
|
||||
DeviceSyncGateways.get().refreshMachineTimeControl();
|
||||
} else {
|
||||
recordPackageOpenTime(topPackage);
|
||||
Log.i(TAG, "checkForegroundAppName: 没有管控2");
|
||||
@@ -377,8 +376,8 @@ public class RunningAppManager {
|
||||
mRemainingTimeMap.clear();
|
||||
mGlobalUsageTime.clear();
|
||||
mAllAppUsageTime.clear();
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
DeviceSyncGateways.get().refreshAppTimeControl();
|
||||
DeviceSyncGateways.get().refreshMachineTimeControl();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.amap.api.location.AMapLocationListener;
|
||||
import com.fuying.sn.core.data.dto.MapBean;
|
||||
import com.fuying.sn.core.common.config.CommonConfig;
|
||||
import com.fuying.sn.core.common.gson.GsonUtils;
|
||||
import com.fuying.sn.runtime.sync.DeviceProfileSync;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.jeremyliao.liveeventbus.LiveEventBus;
|
||||
@@ -134,7 +134,7 @@ public class AmapManager {
|
||||
};
|
||||
|
||||
private void updateAddress(AMapLocation aMapLocation) {
|
||||
DeviceProfileSync.getInstance().updateAdminInfo();
|
||||
DeviceSyncGateways.get().requestProfileSync();
|
||||
}
|
||||
|
||||
private MapBean getMapBean(AMapLocation location) {
|
||||
|
||||
@@ -33,8 +33,7 @@ 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;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
@@ -808,14 +807,14 @@ public class JgyUtils {
|
||||
|
||||
public void wakeUpAppstore() {
|
||||
//启动应用市场
|
||||
Intent bootIntent = new Intent(BootReceiver.BOOT_COMPLETED);
|
||||
Intent bootIntent = new Intent(CommonConfig.ACTION_ZUOYEOS_BOOT_COMPLETED);
|
||||
bootIntent.setComponent(new ComponentName(PACKAGE_APPSTORE, CLASS_APPSTORE));
|
||||
mContext.sendBroadcast(bootIntent);
|
||||
}
|
||||
|
||||
public void wakeUpUpdateTools() {
|
||||
//启动升级组件
|
||||
Intent bootIntent = new Intent(BootReceiver.BOOT_COMPLETED);
|
||||
Intent bootIntent = new Intent(CommonConfig.ACTION_ZUOYEOS_BOOT_COMPLETED);
|
||||
bootIntent.setComponent(new ComponentName(PACKAGE_UPDATETOOLS, CLASS_UPDATETOOLS));
|
||||
mContext.sendBroadcast(bootIntent);
|
||||
}
|
||||
@@ -869,7 +868,7 @@ public class JgyUtils {
|
||||
|
||||
public void restorrNetwork() {
|
||||
Settings.System.putString(mContext.getContentResolver(), CommonConfig.AOLE_ACTION_NETWORK_DISALLOW, "");
|
||||
AppInventorySync.getInstance().getAllappPackage();
|
||||
DeviceSyncGateways.get().requestAppListRefresh();
|
||||
}
|
||||
|
||||
public static void RemoveTask(final Context context, final String packageName) {
|
||||
|
||||
@@ -31,8 +31,8 @@ import com.fuying.sn.core.data.repository.TimeConfigRepository;
|
||||
import com.fuying.sn.core.data.local.db.time.AppRemainingEntity;
|
||||
import com.fuying.sn.core.data.remote.BackgroundRequests;
|
||||
import com.fuying.sn.core.data.remote.UrlAddress;
|
||||
import com.fuying.sn.runtime.sync.AppInventorySync;
|
||||
import com.fuying.sn.runtime.sync.AppUsageReporter;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateway;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.fuying.sn.platform.app.ApkUtils;
|
||||
import com.fuying.sn.platform.app.AppUsedTimeUtils;
|
||||
import com.fuying.sn.platform.system.JgyUtils;
|
||||
@@ -224,7 +224,7 @@ public class TimeManager {
|
||||
|
||||
private void uploadInstalledApps() {
|
||||
// 等服务端收录清单后再拉取应用规则。
|
||||
AppInventorySync.getInstance().SendAppInstallInfo(new AppInventorySync.SendAppInstallInfoCallback() {
|
||||
DeviceSyncGateways.get().requestInstalledAppReport(new DeviceSyncGateway.InstalledAppReportCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
DeviceRepository.removeCache(UrlAddress.APP_TIME_CONTROL);
|
||||
@@ -262,7 +262,7 @@ public class TimeManager {
|
||||
String prevPackage = usedTime.settleAndClear();
|
||||
if (prevPackage != null && !prevPackage.equals(packageName)) {
|
||||
Log.d(TAG, "onUidImportanceChanged Detect app switch. Finishing record for: " + prevPackage);
|
||||
AppUsageReporter.getInstance().sendAppUsageRecord(prevPackage, prevStartTime, nowTime, this::getTimeControl);
|
||||
DeviceSyncGateways.get().reportAppUsage(prevPackage, prevStartTime, nowTime, this::getTimeControl);
|
||||
}
|
||||
|
||||
// 开启对新进入应用的记录(排除系统应用及白名单应用)
|
||||
@@ -282,7 +282,7 @@ public class TimeManager {
|
||||
long prevStartTime = usedTime.getStart_time();
|
||||
String settledPackage = usedTime.settleAndClear();
|
||||
Log.d(TAG, "onUidImportanceChanged App moved to background: " + packageName + ". Uploading usage record.");
|
||||
AppUsageReporter.getInstance().sendAppUsageRecord(packageName, prevStartTime, nowTime, this::getTimeControl);
|
||||
DeviceSyncGateways.get().reportAppUsage(packageName, prevStartTime, nowTime, this::getTimeControl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import com.tencent.mmkv.MMKV;
|
||||
public class
|
||||
BootReceiver extends BroadcastReceiver {
|
||||
private static final String TAG = "BootReceiver";
|
||||
public static final String BOOT_COMPLETED = "zuoyeos.action.BOOT_COMPLETED";
|
||||
public static final String BOOT_COMPLETED = CommonConfig.ACTION_ZUOYEOS_BOOT_COMPLETED;
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.fuying.sn.runtime.sync;
|
||||
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateway;
|
||||
import com.fuying.sn.core.data.dto.MyAppUsageBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 同步网关实现:把平台层的动作转发给既有同步类。
|
||||
*
|
||||
* <p>本类只做转发,不调整请求时机与回调顺序,保证解耦前后行为一致。
|
||||
*/
|
||||
public final class DeviceSyncGatewayImpl implements DeviceSyncGateway {
|
||||
|
||||
@Override
|
||||
public void requestProfileSync() {
|
||||
DeviceProfileSync.getInstance().updateAdminInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestInstalledAppReport(InstalledAppReportCallback callback) {
|
||||
AppInventorySync.getInstance().SendAppInstallInfo(new AppInventorySync.SendAppInstallInfoCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
callback.onSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFail(int code, String msg) {
|
||||
callback.onFail(code, msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestAppListRefresh() {
|
||||
AppInventorySync.getInstance().getAllappPackage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportAppUsage(String packageName, long openTime, long closeTime, Runnable onComplete) {
|
||||
AppUsageReporter.getInstance().sendAppUsageRecord(packageName, openTime, closeTime, onComplete::run);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportAppClose(String packageName, Runnable onComplete) {
|
||||
AppUsageReporter.getInstance().sendCloseApp(packageName, onComplete::run);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshAppTimeControl() {
|
||||
TimePolicySync.getInstance().getAppTimeControl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshMachineTimeControl() {
|
||||
TimePolicySync.getInstance().getMachineTimeControl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestMyAppList(MyAppListCallback callback) {
|
||||
TimePolicySync.getInstance().getMyAppList(new TimePolicySync.MyAppListCallback() {
|
||||
@Override
|
||||
public void setMyAppList(List<MyAppUsageBean> myAppList) {
|
||||
callback.onResult(myAppList);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user