466 lines
16 KiB
Java
466 lines
16 KiB
Java
package com.xscm.moduleutil.base;
|
||
|
||
import static androidx.core.app.ActivityCompat.startActivityForResult;
|
||
|
||
import android.app.Activity;
|
||
import android.app.ActivityManager;
|
||
import android.app.Application;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.content.pm.Signature;
|
||
import android.net.Uri;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.provider.Settings;
|
||
import android.util.Base64;
|
||
import android.webkit.WebView;
|
||
|
||
import androidx.multidex.MultiDex;
|
||
import androidx.multidex.MultiDexApplication;
|
||
|
||
import com.alibaba.android.arouter.launcher.ARouter;
|
||
import com.blankj.utilcode.util.AppUtils;
|
||
import com.blankj.utilcode.util.FileUtils;
|
||
import com.blankj.utilcode.util.LogUtils;
|
||
import com.blankj.utilcode.util.ProcessUtils;
|
||
import com.blankj.utilcode.util.ServiceUtils;
|
||
import com.lahm.library.EasyProtectorLib;
|
||
import com.lahm.library.EmulatorCheckCallback;
|
||
import com.xscm.moduleutil.R;
|
||
import com.xscm.moduleutil.bean.UserBean;
|
||
import com.xscm.moduleutil.event.AppLifecycleEvent;
|
||
import com.xscm.moduleutil.interfaces.AppLifecycleUtil;
|
||
import com.xscm.moduleutil.listener.MessageListenerSingleton;
|
||
import com.xscm.moduleutil.rtc.AgoraManager;
|
||
import com.xscm.moduleutil.service.MqttConnect;
|
||
import com.xscm.moduleutil.service.MyMqttService;
|
||
import com.xscm.moduleutil.utils.SpUtil;
|
||
import com.xscm.moduleutil.utils.UtilConfig;
|
||
import com.xscm.moduleutil.utils.config.EnvironmentEnum;
|
||
import com.xscm.moduleutil.utils.config.EnvironmentPrefs;
|
||
import com.xscm.moduleutil.widget.CommonAppConfig;
|
||
import com.xscm.moduleutil.widget.PiaoPingManager;
|
||
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
|
||
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
|
||
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
|
||
import com.scwang.smartrefresh.layout.api.RefreshFooter;
|
||
import com.scwang.smartrefresh.layout.api.RefreshHeader;
|
||
import com.scwang.smartrefresh.layout.api.RefreshLayout;
|
||
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
|
||
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
|
||
import com.tencent.bugly.crashreport.CrashReport;
|
||
import com.tencent.qcloud.tuicore.TUILogin;
|
||
import com.tencent.qcloud.tuicore.interfaces.TUICallback;
|
||
|
||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||
import org.greenrobot.eventbus.EventBus;
|
||
|
||
import java.lang.reflect.Method;
|
||
import java.security.MessageDigest;
|
||
import java.util.concurrent.Executors;
|
||
import java.util.concurrent.ScheduledExecutorService;
|
||
|
||
import lombok.Getter;
|
||
|
||
|
||
/**
|
||
* Created by cxf on 2017/8/3.
|
||
*/
|
||
|
||
public class CommonAppContext extends MultiDexApplication {
|
||
|
||
private static CommonAppContext sInstance;
|
||
private static Handler sMainThreadHandler;
|
||
private int mCount;
|
||
private boolean mFront;//是否前台
|
||
public String emulator = "0";
|
||
@Getter
|
||
private EnvironmentEnum currentEnvironment;
|
||
public UserBean mUserBean;
|
||
|
||
public boolean isShow;
|
||
public boolean isPlaying;
|
||
|
||
public String playId;
|
||
public String playCover;
|
||
public boolean showSelf;//盲盒是否能送自己
|
||
public String playName;
|
||
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
|
||
private ScheduledExecutorService scheduledExecutorServiceRoom = null;
|
||
private MqttConnect mqttConnect=null;
|
||
|
||
// 添加后台状态标记
|
||
private boolean wasInBackground = false;
|
||
|
||
public void onAppBackground() {
|
||
wasInBackground = true;
|
||
}
|
||
|
||
public void onAppForeground() {
|
||
wasInBackground = false;
|
||
}
|
||
|
||
public boolean wasInBackground() {
|
||
return wasInBackground;
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
sInstance = this;
|
||
sMainThreadHandler = new Handler();
|
||
EnvironmentPrefs prefs = new EnvironmentPrefs(this);
|
||
currentEnvironment = prefs.getSelectedEnvironment();
|
||
initialization();
|
||
|
||
|
||
}
|
||
|
||
public void initialization(){
|
||
UtilConfig.init(this);
|
||
registerActivityLifecycleCallbacks();
|
||
initWebView();
|
||
if (ProcessUtils.isMainProcess()) {
|
||
initARouter();
|
||
if (SpUtil.isAgreePolicy()) {
|
||
checkInEmulator();
|
||
UtilConfig.checkInEmulator();
|
||
AgoraManager.getInstance(this).init(currentEnvironment.getSwSdkAppId());
|
||
MessageListenerSingleton.getInstance();
|
||
CrashReport.initCrashReport(this, "ac3ed4d89f", true);/*bugly初始化*/
|
||
// // 启动 MQTT 服务
|
||
// Intent mqttServiceIntent = new Intent(this, MyMqttService.class);
|
||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||
// startForegroundService(mqttServiceIntent);
|
||
// } else {
|
||
// startService(mqttServiceIntent);
|
||
// }
|
||
|
||
mqttConnect=MqttConnect.getInstance(this,"tcp://62.234.12.147","android-"+ MqttClient.generateClientId());
|
||
mqttConnect.mqttClient();
|
||
|
||
// 每次启动应用时重置状态
|
||
SpUtil.getInstance().setBooleanValue("youth_model_shown", false);
|
||
}
|
||
}
|
||
// piaoPingManager = PiaoPingManager.getInstance(this);
|
||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||
if (!Settings.canDrawOverlays(this)) {
|
||
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
|
||
startActivityForResult(intent, 100);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void startActivityForResult(Intent intent, int i) {
|
||
}
|
||
|
||
|
||
// private PiaoPingManager piaoPingManager;
|
||
|
||
private void initARouter() {
|
||
if (true) {
|
||
ARouter.openDebug();
|
||
ARouter.openLog();
|
||
}
|
||
ARouter.init(this);
|
||
|
||
}
|
||
|
||
private void initWebView() {
|
||
//Android P 以及之后版本不支持同时从多个进程使用具有相同数据目录的WebView
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||
String processName = getProcessName(this);
|
||
if (!AppUtils.getAppPackageName().equals(processName)) {//判断不等于默认进程名称
|
||
WebView.setDataDirectorySuffix(processName);
|
||
}
|
||
}
|
||
}
|
||
|
||
public String getProcessName(Context context) {
|
||
if (context == null) return null;
|
||
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
||
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
|
||
if (processInfo.pid == android.os.Process.myPid()) {
|
||
return processInfo.processName;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
protected void attachBaseContext(Context base) {
|
||
MultiDex.install(this);
|
||
super.attachBaseContext(base);
|
||
}
|
||
|
||
public static CommonAppContext getInstance() {
|
||
if (sInstance == null) {
|
||
try {
|
||
Class clazz = Class.forName("android.app.ActivityThread");
|
||
Method method = clazz.getMethod("currentApplication", new Class[]{});
|
||
Object obj = method.invoke(null, new Object[]{});
|
||
if (obj != null && obj instanceof CommonAppContext) {
|
||
sInstance = (CommonAppContext) obj;
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return sInstance;
|
||
}
|
||
|
||
public void checkInEmulator() {
|
||
emulator = EasyProtectorLib.checkIsRunningInEmulator(this, new EmulatorCheckCallback() {
|
||
@Override
|
||
public void findEmulator(String emulatorInfo) {
|
||
// Logger.e(emulatorInfo);
|
||
}
|
||
}) ? "1" : "0";
|
||
}
|
||
|
||
public static void postDelayed(Runnable runnable, long delayMillis) {
|
||
if (sMainThreadHandler != null) {
|
||
sMainThreadHandler.postDelayed(runnable, delayMillis);
|
||
}
|
||
}
|
||
|
||
public static void post(Runnable runnable) {
|
||
if (sMainThreadHandler != null) {
|
||
sMainThreadHandler.post(runnable);
|
||
}
|
||
}
|
||
|
||
public String getToken() {
|
||
return SpUtil.getToken();
|
||
}
|
||
|
||
private void registerActivityLifecycleCallbacks() {
|
||
registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
|
||
@Override
|
||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onActivityStarted(Activity activity) {
|
||
mCount++;
|
||
if (!mFront) {
|
||
mFront = true;
|
||
LogUtils.e("AppContext------->处于前台");
|
||
EventBus.getDefault().post(new AppLifecycleEvent(true));
|
||
CommonAppConfig.getInstance().setFrontGround(true);
|
||
// FloatWindowHelper.setFloatWindowVisible(true);
|
||
AppLifecycleUtil.onAppFrontGround();
|
||
// 确保在主线程中订阅
|
||
if (activity != null && !activity.isFinishing()) {
|
||
activity.runOnUiThread(() -> {
|
||
// if (piaoPingManager != null) {
|
||
// piaoPingManager.subscribe();
|
||
// }
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onActivityResumed(Activity activity) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onActivityPaused(Activity activity) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onActivityStopped(Activity activity) {
|
||
mCount--;
|
||
if (mCount == 0) {
|
||
mFront = false;
|
||
LogUtils.e("AppContext------->处于后台");
|
||
EventBus.getDefault().post(new AppLifecycleEvent(false));
|
||
CommonAppConfig.getInstance().setFrontGround(false);
|
||
// FloatWindowHelper.setFloatWindowVisible(false);
|
||
AppLifecycleUtil.onAppBackGround();
|
||
if (activity != null && !activity.isFinishing()) {
|
||
activity.runOnUiThread(() -> {
|
||
// if (piaoPingManager != null) {
|
||
// piaoPingManager.unsubscribe();
|
||
// }
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onActivityDestroyed(Activity activity) {
|
||
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 获取App签名md5值
|
||
*/
|
||
public String getAppSignature() {
|
||
try {
|
||
PackageInfo info =
|
||
this.getPackageManager().getPackageInfo(this.getPackageName(),
|
||
PackageManager.GET_SIGNATURES);
|
||
if (info != null) {
|
||
Signature[] signs = info.signatures;
|
||
byte[] bytes = signs[0].toByteArray();
|
||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||
md.update(bytes);
|
||
bytes = md.digest();
|
||
StringBuilder stringBuilder = new StringBuilder(2 * bytes.length);
|
||
for (int i = 0; ; i++) {
|
||
if (i >= bytes.length) {
|
||
return stringBuilder.toString();
|
||
}
|
||
String str = Integer.toString(0xFF & bytes[i], 16);
|
||
if (str.length() == 1) {
|
||
str = "0" + str;
|
||
}
|
||
stringBuilder.append(str);
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 获取facebook散列秘钥
|
||
*/
|
||
public String getFacebookHashKey() {
|
||
try {
|
||
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
|
||
for (Signature signature : info.signatures) {
|
||
MessageDigest md = MessageDigest.getInstance("SHA");
|
||
md.update(signature.toByteArray());
|
||
return Base64.encodeToString(md.digest(), Base64.DEFAULT);
|
||
}
|
||
} catch (Exception e) {
|
||
return "get error";
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public boolean isFront() {
|
||
return mFront;
|
||
}
|
||
|
||
public void startInitSdk() {
|
||
UserBean userBean = SpUtil.getUserBean();
|
||
if (userBean != null) {
|
||
// 在用户 UI 点击登录的时候调用
|
||
TUILogin.login(getBaseContext(), CommonAppContext.getInstance().getCurrentEnvironment().getSdkAppId(), "u" + userBean.getUser_id(), userBean.getTencent_im(), new TUICallback() {
|
||
@Override
|
||
public void onError(final int code, final String desc) {
|
||
LogUtils.e("@@@1", code, "描述:", desc);
|
||
}
|
||
|
||
@Override
|
||
public void onSuccess() {
|
||
LogUtils.e("@@@", "成功");
|
||
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
public void setUser(UserBean userBean) {
|
||
mUserBean = userBean;
|
||
SpUtil.saveUserId(userBean.getUser_id());
|
||
SpUtil.saveUserBean(userBean);
|
||
SpUtil.putToken(userBean.getToken());
|
||
}
|
||
|
||
public UserBean getUser() {
|
||
if (mUserBean == null) {
|
||
mUserBean = SpUtil.getUserBean();
|
||
}
|
||
return mUserBean;
|
||
}
|
||
|
||
public void setUserBean(UserBean bean) {
|
||
mUserBean = bean;
|
||
}
|
||
|
||
public void clearLoginInfo() throws ClassNotFoundException {
|
||
// mUid = null;
|
||
// mToken = null;
|
||
// SpUtil.getInstance().removeValue(
|
||
// SPConstants.USER_ID, SPConstants.TOKEN, SPConstants.USER_INFO
|
||
// );
|
||
isShow = false;
|
||
isPlaying = false;
|
||
mUserBean = null;
|
||
SpUtil.saveUserId(-1);
|
||
SpUtil.saveUserBean(new UserBean());
|
||
SpUtil.putToken("");
|
||
|
||
// piaoPingManager.unsubscribe();
|
||
FileUtils.deleteAllInDir(getCacheDir());
|
||
FileUtils.deleteAllInDir(getExternalCacheDir());
|
||
AgoraManager.getInstance(getApplicationContext()).destroy();
|
||
// 每次启动应用时重置状态
|
||
SpUtil.setBooleanValue("youth_model_shown", false);
|
||
|
||
// 发送广播通知所有Activity刷新状态
|
||
Intent refreshIntent = new Intent("com.xscm.moduleutil.ACTION_USER_LOGOUT");
|
||
sendBroadcast(refreshIntent);
|
||
Intent intent = new Intent("com.xscm.action.LAUNCH_PAGE");
|
||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
getApplicationContext().startActivity(intent);
|
||
}
|
||
|
||
public static boolean isAlipayInstalled(Context context) {
|
||
try {
|
||
context.getPackageManager().getPackageInfo("com.eg.android.AlipayGphone", 0);
|
||
return true;//安装了支付宝
|
||
} catch (PackageManager.NameNotFoundException e) {
|
||
return false; //未安装支付宝
|
||
}
|
||
}
|
||
|
||
//static 代码段可以防止内存泄露
|
||
static {
|
||
//设置全局的Header构建器
|
||
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
|
||
@Override
|
||
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
|
||
ClassicsHeader header = new ClassicsHeader(context);
|
||
header.setDrawableSize(20);
|
||
header.setFinishDuration(0);
|
||
return header;//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header
|
||
// return new CustomRefreshHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header
|
||
}
|
||
});
|
||
//设置全局的Footer构建器
|
||
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
|
||
@Override
|
||
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
|
||
ClassicsFooter classicsFooter = new ClassicsFooter(context);
|
||
classicsFooter.setDrawableSize(20);
|
||
classicsFooter.setFinishDuration(0);
|
||
//指定为经典Footer,默认是 BallPulseFooter
|
||
return classicsFooter;
|
||
}
|
||
});
|
||
}
|
||
}
|