Bug 1241685 - Add TelemetryPreferences. r=ahunt

MozReview-Commit-ID: 5Tw0GUX0tmW

--HG--
extra : rebase_source : 627a1ae0df2923dcc64f186453e46cc70579a080
This commit is contained in:
Michael Comella 2016-06-08 17:20:31 -07:00
parent b053ea4034
commit dbd6e16080
3 changed files with 75 additions and 4 deletions

View File

@ -7,13 +7,10 @@ package org.mozilla.gecko.telemetry;
import org.mozilla.gecko.AppConstants;
public class TelemetryConstants {
// Change these two values to enable upload in developer builds.
// To test, set this to true & change "toolkit.telemetry.server" in about:config.
public static final boolean UPLOAD_ENABLED = AppConstants.MOZILLA_OFFICIAL; // Disabled for developer builds.
public static final String DEFAULT_SERVER_URL = "https://incoming.telemetry.mozilla.org";
public static final String USER_AGENT =
"Firefox-Android-Telemetry/" + AppConstants.MOZ_APP_VERSION + " (" + AppConstants.MOZ_APP_UA_NAME + ")";
public static final String PREF_SERVER_URL = "telemetry-serverUrl";
}

View File

@ -0,0 +1,73 @@
/* 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;
import android.content.Context;
import android.content.SharedPreferences;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.PrefsHelper;
import org.mozilla.gecko.PrefsHelper.PrefHandler;
import java.lang.ref.WeakReference;
/**
* Manages getting and setting any preferences related to telemetry.
*
* This class persists any Gecko preferences beyond shutdown so that these values
* can be accessed on the next run before Gecko is started as we expect Telemetry
* to run before Gecko is available.
*/
public class TelemetryPreferences {
private TelemetryPreferences() {}
private static final String GECKO_PREF_SERVER_URL = "toolkit.telemetry.server";
private static final String SHARED_PREF_SERVER_URL = "telemetry-serverUrl";
// Defaults are a mirror of about:config defaults so we can access them before Gecko is available.
private static final String DEFAULT_SERVER_URL = "https://incoming.telemetry.mozilla.org";
private static final String[] OBSERVED_PREFS = {
GECKO_PREF_SERVER_URL,
};
public static String getServerSchemeHostPort(final Context context, final String profileName) {
return getSharedPrefs(context, profileName).getString(SHARED_PREF_SERVER_URL, DEFAULT_SERVER_URL);
}
public static void initPreferenceObserver(final Context context, final String profileName) {
final PrefHandler prefHandler = new TelemetryPrefHandler(context, profileName);
PrefsHelper.addObserver(OBSERVED_PREFS, prefHandler); // gets preference value when gecko starts.
}
private static SharedPreferences getSharedPrefs(final Context context, final String profileName) {
return GeckoSharedPrefs.forProfileName(context, profileName);
}
private static class TelemetryPrefHandler extends PrefsHelper.PrefHandlerBase {
private final WeakReference<Context> contextWeakReference;
private final String profileName;
private TelemetryPrefHandler(final Context context, final String profileName) {
contextWeakReference = new WeakReference<>(context);
this.profileName = profileName;
}
@Override
public void prefValue(final String pref, final String value) {
final Context context = contextWeakReference.get();
if (context == null) {
return;
}
if (!pref.equals(GECKO_PREF_SERVER_URL)) {
throw new IllegalStateException("Unknown preference: " + pref);
}
getSharedPrefs(context, profileName).edit()
.putString(SHARED_PREF_SERVER_URL, value)
.apply();
}
}
}

View File

@ -593,6 +593,7 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'telemetry/TelemetryCorePingDelegate.java',
'telemetry/TelemetryDispatcher.java',
'telemetry/TelemetryPing.java',
'telemetry/TelemetryPreferences.java',
'telemetry/TelemetryUploadService.java',
'TelemetryContract.java',
'text/FloatingActionModeCallback.java',