Build 1.0_r1(from source)

android.preference
Class GenericInflater<T,P extends GenericInflater.Parent>

java.lang.Object
  extended by android.preference.GenericInflater<T,P>
Direct Known Subclasses:
PreferenceInflater

abstract class GenericInflater<T,P extends GenericInflater.Parent>
extends Object

Generic XML inflater. This has been adapted from LayoutInflater and quickly passed over to use generics.


Nested Class Summary
static interface GenericInflater.Factory<T>
           
static interface GenericInflater.Parent<T>
           
 
Field Summary
protected  Context mContext
           
 
Constructor Summary
protected GenericInflater(Context context)
          Create a new inflater instance associated with a particular Context.
protected GenericInflater(GenericInflater<T,P> original, Context newContext)
          Create a new inflater instance that is a copy of an existing inflater, optionally with its Context changed.
 
Method Summary
abstract  GenericInflater cloneInContext(Context newContext)
          Create a copy of the existing inflater object, with the copy pointing to a different Context than the original.
 T createItem(String name, String prefix, AttributeSet attrs)
          Low-level function for instantiating by name.
 Context getContext()
          Return the context we are running in, for access to resources, class loader, etc.
 String getDefaultPackage()
          Returns the default package, or null if it is not set.
 GenericInflater.Factory<T> getFactory()
          Return the current factory (or null).
 T inflate(int resource, P root)
          Inflate a new item hierarchy from the specified xml resource.
 T inflate(int resource, P root, boolean attachToRoot)
          Inflate a new hierarchy from the specified xml resource.
 T inflate(XmlPullParser parser, P root)
          Inflate a new hierarchy from the specified xml node.
 T inflate(XmlPullParser parser, P root, boolean attachToRoot)
          Inflate a new hierarchy from the specified XML node.
protected  boolean onCreateCustomFromTag(XmlPullParser parser, T parent, AttributeSet attrs)
          Before this inflater tries to create an item from the tag, this method will be called.
protected  T onCreateItem(String name, AttributeSet attrs)
          This routine is responsible for creating the correct subclass of item given the xml element name.
protected  P onMergeRoots(P givenRoot, boolean attachToGivenRoot, P xmlRoot)
           
 void setDefaultPackage(String defaultPackage)
          Sets the default package that will be searched for classes to construct for tag names that have no explicit package.
 void setFactory(GenericInflater.Factory<T> factory)
          Attach a custom Factory interface for creating items while using this inflater.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mContext

protected final Context mContext
Constructor Detail

GenericInflater

protected GenericInflater(Context context)
Create a new inflater instance associated with a particular Context.

Parameters:
context - The Context in which this inflater will create its items; most importantly, this supplies the theme from which the default values for their attributes are retrieved.

GenericInflater

protected GenericInflater(GenericInflater<T,P> original,
                          Context newContext)
Create a new inflater instance that is a copy of an existing inflater, optionally with its Context changed. For use in implementing cloneInContext(android.content.Context).

Parameters:
original - The original inflater to copy.
newContext - The new Context to use.
Method Detail

cloneInContext

public abstract GenericInflater cloneInContext(Context newContext)
Create a copy of the existing inflater object, with the copy pointing to a different Context than the original. This is used by ContextThemeWrapper to create a new inflater to go along with the new Context theme.

Parameters:
newContext - The new Context to associate with the new inflater. May be the same as the original Context if desired.
Returns:
Returns a brand spanking new inflater object associated with the given Context.

setDefaultPackage

public void setDefaultPackage(String defaultPackage)
Sets the default package that will be searched for classes to construct for tag names that have no explicit package.

Parameters:
defaultPackage - The default package. This will be prepended to the tag name, so it should end with a period.

getDefaultPackage

public String getDefaultPackage()
Returns the default package, or null if it is not set.

Returns:
The default package.
See Also:
setDefaultPackage(String)

getContext

public Context getContext()
Return the context we are running in, for access to resources, class loader, etc.


getFactory

public final GenericInflater.Factory<T> getFactory()
Return the current factory (or null). This is called on each element name. If the factory returns an item, add that to the hierarchy. If it returns null, proceed to call onCreateItem(name).


setFactory

public void setFactory(GenericInflater.Factory<T> factory)
Attach a custom Factory interface for creating items while using this inflater. This must not be null, and can only be set once; after setting, you can not change the factory. This is called on each element name as the XML is parsed. If the factory returns an item, that is added to the hierarchy. If it returns null, the next factory default onCreateItem(java.lang.String, android.util.AttributeSet) method is called.

If you have an existing inflater and want to add your own factory to it, use cloneInContext(android.content.Context) to clone the existing instance and then you can use this function (once) on the returned new instance. This will merge your own factory with whatever factory the original instance is using.


inflate

public T inflate(int resource,
                 P root)
Inflate a new item hierarchy from the specified xml resource. Throws InflaterException if there is an error.

Parameters:
resource - ID for an XML resource to load (e.g., R.layout.main_page)
root - Optional parent of the generated hierarchy.
Returns:
The root of the inflated hierarchy. If root was supplied, this is the root item; otherwise it is the root of the inflated XML file.

inflate

public T inflate(XmlPullParser parser,
                 P root)
Inflate a new hierarchy from the specified xml node. Throws InflaterException if there is an error. *

Important   For performance reasons, inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use inflater with an XmlPullParser over a plain XML file at runtime.

Parameters:
parser - XML dom node containing the description of the hierarchy.
root - Optional parent of the generated hierarchy.
Returns:
The root of the inflated hierarchy. If root was supplied, this is the that; otherwise it is the root of the inflated XML file.

inflate

public T inflate(int resource,
                 P root,
                 boolean attachToRoot)
Inflate a new hierarchy from the specified xml resource. Throws InflaterException if there is an error.

Parameters:
resource - ID for an XML resource to load (e.g., R.layout.main_page)
root - Optional root to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot - Whether the inflated hierarchy should be attached to the root parameter?
Returns:
The root of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

inflate

public T inflate(XmlPullParser parser,
                 P root,
                 boolean attachToRoot)
Inflate a new hierarchy from the specified XML node. Throws InflaterException if there is an error.

Important   For performance reasons, inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use inflater with an XmlPullParser over a plain XML file at runtime.

Parameters:
parser - XML dom node containing the description of the hierarchy.
root - Optional to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot - Whether the inflated hierarchy should be attached to the root parameter?
Returns:
The root of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

createItem

public final T createItem(String name,
                          String prefix,
                          AttributeSet attrs)
                   throws ClassNotFoundException,
                          InflateException
Low-level function for instantiating by name. This attempts to instantiate class of the given name found in this inflater's ClassLoader.

There are two things that can happen in an error case: either the exception describing the error will be thrown, or a null will be returned. You must deal with both possibilities -- the former will happen the first time createItem() is called for a class of a particular name, the latter every time there-after for that class name.

Parameters:
name - The full name of the class to be instantiated.
attrs - The XML attributes supplied for this instance.
Returns:
The newly instantied item, or null.
Throws:
ClassNotFoundException
InflateException

onCreateItem

protected T onCreateItem(String name,
                         AttributeSet attrs)
                  throws ClassNotFoundException
This routine is responsible for creating the correct subclass of item given the xml element name. Override it to handle custom item objects. If you override this in your subclass be sure to call through to super.onCreateItem(name) for names you do not recognize.

Parameters:
name - The fully qualified class name of the item to be create.
attrs - An AttributeSet of attributes to apply to the item.
Returns:
The item created.
Throws:
ClassNotFoundException

onCreateCustomFromTag

protected boolean onCreateCustomFromTag(XmlPullParser parser,
                                        T parent,
                                        AttributeSet attrs)
                                 throws XmlPullParserException
Before this inflater tries to create an item from the tag, this method will be called. The parser will be pointing to the start of a tag, you must stop parsing and return when you reach the end of this element!

Parameters:
parser - XML dom node containing the description of the hierarchy.
parent - The item that should be the parent of whatever you create.
attrs - An AttributeSet of attributes to apply to the item.
Returns:
Whether you created a custom object (true), or whether this inflater should proceed to create an item.
Throws:
XmlPullParserException

onMergeRoots

protected P onMergeRoots(P givenRoot,
                         boolean attachToGivenRoot,
                         P xmlRoot)

Build 1.0_r1(from source)

Please submit a feedback, bug or feature