Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to Kotlin #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 25
@@ -36,6 +37,8 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
compile "com.android.support:appcompat-v7:$support_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.anko:anko-common:$anko_version"
testCompile 'junit:junit:4.12'
}

This file was deleted.

@@ -0,0 +1,102 @@
package com.henrysnoek.spotifyandroidsdkexample

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log

import com.spotify.sdk.android.authentication.AuthenticationClient
import com.spotify.sdk.android.authentication.AuthenticationRequest
import com.spotify.sdk.android.authentication.AuthenticationResponse
import com.spotify.sdk.android.player.Config
import com.spotify.sdk.android.player.ConnectionStateCallback
import com.spotify.sdk.android.player.Error
import com.spotify.sdk.android.player.Player
import com.spotify.sdk.android.player.PlayerEvent
import com.spotify.sdk.android.player.Spotify
import com.spotify.sdk.android.player.SpotifyPlayer

class MainActivity : Activity(), Player.NotificationCallback, ConnectionStateCallback {

private var mPlayer: Player? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val builder = AuthenticationRequest.Builder(CLIENT_ID,
AuthenticationResponse.Type.TOKEN,
REDIRECT_URI)
builder.setScopes(arrayOf("user-read-private", "streaming"))
val request = builder.build()

AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {
super.onActivityResult(requestCode, resultCode, intent)

// Check if result comes from the correct activity
if (requestCode == REQUEST_CODE) {
val response = AuthenticationClient.getResponse(resultCode, intent)
if (response.type == AuthenticationResponse.Type.TOKEN) {
val playerConfig = Config(this, response.accessToken, CLIENT_ID)
Spotify.getPlayer(playerConfig, this, object : SpotifyPlayer.InitializationObserver {
override fun onInitialized(spotifyPlayer: SpotifyPlayer) {
mPlayer = spotifyPlayer
mPlayer!!.addConnectionStateCallback(this@MainActivity)
mPlayer!!.addNotificationCallback(this@MainActivity)
}

override fun onError(throwable: Throwable) {
Log.e("MainActivity", "Could not initialize player: " + throwable.message)
}
})
}
}
}

override fun onDestroy() {
Spotify.destroyPlayer(this)
super.onDestroy()
}

override fun onPlaybackEvent(playerEvent: PlayerEvent) {
Log.d("MainActivity", "Playback event received: " + playerEvent.name)
}

override fun onPlaybackError(error: Error) {
Log.d("MainActivity", "Playback error received: " + error.name)
}

override fun onLoggedIn() {
Log.d("MainActivity", "User logged in")

mPlayer!!.playUri(null, "spotify:track:6JEK0CvvjDjjMUBFoXShNZ", 0, 0)
}

override fun onLoggedOut() {
Log.d("MainActivity", "User logged out")
}

override fun onLoginFailed(error: Error) {
Log.d("MainActivity", "Login failed")
}

override fun onTemporaryError() {
Log.d("MainActivity", "Temporary error occurred")
}

override fun onConnectionMessage(message: String) {
Log.d("MainActivity", "Received connection message: " + message)
}

companion object {
private val CLIENT_ID = BuildConfig.CLIENT_ID
private val REDIRECT_URI = BuildConfig.REDIRECT_URI

// Request code that will be used to verify if the result comes from correct activity
// Can be any integer
private val REQUEST_CODE = 1337
}
}
@@ -1,11 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.support_version = '25.2.0'
ext.kotlin_version = '1.0.6'
ext.anko_version = '0.9.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files