-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainActivity.kt
More file actions
487 lines (450 loc) · 14.3 KB
/
MainActivity.kt
File metadata and controls
487 lines (450 loc) · 14.3 KB
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
package com.example.androidobservability
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.Alignment
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import com.example.androidobservability.masking.ComposeMaskingActivity
import com.example.androidobservability.masking.ComposeUserFormActivity
import com.example.androidobservability.masking.ComposeWebActivity
import com.example.androidobservability.masking.XMLUserFormActivity
import com.example.androidobservability.masking.XMLMaskingActivity
import com.example.androidobservability.masking.XMLWebActivity
import com.example.androidobservability.smoothie.SmoothieListActivity
import com.example.androidobservability.ui.theme.AndroidObservabilityTheme
import com.example.androidobservability.ui.theme.DangerRed
import com.example.androidobservability.ui.theme.IdentifyBgColor
import com.example.androidobservability.ui.theme.IdentifyTextColor
import com.launchdarkly.observability.sdk.LDReplay
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel: ViewModel by viewModels()
enableEdgeToEdge()
setContent {
AndroidObservabilityTheme {
@OptIn(ExperimentalMaterial3Api::class)
Scaffold(
modifier = Modifier
.fillMaxSize()
.imePadding(),
topBar = {
TopAppBar(
title = {
Text(
"Android Observability",
modifier = Modifier.padding(start = 8.dp)
)
},
navigationIcon = {
Icon(
painter = painterResource(id = R.drawable.ic_launchdarkly_logo),
contentDescription = "LaunchDarkly",
modifier = Modifier.padding(start = 12.dp),
tint = Color.White
)
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = Color.Black,
titleContentColor = Color.White
)
)
}
) { innerPadding ->
MainScreen(viewModel, innerPadding)
}
}
}
}
}
@Composable
private fun MainScreen(viewModel: ViewModel, innerPadding: PaddingValues) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.verticalScroll(rememberScrollState())
.padding(16.dp)
) {
SessionReplayHeader()
HorizontalDivider(modifier = Modifier.padding(bottom = 16.dp))
MaskingButtons()
Text(
text = "Observability",
style = MaterialTheme.typography.headlineSmall.copy(fontWeight = FontWeight.Bold),
modifier = Modifier.padding(bottom = 8.dp)
)
HorizontalDivider(modifier = Modifier.padding(bottom = 16.dp))
IdentifyButtons(viewModel = viewModel)
InstrumentationButtons(viewModel = viewModel)
MetricButtons(viewModel = viewModel)
CustomerApiButtons(viewModel = viewModel)
}
}
@Composable
private fun SessionReplayHeader() {
var isSessionReplayEnabled by rememberSaveable { mutableStateOf(true) }
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 8.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Session Replay",
style = MaterialTheme.typography.headlineSmall.copy(fontWeight = FontWeight.Bold)
)
Switch(
checked = isSessionReplayEnabled,
onCheckedChange = { enabled ->
isSessionReplayEnabled = enabled
if (enabled) {
LDReplay.start()
} else {
LDReplay.stop()
}
}
)
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun MetricButtons(viewModel: ViewModel) {
Text(
text = "Metric",
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
modifier = Modifier.padding(bottom = 8.dp)
)
FlowRow(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(0.dp)
) {
Button(
onClick = {
viewModel.triggerMetric()
}
) {
Text("Metric")
}
Button(
onClick = {
viewModel.triggerHistogramMetric()
}
) {
Text("Histogram")
}
Button(
onClick = {
viewModel.triggerCountMetric()
}
) {
Text("Count")
}
Button(
onClick = {
viewModel.triggerIncrementalMetric()
}
) {
Text("Incremental")
}
Button(
onClick = {
viewModel.triggerUpDownCounterMetric()
}
) {
Text("UpDownCounter")
}
}
}
@Composable
private fun InstrumentationButtons(viewModel: ViewModel) {
Text(
text = "Instrumentation",
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
modifier = Modifier.padding(bottom = 8.dp, top = 8.dp)
)
Column(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 16.dp)
) {
Button(
onClick = {
viewModel.triggerHttpRequests()
}
) {
Text("Trigger HTTP Request")
}
Button(
onClick = {
viewModel.startForegroundService()
}
) {
Text("Start Foreground Service")
}
Button(
onClick = {
viewModel.startBackgroundService()
}
) {
Text("Start Background Service")
}
Button(
onClick = {
viewModel.triggerCrash()
},
colors = ButtonDefaults.buttonColors(
containerColor = DangerRed,
contentColor = Color.White
)
) {
Text("Trigger Crash")
}
}
}
@Composable
private fun MaskingButtons() {
val context = LocalContext.current
MaskingRow(
name = "User Form",
ctx = context,
activity1 = XMLUserFormActivity::class.java,
activity2 = ComposeUserFormActivity::class.java
)
MaskingRow(
name = "Smoothies",
ctx = context,
activity1 = SmoothieListActivity::class.java,
activity2 = null
)
MaskingRow(
name = "Dialogs",
ctx = context,
activity1 = XMLMaskingActivity::class.java,
activity2 = ComposeMaskingActivity::class.java
)
MaskingRow(
name = "Webviews",
ctx = context,
activity1 = XMLWebActivity::class.java,
activity2 = ComposeWebActivity::class.java
)
}
@Composable
private fun MaskingRow(name: String, ctx: Context, activity1: Class<out Activity>??, activity2: Class<out Activity>??) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = name,
modifier = Modifier.weight(1f)
)
Button(
onClick = { goToActivity(ctx, activity1) },
enabled = activity1 != null,
modifier = Modifier.weight(1f),
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 6.dp)
) {
Text("XML")
}
Button(
onClick = { goToActivity(ctx, activity2) },
enabled = activity2 != null,
modifier = Modifier.weight(1f),
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 6.dp)
) {
Text("Compose")
}
}
}
private fun goToActivity(ctx: Context, activity: Class<out Activity>?){
activity?.let {
ctx.startActivity(
Intent(ctx, it)
)
}
}
@Composable
private fun IdentifyButtons(viewModel: ViewModel) {
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "Identify:",
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
modifier = Modifier.padding(bottom = 8.dp, top = 8.dp)
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 8.dp, bottom = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Button(
onClick = { viewModel.identifyUser() },
colors = ButtonDefaults.buttonColors(
containerColor = IdentifyBgColor,
contentColor = IdentifyTextColor
)
) {
Text("User")
}
Button(
onClick = { viewModel.identifyMulti() },
colors = ButtonDefaults.buttonColors(
containerColor = IdentifyBgColor,
contentColor = IdentifyTextColor
)
) {
Text("Multi")
}
Button(
onClick = { viewModel.identifyAnonymous() },
colors = ButtonDefaults.buttonColors(
containerColor = IdentifyBgColor,
contentColor = IdentifyTextColor
)
) {
Text("Anon")
}
}
}
@Composable
private fun CustomerApiButtons(viewModel: ViewModel) {
var customLogText by remember { mutableStateOf("") }
var customSpanText by remember { mutableStateOf("") }
var flagKey by remember { mutableStateOf("my-feature") }
Text(
text = "Customer API",
style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
modifier = Modifier.padding(bottom = 8.dp, top = 8.dp)
)
Button(
onClick = {
viewModel.triggerError()
},
colors = ButtonDefaults.buttonColors(
containerColor = DangerRed,
contentColor = Color.White
)
) {
Text("Trigger Error")
}
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Button(
onClick = {
viewModel.triggerLog()
}
) {
Text("Trigger Log")
}
Button(
onClick = {
viewModel.triggerLogWithContext(customLogText)
}
) {
Text("Log with Context")
}
}
OutlinedTextField(
value = customLogText,
onValueChange = { customLogText = it },
label = { Text("Log Message") },
modifier = Modifier.padding(8.dp)
)
Button(
onClick = {
viewModel.triggerCustomLog(customLogText)
},
modifier = Modifier.padding(8.dp)
) {
Text("Send custom log")
}
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = {
viewModel.triggerNestedSpans()
}
) {
Text("Trigger Nested Spans")
}
OutlinedTextField(
value = customSpanText,
onValueChange = { customSpanText = it },
label = { Text("Span Name") },
modifier = Modifier.padding(8.dp)
)
Button(
onClick = {
viewModel.triggerCustomSpan(customSpanText)
},
modifier = Modifier.padding(8.dp)
) {
Text("Send custom span")
}
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
value = flagKey,
onValueChange = { flagKey = it },
label = { Text("Flag key") },
modifier = Modifier.padding(8.dp)
)
Button(
onClick = {
viewModel.evaluateBooleanFlag(flagKey)
},
modifier = Modifier.padding(8.dp)
) {
Text("Evaluate boolean flag")
}
}