-
Notifications
You must be signed in to change notification settings - Fork 403
/
MainActivity.kt
169 lines (145 loc) · 5.59 KB
/
MainActivity.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
package com.google.firebase.example.perf.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.example.perf.R
import com.google.firebase.example.perf.kotlin.model.ItemCache
import com.google.firebase.example.perf.kotlin.model.User
import com.google.firebase.Firebase
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.perf.performance
import com.google.firebase.perf.trace
import com.google.firebase.perf.metrics.AddTrace
import com.google.firebase.remoteconfig.remoteConfig
import java.io.DataOutputStream
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
class MainActivity : AppCompatActivity() {
// [START perf_traced_create]
// the `enabled` argument is optional and defaults to true
@AddTrace(name = "onCreateTrace", enabled = true)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// [END perf_traced_create]
fun basicTrace() {
val cache = ItemCache()
// [START perf_basic_trace_start]
val myTrace = Firebase.performance.newTrace("test_trace")
myTrace.start()
// [END perf_basic_trace_start]
// [START perf_basic_trace_increment]
val item = cache.fetch("item")
if (item != null) {
myTrace.incrementMetric("item_cache_hit", 1)
} else {
myTrace.incrementMetric("item_cache_miss", 1)
}
// [END perf_basic_trace_increment]
// [START perf_basic_trace_stop]
myTrace.stop()
// [END perf_basic_trace_stop]
}
fun traceCustomAttributes() {
// [START perf_trace_custom_attrs]
Firebase.performance.newTrace("test_trace").trace {
// Update scenario.
putAttribute("experiment", "A")
// Reading scenario.
val experimentValue = getAttribute("experiment")
// Delete scenario.
removeAttribute("experiment")
// Read attributes.
val traceAttributes = this.attributes
}
// [END perf_trace_custom_attrs]
}
fun disableWithConfig() {
// [START perf_disable_with_config]
// Setup remote config
val config = Firebase.remoteConfig
// You can uncomment the following two statements to permit more fetches when
// validating your app, but you should comment out or delete these lines before
// distributing your app in production.
// val configSettings = remoteConfigSettings {
// minimumFetchIntervalInSeconds = 3600
// }
// config.setConfigSettingsAsync(configSettings)
// Load in-app defaults from an XML file that sets perf_disable to false until you update
// values in the Firebase Console
// Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true
config.setDefaultsAsync(R.xml.remote_config_defaults)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Firebase.performance.isPerformanceCollectionEnabled = !config.getBoolean("perf_disable")
} else {
// An error occurred while setting default parameters
}
}
// [END perf_disable_with_config]
}
fun activateConfig() {
// [START perf_activate_config]
// Remote Config fetches and activates parameter values from the service
val config = Firebase.remoteConfig
config.fetch(3600)
.continueWithTask { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
config.activate()
}
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Parameter values successfully activated
// ...
} else {
// Handle errors
}
}
// [END perf_activate_config]
}
@Throws(Exception::class)
fun manualNetworkTrace() {
val data = "badgerbadgerbadgerbadgerMUSHROOM!".toByteArray()
// [START perf_manual_network_trace]
val url = URL("https://www.google.com")
val metric = Firebase.performance.newHttpMetric(
"https://www.google.com",
FirebasePerformance.HttpMethod.GET,
)
metric.trace {
val conn = url.openConnection() as HttpURLConnection
conn.doOutput = true
conn.setRequestProperty("Content-Type", "application/json")
try {
val outputStream = DataOutputStream(conn.outputStream)
outputStream.write(data)
} catch (ignored: IOException) {
}
// Set HttpMetric attributes
setRequestPayloadSize(data.size.toLong())
setHttpResponseCode(conn.responseCode)
printStreamContent(conn.inputStream)
conn.disconnect()
}
// [END perf_manual_network_trace]
}
fun piiExamples() {
val trace = Firebase.performance.newTrace("trace")
val user = User()
// [START perf_attr_no_pii]
trace.putAttribute("experiment", "A")
// [END perf_attr_no_pii]
// [START perf_attr_pii]
trace.putAttribute("email", user.getEmailAddress())
// [END perf_attr_pii]
}
private fun printStreamContent(stream: InputStream) {
// Unimplemented
// ...
}
}