228 lines
8.0 KiB
Java
228 lines
8.0 KiB
Java
package com.ttstd.dialer.base;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Application;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.os.Build;
|
||
import android.os.Handler;
|
||
import android.os.Looper;
|
||
|
||
import androidx.multidex.MultiDex;
|
||
|
||
import com.alibaba.android.arouter.launcher.ARouter;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.kongzue.dialogx.DialogX;
|
||
import com.tencent.bugly.crashreport.CrashReport;
|
||
import com.tencent.mmkv.MMKV;
|
||
import com.ttstd.dialer.BuildConfig;
|
||
import com.ttstd.dialer.alarmclock.AlarmManagerHelper;
|
||
import com.ttstd.dialer.config.CommonConfig;
|
||
import com.ttstd.dialer.config.SystemIntentAction;
|
||
import com.ttstd.dialer.manager.AppManager;
|
||
import com.ttstd.dialer.manager.MapManager;
|
||
import com.ttstd.dialer.manager.WeatherManager;
|
||
import com.ttstd.dialer.mdm.DeviceManagerService;
|
||
import com.ttstd.dialer.network.OkHttpManager;
|
||
import com.ttstd.dialer.push.PushExecutor;
|
||
import com.ttstd.dialer.receiver.AppChangedReceiver;
|
||
import com.ttstd.dialer.receiver.HourlyChimeManager;
|
||
import com.ttstd.dialer.tts.sherpa_onnx.SherpaOnnxTtsManager;
|
||
import com.ttstd.dialer.utils.Logger;
|
||
import com.ttstd.dialer.utils.NativeUtils;
|
||
import com.ttstd.dialer.utils.SystemUtils;
|
||
import com.ttstd.iconloader.IconCacheManager;
|
||
|
||
import cn.jiguang.api.JCoreInterface;
|
||
import cn.jiguang.api.utils.JCollectionAuth;
|
||
import cn.jpush.android.api.JPushInterface;
|
||
|
||
|
||
public class BaseApplication extends Application {
|
||
private static final String TAG = "BaseApplication";
|
||
|
||
private MMKV mMMKV;
|
||
|
||
/**
|
||
* ViewModel中因为经常旋转导致弱引用为空
|
||
*/
|
||
@SuppressLint("StaticFieldLeak")
|
||
private static Context mAppContext;
|
||
|
||
public static Context getContext() {
|
||
return mAppContext;
|
||
}
|
||
|
||
@Override
|
||
protected void attachBaseContext(Context base) {
|
||
super.attachBaseContext(base);
|
||
MultiDex.install(this);
|
||
}
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
Logger.e(TAG, "onCreate: ");
|
||
mAppContext = getApplicationContext();
|
||
|
||
if (!BuildConfig.DEBUG) {
|
||
catchException();
|
||
}
|
||
// 在开始分析的地方调用,传入路径
|
||
// 如果是放到外部路径,需要添加权限
|
||
// 默认存储在/sdcard/Android/data/packagename/files
|
||
// Debug.startMethodTracing("App" + System.currentTimeMillis());
|
||
init();
|
||
}
|
||
|
||
@Override
|
||
public void onTerminate() {
|
||
super.onTerminate();
|
||
unregisterReceivers();
|
||
}
|
||
|
||
@Override
|
||
public void onLowMemory() {
|
||
super.onLowMemory();
|
||
}
|
||
|
||
@Override
|
||
public void onTrimMemory(int level) {
|
||
super.onTrimMemory(level);
|
||
}
|
||
|
||
private void init() {
|
||
Logger.e(TAG, "init: ");
|
||
Logger.e(TAG, "init: getNonce = " + NativeUtils.getNonce());
|
||
if (SystemUtils.isMainProcessName(this, android.os.Process.myPid())) {
|
||
Logger.initialize(this, BuildConfig.DEBUG);
|
||
Logger.setLogLevel(Logger.LogLevel.DEBUG); // 开发阶段记录所有日志
|
||
|
||
String rootDir = MMKV.initialize(this);
|
||
Logger.e(TAG, "mmkv root: " + rootDir);
|
||
mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||
|
||
DeviceManagerService.init(this);
|
||
OkHttpManager.init(this);
|
||
PushExecutor.init(this);
|
||
initJPush();
|
||
|
||
if (BuildConfig.DEBUG) { // 这两行必须写在init之前,否则这些配置在init过程中将无效
|
||
ARouter.openLog(); // 打印日志
|
||
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
|
||
}
|
||
ARouter.init(this); // 尽可能早,推荐在Application中初始化
|
||
|
||
DialogX.init(this);
|
||
|
||
Logger.e(TAG, "slowInit: ");
|
||
Aria.init(this);
|
||
CrashReport.initCrashReport(getApplicationContext(), "845e3ed68c", false);
|
||
CrashReport.setDeviceId(this, Build.MODEL);
|
||
xcrash.XCrash.init(this);
|
||
|
||
AppManager.init(this);
|
||
MapManager.init(this);
|
||
MapManager.getInstance().initMap();
|
||
MapManager.getInstance().startLocation();
|
||
|
||
WeatherManager.init(this);
|
||
IconCacheManager.init(this);
|
||
SherpaOnnxTtsManager.getInstance().init(this);
|
||
AlarmManagerHelper.rescheduleAllAlarms(this);
|
||
|
||
if (mMMKV.decodeInt(CommonConfig.HOURLY_CHIME_ENABLE, 0) == 1) {
|
||
HourlyChimeManager.startChime(this);
|
||
}
|
||
|
||
registerReceivers();
|
||
}
|
||
}
|
||
|
||
private void initJPush() {
|
||
/*jpush start*/
|
||
JPushInterface.setDebugMode(true);
|
||
// 调整点一:调用启用推送业务功能代码前增加setAuth调用
|
||
boolean isPrivacyReady = true; // app根据是否已弹窗获取隐私授权来赋值
|
||
if (!isPrivacyReady) {
|
||
// JCore 5.0.4之前版本需要显式设置false
|
||
if (JCoreInterface.getJCoreSDKVersionInt() < 504) { // 5.0.4版本号对应504
|
||
JCollectionAuth.setAuth(this, false);
|
||
}
|
||
// 所有版本在未授权时都不应初始化SDK
|
||
return;
|
||
}
|
||
JPushInterface.init(this);
|
||
JPushInterface.setAlias(this, 0, DeviceManagerService.getInstance().getSerial());
|
||
|
||
// 调整点二:App用户同意了隐私政策授权,并且开发者确定要开启推送服务后调用
|
||
// JCore 5.0.4+会自动处理授权状态,可不需要显式设置true
|
||
JCollectionAuth.setAuth(this, true);
|
||
/*jpush end*/
|
||
Logger.e(TAG, "initJPush: inited JPush");
|
||
|
||
}
|
||
|
||
private void catchException() {
|
||
Thread.setDefaultUncaughtExceptionHandler(
|
||
new Thread.UncaughtExceptionHandler() {
|
||
@Override
|
||
public void uncaughtException(Thread t, Throwable e) {
|
||
Logger.e("捕获异常子线程:", Thread.currentThread().getName() +
|
||
"在:" + e.getStackTrace()[0].getClassName());
|
||
}
|
||
}
|
||
);
|
||
//下面是新增方法!
|
||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
while (true) {
|
||
try {
|
||
Looper.loop(); //会先执行这个方法,然后在执行下面的异常捕获方法!
|
||
} catch (Exception e) {
|
||
Logger.e("捕获异常主线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName());
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
private void registerReceivers() {
|
||
registerAppChangedReceive();
|
||
}
|
||
|
||
@Deprecated
|
||
private void unregisterReceivers() {
|
||
if (mAppChangedReceiver != null) {
|
||
unregisterReceiver(mAppChangedReceiver);
|
||
}
|
||
}
|
||
|
||
private AppChangedReceiver mAppChangedReceiver;
|
||
|
||
|
||
private void registerAppChangedReceive() {
|
||
if (null == mAppChangedReceiver) {
|
||
mAppChangedReceiver = new AppChangedReceiver();
|
||
}
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
|
||
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
|
||
filter.addAction(Intent.ACTION_MY_PACKAGE_REPLACED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
|
||
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
|
||
filter.addAction(SystemIntentAction.ACTION_PACKAGE_ENABLE_ROLLBACK);
|
||
filter.addAction(SystemIntentAction.ACTION_CANCEL_ENABLE_ROLLBACK);
|
||
filter.addAction(SystemIntentAction.ACTION_ROLLBACK_COMMITTED);
|
||
filter.addDataScheme("package");
|
||
registerReceiver(mAppChangedReceiver, filter);
|
||
}
|
||
|
||
}
|