diff --git a/app/src/main/cpp/AndroidAssetManager.cpp b/app/src/main/cpp/AndroidAssetManager.cpp index b74505e..f8b369f 100644 --- a/app/src/main/cpp/AndroidAssetManager.cpp +++ b/app/src/main/cpp/AndroidAssetManager.cpp @@ -1,5 +1,8 @@ #include "AndroidAssetManager.h" #include "Log.h" +#include "NativeEngine.h" +#include +#include #include @@ -26,15 +29,114 @@ bool AndroidAssetManager::loadFile(const char* path, FileBuffer* p_file_buffer) return false; } auto length = static_cast(AAsset_getLength64(asset)); - p_file_buffer->data = buffer, p_file_buffer->size = length; + p_file_buffer->data = buffer; + p_file_buffer->size = length; p_file_buffer->internal = asset; return true; } void AndroidAssetManager::releaseFileBuffer(FileBuffer& fb) { - AAsset_close(reinterpret_cast(fb.internal)); + if (fb.internal != nullptr){ + AAsset_close(reinterpret_cast(fb.internal)); + } fb.internal = nullptr; fb.data = nullptr; fb.size = 0; +} + +bool AndroidAssetManager::writeFile(const char* path, const void* data, size_t size) +{ + std::string pfad_komplett = "data/data/tech.recreational.kde/" + std::string(path); + std::ofstream file; + file.open(pfad_komplett, std::fstream::in | std::fstream::out | std::fstream::trunc); + if (file.is_open()) { + //file << data; + if (!file.good()){ + return false; + } + if (file.bad()){ + return false; + } + if (file.fail()){ + return false; + } + if (file.eof()){ + return false; + } + file.clear(); + file.write((const char*)data, size); + auto flags = file.flags(); + if (!file.good()){ + return false; + } + if (file.bad()){ + return false; + } + if (file.fail()){ + return false; + } + if (file.eof()){ + return false; + } + std::streamsize size = file.width(); + file.close(); + std::cout << "Datei erfolgreich erstellt und beschrieben." << std::endl; + } else { + std::cout << "Fehler beim Erstellen der Datei." << std::endl; + } + return true; +} + +size_t AndroidAssetManager::getInternFileSize(const char* path){ + std::string dir = "data/data/tech.recreational.kde/" + std::string(path); + std::ifstream myfile(dir); + std::string line; + size_t size = 0; + if (myfile.is_open()) { + while (std::getline(myfile, line)) { + size += line.size(); + } + myfile.close(); + } + return size; +} + +bool AndroidAssetManager::loadInternFile(const char* path, FileBuffer* p_file_buffer) +{ + p_file_buffer->internal = nullptr; + p_file_buffer->data = nullptr; + p_file_buffer->size = 0; + size_t size = getInternFileSize(path); + if (size == 0){ + return false; + } + std::string dir = "data/data/tech.recreational.kde/" + std::string(path); + std::ifstream myfile(dir); + //std::string line; + if (myfile.is_open()) + { + p_file_buffer->size = size; + char* data = (char*)malloc(size + 1); + if (data == nullptr){ + myfile.clear(); + myfile.close(); + return false; + } + //myfile.get(data, size); + std::string line; + int pos = 0; + while (std::getline(myfile, line)) { + size_t line_size = line.size(); + const char* line_text = line.c_str(); + memcpy(&data[pos], line_text, line_size); + pos += line_size; + } + data[pos] = '\0'; + p_file_buffer->data = data; + myfile.clear(); + myfile.close(); + return true; + } + return false; } \ No newline at end of file diff --git a/app/src/main/cpp/AndroidAssetManager.h b/app/src/main/cpp/AndroidAssetManager.h index 85cb1d3..a48db4b 100644 --- a/app/src/main/cpp/AndroidAssetManager.h +++ b/app/src/main/cpp/AndroidAssetManager.h @@ -16,6 +16,16 @@ public: void releaseFileBuffer(FileBuffer& fb) override; + /// @brief Write data to a file. + /// @param path Path relative to the asset root directory + /// @param data Data buffer + /// @param size Number of bytes + /// @return @c true if writing the file was successfull, @c false otherwise + bool writeFile(const char* path, const void* data, size_t size); + + bool loadInternFile(const char* path, FileBuffer* p_file_buffer); + size_t getInternFileSize(const char* path); + private: AndroidAssetManager(); ~AndroidAssetManager(); diff --git a/app/src/main/java/tech/recreational/kde/KDEActivity.java b/app/src/main/java/tech/recreational/kde/KDEActivity.java index 6d9c141..e7ed5d0 100644 --- a/app/src/main/java/tech/recreational/kde/KDEActivity.java +++ b/app/src/main/java/tech/recreational/kde/KDEActivity.java @@ -1,7 +1,16 @@ package tech.recreational.kde; +import android.app.Application; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.view.View; +import android.view.Window; + import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsControllerCompat; import androidx.core.view.WindowInsetsCompat; +import androidx.fragment.app.FragmentManager; import com.google.androidgamesdk.GameActivity; @@ -10,14 +19,37 @@ public class KDEActivity extends GameActivity { System.loadLibrary("kde"); } + static Window window; + + public void openLink(String url){ + try { + Uri uri = Uri.parse(url); + View view = window.getDecorView().getRootView(); + Context context = view.getContext(); + Intent browserIntent = new Intent(Intent.ACTION_VIEW); + browserIntent.setData(uri); + browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + PackageManager manager = context.getPackageManager(); + // Verify that the intent will resolve to an activity + if (browserIntent.resolveActivity(manager) != null) { + // Here we use an intent without a Chooser unlike the next example + context.startActivity(browserIntent); + } + } + catch (Exception e){ + } + } + protected void onResume() { super.onResume(); hideSystemBars(); } private void hideSystemBars() { - WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController( - getWindow().getDecorView()); + window = getWindow(); + View view = window.getDecorView(); + WindowInsetsControllerCompat windowInsetsController = ViewCompat + .getWindowInsetsController(view); if (windowInsetsController == null) return; windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);