Skip to content

Commit

Permalink
Reformat login code
Browse files Browse the repository at this point in the history
  • Loading branch information
zeevox committed Dec 16, 2017
1 parent 4ce41fc commit 1ab5398
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -48,12 +47,11 @@

public class RegisterActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener{
View.OnClickListener {

RequestQueue rq;
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;

RequestQueue rq;
private SignInButton mSignInButton;

private GoogleApiClient mGoogleApiClient;
Expand Down Expand Up @@ -109,8 +107,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Google Sign In failed
Log.e(TAG, "Google Sign In failed.");
}
}
else{
} else {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Expand All @@ -132,11 +129,11 @@ public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "Authentication Done."+task.getResult().getUser().getDisplayName()+" - "+task.getResult().getUser().getDisplayName()+" - "+task.getResult().getUser().getEmail()+" - "+task.getResult().getUser().getPhotoUrl().getPath()+" - ",
Toast.makeText(RegisterActivity.this, "Authentication Done." + task.getResult().getUser().getDisplayName() + " - " + task.getResult().getUser().getDisplayName() + " - " + task.getResult().getUser().getEmail() + " - " + task.getResult().getUser().getPhotoUrl().getPath() + " - ",
Toast.LENGTH_SHORT).show();
getImage(task.getResult().getUser().getPhotoUrl().toString());

Intent intentdone=new Intent(RegisterActivity.this, SplashActivity.class);
Intent intentdone = new Intent(RegisterActivity.this, SplashActivity.class);
startActivity(intentdone);

}
Expand All @@ -153,7 +150,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.testreg);

// Assign fields
mSignInButton = (SignInButton) findViewById(R.id.login_with_google);
mSignInButton = findViewById(R.id.login_with_google);

// Set click listeners
mSignInButton.setOnClickListener(this);
Expand Down Expand Up @@ -186,7 +183,7 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
getImage(user.getPhotoUrl().toString());


Intent intentdone=new Intent(RegisterActivity.this, SplashActivity.class);
Intent intentdone = new Intent(RegisterActivity.this, SplashActivity.class);
startActivity(intentdone);
finish();

Expand All @@ -205,85 +202,81 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
};

mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions("email", "public_profile","user_friends");
LoginButton loginButton = findViewById(R.id.connectWithFbButton);
loginButton.setReadPermissions("email", "public_profile", "user_friends");

loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
private ProfileTracker mProfileTracker;

@Override
public void onSuccess(LoginResult loginResult) {
try {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
//Saving Facebook Credentials of the user
if(Profile.getCurrentProfile() == null) {
if (Profile.getCurrentProfile() == null) {
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
// profile2 is the new profile
Log.d("check",profile2.getCurrentProfile().toString());
Log.d("check", Profile.getCurrentProfile().toString());
saveFacebookProfile(profile2);
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
// no need to call startTracking() on mProfileTracker
// because it is called by its constructor, internally.
}
else {
Log.d("check",Profile.getCurrentProfile().toString());
} else {
Log.d("check", Profile.getCurrentProfile().toString());
saveFacebookProfile(Profile.getCurrentProfile());
}

Log.d(TAG,"Facebook Login:Saved Credentials to Shared Prefs");
Log.d(TAG, "Facebook Login:Saved Credentials to Shared Prefs");
//Auth with Firebase
handleFacebookAccessToken(loginResult.getAccessToken());

}
catch (Exception e){
} catch (Exception e) {
e.printStackTrace();

}
}

@Override
public void onCancel() {
try{
Log.d(TAG, "facebook:onCancel");
// [START_EXCLUDE]
Toast.makeText(getApplicationContext(), "Facebook Cancel", Toast.LENGTH_LONG).show();
//updateUI(null);
// [END_EXCLUDE]
}
catch (Exception e){
try {
Log.d(TAG, "facebook:onCancel");
// [START_EXCLUDE]
Toast.makeText(getApplicationContext(), "Facebook Cancel", Toast.LENGTH_LONG).show();
//updateUI(null);
// [END_EXCLUDE]
} catch (Exception e) {
e.printStackTrace();

}
}

@Override
public void onError(FacebookException error) {
try{
try {

Log.d(TAG, "facebook:onError", error);
// [START_EXCLUDE]
Toast.makeText(getApplicationContext(), "Facebook Error "+error.getMessage(), Toast.LENGTH_LONG).show();
//updateUI(null);
// [END_EXCLUDE]
}
catch (Exception e){
Log.d(TAG, "facebook:onError", error);
// [START_EXCLUDE]
Toast.makeText(getApplicationContext(), "Facebook Error " + error.getMessage(), Toast.LENGTH_LONG).show();
//updateUI(null);
// [END_EXCLUDE]
} catch (Exception e) {
e.printStackTrace();

}
}
});

}
catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}

rq= Volley.newRequestQueue(this);
rq = Volley.newRequestQueue(this);
}

@Override
Expand All @@ -305,7 +298,7 @@ protected void onStart() {
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
// [START_EXCLUDE silent]
Toast.makeText(getApplicationContext(),"Facebook show dialog",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Facebook show dialog", Toast.LENGTH_LONG).show();
//showProgressDialog();
// [END_EXCLUDE]

Expand All @@ -314,24 +307,23 @@ private void handleFacebookAccessToken(AccessToken token) {
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try{
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// [START_EXCLUDE]
Toast.makeText(getApplicationContext(),"Facebook hide dialog",Toast.LENGTH_LONG).show();
//hideProgressDialog();
// [END_EXCLUDE]
}
catch (Exception e){
try {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// [START_EXCLUDE]
Toast.makeText(getApplicationContext(), "Facebook hide dialog", Toast.LENGTH_LONG).show();
//hideProgressDialog();
// [END_EXCLUDE]
} catch (Exception e) {
e.printStackTrace();

}
Expand All @@ -345,33 +337,33 @@ public void onComplete(@NonNull Task<AuthResult> task) {
public void signOut() {
mAuth.signOut();
LoginManager.getInstance().logOut();
Toast.makeText(getApplicationContext(),"You have been Signout from the Kute",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "You have been Signout from the Kute", Toast.LENGTH_LONG).show();
//updateUI(null);
}
public void saveFacebookProfile(Profile pf)
{
SharedPreferences pref=getApplicationContext().getSharedPreferences("user_credentials",0);
SharedPreferences.Editor editor=pref.edit();
editor.putString("Login_Method","Facebook");

public void saveFacebookProfile(Profile pf) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("user_credentials", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("Login_Method", "Facebook");
editor.putString("Name", pf.getName());
editor.putString("Id",pf.getId());
editor.putString("Profile_Image","null");
editor.putBoolean("Register_db",true);
editor.putBoolean("Sync_Friends_db",true);
editor.putString("Id", pf.getId());
editor.putString("Profile_Image", "null");
editor.putBoolean("Register_db", true);
editor.putBoolean("Sync_Friends_db", true);
editor.apply();
}

public void getImage(String url){
public void getImage(String url) {
ImageRequest ir = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
ImageHandler.saveImageToprefrence(getSharedPreferences(ImageHandler.MainKey,MODE_PRIVATE),response);
ImageView iv=(ImageView)findViewById(R.id.imageView);
iv.setImageBitmap(response);
}
}, 0, 0, null, null);
rq.add(ir);
@Override
public void onResponse(Bitmap response) {
ImageHandler.saveImageToprefrence(getSharedPreferences(ImageHandler.MainKey, MODE_PRIVATE), response);
ImageView iv = findViewById(R.id.imageView);
iv.setImageBitmap(response);
}
}, 0, 0, null, null);
rq.add(ir);

}
}

}
18 changes: 8 additions & 10 deletions kute-android-app/app/src/main/res/layout/testreg.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Activity.RegisterActivity">

<TextView
android:id="@+id/login_status"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAlignment="center"
android:layout_marginTop="20dp" />

android:visibility="gone" />


<com.google.android.gms.common.SignInButton
android:id="@+id/login_with_google"
android:text="login_with_google"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:text="login_with_google" />

<com.facebook.login.widget.LoginButton
android:id="@+id/connectWithFbButton"
Expand All @@ -36,9 +34,9 @@
android:text=" connect_with_facebook" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
android:layout_height="wrap_content" />


</LinearLayout>

0 comments on commit 1ab5398

Please sign in to comment.