ResourcesCompat.java revision b5538e1e9fdc71d53e353ef42d4a76ddf813c318
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 43 * {@code null}. 44 * @return Drawable An object that can be used to draw this resource. 45 * @throws NotFoundException Throws NotFoundException if the given ID does 46 * not exist. 47 */ 48 @SuppressWarnings("deprecation") 49 public static Drawable getDrawable(Resources res, int id, Theme theme) 50 throws NotFoundException { 51 final int version = Build.VERSION.SDK_INT; 52 if (version >= 21) { 53 return ResourcesCompatApi21.getDrawable(res, id, theme); 54 } else { 55 return res.getDrawable(id); 56 } 57 } 58 59 60 /** 61 * Return a drawable object associated with a particular resource ID for 62 * the given screen density in DPI and styled for the specified theme. 63 * <p> 64 * Prior to API level 15, the theme and density will not be applied and 65 * this method simply calls through to {@link Resources#getDrawable(int)}. 66 * <p> 67 * Prior to API level 21, the theme will not be applied and this method 68 * calls through to Resources.getDrawableForDensity(int, int). 69 * 70 * @param id The desired resource identifier, as generated by the aapt 71 * tool. This integer encodes the package, type, and resource 72 * entry. The value 0 is an invalid identifier. 73 * @param density The desired screen density indicated by the resource as 74 * found in {@link android.util.DisplayMetrics}. 75 * @param theme The theme used to style the drawable attributes, may be 76 * {@code null}. 77 * @return Drawable An object that can be used to draw this resource. 78 * @throws NotFoundException Throws NotFoundException if the given ID does 79 * not exist. 80 */ 81 @SuppressWarnings("deprecation") 82 public static Drawable getDrawableForDensity(Resources res, int id, int density, Theme theme) 83 throws NotFoundException { 84 final int version = Build.VERSION.SDK_INT; 85 if (version >= 21) { 86 return ResourcesCompatApi21.getDrawableForDensity(res, id, density, theme); 87 } else if (version >= 15) { 88 return ResourcesCompatIcsMr1.getDrawableForDensity(res, id, density); 89 } else { 90 return res.getDrawable(id); 91 } 92 } 93} 94