Link Oeffnen und Files speichern von Krimi Dinner zurueck uebertragen

This commit is contained in:
Ronja 2023-06-23 10:24:38 +02:00
parent fb6d8fdc3a
commit ab5d284b80
3 changed files with 148 additions and 4 deletions

View File

@ -1,5 +1,8 @@
#include "AndroidAssetManager.h" #include "AndroidAssetManager.h"
#include "Log.h" #include "Log.h"
#include "NativeEngine.h"
#include <iostream>
#include <fstream>
#include <game-activity/native_app_glue/android_native_app_glue.h> #include <game-activity/native_app_glue/android_native_app_glue.h>
@ -26,15 +29,114 @@ bool AndroidAssetManager::loadFile(const char* path, FileBuffer* p_file_buffer)
return false; return false;
} }
auto length = static_cast<size_t>(AAsset_getLength64(asset)); auto length = static_cast<size_t>(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; p_file_buffer->internal = asset;
return true; return true;
} }
void AndroidAssetManager::releaseFileBuffer(FileBuffer& fb) void AndroidAssetManager::releaseFileBuffer(FileBuffer& fb)
{ {
AAsset_close(reinterpret_cast<AAsset*>(fb.internal)); if (fb.internal != nullptr){
AAsset_close(reinterpret_cast<AAsset*>(fb.internal));
}
fb.internal = nullptr; fb.internal = nullptr;
fb.data = nullptr; fb.data = nullptr;
fb.size = 0; 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;
} }

View File

@ -16,6 +16,16 @@ public:
void releaseFileBuffer(FileBuffer& fb) override; 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: private:
AndroidAssetManager(); AndroidAssetManager();
~AndroidAssetManager(); ~AndroidAssetManager();

View File

@ -1,7 +1,16 @@
package tech.recreational.kde; 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.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat; import androidx.core.view.WindowInsetsControllerCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import com.google.androidgamesdk.GameActivity; import com.google.androidgamesdk.GameActivity;
@ -10,14 +19,37 @@ public class KDEActivity extends GameActivity {
System.loadLibrary("kde"); 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() { protected void onResume() {
super.onResume(); super.onResume();
hideSystemBars(); hideSystemBars();
} }
private void hideSystemBars() { private void hideSystemBars() {
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController( window = getWindow();
getWindow().getDecorView()); View view = window.getDecorView();
WindowInsetsControllerCompat windowInsetsController = ViewCompat
.getWindowInsetsController(view);
if (windowInsetsController == null) if (windowInsetsController == null)
return; return;
windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);