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
Implemented downloading features #155
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94efcaa
Implemented downloading features
Tsathogguaa e358516
Removed unneeded button
Tsathogguaa 06a3f07
Refactored onCreate of DownloadActivity
Tsathogguaa 78b7ee5
Further refactoring
Tsathogguaa 260d8e3
Got rid of some tests, though may have to refactor onCreate
Tsathogguaa 6c9bebf
Applied required changes
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
74 changes: 74 additions & 0 deletions
74
app/src/androidTest/java/ch/sdp/vibester/activity/DownloadActivityTest.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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ch.sdp.vibester.activity | ||
|
||
import android.content.Intent | ||
import android.view.View | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.action.ViewActions.* | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.intent.Intents | ||
import androidx.test.espresso.matcher.ViewMatchers.* | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import ch.sdp.vibester.R | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class DownloadActivityTest { | ||
|
||
@Rule | ||
@JvmField | ||
val activityRule = ActivityScenarioRule(DownloadActivity::class.java) | ||
|
||
@Before | ||
fun setUp() { | ||
Intents.init() | ||
} | ||
|
||
@After | ||
fun clean() { | ||
Intents.release() | ||
} | ||
|
||
private var waitForButton: Long = 100 | ||
private var waitForDownload: Long = 2000 | ||
|
||
@Test | ||
fun downloadCorrectSong() { | ||
val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java) | ||
val scn: ActivityScenario<DownloadActivity> = ActivityScenario.launch(intent) | ||
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(waitForDownload) | ||
|
||
onView(withId(R.id.download_songName)).check(matches(withText(""))) | ||
onView(withId(R.id.download_songName)).check(matches(withHint("Try another song!"))) | ||
|
||
val extract = File("storage/emulated/0/Download", "extract_of_$songName") | ||
assert(extract.exists()) | ||
} | ||
|
||
@Test | ||
fun downloadIncorrectSong() { | ||
val intent = Intent(ApplicationProvider.getApplicationContext(), DownloadActivity::class.java) | ||
val scn: ActivityScenario<DownloadActivity> = ActivityScenario.launch(intent) | ||
val songName = "adsfasdgyasdfa" | ||
|
||
onView(withId(R.id.download_songName)).perform(typeText(songName), closeSoftKeyboard()) | ||
Thread.sleep(100) | ||
onView(withId(R.id.download_downloadsong)).perform(click()) | ||
Thread.sleep(2000) | ||
|
||
onView(withId(R.id.download_songName)).check(matches(withText(""))) | ||
onView(withId(R.id.download_songName)).check(matches(withHint("Please retry!"))) | ||
|
||
val extract = File("storage/emulated/0/Download", "extract_of_$songName") | ||
assert(!extract.exists()) | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
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 androidx.appcompat.app.AppCompatActivity | ||
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 ch.sdp.vibester.R | ||
import ch.sdp.vibester.api.ItunesMusicApi | ||
import ch.sdp.vibester.model.Song | ||
import okhttp3.OkHttpClient | ||
import org.w3c.dom.Text | ||
import java.lang.IllegalArgumentException | ||
/* | ||
* Activity that handles downloading of song extracts. | ||
*/ | ||
class DownloadActivity : AppCompatActivity() { | ||
private val STORAGE_PERMISSION_CODE = 1000 | ||
private lateinit var song: Song | ||
private var songName: String = "imagine dragons believer" | ||
private var downloadId: Long = 0 | ||
|
||
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 { | ||
songName = songNameView.text.toString() | ||
val songFuture = ItunesMusicApi.querySong(songName, OkHttpClient(), 1) | ||
try { | ||
song = Song.singleSong(songFuture.get()) | ||
checkPermissionsAndDownload() | ||
} catch (e: IllegalArgumentException) { | ||
alert("Unable to find song, please retry!", "Please retry!", songNameView) | ||
} | ||
} | ||
|
||
var broadcast = object:BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
var id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) | ||
if(id == downloadId) { | ||
alert("Download completed!", "Try another song!", songNameView) | ||
} | ||
} | ||
} | ||
|
||
registerReceiver(broadcast, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) | ||
} | ||
|
||
/* | ||
* 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) { | ||
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, "Permission denied, cannot download!", 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() | ||
} | ||
} | ||
|
||
/* | ||
* Download a file from the private URL value of the class. | ||
*/ | ||
private fun startDownload(): Long { | ||
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) | ||
.setDestinationInExternalPublicDir( | ||
Environment.DIRECTORY_DOWNLOADS, | ||
"extract_of_$songName" | ||
) | ||
|
||
val downloader = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
return downloader.enqueue(request) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -39,6 +39,10 @@ class WelcomeActivity : AppCompatActivity() { | |
sendDirectIntent(AuthenticationActivity::class.java) | ||
} | ||
|
||
fun switchToDownload(view: View) { | ||
sendDirectIntent(DownloadActivity::class.java) | ||
} | ||
|
||
Comment on lines
+42
to
+45
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. Should be easy to test, currently untested 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. Done! |
||
/* | ||
* Belongs to a previously implemented button, taken out for UI purposes. | ||
* Might bring it back, thus leaving the code for now. | ||
|
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".activity.DownloadActivity"> | ||
|
||
<EditText | ||
android:id="@+id/download_songName" | ||
android:layout_width="253dp" | ||
android:layout_height="50dp" | ||
android:layout_marginTop="100dp" | ||
android:layout_marginBottom="50dp" | ||
android:ems="10" | ||
android:hint="@string/download_songname" | ||
android:inputType="textPersonName" | ||
app:layout_constraintBottom_toTopOf="@+id/download_downloadsong" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:ignore="TextContrastCheck" /> | ||
|
||
<Button | ||
android:id="@+id/download_downloadsong" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="50dp" | ||
android:text="@string/download_download" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.497" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/download_songName" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
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
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.
Try using constants for the Thread.sleep calls instead of hardcoded values.
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.
Done!