Skip to content

Commit

Permalink
Implement Open Settings for all platforms (xamarin#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno authored and Redth committed Jun 25, 2018
1 parent 5e6b038 commit 533a4d7
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Samples/Samples/View/AppInfoPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<Label Text="Version Info:" FontAttributes="Bold" Margin="0,6,0,0" />
<Label Text="{Binding AppVersion, StringFormat='Version: {0}'}" />
<Label Text="{Binding AppBuild, StringFormat='Build: {0}'}" />

<Button Text="Open Settings" Command="{Binding OpenSettingsCommand}"/>
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
8 changes: 8 additions & 0 deletions Samples/Samples/ViewModel/AppInfoViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Samples.ViewModel
{
Expand All @@ -11,5 +12,12 @@ public class AppInfoViewModel : BaseViewModel
public string AppVersion => AppInfo.VersionString;

public string AppBuild => AppInfo.BuildString;

public Command OpenSettingsCommand { get; }

public AppInfoViewModel()
{
OpenSettingsCommand = new Command(() => AppInfo.OpenSettings());
}
}
}
25 changes: 21 additions & 4 deletions Xamarin.Essentials/AppInfo/AppInfo.android.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Globalization;
using Android.Content;
using Android.Content.PM;

namespace Xamarin.Essentials
{
public static partial class AppInfo
{
static string GetPackageName() => Platform.AppContext.PackageName;
static string PlatformGetPackageName() => Platform.AppContext.PackageName;

static string GetName()
static string PlatformGetName()
{
var applicationInfo = Platform.AppContext.ApplicationInfo;
var packageManager = Platform.AppContext.PackageManager;
return applicationInfo.LoadLabel(packageManager);
}

static string GetVersionString()
static string PlatformGetVersionString()
{
var pm = Platform.AppContext.PackageManager;
var packageName = Platform.AppContext.PackageName;
Expand All @@ -24,7 +25,7 @@ static string GetVersionString()
}
}

static string GetBuild()
static string PlatformGetBuild()
{
var pm = Platform.AppContext.PackageManager;
var packageName = Platform.AppContext.PackageName;
Expand All @@ -33,5 +34,21 @@ static string GetBuild()
return info.VersionCode.ToString(CultureInfo.InvariantCulture);
}
}

static void PlatformOpenSettings()
{
var context = Platform.CurrentActivity ?? Platform.AppContext;
if (context == null)
return;

var settingsIntent = new Intent();
settingsIntent.SetAction(global::Android.Provider.Settings.ActionApplicationDetailsSettings);
settingsIntent.AddCategory(Intent.CategoryDefault);
settingsIntent.SetData(global::Android.Net.Uri.Parse("package:" + PlatformGetPackageName()));
settingsIntent.AddFlags(ActivityFlags.NewTask);
settingsIntent.AddFlags(ActivityFlags.NoHistory);
settingsIntent.AddFlags(ActivityFlags.ExcludeFromRecents);
context.StartActivity(settingsIntent);
}
}
}
12 changes: 8 additions & 4 deletions Xamarin.Essentials/AppInfo/AppInfo.ios.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using Foundation;
using UIKit;

namespace Xamarin.Essentials
{
public static partial class AppInfo
{
static string GetPackageName() => GetBundleValue("CFBundleIdentifier");
static string PlatformGetPackageName() => GetBundleValue("CFBundleIdentifier");

static string GetName() => GetBundleValue("CFBundleDisplayName") ?? GetBundleValue("CFBundleName");
static string PlatformGetName() => GetBundleValue("CFBundleDisplayName") ?? GetBundleValue("CFBundleName");

static string GetVersionString() => GetBundleValue("CFBundleShortVersionString");
static string PlatformGetVersionString() => GetBundleValue("CFBundleShortVersionString");

static string GetBuild() => GetBundleValue("CFBundleVersion");
static string PlatformGetBuild() => GetBundleValue("CFBundleVersion");

static string GetBundleValue(string key)
=> NSBundle.MainBundle.ObjectForInfoDictionary(key).ToString();

static void PlatformOpenSettings() =>
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
}
}
10 changes: 6 additions & 4 deletions Xamarin.Essentials/AppInfo/AppInfo.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
{
public static partial class AppInfo
{
static string GetPackageName() => throw new NotImplementedInReferenceAssemblyException();
static string PlatformGetPackageName() => throw new NotImplementedInReferenceAssemblyException();

static string GetName() => throw new NotImplementedInReferenceAssemblyException();
static string PlatformGetName() => throw new NotImplementedInReferenceAssemblyException();

static string GetVersionString() => throw new NotImplementedInReferenceAssemblyException();
static string PlatformGetVersionString() => throw new NotImplementedInReferenceAssemblyException();

static string GetBuild() => throw new NotImplementedInReferenceAssemblyException();
static string PlatformGetBuild() => throw new NotImplementedInReferenceAssemblyException();

static void PlatformOpenSettings() => throw new NotImplementedInReferenceAssemblyException();
}
}
10 changes: 6 additions & 4 deletions Xamarin.Essentials/AppInfo/AppInfo.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ namespace Xamarin.Essentials
{
public static partial class AppInfo
{
public static string PackageName => GetPackageName();
public static string PackageName => PlatformGetPackageName();

public static string Name => GetName();
public static string Name => PlatformGetName();

public static string VersionString => GetVersionString();
public static string VersionString => PlatformGetVersionString();

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

public static string BuildString => GetBuild();
public static string BuildString => PlatformGetBuild();

public static void OpenSettings() => PlatformOpenSettings();
}
}
13 changes: 8 additions & 5 deletions Xamarin.Essentials/AppInfo/AppInfo.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ namespace Xamarin.Essentials
{
public static partial class AppInfo
{
static string GetPackageName() => Package.Current.Id.Name;
static string PlatformGetPackageName() => Package.Current.Id.Name;

static string GetName() => Package.Current.DisplayName;
static string PlatformGetName() => Package.Current.DisplayName;

static string GetVersionString()
static string PlatformGetVersionString()
{
var version = Package.Current.Id.Version;
return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}

static string GetBuild()
=> Package.Current.Id.Version.Build.ToString(CultureInfo.InvariantCulture);
static string PlatformGetBuild() =>
Package.Current.Id.Version.Build.ToString(CultureInfo.InvariantCulture);

static void PlatformOpenSettings() =>
Windows.System.Launcher.LaunchUriAsync(new System.Uri("ms-settings:appsfeatures-app")).WatchForError();
}
}
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Member Id="P:Xamarin.Essentials.AccelerometerData.Acceleration" />
</Type>
<Type Name="Xamarin.Essentials.AppInfo" Id="T:Xamarin.Essentials.AppInfo">
<Member Id="M:Xamarin.Essentials.AppInfo.OpenSettings" />
<Member Id="P:Xamarin.Essentials.AppInfo.BuildString" />
<Member Id="P:Xamarin.Essentials.AppInfo.Name" />
<Member Id="P:Xamarin.Essentials.AppInfo.PackageName" />
Expand Down
3 changes: 2 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Member Id="P:Xamarin.Essentials.AccelerometerData.Acceleration" />
</Type>
<Type Name="Xamarin.Essentials.AppInfo" Id="T:Xamarin.Essentials.AppInfo">
<Member Id="M:Xamarin.Essentials.AppInfo.OpenSettings" />
<Member Id="P:Xamarin.Essentials.AppInfo.BuildString" />
<Member Id="P:Xamarin.Essentials.AppInfo.Name" />
<Member Id="P:Xamarin.Essentials.AppInfo.PackageName" />
Expand Down Expand Up @@ -299,11 +300,11 @@
<Member Id="P:Xamarin.Essentials.Placemark.SubThoroughfare" />
<Member Id="P:Xamarin.Essentials.Placemark.Thoroughfare" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Power" Id="T:Xamarin.Essentials.Power">
<Member Id="E:Xamarin.Essentials.Power.EnergySaverStatusChanaged" />
<Member Id="P:Xamarin.Essentials.Power.EnergySaverStatus" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Preferences" Id="T:Xamarin.Essentials.Preferences">
<Member Id="M:Xamarin.Essentials.Preferences.Clear" />
<Member Id="M:Xamarin.Essentials.Preferences.Clear(System.String)" />
Expand Down
3 changes: 2 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Member Id="P:Xamarin.Essentials.AccelerometerData.Acceleration" />
</Type>
<Type Name="Xamarin.Essentials.AppInfo" Id="T:Xamarin.Essentials.AppInfo">
<Member Id="M:Xamarin.Essentials.AppInfo.OpenSettings" />
<Member Id="P:Xamarin.Essentials.AppInfo.BuildString" />
<Member Id="P:Xamarin.Essentials.AppInfo.Name" />
<Member Id="P:Xamarin.Essentials.AppInfo.PackageName" />
Expand Down Expand Up @@ -298,11 +299,11 @@
<Member Id="P:Xamarin.Essentials.Placemark.SubThoroughfare" />
<Member Id="P:Xamarin.Essentials.Placemark.Thoroughfare" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Power" Id="T:Xamarin.Essentials.Power">
<Member Id="E:Xamarin.Essentials.Power.EnergySaverStatusChanaged" />
<Member Id="P:Xamarin.Essentials.Power.EnergySaverStatus" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Preferences" Id="T:Xamarin.Essentials.Preferences">
<Member Id="M:Xamarin.Essentials.Preferences.Clear" />
<Member Id="M:Xamarin.Essentials.Preferences.Clear(System.String)" />
Expand Down
3 changes: 2 additions & 1 deletion docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Member Id="P:Xamarin.Essentials.AccelerometerData.Acceleration" />
</Type>
<Type Name="Xamarin.Essentials.AppInfo" Id="T:Xamarin.Essentials.AppInfo">
<Member Id="M:Xamarin.Essentials.AppInfo.OpenSettings" />
<Member Id="P:Xamarin.Essentials.AppInfo.BuildString" />
<Member Id="P:Xamarin.Essentials.AppInfo.Name" />
<Member Id="P:Xamarin.Essentials.AppInfo.PackageName" />
Expand Down Expand Up @@ -298,11 +299,11 @@
<Member Id="P:Xamarin.Essentials.Placemark.SubThoroughfare" />
<Member Id="P:Xamarin.Essentials.Placemark.Thoroughfare" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Power" Id="T:Xamarin.Essentials.Power">
<Member Id="E:Xamarin.Essentials.Power.EnergySaverStatusChanaged" />
<Member Id="P:Xamarin.Essentials.Power.EnergySaverStatus" />
</Type>
<Type Name="Xamarin.Essentials.Platform" Id="T:Xamarin.Essentials.Platform" />
<Type Name="Xamarin.Essentials.Preferences" Id="T:Xamarin.Essentials.Preferences">
<Member Id="M:Xamarin.Essentials.Preferences.Clear" />
<Member Id="M:Xamarin.Essentials.Preferences.Clear(System.String)" />
Expand Down
24 changes: 23 additions & 1 deletion docs/en/Xamarin.Essentials/AppInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<Interfaces />
<Docs>
<summary>Represents information about the application.</summary>
<remarks></remarks>
<remarks>
<para />
</remarks>
</Docs>
<Members>
<Member MemberName="BuildString">
Expand Down Expand Up @@ -51,6 +53,26 @@
<remarks></remarks>
</Docs>
</Member>
<Member MemberName="OpenSettings">
<MemberSignature Language="C#" Value="public static void OpenSettings ();" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig void OpenSettings() cil managed" />
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.AppInfo.OpenSettings" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>Open the settings menu or page for the application.</summary>
<remarks>
<para />
</remarks>
</Docs>
</Member>
<Member MemberName="PackageName">
<MemberSignature Language="C#" Value="public static string PackageName { get; }" />
<MemberSignature Language="ILAsm" Value=".property string PackageName" />
Expand Down

0 comments on commit 533a4d7

Please sign in to comment.