Home | History | Annotate | Download | only in net
      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 /**
     25  * An inclusive range of UIDs.
     26  *
     27  * @hide
     28  */
     29 public final class UidRange implements Parcelable {
     30     public final int start;
     31     public final int stop;
     32 
     33     public UidRange(int startUid, int stopUid) {
     34         if (startUid < 0) throw new IllegalArgumentException("Invalid start UID.");
     35         if (stopUid < 0) throw new IllegalArgumentException("Invalid stop UID.");
     36         if (startUid > stopUid) throw new IllegalArgumentException("Invalid UID range.");
     37         start = startUid;
     38         stop  = stopUid;
     39     }
     40 
     41     public static UidRange createForUser(int userId) {
     42         return new UidRange(userId * PER_USER_RANGE, (userId + 1) * PER_USER_RANGE - 1);
     43     }
     44 
     45     public int getStartUser() {
     46         return start / PER_USER_RANGE;
     47     }
     48 
     49     public boolean contains(int uid) {
     50         return start <= uid && uid <= stop;
     51     }
     52 
     53     /**
     54      * Returns the count of UIDs in this range.
     55      */
     56     public int count() {
     57         return 1 + stop - start;
     58     }
     59 
     60     /**
     61      * @return {@code true} if this range contains every UID contained by the {@param other} range.
     62      */
     63     public boolean containsRange(UidRange other) {
     64         return start <= other.start && other.stop <= stop;
     65     }
     66 
     67     @Override
     68     public int hashCode() {
     69         int result = 17;
     70         result = 31 * result + start;
     71         result = 31 * result + stop;
     72         return result;
     73     }
     74 
     75     @Override
     76     public boolean equals(Object o) {
     77         if (this == o) {
     78             return true;
     79         }
     80         if (o instanceof UidRange) {
     81             UidRange other = (UidRange) o;
     82             return start == other.start && stop == other.stop;
     83         }
     84         return false;
     85     }
     86 
     87     @Override
     88     public String toString() {
     89         return start + "-" + stop;
     90     }
     91 
     92     // implement the Parcelable interface
     93     @Override
     94     public int describeContents() {
     95         return 0;
     96     }
     97 
     98     @Override
     99     public void writeToParcel(Parcel dest, int flags) {
    100         dest.writeInt(start);
    101         dest.writeInt(stop);
    102     }
    103 
    104     public static final Creator<UidRange> CREATOR =
    105         new Creator<UidRange>() {
    106             @Override
    107             public UidRange createFromParcel(Parcel in) {
    108                 int start = in.readInt();
    109                 int stop = in.readInt();
    110 
    111                 return new UidRange(start, stop);
    112             }
    113             @Override
    114             public UidRange[] newArray(int size) {
    115                 return new UidRange[size];
    116             }
    117     };
    118 }
    119