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