16
16
17
17
import androidx .annotation .NonNull ;
18
18
import androidx .annotation .Nullable ;
19
- import com .google .firebase .crashlytics .internal .Logger ;
20
- import java .util .ArrayList ;
21
- import java .util .Collections ;
22
- import java .util .HashMap ;
23
- import java .util .List ;
24
19
import java .util .Map ;
25
20
26
21
/** Handles attributes set by the user. */
@@ -30,8 +25,8 @@ public class UserMetadata {
30
25
static final int MAX_INTERNAL_KEY_SIZE = 8192 ;
31
26
32
27
private String userId = null ;
33
- private final Map < String , String > attributes = new HashMap <>( );
34
- private final Map < String , String > internalKeys = new HashMap <>( );
28
+ private final KeysMap customKeys = new KeysMap ( MAX_ATTRIBUTES , MAX_ATTRIBUTE_SIZE );
29
+ private final KeysMap internalKeys = new KeysMap ( MAX_ATTRIBUTES , MAX_INTERNAL_KEY_SIZE );
35
30
36
31
public UserMetadata () {}
37
32
@@ -41,95 +36,27 @@ public String getUserId() {
41
36
}
42
37
43
38
public void setUserId (String identifier ) {
44
- userId = sanitizeAttribute (identifier , MAX_ATTRIBUTE_SIZE );
39
+ userId = customKeys . sanitizeAttribute (identifier );
45
40
}
46
41
47
42
@ NonNull
48
43
public Map <String , String > getCustomKeys () {
49
- return Collections . unmodifiableMap ( attributes );
44
+ return customKeys . getKeys ( );
50
45
}
51
46
52
47
public void setCustomKey (String key , String value ) {
53
- setSyncCustomKeys (
54
- new HashMap <String , String >() {
55
- {
56
- put (key , value );
57
- }
58
- },
59
- attributes ,
60
- MAX_ATTRIBUTE_SIZE );
48
+ customKeys .setKey (key , value );
61
49
}
62
50
63
51
public void setCustomKeys (Map <String , String > keysAndValues ) {
64
- setSyncCustomKeys (keysAndValues , attributes , MAX_ATTRIBUTE_SIZE );
52
+ customKeys . setKeys (keysAndValues );
65
53
}
66
54
67
55
public Map <String , String > getInternalKeys () {
68
- return Collections . unmodifiableMap ( internalKeys );
56
+ return internalKeys . getKeys ( );
69
57
}
70
58
71
59
public void setInternalKey (String key , String value ) {
72
- setSyncCustomKeys (
73
- new HashMap <String , String >() {
74
- {
75
- put (key , value );
76
- }
77
- },
78
- internalKeys ,
79
- MAX_INTERNAL_KEY_SIZE );
80
- }
81
-
82
- /** Gatekeeper function for access to attributes or internalKeys */
83
- private synchronized void setSyncCustomKeys (
84
- Map <String , String > keysAndValues , Map <String , String > keys_map , int maxAttributeSize ) {
85
- // We want all access to the keys_map hashmap to be locked so that there is no way to create
86
- // a race condition and add more than MAX_ATTRIBUTES keys.
87
-
88
- // Update any existing keys first, then add any additional keys
89
- Map <String , String > currentKeys = new HashMap <String , String >();
90
- Map <String , String > newKeys = new HashMap <String , String >();
91
-
92
- // Split into current and new keys
93
- for (Map .Entry <String , String > entry : keysAndValues .entrySet ()) {
94
- String key = sanitizeKey (entry .getKey (), maxAttributeSize );
95
- String value =
96
- (entry .getValue () == null ) ? "" : sanitizeAttribute (entry .getValue (), maxAttributeSize );
97
- if (keys_map .containsKey (key )) {
98
- currentKeys .put (key , value );
99
- } else {
100
- newKeys .put (key , value );
101
- }
102
- }
103
-
104
- keys_map .putAll (currentKeys );
105
-
106
- // Add new keys if there is space
107
- if (keys_map .size () + newKeys .size () > MAX_ATTRIBUTES ) {
108
- int keySlotsLeft = MAX_ATTRIBUTES - keys_map .size ();
109
- Logger .getLogger ()
110
- .v ("Exceeded maximum number of custom attributes (" + MAX_ATTRIBUTES + ")." );
111
- List <String > newKeyList = new ArrayList <>(newKeys .keySet ());
112
- newKeys .keySet ().retainAll (newKeyList .subList (0 , keySlotsLeft ));
113
- }
114
- keys_map .putAll (newKeys );
115
- }
116
-
117
- /** Checks that the key is not null then sanitizes it. */
118
- private static String sanitizeKey (String key , int maxAttributeSize ) {
119
- if (key == null ) {
120
- throw new IllegalArgumentException ("Custom attribute key must not be null." );
121
- }
122
- return sanitizeAttribute (key , maxAttributeSize );
123
- }
124
-
125
- /** Trims the string and truncates it to MAX_ATTRIBUTE_SIZE. */
126
- private static String sanitizeAttribute (String input , int maxAttributeSize ) {
127
- if (input != null ) {
128
- input = input .trim ();
129
- if (input .length () > maxAttributeSize ) {
130
- input = input .substring (0 , maxAttributeSize );
131
- }
132
- }
133
- return input ;
60
+ internalKeys .setKey (key , value );
134
61
}
135
62
}
0 commit comments