Skip to content

Commit

Permalink
ref-in ADD_MENGINE_DOWNLOAD_SKADNETWORKITEMS_PLIST
Browse files Browse the repository at this point in the history
improve android already create Activity policy
add NOTIFICATOR_APPLICATION_DID_RECEIVE_TRIM_MEMORY
  • Loading branch information
irov committed Nov 24, 2024
1 parent af9a853 commit c82c5ae
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 53 deletions.
22 changes: 18 additions & 4 deletions cmake/macro_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ MACRO(MENGINE_SUBFOLDER)
SET(APPLICATION_APPLE_GLOBAL_COCOAPODS ${APPLICATION_APPLE_GLOBAL_COCOAPODS} PARENT_SCOPE)
SET(APPLICATION_APPLE_SCRIPT_PHASES ${APPLICATION_APPLE_SCRIPT_PHASES} PARENT_SCOPE)
SET(APPLICATION_APPLE_APPLICATION_DELEGATE ${APPLICATION_APPLE_APPLICATION_DELEGATE} PARENT_SCOPE)
SET(APPLICATION_APPLE_PLISTS ${APPLICATION_APPLE_PLISTS} PARENT_SCOPE)
SET(APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS ${APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS} PARENT_SCOPE)
SET(APPLICATION_MACOS_DYLIB ${APPLICATION_MACOS_DYLIB} PARENT_SCOPE)
SET(APPLICATION_MACOS_DYLIB_WITH_SIGN ${APPLICATION_MACOS_DYLIB_WITH_SIGN} PARENT_SCOPE)

Expand Down Expand Up @@ -1043,9 +1043,23 @@ MACRO(ADD_MENGINE_SCRIPT_PHASE NAME SCRIPT INPUT_FILES)
SET(APPLICATION_APPLE_SCRIPT_PHASES ${APPLICATION_APPLE_SCRIPT_PHASES} PARENT_SCOPE)
ENDMACRO()

MACRO(ADD_MENGINE_PLIST PATH)
LIST(APPEND APPLICATION_APPLE_PLISTS ${PATH})
SET(APPLICATION_APPLE_PLISTS ${APPLICATION_APPLE_PLISTS} PARENT_SCOPE)
MACRO(ADD_MENGINE_DOWNLOAD_SKADNETWORKITEMS_PLIST URL)
get_filename_component(FILENAME ${URL} SKADNETWORKITEMS_PLIST_URL_FILENAME)

set(SKADNETWORKITEMS_PLIST_DOWNLOAD_FILENAME "${MENGINE_SOLUTIONS_CONFIG_DIR}/downloads/Configuration/${PROJECT_NAME}/${SKADNETWORKITEMS_PLIST_URL_FILENAME}")

file(DOWNLOAD ${URL} ${SKADNETWORKITEMS_PLIST_DOWNLOAD_FILENAME} STATUS STATUS_DOWNLOAD_SKADNETWORKIDS)

list(GET STATUS_DOWNLOAD_SKADNETWORKIDS 0 STATUS_DOWNLOAD_SKADNETWORKIDS_CODE)

if(NOT STATUS_DOWNLOAD_SKADNETWORKIDS_CODE EQUAL 0)
message(FATAL_ERROR "Failed to download file: ${URL}")
else()
message(STATUS "File downloaded successfully: ${URL}")
endif()

LIST(APPEND APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS ${SKADNETWORKITEMS_PLIST_DOWNLOAD_FILENAME})
SET(APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS ${APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS} PARENT_SCOPE)
ENDMACRO()

macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
Expand Down
2 changes: 1 addition & 1 deletion cmake/mengine_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SET(APPLICATION_APPLE_GLOBAL_COCOAPODS)
SET(APPLICATION_APPLE_SCRIPT_PHASES)
SET(APPLICATION_APPLE_APPLICATION_DELEGATE)
SET(APPLICATION_APPLE_RESOURCES)
SET(APPLICATION_APPLE_PLISTS)
SET(APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS)
SET(APPLICATION_MACOS_DYLIB)
SET(APPLICATION_MACOS_DYLIB_WITH_SIGN)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ public class MengineActivity extends AppCompatActivity {
public static native void AndroidPlatform_windowFocusChangedEvent(boolean focus);
public static native void AndroidPlatform_quitEvent();
public static native void AndroidPlatform_lowMemory();
public static native void AndroidPlatform_trimMemory(int level);
public static native void AndroidPlatform_changeLocale(String locale);

private boolean m_initializePython;
private boolean m_destroy;
private static boolean ACTIVITY_CREATED = false;
private static boolean ACTIVITY_DESTROY = false;

private boolean m_initializePython = false;

private Object m_nativeApplication;
private Locale m_currentLocale;
Expand Down Expand Up @@ -174,7 +177,7 @@ protected List<MenginePluginEngineListener> getEngineListeners() {

@SuppressWarnings("unchecked")
public <T> T getPlugin(Class<T> cls) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return null;
}

Expand All @@ -186,7 +189,7 @@ public <T> T getPlugin(Class<T> cls) {
}

public String getSessionId() {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return "";
}

Expand All @@ -198,7 +201,7 @@ public String getSessionId() {
}

public String getVersionName() {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return "";
}

Expand All @@ -210,7 +213,7 @@ public String getVersionName() {
}

public void setState(String name, Object value) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return;
}

Expand All @@ -220,7 +223,7 @@ public void setState(String name, Object value) {
}

public boolean hasMetaData(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return false;
}

Expand All @@ -232,7 +235,7 @@ public boolean hasMetaData(String name) {
}

public String getMetaDataString(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return null;
}

Expand All @@ -244,7 +247,7 @@ public String getMetaDataString(String name) {
}

public boolean getMetaDataBoolean(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return false;
}

Expand All @@ -256,7 +259,7 @@ public boolean getMetaDataBoolean(String name) {
}

public int getMetaDataInteger(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return 0;
}

Expand Down Expand Up @@ -328,6 +331,20 @@ public void checkPermission(String permission, Runnable onSuccess, Runnable onFa
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

MengineLog.logMessageRelease(TAG, "onCreate: %s", savedInstanceState);

if (MengineActivity.ACTIVITY_CREATED == true) {
MengineAnalytics.buildEvent("mng_activity_create_failed")
.addParameterString("reason", "application already created")
.logAndFlush();

this.finishWithAlertDialog("[ERROR] Application already created");

return;
}

MengineActivity.ACTIVITY_CREATED = true;

try {
Thread.currentThread().setName("MengineActivity");
} catch (final Exception e) {
Expand All @@ -339,18 +356,19 @@ protected void onCreate(Bundle savedInstanceState) {
MengineApplication application = (MengineApplication)this.getApplication();

if (application.isInvalidInitialize() == true) {
MengineAnalytics.buildEvent("mng_activity_create_failed")
.addParameterString("reason", "application invalid initialize")
.logAndFlush();

this.finishWithAlertDialog("[ERROR] Application invalid initialize");

return;
}

MengineLog.logMessageRelease(TAG, "onCreate: %s", savedInstanceState);

this.setState("activity.lifecycle", "create");
this.setState("activity.init", "begin");

m_initializePython = false;
m_destroy = false;

Looper mainLooper = Looper.getMainLooper();
m_commandHandler = new MengineCommandHandler(mainLooper, this);
Expand Down Expand Up @@ -458,7 +476,19 @@ protected void onCreate(Bundle savedInstanceState) {

String[] optionsArgs = options.split(" ");

m_nativeApplication = AndroidMain_bootstrap(nativeLibraryDir, optionsArgs);
Object nativeApplication = AndroidMain_bootstrap(nativeLibraryDir, optionsArgs);

if (nativeApplication == null) {
MengineAnalytics.buildEvent("mng_activity_create_failed")
.addParameterString("reason", "bootstrap failed")
.logAndFlush();

this.finishWithAlertDialog("[ERROR] bootstrap failed");

return;
}

m_nativeApplication = nativeApplication;

this.setState("activity.init", "plugin_create");

Expand All @@ -484,11 +514,11 @@ protected void onCreate(Bundle savedInstanceState) {
} catch (final MenginePluginInvalidInitializeException e) {
this.setState("activity.init", "plugin_create_exception." + l.getPluginName());

MengineAnalytics.buildEvent("mng_activity_init_failed")
MengineAnalytics.buildEvent("mng_activity_create_failed")
.addParameterException("reason", e)
.logAndFlush();

this.finishWithAlertDialog("[ERROR] onCreate plugin: %s exception: %s"
this.finishWithAlertDialog("[ERROR] create plugin: %s exception: %s"
, l.getPluginName()
, e.getMessage()
);
Expand Down Expand Up @@ -844,15 +874,15 @@ public void onNewIntent(Intent intent) {

@Override
public void onDestroy() {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
MengineLog.logWarning(TAG, "onDestroy: already destroyed");

super.onDestroy();

return;
}

m_destroy = true;
MengineActivity.ACTIVITY_DESTROY = true;

this.setState("activity.lifecycle", "destroy");

Expand All @@ -871,7 +901,7 @@ public void onDestroy() {
}

try {
m_threadMain.join();
m_threadMain.join(1000);
} catch (final InterruptedException e) {
MengineLog.logError(TAG, "thread main join exception: %s"
, e.getMessage()
Expand Down Expand Up @@ -941,6 +971,19 @@ public void onLowMemory() {
MengineActivity.AndroidPlatform_lowMemory();
}

@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);

this.setState("activity.trim_memory", level);

MengineLog.logMessage(TAG, "onTrimMemory level: %d"
, level
);

MengineActivity.AndroidPlatform_trimMemory(level);
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Expand Down Expand Up @@ -1110,7 +1153,7 @@ public void pythonCall(String plugin, String method, Object ... args) {
}

public void addPythonPlugin(String name, Object plugin) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return;
}

Expand All @@ -1127,7 +1170,7 @@ public void addPythonPlugin(String name, Object plugin) {
**********************************************************************************************/

public void activateSemaphore(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return;
}

Expand Down Expand Up @@ -1167,7 +1210,7 @@ public void activateSemaphore(String name) {
}

public void deactivateSemaphore(String name) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return;
}

Expand All @@ -1181,7 +1224,7 @@ public void deactivateSemaphore(String name) {
}

public void waitSemaphore(String name, MengineSemaphoreListener cb) {
if (m_destroy == true) {
if (MengineActivity.ACTIVITY_DESTROY == true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;

public class MengineGoogleLicensingPlugin extends MenginePlugin implements MenginePluginApplicationListener {
public class MengineGoogleLicensingPlugin extends MenginePlugin implements MenginePluginActivityListener {
public static final S tring PLUGIN_NAME = "MengineGLicensing";

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/Applications/AndroidApplication/AndroidMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@

extern "C"
{
//////////////////////////////////////////////////////////////////////////
static bool already_bootstrapped = false;
//////////////////////////////////////////////////////////////////////////
JNIEXPORT jobject JNICALL MENGINE_ACTIVITY_JAVA_INTERFACE( AndroidMain_1bootstrap )(JNIEnv * _env, jclass _cls, jstring _nativeLibraryDir, jobjectArray _args)
{
if( already_bootstrapped == true )
{
__android_log_print( ANDROID_LOG_ERROR, "Mengine", "Android bootstrap already bootstrapped" );

return nullptr;
}

already_bootstrapped = true;

if( Mengine::Mengine_JNI_Initialize( _env ) == JNI_FALSE )
{
__android_log_print( ANDROID_LOG_ERROR, "Mengine", "Android bootstrap JNI initialize failed" );
Expand Down Expand Up @@ -57,6 +68,15 @@ extern "C"

jobject j_application = _env->NewDirectByteBuffer( application, sizeof(Mengine::AndroidApplication) );

if ( j_application == nullptr)
{
__android_log_print( ANDROID_LOG_ERROR, "Mengine", "Android bootstrap failed to create direct buffer" );

delete application;

return nullptr;
}

return j_application;
}
//////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions src/Applications/SDLApplication/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ if(MENGINE_TARGET_APPLE)

set(MENGINE_MERGE_PLIST "${MENGINE_SOLUTIONS_CONFIG_DIR}/res/Configuration/info.plist.in")

if(APPLICATION_APPLE_PLISTS)
MESSAGE("APPLICATION_APPLE_PLISTS: ${APPLICATION_APPLE_PLISTS}")
if(APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS)
MESSAGE("APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS: ${APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS}")

set(PYTHON_MERGE_PLISTS_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/merge_plists.py")
set(PYTHON_MERGE_PLISTS_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/merge_skadnetworkitems_plists.py")

execute_process(
COMMAND ${Python3_EXECUTABLE} ${PYTHON_MERGE_PLISTS_SCRIPT} ${MENGINE_DEPLOY_PATH}/${MENGINE_APPLICATION_DEPLOY_PLIST} ${APPLICATION_APPLE_PLISTS} ${MENGINE_MERGE_PLIST}
COMMAND ${Python3_EXECUTABLE} ${PYTHON_MERGE_PLISTS_SCRIPT} ${MENGINE_DEPLOY_PATH}/${MENGINE_APPLICATION_DEPLOY_PLIST} ${APPLICATION_APPLE_SKADNETWORKITEMS_PLISTS} ${MENGINE_MERGE_PLIST}
RESULT_VARIABLE MENGINE_PROCESS_MERGE_PLISTS_RESULT
OUTPUT_VARIABLE MENGINE_PROCESS_MERGE_PLISTS_OUTPUT
ERROR_VARIABLE MENGINE_PROCESS_MERGE_PLISTS_ERROR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def merge_plists(base, patches, output_path):

with open(base, 'rb') as f:
plist_base = plistlib.load(f)

if 'SKAdNetworkItems' not in plist_base:
print(f"Base plist file not exist 'SKAdNetworkItems': {base}")
sys.exit(1)

for patch in patches:
if not os.path.exists(patch):
Expand All @@ -18,13 +22,14 @@ def merge_plists(base, patches, output_path):
with open(patch, 'rb') as f:
plist_patch = plistlib.load(f)

plist_base = {**plist_base, **plist_patch}

if 'SKAdNetworkItems' in plist_base and 'SKAdNetworkItems' in plist_patch:
plist_base['SKAdNetworkItems'] = plist_base['SKAdNetworkItems'] + [
item for item in plist_patch['SKAdNetworkItems']
if item not in plist_base['SKAdNetworkItems']
]
if isinstance(plist_patch, dict):
if 'SKAdNetworkItems' not in plist_patch:
print(f"Patch plist file not exist 'SKAdNetworkItems': {patch}")
sys.exit(1)

plist_base['SKAdNetworkItems'] = plist_base['SKAdNetworkItems'] + [item for item in plist_patch['SKAdNetworkItems'] if item not in plist_base['SKAdNetworkItems']]
elif isinstance(plist_patch, list):
plist_base['SKAdNetworkItems'] = plist_base['SKAdNetworkItems'] + [item for item in plist_patch if item not in plist_base['SKAdNetworkItems']]

os.makedirs(os.path.dirname(output_path), exist_ok=True)

Expand Down
Loading

0 comments on commit c82c5ae

Please sign in to comment.