Skip to content

add manifest flag for model downloader data collection #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -31,6 +33,8 @@
/** @hide */
public class SharedPreferencesUtil {

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

public static final String PREFERENCES_PACKAGE_NAME = "com.google.firebase.ml.modelDownloader";
Expand Down Expand Up @@ -253,7 +257,8 @@ public synchronized Set<CustomModel> listDownloadedModels() {
/**
* Should Firelog logging be enabled.
*
* @return whether or not firelog events should be logged. If not specifically set, defaults to fi
* @return whether or not firelog events should be logged. Checks shared preference, then
* manifest, finally defaults to Firebase wide data collection switch.
*/
public synchronized boolean getCustomModelStatsCollectionFlag() {
if (getSharedPreferences()
Expand All @@ -262,9 +267,36 @@ public synchronized boolean getCustomModelStatsCollectionFlag() {
.getBoolean(
String.format(EVENT_LOGGING_ENABLED_PATTERN, CUSTOM_MODEL_LIB, persistenceKey), true);
}
Boolean manifestFlag =
readModelDownloaderCollectionEnabledFromManifest(firebaseApp.getApplicationContext());
if (manifestFlag != null) {
return manifestFlag;
}
return firebaseApp.isDataCollectionDefaultEnabled();
}

@Nullable
private static Boolean readModelDownloaderCollectionEnabledFromManifest(
Context applicationContext) {
try {
final PackageManager packageManager = applicationContext.getPackageManager();
if (packageManager != null) {
final ApplicationInfo applicationInfo =
packageManager.getApplicationInfo(
applicationContext.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null
&& applicationInfo.metaData != null
&& applicationInfo.metaData.containsKey(FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED)) {
return applicationInfo.metaData.getBoolean(FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED);
}
}
} catch (PackageManager.NameNotFoundException e) {
// This shouldn't happen since it's this app's package, but fall through to default
// if so.
}
return null;
}

/**
* Set whether firelog logging should be enabled. When not explicitly set, uses the Firebase wide
* data collection switch.
Expand Down