Skip to content

Commit

Permalink
feat: restart on crash + share exception info
Browse files Browse the repository at this point in the history
  • Loading branch information
dic1911 committed Nov 13, 2024
1 parent fd9cc21 commit 07288e4
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.SystemClock;
Expand All @@ -39,6 +40,8 @@

import androidx.multidex.MultiDex;

import com.jakewharton.processphoenix.ProcessPhoenix;

import org.json.JSONObject;
import org.telegram.messenger.voip.VideoCapturerDevice;
import org.telegram.tgnet.ConnectionsManager;
Expand All @@ -48,13 +51,16 @@
import org.telegram.ui.Components.ForegroundDetector;
import org.telegram.ui.Components.Premium.boosts.BoostRepository;
import org.telegram.ui.IUpdateLayout;
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.LauncherIconController;

import java.io.File;
import java.util.LinkedList;

import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.parts.SignturesKt;
import tw.nekomimi.nekogram.utils.FileUtil;
import tw.nekomimi.nekogram.utils.TelegramUtil;

import java.util.ArrayList;
import java.util.Locale;
Expand Down Expand Up @@ -99,6 +105,9 @@ protected void attachBaseContext(Context base) {
}
Thread.currentThread().setUncaughtExceptionHandler((thread, error) -> {
Log.e("nekox", "from " + thread.toString(), error);
String errStr = String.format("%s\n%s", error.getMessage(), TelegramUtil.getStackTraceAsString(error.getStackTrace()));
NekoConfig.lastCrashError.setConfigString(errStr);
ProcessPhoenix.triggerRebirth(applicationContext, new Intent(applicationContext, LaunchActivity.class));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@
import kotlin.Unit;
import kotlin.text.StringsKt;
import tw.nekomimi.nekogram.InternalUpdater;
import tw.nekomimi.nekogram.proxynext.ProxyConfig;
import tw.nekomimi.nekogram.ui.BottomBuilder;
import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.NekoXConfig;
Expand Down Expand Up @@ -386,8 +385,7 @@ protected void onCreate(Bundle savedInstanceState) {
Uri uri = intent.getData();
if (uri != null) {
String url = uri.toString().toLowerCase();
isProxy = url.startsWith("tg:proxy") || url.startsWith("tg://proxy") || url.startsWith("tg:socks") || url.startsWith("tg://socks") ||
url.startsWith(ProxyConfig.SS_PROTOCOL);
isProxy = url.startsWith("tg:proxy") || url.startsWith("tg://proxy") || url.startsWith("tg:socks") || url.startsWith("tg://socks");
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions TMessagesProj/src/main/java/tw/nekomimi/nekogram/NekoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.R;
import org.telegram.ui.ActionBar.AlertDialog;
import org.telegram.ui.LaunchActivity;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.ObjectInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import android.util.Base64;
import android.util.Log;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import cn.hutool.core.util.StrUtil;
import tw.nekomimi.nekogram.config.ConfigItem;
import tw.nekomimi.nekogram.utils.FileUtil;
import tw.nekomimi.nekogram.utils.ShareUtil;

import static tw.nekomimi.nekogram.config.ConfigItem.*;

Expand Down Expand Up @@ -269,6 +279,7 @@ public class NekoConfig {
public static ConfigItem imageMessageSizeTweak = addConfig(R.string.ImageMessageSizeTweak, "ImageMessageSizeTweak", configTypeBool, CHAT, false);
public static ConfigItem hideUnreadCounterOnFolderTabs = addConfig(R.string.HideUnreadCounterOnFolderTabs, "HideUnreadCounterOnFolderTabs", configTypeBool, GENERAL, false);
public static ConfigItem chooseBestVideoQualityByDefault = addConfig(R.string.ChooseBestVideoQualityByDefault, "ChooseBestVideoQualityByDefault", configTypeBool, CHAT, false);
public static ConfigItem lastCrashError = addConfig("LastCrashError", configTypeString, null);

public static ConfigItem customGetQueryBlacklist = addConfig(R.string.BlacklistUrlQueryTitle, "BlacklistUrlQueryTitle", configTypeString, "");
public static ArrayList<String> customGetQueryBlacklistData = new ArrayList<>();
Expand Down Expand Up @@ -709,6 +720,28 @@ public static void saveSearchBlacklist() {
public static void init() {
initStrings();
try {
if (lastCrashError.String() != null && !lastCrashError.String().isBlank()) {
final String errStr = lastCrashError.String();
AndroidUtilities.runOnUIThread(() -> {
Context context = LaunchActivity.getLastFragment().getContext();
new AlertDialog.Builder(context)
.setTitle(LocaleController.getString(R.string.CrashDialogTitle))
.setMessage(LocaleController.getString(R.string.CrashDialogMessage))
.setNeutralButton(LocaleController.getString(R.string.Copy), (__, ___) -> {
AndroidUtilities.addToClipboard(errStr);
lastCrashError.setConfigString(null);
})
.setPositiveButton(LocaleController.getString(R.string.OK), (__, ___) -> {
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss"));
File cacheFile = new File(ApplicationLoader.applicationContext.getCacheDir(), timestamp + ".nekox-crash.txt");
FileUtil.writeUtf8String(errStr, cacheFile);
ShareUtil.shareFile(context, cacheFile);
})
.setNegativeButton(LocaleController.getString(R.string.Cancel), (__, ___) -> lastCrashError.setConfigString(null))
.create().show();
});
lastCrashError.setConfigString(null);
}
updateUseSpoilerMediaChatList();
updatePreferredTranslateTargetLangList();
applyCustomGetQueryBlacklist();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public class NekoExperimentalSettingsActivity extends BaseFragment {
private final AbstractConfigCell overrideSettingFloatRow = cellGroup.appendCell(new ConfigCellTextInput(null, NekoConfig.overrideSettingFloat, LocaleController.getString(R.string.OverrideSettingHint), null, NekoConfig::applyOverriddenValue));
private final AbstractConfigCell divider1 = cellGroup.appendCell(new ConfigCellDivider());

private final AbstractConfigCell header3 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString(R.string.DebugMenu)));
private final AbstractConfigCell triggerCrashRow = cellGroup.appendCell(new ConfigCellSelectBox(LocaleController.getString(R.string.TriggerCrash), null, null,
() -> AndroidUtilities.runOnUIThread(() -> { int[] arr = new int[0]; arr[1] = 0;})));
private final AbstractConfigCell divider2 = cellGroup.appendCell(new ConfigCellDivider());

private UndoView tooltip;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public static void toggleProxyOnOff(boolean cancel) {
toggleProxyOnOffThread.start();
}

public static String getStackTraceAsString(Thread t) {
return Arrays.toString(Arrays.stream(Thread.currentThread().getStackTrace()).skip(3).toArray());
public static String getStackTraceAsString(StackTraceElement[] stackTrace) {
StackTraceElement[] st = (stackTrace == null) ?
(StackTraceElement[]) Arrays.stream(Thread.currentThread().getStackTrace()).skip(3).toArray() : stackTrace;
return Arrays.toString(st);
}
}
3 changes: 3 additions & 0 deletions TMessagesProj/src/main/res/values-ja/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,7 @@
<string name="CopySticker">ステッカーをコピー</string>
<string name="CopyPhotoSticker">画像/ステッカーをコピー</string>
<string name="ChooseBestVideoQualityByDefault">自動的に最高の動画解像度を選ぶ</string>
<string name="CrashDialogTitle">NekoX はクラッシュしちゃった!</string>
<string name="CrashDialogMessage">デバッグインフォをシェアするで開発者を手伝えしますか?</string>
<string name="TriggerCrash">アプリをクラッシュする</string>
</resources>
3 changes: 3 additions & 0 deletions TMessagesProj/src/main/res/values-zh-rCN/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,7 @@
<string name="CopySticker">复制贴纸</string>
<string name="CopyPhotoSticker">复制图像贴纸</string>
<string name="ChooseBestVideoQualityByDefault">预设选择最高影片品质</string>
<string name="CrashDialogTitle">NekoX 发生错误 &lt;&gt;</string>
<string name="CrashDialogMessage">是否分享除错信息以利开发者解决问题?</string>
<string name="TriggerCrash">触发闪退</string>
</resources>
3 changes: 3 additions & 0 deletions TMessagesProj/src/main/res/values-zh-rTW/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,7 @@
<string name="CopySticker">複製貼圖</string>
<string name="CopyPhotoSticker">複製圖片/貼圖</string>
<string name="ChooseBestVideoQualityByDefault">預設選擇最高影片畫質</string>
<string name="CrashDialogTitle">NekoX 發生錯誤 &lt;&gt;</string>
<string name="CrashDialogMessage">是否願意分享除錯資訊以利開發者解決問題?</string>
<string name="TriggerCrash">觸發閃退</string>
</resources>
3 changes: 3 additions & 0 deletions TMessagesProj/src/main/res/values/strings_neko.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,7 @@
<string name="CopyPhotoSticker">Copy Photo/Sticker</string>
<string name="HideUnreadCounterOnFolderTabs">Hide unread counter on folder tabs</string>
<string name="ChooseBestVideoQualityByDefault">Choose best video quality by default</string>
<string name="CrashDialogTitle">NekoX crashed!</string>
<string name="CrashDialogMessage">Help the developer solve this problem by sharing related info?</string>
<string name="TriggerCrash">Trigger crash</string>
</resources>

0 comments on commit 07288e4

Please sign in to comment.