Files
osu-framework/osu.Framework.Android/AndroidGameActivity.cs
2024-08-20 01:26:58 +09:00

71 lines
3.1 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using ManagedBass;
using Org.Libsdl.App;
using osu.Framework.Extensions.ObjectExtensions;
using Debug = System.Diagnostics.Debug;
namespace osu.Framework.Android
{
// since `ActivityAttribute` can't be inherited, the below is only provided as an illustrative example of how to setup an activity for best compatibility.
[Activity(ConfigurationChanges = DEFAULT_CONFIG_CHANGES, Exported = true, LaunchMode = DEFAULT_LAUNCH_MODE, MainLauncher = true)]
public abstract class AndroidGameActivity : SDLActivity
{
protected const ConfigChanges DEFAULT_CONFIG_CHANGES = ConfigChanges.Keyboard
| ConfigChanges.KeyboardHidden
| ConfigChanges.Navigation
| ConfigChanges.Orientation
| ConfigChanges.ScreenLayout
| ConfigChanges.ScreenSize
| ConfigChanges.SmallestScreenSize
| ConfigChanges.Touchscreen
| ConfigChanges.UiMode;
protected const LaunchMode DEFAULT_LAUNCH_MODE = LaunchMode.SingleInstance;
internal static AndroidGameSurface Surface => (AndroidGameSurface)MSurface!;
protected abstract Game CreateGame();
protected override string[] GetLibraries() => new string[] { "SDL3" };
protected override SDLSurface CreateSDLSurface(Context? context) => new AndroidGameSurface(this, context);
protected override void Main()
{
var host = new AndroidGameHost(this);
host.Run(CreateGame());
}
protected override void OnCreate(Bundle? savedInstanceState)
{
Debug.Assert(RuntimeInfo.EntryAssembly.IsNull(), "RuntimeInfo.EntryAssembly should be null on Android and therefore needs to be manually updated.");
RuntimeInfo.EntryAssembly = GetType().Assembly;
// The default current directory on android is '/'.
// On some devices '/' maps to the app data directory. On others it maps to the root of the internal storage.
// In order to have a consistent current directory on all devices the full path of the app data directory is set as the current directory.
System.Environment.CurrentDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
base.OnCreate(savedInstanceState);
}
protected override void OnStop()
{
base.OnStop();
Bass.Pause();
}
protected override void OnRestart()
{
base.OnRestart();
Bass.Start();
}
}
}