osu/osu.Android/OsuGameActivity.cs

122 lines
5.5 KiB
C#
Raw Normal View History

2019-01-31 00:07:32 +00: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.
using System.Collections.Generic;
2020-12-14 19:11:53 +00:00
using System.IO;
using System.Linq;
2020-12-08 18:46:55 +00:00
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Net;
2019-01-31 00:07:32 +00:00
using Android.OS;
using Android.Provider;
2019-01-31 00:07:32 +00:00
using Android.Views;
using osu.Framework.Android;
using osu.Game.Database;
namespace osu.Android
{
2020-12-23 12:38:33 +00:00
[Activity(Theme = "@android:style/Theme.NoTitleBar", MainLauncher = true, ScreenOrientation = ScreenOrientation.FullUser, SupportsPictureInPicture = false, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, HardwareAccelerated = false, LaunchMode = LaunchMode.SingleInstance)]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "content", DataPathPattern = ".*\\\\.osz", DataHost = "*", DataMimeType = "*/*")]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "content", DataPathPattern = ".*\\\\.osk", DataHost = "*", DataMimeType = "*/*")]
2021-01-02 18:14:10 +00:00
[IntentFilter(new[] { Intent.ActionSend, Intent.ActionSendMultiple }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "application/zip", "application/octet-stream" })]
2020-12-23 23:17:26 +00:00
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }, DataSchemes = new[] { "osu", "osump" })]
public class OsuGameActivity : AndroidGameActivity
{
2020-12-23 23:17:26 +00:00
private static readonly string[] osu_url_schemes = { "osu", "osump" };
2020-12-23 12:31:27 +00:00
private OsuGameAndroid game;
protected override Framework.Game CreateGame() => game = new OsuGameAndroid(this);
2019-01-31 00:07:32 +00: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-20 20:29:29 +00: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 00:07:32 +00:00
base.OnCreate(savedInstanceState);
2020-12-14 19:23:49 +00:00
// OnNewIntent() only fires for an activity if it's *re-launched* while it's on top of the activity stack.
// on first launch we still have to fire manually.
// reference: https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)
handleIntent(Intent);
2019-01-31 00:07:32 +00:00
Window.AddFlags(WindowManagerFlags.Fullscreen);
Window.AddFlags(WindowManagerFlags.KeepScreenOn);
}
2020-12-14 19:23:49 +00:00
protected override void OnNewIntent(Intent intent) => handleIntent(intent);
private void handleIntent(Intent intent)
{
2020-12-14 09:12:23 +00:00
switch (intent.Action)
{
2020-12-14 09:12:23 +00:00
case Intent.ActionDefault:
if (intent.Scheme == ContentResolver.SchemeContent)
2020-12-27 13:11:30 +00:00
handleImportFromUris(intent.Data);
2020-12-23 23:17:26 +00:00
else if (osu_url_schemes.Contains(intent.Scheme))
game.HandleLink(intent.DataString);
2020-12-14 09:12:23 +00:00
break;
2020-12-04 21:07:45 +00:00
2020-12-14 09:12:23 +00:00
case Intent.ActionSend:
{
var content = intent.ClipData?.GetItemAt(0);
if (content != null)
handleImportFromUris(content.Uri);
break;
}
case Intent.ActionSendMultiple:
{
var uris = new List<Uri>();
for (int i = 0; i < intent.ClipData?.ItemCount; i++)
{
var content = intent.ClipData?.GetItemAt(i);
if (content != null)
uris.Add(content.Uri);
}
handleImportFromUris(uris.ToArray());
2020-12-14 09:12:23 +00:00
break;
}
}
}
private void handleImportFromUris(params Uri[] uris) => Task.Factory.StartNew(async () =>
{
var tasks = new List<ImportTask>();
2020-12-14 09:12:23 +00:00
await Task.WhenAll(uris.Select(async 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 09:12:23 +00:00
if (cursor == null)
return;
cursor.MoveToFirst();
2020-12-14 09:12:23 +00:00
var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName);
string filename = cursor.GetString(filenameColumn);
// 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();
using (var stream = ContentResolver.OpenInputStream(uri))
await stream.CopyToAsync(copy);
lock (tasks)
{
tasks.Add(new ImportTask(copy, filename));
}
}));
2020-12-14 19:11:53 +00:00
await game.Import(tasks.ToArray());
}, TaskCreationOptions.LongRunning);
}
}