| 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.annotation.SuppressLint;
|
|
|
20 |
import android.app.Activity;
|
|
|
21 |
import android.content.res.TypedArray;
|
|
|
22 |
import android.os.Bundle;
|
|
|
23 |
import android.text.TextUtils;
|
|
|
24 |
import android.util.AttributeSet;
|
|
|
25 |
import com.facebook.AppEventsLogger;
|
|
|
26 |
import com.facebook.FacebookException;
|
|
|
27 |
import com.facebook.Request;
|
|
|
28 |
import com.facebook.Session;
|
|
|
29 |
import com.facebook.android.R;
|
|
|
30 |
import com.facebook.internal.AnalyticsEvents;
|
|
|
31 |
import com.facebook.model.GraphUser;
|
|
|
32 |
|
|
|
33 |
import java.util.*;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Provides a Fragment that displays a list of a user's friends and allows one or more of the
|
|
|
37 |
* friends to be selected.
|
|
|
38 |
*/
|
|
|
39 |
public class FriendPickerFragment extends PickerFragment<GraphUser> {
|
|
|
40 |
/**
|
|
|
41 |
* The key for a String parameter in the fragment's Intent bundle to indicate what user's
|
|
|
42 |
* friends should be shown. The default is to display the currently authenticated user's friends.
|
|
|
43 |
*/
|
|
|
44 |
public static final String USER_ID_BUNDLE_KEY = "com.facebook.widget.FriendPickerFragment.UserId";
|
|
|
45 |
/**
|
|
|
46 |
* The key for a boolean parameter in the fragment's Intent bundle to indicate whether the
|
|
|
47 |
* picker should allow more than one friend to be selected or not.
|
|
|
48 |
*/
|
|
|
49 |
public static final String MULTI_SELECT_BUNDLE_KEY = "com.facebook.widget.FriendPickerFragment.MultiSelect";
|
|
|
50 |
/**
|
|
|
51 |
* The key for a String parameter in the fragment's Intent bundle to indicate the type of friend picker to use.
|
|
|
52 |
* This value is case sensitive, and must match the enum @{link FriendPickerType}
|
|
|
53 |
*/
|
|
|
54 |
public static final String FRIEND_PICKER_TYPE_KEY = "com.facebook.widget.FriendPickerFragment.FriendPickerType";
|
|
|
55 |
|
|
|
56 |
public enum FriendPickerType {
|
|
|
57 |
FRIENDS("/friends", true),
|
|
|
58 |
TAGGABLE_FRIENDS("/taggable_friends", false),
|
|
|
59 |
INVITABLE_FRIENDS("/invitable_friends", false);
|
|
|
60 |
|
|
|
61 |
private final String requestPath;
|
|
|
62 |
private final boolean requestIsCacheable;
|
|
|
63 |
|
|
|
64 |
FriendPickerType(String path, boolean cacheable) {
|
|
|
65 |
this.requestPath = path;
|
|
|
66 |
this.requestIsCacheable = cacheable;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
String getRequestPath() {
|
|
|
70 |
return requestPath;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
boolean isCacheable() {
|
|
|
74 |
return requestIsCacheable;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private static final String ID = "id";
|
|
|
79 |
private static final String NAME = "name";
|
|
|
80 |
|
|
|
81 |
private String userId;
|
|
|
82 |
|
|
|
83 |
private boolean multiSelect = true;
|
|
|
84 |
|
|
|
85 |
// default to Friends for backwards compatibility
|
|
|
86 |
private FriendPickerType friendPickerType = FriendPickerType.FRIENDS;
|
|
|
87 |
|
|
|
88 |
private List<String> preSelectedFriendIds = new ArrayList<String>();
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Default constructor. Creates a Fragment with all default properties.
|
|
|
92 |
*/
|
|
|
93 |
public FriendPickerFragment() {
|
|
|
94 |
this(null);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Constructor.
|
|
|
99 |
* @param args a Bundle that optionally contains one or more values containing additional
|
|
|
100 |
* configuration information for the Fragment.
|
|
|
101 |
*/
|
|
|
102 |
@SuppressLint("ValidFragment")
|
|
|
103 |
public FriendPickerFragment(Bundle args) {
|
|
|
104 |
super(GraphUser.class, R.layout.com_facebook_friendpickerfragment, args);
|
|
|
105 |
setFriendPickerSettingsFromBundle(args);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Gets the ID of the user whose friends should be displayed. If null, the default is to
|
|
|
110 |
* show the currently authenticated user's friends.
|
|
|
111 |
* @return the user ID, or null
|
|
|
112 |
*/
|
|
|
113 |
public String getUserId() {
|
|
|
114 |
return userId;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Sets the ID of the user whose friends should be displayed. If null, the default is to
|
|
|
119 |
* show the currently authenticated user's friends.
|
|
|
120 |
* @param userId the user ID, or null
|
|
|
121 |
*/
|
|
|
122 |
public void setUserId(String userId) {
|
|
|
123 |
this.userId = userId;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Gets whether the user can select multiple friends, or only one friend.
|
|
|
128 |
* @return true if the user can select multiple friends, false if only one friend
|
|
|
129 |
*/
|
|
|
130 |
public boolean getMultiSelect() {
|
|
|
131 |
return multiSelect;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Sets whether the user can select multiple friends, or only one friend.
|
|
|
136 |
* @param multiSelect true if the user can select multiple friends, false if only one friend
|
|
|
137 |
*/
|
|
|
138 |
public void setMultiSelect(boolean multiSelect) {
|
|
|
139 |
if (this.multiSelect != multiSelect) {
|
|
|
140 |
this.multiSelect = multiSelect;
|
|
|
141 |
setSelectionStrategy(createSelectionStrategy());
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Sets the friend picker type for this fragment.
|
|
|
147 |
* @param type the type of friend picker to use.
|
|
|
148 |
*/
|
|
|
149 |
public void setFriendPickerType(FriendPickerType type) {
|
|
|
150 |
this.friendPickerType = type;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Sets the list of friends for pre selection. These friends will be selected by default.
|
|
|
155 |
* @param userIds list of friends as ids
|
|
|
156 |
*/
|
|
|
157 |
public void setSelectionByIds(List<String> userIds) {
|
|
|
158 |
preSelectedFriendIds.addAll(userIds);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Sets the list of friends for pre selection. These friends will be selected by default.
|
|
|
163 |
* @param userIds list of friends as ids
|
|
|
164 |
*/
|
|
|
165 |
public void setSelectionByIds(String... userIds) {
|
|
|
166 |
setSelectionByIds(Arrays.asList(userIds));
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Sets the list of friends for pre selection. These friends will be selected by default.
|
|
|
171 |
* @param graphUsers list of friends as GraphUsers
|
|
|
172 |
*/
|
|
|
173 |
public void setSelection(GraphUser... graphUsers) {
|
|
|
174 |
setSelection(Arrays.asList(graphUsers));
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Sets the list of friends for pre selection. These friends will be selected by default.
|
|
|
179 |
* @param graphUsers list of friends as GraphUsers
|
|
|
180 |
*/
|
|
|
181 |
public void setSelection(List<GraphUser> graphUsers) {
|
|
|
182 |
List<String> userIds = new ArrayList<String>();
|
|
|
183 |
for(GraphUser graphUser: graphUsers) {
|
|
|
184 |
userIds.add(graphUser.getId());
|
|
|
185 |
}
|
|
|
186 |
setSelectionByIds(userIds);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Gets the currently-selected list of users.
|
|
|
191 |
* @return the currently-selected list of users
|
|
|
192 |
*/
|
|
|
193 |
public List<GraphUser> getSelection() {
|
|
|
194 |
return getSelectedGraphObjects();
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
@Override
|
|
|
198 |
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
|
|
|
199 |
super.onInflate(activity, attrs, savedInstanceState);
|
|
|
200 |
TypedArray a = activity.obtainStyledAttributes(attrs, R.styleable.com_facebook_friend_picker_fragment);
|
|
|
201 |
|
|
|
202 |
setMultiSelect(a.getBoolean(R.styleable.com_facebook_friend_picker_fragment_multi_select, multiSelect));
|
|
|
203 |
|
|
|
204 |
a.recycle();
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
public void setSettingsFromBundle(Bundle inState) {
|
|
|
208 |
super.setSettingsFromBundle(inState);
|
|
|
209 |
setFriendPickerSettingsFromBundle(inState);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
void saveSettingsToBundle(Bundle outState) {
|
|
|
213 |
super.saveSettingsToBundle(outState);
|
|
|
214 |
|
|
|
215 |
outState.putString(USER_ID_BUNDLE_KEY, userId);
|
|
|
216 |
outState.putBoolean(MULTI_SELECT_BUNDLE_KEY, multiSelect);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
@Override
|
|
|
220 |
PickerFragmentAdapter<GraphUser> createAdapter() {
|
|
|
221 |
PickerFragmentAdapter<GraphUser> adapter = new PickerFragmentAdapter<GraphUser>(
|
|
|
222 |
this.getActivity()) {
|
|
|
223 |
|
|
|
224 |
@Override
|
|
|
225 |
protected int getGraphObjectRowLayoutId(GraphUser graphObject) {
|
|
|
226 |
return R.layout.com_facebook_picker_list_row;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
@Override
|
|
|
230 |
protected int getDefaultPicture() {
|
|
|
231 |
return R.drawable.com_facebook_profile_default_icon;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
};
|
|
|
235 |
adapter.setShowCheckbox(true);
|
|
|
236 |
adapter.setShowPicture(getShowPictures());
|
|
|
237 |
adapter.setSortFields(Arrays.asList(new String[]{NAME}));
|
|
|
238 |
adapter.setGroupByField(NAME);
|
|
|
239 |
|
|
|
240 |
return adapter;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
@Override
|
|
|
244 |
LoadingStrategy createLoadingStrategy() {
|
|
|
245 |
return new ImmediateLoadingStrategy();
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
@Override
|
|
|
249 |
SelectionStrategy createSelectionStrategy() {
|
|
|
250 |
return multiSelect ? new MultiSelectionStrategy() : new SingleSelectionStrategy();
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
@Override
|
|
|
254 |
Request getRequestForLoadData(Session session) {
|
|
|
255 |
if (adapter == null) {
|
|
|
256 |
throw new FacebookException("Can't issue requests until Fragment has been created.");
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
String userToFetch = (userId != null) ? userId : "me";
|
|
|
260 |
return createRequest(userToFetch, extraFields, session);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
@Override
|
|
|
264 |
String getDefaultTitleText() {
|
|
|
265 |
return getString(R.string.com_facebook_choose_friends);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
@Override
|
|
|
269 |
void logAppEvents(boolean doneButtonClicked) {
|
|
|
270 |
AppEventsLogger logger = AppEventsLogger.newLogger(this.getActivity(), getSession());
|
|
|
271 |
Bundle parameters = new Bundle();
|
|
|
272 |
|
|
|
273 |
// If Done was clicked, we know this completed successfully. If not, we don't know (caller might have
|
|
|
274 |
// dismissed us in response to selection changing, or user might have hit back button). Either way
|
|
|
275 |
// we'll log the number of selections.
|
|
|
276 |
String outcome = doneButtonClicked ? AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_COMPLETED :
|
|
|
277 |
AnalyticsEvents.PARAMETER_DIALOG_OUTCOME_VALUE_UNKNOWN;
|
|
|
278 |
parameters.putString(AnalyticsEvents.PARAMETER_DIALOG_OUTCOME, outcome);
|
|
|
279 |
parameters.putInt("num_friends_picked", getSelection().size());
|
|
|
280 |
|
|
|
281 |
logger.logSdkEvent(AnalyticsEvents.EVENT_FRIEND_PICKER_USAGE, null, parameters);
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
@Override
|
|
|
285 |
public void loadData(boolean forceReload) {
|
|
|
286 |
super.loadData(forceReload);
|
|
|
287 |
setSelectedGraphObjects(preSelectedFriendIds);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
private Request createRequest(String userID, Set<String> extraFields, Session session) {
|
|
|
291 |
Request request = Request.newGraphPathRequest(session, userID + friendPickerType.getRequestPath(), null);
|
|
|
292 |
|
|
|
293 |
Set<String> fields = new HashSet<String>(extraFields);
|
|
|
294 |
String[] requiredFields = new String[]{
|
|
|
295 |
ID,
|
|
|
296 |
NAME
|
|
|
297 |
};
|
|
|
298 |
fields.addAll(Arrays.asList(requiredFields));
|
|
|
299 |
|
|
|
300 |
String pictureField = adapter.getPictureFieldSpecifier();
|
|
|
301 |
if (pictureField != null) {
|
|
|
302 |
fields.add(pictureField);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
Bundle parameters = request.getParameters();
|
|
|
306 |
parameters.putString("fields", TextUtils.join(",", fields));
|
|
|
307 |
request.setParameters(parameters);
|
|
|
308 |
|
|
|
309 |
return request;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
private void setFriendPickerSettingsFromBundle(Bundle inState) {
|
|
|
313 |
// We do this in a separate non-overridable method so it is safe to call from the constructor.
|
|
|
314 |
if (inState != null) {
|
|
|
315 |
if (inState.containsKey(USER_ID_BUNDLE_KEY)) {
|
|
|
316 |
setUserId(inState.getString(USER_ID_BUNDLE_KEY));
|
|
|
317 |
}
|
|
|
318 |
setMultiSelect(inState.getBoolean(MULTI_SELECT_BUNDLE_KEY, multiSelect));
|
|
|
319 |
if (inState.containsKey(FRIEND_PICKER_TYPE_KEY)) {
|
|
|
320 |
try {
|
|
|
321 |
friendPickerType = FriendPickerType.valueOf(inState.getString(FRIEND_PICKER_TYPE_KEY));
|
|
|
322 |
} catch (Exception e) {
|
|
|
323 |
// NOOP
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
private class ImmediateLoadingStrategy extends LoadingStrategy {
|
|
|
330 |
@Override
|
|
|
331 |
protected void onLoadFinished(GraphObjectPagingLoader<GraphUser> loader,
|
|
|
332 |
SimpleGraphObjectCursor<GraphUser> data) {
|
|
|
333 |
super.onLoadFinished(loader, data);
|
|
|
334 |
|
|
|
335 |
// We could be called in this state if we are clearing data or if we are being re-attached
|
|
|
336 |
// in the middle of a query.
|
|
|
337 |
if (data == null || loader.isLoading()) {
|
|
|
338 |
return;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
if (data.areMoreObjectsAvailable()) {
|
|
|
342 |
// We got results, but more are available.
|
|
|
343 |
followNextLink();
|
|
|
344 |
} else {
|
|
|
345 |
// We finished loading results.
|
|
|
346 |
hideActivityCircle();
|
|
|
347 |
|
|
|
348 |
// If this was from the cache, schedule a delayed refresh query (unless we got no results
|
|
|
349 |
// at all, in which case refresh immediately.
|
|
|
350 |
if (data.isFromCache()) {
|
|
|
351 |
loader.refreshOriginalRequest(data.getCount() == 0 ? CACHED_RESULT_REFRESH_DELAY : 0);
|
|
|
352 |
}
|
|
|
353 |
}
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
@Override
|
|
|
357 |
protected boolean canSkipRoundTripIfCached() {
|
|
|
358 |
return friendPickerType.isCacheable();
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
private void followNextLink() {
|
|
|
362 |
// This may look redundant, but this causes the circle to be alpha-dimmed if we have results.
|
|
|
363 |
displayActivityCircle();
|
|
|
364 |
|
|
|
365 |
loader.followNextLink();
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
}
|