Skip to content

Commit

Permalink
android: fixes compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
s.kushnirenko committed Jan 18, 2024
1 parent b32183c commit 6793443
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 33 deletions.
2 changes: 2 additions & 0 deletions android/akhenaten/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,6 @@ dependencies {
}
}
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'com.google.firebase:firebase-firestore:24.10.0'
}
29 changes: 13 additions & 16 deletions android/akhenaten/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature android:glEsVersion="0x00020000" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.hardware.gamepad"
android:required="false" />
<uses-feature
android:name="android.hardware.audio.output"
android:required="false" />
<uses-feature
android:name="android.hardware.type.pc"
android:required="false" />
<uses-feature
android:name="android.hardware.usb.host"
android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.gamepad" android:required="false" />
<uses-feature android:name="android.hardware.audio.output" android:required="false" />
<uses-feature android:name="android.hardware.type.pc" android:required="false" />
<uses-feature android:name="android.hardware.usb.host" android:required="false" />

<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/akhenaten"
android:requestLegacyExternalStorage="true"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.dalerank.akhenaten;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import org.libsdl.app.SDLActivity;

Expand All @@ -11,7 +14,7 @@ public class AkhenatenMainActivity extends SDLActivity {
@Override
public void onStop() {
super.onStop();
releaseAssetManager();
//releaseAssetManager();
FileManager.clearCache();
}

Expand All @@ -29,6 +32,29 @@ public void showDirectorySelection(boolean again) {
startActivityForResult(DirectorySelectionActivity.newIntent(this, again), GET_FOLDER_RESULT);
}

protected boolean shouldAskPermissions() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

@TargetApi(23)
protected void askPermissions() {
String[] permissions = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
int requestCode = 200;
requestPermissions(permissions, requestCode);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (shouldAskPermissions()) {
askPermissions();
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_FOLDER_RESULT) {
if (resultCode == RESULT_OK && data != null && data.getData() != null) {
Expand All @@ -47,5 +73,5 @@ public float getScreenDensity() {
}

private native void gotDirectory();
private native void releaseAssetManager();
//private native void releaseAssetManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;

import java.io.File;
import android.util.Log;

import java.util.ArrayList;
Expand All @@ -17,8 +20,50 @@ public class FileManager {
private static final int FILE_TYPE_DIR = 1;
private static final int FILE_TYPE_FILE = 2;

private static String decodeUrl(String encodedUrl) {
StringBuilder decodedUrlBuilder = new StringBuilder();
char[] charArray = encodedUrl.toCharArray();

for (int i = 0; i < charArray.length; i++) {
char currentChar = charArray[i];

if (currentChar == '%' && i + 2 < charArray.length) {
// Check if there are two characters following '%' for a valid encoded sequence
char hex1 = charArray[i + 1];
char hex2 = charArray[i + 2];

try {
// Convert the two hexadecimal characters to a decimal value and append the corresponding character
int decimalValue = Integer.parseInt("" + hex1 + hex2, 16);
decodedUrlBuilder.append((char) decimalValue);
i += 2; // Skip the two characters processed
} catch (NumberFormatException e) {
// Ignore invalid encoded sequence
decodedUrlBuilder.append(currentChar);
}
} else {
// Append the character as it is
decodedUrlBuilder.append(currentChar);
}
}

return decodedUrlBuilder.toString();
}

@SuppressWarnings("unused")
public static String getAkhenatenPath() {
public static String getPharaohPath(AkhenatenMainActivity activity) {
String decodedPath = decodeUrl(baseUri.toString());
baseUri = Uri.parse(decodedPath);

//try {
// String[] split = baseUri.toString().split(":");
// String absPath = Environment.getExternalStorageDirectory().toString();
// String absDirName = absPath + File.separator + split[2];
// return absDirName;
//} catch (Exception e) {
// Log.e("akhenaten", "Error in getPharaohPath: " + e);
//}

return baseUri.toString();
}

Expand Down Expand Up @@ -157,7 +202,7 @@ public static boolean deleteFile(AkhenatenMainActivity activity, String filePath
}
return DocumentsContract.deleteDocument(activity.getContentResolver(), fileInfo.getUri());
} catch (Exception e) {
Log.e("augustus", "Error in deleteFile: " + e);
Log.e("akhenaten", "Error in deleteFile: " + e);
return false;
}
}
Expand Down Expand Up @@ -214,7 +259,7 @@ public static int openFileDescriptor(AkhenatenMainActivity activity, String file
ParcelFileDescriptor pfd = activity.getContentResolver().openFileDescriptor(fileUri, internalMode);
return (pfd == null) ? 0 : pfd.detachFd();
} catch (Exception e) {
Log.e("augustus", "Error in openFileDescriptor: " + e);
Log.e("akhenaten", "Error in openFileDescriptor: " + e);
return 0;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/content/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,17 @@ vfs::path content_path(const char *path) {
}

vfs::path content_file(const char *filepath) {
bstring256 corrected_filename = content_path(filepath);
vfs::path corrected_filename = content_path(filepath);
#ifndef GAME_PLATFORM_ANDROID
bool exists = std::filesystem::exists(corrected_filename.c_str());
if (exists) {
return corrected_filename;
}

return vfs::path();
#else
return corrected_filename;
#endif
}

const dir_listing *dir_append_files_with_extension(const char *dir, const char *extension) {
Expand Down
2 changes: 1 addition & 1 deletion src/game/mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ bool game_load_campaign_file() {
for (int i = 0; i < MAX_MISSION_CHOICE_BRANCHES; ++i) {
// TODO warning: passing NULL to non-pointer argument
bool end_of_line = (index_of(ptr, '\n', line_size) == 1
|| index_of(ptr, NULL, line_size) == 1);
|| index_of(ptr, '\0', line_size) == 1);
if (!end_of_line)
ptr = get_value(ptr, endl, &step->path_ids[i]);
else
Expand Down
4 changes: 3 additions & 1 deletion src/io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "content/vfs.h"

int io_read_file_into_buffer(const char* filepath, int localizable, buffer* buf, int max_size) {
if (buf == nullptr)
if (buf == nullptr) {
return 0;
}

vfs::path fs_file = vfs::content_file(filepath);
if (fs_file.empty()) {
return 0;
Expand Down
27 changes: 26 additions & 1 deletion src/platform/ahhenaten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <SDL.h>

#include <set>
#include <platform/android/android.h>

#include "imgui.h"
#include "imgui_impl_sdl.h"
Expand Down Expand Up @@ -353,6 +354,9 @@ static void setup(Arguments& args) {

// pre-init engine: assert game directory, pref files, etc.
init_game_environment(ENGINE_ENV_PHARAOH);
#if defined(GAME_PLATFORM_ANDROID)
bool again = false;
#endif // GAME_PLATFORM_ANDROID
while (!pre_init(args.get_data_directory())) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Warning",
Expand All @@ -366,7 +370,28 @@ static void setup(Arguments& args) {
#endif
nullptr);
#if defined(GAME_PLATFORM_ANDROID)
; // do nothing
if (again) {
const SDL_MessageBoxButtonData buttons[] = {
{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "OK"},
{SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel"}
};
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_WARNING, NULL, "Wrong folder selected",
"The selected folder is not a proper Pharaoh folder.\n\n"
"Please select a path directly from either the internal storage "
"or the SD card, otherwise the path may not be recognised.\n\n"
"Press OK to select another folder or Cancel to exit.",
SDL_arraysize(buttons), buttons, NULL
};
int result;
SDL_ShowMessageBox(&messageboxdata, &result);
if (!result) {
exit(-2);
}
}
again = true;
pcstr user_dir = android_show_pharaoh_path_dialog(again);
args.set_data_directory(user_dir);
#else
show_options_window(args);
#endif
Expand Down
13 changes: 7 additions & 6 deletions src/platform/android/android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
#include <SDL.h>
#include <string.h>

static int has_directory;
static bstring256 path;
static volatile int has_directory;
static vfs::path path;

static const char *get_pharaoh_path(void)
static const char *get_pharaoh_path()
{
jni_function_handler handler;
if (!jni_get_static_method_handler(CLASS_FILE_MANAGER, "getPharaohPath", "()Ljava/lang/String;", &handler)) {
if (!jni_get_static_method_handler(CLASS_FILE_MANAGER, "getPharaohPath", "(L" CLASS_AKHENATEN_ACTIVITY ";)Ljava/lang/String;", &handler)) {
jni_destroy_function_handler(&handler);
return NULL;
}

jobject result = handler.env->CallStaticObjectMethod(handler.nclass, handler.method);
jobject result = handler.env->CallStaticObjectMethod(handler.nclass, handler.method, handler.activity);
const char *temp_path = handler.env->GetStringUTFChars((jstring) result, NULL);
path = temp_path;
handler.env->ReleaseStringUTFChars((jstring) result, temp_path);
handler.env->DeleteLocalRef(result);

jni_destroy_function_handler(&handler);

return !path.empty() ? path.c_str() : nullptr;
Expand Down Expand Up @@ -169,7 +170,7 @@ int android_remove_file(const char *filename)
return result;
}

extern "C" JNIEXPORT void JNICALL Java_com_github_dalerank_AKHENATEN_AkhenatenMainActivity_gotDirectory(JNIEnv *env, jobject thiz)
extern "C" JNIEXPORT void JNICALL Java_com_github_dalerank_akhenaten_AkhenatenMainActivity_gotDirectory(JNIEnv *env, jobject thiz)
{
has_directory = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/android/asset_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int asset_handler_get_directory_contents(const char *dir_name, int type, const c
return match;
}

extern "C" JNIEXPORT void JNICALL Java_com_github_dalerank_AKHENATEN_AkhenatenMainActivity_releaseAssetManager(JNIEnv *env, jobject thiz)
extern "C" JNIEXPORT void JNICALL Java_com_github_dalerank_akhenaten_AkhenatenMainActivity_releaseAssetManager(JNIEnv *env, jobject thiz)
{
if (asset_manager) {
JNIEnv *env = (JNIEnv *)SDL_AndroidGetJNIEnv();
Expand Down

0 comments on commit 6793443

Please sign in to comment.