From 6221969d6b50b88092b117d29bccb7a1ecd5ccd2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 28 Oct 2019 21:47:04 -0400 Subject: [PATCH] Working on implementing the Toggle controls like Repeat and Shuffle. Having a little issues with Repeat, haven't started with Shuffle --- app/src/main/cpp/BaseRepository.cpp | 5 +- app/src/main/cpp/Demo.cpp | 60 +++++++- app/src/main/cpp/Demo.h | 12 +- app/src/main/cpp/RepeatRepository.cpp | 6 +- app/src/main/cpp/RepeatRepository.h | 2 +- app/src/main/cpp/RepeatTypes.h | 2 +- app/src/main/cpp/SongRepository.cpp | 144 +----------------- app/src/main/cpp/SongRepository.h | 3 - app/src/main/cpp/model/Song.h | 1 + .../example/mear/activities/MainActivity.kt | 25 ++- .../java/com/example/mear/constants/CPPLib.kt | 5 + .../mear/repositories/APIRepository.kt | 3 +- .../mear/repositories/RepeatRepository.kt | 37 ++++- .../mear/repositories/ShuffleRepository.kt | 5 + 14 files changed, 144 insertions(+), 166 deletions(-) create mode 100644 app/src/main/java/com/example/mear/constants/CPPLib.kt diff --git a/app/src/main/cpp/BaseRepository.cpp b/app/src/main/cpp/BaseRepository.cpp index 2a1f01a..a36ef08 100644 --- a/app/src/main/cpp/BaseRepository.cpp +++ b/app/src/main/cpp/BaseRepository.cpp @@ -29,10 +29,13 @@ namespace repository { namespace local { const auto dbPath = pathOfDatabase(appPath); SQLite::Database db(dbPath, SQLite::OPEN_READONLY); - return db.tableExists(m_tableName); + auto result = db.tableExists(m_tableName); + return result; } catch (std::exception& ex) { auto msg = ex.what(); } + + return false; } bool BaseRepository::isTableEmpty(const std::string& appPath) diff --git a/app/src/main/cpp/Demo.cpp b/app/src/main/cpp/Demo.cpp index e2730a7..e5e6db0 100644 --- a/app/src/main/cpp/Demo.cpp +++ b/app/src/main/cpp/Demo.cpp @@ -18,13 +18,13 @@ #include #include #include +#include +#include +#include #include -#include -#include -#include - #include "APIRepository.h" +#include "RepeatRepository.h" #include "SongRepository.h" #include "Tok.h" #include "TokenRepository.h" @@ -75,6 +75,12 @@ jobject songToObj(JNIEnv *env, const model::Song& song) } +std::string jstringToString(JNIEnv *env, jstring& val) +{ + return env->GetStringUTFChars(val, nullptr); +} + + model::APIInfo retrieveAPIInfo(const std::string& path) { repository::local::APIRepository apiRepo; @@ -127,6 +133,19 @@ model::User retrieveCredentials(const std::string& dataPath) } +int retrieveRepeatMode(const std::string& path) +{ + repository::local::RepeatRepository repeatRepo; + if (!repeatRepo.doesTableExist(path)) { + repeatRepo.createRepeatTable(path); + } + + auto repeatMode = repeatRepo.retrieveRepeatMode(path); + + return static_cast(repeatMode); +} + + bool doesDatabaseExist(const std::string& dataPath) { repository::local::UserRepository userRepo; @@ -207,6 +226,12 @@ void saveToken(const model::Token& token, const std::string& path) tokenRepo.saveToken(token, path); } +void updateRepeatMode(const std::string& path) +{ + repository::local::RepeatRepository repeatRepo; + repeatRepo.updateRepeat(path); +} + extern "C" JNIEXPORT jobjectArray @@ -380,6 +405,21 @@ Java_com_example_mear_repositories_UserRepository_logUser( } +extern "C" +JNIEXPORT jint +JNICALL +Java_com_example_mear_repositories_RepeatRepository_retrieveRepeatMode( + JNIEnv *env, + jobject thisObj, + jstring pathStr + ) { + const auto dataPath = jstringToString(env, pathStr); + auto repeatMode = retrieveRepeatMode(dataPath); + + return repeatMode; +} + + extern "C" JNIEXPORT jboolean JNICALL @@ -501,3 +541,15 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord( saveToken(token, path); } + +extern "C" +JNIEXPORT void +JNICALL +Java_com_example_mear_repositories_RepeatRepository_updateRepeatMode( + JNIEnv *env, + jobject thisObj, + jstring pathStr + ) { + const auto dataPath = jstringToString(env, pathStr); + updateRepeatMode(dataPath); +} \ No newline at end of file diff --git a/app/src/main/cpp/Demo.h b/app/src/main/cpp/Demo.h index 0d90039..435215b 100644 --- a/app/src/main/cpp/Demo.h +++ b/app/src/main/cpp/Demo.h @@ -19,24 +19,30 @@ std::vector retrieveSongs(const model::Token&, const std::string&); jobject songToObj(JNIEnv *env, const model::Song&); +std::string jstringToString(JNIEnv*, jstring&); + model::APIInfo retrieveAPIInfo(const std::string&); model::Song retrieveSong(const std::string&, const std::string&, const int); model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&); -model::Token fettchToken(const model::User&, const std::string&); +model::Token fetchToken(const model::User&, const std::string&); model::Token retrieveSavedToken(const std::string&); -void saveToken(const model::Token&, const std::string&); model::User retrieveCredentials(const std::string&); +int retrieveRepeatMode(const std::string&); + bool doesDatabaseExist(const std::string&); bool apiInformationExist(const std::string&); bool doesTokenExist(const std::string&); bool userCredentialExist(const std::string&); -void saveApiInfo(const model::APIInfo&, const std::string&); +void saveAPIInfo(const model::APIInfo&, const std::string&); void saveCredentials(const model::User&, const std::string&); +void saveToken(const model::Token&, const std::string&); +void updateRepeatMode(const std::string&); + #endif //MEAR_DEMO_H diff --git a/app/src/main/cpp/RepeatRepository.cpp b/app/src/main/cpp/RepeatRepository.cpp index 80bbf17..8724713 100644 --- a/app/src/main/cpp/RepeatRepository.cpp +++ b/app/src/main/cpp/RepeatRepository.cpp @@ -21,7 +21,9 @@ namespace repository { namespace local { const auto result = query.executeStep(); - return static_cast(query.getColumn(1).getInt()); + auto repeatType = query.getColumn(1).getInt(); + auto val = static_cast(result); + return val; } catch (std::exception& ex) { auto msg = ex.what(); } @@ -83,5 +85,5 @@ namespace repository { namespace local { } - std::string RepeatRepository::repeatTable() { return "Repeat"; } + std::string RepeatRepository::repeatTable() noexcept { return "Repeat"; } }} diff --git a/app/src/main/cpp/RepeatRepository.h b/app/src/main/cpp/RepeatRepository.h index 4e9306f..9bf2e7f 100644 --- a/app/src/main/cpp/RepeatRepository.h +++ b/app/src/main/cpp/RepeatRepository.h @@ -20,7 +20,7 @@ namespace repository { namespace local { void createRepeatTable(const std::string&); void updateRepeat(const std::string&); private: - constexpr std::string repeatTable() noexcept; + std::string repeatTable() noexcept; }; }} diff --git a/app/src/main/cpp/RepeatTypes.h b/app/src/main/cpp/RepeatTypes.h index 666682a..824500b 100644 --- a/app/src/main/cpp/RepeatTypes.h +++ b/app/src/main/cpp/RepeatTypes.h @@ -8,7 +8,7 @@ enum class RepeatTypes { RepeatSong = 0, - RepeatOff + RepeatOff = 1 }; diff --git a/app/src/main/cpp/SongRepository.cpp b/app/src/main/cpp/SongRepository.cpp index 04c234d..6900dc2 100644 --- a/app/src/main/cpp/SongRepository.cpp +++ b/app/src/main/cpp/SongRepository.cpp @@ -50,6 +50,7 @@ namespace repository { songJson["artist"].get(), songJson["album"].get(), songJson["genre"].get(), songJson["duration"].get(), songJson["year"].get()); + song.coverArtId = songJson["coverart_id"].get(); songs.push_back(song); } @@ -59,148 +60,6 @@ namespace repository { } - [[deprecated]] - model::Song SongRepository::retrieveSong(const std::string& token, const int id) { - std::string uri(""); - uri.append(std::to_string(id)); - model::Song song; - - CURL *curl; - CURLcode res; - struct curl_slist *chunk = NULL; - curl = curl_easy_init(); - - if (!curl) { - return song; - } - - auto resp = std::make_unique(10000); - - std::string authInfo("Authorization: Bearer "); - authInfo.append(token); - chunk = curl_slist_append(chunk, authInfo.c_str()); - - curl_easy_setopt(curl, CURLOPT_URL, uri.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - - if (res == CURLE_OK) { - auto s = nlohmann::json::parse(resp.get()); - song.id = s["id"].get(); - song.title = s["title"].get(); - song.album = s["album"].get(); - song.artist = s["artist"].get(); - song.genre = s["genre"].get(); - song.duration = s["duration"].get(); - song.year = s["year"].get(); - - return song; - } - - return song; - } - - [[deprecated]] - model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri, - const int id) { - std::string uri(baseUri); - uri.append("/api/v1/song/"); - uri.append(std::to_string(id)); - model::Song song; - - CURL *curl; - CURLcode res; - struct curl_slist *chunk = NULL; - curl = curl_easy_init(); - - if (!curl) { - return song; - } - - auto resp = std::make_unique(10000); - - std::string authInfo("Authorization: Bearer "); - authInfo.append(token); - chunk = curl_slist_append(chunk, authInfo.c_str()); - - curl_easy_setopt(curl, CURLOPT_URL, uri.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - - if (res == CURLE_OK) { - auto s = nlohmann::json::parse(resp.get()); - song.id = s["id"].get(); - song.title = s["title"].get(); - song.album = s["album"].get(); - song.artist = s["artist"].get(); - song.genre = s["genre"].get(); - song.duration = s["duration"].get(); - song.year = s["year"].get(); - - return song; - } - - return song; - } - - [[deprecated]] - model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri, - const model::Song& sng) { - std::string uri(baseUri); - uri.append("/api/v1/song/"); - uri.append(std::to_string(sng.id)); - model::Song song; - - CURL *curl; - CURLcode res; - struct curl_slist *chunk = NULL; - curl = curl_easy_init(); - - if (!curl) { - return sng; - } - - auto resp = std::make_unique(10000); - - std::string authInfo("Authorization: Bearer "); - authInfo.append(token); - chunk = curl_slist_append(chunk, authInfo.c_str()); - - curl_easy_setopt(curl, CURLOPT_URL, uri.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - - if (res == CURLE_OK) { - auto s = nlohmann::json::parse(resp.get()); - song.id = s["id"].get(); - song.title = s["title"].get(); - song.album = s["album"].get(); - song.artist = s["artist"].get(); - song.genre = s["genre"].get(); - song.duration = s["duration"].get(); - song.year = s["year"].get(); - - return song; - } - - return sng; - } - model::Song SongRepository::retrieveSong(const model::Token& token, const model::Song& sng, const std::string& baseUri) { std::string uri(baseUri); @@ -241,6 +100,7 @@ namespace repository { song.genre = s["genre"].get(); song.duration = s["duration"].get(); song.year = s["year"].get(); + song.coverArtId = s["coverart_id"].get(); return song; } diff --git a/app/src/main/cpp/SongRepository.h b/app/src/main/cpp/SongRepository.h index 9056778..1f88514 100644 --- a/app/src/main/cpp/SongRepository.h +++ b/app/src/main/cpp/SongRepository.h @@ -17,9 +17,6 @@ namespace repository { public: std::vector fetchSongs(const model::Token&, const std::string&); - model::Song retrieveSong(const std::string&, const int); - model::Song retrieveSong(const std::string&, const std::string&, const int); - model::Song retrieveSong(const std::string&, const std::string&, const model::Song&); model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&); private: static size_t respBodyRetriever(void*, size_t, size_t, char*); diff --git a/app/src/main/cpp/model/Song.h b/app/src/main/cpp/model/Song.h index f7090d6..2de8619 100644 --- a/app/src/main/cpp/model/Song.h +++ b/app/src/main/cpp/model/Song.h @@ -34,6 +34,7 @@ public: std::string genre; int duration; int year; + int coverArtId; }; } diff --git a/app/src/main/java/com/example/mear/activities/MainActivity.kt b/app/src/main/java/com/example/mear/activities/MainActivity.kt index 3d6abde..87ecac0 100644 --- a/app/src/main/java/com/example/mear/activities/MainActivity.kt +++ b/app/src/main/java/com/example/mear/activities/MainActivity.kt @@ -164,12 +164,13 @@ class MainActivity : BaseServiceActivity() { } } private fun initializeRepeat() { - val repeatMode = RepeatRepository(this).getRepeatMode() + val repeatRepo = RepeatRepository(null) + val repeatMode = repeatRepo.repeatMode(appDirectory()) when (repeatMode) { - ControlTypes.REPEAT_ON -> { + RepeatRepository.RepeatTypes.RepeatSong -> { RepeatTrack.text = resources.getText(R.string.repeat_on) } - ControlTypes.REPEAT_OFF -> { + RepeatRepository.RepeatTypes.RepeatOff -> { RepeatTrack.text = resources.getText(R.string.repeat_off) } } @@ -197,6 +198,7 @@ class MainActivity : BaseServiceActivity() { ShuffleRepository(this).updateShuffleMode(playC) } private fun toggleRepeat() { + /** val repeatText = RepeatTrack.text.toString() val on = resources.getString(R.string.repeat_on) val off = resources.getString(R.string.repeat_off) @@ -216,6 +218,23 @@ class MainActivity : BaseServiceActivity() { } val playC = PlayControls(null, repeatOn) RepeatRepository(this).updateRepeatMode(playC) + */ + val repeatRepo = RepeatRepository(null) + + val appPath = appDirectory() + repeatRepo.alterRepeatMode(appPath) + val repeatMode = repeatRepo.repeatMode(appPath) + + when (repeatMode) { + RepeatRepository.RepeatTypes.RepeatOff -> { + repeatOn = false + RepeatTrack.text = resources.getText(R.string.repeat_off) + } + RepeatRepository.RepeatTypes.RepeatSong -> { + repeatOn = true + RepeatTrack.text = resources.getText(R.string.repeat_on) + } + } } private fun playSongTrack() { diff --git a/app/src/main/java/com/example/mear/constants/CPPLib.kt b/app/src/main/java/com/example/mear/constants/CPPLib.kt new file mode 100644 index 0000000..1950c22 --- /dev/null +++ b/app/src/main/java/com/example/mear/constants/CPPLib.kt @@ -0,0 +1,5 @@ +package com.example.mear.constants + +object CPPLib { + const val NATIVE_LIB = "native-lib" +} \ No newline at end of file diff --git a/app/src/main/java/com/example/mear/repositories/APIRepository.kt b/app/src/main/java/com/example/mear/repositories/APIRepository.kt index ee95b00..dbca99a 100644 --- a/app/src/main/java/com/example/mear/repositories/APIRepository.kt +++ b/app/src/main/java/com/example/mear/repositories/APIRepository.kt @@ -1,6 +1,7 @@ package com.example.mear.repositories import android.net.Uri +import com.example.mear.constants.CPPLib import com.example.mear.models.APIInfo import com.example.mear.models.Song import com.example.mear.models.Token @@ -9,7 +10,7 @@ class APIRepository: BaseRepository() { companion object { init { - System.loadLibrary("native-lib") + System.loadLibrary(CPPLib.NATIVE_LIB) } diff --git a/app/src/main/java/com/example/mear/repositories/RepeatRepository.kt b/app/src/main/java/com/example/mear/repositories/RepeatRepository.kt index 56e386a..a88b25e 100644 --- a/app/src/main/java/com/example/mear/repositories/RepeatRepository.kt +++ b/app/src/main/java/com/example/mear/repositories/RepeatRepository.kt @@ -5,18 +5,24 @@ import android.content.Context import org.jetbrains.anko.db.* import com.example.mear.constants.ControlTypes +import com.example.mear.constants.CPPLib import com.example.mear.database import com.example.mear.models.PlayControls -class RepeatRepository { - var context: Context? = null +class RepeatRepository(var context: Context?) { - - constructor(context: Context) { - this.context = context + companion object { + init { + System.loadLibrary(CPPLib.NATIVE_LIB) + } } + private external fun retrieveRepeatMode(path: String): Int + + private external fun updateRepeatMode(path: String) + + fun getRepeatMode(): String = context!!.database.use { select("Repeat").limit(1) .parseSingle(object: MapRowParser{ @@ -27,6 +33,18 @@ class RepeatRepository { }) } + fun repeatMode(path: String): RepeatTypes { + val repeatType = RepeatTypes.valueOf(retrieveRepeatMode(path)) + + return repeatType!! + } + + + fun alterRepeatMode(path: String) { + updateRepeatMode(path) + } + + fun updateRepeatMode(playControls: PlayControls?) = context!!.database.use { var repeatMode = ControlTypes.REPEAT_OFF if (playControls!!.repeatOn!!) { @@ -35,4 +53,13 @@ class RepeatRepository { update("Repeat", "Mode" to repeatMode).exec() } + + enum class RepeatTypes(val value: Int) { + RepeatSong(0), + RepeatOff(1); + + companion object { + fun valueOf(value: Int) = values().find { it.value == value } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/mear/repositories/ShuffleRepository.kt b/app/src/main/java/com/example/mear/repositories/ShuffleRepository.kt index 3feb6dd..073c61b 100644 --- a/app/src/main/java/com/example/mear/repositories/ShuffleRepository.kt +++ b/app/src/main/java/com/example/mear/repositories/ShuffleRepository.kt @@ -17,6 +17,11 @@ class ShuffleRepository { } + companion object { + init { + System.loadLibrary("native-lib") + } + } fun getShuffleMode(): String = context!!.database.use { select("Shuffle").limit(1) .parseSingle(object : MapRowParser {