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 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     public static UidRange createForUser(int userId) {
     44         return new UidRange(userId * PER_USER_RANGE, (userId + 1) * PER_USER_RANGE - 1);
     45     }
     46 
     47     /** Returns the smallest user Id which is contained in this UidRange */
     48     public int getStartUser() {
     49         return start / PER_USER_RANGE;
     50     }
     51 
     52     /** Returns the largest user Id which is contained in this UidRange */
     53     public int getEndUser() {
     54         return stop / PER_USER_RANGE;
     55     }
     56 
     57     public boolean contains(int uid) {
     58         return start <= uid && uid <= stop;
     59     }
     60 
     61     /**
     62      * Returns the count of UIDs in this range.
     63      */
     64     public int count() {
     65         return 1 + stop - start;
     66     }
     67 
     68     /**
     69      * @return {@code true} if this range contains every UID contained by the {@param other} range.
     70      */
     71     public boolean containsRange(UidRange other) {
     72         return start <= other.start && other.stop <= stop;
     73     }
     74 
     75     @Override
     76     public int hashCode() {
     77         int result = 17;
     78         result = 31 * result + start;
     79         result = 31 * result + stop;
     80         return result;
     81     }
     82 
     83     @Override
     84     public boolean equals(Object o) {
     85         if (this == o) {
     86             return true;
     87         }
     88         if (o instanceof UidRange) {
     89             UidRange other = (UidRange) o;
     90             return start == other.start && stop == other.stop;
     91         }
     92         return false;
     93     }
     94 
     95     @Override
     96     public String toString() {
     97         return start + "-" + stop;
     98     }
     99 
    100     // Implement the Parcelable interface
    101     // TODO: Consider making this class no longer parcelable, since all users are likely in the
    102     // system server.
    103     @Override
    104     public int describeContents() {
    105         return 0;
    106     }
    107 
    108     @Override
    109     public void writeToParcel(Parcel dest, int flags) {
    110         dest.writeInt(start);
    111         dest.writeInt(stop);
    112     }
    113 
    114     public static final @android.annotation.NonNull Creator<UidRange> CREATOR =
    115         new Creator<UidRange>() {
    116             @Override
    117             public UidRange createFromParcel(Parcel in) {
    118                 int start = in.readInt();
    119                 int stop = in.readInt();
    120 
    121                 return new UidRange(start, stop);
    122             }
    123             @Override
    124             public UidRange[] newArray(int size) {
    125                 return new UidRange[size];
    126             }
    127     };
    128 
    129     /**
    130      * Returns whether any of the UidRange in the collection contains the specified uid
    131      *
    132      * @param ranges The collection of UidRange to check
    133      * @param uid the uid in question
    134      * @return {@code true} if the uid is contained within the ranges, {@code false} otherwise
    135      *
    136      * @see UidRange#contains(int)
    137      */
    138     public static boolean containsUid(Collection<UidRange> ranges, int uid) {
    139         if (ranges == null) return false;
    140         for (UidRange range : ranges) {
    141             if (range.contains(uid)) {
    142                 return true;
    143             }
    144         }
    145         return false;
    146     }
    147 }
    148