|
2 | 2 |
|
3 | 3 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
|
4 | 4 | import static android.content.Context.ACTIVITY_SERVICE;
|
| 5 | +import static android.content.Context.RECEIVER_EXPORTED; |
5 | 6 | import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED;
|
6 | 7 |
|
7 | 8 | import android.annotation.SuppressLint;
|
8 | 9 | import android.app.ActivityManager;
|
| 10 | +import android.content.BroadcastReceiver; |
9 | 11 | import android.content.Context;
|
| 12 | +import android.content.Intent; |
| 13 | +import android.content.IntentFilter; |
10 | 14 | import android.content.pm.ApplicationInfo;
|
11 | 15 | import android.content.pm.PackageInfo;
|
12 | 16 | import android.content.pm.PackageManager;
|
|
16 | 20 | import android.util.DisplayMetrics;
|
17 | 21 | import io.sentry.ILogger;
|
18 | 22 | import io.sentry.SentryLevel;
|
| 23 | +import io.sentry.SentryOptions; |
19 | 24 | import io.sentry.protocol.App;
|
20 | 25 | import java.io.BufferedReader;
|
21 | 26 | import java.io.File;
|
@@ -346,6 +351,33 @@ static boolean isForegroundImportance(final @NotNull Context context) {
|
346 | 351 | }
|
347 | 352 | }
|
348 | 353 |
|
| 354 | + /** Register a not exported BroadcastReceiver, independently from platform version. */ |
| 355 | + static @Nullable Intent registerReceiver( |
| 356 | + final @NotNull Context context, |
| 357 | + final @NotNull SentryOptions options, |
| 358 | + final @Nullable BroadcastReceiver receiver, |
| 359 | + final @NotNull IntentFilter filter) { |
| 360 | + return registerReceiver(context, new BuildInfoProvider(options.getLogger()), receiver, filter); |
| 361 | + } |
| 362 | + |
| 363 | + /** Register a not exported BroadcastReceiver, independently from platform version. */ |
| 364 | + @SuppressLint({"NewApi", "UnspecifiedRegisterReceiverFlag"}) |
| 365 | + static @Nullable Intent registerReceiver( |
| 366 | + final @NotNull Context context, |
| 367 | + final @NotNull BuildInfoProvider buildInfoProvider, |
| 368 | + final @Nullable BroadcastReceiver receiver, |
| 369 | + final @NotNull IntentFilter filter) { |
| 370 | + if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.TIRAMISU) { |
| 371 | + // From https://developer.android.com/guide/components/broadcasts#context-registered-receivers |
| 372 | + // If this receiver is listening for broadcasts sent from the system or from other apps, even |
| 373 | + // other apps that you own—use the RECEIVER_EXPORTED flag. If instead this receiver is |
| 374 | + // listening only for broadcasts sent by your app, use the RECEIVER_NOT_EXPORTED flag. |
| 375 | + return context.registerReceiver(receiver, filter, RECEIVER_EXPORTED); |
| 376 | + } else { |
| 377 | + return context.registerReceiver(receiver, filter); |
| 378 | + } |
| 379 | + } |
| 380 | + |
349 | 381 | // we perform an if-check for that, but lint fails to recognize
|
350 | 382 | @SuppressLint("NewApi")
|
351 | 383 | static void setAppPackageInfo(
|
|
0 commit comments