OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.snackbar; |
| 6 |
| 7 import android.view.LayoutInflater; |
| 8 import android.view.View; |
| 9 import android.view.View.OnClickListener; |
| 10 import android.widget.PopupWindow; |
| 11 import android.widget.TextView; |
| 12 |
| 13 import com.google.android.apps.chrome.R; |
| 14 |
| 15 import org.chromium.ui.base.DeviceFormFactor; |
| 16 |
| 17 /** |
| 18 * Visual representation of a snackbar. On phone it has weight the same as entir
e activity, on |
| 19 * tablet it has a fixed width anchored at start-bottom corner of current window
. |
| 20 */ |
| 21 public class SnackbarPopupWindow extends PopupWindow { |
| 22 private final TemplatePreservingTextView mMessageView; |
| 23 private final TextView mActionButtonView; |
| 24 private final int mAnimationDuration; |
| 25 |
| 26 /** |
| 27 * Creates an instance of the {@link SnackbarPopupWindow}. |
| 28 * @param parent Parent View the popup window anchors to |
| 29 * @param listener An {@link OnClickListener} that will be called when the u
ndo button is |
| 30 * clicked. |
| 31 * @param template Format String to display on popup (e.g. "Closed %s"). |
| 32 * @param description Text showing at start of snackbar. |
| 33 * @param actionText Text of action button at the end of snackbar. |
| 34 */ |
| 35 public SnackbarPopupWindow(View parent, OnClickListener listener, String tem
plate, |
| 36 String description, String actionText) { |
| 37 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sn
ack_bar, null); |
| 38 setContentView(view); |
| 39 mMessageView = (TemplatePreservingTextView) view.findViewById(R.id.undob
ar_message); |
| 40 mActionButtonView = (TextView) view.findViewById(R.id.undobar_button); |
| 41 setTextViews(template, description, actionText, false); |
| 42 mAnimationDuration = view.getResources().getInteger( |
| 43 android.R.integer.config_mediumAnimTime); |
| 44 mActionButtonView.setOnClickListener(listener); |
| 45 |
| 46 // Set width and height of popup window |
| 47 boolean isTablet = DeviceFormFactor.isTablet(parent.getContext()); |
| 48 setWidth(isTablet |
| 49 ? parent.getResources().getDimensionPixelSize(R.dimen.undo_bar_t
ablet_width) |
| 50 : parent.getWidth()); |
| 51 |
| 52 setHeight(parent.getResources().getDimensionPixelSize(R.dimen.undo_bar_h
eight)); |
| 53 setAnimationStyle(R.style.SnackbarAnimation); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void dismiss() { |
| 58 // Disable action button during animation. |
| 59 mActionButtonView.setEnabled(false); |
| 60 super.dismiss(); |
| 61 } |
| 62 |
| 63 /** |
| 64 * Sets the text of TextViews in snackbar. If this popup bar is already visi
ble it |
| 65 * will animate the new text alpha, otherwise it will only set the text. |
| 66 * @param template Template that description textview takes to format string
. |
| 67 * @param description The content text of that show at start of snackbar. |
| 68 * @param actionText Text for action button to show. |
| 69 * @param animate Whether or not to animate the text in or set it. |
| 70 */ |
| 71 public void setTextViews(String template, String description, String actionT
ext, |
| 72 boolean animate) { |
| 73 mMessageView.setTemplate(template); |
| 74 setViewText(mMessageView, description, animate); |
| 75 setViewText(mActionButtonView, actionText, animate); |
| 76 } |
| 77 |
| 78 private void setViewText(TextView view, String text, boolean animate) { |
| 79 if (view.getText().toString().equals(text)) return; |
| 80 view.animate().cancel(); |
| 81 if (animate) { |
| 82 view.setAlpha(0.0f); |
| 83 view.setText(text); |
| 84 view.animate().alpha(1.f).setDuration(mAnimationDuration).setListene
r(null); |
| 85 } else { |
| 86 view.setText(text); |
| 87 } |
| 88 } |
| 89 |
| 90 /** |
| 91 * Sends an accessibility event to mMessageView announcing that this window
was added so that |
| 92 * the mMessageView content description is read aloud if accessibility is en
abled. |
| 93 */ |
| 94 public void announceforAccessibility() { |
| 95 mMessageView.announceForAccessibility(mMessageView.getContentDescription
()); |
| 96 } |
| 97 } |
OLD | NEW |