Skip to content

Commit 2056bfc

Browse files
authored
lazy initialization of heartbeat (#1925)
* lazy initialization of heartbeat * google java format
1 parent c99b61c commit 2056bfc

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.firebase.FirebaseApp;
2727
import com.google.firebase.FirebaseOptions;
2828
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
29+
import com.google.firebase.inject.Provider;
2930
import com.google.firebase.installations.FirebaseInstallationsException.Status;
3031
import com.google.firebase.installations.local.IidStore;
3132
import com.google.firebase.installations.local.PersistedInstallation;
@@ -117,8 +118,8 @@ public Thread newThread(Runnable r) {
117118
/** package private constructor. */
118119
FirebaseInstallations(
119120
FirebaseApp firebaseApp,
120-
@Nullable UserAgentPublisher publisher,
121-
@Nullable HeartBeatInfo heartbeatInfo) {
121+
@NonNull Provider<UserAgentPublisher> publisher,
122+
@NonNull Provider<HeartBeatInfo> heartbeatInfo) {
122123
this(
123124
new ThreadPoolExecutor(
124125
CORE_POOL_SIZE,

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallationsRegistrar.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public List<Component<?>> getComponents() {
3434
return Arrays.asList(
3535
Component.builder(FirebaseInstallationsApi.class)
3636
.add(Dependency.required(FirebaseApp.class))
37-
.add(Dependency.required(HeartBeatInfo.class))
38-
.add(Dependency.required(UserAgentPublisher.class))
37+
.add(Dependency.optionalProvider(HeartBeatInfo.class))
38+
.add(Dependency.optionalProvider(UserAgentPublisher.class))
3939
.factory(
4040
c ->
4141
new FirebaseInstallations(
4242
c.get(FirebaseApp.class),
43-
c.get(UserAgentPublisher.class),
44-
c.get(HeartBeatInfo.class)))
43+
c.getProvider(UserAgentPublisher.class),
44+
c.getProvider(HeartBeatInfo.class)))
4545
.build(),
4646
LibraryVersionComponent.create("fire-installations", BuildConfig.VERSION_NAME));
4747
}

firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.android.gms.common.util.VisibleForTesting;
3131
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
3232
import com.google.firebase.heartbeatinfo.HeartBeatInfo.HeartBeat;
33+
import com.google.firebase.inject.Provider;
3334
import com.google.firebase.installations.FirebaseInstallationsException;
3435
import com.google.firebase.installations.FirebaseInstallationsException.Status;
3536
import com.google.firebase.installations.remote.InstallationResponse.ResponseCode;
@@ -101,13 +102,13 @@ public class FirebaseInstallationServiceClient {
101102
static final String PARSING_EXPIRATION_TIME_ERROR_MESSAGE = "Invalid Expiration Timestamp.";
102103

103104
private final Context context;
104-
private final UserAgentPublisher userAgentPublisher;
105-
private final HeartBeatInfo heartbeatInfo;
105+
private final Provider<UserAgentPublisher> userAgentPublisher;
106+
private final Provider<HeartBeatInfo> heartbeatInfo;
106107

107108
public FirebaseInstallationServiceClient(
108109
@NonNull Context context,
109-
@Nullable UserAgentPublisher publisher,
110-
@Nullable HeartBeatInfo heartbeatInfo) {
110+
@NonNull Provider<UserAgentPublisher> publisher,
111+
@NonNull Provider<HeartBeatInfo> heartbeatInfo) {
111112
this.context = context;
112113
this.userAgentPublisher = publisher;
113114
this.heartbeatInfo = heartbeatInfo;
@@ -433,10 +434,12 @@ private HttpURLConnection openHttpURLConnection(URL url, String apiKey)
433434
httpURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
434435
httpURLConnection.addRequestProperty(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_DIRECTIVE);
435436
httpURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName());
436-
if (heartbeatInfo != null && userAgentPublisher != null) {
437-
HeartBeat heartbeat = heartbeatInfo.getHeartBeatCode(FIREBASE_INSTALLATIONS_ID_HEARTBEAT_TAG);
437+
if ((heartbeatInfo.get() != null) && (userAgentPublisher.get() != null)) {
438+
HeartBeat heartbeat =
439+
heartbeatInfo.get().getHeartBeatCode(FIREBASE_INSTALLATIONS_ID_HEARTBEAT_TAG);
438440
if (heartbeat != HeartBeat.NONE) {
439-
httpURLConnection.addRequestProperty(USER_AGENT_HEADER, userAgentPublisher.getUserAgent());
441+
httpURLConnection.addRequestProperty(
442+
USER_AGENT_HEADER, userAgentPublisher.get().getUserAgent());
440443
httpURLConnection.addRequestProperty(
441444
HEART_BEAT_HEADER, Integer.toString(heartbeat.getCode()));
442445
}

0 commit comments

Comments
 (0)