Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import androidx.core.os.Profiling;
import androidx.core.os.SystemTraceRequestBuilder;
import androidx.core.os.BufferFillPolicy;
import com.example.snippets.R;

public class ProfilingManagerJavaSnippets {
public class MainActivityJava extends Activity {

// [START android_profiling_manager_anr_case_study_java_snippet_2]
private static final int NETWORK_TIMEOUT_MILLISECS = 2000;
// [END android_profiling_manager_anr_case_study_java_snippet_2]
public static final String TAG = "ProfilingManager";

@Override
Expand Down Expand Up @@ -125,5 +128,85 @@ public void setupProfileUploadWorker(String resultFilePath) {
// Setup job to upload the profiling result file.
}
// [END android_profiling_manager_triggered_trace_setup_upload_job_java]

// [START android_profiling_manager_anr_case_study_java_snippet_1]
public void addANRTrigger() {
ProfilingManager profilingManager = getApplicationContext().getSystemService(ProfilingManager.class);
List<ProfilingTrigger> triggers = new ArrayList<>();
ProfilingTrigger.Builder triggerBuilder = new ProfilingTrigger.Builder(ProfilingTrigger.TRIGGER_TYPE_ANR);
triggers.add(triggerBuilder.build());
Executor mainExecutor = Executors.newSingleThreadExecutor();
Consumer<ProfilingResult> resultCallback =
profilingResult -> {
// Handle uploading trace to your back-end
};
profilingManager.registerForAllProfilingResults(mainExecutor, resultCallback);
profilingManager.addProfilingTriggers(triggers);
}
// [END android_profiling_manager_anr_case_study_java_snippet_1]

// [START android_profiling_manager_anr_case_study_java_snippet_2]
public void setupButtonCallback() {
findViewById(R.id.submit).setOnClickListener(submitButtonView -> {
Trace.beginSection("MyApp:SubmitButton");
onClickSubmit();
Trace.endSection();
});
}

public void onClickSubmit() {
prepareNetworkRequest();

boolean networkRequestSuccess = false;
int maxAttempts = 10;
while (!networkRequestSuccess && maxAttempts > 0) {
networkRequestSuccess = performNetworkRequest(NETWORK_TIMEOUT_MILLISECS);
maxAttempts--;
}

if (networkRequestSuccess) {
handleNetworkResponse();
}
}

boolean performNetworkRequest(int timeoutMiliseconds) {
// [START_EXCLUDE]
cpuIntensiveComputation(20);
try {
if (Math.random() < 0.2) {
// Simulate performing a network request by waiting a random period of time
int networkRequestTimeMs = (int)(Math.random() * timeoutMiliseconds);
Thread.sleep(networkRequestTimeMs);
return true;
} else {
// Simulate a timeout
Thread.sleep(timeoutMiliseconds);
}
} catch (InterruptedException e) {}
return false;
// [END_EXCLUDE]
}

// [START_EXCLUDE silent]
void cpuIntensiveComputation(int durationMs) {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < durationMs) {}
}
// [END_EXCLUDE silent]

void prepareNetworkRequest() {
// [START_EXCLUDE]
cpuIntensiveComputation(1000);
// [END_EXCLUDE]
}

public void handleNetworkResponse() {
Trace.beginSection("handleNetworkResponse");
// [START_EXCLUDE]
cpuIntensiveComputation(2000);
// [END_EXCLUDE]
Trace.endSection();
}
// [END android_profiling_manager_anr_case_study_java_snippet_2]
}
}
11 changes: 11 additions & 0 deletions misc/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Button" />

</androidx.constraintlayout.widget.ConstraintLayout>