From 7e0a98ca64f187aa142c878ea39e5366b7b05c33 Mon Sep 17 00:00:00 2001 From: Armin Date: Mon, 26 Feb 2024 15:19:28 +0100 Subject: [PATCH] [STAD-571] Set receiver_not_exported flag on Android 14 --- .../de/cyface/datacapturing/DataCapturingService.java | 10 ++++++++-- .../java/de/cyface/datacapturing/PongReceiver.java | 10 ++++++++-- .../de/cyface/synchronization/UploadProgressTest.java | 7 ++++++- .../synchronization/ConnectionStatusReceiver.java | 7 ++++++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/datacapturing/src/main/java/de/cyface/datacapturing/DataCapturingService.java b/datacapturing/src/main/java/de/cyface/datacapturing/DataCapturingService.java index 268c38b8f..6901d6569 100644 --- a/datacapturing/src/main/java/de/cyface/datacapturing/DataCapturingService.java +++ b/datacapturing/src/main/java/de/cyface/datacapturing/DataCapturingService.java @@ -49,6 +49,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.ConnectivityManager; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -732,8 +733,13 @@ Context getContext() { private synchronized void runService(final Measurement measurement, final @NonNull StartUpFinishedHandler startUpFinishedHandler) throws DataCapturingException { final Context context = getContext(); - context.registerReceiver(startUpFinishedHandler, - new IntentFilter(MessageCodes.getServiceStartedActionId(appId))); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.registerReceiver(startUpFinishedHandler, + new IntentFilter(MessageCodes.getServiceStartedActionId(appId)), Context.RECEIVER_NOT_EXPORTED); + } else { + context.registerReceiver(startUpFinishedHandler, + new IntentFilter(MessageCodes.getServiceStartedActionId(appId))); + } Log.d(StartUpFinishedHandler.TAG, "DataCapturingService: StartUpFinishedHandler registered for broadcasts."); Log.d(TAG, "Starting the background service for measurement " + measurement + "!"); diff --git a/datacapturing/src/main/java/de/cyface/datacapturing/PongReceiver.java b/datacapturing/src/main/java/de/cyface/datacapturing/PongReceiver.java index 35ad6332f..34a80fc38 100644 --- a/datacapturing/src/main/java/de/cyface/datacapturing/PongReceiver.java +++ b/datacapturing/src/main/java/de/cyface/datacapturing/PongReceiver.java @@ -28,6 +28,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.Process; @@ -136,8 +137,13 @@ public void checkIsRunningAsync(final long timeout, final @NonNull TimeUnit unit // Run receiver on a different thread so it runs even if calling thread waits for it to return: pongReceiverThread.start(); Handler receiverHandler = new Handler(pongReceiverThread.getLooper()); - context.get().registerReceiver(this, new IntentFilter(pongActionId), null, - receiverHandler); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.get().registerReceiver(this, new IntentFilter(pongActionId), null, + receiverHandler, Context.RECEIVER_NOT_EXPORTED); + } else { + context.get().registerReceiver(this, new IntentFilter(pongActionId), null, + receiverHandler); + } long currentUptimeInMillis = SystemClock.uptimeMillis(); long offset = unit.toMillis(timeout); diff --git a/synchronization/src/androidTestCyface/java/de/cyface/synchronization/UploadProgressTest.java b/synchronization/src/androidTestCyface/java/de/cyface/synchronization/UploadProgressTest.java index f39ac9e28..0b5c208be 100644 --- a/synchronization/src/androidTestCyface/java/de/cyface/synchronization/UploadProgressTest.java +++ b/synchronization/src/androidTestCyface/java/de/cyface/synchronization/UploadProgressTest.java @@ -52,6 +52,7 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SyncResult; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; @@ -145,7 +146,11 @@ public void testUploadProgressHappyPath() throws CursorIsNullException, NoSuchMe filter.addAction(CyfaceConnectionStatusListener.SYNC_FINISHED); filter.addAction(CyfaceConnectionStatusListener.SYNC_PROGRESS); filter.addAction(CyfaceConnectionStatusListener.SYNC_STARTED); - context.registerReceiver(receiver, filter); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED); + } else { + context.registerReceiver(receiver, filter); + } ContentProviderClient client = null; try { diff --git a/synchronization/src/main/java/de/cyface/synchronization/ConnectionStatusReceiver.java b/synchronization/src/main/java/de/cyface/synchronization/ConnectionStatusReceiver.java index 9049ec1e5..df6eee699 100644 --- a/synchronization/src/main/java/de/cyface/synchronization/ConnectionStatusReceiver.java +++ b/synchronization/src/main/java/de/cyface/synchronization/ConnectionStatusReceiver.java @@ -13,6 +13,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.os.Build; import de.cyface.utils.Validate; @@ -44,7 +45,11 @@ public ConnectionStatusReceiver(final Context context) { filter.addAction(SYNC_FINISHED); filter.addAction(SYNC_PROGRESS); filter.addAction(SYNC_STARTED); - context.registerReceiver(this, filter); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.registerReceiver(this, filter, Context.RECEIVER_NOT_EXPORTED); + } else { + context.registerReceiver(this, filter); + } } @Override