Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Roman Kalukiewicz | 384a8c6 | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 20 | import android.os.Parcel; |
Lorenzo Colitti | 86b51bb | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 21 | import android.os.Parcelable; |
Lorenzo Colitti | 8876a3d | 2021-02-16 15:42:21 +0900 | [diff] [blame] | 22 | import android.os.UserHandle; |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 23 | |
Lorenzo Colitti | bad9d91 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 26 | /** |
| 27 | * An inclusive range of UIDs. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
Lorenzo Colitti | 86b51bb | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 31 | public final class UidRange implements Parcelable { |
| 32 | public final int start; |
| 33 | public final int stop; |
| 34 | |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 35 | 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 Colitti | 8876a3d | 2021-02-16 15:42:21 +0900 | [diff] [blame] | 43 | /** 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 Colitti | bad9d91 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 51 | /** Returns the smallest user Id which is contained in this UidRange */ |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 52 | public int getStartUser() { |
lucaslin | 19a48b8 | 2021-02-19 18:21:02 +0800 | [diff] [blame] | 53 | return UserHandle.getUserHandleForUid(start).getIdentifier(); |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Lorenzo Colitti | bad9d91 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 56 | /** Returns the largest user Id which is contained in this UidRange */ |
| 57 | public int getEndUser() { |
lucaslin | 19a48b8 | 2021-02-19 18:21:02 +0800 | [diff] [blame] | 58 | return UserHandle.getUserHandleForUid(stop).getIdentifier(); |
Lorenzo Colitti | bad9d91 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Remi NGUYEN VAN | b2eabd4 | 2021-03-02 12:12:49 +0900 | [diff] [blame^] | 61 | /** Returns whether the UidRange contains the specified UID. */ |
Robin Lee | 722ee0b | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 62 | public boolean contains(int uid) { |
| 63 | return start <= uid && uid <= stop; |
| 64 | } |
| 65 | |
| 66 | /** |
Chalard Jean | 4409dfa | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 67 | * Returns the count of UIDs in this range. |
| 68 | */ |
| 69 | public int count() { |
| 70 | return 1 + stop - start; |
| 71 | } |
| 72 | |
| 73 | /** |
Remi NGUYEN VAN | b2eabd4 | 2021-03-02 12:12:49 +0900 | [diff] [blame^] | 74 | * @return {@code true} if this range contains every UID contained by the {@code other} range. |
Robin Lee | 722ee0b | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 75 | */ |
| 76 | public boolean containsRange(UidRange other) { |
| 77 | return start <= other.start && other.stop <= stop; |
| 78 | } |
| 79 | |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 80 | @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 Kalukiewicz | 384a8c6 | 2020-10-14 15:59:06 -0700 | [diff] [blame] | 89 | public boolean equals(@Nullable Object o) { |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 90 | 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 Colitti | 86b51bb | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 105 | // 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 Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 118 | |
Jeff Sharkey | f852528 | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 119 | public static final @android.annotation.NonNull Creator<UidRange> CREATOR = |
Remi NGUYEN VAN | b2eabd4 | 2021-03-02 12:12:49 +0900 | [diff] [blame^] | 120 | new Creator<UidRange>() { |
| 121 | @Override |
| 122 | public UidRange createFromParcel(Parcel in) { |
| 123 | int start = in.readInt(); |
| 124 | int stop = in.readInt(); |
Lorenzo Colitti | 86b51bb | 2019-03-18 23:50:34 +0900 | [diff] [blame] | 125 | |
Remi NGUYEN VAN | b2eabd4 | 2021-03-02 12:12:49 +0900 | [diff] [blame^] | 126 | return new UidRange(start, stop); |
| 127 | } |
| 128 | @Override |
| 129 | public UidRange[] newArray(int size) { |
| 130 | return new UidRange[size]; |
| 131 | } |
Paul Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 132 | }; |
Lorenzo Colitti | bad9d91 | 2019-04-12 10:48:06 +0000 | [diff] [blame] | 133 | |
| 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 Jensen | 8b5fc62 | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 152 | } |