Skip to content

Commit d4c30ec

Browse files
authored
add manifest flag for model downloader data collection (#2423)
1 parent 411c9fa commit d4c30ec

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import android.content.Context;
1818
import android.content.SharedPreferences;
1919
import android.content.SharedPreferences.Editor;
20+
import android.content.pm.ApplicationInfo;
21+
import android.content.pm.PackageManager;
2022
import android.os.SystemClock;
2123
import androidx.annotation.NonNull;
2224
import androidx.annotation.Nullable;
@@ -31,6 +33,8 @@
3133
/** @hide */
3234
public class SharedPreferencesUtil {
3335

36+
private static final String FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED =
37+
"firebase_model_downloader_collection_enabled";
3438
public static final String DOWNLOADING_MODEL_ID_MATCHER = "downloading_model_id_(.*?)_([^/]+)/?";
3539

3640
public static final String PREFERENCES_PACKAGE_NAME = "com.google.firebase.ml.modelDownloader";
@@ -253,7 +257,8 @@ public synchronized Set<CustomModel> listDownloadedModels() {
253257
/**
254258
* Should Firelog logging be enabled.
255259
*
256-
* @return whether or not firelog events should be logged. If not specifically set, defaults to fi
260+
* @return whether or not firelog events should be logged. Checks shared preference, then
261+
* manifest, finally defaults to Firebase wide data collection switch.
257262
*/
258263
public synchronized boolean getCustomModelStatsCollectionFlag() {
259264
if (getSharedPreferences()
@@ -262,9 +267,36 @@ public synchronized boolean getCustomModelStatsCollectionFlag() {
262267
.getBoolean(
263268
String.format(EVENT_LOGGING_ENABLED_PATTERN, CUSTOM_MODEL_LIB, persistenceKey), true);
264269
}
270+
Boolean manifestFlag =
271+
readModelDownloaderCollectionEnabledFromManifest(firebaseApp.getApplicationContext());
272+
if (manifestFlag != null) {
273+
return manifestFlag;
274+
}
265275
return firebaseApp.isDataCollectionDefaultEnabled();
266276
}
267277

278+
@Nullable
279+
private static Boolean readModelDownloaderCollectionEnabledFromManifest(
280+
Context applicationContext) {
281+
try {
282+
final PackageManager packageManager = applicationContext.getPackageManager();
283+
if (packageManager != null) {
284+
final ApplicationInfo applicationInfo =
285+
packageManager.getApplicationInfo(
286+
applicationContext.getPackageName(), PackageManager.GET_META_DATA);
287+
if (applicationInfo != null
288+
&& applicationInfo.metaData != null
289+
&& applicationInfo.metaData.containsKey(FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED)) {
290+
return applicationInfo.metaData.getBoolean(FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED);
291+
}
292+
}
293+
} catch (PackageManager.NameNotFoundException e) {
294+
// This shouldn't happen since it's this app's package, but fall through to default
295+
// if so.
296+
}
297+
return null;
298+
}
299+
268300
/**
269301
* Set whether firelog logging should be enabled. When not explicitly set, uses the Firebase wide
270302
* data collection switch.

0 commit comments

Comments
 (0)