|
|
|
@ -2,7 +2,6 @@ package de.minimalme.spotify_sdk |
|
|
|
|
|
|
|
import android.app.Activity |
|
|
|
import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.util.Log |
|
|
|
import com.spotify.android.appremote.api.ConnectionParams |
|
|
|
import com.spotify.android.appremote.api.Connector |
|
|
|
@ -20,46 +19,33 @@ import com.spotify.android.appremote.api.error.UserNotAuthorizedException |
|
|
|
import com.spotify.sdk.android.auth.AuthorizationClient |
|
|
|
import com.spotify.sdk.android.auth.AuthorizationRequest |
|
|
|
import com.spotify.sdk.android.auth.AuthorizationResponse |
|
|
|
import de.minimalme.spotify_sdk.subscriptions.CapabilitiesChannel |
|
|
|
import de.minimalme.spotify_sdk.subscriptions.ConnectionStatusChannel |
|
|
|
import de.minimalme.spotify_sdk.subscriptions.PlayerContextChannel |
|
|
|
import de.minimalme.spotify_sdk.subscriptions.PlayerStateChannel |
|
|
|
import de.minimalme.spotify_sdk.subscriptions.UserStatusChannel |
|
|
|
import io.flutter.embedding.engine.plugins.FlutterPlugin |
|
|
|
import io.flutter.embedding.engine.plugins.activity.ActivityAware |
|
|
|
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding |
|
|
|
import io.flutter.plugin.common.EventChannel |
|
|
|
import io.flutter.plugin.common.MethodCall |
|
|
|
import io.flutter.plugin.common.MethodChannel |
|
|
|
import io.flutter.plugin.common.PluginRegistry |
|
|
|
import kotlinx.event.SetEvent |
|
|
|
import kotlinx.event.event |
|
|
|
class SpotifySdkService(private val context: Context? = null) { |
|
|
|
|
|
|
|
class SpotifySdkHelper(private val context: Context? = null) { |
|
|
|
|
|
|
|
// application context |
|
|
|
private var applicationContext : Context? = null |
|
|
|
private var applicationActivity : Activity? = null |
|
|
|
// method channel |
|
|
|
private lateinit var methodChannel : MethodChannel |
|
|
|
private val channelName = "spotify_sdk" |
|
|
|
private val loggingTag = "spotify_sdk" |
|
|
|
|
|
|
|
private val loggingTag = "spotify_sdk" |
|
|
|
|
|
|
|
private val errorConnecting = "errorConnecting" |
|
|
|
private val errorDisconnecting = "errorDisconnecting" |
|
|
|
private val errorConnection = "errorConnection" |
|
|
|
private val errorAuthenticationToken = "authenticationTokenError" |
|
|
|
|
|
|
|
private val requestCodeAuthentication = 1337 |
|
|
|
|
|
|
|
private var spotifyAppRemote: SpotifyAppRemote? = null |
|
|
|
private var playerApi: PlayerApi? = null |
|
|
|
|
|
|
|
//-- Method implementations |
|
|
|
private fun connectToSpotify(clientId: String?, redirectUrl: String?, result: MethodChannel.Result) { |
|
|
|
fun initialize(clientId: String, redirectUrl: String): Boolean { |
|
|
|
getAccessToken(clientId,redirectUrl,""); |
|
|
|
connectToSpotify(clientId,redirectUrl) |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public fun connectToSpotify(clientId: String?, redirectUrl: String?) { |
|
|
|
|
|
|
|
if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) { |
|
|
|
result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") |
|
|
|
// result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") |
|
|
|
} else { |
|
|
|
val connectionParams = ConnectionParams.Builder(clientId) |
|
|
|
.setRedirectUri(redirectUrl) |
|
|
|
@ -75,7 +61,7 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
|
|
|
|
|
initiallyConnected = true |
|
|
|
Log.i(loggingTag, "App Remote successfully connected") |
|
|
|
result.success(true) |
|
|
|
// result.success(true) |
|
|
|
} |
|
|
|
|
|
|
|
override fun onFailure(throwable: Throwable) { |
|
|
|
@ -155,25 +141,23 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
|
// ) |
|
|
|
} else { |
|
|
|
// throw exception as the connect method |
|
|
|
result.error(errorCode, errorMessage, errorDetails) |
|
|
|
// result.error(errorCode, errorMessage, errorDetails) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?, result: MethodChannel.Result) { |
|
|
|
public fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?) { |
|
|
|
if (applicationActivity == null) { |
|
|
|
throw IllegalStateException("getAccessToken needs a foreground activity") |
|
|
|
} |
|
|
|
|
|
|
|
if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) { |
|
|
|
result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") |
|
|
|
// result.error(errorConnecting, "client id or redirectUrl are not set or have invalid format", "") |
|
|
|
} else { |
|
|
|
//Convert String? scope to Array. Delimiter set as comma "," |
|
|
|
val scopeArray = scope?.split(",")?.toTypedArray() |
|
|
|
// methodConnectToSpotify.checkAndSetPendingOperation(result) |
|
|
|
|
|
|
|
val builder = AuthorizationRequest.Builder( |
|
|
|
clientId, |
|
|
|
AuthorizationResponse.Type.TOKEN, |
|
|
|
@ -190,38 +174,18 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun disconnectFromSpotify(result: MethodChannel.Result) { |
|
|
|
public fun disconnectFromSpotify() { |
|
|
|
if (spotifyAppRemote != null && spotifyAppRemote!!.isConnected) { |
|
|
|
SpotifyAppRemote.disconnect(spotifyAppRemote) |
|
|
|
|
|
|
|
// emit connection terminated event |
|
|
|
// connStatusEventChannel( |
|
|
|
// ConnectionStatusChannel.ConnectionEvent( |
|
|
|
// false, |
|
|
|
// "Successfully disconnected from Spotify.", |
|
|
|
// null, |
|
|
|
// null |
|
|
|
// ) |
|
|
|
// ) |
|
|
|
// method success |
|
|
|
result.success(true) |
|
|
|
// result.success(true) |
|
|
|
} else if (!spotifyAppRemote!!.isConnected) { |
|
|
|
result.error(errorDisconnecting, "could not disconnect spotify remote", "you are not connected, no need to disconnect") |
|
|
|
// result.error(errorDisconnecting, "could not disconnect spotify remote", "you are not connected, no need to disconnect") |
|
|
|
} else { |
|
|
|
result.error(errorDisconnecting, "could not disconnect spotify remote", "spotifyAppRemote is not set") |
|
|
|
// result.error(errorDisconnecting, "could not disconnect spotify remote", "spotifyAppRemote is not set") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun play(spotifyUri: String?){ |
|
|
|
// if (playerApi != null && !spotifyUri.isNullOrBlank()) { |
|
|
|
// playerApi.play(spotifyUri) |
|
|
|
// .setResultCallback { result.success(true) } |
|
|
|
// .setErrorCallback { throwable -> result.error(errorPlay, "error when playing uri: $spotifyUri", throwable.toString()) } |
|
|
|
// } else if (spotifyUri.isNullOrBlank()) { |
|
|
|
// result.error(errorPlay, "spotifyUri has invalid format or is not set", "") |
|
|
|
// } else { |
|
|
|
// spotifyRemoteAppNotSetError() |
|
|
|
// } |
|
|
|
public fun play(spotifyUri: String?){ |
|
|
|
if (playerApi != null && !spotifyUri.isNullOrBlank()) { |
|
|
|
playerApi!!.play(spotifyUri) |
|
|
|
} else if (spotifyUri.isNullOrBlank()) { |
|
|
|
@ -230,14 +194,8 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
private fun pause(){ |
|
|
|
// if (playerApi != null) { |
|
|
|
// playerApi.pause() |
|
|
|
// .setResultCallback { result.success(true) } |
|
|
|
// .setErrorCallback { throwable -> result.error(errorPause, "error when pausing", throwable.toString()) } |
|
|
|
// } else { |
|
|
|
// spotifyRemoteAppNotSetError() |
|
|
|
// } |
|
|
|
|
|
|
|
public fun pause(){ |
|
|
|
if (playerApi != null) { |
|
|
|
playerApi!!.pause() |
|
|
|
.setResultCallback {} |