1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content;
18
19import android.os.Parcelable;
20import android.os.Bundle;
21import android.os.Parcel;
22import android.accounts.Account;
23
24/**
25 * Value type that contains information about a periodic sync.
26 */
27public class PeriodicSync implements Parcelable {
28    /** The account to be synced. Can be null. */
29    public final Account account;
30    /** The authority of the sync. Can be null. */
31    public final String authority;
32    /** Any extras that parameters that are to be passed to the sync adapter. */
33    public final Bundle extras;
34    /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */
35    public final long period;
36    /**
37     * {@hide}
38     * How much flexibility can be taken in scheduling the sync, in seconds.
39     */
40    public final long flexTime;
41
42      /**
43       * Creates a new PeriodicSync, copying the Bundle. SM no longer uses this ctor - kept around
44       * becuse it is part of the API.
45       * Note - even calls to the old API will not use this ctor, as
46       * they are given a default flex time.
47       */
48    public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) {
49        this.account = account;
50        this.authority = authority;
51        if (extras == null) {
52            this.extras = new Bundle();
53        } else {
54            this.extras = new Bundle(extras);
55        }
56        this.period = periodInSeconds;
57        // Initialise to a sane value.
58        this.flexTime = 0L;
59    }
60
61    /**
62     * {@hide}
63     * Create a copy of a periodic sync.
64     */
65    public PeriodicSync(PeriodicSync other) {
66        this.account = other.account;
67        this.authority = other.authority;
68        this.extras = new Bundle(other.extras);
69        this.period = other.period;
70        this.flexTime = other.flexTime;
71    }
72
73    /**
74     * {@hide}
75     * A PeriodicSync for a sync with a specified provider.
76     */
77    public PeriodicSync(Account account, String authority, Bundle extras,
78            long period, long flexTime) {
79        this.account = account;
80        this.authority = authority;
81        this.extras = new Bundle(extras);
82        this.period = period;
83        this.flexTime = flexTime;
84    }
85
86    private PeriodicSync(Parcel in) {
87        this.account = in.readParcelable(null);
88        this.authority = in.readString();
89        this.extras = in.readBundle();
90        this.period = in.readLong();
91        this.flexTime = in.readLong();
92    }
93
94    @Override
95    public int describeContents() {
96        return 0;
97    }
98
99    @Override
100    public void writeToParcel(Parcel dest, int flags) {
101        dest.writeParcelable(account, flags);
102        dest.writeString(authority);
103        dest.writeBundle(extras);
104        dest.writeLong(period);
105        dest.writeLong(flexTime);
106    }
107
108    public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() {
109        @Override
110        public PeriodicSync createFromParcel(Parcel source) {
111            return new PeriodicSync(source);
112        }
113
114        @Override
115        public PeriodicSync[] newArray(int size) {
116            return new PeriodicSync[size];
117        }
118    };
119
120    @Override
121    public boolean equals(Object o) {
122        if (o == this) {
123            return true;
124        }
125        if (!(o instanceof PeriodicSync)) {
126            return false;
127        }
128        final PeriodicSync other = (PeriodicSync) o;
129        return account.equals(other.account)
130            && authority.equals(other.authority)
131            && period == other.period
132            && syncExtrasEquals(extras, other.extras);
133    }
134
135    /**
136     * Periodic sync extra comparison function.
137     * {@hide}
138     */
139    public static boolean syncExtrasEquals(Bundle b1, Bundle b2) {
140        if (b1.size() != b2.size()) {
141            return false;
142        }
143        if (b1.isEmpty()) {
144            return true;
145        }
146        for (String key : b1.keySet()) {
147            if (!b2.containsKey(key)) {
148                return false;
149            }
150            if (!b1.get(key).equals(b2.get(key))) {
151                return false;
152            }
153        }
154        return true;
155    }
156
157    @Override
158    public String toString() {
159        return "account: " + account +
160               ", authority: " + authority +
161               ". period: " + period + "s " +
162               ", flex: " + flexTime;
163    }
164}
165