Debian Codegraph @

core/java/com/android/internal/policy/AttributeCache.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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 com.android.internal.policy;
18
19 import android.content.Context;
20 import android.content.pm.ActivityInfo;
21 import android.content.pm.PackageManager;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.content.res.TypedArray;
25 import android.os.UserHandle;
26 import android.util.ArrayMap;
27 import android.util.LruCache;
28 import android.util.SparseArray;
29
30 import com.android.internal.annotations.GuardedBy;
31
32 /**
33 * TODO: This should be better integrated into the system so it doesn't need
34 * special calls from the activity manager to clear it.
35 */
36 /** @hide */
37 public final class AttributeCache {
38 private static final int CACHE_SIZE = 4;
39 private static AttributeCache sInstance = null;
40
41 private final Context mContext;
42
43 @GuardedBy("this")
44 private final LruCache<String, Package> mPackages = new LruCache<>(CACHE_SIZE);
45
46 @GuardedBy("this")
47 private final Configuration mConfiguration = new Configuration();
48
49 public final static class Package {
50 public final Context context;
51 private final SparseArray<ArrayMap<int[], Entry>> mMap = new SparseArray<>();
52
53 public Package(Context c) {
54 context = c;
55 }
56 }
57
58 public final static class Entry {
59 public final Context context;
60 public final TypedArray array;
61
62 public Entry(Context c, TypedArray ta) {
63 context = c;
64 array = ta;
65 }
66
67 void recycle() {
68 if (array != null) {
69 array.recycle();
70 }
71 }
72 }
73
74 public static void init(Context context) {
75 if (sInstance == null) {
76 sInstance = new AttributeCache(context);
77 }
78 }
79
80 public static AttributeCache instance() {
81 return sInstance;
82 }
83
84 public AttributeCache(Context context) {
85 mContext = context;
86 }
87
88 public void removePackage(String packageName) {
89 synchronized (this) {
90 final Package pkg = mPackages.remove(packageName);
91 if (pkg != null) {
92 for (int i = 0; i < pkg.mMap.size(); i++) {
93 final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i);
94 for (int j = 0; j < map.size(); j++) {
95 map.valueAt(j).recycle();
96 }
97 }
98
99 final Resources res = pkg.context.getResources();
100 res.flushLayoutCache();
101 }
102 }
103 }
104
105 public void updateConfiguration(Configuration config) {
106 synchronized (this) {
107 int changes = mConfiguration.updateFrom(config);
108 if ((changes & ~(ActivityInfo.CONFIG_FONT_SCALE |
109 ActivityInfo.CONFIG_KEYBOARD_HIDDEN |
110 ActivityInfo.CONFIG_ORIENTATION)) != 0) {
111 // The configurations being masked out are ones that commonly
112 // change so we don't want flushing the cache... all others
113 // will flush the cache.
114 mPackages.evictAll();
115 }
116 }
117 }
118
119 public Entry get(String packageName, int resId, int[] styleable) {
120 return get(packageName, resId, styleable, UserHandle.USER_CURRENT);
121 }
122
123 public Entry get(String packageName, int resId, int[] styleable, int userId) {
124 synchronized (this) {
125 Package pkg = mPackages.get(packageName);
126 ArrayMap<int[], Entry> map = null;
127 Entry ent = null;
128 if (pkg != null) {
129 map = pkg.mMap.get(resId);
130 if (map != null) {
131 ent = map.get(styleable);
132 if (ent != null) {
133 return ent;
134 }
135 }
136 } else {
137 Context context;
138 try {
139 context = mContext.createPackageContextAsUser(packageName, 0,
140 new UserHandle(userId));
141 if (context == null) {
142 return null;
143 }
144 } catch (PackageManager.NameNotFoundException e) {
145 return null;
146 }
147 pkg = new Package(context);
148 mPackages.put(packageName, pkg);
149 }
150
151 if (map == null) {
152 map = new ArrayMap<>();
153 pkg.mMap.put(resId, map);
154 }
155
156 try {
157 ent = new Entry(pkg.context,
158 pkg.context.obtainStyledAttributes(resId, styleable));
159 map.put(styleable, ent);
160 } catch (Resources.NotFoundException e) {
161 return null;
162 }
163
164 return ent;
165 }
166 }
167 }
168