summaryrefslogtreecommitdiffstats
path: root/core/java/android/content/pm/ThemeInfo.java
blob: e51dbb6ae8cd7118f22129f3d49304f2e4a83f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2010, T-Mobile USA, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.content.pm;

import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParser;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.content.res.Resources;

/**
 * Overall information about "theme" package.  This corresponds
 * to the information collected from AndroidManifest.xml (theme tag).
 *
 * Below is an example of theme tag
 *    <theme
 *        pluto:name="Pluto Default"
 *        pluto:preview="@drawable/preview"
 *        pluto:author="John Doe"
 *        pluto:ringtoneFileName="media/audio/ringtone.mp3"
 *        pluto:notificationRingtoneFileName="media/audio/locked/notification.mp3"
 *        pluto:copyright="T-Mobile, 2009"
 *    />
 *
 * @hide
 */
public final class ThemeInfo extends BaseThemeInfo {
    private enum AttributeIndex {
        THEME_PACKAGE_INDEX,
        PREVIEW_INDEX,
        AUTHOR_INDEX,
        THEME_INDEX,
        THEME_STYLE_NAME_INDEX,
        THUMBNAIL_INDEX,
        RINGTONE_FILE_NAME_INDEX,
        NOTIFICATION_RINGTONE_FILE_NAME_INDEX,
        WALLPAPER_IMAGE_INDEX,
        COPYRIGHT_INDEX,
        RINGTONE_NAME_INDEX,
        NOTIFICATION_RINGTONE_NAME_INDEX,
        STYLE_INDEX;

        public static AttributeIndex get(int ordinal) {
            return values()[ordinal];
        }
    };

    private static final String [] compulsoryAttributes = new String [] {
        "name",
        "preview",
        "author",
        "themeId",
        "styleName",
    };

    private static final String [] optionalAttributes = new String [] {
        "thumbnail",
        "ringtoneFileName",
        "notificationRingtoneFileName",
        "wallpaperImage",
        "copyright",
        "ringtoneName",
        "notificationRingtoneName",
        "styleId",
    };

    private static final Map<String, AttributeIndex> sAttributesLookupTable;

    static {
        sAttributesLookupTable = new HashMap<String, AttributeIndex>();
        for (int i = 0; i < compulsoryAttributes.length; i++) {
            sAttributesLookupTable.put(compulsoryAttributes[i], AttributeIndex.get(i));
        }

        for (int i = 0; i < optionalAttributes.length; i++) {
            sAttributesLookupTable.put(optionalAttributes[i],
                    AttributeIndex.get(compulsoryAttributes.length + i));
        }
    }

    public ThemeInfo(XmlPullParser parser, Resources res, AttributeSet attrs) throws XmlPullParserException {
        super();

        Map<String, AttributeIndex> tempMap =
                new HashMap<String, AttributeIndex>(sAttributesLookupTable);
        int numberOfCompulsoryAttributes = 0;
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (!ApplicationInfo.isPlutoNamespace(parser.getAttributeNamespace(i))) {
                continue;
            }
            String key = attrs.getAttributeName(i);
            if (tempMap.containsKey(key)) {
                AttributeIndex index = tempMap.get(key);
                tempMap.remove(key);

                if (index.ordinal() < compulsoryAttributes.length) {
                    numberOfCompulsoryAttributes++;
                }
                switch (index) {
                    case THEME_PACKAGE_INDEX:
                        // theme name
                        name = getResolvedString(res, attrs, i);
                        break;

                    case THUMBNAIL_INDEX:
                        // theme thumbprint
                        thumbnailResourceId = attrs.getAttributeResourceValue(i, 0);
                        break;

                    case AUTHOR_INDEX:
                        // theme author
                        author = getResolvedString(res, attrs, i);
                        break;

                    case THEME_INDEX:
                        // androidUiStyle attribute
                        themeId = attrs.getAttributeValue(i);
                        break;

                    case THEME_STYLE_NAME_INDEX:
                        themeStyleName = getResolvedString(res, attrs, i);
                        break;

                    case RINGTONE_FILE_NAME_INDEX:
                        // ringtone
                        ringtoneFileName = attrs.getAttributeValue(i);
                        changeDrmFlagIfNeeded(ringtoneFileName);
                        break;

                    case NOTIFICATION_RINGTONE_FILE_NAME_INDEX:
                        // notification ringtone
                        notificationRingtoneFileName = attrs.getAttributeValue(i);
                        changeDrmFlagIfNeeded(notificationRingtoneFileName);
                        break;

                    case WALLPAPER_IMAGE_INDEX:
                        // wallpaperImage attribute
                        wallpaperResourceId = attrs.getAttributeResourceValue(i, 0);
                        break;

                    case COPYRIGHT_INDEX:
                        // themeCopyright attribute
                        copyright = getResolvedString(res, attrs, i);
                        break;

                    case RINGTONE_NAME_INDEX:
                        // ringtone UI name
                        ringtoneName = attrs.getAttributeValue(i);
                        break;

                    case NOTIFICATION_RINGTONE_NAME_INDEX:
                        // notification ringtone UI name
                        notificationRingtoneName = attrs.getAttributeValue(i);
                        break;

                    case STYLE_INDEX:
                        styleResourceId = attrs.getAttributeResourceValue(i, 0);
                        break;

                    case PREVIEW_INDEX:
                        // theme thumbprint
                        previewResourceId = attrs.getAttributeResourceValue(i, 0);
                        break;
                }
            }
        }
        if (numberOfCompulsoryAttributes < compulsoryAttributes.length) {
            throw new XmlPullParserException("Not all compulsory attributes are specified in <theme>");
        }
    }

    public static final Parcelable.Creator<ThemeInfo> CREATOR
            = new Parcelable.Creator<ThemeInfo>() {
        public ThemeInfo createFromParcel(Parcel source) {
            return new ThemeInfo(source);
        }

        public ThemeInfo[] newArray(int size) {
            return new ThemeInfo[size];
        }
    };

    private ThemeInfo(Parcel source) {
        super(source);
    }
}