ResourcesCompat.java revision f0befbfd05c33c4dc6667a051f9b69a47dfe4aa7
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 17package android.support.v4.content.res; 18 19import android.content.res.Resources; 20import android.content.res.Resources.NotFoundException; 21import android.content.res.Resources.Theme; 22import android.graphics.drawable.Drawable; 23import android.os.Build; 24 25/** 26 * Helper for accessing features in {@link android.content.res.Resources} 27 * introduced after API level 4 in a backwards compatible fashion. 28 */ 29public class ResourcesCompat { 30 /** 31 * Return a drawable object associated with a particular resource ID and 32 * styled for the specified theme. Various types of objects will be 33 * returned depending on the underlying resource -- for example, a solid 34 * color, PNG image, scalable image, etc. 35 * <p> 36 * Prior to API level 21, the theme will not be applied and this method 37 * simply calls through to {@link Resources#getDrawable(int)}. 38 * 39 * @param id The desired resource identifier, as generated by the aapt 40 * tool. This integer encodes the package, type, and resource 41 * entry. The value 0 is an invalid identifier. 42 * @param theme The theme used to style the drawable attributes, may be {@code null}. 43 * @return Drawable An object that can be used to draw this resource. 44 * @throws NotFoundException Throws NotFoundException if the given ID does 45 * not exist. 46 */ 47 public static Drawable getDrawable(Resources res, int id, Theme theme) 48 throws NotFoundException { 49 final int version = Build.VERSION.SDK_INT; 50 if (version >= 21) { 51 return ResourcesCompatApi21.getDrawable(res, id, theme); 52 } else { 53 return res.getDrawable(id); 54 } 55 } 56} 57