private Request createRequest( Location location, int radiusInMeters, int resultsLimit, String searchText, Set<String> extraFields, Session session) { Request request = Request.newPlacesSearchRequest( session, location, radiusInMeters, resultsLimit, searchText, null); Set<String> fields = new HashSet<String>(extraFields); String[] requiredFields = new String[] {ID, NAME, LOCATION, CATEGORY, WERE_HERE_COUNT}; fields.addAll(Arrays.asList(requiredFields)); String pictureField = adapter.getPictureFieldSpecifier(); if (pictureField != null) { fields.add(pictureField); } Bundle parameters = request.getParameters(); parameters.putString("fields", TextUtils.join(",", fields)); request.setParameters(parameters); return request; }
public class NewLogin extends FragmentActivity { private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); private static final Location SEATTLE_LOCATION = new Location("") { { setLatitude(47.6097); setLongitude(-122.3331); } }; private final String PENDING_ACTION_BUNDLE_KEY = "com.facebook.samples.hellofacebook:PendingAction"; private Button postStatusUpdateButton; private Button postPhotoButton; private Button pickFriendsButton; private Button pickPlaceButton; private LoginButton loginButton; private ProfilePictureView profilePictureView; private TextView greeting; private PendingAction pendingAction = PendingAction.NONE; private ViewGroup controlsContainer; private GraphUser user; private enum PendingAction { NONE, POST_PHOTO, POST_STATUS_UPDATE } private UiLifecycleHelper uiHelper; private Session.StatusCallback callback = new Session.StatusCallback() { public void call(Session session, SessionState state, Exception exception) { onSessionStateChange(session, state, exception); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); uiHelper = new UiLifecycleHelper(this, callback); uiHelper.onCreate(savedInstanceState); if (savedInstanceState != null) { String name = savedInstanceState.getString(PENDING_ACTION_BUNDLE_KEY); pendingAction = PendingAction.valueOf(name); } setContentView(R.layout.new_login); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setUserInfoChangedCallback( new LoginButton.UserInfoChangedCallback() { public void onUserInfoFetched(GraphUser user) { NewLogin.this.user = user; updateUI(); // It's possible that we were waiting for this.user to be populated in order to post a // status update. handlePendingAction(); } }); profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture); greeting = (TextView) findViewById(R.id.greeting); postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton); postStatusUpdateButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { onClickPostStatusUpdate(); } }); postPhotoButton = (Button) findViewById(R.id.postPhotoButton); postPhotoButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { onClickPostPhoto(); } }); pickFriendsButton = (Button) findViewById(R.id.pickFriendsButton); pickFriendsButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { onClickPickFriends(); } }); pickPlaceButton = (Button) findViewById(R.id.pickPlaceButton); pickPlaceButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { onClickPickPlace(); } }); controlsContainer = (ViewGroup) findViewById(R.id.main_ui_container); final FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment != null) { // If we're being re-created and have a fragment, we need to a) hide the main UI controls and // b) hook up its listeners again. controlsContainer.setVisibility(View.GONE); } // Listen for changes in the back stack so we know if a fragment got popped off because the user // clicked the back button. fm.addOnBackStackChangedListener( new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { if (fm.getBackStackEntryCount() == 0) { // We need to re-show our UI. controlsContainer.setVisibility(View.VISIBLE); } } }); } @Override protected void onResume() { super.onResume(); uiHelper.onResume(); updateUI(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); outState.putString(PENDING_ACTION_BUNDLE_KEY, pendingAction.name()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); uiHelper.onActivityResult(requestCode, resultCode, data); } @Override public void onPause() { super.onPause(); uiHelper.onPause(); } @Override public void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } private void onSessionStateChange(Session session, SessionState state, Exception exception) { if (pendingAction != PendingAction.NONE && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) { new AlertDialog.Builder(NewLogin.this) .setTitle("Cancelled") .setMessage("Permission not granted") .setPositiveButton("ok", null) .show(); pendingAction = PendingAction.NONE; } else if (state == SessionState.OPENED_TOKEN_UPDATED) { handlePendingAction(); } updateUI(); } private void updateUI() { Session session = Session.getActiveSession(); boolean enableButtons = (session != null && session.isOpened()); postStatusUpdateButton.setEnabled(enableButtons); postPhotoButton.setEnabled(enableButtons); pickFriendsButton.setEnabled(enableButtons); pickPlaceButton.setEnabled(enableButtons); if (enableButtons && user != null) { profilePictureView.setProfileId(user.getId()); greeting.setText("Greetings " + user.getFirstName()); } else { profilePictureView.setProfileId(null); greeting.setText(null); } } @SuppressWarnings("incomplete-switch") private void handlePendingAction() { // These actions may re-set pendingAction if they are still pending, but we assume they // will succeed. pendingAction = PendingAction.NONE; } private void onClickPostStatusUpdate() { performPublish(PendingAction.POST_STATUS_UPDATE); } private void onClickPostPhoto() { performPublish(PendingAction.POST_PHOTO); } private void showPickerFragment(PickerFragment<?> fragment) { fragment.setOnErrorListener( new PickerFragment.OnErrorListener() { public void onError(PickerFragment<?> pickerFragment, FacebookException error) { showAlert("error", error.getMessage()); } }); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit(); controlsContainer.setVisibility(View.GONE); // We want the fragment fully created so we can use it immediately. fm.executePendingTransactions(); fragment.loadData(false); } private void onClickPickFriends() { final FriendPickerFragment fragment = new FriendPickerFragment(); setFriendPickerListeners(fragment); showPickerFragment(fragment); } private void setFriendPickerListeners(final FriendPickerFragment fragment) { fragment.setOnDoneButtonClickedListener( new FriendPickerFragment.OnDoneButtonClickedListener() { public void onDoneButtonClicked(PickerFragment<?> pickerFragment) { onFriendPickerDone(fragment); } }); } private void onFriendPickerDone(FriendPickerFragment fragment) { FragmentManager fm = getSupportFragmentManager(); fm.popBackStack(); String results = ""; Collection<GraphUser> selection = fragment.getSelection(); if (selection != null && selection.size() > 0) { ArrayList<String> names = new ArrayList<String>(); for (GraphUser user : selection) { names.add(user.getName()); } results = TextUtils.join(", ", names); } else { results = "no friends"; } showAlert("rar", results); } private void onPlacePickerDone(PlacePickerFragment fragment) { FragmentManager fm = getSupportFragmentManager(); fm.popBackStack(); String result = ""; GraphPlace selection = fragment.getSelection(); if (selection != null) { result = selection.getName(); } else { result = "no place"; } showAlert("picked", result); } private void onClickPickPlace() { final PlacePickerFragment fragment = new PlacePickerFragment(); fragment.setLocation(SEATTLE_LOCATION); fragment.setTitleText("place"); setPlacePickerListeners(fragment); showPickerFragment(fragment); } private void setPlacePickerListeners(final PlacePickerFragment fragment) { fragment.setOnDoneButtonClickedListener( new PlacePickerFragment.OnDoneButtonClickedListener() { public void onDoneButtonClicked(PickerFragment<?> pickerFragment) { onPlacePickerDone(fragment); } }); fragment.setOnSelectionChangedListener( new PlacePickerFragment.OnSelectionChangedListener() { public void onSelectionChanged(PickerFragment<?> pickerFragment) { if (fragment.getSelection() != null) { onPlacePickerDone(fragment); } } }); } private void showAlert(String title, String message) { new AlertDialog.Builder(this) .setTitle(title) .setMessage(message) .setPositiveButton("ok", null) .show(); } private boolean hasPublishPermission() { Session session = Session.getActiveSession(); return session != null && session.getPermissions().contains("publish_actions"); } private void performPublish(PendingAction action) { Session session = Session.getActiveSession(); if (session != null) { pendingAction = action; if (hasPublishPermission()) { // We can do the action right away. handlePendingAction(); } else { // We need to get new permissions, then complete the action when we get called back. session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS)); } } } }
public static <T> Collection<T> unmodifiableCollection(T... ts) { return Collections.unmodifiableCollection(Arrays.asList(ts)); }