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     * How much flexibility can be taken in scheduling the sync, in seconds.
38     * {@hide}
39     */
40    public final long flexTime;
41
42      /**
43       * Creates a new PeriodicSync, copying the Bundle. This constructor is no longer used.
44       */
45    public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) {
46        this.account = account;
47        this.authority = authority;
48        if (extras == null) {
49            this.extras = new Bundle();
50        } else {
51            this.extras = new Bundle(extras);
52        }
53        this.period = periodInSeconds;
54        // Old API uses default flex time. No-one should be using this ctor anyway.
55        this.flexTime = 0L;
56    }
57
58    /**
59     * Create a copy of a periodic sync.
60     * {@hide}
61     */
62    public PeriodicSync(PeriodicSync other) {
63        this.account = other.account;
64        this.authority = other.authority;
65        this.extras = new Bundle(other.extras);
66        this.period = other.period;
67        this.flexTime = other.flexTime;
68    }
69
70    /**
71     * A PeriodicSync for a sync with a specified provider.
72     * {@hide}
73     */
74    public PeriodicSync(Account account, String authority, Bundle extras,
75            long period, long flexTime) {
76        this.account = account;
77        this.authority = authority;
78        this.extras = new Bundle(extras);
79        this.period = period;
80        this.flexTime = flexTime;
81    }
82
83    private PeriodicSync(Parcel in) {
84        this.account = in.readParcelable(null);
85        this.authority = in.readString();
86        this.extras = in.readBundle();
87        this.period = in.readLong();
88        this.flexTime = in.readLong();
89    }
90
91    @Override
92    public int describeContents() {
93        return 0;
94    }
95
96    @Override
97    public void writeToParcel(Parcel dest, int flags) {
98        dest.writeParcelable(account, flags);
99        dest.writeString(authority);
100        dest.writeBundle(extras);
101        dest.writeLong(period);
102        dest.writeLong(flexTime);
103    }
104
105    public static final Creator<PeriodicSync> CREATOR = new Creator<PeriodicSync>() {
106        @Override
107        public PeriodicSync createFromParcel(Parcel source) {
108            return new PeriodicSync(source);
109        }
110
111        @Override
112        public PeriodicSync[] newArray(int size) {
113            return new PeriodicSync[size];
114        }
115    };
116
117    @Override
118    public boolean equals(Object o) {
119        if (o == this) {
120            return true;
121        }
122        if (!(o instanceof PeriodicSync)) {
123            return false;
124        }
125        final PeriodicSync other = (PeriodicSync) o;
126        return account.equals(other.account)
127                && authority.equals(other.authority)
128                && period == other.period
129                && syncExtrasEquals(extras, other.extras);
130    }
131
132    /**
133     * Periodic sync extra comparison function.
134     * {@hide}
135     */
136    public static boolean syncExtrasEquals(Bundle b1, Bundle b2) {
137        if (b1.size() != b2.size()) {
138            return false;
139        }
140        if (b1.isEmpty()) {
141            return true;
142        }
143        for (String key : b1.keySet()) {
144            if (!b2.containsKey(key)) {
145                return false;
146            }
147            if (!b1.get(key).equals(b2.get(key))) {
148                return false;
149            }
150        }
151        return true;
152    }
153
154    @Override
155    public String toString() {
156        return "account: " + account +
157               ", authority: " + authority +
158               ". period: " + period + "s " +
159               ", flex: " + flexTime;
160    }
161}
162