| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook.widget;
|
|
|
18 |
|
|
|
19 |
import android.app.Activity;
|
|
|
20 |
import android.content.Context;
|
|
|
21 |
import android.view.LayoutInflater;
|
|
|
22 |
import android.view.View;
|
|
|
23 |
import android.view.ViewTreeObserver;
|
|
|
24 |
import android.view.Window;
|
|
|
25 |
import android.widget.FrameLayout;
|
|
|
26 |
import android.widget.ImageView;
|
|
|
27 |
import android.widget.PopupWindow;
|
|
|
28 |
import android.widget.TextView;
|
|
|
29 |
import com.facebook.android.R;
|
|
|
30 |
|
|
|
31 |
import java.lang.ref.WeakReference;
|
|
|
32 |
|
|
|
33 |
public class ToolTipPopup {
|
|
|
34 |
|
|
|
35 |
public static enum Style {
|
|
|
36 |
/**
|
|
|
37 |
* The tool tip will be shown with a blue style; including a blue background and blue
|
|
|
38 |
* arrows.
|
|
|
39 |
*/
|
|
|
40 |
BLUE,
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The tool tip will be shown with a black style; including a black background and black
|
|
|
44 |
* arrows.
|
|
|
45 |
*/
|
|
|
46 |
BLACK
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* The default time that the tool tip will be displayed
|
|
|
51 |
*/
|
|
|
52 |
public static final long DEFAULT_POPUP_DISPLAY_TIME = 6000;
|
|
|
53 |
|
|
|
54 |
private final String mText;
|
|
|
55 |
private final WeakReference<View> mAnchorViewRef;
|
|
|
56 |
private final Context mContext;
|
|
|
57 |
private PopupContentView mPopupContent;
|
|
|
58 |
private PopupWindow mPopupWindow;
|
|
|
59 |
private Style mStyle = Style.BLUE;
|
|
|
60 |
private long mNuxDisplayTime = DEFAULT_POPUP_DISPLAY_TIME;
|
|
|
61 |
|
|
|
62 |
private final ViewTreeObserver.OnScrollChangedListener mScrollListener =
|
|
|
63 |
new ViewTreeObserver.OnScrollChangedListener() {
|
|
|
64 |
@Override
|
|
|
65 |
public void onScrollChanged() {
|
|
|
66 |
if (mAnchorViewRef.get() != null &&
|
|
|
67 |
mPopupWindow != null &&
|
|
|
68 |
mPopupWindow.isShowing()) {
|
|
|
69 |
if (mPopupWindow.isAboveAnchor()) {
|
|
|
70 |
mPopupContent.showBottomArrow();
|
|
|
71 |
} else {
|
|
|
72 |
mPopupContent.showTopArrow();
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Create a new ToolTipPopup
|
|
|
80 |
* @param text The text to be displayed in the tool tip
|
|
|
81 |
* @param anchor The view to anchor this tool tip to.
|
|
|
82 |
*/
|
|
|
83 |
public ToolTipPopup(String text, View anchor) {
|
|
|
84 |
mText = text;
|
|
|
85 |
mAnchorViewRef = new WeakReference<View>(anchor);
|
|
|
86 |
mContext = anchor.getContext();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Sets the {@link Style} of this tool tip.
|
|
|
91 |
* @param mStyle
|
|
|
92 |
*/
|
|
|
93 |
public void setStyle(Style mStyle) {
|
|
|
94 |
this.mStyle = mStyle;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Display this tool tip to the user
|
|
|
99 |
*/
|
|
|
100 |
public void show() {
|
|
|
101 |
if (mAnchorViewRef.get() != null) {
|
|
|
102 |
mPopupContent = new PopupContentView(mContext);
|
|
|
103 |
TextView body = (TextView) mPopupContent.findViewById(
|
|
|
104 |
R.id.com_facebook_tooltip_bubble_view_text_body);
|
|
|
105 |
body.setText(mText);
|
|
|
106 |
if (mStyle == Style.BLUE) {
|
|
|
107 |
mPopupContent.bodyFrame.setBackgroundResource(
|
|
|
108 |
R.drawable.com_facebook_tooltip_blue_background);
|
|
|
109 |
mPopupContent.bottomArrow.setImageResource(
|
|
|
110 |
R.drawable.com_facebook_tooltip_blue_bottomnub);
|
|
|
111 |
mPopupContent.topArrow.setImageResource(
|
|
|
112 |
R.drawable.com_facebook_tooltip_blue_topnub);
|
|
|
113 |
mPopupContent.xOut.setImageResource(R.drawable.com_facebook_tooltip_blue_xout);
|
|
|
114 |
} else {
|
|
|
115 |
mPopupContent.bodyFrame.setBackgroundResource(
|
|
|
116 |
R.drawable.com_facebook_tooltip_black_background);
|
|
|
117 |
mPopupContent.bottomArrow.setImageResource(
|
|
|
118 |
R.drawable.com_facebook_tooltip_black_bottomnub);
|
|
|
119 |
mPopupContent.topArrow.setImageResource(
|
|
|
120 |
R.drawable.com_facebook_tooltip_black_topnub);
|
|
|
121 |
mPopupContent.xOut.setImageResource(R.drawable.com_facebook_tooltip_black_xout);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
final Window window = ((Activity) mContext).getWindow();
|
|
|
125 |
final View decorView = window.getDecorView();
|
|
|
126 |
final int decorWidth = decorView.getWidth();
|
|
|
127 |
final int decorHeight = decorView.getHeight();
|
|
|
128 |
registerObserver();
|
|
|
129 |
mPopupContent.onMeasure(
|
|
|
130 |
View.MeasureSpec.makeMeasureSpec(decorWidth, View.MeasureSpec.AT_MOST),
|
|
|
131 |
View.MeasureSpec.makeMeasureSpec(decorHeight, View.MeasureSpec.AT_MOST));
|
|
|
132 |
mPopupWindow = new PopupWindow(
|
|
|
133 |
mPopupContent,
|
|
|
134 |
mPopupContent.getMeasuredWidth(),
|
|
|
135 |
mPopupContent.getMeasuredHeight());
|
|
|
136 |
mPopupWindow.showAsDropDown(mAnchorViewRef.get());
|
|
|
137 |
updateArrows();
|
|
|
138 |
if (mNuxDisplayTime > 0) {
|
|
|
139 |
mPopupContent.postDelayed(new Runnable() {
|
|
|
140 |
@Override
|
|
|
141 |
public void run() {
|
|
|
142 |
dismiss();
|
|
|
143 |
}
|
|
|
144 |
}, mNuxDisplayTime);
|
|
|
145 |
}
|
|
|
146 |
mPopupWindow.setTouchable(true);
|
|
|
147 |
mPopupContent.setOnClickListener(new View.OnClickListener() {
|
|
|
148 |
@Override
|
|
|
149 |
public void onClick(View v) {
|
|
|
150 |
dismiss();
|
|
|
151 |
}
|
|
|
152 |
});
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Set the time (in milliseconds) the tool tip will be displayed. Any number less than or equal
|
|
|
158 |
* to 0 will cause the tool tip to be displayed indefinitely
|
|
|
159 |
* @param displayTime The amount of time (in milliseconds) to display the tool tip
|
|
|
160 |
*/
|
|
|
161 |
public void setNuxDisplayTime(long displayTime) {
|
|
|
162 |
this.mNuxDisplayTime = displayTime;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
private void updateArrows() {
|
|
|
166 |
if (mPopupWindow != null && mPopupWindow.isShowing()) {
|
|
|
167 |
if (mPopupWindow.isAboveAnchor()) {
|
|
|
168 |
mPopupContent.showBottomArrow();
|
|
|
169 |
} else {
|
|
|
170 |
mPopupContent.showTopArrow();
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Dismiss the tool tip
|
|
|
177 |
*/
|
|
|
178 |
public void dismiss() {
|
|
|
179 |
unregisterObserver();
|
|
|
180 |
if (mPopupWindow != null) {
|
|
|
181 |
mPopupWindow.dismiss();
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private void registerObserver() {
|
|
|
186 |
unregisterObserver();
|
|
|
187 |
if (mAnchorViewRef.get() != null) {
|
|
|
188 |
mAnchorViewRef.get().getViewTreeObserver().addOnScrollChangedListener(mScrollListener);
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
private void unregisterObserver() {
|
|
|
193 |
if (mAnchorViewRef.get() != null) {
|
|
|
194 |
mAnchorViewRef.get().getViewTreeObserver().removeOnScrollChangedListener(mScrollListener);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
private class PopupContentView extends FrameLayout {
|
|
|
199 |
private ImageView topArrow;
|
|
|
200 |
private ImageView bottomArrow;
|
|
|
201 |
private View bodyFrame;
|
|
|
202 |
private ImageView xOut;
|
|
|
203 |
|
|
|
204 |
public PopupContentView(Context context) {
|
|
|
205 |
super(context);
|
|
|
206 |
init();
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
private void init() {
|
|
|
210 |
LayoutInflater inflater = LayoutInflater.from(getContext());
|
|
|
211 |
inflater.inflate(R.layout.com_facebook_tooltip_bubble, this);
|
|
|
212 |
topArrow = (ImageView) findViewById(R.id.com_facebook_tooltip_bubble_view_top_pointer);
|
|
|
213 |
bottomArrow = (ImageView) findViewById(
|
|
|
214 |
R.id.com_facebook_tooltip_bubble_view_bottom_pointer);
|
|
|
215 |
bodyFrame = findViewById(R.id.com_facebook_body_frame);
|
|
|
216 |
xOut = (ImageView) findViewById(R.id.com_facebook_button_xout);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
public void showTopArrow() {
|
|
|
220 |
topArrow.setVisibility(View.VISIBLE);
|
|
|
221 |
bottomArrow.setVisibility(View.INVISIBLE);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public void showBottomArrow() {
|
|
|
225 |
topArrow.setVisibility(View.INVISIBLE);
|
|
|
226 |
bottomArrow.setVisibility(View.VISIBLE);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
// Expose so popup content can be sized
|
|
|
230 |
@Override
|
|
|
231 |
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
232 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
}
|