-
Notifications
You must be signed in to change notification settings - Fork 8
/
EmbraceAutomaticVerification.kt
334 lines (296 loc) · 13.3 KB
/
EmbraceAutomaticVerification.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
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
package io.embrace.android.embracesdk
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logger
import io.embrace.android.embracesdk.samples.AutomaticVerificationChecker
import io.embrace.android.embracesdk.samples.VerificationActions
import io.embrace.android.embracesdk.samples.VerifyIntegrationException
import io.embrace.android.embracesdk.session.lifecycle.ActivityLifecycleListener
import io.embrace.android.embracesdk.session.lifecycle.ActivityTracker
import io.embrace.android.embracesdk.session.lifecycle.ProcessStateListener
import io.embrace.android.embracesdk.session.lifecycle.ProcessStateService
import java.io.IOException
import java.util.concurrent.Executors
import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess
/**
* Class that includes the logic to run the automatic Verification executing EmbraceSamples.verifyIntegration() method.
*
* Under the hood this function will create a marker File. If a marker file doesn't already exist, then it execute the following steps:
* 1. Runs {@link io.embrace.android.embracesdk.samples.VerificationActions#runActions()}
* 2. Relaunch the application after the action crash
* 3. Ends session manually and display result
*
*/
internal class EmbraceAutomaticVerification(
private val scheduledExecutorService: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor(),
private val logger: InternalEmbraceLogger
) : ActivityLifecycleListener, ProcessStateListener {
private val handler = Handler(Looper.getMainLooper())
private var foregroundEventTriggered = false
internal lateinit var activityLifecycleTracker: ActivityTracker
internal lateinit var processStateService: ProcessStateService
var automaticVerificationChecker = AutomaticVerificationChecker(logger)
var verificationActions = VerificationActions(Embrace.getInstance(), logger, automaticVerificationChecker)
/**
* This flag track if the verification result popup was displayed or not,
* in case the session continues after running the verification
*/
private var isResultDisplayed = false
companion object {
internal const val TAG = "[EmbraceVerification]"
private const val ON_FOREGROUND_DELAY = 5000L
private const val EMBRACE_CONTACT_EMAIL = "[email protected]"
private const val VERIFY_INTEGRATION_DELAY = 200L
private const val ON_FOREGROUND_TIMEOUT = 5000L
internal val instance = EmbraceAutomaticVerification(logger = logger)
}
fun verifyIntegration() {
instance.setActivityListener()
instance.runVerifyIntegration()
}
fun setActivityListener() {
if (!::activityLifecycleTracker.isInitialized) {
activityLifecycleTracker = checkNotNull(Embrace.getImpl().activityLifecycleTracker)
}
if (!::processStateService.isInitialized) {
processStateService = checkNotNull(Embrace.getImpl().activityService)
}
activityLifecycleTracker.addListener(this)
processStateService.addListener(this)
}
/**
* Started point to run the verification.
* We use a [ScheduledExecutorService] to give enough time to the onForeground callback
* to be executed in order to have a valid context/activity
*/
private fun runVerifyIntegration() {
try {
scheduledExecutorService.schedule(
{ startVerification() },
VERIFY_INTEGRATION_DELAY,
TimeUnit.MILLISECONDS
)
} catch (e: RejectedExecutionException) {
logger.logError("$TAG - Start verification rejected", e)
}
}
fun startVerification() {
val activity = activityLifecycleTracker.foregroundActivity
if (activity != null) {
try {
if (automaticVerificationChecker.createFile(activity)) {
// should run the verification actions
showToast(activity, activity.getString(R.string.automatic_verification_started))
verificationActions.runActions()
} else {
// the verification was already started
logger.logInfo("$TAG Verification almost ready...")
handler.postDelayed({
verifyLifecycle()
}, ON_FOREGROUND_TIMEOUT)
}
} catch (e: IOException) {
logger.logError("$TAG Embrace SDK cannot run the verification in this moment", e)
showToast(activity, activity.getString(R.string.automatic_verification_not_started))
}
} else {
logger.logError("$TAG Embrace SDK cannot run the verification in this moment, Activity is not present")
}
}
private fun verifyLifecycle() {
if (!foregroundEventTriggered) {
logger.logError("$TAG OnForeground event was not triggered")
val exceptionsService = checkNotNull(Embrace.getImpl().internalErrorService)
if (verifyIfInitializerIsDisabled()) {
exceptionsService.handleInternalError(
VerifyIntegrationException("ProcessLifecycleInitializer disabled")
)
showDialogWithError(R.string.automatic_verification_no_initializer_message)
} else {
exceptionsService.handleInternalError(
VerifyIntegrationException("onForeground not invoked")
)
showDialogWithError(R.string.automatic_verification_lifecycle_error_message)
}
}
}
fun runEndSession() {
Embrace.getInstance().endSession()
logger.logInfo("$TAG End session manually")
}
/**
* Tries to detect the condition where the ProcessLifecycleInitializer is removed in the build file
*
* @return true if it detects that ProcessLifecycleInitializer is disabled, false otherwise
*/
private fun verifyIfInitializerIsDisabled(): Boolean {
logger.logInfo("Trying to verify lifecycle annotations")
try {
val appInitializerClass: Class<*>?
try {
appInitializerClass = Class.forName("androidx.startup.AppInitializer")
} catch (cnfe: ClassNotFoundException) {
return false
}
Embrace.getImpl().application?.also { app ->
val getInstance = appInitializerClass.getMethod("getInstance", Context::class.java)
val isEagerlyInitialized =
appInitializerClass.getMethod("isEagerlyInitialized", Class::class.java)
val lifecycleInitializerClass =
Class.forName("androidx.lifecycle.ProcessLifecycleInitializer")
val appInitializer = getInstance.invoke(null, app)
val result = isEagerlyInitialized.invoke(appInitializer, lifecycleInitializerClass) as Boolean
return result.not()
} ?: run {
return false
}
} catch (e: Exception) {
logger.logWarning("$TAG Could not verify if lifecycle annotations are working: $e")
}
return false
}
/**
* Restarts the app after a forced VerifyIntegrationException
* was captured as part of the automatic verification
*/
fun restartAppFromPendingIntent() {
val exitStatus = 2
val activity = activityLifecycleTracker.foregroundActivity
if (activity != null) {
val intent = activity.intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.putExtra("from_verification", true)
with(activity) {
finish()
startActivity(intent)
}
exitProcess(exitStatus)
} else {
logger.logError("Cannot restart app, activity is not present")
}
}
override fun onForeground(coldStart: Boolean, timestamp: Long) {
foregroundEventTriggered = true
val activity = activityLifecycleTracker.foregroundActivity
if (activity != null) {
val fromVerification = activity.intent.getBooleanExtra("from_verification", false)
if (!fromVerification) {
return
}
if (isResultDisplayed) {
logger.logDebug("onForeground called but the result was already displayed")
return
}
handler.postDelayed({
runEndSession()
displayResult()
clearUserData()
automaticVerificationChecker.deleteFile()
}, ON_FOREGROUND_DELAY)
} else {
logger.logError("Cannot restart app, activity is not present")
}
}
private fun clearUserData() {
Embrace.getInstance().clearUserEmail()
Embrace.getInstance().clearUsername()
Embrace.getInstance().clearAllUserPersonas()
Embrace.getInstance().clearUserIdentifier()
Embrace.getInstance().clearUserAsPayer()
}
private fun displayResult() {
isResultDisplayed = true
automaticVerificationChecker.isVerificationCorrect()?.also { isCorrect ->
if (isCorrect) {
logger.logInfo("$TAG Successful - Embrace is ready to go! 🎉")
showSuccessDialog()
} else {
logger.logInfo("$TAG Error - Something is wrong with the Embrace Configuration ⚠️")
showDialogWithError()
}
} ?: logger.logError("Cannot display end message")
}
private fun showToast(activity: Activity, message: String) {
activity.runOnUiThread {
Toast.makeText(
activity,
message,
Toast.LENGTH_LONG
).show()
}
}
private fun showSuccessDialog() {
val activity = activityLifecycleTracker.foregroundActivity
if (activity != null) {
val dialogBuilder = AlertDialog.Builder(activity)
dialogBuilder
.setTitle(activity.getString(R.string.automatic_verification_success_title))
.setMessage(activity.getString(R.string.automatic_verification_success_message))
.setCancelable(true)
.setPositiveButton(activity.getString(R.string.got_it)) { dialog, _ ->
dialog.dismiss()
}
dialogBuilder.create().show()
} else {
logger.logInfo("Verification success! - Cannot display popup")
}
}
private fun showDialogWithError(errorMessage: Int? = null) {
val activity = activityLifecycleTracker.foregroundActivity
if (activity != null) {
val exceptions = automaticVerificationChecker.getExceptions().map { it.message }.toMutableList()
if (errorMessage != null) {
exceptions.add(activity.getString(errorMessage))
}
val errorString = if (exceptions.isNotEmpty()) {
activity.getString(R.string.embrace_verification_errors)
.replace("[X]", exceptions.joinToString("\n👉 ", "👉 "))
} else {
activity.getString(R.string.automatic_verification_default_error_message)
}
val dialogBuilder = AlertDialog.Builder(activity)
dialogBuilder
.setTitle(activity.getString(R.string.automatic_verification_error_title))
.setMessage(errorString)
.setCancelable(true)
.setNegativeButton(activity.getString(R.string.send_error_log)) { dialog, _ ->
sendErrorLog(activity, errorString)
dialog.dismiss()
}
.setPositiveButton(activity.getString(R.string.close)) { dialog, _ ->
dialog.dismiss()
}
dialogBuilder.create().show()
} else {
logger.logError("Verification error - Cannot display popup")
}
}
private fun sendErrorLog(activity: Activity, errorMessage: String) {
val errorLog = generateErrorLog(errorMessage)
val selectorIntent = Intent(Intent.ACTION_SENDTO).setData(Uri.parse("mailto:$EMBRACE_CONTACT_EMAIL"))
val emailIntent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_EMAIL, arrayOf(EMBRACE_CONTACT_EMAIL))
putExtra(Intent.EXTRA_SUBJECT, "Android Verification Log")
putExtra(Intent.EXTRA_TEXT, errorLog)
selector = selectorIntent
}
activity.startActivity(Intent.createChooser(emailIntent, "Send Email"))
}
private fun generateErrorLog(errorMessage: String): String {
var errorLog = "App ID: ${Embrace.getImpl().metadataService?.getAppId()}\n" +
"App Version: ${Embrace.getImpl().metadataService?.getAppVersionName()}"
errorLog += "\n\n-----------------\n\n"
errorLog += errorMessage
return errorLog
}
}