Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- LoginFragment loginFragment = new LoginFragment();
- FacebookSdk.sdkInitialize(this.getApplicationContext());
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.add(R.id.fragment_right_container, loginFragment);
- fragmentTransaction.commit();
- SessionManager sessionManager=new SessionManager();
- Log.d("Is Logged", "" + "" + AccessToken.getCurrentAccessToken());
- /* if(sessionManager.isLoggedIn()){
- Toast.makeText(this,"Logueado!",Toast.LENGTH_LONG).show();
- }*/
- }
- }
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".LoginFragment">
- <!-- TODO: Update blank fragment layout -->
- <TextView
- android:id="@+id/text_details"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text=""
- />
- <com.facebook.login.widget.LoginButton
- android:id="@+id/login_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="30dp"
- android:layout_marginBottom="30dp" />
- </FrameLayout>
- public class LoginFragment extends Fragment {
- private TextView textDetails;
- private CallbackManager callbackManager;
- private AccessTokenTracker tokenTracker;
- private ProfileTracker profileTracker;
- FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
- @Override
- public void onSuccess(LoginResult loginResult) {
- Log.d("Facebook-Callback", "onSuccess");
- AccessToken accessToken = loginResult.getAccessToken();
- Profile profile = Profile.getCurrentProfile();
- displayWelcomeMessage(profile);
- }
- @Override
- public void onCancel() {
- Log.d("Facebook-Cancel", "onCancel");
- }
- @Override
- public void onError(FacebookException error) {
- Log.d("Facebook-Error", "onCancel");
- }
- };
- public LoginFragment() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- callbackManager=CallbackManager.Factory.create();
- setupTokenTracker();
- setupProfileTracker();
- tokenTracker.startTracking();
- profileTracker.startTracking();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment_main, container, false);
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- setupTextDetails(view);
- setupLoginButton(view);
- }
- @Override
- public void onResume() {
- super.onResume();
- Profile profile=Profile.getCurrentProfile();
- displayWelcomeMessage(profile);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- callbackManager.onActivityResult(requestCode, resultCode, data);
- }
- @Override
- public void onStop() {
- super.onStop();
- tokenTracker.stopTracking();
- profileTracker.stopTracking();
- }
- //funciones de usuario
- private void displayWelcomeMessage(Profile profile){
- if(SessionManager.isLoggedIn()){
- textDetails.setText("Logged "+profile.getName());
- }else{
- textDetails.setText("Logged out");
- }
- }
- private void setupTextDetails(View view) {
- textDetails = (TextView) view.findViewById(R.id.text_details);
- }
- private void setupLoginButton(View view) {
- LoginButton buttonLogin = (LoginButton) view.findViewById(R.id.login_button);
- buttonLogin.setFragment(this);
- //I set up the permissions I want to ask for
- buttonLogin.setReadPermissions("user_friends");
- buttonLogin.registerCallback(callbackManager, callback);
- }
- //it's the callback function that listens everytime the profile of the user changes(change profile pic or whatever)
- private void setupProfileTracker() {
- profileTracker= new ProfileTracker() {
- @Override
- protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
- Log.d("New Profile ", "" + newProfile);
- displayWelcomeMessage(newProfile);
- }
- };
- }
- //it's the callback function that listens everytime the token changes(When I lost connection I receive another token for example and the nthis function executes)
- private void setupTokenTracker() {
- tokenTracker= new AccessTokenTracker() {
- @Override
- protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
- //log in the console the new token
- Log.d("New token for User ", "" + newToken);
- }
- };
- }
- }
- public class SessionManager {
- //check if it'slogged in to facebook
- String currentAccessToken=null;
- public void setAccessToken(String newToken){
- currentAccessToken=newToken;
- }
- public static boolean isLoggedIn() {
- AccessToken accessToken = AccessToken.getCurrentAccessToken();
- //Log.d("access token facebook",""+accessToken);
- return accessToken != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement