зеркало из https://github.com/mozilla/gecko-dev.git
Bug 802749 - Make background sync over metered connections optional. r=Grisha Kruglov
MozReview-Commit-ID: 4UhQqyxT90N --HG-- extra : rebase_source : 937a8f6b9515678811b5d03e1a7514ef32b98af1
This commit is contained in:
Родитель
2da58727b1
Коммит
e72a4fece2
|
@ -68,6 +68,10 @@
|
|||
<!ENTITY fxaccount_status_passwords2 'Logins'>
|
||||
<!ENTITY fxaccount_status_tabs 'Open tabs'>
|
||||
<!ENTITY fxaccount_status_additional_settings 'Additional Settings'>
|
||||
<!ENTITY fxaccount_pref_sync_use_metered 'Sync over metered connections'>
|
||||
<!-- Localization note: Only affects background syncing, user initiated
|
||||
syncs will still be done regardless of the connection -->
|
||||
<!ENTITY fxaccount_pref_sync_use_metered_summary 'Allow usage of cellular data and metered wi-fi to sync in the background'>
|
||||
<!ENTITY fxaccount_status_legal 'Legal' >
|
||||
<!-- Localization note: when tapped, the following two strings link to
|
||||
external web pages. Compare fxaccount_policy_{linktos,linkprivacy}:
|
||||
|
|
|
@ -13,13 +13,9 @@ import android.content.IntentFilter;
|
|||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.*;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.preference.Preference.OnPreferenceClickListener;
|
||||
import android.preference.PreferenceCategory;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.DateUtils;
|
||||
|
@ -37,6 +33,7 @@ import org.mozilla.gecko.fxa.SyncStatusListener;
|
|||
import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
|
||||
import org.mozilla.gecko.fxa.login.Married;
|
||||
import org.mozilla.gecko.fxa.login.State;
|
||||
import org.mozilla.gecko.fxa.sync.FxAccountSyncAdapter;
|
||||
import org.mozilla.gecko.fxa.sync.FxAccountSyncStatusHelper;
|
||||
import org.mozilla.gecko.sync.ExtendedJSONObject;
|
||||
import org.mozilla.gecko.sync.SharedPreferencesClientsDataDelegate;
|
||||
|
@ -105,6 +102,7 @@ public class FxAccountStatusFragment
|
|||
protected CheckBoxPreference passwordsPreference;
|
||||
|
||||
protected EditTextPreference deviceNamePreference;
|
||||
protected SwitchPreference syncOverMeteredPreference;
|
||||
protected Preference syncServerPreference;
|
||||
protected Preference syncNowPreference;
|
||||
|
||||
|
@ -184,6 +182,9 @@ public class FxAccountStatusFragment
|
|||
tabsPreference.setOnPreferenceClickListener(this);
|
||||
passwordsPreference.setOnPreferenceClickListener(this);
|
||||
|
||||
syncOverMeteredPreference = (SwitchPreference) ensureFindPreference(FxAccountSyncAdapter.PREFS_SYNC_METERED);
|
||||
syncOverMeteredPreference.setOnPreferenceChangeListener(this);
|
||||
|
||||
deviceNamePreference = (EditTextPreference) ensureFindPreference("device_name");
|
||||
deviceNamePreference.setOnPreferenceChangeListener(this);
|
||||
|
||||
|
@ -948,6 +949,14 @@ public class FxAccountStatusFragment
|
|||
return true;
|
||||
}
|
||||
|
||||
if (preference == syncOverMeteredPreference) {
|
||||
try {
|
||||
fxAccount.getSyncPrefs().edit().putBoolean(FxAccountSyncAdapter.PREFS_SYNC_METERED, (Boolean) newValue).apply();
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Failed to save the new for syncMeteredPreference");
|
||||
}
|
||||
}
|
||||
|
||||
// For everything else, accept the change.
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -147,6 +147,7 @@ public class AndroidFxAccount {
|
|||
|
||||
protected final Context context;
|
||||
private final AccountManager accountManager;
|
||||
private final long neverSynced = -1;
|
||||
|
||||
// This is really, really meant to be final. Only changed when account name changes.
|
||||
// See Bug 1368147.
|
||||
|
@ -647,7 +648,7 @@ public class AndroidFxAccount {
|
|||
*
|
||||
* @param stagesToSync stage names to sync; can be null to sync <b>all</b> known stages.
|
||||
* @param stagesToSkip stage names to skip; can be null to skip <b>no</b> known stages.
|
||||
* @param ignoreSettings whether we should check preferences for syncing over metered connections.
|
||||
* @param ignoreSettings whether we should check if syncing is allowed via in-app or system settings.
|
||||
*/
|
||||
public void requestImmediateSync(String[] stagesToSync, String[] stagesToSkip, boolean ignoreSettings) {
|
||||
FirefoxAccounts.requestImmediateSync(getAndroidAccount(), stagesToSync, stagesToSkip, ignoreSettings);
|
||||
|
@ -799,7 +800,6 @@ public class AndroidFxAccount {
|
|||
}
|
||||
|
||||
public long getLastSyncedTimestamp() {
|
||||
final long neverSynced = -1L;
|
||||
try {
|
||||
return getSyncPrefs().getLong(PREF_KEY_LAST_SYNCED_TIMESTAMP, neverSynced);
|
||||
} catch (Exception e) {
|
||||
|
@ -808,6 +808,15 @@ public class AndroidFxAccount {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean neverSynced() {
|
||||
try {
|
||||
return getSyncPrefs().getLong(PREF_KEY_LAST_SYNCED_TIMESTAMP, neverSynced) == -1;
|
||||
} catch (Exception e) {
|
||||
Logger.warn(LOG_TAG, "Got exception getting last synced time; ignoring.", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Debug only! This is dangerous!
|
||||
public void unsafeTransitionToDefaultEndpoints() {
|
||||
unsafeTransitionToStageEndpoints(
|
||||
|
|
|
@ -12,6 +12,7 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SyncResult;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
@ -73,6 +74,8 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
|
||||
// Tracks the last seen storage hostname for backoff purposes.
|
||||
private static final String PREF_BACKOFF_STORAGE_HOST = "backoffStorageHost";
|
||||
// Preference key for allowing sync over metered connections.
|
||||
public static final String PREFS_SYNC_METERED = "sync.allow_metered";
|
||||
|
||||
// Used to do cheap in-memory rate limiting. Don't sync again if we
|
||||
// successfully synced within this duration.
|
||||
|
@ -502,6 +505,27 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
final Context context = getContext();
|
||||
final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
|
||||
|
||||
// This flag is used to conclude whether we should ignore syncing
|
||||
// based on user preference for syncing over metered connections.
|
||||
boolean shouldRejectSyncViaSettings = false;
|
||||
// Check whether we should ignore settings or not.
|
||||
if (!extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false)) {
|
||||
// If it's not user-initiated, we should check if we are allowed to sync on metered connections.
|
||||
boolean isMeteredAllowed = true;
|
||||
try {
|
||||
isMeteredAllowed = fxAccount.getSyncPrefs().getBoolean(PREFS_SYNC_METERED, true);
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Failed to read sync preferences. Allowing metered connections by default.");
|
||||
}
|
||||
// Check if the device is on a metered connection or not.
|
||||
final ConnectivityManager manager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
final boolean isMetered = manager.isActiveNetworkMetered();
|
||||
// If the connection is metered and syncing over metered connections is
|
||||
// not permitted, we should bail.
|
||||
shouldRejectSyncViaSettings = !isMeteredAllowed && isMetered;
|
||||
}
|
||||
|
||||
|
||||
// NB: we use elapsedRealtime which is time since boot, to ensure our clock is monotonic and isn't
|
||||
// paused while CPU is in the power-saving mode.
|
||||
final long syncDeadline = SystemClock.elapsedRealtime() + SYNC_DEADLINE_DELTA_MILLIS;
|
||||
|
@ -545,9 +569,26 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
Collection<String> knownStageNames = SyncConfiguration.validEngineNames();
|
||||
Collection<String> stageNamesToSync = Utils.getStagesToSyncFromBundle(knownStageNames, extras);
|
||||
|
||||
// If syncing should be rejected due to metered connection preferences
|
||||
// and we are doing the first sync ever, we should at least sync the
|
||||
// 'clients' collection to ensure we upload our local client record.
|
||||
// see {@link <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=802749">Bug 802749</a>}
|
||||
// for more information.
|
||||
if (shouldRejectSyncViaSettings && fxAccount.neverSynced()) {
|
||||
stageNamesToSync.clear();
|
||||
stageNamesToSync.add("clients");
|
||||
}
|
||||
|
||||
final SyncDelegate syncDelegate = new SyncDelegate(latch, syncResult, fxAccount, stageNamesToSync);
|
||||
Result offeredResult = null;
|
||||
|
||||
if (shouldRejectSyncViaSettings && !fxAccount.neverSynced()) {
|
||||
// The user is on a metered connection and has disabled syncing over metered connections,
|
||||
// we should reject the sync.
|
||||
syncDelegate.rejectSync();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// This will be the same chunk of SharedPreferences that we pass through to GlobalSession/SyncConfiguration.
|
||||
final SharedPreferences sharedPrefs = fxAccount.getSyncPrefs();
|
||||
|
|
|
@ -84,6 +84,11 @@
|
|||
android:key="device_name"
|
||||
android:persistent="false"
|
||||
android:title="@string/fxaccount_status_device_name" />
|
||||
<SwitchPreference
|
||||
android:key="sync.allow_metered"
|
||||
android:defaultValue="true"
|
||||
android:title="@string/fxaccount_pref_sync_use_metered"
|
||||
android:summary="@string/fxaccount_pref_sync_use_metered_summary"/>
|
||||
|
||||
<org.mozilla.gecko.fxa.activities.CustomColorPreference
|
||||
android:editable="false"
|
||||
|
|
|
@ -47,6 +47,8 @@
|
|||
<string name="fxaccount_status_history">&fxaccount_status_history;</string>
|
||||
<string name="fxaccount_status_passwords">&fxaccount_status_passwords2;</string>
|
||||
<string name="fxaccount_status_tabs">&fxaccount_status_tabs;</string>
|
||||
<string name="fxaccount_pref_sync_use_metered">&fxaccount_pref_sync_use_metered;</string>
|
||||
<string name="fxaccount_pref_sync_use_metered_summary">&fxaccount_pref_sync_use_metered_summary;</string>
|
||||
<string name="fxaccount_status_additional_settings">&fxaccount_status_additional_settings;</string>
|
||||
<string name="fxaccount_status_legal">&fxaccount_status_legal;</string>
|
||||
<string name="fxaccount_status_linktos">&fxaccount_status_linktos2;</string>
|
||||
|
|
Загрузка…
Ссылка в новой задаче