public override bool Execute() { Log.LogMessage($"Retargeting UWP assets to {TargetPlatform}."); Func <ResourceCandidate, string> resourceToTargetPath; Func <string, string> pathEncoder; switch (TargetPlatform) { case "ios": resourceToTargetPath = resource => iOSResourceConverter.Convert(resource, DefaultLanguage); pathEncoder = p => p; break; case "android": resourceToTargetPath = resource => AndroidResourceConverter.Convert(resource, DefaultLanguage); pathEncoder = s => AndroidResourceNameEncoder.EncodeFileSystemPath(s, AndroidAssetsPrefix ?? "Assets"); break; default: Log.LogMessage($"Skipping unknown platform {TargetPlatform}"); return(true); } Assets = ContentItems.ToArray(); RetargetedAssets = Assets .Select(asset => ProcessContentItem(asset, resourceToTargetPath, pathEncoder)) .Where(a => a != null) .ToArray(); return(true); }
public string Get(string id) { // double reflection in GetIdentifier, should be replaced by something better var intId = _applicationContext.Resources.GetIdentifier(AndroidResourceNameEncoder.Encode(id), "string", _applicationContext.PackageName); if (intId != 0) { return(_applicationContext.Resources.GetString(intId)); } return("[" + id + "]"); }
public static string?Convert(ResourceCandidate resourceCandidate, string defaultLanguage) { try { ValidatePlatform(resourceCandidate); var language = GetLanguage(resourceCandidate.GetQualifierValue("language"), defaultLanguage); var dpi = GetDpi(resourceCandidate.GetQualifierValue("scale")); var fileName = AndroidResourceNameEncoder.Encode(Path.GetFileNameWithoutExtension(resourceCandidate.LogicalPath)) + Path.GetExtension(resourceCandidate.LogicalPath); return(Path.Combine($"drawable{language}{dpi}", fileName)); } catch (Exception ex) { ex.Log().Info($"Couldn't convert {resourceCandidate.ValueAsString} to an Android resource path.", ex); return(null); } }
private static async Task <StorageFile> GetFileFromApplicationUri(CancellationToken ct, Uri uri) { if (uri.Scheme != "ms-appx") { throw new InvalidOperationException("Uri is not using the ms-appx scheme"); } var path = AndroidResourceNameEncoder.EncodeResourcePath(Uri.UnescapeDataString(uri.PathAndQuery).TrimStart(new char[] { '/' })); // Read the contents of our asset var outputCachePath = global::System.IO.Path.Combine(Android.App.Application.Context.CacheDir.AbsolutePath, path); if (typeof(StorageFile).Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug)) { typeof(StorageFile).Log().Debug($"GetFileFromApplicationUriAsyncTask path:{path} outputCachePath:{outputCachePath}"); } // Ensure only one generation of the cached file can occur at a time. // The copy of tha android asset should not be necessary, but requires a significant // refactoring of the StorageFile class to make non-local provides opaque. // This will need to be revisited once we add support for other providers. using var gate = await _assetGate.LockForAsset(ct, path); if (!File.Exists(outputCachePath)) { Directory.CreateDirectory(global::System.IO.Path.GetDirectoryName(outputCachePath)); Stream GetAsset() { if (DrawableHelper.FindResourceIdFromPath(path) is { } resourceId) { return(ContextHelper.Current.Resources.OpenRawResource(resourceId)); } else { var assets = global::Android.App.Application.Context.Assets; return(assets.Open(path)); } }
public static string?Convert(ResourceCandidate resourceCandidate, string defaultLanguage) { try { ValidatePlatform(resourceCandidate); var language = GetLanguage(resourceCandidate.GetQualifierValue("language"), defaultLanguage); var dpi = GetDpi(resourceCandidate.GetQualifierValue("scale")); var theme = GetTheme(resourceCandidate.GetQualifierValue("theme")); var fileName = AndroidResourceNameEncoder.EncodeDrawablePath(resourceCandidate.LogicalPath); return(Path.Combine($"drawable{language}{theme}{dpi}", fileName)); } #if HAS_UNO catch (Exception ex) { ex.Log().Info($"Couldn't convert {resourceCandidate.ValueAsString} to an Android resource path.", ex); #else catch (Exception) { #endif return(null); } }
public static void Write(Dictionary <string, string> resources, string path, string comment = null) { Directory.CreateDirectory(Path.GetDirectoryName(path)); var document = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement("resources", resources.Select(resource => new XElement("string", new XAttribute("formatted", "false"), // allows special characters (%, $, etc.) new XAttribute("name", AndroidResourceNameEncoder.Encode(resource.Key)), Sanitize(resource.Value) ) )) ); comment.Maybe(c => document.AddFirst(new XComment(c))); document.Save(path); }
public void When_EncodeDrawablePath(string input, string expected) { Assert.AreEqual(expected, AndroidResourceNameEncoder.EncodeDrawablePath(input)); }