SyncOperation.java revision 307da1a46b4c9b711bafe8fbaaa6b98e8868c18e
1package android.content;
2
3import android.accounts.Account;
4import android.os.Bundle;
5import android.os.SystemClock;
6
7/**
8 * Value type that represents a sync operation.
9 * @hide
10 */
11public class SyncOperation implements Comparable {
12    public final Account account;
13    public int syncSource;
14    public String authority;
15    public Bundle extras;
16    public final String key;
17    public long earliestRunTime;
18    public boolean expedited;
19    public SyncStorageEngine.PendingOperation pendingOperation;
20
21    public SyncOperation(Account account, int source, String authority, Bundle extras,
22            long delay) {
23        this.account = account;
24        this.syncSource = source;
25        this.authority = authority;
26        this.extras = new Bundle(extras);
27        removeFalseExtra(ContentResolver.SYNC_EXTRAS_UPLOAD);
28        removeFalseExtra(ContentResolver.SYNC_EXTRAS_MANUAL);
29        removeFalseExtra(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS);
30        removeFalseExtra(ContentResolver.SYNC_EXTRAS_EXPEDITED);
31        removeFalseExtra(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS);
32        final long now = SystemClock.elapsedRealtime();
33        if (delay < 0) {
34            this.expedited = true;
35            this.earliestRunTime = now;
36        } else {
37            this.expedited = false;
38            this.earliestRunTime = now + delay;
39        }
40        this.key = toKey();
41    }
42
43    private void removeFalseExtra(String extraName) {
44        if (!extras.getBoolean(extraName, false)) {
45            extras.remove(extraName);
46        }
47    }
48
49    SyncOperation(SyncOperation other) {
50        this.account = other.account;
51        this.syncSource = other.syncSource;
52        this.authority = other.authority;
53        this.extras = new Bundle(other.extras);
54        this.expedited = other.expedited;
55        this.earliestRunTime = SystemClock.elapsedRealtime();
56        this.key = toKey();
57    }
58
59    public String toString() {
60        StringBuilder sb = new StringBuilder();
61        sb.append("authority: ").append(authority);
62        sb.append(" account: ").append(account);
63        sb.append(" extras: ");
64        extrasToStringBuilder(extras, sb);
65        sb.append(" syncSource: ").append(syncSource);
66        sb.append(" when: ").append(earliestRunTime);
67        sb.append(" expedited: ").append(expedited);
68        return sb.toString();
69    }
70
71    private String toKey() {
72        StringBuilder sb = new StringBuilder();
73        sb.append("authority: ").append(authority);
74        sb.append(" account: ").append(account);
75        sb.append(" extras: ");
76        extrasToStringBuilder(extras, sb);
77        return sb.toString();
78    }
79
80    public static void extrasToStringBuilder(Bundle bundle, StringBuilder sb) {
81        sb.append("[");
82        for (String key : bundle.keySet()) {
83            sb.append(key).append("=").append(bundle.get(key)).append(" ");
84        }
85        sb.append("]");
86    }
87
88    public int compareTo(Object o) {
89        SyncOperation other = (SyncOperation)o;
90        if (earliestRunTime == other.earliestRunTime) {
91            return 0;
92        }
93        return (earliestRunTime < other.earliestRunTime) ? -1 : 1;
94    }
95}
96