blob: 427dbc091ced5dd25ff861d6210ebe5d323f7709 [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;
Amith Yamasani4befbec2013-07-10 16:18:01 -070026import android.os.UserHandle;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070027import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Dianne Hackbornde7faf62009-06-30 13:27:30 -070029import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import java.util.WeakHashMap;
31
Dianne Hackbornde7faf62009-06-30 13:27:30 -070032/**
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 */
36public final class AttributeCache {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 private static AttributeCache sInstance = null;
38
39 private final Context mContext;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070040 private final WeakHashMap<String, Package> mPackages =
41 new WeakHashMap<String, Package>();
42 private final Configuration mConfiguration = new Configuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Dianne Hackbornde7faf62009-06-30 13:27:30 -070044 public final static class Package {
45 public final Context context;
46 private final SparseArray<HashMap<int[], Entry>> mMap
47 = new SparseArray<HashMap<int[], Entry>>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Dianne Hackbornde7faf62009-06-30 13:27:30 -070049 public Package(Context c) {
50 context = c;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
52 }
53
54 public final static class Entry {
55 public final Context context;
56 public final TypedArray array;
57
58 public Entry(Context c, TypedArray ta) {
59 context = c;
60 array = ta;
61 }
62 }
63
64 public static void init(Context context) {
65 if (sInstance == null) {
66 sInstance = new AttributeCache(context);
67 }
68 }
69
70 public static AttributeCache instance() {
71 return sInstance;
72 }
73
74 public AttributeCache(Context context) {
75 mContext = context;
76 }
77
Dianne Hackbornde7faf62009-06-30 13:27:30 -070078 public void removePackage(String packageName) {
79 synchronized (this) {
80 mPackages.remove(packageName);
81 }
82 }
83
84 public void updateConfiguration(Configuration config) {
85 synchronized (this) {
86 int changes = mConfiguration.updateFrom(config);
87 if ((changes & ~(ActivityInfo.CONFIG_FONT_SCALE |
88 ActivityInfo.CONFIG_KEYBOARD_HIDDEN |
89 ActivityInfo.CONFIG_ORIENTATION)) != 0) {
90 // The configurations being masked out are ones that commonly
91 // change so we don't want flushing the cache... all others
92 // will flush the cache.
93 mPackages.clear();
94 }
95 }
96 }
97
Amith Yamasani4befbec2013-07-10 16:18:01 -070098 public Entry get(String packageName, int resId, int[] styleable, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 synchronized (this) {
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700100 Package pkg = mPackages.get(packageName);
101 HashMap<int[], Entry> map = null;
102 Entry ent = null;
103 if (pkg != null) {
104 map = pkg.mMap.get(resId);
105 if (map != null) {
106 ent = map.get(styleable);
107 if (ent != null) {
108 return ent;
109 }
110 }
111 } else {
112 Context context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 try {
Amith Yamasani4befbec2013-07-10 16:18:01 -0700114 context = mContext.createPackageContextAsUser(packageName, 0,
115 new UserHandle(userId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 if (context == null) {
117 return null;
118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 } catch (PackageManager.NameNotFoundException e) {
120 return null;
121 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700122 pkg = new Package(context);
123 mPackages.put(packageName, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700125
126 if (map == null) {
127 map = new HashMap<int[], Entry>();
128 pkg.mMap.put(resId, map);
129 }
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 try {
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700132 ent = new Entry(pkg.context,
133 pkg.context.obtainStyledAttributes(resId, styleable));
134 map.put(styleable, ent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 } catch (Resources.NotFoundException e) {
136 return null;
137 }
Dianne Hackbornde7faf62009-06-30 13:27:30 -0700138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return ent;
140 }
141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142}
143