| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 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 |
|