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; 22 import android.os.Parcelable; 23 24 import java.lang.IllegalArgumentException; 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 UidRange(int startUid, int stopUid)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 createForUser(int userId)43 public static UidRange createForUser(int userId) { 44 return new UidRange(userId * PER_USER_RANGE, (userId + 1) * PER_USER_RANGE - 1); 45 } 46 getStartUser()47 public int getStartUser() { 48 return start / PER_USER_RANGE; 49 } 50 contains(int uid)51 public boolean contains(int uid) { 52 return start <= uid && uid <= stop; 53 } 54 55 /** 56 * @return {@code true} if this range contains every UID contained by the {@param other} range. 57 */ containsRange(UidRange other)58 public boolean containsRange(UidRange other) { 59 return start <= other.start && other.stop <= stop; 60 } 61 62 @Override hashCode()63 public int hashCode() { 64 int result = 17; 65 result = 31 * result + start; 66 result = 31 * result + stop; 67 return result; 68 } 69 70 @Override equals(Object o)71 public boolean equals(Object o) { 72 if (this == o) { 73 return true; 74 } 75 if (o instanceof UidRange) { 76 UidRange other = (UidRange) o; 77 return start == other.start && stop == other.stop; 78 } 79 return false; 80 } 81 82 @Override toString()83 public String toString() { 84 return start + "-" + stop; 85 } 86 87 // implement the Parcelable interface 88 @Override describeContents()89 public int describeContents() { 90 return 0; 91 } 92 93 @Override writeToParcel(Parcel dest, int flags)94 public void writeToParcel(Parcel dest, int flags) { 95 dest.writeInt(start); 96 dest.writeInt(stop); 97 } 98 99 public static final Creator<UidRange> CREATOR = 100 new Creator<UidRange>() { 101 @Override 102 public UidRange createFromParcel(Parcel in) { 103 int start = in.readInt(); 104 int stop = in.readInt(); 105 106 return new UidRange(start, stop); 107 } 108 @Override 109 public UidRange[] newArray(int size) { 110 return new UidRange[size]; 111 } 112 }; 113 } 114