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     /**
27      * The {@code null} resource ID.
28      */
29     public static final @AnyRes int ID_NULL = 0;
30 
31     /**
32      * Checks whether the integer {@code id} is a valid resource ID, as generated by AAPT.
33      * <p>Note that a negative integer is not necessarily an invalid resource ID, and custom
34      * validations that compare the {@code id} against {@code 0} are incorrect.</p>
35      * @param id The integer to validate.
36      * @return {@code true} if the integer is a valid resource ID.
37      */
isValid(@nyRes int id)38     public static boolean isValid(@AnyRes int id) {
39         // With the introduction of packages with IDs > 0x7f, resource IDs can be negative when
40         // represented as a signed Java int. Some legacy code assumes -1 is an invalid resource ID,
41         // despite the existing documentation.
42         return id != -1 && (id & 0xff000000) != 0 && (id & 0x00ff0000) != 0;
43     }
44 }
45