1 package com.xtremelabs.robolectric.shadows; 2 3 import static com.xtremelabs.robolectric.Robolectric.shadowOf_; 4 5 import java.io.Serializable; 6 import java.util.ArrayList; 7 import java.util.Map; 8 import java.util.Set; 9 import java.util.TreeMap; 10 11 import android.os.Build; 12 import android.os.Bundle; 13 import android.os.Parcelable; 14 15 import com.xtremelabs.robolectric.Robolectric; 16 import com.xtremelabs.robolectric.internal.Implementation; 17 import com.xtremelabs.robolectric.internal.Implements; 18 19 @SuppressWarnings({ "UnusedDeclaration" }) 20 @Implements(Bundle.class) 21 public class ShadowBundle { 22 Map<String, Object> map = new TreeMap<String, Object>(); 23 __constructor__(Bundle b)24 public void __constructor__(Bundle b) { 25 putAll(b); 26 } 27 28 @Implementation clear()29 public void clear() { 30 map.clear(); 31 } 32 33 @Implementation remove(String key)34 public void remove(String key) { 35 map.remove(key); 36 } 37 38 @Implementation get(String key)39 public Object get(String key) { 40 return map.get(key); 41 } 42 43 @Implementation putString(String key, String value)44 public void putString(String key, String value) { 45 map.put(key, value); 46 } 47 48 @Implementation getString(String key)49 public String getString(String key) { 50 Object value = map.get(key); 51 return value == null || !(value instanceof String) ? null : (String) value; 52 } 53 54 @Implementation getString(String key, String defaultValue)55 public String getString(String key, String defaultValue) { 56 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { 57 throw new RuntimeException(new NoSuchMethodException("Requires API_12")); 58 } 59 Object value = map.get(key); 60 return value == null || !(value instanceof String) ? defaultValue : (String) value; 61 } 62 63 @Implementation putLong(String key, long value)64 public void putLong(String key, long value) { 65 map.put(key, value); 66 } 67 68 @Implementation getLong(String key)69 public long getLong(String key) { 70 return getLong(key, 0); 71 } 72 73 @Implementation getLong(String key, long defaultValue)74 public long getLong(String key, long defaultValue) { 75 Object value = map.get(key); 76 return value == null || !(value instanceof Long) ? defaultValue : (Long) value; 77 } 78 79 @Implementation putInt(String key, int value)80 public void putInt(String key, int value) { 81 map.put(key, value); 82 } 83 84 @Implementation getInt(String key)85 public int getInt(String key) { 86 return getInt(key, 0); 87 } 88 89 @Implementation getInt(String key, int defaultValue)90 public int getInt(String key, int defaultValue) { 91 Object value = map.get(key); 92 return value == null || !(value instanceof Integer) ? defaultValue : (Integer) value; 93 } 94 95 @Implementation putDouble(String key, double value)96 public void putDouble(String key, double value) { 97 map.put(key, value); 98 } 99 100 @Implementation getDouble(String key)101 public double getDouble(String key) { 102 return getDouble(key, 0); 103 } 104 105 @Implementation getDouble(String key, double defaultValue)106 public double getDouble(String key, double defaultValue) { 107 Object value = map.get(key); 108 return value == null || !(value instanceof Double) ? defaultValue : (Double) value; 109 } 110 111 @Implementation putByte(String key, byte value)112 public void putByte(String key, byte value) { 113 map.put(key, value); 114 } 115 116 @Implementation getByte(String key)117 public byte getByte(String key) { 118 return getByte(key, (byte) 0); 119 } 120 121 @Implementation getByte(String key, byte defaultValue)122 public Byte getByte(String key, byte defaultValue) { 123 Object value = map.get(key); 124 return value == null || !(value instanceof Byte) ? defaultValue : (Byte) value; 125 } 126 127 @Implementation putBoolean(String key, boolean value)128 public void putBoolean(String key, boolean value) { 129 map.put(key, value); 130 } 131 132 @Implementation getBoolean(String key)133 public boolean getBoolean(String key) { 134 return getBoolean(key, false); 135 } 136 137 @Implementation getBoolean(String key, boolean defaultValue)138 public boolean getBoolean(String key, boolean defaultValue) { 139 Object value = map.get(key); 140 return value == null || !(value instanceof Boolean) ? defaultValue : (Boolean) value; 141 } 142 143 @Implementation putChar(String key, char value)144 public void putChar(String key, char value) { 145 map.put(key, value); 146 } 147 148 @Implementation getChar(String key)149 public char getChar(String key) { 150 return getChar(key, (char) 0); 151 } 152 153 @Implementation getChar(String key, char defaultValue)154 public char getChar(String key, char defaultValue) { 155 Object value = map.get(key); 156 return value == null || !(value instanceof Character) ? defaultValue : (Character) value; 157 } 158 159 @Implementation putCharSequence(String key, CharSequence value)160 public void putCharSequence(String key, CharSequence value) { 161 map.put(key, value); 162 } 163 164 @Implementation getCharSequence(String key)165 public CharSequence getCharSequence(String key) { 166 Object value = map.get(key); 167 return value == null || !(value instanceof CharSequence) ? null : (CharSequence) value; 168 } 169 170 @Implementation putShort(String key, short value)171 public void putShort(String key, short value) { 172 map.put(key, value); 173 } 174 175 @Implementation getShort(String key)176 public short getShort(String key) { 177 return getShort(key, (short) 0); 178 } 179 180 @Implementation getShort(String key, short defaultValue)181 public short getShort(String key, short defaultValue) { 182 Object value = map.get(key); 183 return value == null || !(value instanceof Short) ? defaultValue : (Short) value; 184 } 185 186 @Implementation putFloat(String key, float value)187 public void putFloat(String key, float value) { 188 map.put(key, value); 189 } 190 191 @Implementation getFloat(String key)192 public float getFloat(String key) { 193 return getFloat(key, 0); 194 } 195 196 @Implementation getFloat(String key, float defaultValue)197 public float getFloat(String key, float defaultValue) { 198 Object value = map.get(key); 199 return value == null || !(value instanceof Float) ? defaultValue : (Float) value; 200 } 201 202 @Implementation putSerializable(String key, Serializable value)203 public void putSerializable(String key, Serializable value) { 204 map.put(key, value); 205 } 206 207 @Implementation getSerializable(String key)208 public Serializable getSerializable(String key) { 209 Object value = map.get(key); 210 return value == null || !(value instanceof Serializable) ? null : (Serializable) value; 211 } 212 213 @Implementation putParcelable(String key, Parcelable value)214 public void putParcelable(String key, Parcelable value) { 215 map.put(key, value); 216 } 217 218 @Implementation putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)219 public void putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) { 220 map.put(key, value); 221 } 222 223 @Implementation getParcelable(String key)224 public Parcelable getParcelable(String key) { 225 Object value = map.get(key); 226 return value == null || !(value instanceof Parcelable) ? null : (Parcelable) value; 227 } 228 229 @Implementation getParcelableArrayList(String key)230 public ArrayList<Parcelable> getParcelableArrayList(String key) { 231 Object value = map.get(key); 232 return value == null || !(value instanceof ArrayList) ? null 233 : (ArrayList<Parcelable>) value; 234 } 235 236 @Implementation getParcelableArray(String key)237 public Parcelable[] getParcelableArray(String key) { 238 Object value = map.get(key); 239 return value == null || !(value instanceof Parcelable[]) ? null : (Parcelable[]) value; 240 } 241 242 @Implementation putParcelableArray(String key, Parcelable[] value)243 public void putParcelableArray(String key, Parcelable[] value) { 244 map.put(key, value); 245 } 246 247 @Implementation putStringArrayList(String key, ArrayList<String> value)248 public void putStringArrayList(String key, ArrayList<String> value) { 249 map.put(key, value); 250 } 251 252 @Implementation getStringArrayList(String key)253 public ArrayList<String> getStringArrayList(String key) { 254 Object value = map.get(key); 255 return value == null || !(value instanceof ArrayList) ? null : (ArrayList<String>) value; 256 } 257 258 @Implementation putCharSequenceArrayList(String key, ArrayList<CharSequence> value)259 public void putCharSequenceArrayList(String key, ArrayList<CharSequence> value) { 260 map.put(key, value); 261 } 262 263 @Implementation getCharSequenceArrayList(String key)264 public ArrayList<CharSequence> getCharSequenceArrayList(String key) { 265 Object value = map.get(key); 266 return value == null || !(value instanceof ArrayList) ? null 267 : (ArrayList<CharSequence>) value; 268 } 269 270 @Implementation putIntegerArrayList(String key, ArrayList<Integer> value)271 public void putIntegerArrayList(String key, ArrayList<Integer> value) { 272 map.put(key, value); 273 } 274 275 @Implementation getIntegerArrayList(String key)276 public ArrayList<Integer> getIntegerArrayList(String key) { 277 Object value = map.get(key); 278 return value == null || !(value instanceof ArrayList) ? null : (ArrayList<Integer>) value; 279 } 280 281 @Implementation putBundle(String key, Bundle value)282 public void putBundle(String key, Bundle value) { 283 map.put(key, value); 284 } 285 286 @Implementation getBundle(String key)287 public Bundle getBundle(String key) { 288 Object value = map.get(key); 289 return value == null || !(value instanceof Bundle) ? null : (Bundle) value; 290 } 291 292 @Implementation putBooleanArray(String key, boolean[] value)293 public void putBooleanArray(String key, boolean[] value) { 294 map.put(key, value); 295 } 296 297 @Implementation getBooleanArray(String key)298 public boolean[] getBooleanArray(String key) { 299 Object value = map.get(key); 300 return value == null || !(value instanceof boolean[]) ? null : (boolean[]) value; 301 } 302 303 @Implementation putByteArray(String key, byte[] value)304 public void putByteArray(String key, byte[] value) { 305 map.put(key, value); 306 } 307 308 @Implementation getByteArray(String key)309 public byte[] getByteArray(String key) { 310 Object value = map.get(key); 311 return value == null || !(value instanceof byte[]) ? null : (byte[]) value; 312 } 313 314 @Implementation putCharArray(String key, char[] value)315 public void putCharArray(String key, char[] value) { 316 map.put(key, value); 317 } 318 319 @Implementation getCharArray(String key)320 public char[] getCharArray(String key) { 321 Object value = map.get(key); 322 return value == null || !(value instanceof char[]) ? null : (char[]) value; 323 } 324 325 @Implementation putDoubleArray(String key, double[] value)326 public void putDoubleArray(String key, double[] value) { 327 map.put(key, value); 328 } 329 330 @Implementation getDoubleArray(String key)331 public double[] getDoubleArray(String key) { 332 Object value = map.get(key); 333 return value == null || !(value instanceof double[]) ? null : (double[]) value; 334 } 335 336 @Implementation putFloatArray(String key, float[] value)337 public void putFloatArray(String key, float[] value) { 338 map.put(key, value); 339 } 340 341 @Implementation getFloatArray(String key)342 public float[] getFloatArray(String key) { 343 Object value = map.get(key); 344 return value == null || !(value instanceof float[]) ? null : (float[]) value; 345 } 346 347 @Implementation putIntArray(String key, int[] value)348 public void putIntArray(String key, int[] value) { 349 map.put(key, value); 350 } 351 352 @Implementation getIntArray(String key)353 public int[] getIntArray(String key) { 354 Object value = map.get(key); 355 return value == null || !(value instanceof int[]) ? null : (int[]) value; 356 } 357 358 @Implementation putLongArray(String key, long[] value)359 public void putLongArray(String key, long[] value) { 360 map.put(key, value); 361 } 362 363 @Implementation getLongArray(String key)364 public long[] getLongArray(String key) { 365 Object value = map.get(key); 366 return value == null || !(value instanceof long[]) ? null : (long[]) value; 367 } 368 369 @Implementation putShortArray(String key, short[] value)370 public void putShortArray(String key, short[] value) { 371 map.put(key, value); 372 } 373 374 @Implementation getShortArray(String key)375 public short[] getShortArray(String key) { 376 Object value = map.get(key); 377 return value == null || !(value instanceof short[]) ? null : (short[]) value; 378 } 379 380 @Implementation putAll(Bundle bundle)381 public void putAll(Bundle bundle) { 382 map.putAll(((ShadowBundle) Robolectric.shadowOf_(bundle)).map); 383 } 384 385 @Implementation putStringArray(String key, String[] value)386 public void putStringArray(String key, String[] value) { 387 map.put(key, value); 388 } 389 390 @Implementation getStringArray(String key)391 public String[] getStringArray(String key) { 392 Object value = map.get(key); 393 return value == null || !(value instanceof String[]) ? null : (String[]) value; 394 } 395 396 @Implementation putCharSequenceArray(String key, CharSequence[] value)397 public void putCharSequenceArray(String key, CharSequence[] value) { 398 map.put(key, value); 399 } 400 401 @Implementation getCharSequenceArray(String key)402 public CharSequence[] getCharSequenceArray(String key) { 403 Object value = map.get(key); 404 return value == null || !(value instanceof CharSequence[]) ? null : (CharSequence[]) value; 405 } 406 407 @Implementation containsKey(String key)408 public boolean containsKey(String key) { 409 return map.containsKey(key); 410 } 411 412 @Implementation isEmpty()413 public boolean isEmpty() { 414 return map.isEmpty(); 415 } 416 417 @Implementation keySet()418 public Set<String> keySet() { 419 return map.keySet(); 420 } 421 422 @Implementation size()423 public int size() { 424 return map.size(); 425 } 426 427 @Override 428 @Implementation equals(Object o)429 public boolean equals(Object o) { 430 if (o == null) 431 return false; 432 o = shadowOf_(o); 433 if (o == null) 434 return false; 435 if (this == o) 436 return true; 437 if (getClass() != o.getClass()) 438 return false; 439 440 ShadowBundle that = (ShadowBundle) o; 441 442 if (map != null ? !map.equals(that.map) : that.map != null) 443 return false; 444 445 return true; 446 } 447 448 @Override 449 @Implementation hashCode()450 public int hashCode() { 451 return map != null ? map.hashCode() : 0; 452 } 453 reset()454 public static void reset() { 455 Robolectric.Reflection.setFinalStaticField(Bundle.class, "EMPTY", new Bundle()); 456 } 457 } 458