• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Android Login With Amazon Extension

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?

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();
                       
        }
       
    }

   
}
 

clee2005

Member
The biggest clue I have is on an Android device (non-Kindle Fire) I see this in the log :

Code:
I/yoyo    (25916): onStop

I/yoyo    (25916): onRestart

I/yoyo    (25916): onStart
Where as on a Kindle (where it works) I only see :

Code:
I/yoyo    ( 8671): onResume
Is this referring the the current activity? I'm wondering if perhaps the activity is no longer active as a webpage has shot into view to login, and after pressing LOGIN on the webpage, the extension activity is not around to listen for events? This is where my Android understanding is lacking.
 

clee2005

Member
It seems the issue was in the extending of the Activity class rather than using the RunnerSocial like :

Code:
public class AmazonLogin extends RunnerSocial {
}
Not sure if this is appropriate design or not, but it does seem to work for both Kindle Fire and Android devices.
 
Top