10f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette/* 20f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Copyright (C) 2014 The Android Open Source Project 30f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * 40f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License"); 50f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * you may not use this file except in compliance with the License. 60f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * You may obtain a copy of the License at 70f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * 80f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * http://www.apache.org/licenses/LICENSE-2.0 90f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * 100f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Unless required by applicable law or agreed to in writing, software 110f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS, 120f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * See the License for the specific language governing permissions and 140f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * limitations under the License. 150f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette */ 160f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette 170f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverettepackage android.support.v4.content.res; 180f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette 190f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viveretteimport android.content.res.Resources; 200f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viveretteimport android.content.res.Resources.NotFoundException; 210f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viveretteimport android.content.res.Resources.Theme; 220f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viveretteimport android.graphics.drawable.Drawable; 230f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viveretteimport android.os.Build; 240f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette 250f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette/** 260f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Helper for accessing features in {@link android.content.res.Resources} 270f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * introduced after API level 4 in a backwards compatible fashion. 280f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette */ 290f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverettepublic class ResourcesCompat { 300f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette /** 310f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Return a drawable object associated with a particular resource ID and 320f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * styled for the specified theme. Various types of objects will be 330f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * returned depending on the underlying resource -- for example, a solid 340f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * color, PNG image, scalable image, etc. 350f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * <p> 360f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * Prior to API level 21, the theme will not be applied and this method 370f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * simply calls through to {@link Resources#getDrawable(int)}. 380f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * 390f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * @param id The desired resource identifier, as generated by the aapt 400f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * tool. This integer encodes the package, type, and resource 410f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * entry. The value 0 is an invalid identifier. 42b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @param theme The theme used to style the drawable attributes, may be 43b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * {@code null}. 440f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * @return Drawable An object that can be used to draw this resource. 450f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * @throws NotFoundException Throws NotFoundException if the given ID does 460f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette * not exist. 470f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette */ 48b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette @SuppressWarnings("deprecation") 49f0befbfd05c33c4dc6667a051f9b69a47dfe4aa7Chris Banes public static Drawable getDrawable(Resources res, int id, Theme theme) 500f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette throws NotFoundException { 510f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette final int version = Build.VERSION.SDK_INT; 520f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette if (version >= 21) { 530f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette return ResourcesCompatApi21.getDrawable(res, id, theme); 540f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette } else { 550f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette return res.getDrawable(id); 560f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette } 570f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette } 58b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette 59b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette 60b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette /** 61b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * Return a drawable object associated with a particular resource ID for 62b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * the given screen density in DPI and styled for the specified theme. 63b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * <p> 64b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * Prior to API level 15, the theme and density will not be applied and 65b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * this method simply calls through to {@link Resources#getDrawable(int)}. 66b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * <p> 67b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * Prior to API level 21, the theme will not be applied and this method 68b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * calls through to Resources.getDrawableForDensity(int, int). 69b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * 70b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @param id The desired resource identifier, as generated by the aapt 71b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * tool. This integer encodes the package, type, and resource 72b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * entry. The value 0 is an invalid identifier. 73b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @param density The desired screen density indicated by the resource as 74b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * found in {@link android.util.DisplayMetrics}. 75b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @param theme The theme used to style the drawable attributes, may be 76b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * {@code null}. 77b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @return Drawable An object that can be used to draw this resource. 78b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * @throws NotFoundException Throws NotFoundException if the given ID does 79b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette * not exist. 80b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette */ 81b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette @SuppressWarnings("deprecation") 82b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette public static Drawable getDrawableForDensity(Resources res, int id, int density, Theme theme) 83b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette throws NotFoundException { 84b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette final int version = Build.VERSION.SDK_INT; 85b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette if (version >= 21) { 86b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette return ResourcesCompatApi21.getDrawableForDensity(res, id, density, theme); 87b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette } else if (version >= 15) { 88b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette return ResourcesCompatIcsMr1.getDrawableForDensity(res, id, density); 89b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette } else { 90b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette return res.getDrawable(id); 91b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette } 92b5538e1e9fdc71d53e353ef42d4a76ddf813c318Alan Viverette } 930f0408b6514f8e9c105d255de3c62dc91744f7a8Alan Viverette} 94