/
MainActivity.java
164 lines (137 loc) · 5.89 KB
/
MainActivity.java
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
package com.google.firebase.example.perf;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.example.perf.kotlin.model.Item;
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.perf.metrics.HttpMetric;
import com.google.firebase.perf.metrics.Trace;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import devrel.firebase.google.com.firebaseoptions.R;
public class MainActivity extends AppCompatActivity {
// [START perf_traced_create]
@Override
@AddTrace(name = "onCreateTrace", enabled = true /* optional */)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// [END perf_traced_create]
public void basicTrace() {
ItemCache cache = new ItemCache();
// [START perf_basic_trace_start]
Trace myTrace = FirebasePerformance.getInstance().newTrace("test_trace");
myTrace.start();
// [END perf_basic_trace_start]
// [START perf_basic_trace_increment]
Item 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]
}
public void traceCustomAttributes() {
// [START perf_trace_custom_attrs]
Trace trace = FirebasePerformance.getInstance().newTrace("test_trace");
// Update scenario.
trace.putAttribute("experiment", "A");
// Reading scenario.
String experimentValue = trace.getAttribute("experiment");
// Delete scenario.
trace.removeAttribute("experiment");
// Read attributes.
Map<String, String> traceAttributes = trace.getAttributes();
// [END perf_trace_custom_attrs]
}
public void disableWithConfig() {
// [START perf_disable_with_config]
// Setup remote config
FirebaseRemoteConfig 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);
if (config.getBoolean("perf_disable")) {
FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false);
} else {
FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true);
}
// [END perf_disable_with_config]
}
public void activateConfig() {
// [START perf_activate_config]
//Remote Config fetches and activates parameter values from the service
final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
config.fetch(3600)
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
config.activateFetched();
} else {
// ...
}
}
});
// [END perf_activate_config]
}
public void manualNetworkTrace() throws Exception {
byte[] data = "badgerbadgerbadgerbadgerMUSHROOM!".getBytes();
// [START perf_manual_network_trace]
HttpMetric metric =
FirebasePerformance.getInstance().newHttpMetric("https://www.google.com",
FirebasePerformance.HttpMethod.GET);
final URL url = new URL("https://www.google.com");
metric.start();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
try {
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
outputStream.write(data);
} catch (IOException ignored) {
}
metric.setRequestPayloadSize(data.length);
metric.setHttpResponseCode(conn.getResponseCode());
printStreamContent(conn.getInputStream());
conn.disconnect();
metric.stop();
// [END perf_manual_network_trace]
}
public void piiExamples() {
Trace trace = FirebasePerformance.getInstance().newTrace("trace");
User user = new 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]
}
public void printStreamContent(InputStream inputStream) {
// Unimplemented
// ...
}
}