@@ -62,7 +62,7 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
62
62
* @param deviceController the controller to use to interact with the device.
63
63
*/
64
64
override fun perform (deviceController : DeviceController ) {
65
- val context = InstrumentationRegistry .getInstrumentation().getTargetContext()
65
+ val context = InstrumentationRegistry .getInstrumentation().targetContext
66
66
if (screenOrientation == getCurrentScreenOrientation(context)) {
67
67
if (Log .isLoggable(TAG , Log .DEBUG )) {
68
68
Log .d(TAG , " Device screen is already in the requested orientation, no need to rotate." )
@@ -75,27 +75,24 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
75
75
return
76
76
}
77
77
78
- var startingAccelRotationSetting = getAccelerometerRotationSetting()
79
78
val currentActivity = getResumedActivityOrNull()
80
- val currentActivityName: String? = currentActivity?.getLocalClassName()
81
- val configChangesHandled =
82
- if (currentActivity != null ) {
83
- currentActivity.isConfigurationChangeHandled(CONFIG_ORIENTATION )
84
- } else {
85
- false
86
- }
79
+ if (currentActivity == null ) {
80
+ Log .d(TAG , " No activity was found in the RESUMED stage." )
81
+ throw DeviceControllerOperationException (
82
+ " Device could not be set to the requested screen orientation because no activity was found."
83
+ )
84
+ }
87
85
86
+ var startingAccelRotationSetting = getAccelerometerRotationSetting()
87
+ val currentActivityName: String? = currentActivity.localClassName
88
+ val configChangesHandled = currentActivity.isConfigurationChangeHandled(CONFIG_ORIENTATION )
88
89
val latch: CountDownLatch = CountDownLatch (1 )
89
90
val requestedOrientation =
90
91
if (screenOrientation == ScreenOrientation .LANDSCAPE ) Configuration .ORIENTATION_LANDSCAPE
91
92
else Configuration .ORIENTATION_PORTRAIT
92
93
93
- if (currentActivity == null || configChangesHandled) {
94
- if (currentActivity == null ) {
95
- Log .d(TAG , " No activity was found in the RESUMED stage." )
96
- } else if (configChangesHandled) {
97
- Log .d(TAG , " The current activity handles configuration changes." )
98
- }
94
+ if (configChangesHandled) {
95
+ Log .d(TAG , " The current activity handles configuration changes." )
99
96
context.registerComponentCallbacks(
100
97
object : ComponentCallbacks {
101
98
override fun onConfigurationChanged (newConfig : Configuration ) {
@@ -122,9 +119,9 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
122
119
object : ActivityLifecycleCallback {
123
120
override fun onActivityLifecycleChanged (activity : Activity , stage : Stage ) {
124
121
if (
125
- activity.getLocalClassName() == currentActivityName &&
122
+ activity.localClassName == currentActivityName &&
126
123
stage == Stage .RESUMED &&
127
- activity.getResources().getConfiguration() .orientation == requestedOrientation
124
+ activity.resources.configuration .orientation == requestedOrientation
128
125
) {
129
126
if (Log .isLoggable(TAG , Log .DEBUG )) {
130
127
Log .d(TAG , " Test activity was resumed in the requested orientation." )
@@ -154,7 +151,7 @@ internal class ScreenOrientationAction(val screenOrientation: ScreenOrientation)
154
151
}
155
152
156
153
private fun getCurrentScreenOrientation (context : Context ): ScreenOrientation {
157
- var currentOrientation = context.getResources().getConfiguration() .orientation
154
+ var currentOrientation = context.resources.configuration .orientation
158
155
return if (currentOrientation == Configuration .ORIENTATION_LANDSCAPE )
159
156
ScreenOrientation .LANDSCAPE
160
157
else ScreenOrientation .PORTRAIT
0 commit comments