Bug 1246816 - Get core ping profile creation date from application install time. r=sebastian

MozReview-Commit-ID: Bo07XuqQDWl

--HG--
extra : rebase_source : 865ca66991eb6baeb7fa4ed15f638e38f3ae1a91
This commit is contained in:
Michael Comella 2016-03-31 15:30:39 -07:00
Родитель 41ebdd9152
Коммит fd7c1ea0b3
4 изменённых файлов: 45 добавлений и 20 удалений

Просмотреть файл

@ -39,6 +39,7 @@ import org.mozilla.gecko.util.INISection;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
@ -680,15 +681,38 @@ public final class GeckoProfile {
}
/**
* @return the profile creation date in the format returned by {@link System#currentTimeMillis()} or -1 if the value
* was not found.
* Gets the profile creation date and persists it if it had to be generated.
*
* To get this value, we first look in times.json. If that could not be accessed, we
* return the package's first install date. This is not a perfect solution because a
* user may have large gap between install time and first use.
*
* A more correct algorithm could be the one performed by the JS code in ProfileAge.jsm
* getOldestProfileTimestamp: walk the tree and return the oldest timestamp on the files
* within the profile. However, since times.json will only not exist for the small
* number of really old profiles, we're okay with the package install date compromise for
* simplicity.
*
* @return the profile creation date in the format returned by {@link System#currentTimeMillis()}
* or -1 if the value could not be persisted.
*/
@WorkerThread
public long getProfileCreationDate() {
public long getAndPersistProfileCreationDate(final Context context) {
try {
return getProfileCreationDateFromTimesFile();
} catch (final IOException e) {
return getAndPersistProfileCreationDateFromFilesystem();
Log.d(LOGTAG, "Unable to retrieve profile creation date from times.json. Getting from system...");
final long packageInstallMillis = org.mozilla.gecko.util.ContextUtils.getPackageInstallTime(context);
try {
persistProfileCreationDateToTimesFile(packageInstallMillis);
} catch (final IOException ioEx) {
// We return -1 to ensure the profileCreationDate
// will either be an error (-1) or a consistent value.
Log.w(LOGTAG, "Unable to persist profile creation date - returning -1");
return -1;
}
return packageInstallMillis;
}
}
@ -703,14 +727,17 @@ public final class GeckoProfile {
}
}
/**
* TODO (bug 1246816): Implement ProfileAge.jsm - getOldestProfileTimestamp. Persist results to times.json.
* Update comment in getProfileCreationDate too.
* @return -1 until implemented.
*/
@WorkerThread
private long getAndPersistProfileCreationDateFromFilesystem() {
return -1;
private void persistProfileCreationDateToTimesFile(final long profileCreationMillis) throws IOException {
final JSONObject obj = new JSONObject();
try {
obj.put(PROFILE_CREATION_DATE_JSON_ATTR, profileCreationMillis);
} catch (final JSONException e) {
// Don't log to avoid leaking data in JSONObject.
throw new IOException("Unable to persist profile creation date to times file");
}
Log.d(LOGTAG, "Attempting to write new profile creation date");
writeFile(TIMES_PATH, obj.toString()); // Ideally we'd throw here too.
}
/**

Просмотреть файл

@ -29,7 +29,7 @@ public class TelemetryConstants {
private CorePing() { /* To prevent instantiation */ }
public static final String NAME = "core";
public static final int VERSION_VALUE = 2; // For version history, see toolkit/components/telemetry/docs/core-ping.rst
public static final int VERSION_VALUE = 3; // For version history, see toolkit/components/telemetry/docs/core-ping.rst
public static final String OS_VALUE = "Android";
public static final String ARCHITECTURE = "arch";

Просмотреть файл

@ -92,12 +92,10 @@ public class TelemetryPingGenerator {
ping.put(CorePing.OS_VERSION, Integer.toString(Build.VERSION.SDK_INT)); // A String for cross-platform reasons.
ping.put(CorePing.SEQ, seq);
ping.putArray(CorePing.EXPERIMENTS, Experiments.getActiveExperiments(context));
// TODO (bug 1246816): Remove this "optional" parameter work-around when
// GeckoProfile.getAndPersistProfileCreationDateFromFilesystem is implemented. That method returns -1
// while it's not implemented so we don't include the parameter in the ping if that's the case.
if (profileCreationDate >= 0) {
ping.put(CorePing.PROFILE_CREATION_DATE, profileCreationDate);
}
// `null` indicates failure more clearly than < 0.
final Long finalProfileCreationDate = (profileCreationDate < 0) ? null : profileCreationDate;
ping.put(CorePing.PROFILE_CREATION_DATE, finalProfileCreationDate);
return ping;
}
}

Просмотреть файл

@ -214,8 +214,8 @@ public class TelemetryUploadService extends BackgroundService {
*/
@WorkerThread
private long getProfileCreationDate(final GeckoProfile profile) {
final long profileMillis = profile.getProfileCreationDate();
// TODO (bug 1246816): Remove this work-around when finishing bug. getProfileCreationDate can return -1,
final long profileMillis = profile.getAndPersistProfileCreationDate(this);
// getAndPersistProfileCreationDate can return -1,
// and we don't want to truncate (-1 / MILLIS) to 0.
if (profileMillis < 0) {
return profileMillis;