@@ -29,15 +29,15 @@ public class DataCollectionArbiter {
29
29
private static final String FIREBASE_CRASHLYTICS_COLLECTION_ENABLED =
30
30
"firebase_crashlytics_collection_enabled" ;
31
31
32
+ private final SharedPreferences sharedPreferences ;
33
+ private final FirebaseApp firebaseApp ;
34
+
32
35
// State for waitForDataCollectionEnabled().
33
- private Object taskLock = new Object ();
36
+ private final Object taskLock = new Object ();
34
37
TaskCompletionSource <Void > dataCollectionEnabledTask = new TaskCompletionSource <>();
35
38
boolean taskResolved = false ;
36
39
37
- private final SharedPreferences sharedPreferences ;
38
- private volatile boolean crashlyticsDataCollectionExplicitlySet ;
39
- private volatile boolean crashlyticsDataCollectionEnabled ;
40
- private final FirebaseApp firebaseApp ;
40
+ private Boolean crashlyticsDataCollectionEnabled ;
41
41
42
42
/**
43
43
* A Task that will be resolved when explicit data collection permission is granted by calling
@@ -47,43 +47,17 @@ public class DataCollectionArbiter {
47
47
new TaskCompletionSource <>();
48
48
49
49
public DataCollectionArbiter (FirebaseApp app ) {
50
- this .firebaseApp = app ;
51
- Context applicationContext = app .getApplicationContext ();
52
- if (applicationContext == null ) {
53
- throw new RuntimeException ("null context" );
54
- }
50
+ final Context applicationContext = app .getApplicationContext ();
55
51
52
+ firebaseApp = app ;
56
53
sharedPreferences = CommonUtils .getSharedPrefs (applicationContext );
57
54
58
- boolean enabled = true ;
59
- boolean explicitlySet = false ;
60
-
61
- if (sharedPreferences .contains (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED )) {
62
- enabled = sharedPreferences .getBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , true );
63
- explicitlySet = true ;
64
- } else {
65
- try {
66
- final PackageManager packageManager = applicationContext .getPackageManager ();
67
- if (packageManager != null ) {
68
- final ApplicationInfo applicationInfo =
69
- packageManager .getApplicationInfo (
70
- applicationContext .getPackageName (), PackageManager .GET_META_DATA );
71
- if (applicationInfo != null
72
- && applicationInfo .metaData != null
73
- && applicationInfo .metaData .containsKey (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED )) {
74
- enabled = applicationInfo .metaData .getBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED );
75
- explicitlySet = true ;
76
- }
77
- }
78
- } catch (PackageManager .NameNotFoundException e ) {
79
- // This shouldn't happen since it's this app's package, but fall through to default
80
- // if so.
81
- Logger .getLogger ().d ("Unable to get PackageManager. Falling through" , e );
82
- }
55
+ Boolean dataCollectionEnabled = getDataCollectionValueFromSharedPreferences (sharedPreferences );
56
+ if (dataCollectionEnabled == null ) {
57
+ dataCollectionEnabled = getDataCollectionValueFromManifest (applicationContext );
83
58
}
84
59
85
- crashlyticsDataCollectionEnabled = enabled ;
86
- crashlyticsDataCollectionExplicitlySet = explicitlySet ;
60
+ crashlyticsDataCollectionEnabled = dataCollectionEnabled ;
87
61
88
62
synchronized (taskLock ) {
89
63
if (isAutomaticDataCollectionEnabled ()) {
@@ -93,27 +67,21 @@ public DataCollectionArbiter(FirebaseApp app) {
93
67
}
94
68
}
95
69
96
- public boolean isAutomaticDataCollectionEnabled () {
97
- if (crashlyticsDataCollectionExplicitlySet ) {
98
- return crashlyticsDataCollectionEnabled ;
99
- }
100
- return firebaseApp .isDataCollectionDefaultEnabled ();
101
- }
102
-
103
- public Task <Void > waitForAutomaticDataCollectionEnabled () {
104
- synchronized (taskLock ) {
105
- return dataCollectionEnabledTask .getTask ();
106
- }
70
+ public synchronized boolean isAutomaticDataCollectionEnabled () {
71
+ return crashlyticsDataCollectionEnabled != null
72
+ ? crashlyticsDataCollectionEnabled
73
+ : firebaseApp .isDataCollectionDefaultEnabled ();
107
74
}
108
75
109
- @ SuppressLint ({"CommitPrefEdits" , "ApplySharedPref" })
110
- public void setCrashlyticsDataCollectionEnabled (boolean enabled ) {
111
- crashlyticsDataCollectionEnabled = enabled ;
112
- crashlyticsDataCollectionExplicitlySet = true ;
113
- sharedPreferences .edit ().putBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , enabled ).commit ();
76
+ public synchronized void setCrashlyticsDataCollectionEnabled (Boolean enabled ) {
77
+ crashlyticsDataCollectionEnabled =
78
+ (enabled != null )
79
+ ? enabled
80
+ : getDataCollectionValueFromManifest (firebaseApp .getApplicationContext ());
81
+ storeDataCollectionValueInSharedPreferences (sharedPreferences , enabled );
114
82
115
83
synchronized (taskLock ) {
116
- if (enabled ) {
84
+ if (isAutomaticDataCollectionEnabled () ) {
117
85
if (!taskResolved ) {
118
86
dataCollectionEnabledTask .trySetResult (null );
119
87
taskResolved = true ;
@@ -127,6 +95,12 @@ public void setCrashlyticsDataCollectionEnabled(boolean enabled) {
127
95
}
128
96
}
129
97
98
+ public Task <Void > waitForAutomaticDataCollectionEnabled () {
99
+ synchronized (taskLock ) {
100
+ return dataCollectionEnabledTask .getTask ();
101
+ }
102
+ }
103
+
130
104
/**
131
105
* Returns a task which will be resolved when either: 1) automatic data collection has been
132
106
* enabled, or 2) grantDataCollectionPermission has been called.
@@ -150,4 +124,55 @@ public void grantDataCollectionPermission(boolean dataCollectionToken) {
150
124
}
151
125
dataCollectionExplicitlyApproved .trySetResult (null );
152
126
}
127
+
128
+ @ SuppressLint ({"ApplySharedPref" })
129
+ private static void storeDataCollectionValueInSharedPreferences (
130
+ SharedPreferences sharedPreferences , Boolean enabled ) {
131
+ final SharedPreferences .Editor prefsEditor = sharedPreferences .edit ();
132
+ if (enabled != null ) {
133
+ prefsEditor .putBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , enabled );
134
+ } else {
135
+ prefsEditor .remove (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED );
136
+ }
137
+ prefsEditor .commit ();
138
+ }
139
+
140
+ private static Boolean getDataCollectionValueFromSharedPreferences (
141
+ SharedPreferences sharedPreferences ) {
142
+ if (sharedPreferences .contains (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED )) {
143
+ return sharedPreferences .getBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED , true );
144
+ }
145
+ return null ;
146
+ }
147
+
148
+ private static Boolean getDataCollectionValueFromManifest (Context applicationContext ) {
149
+ final Boolean manifestSetting =
150
+ readCrashlyticsDataCollectionEnabledFromManifest (applicationContext );
151
+ if (manifestSetting == null ) {
152
+ return null ;
153
+ }
154
+ return Boolean .TRUE .equals (manifestSetting );
155
+ }
156
+
157
+ private static Boolean readCrashlyticsDataCollectionEnabledFromManifest (
158
+ Context applicationContext ) {
159
+ try {
160
+ final PackageManager packageManager = applicationContext .getPackageManager ();
161
+ if (packageManager != null ) {
162
+ final ApplicationInfo applicationInfo =
163
+ packageManager .getApplicationInfo (
164
+ applicationContext .getPackageName (), PackageManager .GET_META_DATA );
165
+ if (applicationInfo != null
166
+ && applicationInfo .metaData != null
167
+ && applicationInfo .metaData .containsKey (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED )) {
168
+ return applicationInfo .metaData .getBoolean (FIREBASE_CRASHLYTICS_COLLECTION_ENABLED );
169
+ }
170
+ }
171
+ } catch (PackageManager .NameNotFoundException e ) {
172
+ // This shouldn't happen since it's this app's package, but fall through to default
173
+ // if so.
174
+ Logger .getLogger ().d ("Unable to get PackageManager. Falling through" , e );
175
+ }
176
+ return null ;
177
+ }
153
178
}
0 commit comments