@@ -11,16 +11,50 @@ import com.adamratzman.spotify.spotifyImplicitGrantApi
1111import com.spotify.sdk.android.auth.AuthorizationClient
1212import com.spotify.sdk.android.auth.AuthorizationRequest
1313import com.spotify.sdk.android.auth.AuthorizationResponse
14+ import com.spotify.sdk.android.auth.LoginActivity
1415
16+ /* *
17+ * Wrapper around spotify-auth's [LoginActivity] that allows configuration of the authentication process, along with
18+ * callbacks on successful and failed authentication. Pair this with [SpotifyDefaultCredentialStore] to easily store credentials.
19+ *
20+ * @property state The state to use to verify the login request.
21+ * @property clientId Your application's Spotify client id.
22+ * @property clientId Your application's Spotify client secret.
23+ * @property redirectUri Your application's Spotify redirect id - NOTE that this should be an android scheme (such as spotifyapp://authback)
24+ * and that this must be registered in your manifest.
25+ * @property useDefaultRedirectHandler Disable if you will not be using [useDefaultRedirectHandler] but will be setting [SpotifyDefaultAuthHelper.activityBack].
26+ *
27+ */
1528public abstract class AbstractSpotifyLoginActivity : Activity () {
1629 public abstract val state: Int
1730 public abstract val clientId: String
1831 public abstract val redirectUri: String
19- public abstract val useDefaultRedirectHandler: Boolean
32+ public open val useDefaultRedirectHandler: Boolean = true
33+
34+ /* *
35+ * Return the scopes that you are going to request from the user here.
36+ */
2037 public abstract fun getRequestingScopes (): List <SpotifyScope >
2138
39+ /* *
40+ * Override this to define what to do after authentication has been successfully completed. A valid, usable
41+ * [spotifyApi] is provided to you. You may likely want to use [SpotifyDefaultCredentialStore] to store/retrieve this token.
42+ *
43+ * @param spotifyApi Valid, usable [SpotifyImplicitGrantApi] that you can use to make requests.
44+ */
2245 public abstract fun onSuccessfulAuthentication (spotifyApi : SpotifyImplicitGrantApi )
46+
47+ /* *
48+ * Override this to define what to do after authentication has failed. You may want to use [SpotifyDefaultCredentialStore] to remove any stored token.
49+ */
2350 public abstract fun onAuthenticationFailed (errorMessage : String )
51+
52+
53+ /* *
54+ * Override this to define what to do after [onSuccessfulAuthentication] has run.
55+ * The default behavior is to finish the activity, and redirect the user back to the activity set on [SpotifyDefaultAuthHelper.activityBack]
56+ * only if [guardValidSpotifyApi] has been used or if [SpotifyDefaultAuthHelper.activityBack] has been set.
57+ */
2458 public open fun redirectAfterOnSuccessAuthentication () {
2559 if (useDefaultRedirectHandler && SpotifyDefaultAuthHelper .activityBack != null ) {
2660 startActivity(Intent (this , SpotifyDefaultAuthHelper .activityBack))
@@ -35,15 +69,21 @@ public abstract class AbstractSpotifyLoginActivity : Activity() {
3569 triggerLoginActivity()
3670 }
3771
72+ /* *
73+ * Trigger the actual spotify-auth login activity to authenticate the user.
74+ */
3875 public fun triggerLoginActivity () {
39- val authorizationRequest =
40- AuthorizationRequest . Builder (clientId, AuthorizationResponse . Type . TOKEN , redirectUri )
41- .setScopes(getRequestingScopes().map { it.uri }.toTypedArray ())
42- .setState(state.toString() )
43- .build()
76+ val authorizationRequest = AuthorizationRequest . Builder (clientId, AuthorizationResponse . Type . TOKEN , redirectUri)
77+ .setScopes(getRequestingScopes().map { it.uri }.toTypedArray() )
78+ .setState(state.toString ())
79+ .build( )
80+
4481 AuthorizationClient .openLoginActivity(this , state, authorizationRequest)
4582 }
4683
84+ /* *
85+ * Processes the result of [LoginActivity], invokes callbacks, then finishes.
86+ */
4787 override fun onActivityResult (requestCode : Int , resultCode : Int , intent : Intent ? ) {
4888 super .onActivityResult(requestCode, resultCode, intent)
4989 if (requestCode == state) {
@@ -58,9 +98,7 @@ public abstract class AbstractSpotifyLoginActivity : Activity() {
5898 val api = spotifyImplicitGrantApi(
5999 clientId = clientId,
60100 token = token
61- ) {
62- refreshTokenProducer = { throw SpotifyException .ReAuthenticationNeededException () }
63- }
101+ )
64102
65103 onSuccessfulAuthentication(api)
66104 redirectAfterOnSuccessAuthentication()
0 commit comments