Add a delay before scheduling local syncs

am: ba1f790369

Change-Id: I4c880c4111f99d80cee7b960a97133cc0b86d61c
This commit is contained in:
Shreyas Basarge
2016-10-12 17:19:56 +00:00
committed by android-build-merger

View File

@@ -795,7 +795,17 @@ public class SyncManager {
* Use {@link AuthorityInfo#UNDEFINED} to sync all authorities. * Use {@link AuthorityInfo#UNDEFINED} to sync all authorities.
*/ */
public void scheduleSync(Account requestedAccount, int userId, int reason, public void scheduleSync(Account requestedAccount, int userId, int reason,
String requestedAuthority, Bundle extras, int targetSyncState) { String requestedAuthority, Bundle extras, int targetSyncState) {
scheduleSync(requestedAccount, userId, reason, requestedAuthority, extras, targetSyncState,
0 /* min delay */);
}
/**
* @param minDelayMillis The sync can't land before this delay expires.
*/
private void scheduleSync(Account requestedAccount, int userId, int reason,
String requestedAuthority, Bundle extras, int targetSyncState,
final long minDelayMillis) {
final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
if (extras == null) { if (extras == null) {
extras = new Bundle(); extras = new Bundle();
@@ -906,7 +916,7 @@ public class SyncManager {
if (result != null if (result != null
&& result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
scheduleSync(account.account, userId, reason, authority, scheduleSync(account.account, userId, reason, authority,
finalExtras, targetSyncState); finalExtras, targetSyncState, minDelayMillis);
} }
} }
)); ));
@@ -967,7 +977,8 @@ public class SyncManager {
postScheduleSyncMessage( postScheduleSyncMessage(
new SyncOperation(account.account, account.userId, new SyncOperation(account.account, account.userId,
owningUid, owningPackage, reason, source, owningUid, owningPackage, reason, source,
authority, newExtras, allowParallelSyncs) authority, newExtras, allowParallelSyncs),
minDelayMillis
); );
} else if (targetSyncState == AuthorityInfo.UNDEFINED } else if (targetSyncState == AuthorityInfo.UNDEFINED
|| targetSyncState == isSyncable) { || targetSyncState == isSyncable) {
@@ -982,7 +993,8 @@ public class SyncManager {
postScheduleSyncMessage( postScheduleSyncMessage(
new SyncOperation(account.account, account.userId, new SyncOperation(account.account, account.userId,
owningUid, owningPackage, reason, source, owningUid, owningPackage, reason, source,
authority, extras, allowParallelSyncs) authority, extras, allowParallelSyncs),
minDelayMillis
); );
} }
} }
@@ -1088,14 +1100,14 @@ public class SyncManager {
} }
/** /**
* Schedule sync based on local changes to a provider. Occurs within interval * Schedule sync based on local changes to a provider. We wait for at least LOCAL_SYNC_DELAY
* [LOCAL_SYNC_DELAY, 2*LOCAL_SYNC_DELAY]. * ms to batch syncs.
*/ */
public void scheduleLocalSync(Account account, int userId, int reason, String authority) { public void scheduleLocalSync(Account account, int userId, int reason, String authority) {
final Bundle extras = new Bundle(); final Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true); extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
scheduleSync(account, userId, reason, authority, extras, scheduleSync(account, userId, reason, authority, extras,
AuthorityInfo.UNDEFINED); AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY);
} }
public SyncAdapterType[] getSyncAdapterTypes(int userId) { public SyncAdapterType[] getSyncAdapterTypes(int userId) {
@@ -1152,9 +1164,10 @@ public class SyncManager {
mSyncHandler.sendMessageDelayed(monitorMessage, SYNC_MONITOR_WINDOW_LENGTH_MILLIS); mSyncHandler.sendMessageDelayed(monitorMessage, SYNC_MONITOR_WINDOW_LENGTH_MILLIS);
} }
private void postScheduleSyncMessage(SyncOperation syncOperation) { private void postScheduleSyncMessage(SyncOperation syncOperation, long minDelayMillis) {
mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_SCHEDULE_SYNC, syncOperation) ScheduleSyncMessagePayload payload =
.sendToTarget(); new ScheduleSyncMessagePayload(syncOperation, minDelayMillis);
mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_SCHEDULE_SYNC, payload).sendToTarget();
} }
/** /**
@@ -1194,6 +1207,16 @@ public class SyncManager {
} }
} }
private static class ScheduleSyncMessagePayload {
final SyncOperation syncOperation;
final long minDelayMillis;
ScheduleSyncMessagePayload(SyncOperation syncOperation, long minDelayMillis) {
this.syncOperation = syncOperation;
this.minDelayMillis = minDelayMillis;
}
}
private void clearBackoffSetting(EndPoint target) { private void clearBackoffSetting(EndPoint target) {
Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target); Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target);
if (backoff != null && backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE && if (backoff != null && backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE &&
@@ -1262,7 +1285,7 @@ public class SyncManager {
if (!op.isPeriodic && op.target.matchesSpec(target)) { if (!op.isPeriodic && op.target.matchesSpec(target)) {
count++; count++;
getJobScheduler().cancel(op.jobId); getJobScheduler().cancel(op.jobId);
postScheduleSyncMessage(op); postScheduleSyncMessage(op, 0 /* min delay */);
} }
} }
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -2417,8 +2440,10 @@ public class SyncManager {
mDataConnectionIsConnected = readDataConnectionState(); mDataConnectionIsConnected = readDataConnectionState();
switch (msg.what) { switch (msg.what) {
case MESSAGE_SCHEDULE_SYNC: case MESSAGE_SCHEDULE_SYNC:
SyncOperation op = (SyncOperation) msg.obj; ScheduleSyncMessagePayload syncPayload =
scheduleSyncOperationH(op); (ScheduleSyncMessagePayload) msg.obj;
SyncOperation op = syncPayload.syncOperation;
scheduleSyncOperationH(op, syncPayload.minDelayMillis);
break; break;
case MESSAGE_START_SYNC: case MESSAGE_START_SYNC:
@@ -3101,7 +3126,8 @@ public class SyncManager {
maybeRescheduleSync(syncResult, syncOperation); maybeRescheduleSync(syncResult, syncOperation);
} else { } else {
// create a normal sync instance that will respect adapter backoffs // create a normal sync instance that will respect adapter backoffs
postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation()); postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation(),
0 /* min delay */);
} }
historyMessage = ContentResolver.syncErrorToString( historyMessage = ContentResolver.syncErrorToString(
syncResultToErrorNumber(syncResult)); syncResultToErrorNumber(syncResult));