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 
17 package android.content.res;
18 
19 import android.content.pm.ActivityInfo.Config;
20 
21 /**
22  * A Cache class which can be used to cache resource objects that are easy to clone but more
23  * expensive to inflate.
24  *
25  * @hide For internal use only.
26  */
27 public class ConfigurationBoundResourceCache<T> extends ThemedResourceCache<ConstantState<T>> {
28     /**
29      * If the resource is cached, creates and returns a new instance of it.
30      *
31      * @param key a key that uniquely identifies the drawable resource
32      * @param resources a Resources object from which to create new instances.
33      * @param theme the theme where the resource will be used
34      * @return a new instance of the resource, or {@code null} if not in
35      *         the cache
36      */
getInstance(long key, Resources resources, Resources.Theme theme)37     public T getInstance(long key, Resources resources, Resources.Theme theme) {
38         final ConstantState<T> entry = get(key, theme);
39         if (entry != null) {
40             return entry.newInstance(resources, theme);
41         }
42 
43         return null;
44     }
45 
46     @Override
shouldInvalidateEntry(ConstantState<T> entry, @Config int configChanges)47     public boolean shouldInvalidateEntry(ConstantState<T> entry, @Config int configChanges) {
48         return Configuration.needNewResources(configChanges, entry.getChangingConfigurations());
49     }
50 }
51