The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 18 | package com.android.server; |
| 19 | |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.content.SharedPreferences; |
| 24 | import android.content.Intent; |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.res.Resources; |
| 28 | import android.content.res.TypedArray; |
| 29 | import android.provider.Settings; |
| 30 | import android.util.Config; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | import java.util.WeakHashMap; |
| 34 | |
| 35 | public final class AttributeCache extends BroadcastReceiver { |
| 36 | private static AttributeCache sInstance = null; |
| 37 | |
| 38 | private final Context mContext; |
| 39 | private final WeakHashMap<Key, Entry> mMap = |
| 40 | new WeakHashMap<Key, Entry>(); |
| 41 | private final WeakHashMap<String, Context> mContexts = |
| 42 | new WeakHashMap<String, Context>(); |
| 43 | |
| 44 | final static class Key { |
| 45 | public final String packageName; |
| 46 | public final int resId; |
| 47 | public final int[] styleable; |
| 48 | |
| 49 | public Key(String inPackageName, int inResId, int[] inStyleable) { |
| 50 | packageName = inPackageName; |
| 51 | resId = inResId; |
| 52 | styleable = inStyleable; |
| 53 | } |
| 54 | |
| 55 | @Override public boolean equals(Object obj) { |
| 56 | try { |
| 57 | if (obj != null) { |
| 58 | Key other = (Key)obj; |
| 59 | return packageName.equals(other.packageName) |
| 60 | && resId == other.resId |
| 61 | && styleable == other.styleable; |
| 62 | } |
| 63 | } catch (ClassCastException e) { |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | @Override public int hashCode() { |
| 69 | return packageName.hashCode() + resId; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public final static class Entry { |
| 74 | public final Context context; |
| 75 | public final TypedArray array; |
| 76 | |
| 77 | public Entry(Context c, TypedArray ta) { |
| 78 | context = c; |
| 79 | array = ta; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public static void init(Context context) { |
| 84 | if (sInstance == null) { |
| 85 | sInstance = new AttributeCache(context); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public static AttributeCache instance() { |
| 90 | return sInstance; |
| 91 | } |
| 92 | |
| 93 | public AttributeCache(Context context) { |
| 94 | mContext = context; |
| 95 | } |
| 96 | |
| 97 | public Entry get(String packageName, int resId, int[] styleable) { |
| 98 | synchronized (this) { |
| 99 | Key key = new Key(packageName, resId, styleable); |
| 100 | Entry ent = mMap.get(key); |
| 101 | if (ent != null) { |
| 102 | return ent; |
| 103 | } |
| 104 | Context context = mContexts.get(packageName); |
| 105 | if (context == null) { |
| 106 | try { |
| 107 | context = mContext.createPackageContext(packageName, 0); |
| 108 | if (context == null) { |
| 109 | return null; |
| 110 | } |
| 111 | mContexts.put(packageName, context); |
| 112 | } catch (PackageManager.NameNotFoundException e) { |
| 113 | return null; |
| 114 | } |
| 115 | } |
| 116 | try { |
| 117 | ent = new Entry(context, |
| 118 | context.obtainStyledAttributes(resId, styleable)); |
| 119 | mMap.put(key, ent); |
| 120 | } catch (Resources.NotFoundException e) { |
| 121 | return null; |
| 122 | } |
| 123 | return ent; |
| 124 | } |
| 125 | } |
| 126 | @Override public void onReceive(Context context, Intent intent) { |
| 127 | } |
| 128 | } |
| 129 | |