Class Configuration

  • All Implemented Interfaces:
    Parcelable, Comparable<Configuration>

    public final class Configuration
    extends Object
    implements Parcelable, Comparable<Configuration>
    This class describes all device configuration information that can impact the resources the application retrieves. This includes both user-specified configuration options (locale and scaling) as well as device configurations (such as input modes, screen size and screen orientation).

    You can acquire this object from Resources, using Resources.getConfiguration(). Thus, from an activity, you can get it by chaining the request with android.app.Activity#getResources:

    Configuration config = getResources().getConfiguration();
    • Constructor Detail

      • Configuration

        public Configuration()
        Construct an invalid Configuration. You must call setToDefaults() for this object to be valid.
      • Configuration

        public Configuration​(Configuration o)
        Makes a deep copy suitable for modification.
    • Method Detail

      • resetScreenLayout

        public static int resetScreenLayout​(int curLayout)
      • reduceScreenLayout

        public static int reduceScreenLayout​(int curLayout,
                                             int longSizeDp,
                                             int shortSizeDp)
      • toString

        public String toString()
        Description copied from class: Object
        Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
           getClass().getName() + '@' + Integer.toHexString(hashCode())

        See Writing a useful toString method if you intend implementing your own toString method.

        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.
      • setToDefaults

        public void setToDefaults()
        Set this object to the system defaults.
      • makeDefault

        @Deprecated
        public void makeDefault()
        Deprecated.
      • updateFrom

        public int updateFrom​(Configuration delta)
        Copy the fields from delta into this Configuration object, keeping track of which ones have changed. Any undefined fields in delta are ignored and not copied in to the current Configuration.
        Returns:
        Returns a bit mask of the changed fields, as per diff(android.content.res.Configuration).
      • diff

        public int diff​(Configuration delta)
        Return a bit mask of the differences between this Configuration object and the given one. Does not change the values of either. Any undefined fields in delta are ignored.
        Returns:
        Returns a bit mask indicating which configuration values has changed, containing any combination of PackageManager.ActivityInfo.CONFIG_FONT_SCALE, PackageManager.ActivityInfo.CONFIG_MCC, PackageManager.ActivityInfo.CONFIG_MNC, PackageManager.ActivityInfo.CONFIG_LOCALE, PackageManager.ActivityInfo.CONFIG_TOUCHSCREEN, PackageManager.ActivityInfo.CONFIG_KEYBOARD, PackageManager.ActivityInfo.CONFIG_NAVIGATION, PackageManager.ActivityInfo.CONFIG_ORIENTATION, PackageManager.ActivityInfo.CONFIG_SCREEN_LAYOUT, or PackageManager.ActivityInfo.CONFIG_SCREEN_SIZE, or PackageManager.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE. PackageManager.ActivityInfo.CONFIG_LAYOUT_DIRECTION.
      • needNewResources

        public static boolean needNewResources​(int configChanges,
                                               int interestingChanges)
        Determine if a new resource needs to be loaded from the bit set of configuration changes returned by updateFrom(Configuration).
        Parameters:
        configChanges - The mask of changes configurations as returned by updateFrom(Configuration).
        interestingChanges - The configuration changes that the resource can handled, as given in TypedValue.changingConfigurations.
        Returns:
        Return true if the resource needs to be loaded, else false.
      • isOtherSeqNewer

        public boolean isOtherSeqNewer​(Configuration other)
      • describeContents

        public int describeContents()
        Parcelable methods
        Specified by:
        describeContents in interface Parcelable
        Returns:
        a bitmask indicating the set of special object types marshalled by the Parcelable.
      • writeToParcel

        public void writeToParcel​(Parcel dest,
                                  int flags)
        Description copied from interface: Parcelable
        Flatten this object in to a Parcel.
        Specified by:
        writeToParcel in interface Parcelable
        Parameters:
        dest - The Parcel in which the object should be written.
        flags - Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE.
      • readFromParcel

        public void readFromParcel​(Parcel source)
      • compareTo

        public int compareTo​(Configuration that)
        Description copied from interface: Comparable
        Compares this object to the specified object to determine their relative order.
        Specified by:
        compareTo in interface Comparable<Configuration>
        Parameters:
        that - the object to compare to this instance.
        Returns:
        a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.
      • equals

        public boolean equals​(Object that)
        Description copied from class: Object
        Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

        The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

        The general contract for the equals and Object.hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

        Overrides:
        equals in class Object
        Parameters:
        that - the object to compare this instance with.
        Returns:
        true if the specified object is equal to this Object; false otherwise.
        See Also:
        Object.hashCode()
      • hashCode

        public int hashCode()
        Description copied from class: Object
        Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

        Note that hash values must not change over time unless information used in equals comparisons also changes.

        See Writing a correct hashCode method if you intend implementing your own hashCode method.

        Overrides:
        hashCode in class Object
        Returns:
        this object's hash code.
        See Also:
        Object.equals(java.lang.Object)
      • setLocale

        public void setLocale​(Locale loc)
        Set the locale. This is the preferred way for setting up the locale (instead of using the direct accessor). This will also set the layout direction according to the locale.
        Parameters:
        loc - The locale. Can be null.
      • isScreenRound

        public boolean isScreenRound()
        Return whether the screen has a round shape. Apps may choose to change styling based on this property, such as the alignment or layout of text or informational icons.
        Returns:
        true if the screen is rounded, false otherwise
      • localeToResourceQualifier

        public static String localeToResourceQualifier​(Locale locale)
      • resourceQualifierString

        public static String resourceQualifierString​(Configuration config)
        Returns a string representation of the configuration that can be parsed by build tools (like AAPT).
      • generateDelta

        public static Configuration generateDelta​(Configuration base,
                                                  Configuration change)
        Generate a delta Configuration between base and change. The resulting delta can be used with updateFrom(Configuration).

        Caveat: If the any of the Configuration's members becomes undefined, then updateFrom(Configuration) will treat it as a no-op and not update that member. This is fine for device configurations as no member is ever undefined.

      • readXmlAttrs

        public static void readXmlAttrs​(XmlPullParser parser,
                                        Configuration configOut)
                                 throws XmlPullParserException,
                                        IOException
        Reads the attributes corresponding to Configuration member fields from the Xml parser. The parser is expected to be on a tag which has Configuration attributes.
        Parameters:
        parser - The Xml parser from which to read attributes.
        configOut - The Configuration to populate from the Xml attributes.
        Throws:
        XmlPullParserException
        IOException
      • writeXmlAttrs

        public static void writeXmlAttrs​(XmlSerializer xml,
                                         Configuration config)
                                  throws IOException
        Writes the Configuration's member fields as attributes into the XmlSerializer. The serializer is expected to have already started a tag so that attributes can be immediately written.
        Parameters:
        xml - The serializer to which to write the attributes.
        config - The Configuration whose member fields to write.
        Throws:
        IOException