Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package co.ortizol.fragments;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import com.facebook.AccessToken;
- import com.facebook.CallbackManager;
- import com.facebook.FacebookCallback;
- import com.facebook.FacebookException;
- import com.facebook.GraphRequest;
- import com.facebook.GraphResponse;
- import com.facebook.Profile;
- import com.facebook.ProfileTracker;
- import com.facebook.login.LoginResult;
- import com.facebook.login.widget.LoginButton;
- import com.google.android.gms.auth.api.Auth;
- import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
- import com.google.android.gms.auth.api.signin.GoogleSignInResult;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.ResultCallback;
- import com.google.android.gms.common.api.Status;
- import org.json.JSONException;
- import org.json.JSONObject;
- import co.ortizol.R;
- /**
- * Representa la lógica de la interfaz para el inicio de sesión.
- */
- public class InicioSesionFragment extends Fragment implements FacebookCallback<LoginResult>,
- View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
- public static final String TAG = InicioSesionFragment.class.getSimpleName();
- private CallbackManager callbackManager;
- private ProfileTracker profileTracker;
- public static final int SIGN_IN_GOOGLE_REQUEST_CODE = 1;
- private GoogleApiClient mGoogleApiClient;
- private int tipoLogin = 1;
- public int getTipoLogin() {
- return tipoLogin;
- }
- public InicioSesionFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment_inicio_sesion, container, false);
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- callbackManager = CallbackManager.Factory.create();
- LoginButton loginButton = (LoginButton) view.findViewById(R.id.lbnFacebook);
- loginButton.setReadPermissions("public_profile", "email");
- loginButton.registerCallback(callbackManager, this);
- profileTracker = new ProfileTracker() {
- @Override
- protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
- if (currentProfile != null) {
- GraphRequest request = GraphRequest.newMeRequest(
- AccessToken.getCurrentAccessToken(),
- new GraphRequest.GraphJSONObjectCallback() {
- @Override
- public void onCompleted(JSONObject object, GraphResponse response) {
- try {
- String email = object.getString("email");
- InicioFragment frgInicio = InicioFragment.getInstance(email);
- getActivity().getSupportFragmentManager().beginTransaction()
- .replace(R.id.layContenedor, frgInicio)
- .addToBackStack(null)
- .commit();
- } catch (JSONException e) {
- e.printStackTrace();
- Log.e(TAG, "onCompleted: Error", e);
- }
- }
- });
- Bundle parameters = new Bundle();
- parameters.putString("fields", "id,name,link,email");
- request.setParameters(parameters);
- request.executeAsync();
- }
- }
- };
- profileTracker.startTracking();
- view.findViewById(R.id.sibIniciarSesion).setOnClickListener(this);
- if(mGoogleApiClient == null) {
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestEmail()
- .build();
- mGoogleApiClient = new GoogleApiClient.Builder(getContext())
- .enableAutoManage(getActivity(), this)
- .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
- .build();
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- callbackManager.onActivityResult(requestCode, resultCode, data);
- }
- @Override
- public void onSuccess(LoginResult loginResult) {
- Log.i(TAG, "onSuccess: " + loginResult.getAccessToken());
- }
- @Override
- public void onCancel() {
- Log.i(TAG, "onCancel: Se ha cancelado la operación de inicio de sesión");
- }
- @Override
- public void onError(FacebookException error) {
- Log.i(TAG, "onError: " + error.getMessage());
- }
- @Override
- public void onClick(View v) {
- signIn();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- }
- private void signIn() {
- Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
- startActivityForResult(signInIntent, SIGN_IN_GOOGLE_REQUEST_CODE);
- }
- public void handleSignInResult(GoogleSignInResult result) {
- Log.d(TAG, "handleSignInResult: " + result.isSuccess());
- if (result.isSuccess()) {
- GoogleSignInAccount account = result.getSignInAccount();
- InicioFragment frgInicio = InicioFragment.getInstance(account.getEmail());
- getActivity().getSupportFragmentManager().beginTransaction()
- .replace(R.id.layContenedor, frgInicio)
- .addToBackStack(null)
- .commit();
- }
- }
- private void signOut() {
- Auth.GoogleSignInApi.signOut(mGoogleApiClient)
- .setResultCallback(new ResultCallback<Status>() {
- @Override
- public void onResult(@NonNull Status status) {
- Log.i(TAG, "onResult: Disconnect with Google");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement