-
Notifications
You must be signed in to change notification settings - Fork 8
/
EmbraceUserService.kt
175 lines (151 loc) · 5.65 KB
/
EmbraceUserService.kt
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
package io.embrace.android.embracesdk.capture.user
import io.embrace.android.embracesdk.internal.utils.Provider
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.logging.InternalErrorType
import io.embrace.android.embracesdk.payload.UserInfo
import io.embrace.android.embracesdk.payload.UserInfo.Companion.ofStored
import io.embrace.android.embracesdk.prefs.PreferencesService
import java.util.concurrent.atomic.AtomicReference
import java.util.regex.Pattern
internal class EmbraceUserService(
private val preferencesService: PreferencesService,
private val logger: EmbLogger
) : UserService {
/**
* Do not access this directly - use [userInfo] and [modifyUserInfo] to get and set this
*/
private val userInfoReference = AtomicReference(DEFAULT_USER)
private val userInfoProvider: Provider<UserInfo> = { ofStored(preferencesService) }
override fun loadUserInfoFromDisk(): UserInfo? {
return try {
ofStored(preferencesService)
} catch (ex: Exception) {
logger.logError("Failed to load user info from persistent storage.", ex)
logger.trackInternalError(InternalErrorType.USER_LOAD_FAIL, ex)
null
}
}
override fun getUserInfo(): UserInfo = userInfo().copy()
override fun setUserIdentifier(userId: String?) {
val currentUserId = userInfo().userId
if (currentUserId != null && currentUserId == userId) {
return
}
modifyUserInfo(userInfo().copy(userId = userId))
preferencesService.userIdentifier = userId
}
override fun clearUserIdentifier() {
setUserIdentifier(null)
}
override fun setUsername(username: String?) {
val currentUserName = userInfo().username
if (currentUserName != null && currentUserName == username) {
return
}
modifyUserInfo(userInfo().copy(username = username))
preferencesService.username = username
}
override fun clearUsername() {
setUsername(null)
}
override fun setUserEmail(email: String?) {
val currentEmail = userInfo().email
if (currentEmail != null && currentEmail == email) {
return
}
modifyUserInfo(userInfo().copy(email = email))
preferencesService.userEmailAddress = email
}
override fun clearUserEmail() {
setUserEmail(null)
}
override fun setUserAsPayer() {
addUserPersona(UserInfo.PERSONA_PAYER)
}
override fun clearUserAsPayer() {
clearUserPersona(UserInfo.PERSONA_PAYER)
}
override fun addUserPersona(persona: String?) {
if (persona == null) {
return
}
if (!VALID_PERSONA.matcher(persona).matches()) {
logger.logWarning("Ignoring persona " + persona + " as it does not match " + VALID_PERSONA.pattern())
return
}
val currentPersonas = userInfo().personas
if (currentPersonas != null) {
if (currentPersonas.size >= PERSONA_LIMIT) {
logger.logWarning("Cannot set persona as the limit of " + PERSONA_LIMIT + " has been reached")
return
}
if (currentPersonas.contains(persona)) {
return
}
}
val newPersonas: Set<String> = userInfo().personas?.plus(persona) ?: mutableSetOf(persona)
modifyUserInfo(userInfo().copy(personas = newPersonas))
preferencesService.userPersonas = newPersonas
}
override fun clearUserPersona(persona: String?) {
if (persona == null) {
return
}
val currentPersonas = userInfo().personas
if (currentPersonas != null && !currentPersonas.contains(persona)) {
logger.logWarning("Persona '$persona' is not set")
return
}
val newPersonas: Set<String> = userInfo().personas?.minus(persona) ?: mutableSetOf()
modifyUserInfo(userInfo().copy(personas = newPersonas))
preferencesService.userPersonas = newPersonas
}
override fun clearAllUserPersonas() {
val currentPersonas = userInfo().personas
if (currentPersonas != null && currentPersonas.isEmpty()) {
return
}
val personas: MutableSet<String> = HashSet()
if (preferencesService.userPayer) {
personas.add(UserInfo.PERSONA_PAYER)
}
if (preferencesService.isUsersFirstDay()) {
personas.add(UserInfo.PERSONA_FIRST_DAY_USER)
}
modifyUserInfo(userInfo().copy(personas = personas))
preferencesService.userPersonas = personas
}
override fun clearAllUserInfo() {
clearUserIdentifier()
clearUserEmail()
clearUsername()
clearAllUserPersonas()
}
private fun userInfo(): UserInfo {
if (userInfoReference.get() === DEFAULT_USER) {
synchronized(userInfoReference) {
if (userInfoReference.get() === DEFAULT_USER) {
userInfoReference.set(userInfoProvider())
}
}
}
return userInfoReference.get()
}
private fun modifyUserInfo(newUserInfo: UserInfo) {
synchronized(userInfoReference) {
userInfoReference.set(newUserInfo)
}
}
companion object {
// Valid persona regex representation.
val VALID_PERSONA: Pattern = Pattern.compile("^[a-zA-Z0-9_]{1,32}$")
// Maximum number of allowed personas.
const val PERSONA_LIMIT = 10
private val DEFAULT_USER = UserInfo(
userId = "",
email = "",
username = "",
personas = emptySet()
)
}
}