Skip to content

Commit 9375894

Browse files
Paige McAuliffecopybara-androidxtest
authored andcommitted
Unregister callbacks outside of callback methods
PiperOrigin-RevId: 637996768
1 parent a4eb9b2 commit 9375894

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

espresso/device/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
**Bug Fixes**
88

99
* Add support for setting screen orientation with multiple resumed activities
10+
* Fix concurrent modification issue when setting screen orientation and fold modes
1011

1112
**New Features**
1213

espresso/device/java/androidx/test/espresso/device/action/ScreenOrientationAction.kt

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,51 +91,58 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
9191
if (screenOrientation == ScreenOrientation.LANDSCAPE) Configuration.ORIENTATION_LANDSCAPE
9292
else Configuration.ORIENTATION_PORTRAIT
9393

94-
if (configChangesHandled) {
95-
Log.d(TAG, "The current activity handles configuration changes.")
96-
context.registerComponentCallbacks(
97-
object : ComponentCallbacks {
98-
override fun onConfigurationChanged(newConfig: Configuration) {
99-
if (newConfig.orientation == requestedOrientation) {
100-
if (Log.isLoggable(TAG, Log.DEBUG)) {
101-
Log.d(TAG, "Application's orientation was set to the requested orientation.")
102-
}
103-
context.unregisterComponentCallbacks(this)
104-
latch.countDown()
94+
val componentCallback =
95+
object : ComponentCallbacks {
96+
override fun onConfigurationChanged(newConfig: Configuration) {
97+
if (newConfig.orientation == requestedOrientation) {
98+
if (Log.isLoggable(TAG, Log.DEBUG)) {
99+
Log.d(TAG, "Application's orientation was set to the requested orientation.")
105100
}
101+
latch.countDown()
106102
}
103+
}
107104

108-
override fun onLowMemory() {}
105+
@Deprecated("Deprecated in API 34") override fun onLowMemory() {}
106+
}
107+
108+
val activityLifecycleCallback =
109+
object : ActivityLifecycleCallback {
110+
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
111+
if (
112+
activity.localClassName == currentActivityName &&
113+
stage == Stage.RESUMED &&
114+
activity.resources.configuration.orientation == requestedOrientation
115+
) {
116+
if (Log.isLoggable(TAG, Log.DEBUG)) {
117+
Log.d(TAG, "Test activity was resumed in the requested orientation.")
118+
}
119+
latch.countDown()
120+
}
109121
}
110-
)
122+
}
123+
124+
if (configChangesHandled) {
125+
Log.d(TAG, "The current activity handles configuration changes.")
126+
context.registerComponentCallbacks(componentCallback)
111127
} else {
112128
Log.d(
113129
TAG,
114130
"The current activity does not handle configuration changes and will be recreated when " +
115-
"its orientation changes."
131+
"its orientation changes.",
116132
)
117-
ActivityLifecycleMonitorRegistry.getInstance()
118-
.addLifecycleCallback(
119-
object : ActivityLifecycleCallback {
120-
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
121-
if (
122-
activity.localClassName == currentActivityName &&
123-
stage == Stage.RESUMED &&
124-
activity.resources.configuration.orientation == requestedOrientation
125-
) {
126-
if (Log.isLoggable(TAG, Log.DEBUG)) {
127-
Log.d(TAG, "Test activity was resumed in the requested orientation.")
128-
}
129-
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
130-
latch.countDown()
131-
}
132-
}
133-
}
134-
)
133+
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(activityLifecycleCallback)
135134
}
135+
136136
deviceController.setScreenOrientation(screenOrientation.getOrientation())
137137
latch.await(5, TimeUnit.SECONDS)
138138

139+
if (configChangesHandled) {
140+
context.unregisterComponentCallbacks(componentCallback)
141+
} else {
142+
ActivityLifecycleMonitorRegistry.getInstance()
143+
.removeLifecycleCallback(activityLifecycleCallback)
144+
}
145+
139146
// Restore accelerometer rotation setting if it was changed
140147
if (
141148
getDeviceApiLevel() >= 21 && startingAccelRotationSetting != getAccelerometerRotationSetting()

espresso/device/java/androidx/test/espresso/device/common/ActivityUtil.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,19 @@ fun getResumedActivityOrNull(): Activity? {
6969
} else if (activities.isEmpty()) {
7070
Log.d(TAG, "No activity found in the RESUMED stage. Waiting up to 2 seconds for one.")
7171
val latch = CountDownLatch(1)
72-
ActivityLifecycleMonitorRegistry.getInstance()
73-
.addLifecycleCallback(
74-
object : ActivityLifecycleCallback {
75-
override fun onActivityLifecycleChanged(newActivity: Activity, stage: Stage) {
76-
if (stage == Stage.RESUMED) {
77-
Log.d(TAG, "Found ${newActivity.getLocalClassName()} in the RESUMED stage.")
78-
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
79-
latch.countDown()
80-
activity = newActivity
81-
}
72+
val callback =
73+
object : ActivityLifecycleCallback {
74+
override fun onActivityLifecycleChanged(newActivity: Activity, stage: Stage) {
75+
if (stage == Stage.RESUMED) {
76+
Log.d(TAG, "Found ${newActivity.localClassName} in the RESUMED stage.")
77+
latch.countDown()
78+
activity = newActivity
8279
}
8380
}
84-
)
81+
}
82+
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(callback)
8583
latch.await(2, TimeUnit.SECONDS)
84+
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(callback)
8685
} else {
8786
activity = activities.elementAtOrNull(0)
8887
}

0 commit comments

Comments
 (0)