clee2005
Member
Hey guys,
I'm trying to put together an Android extension to allow Logging in with an Amazon account (same as you would with Google or Facebook). I have it working on Kindle Fire devices, but for some reason it will not work on a regular Android device even though it should according to the docs.
https://developer.amazon.com/docs/login-with-amazon/documentation-overview.html
I'm just getting familiar with Android programming, so I was hoping someone with more experience might be able to see something I can't. It is odd that it runs on a Kindle Fire as expected, but the events don't seem to be firing on the Android (non-kindle) devices. It's like the listener isn't working... the login screen appears when AmazonLogin_Login() is called but when you actually login, no other events occur.
Any ideas? Suggestions on how to proceed?
I'm trying to put together an Android extension to allow Logging in with an Amazon account (same as you would with Google or Facebook). I have it working on Kindle Fire devices, but for some reason it will not work on a regular Android device even though it should according to the docs.
https://developer.amazon.com/docs/login-with-amazon/documentation-overview.html
I'm just getting familiar with Android programming, so I was hoping someone with more experience might be able to see something I can't. It is odd that it runs on a Kindle Fire as expected, but the events don't seem to be firing on the Android (non-kindle) devices. It's like the listener isn't working... the login screen appears when AmazonLogin_Login() is called but when you actually login, no other events occur.
Any ideas? Suggestions on how to proceed?
Code:
package ${YYAndroidPackageName};
import android.os.Build;
import ${YYAndroidPackageName}.R;
import android.app.Activity;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.RunnerActivity;
import android.util.Log;
import java.io.*;
import android.os.Environment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.amazon.identity.auth.device.AuthError;
import com.amazon.identity.auth.device.api.Listener;
import com.amazon.identity.auth.device.api.authorization.AuthCancellation;
import com.amazon.identity.auth.device.api.authorization.AuthorizationManager;
import com.amazon.identity.auth.device.api.authorization.AuthorizeListener;
import com.amazon.identity.auth.device.api.authorization.AuthorizeRequest;
import com.amazon.identity.auth.device.api.authorization.AuthorizeResult;
import com.amazon.identity.auth.device.api.authorization.ProfileScope;
import com.amazon.identity.auth.device.api.authorization.Scope;
import com.amazon.identity.auth.device.api.authorization.User;
import com.amazon.identity.auth.device.api.workflow.RequestContext;
public class AmazonLogin extends Activity {
final int EVENT_OTHER_SOCIAL = 70;
private static final String TAG = "AmazonLogin";
private RequestContext requestContext;
private boolean mIsLoggedIn;
private String userid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AmazonLogin_Init();
}
public void AmazonLogin_Init() {
requestContext = RequestContext.create(RunnerActivity.CurrentActivity);
requestContext.registerListener(new AuthorizeListener() {
/* Authorization was completed successfully. */
@Override
public void onSuccess(AuthorizeResult authorizeResult) {
Log.i("yoyo", "Fetch User Profile");
fetchUserProfile();
}
/* There was an error during the attempt to authorize the application */
@Override
public void onError(AuthError authError) {
Log.e(TAG, "AuthError during authorization", authError);
}
/* Authorization was cancelled before it could be completed. */
@Override
public void onCancel(AuthCancellation authCancellation) {
Log.e(TAG, "User cancelled authorization");
}
});
// Call on start to see if the user has already logged in.
Log.i("yoyo", "Completed Init of AmazonLogin in...");
}
public void AmazonLogin_Login() {
Log.i("yoyo", "Starting Amazon login...");
try {
AuthorizationManager.authorize(
new AuthorizeRequest.Builder(requestContext)
.addScopes(ProfileScope.profile())
.build()
);
} catch ( Exception e ) {
Log.e("yoyo", e.getMessage());
e.printStackTrace();
}
}
public String AmazonLogin_GetUserId() {
Log.i("yoyo", "Returning Userid : " + userid);
fetchUserProfile();
return userid;
}
@Override
protected void onResume() {
Log.i("yoyo", "AmazonLogin : onResume");
super.onResume();
requestContext.onResume();
}
@Override
protected void onStart() {
Log.i("yoyo", "onStart called in AmazonLogin");
super.onStart();
Scope[] scopes = {ProfileScope.profile()};
AuthorizationManager.getToken(RunnerActivity.CurrentActivity, scopes, new Listener<AuthorizeResult, AuthError>() {
@Override
public void onSuccess(AuthorizeResult result) {
if (result.getAccessToken() != null) {
/* The user is signed in */
fetchUserProfile();
} else {
/* The user is not signed in */
}
}
@Override
public void onError(AuthError ae) {
/* The user is not signed in */
}
});
}
private void RaiseSocialEvent(String key,String value) {
int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString( dsMapIndex, "type", "AmazonLogin" );
RunnerJNILib.DsMapAddString( dsMapIndex, key,value );
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex,EVENT_OTHER_SOCIAL);
}
private void fetchUserProfile() {
Log.i("yoyo", "Fetch User Profile from AmazonLogin called");
try {
User.fetch(RunnerActivity.CurrentActivity, new Listener<User, AuthError>() {
/* fetch completed successfully. */
@Override
public void onSuccess(User user) {
final String name = user.getUserName();
final String email = user.getUserEmail();
final String account = user.getUserId();
final String zipCode = user.getUserPostalCode();
userid = user.getUserId();
RaiseSocialEvent("userid",userid);
}
/* There was an error during the attempt to get the profile. */
@Override
public void onError(AuthError ae) {
RaiseSocialEvent("error","Failed to get user profile data");
}
});
} catch ( Exception e ) {
Log.e("yoyo", e.getMessage());
e.printStackTrace();
}
}
}