|
|
@ -2,7 +2,6 @@ package de.minimalme.spotify_sdk |
|
|
|
|
|
|
|
|
import android.app.Activity |
|
|
import android.app.Activity |
|
|
import android.content.Context |
|
|
import android.content.Context |
|
|
import android.content.Intent |
|
|
|
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
import com.spotify.android.appremote.api.ConnectionParams |
|
|
import com.spotify.android.appremote.api.ConnectionParams |
|
|
import com.spotify.android.appremote.api.Connector |
|
|
import com.spotify.android.appremote.api.Connector |
|
|
@ -20,45 +19,33 @@ import com.spotify.android.appremote.api.error.UserNotAuthorizedException |
|
|
import com.spotify.sdk.android.auth.AuthorizationClient |
|
|
import com.spotify.sdk.android.auth.AuthorizationClient |
|
|
import com.spotify.sdk.android.auth.AuthorizationRequest |
|
|
import com.spotify.sdk.android.auth.AuthorizationRequest |
|
|
import com.spotify.sdk.android.auth.AuthorizationResponse |
|
|
import com.spotify.sdk.android.auth.AuthorizationResponse |
|
|
import de.minimalme.spotify_sdk.subscriptions.CapabilitiesChannel |
|
|
|
|
|
import de.minimalme.spotify_sdk.subscriptions.ConnectionStatusChannel |
|
|
class SpotifySdkHelper(private val context: Context? = null) { |
|
|
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) { |
|
|
|
|
|
|
|
|
|
|
|
// application context |
|
|
// application context |
|
|
private var applicationContext : Context? = null |
|
|
private var applicationContext : Context? = null |
|
|
private var applicationActivity : Activity? = null |
|
|
private var applicationActivity : Activity? = null |
|
|
// method channel |
|
|
// 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 errorConnection = "errorConnection" |
|
|
private val errorAuthenticationToken = "authenticationTokenError" |
|
|
|
|
|
|
|
|
|
|
|
private val requestCodeAuthentication = 1337 |
|
|
private val requestCodeAuthentication = 1337 |
|
|
|
|
|
|
|
|
private var spotifyAppRemote: SpotifyAppRemote? = null |
|
|
private var spotifyAppRemote: SpotifyAppRemote? = null |
|
|
private var playerApi: PlayerApi? = null |
|
|
private var playerApi: PlayerApi? = null |
|
|
|
|
|
|
|
|
public 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()) { |
|
|
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 { |
|
|
} else { |
|
|
val connectionParams = ConnectionParams.Builder(clientId) |
|
|
val connectionParams = ConnectionParams.Builder(clientId) |
|
|
.setRedirectUri(redirectUrl) |
|
|
.setRedirectUri(redirectUrl) |
|
|
@ -74,7 +61,7 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
|
|
|
|
|
|
initiallyConnected = true |
|
|
initiallyConnected = true |
|
|
Log.i(loggingTag, "App Remote successfully connected") |
|
|
Log.i(loggingTag, "App Remote successfully connected") |
|
|
result.success(true) |
|
|
// result.success(true) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onFailure(throwable: Throwable) { |
|
|
override fun onFailure(throwable: Throwable) { |
|
|
@ -154,20 +141,20 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
// ) |
|
|
// ) |
|
|
} else { |
|
|
} else { |
|
|
// throw exception as the connect method |
|
|
// throw exception as the connect method |
|
|
result.error(errorCode, errorMessage, errorDetails) |
|
|
// result.error(errorCode, errorMessage, errorDetails) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?, result: MethodChannel.Result) { |
|
|
public fun getAccessToken(clientId: String?, redirectUrl: String?, scope: String?) { |
|
|
if (applicationActivity == null) { |
|
|
if (applicationActivity == null) { |
|
|
throw IllegalStateException("getAccessToken needs a foreground activity") |
|
|
throw IllegalStateException("getAccessToken needs a foreground activity") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (clientId.isNullOrBlank() || redirectUrl.isNullOrBlank()) { |
|
|
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 { |
|
|
} else { |
|
|
//Convert String? scope to Array. Delimiter set as comma "," |
|
|
//Convert String? scope to Array. Delimiter set as comma "," |
|
|
val scopeArray = scope?.split(",")?.toTypedArray() |
|
|
val scopeArray = scope?.split(",")?.toTypedArray() |
|
|
@ -187,14 +174,14 @@ class SpotifySdkService(private val context: Context? = null) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public fun disconnectFromSpotify(result: MethodChannel.Result) { |
|
|
public fun disconnectFromSpotify() { |
|
|
if (spotifyAppRemote != null && spotifyAppRemote!!.isConnected) { |
|
|
if (spotifyAppRemote != null && spotifyAppRemote!!.isConnected) { |
|
|
SpotifyAppRemote.disconnect(spotifyAppRemote) |
|
|
SpotifyAppRemote.disconnect(spotifyAppRemote) |
|
|
result.success(true) |
|
|
// result.success(true) |
|
|
} else if (!spotifyAppRemote!!.isConnected) { |
|
|
} 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 { |
|
|
} 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") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|