OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.snackbar; | 5 package org.chromium.chrome.browser.snackbar; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.drawable.GradientDrawable; | 8 import android.graphics.drawable.GradientDrawable; |
9 import android.view.LayoutInflater; | 9 import android.view.LayoutInflater; |
10 import android.view.View; | 10 import android.view.View; |
11 import android.view.View.OnClickListener; | 11 import android.view.View.OnClickListener; |
12 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
13 import android.widget.ImageView; | 13 import android.widget.ImageView; |
14 import android.widget.PopupWindow; | 14 import android.widget.PopupWindow; |
15 import android.widget.TextView; | 15 import android.widget.TextView; |
16 | 16 |
17 import org.chromium.base.ApiCompatibilityUtils; | 17 import org.chromium.base.ApiCompatibilityUtils; |
18 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
19 import org.chromium.ui.base.DeviceFormFactor; | 19 import org.chromium.ui.base.DeviceFormFactor; |
20 | 20 |
21 /** | 21 /** |
22 * Visual representation of a snackbar. On phone it fills the width of the activ ity; on tablet it | 22 * Visual representation of a snackbar. On phone it fills the width of the activ ity; on tablet it |
23 * has a fixed width and is anchored at the start-bottom corner of the current w indow. | 23 * has a fixed width and is anchored at the start-bottom corner of the current w indow. |
24 */ | 24 */ |
25 class SnackbarPopupWindow extends PopupWindow { | 25 class SnackbarPopupWindow extends PopupWindow { |
26 private final TemplatePreservingTextView mMessageView; | 26 private final TemplatePreservingTextView mMessageView; |
27 private final TextView mActionButtonView; | 27 private final TextView mActionButtonView; |
28 private final ImageView mProfileImageView; | 28 private final ImageView mProfileImageView; |
29 private final int mAnimationDuration; | 29 private final int mAnimationDuration; |
30 private Snackbar mSnackbar; | |
30 | 31 |
31 /** | 32 /** |
32 * Creates an instance of the {@link SnackbarPopupWindow}. | 33 * Creates an instance of the {@link SnackbarPopupWindow}. |
33 * @param parent Parent View the popup window anchors to | 34 * @param parent Parent View the popup window anchors to |
34 * @param listener An {@link OnClickListener} that will be called when the a ction button is | 35 * @param listener An {@link OnClickListener} that will be called when the a ction button is |
35 * clicked. | 36 * clicked. |
36 * @param snackbar The snackbar to be displayed. | 37 * @param snackbar The snackbar to be displayed. |
37 */ | 38 */ |
38 SnackbarPopupWindow(View parent, OnClickListener listener, Snackbar snackbar ) { | 39 SnackbarPopupWindow(View parent, OnClickListener listener, Snackbar snackbar ) { |
39 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sn ackbar, null); | 40 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sn ackbar, null); |
(...skipping 14 matching lines...) Expand all Loading... | |
54 setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); | 55 setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
55 update(snackbar, false); | 56 update(snackbar, false); |
56 } | 57 } |
57 | 58 |
58 @Override | 59 @Override |
59 public void dismiss() { | 60 public void dismiss() { |
60 // Disable action button during animation. | 61 // Disable action button during animation. |
61 mActionButtonView.setEnabled(false); | 62 mActionButtonView.setEnabled(false); |
62 super.dismiss(); | 63 super.dismiss(); |
63 } | 64 } |
65 /** | |
newt (away)
2016/01/27 02:13:41
newline before this
Ian Wen
2016/01/27 18:14:07
Done.
| |
66 * Updates the view to display data from the given snackbar. No-op if the po pup is already | |
67 * showing the given snackbar. | |
68 * @param snackbar The snackbar to display | |
69 * @return Whether update has actually been executed. | |
70 */ | |
71 boolean update(Snackbar snackbar) { | |
72 return update(snackbar, true); | |
73 } | |
64 | 74 |
65 /** | 75 private boolean update(Snackbar snackbar, boolean animate) { |
66 * Updates the view to display data from the given snackbar. | 76 if (mSnackbar == snackbar) return false; |
67 * | 77 mSnackbar = snackbar; |
68 * @param snackbar The snackbar to display | |
69 * @param animate Whether or not to animate the text in or set it immediatel y | |
70 */ | |
71 void update(Snackbar snackbar, boolean animate) { | |
72 mMessageView.setMaxLines(snackbar.getSingleLine() ? 1 : Integer.MAX_VALU E); | 78 mMessageView.setMaxLines(snackbar.getSingleLine() ? 1 : Integer.MAX_VALU E); |
73 mMessageView.setTemplate(snackbar.getTemplateText()); | 79 mMessageView.setTemplate(snackbar.getTemplateText()); |
74 setViewText(mMessageView, snackbar.getText(), animate); | 80 setViewText(mMessageView, snackbar.getText(), animate); |
75 String actionText = snackbar.getActionText(); | 81 String actionText = snackbar.getActionText(); |
76 | 82 |
77 View view = getContentView(); | 83 View view = getContentView(); |
78 int backgroundColor = snackbar.getBackgroundColor(); | 84 int backgroundColor = snackbar.getBackgroundColor(); |
79 if (backgroundColor == 0) { | 85 if (backgroundColor == 0) { |
80 backgroundColor = ApiCompatibilityUtils.getColor(view.getResources() , | 86 backgroundColor = ApiCompatibilityUtils.getColor(view.getResources() , |
81 R.color.snackbar_background_color); | 87 R.color.snackbar_background_color); |
(...skipping 15 matching lines...) Expand all Loading... | |
97 setViewText(mActionButtonView, snackbar.getActionText(), animate); | 103 setViewText(mActionButtonView, snackbar.getActionText(), animate); |
98 } else { | 104 } else { |
99 mActionButtonView.setVisibility(View.GONE); | 105 mActionButtonView.setVisibility(View.GONE); |
100 } | 106 } |
101 Bitmap profileImage = snackbar.getProfileImage(); | 107 Bitmap profileImage = snackbar.getProfileImage(); |
102 if (profileImage != null) { | 108 if (profileImage != null) { |
103 mProfileImageView.setImageBitmap(profileImage); | 109 mProfileImageView.setImageBitmap(profileImage); |
104 } else { | 110 } else { |
105 ((ViewGroup) view).removeView(mProfileImageView); | 111 ((ViewGroup) view).removeView(mProfileImageView); |
106 } | 112 } |
113 return true; | |
107 } | 114 } |
108 | 115 |
109 private void setViewText(TextView view, CharSequence text, boolean animate) { | 116 private void setViewText(TextView view, CharSequence text, boolean animate) { |
110 if (view.getText().toString().equals(text)) return; | 117 if (view.getText().toString().equals(text)) return; |
111 view.animate().cancel(); | 118 view.animate().cancel(); |
112 if (animate) { | 119 if (animate) { |
113 view.setAlpha(0.0f); | 120 view.setAlpha(0.0f); |
114 view.setText(text); | 121 view.setText(text); |
115 view.animate().alpha(1.f).setDuration(mAnimationDuration).setListene r(null); | 122 view.animate().alpha(1.f).setDuration(mAnimationDuration).setListene r(null); |
116 } else { | 123 } else { |
117 view.setText(text); | 124 view.setText(text); |
118 } | 125 } |
119 } | 126 } |
120 | 127 |
121 /** | 128 /** |
122 * Sends an accessibility event to mMessageView announcing that this window was added so that | 129 * Sends an accessibility event to mMessageView announcing that this window was added so that |
123 * the mMessageView content description is read aloud if accessibility is en abled. | 130 * the mMessageView content description is read aloud if accessibility is en abled. |
124 */ | 131 */ |
125 void announceforAccessibility() { | 132 void announceforAccessibility() { |
126 mMessageView.announceForAccessibility(mMessageView.getContentDescription ()); | 133 mMessageView.announceForAccessibility(mMessageView.getContentDescription ()); |
127 } | 134 } |
128 } | 135 } |
OLD | NEW |