Bug 1243585 - Add methods to CorePingBuilder to generate a builder. r=sebastian
Thoughts:
* An alternative design put this code in a CorePingUtil but I decided these
methods are tied closely to the TelemetryCorePingBuilder.
* Adding these methods makes it less clear what the class is about (without
filtering on public methods that is).
I'm not sure what the best trade-off is.
Note: this is not yet expected to compile.
MozReview-Commit-ID: FQYFP3ioewN
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/core/TelemetryCorePingBuilder.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/core/TelemetryCorePingBuilder.java
@@ -2,28 +2,38 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mozilla.gecko.telemetry.core;
import android.content.Context;
+import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
+import android.support.annotation.WorkerThread;
+import android.text.TextUtils;
import org.mozilla.gecko.AppConstants;
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.Locales;
+import org.mozilla.gecko.distribution.DistributionStoreCallback;
+import org.mozilla.gecko.search.SearchEngine;
+import org.mozilla.gecko.telemetry.TelemetryConstants;
import org.mozilla.gecko.telemetry.TelemetryPing;
import org.mozilla.gecko.telemetry.TelemetryPingBuilder;
import org.mozilla.gecko.util.Experiments;
import org.mozilla.gecko.util.StringUtils;
+import java.io.IOException;
import java.util.Locale;
+import java.util.concurrent.TimeUnit;
/**
* Builds a {@link TelemetryPing} representing a core ping.
*
* See https://gecko.readthedocs.org/en/latest/toolkit/components/telemetry/telemetry/core-ping.html
* for details on the core ping.
*/
public class TelemetryCorePingBuilder extends TelemetryPingBuilder {
@@ -111,30 +121,64 @@ public class TelemetryCorePingBuilder ex
if (distributionID == null) {
throw new IllegalArgumentException("Expected non-null distribution ID");
}
payload.put(DISTRIBUTION_ID, distributionID);
return this;
}
/**
- * @param date a positive date value, or null if there is an error.
+ * @param date The profile creation date in days to the unix epoch (not millis!), or null if there is an error.
*/
public TelemetryCorePingBuilder setProfileCreationDate(@Nullable final Long date) {
if (date != null && date < 0) {
throw new IllegalArgumentException("Expect positive date value. Received: " + date);
}
payload.put(PROFILE_CREATION_DATE, date);
return this;
}
- // TODO (mcomella): We can potentially build two pings with the same seq no if we leave seq as an argument.
/**
* @param seq a positive sequence number.
*/
public TelemetryCorePingBuilder setSequenceNumber(final int seq) {
if (seq < 0) {
throw new IllegalArgumentException("Expected positive sequence number. Recived: " + seq);
}
payload.put(SEQ, seq);
return this;
}
+
+ public static String getServer(final SharedPreferences sharedPrefs) {
+ // TODO (bug 1241685): Sync this preference with the gecko preference.
+ return sharedPrefs.getString(TelemetryConstants.PREF_SERVER_URL, TelemetryConstants.DEFAULT_SERVER_URL);
+ }
+
+ @WorkerThread // synchronous shared prefs write.
+ public static int getAndIncrementSequenceNumberSync(final SharedPreferences sharedPrefsForProfile) {
+ final int seq = sharedPrefsForProfile.getInt(TelemetryConstants.PREF_SEQ_COUNT, 1);
+
+ // We store synchronously before constructing to ensure this sequence number will not be re-used.
+ sharedPrefsForProfile.edit().putInt(TelemetryConstants.PREF_SEQ_COUNT, seq + 1).commit();
+ return seq;
+ }
+
+ /**
+ * @return the profile creation date in the format expected by
+ * {@link TelemetryCorePingBuilder#setProfileCreationDate(Long)}.
+ */
+ @WorkerThread
+ public static Long getProfileCreationDate(final Context context, final GeckoProfile profile) {
+ final long profileMillis = profile.getAndPersistProfileCreationDate(context);
+ if (profileMillis < 0) {
+ return null;
+ }
+ return (long) Math.floor((double) profileMillis / TimeUnit.DAYS.toMillis(1));
+ }
+
+ /**
+ * @return the search engine identifier in the format expected by the core ping.
+ */
+ public static String getEngineIdentifier(final SearchEngine searchEngine) {
+ final String identifier = searchEngine.getIdentifier();
+ return TextUtils.isEmpty(identifier) ? null : identifier;
+ }
}