Page Contents
Overview: Android LocalInstallation class
The LocalInstallation class is not a client mirror of the server Installation Model.
Instead, it wraps the Installation model into an API that makes it easy to implement the correct client semantics.
- When
LocalInstallation.save()is called by the application for the first time, a newInstallationrecord is created on the server. The data are cached in application’sSharedPreferences, thus all subsequent calls ofsave()update this existing record. That way there is exactly oneInstallationrecord for each instance of the application (each phone running the app). - It is ok to create a new
LocalInstallationinstance whenever you need to update any of theInstallationproperties. Since the data is cached inSharedPreferences, allLocalInstallationobjects are initialized from the same data at the creation time. Just don’t forget to callsaveto persist your changes both on the server and in the local storage.
Associating installations with authenticated users
- By default,
userIdisnull, which means the user has not logged in yet (has not created an account yet). -
After a successful login, call
LocalInstallation.setUserId()to update the user relation and save the changes.userRepository.loginUser(email, password, new UserRepository<User>.LoginCallback() { @Override public void onSuccess(AccessToken token, User currentUser) { final LocalInstallation installation = new LocalInstallation(context, adapter); installation.setUserId(currentUser.getId()); installation.save(/* callback */); } @Override public void onError(Throwable t) { // handle the error } ); -
After a logout, set
userIdback tonullin order to flag the installation as anonymous.userRepository.logout(new UserRepository<User>.VoidCallback() { @Override public void onSuccess() { final LocalInstallation installation = new LocalInstallation(context, adapter); installation.setUserId(null); installation.save(/* callback */); } @Override public void onError(Throwable t) { // handle the error } );
Alternatively, you can use the status property to flag the installations where the user was logged out.
This way it is possible to tell which user was the last one logged on the device:
- A new installation has
userId: nullandstatus: "Active". - Login updates the
userIdwith a non-empty id value and setsstatus: "Active". - Logout keeps the
userIdvalue but changes thestatusto a differed value, e.g."LoggedOut". - Subsequent login updates the
userIdto the new id value and setsstatusback to"Active".