From 5d4409392971eb6fe84a18af845863d60e07079b Mon Sep 17 00:00:00 2001 From: Jo Shields Date: Fri, 27 Aug 2021 09:46:20 -0400 Subject: [PATCH] Use partial classes instead, mirroring iOS approach This should give the same output as legacy Xamarin. If it doesn't, I messed up --- .../System.Private.CoreLib.Shared.projitems | 4 +- .../System.Private.CoreLib.csproj | 3 + .../src/System/Environment.Android.cs | 97 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/mono/System.Private.CoreLib/src/System/Environment.Android.cs diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 5a1593f032c69..2f7dd0b08ef70 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2098,7 +2098,7 @@ - + @@ -2325,4 +2325,4 @@ - \ No newline at end of file + diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index 1b57aa747896e..4ad7a0a32b5ed 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -272,6 +272,9 @@ Common\Interop\Unix\System.Native\Interop.GetEnviron.cs + + + diff --git a/src/mono/System.Private.CoreLib/src/System/Environment.Android.cs b/src/mono/System.Private.CoreLib/src/System/Environment.Android.cs new file mode 100644 index 0000000000000..44c8988532f36 --- /dev/null +++ b/src/mono/System.Private.CoreLib/src/System/Environment.Android.cs @@ -0,0 +1,97 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.IO; +using System.Threading; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Diagnostics; + +namespace System +{ + public static partial class Environment + { + private static Dictionary? s_specialFolders; + + private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOption option) + { + if (s_specialFolders == null) + { + Interlocked.CompareExchange(ref s_specialFolders, new Dictionary(), null); + } + + string? path; + lock (s_specialFolders) + { + if (!s_specialFolders.TryGetValue(folder, out path)) + { + path = GetSpecialFolder(folder) ?? string.Empty; + s_specialFolders[folder] = path; + } + } + return path; + } + + private static string? GetSpecialFolder(SpecialFolder folder) + { + string? home = null; + try + { + home = PersistedFiles.GetHomeDirectory(); + } + catch (Exception exc) + { + Debug.Fail($"Unable to get home directory: {exc}"); + } + + // Fall back to '/' when we can't determine the home directory. + // This location isn't writable by non-root users which provides some safeguard + // that the application doesn't write data which is meant to be private. + if (string.IsNullOrEmpty(home)) + { + home = "/"; + } + + switch (folder) + { + case SpecialFolder.Personal: + case SpecialFolder.LocalApplicationData: + return home; + + case SpecialFolder.ApplicationData: + return Path.Combine(home, ".config"); + + case SpecialFolder.Desktop: + case SpecialFolder.DesktopDirectory: + return Path.Combine(home, "Desktop"); + + case SpecialFolder.MyMusic: + return Path.Combine(home, "Music"); + + case SpecialFolder.MyPictures: + return Path.Combine(home, "Pictures"); + + case SpecialFolder.Templates: + return Path.Combine(home, "Templates"); + + case SpecialFolder.MyVideos: + return Path.Combine(home, "Videos"); + + case SpecialFolder.CommonTemplates: + return "/usr/share/templates"; + + case SpecialFolder.Fonts: + return Path.Combine(home, ".fonts"); + + case SpecialFolder.UserProfile: + return GetEnvironmentVariable("HOME"); + + case SpecialFolder.CommonApplicationData: + return "/usr/share"; + + default: + return string.Empty; + } + } + } +}