blob: 2e586b39b5be80c372ccc11c957816824074c005 [file] [log] [blame]
Paul Jensen6bc2c2c2014-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
19import static android.os.UserHandle.PER_USER_RANGE;
20
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.lang.IllegalArgumentException;
25
26/**
27 * An inclusive range of UIDs.
28 *
29 * @hide
30 */
31public 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 public static UidRange createForUser(int userId) {
44 return new UidRange(userId * PER_USER_RANGE, (userId + 1) * PER_USER_RANGE - 1);
45 }
46
47 public int getStartUser() {
48 return start / PER_USER_RANGE;
49 }
50
51 @Override
52 public int hashCode() {
53 int result = 17;
54 result = 31 * result + start;
55 result = 31 * result + stop;
56 return result;
57 }
58
59 @Override
60 public boolean equals(Object o) {
61 if (this == o) {
62 return true;
63 }
64 if (o instanceof UidRange) {
65 UidRange other = (UidRange) o;
66 return start == other.start && stop == other.stop;
67 }
68 return false;
69 }
70
71 @Override
72 public String toString() {
73 return start + "-" + stop;
74 }
75
76 // implement the Parcelable interface
77 @Override
78 public int describeContents() {
79 return 0;
80 }
81
82 @Override
83 public void writeToParcel(Parcel dest, int flags) {
84 dest.writeInt(start);
85 dest.writeInt(stop);
86 }
87
88 public static final Creator<UidRange> CREATOR =
89 new Creator<UidRange>() {
90 @Override
91 public UidRange createFromParcel(Parcel in) {
92 int start = in.readInt();
93 int stop = in.readInt();
94
95 return new UidRange(start, stop);
96 }
97 @Override
98 public UidRange[] newArray(int size) {
99 return new UidRange[size];
100 }
101 };
102}