Skip to content

Commit 58a12f9

Browse files
Upgrade sample to material3
1 parent 2717559 commit 58a12f9

File tree

13 files changed

+156
-88
lines changed

13 files changed

+156
-88
lines changed

MacrobenchmarkSample/app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ plugins {
2222
}
2323

2424
android {
25-
compileSdk = 35
25+
compileSdk = 36
2626
namespace = "com.example.macrobenchmark.target"
2727

2828
defaultConfig {
2929
applicationId = "com.example.macrobenchmark.target"
3030
versionCode = 1
3131
versionName = "1.0"
3232
minSdk = 24
33-
targetSdk = 35
33+
targetSdk = 36
3434
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
3535
}
3636

MacrobenchmarkSample/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
android:theme="@style/Theme.Macrobenchmarktarget">
1515
<profileable
1616
android:shell="true"
17-
tools:targetApi="29,q" />
17+
tools:targetApi="29" />
1818
<!-- [END macrobenchmark_setup_profileable] -->
1919
<!--
2020
Activities need to be exported so the Macrobenchmarks can discover them

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/FullyDrawnStartupActivity.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.ReportDrawnWhen
2222
import androidx.activity.compose.setContent
2323
import androidx.activity.viewModels
24+
import androidx.compose.foundation.layout.Box
2425
import androidx.compose.foundation.layout.Column
25-
import androidx.compose.material.CircularProgressIndicator
26-
import androidx.compose.material.Text
26+
import androidx.compose.material3.CircularProgressIndicator
27+
import androidx.compose.material3.MaterialTheme
28+
import androidx.compose.material3.Text
2729
import androidx.compose.runtime.LaunchedEffect
2830
import androidx.compose.runtime.getValue
2931
import androidx.compose.runtime.mutableStateOf
@@ -38,19 +40,23 @@ class FullyDrawnStartupActivity : ComponentActivity() {
3840
override fun onCreate(savedInstanceState: Bundle?) {
3941
super.onCreate(savedInstanceState)
4042
setContent {
41-
var isLoaded by remember { mutableStateOf(false) }
42-
ReportDrawnWhen { isLoaded }
43+
MaterialTheme {
44+
Box {
45+
var isLoaded by remember { mutableStateOf(false) }
46+
ReportDrawnWhen { isLoaded }
4347

44-
LaunchedEffect(Unit) {
45-
isLoaded = sampleViewModel.data.isReady()
46-
}
48+
LaunchedEffect(Unit) {
49+
isLoaded = sampleViewModel.data.isReady()
50+
}
4751

48-
Column {
49-
Text("Compose Macrobenchmark Target")
50-
if (!isLoaded) {
51-
CircularProgressIndicator()
52-
} else {
53-
Text("Fully Drawn")
52+
Column {
53+
Text("Compose Macrobenchmark Target")
54+
if (!isLoaded) {
55+
CircularProgressIndicator()
56+
} else {
57+
Text("Fully Drawn")
58+
}
59+
}
5460
}
5561
}
5662
}

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/MainActivity.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import androidx.compose.foundation.BorderStroke
2626
import androidx.compose.foundation.layout.Column
2727
import androidx.compose.foundation.layout.fillMaxWidth
2828
import androidx.compose.foundation.layout.padding
29-
import androidx.compose.material.MaterialTheme
30-
import androidx.compose.material.Text
31-
import androidx.compose.material.TextButton
32-
import androidx.compose.material.TopAppBar
29+
import androidx.compose.material3.ExperimentalMaterial3Api
30+
import androidx.compose.material3.MaterialTheme
31+
import androidx.compose.material3.Text
32+
import androidx.compose.material3.TextButton
33+
import androidx.compose.material3.TopAppBar
3334
import androidx.compose.runtime.Composable
3435
import androidx.compose.ui.ExperimentalComposeUiApi
3536
import androidx.compose.ui.Modifier
@@ -57,7 +58,7 @@ private const val TAG = "MainActivity"
5758

5859
class MainActivity : ComponentActivity() {
5960

60-
@OptIn(ExperimentalComposeUiApi::class)
61+
@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
6162
override fun onCreate(savedInstanceState: Bundle?) {
6263
super.onCreate(savedInstanceState)
6364
setContent {
@@ -132,7 +133,7 @@ class MainActivity : ComponentActivity() {
132133
.padding(8.dp)
133134
.testTag(testTag),
134135
onClick = onClick,
135-
border = BorderStroke(1.dp, MaterialTheme.colors.primary)
136+
border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary)
136137
) {
137138
Text(name)
138139
}

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/clicklatency/ComposeActivity.kt

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
2222
import androidx.appcompat.app.AlertDialog
2323
import androidx.compose.foundation.clickable
24-
import androidx.compose.foundation.layout.Box
2524
import androidx.compose.foundation.layout.Column
2625
import androidx.compose.foundation.layout.Row
2726
import androidx.compose.foundation.layout.Spacer
@@ -30,11 +29,13 @@ import androidx.compose.foundation.layout.padding
3029
import androidx.compose.foundation.layout.wrapContentSize
3130
import androidx.compose.foundation.lazy.LazyColumn
3231
import androidx.compose.foundation.lazy.items
33-
import androidx.compose.material.Card
34-
import androidx.compose.material.Checkbox
35-
import androidx.compose.material.MaterialTheme
36-
import androidx.compose.material.Text
37-
import androidx.compose.material.TextField
32+
import androidx.compose.material3.Card
33+
import androidx.compose.material3.Checkbox
34+
import androidx.compose.material3.ExperimentalMaterial3Api
35+
import androidx.compose.material3.MaterialTheme
36+
import androidx.compose.material3.Text
37+
import androidx.compose.material3.TextField
38+
import androidx.compose.material3.TopAppBar
3839
import androidx.compose.runtime.Composable
3940
import androidx.compose.runtime.getValue
4041
import androidx.compose.runtime.mutableStateOf
@@ -43,9 +44,6 @@ import androidx.compose.runtime.setValue
4344
import androidx.compose.ui.Alignment
4445
import androidx.compose.ui.ExperimentalComposeUiApi
4546
import androidx.compose.ui.Modifier
46-
import androidx.compose.ui.platform.testTag
47-
import androidx.compose.ui.semantics.semantics
48-
import androidx.compose.ui.semantics.testTagsAsResourceId
4947
import androidx.compose.ui.tooling.preview.Preview
5048
import androidx.compose.ui.unit.dp
5149
import androidx.tracing.trace
@@ -68,49 +66,35 @@ class ComposeActivity : ComponentActivity() {
6866
}
6967
}
7068

69+
@OptIn(ExperimentalMaterial3Api::class)
7170
@Composable
7271
private fun BenchmarkComposeList(data: List<Entry>) {
7372
MaterialTheme {
74-
Box(
75-
modifier = Modifier.semantics {
76-
// Allows to use testTag() for UiAutomator's resource-id.
77-
// It can be enabled high in the compose hierarchy,
78-
// so that it's enabled for the whole subtree
79-
testTagsAsResourceId = true
80-
}
81-
) {
82-
// Thanks to [SemanticsPropertyReceiver.testTagsAsResourceId],
83-
// [Modifier.testTag]s will be propagated to resource-id
84-
// and can be accessed from benchmarks.
85-
var value by remember { mutableStateOf("Enter text here") }
86-
Column(modifier = Modifier.fillMaxWidth()) {
87-
TextField(
88-
modifier = Modifier
89-
.fillMaxWidth()
90-
.testTag("input"),
91-
value = value,
92-
onValueChange = { value = it },
93-
placeholder = { Text("Enter text here") }
94-
)
73+
var value by remember { mutableStateOf("Enter text here") }
74+
Column(modifier = Modifier.fillMaxWidth()) {
75+
TopAppBar(title = { Text("Compose Sample") })
76+
TextField(
77+
modifier = Modifier
78+
.fillMaxWidth(),
79+
value = value,
80+
onValueChange = { value = it },
81+
placeholder = { Text("Enter text here") }
82+
)
9583

96-
LazyColumn(
97-
modifier = Modifier
98-
.testTag("myLazyColumn")
99-
) {
100-
items(data, key = { it.contents }) { item ->
101-
EntryRow(
102-
entry = item,
103-
modifier = Modifier
104-
.padding(8.dp)
105-
.clickable {
106-
ClickTrace.onClickPerformed()
107-
AlertDialog
108-
.Builder(this@ComposeActivity)
109-
.setMessage("Item clicked")
110-
.show()
111-
}
112-
)
113-
}
84+
LazyColumn {
85+
items(data, key = { it.contents }) { item ->
86+
EntryRow(
87+
entry = item,
88+
modifier = Modifier
89+
.padding(8.dp)
90+
.clickable {
91+
ClickTrace.onClickPerformed()
92+
AlertDialog
93+
.Builder(this@ComposeActivity)
94+
.setMessage("Item clicked")
95+
.show()
96+
}
97+
)
11498
}
11599
}
116100
}

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/clicklatency/ListViewActivity.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import android.widget.BaseAdapter
2424
import android.widget.TextView
2525
import androidx.appcompat.app.AlertDialog
2626
import androidx.appcompat.app.AppCompatActivity
27+
import androidx.core.view.ViewCompat
28+
import androidx.core.view.WindowInsetsCompat
29+
import androidx.core.view.updatePadding
2730
import com.example.macrobenchmark.target.databinding.ActivityListViewBinding
2831
import com.example.macrobenchmark.target.recyclerview.Entry
2932
import com.example.macrobenchmark.target.util.ClickTrace
@@ -73,5 +76,19 @@ class ListViewActivity : AppCompatActivity() {
7376
.show()
7477
}
7578

79+
ViewCompat.setOnApplyWindowInsetsListener(binding.listview) { v, insets ->
80+
val bars = insets.getInsets(
81+
WindowInsetsCompat.Type.systemBars()
82+
or WindowInsetsCompat.Type.displayCutout()
83+
)
84+
v.updatePadding(
85+
left = bars.left,
86+
top = bars.top,
87+
right = bars.right,
88+
bottom = bars.bottom,
89+
)
90+
WindowInsetsCompat.CONSUMED
91+
}
92+
7693
}
7794
}

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/clicklatency/NestedRecyclerActivity.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package com.example.macrobenchmark.target.activity.clicklatency
1919
import android.os.Bundle
2020
import androidx.activity.viewModels
2121
import androidx.appcompat.app.AppCompatActivity
22+
import androidx.core.view.ViewCompat
23+
import androidx.core.view.WindowInsetsCompat
24+
import androidx.core.view.updatePadding
2225
import androidx.lifecycle.Lifecycle
2326
import androidx.lifecycle.lifecycleScope
2427
import androidx.lifecycle.repeatOnLifecycle
@@ -55,6 +58,20 @@ class NestedRecyclerActivity : AppCompatActivity() {
5558
}
5659
}
5760
}
61+
62+
ViewCompat.setOnApplyWindowInsetsListener(binding.recycler) { v, insets ->
63+
val bars = insets.getInsets(
64+
WindowInsetsCompat.Type.systemBars()
65+
or WindowInsetsCompat.Type.displayCutout()
66+
)
67+
v.updatePadding(
68+
left = bars.left,
69+
top = bars.top,
70+
right = bars.right,
71+
bottom = bars.bottom,
72+
)
73+
WindowInsetsCompat.CONSUMED
74+
}
5875
}
5976
}
6077

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/clicklatency/RecyclerViewActivity.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package com.example.macrobenchmark.target.activity.clicklatency
1818

1919
import android.os.Bundle
2020
import androidx.appcompat.app.AppCompatActivity
21+
import androidx.core.view.ViewCompat
22+
import androidx.core.view.WindowInsetsCompat
23+
import androidx.core.view.updatePadding
2124
import androidx.recyclerview.widget.LinearLayoutManager
2225
import com.example.macrobenchmark.target.databinding.ActivityRecyclerViewBinding
2326
import com.example.macrobenchmark.target.recyclerview.Entry
@@ -30,6 +33,20 @@ open class RecyclerViewActivity : AppCompatActivity() {
3033
val binding = ActivityRecyclerViewBinding.inflate(layoutInflater)
3134
setContentView(binding.root)
3235

36+
ViewCompat.setOnApplyWindowInsetsListener(binding.recycler) { v, insets ->
37+
val bars = insets.getInsets(
38+
WindowInsetsCompat.Type.systemBars()
39+
or WindowInsetsCompat.Type.displayCutout()
40+
)
41+
v.updatePadding(
42+
left = bars.left,
43+
top = bars.top,
44+
right = bars.right,
45+
bottom = bars.bottom,
46+
)
47+
WindowInsetsCompat.CONSUMED
48+
}
49+
3350
// This argument allows the Macrobenchmark tests control the content being tested.
3451
// In your app, you could use this approach to navigate to a consistent UI.
3552
// e.g. Here the UI is being populated with a well known number of list items.

MacrobenchmarkSample/app/src/main/kotlin/com/example/macrobenchmark/target/activity/clicklatency/ScrollViewActivity.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import android.view.LayoutInflater
2121
import android.widget.TextView
2222
import androidx.appcompat.app.AlertDialog
2323
import androidx.appcompat.app.AppCompatActivity
24+
import androidx.core.view.ViewCompat
25+
import androidx.core.view.WindowInsetsCompat
26+
import androidx.core.view.updatePadding
2427
import com.example.macrobenchmark.target.R
2528
import com.example.macrobenchmark.target.databinding.ActivityScrollViewBinding
2629
import com.example.macrobenchmark.target.recyclerview.Entry
@@ -36,6 +39,21 @@ class ScrollViewActivity : AppCompatActivity() {
3639
title = "ScrollView Sample"
3740
val binding = ActivityScrollViewBinding.inflate(layoutInflater)
3841
setContentView(binding.root)
42+
43+
ViewCompat.setOnApplyWindowInsetsListener(binding.scrollcontent) { v, insets ->
44+
val bars = insets.getInsets(
45+
WindowInsetsCompat.Type.systemBars()
46+
or WindowInsetsCompat.Type.displayCutout()
47+
)
48+
v.updatePadding(
49+
left = bars.left,
50+
top = bars.top,
51+
right = bars.right,
52+
bottom = bars.bottom,
53+
)
54+
WindowInsetsCompat.CONSUMED
55+
}
56+
3957
val itemCount = intent.getIntExtra(RecyclerViewActivity.EXTRA_ITEM_COUNT, 1000)
4058

4159
val items = List(itemCount) {

0 commit comments

Comments
 (0)