|
7 | 7 | import android.annotation.SuppressLint; |
8 | 8 | import android.app.ActivityManager; |
9 | 9 | import android.content.BroadcastReceiver; |
| 10 | +import android.content.ComponentName; |
10 | 11 | import android.content.Context; |
11 | 12 | import android.content.Intent; |
12 | 13 | import android.content.IntentFilter; |
|
27 | 28 | import java.io.IOException; |
28 | 29 | import java.util.Arrays; |
29 | 30 | import java.util.HashMap; |
| 31 | +import java.util.List; |
30 | 32 | import java.util.Map; |
31 | 33 | import org.jetbrains.annotations.ApiStatus; |
32 | 34 | import org.jetbrains.annotations.NotNull; |
@@ -285,6 +287,36 @@ public static boolean isForegroundImportance() { |
285 | 287 | return isForegroundImportance.getValue(); |
286 | 288 | } |
287 | 289 |
|
| 290 | + /** |
| 291 | + * Determines if the app is a packaged android library for running Compose Preview Mode |
| 292 | + * |
| 293 | + * @param context the context |
| 294 | + * @return true, if the app is actually a library running as an app for Compose Preview Mode |
| 295 | + */ |
| 296 | + @ApiStatus.Internal |
| 297 | + public static boolean appIsLibraryForComposePreview(final @NotNull Context context) { |
| 298 | + // Jetpack Compose Preview (aka "Run Preview on Device") |
| 299 | + // uses the androidTest flavor for android library modules, |
| 300 | + // so let's fail-fast by checking this first |
| 301 | + if (context.getPackageName().endsWith(".test")) { |
| 302 | + try { |
| 303 | + final @NotNull ActivityManager activityManager = |
| 304 | + (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 305 | + final @NotNull List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks(); |
| 306 | + for (final ActivityManager.AppTask task : appTasks) { |
| 307 | + final @Nullable ComponentName component = task.getTaskInfo().baseIntent.getComponent(); |
| 308 | + if (component != null |
| 309 | + && component.getClassName().equals("androidx.compose.ui.tooling.PreviewActivity")) { |
| 310 | + return true; |
| 311 | + } |
| 312 | + } |
| 313 | + } catch (Throwable t) { |
| 314 | + // ignored |
| 315 | + } |
| 316 | + } |
| 317 | + return false; |
| 318 | + } |
| 319 | + |
288 | 320 | /** |
289 | 321 | * Get the device's current kernel version, as a string. Attempts to read /proc/version, and falls |
290 | 322 | * back to the 'os.version' System Property. |
|
0 commit comments