Downloading #57
+23
-24
@@ -285,11 +285,8 @@ void downloadSong(Song& song, const Token& token, const Str& path) {
|
||||
saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size());
|
||||
saveSong.close();
|
||||
|
||||
song.path = downloadedSong.path;
|
||||
repository::local::SongRepository localSongRepo(path);
|
||||
if (!localSongRepo.doesTableExist(path)) {
|
||||
localSongRepo.createSongTable(path);
|
||||
}
|
||||
|
||||
localSongRepo.saveSong(downloadedSong, path);
|
||||
}
|
||||
|
||||
@@ -524,6 +521,28 @@ Java_com_example_mear_repositories_TokenRepository_retrieveTokenRecord(
|
||||
return tokenObj;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jobject
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepository_downloadSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject tokenObj,
|
||||
jobject songObj,
|
||||
jstring pathStr
|
||||
) {
|
||||
auto tokenClass = env->GetObjectClass(tokenObj);
|
||||
auto accessTokenId = env->GetFieldID(tokenClass, "accessToken", "Ljava/lang/String;");
|
||||
auto accessTokenVal = (jstring) env->GetObjectField(tokenObj, accessTokenId);
|
||||
model::Token token(env->GetStringUTFChars(accessTokenVal, nullptr));
|
||||
|
||||
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
|
||||
auto path = env->GetStringUTFChars(pathStr, nullptr);
|
||||
downloadSong(song, token, path);
|
||||
|
||||
return songToObj(env, song);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jobject
|
||||
JNICALL
|
||||
@@ -764,26 +783,6 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord(
|
||||
saveToken(token, path);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepository_downloadSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject tokenObj,
|
||||
jobject songObj,
|
||||
jstring pathStr
|
||||
) {
|
||||
auto tokenClass = env->GetObjectClass(tokenObj);
|
||||
auto accessTokenId = env->GetFieldID(tokenClass, "accessToken", "Ljava/lang/String;");
|
||||
auto accessTokenVal = (jstring) env->GetObjectField(tokenObj, accessTokenId);
|
||||
model::Token token(env->GetStringUTFChars(accessTokenVal, nullptr));
|
||||
|
||||
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
|
||||
auto path = env->GetStringUTFChars(pathStr, nullptr);
|
||||
downloadSong(song, token, path);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
|
||||
@@ -93,14 +93,9 @@ namespace manager {
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B deleteSong(const Song& song, const Str& path) {
|
||||
auto s = fullSongPath(song, path);
|
||||
auto result = std::remove(s.c_str());
|
||||
const auto s = fullSongPath(song, path);
|
||||
|
||||
if (result == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return (remove(s.c_str()) == 0) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
@@ -128,16 +123,7 @@ namespace manager {
|
||||
albumPath.append(song.albumArtist);
|
||||
albumPath.append("/");
|
||||
albumPath.append(song.album);
|
||||
/**
|
||||
if (song.disc == 0) {
|
||||
albumPath.append("/");
|
||||
albumPath.append("disc1");
|
||||
} else {
|
||||
albumPath.append("/");
|
||||
albumPath.append("disc");
|
||||
albumPath.append(std::to_string(song.disc));
|
||||
}
|
||||
*/
|
||||
|
||||
DIR* dir = opendir(albumPath.c_str());
|
||||
|
||||
if (dir) {
|
||||
|
||||
@@ -151,16 +151,6 @@ namespace repository {
|
||||
auto res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end()));
|
||||
/**
|
||||
downloadedSong.filename = "track";
|
||||
if (song.track < 10) {
|
||||
downloadedSong.filename.append("0");
|
||||
downloadedSong.filename.append(std::to_string(song.track));
|
||||
} else {
|
||||
downloadedSong.filename.append(std::to_string(song.track));
|
||||
}
|
||||
downloadedSong.filename.append(".mp3");
|
||||
*/
|
||||
|
||||
return downloadedSong;
|
||||
}
|
||||
|
||||
@@ -373,17 +373,22 @@ class MainActivity : BaseServiceActivity() {
|
||||
R.id.action_song_delete -> {
|
||||
// TODO: handle song deletion
|
||||
val ss = true
|
||||
val appPath = appDirectory()
|
||||
val trackRepo = TrackRepository()
|
||||
// The method I am calling has not been implemented on the C++
|
||||
// side
|
||||
trackRepo.delete(musicService!!.getCurrentSong(), appPath)
|
||||
}
|
||||
R.id.action_song_download -> {
|
||||
val appPath = appDirectory()
|
||||
val apiRepo = APIRepository()
|
||||
val tokenRepo = TokenRepository()
|
||||
val trackRepo = TrackRepository()
|
||||
val song = musicService!!.getCurrentSong()
|
||||
var song = musicService!!.getCurrentSong()
|
||||
|
||||
val token = tokenRepo.retrieveToken(appPath)
|
||||
val apiInfo = apiRepo.retrieveRecord(appPath)
|
||||
trackRepo.download(token, song, appPath)
|
||||
song = trackRepo.download(token, song, appPath)
|
||||
musicService!!.changeSongDownloadStatus()
|
||||
// TODO: implement something to include the downloaded song into the songQueue
|
||||
// essentially replacing the song that already exists, that way one can
|
||||
|
||||
@@ -97,7 +97,6 @@ class MusicService(var appPath: String = ""): Service() {
|
||||
s.downloaded = true
|
||||
}
|
||||
}
|
||||
// songQueue[currentSongIndex!!].downloaded = true
|
||||
}
|
||||
|
||||
fun goToPosition(progress: Int) { trackPlayer!!.seekTo(progress) }
|
||||
@@ -142,11 +141,8 @@ class MusicService(var appPath: String = ""): Service() {
|
||||
if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) {
|
||||
currentSongIndex = Random.nextInt(0, songQueue.size - 1)
|
||||
} else if (!parseRepeatMode(repeatMode)) {
|
||||
if (currentSongIndex == 0) {
|
||||
currentSongIndex = songQueue.size - 1
|
||||
} else {
|
||||
currentSongIndex = currentSongIndex!! - 1
|
||||
}
|
||||
currentSongIndex = if (currentSongIndex == 0)
|
||||
songQueue.size - 1 else currentSongIndex!! - 1
|
||||
}
|
||||
|
||||
currentSong = songQueue[currentSongIndex!!]
|
||||
@@ -184,12 +180,8 @@ class MusicService(var appPath: String = ""): Service() {
|
||||
if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) {
|
||||
currentSongIndex = Random.nextInt(0, songQueue.size - 1)
|
||||
} else if (!parseRepeatMode(repeatMode)) {
|
||||
if ((currentSongIndex!! + 1) == songQueue.size) {
|
||||
currentSongIndex = 0
|
||||
}
|
||||
else {
|
||||
currentSongIndex = currentSongIndex!! + 1
|
||||
}
|
||||
currentSongIndex = if ((currentSongIndex!! + 1) == songQueue.size)
|
||||
0 else currentSongIndex!! + 1
|
||||
}
|
||||
|
||||
currentSong = songQueue[currentSongIndex!!]
|
||||
@@ -236,26 +228,22 @@ class MusicService(var appPath: String = ""): Service() {
|
||||
|
||||
val token = tokenRepo.retrieveToken(appPath)
|
||||
val apiInfo = apiRepo.retrieveRecord(appPath)
|
||||
// val songs = trackRepo.fetchSongs(token, apiInfo.uri)
|
||||
val songs = trackRepo.fetchSongsIncludingDownloaded(token, apiInfo.uri, appPath)
|
||||
songQueue = songs.toMutableList()
|
||||
|
||||
if (retrieveShuffleMode()!!) {
|
||||
currentSongIndex = Random.nextInt(0, songs.size - 1)
|
||||
} else {
|
||||
currentSongIndex = 0
|
||||
}
|
||||
currentSongIndex = if (retrieveShuffleMode()!!)
|
||||
Random.nextInt(0, songQueue.size - 1) else 0
|
||||
|
||||
currentSong = songQueue[currentSongIndex!!]
|
||||
if (currentSong.downloaded) {
|
||||
trackPlayer!!.setDataSource(currentSong.path)
|
||||
}
|
||||
else {
|
||||
|
||||
trackPlayer!!.setDataSource(this,
|
||||
APIRepository.retrieveSongStreamUri(apiInfo, currentSong),
|
||||
APIRepository.retrieveSongStreamHeader(token))
|
||||
}
|
||||
|
||||
trackPlayer!!.prepareAsync()
|
||||
trackPlayer!!.setOnCompletionListener {
|
||||
playNextTrack()
|
||||
@@ -269,25 +257,19 @@ class MusicService(var appPath: String = ""): Service() {
|
||||
|
||||
private fun retrieveShuffleMode(): Boolean? {
|
||||
val shuffleRepo = ShuffleRepository(null)
|
||||
val shuffleMode = shuffleRepo.shuffleMode(appPath)
|
||||
var shuffleOn = false
|
||||
|
||||
when (shuffleMode) {
|
||||
ShuffleTypes.ShuffleOn -> shuffleOn = true
|
||||
ShuffleTypes.ShuffleOff -> shuffleOn = false
|
||||
return when (shuffleRepo.shuffleMode(appPath)) {
|
||||
ShuffleTypes.ShuffleOn -> true
|
||||
ShuffleTypes.ShuffleOff -> false
|
||||
else -> false
|
||||
}
|
||||
|
||||
return shuffleOn
|
||||
}
|
||||
|
||||
private fun parseRepeatMode(repeatMode: RepeatTypes): Boolean {
|
||||
var repeatOn: Boolean? = null
|
||||
when (repeatMode) {
|
||||
RepeatTypes.RepeatOff -> repeatOn = false
|
||||
RepeatTypes.RepeatSong -> repeatOn = true
|
||||
return when (repeatMode) {
|
||||
RepeatTypes.RepeatOff -> false
|
||||
RepeatTypes.RepeatSong -> true
|
||||
}
|
||||
|
||||
return repeatOn!!
|
||||
}
|
||||
|
||||
private fun offlinePlaySong(song: Song) {
|
||||
|
||||
@@ -22,8 +22,9 @@ class TrackRepository(var context: Context? = null) {
|
||||
private external fun retrieveSongsIncludingDownloaded(token: Token, uri: String, path: String): Array<Song>
|
||||
|
||||
private external fun retrieveSong(token: Token, song: Song, uri: String): Song
|
||||
private external fun downloadSong(token: Token, song: Song, path: String): Song
|
||||
|
||||
private external fun downloadSong(token: Token, song: Song, path: String)
|
||||
private external fun deleteSong(song: Song, path: String)
|
||||
|
||||
|
||||
fun fetchSongs(token: Token, uri: String): Array<Song> {
|
||||
@@ -43,8 +44,11 @@ class TrackRepository(var context: Context? = null) {
|
||||
downloadSong(token, song, uri)
|
||||
}
|
||||
|
||||
fun download(token: Token, song: Song, path: String) {
|
||||
downloadSong(token, song, path)
|
||||
val s = 4
|
||||
fun download(token: Token, song: Song, path: String): Song {
|
||||
return downloadSong(token, song, path)
|
||||
}
|
||||
|
||||
fun delete(song: Song, path: String) {
|
||||
deleteSong(song, path)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user