1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.telephony;
import android.annotation.BytesLong;
import android.annotation.CurrentTimeMillisLong;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.telephony.Annotation.NetworkType;
import android.util.Range;
import android.util.RecurrenceRule;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.Period;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
/**
* Description of a billing relationship plan between a carrier and a specific
* subscriber. This information is used to present more useful UI to users, such
* as explaining how much mobile data they have remaining, and what will happen
* when they run out.
*
* If specifying network types, the developer must supply at least one plan
* that applies to all network types (default), and all additional plans
* may not include a particular network type more than once.
* This is enforced by {@link SubscriptionManager} when setting the plans.
*
* Plan selection will prefer plans that have specific network types defined
* over plans that apply to all network types.
*
* @see SubscriptionManager#setSubscriptionPlans(int, java.util.List)
* @see SubscriptionManager#getSubscriptionPlans(int)
*/
public final class SubscriptionPlan implements Parcelable {
/** {@hide} */
@IntDef(prefix = "LIMIT_BEHAVIOR_", value = {
LIMIT_BEHAVIOR_UNKNOWN,
LIMIT_BEHAVIOR_DISABLED,
LIMIT_BEHAVIOR_BILLED,
LIMIT_BEHAVIOR_THROTTLED,
})
@Retention(RetentionPolicy.SOURCE)
public @interface LimitBehavior {}
/** When a resource limit is hit, the behavior is unknown. */
public static final int LIMIT_BEHAVIOR_UNKNOWN = -1;
/** When a resource limit is hit, access is disabled. */
public static final int LIMIT_BEHAVIOR_DISABLED = 0;
/** When a resource limit is hit, the user is billed automatically. */
public static final int LIMIT_BEHAVIOR_BILLED = 1;
/** When a resource limit is hit, access is throttled to a slower rate. */
public static final int LIMIT_BEHAVIOR_THROTTLED = 2;
/** Value indicating a number of bytes is unknown. */
public static final long BYTES_UNKNOWN = -1;
/** Value indicating a number of bytes is unlimited. */
public static final long BYTES_UNLIMITED = Long.MAX_VALUE;
/** Value indicating a timestamp is unknown. */
public static final long TIME_UNKNOWN = -1;
private final RecurrenceRule cycleRule;
private CharSequence title;
private CharSequence summary;
private long dataLimitBytes = BYTES_UNKNOWN;
private int dataLimitBehavior = LIMIT_BEHAVIOR_UNKNOWN;
private long dataUsageBytes = BYTES_UNKNOWN;
private long dataUsageTime = TIME_UNKNOWN;
private @NetworkType int[] networkTypes;
private SubscriptionPlan(RecurrenceRule cycleRule) {
this.cycleRule = Preconditions.checkNotNull(cycleRule);
this.networkTypes = Arrays.copyOf(TelephonyManager.getAllNetworkTypes(),
TelephonyManager.getAllNetworkTypes().length);
}
private SubscriptionPlan(Parcel source) {
cycleRule = source.readParcelable(null, android.util.RecurrenceRule.class);
title = source.readCharSequence();
summary = source.readCharSequence();
dataLimitBytes = source.readLong();
dataLimitBehavior = source.readInt();
dataUsageBytes = source.readLong();
dataUsageTime = source.readLong();
networkTypes = source.createIntArray();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(cycleRule, flags);
dest.writeCharSequence(title);
dest.writeCharSequence(summary);
dest.writeLong(dataLimitBytes);
dest.writeInt(dataLimitBehavior);
dest.writeLong(dataUsageBytes);
dest.writeLong(dataUsageTime);
dest.writeIntArray(networkTypes);
}
@Override
public String toString() {
return new StringBuilder("SubscriptionPlan{")
.append("cycleRule=").append(cycleRule)
.append(" title=").append(title)
.append(" summary=").append(summary)
.append(" dataLimitBytes=").append(dataLimitBytes)
.append(" dataLimitBehavior=").append(dataLimitBehavior)
.append(" dataUsageBytes=").append(dataUsageBytes)
.append(" dataUsageTime=").append(dataUsageTime)
.append(" networkTypes=").append(Arrays.toString(networkTypes))
.append("}").toString();
}
@Override
public int hashCode() {
return Objects.hash(cycleRule, title, summary, dataLimitBytes, dataLimitBehavior,
dataUsageBytes, dataUsageTime, Arrays.hashCode(networkTypes));
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof SubscriptionPlan) {
final SubscriptionPlan other = (SubscriptionPlan) obj;
return Objects.equals(cycleRule, other.cycleRule)
&& Objects.equals(title, other.title)
&& Objects.equals(summary, other.summary)
&& dataLimitBytes == other.dataLimitBytes
&& dataLimitBehavior == other.dataLimitBehavior
&& dataUsageBytes == other.dataUsageBytes
&& dataUsageTime == other.dataUsageTime
&& Arrays.equals(networkTypes, other.networkTypes);
}
return false;
}
public static final @android.annotation.NonNull Parcelable.Creator<SubscriptionPlan> CREATOR = new Parcelable.Creator<SubscriptionPlan>() {
@Override
public SubscriptionPlan createFromParcel(Parcel source) {
return new SubscriptionPlan(source);
}
@Override
public SubscriptionPlan[] newArray(int size) {
return new SubscriptionPlan[size];
}
};
/** {@hide} */
public @NonNull RecurrenceRule getCycleRule() {
return cycleRule;
}
/** Return the short title of this plan. */
public @Nullable CharSequence getTitle() {
return title;
}
/** Return the short summary of this plan. */
public @Nullable CharSequence getSummary() {
return summary;
}
/**
* Return the usage threshold at which data access changes according to
* {@link #getDataLimitBehavior()}.
*/
public @BytesLong long getDataLimitBytes() {
return dataLimitBytes;
}
/**
* Return the behavior of data access when usage reaches
* {@link #getDataLimitBytes()}.
*/
public @LimitBehavior int getDataLimitBehavior() {
return dataLimitBehavior;
}
/**
* Return a snapshot of currently known mobile data usage at
* {@link #getDataUsageTime()}.
*/
public @BytesLong long getDataUsageBytes() {
return dataUsageBytes;
}
/**
* Return the time at which {@link #getDataUsageBytes()} was valid.
*/
public @CurrentTimeMillisLong long getDataUsageTime() {
return dataUsageTime;
}
/**
* Return an array containing all {@link NetworkType}s this SubscriptionPlan applies to.
* @see TelephonyManager for network types values
*/
public @NonNull @NetworkType int[] getNetworkTypes() {
return Arrays.copyOf(networkTypes, networkTypes.length);
}
/**
* Return an iterator that will return all valid data usage cycles based on
* any recurrence rules. The iterator starts from the currently active cycle
* and walks backwards through time.
*/
public Iterator<Range<ZonedDateTime>> cycleIterator() {
return cycleRule.cycleIterator();
}
/**
* Builder for a {@link SubscriptionPlan}.
*/
public static class Builder {
private final SubscriptionPlan plan;
/** {@hide} */
public Builder(ZonedDateTime start, ZonedDateTime end, Period period) {
plan = new SubscriptionPlan(new RecurrenceRule(start, end, period));
}
/**
* Start defining a {@link SubscriptionPlan} that covers a very specific
* window of time, and never automatically recurs.
*
* @param start The exact time at which the plan starts.
* @param end The exact time at which the plan ends.
*/
public static Builder createNonrecurring(ZonedDateTime start, ZonedDateTime end) {
if (!end.isAfter(start)) {
throw new IllegalArgumentException(
"End " + end + " isn't after start " + start);
}
return new Builder(start, end, null);
}
/**
* Start defining a {@link SubscriptionPlan} that starts at a specific
* time, and automatically recurs after each specific period of time,
* repeating indefinitely.
* <p>
* When the given period is set to exactly one month, the plan will
* always recur on the day of the month defined by
* {@link ZonedDateTime#getDayOfMonth()}. When a particular month ends
* before this day, the plan will recur on the last possible instant of
* that month.
*
* @param start The exact time at which the plan starts.
* @param period The period after which the plan automatically recurs.
*/
public static Builder createRecurring(ZonedDateTime start, Period period) {
if (period.isZero() || period.isNegative()) {
throw new IllegalArgumentException("Period " + period + " must be positive");
}
return new Builder(start, null, period);
}
/** {@hide} */
@SystemApi
@Deprecated
public static Builder createRecurringMonthly(ZonedDateTime start) {
return new Builder(start, null, Period.ofMonths(1));
}
/** {@hide} */
@SystemApi
@Deprecated
public static Builder createRecurringWeekly(ZonedDateTime start) {
return new Builder(start, null, Period.ofDays(7));
}
/** {@hide} */
@SystemApi
@Deprecated
public static Builder createRecurringDaily(ZonedDateTime start) {
return new Builder(start, null, Period.ofDays(1));
}
public SubscriptionPlan build() {
return plan;
}
/** Set the short title of this plan. */
public Builder setTitle(@Nullable CharSequence title) {
plan.title = title;
return this;
}
/** Set the short summary of this plan. */
public Builder setSummary(@Nullable CharSequence summary) {
plan.summary = summary;
return this;
}
/**
* Set the usage threshold at which data access changes.
*
* @param dataLimitBytes the usage threshold at which data access
* changes
* @param dataLimitBehavior the behavior of data access when usage
* reaches the threshold
*/
public Builder setDataLimit(@BytesLong long dataLimitBytes,
@LimitBehavior int dataLimitBehavior) {
if (dataLimitBytes < 0) {
throw new IllegalArgumentException("Limit bytes must be positive");
}
if (dataLimitBehavior < 0) {
throw new IllegalArgumentException("Limit behavior must be defined");
}
plan.dataLimitBytes = dataLimitBytes;
plan.dataLimitBehavior = dataLimitBehavior;
return this;
}
/**
* Set a snapshot of currently known mobile data usage.
*
* @param dataUsageBytes the currently known mobile data usage
* @param dataUsageTime the time at which this snapshot was valid
*/
public Builder setDataUsage(@BytesLong long dataUsageBytes,
@CurrentTimeMillisLong long dataUsageTime) {
if (dataUsageBytes < 0) {
throw new IllegalArgumentException("Usage bytes must be positive");
}
if (dataUsageTime < 0) {
throw new IllegalArgumentException("Usage time must be positive");
}
plan.dataUsageBytes = dataUsageBytes;
plan.dataUsageTime = dataUsageTime;
return this;
}
/**
* Set the network types this SubscriptionPlan applies to. By default the plan will apply
* to all network types. An empty array means this plan applies to no network types.
*
* @param networkTypes an array of all {@link NetworkType}s that apply to this plan.
* @see TelephonyManager for network type values
*/
public @NonNull Builder setNetworkTypes(@NonNull @NetworkType int[] networkTypes) {
plan.networkTypes = Arrays.copyOf(networkTypes, networkTypes.length);
return this;
}
/**
* Reset any network types that were set with {@link #setNetworkTypes(int[])}.
* This will make the SubscriptionPlan apply to all network types.
*/
public @NonNull Builder resetNetworkTypes() {
plan.networkTypes = Arrays.copyOf(TelephonyManager.getAllNetworkTypes(),
TelephonyManager.getAllNetworkTypes().length);
return this;
}
}
}
|