Android: Track whether app is in foreground
This commit is contained in:
parent
c536754ffe
commit
01b44837f4
|
@ -6,6 +6,7 @@ import android.app.Application;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
|
|
||||||
|
import org.dolphinemu.dolphinemu.utils.ActivityTracker;
|
||||||
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;
|
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;
|
||||||
import org.dolphinemu.dolphinemu.utils.Java_GCAdapter;
|
import org.dolphinemu.dolphinemu.utils.Java_GCAdapter;
|
||||||
import org.dolphinemu.dolphinemu.utils.Java_WiimoteAdapter;
|
import org.dolphinemu.dolphinemu.utils.Java_WiimoteAdapter;
|
||||||
|
@ -20,6 +21,7 @@ public class DolphinApplication extends Application
|
||||||
{
|
{
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
application = this;
|
application = this;
|
||||||
|
registerActivityLifecycleCallbacks(new ActivityTracker());
|
||||||
VolleyUtil.init(getApplicationContext());
|
VolleyUtil.init(getApplicationContext());
|
||||||
System.loadLibrary("main");
|
System.loadLibrary("main");
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.dolphinemu.dolphinemu.utils
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.app.Application.ActivityLifecycleCallbacks
|
||||||
|
import android.os.Bundle
|
||||||
|
|
||||||
|
class ActivityTracker : ActivityLifecycleCallbacks {
|
||||||
|
val resumedActivities = HashSet<Activity>()
|
||||||
|
var backgroundExecutionAllowed = false
|
||||||
|
|
||||||
|
override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}
|
||||||
|
|
||||||
|
override fun onActivityStarted(activity: Activity) {}
|
||||||
|
|
||||||
|
override fun onActivityResumed(activity: Activity) {
|
||||||
|
resumedActivities.add(activity)
|
||||||
|
if (!backgroundExecutionAllowed && !resumedActivities.isEmpty()) {
|
||||||
|
backgroundExecutionAllowed = true
|
||||||
|
setBackgroundExecutionAllowedNative(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityPaused(activity: Activity) {
|
||||||
|
resumedActivities.remove(activity)
|
||||||
|
if (backgroundExecutionAllowed && resumedActivities.isEmpty()) {
|
||||||
|
backgroundExecutionAllowed = false
|
||||||
|
setBackgroundExecutionAllowedNative(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityStopped(activity: Activity) {}
|
||||||
|
|
||||||
|
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {}
|
||||||
|
|
||||||
|
override fun onActivityDestroyed(activity: Activity) {}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
external fun setBackgroundExecutionAllowedNative(allowed: Boolean)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2024 Dolphin Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
|
||||||
|
#include "Common/Logging/Log.h"
|
||||||
|
#include "Core/AchievementManager.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_org_dolphinemu_dolphinemu_utils_ActivityTracker_setBackgroundExecutionAllowedNative(
|
||||||
|
JNIEnv*, jclass, jboolean allowed)
|
||||||
|
{
|
||||||
|
// This is called with allowed == false when the app goes into the background.
|
||||||
|
// We use this to stop continuously running background threads so we don't waste battery.
|
||||||
|
|
||||||
|
INFO_LOG_FMT(CORE, "SetBackgroundExecutionAllowed {}", allowed);
|
||||||
|
AchievementManager::GetInstance().SetBackgroundExecutionAllowed(allowed);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
add_library(main SHARED
|
add_library(main SHARED
|
||||||
|
ActivityTracker.cpp
|
||||||
Cheats/ARCheat.cpp
|
Cheats/ARCheat.cpp
|
||||||
Cheats/Cheats.h
|
Cheats/Cheats.h
|
||||||
Cheats/GeckoCheat.cpp
|
Cheats/GeckoCheat.cpp
|
||||||
|
@ -11,6 +12,7 @@ add_library(main SHARED
|
||||||
GameList/GameFile.cpp
|
GameList/GameFile.cpp
|
||||||
GameList/GameFile.h
|
GameList/GameFile.h
|
||||||
GameList/GameFileCache.cpp
|
GameList/GameFileCache.cpp
|
||||||
|
GpuDriver.cpp
|
||||||
Host.cpp
|
Host.cpp
|
||||||
Host.h
|
Host.h
|
||||||
InfinityConfig.cpp
|
InfinityConfig.cpp
|
||||||
|
@ -32,7 +34,6 @@ add_library(main SHARED
|
||||||
RiivolutionPatches.cpp
|
RiivolutionPatches.cpp
|
||||||
SkylanderConfig.cpp
|
SkylanderConfig.cpp
|
||||||
WiiUtils.cpp
|
WiiUtils.cpp
|
||||||
GpuDriver.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(main
|
target_link_libraries(main
|
||||||
|
|
|
@ -158,6 +158,10 @@ bool AchievementManager::IsGameLoaded() const
|
||||||
return game_info && game_info->id != 0;
|
return game_info && game_info->id != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AchievementManager::SetBackgroundExecutionAllowed(bool allowed)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void AchievementManager::FetchPlayerBadge()
|
void AchievementManager::FetchPlayerBadge()
|
||||||
{
|
{
|
||||||
FetchBadge(&m_player_badge, RC_IMAGE_TYPE_USER,
|
FetchBadge(&m_player_badge, RC_IMAGE_TYPE_USER,
|
||||||
|
|
|
@ -96,6 +96,7 @@ public:
|
||||||
bool HasAPIToken() const;
|
bool HasAPIToken() const;
|
||||||
void LoadGame(const std::string& file_path, const DiscIO::Volume* volume);
|
void LoadGame(const std::string& file_path, const DiscIO::Volume* volume);
|
||||||
bool IsGameLoaded() const;
|
bool IsGameLoaded() const;
|
||||||
|
void SetBackgroundExecutionAllowed(bool allowed);
|
||||||
|
|
||||||
void FetchPlayerBadge();
|
void FetchPlayerBadge();
|
||||||
void FetchGameBadges();
|
void FetchGameBadges();
|
||||||
|
@ -239,6 +240,8 @@ public:
|
||||||
|
|
||||||
constexpr void LoadGame(const std::string&, const DiscIO::Volume*) {}
|
constexpr void LoadGame(const std::string&, const DiscIO::Volume*) {}
|
||||||
|
|
||||||
|
constexpr void SetBackgroundExecutionAllowed(bool allowed) {}
|
||||||
|
|
||||||
constexpr void DoFrame() {}
|
constexpr void DoFrame() {}
|
||||||
|
|
||||||
constexpr void CloseGame() {}
|
constexpr void CloseGame() {}
|
||||||
|
|
Loading…
Reference in New Issue