Skip to content

lazy initialization of heartbeat #1925

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 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
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 @@ -26,6 +26,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsException.Status;
import com.google.firebase.installations.local.IidStore;
import com.google.firebase.installations.local.PersistedInstallation;
Expand Down Expand Up @@ -117,8 +118,8 @@ public Thread newThread(Runnable r) {
/** package private constructor. */
FirebaseInstallations(
FirebaseApp firebaseApp,
@Nullable UserAgentPublisher publisher,
@Nullable HeartBeatInfo heartbeatInfo) {
@NonNull Provider<UserAgentPublisher> publisher,
@NonNull Provider<HeartBeatInfo> heartbeatInfo) {
this(
new ThreadPoolExecutor(
CORE_POOL_SIZE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public List<Component<?>> getComponents() {
return Arrays.asList(
Component.builder(FirebaseInstallationsApi.class)
.add(Dependency.required(FirebaseApp.class))
.add(Dependency.required(HeartBeatInfo.class))
.add(Dependency.required(UserAgentPublisher.class))
.add(Dependency.optionalProvider(HeartBeatInfo.class))
.add(Dependency.optionalProvider(UserAgentPublisher.class))
.factory(
c ->
new FirebaseInstallations(
c.get(FirebaseApp.class),
c.get(UserAgentPublisher.class),
c.get(HeartBeatInfo.class)))
c.getProvider(UserAgentPublisher.class),
c.getProvider(HeartBeatInfo.class)))
.build(),
LibraryVersionComponent.create("fire-installations", BuildConfig.VERSION_NAME));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.android.gms.common.util.VisibleForTesting;
import com.google.firebase.heartbeatinfo.HeartBeatInfo;
import com.google.firebase.heartbeatinfo.HeartBeatInfo.HeartBeat;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsException;
import com.google.firebase.installations.FirebaseInstallationsException.Status;
import com.google.firebase.installations.remote.InstallationResponse.ResponseCode;
Expand Down Expand Up @@ -101,13 +102,13 @@ public class FirebaseInstallationServiceClient {
static final String PARSING_EXPIRATION_TIME_ERROR_MESSAGE = "Invalid Expiration Timestamp.";

private final Context context;
private final UserAgentPublisher userAgentPublisher;
private final HeartBeatInfo heartbeatInfo;
private final Provider<UserAgentPublisher> userAgentPublisher;
private final Provider<HeartBeatInfo> heartbeatInfo;

public FirebaseInstallationServiceClient(
@NonNull Context context,
@Nullable UserAgentPublisher publisher,
@Nullable HeartBeatInfo heartbeatInfo) {
@NonNull Provider<UserAgentPublisher> publisher,
@NonNull Provider<HeartBeatInfo> heartbeatInfo) {
this.context = context;
this.userAgentPublisher = publisher;
this.heartbeatInfo = heartbeatInfo;
Expand Down Expand Up @@ -433,10 +434,12 @@ private HttpURLConnection openHttpURLConnection(URL url, String apiKey)
httpURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
httpURLConnection.addRequestProperty(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_DIRECTIVE);
httpURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName());
if (heartbeatInfo != null && userAgentPublisher != null) {
HeartBeat heartbeat = heartbeatInfo.getHeartBeatCode(FIREBASE_INSTALLATIONS_ID_HEARTBEAT_TAG);
if ((heartbeatInfo.get() != null) && (userAgentPublisher.get() != null)) {
HeartBeat heartbeat =
heartbeatInfo.get().getHeartBeatCode(FIREBASE_INSTALLATIONS_ID_HEARTBEAT_TAG);
if (heartbeat != HeartBeat.NONE) {
httpURLConnection.addRequestProperty(USER_AGENT_HEADER, userAgentPublisher.getUserAgent());
httpURLConnection.addRequestProperty(
USER_AGENT_HEADER, userAgentPublisher.get().getUserAgent());
httpURLConnection.addRequestProperty(
HEART_BEAT_HEADER, Integer.toString(heartbeat.getCode()));
}
Expand Down