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 ? v : (short) ((v << 8) | (v >> 8)); 21 } 22 dtohs(char v)23 static char dtohs(char v) { 24 return littleEndian ? v : (char) ((v << 8) | (v >> 8)); 25 } 26 dtohl(int v)27 static int dtohl(int v) { 28 return littleEndian 29 ? v 30 : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24); 31 } 32 htods(short v)33 static short htods(short v) { 34 return littleEndian ? v : (short) ((v << 8) | (v >> 8)); 35 } 36 htodl(int v)37 static int htodl(int v) { 38 return littleEndian 39 ? v 40 : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24); 41 } 42 isTruthy(int i)43 public static boolean isTruthy(int i) { 44 return i != 0; 45 } 46 isTruthy(Object o)47 public static boolean isTruthy(Object o) { 48 return o != null; 49 } 50 51 @FormatMethod ALOGD(@ormatString String message, Object... args)52 static void ALOGD(@FormatString String message, Object... args) { 53 if (DEBUG) { 54 System.out.println("DEBUG: " + String.format(message, args)); 55 } 56 } 57 58 @FormatMethod ALOGW(@ormatString String message, Object... args)59 static void ALOGW(@FormatString String message, Object... args) { 60 System.out.println("WARN: " + String.format(message, args)); 61 } 62 63 @FormatMethod ALOGV(@ormatString String message, Object... args)64 public static void ALOGV(@FormatString String message, Object... args) { 65 if (DEBUG) { 66 System.out.println("VERBOSE: " + String.format(message, args)); 67 } 68 } 69 70 @FormatMethod ALOGI(@ormatString String message, Object... args)71 public static void ALOGI(@FormatString String message, Object... args) { 72 if (DEBUG) { 73 System.out.println("INFO: " + String.format(message, args)); 74 } 75 } 76 77 @FormatMethod ALOGE(@ormatString String message, Object... args)78 static void ALOGE(@FormatString String message, Object... args) { 79 System.out.println("ERROR: " + String.format(message, args)); 80 } 81 82 @FormatMethod LOG_FATAL_IF(boolean assertion, @FormatString String message, Object... args)83 static void LOG_FATAL_IF(boolean assertion, @FormatString String message, Object... args) { 84 assert !assertion : String.format(message, args); 85 } 86 ATRACE_CALL()87 static void ATRACE_CALL() {} 88 ATRACE_NAME(String s)89 public static void ATRACE_NAME(String s) {} 90 UNLIKELY(boolean b)91 static boolean UNLIKELY(boolean b) { 92 return b; 93 } 94 CHECK(boolean b)95 public static void CHECK(boolean b) { 96 assert b; 97 } 98 logError(String s)99 static void logError(String s) { 100 System.err.println(s); 101 } 102 logWarning(String s)103 static void logWarning(String s) { 104 System.err.println("[WARN] " + s); 105 } 106 ReadUtf16StringFromDevice(char[] src, int len )107 static String ReadUtf16StringFromDevice(char[] src, int len /*, std::string* out*/) { 108 int i = 0; 109 StringBuilder strBuf = new StringBuilder(); 110 while (src[i] != '\0' && len != 0) { 111 char c = dtohs(src[i]); 112 // utf16_to_utf8(&c, 1, buf, sizeof(buf)); 113 // out->append(buf, strlen(buf)); 114 strBuf.append(c); 115 ++i; 116 --len; 117 } 118 return strBuf.toString(); 119 } 120 } 121