зеркало из 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 : 39d6b13861b93b29bee28a116ee076295c2795f0
This commit is contained in:
Родитель
689d61c952
Коммит
db3c651010
|
@ -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;
|
||||
|
@ -28,6 +24,7 @@ import com.squareup.picasso.Picasso;
|
|||
import com.squareup.picasso.Target;
|
||||
|
||||
import org.mozilla.gecko.AppConstants;
|
||||
import org.mozilla.gecko.GeckoSharedPrefs;
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountUtils;
|
||||
|
@ -37,6 +34,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 +103,7 @@ public class FxAccountStatusFragment
|
|||
protected CheckBoxPreference passwordsPreference;
|
||||
|
||||
protected EditTextPreference deviceNamePreference;
|
||||
protected SwitchPreference syncOverMeteredPreference;
|
||||
protected Preference syncServerPreference;
|
||||
protected Preference syncNowPreference;
|
||||
|
||||
|
@ -184,6 +183,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 +950,10 @@ public class FxAccountStatusFragment
|
|||
return true;
|
||||
}
|
||||
|
||||
if (preference == syncOverMeteredPreference) {
|
||||
GeckoSharedPrefs.forApp(getContext()).edit().putBoolean(FxAccountSyncAdapter.PREFS_SYNC_METERED, (Boolean) newValue).apply();
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
@ -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,10 +12,12 @@ 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;
|
||||
|
||||
import org.mozilla.gecko.GeckoSharedPrefs;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountUtils;
|
||||
import org.mozilla.gecko.background.fxa.SkewHandler;
|
||||
|
@ -73,6 +75,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.
|
||||
|
@ -496,6 +500,22 @@ public class FxAccountSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||
*/
|
||||
@Override
|
||||
public void onPerformSync(final Account account, final Bundle extras, final String authority, ContentProviderClient provider, final SyncResult syncResult) {
|
||||
|
||||
// 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.
|
||||
final boolean isMeteredAllowed = GeckoSharedPrefs.forApp(getContext()).getBoolean(PREFS_SYNC_METERED, true);
|
||||
// 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;
|
||||
}
|
||||
|
||||
Logger.setThreadLogTag(FxAccountConstants.GLOBAL_LOG_TAG);
|
||||
Logger.resetLogging();
|
||||
|
||||
|
@ -545,9 +565,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>
|
||||
|
|
Загрузка…
Ссылка в новой задаче