|
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 |
@@ -110,6 +113,84 @@ public void accept(ProfilingResult profilingResult) { |
110 | 113 | } |
111 | 114 | // [END android_profiling_manager_triggered_trace_java] |
112 | 115 |
|
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] |
114 | 195 | } |
115 | 196 | } |
0 commit comments