Skip to content

Added device compatibility mode snippets. #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
723165c
Added device compatibility mode snippets.
JonEckenrode Apr 8, 2025
9fc778c
Added Kotlin snippets.
JonEckenrode Apr 8, 2025
22ab49a
Apply Spotless
JonEckenrode Apr 8, 2025
a8b7943
Added copyright statement.
JonEckenrode Apr 8, 2025
304417d
Merge branch 'device_compatibility_mode' of github.com:android/snippe…
JonEckenrode Apr 8, 2025
c0cd7b9
Merge branch 'main' into device_compatibility_mode
JonEckenrode Apr 8, 2025
d11bb16
Update libs.versions.toml
JonEckenrode Apr 11, 2025
f97b75a
Update build.gradle.kts
JonEckenrode Apr 11, 2025
eef7ec5
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode Apr 11, 2025
d9b1399
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode Apr 11, 2025
fbbb357
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 11, 2025
57c14a2
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode Apr 11, 2025
fa1ba5f
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode Apr 11, 2025
39775c8
Apply Spotless
JonEckenrode Apr 11, 2025
3cf4d29
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 11, 2025
52e8ee5
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode Apr 11, 2025
b32d7c4
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode Apr 11, 2025
7b6f7c6
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 11, 2025
d803902
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 11, 2025
8aa776a
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 11, 2025
8dbcbcf
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode Apr 12, 2025
2450fba
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode Apr 12, 2025
ea46f4c
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode Apr 12, 2025
3fd5299
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode Apr 12, 2025
578e40f
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode Apr 12, 2025
c5d9a23
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode Apr 12, 2025
d30ae93
Merge branch 'main' into device_compatibility_mode
JonEckenrode Apr 16, 2025
2d7e903
Merge branch 'main' into device_compatibility_mode
JonEckenrode Apr 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ wear = "1.3.0"
wearComposeFoundation = "1.4.1"
wearComposeMaterial = "1.4.1"
wearToolingPreview = "1.0.0"
junitVersion = "1.2.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - this should be androidx-test-junit since this is a version for androidx.test extensions of junit, not junit itself which there's also a version for defined above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.


[libraries]
accompanist-adaptive = { module = "com.google.accompanist:accompanist-adaptive", version.ref = "accompanist" }
Expand Down Expand Up @@ -125,6 +126,7 @@ androidx-startup-runtime = {module = "androidx.startup:startup-runtime", version
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
androidx-test-runner = "androidx.test:runner:1.6.2"
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" }
androidx-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "tiles" }
androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", version.ref = "tiles" }
androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tiles" }
Expand Down
4 changes: 3 additions & 1 deletion misc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ android {

}
dependencies {
val composeBom = platform(libs.androidx.compose.bom)
val composeBom = platform(libs.androidx.compose.bom)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - this formatting change is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored. One of the formatters did that.--I think Android Studio.

implementation(composeBom)
androidTestImplementation(composeBom)
implementation(libs.androidx.core.ktx)
Expand All @@ -70,6 +70,8 @@ dependencies {
implementation(libs.androidx.window.java)
implementation(libs.appcompat)
testImplementation(libs.junit)
testImplementation(kotlin("test"))
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.snippets;

import android.app.Activity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import org.junit.Rule;
import static org.junit.Assert.assertFalse;

public class DeviceCompatibilityModeTestJavaSnippets extends AppCompatActivity {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - I don't think any of these test classes need to subclass Activity, they can just be plain classes that don't extend anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I got too used to doing this for other snippets. Removed.


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

// [START android_device_compatibility_mode_assert_isLetterboxed_java]
@Rule
public ActivityScenarioRule<MainActivity> rule = new ActivityScenarioRule<>(MainActivity.class);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should ba annotated with @Test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Good review, Alex. Thanks.

public void activity_launched_notLetterBoxed() {
try (ActivityScenario<MainActivity> scenario =
ActivityScenario.launch(MainActivity.class)) {
scenario.onActivity( activity -> {
assertFalse(isLetterboxed(activity));
});
}
}
// [END android_device_compatibility_mode_assert_isLetterboxed_java]


// Method used by snippets.
public boolean isLetterboxed(Activity activity) {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.snippets

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.test.ext.junit.rules.ActivityScenarioRule
import org.junit.Assert.assertFalse
import org.junit.Rule
import org.junit.Test

class DeviceCompatibilityModeTestKotlinSnippets : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

// [START android_device_compatibility_mode_assert_isLetterboxed_kotlin]
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun activity_launched_notLetterBoxed() {
activityRule.scenario.onActivity {
assertFalse(it.isLetterboxed())
}
}
// [END android_device_compatibility_mode_assert_isLetterboxed_kotlin]

// Classes used by snippets.

class MainActivity : AppCompatActivity() {

fun isLetterboxed(): Boolean {
return true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.snippets;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.window.layout.WindowMetricsCalculator;

public class DeviceCompatibilityModeJavaSnippets extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@RequiresApi(api=VERSION_CODES.N)
// [START android_device_compatibility_mode_isLetterboxed_java]
public boolean isLetterboxed(Activity activity) {
if (activity.isInMultiWindowMode()) {
return false;
}

WindowMetricsCalculator wmc = WindowMetricsCalculator.getOrCreate();
Rect currentBounds = wmc.computeCurrentWindowMetrics(activity).getBounds();
Rect maxBounds = wmc.computeMaximumWindowMetrics(activity).getBounds();

boolean isScreenPortrait = maxBounds.height() > maxBounds.width();

return (isScreenPortrait)
? currentBounds.height() < maxBounds.height()
: currentBounds.width() < maxBounds.width();
}
// [END android_device_compatibility_mode_isLetterboxed_java]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.snippets

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.window.layout.WindowMetricsCalculator

class DeviceCompatibilityModeKotlinSnippets : AppCompatActivity() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - this doesn't need to extend AppCompatActivity either


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

@RequiresApi(Build.VERSION_CODES.N)
// [START android_device_compatibility_mode_isLetterboxed_kotlin]
fun isLetterboxed(activity: AppCompatActivity): Boolean {
if (isInMultiWindowMode) return false

val wmc = WindowMetricsCalculator.getOrCreate()
val currentBounds = wmc.computeCurrentWindowMetrics(this).bounds
val maxBounds = wmc.computeMaximumWindowMetrics(this).bounds

val isScreenPortrait = maxBounds.height() > maxBounds.width()

return if (isScreenPortrait) {
currentBounds.height() < maxBounds.height()
} else {
currentBounds.width() < maxBounds.width()
}
}
// [END android_device_compatibility_mode_isLetterboxed_kotlin]
}