Skip to content

Commit fefd41b

Browse files
committed
ProfilingManager docs V2: snippets for ANR section
1 parent 95aeebd commit fefd41b

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

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

Lines changed: 84 additions & 1 deletion
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
@@ -125,5 +128,85 @@ public void setupProfileUploadWorker(String resultFilePath) {
125128
// Setup job to upload the profiling result file.
126129
}
127130
// [END android_profiling_manager_triggered_trace_setup_upload_job_java]
131+
132+
// [START android_profiling_manager_anr_case_study_java_snippet_1]
133+
public void addANRTrigger() {
134+
ProfilingManager profilingManager = getApplicationContext().getSystemService(ProfilingManager.class);
135+
List<ProfilingTrigger> triggers = new ArrayList<>();
136+
ProfilingTrigger.Builder triggerBuilder = new ProfilingTrigger.Builder(ProfilingTrigger.TRIGGER_TYPE_ANR);
137+
triggers.add(triggerBuilder.build());
138+
Executor mainExecutor = Executors.newSingleThreadExecutor();
139+
Consumer<ProfilingResult> resultCallback =
140+
profilingResult -> {
141+
// Handle uploading trace to your back-end
142+
};
143+
profilingManager.registerForAllProfilingResults(mainExecutor, resultCallback);
144+
profilingManager.addProfilingTriggers(triggers);
145+
}
146+
// [END android_profiling_manager_anr_case_study_java_snippet_1]
147+
148+
// [START android_profiling_manager_anr_case_study_java_snippet_2]
149+
public void setupButtonCallback() {
150+
findViewById(R.id.submit).setOnClickListener(submitButtonView -> {
151+
Trace.beginSection("MyApp:SubmitButton");
152+
onClickSubmit();
153+
Trace.endSection();
154+
});
155+
}
156+
157+
public void onClickSubmit() {
158+
prepareNetworkRequest();
159+
160+
boolean networkRequestSuccess = false;
161+
int maxAttempts = 10;
162+
while (!networkRequestSuccess && maxAttempts > 0) {
163+
networkRequestSuccess = performNetworkRequest(NETWORK_TIMEOUT_MILLISECS);
164+
maxAttempts--;
165+
}
166+
167+
if (networkRequestSuccess) {
168+
handleNetworkResponse();
169+
}
170+
}
171+
172+
boolean performNetworkRequest(int timeoutMiliseconds) {
173+
// [START_EXCLUDE]
174+
cpuIntensiveComputation(20);
175+
try {
176+
if (Math.random() < 0.2) {
177+
// Simulate performing a network request by waiting a random period of time
178+
int networkRequestTimeMs = (int)(Math.random() * timeoutMiliseconds);
179+
Thread.sleep(networkRequestTimeMs);
180+
return true;
181+
} else {
182+
// Simulate a timeout
183+
Thread.sleep(timeoutMiliseconds);
184+
}
185+
} catch (InterruptedException e) {}
186+
return false;
187+
// [END_EXCLUDE]
188+
}
189+
190+
// [START_EXCLUDE silent]
191+
void cpuIntensiveComputation(int durationMs) {
192+
long start = System.currentTimeMillis();
193+
while (System.currentTimeMillis() - start < durationMs) {}
194+
}
195+
// [END_EXCLUDE silent]
196+
197+
void prepareNetworkRequest() {
198+
// [START_EXCLUDE]
199+
cpuIntensiveComputation(1000);
200+
// [END_EXCLUDE]
201+
}
202+
203+
public void handleNetworkResponse() {
204+
Trace.beginSection("handleNetworkResponse");
205+
// [START_EXCLUDE]
206+
cpuIntensiveComputation(2000);
207+
// [END_EXCLUDE]
208+
Trace.endSection();
209+
}
210+
// [END android_profiling_manager_anr_case_study_java_snippet_2]
128211
}
129212
}

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)