• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res.android;
2 
3 import java.nio.ByteOrder;
4 
5 public class Util {
6 
7   public static final boolean JNI_TRUE = true;
8   public static final boolean JNI_FALSE = false;
9 
10   public static final int SIZEOF_SHORT = 2;
11   public static final int SIZEOF_INT = 4;
12   public static final int SIZEOF_CPTR = 4;
13   private static boolean littleEndian = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN;
14 
15   private static final boolean DEBUG = false;
16 
dtohs(short v)17   static short dtohs(short v) {
18     return littleEndian
19         ? v
20         : (short) ((v << 8) | (v >> 8));
21   }
22 
dtohs(char v)23   static char dtohs(char v) {
24     return littleEndian
25         ? v
26         : (char) ((v << 8) | (v >> 8));
27   }
28 
dtohl(int v)29   static int dtohl(int v) {
30     return littleEndian
31         ? v
32         : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24);
33   }
34 
htods(short v)35   static short htods(short v) {
36     return littleEndian
37         ? v
38         : (short) ((v << 8) | (v >> 8));
39   }
40 
htodl(int v)41   static int htodl(int v) {
42     return littleEndian
43         ? v
44         : (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24);
45   }
46 
isTruthy(int i)47   public static boolean isTruthy(int i) {
48     return i != 0;
49   }
50 
isTruthy(Object o)51   public static boolean isTruthy(Object o) {
52     return o != null;
53   }
54 
ALOGD(String message, Object... args)55   static void ALOGD(String message, Object... args) {
56     if (DEBUG) {
57       System.out.println("DEBUG: " + String.format(message, args));
58     }
59   }
60 
ALOGW(String message, Object... args)61   static void ALOGW(String message, Object... args) {
62     System.out.println("WARN: " + String.format(message, args));
63   }
64 
ALOGV(String message, Object... args)65   public static void ALOGV(String message, Object... args) {
66     if (DEBUG) {
67       System.out.println("VERBOSE: " + String.format(message, args));
68     }
69   }
70 
ALOGI(String message, Object... args)71   public static void ALOGI(String message, Object... args) {
72     if (DEBUG) {
73       System.out.println("INFO: " + String.format(message, args));
74     }
75   }
76 
ALOGE(String message, Object... args)77   static void ALOGE(String message, Object... args) {
78     System.out.println("ERROR: " + String.format(message, args));
79   }
80 
LOG_FATAL_IF(boolean assertion, String message, Object... args)81   static void LOG_FATAL_IF(boolean assertion, String message, Object... args) {
82     assert !assertion : String.format(message, args);
83   }
84 
ATRACE_CALL()85   static void ATRACE_CALL() {
86   }
87 
ATRACE_NAME(String s)88   public static void ATRACE_NAME(String s) {
89   }
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     char[] buf = new char[5];
109     int i = 0;
110     StringBuilder strBuf = new StringBuilder();
111     while (src[i] != '\0' && len != 0) {
112       char c = dtohs(src[i]);
113       // utf16_to_utf8(&c, 1, buf, sizeof(buf));
114       // out->append(buf, strlen(buf));
115       strBuf.append(c);
116       ++i;
117       --len;
118     }
119     return strBuf.toString();
120   }
121 }
122