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.
|
2019-01-06 12:15:09 +08:00
|
|
|
|
|
2020-12-15 03:11:53 +08:00
|
|
|
|
using System.IO;
|
2020-12-24 07:17:26 +08:00
|
|
|
|
using System.Linq;
|
2020-12-09 02:46:55 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2019-01-06 12:15:09 +08:00
|
|
|
|
using Android.App;
|
2020-12-02 03:12:09 +08:00
|
|
|
|
using Android.Content;
|
2019-01-06 12:15:09 +08:00
|
|
|
|
using Android.Content.PM;
|
2020-12-06 01:30:40 +08:00
|
|
|
|
using Android.Net;
|
2019-01-31 08:07:32 +08:00
|
|
|
|
using Android.OS;
|
2020-12-05 18:50:08 +08:00
|
|
|
|
using Android.Provider;
|
2019-01-31 08:07:32 +08:00
|
|
|
|
using Android.Views;
|
2019-01-06 12:15:09 +08:00
|
|
|
|
using osu.Framework.Android;
|
|
|
|
|
|
|
|
|
|
namespace osu.Android
|
|
|
|
|
{
|
2020-12-23 20:38:33 +08:00
|
|
|
|
[Activity(Theme = "@android:style/Theme.NoTitleBar", MainLauncher = true, ScreenOrientation = ScreenOrientation.FullUser, SupportsPictureInPicture = false, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, HardwareAccelerated = false, LaunchMode = LaunchMode.SingleInstance)]
|
2020-12-28 22:13:33 +08:00
|
|
|
|
[IntentFilter(new[] { Intent.ActionDefault, Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "content", DataPathPattern = ".*\\\\.osz", DataHost = "*", DataMimeType = "*/*")]
|
|
|
|
|
[IntentFilter(new[] { Intent.ActionDefault, Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "content", DataPathPattern = ".*\\\\.osk", DataHost = "*", DataMimeType = "*/*")]
|
|
|
|
|
[IntentFilter(new[] { Intent.ActionDefault, Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "file", DataPathPattern = ".*\\\\.osz", DataHost = "*", DataMimeType = "*/*")]
|
|
|
|
|
[IntentFilter(new[] { Intent.ActionDefault, Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataScheme = "file", DataPathPattern = ".*\\\\.osk", DataHost = "*", DataMimeType = "*/*")]
|
2020-12-24 07:17:26 +08:00
|
|
|
|
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }, DataSchemes = new[] { "osu", "osump" })]
|
2019-01-06 12:15:09 +08:00
|
|
|
|
public class OsuGameActivity : AndroidGameActivity
|
|
|
|
|
{
|
2020-12-24 07:17:26 +08:00
|
|
|
|
private static readonly string[] osu_url_schemes = { "osu", "osump" };
|
2020-12-23 20:31:27 +08:00
|
|
|
|
|
2020-12-03 01:03:49 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2019-10-15 03:46:01 +08:00
|
|
|
|
// 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.
|
2019-10-15 03:46:01 +08:00
|
|
|
|
System.Environment.CurrentDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
|
|
|
|
|
|
2019-01-31 08:07:32 +08:00
|
|
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
|
|
2020-12-15 03:23:49 +08: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);
|
2020-12-03 01:03:49 +08:00
|
|
|
|
|
2019-01-31 08:07:32 +08:00
|
|
|
|
Window.AddFlags(WindowManagerFlags.Fullscreen);
|
|
|
|
|
Window.AddFlags(WindowManagerFlags.KeepScreenOn);
|
|
|
|
|
}
|
2020-12-03 01:03:49 +08:00
|
|
|
|
|
2020-12-15 03:23:49 +08:00
|
|
|
|
protected override void OnNewIntent(Intent intent) => handleIntent(intent);
|
|
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent)
|
2020-12-03 01:03:49 +08:00
|
|
|
|
{
|
2020-12-14 17:12:23 +08:00
|
|
|
|
switch (intent.Action)
|
2020-12-03 01:03:49 +08:00
|
|
|
|
{
|
2020-12-14 17:12:23 +08:00
|
|
|
|
case Intent.ActionDefault:
|
|
|
|
|
if (intent.Scheme == ContentResolver.SchemeContent)
|
|
|
|
|
handleImportFromUri(intent.Data);
|
2020-12-24 07:17:26 +08:00
|
|
|
|
else if (osu_url_schemes.Contains(intent.Scheme))
|
|
|
|
|
game.HandleLink(intent.DataString);
|
2020-12-14 17:12:23 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2020-12-06 01:30:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 04:25:53 +08:00
|
|
|
|
private void handleImportFromUri(Uri uri) => Task.Factory.StartNew(async () =>
|
2020-12-06 01:30:40 +08:00
|
|
|
|
{
|
2020-12-15 03:01:28 +08:00
|
|
|
|
// 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;
|
|
|
|
|
|
2020-12-06 01:30:40 +08:00
|
|
|
|
cursor.MoveToFirst();
|
|
|
|
|
|
2020-12-14 17:12:23 +08:00
|
|
|
|
var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName);
|
2020-12-15 03:11:53 +08:00
|
|
|
|
string filename = cursor.GetString(filenameColumn);
|
2020-12-14 17:12:23 +08:00
|
|
|
|
|
2020-12-15 03:11:53 +08:00
|
|
|
|
// 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();
|
2020-12-16 04:25:53 +08:00
|
|
|
|
using (var stream = ContentResolver.OpenInputStream(uri))
|
|
|
|
|
await stream.CopyToAsync(copy);
|
2020-12-15 03:11:53 +08:00
|
|
|
|
|
2020-12-16 04:25:53 +08:00
|
|
|
|
await game.Import(copy, filename);
|
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
2019-01-06 12:15:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|