| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.telephony; |
| 18 |
|
| 19 |
import android.annotation.BytesLong; |
| 20 |
import android.annotation.CurrentTimeMillisLong; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.SystemApi; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.telephony.Annotation.NetworkType; |
| 28 |
import android.util.Range; |
| 29 |
import android.util.RecurrenceRule; |
| 30 |
|
| 31 |
import com.android.internal.util.Preconditions; |
| 32 |
|
| 33 |
import java.lang.annotation.Retention; |
| 34 |
import java.lang.annotation.RetentionPolicy; |
| 35 |
import java.time.Period; |
| 36 |
import java.time.ZonedDateTime; |
| 37 |
import java.util.Arrays; |
| 38 |
import java.util.Iterator; |
| 39 |
import java.util.Objects; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public final class SubscriptionPlan implements Parcelable { |
| 59 |
|
| 60 |
@IntDef(prefix = "LIMIT_BEHAVIOR_", value = { |
| 61 |
LIMIT_BEHAVIOR_UNKNOWN, |
| 62 |
LIMIT_BEHAVIOR_DISABLED, |
| 63 |
LIMIT_BEHAVIOR_BILLED, |
| 64 |
LIMIT_BEHAVIOR_THROTTLED, |
| 65 |
}) |
| 66 |
@Retention(RetentionPolicy.SOURCE) |
| 67 |
public @interface LimitBehavior {} |
| 68 |
|
| 69 |
|
| 70 |
public static final int LIMIT_BEHAVIOR_UNKNOWN = -1; |
| 71 |
|
| 72 |
public static final int LIMIT_BEHAVIOR_DISABLED = 0; |
| 73 |
|
| 74 |
public static final int LIMIT_BEHAVIOR_BILLED = 1; |
| 75 |
|
| 76 |
public static final int LIMIT_BEHAVIOR_THROTTLED = 2; |
| 77 |
|
| 78 |
|
| 79 |
public static final long BYTES_UNKNOWN = -1; |
| 80 |
|
| 81 |
public static final long BYTES_UNLIMITED = Long.MAX_VALUE; |
| 82 |
|
| 83 |
|
| 84 |
public static final long TIME_UNKNOWN = -1; |
| 85 |
|
| 86 |
private final RecurrenceRule cycleRule; |
| 87 |
private CharSequence title; |
| 88 |
private CharSequence summary; |
| 89 |
private long dataLimitBytes = BYTES_UNKNOWN; |
| 90 |
private int dataLimitBehavior = LIMIT_BEHAVIOR_UNKNOWN; |
| 91 |
private long dataUsageBytes = BYTES_UNKNOWN; |
| 92 |
private long dataUsageTime = TIME_UNKNOWN; |
| 93 |
private @NetworkType int[] networkTypes; |
| 94 |
|
| 95 |
private SubscriptionPlan(RecurrenceRule cycleRule) { |
| 96 |
this.cycleRule = Preconditions.checkNotNull(cycleRule); |
| 97 |
this.networkTypes = Arrays.copyOf(TelephonyManager.getAllNetworkTypes(), |
| 98 |
TelephonyManager.getAllNetworkTypes().length); |
| 99 |
} |
| 100 |
|
| 101 |
private SubscriptionPlan(Parcel source) { |
| 102 |
cycleRule = source.readParcelable(null, android.util.RecurrenceRule.class); |
| 103 |
title = source.readCharSequence(); |
| 104 |
summary = source.readCharSequence(); |
| 105 |
dataLimitBytes = source.readLong(); |
| 106 |
dataLimitBehavior = source.readInt(); |
| 107 |
dataUsageBytes = source.readLong(); |
| 108 |
dataUsageTime = source.readLong(); |
| 109 |
networkTypes = source.createIntArray(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public int describeContents() { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void writeToParcel(Parcel dest, int flags) { |
| 119 |
dest.writeParcelable(cycleRule, flags); |
| 120 |
dest.writeCharSequence(title); |
| 121 |
dest.writeCharSequence(summary); |
| 122 |
dest.writeLong(dataLimitBytes); |
| 123 |
dest.writeInt(dataLimitBehavior); |
| 124 |
dest.writeLong(dataUsageBytes); |
| 125 |
dest.writeLong(dataUsageTime); |
| 126 |
dest.writeIntArray(networkTypes); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public String toString() { |
| 131 |
return new StringBuilder("SubscriptionPlan{") |
| 132 |
.append("cycleRule=").append(cycleRule) |
| 133 |
.append(" title=").append(title) |
| 134 |
.append(" summary=").append(summary) |
| 135 |
.append(" dataLimitBytes=").append(dataLimitBytes) |
| 136 |
.append(" dataLimitBehavior=").append(dataLimitBehavior) |
| 137 |
.append(" dataUsageBytes=").append(dataUsageBytes) |
| 138 |
.append(" dataUsageTime=").append(dataUsageTime) |
| 139 |
.append(" networkTypes=").append(Arrays.toString(networkTypes)) |
| 140 |
.append("}").toString(); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public int hashCode() { |
| 145 |
return Objects.hash(cycleRule, title, summary, dataLimitBytes, dataLimitBehavior, |
| 146 |
dataUsageBytes, dataUsageTime, Arrays.hashCode(networkTypes)); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean equals(@Nullable Object obj) { |
| 151 |
if (obj instanceof SubscriptionPlan) { |
| 152 |
final SubscriptionPlan other = (SubscriptionPlan) obj; |
| 153 |
return Objects.equals(cycleRule, other.cycleRule) |
| 154 |
&& Objects.equals(title, other.title) |
| 155 |
&& Objects.equals(summary, other.summary) |
| 156 |
&& dataLimitBytes == other.dataLimitBytes |
| 157 |
&& dataLimitBehavior == other.dataLimitBehavior |
| 158 |
&& dataUsageBytes == other.dataUsageBytes |
| 159 |
&& dataUsageTime == other.dataUsageTime |
| 160 |
&& Arrays.equals(networkTypes, other.networkTypes); |
| 161 |
} |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
public static final @android.annotation.NonNull Parcelable.Creator<SubscriptionPlan> CREATOR = new Parcelable.Creator<SubscriptionPlan>() { |
| 166 |
@Override |
| 167 |
public SubscriptionPlan createFromParcel(Parcel source) { |
| 168 |
return new SubscriptionPlan(source); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public SubscriptionPlan[] newArray(int size) { |
| 173 |
return new SubscriptionPlan[size]; |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
public @NonNull RecurrenceRule getCycleRule() { |
| 179 |
return cycleRule; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
public @Nullable CharSequence getTitle() { |
| 184 |
return title; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
public @Nullable CharSequence getSummary() { |
| 189 |
return summary; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public @BytesLong long getDataLimitBytes() { |
| 197 |
return dataLimitBytes; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public @LimitBehavior int getDataLimitBehavior() { |
| 205 |
return dataLimitBehavior; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public @BytesLong long getDataUsageBytes() { |
| 213 |
return dataUsageBytes; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public @CurrentTimeMillisLong long getDataUsageTime() { |
| 220 |
return dataUsageTime; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public @NonNull @NetworkType int[] getNetworkTypes() { |
| 228 |
return Arrays.copyOf(networkTypes, networkTypes.length); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public Iterator<Range<ZonedDateTime>> cycleIterator() { |
| 237 |
return cycleRule.cycleIterator(); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public static class Builder { |
| 244 |
private final SubscriptionPlan plan; |
| 245 |
|
| 246 |
|
| 247 |
public Builder(ZonedDateTime start, ZonedDateTime end, Period period) { |
| 248 |
plan = new SubscriptionPlan(new RecurrenceRule(start, end, period)); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public static Builder createNonrecurring(ZonedDateTime start, ZonedDateTime end) { |
| 259 |
if (!end.isAfter(start)) { |
| 260 |
throw new IllegalArgumentException( |
| 261 |
"End " + end + " isn't after start " + start); |
| 262 |
} |
| 263 |
return new Builder(start, end, null); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public static Builder createRecurring(ZonedDateTime start, Period period) { |
| 281 |
if (period.isZero() || period.isNegative()) { |
| 282 |
throw new IllegalArgumentException("Period " + period + " must be positive"); |
| 283 |
} |
| 284 |
return new Builder(start, null, period); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
@SystemApi |
| 289 |
@Deprecated |
| 290 |
public static Builder createRecurringMonthly(ZonedDateTime start) { |
| 291 |
return new Builder(start, null, Period.ofMonths(1)); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
@SystemApi |
| 296 |
@Deprecated |
| 297 |
public static Builder createRecurringWeekly(ZonedDateTime start) { |
| 298 |
return new Builder(start, null, Period.ofDays(7)); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
@SystemApi |
| 303 |
@Deprecated |
| 304 |
public static Builder createRecurringDaily(ZonedDateTime start) { |
| 305 |
return new Builder(start, null, Period.ofDays(1)); |
| 306 |
} |
| 307 |
|
| 308 |
public SubscriptionPlan build() { |
| 309 |
return plan; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public Builder setTitle(@Nullable CharSequence title) { |
| 314 |
plan.title = title; |
| 315 |
return this; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
public Builder setSummary(@Nullable CharSequence summary) { |
| 320 |
plan.summary = summary; |
| 321 |
return this; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public Builder setDataLimit(@BytesLong long dataLimitBytes, |
| 333 |
@LimitBehavior int dataLimitBehavior) { |
| 334 |
if (dataLimitBytes < 0) { |
| 335 |
throw new IllegalArgumentException("Limit bytes must be positive"); |
| 336 |
} |
| 337 |
if (dataLimitBehavior < 0) { |
| 338 |
throw new IllegalArgumentException("Limit behavior must be defined"); |
| 339 |
} |
| 340 |
plan.dataLimitBytes = dataLimitBytes; |
| 341 |
plan.dataLimitBehavior = dataLimitBehavior; |
| 342 |
return this; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public Builder setDataUsage(@BytesLong long dataUsageBytes, |
| 352 |
@CurrentTimeMillisLong long dataUsageTime) { |
| 353 |
if (dataUsageBytes < 0) { |
| 354 |
throw new IllegalArgumentException("Usage bytes must be positive"); |
| 355 |
} |
| 356 |
if (dataUsageTime < 0) { |
| 357 |
throw new IllegalArgumentException("Usage time must be positive"); |
| 358 |
} |
| 359 |
plan.dataUsageBytes = dataUsageBytes; |
| 360 |
plan.dataUsageTime = dataUsageTime; |
| 361 |
return this; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public @NonNull Builder setNetworkTypes(@NonNull @NetworkType int[] networkTypes) { |
| 372 |
plan.networkTypes = Arrays.copyOf(networkTypes, networkTypes.length); |
| 373 |
return this; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public @NonNull Builder resetNetworkTypes() { |
| 381 |
plan.networkTypes = Arrays.copyOf(TelephonyManager.getAllNetworkTypes(), |
| 382 |
TelephonyManager.getAllNetworkTypes().length); |
| 383 |
return this; |
| 384 |
} |
| 385 |
} |
| 386 |
} |