Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Jwen/end game download #345

Merged
merged 23 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fc89193
change var to val in DownloadActivity.kt
jiabaow May 24, 2022
bb20f93
inline alert
jiabaow May 24, 2022
9502fe0
inline alert
jiabaow May 24, 2022
c5f2698
inline else statement and remove unnecessary val
jiabaow May 24, 2022
96ce08b
rename ImageHelper.kt to Helper.kt and create setReturnToMainListener()
jiabaow May 24, 2022
0f3a8de
change var to val in DeleteSongsActivity.kt
jiabaow May 24, 2022
19d11b4
clean song_item_layout.xml
jiabaow May 24, 2022
8e3ae00
clean up SongListAdapter.kt
jiabaow May 24, 2022
f6718bf
remove extra empty line in DownloadActivity.kt
jiabaow May 24, 2022
e8a01d0
set up listener for download btn
jiabaow May 24, 2022
38c1fa6
download song by copy paste
jiabaow May 24, 2022
2509853
extract DownloadFunctionalityActivity.kt
jiabaow May 24, 2022
efb5e31
Merge branch 'main' into jwen/end-game-download
jiabaow May 25, 2022
cda98e2
Merge branch 'main' into jwen/end-game-download
jiabaow May 25, 2022
deb5658
Test commit
Tsathogguaa May 26, 2022
1dc4947
Fixed weird test behavior
Tsathogguaa May 26, 2022
be00020
Modified test to see current coverage on the PR
Tsathogguaa May 26, 2022
9d12478
Halfway done with buttons
Tsathogguaa May 26, 2022
711d85a
Added docs and some readability updates
Tsathogguaa May 26, 2022
f917b99
Merge branch 'main' into jwen/end-game-download
jiabaow May 26, 2022
8ccdabc
Added class doc
Tsathogguaa May 26, 2022
25329df
Merge branch 'jwen/end-game-download' of github.com:MaximeZmt/SDP_202…
Tsathogguaa May 26, 2022
2cb3206
Merge branch 'main' into jwen/end-game-download
Tsathogguaa May 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class DownloadActivityTest {
fun downloadIncorrectSong() {
val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java)
val scn: ActivityScenario<DownloadActivity> = ActivityScenario.launch(intent)
assertEquals(false, DownloadActivity.downloadStarted)
assertEquals(false, DownloadFunctionalityActivity.downloadStarted)
val songName = "adsfasdgyasdfa"

onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard())
Thread.sleep(waitForButton)
onView(withId(R.id.download_downloadsong)).perform(click())

while(!DownloadActivity.downloadComplete) {
while(!DownloadFunctionalityActivity.downloadComplete) {
Thread.sleep(waitForDownload)
}
Thread.sleep(waitForButton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class GameEndingActivityTest {
clickOnViewChild(R.id.song_download))
)
checkRecyclerSubViews(R.id.end_song_list, 0, withEffectiveVisibility(Visibility.INVISIBLE), R.id.song_download)
checkRecyclerSubViews(R.id.end_song_list, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.song_download_done)
checkRecyclerSubViews(R.id.end_song_list, 0, withEffectiveVisibility(Visibility.VISIBLE), R.id.song_download_ongoing)
}


Expand Down
36 changes: 17 additions & 19 deletions app/src/main/java/ch/sdp/vibester/activity/DeleteSongsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.TextView
import android.widget.Toast
import ch.sdp.vibester.R
import ch.sdp.vibester.helper.IntentSwitcher
import ch.sdp.vibester.helper.Helper
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.io.*
/**
Expand All @@ -28,9 +28,7 @@ class DeleteSongsActivity : AppCompatActivity() {

setContentView(R.layout.activity_delete_songs)

findViewById<FloatingActionButton>(R.id.delete_returnToMain).setOnClickListener {
IntentSwitcher.switch(this, MainActivity::class.java)
}
Helper().setReturnToMainListener(findViewById<FloatingActionButton>(R.id.delete_returnToMain), this)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 3 locations. Consider refactoring.


val layout: LinearLayout = findViewById(R.id.delete_songs_linear)
generateButtons(layout)
Expand All @@ -41,17 +39,17 @@ class DeleteSongsActivity : AppCompatActivity() {
* Assigns each button a call to the "deleteDownloadedSong" function as a listener.
*/
private fun generateButtons(layout: LinearLayout) {
var records = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
val records = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")

if(!records.exists() || records.length() == 0L) {
createNoSongsView(layout)
} else {
var reader = BufferedReader(FileReader(records))
val reader = BufferedReader(FileReader(records))
var currentLine = reader.readLine()
var iterator = 0

while(currentLine != null) {
var trimmed = currentLine.trim()
val trimmed = currentLine.trim()
if(trimmed.isNotEmpty()) {
val deleteButton = createButton(trimmed, iterator, layout)
layout.addView(deleteButton)
Expand Down Expand Up @@ -83,18 +81,18 @@ class DeleteSongsActivity : AppCompatActivity() {
* from the text file which keeps track of all downloads.
*/
private fun deleteDownloadedSong(btn: View, layout: LinearLayout): Boolean {
var records = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
var tempRecords = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "temp.txt")
val records = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
val tempRecords = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "temp.txt")

var recordReader = BufferedReader(FileReader(records))
var recordWriter = BufferedWriter(FileWriter(tempRecords, true))
val recordReader = BufferedReader(FileReader(records))
val recordWriter = BufferedWriter(FileWriter(tempRecords, true))

var currentLine = recordReader.readLine()
var button: Button = btn as Button
val button: Button = btn as Button
val buttonText = button.text.toString()

while (currentLine != null) {
var trimmed = currentLine.trim()
val trimmed = currentLine.trim()

if (trimmed == buttonText) {
currentLine = recordReader.readLine()
Expand All @@ -116,7 +114,7 @@ class DeleteSongsActivity : AppCompatActivity() {
records.delete()
if (tempRecords.renameTo(records) && removeFromProperties(buttonText)) {
layout.removeView(btn)
var songToDelete = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_${button.text}")
val songToDelete = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_${button.text}")
if (songToDelete.delete()) {
Toast.makeText(applicationContext, "Song successfully removed!", Toast.LENGTH_LONG).show()
return true
Expand All @@ -131,11 +129,11 @@ class DeleteSongsActivity : AppCompatActivity() {
* @param buttonText: Text which contains song name - artist name
*/
private fun removeFromProperties(buttonText: String): Boolean {
var properties = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
var tempProp = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "tempProp.txt")
val properties = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")
val tempProp = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "tempProp.txt")

var propReader = BufferedReader(FileReader(properties))
var propWriter = BufferedWriter(FileWriter(tempProp, true))
val propReader = BufferedReader(FileReader(properties))
val propWriter = BufferedWriter(FileWriter(tempProp, true))

var currentLine = propReader.readLine()
val buttonSplit = buttonText.trim().split("-")
Expand All @@ -147,7 +145,7 @@ class DeleteSongsActivity : AppCompatActivity() {
* We write every line on a new file except the one which we want to delete, thus we continue if we match.
*/
while (currentLine != null) {
var trimmed = currentLine.trim()
val trimmed = currentLine.trim()
val split = trimmed.split(" - ")

if (split[0].trim().lowercase() == buttonSplit[0].trim().lowercase()
Expand Down
208 changes: 9 additions & 199 deletions app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt
Original file line number Diff line number Diff line change
@@ -1,231 +1,41 @@
package ch.sdp.vibester.activity

import android.Manifest
import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.R
import ch.sdp.vibester.api.ItunesMusicApi
import ch.sdp.vibester.helper.IntentSwitcher
import ch.sdp.vibester.model.Song
import ch.sdp.vibester.helper.Helper
import com.google.android.material.floatingactionbutton.FloatingActionButton
import okhttp3.OkHttpClient
import java.io.File
import java.lang.IllegalArgumentException

/**
* Activity that handles downloading of song extracts.
*/
class DownloadActivity : AppCompatActivity() {
//Companion object to indicate when the download completes.
companion object {
var downloadComplete = false
var downloadStarted = false
}

private val STORAGE_PERMISSION_CODE = 1000
private lateinit var song: Song
private lateinit var songName: String
private var downloadId: Long = 0

class DownloadActivity : DownloadFunctionalityActivity() {

/**
* Generic onCreate function, nothing of interest.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_download)


val songNameView = findViewById<TextView>(R.id.download_songName)
val downloadButton = findViewById<Button>(R.id.download_downloadsong)


downloadButton.setOnClickListener {
downloadListener(songNameView)
}

findViewById<FloatingActionButton>(R.id.download_returnToMain).setOnClickListener {
IntentSwitcher.switch(this, MainActivity::class.java)
}

var broadcast = object:BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
var id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (id == downloadId) {
alert(
getString(R.string.download_download_complete),
getString(R.string.download_try_another),
songNameView
)
}
}
}
registerReceiver(broadcast, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}

/**
* Function that handles deletion button pushes.
*/
private fun downloadListener(songView: TextView) {
if (downloadStarted) {
Toast.makeText(applicationContext, getString(R.string.download_already_downloading), Toast.LENGTH_LONG).show()
editTextView(getString(R.string.download_please_retry_later), songView)
} else {
downloadStarted = true
downloadComplete = false
songName = songView.text.toString()

if (checkExistingSong()) {
alert(
getString(R.string.download_already_done),
getString(R.string.download_try_different),
songView
)
} else {
getAndDownload(songView)
}
}
}

private fun getAndDownload(songView: TextView) {
val songFuture = ItunesMusicApi.querySong(songName, OkHttpClient(), 1)
try {
song = Song.singleSong(songFuture.get())
songName = song.getTrackName().lowercase() + " - " + song.getArtistName().lowercase()
checkPermissionsAndDownload()
} catch (e: IllegalArgumentException) {
alert(
getString(R.string.download_unable_to_find),
getString(R.string.download_retry),
songView
)
}
}

/**
* Displays a Toast on the screen while editing the existing textView.
*
* @param toast: String to be displayed on the Toast.
* @param hint : String to be set as the hint of the textView.
* @param view : The textView that will be updated.
*/
private fun alert(toast: String, hint: String, view: TextView) {
downloadComplete = true
downloadStarted = false
Toast.makeText(applicationContext, toast, Toast.LENGTH_LONG).show()
editTextView(hint, view)
}

/**
* Sets the hint of the given textview with the given hint, and clears the entered text.
*
* @param hint : String to be set as the hint of the textView.
* @param songNameView : The textView that will be updated.
*/
private fun editTextView(hint: String, songNameView: TextView) {
songNameView.text = ""
songNameView.hint = hint
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
downloadId = startDownload()
}
} else {
Toast.makeText(this, getString(R.string.download_permission_denied), Toast.LENGTH_LONG).show()
}
}

/**
* Checks if the required app permissions are already given. If not, request those permissions.
*/
private fun checkPermissionsAndDownload() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION_CODE)
} else {
downloadId = startDownload()
}
} else {
downloadId = startDownload()
downloadListener(songNameView, null)
}
}

/**
* Download a file from the private URL value of the class.
*/
private fun startDownload(): Long {
record()
val request = DownloadManager.Request(Uri.parse(song.getPreviewUrl()))
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_MOBILE
or DownloadManager.Request.NETWORK_WIFI
)
.setTitle("extract_of_$songName")
.setAllowedOverRoaming(true)
.setDescription("Downloading extract of the song + $songName")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalFilesDir(this,
Environment.DIRECTORY_DOWNLOADS,
"extract_of_$songName")
Helper().setReturnToMainListener(findViewById<FloatingActionButton>(R.id.download_returnToMain), this)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 3 locations. Consider refactoring.


val downloader = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
return downloader.enqueue(request)
createDownloadReceiver(songNameView)
}

/**
* Indicator of if the song already exists or not.
* Generic function to switch to a different, namely DeleteSongsActivity activity.
*/
private fun checkExistingSong(): Boolean {
var existing = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "extract_of_$songName")
return existing.exists()
}

/**
* Helps keep track of the currently downloaded songs in the form of a txt file.
* If the file does not exist, it is created.
*/
private fun record() {
var records = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")

if (!records.exists()) {
records.createNewFile()
}
records.appendText("$songName\n")
recordProperties()
}

/**
* Records the properties of a song.
* Order of storage: Track name - artist name - artwork URL - preview URL.
*/
private fun recordProperties() {
var properties = File(applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "properties.txt")

if (!properties.exists()) {
properties.createNewFile()
}

val trackName = song.getTrackName()
val artistName = song.getArtistName()
val artworkURL = song.getArtworkUrl()
val previewURL = song.getPreviewUrl()
properties.appendText("$trackName - $artistName - $artworkURL - $previewURL\n")
}

fun switchToDeleteSongs(view: View) {
val intent = Intent(this, DeleteSongsActivity::class.java)
startActivity(intent)
Expand Down
Loading