Class SdkUtils


  • public class SdkUtils
    extends java.lang.Object
    Miscellaneous utilities used by the Android SDK tools
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String FILENAME_PREFIX
      Prefix in comments which mark the source locations for merge results
      static java.util.List<java.lang.String> IMAGE_EXTENSIONS  
    • Constructor Summary

      Constructors 
      Constructor Description
      SdkUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String camelCaseToConstantName​(java.lang.String camelCaseName)
      Translates a camel case name (e.g. xmlName) into a Java / C++ constant name (e.g.
      static java.lang.String constantNameToCamelCase​(java.lang.String constantName)
      Translates a Java / C++ constant name (e.g.
      static java.lang.String constantNameToXmlName​(java.lang.String constantName)
      Translates a Java / C++ constant name (e.g.
      static void copyXmlWithSourceReference​(java.io.File from, java.io.File to)
      Copies the given XML file to the given new path.
      static java.lang.String createPathComment​(java.io.File file, boolean includePadding)
      Creates the path comment XML string.
      static boolean endsWith​(java.lang.CharSequence sequence, int endOffset, java.lang.CharSequence suffix)
      Returns true if the given sequence ends at the given offset with the given suffix (case sensitive)
      static boolean endsWith​(java.lang.CharSequence sequence, java.lang.CharSequence suffix)
      Returns true if the given sequence ends with the given suffix (case sensitive).
      static boolean endsWithIgnoreCase​(java.lang.String string, java.lang.String suffix)
      Returns true if the given string ends with the given suffix, using a case-insensitive comparison.
      static java.net.URL fileToUrl​(java.io.File file)
      Returns the corresponding URL for the given File
      static java.lang.String fileToUrlString​(java.io.File file)
      Returns the corresponding URL string for the given File
      static java.lang.String getLineSeparator()
      Returns the default line separator to use.
      static java.lang.String getResourceFieldName​(java.lang.String resourceName)
      Get the R field name from a resource name, since AAPT will flatten the namespace, turning dots, dashes and colons into _
      static boolean hasImageExtension​(java.lang.String path)
      Returns true if the given file path points to an image file recognized by Android.
      static boolean hasUpperCaseCharacter​(java.lang.String s)
      Returns true if the given string has an upper case character.
      static double parseLocalizedDouble​(java.lang.String string)
      Returns the given localized string as a double.
      static double parseLocalizedDouble​(java.lang.String string, double defaultValue)
      Returns the given localized string as a double.
      static int parseLocalizedInt​(java.lang.String string)
      Returns the given localized string as an int.
      static int parseLocalizedInt​(java.lang.String string, int defaultValue)
      Returns the given localized string as an int.
      static boolean startsWith​(java.lang.String string, int offset, java.lang.String prefix)
      Returns true if the given string starts at the given offset with the given prefix, case insensitively.
      static boolean startsWithIgnoreCase​(java.lang.String string, java.lang.String prefix)
      Returns true if the given string starts with the given prefix, using a case-insensitive comparison.
      static java.lang.String stripWhitespace​(java.lang.String string)
      Strips the whitespace from the given string
      static java.io.File urlToFile​(java.lang.String url)
      Returns the corresponding File for the given file:// url
      static java.io.File urlToFile​(java.net.URL url)  
      static java.lang.String wrap​(java.lang.String text, int lineWidth, java.lang.String hangingIndent)
      Wraps the given text at the given line width, with an optional hanging indent.
      static java.lang.String xmlNameToConstantName​(java.lang.String xmlName)
      Translates an XML name (e.g. xml-name) into a Java / C++ constant name (e.g.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FILENAME_PREFIX

        public static final java.lang.String FILENAME_PREFIX
        Prefix in comments which mark the source locations for merge results
        See Also:
        Constant Field Values
      • IMAGE_EXTENSIONS

        public static final java.util.List<java.lang.String> IMAGE_EXTENSIONS
    • Constructor Detail

      • SdkUtils

        public SdkUtils()
    • Method Detail

      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(@NonNull
                                                 java.lang.String string,
                                                 @NonNull
                                                 java.lang.String suffix)
        Returns true if the given string ends with the given suffix, using a case-insensitive comparison.
        Parameters:
        string - the full string to be checked
        suffix - the suffix to be checked for
        Returns:
        true if the string case-insensitively ends with the given suffix
      • endsWith

        public static boolean endsWith​(@NonNull
                                       java.lang.CharSequence sequence,
                                       @NonNull
                                       java.lang.CharSequence suffix)
        Returns true if the given sequence ends with the given suffix (case sensitive).
        Parameters:
        sequence - the character sequence to be checked
        suffix - the suffix to look for
        Returns:
        true if the given sequence ends with the given suffix
      • endsWith

        public static boolean endsWith​(@NonNull
                                       java.lang.CharSequence sequence,
                                       int endOffset,
                                       @NonNull
                                       java.lang.CharSequence suffix)
        Returns true if the given sequence ends at the given offset with the given suffix (case sensitive)
        Parameters:
        sequence - the character sequence to be checked
        endOffset - the offset at which the sequence is considered to end
        suffix - the suffix to look for
        Returns:
        true if the given sequence ends with the given suffix
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(@NonNull
                                                   java.lang.String string,
                                                   @NonNull
                                                   java.lang.String prefix)
        Returns true if the given string starts with the given prefix, using a case-insensitive comparison.
        Parameters:
        string - the full string to be checked
        prefix - the prefix to be checked for
        Returns:
        true if the string case-insensitively starts with the given prefix
      • startsWith

        public static boolean startsWith​(@NonNull
                                         java.lang.String string,
                                         int offset,
                                         @NonNull
                                         java.lang.String prefix)
        Returns true if the given string starts at the given offset with the given prefix, case insensitively.
        Parameters:
        string - the full string to be checked
        offset - the offset in the string to start looking
        prefix - the prefix to be checked for
        Returns:
        true if the string case-insensitively starts at the given offset with the given prefix
      • stripWhitespace

        public static java.lang.String stripWhitespace​(@NonNull
                                                       java.lang.String string)
        Strips the whitespace from the given string
        Parameters:
        string - the string to be cleaned up
        Returns:
        the string, without whitespace
      • hasUpperCaseCharacter

        public static boolean hasUpperCaseCharacter​(@NonNull
                                                    java.lang.String s)
        Returns true if the given string has an upper case character.
        Parameters:
        s - the string to check
        Returns:
        true if it contains uppercase characters
      • getLineSeparator

        @NonNull
        public static java.lang.String getLineSeparator()
        Returns the default line separator to use.

        NOTE: If you have an associated IDocument (Eclipse), it is better to call TextUtilities#getDefaultLineDelimiter(IDocument) since that will allow (for example) editing a \r\n-delimited document on a \n-delimited platform and keep a consistent usage of delimiters in the file.

        Returns:
        the delimiter string to use
      • wrap

        @NonNull
        public static java.lang.String wrap​(@NonNull
                                            java.lang.String text,
                                            int lineWidth,
                                            @Nullable
                                            java.lang.String hangingIndent)
        Wraps the given text at the given line width, with an optional hanging indent.
        Parameters:
        text - the text to be wrapped
        lineWidth - the number of characters to wrap the text to
        hangingIndent - the hanging indent (to be used for the second and subsequent lines in each paragraph, or null if not known
        Returns:
        the string, wrapped
      • parseLocalizedInt

        public static int parseLocalizedInt​(@NonNull
                                            java.lang.String string)
                                     throws java.text.ParseException
        Returns the given localized string as an int. For example, in the US locale, "1,000", will return 1000. In the French locale, "1.000" will return 1000. It will return 0 for empty strings.

        To parse a string without catching parser exceptions, call parseLocalizedInt(String, int) instead, passing the default value to be returned if the format is invalid.

        Parameters:
        string - the string to be parsed
        Returns:
        the integer value
        Throws:
        java.text.ParseException - if the format is not correct
      • parseLocalizedInt

        public static int parseLocalizedInt​(@NonNull
                                            java.lang.String string,
                                            int defaultValue)
        Returns the given localized string as an int. For example, in the US locale, "1,000", will return 1000. In the French locale, "1.000" will return 1000. If the format is invalid, returns the supplied default value instead.
        Parameters:
        string - the string to be parsed
        defaultValue - the value to be returned if there is a parsing error
        Returns:
        the integer value
      • parseLocalizedDouble

        public static double parseLocalizedDouble​(@NonNull
                                                  java.lang.String string)
                                           throws java.text.ParseException
        Returns the given localized string as a double. For example, in the US locale, "3.14", will return 3.14. In the French locale, "3,14" will return 3.14. It will return 0 for empty strings.

        To parse a string without catching parser exceptions, call parseLocalizedDouble(String, double) instead, passing the default value to be returned if the format is invalid.

        Parameters:
        string - the string to be parsed
        Returns:
        the double value
        Throws:
        java.text.ParseException - if the format is not correct
      • parseLocalizedDouble

        public static double parseLocalizedDouble​(@NonNull
                                                  java.lang.String string,
                                                  double defaultValue)
        Returns the given localized string as a double. For example, in the US locale, "3.14", will return 3.14. In the French locale, "3,14" will return 3.14. If the format is invalid, returns the supplied default value instead.
        Parameters:
        string - the string to be parsed
        defaultValue - the value to be returned if there is a parsing error
        Returns:
        the double value
      • urlToFile

        @NonNull
        public static java.io.File urlToFile​(@NonNull
                                             java.lang.String url)
                                      throws java.net.MalformedURLException
        Returns the corresponding File for the given file:// url
        Parameters:
        url - the URL string, e.g. file://foo/bar
        Returns:
        the corresponding File (which may or may not exist)
        Throws:
        java.net.MalformedURLException - if the URL string is malformed or is not a file: URL
      • urlToFile

        @NonNull
        public static java.io.File urlToFile​(@NonNull
                                             java.net.URL url)
                                      throws java.net.MalformedURLException
        Throws:
        java.net.MalformedURLException
      • fileToUrlString

        public static java.lang.String fileToUrlString​(@NonNull
                                                       java.io.File file)
                                                throws java.net.MalformedURLException
        Returns the corresponding URL string for the given File
        Parameters:
        file - the file to look up the URL for
        Returns:
        the corresponding URL
        Throws:
        java.net.MalformedURLException - in very unexpected cases
      • fileToUrl

        public static java.net.URL fileToUrl​(@NonNull
                                             java.io.File file)
                                      throws java.net.MalformedURLException
        Returns the corresponding URL for the given File
        Parameters:
        file - the file to look up the URL for
        Returns:
        the corresponding URL
        Throws:
        java.net.MalformedURLException - in very unexpected cases
      • createPathComment

        public static java.lang.String createPathComment​(@NonNull
                                                         java.io.File file,
                                                         boolean includePadding)
                                                  throws java.net.MalformedURLException
        Creates the path comment XML string. Note that it does not escape characters such as & and <; those are expected to be escaped by the caller (for example, handled by a call to Document.createComment(String))
        Parameters:
        file - the file to create a path comment for
        includePadding - whether to include padding. The final comment recognized by error recognizers expect padding between the <!-- and the start marker (From:); you can disable padding if the caller already is in a context where the padding has been added.
        Returns:
        the corresponding XML contents of the string
        Throws:
        java.net.MalformedURLException
      • copyXmlWithSourceReference

        public static void copyXmlWithSourceReference​(@NonNull
                                                      java.io.File from,
                                                      @NonNull
                                                      java.io.File to)
                                               throws java.io.IOException
        Copies the given XML file to the given new path. It also inserts a comment at the end of the file which points to the original source location. This is intended for use with error parsers which can rewrite for example AAPT error messages in say layout or manifest files, which occur in the merged (copied) output, and present it as an error pointing to one of the user's original source files.
        Throws:
        java.io.IOException
      • xmlNameToConstantName

        public static java.lang.String xmlNameToConstantName​(java.lang.String xmlName)
        Translates an XML name (e.g. xml-name) into a Java / C++ constant name (e.g. XML_NAME)
        Parameters:
        xmlName - the hyphen separated lower case xml name.
        Returns:
        the equivalent constant name.
      • camelCaseToConstantName

        public static java.lang.String camelCaseToConstantName​(java.lang.String camelCaseName)
        Translates a camel case name (e.g. xmlName) into a Java / C++ constant name (e.g. XML_NAME)
        Parameters:
        camelCaseName - the camel case name.
        Returns:
        the equivalent constant name.
      • constantNameToCamelCase

        public static java.lang.String constantNameToCamelCase​(java.lang.String constantName)
        Translates a Java / C++ constant name (e.g. XML_NAME) into camel case name (e.g. xmlName)
        Parameters:
        constantName - the constant name.
        Returns:
        the equivalent camel case name.
      • constantNameToXmlName

        public static java.lang.String constantNameToXmlName​(java.lang.String constantName)
        Translates a Java / C++ constant name (e.g. XML_NAME) into a XML case name (e.g. xml-name)
        Parameters:
        constantName - the constant name.
        Returns:
        the equivalent XML name.
      • getResourceFieldName

        @NonNull
        public static java.lang.String getResourceFieldName​(@NonNull
                                                            java.lang.String resourceName)
        Get the R field name from a resource name, since AAPT will flatten the namespace, turning dots, dashes and colons into _
        Parameters:
        resourceName - the name to convert
        Returns:
        the corresponding R field name
      • hasImageExtension

        public static boolean hasImageExtension​(java.lang.String path)
        Returns true if the given file path points to an image file recognized by Android. See http://developer.android.com/guide/appendix/media-formats.html for details.
        Parameters:
        path - the filename to be tested
        Returns:
        true if the file represents an image file