Skip to content

Commit ff6cc57

Browse files
authored
Proposed fix for issue 5593 (#5597)
Possible fix for #5593 Android 14 requires `RECEIVER_EXPORTED` or `RECEIVER_NOT_EXPORTED` flag when using `registerReceiver` command: Currently, `com.google.firebase:firebase-ml-modeldownloader:24.2.1` uses the `registerReceiver` without this flag: https://github.com/firebase/firebase-android-sdk/blob/ac19e5d561d6a7a24a6004746d95f4e08024fdc4/firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java#L214-L215 Proposed fix: ```java context.registerReceiver( broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), context.RECEIVER_NOT_EXPORTED); ``` An alternative solution is using ContextCompat but requires version 1.9.0 of `androidx.core:core`. I noticed in the [Android documentation](https://developer.android.com/guide/components/broadcasts#java) is using `ContextCompat` for calling `registerReceiver`, or for the flag `RECEIVER_EXPORTED` & `RECEIVER_NOT_EXPORTED`: ```java ContextCompat.registerReceiver( context, broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), ContextCompat.RECEIVER_NOT_EXPORTED); ``` However, `ContextCompat.RECEIVER_NOT_EXPORTED` and the `RECEIVER_NOT_EXPORTED` from `context` have the same value, so I don't think this is necessary to use ContextCompat. FYI, added `@SuppressLint("WrongConstant")` annotation due to the method throwing an error: https://stackoverflow.com/a/74433641
1 parent 3d88f62 commit ff6cc57

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

firebase-ml-modeldownloader/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
2-
2+
* [fixed] Fixed `SecurityException` where the `RECEIVER_EXPORTED` or `RECEIVER_NOT_EXPORTED` flag should be
3+
specified when registerReceiver is being used. [#5597](https://github.com/firebase/firebase-android-sdk/pull/5597)
34

45
# 24.2.1
56
* [changed] Internal infrastructure improvements.

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.ml.modeldownloader.internal;
1616

17+
import android.annotation.SuppressLint;
1718
import android.app.DownloadManager;
1819
import android.app.DownloadManager.Query;
1920
import android.app.DownloadManager.Request;
@@ -23,6 +24,7 @@
2324
import android.content.IntentFilter;
2425
import android.database.Cursor;
2526
import android.net.Uri;
27+
import android.os.Build;
2628
import android.os.Build.VERSION;
2729
import android.os.Build.VERSION_CODES;
2830
import android.os.ParcelFileDescriptor;
@@ -207,12 +209,20 @@ private synchronized void removeDownloadTaskInstance(long downloadId) {
207209
this.receiverMaps.remove(downloadId);
208210
}
209211

212+
@SuppressLint("WrongConstant")
210213
private Task<Void> registerReceiverForDownloadId(long downloadId, String modelName) {
211214
BroadcastReceiver broadcastReceiver = getReceiverInstance(downloadId, modelName);
212215
// It is okay to always register here. Since the broadcast receiver is the same via the lookup
213216
// for the same download id, the same broadcast receiver will be notified only once.
214-
context.registerReceiver(
215-
broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
217+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
218+
context.registerReceiver(
219+
broadcastReceiver,
220+
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
221+
Context.RECEIVER_EXPORTED);
222+
} else {
223+
context.registerReceiver(
224+
broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
225+
}
216226

217227
return getTaskCompletionSourceInstance(downloadId).getTask();
218228
}

0 commit comments

Comments
 (0)