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 import android.ravenwood.annotation.RavenwoodKeepWholeClass; 20 21 /** 22 * Provides a set of utility methods for dealing with Resource IDs. 23 * @hide 24 */ 25 @RavenwoodKeepWholeClass 26 public final class ResourceId { 27 /** 28 * Checks whether the integer {@code id} is a valid resource ID, as generated by AAPT. 29 * <p>Note that a negative integer is not necessarily an invalid resource ID, and custom 30 * validations that compare the {@code id} against {@code 0} are incorrect.</p> 31 * @param id The integer to validate. 32 * @return {@code true} if the integer is a valid resource ID. 33 */ isValid(@nyRes int id)34 public static boolean isValid(@AnyRes int id) { 35 // With the introduction of packages with IDs > 0x7f, resource IDs can be negative when 36 // represented as a signed Java int. Some legacy code assumes -1 is an invalid resource ID, 37 // despite the existing documentation. 38 return id != -1 && (id & 0xff000000) != 0 && (id & 0x00ff0000) != 0; 39 } 40 } 41