1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:47:28 +08:00
osu-lazer/osu.Android/OsuGameActivity.cs

91 lines
3.6 KiB
C#
Raw Normal View History

2019-01-31 08:07:32 +08:00
// 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.
2020-12-15 03:11:53 +08:00
using System.IO;
2020-12-09 02:46:55 +08:00
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Net;
2019-01-31 08:07:32 +08:00
using Android.OS;
using Android.Provider;
2019-01-31 08:07:32 +08:00
using Android.Views;
using osu.Framework.Android;
namespace osu.Android
{
[Activity(Theme = "@android:style/Theme.NoTitleBar", MainLauncher = true, ScreenOrientation = ScreenOrientation.FullUser, SupportsPictureInPicture = false, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, HardwareAccelerated = false)]
[IntentFilter(new[] { Intent.ActionDefault, Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataPathPatterns = new[] { ".*\\.osz", ".*\\.osk", ".*\\.osr" }, DataMimeType = "application/*")]
public class OsuGameActivity : AndroidGameActivity
{
private OsuGameAndroid game;
protected override Framework.Game CreateGame() => game = new OsuGameAndroid(this);
2019-01-31 08:07:32 +08:00
protected override void OnCreate(Bundle savedInstanceState)
{
// 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.
2019-10-21 04:29:29 +08:00
// 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.Personal);
2019-01-31 08:07:32 +08:00
base.OnCreate(savedInstanceState);
OnNewIntent(Intent);
2019-01-31 08:07:32 +08:00
Window.AddFlags(WindowManagerFlags.Fullscreen);
Window.AddFlags(WindowManagerFlags.KeepScreenOn);
}
protected override void OnNewIntent(Intent intent)
{
2020-12-14 17:12:23 +08:00
switch (intent.Action)
{
2020-12-14 17:12:23 +08:00
case Intent.ActionDefault:
if (intent.Scheme == ContentResolver.SchemeContent)
handleImportFromUri(intent.Data);
break;
2020-12-05 05:07:45 +08:00
2020-12-14 17:12:23 +08:00
case Intent.ActionSend:
{
var content = intent.ClipData?.GetItemAt(0);
if (content != null)
handleImportFromUri(content.Uri);
break;
}
}
}
private void handleImportFromUri(Uri uri)
{
// there are more performant overloads of this method, but this one is the most backwards-compatible
// (dates back to API 1).
var cursor = ContentResolver?.Query(uri, null, null, null, null);
2020-12-14 17:12:23 +08:00
if (cursor == null)
return;
cursor.MoveToFirst();
2020-12-14 17:12:23 +08:00
var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName);
var stream = ContentResolver.OpenInputStream(uri);
2020-12-15 03:11:53 +08:00
string filename = cursor.GetString(filenameColumn);
2020-12-14 17:12:23 +08:00
if (stream != null)
2020-12-15 03:11:53 +08:00
Task.Factory.StartNew(() => runImport(stream, filename), TaskCreationOptions.LongRunning);
}
private Task runImport(Stream stream, string filename)
{
// SharpCompress requires archive streams to be seekable, which the stream opened by
// OpenInputStream() seems to not necessarily be.
// copy to an arbitrary-access memory stream to be able to proceed with the import.
var copy = new MemoryStream();
stream.CopyTo(copy);
return game.Import(copy, filename);
}
}
}