API Login

In order to download environment data from the NEON Cloud, we need to enter our credentials into our Android app to allow access.

To enter credentials you can call the NEON Login Activity. This will allow the user to enter a user name and password or sign in with a Google account.

Start the Login Activity

We can call NeonSettings.startLoginActivityForResult() to bring up the NEON login screen. To know when to present this to the user, we can check the AUTHENTICATION event for the value of NO_CREDENTIALS_SET.

We can add a switch statement to our onEvent() listener that handles the AUTHENTICATION event as shown below.

private static int LOGIN_ACTIVITY_REQUEST_CODE = 1002;

@Override
public void onEvent(NeonEventType neonEventType, INeonEvent iNeonEvent) {
    switch(neonEventType)
    {
        case AUTHENTICATION:
            AuthenticationEvent ae = (AuthenticationEvent)iNeonEvent;
            if (ae.getType() == null)
                break;
            switch (ae.getType())
            {
                case NO_CREDENTIALS_SET: NeonSettings.startLoginActivityForResult(LOGIN_ACTIVITY_REQUEST_CODE, MapsActivity.this); break;
                case MANDATORY_UPDATE_REQUIRED: NeonSettings.upgradeNeonLocationServices(MapsActivity.this, UPGRADE_ACTIVITY_REQUEST_CODE,true); break;
                case UNRESOLVED_AUTHENTICATION_ERROR: Toast.makeText(getApplicationContext(), "Error in login.  Please visit NEON Settings page to resolve errors",Toast.LENGTH_LONG).show(); break;
                case SUCCESS:
                    Log.i("NEONSampleApp", "successfully logged in to Neon Location Service");

                    //any work that requires a login can be started here
                    break;
                default: break;
            }
            break;
        default: break;
    }
}

The code above checks the authentication event and responds to the possible values. If the NO_CREDENTIALS_SET event is received, we should bring up the login screen and give the user a chance to login. If the MANDATORY_UPDATE_REQUIRED event is sent, then the NEON Location Service needs to be updated before it can be used and we present the upgrade activity. For any other errors, the user should be directed to the NEON Settings page to resolve them. Finally, on SUCCESS, any work that requires network connection to NEON Cloud should be initiated.

Handle Cancellation

The NeonSettings.startLoginActivityForResult() can return a result to the sample app that indicates that the user has canceled this activity and you can choose to take an appropriate action such as closing the activity as shown below.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE)
    {
        switch (resultCode) {
            case Activity.RESULT_CANCELED:
                Log.i("NEONSampleApp","login was canceled, closing activity");
                shutdown();
                finish();
                break;
            default:
                break;
        }
    }
    else if(requestCode == UPGRADE_ACTIVITY_REQUEST_CODE) {
        switch (resultCode) {
            case Activity.RESULT_CANCELED:
                Log.i("NEONSampleApp", "upgrade was canceled, closing activity");
                shutdown();
                finish();
                break;
            default:
                break;
        }
    }
}

Credential Storage

If you start the sample app, it will immediately grab this event and present you with the NEON Login screen where you can enter your credentials. A session token for these credentials will be generated and stored automatically. You won’t need to log-in again until your credentials expire.

Login Screen