Skip to content

Commit

Permalink
[skp] feat: crash handler use broadcast.
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Feb 4, 2025
1 parent 06b2a35 commit c9a4edf
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 60 deletions.
7 changes: 0 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@
</intent-filter>
</activity>

<activity
android:name=".ui.app.crash.CrashHandlerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="false"
android:theme="@style/Theme.HyperCeiler.Translucent.NoActionBar.NoAnimation">
</activity>

<activity-alias
android:name=".ui.LauncherActivity"
android:exported="true"
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/com/sevtinge/hyperceiler/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import android.os.Looper;

import com.hchen.hooktool.log.AndroidLog;
import com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerActivity;
import com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerDialog;
import com.sevtinge.hyperceiler.utils.prefs.PrefsUtils;

import java.io.PrintWriter;
Expand Down Expand Up @@ -73,13 +73,11 @@ private void handleMainThreadCrash(String crashInfo) {
}

private void showEmergencyDialog(String crashInfo) {
Context context = getBaseContext();
new Handler(Looper.getMainLooper()).post(() -> {
try {
Intent intent = new Intent(context, CrashHandlerActivity.class);
Intent intent = new Intent(CrashHandlerDialog.CrashHandlerBroadcastReceiver.CRASH_HANDLER);
intent.putExtra("crashInfo", crashInfo);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
sendBroadcast(intent);
} catch (Throwable e) {
AndroidLog.logE(TAG, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,31 @@
*/
package com.sevtinge.hyperceiler.ui.app.crash;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Nullable;

import com.hchen.hooktool.log.AndroidLog;
import com.sevtinge.hyperceiler.utils.shell.ShellInit;

import fan.appcompat.app.AlertDialog;
import fan.appcompat.app.AppCompatActivity;

public class CrashHandlerActivity extends AppCompatActivity {
private static final String TAG = "CrashHandlerActivity";
public class CrashHandlerDialog {
private static final String TAG = "CrashHandlerDialog";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String crashInfo = getIntent().getStringExtra("crashInfo");
CrashHandlerDialog(Context context, Intent intent) {
String crashInfo = intent.getStringExtra("crashInfo");

new AlertDialog.Builder(this)
new AlertDialog.Builder(context)
.setTitle("错误")
.setMessage("模块发生致命崩溃事件,无法继续运行!请携带以下报错信息进行反馈!\n" + crashInfo)
.setPositiveButton("结束进程", (d, w) -> {
AndroidLog.logI(TAG, "kill myself!!");
moveTaskToBack(true);
finish();
((Activity) context).moveTaskToBack(true);
((Activity) context).finish();

new Handler(Looper.getMainLooper()).postDelayed(() -> {
ShellInit.getShell().run("am force-stop com.sevtinge.hyperceiler").sync();
Expand All @@ -54,4 +52,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.setHapticFeedbackEnabled(true)
.show();
}

public static class CrashHandlerBroadcastReceiver extends BroadcastReceiver {
public static final String CRASH_HANDLER = "com.sevtinge.hyperceiler.CrashHandler";

@Override
public void onReceive(Context context, Intent intent) {
if (CRASH_HANDLER.equals(intent.getAction())) {
new CrashHandlerDialog(context, intent);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sevtinge.hyperceiler.ui.app.main;

import static com.sevtinge.hyperceiler.module.base.tool.AppsTool.isModuleActive;
import static com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerDialog.CrashHandlerBroadcastReceiver.CRASH_HANDLER;
import static com.sevtinge.hyperceiler.ui.app.main.utils.PersistConfig.isLunarNewYearThemeView;
import static com.sevtinge.hyperceiler.ui.app.main.utils.PersistConfig.isNeedGrayView;
import static com.sevtinge.hyperceiler.utils.devicesdk.DeviceSDKKt.isTablet;
Expand All @@ -11,6 +12,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
Expand All @@ -31,6 +33,7 @@
import com.sevtinge.hyperceiler.module.base.tool.AppsTool;
import com.sevtinge.hyperceiler.prefs.PreferenceHeader;
import com.sevtinge.hyperceiler.prefs.XmlPreference;
import com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerDialog;
import com.sevtinge.hyperceiler.ui.app.holiday.HolidayHelper;
import com.sevtinge.hyperceiler.ui.app.main.utils.LanguageHelper;
import com.sevtinge.hyperceiler.ui.app.safe.CrashData;
Expand All @@ -57,11 +60,11 @@
import fan.preference.PreferenceFragment;

public class HyperCeilerTabActivity extends NaviBaseActivity
implements PreferenceFragment.OnPreferenceStartFragmentCallback, IResult {
implements PreferenceFragment.OnPreferenceStartFragmentCallback, IResult {

private Handler handler;
private Context context;

private CrashHandlerDialog.CrashHandlerBroadcastReceiver mCrashHandlerBroadcastReceiver;
private ArrayList<String> appCrash = new ArrayList<>();

@Override
Expand All @@ -75,6 +78,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
new HolidayHelper(this);
}

mCrashHandlerBroadcastReceiver = new CrashHandlerDialog.CrashHandlerBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(CRASH_HANDLER);
registerReceiver(mCrashHandlerBroadcastReceiver, intentFilter, Context.RECEIVER_EXPORTED);

SharedPreferences mPrefs = PrefsUtils.mSharedPreferences;
String languageSetting = mPrefs.getString("prefs_key_settings_app_language", "-1");
if (!"-1".equals(languageSetting)) {
Expand Down Expand Up @@ -171,8 +178,8 @@ public void error(String reason) {
@Override
public boolean onPreferenceStartFragment(@NonNull PreferenceFragmentCompat caller, @NonNull Preference pref) {
if (caller instanceof NavigatorFragmentListener &&
Navigator.get(caller).getNavigationMode() == Navigator.Mode.NLC &&
isTablet()) {
Navigator.get(caller).getNavigationMode() == Navigator.Mode.NLC &&
isTablet()) {
Bundle args = new Bundle();
Bundle savedInstanceState = new Bundle();
if (pref instanceof XmlPreference xmlPreference) {
Expand Down Expand Up @@ -283,6 +290,7 @@ protected void onPause() {
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mCrashHandlerBroadcastReceiver);
ShellInit.destroy();
ThreadPoolManager.shutdown();
PreferenceHeader.mUninstallApp.clear();
Expand All @@ -292,18 +300,19 @@ public void onDestroy() {
// 权限申请
public void requestPermissions() {
PermissionUtils.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1,
// 实现接口方法
new PermissionUtils.OnPermissionListener() {
@Override
public void onPermissionGranted(Context context) {
// 获取权限成功
}
// 实现接口方法
new PermissionUtils.OnPermissionListener() {
@Override
public void onPermissionGranted(Context context) {
// 获取权限成功
}

@Override
public void onPermissionDenied() {
// 获取权限失败
finish();
}
});
@Override
public void onPermissionDenied() {
// 获取权限失败
finish();
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/*
* This file is part of HyperCeiler.
* This file is part of HyperCeiler.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* HyperCeiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Copyright (C) 2023-2025 HyperCeiler Contributions
*/
* Copyright (C) 2023-2025 HyperCeiler Contributions
*/
package com.sevtinge.hyperceiler.ui.base;

import static com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerDialog.CrashHandlerBroadcastReceiver.CRASH_HANDLER;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import com.sevtinge.hyperceiler.ui.app.crash.CrashHandlerDialog;
import com.sevtinge.hyperceiler.ui.base.sub.MultiActionSettings;
import com.sevtinge.hyperceiler.ui.hooker.framework.OtherSettings;
import com.sevtinge.hyperceiler.ui.hooker.home.HomeDockSettings;
Expand All @@ -34,14 +39,21 @@
import fan.preference.PreferenceFragment;

public abstract class SettingsActivity extends BaseSettingsActivity implements PreferenceFragment.OnPreferenceStartFragmentCallback {
private CrashHandlerDialog.CrashHandlerBroadcastReceiver mCrashHandlerBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mCrashHandlerBroadcastReceiver = new CrashHandlerDialog.CrashHandlerBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(CRASH_HANDLER);
registerReceiver(mCrashHandlerBroadcastReceiver, intentFilter, Context.RECEIVER_EXPORTED);

initCreate();
}

public void initCreate() {}
public void initCreate() {
}

public void onStartSettingsForArguments(Preference preference, boolean isAddPreferenceKey) {
mProxy.onStartSettingsForArguments(SubSettings.class, preference, isAddPreferenceKey);
Expand All @@ -50,13 +62,19 @@ public void onStartSettingsForArguments(Preference preference, boolean isAddPref
@Override
public boolean onPreferenceStartFragment(@NonNull PreferenceFragmentCompat caller, @NonNull Preference pref) {
boolean isAddPreferenceKey = caller instanceof OtherSettings ||
caller instanceof HomeDockSettings ||
caller instanceof HomeFolderSettings ||
caller instanceof AlertDialogSettings ||
caller instanceof HomeGestureSettings ||
caller instanceof MultiActionSettings;
caller instanceof HomeDockSettings ||
caller instanceof HomeFolderSettings ||
caller instanceof AlertDialogSettings ||
caller instanceof HomeGestureSettings ||
caller instanceof MultiActionSettings;

onStartSettingsForArguments(pref, isAddPreferenceKey);
return true;
}

@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mCrashHandlerBroadcastReceiver);
}
}

0 comments on commit c9a4edf

Please sign in to comment.