View in Java Kotlin View on GitHub
Sample viewer app
Retrieve a user's details via a Portal.
Use case
This portal information can be used to provide a customized UI experience for the user. For example, you can show a thumbnail next to their username in the header of an application to indicate that they are currently logged in. Additionally, apps such as Collector and Explorer use this functionality to integrate with Portal.
How to use the sample
When prompted, enter your ArcGIS Online credentials.
How it works
Create a Portal
, requesting an URL and requiring login.
When the app launches, the portal is loaded, which triggers an authentication challenge.
Display a login screen with AuthenticationView
.
Upon successful login, get a PortalUser
using portal.user
. Get user attributes using:
portalUser.fullName
portalUser.email
portalUser.created
Load a thumbnail image using portalUser.fetchThumbnailAsync()
Relevant API
AuthenticationManager
DefaultAuthenticationChallengeHandler
PortalInfo
PortalUser
About the data
This sample signs into your ArcGIS online account and displays the user's profile information.
account, avatar, bio, cloud and portal, email, login, picture, profile, user, username
Sample CodeMainActivity.kt
Use dark colors for code blocks Copy
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright 2017 Esri
*
* 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.
*
*/
package com.esri.arcgisruntime.sample.portaluserinfo
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.portal.Portal
import com.esri.arcgisruntime.sample.portaluserinfo.databinding.ActivityMainBinding
import com.esri.arcgisruntime.security.AuthenticationManager
import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity () {
private val activityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
private val userImage: ImageView by lazy {
activityMainBinding.userImage
}
private val portalName: TextView by lazy {
activityMainBinding.content.portalName
}
private val userName: TextView by lazy {
activityMainBinding.content.userName
}
private val email: TextView by lazy {
activityMainBinding.content.email
}
private val createDate: TextView by lazy {
activityMainBinding.content.createDate
}
// objects that implement Loadable must be class fields to prevent being garbage collected before loading
private lateinit var portal: Portal
override fun onCreate (savedInstanceState: Bundle ?) {
super .onCreate(savedInstanceState)
setContentView(activityMainBinding.root)
// Set the DefaultAuthenticationChallengeHandler to allow authentication with the portal.
val handler = DefaultAuthenticationChallengeHandler( this )
AuthenticationManager.setAuthenticationChallengeHandler(handler)
// Set loginRequired to true always prompt for credential,
// When set to false to only login if required by the portal
portal = Portal( "https://www.arcgis.com" , true )
portal.addDoneLoadingListener {
when (portal.loadStatus) {
LoadStatus.LOADED -> {
val portalInformation = portal.portalInfo
val portalInfoName = portalInformation.portalName
portalName.text = portalInfoName
// this portal does not require authentication, if null send toast message
if (portal.user != null ) {
// Get the authenticated portal user
val user = portal.user
// get the users full name
val fullname = user.fullName
userName.text = fullname
// get the users email
val userEmail = user.email
email.text = userEmail
// get the created date
val startDate = user.created
val simpleDateFormat = SimpleDateFormat( "dd-MMM-yyyy" , Locale.US)
val formatDate = simpleDateFormat.format(startDate.time)
createDate.text = formatDate
// check if user profile thumbnail exists
if (user.thumbnailFileName != null ) {
// fetch the thumbnail
val thumbnailFuture = user.fetchThumbnailAsync()
thumbnailFuture.addDoneListener {
val itemThumbnailData = thumbnailFuture. get ()
if (itemThumbnailData != null && itemThumbnailData.isNotEmpty()) {
// create Bitmap to use as required
val itemThumbnail =
BitmapFactory.decodeByteArray(
itemThumbnailData,
0 ,
itemThumbnailData.size
)
// set the Bitmap to the ImageView
userImage.setImageBitmap(itemThumbnail)
}
}
} else {
Toast.makeText(
this ,
"No thumbnail associated with $fullname " ,
Toast.LENGTH_LONG
)
.show()
}
} else {
Toast.makeText(
this ,
"User did not authenticate against $portalInfoName " ,
Toast.LENGTH_LONG
).show()
}
}
LoadStatus.FAILED_TO_LOAD -> {
Toast.makeText( this , "Portal failed to load" , Toast.LENGTH_LONG).show()
}
}
}
portal.loadAsync()
}
}