blob: 26518d32edcbaaa55dc91f6bfb8188865e39cb5e [file] [log] [blame]
Paul Jensen8b5fc622014-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
Roman Kalukiewicz384a8c62020-10-14 15:59:06 -070019import android.annotation.Nullable;
Paul Jensen8b5fc622014-05-07 15:27:40 -040020import android.os.Parcel;
Lorenzo Colitti86b51bb2019-03-18 23:50:34 +090021import android.os.Parcelable;
Lorenzo Colitti8876a3d2021-02-16 15:42:21 +090022import android.os.UserHandle;
Paul Jensen8b5fc622014-05-07 15:27:40 -040023
Lorenzo Colittibad9d912019-04-12 10:48:06 +000024import java.util.Collection;
25
Paul Jensen8b5fc622014-05-07 15:27:40 -040026/**
27 * An inclusive range of UIDs.
28 *
29 * @hide
30 */
Lorenzo Colitti86b51bb2019-03-18 23:50:34 +090031public final class UidRange implements Parcelable {
32 public final int start;
33 public final int stop;
34
Paul Jensen8b5fc622014-05-07 15:27:40 -040035 public UidRange(int startUid, int stopUid) {
36 if (startUid < 0) throw new IllegalArgumentException("Invalid start UID.");
37 if (stopUid < 0) throw new IllegalArgumentException("Invalid stop UID.");
38 if (startUid > stopUid) throw new IllegalArgumentException("Invalid UID range.");
39 start = startUid;
40 stop = stopUid;
41 }
42
Lorenzo Colitti8876a3d2021-02-16 15:42:21 +090043 /** Creates a UidRange for the specified user. */
44 public static UidRange createForUser(UserHandle user) {
45 final UserHandle nextUser = UserHandle.of(user.getIdentifier() + 1);
46 final int start = UserHandle.getUid(user, 0 /* appId */);
47 final int end = UserHandle.getUid(nextUser, 0) - 1;
48 return new UidRange(start, end);
49 }
50
Lorenzo Colittibad9d912019-04-12 10:48:06 +000051 /** Returns the smallest user Id which is contained in this UidRange */
Paul Jensen8b5fc622014-05-07 15:27:40 -040052 public int getStartUser() {
lucaslin19a48b82021-02-19 18:21:02 +080053 return UserHandle.getUserHandleForUid(start).getIdentifier();
Paul Jensen8b5fc622014-05-07 15:27:40 -040054 }
55
Lorenzo Colittibad9d912019-04-12 10:48:06 +000056 /** Returns the largest user Id which is contained in this UidRange */
57 public int getEndUser() {
lucaslin19a48b82021-02-19 18:21:02 +080058 return UserHandle.getUserHandleForUid(stop).getIdentifier();
Lorenzo Colittibad9d912019-04-12 10:48:06 +000059 }
60
Remi NGUYEN VANb2eabd42021-03-02 12:12:49 +090061 /** Returns whether the UidRange contains the specified UID. */
Robin Lee722ee0b2016-05-09 12:32:27 +010062 public boolean contains(int uid) {
63 return start <= uid && uid <= stop;
64 }
65
66 /**
Chalard Jean4409dfa2018-02-26 19:00:45 +090067 * Returns the count of UIDs in this range.
68 */
69 public int count() {
70 return 1 + stop - start;
71 }
72
73 /**
Remi NGUYEN VANb2eabd42021-03-02 12:12:49 +090074 * @return {@code true} if this range contains every UID contained by the {@code other} range.
Robin Lee722ee0b2016-05-09 12:32:27 +010075 */
76 public boolean containsRange(UidRange other) {
77 return start <= other.start && other.stop <= stop;
78 }
79
Paul Jensen8b5fc622014-05-07 15:27:40 -040080 @Override
81 public int hashCode() {
82 int result = 17;
83 result = 31 * result + start;
84 result = 31 * result + stop;
85 return result;
86 }
87
88 @Override
Roman Kalukiewicz384a8c62020-10-14 15:59:06 -070089 public boolean equals(@Nullable Object o) {
Paul Jensen8b5fc622014-05-07 15:27:40 -040090 if (this == o) {
91 return true;
92 }
93 if (o instanceof UidRange) {
94 UidRange other = (UidRange) o;
95 return start == other.start && stop == other.stop;
96 }
97 return false;
98 }
99
100 @Override
101 public String toString() {
102 return start + "-" + stop;
103 }
104
Lorenzo Colitti86b51bb2019-03-18 23:50:34 +0900105 // Implement the Parcelable interface
106 // TODO: Consider making this class no longer parcelable, since all users are likely in the
107 // system server.
108 @Override
109 public int describeContents() {
110 return 0;
111 }
112
113 @Override
114 public void writeToParcel(Parcel dest, int flags) {
115 dest.writeInt(start);
116 dest.writeInt(stop);
117 }
Paul Jensen8b5fc622014-05-07 15:27:40 -0400118
Jeff Sharkeyf8525282019-02-28 12:06:45 -0700119 public static final @android.annotation.NonNull Creator<UidRange> CREATOR =
Remi NGUYEN VANb2eabd42021-03-02 12:12:49 +0900120 new Creator<UidRange>() {
121 @Override
122 public UidRange createFromParcel(Parcel in) {
123 int start = in.readInt();
124 int stop = in.readInt();
Lorenzo Colitti86b51bb2019-03-18 23:50:34 +0900125
Remi NGUYEN VANb2eabd42021-03-02 12:12:49 +0900126 return new UidRange(start, stop);
127 }
128 @Override
129 public UidRange[] newArray(int size) {
130 return new UidRange[size];
131 }
Paul Jensen8b5fc622014-05-07 15:27:40 -0400132 };
Lorenzo Colittibad9d912019-04-12 10:48:06 +0000133
134 /**
135 * Returns whether any of the UidRange in the collection contains the specified uid
136 *
137 * @param ranges The collection of UidRange to check
138 * @param uid the uid in question
139 * @return {@code true} if the uid is contained within the ranges, {@code false} otherwise
140 *
141 * @see UidRange#contains(int)
142 */
143 public static boolean containsUid(Collection<UidRange> ranges, int uid) {
144 if (ranges == null) return false;
145 for (UidRange range : ranges) {
146 if (range.contains(uid)) {
147 return true;
148 }
149 }
150 return false;
151 }
Paul Jensen8b5fc622014-05-07 15:27:40 -0400152}