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