Bug 1572806 - Added profile_date parameter to Telemetry Activation Ping. r=VladBaicu, a=RyanVM
authorAndrei Lazar <andrei.a.lazar@softvision.ro>
Wed, 25 Sep 2019 15:24:34 +0000
changeset 523831 f10977a01ff66340225e2310d8ff88bfd42ffa6b
parent 523830 bdcc7ff3b644fa4859b03c5e768d32087254f56f
child 523832 c62c32d9da65ea00ee8afd920777961fbf9697ee
push id469
push userryanvm@gmail.com
push dateThu, 10 Oct 2019 18:23:07 +0000
treeherdermozilla-esr68@b07f17122272 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersVladBaicu, RyanVM
bugs1572806
milestone68.2.0
Bug 1572806 - Added profile_date parameter to Telemetry Activation Ping. r=VladBaicu, a=RyanVM Profile creation date (profile_date) is now embedded in Telemetry Activation Ping. Differential Revision: https://phabricator.services.mozilla.com/D47097
mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryActivationPingDelegate.java
mobile/android/base/java/org/mozilla/gecko/telemetry/pingbuilders/TelemetryActivationPingBuilder.java
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryActivationPingDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/TelemetryActivationPingDelegate.java
@@ -85,16 +85,18 @@ public class TelemetryActivationPingDele
 
             final TelemetryActivationPingBuilder pingBuilder = new TelemetryActivationPingBuilder(activity);
             if (identifier != null) {
                 pingBuilder.setIdentifier(identifier);
             } else {
                 pingBuilder.setClientID(clientID);
             }
 
+            pingBuilder.setProfileCreationDate(TelemetryActivationPingBuilder.getProfileCreationDate(activity, profile));
+
             getTelemetryDispatcher().queuePingForUpload(activity, pingBuilder);
         });
     }
 
     @WorkerThread // via constructor
     private TelemetryDispatcher getTelemetryDispatcher() {
         if (telemetryDispatcher == null) {
             final GeckoProfile profile = GeckoThread.getActiveProfile();
--- a/mobile/android/base/java/org/mozilla/gecko/telemetry/pingbuilders/TelemetryActivationPingBuilder.java
+++ b/mobile/android/base/java/org/mozilla/gecko/telemetry/pingbuilders/TelemetryActivationPingBuilder.java
@@ -1,27 +1,31 @@
 package org.mozilla.gecko.telemetry.pingbuilders;
 
 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 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.telemetry.TelemetryOutgoingPing;
 import org.mozilla.gecko.util.DateUtil;
 import org.mozilla.gecko.util.StringUtils;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Locale;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Builds a {@link TelemetryOutgoingPing} representing a activation ping.
  *
  */
 public class TelemetryActivationPingBuilder extends TelemetryPingBuilder {
     private static final String LOGTAG = StringUtils.safeSubstring(TelemetryActivationPingBuilder.class.getSimpleName(), 0, 23);
 
@@ -38,16 +42,17 @@ public class TelemetryActivationPingBuil
     private static final String CLIENT_ID = "clientId";
     private static final String MANUFACTURER = "manufacturer";
     private static final String MODEL = "model";
     private static final String DISTRIBUTION_ID = "distribution_id";
     private static final String LOCALE = "locale";
     private static final String OS_ATTR = "os";
     private static final String OS_VERSION = "osversion";
     private static final String PING_CREATION_DATE = "created";
+    private static final String PROFILE_CREATION_DATE = "profile_date";
     private static final String TIMEZONE_OFFSET = "tz";
     private static final String APP_NAME = "app_name";
     private static final String CHANNEL = "channel";
 
     public TelemetryActivationPingBuilder(final Context context) {
         super(VERSION_VALUE, true);
         initPayloadConstants(context);
     }
@@ -120,9 +125,33 @@ public class TelemetryActivationPingBuil
 
     public TelemetryActivationPingBuilder setClientID(@NonNull final String clientID) {
         if (clientID == null) {
             throw new IllegalArgumentException("Expected non-null clientID");
         }
         payload.put(CLIENT_ID, clientID);
         return this;
     }
+
+    /**
+     * @param date The profile creation date in days to the unix epoch (not millis!), or null if there is an error.
+     */
+    public TelemetryActivationPingBuilder 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;
+    }
+
+    /**
+     * @return the profile creation date in the format expected by
+     *         {@link TelemetryActivationPingBuilder#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));
+    }
 }