Implemented permission to prompt the user to allow the app to read from the external storage #30
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package com.example.mear.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
|
||||
import java.lang.Exception
|
||||
import java.lang.Runnable
|
||||
@@ -40,6 +44,7 @@ class MainActivity : BaseServiceActivity() {
|
||||
setContentView(R.layout.activity_main)
|
||||
setSupportActionBar(toolbar)
|
||||
|
||||
permissionPrompt()
|
||||
|
||||
initialize()
|
||||
}
|
||||
@@ -127,8 +132,7 @@ class MainActivity : BaseServiceActivity() {
|
||||
startActivity(Intent(this, SongViewActivity::class.java))
|
||||
}
|
||||
SettingsLink.setOnClickListener {
|
||||
val intent = Intent(this, SettingsActivity::class.java)
|
||||
startActivity(intent)
|
||||
startActivity(Intent(this, SettingsActivity::class.java))
|
||||
}
|
||||
}
|
||||
private fun initializeServices() {
|
||||
@@ -261,15 +265,6 @@ class MainActivity : BaseServiceActivity() {
|
||||
if (coverExt.hasCover()) {
|
||||
trackCover = coverExt.retrieveCover()
|
||||
}
|
||||
/**
|
||||
val mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(currTrack.songPath)
|
||||
|
||||
if (mmr.embeddedPicture != null) {
|
||||
trackCover = mmr.embeddedPicture
|
||||
}
|
||||
*/
|
||||
|
||||
resetControls()
|
||||
|
||||
TrackTitle.text = trackTitle
|
||||
@@ -310,7 +305,23 @@ class MainActivity : BaseServiceActivity() {
|
||||
PlayTrack.setColorFilter(Color.GREEN)
|
||||
}
|
||||
}
|
||||
private fun permissionPrompt() {
|
||||
if (ContextCompat.checkSelfPermission(this,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||
}
|
||||
else {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 0)
|
||||
}
|
||||
}
|
||||
else {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private var musicTrackTimeUpdateTask = object: Runnable {
|
||||
override fun run() {
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.example.mear.activities
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.support.annotation.RequiresApi
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.view.View
|
||||
|
||||
@@ -11,23 +10,13 @@ import java.lang.Exception
|
||||
import kotlinx.android.synthetic.main.activity_song_view.*
|
||||
import kotlinx.android.synthetic.main.content_song_view.*
|
||||
|
||||
import com.example.mear.activities.BaseServiceActivity
|
||||
import com.example.mear.adapters.RecyclerAdapter
|
||||
import com.example.mear.models.TrackItems
|
||||
import com.example.mear.R
|
||||
import com.example.mear.repositories.TrackRepository
|
||||
import com.example.mear.util.ExtractCover
|
||||
import org.jetbrains.anko.act
|
||||
|
||||
class SongViewActivity : BaseServiceActivity() {
|
||||
|
||||
/**
|
||||
override fun onItemClick(position: Int) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
val sds = "ddddd"
|
||||
}
|
||||
*/
|
||||
|
||||
private var trackListItems = arrayListOf<TrackItems>()
|
||||
private lateinit var adapter: RecyclerAdapter
|
||||
private lateinit var linearLayoutManager: LinearLayoutManager
|
||||
@@ -55,7 +44,6 @@ class SongViewActivity : BaseServiceActivity() {
|
||||
if (trackListItems.size == 0) {
|
||||
val df = ""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -68,10 +56,6 @@ class SongViewActivity : BaseServiceActivity() {
|
||||
|
||||
trackListItems = retrieveTrackItems()
|
||||
|
||||
|
||||
|
||||
//adapter = RecyclerAdapter(trackListItems)
|
||||
//adapter = RecyclerAdapter(this, trackListItems)
|
||||
adapter = RecyclerAdapter({trackItem: TrackItems -> playTrack(trackItem)}, trackListItems)
|
||||
adapter.configureActivity(this)
|
||||
trackList.adapter = adapter
|
||||
@@ -80,8 +64,8 @@ class SongViewActivity : BaseServiceActivity() {
|
||||
}
|
||||
trackList.setHasFixedSize(true)
|
||||
trackList.setItemViewCacheSize(20);
|
||||
//trackList.setDrawingCacheEnabled(true);
|
||||
//trackList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
|
||||
trackList.setDrawingCacheEnabled(true);
|
||||
trackList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
@@ -96,48 +80,8 @@ class SongViewActivity : BaseServiceActivity() {
|
||||
|
||||
val tracks = TrackRepository(this).getAll()
|
||||
|
||||
var index = 0
|
||||
val max = 200
|
||||
var done = false
|
||||
/**
|
||||
tracks.parallelStream().map {track ->
|
||||
val cover = ExtractCover(track.songPath)
|
||||
var trackCover = ByteArray(0)
|
||||
if (cover.hasCover() && !done) {
|
||||
if (index < max ) {
|
||||
trackCover = cover.retrieveCover()
|
||||
}
|
||||
else {
|
||||
done = true
|
||||
}
|
||||
index++
|
||||
}
|
||||
if (done) {
|
||||
//return trackItems
|
||||
}
|
||||
|
||||
val trackItem = TrackItems(track.id, track.title, track.artist, trackCover!!)
|
||||
trackItems.add(trackItem)
|
||||
|
||||
}
|
||||
*/
|
||||
for (track in tracks) {
|
||||
//val cover = ExtractCover(track.songPath)
|
||||
var trackCover = ByteArray(0)
|
||||
/**
|
||||
if (cover.hasCover() && !done) {
|
||||
if (index < max ) {
|
||||
trackCover = cover.retrieveCover()
|
||||
}
|
||||
else {
|
||||
done = true
|
||||
}
|
||||
index++
|
||||
}
|
||||
if (done) {
|
||||
return trackItems
|
||||
}
|
||||
*/
|
||||
val trackItem = TrackItems(track.id, track.title, track.artist, trackCover!!)
|
||||
trackItems.add(trackItem)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package com.example.mear.adapters
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@@ -17,63 +10,15 @@ import kotlinx.android.synthetic.main.fragment_song_view.view.*
|
||||
|
||||
import com.squareup.picasso.Picasso
|
||||
|
||||
import com.example.mear.activities.SongViewActivity
|
||||
import com.example.mear.constants.Filenames
|
||||
import com.example.mear.inflate
|
||||
import com.example.mear.models.TrackItems
|
||||
import com.example.mear.R
|
||||
import com.example.mear.playback.service.MusicService
|
||||
import com.example.mear.repositories.TrackRepository
|
||||
//import android.R
|
||||
|
||||
|
||||
|
||||
//class RecyclerAdapter(private val mOnClickListenerI: trackItemClickListener, private val trackItemsSourceInit: ArrayList<TrackItems>) :
|
||||
//class RecyclerAdapter(private val mOnClickListenerI: trackItemClickListener, private val trackItemsSourceInit: ArrayList<TrackItems>) :
|
||||
class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private val trackItemsSourceInit: ArrayList<TrackItems>) :
|
||||
RecyclerView.Adapter<RecyclerView.ViewHolder>()
|
||||
//Application.ActivityLifecycleCallbacks {
|
||||
{
|
||||
|
||||
//private val mOnClickListenerI: trackItemClickListener? = null
|
||||
private val mOnClick = mOnClickListenerI
|
||||
|
||||
/**
|
||||
override fun onActivityPaused(activity: Activity?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivityStarted(activity: Activity?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivityDestroyed(activity: Activity?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivityStopped(activity: Activity?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun onActivityResumed(activity: Activity?) {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
init {
|
||||
}
|
||||
|
||||
//var activityInst: Activity? = act
|
||||
//var musicService: MusicService? = null
|
||||
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
open interface trackItemClickListener{
|
||||
fun onClick(view: View)
|
||||
@@ -85,73 +30,26 @@ class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private va
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
val itemPhoto = trackItemsSourceInit!![position]
|
||||
//(holder as TrackItemsHolder).bindTrackItems(itemPhoto)
|
||||
(holder as TrackItemsHolder).bindTrackItems(itemPhoto, mOnClickListenerI)
|
||||
}
|
||||
|
||||
// override fun onCreateViewHolder(parent: ViewGroup, position: Int): TrackItemsHolder {
|
||||
override fun onCreateViewHolder(parent: ViewGroup, position: Int): RecyclerView.ViewHolder {
|
||||
val inflatedView = parent.inflate(R.layout.fragment_song_view, false)
|
||||
//inflatedView.setOnClickListener(parent.parent)
|
||||
return TrackItemsHolder(inflatedView)
|
||||
//return TrackItemsHolder(inflatedView, mOnClick)
|
||||
}
|
||||
|
||||
fun configureActivity(actvity: Activity) {
|
||||
//activityInst = actvity
|
||||
//activityInst!!.application.registerActivityLifecycleCallbacks(this)
|
||||
}
|
||||
|
||||
class TrackItemsHolder(v: View) : RecyclerView.ViewHolder(v) {
|
||||
|
||||
|
||||
|
||||
private var view: View = v
|
||||
//private var act = activity
|
||||
//private var musicService: MusicService? = null
|
||||
private var trackItem: TrackItems? = null
|
||||
private val imgWidth = 90
|
||||
private val imgHeight = 90
|
||||
private var mBound = false
|
||||
|
||||
|
||||
init {
|
||||
//v.setOnClickListener(this)
|
||||
}
|
||||
|
||||
//constructor(vi: View) : super(this)
|
||||
//constructor(vi: View) : super(this)
|
||||
|
||||
|
||||
/**
|
||||
private val connection = object : ServiceConnection {
|
||||
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||
val binder = service as MusicService.LocalBinder
|
||||
musicService = binder.service
|
||||
mBound = true
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||
mBound = false
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
override fun onClick(v: View) {
|
||||
try {
|
||||
//val context = itemView.context
|
||||
//val showPhotoIntent = Intent(context, SongViewActivity::class.java)
|
||||
//showPhotoIntent.putExtra(PHOTO_KEY, trackItem)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fun bindTrackItems(trackItems: TrackItems, clickList: (TrackItems) -> Unit) {
|
||||
try {
|
||||
@@ -172,7 +70,6 @@ class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private va
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val PHOTO_KEY = "PHOTO"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,14 +35,16 @@ class TrackManager(var allSongPath: MutableList<String>) {
|
||||
val track = Track(id, trackTitle, trackArtist, trackAlbum, trackLength,
|
||||
ByteArray(0), musicPath)
|
||||
dumpToDatabase(ctx, track)
|
||||
saveTrackCoverToDisk(ctx, id, mmr.embeddedPicture)
|
||||
if (mmr.embeddedPicture!=null) {
|
||||
saveTrackCoverToDisk(ctx, id, mmr.embeddedPicture)
|
||||
}
|
||||
id++
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
TrackRepository(ctx).createSongCount((id -1))
|
||||
TrackRepository(ctx).createSongCount((id ))
|
||||
return id.dec()
|
||||
}
|
||||
private fun dumpToDatabase(ctx: Context, track: Track) {
|
||||
@@ -57,8 +59,6 @@ class TrackManager(var allSongPath: MutableList<String>) {
|
||||
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
|
||||
it.write(fileContents)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
var allTracks: MutableList<Track>? = null
|
||||
|
||||
Reference in New Issue
Block a user