summaryrefslogtreecommitdiff
path: root/app/src/test/java/android/os/Bundle.java
blob: ea869cb21018b52e1d2abcacfcf894518a57a177 (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
package android.os;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Bundle {

    /** An unmodifiable {@code Bundle} that is always {@link #isEmpty() empty}. */
    public static final Bundle EMPTY;

    /**
     * Special extras used to denote extras have been stripped off.
     * @hide
     */
    public static final Bundle STRIPPED;

    static {
        EMPTY = new Bundle();

        STRIPPED = new Bundle();
        STRIPPED.putInt("STRIPPED", 1);
    }

    final Map<String, Boolean> fakeBooleanBundle = new HashMap<>();
    final Map<String, String> fakeStringBundle = new HashMap<>();
    final Map<String, Integer> fakeIntBundle = new HashMap<>();
    final Map<String, Parcelable> fakeParcelableBundle = new HashMap<>();


    public void putString(String key, String value) {
        fakeStringBundle.put(key, value);
    }

    public String getString(String key) {
        return fakeStringBundle.get(key);
    }

    public void putBoolean(String key, boolean value) {
        fakeBooleanBundle.put(key, value);
    }

    public boolean getBoolean(String key) {
        return fakeBooleanBundle.getOrDefault(key, false);
    }

    public void putInt(String key, int value) {
        fakeIntBundle.put(key, value);
    }

    public int getInt(String key) {
        return fakeIntBundle.getOrDefault(key, 0);
    }

    public void putParcelable(String key, Parcelable value) {
        fakeParcelableBundle.put(key, value);
    }

    public Parcelable getParcelable(String key) {
        return fakeParcelableBundle.get(key);
    }

    public Object get(String key) {
        if (fakeBooleanBundle.containsKey(key)) {
            return fakeBooleanBundle.get(key);
        } else if (fakeIntBundle.containsKey(key)) {
            return fakeIntBundle.get(key);
        } else if (fakeStringBundle.containsKey(key)) {
            return fakeStringBundle.get(key);
        } else {
            return fakeParcelableBundle.get(key);
        }
    }

    public Set<String> keySet() {
        //this whole approach as a drawback:
        //you should not add the same keys for values of different types
        HashSet<String> keys = new HashSet<String>();
        keys.addAll(fakeBooleanBundle.keySet());
        keys.addAll(fakeIntBundle.keySet());
        keys.addAll(fakeStringBundle.keySet());
        keys.addAll(fakeParcelableBundle.keySet());
        return keys;
    }

    public boolean containsKey(String key) {
        return fakeBooleanBundle.containsKey(key) ||
                fakeStringBundle.containsKey(key) ||
                fakeIntBundle.containsKey(key) ||
                fakeParcelableBundle.containsKey(key);
    }

    public void remove(String key) {
        fakeBooleanBundle.remove(key);
        fakeIntBundle.remove(key);
        fakeParcelableBundle.remove(key);
        fakeStringBundle.remove(key);
    }

}