-
Notifications
You must be signed in to change notification settings - Fork 211
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
723165c
Added device compatibility mode snippets.
JonEckenrode 9fc778c
Added Kotlin snippets.
JonEckenrode 22ab49a
Apply Spotless
JonEckenrode a8b7943
Added copyright statement.
JonEckenrode 304417d
Merge branch 'device_compatibility_mode' of github.com:android/snippe…
JonEckenrode c0cd7b9
Merge branch 'main' into device_compatibility_mode
JonEckenrode d11bb16
Update libs.versions.toml
JonEckenrode f97b75a
Update build.gradle.kts
JonEckenrode eef7ec5
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode d9b1399
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode fbbb357
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode 57c14a2
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode fa1ba5f
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode 39775c8
Apply Spotless
JonEckenrode 3cf4d29
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode 52e8ee5
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode b32d7c4
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode 7b6f7c6
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode d803902
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode 8aa776a
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode 8dbcbcf
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode 2450fba
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode ea46f4c
Update DeviceCompatibilityModeTestJavaSnippets.java
JonEckenrode 3fd5299
Update DeviceCompatibilityModeTestKotlinSnippets.kt
JonEckenrode 578e40f
Update DeviceCompatibilityModeKotlinSnippets.kt
JonEckenrode c5d9a23
Update DeviceCompatibilityModeJavaSnippets.java
JonEckenrode d30ae93
Merge branch 'main' into device_compatibility_mode
JonEckenrode 2d7e903
Merge branch 'main' into device_compatibility_mode
JonEckenrode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestJavaSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 androidx.appcompat.app.AppCompatActivity | ||
import androidx.test.core.app.ActivityScenario; | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class DeviceCompatibilityModeTestJavaSnippets { | ||
|
||
// [START android_device_compatibility_mode_assert_isLetterboxed_java] | ||
@Rule | ||
public ActivityScenarioRule<MainActivity> rule = new ActivityScenarioRule<>(MainActivity.class); | ||
|
||
@Test | ||
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(AppCompatActivity activity) { | ||
return true; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
misc/src/androidTest/java/com/example/snippets/DeviceCompatibilityModeTestKotlinSnippets.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 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 { | ||
|
||
// [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 | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
misc/src/main/java/com/example/snippets/DeviceCompatibilityModeJavaSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.graphics.Rect; | ||
import android.os.Build.VERSION_CODES; | ||
import androidx.annotation.RequiresApi; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.window.layout.WindowMetricsCalculator; | ||
|
||
public class DeviceCompatibilityModeJavaSnippets { | ||
|
||
@RequiresApi(api=VERSION_CODES.N) | ||
// [START android_device_compatibility_mode_isLetterboxed_java] | ||
public boolean isLetterboxed(AppCompatActivity 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] | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
misc/src/main/java/com/example/snippets/DeviceCompatibilityModeKotlinSnippets.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - this doesn't need to extend |
||
|
||
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] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.