| 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.BroadcastReceiver;
|
|
|
21 |
import android.content.Context;
|
|
|
22 |
import android.content.Intent;
|
|
|
23 |
import android.content.IntentFilter;
|
|
|
24 |
import android.content.res.TypedArray;
|
|
|
25 |
import android.graphics.Color;
|
|
|
26 |
import android.os.Bundle;
|
|
|
27 |
import android.support.v4.content.LocalBroadcastManager;
|
|
|
28 |
import android.util.AttributeSet;
|
|
|
29 |
import android.util.TypedValue;
|
|
|
30 |
import android.view.Gravity;
|
|
|
31 |
import android.view.View;
|
|
|
32 |
import android.widget.FrameLayout;
|
|
|
33 |
import android.widget.LinearLayout;
|
|
|
34 |
import android.widget.TextView;
|
|
|
35 |
import com.facebook.android.R;
|
|
|
36 |
import com.facebook.internal.*;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* This class provides the UI for displaying the Facebook Like button and its associated components.
|
|
|
40 |
*/
|
|
|
41 |
public class LikeView extends FrameLayout {
|
|
|
42 |
|
|
|
43 |
// ***
|
|
|
44 |
// Keep all the enum values in sync with attrs.xml
|
|
|
45 |
// ***
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Encapsulates the valid values for the facebook:style attribute for a LikeView
|
|
|
49 |
*/
|
|
|
50 |
public enum Style {
|
|
|
51 |
/**
|
|
|
52 |
* Setting the attribute to this value will display the button and a sentence near it that describes the
|
|
|
53 |
* social sentence for the associated object.
|
|
|
54 |
*
|
|
|
55 |
* This is the default value
|
|
|
56 |
*/
|
|
|
57 |
STANDARD("standard", 0),
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Setting the attribute to this value will display the button by itself, with no other components
|
|
|
61 |
*/
|
|
|
62 |
BUTTON("button", 1),
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Setting the attribute to this value will display the button and a box near it with the number of likes
|
|
|
66 |
* for the associated object
|
|
|
67 |
*/
|
|
|
68 |
BOX_COUNT("box_count", 2);
|
|
|
69 |
|
|
|
70 |
static Style DEFAULT = STANDARD;
|
|
|
71 |
|
|
|
72 |
static Style fromInt(int enumValue) {
|
|
|
73 |
for (Style style : values()) {
|
|
|
74 |
if (style.getValue() == enumValue) {
|
|
|
75 |
return style;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return null;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
private String stringValue;
|
|
|
83 |
private int intValue;
|
|
|
84 |
private Style(String stringValue, int value) {
|
|
|
85 |
this.stringValue = stringValue;
|
|
|
86 |
this.intValue = value;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
@Override
|
|
|
90 |
public String toString() {
|
|
|
91 |
return stringValue;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private int getValue() {
|
|
|
95 |
return intValue;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Encapsulates the valid values for the facebook:horizontal_alignment attribute for a LikeView.
|
|
|
101 |
*/
|
|
|
102 |
public enum HorizontalAlignment {
|
|
|
103 |
/**
|
|
|
104 |
* Setting the attribute to this value will center the button and auxiliary view in the parent view.
|
|
|
105 |
*
|
|
|
106 |
* This is the default value
|
|
|
107 |
*/
|
|
|
108 |
CENTER("center", 0),
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Setting the attribute to this value will left-justify the button and auxiliary view in the parent view.
|
|
|
112 |
*/
|
|
|
113 |
LEFT("left", 1),
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Setting the attribute to this value will right-justify the button and auxiliary view in the parent view.
|
|
|
117 |
* If the facebook:auxiliary_view_position is set to INLINE, then the auxiliary view will be on the
|
|
|
118 |
* left of the button
|
|
|
119 |
*/
|
|
|
120 |
RIGHT("right", 2);
|
|
|
121 |
|
|
|
122 |
static HorizontalAlignment DEFAULT = CENTER;
|
|
|
123 |
|
|
|
124 |
static HorizontalAlignment fromInt(int enumValue) {
|
|
|
125 |
for (HorizontalAlignment horizontalAlignment : values()) {
|
|
|
126 |
if (horizontalAlignment.getValue() == enumValue) {
|
|
|
127 |
return horizontalAlignment;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return null;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
private String stringValue;
|
|
|
135 |
private int intValue;
|
|
|
136 |
private HorizontalAlignment(String stringValue, int value) {
|
|
|
137 |
this.stringValue = stringValue;
|
|
|
138 |
this.intValue = value;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
@Override
|
|
|
142 |
public String toString() {
|
|
|
143 |
return stringValue;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
private int getValue() {
|
|
|
147 |
return intValue;
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Encapsulates the valid values for the facebook:auxiliary_view_position attribute for a LikeView.
|
|
|
153 |
*/
|
|
|
154 |
public enum AuxiliaryViewPosition {
|
|
|
155 |
/**
|
|
|
156 |
* Setting the attribute to this value will put the social-sentence or box-count below the like button.
|
|
|
157 |
* If the facebook:style is set to BUTTON, then this has no effect.
|
|
|
158 |
*
|
|
|
159 |
* This is the default value
|
|
|
160 |
*/
|
|
|
161 |
BOTTOM("bottom", 0),
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Setting the attribute to this value will put the social-sentence or box-count inline with the like button.
|
|
|
165 |
* The auxiliary view will be to the left of the button if the facebook:horizontal_alignment is set to RIGHT.
|
|
|
166 |
* In all other cases, it will be to the right of the button.
|
|
|
167 |
* If the facebook:style is set to BUTTON, then this has no effect.
|
|
|
168 |
*/
|
|
|
169 |
INLINE("inline", 1),
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Setting the attribute to this value will put the social-sentence or box-count above the like button.
|
|
|
173 |
* If the facebook:style is set to BUTTON, then this has no effect.
|
|
|
174 |
*/
|
|
|
175 |
TOP("top", 2);
|
|
|
176 |
|
|
|
177 |
static AuxiliaryViewPosition DEFAULT = BOTTOM;
|
|
|
178 |
|
|
|
179 |
static AuxiliaryViewPosition fromInt(int enumValue) {
|
|
|
180 |
for (AuxiliaryViewPosition auxViewPosition : values()) {
|
|
|
181 |
if (auxViewPosition.getValue() == enumValue) {
|
|
|
182 |
return auxViewPosition;
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
return null;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
private String stringValue;
|
|
|
190 |
private int intValue;
|
|
|
191 |
private AuxiliaryViewPosition(String stringValue, int value) {
|
|
|
192 |
this.stringValue = stringValue;
|
|
|
193 |
this.intValue = value;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
@Override
|
|
|
197 |
public String toString() {
|
|
|
198 |
return stringValue;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
private int getValue() {
|
|
|
202 |
return intValue;
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
private static final int NO_FOREGROUND_COLOR = -1;
|
|
|
207 |
|
|
|
208 |
private String objectId;
|
|
|
209 |
private LinearLayout containerView;
|
|
|
210 |
private LikeButton likeButton;
|
|
|
211 |
private LikeBoxCountView likeBoxCountView;
|
|
|
212 |
private TextView socialSentenceView;
|
|
|
213 |
private LikeActionController likeActionController;
|
|
|
214 |
private OnErrorListener onErrorListener;
|
|
|
215 |
private BroadcastReceiver broadcastReceiver;
|
|
|
216 |
private LikeActionControllerCreationCallback creationCallback;
|
|
|
217 |
|
|
|
218 |
private Style likeViewStyle = Style.DEFAULT;
|
|
|
219 |
private HorizontalAlignment horizontalAlignment = HorizontalAlignment.DEFAULT;
|
|
|
220 |
private AuxiliaryViewPosition auxiliaryViewPosition = AuxiliaryViewPosition.DEFAULT;
|
|
|
221 |
private int foregroundColor = NO_FOREGROUND_COLOR;
|
|
|
222 |
|
|
|
223 |
private int edgePadding;
|
|
|
224 |
private int internalPadding;
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* If your app does not use UiLifeCycleHelper, then you must call this method in the calling activity's
|
|
|
228 |
* onActivityResult method, to process any pending like actions, where tapping the button had resulted in
|
|
|
229 |
* the Like dialog being shown in the Facebook application.
|
|
|
230 |
*
|
|
|
231 |
* @param context Hosting context
|
|
|
232 |
* @param requestCode From the originating call to onActivityResult
|
|
|
233 |
* @param resultCode From the originating call to onActivityResult
|
|
|
234 |
* @param data From the originating call to onActivityResult
|
|
|
235 |
* @return Indication of whether the Intent was handled
|
|
|
236 |
*/
|
|
|
237 |
public static boolean handleOnActivityResult(Context context,
|
|
|
238 |
int requestCode,
|
|
|
239 |
int resultCode,
|
|
|
240 |
Intent data) {
|
|
|
241 |
return LikeActionController.handleOnActivityResult(context, requestCode, resultCode, data);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Constructor
|
|
|
246 |
*
|
|
|
247 |
* @param context Context for this View
|
|
|
248 |
*/
|
|
|
249 |
public LikeView(Context context) {
|
|
|
250 |
super(context);
|
|
|
251 |
initialize(context);
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
/**
|
|
|
255 |
* Constructor
|
|
|
256 |
*
|
|
|
257 |
* @param context Context for this View
|
|
|
258 |
* @param attrs AttributeSet for this View.
|
|
|
259 |
*/
|
|
|
260 |
public LikeView(Context context, AttributeSet attrs) {
|
|
|
261 |
super(context, attrs);
|
|
|
262 |
parseAttributes(attrs);
|
|
|
263 |
initialize(context);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Sets the associated object for this LikeView. Can be changed during runtime.
|
|
|
268 |
* @param objectId Object Id
|
|
|
269 |
*/
|
|
|
270 |
public void setObjectId(String objectId) {
|
|
|
271 |
objectId = Utility.coerceValueIfNullOrEmpty(objectId, null);
|
|
|
272 |
if (!Utility.areObjectsEqual(objectId, this.objectId)) {
|
|
|
273 |
setObjectIdForced(objectId);
|
|
|
274 |
|
|
|
275 |
updateLikeStateAndLayout();
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Sets the facebook:style for this LikeView. Can be changed during runtime.
|
|
|
281 |
* @param likeViewStyle Should be either LikeView.STANDARD, LikeView.BUTTON or LikeView.BOX_COUNT
|
|
|
282 |
*/
|
|
|
283 |
public void setLikeViewStyle(Style likeViewStyle) {
|
|
|
284 |
likeViewStyle = likeViewStyle != null ? likeViewStyle : Style.DEFAULT;
|
|
|
285 |
if (this.likeViewStyle != likeViewStyle) {
|
|
|
286 |
this.likeViewStyle = likeViewStyle;
|
|
|
287 |
|
|
|
288 |
updateLayout();
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Sets the facebook:auxiliary_view_position for this LikeView. Can be changed during runtime.
|
|
|
294 |
* @param auxiliaryViewPosition Should be either LikeView.TOP, LikeView.INLINE or LikeView.BOTTOM
|
|
|
295 |
*/
|
|
|
296 |
public void setAuxiliaryViewPosition(AuxiliaryViewPosition auxiliaryViewPosition) {
|
|
|
297 |
auxiliaryViewPosition = auxiliaryViewPosition != null ? auxiliaryViewPosition : AuxiliaryViewPosition.DEFAULT;
|
|
|
298 |
if (this.auxiliaryViewPosition != auxiliaryViewPosition) {
|
|
|
299 |
this.auxiliaryViewPosition = auxiliaryViewPosition;
|
|
|
300 |
|
|
|
301 |
updateLayout();
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Sets the facebook:horizontal_alignment for this LikeView. Can be changed during runtime.
|
|
|
307 |
* @param horizontalAlignment Should be either LikeView.LEFT, LikeView.CENTER or LikeView.RIGHT
|
|
|
308 |
*/
|
|
|
309 |
public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
|
|
|
310 |
horizontalAlignment = horizontalAlignment != null ? horizontalAlignment : HorizontalAlignment.DEFAULT;
|
|
|
311 |
if (this.horizontalAlignment != horizontalAlignment) {
|
|
|
312 |
this.horizontalAlignment = horizontalAlignment;
|
|
|
313 |
|
|
|
314 |
updateLayout();
|
|
|
315 |
}
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Sets the facebook:foreground_color for this LikeView. Can be changed during runtime.
|
|
|
320 |
* The color is only used for the social sentence text.
|
|
|
321 |
* @param foregroundColor And valid android.graphics.Color value.
|
|
|
322 |
*/
|
|
|
323 |
public void setForegroundColor(int foregroundColor) {
|
|
|
324 |
if (this.foregroundColor != foregroundColor) {
|
|
|
325 |
socialSentenceView.setTextColor(foregroundColor);
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
/**
|
|
|
330 |
* Sets an OnErrorListener for this instance of LikeView to call into when
|
|
|
331 |
* certain exceptions occur.
|
|
|
332 |
*
|
|
|
333 |
* @param onErrorListener The listener object to set
|
|
|
334 |
*/
|
|
|
335 |
public void setOnErrorListener(OnErrorListener onErrorListener) {
|
|
|
336 |
this.onErrorListener = onErrorListener;
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Returns the current OnErrorListener for this instance of LikeView.
|
|
|
341 |
*
|
|
|
342 |
* @return The OnErrorListener
|
|
|
343 |
*/
|
|
|
344 |
public OnErrorListener getOnErrorListener() {
|
|
|
345 |
return onErrorListener;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
@Override
|
|
|
349 |
protected void onDetachedFromWindow() {
|
|
|
350 |
// Disassociate from the object
|
|
|
351 |
setObjectId(null);
|
|
|
352 |
|
|
|
353 |
super.onDetachedFromWindow();
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
private void parseAttributes(AttributeSet attrs) {
|
|
|
357 |
if (attrs == null || getContext() == null) {
|
|
|
358 |
return;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_like_view);
|
|
|
362 |
if (a == null) {
|
|
|
363 |
return;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
objectId = Utility.coerceValueIfNullOrEmpty(a.getString(R.styleable.com_facebook_like_view_object_id), null);
|
|
|
367 |
likeViewStyle = Style.fromInt(
|
|
|
368 |
a.getInt(R.styleable.com_facebook_like_view_style,
|
|
|
369 |
Style.DEFAULT.getValue()));
|
|
|
370 |
if (likeViewStyle == null) {
|
|
|
371 |
throw new IllegalArgumentException("Unsupported value for LikeView 'style'");
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
auxiliaryViewPosition = AuxiliaryViewPosition.fromInt(
|
|
|
375 |
a.getInt(R.styleable.com_facebook_like_view_auxiliary_view_position,
|
|
|
376 |
AuxiliaryViewPosition.DEFAULT.getValue()));
|
|
|
377 |
if (auxiliaryViewPosition == null) {
|
|
|
378 |
throw new IllegalArgumentException("Unsupported value for LikeView 'auxiliary_view_position'");
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
horizontalAlignment = HorizontalAlignment.fromInt(
|
|
|
382 |
a.getInt(R.styleable.com_facebook_like_view_horizontal_alignment,
|
|
|
383 |
HorizontalAlignment.DEFAULT.getValue()));
|
|
|
384 |
if (horizontalAlignment == null) {
|
|
|
385 |
throw new IllegalArgumentException("Unsupported value for LikeView 'horizontal_alignment'");
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
foregroundColor = a.getColor(R.styleable.com_facebook_like_view_foreground_color, NO_FOREGROUND_COLOR);
|
|
|
389 |
|
|
|
390 |
a.recycle();
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
// If attributes were present, parseAttributes MUST be called before initialize() to ensure proper behavior
|
|
|
394 |
private void initialize(Context context) {
|
|
|
395 |
edgePadding = getResources().getDimensionPixelSize(R.dimen.com_facebook_likeview_edge_padding);
|
|
|
396 |
internalPadding = getResources().getDimensionPixelSize(R.dimen.com_facebook_likeview_internal_padding);
|
|
|
397 |
if (foregroundColor == NO_FOREGROUND_COLOR) {
|
|
|
398 |
foregroundColor = getResources().getColor(R.color.com_facebook_likeview_text_color);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
setBackgroundColor(Color.TRANSPARENT);
|
|
|
402 |
|
|
|
403 |
containerView = new LinearLayout(context);
|
|
|
404 |
LayoutParams containerViewLayoutParams = new LayoutParams(
|
|
|
405 |
LayoutParams.WRAP_CONTENT,
|
|
|
406 |
LayoutParams.WRAP_CONTENT);
|
|
|
407 |
containerView.setLayoutParams(containerViewLayoutParams);
|
|
|
408 |
|
|
|
409 |
initializeLikeButton(context);
|
|
|
410 |
initializeSocialSentenceView(context);
|
|
|
411 |
initializeLikeCountView(context);
|
|
|
412 |
|
|
|
413 |
containerView.addView(likeButton);
|
|
|
414 |
containerView.addView(socialSentenceView);
|
|
|
415 |
containerView.addView(likeBoxCountView);
|
|
|
416 |
|
|
|
417 |
addView(containerView);
|
|
|
418 |
|
|
|
419 |
setObjectIdForced(this.objectId);
|
|
|
420 |
updateLikeStateAndLayout();
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
private void initializeLikeButton(Context context) {
|
|
|
424 |
likeButton = new LikeButton(
|
|
|
425 |
context,
|
|
|
426 |
likeActionController != null ? likeActionController.isObjectLiked() : false);
|
|
|
427 |
likeButton.setOnClickListener(new OnClickListener() {
|
|
|
428 |
@Override
|
|
|
429 |
public void onClick(View v) {
|
|
|
430 |
toggleLike();
|
|
|
431 |
}
|
|
|
432 |
});
|
|
|
433 |
|
|
|
434 |
LinearLayout.LayoutParams buttonLayout = new LinearLayout.LayoutParams(
|
|
|
435 |
LayoutParams.WRAP_CONTENT,
|
|
|
436 |
LayoutParams.WRAP_CONTENT);
|
|
|
437 |
|
|
|
438 |
likeButton.setLayoutParams(buttonLayout);
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
private void initializeSocialSentenceView(Context context) {
|
|
|
442 |
socialSentenceView = new TextView(context);
|
|
|
443 |
socialSentenceView.setTextSize(
|
|
|
444 |
TypedValue.COMPLEX_UNIT_PX,
|
|
|
445 |
getResources().getDimension(R.dimen.com_facebook_likeview_text_size));
|
|
|
446 |
socialSentenceView.setMaxLines(2);
|
|
|
447 |
socialSentenceView.setTextColor(foregroundColor);
|
|
|
448 |
socialSentenceView.setGravity(Gravity.CENTER);
|
|
|
449 |
|
|
|
450 |
LinearLayout.LayoutParams socialSentenceViewLayout = new LinearLayout.LayoutParams(
|
|
|
451 |
LayoutParams.WRAP_CONTENT,
|
|
|
452 |
LayoutParams.MATCH_PARENT);
|
|
|
453 |
socialSentenceView.setLayoutParams(socialSentenceViewLayout);
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
private void initializeLikeCountView(Context context) {
|
|
|
457 |
likeBoxCountView = new LikeBoxCountView(context);
|
|
|
458 |
|
|
|
459 |
LinearLayout.LayoutParams likeCountViewLayout = new LinearLayout.LayoutParams(
|
|
|
460 |
LayoutParams.MATCH_PARENT,
|
|
|
461 |
LayoutParams.MATCH_PARENT);
|
|
|
462 |
likeBoxCountView.setLayoutParams(likeCountViewLayout);
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
private void toggleLike() {
|
|
|
466 |
if (likeActionController != null) {
|
|
|
467 |
Activity activity = (Activity)getContext();
|
|
|
468 |
likeActionController.toggleLike(activity, getAnalyticsParameters());
|
|
|
469 |
}
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
private Bundle getAnalyticsParameters() {
|
|
|
473 |
Bundle params = new Bundle();
|
|
|
474 |
params.putString(AnalyticsEvents.PARAMETER_LIKE_VIEW_STYLE, likeViewStyle.toString());
|
|
|
475 |
params.putString(AnalyticsEvents.PARAMETER_LIKE_VIEW_AUXILIARY_POSITION, auxiliaryViewPosition.toString());
|
|
|
476 |
params.putString(AnalyticsEvents.PARAMETER_LIKE_VIEW_HORIZONTAL_ALIGNMENT, horizontalAlignment.toString());
|
|
|
477 |
params.putString(AnalyticsEvents.PARAMETER_LIKE_VIEW_OBJECT_ID, Utility.coerceValueIfNullOrEmpty(objectId, ""));
|
|
|
478 |
return params;
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
private void setObjectIdForced(String newObjectId) {
|
|
|
482 |
tearDownObjectAssociations();
|
|
|
483 |
|
|
|
484 |
objectId = newObjectId;
|
|
|
485 |
if (Utility.isNullOrEmpty(newObjectId)) {
|
|
|
486 |
return;
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
creationCallback = new LikeActionControllerCreationCallback();
|
|
|
490 |
LikeActionController.getControllerForObjectId(
|
|
|
491 |
getContext(),
|
|
|
492 |
newObjectId,
|
|
|
493 |
creationCallback);
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
private void associateWithLikeActionController(LikeActionController likeActionController) {
|
|
|
497 |
this.likeActionController = likeActionController;
|
|
|
498 |
|
|
|
499 |
this.broadcastReceiver = new LikeControllerBroadcastReceiver();
|
|
|
500 |
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
|
|
|
501 |
|
|
|
502 |
// add the broadcast receiver
|
|
|
503 |
IntentFilter filter = new IntentFilter();
|
|
|
504 |
filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_UPDATED);
|
|
|
505 |
filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR);
|
|
|
506 |
filter.addAction(LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_RESET);
|
|
|
507 |
|
|
|
508 |
localBroadcastManager.registerReceiver(broadcastReceiver, filter);
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
private void tearDownObjectAssociations() {
|
|
|
512 |
if (broadcastReceiver != null) {
|
|
|
513 |
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
|
|
|
514 |
localBroadcastManager.unregisterReceiver(broadcastReceiver);
|
|
|
515 |
|
|
|
516 |
broadcastReceiver = null;
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
// If we were already waiting on a controller to be given back, make sure we aren't waiting anymore.
|
|
|
520 |
// Otherwise when that controller is given back to the callback, it will go and register a broadcast receiver
|
|
|
521 |
// for it.
|
|
|
522 |
if (creationCallback != null) {
|
|
|
523 |
creationCallback.cancel();
|
|
|
524 |
|
|
|
525 |
creationCallback = null;
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
likeActionController = null;
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
private void updateLikeStateAndLayout() {
|
|
|
532 |
if (likeActionController == null) {
|
|
|
533 |
likeButton.setLikeState(false);
|
|
|
534 |
socialSentenceView.setText(null);
|
|
|
535 |
likeBoxCountView.setText(null);
|
|
|
536 |
} else {
|
|
|
537 |
likeButton.setLikeState(likeActionController.isObjectLiked());
|
|
|
538 |
socialSentenceView.setText(likeActionController.getSocialSentence());
|
|
|
539 |
likeBoxCountView.setText(likeActionController.getLikeCountString());
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
updateLayout();
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
private void updateLayout() {
|
|
|
546 |
// Make sure the container is horizontally aligned according to specifications.
|
|
|
547 |
LayoutParams containerViewLayoutParams = (LayoutParams)containerView.getLayoutParams();
|
|
|
548 |
LinearLayout.LayoutParams buttonLayoutParams = (LinearLayout.LayoutParams)likeButton.getLayoutParams();
|
|
|
549 |
int viewGravity =
|
|
|
550 |
horizontalAlignment == HorizontalAlignment.LEFT ? Gravity.LEFT :
|
|
|
551 |
horizontalAlignment == HorizontalAlignment.CENTER ? Gravity.CENTER_HORIZONTAL : Gravity.RIGHT;
|
|
|
552 |
|
|
|
553 |
containerViewLayoutParams.gravity = viewGravity | Gravity.TOP;
|
|
|
554 |
buttonLayoutParams.gravity = viewGravity;
|
|
|
555 |
|
|
|
556 |
// Choose the right auxiliary view to make visible.
|
|
|
557 |
socialSentenceView.setVisibility(GONE);
|
|
|
558 |
likeBoxCountView.setVisibility(GONE);
|
|
|
559 |
|
|
|
560 |
View auxView;
|
|
|
561 |
if (likeViewStyle == Style.STANDARD &&
|
|
|
562 |
likeActionController != null &&
|
|
|
563 |
!Utility.isNullOrEmpty(likeActionController.getSocialSentence())) {
|
|
|
564 |
auxView = socialSentenceView;
|
|
|
565 |
} else if (likeViewStyle == Style.BOX_COUNT &&
|
|
|
566 |
likeActionController != null &&
|
|
|
567 |
!Utility.isNullOrEmpty(likeActionController.getLikeCountString())) {
|
|
|
568 |
updateBoxCountCaretPosition();
|
|
|
569 |
auxView = likeBoxCountView;
|
|
|
570 |
} else {
|
|
|
571 |
// No more work to be done.
|
|
|
572 |
return;
|
|
|
573 |
}
|
|
|
574 |
auxView.setVisibility(VISIBLE);
|
|
|
575 |
|
|
|
576 |
// Now position the auxiliary view properly
|
|
|
577 |
LinearLayout.LayoutParams auxViewLayoutParams = (LinearLayout.LayoutParams)auxView.getLayoutParams();
|
|
|
578 |
auxViewLayoutParams.gravity = viewGravity;
|
|
|
579 |
|
|
|
580 |
containerView.setOrientation(
|
|
|
581 |
auxiliaryViewPosition == AuxiliaryViewPosition.INLINE ?
|
|
|
582 |
LinearLayout.HORIZONTAL :
|
|
|
583 |
LinearLayout.VERTICAL);
|
|
|
584 |
|
|
|
585 |
if (auxiliaryViewPosition == AuxiliaryViewPosition.TOP ||
|
|
|
586 |
(auxiliaryViewPosition == AuxiliaryViewPosition.INLINE &&
|
|
|
587 |
horizontalAlignment == HorizontalAlignment.RIGHT)) {
|
|
|
588 |
// Button comes after the auxiliary view. Make sure it is at the end
|
|
|
589 |
containerView.removeView(likeButton);
|
|
|
590 |
containerView.addView(likeButton);
|
|
|
591 |
} else {
|
|
|
592 |
// In all other cases, the button comes first
|
|
|
593 |
containerView.removeView(auxView);
|
|
|
594 |
containerView.addView(auxView);
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
switch (auxiliaryViewPosition) {
|
|
|
598 |
case TOP:
|
|
|
599 |
auxView.setPadding(edgePadding, edgePadding, edgePadding, internalPadding);
|
|
|
600 |
break;
|
|
|
601 |
case BOTTOM:
|
|
|
602 |
auxView.setPadding(edgePadding, internalPadding, edgePadding, edgePadding);
|
|
|
603 |
break;
|
|
|
604 |
case INLINE:
|
|
|
605 |
if (horizontalAlignment == HorizontalAlignment.RIGHT) {
|
|
|
606 |
auxView.setPadding(edgePadding, edgePadding, internalPadding, edgePadding);
|
|
|
607 |
} else {
|
|
|
608 |
auxView.setPadding(internalPadding, edgePadding, edgePadding, edgePadding);
|
|
|
609 |
}
|
|
|
610 |
break;
|
|
|
611 |
}
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
private void updateBoxCountCaretPosition() {
|
|
|
615 |
switch (auxiliaryViewPosition) {
|
|
|
616 |
case TOP:
|
|
|
617 |
likeBoxCountView.setCaretPosition(LikeBoxCountView.LikeBoxCountViewCaretPosition.BOTTOM);
|
|
|
618 |
break;
|
|
|
619 |
case BOTTOM:
|
|
|
620 |
likeBoxCountView.setCaretPosition(LikeBoxCountView.LikeBoxCountViewCaretPosition.TOP);
|
|
|
621 |
break;
|
|
|
622 |
case INLINE:
|
|
|
623 |
likeBoxCountView.setCaretPosition(
|
|
|
624 |
horizontalAlignment == HorizontalAlignment.RIGHT ?
|
|
|
625 |
LikeBoxCountView.LikeBoxCountViewCaretPosition.RIGHT :
|
|
|
626 |
LikeBoxCountView.LikeBoxCountViewCaretPosition.LEFT);
|
|
|
627 |
break;
|
|
|
628 |
}
|
|
|
629 |
}
|
|
|
630 |
|
|
|
631 |
/**
|
|
|
632 |
* Callback interface that will be called when a network or other error is encountered
|
|
|
633 |
* while logging in.
|
|
|
634 |
*/
|
|
|
635 |
public interface OnErrorListener {
|
|
|
636 |
/**
|
|
|
637 |
* Called when a network or other error is encountered.
|
|
|
638 |
* @param errorBundle a FacebookException representing the error that was encountered.
|
|
|
639 |
*/
|
|
|
640 |
void onError(Bundle errorBundle);
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
private class LikeControllerBroadcastReceiver extends BroadcastReceiver {
|
|
|
644 |
@Override
|
|
|
645 |
public void onReceive(Context context, Intent intent) {
|
|
|
646 |
String intentAction = intent.getAction();
|
|
|
647 |
Bundle extras = intent.getExtras();
|
|
|
648 |
boolean shouldRespond = true;
|
|
|
649 |
if (extras != null) {
|
|
|
650 |
// See if an Id was set in the broadcast Intent. If it was, treat it as a filter.
|
|
|
651 |
String broadcastObjectId = extras.getString(LikeActionController.ACTION_OBJECT_ID_KEY);
|
|
|
652 |
shouldRespond = Utility.isNullOrEmpty(broadcastObjectId) ||
|
|
|
653 |
Utility.areObjectsEqual(objectId, broadcastObjectId);
|
|
|
654 |
}
|
|
|
655 |
|
|
|
656 |
if (!shouldRespond) {
|
|
|
657 |
return;
|
|
|
658 |
}
|
|
|
659 |
|
|
|
660 |
if (LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_UPDATED.equals(intentAction)) {
|
|
|
661 |
updateLikeStateAndLayout();
|
|
|
662 |
} else if (LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_ERROR.equals(intentAction)) {
|
|
|
663 |
if (onErrorListener != null) {
|
|
|
664 |
onErrorListener.onError(extras);
|
|
|
665 |
}
|
|
|
666 |
} else if (LikeActionController.ACTION_LIKE_ACTION_CONTROLLER_DID_RESET.equals(intentAction)) {
|
|
|
667 |
// This will recreate the controller and associated objects
|
|
|
668 |
setObjectIdForced(objectId);
|
|
|
669 |
updateLikeStateAndLayout();
|
|
|
670 |
}
|
|
|
671 |
}
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
private class LikeActionControllerCreationCallback implements LikeActionController.CreationCallback {
|
|
|
675 |
private boolean isCancelled;
|
|
|
676 |
|
|
|
677 |
public void cancel() {
|
|
|
678 |
isCancelled = true;
|
|
|
679 |
}
|
|
|
680 |
|
|
|
681 |
@Override
|
|
|
682 |
public void onComplete(LikeActionController likeActionController) {
|
|
|
683 |
if (isCancelled) {
|
|
|
684 |
return;
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
associateWithLikeActionController(likeActionController);
|
|
|
688 |
updateLikeStateAndLayout();
|
|
|
689 |
|
|
|
690 |
LikeView.this.creationCallback = null;
|
|
|
691 |
}
|
|
|
692 |
}
|
|
|
693 |
}
|