blob: 793c82dc68e104d7b6ff1c65e6b41bc4ab88b787 [file] [log] [blame]
Paul Jensen6bc2c2c2014-05-07 15:27:40 -04001/*
2 * Copyright (C) 2014 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.net;
18
19import static android.os.UserHandle.PER_USER_RANGE;
20
21import android.os.Parcel;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040022
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040023/**
24 * An inclusive range of UIDs.
25 *
26 * @hide
27 */
Luke Huang77017912018-10-19 15:57:05 +090028public final class UidRange extends UidRangeParcel {
29 private UidRange() {}
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040030 public UidRange(int startUid, int stopUid) {
31 if (startUid < 0) throw new IllegalArgumentException("Invalid start UID.");
32 if (stopUid < 0) throw new IllegalArgumentException("Invalid stop UID.");
33 if (startUid > stopUid) throw new IllegalArgumentException("Invalid UID range.");
34 start = startUid;
35 stop = stopUid;
36 }
37
38 public static UidRange createForUser(int userId) {
39 return new UidRange(userId * PER_USER_RANGE, (userId + 1) * PER_USER_RANGE - 1);
40 }
41
42 public int getStartUser() {
43 return start / PER_USER_RANGE;
44 }
45
Robin Lee4d03abc2016-05-09 12:32:27 +010046 public boolean contains(int uid) {
47 return start <= uid && uid <= stop;
48 }
49
50 /**
Chalard Jean07ace0f2018-02-26 19:00:45 +090051 * Returns the count of UIDs in this range.
52 */
53 public int count() {
54 return 1 + stop - start;
55 }
56
57 /**
Robin Lee4d03abc2016-05-09 12:32:27 +010058 * @return {@code true} if this range contains every UID contained by the {@param other} range.
59 */
60 public boolean containsRange(UidRange other) {
61 return start <= other.start && other.stop <= stop;
62 }
63
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040064 @Override
65 public int hashCode() {
66 int result = 17;
67 result = 31 * result + start;
68 result = 31 * result + stop;
69 return result;
70 }
71
72 @Override
73 public boolean equals(Object o) {
74 if (this == o) {
75 return true;
76 }
77 if (o instanceof UidRange) {
78 UidRange other = (UidRange) o;
79 return start == other.start && stop == other.stop;
80 }
81 return false;
82 }
83
84 @Override
85 public String toString() {
86 return start + "-" + stop;
87 }
88
Luke Huang77017912018-10-19 15:57:05 +090089 /**
90 * DO NOT override "writeToParcel" and "readFromParcel" in this class.
91 * The parceling code is autogenerated by the superclass.
92 */
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040093
94 public static final Creator<UidRange> CREATOR =
95 new Creator<UidRange>() {
96 @Override
97 public UidRange createFromParcel(Parcel in) {
Luke Huang77017912018-10-19 15:57:05 +090098 UidRange obj = new UidRange();
99 obj.readFromParcel(in);
100 return obj;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400101 }
102 @Override
103 public UidRange[] newArray(int size) {
104 return new UidRange[size];
105 }
106 };
107}