| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os.health; |
| 18 |
|
| 19 |
import android.annotation.TestApi; |
| 20 |
|
| 21 |
import java.lang.annotation.ElementType; |
| 22 |
import java.lang.annotation.Retention; |
| 23 |
import java.lang.annotation.RetentionPolicy; |
| 24 |
import java.lang.annotation.Target; |
| 25 |
import java.lang.reflect.Field; |
| 26 |
import java.util.Arrays; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
@TestApi |
| 34 |
public class HealthKeys { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static final int UNKNOWN_KEY = 0; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static final int BASE_UID = 10000; |
| 48 |
public static final int BASE_PID = 20000; |
| 49 |
public static final int BASE_PROCESS = 30000; |
| 50 |
public static final int BASE_PACKAGE = 40000; |
| 51 |
public static final int BASE_SERVICE = 50000; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final int TYPE_TIMER = 0; |
| 57 |
public static final int TYPE_MEASUREMENT = 1; |
| 58 |
public static final int TYPE_STATS = 2; |
| 59 |
public static final int TYPE_TIMERS = 3; |
| 60 |
public static final int TYPE_MEASUREMENTS = 4; |
| 61 |
|
| 62 |
public static final int TYPE_COUNT = 5; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Retention(RetentionPolicy.RUNTIME) |
| 69 |
@Target({ElementType.FIELD}) |
| 70 |
public @interface Constant { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int type(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@TestApi |
| 84 |
public static class Constants { |
| 85 |
private final String mDataType; |
| 86 |
private final int[][] mKeys = new int[TYPE_COUNT][]; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public Constants(Class clazz) { |
| 93 |
|
| 94 |
mDataType = clazz.getSimpleName(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
final Field[] fields = clazz.getDeclaredFields(); |
| 99 |
final Class<Constant> annotationClass = Constant.class; |
| 100 |
|
| 101 |
final int N = fields.length; |
| 102 |
|
| 103 |
final SortedIntArray[] keys = new SortedIntArray[mKeys.length]; |
| 104 |
for (int i=0; i<keys.length; i++) { |
| 105 |
keys[i] = new SortedIntArray(N); |
| 106 |
} |
| 107 |
|
| 108 |
for (int i=0; i<N; i++) { |
| 109 |
final Field field = fields[i]; |
| 110 |
final Constant constant = field.getAnnotation(annotationClass); |
| 111 |
if (constant != null) { |
| 112 |
final int type = constant.type(); |
| 113 |
if (type >= keys.length) { |
| 114 |
throw new RuntimeException("Unknown Constant type " + type |
| 115 |
+ " on " + field); |
| 116 |
} |
| 117 |
try { |
| 118 |
keys[type].addValue(field.getInt(null)); |
| 119 |
} catch (IllegalAccessException ex) { |
| 120 |
throw new RuntimeException("Can't read constant value type=" + type |
| 121 |
+ " field=" + field, ex); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
for (int i=0; i<keys.length; i++) { |
| 127 |
mKeys[i] = keys[i].getArray(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public String getDataType() { |
| 136 |
return mDataType; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public int getSize(int type) { |
| 149 |
return mKeys[type].length; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int getIndex(int type, int key) { |
| 163 |
final int index = Arrays.binarySearch(mKeys[type], key); |
| 164 |
if (index >= 0) { |
| 165 |
return index; |
| 166 |
} else { |
| 167 |
throw new RuntimeException("Unknown Constant " + key + " (of type " |
| 168 |
+ type + " )"); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public int[] getKeys(int type) { |
| 176 |
return mKeys[type]; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private static class SortedIntArray { |
| 184 |
int mCount; |
| 185 |
int[] mArray; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
SortedIntArray(int maxCount) { |
| 191 |
mArray = new int[maxCount]; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void addValue(int value) { |
| 198 |
mArray[mCount++] = value; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
int[] getArray() { |
| 206 |
if (mCount == mArray.length) { |
| 207 |
Arrays.sort(mArray); |
| 208 |
return mArray; |
| 209 |
} else { |
| 210 |
final int[] result = new int[mCount]; |
| 211 |
System.arraycopy(mArray, 0, result, 0, mCount); |
| 212 |
Arrays.sort(result); |
| 213 |
return result; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|