Skip to content

Commit a47475d

Browse files
ymariangsajith
authored andcommitted
Support drag actions for A11y in draggable card demo
PiperOrigin-RevId: 245418849
1 parent 7104802 commit a47475d

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

catalog/java/io/material/catalog/card/DraggableCardFragment.java

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,33 @@
1818

1919
import io.material.catalog.R;
2020

21+
import android.animation.LayoutTransition;
22+
import android.os.Build;
23+
import android.os.Build.VERSION;
24+
import android.os.Build.VERSION_CODES;
2125
import android.os.Bundle;
2226
import androidx.annotation.NonNull;
2327
import androidx.annotation.Nullable;
2428
import com.google.android.material.card.MaterialCardView;
29+
import androidx.coordinatorlayout.widget.CoordinatorLayout;
30+
import android.view.Gravity;
2531
import android.view.LayoutInflater;
2632
import android.view.View;
33+
import android.view.View.AccessibilityDelegate;
2734
import android.view.ViewGroup;
35+
import android.view.accessibility.AccessibilityNodeInfo;
36+
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
2837
import io.material.catalog.draggable.DraggableCoordinatorLayout;
38+
import io.material.catalog.draggable.DraggableCoordinatorLayout.ViewDragListener;
2939
import io.material.catalog.feature.DemoFragment;
3040

31-
/** A fragment with a draggable MaterialCardView */
41+
/**
42+
* A fragment with a draggable MaterialCardView
43+
*/
3244
public class DraggableCardFragment extends DemoFragment {
3345

46+
private MaterialCardView card;
47+
3448
@Override
3549
public int getDemoTitleResId() {
3650
return R.string.cat_card_draggable_card;
@@ -43,15 +57,21 @@ public View onCreateDemoView(
4357
layoutInflater.inflate(
4458
R.layout.cat_card_draggable_fragment, viewGroup, false /* attachToRoot */);
4559
DraggableCoordinatorLayout container = (DraggableCoordinatorLayout) view;
46-
final MaterialCardView card = view.findViewById(R.id.draggable_card);
60+
LayoutTransition transition = ((CoordinatorLayout) view).getLayoutTransition();
61+
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
62+
transition.enableTransitionType(LayoutTransition.CHANGING);
63+
}
64+
65+
card = view.findViewById(R.id.draggable_card);
66+
card.setAccessibilityDelegate(cardDelegate);
4767
container.addDraggableChild(card);
4868

49-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
69+
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
5070
return view;
5171
}
5272

5373
container.setViewDragListener(
54-
new DraggableCoordinatorLayout.ViewDragListener() {
74+
new ViewDragListener() {
5575
@Override
5676
public void onViewCaptured(@NonNull View view, int i) {
5777
card.setDragged(true);
@@ -65,4 +85,73 @@ public void onViewReleased(@NonNull View view, float v, float v1) {
6585

6686
return view;
6787
}
88+
89+
private final AccessibilityDelegate cardDelegate = new AccessibilityDelegate() {
90+
@Override
91+
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
92+
super.onInitializeAccessibilityNodeInfo(host, info);
93+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
94+
return;
95+
}
96+
97+
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) card
98+
.getLayoutParams();
99+
int gravity = layoutParams.gravity;
100+
boolean isOnLeft = (gravity & Gravity.LEFT) == Gravity.LEFT;
101+
boolean isOnRight = (gravity & Gravity.RIGHT) == Gravity.RIGHT;
102+
boolean isOnTop = (gravity & Gravity.TOP) == Gravity.TOP;
103+
boolean isOnBottom = (gravity & Gravity.BOTTOM) == Gravity.BOTTOM;
104+
boolean isOnCenter = (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.CENTER_HORIZONTAL;
105+
106+
if (!(isOnTop && isOnLeft)) {
107+
info.addAction(new AccessibilityAction(R.id.move_card_top_left_action,
108+
getString(R.string.cat_card_action_move_top_left)));
109+
}
110+
if (!(isOnTop && isOnRight)) {
111+
info.addAction(new AccessibilityAction(R.id.move_card_top_right_action,
112+
getString(R.string.cat_card_action_move_top_right)));
113+
}
114+
if (!(isOnBottom && isOnLeft)) {
115+
info.addAction(new AccessibilityAction(R.id.move_card_bottom_left_action,
116+
getString(R.string.cat_card_action_move_bottom_left)));
117+
}
118+
if (!(isOnBottom && isOnRight)) {
119+
info.addAction(new AccessibilityAction(
120+
R.id.move_card_bottom_right_action,
121+
getString(R.string.cat_card_action_move_bottom_right)));
122+
}
123+
if (!isOnCenter) {
124+
info.addAction(new AccessibilityAction(
125+
R.id.move_card_center_action,
126+
getString(R.string.cat_card_action_move_center)));
127+
}
128+
}
129+
130+
@Override
131+
public boolean performAccessibilityAction(View host, int action, Bundle arguments) {
132+
int gravity;
133+
if (action == R.id.move_card_top_left_action) {
134+
gravity = Gravity.TOP | Gravity.LEFT;
135+
} else if (action == R.id.move_card_top_right_action) {
136+
gravity = Gravity.TOP | Gravity.RIGHT;
137+
} else if (action == R.id.move_card_bottom_left_action) {
138+
gravity = Gravity.BOTTOM | Gravity.LEFT;
139+
} else if (action == R.id.move_card_bottom_right_action) {
140+
gravity = Gravity.BOTTOM | Gravity.RIGHT;
141+
} else if (action == R.id.move_card_center_action) {
142+
gravity = Gravity.CENTER;
143+
} else {
144+
return super.performAccessibilityAction(host, action, arguments);
145+
}
146+
147+
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) card
148+
.getLayoutParams();
149+
if (layoutParams.gravity != gravity) {
150+
layoutParams.gravity = gravity;
151+
card.requestLayout();
152+
}
153+
154+
return true;
155+
}
156+
};
68157
}

catalog/java/io/material/catalog/card/res/layout/cat_card_draggable_fragment.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
<io.material.catalog.draggable.DraggableCoordinatorLayout
1919
xmlns:android="http://schemas.android.com/apk/res/android"
2020
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
android:id="@+id/root"
2122
android:layout_width="match_parent"
23+
android:animateLayoutChanges="true"
2224
android:layout_height="match_parent">
2325
<com.google.android.material.card.MaterialCardView
2426
android:id="@+id/draggable_card"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2019 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<resources>
18+
19+
<item name="move_card_top_left_action" type="id"/>
20+
21+
<item name="move_card_top_right_action" type="id"/>
22+
23+
<item name="move_card_bottom_left_action" type="id"/>
24+
25+
<item name="move_card_bottom_right_action" type="id"/>
26+
27+
<item name="move_card_center_action" type="id"/>
28+
29+
</resources>
30+

catalog/java/io/material/catalog/card/res/values/strings.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,15 @@
8080
<string name="cat_card_rich_media_chip_title_4">Item 4</string>
8181
<string name="cat_card_rich_media_action_1">Action 1</string>
8282
<string name="cat_card_rich_media_action_2">Action 2</string>
83+
84+
<string description="A description of an action that a user of an accessibility service can perform to move the card to the top left corner of the screen. [CHAR LIMIT=25]"
85+
name="cat_card_action_move_top_left">Move to top left</string>
86+
<string description="A description of an action that a user of an accessibility service can perform to move the card to the top right corner of the screen. [CHAR LIMIT=25]"
87+
name="cat_card_action_move_top_right">Move to top right</string>
88+
<string description="A description of an action that a user of an accessibility service can perform to move the card to the bottom left corner of the screen. [CHAR LIMIT=25]"
89+
name="cat_card_action_move_bottom_left">Move to bottom left</string>
90+
<string description="A description of an action that a user of an accessibility service can perform to move the card to the bottom right corner of the screen. [CHAR LIMIT=25]"
91+
name="cat_card_action_move_bottom_right">Move to bottom right</string>
92+
<string description="A description of an action that a user of an accessibility service can perform to move the card to the center of the screen. [CHAR LIMIT=25]"
93+
name="cat_card_action_move_center">Move to center</string>
8394
</resources>

0 commit comments

Comments
 (0)