Skip to content

Commit d68fdfb

Browse files
authored
Added device compatibility mode snippets. (#492)
* Added device compatibility mode snippets. * Added Kotlin snippets. * Apply Spotless * Added copyright statement. * Update libs.versions.toml * Update build.gradle.kts * Update DeviceCompatibilityModeJavaSnippets.java * Update DeviceCompatibilityModeTestKotlinSnippets.kt * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeKotlinSnippets.kt * Update DeviceCompatibilityModeKotlinSnippets.kt * Apply Spotless * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeTestKotlinSnippets.kt * Update DeviceCompatibilityModeJavaSnippets.java * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeKotlinSnippets.kt * Update DeviceCompatibilityModeJavaSnippets.java * Update DeviceCompatibilityModeTestJavaSnippets.java * Update DeviceCompatibilityModeTestKotlinSnippets.kt * Update DeviceCompatibilityModeKotlinSnippets.kt * Update DeviceCompatibilityModeJavaSnippets.java
1 parent 48a0127 commit d68fdfb

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

Diff for: gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ androidx-paging = "3.3.6"
2121
androidx-startup-runtime = "1.2.0"
2222
androidx-test = "1.6.1"
2323
androidx-test-espresso = "3.6.1"
24+
androidx-test-junit = "1.2.1"
2425
androidx-window = "1.4.0-rc01"
2526
androidx-window-core = "1.4.0-beta02"
2627
androidx-window-java = "1.3.0"
@@ -130,6 +131,7 @@ androidx-startup-runtime = {module = "androidx.startup:startup-runtime", version
130131
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
131132
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
132133
androidx-test-runner = "androidx.test:runner:1.6.2"
134+
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-junit" }
133135
androidx-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "tiles" }
134136
androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", version.ref = "tiles" }
135137
androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" }

Diff for: misc/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ dependencies {
7070
implementation(libs.androidx.window.java)
7171
implementation(libs.appcompat)
7272
testImplementation(libs.junit)
73+
testImplementation(kotlin("test"))
74+
androidTestImplementation(libs.androidx.test.ext.junit)
7375
androidTestImplementation(libs.junit)
7476
androidTestImplementation(libs.androidx.test.core)
7577
androidTestImplementation(libs.androidx.test.runner)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
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+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets;
18+
19+
import androidx.appcompat.app.AppCompatActivity
20+
import androidx.test.core.app.ActivityScenario;
21+
import androidx.test.ext.junit.rules.ActivityScenarioRule;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import static org.junit.Assert.assertFalse;
25+
26+
public class DeviceCompatibilityModeTestJavaSnippets {
27+
28+
// [START android_device_compatibility_mode_assert_isLetterboxed_java]
29+
@Rule
30+
public ActivityScenarioRule<MainActivity> rule = new ActivityScenarioRule<>(MainActivity.class);
31+
32+
@Test
33+
public void activity_launched_notLetterBoxed() {
34+
try (ActivityScenario<MainActivity> scenario =
35+
ActivityScenario.launch(MainActivity.class)) {
36+
scenario.onActivity( activity -> {
37+
assertFalse(isLetterboxed(activity));
38+
});
39+
}
40+
}
41+
// [END android_device_compatibility_mode_assert_isLetterboxed_java]
42+
43+
44+
// Method used by snippets.
45+
public boolean isLetterboxed(AppCompatActivity activity) {
46+
return true;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
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+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets
18+
19+
import androidx.appcompat.app.AppCompatActivity
20+
import androidx.test.ext.junit.rules.ActivityScenarioRule
21+
import org.junit.Assert.assertFalse
22+
import org.junit.Rule
23+
import org.junit.Test
24+
25+
class DeviceCompatibilityModeTestKotlinSnippets {
26+
27+
// [START android_device_compatibility_mode_assert_isLetterboxed_kotlin]
28+
@get:Rule
29+
val activityRule = ActivityScenarioRule(MainActivity::class.java)
30+
31+
@Test
32+
fun activity_launched_notLetterBoxed() {
33+
activityRule.scenario.onActivity {
34+
assertFalse(it.isLetterboxed())
35+
}
36+
}
37+
// [END android_device_compatibility_mode_assert_isLetterboxed_kotlin]
38+
39+
// Classes used by snippets.
40+
41+
class MainActivity : AppCompatActivity() {
42+
43+
fun isLetterboxed(): Boolean {
44+
return true
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
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+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets;
18+
19+
import android.graphics.Rect;
20+
import android.os.Build.VERSION_CODES;
21+
import androidx.annotation.RequiresApi;
22+
import androidx.appcompat.app.AppCompatActivity;
23+
import androidx.window.layout.WindowMetricsCalculator;
24+
25+
public class DeviceCompatibilityModeJavaSnippets {
26+
27+
@RequiresApi(api=VERSION_CODES.N)
28+
// [START android_device_compatibility_mode_isLetterboxed_java]
29+
public boolean isLetterboxed(AppCompatActivity activity) {
30+
if (activity.isInMultiWindowMode()) {
31+
return false;
32+
}
33+
34+
WindowMetricsCalculator wmc = WindowMetricsCalculator.getOrCreate();
35+
Rect currentBounds = wmc.computeCurrentWindowMetrics(activity).getBounds();
36+
Rect maxBounds = wmc.computeMaximumWindowMetrics(activity).getBounds();
37+
38+
boolean isScreenPortrait = maxBounds.height() > maxBounds.width();
39+
40+
return (isScreenPortrait)
41+
? currentBounds.height() < maxBounds.height()
42+
: currentBounds.width() < maxBounds.width();
43+
}
44+
// [END android_device_compatibility_mode_isLetterboxed_java]
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
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+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets
18+
19+
import android.os.Build
20+
import android.os.Bundle
21+
import androidx.annotation.RequiresApi
22+
import androidx.appcompat.app.AppCompatActivity
23+
import androidx.window.layout.WindowMetricsCalculator
24+
25+
class DeviceCompatibilityModeKotlinSnippets : AppCompatActivity() {
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
}
30+
31+
@RequiresApi(Build.VERSION_CODES.N)
32+
// [START android_device_compatibility_mode_isLetterboxed_kotlin]
33+
fun isLetterboxed(activity: AppCompatActivity): Boolean {
34+
if (isInMultiWindowMode) return false
35+
36+
val wmc = WindowMetricsCalculator.getOrCreate()
37+
val currentBounds = wmc.computeCurrentWindowMetrics(this).bounds
38+
val maxBounds = wmc.computeMaximumWindowMetrics(this).bounds
39+
40+
val isScreenPortrait = maxBounds.height() > maxBounds.width()
41+
42+
return if (isScreenPortrait) {
43+
currentBounds.height() < maxBounds.height()
44+
} else {
45+
currentBounds.width() < maxBounds.width()
46+
}
47+
}
48+
// [END android_device_compatibility_mode_isLetterboxed_kotlin]
49+
}

0 commit comments

Comments
 (0)