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

270 lines
10 KiB
Java
Raw Normal View History

2022-07-18 15:31:45 +08:00
package com.shayu.phonelive;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.http.HttpResponseCache;
import android.os.Bundle;
import android.os.Message;
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;
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;
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.Constants;
import com.yunbao.common.utils.L;
import com.yunbao.live.socket.SocketRyClient;
import com.yunbao.live.utils.LiveImDeletUtil;
import java.io.File;
import java.io.IOException;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.message.TextMessage;
import static com.adjust.sdk.AdjustConfig.ENVIRONMENT_PRODUCTION;
import static com.yunbao.live.socket.SocketClient.mSocketHandler;
//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 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) {
}
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
}
}
@Override
public void onCreate() {
super.onCreate();
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);
}
String environment = AdjustConfig.ENVIRONMENT_PRODUCTION;
AdjustConfig config = new AdjustConfig(this, "3om5fbglyqdc", environment);
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);
//谷歌激活
mFirebaseAnalytics.logEvent("FS_complete_1st_loading", null);
//FB激活
logger.logEvent("FB_complete_1st_loading");
//初始化 AndroidUtilCode
Utils.init(this);
liveImDeletUtil = new LiveImDeletUtil();
// 融云
RongIMClient.getInstance().setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() {
/**
* 接收实时或者离线消息
* 注意:
* 1. 针对接收离线消息时服务端会将 200 条消息打成一个包发到客户端客户端对这包数据进行解析
* 2. hasPackage 标识是否还有剩余的消息包left 标识这包消息解析完逐条抛送给 App 层后剩余多少条
* 如何判断离线消息收完
* 1. hasPackage left 都为 0
* 2. hasPackage 0 标识当前正在接收最后一包200条消息left 0 标识最后一包的最后一条消息也已接收完毕
*
* @param message 接收到的消息对象
* @param left 每个数据包数据逐条上抛后还剩余的条数
* @param hasPackage 是否在服务端还存在未下发的消息包
* @param offline 消息是否离线消息
* @return 是否处理消息 如果 App 处理了此消息返回 true; 否则返回 false SDK 处理
*/
@Override
public boolean onReceived(io.rong.imlib.model.Message message, int left, boolean hasPackage, boolean offline) {
Log.e("rytest", message.getContent() + "");
if (message.getConversationType() == Conversation.ConversationType.CHATROOM) {
//群聊
Message msg = Message.obtain();
msg.what = Constants.SOCKET_WHAT_BROADCAST;
if (!"".equals(message.getContent()) && message.getContent() != null) {
if (message.getObjectName().equals("RC:TxtMsg")) {
TextMessage content = (TextMessage) message.getContent();
msg.obj = content.getContent();
if (SocketRyClient.mSocketHandler != null && Ingroup == 1) {
SocketRyClient.mSocketHandler.sendMessage(msg);
}
TextMessage rongMsg = (TextMessage) message.getContent();
String firstIndexRM = "";
if (rongMsg.getContent().length() > 4) {
firstIndexRM = rongMsg.getContent().substring(0, 5);
}
String firstIndexRM2 = "";
if (rongMsg.getContent().length() > 9) {
firstIndexRM2 = rongMsg.getContent().substring(0, 10);
}
if ("{\"msg".equals(firstIndexRM) || "{\"retcode\"".equals(firstIndexRM2)) {
liveImDeletUtil.deleteMessages(message.getTargetId(), message.getMessageId());
}
}
}
} else {
//私聊
Message msg = Message.obtain();
msg.what = Constants.SOCKET_WHAT_BROADCAST;
if (!"".equals(message.getContent()) && message.getContent() != null) {
if (message.getObjectName().equals("RC:TxtMsg")) {
TextMessage content = (TextMessage) message.getContent();
msg.obj = content.getContent();
if (content.getContent().contains("_method_")) {
if (SocketRyClient.mSocketHandler != null) {
CommonAppContext.Ingroup = 1;
SocketRyClient.mSocketHandler.sendMessage(msg);
}
}
TextMessage rongMsg = (TextMessage) message.getContent();
String firstIndexRM = "";
if (rongMsg.getContent().length() > 4) {
firstIndexRM = rongMsg.getContent().substring(0, 5);
}
String firstIndexRM2 = "";
if (rongMsg.getContent().length() > 9) {
firstIndexRM2 = rongMsg.getContent().substring(0, 10);
}
if ("{\"msg".equals(firstIndexRM) || "{\"retcode\"".equals(firstIndexRM2)) {
liveImDeletUtil.deleteMessages(message.getTargetId(), message.getMessageId());
}
}
}
}
return false;
}
});
//监听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);
}
}
});
}
public boolean isMainProcess() {
int pid = android.os.Process.myPid();
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return getApplicationInfo().packageName.equals(appProcess.processName);
}
}
return false;
}
}