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

Commit

Permalink
Merge pull request #237 from MaximeZmt/tsathogguaa/download_bugfix
Browse files Browse the repository at this point in the history
Bug fixes and slight documentation.
  • Loading branch information
Tsathogguaa authored May 5, 2022
2 parents 99d421e + 7141d3c commit 1f2ba5a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ch.sdp.vibester.activity

import android.app.Activity
import android.content.Intent
import android.os.Environment
import android.util.Log
import android.view.View
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
Expand All @@ -17,7 +14,6 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule
import ch.sdp.vibester.R
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -43,13 +39,11 @@ class DownloadActivityTest {
private var waitForButton: Long = 100
private var waitForDownload: Long = 1000

/*
@Test
//Test that takes too long to execute. Uncomment towards the last sprint.
fun downloadCorrectSong() {
fun downloadIncorrectSong() {
val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java)
val scn: ActivityScenario<DownloadActivity> = ActivityScenario.launch(intent)
val songName = "imagine dragons believer"
val songName = "adsfasdgyasdfa"

onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard())
Thread.sleep(waitForButton)
Expand All @@ -61,53 +55,58 @@ class DownloadActivityTest {
Thread.sleep(waitForButton)

onView(withId(R.id.download_songName)).check(matches(withText("")))
onView(withId(R.id.download_songName)).check(matches(withHint("Try another song!")))
onView(withId(R.id.download_songName)).check(matches(withHint("Please retry!")))

scn.onActivity { activity ->
val extract = File(activity.applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"extract_of_$songName")
assert(extract.exists())
extract.delete()
assert(!extract.exists())
if(extract.exists()) {
extract.delete()
}

val records = File(activity.applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
assert(records.exists())
val text = FileInputStream(records).bufferedReader().use {out -> out.readLine()}
assertEquals(songName, text)
records.delete()
assert(!records.exists())
if(records.exists()) {
records.delete()
}
}
} */
}

/*
//Test that takes too long to execute. Uncomment towards the last sprint.
@Test
fun downloadIncorrectSong() {
fun downloadMultipleSongs() {
val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java)
val scn: ActivityScenario<DownloadActivity> = ActivityScenario.launch(intent)
val songName = "adsfasdgyasdfa"
val songName = "imagine dragons believer"
onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard())
Thread.sleep(waitForButton)
onView(withId(R.id.download_downloadsong)).perform(click())
Thread.sleep(waitForButton)
onView(withId(R.id.download_downloadsong)).perform(click())
onView(withId(R.id.download_songName)).check(matches(withText("")))
onView(withId(R.id.download_songName)).check(matches(withHint("Please retry later!")))
while(!DownloadActivity.downloadComplete) {
Thread.sleep(waitForDownload)
}
Thread.sleep(waitForButton)

onView(withId(R.id.download_songName)).check(matches(withText("")))
onView(withId(R.id.download_songName)).check(matches(withHint("Please retry!")))
scn.onActivity { activity ->
val extract = File(activity.applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"extract_of_$songName")
assert(!extract.exists())
assert(extract.exists())
if(extract.exists()) {
extract.delete()
}
val records = File(activity.applicationContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "records.txt")
assert(!records.exists())
assert(records.exists())
if(records.exists()) {
records.delete()
}
}
}
}*/

/*
//Test that takes too long to execute. Uncomment towards the last sprint.
Expand Down
44 changes: 32 additions & 12 deletions app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ 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 var songName: String = "imagine dragons believer"
private lateinit var songName: String
private var downloadId: Long = 0


Expand Down Expand Up @@ -73,22 +74,40 @@ class DownloadActivity : AppCompatActivity() {
* Function that handles deletion button pushes.
*/
private fun downloadListener(songView: TextView) {
downloadComplete = false
songName = songView.text.toString()

if(checkExistingSong()) {
alert(getString(R.string.download_already_done), getString(R.string.download_try_different), songView)
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 {
val songFuture = ItunesMusicApi.querySong(songName, OkHttpClient(), 1)
try {
song = Song.singleSong(songFuture.get())
checkPermissionsAndDownload()
} catch (e: IllegalArgumentException) {
alert(getString(R.string.download_unable_to_find), getString(R.string.download_retry), songView)
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())
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.
*
Expand All @@ -98,6 +117,7 @@ class DownloadActivity : AppCompatActivity() {
*/
private fun alert(toast: String, hint: String, view: TextView) {
downloadComplete = true
downloadStarted = false
Toast.makeText(applicationContext, toast, Toast.LENGTH_LONG).show()
editTextView(hint, view)
}
Expand Down
45 changes: 39 additions & 6 deletions app/src/main/java/ch/sdp/vibester/activity/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ import kotlinx.coroutines.launch

/**
* Common set up for all games (difficulty level, progress bar)
* CHECK IF THE DOCS ARE CORRECT OR NOT!
*/
open class GameActivity : AppCompatActivity() {
open val handler = Handler()
open var maxTime: Int = 30
var runnable: Runnable? = null

/**
* Sets the countdown timer's maximum(initial) value.
*/
fun setMax(intent: Intent) {
if(intent.hasExtra("Difficulty")) {
when(intent.extras?.getString("Difficulty", "Easy")) {
Expand All @@ -37,14 +41,20 @@ open class GameActivity : AppCompatActivity() {
}
}

/**
* Initializes the progress bar, i.e the timer to static values, mostly read from intents.
*/
fun initializeBarTimer(myBar: ProgressBar) {
myBar.max = maxTime
myBar.progress = maxTime
myBar.progressTintList = ColorStateList.valueOf(getColor(R.color.cg_blue))
}

/**
* Handles unit decrease of the timer of the progress bar, and the effects of this change.
*/
fun decreaseBarTimer(myBar: ProgressBar) {
if (myBar.progress == maxTime/2) {
if (myBar.progress == maxTime/2 && myBar.progress > 5) {
myBar.progressTintList =
ColorStateList.valueOf(getColor(R.color.maximum_yellow_red))
} else if (myBar.progress == 5) {
Expand All @@ -54,12 +64,19 @@ open class GameActivity : AppCompatActivity() {
myBar.progress -= 1
}

/**
* Checks the state of the runnable and removes callbacks.
*/
fun checkRunnable() {
if (runnable != null) {
handler.removeCallbacks(runnable!!)
}
}

/**
* Called upon the ending of a game. Passes gathered information during the game to the next
* activity through the intent.
*/
fun switchToEnding(gameManager: GameManager) {
val intent = Intent(this, GameEndingActivity::class.java)
val incArray: ArrayList<String> = ArrayList(
Expand All @@ -82,6 +99,9 @@ open class GameActivity : AppCompatActivity() {
startActivity(intent)
}

/**
* Sets the visibility of the given button to VISIBLE if value is true, GONE otherwise.
*/
fun toggleBtnVisibility(btnId: Int, value: Boolean){
findViewById<Button>(btnId).visibility = if (value) VISIBLE else GONE
}
Expand All @@ -97,22 +117,37 @@ open class GameActivity : AppCompatActivity() {
}
}

/**
* Function to check if the game has ended or not.
*/
fun isEndGame(gameManager: GameManager): Boolean {
return !gameManager.checkGameStatus() || !gameManager.setNextSong()
}

/**
* Function used for testing. Do not call unless it is for that specific purpose.
*/
fun superTestProgressBar(myBar: ProgressBar, progressTime: Int=0) {
myBar.progress = progressTime
}

/**
* Function used for testing. Do not call unless it is for that specific purpose.
*/
fun superTestProgressBarColor(myBar: ProgressBar): ColorStateList? {
return myBar.progressTintList
}

/**
* Shows a variable score on a toast.
*/
fun toastShowCorrect(ctx: Context, score: Int) {
Toast.makeText(ctx, ctx.getString(R.string.correct_message, score), Toast.LENGTH_SHORT).show()
}

/**
* Shows a given message on a toast.
*/
fun toastShowSimpleMsg(ctx: Context, SId: Int) {
Toast.makeText(ctx, ctx.getString(SId), Toast.LENGTH_SHORT).show()
}
Expand Down Expand Up @@ -162,6 +197,9 @@ open class GameActivity : AppCompatActivity() {
}
}

/**
* Add a given song to a new layout in a particular manner, which is then returned.
*/
fun showSongAndImage(song: Song, ctx: Context): LinearLayout {
val linLay = LinearLayout(ctx)
linLay.setHorizontalGravity(1)
Expand All @@ -173,9 +211,4 @@ open class GameActivity : AppCompatActivity() {

return linLay
}





}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
<string name="download_unable_to_find">Unable to find song, please retry!</string>
<string name="download_retry">Please retry!</string>
<string name="download_permission_denied">Permission denied, cannot download!</string>
<string name="download_already_downloading">A song is being downloaded, please be patient!</string>
<string name="download_please_retry_later">Please retry later!</string>

<string name="Singleplayer">Single-player</string>
<string name="Multiplayer">Multiplayer</string>
Expand Down

0 comments on commit 1f2ba5a

Please sign in to comment.