summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/GeckoBundle.java
blob: 4ed37872f26695056d80287536e87dafa7347ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.util;

import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.collection.SimpleArrayMap;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
import org.mozilla.gecko.annotation.WrapForJNI;

/**
 * A lighter-weight version of Bundle that adds support for type coercion (e.g. int to double) in
 * order to better cooperate with JS objects.
 */
@RobocopTarget
public final class GeckoBundle implements Parcelable {
  private static final String LOGTAG = "GeckoBundle";
  private static final boolean DEBUG = false;

  @WrapForJNI(calledFrom = "gecko")
  private static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];

  private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  private static final int[] EMPTY_INT_ARRAY = new int[0];
  private static final long[] EMPTY_LONG_ARRAY = new long[0];
  private static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
  private static final String[] EMPTY_STRING_ARRAY = new String[0];
  private static final GeckoBundle[] EMPTY_BUNDLE_ARRAY = new GeckoBundle[0];

  private SimpleArrayMap<String, Object> mMap;

  /** Construct an empty GeckoBundle. */
  public GeckoBundle() {
    mMap = new SimpleArrayMap<>();
  }

  /**
   * Construct an empty GeckoBundle with specific capacity.
   *
   * @param capacity Initial capacity.
   */
  public GeckoBundle(final int capacity) {
    mMap = new SimpleArrayMap<>(capacity);
  }

  /**
   * Construct a copy of another GeckoBundle.
   *
   * @param bundle GeckoBundle to copy from.
   */
  public GeckoBundle(final GeckoBundle bundle) {
    mMap = new SimpleArrayMap<>(bundle.mMap);
  }

  @WrapForJNI(calledFrom = "gecko")
  private GeckoBundle(final String[] keys, final Object[] values) {
    final int len = keys.length;
    mMap = new SimpleArrayMap<>(len);
    for (int i = 0; i < len; i++) {
      mMap.put(keys[i], values[i]);
    }
  }

  /** Clear all mappings. */
  public void clear() {
    mMap.clear();
  }

  /**
   * Returns whether a mapping exists. Null String, Bundle, or arrays are treated as nonexistent.
   *
   * @param key Key to look for.
   * @return True if the specified key exists and the value is not null.
   */
  public boolean containsKey(final String key) {
    return mMap.get(key) != null;
  }

  /**
   * Returns the value associated with a mapping as an Object.
   *
   * @param key Key to look for.
   * @return Mapping value or null if the mapping does not exist.
   */
  public Object get(final String key) {
    return mMap.get(key);
  }

  /**
   * Returns the value associated with a boolean mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Boolean value
   */
  public boolean getBoolean(final String key, final boolean defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : (Boolean) value;
  }

  /**
   * Returns the value associated with a boolean mapping, or false if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Boolean value
   */
  public boolean getBoolean(final String key) {
    return getBoolean(key, false);
  }

  /**
   * Returns the value associated with a Boolean mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Boolean value
   */
  public Boolean getBooleanObject(final String key, final Boolean defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : (Boolean) value;
  }

  /**
   * Returns the value associated with a Boolean mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Boolean value
   */
  public Boolean getBooleanObject(final String key) {
    return getBooleanObject(key, null);
  }

  /**
   * Returns the value associated with a boolean array mapping, or null if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @return Boolean array value
   */
  public boolean[] getBooleanArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0 ? EMPTY_BOOLEAN_ARRAY : (boolean[]) value;
  }

  /**
   * Returns the value associated with a double mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Double value
   */
  public double getDouble(final String key, final double defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : ((Number) value).doubleValue();
  }

  /**
   * Returns the value associated with a double mapping, or 0.0 if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Double value
   */
  public double getDouble(final String key) {
    return getDouble(key, 0.0);
  }

  private static double[] getDoubleArray(final int[] array) {
    final int len = array.length;
    final double[] ret = new double[len];
    for (int i = 0; i < len; i++) {
      ret[i] = (double) array[i];
    }
    return ret;
  }

  /**
   * Returns the value associated with a double array mapping, or null if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @return Double array value
   */
  public double[] getDoubleArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0
            ? EMPTY_DOUBLE_ARRAY
            : value instanceof int[] ? getDoubleArray((int[]) value) : (double[]) value;
  }

  /**
   * Returns the value associated with an int mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Int value
   */
  public int getInt(final String key, final int defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : ((Number) value).intValue();
  }

  /**
   * Returns the value associated with an int mapping, or 0 if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Int value
   */
  public int getInt(final String key) {
    return getInt(key, 0);
  }

  /**
   * Returns the value associated with an Integer mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Int value
   */
  public Integer getInteger(final String key, final Integer defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : ((Integer) value);
  }

  /**
   * Returns the value associated with an Integer mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Int value
   */
  public Integer getInteger(final String key) {
    return getInteger(key, null);
  }

  private static int[] getIntArray(final double[] array) {
    final int len = array.length;
    final int[] ret = new int[len];
    for (int i = 0; i < len; i++) {
      ret[i] = (int) array[i];
    }
    return ret;
  }

  /**
   * Returns the value associated with an int array mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Int array value
   */
  public int[] getIntArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0
            ? EMPTY_INT_ARRAY
            : value instanceof double[] ? getIntArray((double[]) value) : (int[]) value;
  }

  /**
   * Returns the value associated with an byte array mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Byte array value
   */
  public byte[] getByteArray(final String key) {
    final Object value = mMap.get(key);
    return value == null ? null : Array.getLength(value) == 0 ? EMPTY_BYTE_ARRAY : (byte[]) value;
  }

  /**
   * Returns the value associated with an int/double mapping as a long value, or defaultValue if the
   * mapping does not exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping does not exist.
   * @return Long value
   */
  public long getLong(final String key, final long defaultValue) {
    final Object value = mMap.get(key);
    return value == null ? defaultValue : ((Number) value).longValue();
  }

  /**
   * Returns the value associated with an int/double mapping as a long value, or 0 if the mapping
   * does not exist.
   *
   * @param key Key to look for.
   * @return Long value
   */
  public long getLong(final String key) {
    return getLong(key, 0L);
  }

  private static long[] getLongArray(final Object array) {
    final int len = Array.getLength(array);
    final long[] ret = new long[len];
    for (int i = 0; i < len; i++) {
      ret[i] = ((Number) Array.get(array, i)).longValue();
    }
    return ret;
  }

  /**
   * Returns the value associated with an int/double array mapping as a long array, or null if the
   * mapping does not exist.
   *
   * @param key Key to look for.
   * @return Long array value
   */
  public long[] getLongArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0 ? EMPTY_LONG_ARRAY : getLongArray(value);
  }

  /**
   * Returns the value associated with a String mapping, or defaultValue if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @param defaultValue Value to return if mapping value is null or mapping does not exist.
   * @return String value
   */
  public String getString(final String key, final String defaultValue) {
    // If the key maps to null, technically we should return null because the mapping
    // exists and null is a valid string value. However, people expect the default
    // value to be returned instead, so we make an exception to return the default value.
    final Object value = mMap.get(key);
    return value == null ? defaultValue : (String) value;
  }

  /**
   * Returns the value associated with a String mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return String value
   */
  public String getString(final String key) {
    return getString(key, null);
  }

  // The only case where we convert String[] to/from GeckoBundle[] is if every element
  // is null.
  private static int getNullArrayLength(final Object array) {
    final int len = Array.getLength(array);
    for (int i = 0; i < len; i++) {
      if (Array.get(array, i) != null) {
        throw new ClassCastException("Cannot cast array type");
      }
    }
    return len;
  }

  /**
   * Returns the value associated with a String array mapping, or null if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @return String array value
   */
  public String[] getStringArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0
            ? EMPTY_STRING_ARRAY
            : !(value instanceof String[])
                ? new String[getNullArrayLength(value)]
                : (String[]) value;
  }

  /*
   * Returns the value associated with a RectF mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return RectF value
   */
  public RectF getRectF(final String key) {
    final GeckoBundle rectBundle = getBundle(key);
    if (rectBundle == null) {
      return null;
    }

    return new RectF(
        (float) rectBundle.getDouble("left"),
        (float) rectBundle.getDouble("top"),
        (float) rectBundle.getDouble("right"),
        (float) rectBundle.getDouble("bottom"));
  }

  /**
   * Returns the value associated with a Point mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Point value
   */
  public Point getPoint(final String key) {
    final GeckoBundle ptBundle = getBundle(key);
    if (ptBundle == null) {
      return null;
    }

    return new Point(ptBundle.getInt("x"), ptBundle.getInt("y"));
  }

  /**
   * Returns the value associated with a PointF mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return Point value
   */
  public PointF getPointF(final String key) {
    final GeckoBundle ptBundle = getBundle(key);
    if (ptBundle == null) {
      return null;
    }

    return new PointF((float) ptBundle.getDouble("x"), (float) ptBundle.getDouble("y"));
  }

  /**
   * Returns the value associated with a GeckoBundle mapping, or null if the mapping does not exist.
   *
   * @param key Key to look for.
   * @return GeckoBundle value
   */
  public GeckoBundle getBundle(final String key) {
    return (GeckoBundle) mMap.get(key);
  }

  /**
   * Returns the value associated with a GeckoBundle array mapping, or null if the mapping does not
   * exist.
   *
   * @param key Key to look for.
   * @return GeckoBundle array value
   */
  public GeckoBundle[] getBundleArray(final String key) {
    final Object value = mMap.get(key);
    return value == null
        ? null
        : Array.getLength(value) == 0
            ? EMPTY_BUNDLE_ARRAY
            : !(value instanceof GeckoBundle[])
                ? new GeckoBundle[getNullArrayLength(value)]
                : (GeckoBundle[]) value;
  }

  /**
   * Returns whether this GeckoBundle has no mappings.
   *
   * @return True if no mapping exists.
   */
  public boolean isEmpty() {
    return mMap.isEmpty();
  }

  /**
   * Returns an array of all mapped keys.
   *
   * @return String array containing all mapped keys.
   */
  @WrapForJNI(calledFrom = "gecko")
  public String[] keys() {
    final int len = mMap.size();
    final String[] ret = new String[len];
    for (int i = 0; i < len; i++) {
      ret[i] = mMap.keyAt(i);
    }
    return ret;
  }

  @WrapForJNI(calledFrom = "gecko")
  private Object[] values() {
    final int len = mMap.size();
    final Object[] ret = new Object[len];
    for (int i = 0; i < len; i++) {
      ret[i] = mMap.valueAt(i);
    }
    return ret;
  }

  private void put(final String key, final Object value) {
    // We intentionally disallow a generic put() method for type safety and sanity. For
    // example, we assume elsewhere in the code that a value belongs to a small list of
    // predefined types, and cannot be any arbitrary object. If you want to put an
    // Object in the bundle, check the type of the Object first and call the
    // corresponding put methods. For example,
    //
    //   if (obj instanceof Integer) {
    //     bundle.putInt(key, (Integer) key);
    //   } else if (obj instanceof String) {
    //     bundle.putString(key, (String) obj);
    //   } else {
    //     throw new IllegalArgumentException("unexpected type");
    //   }
    throw new UnsupportedOperationException();
  }

  /**
   * Map a key to a boolean value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBoolean(final String key, final boolean value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a boolean array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBooleanArray(final String key, final boolean[] value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a boolean array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBooleanArray(final String key, final Boolean[] value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final boolean[] array = new boolean[value.length];
    for (int i = 0; i < value.length; i++) {
      array[i] = value[i];
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a boolean array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBooleanArray(final String key, final Collection<Boolean> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final boolean[] array = new boolean[value.size()];
    int i = 0;
    for (final Boolean element : value) {
      array[i++] = element;
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a double value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putDouble(final String key, final double value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putDoubleArray(final String key, final double[] value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putDoubleArray(final String key, final Double[] value) {
    putDoubleArray(key, Arrays.asList(value));
  }

  /**
   * Map a key to a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putDoubleArray(final String key, final Collection<Double> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final double[] array = new double[value.size()];
    int i = 0;
    for (final Double element : value) {
      array[i++] = element;
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to an int value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putInt(final String key, final int value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to an int array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putIntArray(final String key, final int[] value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a int array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putIntArray(final String key, final Integer[] value) {
    putIntArray(key, Arrays.asList(value));
  }

  /**
   * Map a key to a int array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putIntArray(final String key, final Collection<Integer> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final int[] array = new int[value.size()];
    int i = 0;
    for (final Integer element : value) {
      array[i++] = element;
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a long value stored as a double value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putLong(final String key, final long value) {
    mMap.put(key, (double) value);
  }

  /**
   * Map a key to a long array value stored as a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putLongArray(final String key, final long[] value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final double[] array = new double[value.length];
    for (int i = 0; i < value.length; i++) {
      array[i] = (double) value[i];
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a long array value stored as a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putLongArray(final String key, final Long[] value) {
    putLongArray(key, Arrays.asList(value));
  }

  /**
   * Map a key to a long array value stored as a double array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putLongArray(final String key, final Collection<Long> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final double[] array = new double[value.size()];
    int i = 0;
    for (final Long element : value) {
      array[i++] = (double) element;
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a String value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putString(final String key, final String value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a String array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putStringArray(final String key, final String[] value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a String array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putStringArray(final String key, final Collection<String> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final String[] array = new String[value.size()];
    int i = 0;
    for (final String element : value) {
      array[i++] = element;
    }
    mMap.put(key, array);
  }

  /**
   * Map a key to a GeckoBundle value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBundle(final String key, final GeckoBundle value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a GeckoBundle array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBundleArray(final String key, final GeckoBundle[] value) {
    mMap.put(key, value);
  }

  /**
   * Map a key to a GeckoBundle array value.
   *
   * @param key Key to map.
   * @param value Value to map to.
   */
  public void putBundleArray(final String key, final Collection<GeckoBundle> value) {
    if (value == null) {
      mMap.put(key, null);
      return;
    }
    final GeckoBundle[] array = new GeckoBundle[value.size()];
    int i = 0;
    for (final GeckoBundle element : value) {
      array[i++] = element;
    }
    mMap.put(key, array);
  }

  /**
   * Remove a mapping.
   *
   * @param key Key to remove.
   */
  public void remove(final String key) {
    mMap.remove(key);
  }

  /**
   * Returns number of mappings in this GeckoBundle.
   *
   * @return Number of mappings.
   */
  public int size() {
    return mMap.size();
  }

  private static Object normalizeValue(final Object value) {
    if (value instanceof Integer) {
      // We treat int and double as the same type.
      return ((Integer) value).doubleValue();

    } else if (value instanceof int[]) {
      // We treat int[] and double[] as the same type.
      final int[] array = (int[]) value;
      return array.length == 0 ? EMPTY_STRING_ARRAY : getDoubleArray(array);

    } else if (value != null && value.getClass().isArray()) {
      // We treat arrays of all nulls as the same type, including empty arrays.
      final int len = Array.getLength(value);
      for (int i = 0; i < len; i++) {
        if (Array.get(value, i) != null) {
          return value;
        }
      }
      return len == 0 ? EMPTY_STRING_ARRAY : new String[len];
    }
    return value;
  }

  @Override // Object
  public boolean equals(final Object other) {
    if (!(other instanceof GeckoBundle)) {
      return false;
    }

    // Support library's SimpleArrayMap.equals is buggy, so roll our own version.
    final SimpleArrayMap<String, Object> otherMap = ((GeckoBundle) other).mMap;
    if (mMap == otherMap) {
      return true;
    }
    if (mMap.size() != otherMap.size()) {
      return false;
    }

    for (int i = 0; i < mMap.size(); i++) {
      final String thisKey = mMap.keyAt(i);
      final int otherKey = otherMap.indexOfKey(thisKey);
      if (otherKey < 0) {
        return false;
      }
      final Object thisValue = normalizeValue(mMap.valueAt(i));
      final Object otherValue = normalizeValue(otherMap.valueAt(otherKey));
      if (thisValue == otherValue) {
        continue;
      } else if (thisValue == null || otherValue == null) {
        return false;
      }

      final Class<?> thisClass = thisValue.getClass();
      final Class<?> otherClass = otherValue.getClass();
      if (thisClass != otherClass && !thisClass.equals(otherClass)) {
        return false;
      } else if (!thisClass.isArray()) {
        if (!thisValue.equals(otherValue)) {
          return false;
        }
        continue;
      }

      // Work with both primitive arrays and Object arrays, unlike Arrays.equals().
      final int thisLen = Array.getLength(thisValue);
      final int otherLen = Array.getLength(otherValue);
      if (thisLen != otherLen) {
        return false;
      }
      for (int j = 0; j < thisLen; j++) {
        final Object thisElem = Array.get(thisValue, j);
        final Object otherElem = Array.get(otherValue, j);
        if (thisElem != otherElem
            && (thisElem == null || otherElem == null || !thisElem.equals(otherElem))) {
          return false;
        }
      }
    }
    return true;
  }

  @Override // Object
  public int hashCode() {
    return mMap.hashCode();
  }

  @Override // Object
  public String toString() {
    return mMap.toString();
  }

  public JSONObject toJSONObject() throws JSONException {
    final JSONObject out = new JSONObject();
    for (int i = 0; i < mMap.size(); i++) {
      final Object value = mMap.valueAt(i);
      final Object jsonValue;

      if (value instanceof GeckoBundle) {
        jsonValue = ((GeckoBundle) value).toJSONObject();
      } else if (value instanceof GeckoBundle[]) {
        final GeckoBundle[] array = (GeckoBundle[]) value;
        final JSONArray jsonArray = new JSONArray();
        for (final GeckoBundle element : array) {
          jsonArray.put(element == null ? JSONObject.NULL : element.toJSONObject());
        }
        jsonValue = jsonArray;
      } else if (Build.VERSION.SDK_INT >= 19) {
        final Object wrapped = JSONObject.wrap(value);
        jsonValue = wrapped != null ? wrapped : value.toString();
      } else if (value == null) {
        jsonValue = JSONObject.NULL;
      } else if (value.getClass().isArray()) {
        final JSONArray jsonArray = new JSONArray();
        for (int j = 0; j < Array.getLength(value); j++) {
          jsonArray.put(Array.get(value, j));
        }
        jsonValue = jsonArray;
      } else {
        jsonValue = value;
      }
      out.put(mMap.keyAt(i), jsonValue);
    }
    return out;
  }

  public Bundle toBundle() {
    final Bundle out = new Bundle(mMap.size());
    for (int i = 0; i < mMap.size(); i++) {
      final String key = mMap.keyAt(i);
      final Object val = mMap.valueAt(i);

      if (val == null) {
        out.putString(key, null);
      } else if (val instanceof GeckoBundle) {
        out.putBundle(key, ((GeckoBundle) val).toBundle());
      } else if (val instanceof GeckoBundle[]) {
        final GeckoBundle[] array = (GeckoBundle[]) val;
        final Parcelable[] parcelables = new Parcelable[array.length];
        for (int j = 0; j < array.length; j++) {
          if (array[j] != null) {
            parcelables[j] = array[j].toBundle();
          }
        }
        out.putParcelableArray(key, parcelables);
      } else if (val instanceof Boolean) {
        out.putBoolean(key, (Boolean) val);
      } else if (val instanceof boolean[]) {
        out.putBooleanArray(key, (boolean[]) val);
      } else if (val instanceof Byte || val instanceof Short || val instanceof Integer) {
        out.putInt(key, ((Number) val).intValue());
      } else if (val instanceof int[]) {
        out.putIntArray(key, (int[]) val);
      } else if (val instanceof Float || val instanceof Double || val instanceof Long) {
        out.putDouble(key, ((Number) val).doubleValue());
      } else if (val instanceof double[]) {
        out.putDoubleArray(key, (double[]) val);
      } else if (val instanceof CharSequence || val instanceof Character) {
        out.putString(key, val.toString());
      } else if (val instanceof String[]) {
        out.putStringArray(key, (String[]) val);
      } else {
        throw new UnsupportedOperationException();
      }
    }
    return out;
  }

  public static GeckoBundle fromBundle(final Bundle bundle) {
    if (bundle == null) {
      return null;
    }

    final String[] keys = new String[bundle.size()];
    final Object[] values = new Object[bundle.size()];
    int i = 0;

    for (final String key : bundle.keySet()) {
      final Object value = bundle.get(key);
      keys[i] = key;

      if (value instanceof Bundle || value == null) {
        values[i] = fromBundle((Bundle) value);
      } else if (value instanceof Parcelable[]) {
        final Parcelable[] array = (Parcelable[]) value;
        final GeckoBundle[] out = new GeckoBundle[array.length];
        for (int j = 0; j < array.length; j++) {
          out[j] = fromBundle((Bundle) array[j]);
        }
        values[i] = out;
      } else if (value instanceof Boolean
          || value instanceof Integer
          || value instanceof Double
          || value instanceof String
          || value instanceof boolean[]
          || value instanceof int[]
          || value instanceof double[]
          || value instanceof String[]) {
        values[i] = value;
      } else if (value instanceof Byte || value instanceof Short) {
        values[i] = ((Number) value).intValue();
      } else if (value instanceof Float || value instanceof Long) {
        values[i] = ((Number) value).doubleValue();
      } else if (value instanceof CharSequence || value instanceof Character) {
        values[i] = value.toString();
      } else {
        throw new UnsupportedOperationException();
      }

      i++;
    }
    return new GeckoBundle(keys, values);
  }

  private static Object fromJSONValue(final Object value) throws JSONException {
    if (value == null || value == JSONObject.NULL) {
      return null;
    } else if (value instanceof JSONObject) {
      return fromJSONObject((JSONObject) value);
    }
    if (value instanceof JSONArray) {
      final JSONArray array = (JSONArray) value;
      final int len = array.length();
      if (len == 0) {
        return EMPTY_BOOLEAN_ARRAY;
      }
      Object out = null;
      for (int i = 0; i < len; i++) {
        final Object element = fromJSONValue(array.opt(i));
        if (element == null) {
          continue;
        }
        if (out == null) {
          Class<?> type = element.getClass();
          if (type == Boolean.class) {
            type = boolean.class;
          } else if (type == Integer.class) {
            type = int.class;
          } else if (type == Double.class) {
            type = double.class;
          }
          out = Array.newInstance(type, len);
        }
        Array.set(out, i, element);
      }
      if (out == null) {
        // Treat all-null arrays as String arrays.
        return new String[len];
      }
      return out;
    }
    if (value instanceof Boolean
        || value instanceof Integer
        || value instanceof Double
        || value instanceof String) {
      return value;
    }
    if (value instanceof Byte || value instanceof Short) {
      return ((Number) value).intValue();
    }
    if (value instanceof Float || value instanceof Long) {
      return ((Number) value).doubleValue();
    }
    return value.toString();
  }

  public static GeckoBundle fromJSONObject(final JSONObject obj) throws JSONException {
    if (obj == null || obj == JSONObject.NULL) {
      return null;
    }

    final String[] keys = new String[obj.length()];
    final Object[] values = new Object[obj.length()];

    final Iterator<String> iter = obj.keys();
    for (int i = 0; iter.hasNext(); i++) {
      final String key = iter.next();
      keys[i] = key;
      values[i] = fromJSONValue(obj.opt(key));
    }
    return new GeckoBundle(keys, values);
  }

  @Override // Parcelable
  public int describeContents() {
    return 0;
  }

  @Override // Parcelable
  public void writeToParcel(final Parcel dest, final int flags) {
    final int len = mMap.size();
    dest.writeInt(len);

    for (int i = 0; i < len; i++) {
      dest.writeString(mMap.keyAt(i));
      dest.writeValue(mMap.valueAt(i));
    }
  }

  // AIDL code may call readFromParcel even though it's not part of Parcelable.
  public void readFromParcel(final Parcel source) {
    final ClassLoader loader = getClass().getClassLoader();
    final int len = source.readInt();
    mMap.clear();
    mMap.ensureCapacity(len);

    for (int i = 0; i < len; i++) {
      final String key = source.readString();
      Object val = source.readValue(loader);

      if (val instanceof Parcelable[]) {
        final Parcelable[] array = (Parcelable[]) val;
        val = Arrays.copyOf(array, array.length, GeckoBundle[].class);
      }

      mMap.put(key, val);
    }
  }

  public static final Parcelable.Creator<GeckoBundle> CREATOR =
      new Parcelable.Creator<GeckoBundle>() {
        @Override
        public GeckoBundle createFromParcel(final Parcel source) {
          final GeckoBundle bundle = new GeckoBundle(0);
          bundle.readFromParcel(source);
          return bundle;
        }

        @Override
        public GeckoBundle[] newArray(final int size) {
          return new GeckoBundle[size];
        }
      };
}