pdlivexp/app/src/main/java/com/shayu/phonelive/AppContext.java

369 lines
14 KiB
Java
Raw Normal View History

2022-07-18 15:31:45 +08:00
package com.shayu.phonelive;
2022-10-17 15:10:03 +08:00
import static com.yunbao.live.socket.SocketClient.mSocketHandler;
2022-07-18 15:31:45 +08:00
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.http.HttpResponseCache;
2022-10-24 17:55:43 +08:00
import android.os.Build;
2022-07-18 15:31:45 +08:00
import android.os.Bundle;
2022-10-24 17:55:43 +08:00
import android.os.Handler;
import android.os.Looper;
2022-07-18 15:31:45 +08:00
import android.os.Message;
import android.text.TextUtils;
2022-07-18 15:31:45 +08:00
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.AdjustEvent;
import com.adjust.sdk.LogLevel;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.Utils;
import com.facebook.appevents.AppEventsLogger;
import com.fm.openinstall.OpenInstall;
2022-10-24 17:55:43 +08:00
import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
2022-07-18 15:31:45 +08:00
import com.tencent.imsdk.v2.V2TIMGroupMemberInfo;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMSimpleMsgListener;
import com.tencent.imsdk.v2.V2TIMUserInfo;
import com.yunbao.common.BuildConfig;
2022-09-28 16:25:57 +08:00
import com.yunbao.common.CommonAppConfig;
2022-07-18 15:31:45 +08:00
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.Constants;
import com.yunbao.common.bean.CrashSaveBean;
2022-07-20 16:57:42 +08:00
import com.yunbao.common.http.CommonHttpUtil;
import com.yunbao.common.http.HttpCallback;
2022-09-06 14:35:46 +08:00
import com.yunbao.common.manager.imrongcloud.InstructorSendReward;
import com.yunbao.common.manager.imrongcloud.InstructorSendRewardProvider;
import com.yunbao.common.manager.imrongcloud.MessageIMManager;
2022-09-06 14:35:46 +08:00
import com.yunbao.common.manager.imrongcloud.RecommendLiveRoom;
import com.yunbao.common.manager.imrongcloud.RongcloudIMManager;
2022-07-18 15:31:45 +08:00
import com.yunbao.common.utils.L;
2022-09-05 17:46:43 +08:00
import com.yunbao.common.utils.SpUtil;
import com.yunbao.faceunity.FaceManager;
2022-07-22 17:49:50 +08:00
import com.yunbao.live.socket.SocketRyClient;
2022-07-18 15:31:45 +08:00
import com.yunbao.live.utils.LiveImDeletUtil;
import com.yunbao.live.views.PortraitLiveManager;
2022-10-17 15:10:03 +08:00
import com.yunbao.live.views.RecommendLiveRoomProvider;
2022-09-05 17:46:43 +08:00
import com.yunbao.main.activity.MsgSettActivity;
2022-07-18 15:31:45 +08:00
2022-10-17 15:10:03 +08:00
import org.greenrobot.eventbus.EventBus;
2022-07-18 15:31:45 +08:00
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
2022-09-06 14:35:46 +08:00
import java.util.ArrayList;
2022-10-24 17:55:43 +08:00
import java.util.Arrays;
import java.util.List;
2022-07-18 15:31:45 +08:00
2022-09-06 14:35:46 +08:00
import io.rong.imkit.config.RongConfigCenter;
2022-07-22 17:49:50 +08:00
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
2022-09-06 14:35:46 +08:00
import io.rong.imlib.model.MessageContent;
2022-07-22 17:49:50 +08:00
import io.rong.message.TextMessage;
2022-07-18 15:31:45 +08:00
//import cn.tillusory.sdk.TiSDK;
//import com.squareup.leakcanary.LeakCanary;
/**
* Created by cxf on 2017/8/3.
*/
public class AppContext extends CommonAppContext {
public static AppContext sInstance;
public LiveImDeletUtil liveImDeletUtil;
private final static List<WeakReference<Activity>> activities = new ArrayList<>();
2022-07-18 15:31:45 +08:00
private static final class AdjustLifecycleCallbacks implements ActivityLifecycleCallbacks {
@Override
public void onActivityResumed(Activity activity) {
Adjust.onResume();
}
@Override
public void onActivityPaused(Activity activity) {
Adjust.onPause();
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
2022-10-17 15:10:03 +08:00
for (WeakReference<Activity> reference : activities) {
if (reference.get() == activity) {
2022-10-17 15:10:03 +08:00
activities.remove(reference);
return;
}
}
2022-07-18 15:31:45 +08:00
}
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
activities.add(new WeakReference<>(activity));
2022-10-27 18:30:00 +08:00
CrashSaveBean.getInstance().setActivitySize(activities);
2022-07-18 15:31:45 +08:00
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
}
}
@Override
public void onCreate() {
super.onCreate();
//注册全局异常捕获
if (!isMainProcess()) {
return;
}
CrashSaveBean.getInstance().setStartTime(System.currentTimeMillis());
registerError();
2022-10-24 17:55:43 +08:00
registerFirebaseCrash();
2022-07-18 15:31:45 +08:00
sInstance = this;
L.setDeBug(BuildConfig.DEBUG);
AppEventsLogger.activateApp(this);
2022-10-24 17:55:43 +08:00
FirebaseAnalytics.getInstance(this);
2022-07-18 15:31:45 +08:00
File httpCacheDir = new File(getCacheDir(), "https");
try {
HttpResponseCache.install(httpCacheDir, 1024 * 1024 * 128);
} catch (IOException e) {
e.printStackTrace();
}
//初始化 ARouter
if (BuildConfig.DEBUG) {
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
2022-08-17 13:34:29 +08:00
2022-07-18 15:31:45 +08:00
//初始化 邀请码库
if (isMainProcess()) {
OpenInstall.init(this);
}
String environment = AdjustConfig.ENVIRONMENT_PRODUCTION;
AdjustConfig config = new AdjustConfig(this, "3om5fbglyqdc", environment);
2022-09-28 18:27:29 +08:00
if (CommonAppConfig.IS_GOOGLE_PLAY == false) {
config.setDefaultTracker("xa7k5ut");
2022-09-28 16:25:57 +08:00
}
2022-07-18 15:31:45 +08:00
Adjust.onCreate(config);
registerActivityLifecycleCallbacks(new AdjustLifecycleCallbacks());
config.setLogLevel(LogLevel.WARN);
//激活操作记录
AdjustEvent adjustEvent = new AdjustEvent("m7wk0c");
Adjust.trackEvent(adjustEvent);
//谷歌激活
mFirebaseAnalytics.logEvent("FS_activation", null);
//FB激活
logger.logEvent("FB_activation");
AdjustEvent adjustEvent2 = new AdjustEvent("p7igfz");
Adjust.trackEvent(adjustEvent2);
2022-07-20 16:57:42 +08:00
CommonHttpUtil.setAdvertisingChannels("p7igfz", new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
2022-07-22 17:49:50 +08:00
if (code == 0) {
2022-07-20 16:57:42 +08:00
//谷歌激活
mFirebaseAnalytics.logEvent("FS_complete_1st_loading", null);
//FB激活
logger.logEvent("FB_complete_1st_loading");
}
}
});
2022-07-18 15:31:45 +08:00
//初始化 AndroidUtilCode
Utils.init(this);
liveImDeletUtil = new LiveImDeletUtil();
2022-07-22 17:49:50 +08:00
2022-07-21 11:24:15 +08:00
//初始化融云
RongcloudIMManager.initRongIM(this);
2022-09-06 14:35:46 +08:00
ArrayList<Class<? extends MessageContent>> myMessages = new ArrayList<>();
myMessages.add(RecommendLiveRoom.class);
myMessages.add(InstructorSendReward.class);
RongIMClient.registerMessageType(myMessages);
// 注册自定义消息模板
2022-09-06 16:36:34 +08:00
RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider(getApplicationContext()));
2022-09-06 14:35:46 +08:00
RongConfigCenter.conversationConfig().addMessageProvider(new RecommendLiveRoomProvider(getApplicationContext()));
2022-07-22 16:27:19 +08:00
2022-07-25 16:06:39 +08:00
RongcloudIMManager.addRongcloudIMOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() {
@Override
public boolean onReceived(io.rong.imlib.model.Message message, int i, boolean b, boolean b1) {
Log.e("wewe", message.getConversationType() + "112121");
2022-07-26 13:14:43 +08:00
MessageIMManager.get(sInstance).getSystemForRongcloud(message);
2022-07-25 16:06:39 +08:00
//融云直播间聊天
Message msg = Message.obtain();
msg.what = Constants.SOCKET_WHAT_BROADCAST;
try {
if (!"".equals(message.getContent()) && message.getContent() != null) {
TextMessage content = (TextMessage) message.getContent();
if (content.getContent().contains("_method_")) {
msg.obj = content.getContent();
2022-09-01 15:12:58 +08:00
//观众页面
if (SocketRyClient.mSocketHandler != null
&& (TextUtils.equals("__system__", message.getTargetId())
2022-08-26 16:37:12 +08:00
|| ("g" + PortraitLiveManager.liveID).contains(message.getTargetId()))) {
2022-07-25 16:06:39 +08:00
SocketRyClient.mSocketHandler.sendMessage(msg);
}
2022-09-01 15:12:58 +08:00
//主播页面
if (TextUtils.isEmpty(PortraitLiveManager.liveID) && SocketRyClient.mSocketHandler != null) {
SocketRyClient.mSocketHandler.sendMessage(msg);
}
2022-09-17 16:54:58 +08:00
} else if (message.getConversationType() == Conversation.ConversationType.PRIVATE) {//私聊信息
EventBus.getDefault().post(message);
2022-07-25 16:06:39 +08:00
}
}
} catch (Exception e) {
2022-09-17 16:54:58 +08:00
e.printStackTrace();
2022-07-25 16:06:39 +08:00
}
return false;
}
});
2022-07-18 15:31:45 +08:00
2022-08-26 13:00:45 +08:00
2022-07-18 15:31:45 +08:00
//监听im消息
V2TIMManager.getInstance().addSimpleMsgListener(new V2TIMSimpleMsgListener() {
@Override
public void onRecvGroupTextMessage(String msgID, String groupID, V2TIMGroupMemberInfo sender, String text) {
super.onRecvGroupTextMessage(msgID, groupID, sender, text);
Log.e("asasas", text);
Message msg = Message.obtain();
msg.what = Constants.SOCKET_WHAT_BROADCAST;
msg.obj = text;
if (mSocketHandler != null && Ingroup == 1) {
mSocketHandler.sendMessage(msg);
}
}
@Override
public void onRecvC2CTextMessage(String msgID, V2TIMUserInfo sender, String text) {
super.onRecvC2CTextMessage(msgID, sender, text);
Message msg = Message.obtain();
msg.what = Constants.SOCKET_WHAT_BROADCAST;
msg.obj = text;
if (mSocketHandler != null) {
CommonAppContext.Ingroup = 1;
mSocketHandler.sendMessage(msg);
}
}
2022-11-07 14:51:44 +08:00
2022-07-18 15:31:45 +08:00
});
2022-09-05 17:46:43 +08:00
configSPApp();
2022-09-17 16:54:58 +08:00
//初始化美颜SDK
FaceManager.initFaceUnity(this);
2022-09-05 17:46:43 +08:00
}
2022-07-18 15:31:45 +08:00
2022-09-05 17:46:43 +08:00
/**
* 配置SharedPreferences默认值
*/
2022-09-17 16:54:58 +08:00
private void configSPApp() {
if (!SpUtil.getInstance().isExists(MsgSettActivity.SWITCH_PRIVATE_CHAT_MSG)) {
SpUtil.getInstance().setBooleanValue(MsgSettActivity.SWITCH_PRIVATE_CHAT_MSG, true);
2022-09-05 17:46:43 +08:00
}
2022-07-18 15:31:45 +08:00
}
public boolean isMainProcess() {
int pid = android.os.Process.myPid();
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
2022-10-28 13:50:34 +08:00
if (activityManager.getRunningAppProcesses() == null) {
return true;
}
2022-07-18 15:31:45 +08:00
for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return getApplicationInfo().packageName.equals(appProcess.processName);
}
}
return false;
}
2022-07-21 11:24:15 +08:00
@Override
public void onTerminate() {
RongcloudIMManager.removeRongcloudIMOnReceiveMessageListener();
super.onTerminate();
}
/**
* 注册全局异常捕获有需要时可以在onCreate调用
*/
2022-08-26 16:37:12 +08:00
private void registerError() {
NeverCrashUtils.getInstance()
.setDebugMode(BuildConfig.DEBUG)
.setMainCrashHandler((t, e) -> {
Log.e("ApplicationError", "主线程异常");//此处log只是展示当debug为true时主类内部log会打印异常信息
e.printStackTrace();
//闪退后finish所有Activity并且杀死进程
for (WeakReference<Activity> activity : activities) {
if (activity != null && activity.get() != null) {
activity.get().finish();
}
}
2022-10-24 17:55:43 +08:00
/* Process.killProcess(Process.myPid());
System.exit(0);*/
setFirebaseCrashData();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
throw new RuntimeException(e);
}, 100);
})
.setUncaughtCrashHandler((t, e) -> {
Log.e("ApplicationError", "子线程异常");//此处log只是展示当debug为true时主类内部log会打印异常信息
e.printStackTrace();
})
.register(this);
}
public void registerFirebaseCrash() {
2022-10-24 18:26:31 +08:00
if (!CommonAppConfig.IS_UPLOAD_ERROR_LOG) {
return;
}
2022-10-24 17:55:43 +08:00
FirebaseAnalytics.getInstance(this);
FirebaseApp.initializeApp(this);
}
2022-10-28 13:50:34 +08:00
private void setFirebaseCrashData() {
if (!CommonAppConfig.IS_UPLOAD_ERROR_LOG) {
return;
}
String uid = CommonAppConfig.getInstance().getUid();
2022-10-24 17:55:43 +08:00
FirebaseCrashlytics.getInstance().setUserId(CommonAppConfig.getInstance().getUid());
if (uid != null && !uid.isEmpty()) {
FirebaseCrashlytics.getInstance().setCustomKey("uid", uid);
} else {
FirebaseCrashlytics.getInstance().setCustomKey("uid", "uid is null");
}
FirebaseCrashlytics.getInstance().setCustomKey("userData", SpUtil.getInstance().getStringValue(SpUtil.USER_INFO));
FirebaseCrashlytics.getInstance().setCustomKey("isGoogle", CommonAppConfig.IS_GOOGLE_PLAY);
2022-10-24 17:55:43 +08:00
FirebaseCrashlytics.getInstance().setCustomKey("PhoneName", Build.BRAND);
FirebaseCrashlytics.getInstance().setCustomKey("Phone", Build.MODEL);
FirebaseCrashlytics.getInstance().setCustomKey("CPU", Arrays.toString(Build.SUPPORTED_ABIS));
FirebaseCrashlytics.getInstance().setCustomKey("RunTime", (System.currentTimeMillis() - CrashSaveBean.getInstance().getStartTime()));
FirebaseCrashlytics.getInstance().setCustomKey("enterRoom", CrashSaveBean.getInstance().getEnterRoom());
FirebaseCrashlytics.getInstance().setCustomKey("slidingRoom", CrashSaveBean.getInstance().getSlidingRoom());
FirebaseCrashlytics.getInstance().setCustomKey("playSvga", CrashSaveBean.getInstance().getPlaySvga());
2022-10-27 17:56:27 +08:00
FirebaseCrashlytics.getInstance().setCustomKey("ActivitySize", CrashSaveBean.getInstance().getActivitySize());
2022-10-24 17:55:43 +08:00
}
2022-07-18 15:31:45 +08:00
}