Paul Jensen | 6bc2c2c | 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 | |
| 19 | import static android.os.UserHandle.PER_USER_RANGE; |
| 20 | |
| 21 | import android.os.Parcel; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 22 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 23 | /** |
| 24 | * An inclusive range of UIDs. |
| 25 | * |
| 26 | * @hide |
| 27 | */ |
Luke Huang | 7701791 | 2018-10-19 15:57:05 +0900 | [diff] [blame] | 28 | public final class UidRange extends UidRangeParcel { |
| 29 | private UidRange() {} |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 30 | 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 Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 46 | public boolean contains(int uid) { |
| 47 | return start <= uid && uid <= stop; |
| 48 | } |
| 49 | |
| 50 | /** |
Chalard Jean | 07ace0f | 2018-02-26 19:00:45 +0900 | [diff] [blame] | 51 | * Returns the count of UIDs in this range. |
| 52 | */ |
| 53 | public int count() { |
| 54 | return 1 + stop - start; |
| 55 | } |
| 56 | |
| 57 | /** |
Robin Lee | 4d03abc | 2016-05-09 12:32:27 +0100 | [diff] [blame] | 58 | * @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 Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 64 | @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 Huang | 7701791 | 2018-10-19 15:57:05 +0900 | [diff] [blame] | 89 | /** |
| 90 | * DO NOT override "writeToParcel" and "readFromParcel" in this class. |
| 91 | * The parceling code is autogenerated by the superclass. |
| 92 | */ |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 93 | |
| 94 | public static final Creator<UidRange> CREATOR = |
| 95 | new Creator<UidRange>() { |
| 96 | @Override |
| 97 | public UidRange createFromParcel(Parcel in) { |
Luke Huang | 7701791 | 2018-10-19 15:57:05 +0900 | [diff] [blame] | 98 | UidRange obj = new UidRange(); |
| 99 | obj.readFromParcel(in); |
| 100 | return obj; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 101 | } |
| 102 | @Override |
| 103 | public UidRange[] newArray(int size) { |
| 104 | return new UidRange[size]; |
| 105 | } |
| 106 | }; |
| 107 | } |