Skip to content

Commit

Permalink
Change snackbar duration timeout for a11y
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 255456517
  • Loading branch information
ymarian authored and raajkumars committed Jul 3, 2019
1 parent 0e4142e commit 469c126
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/java/com/google/android/material/snackbar/Snackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import com.google.android.material.R;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_CONTROLS;
import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_ICONS;
import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_TEXT;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
Expand Down Expand Up @@ -326,10 +331,22 @@ public Snackbar setAction(CharSequence text, final View.OnClickListener listener

@Override
public int getDuration() {
int userSetDuration = super.getDuration();
if (userSetDuration == BaseTransientBottomBar.LENGTH_INDEFINITE) {
return BaseTransientBottomBar.LENGTH_INDEFINITE;
}

if (VERSION.SDK_INT >= VERSION_CODES.Q) {
int controlsFlag = hasAction ? FLAG_CONTENT_CONTROLS : 0;
return accessibilityManager.getRecommendedTimeoutMillis(
userSetDuration,
controlsFlag | FLAG_CONTENT_ICONS | FLAG_CONTENT_TEXT);
}

// If touch exploration is enabled override duration to give people chance to interact.
return hasAction && accessibilityManager.isTouchExplorationEnabled()
? BaseTransientBottomBar.LENGTH_INDEFINITE
: super.getDuration();
: userSetDuration;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2019 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.snackbar;

import com.google.android.material.R;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.internal.DoNotInstrument;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowAccessibilityManager;


/**
* Tests for {@link com.google.android.material.snackbar.Snackbar}.
*/
@RunWith(RobolectricTestRunner.class)
@DoNotInstrument
public class SnackbarTest {

private Snackbar snackbar;
private Context activity;
private ShadowAccessibilityManager accessibilityManager;

@Before
public void createActivityAndShadow() {
ApplicationProvider.getApplicationContext()
.setTheme(R.style.Theme_MaterialComponents_Light_NoActionBar);
activity = Robolectric.buildActivity(AppCompatActivity.class).create().get();
accessibilityManager = Shadow.
extract(activity.getSystemService(Context.ACCESSIBILITY_SERVICE));
}

@Test
public void testGetDuration_whenTouchExplorationEnabled_isIndefinite() {
accessibilityManager.setTouchExplorationEnabled(true);

CoordinatorLayout view = new CoordinatorLayout(activity);
snackbar = Snackbar.make(view, "Test text", Snackbar.LENGTH_LONG).setAction("STUFF!", v -> {});

assertThat(snackbar.getDuration()).isEqualTo(Snackbar.LENGTH_INDEFINITE);
}

@Test
public void testGetDuration_whenTouchExplorationDisabled_isProvidedValue() {
accessibilityManager.setTouchExplorationEnabled(false);

CoordinatorLayout view = new CoordinatorLayout(activity);
snackbar = Snackbar.make(view, "Test text", 300).setAction("STUFF!", v -> {});

assertThat(snackbar.getDuration()).isEqualTo(300);
}
}

0 comments on commit 469c126

Please sign in to comment.