-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Aug 14, 2019
1 parent
1012e02
commit 51b4d8a
Showing
22 changed files
with
599 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/masoudss/activity/SelectAudioActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.masoudss.activity | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.database.Cursor | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
import kotlinx.android.synthetic.main.activity_select_audio.* | ||
import android.provider.MediaStore | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.masoudss.model.AudioModel | ||
import com.masoudss.R | ||
import com.masoudss.adapter.AudioAdapter | ||
import org.jetbrains.anko.doAsync | ||
import org.jetbrains.anko.uiThread | ||
|
||
|
||
class SelectAudioActivity : AppCompatActivity() { | ||
|
||
private val audioList = ArrayList<AudioModel>() | ||
private val projection = arrayOf( | ||
MediaStore.Audio.Media._ID, | ||
MediaStore.Audio.Media.ARTIST, | ||
MediaStore.Audio.Media.TITLE, | ||
MediaStore.Audio.Media.DATA) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_select_audio) | ||
initViews() | ||
loadAudioFiles() | ||
} | ||
|
||
private fun initViews(){ | ||
audioRecyclerView.layoutManager = LinearLayoutManager(this) | ||
audioRecyclerView.adapter = AudioAdapter(this@SelectAudioActivity, audioList) | ||
} | ||
|
||
private fun loadAudioFiles() { | ||
|
||
doAsync { | ||
|
||
var cursor: Cursor? = null | ||
try { | ||
cursor = contentResolver.query( | ||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, | ||
projection, | ||
null, | ||
null, | ||
MediaStore.Audio.Media.DATE_ADDED + " DESC") | ||
|
||
while (cursor!!.moveToNext()) { | ||
val audioModel = AudioModel() | ||
audioModel.artist = cursor.getString(1) | ||
audioModel.title = cursor.getString(2) | ||
audioModel.path = cursor.getString(3) | ||
audioList.add(audioModel) | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} finally { | ||
cursor?.close() | ||
} | ||
|
||
uiThread { | ||
audioRecyclerView.adapter?.notifyDataSetChanged() | ||
} | ||
} | ||
} | ||
|
||
fun onSelectAudio(audioModel: AudioModel){ | ||
val intent = Intent() | ||
intent.putExtra("path",audioModel.path) | ||
setResult(Activity.RESULT_OK,intent) | ||
finish() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.masoudss.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.masoudss.R | ||
import com.masoudss.activity.SelectAudioActivity | ||
import com.masoudss.model.AudioModel | ||
import kotlinx.android.synthetic.main.item_audio.view.* | ||
|
||
class AudioAdapter(private val activity: SelectAudioActivity,private val audioList : ArrayList<AudioModel>) : | ||
RecyclerView.Adapter<AudioAdapter.AudioViewHolder>() { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AudioViewHolder { | ||
return AudioViewHolder(LayoutInflater.from(activity).inflate(R.layout.item_audio,parent,false)) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return audioList.size | ||
} | ||
|
||
override fun onBindViewHolder(holder: AudioViewHolder, position: Int) { | ||
holder.itemView.title.text = "${audioList[position].title}\n${audioList[position].artist}".trim() | ||
} | ||
|
||
inner class AudioViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
|
||
init { | ||
view.setOnClickListener { | ||
activity.onSelectAudio(audioList[adapterPosition]) | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.masoudss.model | ||
|
||
class AudioModel { | ||
|
||
var title = "" | ||
|
||
var artist = "" | ||
|
||
var path = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FFFFFF" | ||
android:pathData="M12,1c-4.97,0 -9,4.03 -9,9v7c0,1.66 1.34,3 3,3h3v-8H5v-2c0,-3.87 3.13,-7 7,-7s7,3.13 7,7v2h-4v8h3c1.66,0 3,-1.34 3,-3v-7c0,-4.97 -4.03,-9 -9,-9z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FFFFFF" | ||
android:pathData="M19,3L4.99,3c-1.11,0 -1.98,0.9 -1.98,2L3,19c0,1.1 0.88,2 1.99,2L19,21c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM19,15h-4c0,1.66 -1.35,3 -3,3s-3,-1.34 -3,-3L4.99,15L4.99,5L19,5v10zM16,10h-2L14,7h-4v3L8,10l4,4 4,-4z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".activity.SelectAudioActivity"> | ||
|
||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/audioRecyclerView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
|
||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
Oops, something went wrong.