|
18 | 18 | import androidx.core.os.Profiling; |
19 | 19 | import androidx.core.os.SystemTraceRequestBuilder; |
20 | 20 | import androidx.core.os.BufferFillPolicy; |
| 21 | +import com.example.snippets.R; |
21 | 22 |
|
22 | 23 | public class ProfilingManagerJavaSnippets { |
23 | 24 | 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] |
25 | 28 | public static final String TAG = "ProfilingManager"; |
26 | 29 |
|
27 | 30 | @Override |
@@ -125,5 +128,85 @@ public void setupProfileUploadWorker(String resultFilePath) { |
125 | 128 | // Setup job to upload the profiling result file. |
126 | 129 | } |
127 | 130 | // [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] |
128 | 211 | } |
129 | 212 | } |
0 commit comments