From 3cb3162beb06ef4bcfb8d268fa3970010480789a Mon Sep 17 00:00:00 2001 From: shabinder Date: Fri, 17 Jul 2020 20:36:21 +0530 Subject: [PATCH] Basic Functionality Added and Started Understanding how Spotify Api works --- .idea/dictionaries/shabinder.xml | 1 + app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 13 +++ .../musicForEveryone/MainActivity.kt | 96 +++++++++++++++++-- .../musicForEveryone/MainViewModel.kt | 15 +++ .../fragments/MainFragment.kt | 52 ++++------ .../fragments/MainViewModel.kt | 7 -- app/src/main/res/layout/main_activity.xml | 1 + app/src/main/res/layout/main_fragment.xml | 35 +++---- app/src/main/res/values/strings.xml | 2 +- 10 files changed, 158 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/com/shabinder/musicForEveryone/MainViewModel.kt delete mode 100644 app/src/main/java/com/shabinder/musicForEveryone/fragments/MainViewModel.kt diff --git a/.idea/dictionaries/shabinder.xml b/.idea/dictionaries/shabinder.xml index ef30233c..53539b4a 100644 --- a/.idea/dictionaries/shabinder.xml +++ b/.idea/dictionaries/shabinder.xml @@ -1,6 +1,7 @@ + musicforeveryone shabinder spotify spotifydownloader diff --git a/app/build.gradle b/app/build.gradle index 8c485cc5..27e9bf91 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.0' implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.browser:browser:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.lifecycle:lifecycle-extensions: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.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.squareup.okhttp3:okhttp:4.8.0' + implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8b7fe840..b53b56ca 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,14 @@ + + + + + + + + + @@ -16,6 +25,10 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt b/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt index 09862348..55d3aa2f 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt @@ -1,18 +1,100 @@ package com.shabinder.musicForEveryone +import android.Manifest +import android.content.Intent +import android.os.Build import android.os.Bundle +import android.os.StrictMode +import android.util.Log 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() { - + 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?) { super.onCreate(savedInstanceState) - setContentView(R.layout.main_activity) - if (savedInstanceState == null) { - supportFragmentManager.beginTransaction() - .replace(R.id.container, MainFragment.newInstance()) - .commitNow() + binding = DataBindingUtil.setContentView(this,R.layout.main_activity) + + mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java) + val policy = + 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 + ) + } + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/musicForEveryone/MainViewModel.kt b/app/src/main/java/com/shabinder/musicForEveryone/MainViewModel.kt new file mode 100644 index 00000000..7c8aa62c --- /dev/null +++ b/app/src/main/java/com/shabinder/musicForEveryone/MainViewModel.kt @@ -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().apply { value = 0 } + var userName = MutableLiveData().apply { value = "Placeholder" } + + + var spotify :SpotifyService? = null + +} \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt b/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt index 7db39dca..78a9f5a4 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt @@ -4,56 +4,36 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment -import androidx.lifecycle.ViewModelProviders -import com.shabinder.musicForEveryone.MainActivity +import androidx.lifecycle.ViewModelProvider +import com.shabinder.musicForEveryone.MainViewModel import com.shabinder.musicForEveryone.R -import com.shabinder.musicForEveryone.ui.main.MainViewModel +import com.shabinder.musicForEveryone.databinding.MainFragmentBinding import kaaes.spotify.webapi.android.SpotifyService + class MainFragment : Fragment() { - - 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 + lateinit var binding:MainFragmentBinding + private lateinit var mainViewModel: MainViewModel + var spotify : SpotifyService? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - - if(spotify==null){ - authenticateSpotify() - } - - requestPermission() - - return inflater.inflate(R.layout.main_fragment, container, false) + binding = DataBindingUtil.inflate(inflater,R.layout.main_fragment,container,false) + return binding.root } - 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?) { super.onActivityCreated(savedInstanceState) - viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) - // TODO: Use the ViewModel + mainViewModel = ViewModelProvider(this.requireActivity()).get(MainViewModel::class.java) + spotify = mainViewModel.spotify + mainViewModel.userName.observeForever { + binding.message.text = it + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainViewModel.kt b/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainViewModel.kt deleted file mode 100644 index 944f13ba..00000000 --- a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainViewModel.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.shabinder.musicForEveryone.ui.main - -import androidx.lifecycle.ViewModel - -class MainViewModel : ViewModel() { - // TODO: Implement the ViewModel -} \ No newline at end of file diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index 812f19cc..1bce3398 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -4,6 +4,7 @@ xmlns:tools="http://schemas.android.com/tools"> diff --git a/app/src/main/res/layout/main_fragment.xml b/app/src/main/res/layout/main_fragment.xml index d3e703bb..ca69c1b6 100644 --- a/app/src/main/res/layout/main_fragment.xml +++ b/app/src/main/res/layout/main_fragment.xml @@ -1,20 +1,23 @@ - + xmlns:tools="http://schemas.android.com/tools"> + - + - \ No newline at end of file + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cbee0958..db2ef265 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - spotifyDownloader + musicForEveryone \ No newline at end of file