Created
August 24, 2017 08:35
-
-
Save nabeelnazir163/241395d89919512722cc2b51d5f1bcee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.nabeel.ask.ui.activities; | |
import com.example.nabeel.ask.R; | |
import com.example.nabeel.ask.loginas; | |
import com.example.nabeel.ask.models.User; | |
import com.example.nabeel.ask.signup; | |
import com.example.nabeel.ask.utils.BaseActivity; | |
import com.example.nabeel.ask.utils.FirebaseUtils; | |
import com.facebook.AccessToken; | |
import com.facebook.AccessTokenTracker; | |
import com.facebook.CallbackManager; | |
import com.facebook.FacebookCallback; | |
import com.facebook.FacebookException; | |
import com.facebook.FacebookSdk; | |
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.GoogleSignInResult; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.SignInButton; | |
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 com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.auth.AuthCredential; | |
import com.google.firebase.auth.AuthResult; | |
import com.google.firebase.auth.GoogleAuthProvider; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class signin extends BaseActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { | |
//protected FirebaseUser mFirebaseUser; | |
// private LinearLayout Prof_Section; | |
private LoginButton loginButton; | |
private CallbackManager callbackManager; | |
private AccessTokenTracker accessTokenTracker; | |
private ProfileTracker profileTracker; | |
// private Button SignOut; | |
//protected FirebaseAuth mAuth; | |
// private TextView Name , Email; | |
private SignInButton SignIn; | |
private static final int REQ_CODE = 9001; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
FacebookSdk.sdkInitialize(getApplicationContext()); | |
setContentView(R.layout.activity_signin); | |
// Prof_Section = (LinearLayout) findViewById(R.id.prof_section); | |
// SignOut = (Button) findViewById(R.id.bn_logout); | |
// Name = (TextView) findViewById(R.id.name); | |
// Email = (TextView) findViewById(R.id.email); | |
SignIn = (SignInButton) findViewById(R.id.bn_login); | |
SignIn.setSize(SignInButton.SIZE_ICON_ONLY); | |
SignIn.setOnClickListener(this); | |
// SignOut.setOnClickListener(this); | |
// Prof_Section.setVisibility(View.GONE); | |
loginButton = (LoginButton) findViewById(R.id.login_button); | |
callbackManager = CallbackManager.Factory.create(); | |
accessTokenTracker = new AccessTokenTracker() { | |
@Override | |
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken currentToken) { | |
} | |
}; | |
profileTracker = new ProfileTracker() { | |
@Override | |
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) { | |
// nextActivity(newProfile); | |
} | |
}; | |
accessTokenTracker.startTracking(); | |
profileTracker.startTracking(); | |
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() { | |
@Override | |
public void onSuccess(LoginResult loginResult) { | |
Profile profile1 = Profile.getCurrentProfile(); | |
// nextActivity(profile1); | |
Toast.makeText(getApplicationContext() , " Logging in " , Toast.LENGTH_SHORT).show(); | |
/* i = new Intent(signin.this, MainActivity.class); | |
startActivity(i); */ | |
} | |
@Override | |
public void onCancel() { | |
} | |
@Override | |
public void onError(FacebookException error) { | |
} | |
}; | |
loginButton.setReadPermissions("user_friends"); | |
loginButton.registerCallback(callbackManager , callback); | |
TextView tv_su = (TextView) findViewById(R.id.signup); | |
tv_su.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent i = new Intent(signin.this, signup.class); | |
startActivity(i); | |
} | |
}); | |
TextView tv_si = (TextView) findViewById(R.id.login); | |
tv_si.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent si = new Intent(signin.this, loginas.class); | |
startActivity(si); | |
} | |
}); | |
} | |
@Override | |
protected void onResume(){ | |
super.onResume(); | |
Profile profile1 = Profile.getCurrentProfile(); | |
// nextActivity(profile1); | |
} | |
@Override | |
protected void onPause(){ | |
super.onPause(); | |
} | |
protected void onStop(){ | |
super.onStop(); | |
accessTokenTracker.stopTracking(); | |
profileTracker.startTracking(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (resultCode == Activity.RESULT_OK) { | |
if (requestCode == REQ_CODE) { | |
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); | |
handleresult(result); | |
} else { | |
callbackManager.onActivityResult(requestCode, resultCode, data); | |
} | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()){ | |
case R.id.bn_login: | |
signIn(); | |
break; | |
} | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
} | |
private void signIn(){ | |
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); | |
startActivityForResult(intent,REQ_CODE); | |
} | |
private void signOut(){ | |
mAuth.signOut(); | |
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() { | |
@Override | |
public void onResult(@NonNull Status status) { | |
//updateUI(false); | |
} | |
}); | |
} | |
private void handleresult(GoogleSignInResult result){ | |
if(result.isSuccess()){ | |
GoogleSignInAccount account = result.getSignInAccount(); | |
firebaseAuthWithGoogle(account); | |
} | |
} | |
private void firebaseAuthWithGoogle(final GoogleSignInAccount account) { | |
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); | |
mAuth.signInWithCredential(credential) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
//Let's create the models quick(I know! it's weird time in this tutorial | |
//To be building it | |
User user = new User(); | |
String photoUrl = null; | |
if (account.getPhotoUrl() != null) { | |
user.setPhotoUrl(account.getPhotoUrl().toString()); | |
} | |
user.setEmail(account.getEmail()); | |
user.setUser(account.getDisplayName()); | |
user.setUid(mAuth.getCurrentUser().getUid()); | |
FirebaseUtils.getUserRef(account.getEmail().replace(".", ",")) | |
.setValue(user, new DatabaseReference.CompletionListener() { | |
@Override | |
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { | |
mFirebaseUser = mAuth.getCurrentUser(); | |
Intent sendData = new Intent(signin.this , navigation.class); | |
startActivity(sendData); | |
//finish(); | |
} | |
}); | |
} else { | |
// dismissProgressDialog(); | |
} | |
} | |
}); | |
} | |
private void updateUI(boolean isLogin){ | |
if(isLogin){ | |
// Prof_Section.setVisibility(View.VISIBLE); | |
SignIn.setVisibility(View.GONE); | |
} | |
else{ | |
// Prof_Section.setVisibility(View.GONE); | |
SignIn.setVisibility(View.VISIBLE); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.nabeel.ask; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Window; | |
import com.example.nabeel.ask.ui.activities.navigation; | |
import com.example.nabeel.ask.ui.activities.signin; | |
public class splash extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
setContentView(R.layout.activity_main); | |
final int welcomeScreenDisplay = 2000; | |
/** create a thread to show splash up to splash time */ | |
Thread welcomeThread = new Thread() { | |
int wait = 0; | |
@Override | |
public void run() { | |
try { | |
sleep(1000); | |
Intent i = new Intent(getApplicationContext(),navigation.class); | |
startActivity(i); | |
finish(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
welcomeThread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment