summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/actionbar/SimpleMenuInflater.java
blob: 885a4d3bcc981d280badaaf2d091b5e18798624d (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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 com.android.gallery3d.actionbar;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.InflateException;

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

import java.io.IOException;

public class SimpleMenuInflater {

    private static final String TAG_MENU = "menu";
    private static final String TAG_ITEM = "item";

    private final Context mContext;

    public SimpleMenuInflater(Context context) {
        mContext = context;
    }

    public SimpleMenu inflate(SimpleMenu menu, int menuRes) throws InflateException {
        XmlResourceParser parser = mContext.getResources().getXml(menuRes);
        try {
            return inflateInternal(menu, parser);
        } catch (XmlPullParserException e) {
            throw new InflateException(e);
        } catch (IOException e) {
            throw new InflateException(e);
        } finally {
            if (parser != null) parser.close();
        }
    }

    private SimpleMenu inflateInternal(SimpleMenu menu, XmlPullParser parser)
            throws XmlPullParserException, IOException {
        int eventType = parser.getEventType();
        do {
            if (eventType == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if (TAG_MENU.equals(tagName)) {
                    eventType = parser.next();
                    break;
                }
                throw new RuntimeException("unexpected tag: " + tagName);
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.END_DOCUMENT);

        if (menu == null) menu = new SimpleMenu();
        AttributeSet attrs = Xml.asAttributeSet(parser);

        boolean reachedEndOfMenu = false;
        while (!reachedEndOfMenu) {
            switch (eventType) {
                case XmlPullParser.START_TAG: {
                    String tagName = parser.getName();
                    if (TAG_ITEM.equals(tagName)) {
                        menu.addItem(parseItem(attrs));
                    } else if (TAG_MENU.equals(tagName)) {
                        throw new RuntimeException("nested menu not supported");
                    } else {
                        // ignore all other tags
                        parser.next();
                    }
                    break;
                }
                case XmlPullParser.END_TAG: {
                    String tagName = parser.getName();
                    if (TAG_MENU.equals(tagName)) {
                        reachedEndOfMenu = true;
                        break;
                    }
                    break;
                }
                case XmlPullParser.END_DOCUMENT: {
                    throw new RuntimeException("unexpected end of document");
                }
            }
            eventType = parser.next();
        }
        return menu;
    }

    private static final String ATTR_ID = "id";
    private static final String ATTR_ICON = "icon";
    private static final String ATTR_TITLE = "title";
    private static final String ATTR_VISIBLE = "visible";
    private static final String ATTR_SHOW_AS_ACTION = "showAsAction";

    private SimpleMenu.Item parseItem(AttributeSet attrs) {
        SimpleMenu.Item item = new SimpleMenu.Item();
        Resources res = mContext.getResources();
        for (int i = 0, n = attrs.getAttributeCount(); i < n; ++i) {
            String attrName = attrs.getAttributeName(i);
            if (ATTR_ID.equals(attrName)) {
                item.id = attrs.getAttributeResourceValue(i, 0);
            } else if (ATTR_ICON.equals(attrName)) {
                item.iconId = attrs.getAttributeResourceValue(i, 0);
            } else if (ATTR_TITLE.equals(attrName)) {
                int id = attrs.getAttributeResourceValue(i, 0);
                item.title = id == 0 ? null : res.getString(id);
            } else if (ATTR_VISIBLE.equals(attrName)) {
                item.visible = attrs.getAttributeBooleanValue(i, true);
            } else if (ATTR_SHOW_AS_ACTION.equals(attrName)) {
                item.showAsAction = attrs.getAttributeIntValue(i, 0);
            }
        }
        return item;
    }

}