1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 package android.content.res;
17 
18 import android.annotation.AnyRes;
19 
20 /**
21  * Provides a set of utility methods for dealing with Resource IDs.
22  * @hide
23  */
24 public final class ResourceId {
25     /**
26      * Checks whether the integer {@code id} is a valid resource ID, as generated by AAPT.
27      * <p>Note that a negative integer is not necessarily an invalid resource ID, and custom
28      * validations that compare the {@code id} against {@code 0} are incorrect.</p>
29      * @param id The integer to validate.
30      * @return {@code true} if the integer is a valid resource ID.
31      */
isValid(@nyRes int id)32     public static boolean isValid(@AnyRes int id) {
33         // With the introduction of packages with IDs > 0x7f, resource IDs can be negative when
34         // represented as a signed Java int. Some legacy code assumes -1 is an invalid resource ID,
35         // despite the existing documentation.
36         return id != -1 && (id & 0xff000000) != 0 && (id & 0x00ff0000) != 0;
37     }
38 }
39