-
Notifications
You must be signed in to change notification settings - Fork 210
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
Changes from 6 commits
723165c
9fc778c
22ab49a
a8b7943
304417d
c0cd7b9
d11bb16
f97b75a
eef7ec5
d9b1399
fbbb357
57c14a2
fa1ba5f
39775c8
3cf4d29
52e8ee5
b32d7c4
7b6f7c6
d803902
8aa776a
8dbcbcf
2450fba
ea46f4c
3fd5299
578e40f
c5d9a23
d30ae93
2d7e903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ android { | |
|
||
} | ||
dependencies { | ||
val composeBom = platform(libs.androidx.compose.bom) | ||
val composeBom = platform(libs.androidx.compose.bom) | ||
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 formatting change is unnecessary 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. Restored. One of the formatters did that.--I think Android Studio. |
||
implementation(composeBom) | ||
androidTestImplementation(composeBom) | ||
implementation(libs.androidx.core.ktx) | ||
|
@@ -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) | ||
|
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 { | ||
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 - I don't think any of these test classes need to subclass 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. 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); | ||
|
||
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. I think this should ba annotated with 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. 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() { | ||
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] | ||
} |
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.
Nit - this should be
androidx-test-junit
since this is a version for androidx.test extensions ofjunit
, notjunit
itself which there's also a version for defined above.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.
Changed.