From aeeeb7e22078123b90bacdc384f6d127cf43b121 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Mon, 12 May 2025 10:46:32 -0700 Subject: [PATCH] Moving some static debug initialization out of Launcher to MainProcessInitializer Bug: 417227133 Test: Presubmit Flag: EXEMPT refactor and debug code Change-Id: If16878b6fb93333989897ce7d2f793395c8a14dd --- src/com/android/launcher3/Launcher.java | 66 ----------------- .../launcher3/MainProcessInitializer.java | 71 ++++++++++++++++++- 2 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 927c93b1fb..85bb7638a5 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -16,8 +16,6 @@ package com.android.launcher3; -import static android.app.PendingIntent.FLAG_IMMUTABLE; -import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; import static android.content.pm.ActivityInfo.CONFIG_UI_MODE; import static android.view.WindowInsetsAnimation.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE; import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; @@ -29,7 +27,6 @@ import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE; import static com.android.launcher3.AbstractFloatingView.getTopOpenViewWithType; import static com.android.launcher3.Flags.allAppsBlur; import static com.android.launcher3.Flags.enableAddAppWidgetViaConfigActivityV2; -import static com.android.launcher3.Flags.enableStrictMode; import static com.android.launcher3.Flags.enableWorkspaceInflation; import static com.android.launcher3.LauncherAnimUtils.HOTSEAT_SCALE_PROPERTY_FACTORY; import static com.android.launcher3.LauncherAnimUtils.SCALE_INDEX_WIDGET_TRANSITION; @@ -104,10 +101,6 @@ import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ValueAnimator; import android.annotation.TargetApi; -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; @@ -125,7 +118,6 @@ import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; -import android.os.StrictMode; import android.os.SystemClock; import android.os.Trace; import android.os.UserHandle; @@ -290,8 +282,6 @@ public class Launcher extends StatefulActivity static final boolean LOGD = false; - static final boolean DEBUG_STRICT_MODE = false; - private static final float BOUNCE_ANIMATION_TENSION = 1.3f; /** @@ -303,8 +293,6 @@ public class Launcher extends StatefulActivity public static final String INTENT_ACTION_ALL_APPS_TOGGLE = "launcher.intent_action_all_apps_toggle"; - private static boolean sIsNewProcess = true; - private StateManager mStateManager; private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500; @@ -426,60 +414,6 @@ public class Launcher extends StatefulActivity mStartupLatencyLogger = StartupLatencyLogger.getLogger(this); mStartupLatencyLogger.logStart(LAUNCHER_LATENCY_STARTUP_ACTIVITY_ON_CREATE); - if (DEBUG_STRICT_MODE - || (FeatureFlags.IS_STUDIO_BUILD && enableStrictMode())) { - StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() - .detectDiskReads() - .detectDiskWrites() - .detectNetwork() // or .detectAll() for all detectable problems - .penaltyLog() - .build()); - StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() - .detectLeakedSqlLiteObjects() - .detectLeakedClosableObjects() - .detectActivityLeaks() - .penaltyLog() - .penaltyDeath() - .build()); - } - - if (Utilities.IS_DEBUG_DEVICE && FeatureFlags.NOTIFY_CRASHES.get()) { - final String notificationChannelId = "com.android.launcher3.Debug"; - final String notificationChannelName = "Debug"; - final String notificationTag = "Debug"; - final int notificationId = 0; - - NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(new NotificationChannel( - notificationChannelId, notificationChannelName, - NotificationManager.IMPORTANCE_HIGH)); - - Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> { - String stackTrace = Log.getStackTraceString(throwable); - - Intent shareIntent = new Intent(Intent.ACTION_SEND); - shareIntent.setType("text/plain"); - shareIntent.putExtra(Intent.EXTRA_TEXT, stackTrace); - shareIntent = Intent.createChooser(shareIntent, null); - PendingIntent sharePendingIntent = PendingIntent.getActivity( - this, 0, shareIntent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE); - - Notification notification = new Notification.Builder(this, notificationChannelId) - .setSmallIcon(android.R.drawable.ic_menu_close_clear_cancel) - .setContentTitle("Launcher crash detected!") - .setStyle(new Notification.BigTextStyle().bigText(stackTrace)) - .addAction(android.R.drawable.ic_menu_share, "Share", sharePendingIntent) - .build(); - notificationManager.notify(notificationTag, notificationId, notification); - - Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = - Thread.getDefaultUncaughtExceptionHandler(); - if (defaultUncaughtExceptionHandler != null) { - defaultUncaughtExceptionHandler.uncaughtException(thread, throwable); - } - }); - } - super.onCreate(savedInstanceState); setWallpaperDependentTheme(this); diff --git a/src/com/android/launcher3/MainProcessInitializer.java b/src/com/android/launcher3/MainProcessInitializer.java index 9944ef6b4a..1dabb1cb2d 100644 --- a/src/com/android/launcher3/MainProcessInitializer.java +++ b/src/com/android/launcher3/MainProcessInitializer.java @@ -16,8 +16,21 @@ package com.android.launcher3; -import android.content.Context; +import static android.app.PendingIntent.FLAG_IMMUTABLE; +import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; +import static com.android.launcher3.Flags.enableStrictMode; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.StrictMode; +import android.util.Log; + +import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.graphics.BitmapCreationCheck; import com.android.launcher3.logging.FileLog; import com.android.launcher3.util.ResourceBasedOverride; @@ -27,6 +40,8 @@ import com.android.launcher3.util.ResourceBasedOverride; */ public class MainProcessInitializer implements ResourceBasedOverride { + private static final boolean DEBUG_STRICT_MODE = false; + public static void initialize(Context context) { Overrides.getObject( MainProcessInitializer.class, context, R.string.main_process_initializer_class) @@ -39,5 +54,59 @@ public class MainProcessInitializer implements ResourceBasedOverride { if (BitmapCreationCheck.ENABLED) { BitmapCreationCheck.startTracking(context); } + + if (DEBUG_STRICT_MODE || (BuildConfig.IS_STUDIO_BUILD && enableStrictMode())) { + StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() + .detectDiskReads() + .detectDiskWrites() + .detectNetwork() // or .detectAll() for all detectable problems + .penaltyLog() + .build()); + StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() + .detectLeakedSqlLiteObjects() + .detectLeakedClosableObjects() + .detectActivityLeaks() + .penaltyLog() + .penaltyDeath() + .build()); + } + + if (BuildConfig.IS_DEBUG_DEVICE && FeatureFlags.NOTIFY_CRASHES.get()) { + final String notificationChannelId = "com.android.launcher3.Debug"; + final String notificationChannelName = "Debug"; + final String notificationTag = "Debug"; + final int notificationId = 0; + + NotificationManager notificationManager = + context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(new NotificationChannel( + notificationChannelId, notificationChannelName, + NotificationManager.IMPORTANCE_HIGH)); + + Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> { + String stackTrace = Log.getStackTraceString(throwable); + + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_TEXT, stackTrace); + shareIntent = Intent.createChooser(shareIntent, null); + PendingIntent sharePendingIntent = PendingIntent.getActivity( + context, 0, shareIntent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE); + + Notification notification = new Notification.Builder(context, notificationChannelId) + .setSmallIcon(android.R.drawable.ic_menu_close_clear_cancel) + .setContentTitle("Launcher crash detected!") + .setStyle(new Notification.BigTextStyle().bigText(stackTrace)) + .addAction(android.R.drawable.ic_menu_share, "Share", sharePendingIntent) + .build(); + notificationManager.notify(notificationTag, notificationId, notification); + + Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = + Thread.getDefaultUncaughtExceptionHandler(); + if (defaultUncaughtExceptionHandler != null) { + defaultUncaughtExceptionHandler.uncaughtException(thread, throwable); + } + }); + } } }