Skip to content

Commit

Permalink
Unregister callbacks outside of callback methods
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 636599205
  • Loading branch information
Paige McAuliffe authored and copybara-androidxtest committed May 28, 2024
1 parent a4eb9b2 commit 2935194
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
1 change: 1 addition & 0 deletions espresso/device/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug Fixes**

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

**New Features**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,58 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
if (screenOrientation == ScreenOrientation.LANDSCAPE) Configuration.ORIENTATION_LANDSCAPE
else Configuration.ORIENTATION_PORTRAIT

if (configChangesHandled) {
Log.d(TAG, "The current activity handles configuration changes.")
context.registerComponentCallbacks(
object : ComponentCallbacks {
override fun onConfigurationChanged(newConfig: Configuration) {
if (newConfig.orientation == requestedOrientation) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Application's orientation was set to the requested orientation.")
}
context.unregisterComponentCallbacks(this)
latch.countDown()
val componentCallback =
object : ComponentCallbacks {
override fun onConfigurationChanged(newConfig: Configuration) {
if (newConfig.orientation == requestedOrientation) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Application's orientation was set to the requested orientation.")
}
latch.countDown()
}
}

override fun onLowMemory() {}
@Deprecated("Deprecated in API 34") override fun onLowMemory() {}
}

val activityLifecycleCallback =
object : ActivityLifecycleCallback {
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
if (
activity.localClassName == currentActivityName &&
stage == Stage.RESUMED &&
activity.resources.configuration.orientation == requestedOrientation
) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Test activity was resumed in the requested orientation.")
}
latch.countDown()
}
}
)
}

if (configChangesHandled) {
Log.d(TAG, "The current activity handles configuration changes.")
context.registerComponentCallbacks(componentCallback)
} else {
Log.d(
TAG,
"The current activity does not handle configuration changes and will be recreated when " +
"its orientation changes."
"its orientation changes.",
)
ActivityLifecycleMonitorRegistry.getInstance()
.addLifecycleCallback(
object : ActivityLifecycleCallback {
override fun onActivityLifecycleChanged(activity: Activity, stage: Stage) {
if (
activity.localClassName == currentActivityName &&
stage == Stage.RESUMED &&
activity.resources.configuration.orientation == requestedOrientation
) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Test activity was resumed in the requested orientation.")
}
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
latch.countDown()
}
}
}
)
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(activityLifecycleCallback)
}

deviceController.setScreenOrientation(screenOrientation.getOrientation())
latch.await(5, TimeUnit.SECONDS)

if (configChangesHandled) {
context.unregisterComponentCallbacks(componentCallback)
} else {
ActivityLifecycleMonitorRegistry.getInstance()
.removeLifecycleCallback(activityLifecycleCallback)
}

// Restore accelerometer rotation setting if it was changed
if (
getDeviceApiLevel() >= 21 && startingAccelRotationSetting != getAccelerometerRotationSetting()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,19 @@ fun getResumedActivityOrNull(): Activity? {
} else if (activities.isEmpty()) {
Log.d(TAG, "No activity found in the RESUMED stage. Waiting up to 2 seconds for one.")
val latch = CountDownLatch(1)
ActivityLifecycleMonitorRegistry.getInstance()
.addLifecycleCallback(
object : ActivityLifecycleCallback {
override fun onActivityLifecycleChanged(newActivity: Activity, stage: Stage) {
if (stage == Stage.RESUMED) {
Log.d(TAG, "Found ${newActivity.getLocalClassName()} in the RESUMED stage.")
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this)
latch.countDown()
activity = newActivity
}
val callback =
object : ActivityLifecycleCallback {
override fun onActivityLifecycleChanged(newActivity: Activity, stage: Stage) {
if (stage == Stage.RESUMED) {
Log.d(TAG, "Found ${newActivity.localClassName} in the RESUMED stage.")
latch.countDown()
activity = newActivity
}
}
)
}
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(callback)
latch.await(2, TimeUnit.SECONDS)
ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(callback)
} else {
activity = activities.elementAtOrNull(0)
}
Expand Down

0 comments on commit 2935194

Please sign in to comment.