303 lines
10 KiB
Java
303 lines
10 KiB
Java
package com.qxcm.moduleutil.base;
|
|
|
|
import android.app.Activity;
|
|
import android.app.ActivityManager;
|
|
import android.app.Application;
|
|
import android.app.LauncherActivity;
|
|
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.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.text.TextUtils;
|
|
import android.util.Base64;
|
|
import android.webkit.WebView;
|
|
|
|
import androidx.multidex.MultiDex;
|
|
import androidx.multidex.MultiDexApplication;
|
|
|
|
import com.blankj.utilcode.util.AppUtils;
|
|
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.qxcm.moduleutil.bean.UserBean;
|
|
import com.qxcm.moduleutil.event.AppLifecycleEvent;
|
|
import com.qxcm.moduleutil.interfaces.AppLifecycleUtil;
|
|
import com.qxcm.moduleutil.utils.FloatWindowHelper;
|
|
import com.qxcm.moduleutil.utils.SPConstants;
|
|
import com.qxcm.moduleutil.utils.SpUtil;
|
|
import com.qxcm.moduleutil.utils.UtilConfig;
|
|
import com.qxcm.moduleutil.utils.config.EnvironmentEnum;
|
|
import com.qxcm.moduleutil.utils.config.EnvironmentPrefs;
|
|
import com.qxcm.moduleutil.widget.CommonAppConfig;
|
|
import com.qxcm.moduleutil.widget.Constants;
|
|
|
|
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 java.util.concurrent.TimeUnit;
|
|
|
|
import io.reactivex.disposables.Disposable;
|
|
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;
|
|
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
|
|
private ScheduledExecutorService scheduledExecutorServiceRoom = null;
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
sInstance = this;
|
|
sMainThreadHandler=new Handler();
|
|
UtilConfig.init(this);
|
|
registerActivityLifecycleCallbacks();
|
|
initWebView();
|
|
if (ProcessUtils.isMainProcess()) {
|
|
if (SpUtil.isAgreePolicy()) {
|
|
checkInEmulator();
|
|
UtilConfig.checkInEmulator();
|
|
}
|
|
}
|
|
EnvironmentPrefs prefs = new EnvironmentPrefs(this);
|
|
currentEnvironment = prefs.getSelectedEnvironment();
|
|
}
|
|
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();
|
|
|
|
}
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|
|
|
|
@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(){
|
|
|
|
}
|
|
|
|
public void setUser(UserBean userBean) {
|
|
mUserBean = userBean;
|
|
SpUtil.saveUserId(userBean.getUser_id());
|
|
SpUtil.saveUserInfo(userBean);
|
|
SpUtil.putToken(userBean.getToken());
|
|
}
|
|
|
|
public UserBean getUser() {
|
|
if (mUserBean == null) {
|
|
mUserBean = SpUtil.getUserInfo();
|
|
}
|
|
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
|
|
// );
|
|
mUserBean = null;
|
|
SpUtil.saveUserId(-1);
|
|
SpUtil.saveUserInfo(new UserBean());
|
|
SpUtil.putToken("");
|
|
Intent intent = new Intent("com.example.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; //未安装支付宝
|
|
}
|
|
}
|
|
}
|