17
17
import android .content .Context ;
18
18
import android .content .SharedPreferences ;
19
19
import android .content .SharedPreferences .Editor ;
20
+ import android .content .pm .ApplicationInfo ;
21
+ import android .content .pm .PackageManager ;
20
22
import android .os .SystemClock ;
21
23
import androidx .annotation .NonNull ;
22
24
import androidx .annotation .Nullable ;
31
33
/** @hide */
32
34
public class SharedPreferencesUtil {
33
35
36
+ private static final String FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED =
37
+ "firebase_model_downloader_collection_enabled" ;
34
38
public static final String DOWNLOADING_MODEL_ID_MATCHER = "downloading_model_id_(.*?)_([^/]+)/?" ;
35
39
36
40
public static final String PREFERENCES_PACKAGE_NAME = "com.google.firebase.ml.modelDownloader" ;
@@ -253,7 +257,8 @@ public synchronized Set<CustomModel> listDownloadedModels() {
253
257
/**
254
258
* Should Firelog logging be enabled.
255
259
*
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.
257
262
*/
258
263
public synchronized boolean getCustomModelStatsCollectionFlag () {
259
264
if (getSharedPreferences ()
@@ -262,9 +267,36 @@ public synchronized boolean getCustomModelStatsCollectionFlag() {
262
267
.getBoolean (
263
268
String .format (EVENT_LOGGING_ENABLED_PATTERN , CUSTOM_MODEL_LIB , persistenceKey ), true );
264
269
}
270
+ Boolean manifestFlag =
271
+ readModelDownloaderCollectionEnabledFromManifest (firebaseApp .getApplicationContext ());
272
+ if (manifestFlag != null ) {
273
+ return manifestFlag ;
274
+ }
265
275
return firebaseApp .isDataCollectionDefaultEnabled ();
266
276
}
267
277
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
+
268
300
/**
269
301
* Set whether firelog logging should be enabled. When not explicitly set, uses the Firebase wide
270
302
* data collection switch.
0 commit comments