/
MainActivity.kt
146 lines (122 loc) · 5.02 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
package com.google.firebase.example.perf.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.example.perf.kotlin.model.ItemCache
import com.google.firebase.example.perf.kotlin.model.User
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.perf.metrics.AddTrace
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import devrel.firebase.google.com.firebaseoptions.R
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]
@AddTrace(name = "onCreateTrace", enabled = true /* optional */)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// [END perf_traced_create]
fun basicTrace() {
val cache = ItemCache()
// [START perf_basic_trace_start]
val myTrace = FirebasePerformance.getInstance().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]
val trace = FirebasePerformance.getInstance().newTrace("test_trace")
// Update scenario.
trace.putAttribute("experiment", "A")
// Reading scenario.
val experimentValue = trace.getAttribute("experiment")
// Delete scenario.
trace.removeAttribute("experiment")
// Read attributes.
val traceAttributes = trace.attributes
// [END perf_trace_custom_attrs]
}
fun disableWithConfig() {
// [START perf_disable_with_config]
// Setup remote config
val config = FirebaseRemoteConfig.getInstance()
// 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.
// FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
// .setDeveloperModeEnabled(BuildConfig.DEBUG)
// .build();
// mFirebaseRemoteConfig.setConfigSettings(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.setDefaults(R.xml.remote_config_defaults)
FirebasePerformance.getInstance().isPerformanceCollectionEnabled = !config.getBoolean("perf_disable")
// [END perf_disable_with_config]
}
fun activateConfig() {
// [START perf_activate_config]
// Remote Config fetches and activates parameter values from the service
val config = FirebaseRemoteConfig.getInstance()
config.fetch(3600)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
config.activateFetched()
} else {
// ...
}
}
// [END perf_activate_config]
}
@Throws(Exception::class)
fun manualNetworkTrace() {
val data = "badgerbadgerbadgerbadgerMUSHROOM!".toByteArray()
// [START perf_manual_network_trace]
val metric = FirebasePerformance.getInstance().newHttpMetric("https://www.google.com",
FirebasePerformance.HttpMethod.GET)
val url = URL("https://www.google.com")
metric.start()
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) {
}
metric.setRequestPayloadSize(data.size.toLong())
metric.setHttpResponseCode(conn.responseCode)
printStreamContent(conn.inputStream)
conn.disconnect()
metric.stop()
// [END perf_manual_network_trace]
}
fun piiExamples() {
val trace = FirebasePerformance.getInstance().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
// ...
}
}