Create and Manage Users
User Service
1. Dependencies
Open the build.gradle file for your project and add the following dependency:
| // inside /<module>/build.gradle
...
dependencies {
...
implementation "com.telenav.sdk.vivid:android-sdk-userservice:0.9.0"
}
...
|
2. Initialize User account manager
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun init() {
val applicationContext = getActivity().getApplication()
userServiceManager = UserServiceManager.Factory.createInstance()
userServiceManager.init(applicationContext)
}
|
3. Register User account
This API registers a user with the system. Once the user is successfully registered, he/she can log into the system to further use User Servcies.
Before calling login API, it's required that this API is successfully executed. Primarily this API should be the first API when the app is launched first time.
Without successful registration, user will never be able to log in and hence will never have secure token to access any further services.
For anonymous user, this API will never fail. For any other type of user registration, it will fail if there exist a user with the same credentials. This API can also be called after user logs out of application and dose not uninstall the app.
Besides the username and password mandatory parameters, firstname, lastname, avatarUrl, contactsList, additionalInfo and a VividProfile can be added during the register call. AdditionalInfo is a map with unconstrained String keys and values. The status code indicates the result of the action.
For carrier user, before calling register api, please get billingId first and then set billingId in "additional_info" with key "BILLING_ID".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | private lateinit var userServiceManager: UserServiceManager
//init()....
fun register() {
val contacts: ArrayList<Contact> = ArrayList()
val contact = Contact()
contact.contactType = ContactType.PHONE
contact.contactValue = "0800900100"
contacts.add(contact)
val map: HashMap<String, String> = HashMap()
map["telenavEmployee"] = "true"
val userServiceResponse = userServiceManager.registerRequest(
"john.doe1@telenav.com", "pass1",
"john", "doe", "pics.com/joe", contacts, map,
VividUserProfile(null, "John", "Doe")
)
Log.d(TAG,"Register status "+ userServiceResponse?.userServiceStatus!!.statusCode)
}
|
4. Register guest (anonymous) account
Handles both register and login for a Guest(anonymous) user, at the same time. This API will never fail.
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun login() {
val response = userServiceManager.registerAndLoginGuest()
Log.d(TAG,"Register status "+ response?.userServiceStatus!!.statusCode)
}
|
5. Login/Logout API
The required parameters are username and password. The response contains both the secret token, refresh token and user id.
1
2
3
4
5
6
7
8
9
10
11
12 | private lateinit var userServiceManager: UserServiceManager
//init()....
fun login() {
//login API call
val loginResponse = userServiceManager.login(DEFAULT_USERNAME, DEFAULT_PASSWORD)
Log.d(TAG,"Access token "+loginResponse?.tokenSet!!.secureToken)
//logout API call
val logoutResponse = userServiceManager.logout(loginResponse?.tokenSet!!)
Log.d(TAG,"Logout status "+ logoutResponse?.userServiceStatus!!.statusCode)
}
|
6. Login with Google Account
Executes user login based on google account id and the accessToken received from Google. The response contains both the secret token, refresh token and user id.
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun loginWithGoogle(accessToken: String) {
val loginWithGoogleResponse = userServiceManager.registerLoginWithGoogle(GOOGLE_ACCOUNT_ID, accessToken)
Log.d(TAG,"Secure token "+loginWithGoogleResponse?.tokenSet!!.secureToken)
}
|
7. Refresh token
Pass a token set to the refreshSecureToken method in order to get a fresh token set.
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun refreshToken(tokenSet){
......
val refreshTokenResponse = userServiceManager.refreshSecureToken(tokenSet)
Log.d(TAG,"RefreshTokenResponse status "+ refreshTokenResponse?.userServiceStatus!!.statusCode)
}
|
8. Get session
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun getSession(secureToken: String){
......
val getSessionResponse = userServiceManager.getSession(secureToken)
Log.d(TAG,"GetSessionResponse status "+ getSessionResponse?.userServiceStatus!!.statusCode)
}
|
9. Change password
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun changePassword(secureToken: String, oldPassword: String, newPassword: String){
......
val changePasswordResponse = userServiceManager.changePasswordResponse(secureToken, oldPassword, newPassword)
Log.d(TAG,"ChangePasswordResponse status "+ getSessionResponse?.userServiceStatus!!.statusCode)
}
|
10. Get password reset token
SecureToken parameter is optional.
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun getPasswordResetToken(email: String, secureToken: String?){
......
val getPasswordResetTokenResponse = userServiceManager.getPasswordResetToken(email, secureToken)
Log.d(TAG,"GetPasswordResetTokenResponse status "+ getPasswordResetTokenResponse?.userServiceStatus!!.statusCode)
}
|
11. Reset password
SecureToken parameter is optional.
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun resetPassword(resetToken: String, password: String, secureToken: String?){
......
val resetPasswordResponse = userServiceManager.resetPassword(resetToken, password, secureToken)
Log.d(TAG,"resetPasswordResponse status "+ resetPasswordResponse?.userServiceStatus!!.statusCode)
}
|
12. Add credential
| private lateinit var userServiceManager: UserServiceManager
//init()....
fun addCredential(userId: String, email: String, password: String, secureToken: String){
......
val addCredentialResponse = userServiceManager.resetPassword(resetToken, password, secureToken)
Log.d(TAG,"addCredentialResponse status "+ addCredentialResponse?.userServiceStatus!!.statusCode)
}
|
13. Initialize User profile manager
Context is needed in order to initialize profile manager.
| private lateinit var userProfileServiceManager: UserProfileServiceManager
fun init() {
val applicationContext = getActivity().getApplication()
userProfileServiceManager = UserProfileServiceManager.Factory.createInstance()
userProfileServiceManager.init(applicationContext)
}
|
14. Save user profile
VividUserProfile object can be initialized only with the data needed to be updated. It remains at user decision which attributes should be updated. (like: phone number only, birthdate only, security questions...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | private lateinit var userProfileServiceManager: UserProfileServiceManager
//init()....
fun saveUserName(){
val loginResponse = userServiceManager.login(DEFAULT_USERNAME, DEFAULT_PASSWORD)
val vividUserProfile = VividUserProfile(
firstName = "John",
lastName = "Doe",
userProfileBirthday = "12/06/2000",
userContactPhone = "004070006004",
userGender = "male",
userProfileCountry = "US"
)
val saveProfile =
userProfileServiceManager.saveUserProfile(
vividUserProfile, getUserContext(loginResponse)
)
Log.DEBUG(TAG,"Save profile status "+ saveProfile.statusCode)
val vividUserProfile = VividUserProfile(
userContactPhone = "0040700000004",
)
val saveProfile =
userProfileServiceManager.saveUserProfile(
vividUserProfile, getUserContext(loginResponse)
)
Log.DEBUG(TAG,"Save profile status "+ saveProfile.statusCode)
}
|
15. Get user profile
| private lateinit var userProfileServiceManager: UserProfileServiceManager
//init()....
fun getUserProfile():VividUserProfile{
val userProfile = userProfileServiceManager.getProfile(loginResponse.tokenSet.secureToken)
Log.DEBUG(TAG,"User profile "+ userProfile.toString())
}
|
16. Save user profile first name and last name
1
2
3
4
5
6
7
8
9
10
11
12
13 | private lateinit var userProfileServiceManager: UserProfileServiceManager
//init()....
fun saveUserName(firstName:String, lastName:String){
val loginResponse = userServiceManager.login(DEFAULT_USERNAME, DEFAULT_PASSWORD)
val saveProfile =
userProfileServiceManager.saveUserProfileName(
firstName = CHANGED_FIRST_NAME, lastName = CHANGED_LAST_NAME, userContext = getUserContext(loginResponse)
)
Log.DEBUG(TAG,"Save profile status "+ saveProfile.statusCode)
}
|
17. Confirm email id
1
2
3
4
5
6
7
8
9
10
11
12 | private lateinit var userProfileServiceManager: UserProfileServiceManager
//init()....
fun saveUserName(firstName:String, lastName:String){
val loginResponse = userServiceManager.login(DEFAULT_USERNAME, DEFAULT_PASSWORD)
val saveProfile =
userProfileServiceManager.saveConfirmedEmailValue(
EMAIL_CONFIRMED, getUserContext(loginResponse))
Log.DEBUG(TAG,"Save profile status "+ saveProfile.statusCode)
}
|
User service content provider
1. Dependencies
Open the build.gradle file for your project and add the following dependency
| // inside <module> build.gradle
...
dependencies {
...
implementation 'com.telenav.sdk.vivid.userservice:vivid-sdk-userservice-contentprovider:{userservice_version}'//e.g 0.39.0.6
}
...
|
2. Add components to the AndroidManifest.xml
AndroidManifest.xml should contain the following service and content provider components inside the application tag:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | <application>
...
<service
android:name="com.telenav.sdk.vivid.userservice.contentprovider.UserService"
android:exported="true"
android:process=":vivid_sdk_userservice_contentprovider"/>
<provider
android:name="com.telenav.sdk.vivid.userservice.contentprovider.provider.UserContentProvider"
android:authorities="com.telenav.vivid.provider"
android:exported="true"
android:process=":vivid_sdk_userservice_contentprovider"/>
...
</application>
|
3. Create a UserService instance
UserService stub object is used to call AIDL APIs directly. Below you can find an example of how to get the UserService instance for AIDL calls, and how to register a UserListener:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 | private val serviceConnection = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
Timber.d("UserService: Disconnected")
userService = null
}
override fun onBindingDied(name: ComponentName?) {
super.onBindingDied(name)
Timber.w("UserService: Disconnected")
userService = null
}
override fun onNullBinding(name: ComponentName?) {
Timber.e("UserService: Unable to Connected")
super.onNullBinding(name)
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Timber.d("UserService: Connected")
// Here we get the userservice instance
userService = IUserService.Stub.asInterface(service)
userService?.safeCall {
it.addUserListener(userListener)
}
}
}
override fun init(appContext: Context) {
if (userService == null) {
Timber.d("bind UserService")
appContext.bindService(
Intent(
appContext,
UserService::class.java
),
serviceConnection,
Context.BIND_AUTO_CREATE
)
}
}
|
4. Initializing the environment
Method used to reinitialize UserService with a different environment (URL, API key, API secret).
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun init(serviceUrl: String?,
apiKey: String?,
apiSecretKey: String?,
onFailure: OnFailureListener?) {
userServiceImpl.addUserListener(userListener)
userServiceImpl.reinitService(serviceUrl, apiKey, apiSecretKey, onFailure)
}
|
5. Add user listener
The following method allows to add custom user listener.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun addUserListener() {
userServiceImpl.addUserListener(object : UserListener.Stub() {
override fun onUserLogin(user: User?) {
//...
}
override fun onActiveUserChanged(user: User?) {
//...
}
override fun onUserLogout(user: User?) {
//...
}
})
}
|
6. Remove user listener
The following method allows to remove user listener.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun removeUserListener(userListener: UserListener?) {
userServiceImpl.removeUserListener(userListener)
}
|
7. Login
The required parameters are username, password and an OnFailureListener object. The response contains both the secret token, refresh token and user id.
Note
Add user listener in order to perform login. It is valid for login with Google account and anonymous login too.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun login(email: String, password: String, onFailureListener: OnFailureListener) {
userServiceImpl.addUserListener(userListener)
val response = userServiceImpl.login(email, password, onFailureListener)
Log.d(TAG,"Secure token "+response?.tokenSet!!.secureToken)
}
|
8. Login with Google Account
The required parameter is an OnFailureListener object.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun loginWithGoogle(onFailureListener: OnFailureListener) {
//...
userServiceImpl.loginWithGoogle(onFailureListener)
}
|
9. Login and Register with Google Account
The required parameters are googleAccount, accessToken and an OnFailureListener object.
Note
This method should not be called from a Client application, please call loginWithGoogle().
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun loginWithGoogle(googleAccount: GoogleAccount, accessToken: String, onFailure: OnFailureListener?) {
//...
val response = userServiceImpl.loginAndRegisterWithGoogleAccount(googleAccount, accessToken, FailureListener)
Log.d(TAG,"Secure token "+response?.tokenSet!!.secureToken)
}
|
10. Login anonymous user
Handles login for an anonymous(guest) user.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun loginAnonymous(onFailureListener: OnFailureListener) {
//...
val response = userServiceImpl.loginAnonymousUser(onFailureListener)
Log.d(TAG,"Login status "+ response?.userServiceStatus!!.statusCode)
}
|
11. Register
The following method is used to register a new user. It returns the UserServiceResponse with userId generated for the new registered user.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun register(email: String, password: String, onFailure: OnFailureListener?) {
//...
val response = userServiceImpl.register(email, password, onFailure)
Log.d(TAG,"Register status "+ response?.userServiceStatus!!.statusCode)
Log.d(TAG,"New user id: "+ response?.userId)
}
|
12. Register with info
The following method is used to register a new user with multiple parameters. It returns the UserServiceResponse with userId generated for the new registered user.
1
2
3
4
5
6
7
8
9
10
11
12 | private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun registerWithInfo(vividUserProfile: VividUserProfile?, onFailure: OnFailureListener?) {
//...
val response = userServiceImpl.registerWithInfo(DEFAULT_USERNAME, DEFAULT_PASSWORD,
DEFAULT_FIRST_NAME, DEFAULT_LAST_NAME,
DEFAULT_AVATAR_URL, vividUserProfile, onFailure)
Log.d(TAG,"Register status "+ response?.userServiceStatus!!.statusCode)
Log.d(TAG,"New user id: "+ response?.userId)
}
|
13. Logout
The following method is used for performing user logout. Can be used for anonymous and regular users.
Note
In order to perform logout you need to add user listener and to login.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun logout(userId: String, onFailureListener: OnFailureListener) {
//...
val response = userServiceImpl.logout(userId, onFailureListener)
Log.d(TAG,"Logout status "+ response?.userServiceStatus!!.statusCode)
}
|
14. Logout current user
The following method is used to perform logout current user from TN account.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun logoutCurrentUser(onFailureListener: OnFailureListener) {
//...
userServiceImpl.logoutCurrentUser(onFailureListener)
}
|
15. Sign out from Google Account
The following method is used to perform a logout/sign out from Google account, not for TN UserService.
| private lateinit var userServiceImpl: UserServiceImpl
private lateinit var userListener: UserListener
//init()....
fun signOutGoogleAccount(onFailureListener: OnFailureListener) {
//...
userServiceImpl.signOutFromGoogle(onFailureListener)
}
|
16. Switch current user
Method used to switch the active user (from the logged in users).
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun switchCurrentUser(userId: String, onFailure: OnFailureListener?) {
//...
userServiceImpl.switchCurrentUser(userId, onFailure)
}
|
17. Switch Google Account
Method used to trigger switch google account flow.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun switchGoogleAccount(onFailure: OnFailureListener?) {
//...
userServiceImpl.switchGoogleAccount(onFailure)
}
|
18. Get current user
The following method allows receiving the current user information.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun getCurrentUser() {
//...
val currentUser = userServiceImpl.getCurrentUser()//returns a User?
Log.d(TAG,"User Id "+ currentUser?.id)
Log.d(TAG,"Secure token "+ currentUser?.secureToken)
}
|
19. Get logged in users
Method used to retrieve the logged in users.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun getLoggedInUsers() {
//...
val loggedInUsers = userServiceImpl.getLoggedInUsers()//returns a MutableList<User>?
}
|
20. Get user's status
The following methods are used for determining the current user status.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun getUserStatus(userId: String) {
Log.d(TAG,"There is a logged in user: "+ userServiceImpl?.hasUserLogin())
Log.d(TAG,"Is anonymous user: "+ userServiceImpl?.isAnonymousUser(userId))
}
|
21. Get user profile
The following method is used to get the profile information of a user.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun getUserProfile(userId: String, onFailureListener: OnFailureListener) {
//...
val userProfile = userServiceImpl.getProfile(userId, onFailureListener)//returns a VividUserProfile?
Log.d(TAG,"User profile email: "+ userProfile?.email)
}
|
22. Get current user profile
The following method is used to get the current profile information of a user.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun getCurrentUserProfile() {
//...
val currentUserProfile = userServiceImpl.getCurrentProfile()//returns a VividUserProfile?
Log.d(TAG,"User profile email: "+ currentUserProfile?.email)
}
|
23. Save user profile
The following method is used to save the profile information of a user.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun saveUserProfile(vividUserProfile: VividUserProfile, userContext: UserContext, onFailure: OnFailureListener) {
//...
userServiceImpl.saveProfile(vividUserProfile, userContext, onFailure)
}
|
24. Register process death
The following method is used to link an application lifecycle to the UserService service lifecycle. In this way the service is notified whenever a client app is not alive anymore and safely removes its listeners.
| private lateinit var userServiceImpl: UserServiceImpl
//init()....
fun registerProcessDeath(binder: IBinder,
packageName: String,
userListener: UserListener?) {
//...
val isLinkedToDeath = userServiceImpl.registerProcessDeath(binder, packageName, userListener)//returns a Boolean
}
|
Query the content provider
Use content provider Uri to query user data. Currently Uri's available for query are:
- UserServiceContracts.currentUserUri
- UserServiceContracts.userProfileUri
- UserServiceContracts.loggedInUsers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | fun queryContentProvider(context: Context,
projection: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?) {
//...
//perform login..
val cursor = context.contentResolver.query(UserServiceContracts.loggedInUsers,
projection,
selection,
selectionArgs,
sortOrder)
Log.d(TAG,"Number of logged in users: "+ cursor!!.count)
//get the value of IS_ANONYMOUS column
val isAnonymous = cursor.getString(cursor.getColumnIndex(UserServiceContracts.UserColumns.IS_ANONYMOUS))
//get the value of EMAIL column
val email = cursor.getString(cursor.getColumnIndex(UserServiceContracts.UserColumns.EMAIL))
}
|