Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions agdk/common/base_game_framework/include/debug_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
#include <cstdint>
#include <string>

// Enables compiler printf-style format string checking (-Wformat) on logging functions.
// format_index and args_index are 1-based parameter positions; pass 0 for args_index
// on functions taking a va_list.
#if defined(__GNUC__) || defined(__clang__)
#define BGF_PRINTF_FORMAT(format_index, args_index) \
__attribute__((format(printf, format_index, args_index)))
#else
#define BGF_PRINTF_FORMAT(format_index, args_index)
#endif

namespace base_game_framework {

/**
Expand Down Expand Up @@ -130,7 +140,7 @@ namespace base_game_framework {
const DebugLogLevel log_level,
const char* tag,
const char* format,
...);
...) BGF_PRINTF_FORMAT(4, 5);

/**
* @brief Log a debug message with variable parameters
Expand All @@ -145,7 +155,7 @@ namespace base_game_framework {
const DebugLogLevel log_level,
const char* tag,
const char* format,
va_list specifiers);
va_list specifiers) BGF_PRINTF_FORMAT(4, 0);

/**
* @brief Log a debug message with a string constant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.view.Display;
import android.view.Surface;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

Expand Down Expand Up @@ -58,8 +59,11 @@ private class FreeSpace {

public static final int CONTEXT_GET_DISPLAY_MIN_API = Build.VERSION_CODES.R;

private static final String TAG = "BaseGameFrameworkUtils";

private Activity mGameActivity;
private FreeSpace mFreeSpace;
// Final so readers and the background retrieval thread always lock the same object.
private final FreeSpace mFreeSpace = new FreeSpace();

public BaseGameFrameworkUtils(Activity activity) {
mGameActivity = activity;
Expand All @@ -75,10 +79,12 @@ public boolean GetAndroidFeatureSupported(int featureId) {
}

public int GetExternalStorageEmulated() {
if (Environment.isExternalStorageEmulated(mGameActivity.getExternalFilesDir(null))) {
return 1;
}
return 0;
// Null if shared storage is currently unavailable; fall back to the primary volume.
File externalDir = mGameActivity.getExternalFilesDir(null);
boolean emulated = (externalDir != null)
? Environment.isExternalStorageEmulated(externalDir)
: Environment.isExternalStorageEmulated();
return emulated ? 1 : 0;
}

public int GetDisplayDPI() {
Expand All @@ -96,7 +102,11 @@ public String GetRootPath(int pathType) {
returnPath = mGameActivity.getFilesDir().getAbsolutePath();
break;
case kRootPathExternalStorage:
returnPath = mGameActivity.getExternalFilesDir(null).getAbsolutePath();
// Null if shared storage is currently unavailable.
File externalDir = mGameActivity.getExternalFilesDir(null);
if (externalDir != null) {
returnPath = externalDir.getAbsolutePath();
}
break;
case kRootPathCacheStorage:
returnPath = mGameActivity.getCacheDir().getAbsolutePath();
Expand Down Expand Up @@ -129,44 +139,54 @@ public long GetEstimatedFreeSpace(int pathType) {

public void Initialize() {
retrieveFreeSpace();
registerUtilObject();
registerUtilObject(mGameActivity);
}

private void retrieveFreeSpace() {
mFreeSpace = new FreeSpace();
if (Build.VERSION.SDK_INT >= FREE_SPACE_MIN_API) {
synchronized (mFreeSpace) {
new Thread(new Runnable() {
public void run() {
try {
StorageManager storageManager = (StorageManager)
mGameActivity.getSystemService(Context.STORAGE_SERVICE);

UUID cacheUUID = storageManager.getUuidForPath(
mGameActivity.getCacheDir());
long quota = storageManager.getCacheQuotaBytes(cacheUUID);
mFreeSpace.cacheSpace = quota;

UUID internalUUID = storageManager.getUuidForPath(
mGameActivity.getFilesDir());
mFreeSpace.internalSpace =
storageManager.getAllocatableBytes(internalUUID);

UUID externalUUID = storageManager.getUuidForPath(
mGameActivity.getExternalFilesDir(null));
mFreeSpace.externalSpace = storageManager.getAllocatableBytes(
externalUUID);
mFreeSpace.retrieved = true;
} catch (IOException e) {
new Thread(new Runnable() {
public void run() {
// Query into locals, then publish all values under the lock so
// GetEstimatedFreeSpace() never observes a partially updated FreeSpace.
long cacheSpace = 0;
long internalSpace = 0;
long externalSpace = 0;
try {
StorageManager storageManager = (StorageManager)
mGameActivity.getSystemService(Context.STORAGE_SERVICE);

UUID cacheUUID = storageManager.getUuidForPath(
mGameActivity.getCacheDir());
cacheSpace = storageManager.getCacheQuotaBytes(cacheUUID);

UUID internalUUID = storageManager.getUuidForPath(
mGameActivity.getFilesDir());
internalSpace = storageManager.getAllocatableBytes(internalUUID);

// Null if shared storage is currently unavailable.
File externalDir = mGameActivity.getExternalFilesDir(null);
if (externalDir != null) {
UUID externalUUID = storageManager.getUuidForPath(externalDir);
externalSpace = storageManager.getAllocatableBytes(externalUUID);
}
} catch (IOException | RuntimeException e) {
// getUuidForPath() can also throw IllegalArgumentException.
Log.w(TAG, "Failed to retrieve free space", e);
} finally {
synchronized (mFreeSpace) {
mFreeSpace.cacheSpace = cacheSpace;
mFreeSpace.internalSpace = internalSpace;
mFreeSpace.externalSpace = externalSpace;
mFreeSpace.retrieved = true;
e.printStackTrace();
}
}
}).start();
}
}
}).start();
} else {
// If no API support, set retrieved, but all sizes are 0
mFreeSpace.retrieved = true;
synchronized (mFreeSpace) {
mFreeSpace.retrieved = true;
}
}
}

Expand All @@ -184,5 +204,7 @@ public int getDeviceRotation()
}
return rotation;
}
public native void registerUtilObject();
// `activity` identifies the owner so native code only releases this registration
// when that Activity's native thread shuts down.
private native void registerUtilObject(Activity activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ void BaseGameFramework_Destroy() {
FilesystemManager::ShutdownInstance();
DebugManager::ShutdownInstance();
android_app *app = PlatformUtilAndroid::GetAndroidApp();
PlatformUtilAndroid::ReleaseUtilObject(PlatformUtilAndroid::GetMainThreadJNIEnv(),
app->activity->javaGameActivity);
app->activity->vm->DetachCurrentThread();

// Don't leave dangling pointers to the detached JNIEnv / destroyed android_app
// between this shutdown and the next BaseGameFramework_Init(). Skip if a newer
// instance has already re-initialized these process-wide values.
if (PlatformUtilAndroid::GetAndroidApp() == app) {
PlatformUtilAndroid::SetMainThreadJniEnv(nullptr);
PlatformUtilAndroid::SetActivityClassObject(nullptr);
PlatformUtilAndroid::SetNativeWindow(nullptr);
PlatformUtilAndroid::SetAndroidApp(nullptr);
}
}

} // namespace basegameframework
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ void FilesystemManager::ShutdownInstance() {

FilesystemManager::FilesystemManager() {
JNIEnv *env = PlatformUtilAndroid::GetMainThreadJNIEnv();
jclass util_class = PlatformUtilAndroid::GetUtilClass();
jobject util_object = PlatformUtilAndroid::GetUtilClassObject();
if (env != nullptr && util_object != nullptr) {
jobject util_object = PlatformUtilAndroid::NewUtilObjectLocalRef(env);
if (util_object != nullptr) {
jclass util_class = env->GetObjectClass(util_object);
jmethodID method_getrootpath = env->GetMethodID(util_class, "GetRootPath",
"(I)Ljava/lang/String;");
jstring ipath_jstring = static_cast<jstring>(
env->CallObjectMethod(util_object, method_getrootpath,
static_cast<jint>(kRootPathInternalStorage)));
const char *ipath = env->GetStringUTFChars(ipath_jstring, NULL);
root_path_internal_ = ipath;
env->DeleteLocalRef(ipath_jstring);

jstring epath_jstring = static_cast<jstring>(
env->CallObjectMethod(util_object, method_getrootpath,
static_cast<jint>(kRootPathExternalStorage)));
const char *epath = env->GetStringUTFChars(epath_jstring, NULL);
root_path_external_ = epath;
env->DeleteLocalRef(epath_jstring);

jstring cpath_jstring = static_cast<jstring>(
env->CallObjectMethod(util_object, method_getrootpath,
static_cast<jint>(kRootPathCache)));
const char *cpath = env->GetStringUTFChars(cpath_jstring, NULL);
root_path_cache_ = cpath;
env->DeleteLocalRef(cpath_jstring);
} else if (util_object == nullptr) {
auto get_root_path = [env, util_object, method_getrootpath](RootPathType path_type) {
jstring path_jstring = static_cast<jstring>(
env->CallObjectMethod(util_object, method_getrootpath,
static_cast<jint>(path_type)));
std::string path;
if (!PlatformUtilAndroid::ClearPendingException(env)) {
path = PlatformUtilAndroid::JStringToString(env, path_jstring);
}
env->DeleteLocalRef(path_jstring);
return path;
};
if (method_getrootpath != nullptr) {
root_path_internal_ = get_root_path(kRootPathInternalStorage);
root_path_external_ = get_root_path(kRootPathExternalStorage);
root_path_cache_ = get_root_path(kRootPathCache);
} else {
PlatformUtilAndroid::ClearPendingException(env);
}
env->DeleteLocalRef(util_class);
env->DeleteLocalRef(util_object);
} else if (env != nullptr) {
DebugManager::Log(DebugManager::kLog_Channel_Default,
DebugManager::kLog_Level_Error,
"FilesystemManager",
Expand All @@ -71,13 +71,21 @@ FilesystemManager::FilesystemManager() {
bool FilesystemManager::GetExternalStorageEmulated() const {
bool emulated = true;
JNIEnv *env = PlatformUtilAndroid::GetMainThreadJNIEnv();
jclass util_class = PlatformUtilAndroid::GetUtilClass();
jobject util_object = PlatformUtilAndroid::GetUtilClassObject();
if (env != nullptr && util_object != nullptr) {
jobject util_object = PlatformUtilAndroid::NewUtilObjectLocalRef(env);
if (util_object != nullptr) {
jclass util_class = env->GetObjectClass(util_object);
jmethodID method_getemulated = env->GetMethodID(util_class, "GetExternalStorageEmulated",
"()I");
jint jemulated = env->CallIntMethod(util_object, method_getemulated);
emulated = jemulated == 1 ? true : false;
if (method_getemulated != nullptr) {
const jint jemulated = env->CallIntMethod(util_object, method_getemulated);
if (!PlatformUtilAndroid::ClearPendingException(env)) {
emulated = (jemulated == 1);
}
} else {
PlatformUtilAndroid::ClearPendingException(env);
}
env->DeleteLocalRef(util_class);
env->DeleteLocalRef(util_object);
}
return emulated;
}
Expand All @@ -95,14 +103,22 @@ int64_t FilesystemManager::GetFreeSpace(const RootPathType path_type) const {
int64_t free_space = -1;

JNIEnv *env = PlatformUtilAndroid::GetMainThreadJNIEnv();
jclass util_class = PlatformUtilAndroid::GetUtilClass();
jobject util_object = PlatformUtilAndroid::GetUtilClassObject();
if (env != nullptr && util_object != nullptr) {
jobject util_object = PlatformUtilAndroid::NewUtilObjectLocalRef(env);
if (util_object != nullptr) {
jclass util_class = env->GetObjectClass(util_object);
jmethodID method_getfreespace = env->GetMethodID(util_class, "GetEstimatedFreeSpace",
"(I)J");
jlong jfree_space = env->CallLongMethod(util_object, method_getfreespace,
static_cast<jint>(path_type));
free_space = static_cast<int64_t>(jfree_space);
if (method_getfreespace != nullptr) {
const jlong jfree_space = env->CallLongMethod(util_object, method_getfreespace,
static_cast<jint>(path_type));
if (!PlatformUtilAndroid::ClearPendingException(env)) {
free_space = static_cast<int64_t>(jfree_space);
}
} else {
PlatformUtilAndroid::ClearPendingException(env);
}
env->DeleteLocalRef(util_class);
env->DeleteLocalRef(util_object);
}
return free_space;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ enum EventKeyCode : int32_t {
kKeyCode_Unknown = AKEYCODE_UNKNOWN,
kKeyCode_Soft_Left = AKEYCODE_SOFT_LEFT,
kKeyCode_Soft_Right = AKEYCODE_SOFT_RIGHT,
kKeyCode_Gome = AKEYCODE_HOME,
kKeyCode_Home = AKEYCODE_HOME,
// Misspelled alias retained for source compatibility.
kKeyCode_Gome [[deprecated("Use kKeyCode_Home instead")]] = kKeyCode_Home,
kKeyCode_Back = AKEYCODE_BACK,
kKeyCode_Call = AKEYCODE_CALL,
kKeyCode_Endcall = AKEYCODE_ENDCALL,
Expand Down
Loading
Loading