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 17 package android.content; 18 19 import android.accounts.Account; 20 import android.annotation.Nullable; 21 import android.os.Bundle; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.Objects; 26 27 /** 28 * Value type that contains information about a periodic sync. 29 */ 30 public class PeriodicSync implements Parcelable { 31 /** The account to be synced. Can be null. */ 32 public final Account account; 33 /** The authority of the sync. Can be null. */ 34 public final String authority; 35 /** Any extras that parameters that are to be passed to the sync adapter. */ 36 public final Bundle extras; 37 /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */ 38 public final long period; 39 /** 40 * How much flexibility can be taken in scheduling the sync, in seconds. 41 * {@hide} 42 */ 43 public final long flexTime; 44 45 /** 46 * Creates a new PeriodicSync, copying the Bundle. This constructor is no longer used. 47 */ PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds)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 // Old API uses default flex time. No-one should be using this ctor anyway. 58 this.flexTime = 0L; 59 } 60 61 /** 62 * Create a copy of a periodic sync. 63 * {@hide} 64 */ PeriodicSync(PeriodicSync other)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 * A PeriodicSync for a sync with a specified provider. 75 * {@hide} 76 */ PeriodicSync(Account account, String authority, Bundle extras, long period, long flexTime)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 PeriodicSync(Parcel in)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 describeContents()95 public int describeContents() { 96 return 0; 97 } 98 99 @Override writeToParcel(Parcel dest, int flags)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 @android.annotation.NonNull 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 equals(@ullable Object o)121 public boolean equals(@Nullable 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 */ syncExtrasEquals(Bundle b1, Bundle b2)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 // Null check. According to ContentResolver#validateSyncExtrasBundle null-valued keys 151 // are allowed in the bundle. 152 if (!Objects.equals(b1.get(key), b2.get(key))) { 153 return false; 154 } 155 } 156 return true; 157 } 158 159 @Override toString()160 public String toString() { 161 return "account: " + account + 162 ", authority: " + authority + 163 ". period: " + period + "s " + 164 ", flex: " + flexTime; 165 } 166 } 167