1e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette/* 2e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * Copyright (C) 2015 The Android Open Source Project 3e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * 4e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License"); 5e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * you may not use this file except in compliance with the License. 6e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * You may obtain a copy of the License at 7e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * 8e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * http://www.apache.org/licenses/LICENSE-2.0 9e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * 10e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * Unless required by applicable law or agreed to in writing, software 11e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS, 12e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * See the License for the specific language governing permissions and 14e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * limitations under the License. 15e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette */ 16e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette 17e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverettepackage android.content.res; 18e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette 19e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viveretteimport android.graphics.drawable.Drawable; 20e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette 21e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette/** 22e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * Class which can be used to cache Drawable resources against a theme. 23e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette */ 24e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viveretteclass DrawableCache extends ThemedResourceCache<Drawable.ConstantState> { 25e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette /** 26e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * If the resource is cached, creates and returns a new instance of it. 27e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * 28e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * @param key a key that uniquely identifies the drawable resource 29fb302ccd8e0610a09691ea5503ff8111dc7a2e41Adam Lesinski * @param resources a Resources object from which to create new instances. 30e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * @param theme the theme where the resource will be used 31e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * @return a new instance of the resource, or {@code null} if not in 32e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette * the cache 33e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette */ 34fb302ccd8e0610a09691ea5503ff8111dc7a2e41Adam Lesinski public Drawable getInstance(long key, Resources resources, Resources.Theme theme) { 35e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette final Drawable.ConstantState entry = get(key, theme); 36e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette if (entry != null) { 37fb302ccd8e0610a09691ea5503ff8111dc7a2e41Adam Lesinski return entry.newDrawable(resources, theme); 38e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette } 39e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette 40e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette return null; 41e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette } 42e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette 43e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette @Override 44e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette public boolean shouldInvalidateEntry(Drawable.ConstantState entry, int configChanges) { 457ef1e773fdf5f7aadf151a682c7f55a1e4f3c502Alan Viverette return Configuration.needNewResources(configChanges, entry.getChangingConfigurations()); 46e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette } 47e54d245b993e1347cb32c23a6bdc907a45fab324Alan Viverette} 48