|
33 | 33 | import com.developer.crashx.config.CrashConfig;
|
34 | 34 |
|
35 | 35 | /**
|
36 |
| - * @author akshay sunil masram |
| 36 | + * @author tkdco , TutorialsAndroid |
37 | 37 | */
|
38 | 38 | public final class CrashActivity {
|
39 | 39 |
|
@@ -81,81 +81,78 @@ public static void install(@Nullable final Context context) {
|
81 | 81 |
|
82 | 82 | application = (Application) context.getApplicationContext();
|
83 | 83 |
|
84 |
| - Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { |
85 |
| - @Override |
86 |
| - public void uncaughtException(Thread thread, final Throwable throwable) { |
87 |
| - if (config.isEnabled()) { |
88 |
| - Log.e(TAG, "App has crashed, executing CrashActivity's UncaughtExceptionHandler", throwable); |
| 84 | + Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { |
| 85 | + if (config.isEnabled()) { |
| 86 | + Log.e(TAG, "App has crashed, executing CrashActivity's UncaughtExceptionHandler", throwable); |
| 87 | + |
| 88 | + if (hasCrashedInTheLastSeconds(application)) { |
| 89 | + Log.e(TAG, "App already crashed recently, not starting custom error activity because we could enter a restart loop. Are you sure that your app does not crash directly on init?", throwable); |
| 90 | + if (oldHandler != null) { |
| 91 | + oldHandler.uncaughtException(thread, throwable); |
| 92 | + return; |
| 93 | + } |
| 94 | + } else { |
| 95 | + setLastCrashTimestamp(application, new Date().getTime()); |
| 96 | + |
| 97 | + Class<? extends Activity> errorActivityClass = config.getErrorActivityClass(); |
| 98 | + |
| 99 | + if (errorActivityClass == null) { |
| 100 | + errorActivityClass = guessErrorActivityClass(application); |
| 101 | + } |
89 | 102 |
|
90 |
| - if (hasCrashedInTheLastSeconds(application)) { |
91 |
| - Log.e(TAG, "App already crashed recently, not starting custom error activity because we could enter a restart loop. Are you sure that your app does not crash directly on init?", throwable); |
| 103 | + if (isStackTraceLikelyConflictive(throwable, errorActivityClass)) { |
| 104 | + Log.e(TAG, "Your application class or your error activity have crashed, the custom activity will not be launched!"); |
92 | 105 | if (oldHandler != null) {
|
93 | 106 | oldHandler.uncaughtException(thread, throwable);
|
94 | 107 | return;
|
95 | 108 | }
|
96 |
| - } else { |
97 |
| - setLastCrashTimestamp(application, new Date().getTime()); |
| 109 | + } else if (config.getBackgroundMode() == CrashConfig.BACKGROUND_MODE_SHOW_CUSTOM || !isInBackground) { |
98 | 110 |
|
99 |
| - Class<? extends Activity> errorActivityClass = config.getErrorActivityClass(); |
| 111 | + final Intent intent = new Intent(application, errorActivityClass); |
| 112 | + StringWriter sw = new StringWriter(); |
| 113 | + PrintWriter pw = new PrintWriter(sw); |
| 114 | + throwable.printStackTrace(pw); |
| 115 | + String stackTraceString = sw.toString(); |
100 | 116 |
|
101 |
| - if (errorActivityClass == null) { |
102 |
| - errorActivityClass = guessErrorActivityClass(application); |
| 117 | + if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) { |
| 118 | + String disclaimer = " [stack trace too large]"; |
| 119 | + stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer; |
103 | 120 | }
|
| 121 | + intent.putExtra(EXTRA_STACK_TRACE, stackTraceString); |
104 | 122 |
|
105 |
| - if (isStackTraceLikelyConflictive(throwable, errorActivityClass)) { |
106 |
| - Log.e(TAG, "Your application class or your error activity have crashed, the custom activity will not be launched!"); |
107 |
| - if (oldHandler != null) { |
108 |
| - oldHandler.uncaughtException(thread, throwable); |
109 |
| - return; |
110 |
| - } |
111 |
| - } else if (config.getBackgroundMode() == CrashConfig.BACKGROUND_MODE_SHOW_CUSTOM || !isInBackground) { |
112 |
| - |
113 |
| - final Intent intent = new Intent(application, errorActivityClass); |
114 |
| - StringWriter sw = new StringWriter(); |
115 |
| - PrintWriter pw = new PrintWriter(sw); |
116 |
| - throwable.printStackTrace(pw); |
117 |
| - String stackTraceString = sw.toString(); |
118 |
| - |
119 |
| - if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) { |
120 |
| - String disclaimer = " [stack trace too large]"; |
121 |
| - stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer; |
122 |
| - } |
123 |
| - intent.putExtra(EXTRA_STACK_TRACE, stackTraceString); |
124 |
| - |
125 |
| - if (config.isTrackActivities()) { |
126 |
| - StringBuilder activityLogStringBuilder = new StringBuilder(); |
127 |
| - while (!activityLog.isEmpty()) { |
128 |
| - activityLogStringBuilder.append(activityLog.poll()); |
129 |
| - } |
130 |
| - intent.putExtra(EXTRA_ACTIVITY_LOG, activityLogStringBuilder.toString()); |
| 123 | + if (config.isTrackActivities()) { |
| 124 | + StringBuilder activityLogStringBuilder = new StringBuilder(); |
| 125 | + while (!activityLog.isEmpty()) { |
| 126 | + activityLogStringBuilder.append(activityLog.poll()); |
131 | 127 | }
|
| 128 | + intent.putExtra(EXTRA_ACTIVITY_LOG, activityLogStringBuilder.toString()); |
| 129 | + } |
132 | 130 |
|
133 |
| - if (config.isShowRestartButton() && config.getRestartActivityClass() == null) { |
134 |
| - config.setRestartActivityClass(guessRestartActivityClass(application)); |
135 |
| - } |
| 131 | + if (config.isShowRestartButton() && config.getRestartActivityClass() == null) { |
| 132 | + config.setRestartActivityClass(guessRestartActivityClass(application)); |
| 133 | + } |
136 | 134 |
|
137 |
| - intent.putExtra(EXTRA_CONFIG, config); |
138 |
| - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
139 |
| - if (config.getEventListener() != null) { |
140 |
| - config.getEventListener().onLaunchErrorActivity(); |
141 |
| - } |
142 |
| - application.startActivity(intent); |
143 |
| - } else if (config.getBackgroundMode() == CrashConfig.BACKGROUND_MODE_CRASH) { |
144 |
| - if (oldHandler != null) { |
145 |
| - oldHandler.uncaughtException(thread, throwable); |
146 |
| - return; |
147 |
| - } |
| 135 | + intent.putExtra(EXTRA_CONFIG, config); |
| 136 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 137 | + if (config.getEventListener() != null) { |
| 138 | + config.getEventListener().onLaunchErrorActivity(); |
| 139 | + } |
| 140 | + application.startActivity(intent); |
| 141 | + } else if (config.getBackgroundMode() == CrashConfig.BACKGROUND_MODE_CRASH) { |
| 142 | + if (oldHandler != null) { |
| 143 | + oldHandler.uncaughtException(thread, throwable); |
| 144 | + return; |
148 | 145 | }
|
149 | 146 | }
|
150 |
| - final Activity lastActivity = lastActivityCreated.get(); |
151 |
| - if (lastActivity != null) { |
152 |
| - lastActivity.finish(); |
153 |
| - lastActivityCreated.clear(); |
154 |
| - } |
155 |
| - killCurrentProcess(); |
156 |
| - } else if (oldHandler != null) { |
157 |
| - oldHandler.uncaughtException(thread, throwable); |
158 | 147 | }
|
| 148 | + final Activity lastActivity = lastActivityCreated.get(); |
| 149 | + if (lastActivity != null) { |
| 150 | + lastActivity.finish(); |
| 151 | + lastActivityCreated.clear(); |
| 152 | + } |
| 153 | + killCurrentProcess(); |
| 154 | + } else if (oldHandler != null) { |
| 155 | + oldHandler.uncaughtException(thread, throwable); |
159 | 156 | }
|
160 | 157 | });
|
161 | 158 | application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
|
|
0 commit comments