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
|
package com.example.mear.activities
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
|
import android.support.v4.app.ActivityCompat
|
||||||
|
import android.support.v4.content.ContextCompat
|
||||||
|
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
import java.lang.Runnable
|
import java.lang.Runnable
|
||||||
@@ -40,6 +44,7 @@ class MainActivity : BaseServiceActivity() {
|
|||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
setSupportActionBar(toolbar)
|
setSupportActionBar(toolbar)
|
||||||
|
|
||||||
|
permissionPrompt()
|
||||||
|
|
||||||
initialize()
|
initialize()
|
||||||
}
|
}
|
||||||
@@ -127,8 +132,7 @@ class MainActivity : BaseServiceActivity() {
|
|||||||
startActivity(Intent(this, SongViewActivity::class.java))
|
startActivity(Intent(this, SongViewActivity::class.java))
|
||||||
}
|
}
|
||||||
SettingsLink.setOnClickListener {
|
SettingsLink.setOnClickListener {
|
||||||
val intent = Intent(this, SettingsActivity::class.java)
|
startActivity(Intent(this, SettingsActivity::class.java))
|
||||||
startActivity(intent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private fun initializeServices() {
|
private fun initializeServices() {
|
||||||
@@ -261,15 +265,6 @@ class MainActivity : BaseServiceActivity() {
|
|||||||
if (coverExt.hasCover()) {
|
if (coverExt.hasCover()) {
|
||||||
trackCover = coverExt.retrieveCover()
|
trackCover = coverExt.retrieveCover()
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
val mmr = MediaMetadataRetriever()
|
|
||||||
mmr.setDataSource(currTrack.songPath)
|
|
||||||
|
|
||||||
if (mmr.embeddedPicture != null) {
|
|
||||||
trackCover = mmr.embeddedPicture
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
resetControls()
|
resetControls()
|
||||||
|
|
||||||
TrackTitle.text = trackTitle
|
TrackTitle.text = trackTitle
|
||||||
@@ -310,7 +305,23 @@ class MainActivity : BaseServiceActivity() {
|
|||||||
PlayTrack.setColorFilter(Color.GREEN)
|
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 {
|
private var musicTrackTimeUpdateTask = object: Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.example.mear.activities
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.annotation.RequiresApi
|
import android.support.annotation.RequiresApi
|
||||||
import android.support.v7.app.AppCompatActivity
|
|
||||||
import android.support.v7.widget.LinearLayoutManager
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
import android.view.View
|
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.activity_song_view.*
|
||||||
import kotlinx.android.synthetic.main.content_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.adapters.RecyclerAdapter
|
||||||
import com.example.mear.models.TrackItems
|
import com.example.mear.models.TrackItems
|
||||||
import com.example.mear.R
|
import com.example.mear.R
|
||||||
import com.example.mear.repositories.TrackRepository
|
import com.example.mear.repositories.TrackRepository
|
||||||
import com.example.mear.util.ExtractCover
|
|
||||||
import org.jetbrains.anko.act
|
|
||||||
|
|
||||||
class SongViewActivity : BaseServiceActivity() {
|
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 var trackListItems = arrayListOf<TrackItems>()
|
||||||
private lateinit var adapter: RecyclerAdapter
|
private lateinit var adapter: RecyclerAdapter
|
||||||
private lateinit var linearLayoutManager: LinearLayoutManager
|
private lateinit var linearLayoutManager: LinearLayoutManager
|
||||||
@@ -55,7 +44,6 @@ class SongViewActivity : BaseServiceActivity() {
|
|||||||
if (trackListItems.size == 0) {
|
if (trackListItems.size == 0) {
|
||||||
val df = ""
|
val df = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,10 +56,6 @@ class SongViewActivity : BaseServiceActivity() {
|
|||||||
|
|
||||||
trackListItems = retrieveTrackItems()
|
trackListItems = retrieveTrackItems()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//adapter = RecyclerAdapter(trackListItems)
|
|
||||||
//adapter = RecyclerAdapter(this, trackListItems)
|
|
||||||
adapter = RecyclerAdapter({trackItem: TrackItems -> playTrack(trackItem)}, trackListItems)
|
adapter = RecyclerAdapter({trackItem: TrackItems -> playTrack(trackItem)}, trackListItems)
|
||||||
adapter.configureActivity(this)
|
adapter.configureActivity(this)
|
||||||
trackList.adapter = adapter
|
trackList.adapter = adapter
|
||||||
@@ -80,8 +64,8 @@ class SongViewActivity : BaseServiceActivity() {
|
|||||||
}
|
}
|
||||||
trackList.setHasFixedSize(true)
|
trackList.setHasFixedSize(true)
|
||||||
trackList.setItemViewCacheSize(20);
|
trackList.setItemViewCacheSize(20);
|
||||||
//trackList.setDrawingCacheEnabled(true);
|
trackList.setDrawingCacheEnabled(true);
|
||||||
//trackList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
|
trackList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
|
||||||
}
|
}
|
||||||
catch (ex: Exception) {
|
catch (ex: Exception) {
|
||||||
val exMsg = ex.message
|
val exMsg = ex.message
|
||||||
@@ -96,48 +80,8 @@ class SongViewActivity : BaseServiceActivity() {
|
|||||||
|
|
||||||
val tracks = TrackRepository(this).getAll()
|
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) {
|
for (track in tracks) {
|
||||||
//val cover = ExtractCover(track.songPath)
|
|
||||||
var trackCover = ByteArray(0)
|
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!!)
|
val trackItem = TrackItems(track.id, track.title, track.artist, trackCover!!)
|
||||||
trackItems.add(trackItem)
|
trackItems.add(trackItem)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,6 @@
|
|||||||
package com.example.mear.adapters
|
package com.example.mear.adapters
|
||||||
|
|
||||||
import android.app.Activity
|
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.support.v7.widget.RecyclerView
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@@ -17,63 +10,15 @@ import kotlinx.android.synthetic.main.fragment_song_view.view.*
|
|||||||
|
|
||||||
import com.squareup.picasso.Picasso
|
import com.squareup.picasso.Picasso
|
||||||
|
|
||||||
import com.example.mear.activities.SongViewActivity
|
|
||||||
import com.example.mear.constants.Filenames
|
import com.example.mear.constants.Filenames
|
||||||
import com.example.mear.inflate
|
import com.example.mear.inflate
|
||||||
import com.example.mear.models.TrackItems
|
import com.example.mear.models.TrackItems
|
||||||
import com.example.mear.R
|
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>) :
|
class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private val trackItemsSourceInit: ArrayList<TrackItems>) :
|
||||||
RecyclerView.Adapter<RecyclerView.ViewHolder>()
|
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
|
|
||||||
|
|
||||||
open interface trackItemClickListener{
|
open interface trackItemClickListener{
|
||||||
fun onClick(view: View)
|
fun onClick(view: View)
|
||||||
@@ -85,73 +30,26 @@ class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private va
|
|||||||
|
|
||||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||||
val itemPhoto = trackItemsSourceInit!![position]
|
val itemPhoto = trackItemsSourceInit!![position]
|
||||||
//(holder as TrackItemsHolder).bindTrackItems(itemPhoto)
|
|
||||||
(holder as TrackItemsHolder).bindTrackItems(itemPhoto, mOnClickListenerI)
|
(holder as TrackItemsHolder).bindTrackItems(itemPhoto, mOnClickListenerI)
|
||||||
}
|
}
|
||||||
|
|
||||||
// override fun onCreateViewHolder(parent: ViewGroup, position: Int): TrackItemsHolder {
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, position: Int): RecyclerView.ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, position: Int): RecyclerView.ViewHolder {
|
||||||
val inflatedView = parent.inflate(R.layout.fragment_song_view, false)
|
val inflatedView = parent.inflate(R.layout.fragment_song_view, false)
|
||||||
//inflatedView.setOnClickListener(parent.parent)
|
|
||||||
return TrackItemsHolder(inflatedView)
|
return TrackItemsHolder(inflatedView)
|
||||||
//return TrackItemsHolder(inflatedView, mOnClick)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun configureActivity(actvity: Activity) {
|
fun configureActivity(actvity: Activity) {
|
||||||
//activityInst = actvity
|
|
||||||
//activityInst!!.application.registerActivityLifecycleCallbacks(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class TrackItemsHolder(v: View) : RecyclerView.ViewHolder(v) {
|
class TrackItemsHolder(v: View) : RecyclerView.ViewHolder(v) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private var view: View = v
|
private var view: View = v
|
||||||
//private var act = activity
|
|
||||||
//private var musicService: MusicService? = null
|
|
||||||
private var trackItem: TrackItems? = null
|
private var trackItem: TrackItems? = null
|
||||||
private val imgWidth = 90
|
private val imgWidth = 90
|
||||||
private val imgHeight = 90
|
private val imgHeight = 90
|
||||||
private var mBound = false
|
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) {
|
fun bindTrackItems(trackItems: TrackItems, clickList: (TrackItems) -> Unit) {
|
||||||
try {
|
try {
|
||||||
@@ -172,7 +70,6 @@ class RecyclerAdapter(val mOnClickListenerI: (TrackItems) -> Unit , private va
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
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,
|
val track = Track(id, trackTitle, trackArtist, trackAlbum, trackLength,
|
||||||
ByteArray(0), musicPath)
|
ByteArray(0), musicPath)
|
||||||
dumpToDatabase(ctx, track)
|
dumpToDatabase(ctx, track)
|
||||||
saveTrackCoverToDisk(ctx, id, mmr.embeddedPicture)
|
if (mmr.embeddedPicture!=null) {
|
||||||
|
saveTrackCoverToDisk(ctx, id, mmr.embeddedPicture)
|
||||||
|
}
|
||||||
id++
|
id++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ex: Exception) {
|
catch (ex: Exception) {
|
||||||
val exMsg = ex.message
|
val exMsg = ex.message
|
||||||
}
|
}
|
||||||
TrackRepository(ctx).createSongCount((id -1))
|
TrackRepository(ctx).createSongCount((id ))
|
||||||
return id.dec()
|
return id.dec()
|
||||||
}
|
}
|
||||||
private fun dumpToDatabase(ctx: Context, track: Track) {
|
private fun dumpToDatabase(ctx: Context, track: Track) {
|
||||||
@@ -57,8 +59,6 @@ class TrackManager(var allSongPath: MutableList<String>) {
|
|||||||
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
|
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
|
||||||
it.write(fileContents)
|
it.write(fileContents)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var allTracks: MutableList<Track>? = null
|
var allTracks: MutableList<Track>? = null
|
||||||
|
|||||||
Reference in New Issue
Block a user