RxSmartLock

Additional

Language
Java
Version
1.1.4 (Jan 4, 2016)
Created
Dec 13, 2015
Updated
Jan 6, 2016 (Retired)
Owner
Soheil Malekzadeh (ShlMlkzdh)
Contributor
Soheil Malekzadeh (ShlMlkzdh)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

RxSmartLock

Reactive extension for Google's Smart Lock for Passwords API.

Introduction

Sebastiano Poggi has written a fantastic blog post on Smart Lock. Read it here.

Usage

In order to use RxSmartLock, create a SmartLock object using the SmartLock.Builder class in the onCreate method of your Activity:

public class MainActivity extends AppCompatActivity {

    private SmartLock mSmartLock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSmartLock = new SmartLock.Builder(this)
                .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.FACEBOOK) // Use the constants defined in IdentityProviders to specify commonly-used sign-in providers.
                .setPasswordLoginSupported(true) // Used for password-based sign-in.
                .build();

    }

}

Now you are ready to use RxSmartLock.

Retrieve a User's Stored Credentials

Automatically sign users into your app by using the Credentials API to request and retrieve stored credentials for your users.

To request stored credentials, call the retrieveCredential() method on the SmartLock object that you created from anywhere in your Activity. This method returns an observabale Observable<Credential> which you can subscribe to:

mSubscription = mSmartLock.retrieveCredential().subscribe(new Subscriber<Credential>() {

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

        // Handle unsuccessful and incomplete credential requests.
        if (e instanceof StatusException) {
            Status status = ((StatusException) e).getStatus();
            if (status.hasResolution()) {
                // Prompt the user to choose a saved credential; do not show the hint selector.
                try {
                    status.startResolutionForResult(MainActivity.this, CREDENTIAL_REQUEST_RC);
                } catch (IntentSender.SendIntentException e1) {
                    Log.e(TAG, "STATUS: Failed to send resolution.");
                }
            } else {
                // The user must create an account or sign in manually.
                Log.e(TAG, "STATUS: Unsuccessful credential request.");
            }
        }

    }

    @Override
    public void onNext(Credential credential) {
        // On a successful credential request, use the resulting Credential object to complete the user's sign-in to your app.
    }

});

When user input is required to select a credential, the Status.hasResolution() method returns true. In this case, call the status object's startResolutionForResult() method to prompt the user to choose an account. Then, retrieve the user's chosen credentials from the activity's onActivityResult() method by calling the retrieveCredentialFromIntent(Intent) method on the SmartLock object that you created. This method returns an observabale Observable<Credential> which you can again subscribe to:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        
    super.onActivityResult(requestCode, resultCode, data);
        
    if (requestCode == CREDENTIAL_REQUEST_RC) {
        if (resultCode == RESULT_OK) {
            mSmartLock.retrieveCredentialFromIntent(data).subscribe(new Action1<Credential>() {
                @Override
                public void call(Credential credential) {
                    // On a successful credential request, use the resulting Credential object to complete the user's sign-in to your app.
                }
            });
        } else {
            Log.e(TAG, "Credential Read: NOT OK");
        }
    }

}

For more information, check out the official documentaion on how to handle successful credential requests.

Store a User's Credentials

After users successfully sign in, create accounts, or change passwords, allow them to store their credentials to automate future authentication in your app.

To save users' credentials, you can call the storeCredential(Credential) method on the SmartLock object that you created from anywhere in your Activity. This method returns an observabale Observable<Boolean> which you can subscribe to:

mSubscription = mSmartLock.storeCredential(credential).subscribe(new Subscriber<Boolean>() {
            
    @Override
    public void onCompleted() {
      
    }

    @Override
    public void onError(Throwable e) {
    
        // Try to resolve the save request. 
        // This will prompt the user if the credential is new.
        if (e instanceof StatusException) {
            Status status = ((StatusException) e).getStatus();
            if (status.hasResolution()) {
                try {
                    status.startResolutionForResult(MainActivity.this, CREDENTIAL_STORE_RC);
                } catch (IntentSender.SendIntentException e1) {
                    Log.e(TAG, "STATUS: Failed to send resolution.");
                }
            }
        }
        
    }

    @Override
    public void onNext(Boolean aBoolean) {
    
        if (aBoolean) {
            // We have successfully saved the user's credential.
        }
        
    }
            
});

If the call to SmartLock.storeCredential(Credential) is not immediately successful, the credentials might be new, in which case the user must confirm the save request. Resolve the save request with startResolutionForResult() to prompt the user for confirmation.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CREDENTIAL_STORE_RC) {
        if (resultCode == RESULT_OK) {
            Log.d(TAG, "SAVE: OK");
        } else {
            Log.e(TAG, "SAVE: Canceled by user.");
        }
    }

}

To learn more about how to store a user's credentials, check out the official documentaion.

Delete Stored Credentials

Delete credentials from Smart Lock when either of the following circumstances occur:

  • Signing in with the credentials fails because the account no longer exists or the password is incorrect.
  • The user completes the app's account deletion flow.

To delete credentials, you can call the deleteCredential(Credential) method on the SmartLock object that you created from anywhere in your Activity. This method returns an observabale Observable<Boolean> which you can subscribe to:

mSubscription = mSmartLock.deleteCredential(credential).subscribe(new Subscriber<Boolean>() {
            
    @Override
    public void onCompleted() {
      
    }

    @Override
    public void onError(Throwable e) {
    
        // Handle unsuccessful deletion.
        if (e instanceof StatusException) {
            Status status = ((StatusException) e).getStatus();
            Log.e(TAG, status.getStatusMessage());
        }
        
    }

    @Override
    public void onNext(Boolean aBoolean) {
    
        if (aBoolean) {
            // Credential was deleted successfully.
        }
        
    }
            
});

Google Play Services Connection Failures Handling

Since Smart Lock for Passwords is a Google Play Services API, we need to handle possible connection failures when the connection to Google Play services fails or becomes suspended. This is how you can deal with these situtations gracefully when using RxSmartLock:

@Override
public void onError(Throwable e) {

    if (e instanceof ConnectionSuspendedException) {
        int errorCode = ((ConnectionSuspendedException) e).getCode();
        // Handle the connection suspension errors here.
    }

}
@Override
public void onError(Throwable e) {

    if (e instanceof ConnectionException) {
        ConnectionResult result = ((ConnectionException) e).getConnectionResult();
        // Handle the connection failed errors here.
        if (result.hasResolution()) {
            result.startResolutionForResult(MainActivity.this, RESULT_RC);
        }
    }

}

You can find more information on how to handle connection failures here.

Add RxSmartLock To Your Project

Add this to your build.gradle file:

dependencies {
     compile 'com.shlmlkzdh:rx-smart-lock:1.1.4'
}

Additional Notes

This library is inspired by the ReactiveLocation library for Android developed by Michał Charmas and a great blog post written by Sebastiano Poggi on Smart Lock. Thank you guys!

License

Copyright 2015 - 2016 Soheil Malekzadeh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.