Skip to content

Commit 319f5f1

Browse files
authored
fix(auth): Remove WindowManager usage from DeviceDataCollector (#3061)
1 parent a63c2b9 commit 319f5f1

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/asf/DeviceDataCollector.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package com.amplifyframework.auth.cognito.asf
1717

1818
import android.content.Context
1919
import android.provider.Settings
20-
import android.view.Display
21-
import android.view.WindowManager
2220
import java.util.Locale
2321
import java.util.TimeZone
2422
import java.util.concurrent.TimeUnit
@@ -77,24 +75,19 @@ internal class DeviceDataCollector(private val deviceId: String) : DataCollector
7775
return (if (hours < 0) "-" else "") + String.format(Locale.US, "%02d:%02d", abs(hours), minutes)
7876
}
7977

80-
private fun getDisplay(context: Context): Display {
81-
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
82-
return windowManager.defaultDisplay
83-
}
84-
8578
/**
8679
* {@inheritDoc}
8780
*/
8881
override fun collect(context: Context): Map<String, String?> {
89-
val display = getDisplay(context)
82+
val displayMetrics = context.resources.displayMetrics
9083
return mapOf(
9184
TIMEZONE to timezoneOffset,
9285
PLATFORM_KEY to PLATFORM_VALUE,
9386
THIRD_PARTY_DEVICE_AGENT to thirdPartyDeviceAgent,
9487
DEVICE_AGENT to deviceId,
9588
DEVICE_LANGUAGE to language,
96-
DEVICE_HEIGHT to display.height.toString(),
97-
DEVICE_WIDTH to display.width.toString()
89+
DEVICE_HEIGHT to displayMetrics.heightPixels.toString(),
90+
DEVICE_WIDTH to displayMetrics.widthPixels.toString()
9891
)
9992
}
10093
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.auth.cognito.asf
17+
18+
import android.content.Context
19+
import androidx.test.core.app.ApplicationProvider
20+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.DEVICE_AGENT
21+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.DEVICE_HEIGHT
22+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.DEVICE_LANGUAGE
23+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.DEVICE_WIDTH
24+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.PLATFORM_KEY
25+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.THIRD_PARTY_DEVICE_AGENT
26+
import com.amplifyframework.auth.cognito.asf.DeviceDataCollector.Companion.TIMEZONE
27+
import io.kotest.matchers.maps.shouldContainExactly
28+
import io.mockk.every
29+
import io.mockk.mockkStatic
30+
import io.mockk.unmockkStatic
31+
import java.util.SimpleTimeZone
32+
import java.util.TimeZone
33+
import org.junit.After
34+
import org.junit.Before
35+
import org.junit.Test
36+
import org.junit.runner.RunWith
37+
import org.robolectric.RobolectricTestRunner
38+
import org.robolectric.annotation.Config
39+
40+
@RunWith(RobolectricTestRunner::class)
41+
@Config(qualifiers = "en-rUS-w75dp-h150dp")
42+
class DeviceDataCollectorTest {
43+
44+
@Before
45+
fun setup() {
46+
mockkStatic(TimeZone::class)
47+
every { TimeZone.getDefault() } returns SimpleTimeZone(0, "UTC")
48+
}
49+
50+
@After
51+
fun teardown() {
52+
unmockkStatic(TimeZone::class)
53+
}
54+
55+
@Test
56+
fun `returns expected values`() {
57+
val context = ApplicationProvider.getApplicationContext<Context>()
58+
59+
val collector = DeviceDataCollector("deviceId")
60+
val data = collector.collect(context)
61+
62+
data shouldContainExactly mapOf(
63+
TIMEZONE to "00:00",
64+
PLATFORM_KEY to "ANDROID",
65+
THIRD_PARTY_DEVICE_AGENT to "android_id",
66+
DEVICE_AGENT to "deviceId",
67+
DEVICE_LANGUAGE to "en_US",
68+
DEVICE_HEIGHT to "150",
69+
DEVICE_WIDTH to "75"
70+
)
71+
}
72+
}

0 commit comments

Comments
 (0)