xml file
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/layout_margin_20dp" android:layout_marginBottom="@dimen/layout_margin_30dp" android:orientation="horizontal" android:weightSum="2">
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1">
<com.facebook.login.widget.LoginButton android:id="@+id/btn_facebook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
<ImageView android:id="@+id/iv_facebook" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginRight="@dimen/layout_margin_10dp" android:scaleType="fitEnd" android:src="@drawable/ic_facebook" android:visibility="visible" />
</FrameLayout>
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1">
<com.google.android.gms.common.SignInButton android:id="@+id/btn_google_sign_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
<ImageView android:id="@+id/iv_google" android:layout_width="fill_parent" android:layout_height="55dp" android:scaleType="fitStart" android:layout_marginLeft="@dimen/layout_margin_10dp" android:src="@drawable/ic_google" android:visibility="visible" />
</FrameLayout>
</LinearLayout>
public class LoginActivity extends FragmentActivity implements View.OnClickListener,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "[LoginActivity] : ";
private Context context = LoginActivity.this;
private LayoutInflater inflater;
private LinearLayout ll_registration;
private TextView tv_forgot_password;
private Button sign_in_button;
private ImageView iv_google, iv_facebook;
private Intent intent;
private GoogleSignInClient mGoogleSignInClient;
private GoogleApiClient mGoogleApiClient;
private CircularProgressViewDialog circularProgressViewDialog;
private int RC_SIGN_IN = 100;
private String googleId, googleToken, email, fullName;
private EditText et_email_mobile, et_password;
private LoginButton btn_facebook;
private CallbackManager callbackManager;
private String fileNameWithPath;
private SignInButton btn_google_sign_in;
private boolean isLoginDone = false;
private GoogleSignInOptions gso;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity_layout);
Log.i(TAG, "onCreate(), called for: ");
try {
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
// AppEventsLogger.activateApp(this);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
/* * code for hiding keyboard for focus on ic_username */ getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
isLoginDone = (Boolean) ApplicationSession.getFromPreference(Constants.Preferences.PREF_LOGIN_DONE,
ApplicationSession.FieldType.BOOLEAN);
Log.i(TAG, "onCreate(), isLoginDone : " + isLoginDone);
if(isLoginDone){
Intent intent = new Intent(context, MainNavigationBottomActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
tv_forgot_password = (TextView) findViewById(R.id.tv_forgot_password);
ll_registration = (LinearLayout) findViewById(R.id.ll_registration);
sign_in_button = (Button) findViewById(R.id.sign_in_button);
iv_google = (ImageView) findViewById(R.id.iv_google);
iv_facebook = (ImageView) findViewById(R.id.iv_facebook);
et_email_mobile = (EditText) findViewById(R.id.et_email_mobile);
et_email_mobile.setOnEditorActionListener(editDoneLogin);
et_password = (EditText) findViewById(R.id.et_password);
et_password.setOnEditorActionListener(editDoneLogin);
btn_facebook = (LoginButton) findViewById(R.id.btn_facebook);
btn_google_sign_in = (SignInButton) findViewById(R.id.btn_google_sign_in);
sign_in_button.setOnClickListener(this);
ll_registration.setOnClickListener(this);
tv_forgot_password.setOnClickListener(this);
iv_facebook.setOnClickListener(this);
iv_google.setOnClickListener(this);
//Initializing google signin option initializeForGoogleSignIn();
//Initializing Facebook signin option initializeForFacebookSignIn();
} catch (Exception e) {
e.printStackTrace();
}
}
TextView.OnEditorActionListener editDoneLogin = new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(TAG, "onEditorAction(), actionId : " + actionId);
if (actionId == EditorInfo.IME_ACTION_DONE ||
(event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
login();
return false;
}
return false;
}
};
@Override public void onClick(View v) {
Log.i(TAG, "onClick() called : ");
try {
switch (v.getId()) {
case R.id.iv_google:
btn_google_sign_in.performClick();
googleSignIn();
break;
case R.id.iv_facebook:
btn_facebook.performClick();
break;
case R.id.sign_in_button:
//login(); ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_TYPE, Constants.LoginType.EMAIL);
Intent intent = new Intent(context, MainNavigationBottomActivity.class);
startActivity(intent);
LoginActivity.this.finish();
break;
case R.id.tv_forgot_password:
intent = new Intent(LoginActivity.this, ForgotPasswordActivity.class);
startActivity(intent);
break;
case R.id.ll_registration:
intent = new Intent(LoginActivity.this, RegistrationActivity.class);
startActivity(intent);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void initializeForGoogleSignIn() {
Log.i(TAG, "initializeForGoogleSignIn() called ");
try {
/* GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(URL.WEB_CLIENT_ID) .requestServerAuthCode(URL.WEB_CLIENT_ID, false) .requestEmail() .build();*/
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
btn_google_sign_in.setSize(SignInButton.SIZE_STANDARD);
btn_google_sign_in.setScopes(gso.getScopeArray());
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(LoginActivity.this, LoginActivity.this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
btn_google_sign_in.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
googleSignIn();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void googleSignIn() {
Log.i(TAG, "googleSignIn(), called ");
try {
if (circularProgressViewDialog == null) {
circularProgressViewDialog = new CircularProgressViewDialog(context);
}
circularProgressViewDialog.getWindow().setBackgroundDrawable(new
ColorDrawable(android.graphics.Color.TRANSPARENT));
circularProgressViewDialog.showDialog();
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
} catch (Exception e) {
e.printStackTrace();
}
}
private void handleSignInResultForGoogleLogin(GoogleSignInResult result) {
Log.i(TAG, "handleSignInResultForGoogleLogin(GoogleSignInResult) called for GoogleSignInResult : " + result);
//If the login succeed try {
if (result != null && result.isSuccess()) {
//Getting google account GoogleSignInAccount account = result.getSignInAccount();
Log.i(TAG, "handleSignInResultForGoogleLogin(), account : " + account);
googleId = account.getId().trim();
googleToken = account.getIdToken();
fullName = account.getDisplayName();
email = account.getEmail();
Uri googleFileNameWithPath = account.getPhotoUrl();
String[] firstNameLastName = fullName.split(" ");
String firstName = "";
String lastName = "";
if (firstNameLastName != null && firstNameLastName.length > 1) {
firstName = firstNameLastName[0];
lastName = firstNameLastName[firstNameLastName.length - 1];
} else {
firstName = fullName;
}
ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_TYPE, Constants.LoginType.GOOGLE_PLUS);
ApplicationSession.setToPreference(Constants.Preferences.PREF_GOOGLE_ID, googleId);
ApplicationSession.setToPreference(Constants.Preferences.PREF_GOOGLE_TOKEN, googleToken);
ApplicationSession.setToPreference(Constants.Preferences.PREF_PROFILE_PIC_PATH, googleFileNameWithPath);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_FULL_NAME, fullName);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_LOCATION, null);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_EMAIL, email);
ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_DONE, true);
Log.i(TAG, "handleSignInResultForGoogleLogin(), googleId : " + googleId);
Log.i(TAG, "handleSignInResultForGoogleLogin(), googleToken : [" + googleToken + "]");
Log.i(TAG, "handleSignInResultForGoogleLogin(), fullName : " + fullName);
Log.i(TAG, "handleSignInResultForGoogleLogin(), email : " + email);
Log.i(TAG, "handleSignInResultForGoogleLogin(), googleFileNameWithPath : " + googleFileNameWithPath);
Log.i(TAG, "handleSignInResultForGoogleLogin(), firstName : " + firstName);
Log.i(TAG, "handleSignInResultForGoogleLogin(), lastName : " + lastName);
Intent intent = new Intent(LoginActivity.this, MainNavigationBottomActivity.class);
startActivity(intent);
LoginActivity.this.finish();
if (circularProgressViewDialog != null) {
circularProgressViewDialog.hideDialog();
}
} else {
//If login fails Helper.showCustomToast("Google Login Failed", getResources().getDrawable(R.drawable.cross), LoginActivity.this);
if (circularProgressViewDialog != null) {
circularProgressViewDialog.hideDialog();
}
}
} catch (Exception e) {
e.printStackTrace();
if (circularProgressViewDialog != null) {
circularProgressViewDialog.hideDialog();
}
}
}
@Override public void onConnectionFailed(ConnectionResult connectionResult) {
try {
if (circularProgressViewDialog != null) {
circularProgressViewDialog.hideDialog();
}
Helper.showCustomToast("Login Connection Failed", getResources().getDrawable(R.drawable.cross),
LoginActivity.this);
} catch (Exception e) {
e.printStackTrace();
}
}
// facebook private void initializeForFacebookSignIn() {
Log.i(TAG, "initializeForFacebookSignIn() called : ");
try {
btn_facebook.setReadPermissions("public_profile email");
if (AccessToken.getCurrentAccessToken() != null) {
Log.i(TAG, "AccessToken.getCurrentAccessToken()" + AccessToken.getCurrentAccessToken());
facebookRequestData();
}
Log.i(TAG, "AccessToken.getCurrentAccessToken()" + AccessToken.getCurrentAccessToken());
btn_facebook.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if (AccessToken.getCurrentAccessToken() != null) {
}
}
});
btn_facebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " btn_facebook.registerCallback" + loginResult);
if (AccessToken.getCurrentAccessToken() != null) {
facebookRequestData();
}
}
@Override public void onCancel() {
}
@Override public void onError(FacebookException exception) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void facebookRequestData() {
Log.i(TAG, " facebookRequestData() called");
try{
if (circularProgressViewDialog == null) {
circularProgressViewDialog = new CircularProgressViewDialog(context);
}
circularProgressViewDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
circularProgressViewDialog.showDialog();
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override public void onCompleted(JSONObject object, GraphResponse response) {
Log.i(TAG, " facebookRequestData().onCompleted()JSon object" + object);
Log.i(TAG, " facebookRequestData().onCompleted() *GraphResponse" + response);
JSONObject json = response.getJSONObject();
try {
if (json != null) {
//String text = "<b>Name :</b> " + json.getString("name") + "<br><br><b>Email :</b> " + json.getString("email") + "<br><br><b>Profile link :</b> " + json.getString("link"); String profileId = json.getString("id");
String fullName = json.getString("name");
String email = json.getString("email");
String firstName = "";
String[] firstNameLastName;
String lastName = "";
setFacebookProfilePic(profileId);
ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_TYPE, Constants.LoginType.FACEBOOK);
ApplicationSession.setToPreference(Constants.Preferences.PREF_PROFILE_PIC_PATH, fileNameWithPath);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_FULL_NAME, fullName);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_LOCATION, null);
ApplicationSession.setToPreference(Constants.Preferences.PREF_USER_EMAIL, email);
ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_DONE, true);
Log.i(TAG, " facebookRequestData()fileNameWithPath" + fileNameWithPath);
Log.i(TAG, " facebookRequestData()fullName" + fullName);
firstNameLastName = fullName.split(" ");
if (firstNameLastName != null && firstNameLastName.length > 1) {
firstName = firstNameLastName[0];
lastName = firstNameLastName[firstNameLastName.length - 1];
} else {
firstName = fullName;
}
Intent intent = new Intent(LoginActivity.this, MainNavigationBottomActivity.class);
startActivity(intent);
LoginActivity.this.finish();
// createUserViaSocialMedia(email, firstName, lastName, Constants.LoginType.FACEBOOK); }
} catch (JSONException e) {
e.printStackTrace();
} finally {
circularProgressViewDialog.hideDialog();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
}catch (Exception e){
e.printStackTrace();
}
}
private void setFacebookProfilePic(String userID) {
Log.i(TAG, "setFacebookProfilePic()");
try {
java.net.URL imageURL = new java.net.URL("https://graph.facebook.com/" + userID + "/picture?type=large");
Log.i(TAG, "setFacebookProfilePic()imageURL" + imageURL);
fileNameWithPath = imageURL.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private void login() {
Log.i(TAG, "login() called ");
try {
if (et_email_mobile.getText().toString().trim().length() == 0) {
Helper.showCustomToast(getResources().getString(R.string.enter_email), getResources().getDrawable(R.drawable.cross), LoginActivity.this);
et_email_mobile.requestFocus();
return;
} else if (!Patterns.EMAIL_ADDRESS.matcher(et_email_mobile.getText().toString().trim()).matches() &&
!Helper.isValidPhoneNumber(et_email_mobile.getText().toString().trim())) {
Helper.showCustomToast(getResources().getString(R.string.enter_a_valid_email_address_or_mobile_number), getResources().getDrawable(R.drawable.cross), LoginActivity.this);
et_email_mobile.requestFocus();
return;
} else if (et_password.getText().toString().trim().length() == 0) {
Helper.showCustomToast(getResources().getString(R.string.enter_password), getResources().getDrawable(R.drawable.cross), this);
et_password.requestFocus();
return;
} else {
String userName = et_email_mobile.getText().toString().trim();
String password = et_password.getText().toString().trim();
String loginType = Constants.LoginType.EMAIL;
if (Patterns.EMAIL_ADDRESS.matcher(userName).matches()) {
loginType = Constants.LoginType.EMAIL;
} else if (Helper.isValidPhoneNumber(userName)) {
loginType = Constants.LoginType.MOBILE;
}
if (Helper.isNetworkAvailable(this)) {
ApplicationSession.setToPreference(Constants.Preferences.PREF_LOGIN_DONE, true);
Intent intent = new Intent(context, MainNavigationBottomActivity.class);
startActivity(intent);
LoginActivity.this.finish();
} else {
Helper.showCustomToast(getResources().getString(R.string.no_network_available), getResources().getDrawable(R.drawable.no_network), this);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult(int, int, Intent), called requestCode : " + requestCode + ", resultCode : " + resultCode);
try {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach // a listener. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Log.i(TAG, "onActivityResult(), result : " + result);
Log.i(TAG, "onActivityResult(), result.isSuccess() : " + result.isSuccess());
handleSignInResultForGoogleLogin(result);
} else {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Geadel
// classpath 'com.google.gms:google-services:3.0.0' classpath 'com.google.gms:google-services:4.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.novoda:bintray-release:0.8.0'
Dependency
implementation 'com.google.android.gms:play-services-maps:16.0.0'implementation 'com.google.android.gms:play-services-auth:16.0.1'implementation 'com.google.android.gms:play-services-places:16.0.0'implementation 'com.google.android.gms:play-services-location:16.0.0'implementation 'com.google.android.gms:play-services-nearby:16.0.0'//facebookimplementation 'com.facebook.android:facebook-android-sdk:4.40.0'
implementation 'com.sromku:simple-fb:4.1.1'
Comments
Post a Comment