refactor(core): BaseApplication 初始化拆分为分模块 Initializer 注册
- 移除 utilsInint 中逐个管理器的硬编码初始化,改由 AppInitializerRegistry 统一调度 - Core/Runtime/Platform 各自登记初始化器,执行顺序由声明的依赖推导 - 区分 runRequired 与 runDeferred 两阶段执行 - 崩溃兜底与启动 Service 逻辑从 BaseApplication 中移出
This commit is contained in:
1 parent
86f0bc9625
commit
f53ce61752
7 files changed
+829
-185
No files matched your search
@@ -2,65 +2,22 @@ package com.fuying.sn.core.base;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Application;
|
||||
import android.content.Intent;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.core.common.device.DeviceIdUtils;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.fuying.sn.core.base.init.AppInitializerRegistry;
|
||||
import com.fuying.sn.core.base.init.CoreInitializers;
|
||||
import com.fuying.sn.core.common.system.AppEnvironment;
|
||||
import com.fuying.sn.core.data.local.cache.CacheHelper;
|
||||
import com.fuying.sn.core.data.local.db.log.LogDBManager;
|
||||
import com.fuying.sn.core.data.repository.DeviceRepository;
|
||||
import com.fuying.sn.platform.app.AppUsedTimeUtils;
|
||||
import com.fuying.sn.platform.app.RunningAppManager;
|
||||
import com.fuying.sn.platform.control.ControlManager;
|
||||
import com.fuying.sn.platform.location.AmapManager;
|
||||
import com.fuying.sn.platform.system.DeviceManager;
|
||||
import com.fuying.sn.platform.system.JgyUtils;
|
||||
import com.fuying.sn.platform.init.PlatformInitializers;
|
||||
import com.fuying.sn.platform.system.SystemUtils;
|
||||
import com.fuying.sn.platform.time.TimeControlManager;
|
||||
import com.fuying.sn.platform.time.TimeManager;
|
||||
import com.fuying.sn.runtime.push.AliyunPushManager;
|
||||
import com.fuying.sn.runtime.push.PushManager;
|
||||
import com.fuying.sn.runtime.service.DownloadService;
|
||||
import com.fuying.sn.runtime.service.ManagerService;
|
||||
import com.fuying.sn.runtime.service.SocketService;
|
||||
import com.fuying.sn.runtime.service.main.MainService;
|
||||
import com.fuying.sn.runtime.sync.AppInventorySync;
|
||||
import com.fuying.sn.runtime.sync.AppPolicySync;
|
||||
import com.fuying.sn.runtime.sync.AppUpdateCoordinator;
|
||||
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;
|
||||
import com.hjq.toast.Toaster;
|
||||
import com.tencent.bugly.crashreport.CrashReport;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
|
||||
import java.util.Collections;
|
||||
import com.fuying.sn.runtime.init.RuntimeInitializers;
|
||||
|
||||
public class BaseApplication extends Application {
|
||||
private static final String TAG = "BaseApplication";
|
||||
|
||||
/**
|
||||
* 兜底循环内允许的连续主线程异常次数,超过后交回默认处理器重启进程
|
||||
*/
|
||||
private static final int MAX_CONTINUOUS_CRASH = 5;
|
||||
/**
|
||||
* 两次主线程异常超过该间隔(毫秒)就重新计数,避免偶发异常被累计成连续崩溃
|
||||
*/
|
||||
private static final long CRASH_RESET_INTERVAL = 30_000L;
|
||||
|
||||
/**
|
||||
* ViewModel中因为经常旋转导致弱引用为空
|
||||
*/
|
||||
@@ -93,60 +50,15 @@ public class BaseApplication extends Application {
|
||||
Log.e(TAG, "debugTest: " + getCacheDir());
|
||||
Log.e(TAG, "debugTest: " + Environment.getExternalStorageDirectory());
|
||||
}
|
||||
utilsInint();
|
||||
// 各模块只登记自己的初始化器,执行顺序由声明的依赖推导,这里不再逐个调用具体管理器
|
||||
AppInitializerRegistry.install(CoreInitializers.create());
|
||||
AppInitializerRegistry.install(RuntimeInitializers.create());
|
||||
AppInitializerRegistry.install(PlatformInitializers.create());
|
||||
AppInitializerRegistry.runRequired(this);
|
||||
AppInitializerRegistry.runDeferred(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void utilsInint() {
|
||||
String rootDir = MMKV.initialize(this);
|
||||
Log.e(TAG, "mmkv root: " + rootDir);
|
||||
|
||||
PushManager.init(this);
|
||||
aliyunPushInit();
|
||||
|
||||
Toaster.init(this);
|
||||
JgyUtils.init(this);
|
||||
CrashReport.initCrashReport(getApplicationContext(), "e5f026c8d5", false);
|
||||
CrashReport.setDeviceId(this, DeviceIdUtils.getSerial());
|
||||
if (!BuildConfig.DEBUG) {
|
||||
// Bugly 先接管默认异常处理器,兜底处理器才能把子线程异常继续传递给它完成上报
|
||||
catchException();
|
||||
}
|
||||
|
||||
LogDBManager.init(this);
|
||||
Aria.init(this);
|
||||
Aria.download(this).resumeAllTask();
|
||||
|
||||
CacheHelper syncCache = new CacheHelper(this);
|
||||
DeviceRepository.init(this, syncCache);
|
||||
DeviceProfileSync.init(this);
|
||||
PushTagSync.init();
|
||||
AppInventorySync.init(this);
|
||||
BrowserPolicySync.init(this);
|
||||
AppPolicySync.init(this, syncCache);
|
||||
DeviceSettingsSync.init(this, syncCache);
|
||||
RemoteDiagnostics.init(this);
|
||||
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);
|
||||
} else {
|
||||
TimeManager.init(this);
|
||||
}
|
||||
TimeControlManager.init(this);
|
||||
ControlManager.init(this);
|
||||
// ControlPanelManager.init(this);
|
||||
DeviceManager.init(this);
|
||||
AmapManager.init(this);
|
||||
AppUsedTimeUtils.init(this);
|
||||
JgyUtils.hookWebView();
|
||||
startService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTerminate() {
|
||||
super.onTerminate();
|
||||
@@ -159,91 +71,4 @@ public class BaseApplication extends Application {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startService() {
|
||||
startService(new Intent(this, MainService.class));
|
||||
startService(new Intent(this, SocketService.class));
|
||||
startService(new Intent(this, DownloadService.class));
|
||||
startService(new Intent(this, ManagerService.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 线上崩溃兜底:主线程单次异常只记录并继续分发消息,避免常驻进程被一次异常打断;
|
||||
* 子线程异常继续交给原有处理器(Bugly),保证崩溃能正常上报。
|
||||
*/
|
||||
private void catchException() {
|
||||
final Thread.UncaughtExceptionHandler originHandler =
|
||||
Thread.getDefaultUncaughtExceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
logCrash("子线程", t, e);
|
||||
if (originHandler != null) {
|
||||
// 不吞掉异常:默认处理器里是 Bugly 的上报与系统退出逻辑
|
||||
originHandler.uncaughtException(t, e);
|
||||
} else {
|
||||
Log.e(TAG, "catchException: 默认异常处理器为空,子线程崩溃无法上报");
|
||||
}
|
||||
}
|
||||
});
|
||||
// 主线程消息分发的异常会冒泡出 Looper.loop() 直接结束进程,用嵌套循环拦下来单独记录
|
||||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int crashCount = 0;
|
||||
long lastCrashTime = 0L;
|
||||
while (true) {
|
||||
try {
|
||||
Looper.loop(); // 接管主线程消息分发,消息里抛出的异常会落到下面的 catch
|
||||
} catch (Throwable e) {
|
||||
// 使用单调时钟,避免 NTP 校时导致时间窗被错误重置
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
if (now - lastCrashTime > CRASH_RESET_INTERVAL) {
|
||||
crashCount = 0;
|
||||
}
|
||||
lastCrashTime = now;
|
||||
crashCount++;
|
||||
logCrash("主线程", Thread.currentThread(), e);
|
||||
if (e instanceof Error || crashCount >= MAX_CONTINUOUS_CRASH) {
|
||||
// Error 不可恢复、或短时间内反复崩溃说明进程状态已不可信,
|
||||
// 交回默认处理器让系统按 START_STICKY 重新拉起,不再继续兜底
|
||||
Log.e(TAG, "catchException: 主线程连续异常次数=" + crashCount);
|
||||
if (originHandler != null) {
|
||||
originHandler.uncaughtException(Thread.currentThread(), e);
|
||||
return;
|
||||
}
|
||||
// 没有兜底处理器时主动结束进程,避免带着已损坏的状态继续分发消息
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(10);
|
||||
}
|
||||
reportCatchedException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void logCrash(String source, Thread thread, Throwable e) {
|
||||
// 直接取 getStackTrace()[0] 在堆栈为空时会在异常处理里二次崩溃,这里取完整堆栈
|
||||
Log.e(TAG, "logCrash: " + source + " thread=" + thread.getName()
|
||||
+ " 在:" + Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
private void reportCatchedException(Throwable e) {
|
||||
try {
|
||||
// 被兜底拦下的异常不会经过 Bugly 的 UncaughtExceptionHandler,必须主动上报
|
||||
CrashReport.postCatchedException(e);
|
||||
} catch (Throwable reportError) {
|
||||
Log.e(TAG, "reportCatchedException: 崩溃上报失败 "
|
||||
+ Log.getStackTraceString(reportError));
|
||||
}
|
||||
}
|
||||
|
||||
private void aliyunPushInit() {
|
||||
// 推送的初始化、注册、别名与标签全部交由 AliyunPushManager 统一处理
|
||||
AliyunPushManager.init(this);
|
||||
// 机型标签先挂起,注册成功后由管理器自动补齐
|
||||
AliyunPushManager.getInstance().bindTags(Collections.singleton(BuildConfig.platform));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.fuying.sn.core.base.init;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用启动初始化器。
|
||||
*
|
||||
* <p>签名对齐 Jetpack App Startup 的 {@code androidx.startup.Initializer},区别只在于由
|
||||
* BaseApplication 显式登记并分两阶段执行(相当于官方的「手动初始化」模式),
|
||||
* 这样可以保留主进程门控、延迟阶段与单模块失败隔离——自动发现模式这三者都做不到。
|
||||
*
|
||||
* <p>各模块把自己的启动动作收敛到一个实现类并交给 {@link AppInitializerRegistry} 登记,
|
||||
* BaseApplication 只负责按依赖推导出的顺序执行,不再逐个感知具体的管理器。
|
||||
*
|
||||
* <p>实现类必须提供无参构造,同一个类只允许登记一次。
|
||||
*/
|
||||
public interface AppInitializer {
|
||||
|
||||
/**
|
||||
* 声明依赖的初始化器类型,执行时保证依赖先完成。
|
||||
*
|
||||
* <p>只声明真实的先后约束;未登记的依赖会被跳过并记录日志。
|
||||
*
|
||||
* @return 依赖的初始化器类型列表,没有依赖时返回空列表
|
||||
*/
|
||||
default List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否必须在 Application.onCreate 中同步完成。
|
||||
*
|
||||
* <p>返回 false 时 init 整体推迟到首帧后的主线程空闲阶段,
|
||||
* 适用于不影响常驻服务启动、广播接收注册的模块。
|
||||
*
|
||||
* @return 必须同步初始化返回 true
|
||||
*/
|
||||
boolean required();
|
||||
|
||||
/**
|
||||
* 初始化主体。
|
||||
*
|
||||
* @param application 应用上下文
|
||||
*/
|
||||
void init(Application application);
|
||||
|
||||
/**
|
||||
* 首帧后的主线程空闲阶段执行,用于把耗时的预热动作从 onCreate 挪走。
|
||||
*
|
||||
* @param application 应用上下文
|
||||
*/
|
||||
default void deferred(Application application) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.fuying.sn.core.base.init;
|
||||
|
||||
import android.app.Application;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.MessageQueue;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 初始化器注册表:收集各模块的初始化器,按依赖推导执行顺序,并分两个阶段执行。
|
||||
*
|
||||
* <p>required 阶段在 Application.onCreate 的主线程上同步执行全部必需初始化器;
|
||||
* deferred 阶段在主线程首轮消息跑完后的空闲时执行非必需初始化器与所有初始化器的 deferred 动作。
|
||||
*/
|
||||
public final class AppInitializerRegistry {
|
||||
|
||||
private static final String TAG = "AppInitializerRegistry";
|
||||
|
||||
/**
|
||||
* 用 LinkedHashMap 保留登记顺序,依赖相同时排序结果才稳定
|
||||
*/
|
||||
private static final Map<Class<?>, AppInitializer> sInitializers = new LinkedHashMap<>();
|
||||
private static List<AppInitializer> sOrdered;
|
||||
private static boolean sDeferredStarted;
|
||||
|
||||
private AppInitializerRegistry() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 登记一批初始化器。
|
||||
*
|
||||
* @param initializers 初始化器列表,允许为空
|
||||
*/
|
||||
public static synchronized void install(List<AppInitializer> initializers) {
|
||||
if (initializers == null) {
|
||||
return;
|
||||
}
|
||||
for (AppInitializer initializer : initializers) {
|
||||
if (initializer == null) {
|
||||
continue;
|
||||
}
|
||||
Class<?> type = initializer.getClass();
|
||||
if (sInitializers.containsKey(type)) {
|
||||
Log.e(TAG, "install: 重复登记,已忽略 " + type.getSimpleName());
|
||||
continue;
|
||||
}
|
||||
sInitializers.put(type, initializer);
|
||||
}
|
||||
// 登记表变化后旧的排序结果失效
|
||||
sOrdered = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行必需初始化器,必须在主线程调用。
|
||||
*
|
||||
* @param application 应用上下文
|
||||
*/
|
||||
public static synchronized void runRequired(Application application) {
|
||||
for (AppInitializer initializer : ordered()) {
|
||||
if (initializer.required()) {
|
||||
execute(initializer, application, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 挂起延迟初始化,在主线程首轮消息跑完后的空闲阶段执行。
|
||||
*
|
||||
* @param application 应用上下文
|
||||
*/
|
||||
public static void runDeferred(final Application application) {
|
||||
final List<AppInitializer> ordered;
|
||||
synchronized (AppInitializerRegistry.class) {
|
||||
if (sDeferredStarted) {
|
||||
return;
|
||||
}
|
||||
sDeferredStarted = true;
|
||||
ordered = ordered();
|
||||
}
|
||||
// 先让 onCreate 返回后的首轮消息与首帧先跑完,再挂空闲任务,避免和绘制抢主线程
|
||||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
|
||||
@Override
|
||||
public boolean queueIdle() {
|
||||
for (AppInitializer initializer : ordered) {
|
||||
execute(initializer, application, true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行单个初始化器,失败只记录不中断,避免一个模块拖垮整个启动流程。
|
||||
*
|
||||
* @param initializer 初始化器
|
||||
* @param application 应用上下文
|
||||
* @param deferredPhase 是否处于延迟阶段
|
||||
*/
|
||||
private static void execute(AppInitializer initializer, Application application, boolean deferredPhase) {
|
||||
String name = initializer.getClass().getSimpleName();
|
||||
try {
|
||||
if (!deferredPhase) {
|
||||
initializer.init(application);
|
||||
return;
|
||||
}
|
||||
if (!initializer.required()) {
|
||||
// 非必需模块整体推迟到这里,所以要先补 init 再走 deferred
|
||||
initializer.init(application);
|
||||
}
|
||||
initializer.deferred(application);
|
||||
} catch (Throwable e) {
|
||||
// 单个模块初始化失败不阻断其它模块,但必须留下可定位的日志
|
||||
Log.e(TAG, "execute: " + name + " 初始化失败,阶段=" + (deferredPhase ? "deferred" : "required")
|
||||
+ " " + Log.getStackTraceString(e));
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized List<AppInitializer> ordered() {
|
||||
if (sOrdered == null) {
|
||||
sOrdered = sort(new ArrayList<>(sInitializers.values()));
|
||||
}
|
||||
return sOrdered;
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度优先拓扑排序:先输出依赖,再输出自身,顺序在依赖相同时保持登记顺序。
|
||||
*
|
||||
* @param input 已登记的初始化器
|
||||
* @return 可执行顺序
|
||||
*/
|
||||
private static List<AppInitializer> sort(List<AppInitializer> input) {
|
||||
List<AppInitializer> ordered = new ArrayList<>(input.size());
|
||||
Set<Class<?>> visiting = new HashSet<>();
|
||||
Set<Class<?>> finished = new HashSet<>();
|
||||
for (AppInitializer initializer : input) {
|
||||
visit(initializer, visiting, finished, ordered);
|
||||
}
|
||||
return ordered;
|
||||
}
|
||||
|
||||
private static void visit(AppInitializer initializer, Set<Class<?>> visiting,
|
||||
Set<Class<?>> finished, List<AppInitializer> ordered) {
|
||||
Class<?> type = initializer.getClass();
|
||||
if (finished.contains(type)) {
|
||||
return;
|
||||
}
|
||||
if (!visiting.add(type)) {
|
||||
// 环依赖直接返回,保证启动阶段不会死循环
|
||||
Log.e(TAG, "visit: 检测到循环依赖 " + type.getSimpleName());
|
||||
return;
|
||||
}
|
||||
List<Class<? extends AppInitializer>> dependencies = initializer.dependencies();
|
||||
if (dependencies != null) {
|
||||
for (Class<? extends AppInitializer> dependency : dependencies) {
|
||||
AppInitializer target = sInitializers.get(dependency);
|
||||
if (target == null) {
|
||||
Log.e(TAG, "visit: " + type.getSimpleName() + " 声明的依赖未登记 " + dependency.getSimpleName());
|
||||
continue;
|
||||
}
|
||||
visit(target, visiting, finished, ordered);
|
||||
}
|
||||
}
|
||||
visiting.remove(type);
|
||||
finished.add(type);
|
||||
ordered.add(initializer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.fuying.sn.core.base.init;
|
||||
|
||||
import android.app.Application;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fuying.sn.core.common.device.DeviceIdUtils;
|
||||
import com.hjq.toast.Toaster;
|
||||
import com.tencent.bugly.crashreport.CrashReport;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* core 层初始化器集合:本地存储、Toast、Bugly 与崩溃兜底。
|
||||
*
|
||||
* <p>BaseApplication 只通过这个入口拿到初始化器,自身不再逐个调用具体类。
|
||||
*/
|
||||
public final class CoreInitializers {
|
||||
|
||||
private CoreInitializers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 core 层初始化器列表。
|
||||
*
|
||||
* @return 初始化器列表,列表顺序仅代表登记顺序,实际执行顺序由依赖决定
|
||||
*/
|
||||
public static List<AppInitializer> create() {
|
||||
List<AppInitializer> initializers = new ArrayList<>();
|
||||
initializers.add(new Storage());
|
||||
initializers.add(new ToastSupport());
|
||||
initializers.add(new Bugly());
|
||||
initializers.add(new CrashGuard());
|
||||
return initializers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地存储:MMKV 是所有配置读写的底座,必须最先完成。
|
||||
*/
|
||||
public static final class Storage implements AppInitializer {
|
||||
|
||||
private static final String TAG = "CoreInitializers.Storage";
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
String rootDir = MMKV.initialize(application);
|
||||
Log.e(TAG, "init: mmkv root = " + rootDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局 Toast:常驻服务与远程指令都会用到,必须在服务启动前就绪。
|
||||
*/
|
||||
public static final class ToastSupport implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
Toaster.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bugly 崩溃上报:设备标识在启动阶段一次性写入。
|
||||
*/
|
||||
public static final class Bugly implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
CrashReport.initCrashReport(application, "e5f026c8d5", false);
|
||||
CrashReport.setDeviceId(application, DeviceIdUtils.getSerial());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package com.fuying.sn.core.base.init;
|
||||
|
||||
import android.app.Application;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.tencent.bugly.crashreport.CrashReport;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 线上崩溃兜底:主线程单次异常只记录并继续分发消息,避免常驻进程被一次异常打断;
|
||||
* 子线程异常继续交给原有处理器(Bugly),保证崩溃能正常上报。
|
||||
*/
|
||||
public final class CrashGuard implements AppInitializer {
|
||||
|
||||
private static final String TAG = "CrashGuard";
|
||||
|
||||
/**
|
||||
* 兜底循环内允许的连续主线程异常次数,超过后交回默认处理器重启进程
|
||||
*/
|
||||
private static final int MAX_CONTINUOUS_CRASH = 5;
|
||||
/**
|
||||
* 两次主线程异常超过该间隔(毫秒)就重新计数,避免偶发异常被累计成连续崩溃
|
||||
*/
|
||||
private static final long CRASH_RESET_INTERVAL = 30_000L;
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
// 兜底处理器要把子线程异常继续交给 Bugly 上报,Bugly 必须先接管默认处理器
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(CoreInitializers.Bugly.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
// 调试包不接管,保留 IDE 里的完整崩溃堆栈
|
||||
if (BuildConfig.DEBUG) {
|
||||
return;
|
||||
}
|
||||
install();
|
||||
}
|
||||
|
||||
private void install() {
|
||||
final Thread.UncaughtExceptionHandler originHandler =
|
||||
Thread.getDefaultUncaughtExceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
@Override
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
logCrash("子线程", t, e);
|
||||
if (originHandler != null) {
|
||||
// 不吞掉异常:默认处理器里是 Bugly 的上报与系统退出逻辑
|
||||
originHandler.uncaughtException(t, e);
|
||||
} else {
|
||||
Log.e(TAG, "catchException: 默认异常处理器为空,子线程崩溃无法上报");
|
||||
}
|
||||
}
|
||||
});
|
||||
// 主线程消息分发的异常会冒泡出 Looper.loop() 直接结束进程,用嵌套循环拦下来单独记录
|
||||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int crashCount = 0;
|
||||
long lastCrashTime = 0L;
|
||||
while (true) {
|
||||
try {
|
||||
Looper.loop(); // 接管主线程消息分发,消息里抛出的异常会落到下面的 catch
|
||||
} catch (Throwable e) {
|
||||
// 使用单调时钟,避免 NTP 校时导致时间窗被错误重置
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
if (now - lastCrashTime > CRASH_RESET_INTERVAL) {
|
||||
crashCount = 0;
|
||||
}
|
||||
lastCrashTime = now;
|
||||
crashCount++;
|
||||
logCrash("主线程", Thread.currentThread(), e);
|
||||
if (e instanceof Error || crashCount >= MAX_CONTINUOUS_CRASH) {
|
||||
// Error 不可恢复、或短时间内反复崩溃说明进程状态已不可信,
|
||||
// 交回默认处理器让系统按 START_STICKY 重新拉起,不再继续兜底
|
||||
Log.e(TAG, "catchException: 主线程连续异常次数=" + crashCount);
|
||||
if (originHandler != null) {
|
||||
originHandler.uncaughtException(Thread.currentThread(), e);
|
||||
return;
|
||||
}
|
||||
// 没有兜底处理器时主动结束进程,避免带着已损坏的状态继续分发消息
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(10);
|
||||
}
|
||||
reportCatchedException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void logCrash(String source, Thread thread, Throwable e) {
|
||||
// 直接取 getStackTrace()[0] 在堆栈为空时会在异常处理里二次崩溃,这里取完整堆栈
|
||||
Log.e(TAG, "logCrash: " + source + " thread=" + thread.getName()
|
||||
+ " 在:" + Log.getStackTraceString(e));
|
||||
}
|
||||
|
||||
private void reportCatchedException(Throwable e) {
|
||||
try {
|
||||
// 被兜底拦下的异常不会经过 Bugly 的 UncaughtExceptionHandler,必须主动上报
|
||||
CrashReport.postCatchedException(e);
|
||||
} catch (Throwable reportError) {
|
||||
Log.e(TAG, "reportCatchedException: 崩溃上报失败 "
|
||||
+ Log.getStackTraceString(reportError));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.fuying.sn.platform.init;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.core.base.init.AppInitializer;
|
||||
import com.fuying.sn.core.base.init.CoreInitializers;
|
||||
import com.fuying.sn.platform.app.AppUsedTimeUtils;
|
||||
import com.fuying.sn.platform.app.RunningAppManager;
|
||||
import com.fuying.sn.platform.control.ControlManager;
|
||||
import com.fuying.sn.platform.location.AmapManager;
|
||||
import com.fuying.sn.platform.system.DeviceManager;
|
||||
import com.fuying.sn.platform.system.JgyUtils;
|
||||
import com.fuying.sn.platform.time.TimeControlManager;
|
||||
import com.fuying.sn.platform.time.TimeManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* platform 层初始化器集合:系统能力、管控、计时与定位。
|
||||
*
|
||||
* <p>这些单例是常驻服务的直接依赖,因此全部在 required 阶段完成;
|
||||
* 与具体调用方无关的预热动作(如 WebView 注入)放在 deferred 阶段。
|
||||
*/
|
||||
public final class PlatformInitializers {
|
||||
|
||||
private PlatformInitializers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 platform 层初始化器列表。
|
||||
*
|
||||
* @return 初始化器列表,列表顺序仅代表登记顺序,实际执行顺序由依赖决定
|
||||
*/
|
||||
public static List<AppInitializer> create() {
|
||||
List<AppInitializer> initializers = new ArrayList<>();
|
||||
initializers.add(new System());
|
||||
initializers.add(new AppMonitor());
|
||||
initializers.add(new TimeControl());
|
||||
initializers.add(new Control());
|
||||
initializers.add(new Device());
|
||||
initializers.add(new Location());
|
||||
initializers.add(new UsageStats());
|
||||
return initializers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统工具:构造时读取 ROM 信息,WebView 注入放到首帧后执行。
|
||||
*/
|
||||
public static final class System implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(CoreInitializers.Storage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
JgyUtils.init(application);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deferred(Application application) {
|
||||
JgyUtils.hookWebView();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用运行监控:旧版本走 RunningAppManager,新版本交给 TimeManager。
|
||||
*/
|
||||
public static final class AppMonitor implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(CoreInitializers.Storage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
if (BuildConfig.VERSION_CODE < 88) {
|
||||
RunningAppManager.init(application);
|
||||
} else {
|
||||
TimeManager.init(application);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用时间管控:构造时从磁盘缓存恢复上次下发的管控策略。
|
||||
*/
|
||||
public static final class TimeControl implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
TimeControlManager.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统设置管控:构造时持有 ContentResolver。
|
||||
*/
|
||||
public static final class Control implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
ControlManager.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备操作:重启、恢复出厂等系统级动作。
|
||||
*/
|
||||
public static final class Device implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
DeviceManager.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定位:构造时创建高德定位客户端,服务启动后可能立刻上报位置,不能推迟。
|
||||
*/
|
||||
public static final class Location implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
AmapManager.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用使用时长记录:前台切换时会立刻写入。
|
||||
*/
|
||||
public static final class UsageStats implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
AppUsedTimeUtils.init(application);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package com.fuying.sn.runtime.init;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.fuying.sn.BuildConfig;
|
||||
import com.fuying.sn.core.base.init.AppInitializer;
|
||||
import com.fuying.sn.core.base.init.CoreInitializers;
|
||||
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
|
||||
import com.fuying.sn.core.data.local.cache.CacheHelper;
|
||||
import com.fuying.sn.core.data.local.db.log.LogDBManager;
|
||||
import com.fuying.sn.core.data.repository.DeviceRepository;
|
||||
import com.fuying.sn.platform.init.PlatformInitializers;
|
||||
import com.fuying.sn.runtime.push.AliyunPushManager;
|
||||
import com.fuying.sn.runtime.push.PushManager;
|
||||
import com.fuying.sn.runtime.service.DownloadService;
|
||||
import com.fuying.sn.runtime.service.ManagerService;
|
||||
import com.fuying.sn.runtime.service.SocketService;
|
||||
import com.fuying.sn.runtime.service.main.MainService;
|
||||
import com.fuying.sn.runtime.sync.AppInventorySync;
|
||||
import com.fuying.sn.runtime.sync.AppPolicySync;
|
||||
import com.fuying.sn.runtime.sync.AppUpdateCoordinator;
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* runtime 层初始化器集合:推送、日志、下载、数据同步与常驻服务。
|
||||
*
|
||||
* <p>需要长期保持的注册关系(推送、同步、广播)在 required 阶段完成;
|
||||
* 只在闲时才有必要的动作(标签补齐、下载续传)放到 deferred 阶段。
|
||||
*/
|
||||
public final class RuntimeInitializers {
|
||||
|
||||
private RuntimeInitializers() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 runtime 层初始化器列表。
|
||||
*
|
||||
* @return 初始化器列表,列表顺序仅代表登记顺序,实际执行顺序由依赖决定
|
||||
*/
|
||||
public static List<AppInitializer> create() {
|
||||
List<AppInitializer> initializers = new ArrayList<>();
|
||||
initializers.add(new Push());
|
||||
initializers.add(new LogDb());
|
||||
initializers.add(new Download());
|
||||
initializers.add(new DataSync());
|
||||
initializers.add(new Services());
|
||||
return initializers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送:消息分发与阿里云通道必须在推送到达前就绪。
|
||||
*/
|
||||
public static final class Push implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(CoreInitializers.Storage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
PushManager.init(application);
|
||||
// 推送的初始化、注册、别名与标签全部交由 AliyunPushManager 统一处理
|
||||
AliyunPushManager.init(application);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deferred(Application application) {
|
||||
// 机型标签先挂起,注册成功后由管理器自动补齐,放到空闲阶段避免和启动抢资源
|
||||
AliyunPushManager.getInstance().bindTags(Collections.singleton(BuildConfig.platform));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志库:崩溃与远程 log 都依赖它。
|
||||
*/
|
||||
public static final class LogDb implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
LogDBManager.init(application);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载:Aria 必须在 DownloadService 启动前完成初始化,续传任务放到空闲阶段。
|
||||
*/
|
||||
public static final class Download implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
Aria.init(application);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deferred(Application application) {
|
||||
Aria.download(application).resumeAllTask();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据同步:共享一个磁盘缓存实例,并注册平台层需要的同步网关。
|
||||
*/
|
||||
public static final class DataSync implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(CoreInitializers.Storage.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
CacheHelper syncCache = new CacheHelper(application);
|
||||
DeviceRepository.init(application, syncCache);
|
||||
DeviceProfileSync.init(application);
|
||||
PushTagSync.init();
|
||||
AppInventorySync.init(application);
|
||||
BrowserPolicySync.init(application);
|
||||
AppPolicySync.init(application, syncCache);
|
||||
DeviceSettingsSync.init(application, syncCache);
|
||||
RemoteDiagnostics.init(application);
|
||||
TimePolicySync.init(application, syncCache);
|
||||
AppUpdateCoordinator.init(application, syncCache);
|
||||
AppUsageReporter.init(application);
|
||||
// 平台层通过网关触发同步动作,避免 platform 反向依赖 runtime;需在同步类初始化之后注册
|
||||
DeviceSyncGateways.register(new DeviceSyncGatewayImpl());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 常驻服务:会立刻使用管控、计时与同步能力,必须最后启动。
|
||||
*/
|
||||
public static final class Services implements AppInitializer {
|
||||
|
||||
@Override
|
||||
public List<Class<? extends AppInitializer>> dependencies() {
|
||||
return Arrays.<Class<? extends AppInitializer>>asList(
|
||||
DataSync.class,
|
||||
PlatformInitializers.System.class,
|
||||
PlatformInitializers.AppMonitor.class,
|
||||
PlatformInitializers.TimeControl.class,
|
||||
PlatformInitializers.Control.class,
|
||||
PlatformInitializers.Device.class,
|
||||
PlatformInitializers.Location.class,
|
||||
PlatformInitializers.UsageStats.class
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean required() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Application application) {
|
||||
application.startService(new Intent(application, MainService.class));
|
||||
application.startService(new Intent(application, SocketService.class));
|
||||
application.startService(new Intent(application, DownloadService.class));
|
||||
application.startService(new Intent(application, ManagerService.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user