Basic Functionality Added and Started Understanding how Spotify Api works

This commit is contained in:
shabinder 2020-07-17 20:36:21 +05:30
parent d2faca10fb
commit 3cb3162beb
10 changed files with 158 additions and 68 deletions

View File

@ -1,6 +1,7 @@
<component name="ProjectDictionaryState"> <component name="ProjectDictionaryState">
<dictionary name="shabinder"> <dictionary name="shabinder">
<words> <words>
<w>musicforeveryone</w>
<w>shabinder</w> <w>shabinder</w>
<w>spotify</w> <w>spotify</w>
<w>spotifydownloader</w> <w>spotifydownloader</w>

View File

@ -37,6 +37,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0' implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.browser:browser:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
@ -57,10 +58,11 @@ dependencies {
implementation 'com.google.apis:google-api-services-youtube:v3-rev180-1.22.0' implementation 'com.google.apis:google-api-services-youtube:v3-rev180-1.22.0'
implementation 'com.google.oauth-client:google-oauth-client:1.22.0' implementation 'com.google.oauth-client:google-oauth-client:1.22.0'
implementation 'com.spotify.android:auth:1.2.3' implementation 'com.spotify.android:auth:1.1.0'
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.okhttp3:okhttp:4.8.0' implementation 'com.squareup.okhttp3:okhttp:4.8.0'
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

View File

@ -2,6 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shabinder.musicForEveryone"> package="com.shabinder.musicForEveryone">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
@ -9,6 +17,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name="com.shabinder.musicForEveryone.MainActivity"> <activity android:name="com.shabinder.musicForEveryone.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@ -16,6 +25,10 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name="com.spotify.sdk.android.authentication.LoginActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application> </application>
</manifest> </manifest>

View File

@ -1,18 +1,100 @@
package com.shabinder.musicForEveryone package com.shabinder.musicForEveryone
import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.StrictMode
import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.shabinder.musicForEveryone.fragments.MainFragment import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.shabinder.musicForEveryone.databinding.MainActivityBinding
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.authentication.LoginActivity
import kaaes.spotify.webapi.android.SpotifyApi
import kaaes.spotify.webapi.android.SpotifyService
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var binding: MainActivityBinding
val REDIRECT_URI = "musicforeveryone://callback"
val CLIENT_ID:String = "694d8bf4f6ec420fa66ea7fb4c68f89d"
// val musicDirectory = File(this.filesDir?.absolutePath + "/Music/")
var message :String =""
var token :String =""
var spotify: SpotifyService? = null
lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity) binding = DataBindingUtil.setContentView(this,R.layout.main_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction() mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
.replace(R.id.container, MainFragment.newInstance()) val policy =
.commitNow() StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
//TODO Use Coroutines
if(spotify==null){
authenticateSpotify()
}
requestPermission()
}
private fun authenticateSpotify() {
val builder = AuthenticationRequest.Builder(CLIENT_ID,AuthenticationResponse.Type.TOKEN,REDIRECT_URI)
.setShowDialog(true)
.setScopes(arrayOf("user-read-private","streaming","user-read-email","user-modify-playback-state","user-top-read","user-library-modify","user-read-currently-playing","user-library-read","user-read-recently-played"))
val request: AuthenticationRequest = builder.build()
AuthenticationClient.openLoginActivity(this, LoginActivity.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 == LoginActivity.REQUEST_CODE) {
val response = AuthenticationClient.getResponse(resultCode, intent)
when (response.type) {
AuthenticationResponse.Type.TOKEN -> {
Log.i("Network",response.accessToken.toString())
token = response.accessToken
val api = SpotifyApi()
api.setAccessToken(token)
spotify = api.service
mainViewModel.spotify = api.service
//Initiate Processes In Main Fragment
val me = spotify?.me?.display_name
mainViewModel.userName.value = me
Log.i("Network",me!!)
}
AuthenticationResponse.Type.ERROR -> {
Log.i("Network",response.error.toString())
}
else -> {
} }
} }
}
}
private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
786
)
}
}
} }

View File

@ -0,0 +1,15 @@
package com.shabinder.musicForEveryone
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kaaes.spotify.webapi.android.SpotifyService
class MainViewModel : ViewModel() {
var apiCame = MutableLiveData<Int>().apply { value = 0 }
var userName = MutableLiveData<String>().apply { value = "Placeholder" }
var spotify :SpotifyService? = null
}

View File

@ -4,56 +4,36 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProvider
import com.shabinder.musicForEveryone.MainActivity import com.shabinder.musicForEveryone.MainViewModel
import com.shabinder.musicForEveryone.R import com.shabinder.musicForEveryone.R
import com.shabinder.musicForEveryone.ui.main.MainViewModel import com.shabinder.musicForEveryone.databinding.MainFragmentBinding
import kaaes.spotify.webapi.android.SpotifyService import kaaes.spotify.webapi.android.SpotifyService
class MainFragment : Fragment() { class MainFragment : Fragment() {
lateinit var binding:MainFragmentBinding
var spotify: SpotifyService? = null private lateinit var mainViewModel: MainViewModel
var spotify : SpotifyService? = null
companion object {
fun newInstance() = MainFragment()
const val CLIENT_ID:String = "4fe3fecfe5334023a1472516cc99d805"
const val spotify_client_secret:String = "0f02b7c483c04257984695007a4a8d5c"
}
private lateinit var viewModel: MainViewModel
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
binding = DataBindingUtil.inflate(inflater,R.layout.main_fragment,container,false)
if(spotify==null){ return binding.root
authenticateSpotify()
} }
requestPermission()
return inflater.inflate(R.layout.main_fragment, container, false)
}
fun authenticateSpotify() {
val builder: AuthenticationRequest.Builder = Builder(
CLIENT_ID,
AuthenticationResponse.Type.TOKEN,
MainActivity.REDIRECT_URI
)
builder.setScopes(arrayOf("user-read-private", "streaming"))
val request: AuthenticationRequest = builder.build()
AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request)
}
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) mainViewModel = ViewModelProvider(this.requireActivity()).get(MainViewModel::class.java)
// TODO: Use the ViewModel spotify = mainViewModel.spotify
mainViewModel.userName.observeForever {
binding.message.text = it
}
} }
} }

View File

@ -1,7 +0,0 @@
package com.shabinder.musicForEveryone.ui.main
import androidx.lifecycle.ViewModel
class MainViewModel : ViewModel() {
// TODO: Implement the ViewModel
}

View File

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/mainActivity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main" android:id="@+id/main"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -18,3 +20,4 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -1,3 +1,3 @@
<resources> <resources>
<string name="app_name">spotifyDownloader</string> <string name="app_name">musicForEveryone</string>
</resources> </resources>