Send preDraw message at front of queue pre API 24
Prior to API level 24, JankStats uses an OnPreDrawListener to track frame
times. The handler for posting these messages originally posted them at the
front of the queue. This CL returns to that logic.
Bug: 233358407
Test: JankStatsTest runs, manual testing
RelNote: "Return to original logic of posting OnPreDrawListener messages at
front of queue, for more consistent and predictable frame timing."
Change-Id: I05a434fe9453ea1be28d398e3eb284dd9b0cb64a
diff --git a/metrics/integration-tests/janktest/src/main/AndroidManifest.xml b/metrics/integration-tests/janktest/src/main/AndroidManifest.xml
index 5a9eabd..3250f3b 100644
--- a/metrics/integration-tests/janktest/src/main/AndroidManifest.xml
+++ b/metrics/integration-tests/janktest/src/main/AndroidManifest.xml
@@ -34,6 +34,17 @@
</intent-filter>
</activity>
<activity
+ android:name=".SimpleLoggingActivity"
+ android:label="@string/app_name"
+ android:exported="true"
+ android:theme="@style/Theme.Androidx.NoActionBar">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ <activity
android:name=".JankAggregatorActivity"
android:label="@string/app_name"
android:exported="true"
diff --git a/metrics/integration-tests/janktest/src/main/java/androidx/metrics/performance/janktest/SimpleLoggingActivity.kt b/metrics/integration-tests/janktest/src/main/java/androidx/metrics/performance/janktest/SimpleLoggingActivity.kt
new file mode 100644
index 0000000..ae5d51f
--- /dev/null
+++ b/metrics/integration-tests/janktest/src/main/java/androidx/metrics/performance/janktest/SimpleLoggingActivity.kt
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.metrics.performance.janktest
+
+import android.os.Bundle
+import android.util.Log
+import android.view.View
+import androidx.appcompat.app.AppCompatActivity
+import androidx.metrics.performance.FrameData
+import androidx.metrics.performance.JankStats
+import androidx.metrics.performance.PerformanceMetricsState
+import java.util.Date
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.asExecutor
+
+class SimpleLoggingActivity : AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.simple_layout)
+
+ JankStats.createAndTrack(window, Dispatchers.Default.asExecutor(),
+ object : JankStats.OnFrameListener {
+ override fun onFrame(frameData: FrameData) {
+ Log.d("MainActivity", frameData.toString())
+ }
+ })
+
+ findViewById<View>(R.id.button).setOnClickListener {
+ val stateHolder = PerformanceMetricsState.getForHierarchy(it).state!!
+ stateHolder.addSingleFrameState("stateKey1", "stateValue")
+ stateHolder.addState("stateKey2", "${Date()}")
+ }
+ }
+}
\ No newline at end of file
diff --git a/metrics/integration-tests/janktest/src/main/res/layout/simple_layout.xml b/metrics/integration-tests/janktest/src/main/res/layout/simple_layout.xml
new file mode 100644
index 0000000..fd53e82
--- /dev/null
+++ b/metrics/integration-tests/janktest/src/main/res/layout/simple_layout.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ Copyright 2022 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <Button
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Click Me"
+ android:id="@+id/button"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/metrics/metrics-performance/src/main/java/androidx/metrics/performance/JankStatsApi16Impl.kt b/metrics/metrics-performance/src/main/java/androidx/metrics/performance/JankStatsApi16Impl.kt
index e0774cc..88be2de 100644
--- a/metrics/metrics-performance/src/main/java/androidx/metrics/performance/JankStatsApi16Impl.kt
+++ b/metrics/metrics-performance/src/main/java/androidx/metrics/performance/JankStatsApi16Impl.kt
@@ -175,7 +175,7 @@
decorView?.let {
val frameStart = getFrameStartTime()
with(decorView) {
- handler.sendMessage(Message.obtain(handler) {
+ handler.sendMessageAtFrontOfQueue(Message.obtain(handler) {
val now = System.nanoTime()
val expectedDuration = getExpectedFrameDuration(decorView)
// prevent concurrent modification of delegates list by synchronizing on