1717package android .content ;
1818
1919import com .android .internal .R ;
20- import com .android .internal .util .ArrayUtils ;
2120import com .google .android .collect .Lists ;
2221import com .google .android .collect .Maps ;
2322
3231import android .app .Notification ;
3332import android .app .NotificationManager ;
3433import android .app .PendingIntent ;
35- import android .app .DownloadManager .Request ;
3634import android .content .SyncStorageEngine .OnSyncRequestListener ;
3735import android .content .pm .ApplicationInfo ;
3836import android .content .pm .PackageManager ;
@@ -1998,13 +1996,21 @@ private long maybeStartNextSyncLocked() {
19981996 ActiveSyncContext conflict = null ;
19991997 ActiveSyncContext longRunning = null ;
20001998 ActiveSyncContext toReschedule = null ;
1999+ ActiveSyncContext oldestNonExpeditedRegular = null ;
20012000
20022001 for (ActiveSyncContext activeSyncContext : mActiveSyncContexts ) {
20032002 final SyncOperation activeOp = activeSyncContext .mSyncOperation ;
20042003 if (activeOp .isInitialization ()) {
20052004 numInit ++;
20062005 } else {
20072006 numRegular ++;
2007+ if (!activeOp .isExpedited ()) {
2008+ if (oldestNonExpeditedRegular == null
2009+ || (oldestNonExpeditedRegular .mStartTime
2010+ > activeSyncContext .mStartTime )) {
2011+ oldestNonExpeditedRegular = activeSyncContext ;
2012+ }
2013+ }
20082014 }
20092015 if (activeOp .account .type .equals (candidate .account .type )
20102016 && activeOp .authority .equals (candidate .authority )
@@ -2027,8 +2033,13 @@ private long maybeStartNextSyncLocked() {
20272033 Log .v (TAG , " numActiveInit=" + numInit + ", numActiveRegular=" + numRegular );
20282034 Log .v (TAG , " longRunning: " + longRunning );
20292035 Log .v (TAG , " conflict: " + conflict );
2036+ Log .v (TAG , " oldestNonExpeditedRegular: " + oldestNonExpeditedRegular );
20302037 }
20312038
2039+ final boolean roomAvailable = candidateIsInitialization
2040+ ? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
2041+ : numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS ;
2042+
20322043 if (conflict != null ) {
20332044 if (candidateIsInitialization && !conflict .mSyncOperation .isInitialization ()
20342045 && numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS ) {
@@ -2048,23 +2059,32 @@ private long maybeStartNextSyncLocked() {
20482059 } else {
20492060 continue ;
20502061 }
2051- } else {
2052- final boolean roomAvailable = candidateIsInitialization
2053- ? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS
2054- : numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS ;
2055- if (roomAvailable ) {
2056- // dispatch candidate
2057- } else if (longRunning != null
2058- && (candidateIsInitialization
2059- == longRunning .mSyncOperation .isInitialization ())) {
2060- toReschedule = longRunning ;
2061- if (Log .isLoggable (TAG , Log .VERBOSE )) {
2062- Log .v (TAG , "canceling and rescheduling sync since it ran roo long, "
2063- + longRunning );
2064- }
2065- } else {
2066- continue ;
2062+ } else if (roomAvailable ) {
2063+ // dispatch candidate
2064+ } else if (candidate .isExpedited () && oldestNonExpeditedRegular != null
2065+ && !candidateIsInitialization ) {
2066+ // We found an active, non-expedited regular sync. We also know that the
2067+ // candidate doesn't conflict with this active sync since conflict
2068+ // is null. Reschedule the active sync and start the candidate.
2069+ toReschedule = oldestNonExpeditedRegular ;
2070+ if (Log .isLoggable (TAG , Log .VERBOSE )) {
2071+ Log .v (TAG , "canceling and rescheduling sync since an expedited is ready to run, "
2072+ + oldestNonExpeditedRegular );
20672073 }
2074+ } else if (longRunning != null
2075+ && (candidateIsInitialization
2076+ == longRunning .mSyncOperation .isInitialization ())) {
2077+ // We found an active, long-running sync. Reschedule the active
2078+ // sync and start the candidate.
2079+ toReschedule = longRunning ;
2080+ if (Log .isLoggable (TAG , Log .VERBOSE )) {
2081+ Log .v (TAG , "canceling and rescheduling sync since it ran roo long, "
2082+ + longRunning );
2083+ }
2084+ } else {
2085+ // we were unable to find or make space to run this candidate, go on to
2086+ // the next one
2087+ continue ;
20682088 }
20692089
20702090 if (toReschedule != null ) {
@@ -2516,7 +2536,7 @@ public long insertStartSyncEvent(SyncOperation syncOperation) {
25162536
25172537 return mSyncStorageEngine .insertStartSyncEvent (
25182538 syncOperation .account , syncOperation .userId , syncOperation .authority ,
2519- now , source );
2539+ now , source , syncOperation . isInitialization () );
25202540 }
25212541
25222542 public void stopSyncEvent (long rowId , SyncOperation syncOperation , String resultMessage ,
0 commit comments