Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Adaptive] [Side Sheets] Add SideSheetCallback listener to SideSheetB…
…ehavior to track @SheetState state change events. Includes a fix for a really strange issue where setting the background color from the callback worked, but if setText was called, it would cause the sheet to flash off the screen when STATE_EXPANDED was reached. PiperOrigin-RevId: 493409073 (cherry picked from commit 2468d6c)
- Loading branch information
Showing
6 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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
47 changes: 47 additions & 0 deletions
47
lib/java/com/google/android/material/sidesheet/SideSheetCallback.java
This file contains 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 (C) 2022 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 | ||
* | ||
* http://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.google.android.material.sidesheet; | ||
|
||
import android.view.View; | ||
import androidx.annotation.NonNull; | ||
import com.google.android.material.sidesheet.Sheet.SheetState; | ||
|
||
/** Callback that monitors side sheet events. */ | ||
public abstract class SideSheetCallback { | ||
|
||
/** | ||
* Called when the sheet changes its state. | ||
* | ||
* @param sheet The sheet view. | ||
* @param newState The new state. This should be one of {@link SideSheetBehavior#STATE_DRAGGING}, | ||
* {@link SideSheetBehavior#STATE_SETTLING}, {@link SideSheetBehavior#STATE_EXPANDED} or | ||
* {@link SideSheetBehavior#STATE_HIDDEN}. | ||
*/ | ||
public abstract void onStateChanged(@NonNull View sheet, @SheetState int newState); | ||
|
||
/** | ||
* Called when the sheet is being dragged. | ||
* | ||
* @param sheet The sheet view. | ||
* @param slideOffset The new offset of this sheet within [0,1] range. Offset increases as this | ||
* sheet is moving towards the outward edge. A value of 0 means that the sheet is hidden, and | ||
* a value of 1 means that the sheet is fully expanded. | ||
*/ | ||
public abstract void onSlide(@NonNull View sheet, float slideOffset); | ||
|
||
void onLayout(@NonNull View sheet) {} | ||
} |
121 changes: 121 additions & 0 deletions
121
lib/javatests/com/google/android/material/sidesheet/SideSheetCallbackTest.java
This file contains 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,121 @@ | ||
/* | ||
* Copyright (C) 2022 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 | ||
* | ||
* http://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.google.android.material.sidesheet; | ||
|
||
import com.google.android.material.test.R; | ||
|
||
import static android.graphics.Color.RED; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.robolectric.Shadows.shadowOf; | ||
|
||
import android.graphics.drawable.ColorDrawable; | ||
import android.os.Bundle; | ||
import android.os.Looper; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import android.view.View; | ||
import androidx.annotation.NonNull; | ||
import androidx.coordinatorlayout.widget.CoordinatorLayout; | ||
import androidx.core.view.ViewCompat; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
/** Tests for {@link com.google.android.material.sidesheet.SideSheetBehavior}. */ | ||
@RunWith(RobolectricTestRunner.class) | ||
public class SideSheetCallbackTest { | ||
|
||
private View sideSheet; | ||
private SideSheetBehavior<View> sideSheetBehavior; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
AppCompatActivity activity = Robolectric.buildActivity(TestActivity.class).setup().get(); | ||
CoordinatorLayout coordinatorLayout = | ||
(CoordinatorLayout) activity.getLayoutInflater().inflate(R.layout.test_side_sheet, null); | ||
sideSheet = coordinatorLayout.findViewById(R.id.test_side_sheet_container); | ||
sideSheetBehavior = SideSheetBehavior.from(sideSheet); | ||
|
||
activity.setContentView(coordinatorLayout); | ||
|
||
// Wait until the layout is measured. | ||
shadowOf(Looper.getMainLooper()).idle(); | ||
} | ||
|
||
@Test | ||
public void test_setSheetRedOnExpandWithCallback_sheetIsRedOnExpand() { | ||
// Create a callback and add it to the side sheet behavior. | ||
sideSheetBehavior.addCallback( | ||
new SideSheetCallback() { | ||
@Override | ||
public void onStateChanged(@NonNull View sheet, int newState) { | ||
if (newState == SideSheetBehavior.STATE_EXPANDED) { | ||
ViewCompat.setBackground(sideSheet, new ColorDrawable(RED)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSlide(@NonNull View sheet, float slideOffset) {} | ||
}); | ||
|
||
sideSheetBehavior.expand(); | ||
shadowOf(Looper.getMainLooper()).idle(); | ||
|
||
assertThat(sideSheetBehavior.getState()).isEqualTo(SideSheetBehavior.STATE_EXPANDED); | ||
assertThat(sideSheet.getBackground()).isInstanceOf(ColorDrawable.class); | ||
assertThat(((ColorDrawable) sideSheet.getBackground()).getColor()).isEqualTo(RED); | ||
} | ||
|
||
@Test | ||
public void test_removeCallback_callbackIsRemoved() { | ||
SideSheetCallback sideSheetCallback = createExpandedRedSideSheetCallback(); | ||
// Ensure that side sheet doesn't already have a background. | ||
ViewCompat.setBackground(sideSheet, null); | ||
|
||
sideSheetBehavior.addCallback(sideSheetCallback); | ||
sideSheetBehavior.removeCallback(sideSheetCallback); | ||
|
||
sideSheetBehavior.expand(); | ||
shadowOf(Looper.getMainLooper()).idle(); | ||
|
||
assertThat(sideSheetBehavior.getState()).isEqualTo(SideSheetBehavior.STATE_EXPANDED); | ||
assertThat(sideSheet.getBackground()).isNull(); | ||
} | ||
|
||
private SideSheetCallback createExpandedRedSideSheetCallback() { | ||
return new SideSheetCallback() { | ||
@Override | ||
public void onStateChanged(@NonNull View sheet, int newState) { | ||
if (newState == SideSheetBehavior.STATE_EXPANDED) { | ||
ViewCompat.setBackground(sideSheet, new ColorDrawable(RED)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSlide(@NonNull View sheet, float slideOffset) {} | ||
}; | ||
} | ||
|
||
private static class TestActivity extends AppCompatActivity { | ||
@Override | ||
protected void onCreate(Bundle bundle) { | ||
super.onCreate(bundle); | ||
setTheme(R.style.Theme_Material3_Light_NoActionBar); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
lib/javatests/com/google/android/material/sidesheet/res/layout/test_side_sheet.xml
This file contains 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,39 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Copyright 2022 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 | ||
http://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. | ||
--> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/test_coordinator_layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:fitsSystemWindows="true"> | ||
|
||
<LinearLayout | ||
android:id="@+id/test_side_sheet_container" | ||
style="@style/Widget.Material3.SideSheet" | ||
android:layout_width="256dp" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
app:layout_behavior="@string/side_sheet_behavior" | ||
tools:targetApi="lollipop"> | ||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="match_parent" | ||
android:text="Test Side Sheet Content" /> | ||
</LinearLayout> | ||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |