Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[essentials] use Lazy<T> for Android application-wide values #7996

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 27 additions & 63 deletions src/Essentials/src/AppInfo/AppInfo.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,21 @@ namespace Microsoft.Maui.ApplicationModel
{
class AppInfoImplementation : IAppInfo
{
public string PackageName => Application.Context.PackageName;
static readonly Lazy<string> _name = new Lazy<string>(() => Application.Context.ApplicationInfo.LoadLabel(Application.Context.PackageManager));
static readonly Lazy<string> _packageName = new Lazy<string>(() => Application.Context.PackageName);
static readonly Lazy<PackageInfo> _packageInfo = new Lazy<PackageInfo>(() => Application.Context.PackageManager.GetPackageInfo(_packageName.Value, PackageInfoFlags.MetaData));
static readonly Lazy<AppTheme> _requestedTheme = new Lazy<AppTheme>(GetRequestedTheme);
static readonly Lazy<LayoutDirection> _layoutDirection = new Lazy<LayoutDirection>(GetLayoutDirection);

public string Name
{
get
{
var applicationInfo = Application.Context.ApplicationInfo;
var packageManager = Application.Context.PackageManager;
return applicationInfo.LoadLabel(packageManager);
}
}
public string PackageName => _packageName.Value;

public string Name => _name.Value;

public System.Version Version => Utils.ParseVersion(VersionString);

public string VersionString
{
get
{
var pm = Application.Context.PackageManager;
var packageName = Application.Context.PackageName;
using (var info = pm.GetPackageInfo(packageName, PackageInfoFlags.MetaData))
{
return info.VersionName;
}
}
}
public string VersionString => _packageInfo.Value.VersionName;

public string BuildString
{
get
{
var pm = Application.Context.PackageManager;
var packageName = Application.Context.PackageName;
using (var info = pm.GetPackageInfo(packageName, PackageInfoFlags.MetaData))
{
#if __ANDROID_28__
return PackageInfoCompat.GetLongVersionCode(info).ToString(CultureInfo.InvariantCulture);
#else
#pragma warning disable CS0618 // Type or member is obsolete
return info.VersionCode.ToString(CultureInfo.InvariantCulture);
#pragma warning restore CS0618 // Type or member is obsolete
#endif
}
}
}
public string BuildString => PackageInfoCompat.GetLongVersionCode(_packageInfo.Value).ToString(CultureInfo.InvariantCulture);

public void ShowSettingsUI()
{
Expand All @@ -67,40 +37,34 @@ public void ShowSettingsUI()
settingsIntent.SetData(global::Android.Net.Uri.Parse("package:" + PackageName));

var flags = ActivityFlags.NewTask | ActivityFlags.NoHistory | ActivityFlags.ExcludeFromRecents;

#if __ANDROID_24__
if (OperatingSystem.IsAndroidVersionAtLeast(24))
flags |= ActivityFlags.LaunchAdjacent;
#endif
settingsIntent.SetFlags(flags);

context.StartActivity(settingsIntent);
}

public AppTheme RequestedTheme
=> (Application.Context.Resources.Configuration.UiMode & UiMode.NightMask) switch
{
UiMode.NightYes => AppTheme.Dark,
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};
static AppTheme GetRequestedTheme() => (Application.Context.Resources.Configuration.UiMode & UiMode.NightMask) switch
{
UiMode.NightYes => AppTheme.Dark,
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};

public AppTheme RequestedTheme => _requestedTheme.Value;

public AppPackagingModel PackagingModel => AppPackagingModel.Packaged;

public LayoutDirection RequestedLayoutDirection
static LayoutDirection GetLayoutDirection()
{
get
{
if (!OperatingSystem.IsAndroidVersionAtLeast(17))
return LayoutDirection.LeftToRight;

var config = Application.Context.Resources?.Configuration;
if (config == null)
return LayoutDirection.Unknown;

return (config.LayoutDirection == Android.Views.LayoutDirection.Rtl) ? LayoutDirection.RightToLeft :
LayoutDirection.LeftToRight;
}
var config = Application.Context.Resources?.Configuration;
if (config == null)
return LayoutDirection.Unknown;

return (config.LayoutDirection == Android.Views.LayoutDirection.Rtl) ? LayoutDirection.RightToLeft :
LayoutDirection.LeftToRight;
}

public LayoutDirection RequestedLayoutDirection => _layoutDirection.Value;
}
}