Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,27 @@ https://docs.unrealengine.com/en-US/SharingAndReleasing/Mobile/UnrealPluginLangu
<!-- For Android 14 (API 34) -->
<addPermission android:name="android.permission.READ_EXTERNAL_STORAGE" android:minSdkVersion="33"/>

<!-- For Wake Up -->
<addPermission android:name="android.permission.WAKE_LOCK"/>
<addPermission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

<!-- For Auto Start -->
<addPermission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<addPermission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<!-- add Elements -->
<addElements tag="application">
<!-- Your lines that you want to add to the "application" section -->

<!--
EXAMPLE!
<addAttribute tag="application" name="android:requestLegacyExternalStorage" value="true"/>
-->
<service android:name="com.Plugins.MobileNativeCode.WakeUpService" android:exported="false"/>
<service android:name="com.Plugins.MobileNativeCode.BootService" android:exported="false"/>
<receiver
android:name="com.Plugins.MobileNativeCode.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

</addElements>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.Plugins.MobileNativeCode;

import androidx.annotation.Keep;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;
import android.util.Log;

@Keep
public class AppLifecycleObserver implements LifecycleObserver {
// Class tag for logging
@Keep
private static final String TAG = "AppLifecycleObserver";

@Keep
private static boolean isAppForeground = false;

@Keep
private static AppLifecycleObserver instance;

@Keep
AppLifecycleObserver() {
Log.d(TAG, "Init AppLifecycleObserver");
}

// First launch application
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreateEvent() {
Log.d(TAG, "Call onCreateEvent");
}

// The application is opened or returned from the background
@Keep
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStartEvent() {
Log.d(TAG, "Call onStartEvent");
isAppForeground = true;
}

// The application is no longer visible on the screen (but still running in the background)
@Keep
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPauseEvent() {
Log.d(TAG, "Call onPauseEvent");
}

// The application
@Keep
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResumeEvent() {
Log.d(TAG, "Call onResumeEvent");
}

// The application is no longer visible on the screen (but still running in the background)
@Keep
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStopEvent() {
Log.d(TAG, "Call onStopEvent");
isAppForeground = false;
}

// The application is destroyed
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyEvent() {
Log.d(TAG, "Call onDestroyEvent");
}

@Keep
public static boolean getIsAppForeground() {
return isAppForeground;
}

@Keep
public static void register() {
if (instance == null) {
instance = new AppLifecycleObserver();
ProcessLifecycleOwner.get().getLifecycle().addObserver(instance);
Log.d(TAG, "AppLifecycleObserver registered.");
} else {
Log.d(TAG, "AppLifecycleObserver has been registered, don't need register anymore.");
}
}

@Keep
public static void unregister() {
if (instance != null) {
ProcessLifecycleOwner.get().getLifecycle().removeObserver(instance);
instance = null;
Log.d(TAG, "AppLifecycleObserver unregistered.");
} else {
Log.d(TAG, "AppLifecycleObserver has been null, don't need unregister anymore.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.Plugins.MobileNativeCode;

import androidx.annotation.Keep;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

@Keep
public class AutoStartManager {
@Keep
private static final String TAG = "AutoStartManager";

@Keep
private static final String PREFS_NAME = "AutoStartPrefs";

@Keep
private static final String KEY_AUTO_START = "AUTO_START";

@Keep
public static void enableAutoStart(Activity activity) {
setAutoStart(activity.getApplicationContext(), true);
}

@Keep
public static void disableAutoStart(Activity activity) {
setAutoStart(activity.getApplicationContext(), false);
}

@Keep
public static boolean isAutoStartEnable(Activity activity) {
return isAutoStart(activity.getApplicationContext());
}

@Keep
public static boolean isAutoStart(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_AUTO_START, false);
}

@Keep
private static void setAutoStart(Context context, boolean enabled) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(KEY_AUTO_START, enabled).apply();
Log.d(TAG, "AutoStart set to: " + enabled);
}

// public static void lockTask(Activity activity) { ... }
// public static void hideSystemUI(Activity activity) { ... }
// public static void scheduleReboot(...) { ... }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.Plugins.MobileNativeCode;

import androidx.annotation.Keep;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

@Keep
public class BootReceiver extends BroadcastReceiver {
@Keep
private static final String TAG = "BootReceiver";

@Keep
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Call onReceive");

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
boolean isAutoStartEnable = AutoStartManager.isAutoStart(context);

Log.d(TAG, "Boot completed. Auto start: " + isAutoStartEnable);

if (isAutoStartEnable) {
Intent serviceIntent = new Intent(context, BootService.class);
context.startForegroundService(serviceIntent);
Log.d(TAG, "Started BootService");
}
}
}
}
Loading