Skip to content

Commit

Permalink
feat: Using ms-appdata paths in Image, MediaPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Apr 4, 2020
1 parent cc93e1f commit 42ecf36
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/Uno.Foundation/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public static string GetExtension(this Uri uri)
return String.Empty;
}

public static bool IsAppData(this Uri uri)
{
return uri.Scheme.Equals("ms-appdata", StringComparison.OrdinalIgnoreCase);
}

public static bool IsLocalResource(this Uri uri)
{
return uri.Scheme.Equals("ms-appx", StringComparison.OrdinalIgnoreCase)
|| uri.Scheme.Equals("ms-appdata", StringComparison.OrdinalIgnoreCase);
return uri.Scheme.Equals("ms-appx", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
18 changes: 11 additions & 7 deletions src/Uno.UI/UI/Xaml/Media/ImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Uno;
using Uno.Diagnostics.Eventing;
using Windows.UI.Xaml.Media.Imaging;
using Uno.Helpers;

#if !IS_UNO
using Uno.Web.Query;
Expand Down Expand Up @@ -99,26 +100,29 @@ internal void InitFromUri(Uri uri)
uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart("/"));
}

var isResource = uri.Scheme.Equals(MsAppXScheme, StringComparison.OrdinalIgnoreCase)
|| uri.Scheme.Equals(MsAppDataScheme, StringComparison.OrdinalIgnoreCase);

if (isResource)
if (uri.IsLocalResource())
{
InitFromResource(uri);
return;
}

if (uri.IsAppData())
{
var filePath = AppDataUriEvaluator.ToPath(uri);
InitFromFile(filePath);
}

if (uri.IsFile)
{
InitFromFile(uri);
InitFromFile(uri.PathAndQuery);
}

WebUri = uri;
}

private void InitFromFile(Uri uri)
private void InitFromFile(string filePath)
{
FilePath = uri.PathAndQuery;
FilePath = filePath;
}

partial void InitFromResource(Uri uri);
Expand Down
15 changes: 10 additions & 5 deletions src/Uno.UWP/Media/Playback/MediaPlayer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using Uno.Logging;
using AndroidMediaPlayer = Android.Media.MediaPlayer;
using System.Collections.Generic;
using Uno;
using Uno.Helpers;

namespace Windows.Media.Playback
{
Expand Down Expand Up @@ -46,7 +48,6 @@ public partial class MediaPlayer :
private int _playlistIndex;

const string MsAppXScheme = "ms-appx";
const string MsAppDataScheme = "ms-appdata";

public virtual IVideoSurface RenderSurface { get; private set; } = new VideoSurface(Application.Context);

Expand Down Expand Up @@ -159,17 +160,21 @@ private void SetVideoSource(Uri uri)
uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart("/"));
}

var isResource = uri.Scheme.Equals(MsAppXScheme, StringComparison.OrdinalIgnoreCase)
|| uri.Scheme.Equals(MsAppDataScheme, StringComparison.OrdinalIgnoreCase);

if (isResource)
if (uri.IsLocalResource())
{
var filename = global::System.IO.Path.GetFileName(uri.LocalPath);
var afd = Application.Context.Assets.OpenFd(filename);
_player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
return;
}

if (uri.IsAppData())
{
var filePath = AppDataUriEvaluator.ToPath(uri);
_player.SetDataSource(filePath);
return;
}

if (uri.IsFile)
{
_player.SetDataSource(Application.Context, Android.Net.Uri.Parse(uri.PathAndQuery));
Expand Down
15 changes: 9 additions & 6 deletions src/Uno.UWP/Media/Playback/MediaPlayer.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Uno.Logging;
using Uno.Media.Playback;
using Windows.Media.Core;
using Uno.Helpers;

namespace Windows.Media.Playback
{
Expand Down Expand Up @@ -119,7 +120,6 @@ private void OnMediaEndedCore(object sender, NSNotificationEventArgs args)
public static NSString RateObservationContext = new NSString("AVCustomEditPlayerViewControllerRateObservationContext");

const string MsAppXScheme = "ms-appx";
const string MsAppDataScheme = "ms-appdata";

public IVideoSurface RenderSurface { get; } = new VideoSurface();

Expand Down Expand Up @@ -262,19 +262,22 @@ private static NSUrl DecodeUri(Uri uri)
if (!uri.IsAbsoluteUri || uri.Scheme == "")
{
uri = new Uri(MsAppXScheme + ":///" + uri.OriginalString.TrimStart(new char[] { '/' }));
}

var isResource = uri.Scheme.Equals(MsAppXScheme, StringComparison.OrdinalIgnoreCase)
|| uri.Scheme.Equals(MsAppDataScheme, StringComparison.OrdinalIgnoreCase);
}

if (isResource)
if (uri.IsLocalResource())
{
var file = uri.PathAndQuery.TrimStart(new[] { '/' });
var fileName = Path.GetFileNameWithoutExtension(file);
var fileExtension = Path.GetExtension(file)?.Replace(".", "");
return NSBundle.MainBundle.GetUrlForResource(fileName, fileExtension);
}

if (uri.IsAppData())
{
var filePath = AppDataUriEvaluator.ToPath(uri);
return NSUrl.CreateFileUrl(filePath, relativeToUrl: null);
}

if (uri.IsFile)
{
return NSUrl.CreateFileUrl(uri.PathAndQuery, relativeToUrl: null);
Expand Down

0 comments on commit 42ecf36

Please sign in to comment.