Remi NGUYEN VAN | 5115480 | 2021-03-02 12:12:49 +0900 | [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 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | import android.os.UserHandle; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | |
| 26 | /** |
| 27 | * An inclusive range of UIDs. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
| 31 | public final class UidRange implements Parcelable { |
| 32 | public final int start; |
| 33 | public final int stop; |
| 34 | |
| 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 | |
| 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 | |
| 51 | /** Returns the smallest user Id which is contained in this UidRange */ |
| 52 | public int getStartUser() { |
| 53 | return UserHandle.getUserHandleForUid(start).getIdentifier(); |
| 54 | } |
| 55 | |
| 56 | /** Returns the largest user Id which is contained in this UidRange */ |
| 57 | public int getEndUser() { |
| 58 | return UserHandle.getUserHandleForUid(stop).getIdentifier(); |
| 59 | } |
| 60 | |
| 61 | /** Returns whether the UidRange contains the specified UID. */ |
| 62 | public boolean contains(int uid) { |
| 63 | return start <= uid && uid <= stop; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the count of UIDs in this range. |
| 68 | */ |
| 69 | public int count() { |
| 70 | return 1 + stop - start; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return {@code true} if this range contains every UID contained by the {@code other} range. |
| 75 | */ |
| 76 | public boolean containsRange(UidRange other) { |
| 77 | return start <= other.start && other.stop <= stop; |
| 78 | } |
| 79 | |
| 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 |
| 89 | public boolean equals(@Nullable Object o) { |
| 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 | |
| 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 | } |
| 118 | |
| 119 | public static final @android.annotation.NonNull Creator<UidRange> CREATOR = |
| 120 | new Creator<UidRange>() { |
| 121 | @Override |
| 122 | public UidRange createFromParcel(Parcel in) { |
| 123 | int start = in.readInt(); |
| 124 | int stop = in.readInt(); |
| 125 | |
| 126 | return new UidRange(start, stop); |
| 127 | } |
| 128 | @Override |
| 129 | public UidRange[] newArray(int size) { |
| 130 | return new UidRange[size]; |
| 131 | } |
| 132 | }; |
| 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 | } |
| 152 | } |