FileDocCategorySizeDatePackage
Utils.javaAPI DocAndroid 5.1 API11063Thu Mar 12 22:22:42 GMT 2015com.android.gallery3d.common

Utils

public class Utils extends Object

Fields Summary
private static final String
TAG
private static final String
DEBUG_TAG
private static final long
POLY64REV
private static final long
INITIALCRC
private static long[]
sCrcTable
private static final boolean
IS_DEBUG_BUILD
private static final String
MASK_STRING
Constructors Summary
Methods Summary
public static voidassertTrue(boolean cond)


    // Throws AssertionError if the input is false.
         
        if (!cond) {
            throw new AssertionError();
        }
    
public static intceilLog2(float value)

        int i;
        for (i = 0; i < 31; i++) {
            if ((1 << i) >= value) break;
        }
        return i;
    
public static TcheckNotNull(T object)

        if (object == null) throw new NullPointerException();
        return object;
    
public static intclamp(int x, int min, int max)

        if (x > max) return max;
        if (x < min) return min;
        return x;
    
public static floatclamp(float x, float min, float max)

        if (x > max) return max;
        if (x < min) return min;
        return x;
    
public static longclamp(long x, long min, long max)

        if (x > max) return max;
        if (x < min) return min;
        return x;
    
public static voidcloseSilently(java.io.Closeable c)

        if (c == null) return;
        try {
            c.close();
        } catch (IOException t) {
            Log.w(TAG, "close fail ", t);
        }
    
public static voidcloseSilently(android.os.ParcelFileDescriptor fd)

        try {
            if (fd != null) fd.close();
        } catch (Throwable t) {
            Log.w(TAG, "fail to close", t);
        }
    
public static voidcloseSilently(android.database.Cursor cursor)

        try {
            if (cursor != null) cursor.close();
        } catch (Throwable t) {
            Log.w(TAG, "fail to close", t);
        }
    
public static intcompare(long a, long b)

        return a < b ? -1 : a == b ? 0 : 1;
    
public static java.lang.String[]copyOf(java.lang.String[] source, int newSize)

        String[] result = new String[newSize];
        newSize = Math.min(source.length, newSize);
        System.arraycopy(source, 0, result, 0, newSize);
        return result;
    
public static final longcrc64Long(java.lang.String in)
A function thats returns a 64-bit crc for string

param
in input string
return
a 64-bit crc value

        if (in == null || in.length() == 0) {
            return 0;
        }
        return crc64Long(getBytes(in));
    
public static final longcrc64Long(byte[] buffer)

        // http://bioinf.cs.ucl.ac.uk/downloads/crc64/crc64.c
        long part;
        for (int i = 0; i < 256; i++) {
            part = i;
            for (int j = 0; j < 8; j++) {
                long x = ((int) part & 1) != 0 ? POLY64REV : 0;
                part = (part >> 1) ^ x;
            }
            sCrcTable[i] = part;
        }
    
        long crc = INITIALCRC;
        for (int k = 0, n = buffer.length; k < n; ++k) {
            crc = sCrcTable[(((int) crc) ^ buffer[k]) & 0xff] ^ (crc >> 8);
        }
        return crc;
    
public static voiddebug(java.lang.String message, java.lang.Object args)

        Log.v(DEBUG_TAG, String.format(message, args));
    
public static java.lang.StringensureNotNull(java.lang.String value)

        return value == null ? "" : value;
    
public static booleanequals(java.lang.Object a, java.lang.Object b)

        return (a == b) || (a == null ? false : a.equals(b));
    
public static java.lang.StringescapeXml(java.lang.String s)

return
String with special XML characters escaped.

        StringBuilder sb = new StringBuilder();
        for (int i = 0, len = s.length(); i < len; ++i) {
            char c = s.charAt(i);
            switch (c) {
                case '<":  sb.append("<"); break;
                case '>":  sb.append(">"); break;
                case '\"": sb.append("""); break;
                case '\'": sb.append("'"); break;
                case '&":  sb.append("&"); break;
                default: sb.append(c);
            }
        }
        return sb.toString();
    
public static voidfail(java.lang.String message, java.lang.Object args)

        throw new AssertionError(
                args.length == 0 ? message : String.format(message, args));
    
public static intfloorLog2(float value)

        int i;
        for (i = 0; i < 31; i++) {
            if ((1 << i) > value) break;
        }
        return i - 1;
    
public static byte[]getBytes(java.lang.String in)

        byte[] result = new byte[in.length() * 2];
        int output = 0;
        for (char ch : in.toCharArray()) {
            result[output++] = (byte) (ch & 0xFF);
            result[output++] = (byte) (ch >> 8);
        }
        return result;
    
public static java.lang.StringgetUserAgent(android.content.Context context)

        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            throw new IllegalStateException("getPackageInfo failed");
        }
        return String.format("%s/%s; %s/%s/%s/%s; %s/%s/%s",
                packageInfo.packageName,
                packageInfo.versionName,
                Build.BRAND,
                Build.DEVICE,
                Build.MODEL,
                Build.ID,
                Build.VERSION.SDK_INT,
                Build.VERSION.RELEASE,
                Build.VERSION.INCREMENTAL);
    
public static booleanhandleInterrruptedException(java.lang.Throwable e)

        // A helper to deal with the interrupt exception
        // If an interrupt detected, we will setup the bit again.
        if (e instanceof InterruptedIOException
                || e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
            return true;
        }
        return false;
    
public static floatinterpolateAngle(float source, float target, float progress)

        // interpolate the angle from source to target
        // We make the difference in the range of [-179, 180], this is the
        // shortest path to change source to target.
        float diff = target - source;
        if (diff < 0) diff += 360f;
        if (diff > 180) diff -= 360f;

        float result = source + diff * progress;
        return result < 0 ? result + 360f : result;
    
public static floatinterpolateScale(float source, float target, float progress)

        return source + progress * (target - source);
    
public static booleanisNullOrEmpty(java.lang.String exifMake)

        return TextUtils.isEmpty(exifMake);
    
public static booleanisOpaque(int color)

        return color >>> 24 == 0xFF;
    
public static java.lang.StringmaskDebugInfo(java.lang.Object info)

        if (info == null) return null;
        String s = info.toString();
        int length = Math.min(s.length(), MASK_STRING.length());
        return IS_DEBUG_BUILD ? s : MASK_STRING.substring(0, length);
    
public static intnextPowerOf2(int n)

        if (n <= 0 || n > (1 << 30)) throw new IllegalArgumentException("n is invalid: " + n);
        n -= 1;
        n |= n >> 16;
        n |= n >> 8;
        n |= n >> 4;
        n |= n >> 2;
        n |= n >> 1;
        return n + 1;
    
public static floatparseFloatSafely(java.lang.String content, float defaultValue)

        if (content == null) return defaultValue;
        try {
            return Float.parseFloat(content);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    
public static intparseIntSafely(java.lang.String content, int defaultValue)

        if (content == null) return defaultValue;
        try {
            return Integer.parseInt(content);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    
public static intprevPowerOf2(int n)

        if (n <= 0) throw new IllegalArgumentException();
        return Integer.highestOneBit(n);
    
public static voidswap(int[] array, int i, int j)

        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    
public static voidwaitWithoutInterrupt(java.lang.Object object)

        try {
            object.wait();
        } catch (InterruptedException e) {
            Log.w(TAG, "unexpected interrupt: " + object);
        }