Created
April 23, 2021 03:46
-
-
Save lokeshsuryan/821fd3e7406a41d09f7b5b92a758c4f8 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.huawei.HMSAuthService; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import com.hw.unity.Agc.Auth.ThirdPartyLogin.LoginManager; | |
import com.unity3d.player.UnityPlayerActivity; | |
import android.util.Log; | |
import com.huawei.agconnect.auth.AGConnectAuth; | |
import com.huawei.agconnect.auth.AGConnectAuthCredential; | |
import com.huawei.agconnect.auth.AGConnectUser; | |
import com.huawei.agconnect.auth.PhoneAuthProvider; | |
import com.huawei.agconnect.auth.SignInResult; | |
import com.huawei.agconnect.auth.VerifyCodeResult; | |
import com.huawei.agconnect.auth.VerifyCodeSettings; | |
import com.huawei.hmf.tasks.OnFailureListener; | |
import com.huawei.hmf.tasks.OnSuccessListener; | |
import com.huawei.hmf.tasks.Task; | |
import com.huawei.hmf.tasks.TaskExecutors; | |
import java.util.Locale; | |
public class MainActivity extends UnityPlayerActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
LoginManager.getInstance().initialize(this); | |
Log.d("DATA"," Inside onCreate "); | |
} | |
public static void AnonymousLogin(){ | |
AGConnectAuth.getInstance().signInAnonymously().addOnSuccessListener(new OnSuccessListener<SignInResult>() { | |
@Override | |
public void onSuccess(SignInResult signInResult) { | |
AGConnectUser user = signInResult.getUser(); | |
String uid = user.getUid(); | |
Log.d("DATA"," Login Anonymous UID : "+uid); | |
} | |
}).addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(Exception e) { | |
Log.d("DATA"," Inside ERROR "+e.getMessage()); | |
} | |
}); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
{ | |
LoginManager.getInstance().onActivityResult(requestCode, resultCode, data); | |
} | |
public static void sendVerifCode(String phone) { | |
VerifyCodeSettings settings = VerifyCodeSettings.newBuilder() | |
.action(VerifyCodeSettings.ACTION_REGISTER_LOGIN) | |
.sendInterval(30) // Shortest sending interval, 30–120s | |
.build(); | |
String countCode = "+91"; | |
String phoneNumber = phone; | |
if (notEmptyString(countCode) && notEmptyString(phoneNumber)) { | |
Task<VerifyCodeResult> task = PhoneAuthProvider.requestVerifyCode(countCode, phoneNumber, settings); | |
task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() { | |
@Override | |
public void onSuccess(VerifyCodeResult verifyCodeResult) { | |
Log.d("DATA"," ==>"+verifyCodeResult); | |
} | |
}).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() { | |
@Override | |
public void onFailure(Exception e) { | |
Log.d("DATA"," Inside onFailure"); | |
} | |
}); | |
} | |
} | |
static boolean notEmptyString(String string) { | |
return string != null && !string.isEmpty() && !string.equals(""); | |
} | |
public static void linkPhone(String verifyCode1,String phone) { | |
Log.d("DATA", " verifyCode1 "+verifyCode1); | |
String phoneNumber = phone; | |
String countCode = "+91"; | |
String verifyCode = verifyCode1; | |
Log.e("DATA", " verifyCode "+verifyCode); | |
AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode( | |
countCode, | |
phoneNumber, | |
null, // password, can be null | |
verifyCode); | |
AGConnectAuth.getInstance().getCurrentUser().link(credential).addOnSuccessListener(new OnSuccessListener<SignInResult>() { | |
@Override | |
public void onSuccess(SignInResult signInResult) { | |
String phoneNumber = signInResult.getUser().getPhone(); | |
String uid = signInResult.getUser().getUid(); | |
Log.d("DATA", "phone number: " + phoneNumber + ", uid: " + uid); | |
} | |
}).addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(Exception e) { | |
Log.e("DATA", "Login error, please try again, error:" + e.getMessage()); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment