From 9707898af9c331d814ad2b4879d22a885ff0387e Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 8 Apr 2023 15:45:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=9B=B4=E6=92=AD=E9=97=B4UI?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E5=BF=83=E6=84=BF=E5=8D=95=E6=B5=AE?= =?UTF-8?q?=E7=AA=97=E4=B8=80=E5=A0=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shayu/phonelive/AppContext.java | 33 ++++++++++++------- .../com/shayu/phonelive/NeverCrashUtils.java | 23 +++++++------ .../common/http/live/LiveNetManager.java | 1 + .../com/yunbao/common/utils/AppManager.java | 11 +++++++ .../presenter/LiveRoomCheckLivePresenter.java | 3 ++ .../yunbao/live/views/LiveRoomViewHolder.java | 6 +++- .../live/views/PortraitLiveManager.java | 32 +++++++++++++++--- .../layout/view_live_top_banner_call_me.xml | 2 +- 8 files changed, 84 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/shayu/phonelive/AppContext.java b/app/src/main/java/com/shayu/phonelive/AppContext.java index 31fefcb5f..3783dfb0d 100644 --- a/app/src/main/java/com/shayu/phonelive/AppContext.java +++ b/app/src/main/java/com/shayu/phonelive/AppContext.java @@ -13,6 +13,7 @@ import android.os.Looper; import android.os.Message; import android.text.TextUtils; import android.util.Log; +import android.os.Process; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -42,6 +43,7 @@ import com.yunbao.common.manager.imrongcloud.RongcloudIMManager; import com.yunbao.common.utils.AppManager; import com.yunbao.common.utils.L; import com.yunbao.common.utils.SpUtil; +import com.yunbao.common.utils.ToastUtil; import com.yunbao.live.socket.SocketRyClient; import com.yunbao.live.utils.LiveImDeletUtil; import com.yunbao.live.views.PortraitLiveManager; @@ -128,6 +130,12 @@ public class AppContext extends CommonAppContext { if (!isMainProcess()) { return; } + AppManager.runDebugCode(new Runnable() { + @Override + public void run() { + ToastUtil.show("Debug代码"); + } + }); CrashSaveBean.getInstance().setStartTime(System.currentTimeMillis()); //注册全局异常捕获 registerError(); @@ -280,18 +288,21 @@ public class AppContext extends CommonAppContext { .setMainCrashHandler((t, e) -> { Log.e("ApplicationError", "主线程异常");//此处log只是展示,当debug为true时,主类内部log会打印异常信息 e.printStackTrace(); - //闪退后finish所有Activity并且杀死进程 - /* for (WeakReference activity : activities) { - if (activity != null && activity.get() != null) { - activity.get().finish(); + 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);*/ + 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会打印异常信息 diff --git a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java index 5e7fecaa1..e3593332a 100644 --- a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java +++ b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java @@ -10,6 +10,7 @@ import android.widget.Toast; import com.yunbao.common.BuildConfig; import com.yunbao.common.bean.CrashSaveBean; +import com.yunbao.common.utils.AppManager; import com.yunbao.common.utils.FileUtil; import com.yunbao.common.utils.SpUtil; @@ -91,10 +92,12 @@ public class NeverCrashUtils { * * @param application application */ + private boolean errorWhile = true; + public void register(Application application) { //主线程异常拦截 new Handler(Looper.getMainLooper()).post(() -> { - while (true) { + while (errorWhile) { try { Looper.loop(); } catch (Throwable e) { @@ -102,12 +105,12 @@ public class NeverCrashUtils { Log.e(TAG, "未捕获的主线程异常行为", e); } e.printStackTrace(); - if(BuildConfig.DEBUG) { - Toast.makeText(application, "发生闪退:"+e.getMessage(), Toast.LENGTH_SHORT).show(); - } + + AppManager.runDebugCode(() -> Toast.makeText(application, "发生闪退:" + e.getMessage(), Toast.LENGTH_SHORT).show()); FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()), throwableToString(e), "error.log"); getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); - // return; + AppManager.runDebugCode(() -> errorWhile = false); + // return; } } }); @@ -142,11 +145,11 @@ public class NeverCrashUtils { writer.write("PhoneName=" + Build.BRAND + "\n"); writer.write("Phone=" + Build.MODEL + "\n"); writer.write("CPU=" + Arrays.toString(Build.SUPPORTED_ABIS) + "\n"); - writer.write("runTime=" + (System.currentTimeMillis() - CrashSaveBean.getInstance().getStartTime())+ "\n"); - writer.write("enterRoom=" + CrashSaveBean.getInstance().getEnterRoom()+ "\n"); - writer.write("slidingRoom=" + CrashSaveBean.getInstance().getSlidingRoom()+ "\n"); - writer.write("playSvga=" + CrashSaveBean.getInstance().getPlaySvga()+ "\n"); - writer.write("ActivitySize=" + CrashSaveBean.getInstance().getActivitySize()+ "\n"); + writer.write("runTime=" + (System.currentTimeMillis() - CrashSaveBean.getInstance().getStartTime()) + "\n"); + writer.write("enterRoom=" + CrashSaveBean.getInstance().getEnterRoom() + "\n"); + writer.write("slidingRoom=" + CrashSaveBean.getInstance().getSlidingRoom() + "\n"); + writer.write("playSvga=" + CrashSaveBean.getInstance().getPlaySvga() + "\n"); + writer.write("ActivitySize=" + CrashSaveBean.getInstance().getActivitySize() + "\n"); writer.write("UserData=" + SpUtil.getInstance().getStringValue(SpUtil.USER_INFO) + "\n"); writer.write("[ERROR]"); PrintWriter printWriter = new PrintWriter(writer); diff --git a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java index 12e846038..fa80df2a6 100644 --- a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java +++ b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java @@ -371,6 +371,7 @@ public class LiveNetManager { }, new Consumer() { @Override public void accept(Throwable throwable) throws Exception { + Log.e("异常", "checkLive: ",throwable); if (callBack != null) { callBack.onError(throwable.getMessage()); } diff --git a/common/src/main/java/com/yunbao/common/utils/AppManager.java b/common/src/main/java/com/yunbao/common/utils/AppManager.java index 95f7d5a83..5bda4db57 100644 --- a/common/src/main/java/com/yunbao/common/utils/AppManager.java +++ b/common/src/main/java/com/yunbao/common/utils/AppManager.java @@ -2,6 +2,8 @@ package com.yunbao.common.utils; import android.app.Activity; +import com.yunbao.common.BuildConfig; + import java.util.Stack; public class AppManager { @@ -114,6 +116,15 @@ public class AppManager { activityStack.clear(); } + /** + *仅在debug下运行的代码 + */ + public static void runDebugCode(Runnable runnable) { + if (BuildConfig.DEBUG) { + runnable.run(); + } + } + /** * 退出应用程序 diff --git a/live/src/main/java/com/yunbao/live/presenter/LiveRoomCheckLivePresenter.java b/live/src/main/java/com/yunbao/live/presenter/LiveRoomCheckLivePresenter.java index eb3670fe7..3a2cebade 100644 --- a/live/src/main/java/com/yunbao/live/presenter/LiveRoomCheckLivePresenter.java +++ b/live/src/main/java/com/yunbao/live/presenter/LiveRoomCheckLivePresenter.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.text.TextUtils; +import android.util.Log; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -109,6 +110,7 @@ public class LiveRoomCheckLivePresenter { @Override public void onError(String error) { + Log.e("直播间异常", "onError: "+error ); Bus.get().post(new LiveAudienceEvent() .setType(LiveAudienceEvent.LiveAudienceType.LIVE_ROOM_EXCEPTION)); ToastUtil.show(error); @@ -159,6 +161,7 @@ public class LiveRoomCheckLivePresenter { } } } else { + Log.e("直播间异常", "Error: "+msg ); Bus.get().post(new LiveAudienceEvent() .setType(LiveAudienceEvent.LiveAudienceType.LIVE_ROOM_EXCEPTION)); ToastUtil.show(msg); diff --git a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java index 438f2528d..839cc3f58 100644 --- a/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java +++ b/live/src/main/java/com/yunbao/live/views/LiveRoomViewHolder.java @@ -4742,7 +4742,11 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis .setmLink(CommonAppConfig.HOST + "/index.php") .setType(1); bannerBean.setShow_type("2"); - mTopBannerList.add(bannerBean); + if (mTopBannerList.size() == 1 || mTopBannerList.size() == 0) { + mTopBannerList.add(bannerBean); + } else { + mTopBannerList.set(1, bannerBean); + } topBanner1.update(mTopBannerList); } diff --git a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java index 56bc0d7c7..bcdb921ee 100644 --- a/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java +++ b/live/src/main/java/com/yunbao/live/views/PortraitLiveManager.java @@ -52,6 +52,7 @@ import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.HttpClient; import com.yunbao.common.http.live.LiveNetManager; import com.yunbao.common.manager.IMLoginManager; +import com.yunbao.common.utils.AppManager; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DialogUitl; import com.yunbao.common.utils.MicStatusManager; @@ -191,6 +192,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe private String leaveStream = "", leaveGroupId = ""; public static PortraitLiveManager portraitLiveManager; private WishModel wishListProgress; + private static int waitShowTopBannerTime = 6000; public PortraitLiveManager setQuitF(boolean quitF) { isQuitF = quitF; @@ -203,6 +205,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe liveImDeletUtil = new LiveImDeletUtil(); portraitLiveManager = this; ininView(); + AppManager.runDebugCode(() -> waitShowTopBannerTime = 1000); } @@ -253,9 +256,10 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe public void run() { loading.setVisibility(View.GONE); enterRoomLeaveHandler.post(enterRoomLeaveRunnable); - if (topBannerRunnable != null) { - topBannerHandler.postDelayed(topBannerRunnable, 1000); + if (topBannerRunnable == null || topBannerHandler == null) { + initTopBannerHandler(); } + topBannerHandler.postDelayed(topBannerRunnable, waitShowTopBannerTime); } }; private Runnable topBannerRunnable = new Runnable() { @@ -274,6 +278,24 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe }; private Handler topBannerHandler = new Handler(); + private void initTopBannerHandler() { + topBannerHandler = new Handler(); + topBannerRunnable = new Runnable() { + @Override + public void run() { + if (mLiveRoomViewHolder != null) { + //初始化顶部banner + mLiveRoomViewHolder.initTopBanner(); + mLiveRoomViewHolder.updateTopBanner(); + topBannerHandler.removeCallbacks(topBannerRunnable); + topBannerRunnable = null; + topBannerHandler = null; + } + + } + }; + } + @Override public synchronized void onAdd(LiveBean data, int liveType, int liveTypeVal, int liveSdk) { isEnterRoom = false; @@ -359,7 +381,9 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe mLiveRoomViewHolder.resetView(); enterRoomModel.clear(); enterRoomLeaveHandler.removeCallbacks(enterRoomLeaveRunnable); - topBannerHandler.removeCallbacks(topBannerRunnable); + if (topBannerHandler != null && topBannerRunnable != null) { + topBannerHandler.removeCallbacks(topBannerRunnable); + } mLiveRoomViewHolder.setAvatar(data.getAvatar()); mLiveRoomViewHolder.setAnchorLevel(data.getLevelAnchor()); mLiveRoomViewHolder.setName(data.getUserNiceName()); @@ -418,7 +442,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe mLivePlayViewHolder.setAnchorPlayModel(anchorClarityType); //是否热度卡加成 liveHandler.postDelayed(() -> { - if(mLiveRoomViewHolder==null){ + if (mLiveRoomViewHolder == null) { return; } mLiveRoomViewHolder.getIsHot(data.getIsUseHotCard()); diff --git a/live/src/main/res/layout/view_live_top_banner_call_me.xml b/live/src/main/res/layout/view_live_top_banner_call_me.xml index b9298a2e5..caf9e2661 100644 --- a/live/src/main/res/layout/view_live_top_banner_call_me.xml +++ b/live/src/main/res/layout/view_live_top_banner_call_me.xml @@ -7,7 +7,7 @@