Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion test-app/app/src/main/java/com/tns/RuntimeHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tns;

import android.app.ActivityManager;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
Expand All @@ -25,6 +27,8 @@ private RuntimeHelper() {

private static AndroidJsV8Inspector v8Inspector;

private static final String ERROR_REPORT_ACTIVITY = "com.tns.ErrorReportActivity";

// hasErrorIntent tells you if there was an event (with an uncaught
// exception) raised from ErrorReport
private static boolean hasErrorIntent(Context context) {
Expand All @@ -48,7 +52,7 @@ private static boolean hasErrorIntent(Context context) {

if (errFile.exists()) {
errFile.delete();
hasErrorIntent = true;
hasErrorIntent = isErrorActivityStarting(context);
}
}
} catch (Exception e) {
Expand All @@ -58,6 +62,35 @@ private static boolean hasErrorIntent(Context context) {
return hasErrorIntent;
}

// The marker outlives a crash whose error activity never started (Android
// can block that start from the background), so it only counts when this
// process is starting for the error activity. Skipping runtime init for
// any other activity leaves it without a runtime.
private static boolean isErrorActivityStarting(Context context) {
try {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return true;
}
for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
ActivityManager.RecentTaskInfo info = task.getTaskInfo();
ComponentName top;
if (Build.VERSION.SDK_INT >= 23) {
top = info.topActivity;
} else {
top = info.baseIntent != null ? info.baseIntent.getComponent() : null;
}
if (top != null && ERROR_REPORT_ACTIVITY.equals(top.getClassName())) {
return true;
}
}
return false;
} catch (Exception e) {
Log.d(logTag, "Could not tell which activity is starting: " + e.getMessage());
return true;
}
}

public static Runtime initRuntime(Context context) {
if (Runtime.isInitialized()) {
return Runtime.getCurrentRuntime();
Expand Down