Java tutorial
/** * Copyright 2010-present Facebook. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.coffeecups.testproject; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.facebook.SessionState; import com.facebook.UiLifecycleHelper; import com.facebook.model.GraphUser; import com.facebook.Session; import java.util.ArrayList; import java.util.Collection; public class FriendPickerSampleActivity extends FragmentActivity { private static final int PICK_FRIENDS_ACTIVITY = 1; private Button pickFriendsButton; private TextView resultsTextView; private UiLifecycleHelper lifecycleHelper; boolean pickFriendsWhenSessionOpened; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); resultsTextView = (TextView) findViewById(R.id.resultsTextView); pickFriendsButton = (Button) findViewById(R.id.pickFriendsButton); pickFriendsButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onClickPickFriends(); } }); lifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { onSessionStateChanged(session, state, exception); } }); lifecycleHelper.onCreate(savedInstanceState); ensureOpenSession(); } @Override protected void onStart() { super.onStart(); // Update the display every time we are started. displaySelectedFriends(RESULT_OK); } public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PICK_FRIENDS_ACTIVITY: displaySelectedFriends(resultCode); break; default: Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); break; } } private boolean ensureOpenSession() { if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) { Session.openActiveSession(this, true, new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { onSessionStateChanged(session, state, exception); } }); return false; } return true; } private void onSessionStateChanged(Session session, SessionState state, Exception exception) { if (pickFriendsWhenSessionOpened && state.isOpened()) { pickFriendsWhenSessionOpened = false; startPickFriendsActivity(); } } private void displaySelectedFriends(int resultCode) { String results = ""; FriendPickerApplication application = (FriendPickerApplication) getApplication(); Collection<GraphUser> selection = application.getSelectedUsers(); 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 selected>"; } resultsTextView.setText(results); } private void onClickPickFriends() { startPickFriendsActivity(); } private void startPickFriendsActivity() { if (ensureOpenSession()) { FriendPickerApplication application = (FriendPickerApplication) getApplication(); application.setSelectedUsers(null); Intent intent = new Intent(this, PickFriendsActivity.class); // Note: The following line is optional, as multi-select behavior is the default for // FriendPickerFragment. It is here to demonstrate how parameters could be passed to the // friend picker if single-select functionality was desired, or if a different user ID was // desired (for instance, to see friends of a friend). PickFriendsActivity.populateParameters(intent, null, true, true); startActivityForResult(intent, PICK_FRIENDS_ACTIVITY); } else { pickFriendsWhenSessionOpened = true; } } }