Skip to content

Commit 28d6a04

Browse files
committed
Adds app is foregrouded
1 parent 42680f2 commit 28d6a04

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

OpacityCore/src/main/cpp/OpacityCore.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ extern "C" const char *get_ip_address() {
186186
return result; // Caller must free this memory
187187
}
188188

189+
extern "C" bool android_is_app_foregrounded() {
190+
JNIEnv *env = GetJniEnv();
191+
jclass jOpacityCore = env->GetObjectClass(java_object);
192+
jmethodID method = env->GetMethodID(jOpacityCore, "isAppForegrounded", "()Z");
193+
return env->CallBooleanMethod(java_object, method);
194+
}
195+
189196
extern "C" void android_close_webview() {
190197
JNIEnv *env = GetJniEnv();
191198
// Get the Kotlin class

OpacityCore/src/main/kotlin/com/opacitylabs/opacitycore/OpacityCore.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ object OpacityCore {
5959
return sRuntime
6060
}
6161

62+
fun isAppForegrounded(): Boolean {
63+
return try {
64+
val activityManager = appContext.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
65+
val runningProcesses = activityManager.runningAppProcesses
66+
if (runningProcesses != null) {
67+
for (processInfo in runningProcesses) {
68+
if (processInfo.processName == appContext.packageName) {
69+
return processInfo.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
70+
}
71+
}
72+
}
73+
false
74+
} catch (e: Exception) {
75+
false
76+
}
77+
}
78+
6279
fun securelySet(key: String, value: String) {
6380
cryptoManager.set(key, value)
6481
}

0 commit comments

Comments
 (0)