package com.shayu.phonelive; import static com.yunbao.live.socket.SocketClient.mSocketHandler; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.net.http.HttpResponseCache; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.Process; import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.alibaba.android.arouter.launcher.ARouter; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.blankj.utilcode.util.Utils; import com.facebook.appevents.AppEventsLogger; import com.fm.openinstall.OpenInstall; import com.google.gson.Gson; import com.shayu.phonelive.utils.LogUtils; 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.umeng.analytics.MobclickAgent; import com.umeng.commonsdk.UMConfigure; import com.yunbao.common.BuildConfig; import com.yunbao.common.CommonAppConfig; import com.yunbao.common.CommonAppContext; import com.yunbao.common.Constants; import com.yunbao.common.bean.AnchorStartLiveBean; import com.yunbao.common.bean.CrashSaveBean; import com.yunbao.common.event.SudGameSocketImEvent; import com.yunbao.common.manager.imrongcloud.InstructorSendReward; import com.yunbao.common.manager.imrongcloud.InstructorSendRewardProvider; import com.yunbao.common.manager.imrongcloud.MessageIMManager; import com.yunbao.common.manager.imrongcloud.RecommendLiveRoom; import com.yunbao.common.manager.imrongcloud.RongcloudIMManager; import com.yunbao.common.utils.AppManager; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.GoogleUtils; import com.yunbao.common.utils.L; import com.yunbao.common.utils.SpUtil; import com.yunbao.live.socket.SocketReceiveBean; import com.yunbao.live.socket.SocketRyClient; import com.yunbao.live.utils.LiveImDeletUtil; import com.yunbao.live.views.PortraitLiveManager; import com.yunbao.live.views.RecommendLiveRoomProvider; import com.yunbao.main.activity.MsgSettActivity; import org.greenrobot.eventbus.EventBus; import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import io.rong.imkit.config.RongConfigCenter; import io.rong.imlib.RongIMClient; import io.rong.imlib.model.Conversation; import io.rong.imlib.model.MessageContent; import io.rong.message.TextMessage; //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> activities = new ArrayList<>(); private static final class AdjustLifecycleCallbacks implements ActivityLifecycleCallbacks { @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(@NonNull Activity activity) { } @Override public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) { } @Override public void onActivityDestroyed(@NonNull Activity activity) { for (WeakReference reference : activities) { if (reference.get() == activity) { activities.remove(reference); return; } } AppManager.getInstance().removeActivity(activity); } @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { activities.add(new WeakReference<>(activity)); CrashSaveBean.getInstance().setActivitySize(activities); AppManager.getInstance().addActivity(activity); } @Override public void onActivityStarted(@NonNull Activity activity) { } } @Override public void onCreate() { super.onCreate(); if (!isMainProcess()) { return; } CrashSaveBean.getInstance().setStartTime(System.currentTimeMillis()); //注册全局异常捕获 registerError(); registerFirebaseCrash(); LogUtils.start(this); sInstance = this; L.setDeBug(BuildConfig.DEBUG); AppEventsLogger.activateApp(this); 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); //初始化 邀请码库 if (isMainProcess()) { OpenInstall.init(this); } //设置LOG开关,默认为false UMConfigure.setLogEnabled(true); //友盟正式初始化 UMConfigure.init(getApplicationContext(), "64e40ee55488fe7b3afa2c96", "PDLive", UMConfigure.DEVICE_TYPE_PHONE, "64e40ee55488fe7b3afa2c96"); //集成umeng-crash-vx.x.x.aar,则需要关闭原有统计SDK异常捕获功能 MobclickAgent.setCatchUncaughtExceptions(false); //PushSDK初始化(如使用推送SDK,必须调用此方法) //统计SDK是否支持采集在子进程中打点的自定义事件,默认不支持 UMConfigure.setProcessEvent(true);//支持多进程打点 MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO); UMConfigure.submitPolicyGrantResult(getApplicationContext(), true); registerActivityLifecycleCallbacks(new AdjustLifecycleCallbacks()); //初始化 AndroidUtilCode Utils.init(this); liveImDeletUtil = new LiveImDeletUtil(); //初始化融云 RongcloudIMManager.initRongIM(this); ArrayList> myMessages = new ArrayList<>(); myMessages.add(RecommendLiveRoom.class); myMessages.add(InstructorSendReward.class); RongIMClient.registerMessageType(myMessages); // 注册自定义消息模板 RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider(getApplicationContext())); RongConfigCenter.conversationConfig().addMessageProvider(new RecommendLiveRoomProvider(getApplicationContext())); 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"); MessageIMManager.get(sInstance).getSystemForRongcloud(message); //融云直播间聊天 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(); //观众页面 if (SocketRyClient.mSocketHandler != null && (TextUtils.equals("__system__", message.getTargetId()) || ("g" + PortraitLiveManager.liveID).contains(message.getTargetId()))) { SocketRyClient.mSocketHandler.sendMessage(msg); } else if (content.getContent().contains("LivePK_anchorStartLiveNotify")) {//开播通知 SocketReceiveBean received = JSON.parseObject(content.getContent(), SocketReceiveBean.class); JSONObject map = received.getMsg().getJSONObject(0); sendStartAnchorLive(map); } else if (message.getTargetId().contains("v")) { String contentJson = ((TextMessage) message.getContent()).getContent(); Log.e("wewe", contentJson); SudGameSocketImEvent sudGameSocketImEvent = new Gson().fromJson(contentJson, SudGameSocketImEvent.class); Bus.get().post(sudGameSocketImEvent); } //主播页面 if (TextUtils.isEmpty(PortraitLiveManager.liveID) && SocketRyClient.mSocketHandler != null) { SocketRyClient.mSocketHandler.sendMessage(msg); } } else if (message.getConversationType() == Conversation.ConversationType.PRIVATE) {//私聊信息 EventBus.getDefault().post(message); } } } catch (Exception e) { e.printStackTrace(); } return false; } private void sendStartAnchorLive(JSONObject map) { AnchorStartLiveBean bean = new AnchorStartLiveBean(); bean.setAnchorName(map.getString("anchorName")); bean.setRoomId(map.getString("roomid")); bean.setAvatar(map.getString("avatar")); EventBus.getDefault().post(bean); } }); //监听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); } } }); configSPApp(); //初始化美颜SDK // FaceManager.initFaceUnity(this); } /** * 配置SharedPreferences默认值 */ private void configSPApp() { if (!SpUtil.getInstance().isExists(MsgSettActivity.SWITCH_PRIVATE_CHAT_MSG)) { SpUtil.getInstance().setBooleanValue(MsgSettActivity.SWITCH_PRIVATE_CHAT_MSG, true); } } public boolean isMainProcess() { int pid = android.os.Process.myPid(); ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); if (activityManager.getRunningAppProcesses() == null) { return true; } for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) { if (appProcess.pid == pid) { return getApplicationInfo().packageName.equals(appProcess.processName); } } return false; } @Override public void onTerminate() { RongcloudIMManager.removeRongcloudIMOnReceiveMessageListener(); super.onTerminate(); } /** * 注册全局异常捕获,有需要时可以在onCreate调用 */ private void registerError() { NeverCrashUtils.getInstance().setDebugMode(BuildConfig.DEBUG).setMainCrashHandler((t, e) -> { Log.e("ApplicationError", "主线程异常");//此处log只是展示,当debug为true时,主类内部log会打印异常信息 e.printStackTrace(); AppManager.runDebugCode(() -> { //闪退后finish所有Activity并且杀死进程 for (WeakReference activity : activities) { if (activity != null && activity.get() != null) { activity.get().finish(); } } 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() { if (!CommonAppConfig.IS_UPLOAD_ERROR_LOG) { return; } if (CommonAppConfig.IS_GOOGLE_PLAY != 2) { GoogleUtils.newInstance(this).initializeApp(this); } } public static void setFirebaseCrashData() { if (!CommonAppConfig.IS_UPLOAD_ERROR_LOG) { return; } String uid = CommonAppConfig.getInstance().getUid(); if (CommonAppConfig.IS_GOOGLE_PLAY != 2) { GoogleUtils.newInstance(sInstance).setFirebaseCrashData( uid, SpUtil.getInstance().getStringValue(SpUtil.USER_INFO), String.valueOf(CommonAppConfig.IS_GOOGLE_PLAY), Arrays.toString(Build.SUPPORTED_ABIS), String.valueOf(CrashSaveBean.getInstance().getStartTime()), String.valueOf(CrashSaveBean.getInstance().getEnterRoom()), String.valueOf(CrashSaveBean.getInstance().getSlidingRoom()), String.valueOf(CrashSaveBean.getInstance().getPlaySvga()), CrashSaveBean.getInstance().getActivitySize()); } } }