1 package org.robolectric.res.android; 2 3 import com.google.errorprone.annotations.FormatMethod; 4 import com.google.errorprone.annotations.FormatString; 5 import java.nio.ByteOrder; 6 7 public class Util { 8 9 public static final boolean JNI_TRUE = true; 10 public static final boolean JNI_FALSE = false; 11 12 public static final int SIZEOF_SHORT = 2; 13 public static final int SIZEOF_INT = 4; 14 public static final int SIZEOF_CPTR = 4; 15 private static boolean littleEndian = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; 16 17 private static final boolean DEBUG = false; 18 dtohs(short v)19 static short dtohs(short v) { 20 return littleEndian 21 ? v 22 : (short) ((v << 8) | (v >> 8)); 23 } 24 dtohs(char v)25 static char dtohs(char v) { 26 return littleEndian 27 ? v 28 : (char) ((v << 8) | (v >> 8)); 29 } 30 dtohl(int v)31 static int dtohl(int v) { 32 return littleEndian 33 ? v 34 : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24); 35 } 36 htods(short v)37 static short htods(short v) { 38 return littleEndian 39 ? v 40 : (short) ((v << 8) | (v >> 8)); 41 } 42 htodl(int v)43 static int htodl(int v) { 44 return littleEndian 45 ? v 46 : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24); 47 } 48 isTruthy(int i)49 public static boolean isTruthy(int i) { 50 return i != 0; 51 } 52 isTruthy(Object o)53 public static boolean isTruthy(Object o) { 54 return o != null; 55 } 56 57 @FormatMethod ALOGD(@ormatString String message, Object... args)58 static void ALOGD(@FormatString String message, Object... args) { 59 if (DEBUG) { 60 System.out.println("DEBUG: " + String.format(message, args)); 61 } 62 } 63 64 @FormatMethod ALOGW(@ormatString String message, Object... args)65 static void ALOGW(@FormatString String message, Object... args) { 66 System.out.println("WARN: " + String.format(message, args)); 67 } 68 69 @FormatMethod ALOGV(@ormatString String message, Object... args)70 public static void ALOGV(@FormatString String message, Object... args) { 71 if (DEBUG) { 72 System.out.println("VERBOSE: " + String.format(message, args)); 73 } 74 } 75 76 @FormatMethod ALOGI(@ormatString String message, Object... args)77 public static void ALOGI(@FormatString String message, Object... args) { 78 if (DEBUG) { 79 System.out.println("INFO: " + String.format(message, args)); 80 } 81 } 82 83 @FormatMethod ALOGE(@ormatString String message, Object... args)84 static void ALOGE(@FormatString String message, Object... args) { 85 System.out.println("ERROR: " + String.format(message, args)); 86 } 87 88 @FormatMethod LOG_FATAL_IF(boolean assertion, @FormatString String message, Object... args)89 static void LOG_FATAL_IF(boolean assertion, @FormatString String message, Object... args) { 90 assert !assertion : String.format(message, args); 91 } 92 ATRACE_CALL()93 static void ATRACE_CALL() { 94 } 95 ATRACE_NAME(String s)96 public static void ATRACE_NAME(String s) { 97 } 98 UNLIKELY(boolean b)99 static boolean UNLIKELY(boolean b) { 100 return b; 101 } 102 CHECK(boolean b)103 public static void CHECK(boolean b) { 104 assert b; 105 } 106 logError(String s)107 static void logError(String s) { 108 System.err.println(s); 109 } 110 logWarning(String s)111 static void logWarning(String s) { 112 System.err.println("[WARN] " + s); 113 } 114 ReadUtf16StringFromDevice(char[] src, int len )115 static String ReadUtf16StringFromDevice(char[] src, int len/*, std::string* out*/) { 116 int i = 0; 117 StringBuilder strBuf = new StringBuilder(); 118 while (src[i] != '\0' && len != 0) { 119 char c = dtohs(src[i]); 120 // utf16_to_utf8(&c, 1, buf, sizeof(buf)); 121 // out->append(buf, strlen(buf)); 122 strBuf.append(c); 123 ++i; 124 --len; 125 } 126 return strBuf.toString(); 127 } 128 } 129