This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Jwen/end game download #345
Merged
Merged
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 bb20f93
inline alert
jiabaow 9502fe0
inline alert
jiabaow c5f2698
inline else statement and remove unnecessary val
jiabaow 96ce08b
rename ImageHelper.kt to Helper.kt and create setReturnToMainListener()
jiabaow 0f3a8de
change var to val in DeleteSongsActivity.kt
jiabaow 19d11b4
clean song_item_layout.xml
jiabaow 8e3ae00
clean up SongListAdapter.kt
jiabaow f6718bf
remove extra empty line in DownloadActivity.kt
jiabaow e8a01d0
set up listener for download btn
jiabaow 38c1fa6
download song by copy paste
jiabaow 2509853
extract DownloadFunctionalityActivity.kt
jiabaow efb5e31
Merge branch 'main' into jwen/end-game-download
jiabaow cda98e2
Merge branch 'main' into jwen/end-game-download
jiabaow deb5658
Test commit
Tsathogguaa 1dc4947
Fixed weird test behavior
Tsathogguaa be00020
Modified test to see current coverage on the PR
Tsathogguaa 9d12478
Halfway done with buttons
Tsathogguaa 711d85a
Added docs and some readability updates
Tsathogguaa f917b99
Merge branch 'main' into jwen/end-game-download
jiabaow 8ccdabc
Added class doc
Tsathogguaa 25329df
Merge branch 'jwen/end-game-download' of github.com:MaximeZmt/SDP_202…
Tsathogguaa 2cb3206
Merge branch 'main' into jwen/end-game-download
Tsathogguaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
208 changes: 9 additions & 199 deletions
208
app/src/main/java/ch/sdp/vibester/activity/DownloadActivity.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 |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.