blob: 81378dcdc51aef63584e4af3cb7814b8193fbfc4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18package com.android.server;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070021import android.content.pm.ActivityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.pm.PackageManager;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070023import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.res.Resources;
25import android.content.res.TypedArray;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070026import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Dianne Hackbornde7faf62009-06-30 13:27:30 -070028import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.util.WeakHashMap;
30
Dianne Hackbornde7faf62009-06-30 13:27:30 -070031/**
32 * TODO: This should be better integrated into the system so it doesn't need
33 * special calls from the activity manager to clear it.
34 */
35public final class AttributeCache {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 private static AttributeCache sInstance = null;
37
38 private final Context mContext;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070039 private final WeakHashMap<String, Package> mPackages =
40 new WeakHashMap<String, Package>();
41 private final Configuration mConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Dianne Hackbornde7faf62009-06-30 13:27:30 -070043 public final static class Package {
44 public final Context context;
45 private final SparseArray<HashMap<int[], Entry>> mMap
46 = new SparseArray<HashMap<int[], Entry>>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Dianne Hackbornde7faf62009-06-30 13:27:30 -070048 public Package(Context c) {
49 context = c;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 }
51 }
52
53 public final static class Entry {
54 public final Context context;
55 public final TypedArray array;
56
57 public Entry(Context c, TypedArray ta) {
58 context = c;
59 array = ta;
60 }
61 }
62
63 public static void init(Context context) {
64 if (sInstance == null) {
65 sInstance = new AttributeCache(context);
66 }
67 }
68
69 public static AttributeCache instance() {
70 return sInstance;
71 }
72
73 public AttributeCache(Context context) {
74 mContext = context;
75 }
76
Dianne Hackbornde7faf62009-06-30 13:27:30 -070077 public void removePackage(String packageName) {
78 synchronized (this) {
79 mPackages.remove(packageName);
80 }
81 }
82
83 public void updateConfiguration(Configuration config) {
84 synchronized (this) {
85 int changes = mConfiguration.updateFrom(config);
86 if ((changes & ~(ActivityInfo.CONFIG_FONT_SCALE |
87 ActivityInfo.CONFIG_KEYBOARD_HIDDEN |
88 ActivityInfo.CONFIG_ORIENTATION)) != 0) {
89 // The configurations being masked out are ones that commonly
90 // change so we don't want flushing the cache... all others
91 // will flush the cache.
92 mPackages.clear();
93 }
94 }
95 }
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public Entry get(String packageName, int resId, int[] styleable) {
98 synchronized (this) {
Dianne Hackbornde7faf62009-06-30 13:27:30 -070099 Package pkg = mPackages.get(packageName);
100 HashMap<int[], Entry> map = null;
101 Entry ent = null;
102 if (pkg != null) {
103 map = pkg.mMap.get(resId);
104 if (map != null) {
105 ent = map.get(styleable);
106 if (ent != null) {
107 return ent;
108 }
109 }
110 } else {
111 Context context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 try {
113 context = mContext.createPackageContext(packageName, 0);
114 if (context == null) {
115 return null;
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 } catch (PackageManager.NameNotFoundException e) {
118 return null;
119 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700120 pkg = new Package(context);
121 mPackages.put(packageName, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700123
124 if (map == null) {
125 map = new HashMap<int[], Entry>();
126 pkg.mMap.put(resId, map);
127 }
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 try {
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700130 ent = new Entry(pkg.context,
131 pkg.context.obtainStyledAttributes(resId, styleable));
132 map.put(styleable, ent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 } catch (Resources.NotFoundException e) {
134 return null;
135 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 return ent;
138 }
139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141