Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

[WIP] Permission plugin #83

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions MvvmCross.Plugins.sln
Expand Up @@ -305,6 +305,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvvmCross.Plugins.Email.Win
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvvmCross.Plugins.Email.WindowsUWP", "Email\MvvmCross.Plugins.Email.WindowsUWP\MvvmCross.Plugins.Email.WindowsUWP.csproj", "{645DE858-1C4C-4F7B-98A5-070EE1D331E6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Permissions", "Permissions", "{8209CF8F-801C-4982-A398-A3EAF99D4BCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvvmCross.Plugins.Permissions", "Permissions\Core\MvvmCross.Plugins.Permissions.csproj", "{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2333,6 +2337,22 @@ Global
{645DE858-1C4C-4F7B-98A5-070EE1D331E6}.Release|x64.Build.0 = Release|x64
{645DE858-1C4C-4F7B-98A5-070EE1D331E6}.Release|x86.ActiveCfg = Release|x86
{645DE858-1C4C-4F7B-98A5-070EE1D331E6}.Release|x86.Build.0 = Release|x86
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|ARM.ActiveCfg = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|ARM.Build.0 = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|x64.ActiveCfg = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|x64.Build.0 = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|x86.ActiveCfg = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Debug|x86.Build.0 = Debug|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|Any CPU.Build.0 = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|ARM.ActiveCfg = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|ARM.Build.0 = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|x64.ActiveCfg = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|x64.Build.0 = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|x86.ActiveCfg = Release|Any CPU
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2464,5 +2484,6 @@ Global
{0B08507A-DA49-4069-B8CF-0FB0B7895B21} = {EB7A7EA9-995D-44EC-A4C9-7936BD6DF759}
{D381A63C-994E-48FB-A32E-B3667BFEFC84} = {83DC5338-32A2-47D1-974E-1F3ACC600A03}
{645DE858-1C4C-4F7B-98A5-070EE1D331E6} = {83DC5338-32A2-47D1-974E-1F3ACC600A03}
{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841} = {8209CF8F-801C-4982-A398-A3EAF99D4BCC}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions Permissions/Core/IMvxPermissions.cs
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MvvmCross.Plugins.Permissions
{
public interface IMvxPermissions
{
Task<bool> ShouldShowRequestPermissionRationaleAsync(MvxPermission permission);
Task<MvxPermissionStatus> CheckPermissionStatusAsync(MvxPermission permission);
Task<Dictionary<MvxPermission, MvxPermissionStatus>> RequestPermissionsAsync(params MvxPermission[] permissions);
}
}
74 changes: 74 additions & 0 deletions Permissions/Core/MvvmCross.Plugins.Permissions.csproj
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B3B46E7E-CDA2-4297-8700-6E8EBBE3A841}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MvvmCross.Plugins.Permissions</RootNamespace>
<AssemblyName>MvvmCross.Plugins.Permissions</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\bin\Debug\Mvx\Portable\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\Release\Mvx\Portable\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="IMvxPermissions.cs" />
<Compile Include="MvxPermission.cs" />
<Compile Include="MvxPermissionExtensions.cs" />
<Compile Include="MvxPermissions.cs" />
<Compile Include="MvxPermissionStatus.cs" />
<Compile Include="PluginLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MvvmCross.Core, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Core.4.1.0\lib\portable-net45+win+wpa81+wp80\MvvmCross.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Platform, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Platform.4.1.0\lib\portable-net45+win+wpa81+wp80\MvvmCross.Platform.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Plugin.Permissions, Version=1.1.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Plugin.Permissions.1.1.7\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Permissions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Plugin.Permissions.Abstractions, Version=1.1.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Plugin.Permissions.1.1.7\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Permissions.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
18 changes: 18 additions & 0 deletions Permissions/Core/MvxPermission.cs
@@ -0,0 +1,18 @@
namespace MvvmCross.Plugins.Permissions
{
public enum MvxPermission
{
Unknown,
Calendar,
Camera,
Contacts,
Location,
Microphone,
Phone,
Photos,
Reminders,
Sensors,
Sms,
Storage
}
}
103 changes: 103 additions & 0 deletions Permissions/Core/MvxPermissionExtensions.cs
@@ -0,0 +1,103 @@
using Plugin.Permissions.Abstractions;

namespace MvvmCross.Plugins.Permissions
{
public static class MvxPermissionExtensions
{
public static Permission ToPermission(this MvxPermission permission)
{
switch (permission)
{
case MvxPermission.Calendar:
return Permission.Calendar;
case MvxPermission.Camera:
return Permission.Camera;
case MvxPermission.Contacts:
return Permission.Contacts;
case MvxPermission.Location:
return Permission.Location;
case MvxPermission.Microphone:
return Permission.Microphone;
case MvxPermission.Phone:
return Permission.Phone;
case MvxPermission.Photos:
return Permission.Photos;
case MvxPermission.Reminders:
return Permission.Reminders;
case MvxPermission.Sensors:
return Permission.Sensors;
case MvxPermission.Sms:
return Permission.Sms;
case MvxPermission.Storage:
return Permission.Storage;
default:
return Permission.Unknown;
}
}

public static PermissionStatus ToPermissionStatus(this MvxPermissionStatus permissionStatus)
{
switch (permissionStatus)
{
case MvxPermissionStatus.Denied:
return PermissionStatus.Denied;
case MvxPermissionStatus.Disabled:
return PermissionStatus.Disabled;
case MvxPermissionStatus.Granted:
return PermissionStatus.Granted;
case MvxPermissionStatus.Restricted:
return PermissionStatus.Restricted;
default:
return PermissionStatus.Unknown;
}
}

public static MvxPermission ToMvxPermission(this Permission permission)
{
switch (permission)
{
case Permission.Calendar:
return MvxPermission.Calendar;
case Permission.Camera:
return MvxPermission.Camera;
case Permission.Contacts:
return MvxPermission.Contacts;
case Permission.Location:
return MvxPermission.Location;
case Permission.Microphone:
return MvxPermission.Microphone;
case Permission.Phone:
return MvxPermission.Phone;
case Permission.Photos:
return MvxPermission.Photos;
case Permission.Reminders:
return MvxPermission.Reminders;
case Permission.Sensors:
return MvxPermission.Sensors;
case Permission.Sms:
return MvxPermission.Sms;
case Permission.Storage:
return MvxPermission.Storage;
default:
return MvxPermission.Unknown;
}
}

public static MvxPermissionStatus ToMvxPermissionStatus(this PermissionStatus permissionStatus)
{
switch (permissionStatus)
{
case PermissionStatus.Denied:
return MvxPermissionStatus.Denied;
case PermissionStatus.Disabled:
return MvxPermissionStatus.Disabled;
case PermissionStatus.Granted:
return MvxPermissionStatus.Granted;
case PermissionStatus.Restricted:
return MvxPermissionStatus.Restricted;
default:
return MvxPermissionStatus.Unknown;
}
}
}
}
11 changes: 11 additions & 0 deletions Permissions/Core/MvxPermissionStatus.cs
@@ -0,0 +1,11 @@
namespace MvvmCross.Plugins.Permissions
{
public enum MvxPermissionStatus
{
Denied,
Disabled,
Granted,
Restricted,
Unknown
}
}
29 changes: 29 additions & 0 deletions Permissions/Core/MvxPermissions.cs
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Plugin.Permissions;

namespace MvvmCross.Plugins.Permissions
{
public class MvxPermissions : IMvxPermissions
{
public Task<bool> ShouldShowRequestPermissionRationaleAsync(MvxPermission permission)
{
return CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(permission.ToPermission());
}

public async Task<MvxPermissionStatus> CheckPermissionStatusAsync(MvxPermission permission)
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission.ToPermission()).ConfigureAwait(false);
return status.ToMvxPermissionStatus();
}

public async Task<Dictionary<MvxPermission, MvxPermissionStatus>> RequestPermissionsAsync(params MvxPermission[] permissions)
{
var nativePermissions = permissions.Select(p => p.ToPermission()).ToArray();

var status = await CrossPermissions.Current.RequestPermissionsAsync(nativePermissions).ConfigureAwait(false);
return status.ToDictionary(k => k.Key.ToMvxPermission(), v => v.Value.ToMvxPermissionStatus());
}
}
}
29 changes: 29 additions & 0 deletions Permissions/Core/PluginLoader.cs
@@ -0,0 +1,29 @@
// PluginLoader.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt

using MvvmCross.Platform;
using MvvmCross.Platform.Plugins;

namespace MvvmCross.Plugins.Permissions
{
public class PluginLoader
: IMvxPluginLoader
{
public static readonly PluginLoader Instance = new PluginLoader();

private bool _loaded;

public void EnsureLoaded()
{
if (_loaded)
{
return;
}

Mvx.RegisterSingleton<IMvxPermissions>(new MvxPermissions());
_loaded = true;
}
}
}
30 changes: 30 additions & 0 deletions Permissions/Core/Properties/AssemblyInfo.cs
@@ -0,0 +1,30 @@
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Core")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6 changes: 6 additions & 0 deletions Permissions/Core/packages.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MvvmCross.Core" version="4.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="MvvmCross.Platform" version="4.1.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="Plugin.Permissions" version="1.1.7" targetFramework="portable45-net45+win8+wpa81" />
</packages>