Skip to content

Commit 902592b

Browse files
authored
Merge pull request #289 from android/window-manager
updated WindowMetricsActivity with the new WindowSizeClass APIs
2 parents 4b5f336 + 5cdd7ba commit 902592b

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

samples/user-interface/windowmanager/src/main/java/com/example/platform/ui/windowmanager/WindowMetricsActivity.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import androidx.appcompat.app.AppCompatActivity
2222
import androidx.recyclerview.widget.RecyclerView
2323
import androidx.window.layout.WindowMetricsCalculator
2424
import androidx.window.core.layout.WindowSizeClass
25+
import androidx.window.core.layout.WindowSizeClass.Companion.HEIGHT_DP_EXPANDED_LOWER_BOUND
26+
import androidx.window.core.layout.WindowSizeClass.Companion.HEIGHT_DP_MEDIUM_LOWER_BOUND
27+
import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_EXPANDED_LOWER_BOUND
28+
import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_MEDIUM_LOWER_BOUND
29+
import androidx.window.core.layout.computeWindowSizeClass
2530

2631
import com.example.platform.ui.windowmanager.infolog.InfoLogAdapter
2732

@@ -48,9 +53,38 @@ class WindowMetricsActivity : AppCompatActivity() {
4853
val width = windowMetrics.bounds.width()
4954
val height = windowMetrics.bounds.height()
5055
val density = resources.displayMetrics.density
51-
val windowSizeClass = WindowSizeClass.compute(width/density, height/density)
56+
val windowSizeClass = WindowSizeClass.BREAKPOINTS_V1.computeWindowSizeClass(width/density, height/density)
57+
5258
adapter.append("WindowMetrics update", "width: $width, height: $height")
53-
adapter.append("WindowSize Class", "" + windowSizeClass.windowHeightSizeClass + "\n" + windowSizeClass.windowWidthSizeClass)
59+
adapter.append("WindowSize Class", "Height: " + getHeightSizeClass(windowSizeClass) + "\nWidth: " + getWidthSizeClass(windowSizeClass))
5460
adapter.notifyDataSetChanged()
5561
}
62+
63+
private fun getWidthSizeClass(sizeClass: WindowSizeClass): String {
64+
return when {
65+
sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_EXPANDED_LOWER_BOUND) -> {
66+
"EXPANDED"
67+
}
68+
sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_MEDIUM_LOWER_BOUND) -> {
69+
"MEDIUM"
70+
}
71+
else -> {
72+
"COMPACT"
73+
}
74+
}
75+
}
76+
77+
private fun getHeightSizeClass(sizeClass: WindowSizeClass): String {
78+
return when {
79+
sizeClass.isHeightAtLeastBreakpoint(HEIGHT_DP_EXPANDED_LOWER_BOUND) -> {
80+
"EXPANDED"
81+
}
82+
sizeClass.isHeightAtLeastBreakpoint(HEIGHT_DP_MEDIUM_LOWER_BOUND) -> {
83+
"MEDIUM"
84+
}
85+
else -> {
86+
"COMPACT"
87+
}
88+
}
89+
}
5690
}

samples/user-interface/windowmanager/src/main/res/layout/activity_window_metrics.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
android:layout_width="match_parent"
2121
android:layout_height="match_parent"
2222
android:fitsSystemWindows="true"
23+
android:layout_margin="8dp"
2324
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

samples/user-interface/windowmanager/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<string name="split_layout_demo_description">Demo of a layout that splits the content to sides of a fold or a hinge. If not present or minimal size requirements are not meant, it behave like a FrameLayout.</string>
4141
<string name="presentation_demo_description">Demo of using Presentation API to show content on secondary display.</string>
4242
<string name="window_metrics">Window metrics</string>
43-
<string name="window_metrics_description">Demo of using WindowMetrics API with Activity handling rotations.</string>
43+
<string name="window_metrics_description">Demo of using WindowMetrics API and WindowSizeClass.</string>
4444
<string name="current_split_attributes">Current SplitAttributes:</string>>
4545
<string name="current_animation_background_color">Current Animation Background Color:</string>>
4646
<string name="toast_split_not_support">Please enable PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED and ensure device supports splits.</string>

0 commit comments

Comments
 (0)