-
Notifications
You must be signed in to change notification settings - Fork 571
/
FirebaseApp.java
717 lines (648 loc) · 25.2 KB
/
FirebaseApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.firebase;
import static com.google.android.gms.common.util.Base64Utils.encodeUrlSafeNoPadding;
import android.annotation.TargetApi;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.collection.ArrayMap;
import androidx.core.os.UserManagerCompat;
import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.android.gms.common.api.internal.BackgroundDetector;
import com.google.android.gms.common.internal.Objects;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.common.util.PlatformVersion;
import com.google.android.gms.common.util.ProcessUtils;
import com.google.firebase.components.Component;
import com.google.firebase.components.ComponentDiscovery;
import com.google.firebase.components.ComponentDiscoveryService;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.ComponentRuntime;
import com.google.firebase.components.Lazy;
import com.google.firebase.concurrent.ExecutorsRegistrar;
import com.google.firebase.events.Publisher;
import com.google.firebase.heartbeatinfo.DefaultHeartBeatController;
import com.google.firebase.inject.Provider;
import com.google.firebase.internal.DataCollectionConfigStorage;
import com.google.firebase.provider.FirebaseInitProvider;
import com.google.firebase.tracing.ComponentMonitor;
import com.google.firebase.tracing.FirebaseTrace;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/**
* The entry point of Firebase SDKs. It holds common configuration and state for Firebase APIs. Most
* applications don't need to directly interact with FirebaseApp.
*
* <p>For a vast majority of apps, {@link FirebaseInitProvider} will handle the initialization of
* Firebase for the default project that it's configured to work with, via the data contained in the
* app's <code>google-services.json</code> file. This <code>
* ContentProvider</code> is merged into the app's manifest by default when building with Gradle,
* and it runs automatically at app launch. <strong>No additional lines of code are needed in this
* case.</strong>
*
* <p>In the event that an app requires access to another Firebase project <strong>in addition
* to</strong> the default project, {@link FirebaseApp#initializeApp(Context, FirebaseOptions,
* String)} must be used to create that relationship programmatically. The name parameter must be
* unique. To connect to the resources exposed by that project, use the {@link FirebaseApp} object
* returned by {@link FirebaseApp#getInstance(String)}, passing it the same name used with <code>
* initializeApp</code>. This object must be passed to the static accessor of the feature that
* provides the resource. For example, {@link
* com.google.firebase.storage.FirebaseStorage#getInstance(FirebaseApp)getInstance(FirebaseApp)} is
* used to access the storage bucket provided by the additional project, whereas {@link
* com.google.firebase.storage.FirebaseStorage#getInstance()} is used to access the default project.
*
* <p>Any <code>FirebaseApp</code> initialization must occur only in the main process of the app.
* Use of Firebase in processes other than the main process is not supported and will likely cause
* problems related to resource contention.
*/
public class FirebaseApp {
private static final String LOG_TAG = "FirebaseApp";
public static final @NonNull String DEFAULT_APP_NAME = "[DEFAULT]";
private static final Object LOCK = new Object();
/** A map of (name, FirebaseApp) instances. */
@GuardedBy("LOCK")
static final Map<String, FirebaseApp> INSTANCES = new ArrayMap<>();
private final Context applicationContext;
private final String name;
private final FirebaseOptions options;
private final ComponentRuntime componentRuntime;
// Default disabled. We released Firebase publicly without this feature, so making it default
// enabled is a backwards incompatible change.
private final AtomicBoolean automaticResourceManagementEnabled = new AtomicBoolean(false);
private final AtomicBoolean deleted = new AtomicBoolean();
private final Lazy<DataCollectionConfigStorage> dataCollectionConfigStorage;
private final Provider<DefaultHeartBeatController> defaultHeartBeatController;
private final List<BackgroundStateChangeListener> backgroundStateChangeListeners =
new CopyOnWriteArrayList<>();
private final List<FirebaseAppLifecycleListener> lifecycleListeners =
new CopyOnWriteArrayList<>();
/** Returns the application {@link Context}. */
@NonNull
public Context getApplicationContext() {
checkNotDeleted();
return applicationContext;
}
/** Returns the unique name of this app. */
@NonNull
public String getName() {
checkNotDeleted();
return name;
}
/** Returns the specified {@link FirebaseOptions}. */
@NonNull
public FirebaseOptions getOptions() {
checkNotDeleted();
return options;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof FirebaseApp)) {
return false;
}
return name.equals(((FirebaseApp) o).getName());
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("name", name).add("options", options).toString();
}
/** Returns a mutable list of all FirebaseApps. */
@NonNull
public static List<FirebaseApp> getApps(@NonNull Context context) {
synchronized (LOCK) {
return new ArrayList<>(INSTANCES.values());
}
}
/**
* Returns the default (first initialized) instance of the {@link FirebaseApp}.
*
* @throws IllegalStateException if the default app was not initialized.
*/
@NonNull
public static FirebaseApp getInstance() {
synchronized (LOCK) {
FirebaseApp defaultApp = INSTANCES.get(DEFAULT_APP_NAME);
if (defaultApp == null) {
throw new IllegalStateException(
"Default FirebaseApp is not initialized in this "
+ "process "
+ ProcessUtils.getMyProcessName()
+ ". Make sure to call "
+ "FirebaseApp.initializeApp(Context) first.");
}
defaultApp.defaultHeartBeatController.get().registerHeartBeat();
return defaultApp;
}
}
/**
* Returns the instance identified by the unique name, or throws if it does not exist.
*
* @param name represents the name of the {@link FirebaseApp} instance.
* @throws IllegalStateException if the {@link FirebaseApp} was not initialized, either via {@link
* #initializeApp(Context, FirebaseOptions, String)}.
*/
@NonNull
public static FirebaseApp getInstance(@NonNull String name) {
synchronized (LOCK) {
FirebaseApp firebaseApp = INSTANCES.get(normalize(name));
if (firebaseApp != null) {
firebaseApp.defaultHeartBeatController.get().registerHeartBeat();
return firebaseApp;
}
List<String> availableAppNames = getAllAppNames();
String availableAppNamesMessage;
if (availableAppNames.isEmpty()) {
availableAppNamesMessage = "";
} else {
availableAppNamesMessage =
"Available app names: " + TextUtils.join(", ", availableAppNames);
}
String errorMessage =
String.format(
"FirebaseApp with name %s doesn't exist. %s", name, availableAppNamesMessage);
throw new IllegalStateException(errorMessage);
}
}
/**
* Initializes the default FirebaseApp instance using string resource values - populated from
* google-services.json. It also initializes Firebase Analytics for the current process.
*
* <p>This method is called at app startup time by {@link FirebaseInitProvider}. Call this method
* before any Firebase APIs in components outside the main process.
*
* <p>The {@link FirebaseOptions} values used by the default app instance are read from string
* resources.
*
* <p>
*
* @return the default FirebaseApp, if either it has been initialized previously, or Firebase API
* keys are present in string resources. Returns null otherwise.
*/
@Nullable
public static FirebaseApp initializeApp(@NonNull Context context) {
synchronized (LOCK) {
if (INSTANCES.containsKey(DEFAULT_APP_NAME)) {
return getInstance();
}
FirebaseOptions firebaseOptions = FirebaseOptions.fromResource(context);
if (firebaseOptions == null) {
Log.w(
LOG_TAG,
"Default FirebaseApp failed to initialize because no default "
+ "options were found. This usually means that com.google.gms:google-services was "
+ "not applied to your gradle project.");
return null;
}
return initializeApp(context, firebaseOptions);
}
}
/**
* Initializes the default {@link FirebaseApp} instance. Same as {@link #initializeApp(Context,
* FirebaseOptions, String)}, but it uses {@link #DEFAULT_APP_NAME} as name.
*
* <p>It's only required to call this to initialize Firebase if it's <strong>not possible</strong>
* to do so automatically in {@link FirebaseInitProvider}. Automatic initialization that way is
* the expected situation.
*/
@NonNull
public static FirebaseApp initializeApp(
@NonNull Context context, @NonNull FirebaseOptions options) {
return initializeApp(context, options, DEFAULT_APP_NAME);
}
/**
* A factory method to initialize a {@link FirebaseApp}.
*
* @param context represents the {@link Context}
* @param options represents the global {@link FirebaseOptions}
* @param name unique name for the app. It is an error to initialize an app with an already
* existing name. Starting and ending whitespace characters in the name are ignored (trimmed).
* @return an instance of {@link FirebaseApp}
* @throws IllegalStateException if an app with the same name has already been initialized.
*/
@NonNull
public static FirebaseApp initializeApp(
@NonNull Context context, @NonNull FirebaseOptions options, @NonNull String name) {
GlobalBackgroundStateListener.ensureBackgroundStateListenerRegistered(context);
String normalizedName = normalize(name);
final FirebaseApp firebaseApp;
Context applicationContext;
if (context.getApplicationContext() == null) {
// In shared processes' content providers getApplicationContext() can return null.
applicationContext = context;
} else {
applicationContext = context.getApplicationContext();
}
synchronized (LOCK) {
Preconditions.checkState(
!INSTANCES.containsKey(normalizedName),
"FirebaseApp name " + normalizedName + " already exists!");
Preconditions.checkNotNull(applicationContext, "Application context cannot be null.");
firebaseApp = new FirebaseApp(applicationContext, normalizedName, options);
INSTANCES.put(normalizedName, firebaseApp);
}
firebaseApp.initializeAllApis();
return firebaseApp;
}
/**
* Deletes the {@link FirebaseApp} and all its data. All calls to this {@link FirebaseApp}
* instance will throw once it has been called.
*
* <p>A no-op if delete was called before.
*
* @hide
*/
public void delete() {
boolean valueChanged = deleted.compareAndSet(false /* expected */, true);
if (!valueChanged) {
return;
}
synchronized (LOCK) {
INSTANCES.remove(this.name);
}
notifyOnAppDeleted();
}
/**
* Returns an instance of the requested component.
*
* @hide
*/
@KeepForSdk
public <T> T get(Class<T> anInterface) {
checkNotDeleted();
return componentRuntime.get(anInterface);
}
/**
* If set to true it indicates that Firebase should close database connections automatically when
* the app is in the background. Disabled by default.
*/
public void setAutomaticResourceManagementEnabled(boolean enabled) {
checkNotDeleted();
boolean updated =
automaticResourceManagementEnabled.compareAndSet(
!enabled /* expect */, enabled /* update */);
if (updated) {
boolean inBackground = BackgroundDetector.getInstance().isInBackground();
if (enabled && inBackground) {
// Automatic resource management has been enabled while the app is in the
// background, notify the listeners of the app being in the background.
notifyBackgroundStateChangeListeners(true);
} else if (!enabled && inBackground) {
// Automatic resource management has been disabled while the app is in the
// background, act as if we were in the foreground.
notifyBackgroundStateChangeListeners(false);
}
}
}
/**
* Determine whether automatic data collection is enabled or disabled by default in all SDKs.
*
* <p>Note: this value is respected by all SDKs unless overridden by the developer via SDK
* specific mechanisms.
*
* @return true if automatic data collection is enabled by default and false otherwise
* @hide
*/
@KeepForSdk
public boolean isDataCollectionDefaultEnabled() {
checkNotDeleted();
return dataCollectionConfigStorage.get().isEnabled();
}
/**
* Enable or disable automatic data collection across all SDKs.
*
* <p>Note: this value is respected by all SDKs unless overridden by the developer via SDK
* specific mechanisms.
*
* @hide
*/
@KeepForSdk
public void setDataCollectionDefaultEnabled(Boolean enabled) {
checkNotDeleted();
dataCollectionConfigStorage.get().setEnabled(enabled);
}
/**
* Enable or disable automatic data collection across all SDKs.
*
* <p>Note: this value is respected by all SDKs unless overridden by the developer via SDK
* specific mechanisms.
*
* @hide
* @deprecated Use {@link #setDataCollectionDefaultEnabled(Boolean)} instead.
*/
@KeepForSdk
@Deprecated
public void setDataCollectionDefaultEnabled(boolean enabled) {
setDataCollectionDefaultEnabled(Boolean.valueOf(enabled));
}
/**
* Default constructor.
*
* @hide
*/
protected FirebaseApp(Context applicationContext, String name, FirebaseOptions options) {
this.applicationContext = Preconditions.checkNotNull(applicationContext);
this.name = Preconditions.checkNotEmpty(name);
this.options = Preconditions.checkNotNull(options);
StartupTime startupTime = FirebaseInitProvider.getStartupTime();
FirebaseTrace.pushTrace("Firebase");
FirebaseTrace.pushTrace("ComponentDiscovery");
List<Provider<ComponentRegistrar>> registrars =
ComponentDiscovery.forContext(applicationContext, ComponentDiscoveryService.class)
.discoverLazy();
FirebaseTrace.popTrace(); // ComponentDiscovery
FirebaseTrace.pushTrace("Runtime");
ComponentRuntime.Builder builder =
ComponentRuntime.builder(com.google.firebase.concurrent.UiExecutor.INSTANCE)
.addLazyComponentRegistrars(registrars)
.addComponentRegistrar(new FirebaseCommonRegistrar())
.addComponentRegistrar(new ExecutorsRegistrar())
.addComponent(Component.of(applicationContext, Context.class))
.addComponent(Component.of(this, FirebaseApp.class))
.addComponent(Component.of(options, FirebaseOptions.class))
.setProcessor(new ComponentMonitor());
// Don't provide StartupTime in direct boot mode or if Firebase was manually started
if (UserManagerCompat.isUserUnlocked(applicationContext)
&& FirebaseInitProvider.isCurrentlyInitializing()) {
builder.addComponent(Component.of(startupTime, StartupTime.class));
}
componentRuntime = builder.build();
FirebaseTrace.popTrace(); // Runtime
dataCollectionConfigStorage =
new Lazy<>(
() ->
new DataCollectionConfigStorage(
applicationContext,
getPersistenceKey(),
componentRuntime.get(Publisher.class)));
defaultHeartBeatController = componentRuntime.getProvider(DefaultHeartBeatController.class);
addBackgroundStateChangeListener(
background -> {
if (!background) {
defaultHeartBeatController.get().registerHeartBeat();
}
});
FirebaseTrace.popTrace(); // Firebase
}
private void checkNotDeleted() {
Preconditions.checkState(!deleted.get(), "FirebaseApp was deleted");
}
/** @hide */
@KeepForSdk
@VisibleForTesting
public boolean isDefaultApp() {
return DEFAULT_APP_NAME.equals(getName());
}
/** @hide */
@VisibleForTesting
@RestrictTo(Scope.TESTS)
void initializeAllComponents() {
componentRuntime.initializeAllComponentsForTests();
}
private void notifyBackgroundStateChangeListeners(boolean background) {
Log.d(LOG_TAG, "Notifying background state change listeners.");
for (BackgroundStateChangeListener listener : backgroundStateChangeListeners) {
listener.onBackgroundStateChanged(background);
}
}
/**
* Registers a background state change listener. Make sure to call {@link
* #removeBackgroundStateChangeListener(BackgroundStateChangeListener)} as appropriate to avoid
* memory leaks.
*
* <p>If automatic resource management is enabled and the app is in the background a callback is
* triggered immediately.
*
* @hide
* @see BackgroundStateChangeListener
*/
@KeepForSdk
public void addBackgroundStateChangeListener(BackgroundStateChangeListener listener) {
checkNotDeleted();
if (automaticResourceManagementEnabled.get()
&& BackgroundDetector.getInstance().isInBackground()) {
listener.onBackgroundStateChanged(true /* isInBackground */);
}
backgroundStateChangeListeners.add(listener);
}
/**
* Unregisters the background state change listener.
*
* @hide
*/
@KeepForSdk
public void removeBackgroundStateChangeListener(BackgroundStateChangeListener listener) {
checkNotDeleted();
backgroundStateChangeListeners.remove(listener);
}
/**
* Use this key to store data per FirebaseApp.
*
* @hide
*/
@KeepForSdk
public String getPersistenceKey() {
return encodeUrlSafeNoPadding(getName().getBytes(Charset.defaultCharset()))
+ "+"
+ encodeUrlSafeNoPadding(
getOptions().getApplicationId().getBytes(Charset.defaultCharset()));
}
/**
* If an API has locally stored data it must register lifecycle listeners at initialization time.
*
* @hide
*/
// TODO: make sure that all APIs that are interested in these events are
// initialized using reflection when an app is deleted (for v5).
@KeepForSdk
public void addLifecycleEventListener(@NonNull FirebaseAppLifecycleListener listener) {
checkNotDeleted();
Preconditions.checkNotNull(listener);
lifecycleListeners.add(listener);
}
/** @hide */
@KeepForSdk
public void removeLifecycleEventListener(@NonNull FirebaseAppLifecycleListener listener) {
checkNotDeleted();
Preconditions.checkNotNull(listener);
lifecycleListeners.remove(listener);
}
/**
* Notifies all listeners with the name and options of the deleted {@link FirebaseApp} instance.
*/
private void notifyOnAppDeleted() {
for (FirebaseAppLifecycleListener listener : lifecycleListeners) {
listener.onDeleted(name, options);
}
}
/** @hide */
@VisibleForTesting
public static void clearInstancesForTest() {
// TODO: also delete, once functionality is implemented.
synchronized (LOCK) {
INSTANCES.clear();
}
}
/**
* Returns persistence key. Exists to support getting {@link FirebaseApp} persistence key after
* the app has been deleted.
*
* @hide
*/
@KeepForSdk
public static String getPersistenceKey(String name, FirebaseOptions options) {
return encodeUrlSafeNoPadding(name.getBytes(Charset.defaultCharset()))
+ "+"
+ encodeUrlSafeNoPadding(options.getApplicationId().getBytes(Charset.defaultCharset()));
}
private static List<String> getAllAppNames() {
List<String> allAppNames = new ArrayList<>();
synchronized (LOCK) {
for (FirebaseApp app : INSTANCES.values()) {
allAppNames.add(app.getName());
}
}
Collections.sort(allAppNames);
return allAppNames;
}
/** Initializes all appropriate APIs for this instance. */
private void initializeAllApis() {
boolean inDirectBoot = !UserManagerCompat.isUserUnlocked(applicationContext);
if (inDirectBoot) {
Log.i(
LOG_TAG,
"Device in Direct Boot Mode: postponing initialization of Firebase APIs for app "
+ getName());
// Ensure that all APIs are initialized once the user unlocks the phone.
UserUnlockReceiver.ensureReceiverRegistered(applicationContext);
} else {
Log.i(LOG_TAG, "Device unlocked: initializing all Firebase APIs for app " + getName());
componentRuntime.initializeEagerComponents(isDefaultApp());
defaultHeartBeatController.get().registerHeartBeat();
}
}
/** Normalizes the app name. */
private static String normalize(@NonNull String name) {
return name.trim();
}
/**
* Used to deliver notifications about whether the app is in the background. The first callback is
* invoked inline if the app is in the background.
*
* <p>Doesn't fire on pre-ICS devices.
*
* <p>If the app is in the background and {@link #setAutomaticResourceManagementEnabled(boolean)}
* is set to false.
*
* <p>
*
* @hide
*/
@KeepForSdk
public interface BackgroundStateChangeListener {
/**
* @param background True, if the app is in the background and automatic resource management is
* enabled.
*/
@KeepForSdk
void onBackgroundStateChanged(boolean background);
}
/**
* Utility class that initializes Firebase APIs when the user unlocks the device, if the app is
* first started in direct boot mode.
*
* @hide
*/
@TargetApi(Build.VERSION_CODES.N)
private static class UserUnlockReceiver extends BroadcastReceiver {
private static AtomicReference<UserUnlockReceiver> INSTANCE = new AtomicReference<>();
private final Context applicationContext;
public UserUnlockReceiver(Context applicationContext) {
this.applicationContext = applicationContext;
}
private static void ensureReceiverRegistered(Context applicationContext) {
if (INSTANCE.get() == null) {
UserUnlockReceiver receiver = new UserUnlockReceiver(applicationContext);
if (INSTANCE.compareAndSet(null /* expected */, receiver)) {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_USER_UNLOCKED);
applicationContext.registerReceiver(receiver, intentFilter);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
// API initialization is idempotent.
synchronized (LOCK) {
for (FirebaseApp app : INSTANCES.values()) {
app.initializeAllApis();
}
}
unregister();
}
public void unregister() {
applicationContext.unregisterReceiver(this);
}
}
/** Registers an activity lifecycle listener on ICS+ devices. */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private static class GlobalBackgroundStateListener
implements BackgroundDetector.BackgroundStateChangeListener {
private static AtomicReference<GlobalBackgroundStateListener> INSTANCE =
new AtomicReference<>();
private static void ensureBackgroundStateListenerRegistered(Context context) {
if (!(PlatformVersion.isAtLeastIceCreamSandwich()
&& context.getApplicationContext() instanceof Application)) {
return;
}
Application application = (Application) context.getApplicationContext();
if (INSTANCE.get() == null) {
GlobalBackgroundStateListener listener = new GlobalBackgroundStateListener();
if (INSTANCE.compareAndSet(null /* expected */, listener)) {
BackgroundDetector.initialize(application);
BackgroundDetector.getInstance().addListener(listener);
}
}
}
@Override
public void onBackgroundStateChanged(boolean background) {
synchronized (LOCK) {
for (FirebaseApp app : new ArrayList<>(INSTANCES.values())) {
if (app.automaticResourceManagementEnabled.get()) {
app.notifyBackgroundStateChangeListeners(background);
}
}
}
}
}
}