You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * Copyright (C) 2012 The MusicMod 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. */packagenet.londatiga.android;
importjava.io.IOException;
importorg.xmlpull.v1.XmlPullParser;
importorg.xmlpull.v1.XmlPullParserException;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.res.XmlResourceParser;
importandroid.graphics.drawable.Drawable;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.util.Xml;
importandroid.view.InflateException;
/** * This class is used to instantiate menu XML files into Menu objects. * <p> * For performance reasons, menu inflation relies heavily on pre-processing of * XML files that is done at build time. Therefore, it is not currently possible * to use MenuInflater with an XmlPullParser over a plain XML file at runtime; * it only works with an XmlPullParser returned from a compiled resource (R. * <em>something</em> file.) */publicclassQuickActionInflater {
/** Menu tag name in XML. */privatestaticfinalStringXML_MENU = "menu";
/** Item tag name in XML. */privatestaticfinalStringXML_ITEM = "item";
/** Namespaces to read attributes */privatestaticfinalStringANDROID_NS = "http://schemas.android.com/apk/res/android";
/** Attribute names */privatestaticfinalStringATTR_ID = "id";
privatestaticfinalStringATTR_TITLE = "title";
privatestaticfinalStringATTR_ICON = "icon";
privateContextmContext;
/** * Constructs a menu inflater. * * @see Activity#getMenuInflater() */publicQuickActionInflater(Contextcontext) {
mContext = context;
}
/** * Inflate a menu hierarchy from the specified XML resource. Throws * {@link InflateException} if there is an error. * * @param menuRes Resource ID for an XML layout resource to load (e.g., * <code>R.menu.main_activity</code>) * @param menu The Menu to inflate into. The items and submenus will be * added to this Menu. */publicvoidinflate(intmenuRes, QuickActionaction) {
XmlResourceParserparser = null;
try {
parser = mContext.getResources().getLayout(menuRes);
AttributeSetattrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs, action);
} catch (XmlPullParserExceptione) {
thrownewInflateException("Error inflating menu XML", e);
} catch (IOExceptione) {
thrownewInflateException("Error inflating menu XML", e);
} finally {
if (parser != null) parser.close();
}
}
/** * Called internally to fill the given menu. If a sub menu is seen, it will * call this recursively. */privatevoidparseMenu(XmlPullParserparser, AttributeSetattrs, QuickActionaction)
throwsXmlPullParserException, IOException {
QuickActionStatequickActionState = newQuickActionState(action);
inteventType = parser.getEventType();
StringtagName;
booleanlookingForEndOfUnknownTag = false;
StringunknownTagName = null;
// This loop will skip to the menu start tagdo {
if (eventType == XmlPullParser.START_TAG) {
tagName = parser.getName();
if (tagName.equals(XML_MENU)) {
// Go to next tageventType = parser.next();
break;
}
thrownewRuntimeException("Expecting menu, got " + tagName);
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
booleanreachedEndOfMenu = false;
while (!reachedEndOfMenu) {
switch (eventType) {
caseXmlPullParser.START_TAG:
if (lookingForEndOfUnknownTag) {
break;
}
tagName = parser.getName();
if (tagName.equals(XML_ITEM)) {
quickActionState.readItem(attrs);
} else {
lookingForEndOfUnknownTag = true;
unknownTagName = tagName;
}
break;
caseXmlPullParser.END_TAG:
tagName = parser.getName();
if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
lookingForEndOfUnknownTag = false;
unknownTagName = null;
} elseif (tagName.equals(XML_ITEM)) {
// Add the item if it hasn't been added (if the item was// a submenu, it would have been added already)if (!quickActionState.hasAddedItem()) {
quickActionState.addItem();
}
} elseif (tagName.equals(XML_MENU)) {
reachedEndOfMenu = true;
}
break;
caseXmlPullParser.END_DOCUMENT:
thrownewRuntimeException("Unexpected end of document");
}
eventType = parser.next();
}
}
/** * State for the current menu. * <p> * Groups can not be nested unless there is another menu (which will have * its state class). */privateclassQuickActionState {
privateQuickActionaction;
privatebooleanitemAdded;
privateintitemId;
privateStringitemTitle;
privateDrawableitemIcon;
publicQuickActionState(finalQuickActionaction) {
this.action = action;
}
/** * Called when the parser is pointing to an item tag. */publicvoidreadItem(AttributeSetattrs) {
// Inherit attributes from the group as default valueLog.d("QuickActionInflater", "item read! itemId = " + itemId);
itemId = attrs.getAttributeResourceValue(ANDROID_NS, ATTR_ID, 0);
itemTitle = mContext.getString(attrs.getAttributeResourceValue(ANDROID_NS, ATTR_TITLE, 0));
itemIcon = mContext.getResources().getDrawable(attrs.getAttributeResourceValue(ANDROID_NS, ATTR_ICON, android.R.color.transparent));
itemAdded = false;
}
privateActionItemsetItem() {
returnnewActionItem(itemId,itemTitle,itemIcon);
}
publicvoidaddItem() {
itemAdded = true;
action.addActionItem(setItem());
}
publicbooleanhasAddedItem() {
returnitemAdded;
}
}
}
The text was updated successfully, but these errors were encountered:
kashban
added a commit
to kashban/NewQuickAction3D
that referenced
this issue
May 12, 2017
Hi! I used your code in my own project. I'd like to contribute some code for you!
This code is QuickActionInflater, use it as MenuInflater, example:
Source code:
The text was updated successfully, but these errors were encountered: