A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Generic;
6using System.IO;
7using Android.App;
8using Android.Content;
9using osu.Framework.Android.Graphics.Textures;
10using osu.Framework.Android.Graphics.Video;
11using osu.Framework.Android.Input;
12using osu.Framework.Configuration;
13using osu.Framework.Graphics.Textures;
14using osu.Framework.Graphics.Video;
15using osu.Framework.Input;
16using osu.Framework.Input.Handlers;
17using osu.Framework.Input.Handlers.Midi;
18using osu.Framework.IO.Stores;
19using osu.Framework.Platform;
20using osu.Framework.Threading;
21using Uri = Android.Net.Uri;
22
23namespace osu.Framework.Android
24{
25 public class AndroidGameHost : OsuTKGameHost
26 {
27 private readonly AndroidGameView gameView;
28
29 public AndroidGameHost(AndroidGameView gameView)
30 {
31 this.gameView = gameView;
32 }
33
34 protected override void SetupConfig(IDictionary<FrameworkSetting, object> defaultOverrides)
35 {
36 if (!defaultOverrides.ContainsKey(FrameworkSetting.ExecutionMode))
37 defaultOverrides.Add(FrameworkSetting.ExecutionMode, ExecutionMode.SingleThread);
38
39 base.SetupConfig(defaultOverrides);
40 }
41
42 protected override IWindow CreateWindow() => new AndroidGameWindow(gameView);
43
44 protected override bool LimitedMemoryEnvironment => true;
45
46 public override bool CanExit => false;
47
48 public override bool OnScreenKeyboardOverlapsGameWindow => true;
49
50 public override ITextInputSource GetTextInput() => new AndroidTextInput(gameView);
51
52 protected override IEnumerable<InputHandler> CreateAvailableInputHandlers() =>
53 new InputHandler[]
54 {
55 new AndroidKeyboardHandler(gameView),
56 new AndroidTouchHandler(gameView),
57 new MidiHandler()
58 };
59
60 public override Storage GetStorage(string path) => new AndroidStorage(path, this);
61
62 public override IEnumerable<string> UserStoragePaths => new[]
63 {
64 Application.Context.GetExternalFilesDir(string.Empty).ToString()
65 };
66
67 public override void OpenFileExternally(string filename)
68 => throw new NotImplementedException();
69
70 public override void OpenUrlExternally(string url)
71 {
72 var activity = (Activity)gameView.Context;
73
74 using (var intent = new Intent(Intent.ActionView, Uri.Parse(url)))
75 {
76 if (intent.ResolveActivity(activity.PackageManager) != null)
77 activity.StartActivity(intent);
78 }
79 }
80
81 public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore)
82 => new AndroidTextureLoaderStore(underlyingStore);
83
84 public override VideoDecoder CreateVideoDecoder(Stream stream)
85 => new AndroidVideoDecoder(stream);
86
87 protected override void PerformExit(bool immediately)
88 {
89 // Do not exit on Android, Window.Run() does not block
90 }
91 }
92}