Skip to content

Commit f777c67

Browse files
ymariancketcham
authored andcommitted
Override snackbar duration for touch exploration
Show the Snackbar for indefinite time if touch exploration is enabled and it has an action. Also make focusable instead of clickable so it doesn't read double tap to interact PiperOrigin-RevId: 204821491
1 parent a4292a1 commit f777c67

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public View getView() {
355355

356356
/** Show the {@link BaseTransientBottomBar}. */
357357
public void show() {
358-
SnackbarManager.getInstance().show(duration, managerCallback);
358+
SnackbarManager.getInstance().show(getDuration(), managerCallback);
359359
}
360360

361361
/** Dismiss the {@link BaseTransientBottomBar}. */
@@ -694,7 +694,7 @@ protected SnackbarBaseLayout(Context context, AttributeSet attrs) {
694694
}
695695
a.recycle();
696696

697-
setClickable(true);
697+
setFocusable(true);
698698
}
699699

700700
@Override

lib/java/com/google/android/material/snackbar/Snackbar.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.view.View;
3838
import android.view.ViewGroup;
3939
import android.view.ViewParent;
40+
import android.view.accessibility.AccessibilityManager;
4041
import android.widget.FrameLayout;
4142
import android.widget.TextView;
4243

@@ -60,6 +61,9 @@
6061
*/
6162
public final class Snackbar extends BaseTransientBottomBar<Snackbar> {
6263

64+
private final AccessibilityManager accessibilityManager;
65+
private boolean hasAction;
66+
6367
/** @hide */
6468
@RestrictTo(LIBRARY_GROUP)
6569
@IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
@@ -127,6 +131,8 @@ private Snackbar(
127131
View content,
128132
com.google.android.material.snackbar.ContentViewCallback contentViewCallback) {
129133
super(parent, content, contentViewCallback);
134+
accessibilityManager =
135+
(AccessibilityManager) parent.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
130136
}
131137

132138
// TODO: Delete this once custom Robolectric shadows no longer depend on this method being present
@@ -284,11 +290,12 @@ public Snackbar setAction(@StringRes int resId, View.OnClickListener listener) {
284290
public Snackbar setAction(CharSequence text, final View.OnClickListener listener) {
285291
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) this.view.getChildAt(0);
286292
final TextView tv = contentLayout.getActionView();
287-
288293
if (TextUtils.isEmpty(text) || listener == null) {
289294
tv.setVisibility(View.GONE);
290295
tv.setOnClickListener(null);
296+
hasAction = false;
291297
} else {
298+
hasAction = true;
292299
tv.setVisibility(View.VISIBLE);
293300
tv.setText(text);
294301
tv.setOnClickListener(
@@ -304,6 +311,14 @@ public void onClick(View view) {
304311
return this;
305312
}
306313

314+
@Override
315+
public int getDuration() {
316+
// If touch exploration is enabled override duration to give people chance to interact.
317+
return hasAction && accessibilityManager.isTouchExplorationEnabled()
318+
? BaseTransientBottomBar.LENGTH_INDEFINITE
319+
: super.getDuration();
320+
}
321+
307322
/**
308323
* Sets the text color of the action specified in {@link #setAction(CharSequence,
309324
* View.OnClickListener)}.

0 commit comments

Comments
 (0)