Skip to content

Commit f86ffef

Browse files
committed
Add snippets for anr case study for ProfilingManager v2 docs
1 parent 416e124 commit f86ffef

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

misc/src/main/java/com/example/snippets/profiling/ProfilingManagerJavaSnippets.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import androidx.core.os.Profiling;
1919
import androidx.core.os.SystemTraceRequestBuilder;
2020
import androidx.core.os.BufferFillPolicy;
21+
import com.example.snippets.R;
2122

2223
public class ProfilingManagerJavaSnippets {
2324
public class MainActivityJava extends Activity {
24-
25+
// [START android_profiling_manager_anr_case_study_java_snippet_2]
26+
private static final int NETWORK_TIMEOUT_MILLISECS = 2000;
27+
// [END android_profiling_manager_anr_case_study_java_snippet_2]
2528
public static final String TAG = "ProfilingManager";
2629

2730
@Override
@@ -110,6 +113,84 @@ public void accept(ProfilingResult profilingResult) {
110113
}
111114
// [END android_profiling_manager_triggered_trace_java]
112115

113-
116+
// [START android_profiling_manager_anr_case_study_java_snippet_1]
117+
public void addANRTrigger() {
118+
ProfilingManager profilingManager = getApplicationContext().getSystemService(ProfilingManager.class);
119+
List<ProfilingTrigger> triggers = new ArrayList<>();
120+
ProfilingTrigger.Builder triggerBuilder = new ProfilingTrigger.Builder(ProfilingTrigger.TRIGGER_TYPE_ANR);
121+
triggers.add(triggerBuilder.build());
122+
Executor mainExecutor = Executors.newSingleThreadExecutor();
123+
Consumer<ProfilingResult> resultCallback =
124+
profilingResult -> {
125+
// Handle uploading trace to your back-end
126+
};
127+
profilingManager.registerForAllProfilingResults(mainExecutor, resultCallback);
128+
profilingManager.addProfilingTriggers(triggers);
129+
}
130+
// [END android_profiling_manager_anr_case_study_java_snippet_1]
131+
132+
// [START android_profiling_manager_anr_case_study_java_snippet_2]
133+
public void setupButtonCallback() {
134+
findViewById(R.id.submit).setOnClickListener(submitButtonView -> {
135+
Trace.beginSection("MyApp:SubmitButton");
136+
onClickSubmit();
137+
Trace.endSection();
138+
});
139+
}
140+
141+
public void onClickSubmit() {
142+
prepareNetworkRequest();
143+
144+
boolean networkRequestSuccess = false;
145+
int maxAttempts = 10;
146+
while (!networkRequestSuccess && maxAttempts > 0) {
147+
networkRequestSuccess = performNetworkRequest(NETWORK_TIMEOUT_MILLISECS);
148+
maxAttempts--;
149+
}
150+
151+
if (networkRequestSuccess) {
152+
handleNetworkResponse();
153+
}
154+
}
155+
156+
boolean performNetworkRequest(int timeoutMiliseconds) {
157+
// [START_EXCLUDE]
158+
cpuIntensiveComputation(20);
159+
try {
160+
if (Math.random() < 0.2) {
161+
// Simulate performing a network request by waiting a random period of time
162+
int networkRequestTimeMs = (int)(Math.random() * timeoutMiliseconds);
163+
Thread.sleep(networkRequestTimeMs);
164+
return true;
165+
} else {
166+
// Simulate a timeout
167+
Thread.sleep(timeoutMiliseconds);
168+
}
169+
} catch (InterruptedException e) {}
170+
return false;
171+
// [END_EXCLUDE]
172+
}
173+
174+
// [START_EXCLUDE silent]
175+
void cpuIntensiveComputation(int durationMs) {
176+
long start = System.currentTimeMillis();
177+
while (System.currentTimeMillis() - start < durationMs) {}
178+
}
179+
// [END_EXCLUDE silent]
180+
181+
void prepareNetworkRequest() {
182+
// [START_EXCLUDE]
183+
cpuIntensiveComputation(1000);
184+
// [END_EXCLUDE]
185+
}
186+
187+
public void handleNetworkResponse() {
188+
Trace.beginSection("handleNetworkResponse");
189+
// [START_EXCLUDE]
190+
cpuIntensiveComputation(2000);
191+
// [END_EXCLUDE]
192+
Trace.endSection();
193+
}
194+
// [END android_profiling_manager_anr_case_study_java_snippet_2]
114195
}
115196
}

misc/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@
4040
app:layout_constraintRight_toRightOf="parent"
4141
app:layout_constraintTop_toTopOf="parent" />
4242

43+
<Button
44+
android:id="@+id/submit"
45+
android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:layout_margin="150dp"
48+
android:text="Button" />
49+
4350
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)