コード例 #1
0
        /// <summary>
        /// Checks a selected privilege. Requests a privilege if not set.
        /// </summary>
        /// <param name="privilege">The privilege to check.</param>
        private static void CheckPrivilege(string privilege)
        {
            switch (PrivacyPrivilegeManager.CheckPermission(privilege))
            {
            case CheckResult.Allow:
                SetPermission(privilege, true);
                break;

            case CheckResult.Deny:
                SetPermission(privilege, false);
                break;

            case CheckResult.Ask:
                PrivacyPrivilegeManager.RequestPermission(privilege);
                PrivacyPrivilegeManager.GetResponseContext(privilege)
                .TryGetTarget(out PrivacyPrivilegeManager.ResponseContext context);

                if (context != null)
                {
                    context.ResponseFetched += PrivilegeRequestResponse;
                }

                break;
            }

            AllPrivilegesChecked();
        }
コード例 #2
0
ファイル: PrivilegesHelper.cs プロジェクト: patmosxx-v2/uno
        internal static async Task <bool> RequestAsync(string privilege)
        {
            EnsureDeclared(privilege);

            var checkResult = PrivacyPrivilegeManager.CheckPermission(privilege);

            if (checkResult == CheckResult.Ask)
            {
                var completionSource = new TaskCompletionSource <bool>();
                if (PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out var context))
                {
                    void OnResponseFetched(object sender, RequestResponseEventArgs e)
                    {
                        completionSource.TrySetResult(e.result == RequestResult.AllowForever);
                    }

                    context.ResponseFetched += OnResponseFetched;
                    PrivacyPrivilegeManager.RequestPermission(privilege);
                    var result = await completionSource.Task;
                    context.ResponseFetched -= OnResponseFetched;
                    return(result);
                }
                return(false);
            }
            else if (checkResult == CheckResult.Deny)
            {
                return(false);
            }
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Returns true if all required privileges are granted, false otherwise.
        /// </summary>
        /// <returns>Task with check result.</returns>
        public async Task <bool> CheckPrivileges()
        {
            CheckResult result = PrivacyPrivilegeManager.CheckPermission(HEALTHINFO_PRIVILEGE);

            switch (result)
            {
            case CheckResult.Allow:
                return(true);

            case CheckResult.Deny:
                return(false);

            case CheckResult.Ask:
                PrivacyPrivilegeManager.ResponseContext context = null;
                PrivacyPrivilegeManager.GetResponseContext(HEALTHINFO_PRIVILEGE)
                .TryGetTarget(out context);

                if (context == null)
                {
                    Log.Error("STT", "Unable to get privilege response context");
                    return(false);
                }

                _checkPrivilegesTask = new TaskCompletionSource <bool>();

                context.ResponseFetched += PrivilegeManagerOnResponseFetched;

                PrivacyPrivilegeManager.RequestPermission(HEALTHINFO_PRIVILEGE);
                return(await _checkPrivilegesTask.Task);

            default:
                return(false);
            }
        }
コード例 #4
0
        /// <summary>
        /// Checks location privilege. Request location privilege if not set.
        /// Starts application when privilege check is complete.
        /// </summary>
        private void LocationPrivilegeCheck()
        {
            CheckResult result = PrivacyPrivilegeManager.CheckPermission(Config.LocationPrivilege);

            switch (result)
            {
            case CheckResult.Allow:
                ViewModels.ViewModelLocator.ViewModel.IsGPSAvailable = true;
                StartApplication();

                break;

            case CheckResult.Deny:
                ViewModels.ViewModelLocator.ViewModel.IsGPSAvailable = false;
                StartApplication();

                break;

            case CheckResult.Ask:
                PrivacyPrivilegeManager.GetResponseContext(Config.LocationPrivilege)
                .TryGetTarget(out PrivacyPrivilegeManager.ResponseContext context);

                if (context != null)
                {
                    context.ResponseFetched += PPM_RequestResponse;
                }

                PrivacyPrivilegeManager.RequestPermission(Config.LocationPrivilege);
                break;
            }
        }
コード例 #5
0
        private void CheckPrivileges()
        {
            // Check permission status (allow, deny, ask) to determinate action which has to be taken
            string      privilege = "http://tizen.org.privilege/healthinfo";
            CheckResult result    = PrivacyPrivilegeManager.CheckPermission(privilege);

            if (result == CheckResult.Allow)
            {
                OnPrivilegesGranted();
            }
            else if (result == CheckResult.Deny)
            {
                OnPrivilegesDenied();
            }
            else // the user must be asked about granting the privilege
            {
                PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out var context);

                if (context != null)
                {
                    context.ResponseFetched += (sender, e) =>
                    {
                        if (e.cause == CallCause.Answer && e.result == RequestResult.AllowForever)
                        {
                            OnPrivilegesGranted();
                        }
                        else
                        {
                            OnPrivilegesDenied();
                        }
                    };
                }
            }
        }
コード例 #6
0
 private void SetupPrivilegeHandler()
 {
     PrivacyPrivilegeManager.ResponseContext context = null;
     if (PrivacyPrivilegeManager.GetResponseContext(hrmPrivilege).TryGetTarget(out context))
     {
         context.ResponseFetched += PrivilegeResponseHandler;
     }
 }
コード例 #7
0
 private static void SetupPPMHandler(string privilege)
 {
     PrivacyPrivilegeManager.ResponseContext context = null;
     if (PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out context))
     {
         context.ResponseFetched += PPMResponseHandler;
     }
 }
コード例 #8
0
        /// <summary>
        /// Used to set privilege event listener and request the privilege.
        /// </summary>
        void AskPrivacyPrivilege(string privilege)
        {
            // Set event listener for privilege
            PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out context);
            if (context != null)
            {
                context.ResponseFetched += PPM_RequestResponse;
            }

            // Request pop-up message for privileges
            PrivacyPrivilegeManager.RequestPermission(privilege);
        }
コード例 #9
0
        /// <summary>
        /// Request user permission for privacy privileges
        /// </summary>
        /// <param name="service">privacy privilege</param>
        /// <returns>true if user consent is gained</returns>
        public async Task <bool> GetPermission(string service)
        {
            try
            {
                GrantedPermission = -1;
                // Gets the status of a privacy privilege permission.
                CheckResult result = PrivacyPrivilegeManager.CheckPermission(service);
                switch (result)
                {
                case CheckResult.Allow:
                    // user consent for privacy privilege is already gained
                    Console.WriteLine("\n\n\nuser consent for privacy privilege is already gained\n\n");
                    return(true);

                case CheckResult.Deny:
                case CheckResult.Ask:
                    // User permission request should be required
                    tcs = new TaskCompletionSource <bool>();
                    // Gets the response context for a given privilege.
                    var reponseContext = PrivacyPrivilegeManager.GetResponseContext(service);
                    PrivacyPrivilegeManager.ResponseContext context = null;
                    if (reponseContext.TryGetTarget(out context))
                    {
                        if (context != null)
                        {
                            context.ResponseFetched += Context_ResponseFetched;
                        }
                    }
                    // Try to get the permission for service from a user.
                    PrivacyPrivilegeManager.RequestPermission(service);

                    // Check if permission is granted or not every second
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Device.StartTimer(new TimeSpan(0, 0, 0, 1, 0), CheckPermission);
                    });

                    return(await tcs.Task);

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[UserPermission] (" + service + ") Error :" + e.Message + ", " + e.StackTrace + ", " + e.InnerException);
                return(false);
            }
        }
コード例 #10
0
        private Task RequestPermission()
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            var response = PrivacyPrivilegeManager.GetResponseContext(MEDIASTORAGE_PRIVILEGE);

            PrivacyPrivilegeManager.ResponseContext target;
            response.TryGetTarget(out target);
            target.ResponseFetched += (s, e) =>
            {
                tcs.SetResult(true);
                _mainPage.RestartScan();
            };
            PrivacyPrivilegeManager.RequestPermission(MEDIASTORAGE_PRIVILEGE);

            return(tcs.Task);
        }
コード例 #11
0
        /// <summary>
        /// Check privacy privilege and if need to ask for user, send request for PPM.
        /// </summary>
        public bool CheckPrivilege()
        {
            // Make array list for requesting privacy privilege
            // Contacts need 2 privilege, contact read and account write.
            ArrayList PrivilegeList = new ArrayList();

            PrivilegeList.Add("http://tizen.org/privilege/contact.read");
            PrivilegeList.Add("http://tizen.org/privilege/contact.write");
            int privilageAcceptedCount = 0;

            // Check and request privacy privilege if app is needed
            foreach (string list in PrivilegeList)
            {
                PrivacyPrivilegeManager.GetResponseContext(list).TryGetTarget(out context);
                if (context != null)
                {
                    ++CBCount;
                    context.ResponseFetched += PPM_RequestResponse;
                }

                CheckResult result = PrivacyPrivilegeManager.CheckPermission(list);
                switch (result)
                {
                case CheckResult.Allow:
                    /// Privilege can be used
                    privilageAcceptedCount++;
                    break;

                case CheckResult.Deny:
                    /// Privilege can't be used
                    break;

                case CheckResult.Ask:
                    /// Request permission to user
                    PrivacyPrivilegeManager.RequestPermission(list);
                    break;
                }
            }
            if (privilageAcceptedCount == PrivilegeList.Count)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #12
0
        /// <summary>
        /// Check if a permission is granted and request a permission if it's not granted
        /// </summary>
        /// <param name="privilege">privilege name</param>
        /// <returns>bool</returns>
        public async Task <bool> CheckAndRequestPermission(string privilege)
        {
            try
            {
                // Gets the status of a privacy privilege permission.
                CheckResult result = PrivacyPrivilegeManager.CheckPermission(privilege);
                Log.Debug(Utility.LOG_TAG, "State: " + result.ToString());
                switch (result)
                {
                case CheckResult.Allow:
                    // Permission has already been granted
                    // Privilege can be used
                    return(true);

                case CheckResult.Deny:
                    // Privilege can't be used
                    Log.Debug(Utility.LOG_TAG, "In this case, " + privilege + " privilege is not available until the user changes the permission state from the Settings application.");
                    return(false);

                case CheckResult.Ask:
                    // Permission is not granted so we need to request the permission
                    PrivacyPrivilegeManager.ResponseContext context;
                    PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out context);
                    if (context == null)
                    {
                        return(false);
                    }

                    tcs = new TaskCompletionSource <bool>();
                    context.ResponseFetched += PPMResponseHandler;
                    // Request a permission
                    // Calling it brings up a system dialog that an app user can decide to approve or deny it.
                    PrivacyPrivilegeManager.RequestPermission(privilege);
                    return(await tcs.Task);

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                // Handle exception
                Log.Error(Utility.LOG_TAG, "An error occurred. : " + e.Message);
                return(false);
            }
        }
コード例 #13
0
            async Task <PermissionStatus> CheckPrivilegeAsync(bool ask)
            {
                if (RequiredPrivileges == null || !RequiredPrivileges.Any())
                {
                    return(PermissionStatus.Granted);
                }

                EnsureDeclared();

                var tizenPrivileges = RequiredPrivileges.Where(p => p.isRuntime);

                foreach (var(tizenPrivilege, isRuntime) in tizenPrivileges)
                {
                    var checkResult = PrivacyPrivilegeManager.CheckPermission(tizenPrivilege);
                    if (checkResult == CheckResult.Ask)
                    {
                        if (ask)
                        {
                            var tcs = new TaskCompletionSource <bool>();
                            PrivacyPrivilegeManager.GetResponseContext(tizenPrivilege)
                            .TryGetTarget(out var context);
                            void OnResponseFetched(object sender, RequestResponseEventArgs e)
                            {
                                tcs.TrySetResult(e.result == RequestResult.AllowForever);
                            }

                            context.ResponseFetched += OnResponseFetched;
                            PrivacyPrivilegeManager.RequestPermission(tizenPrivilege);
                            var result = await tcs.Task;
                            context.ResponseFetched -= OnResponseFetched;
                            if (result)
                            {
                                continue;
                            }
                        }
                        return(PermissionStatus.Denied);
                    }
                    else if (checkResult == CheckResult.Deny)
                    {
                        return(PermissionStatus.Denied);
                    }
                }
                return(PermissionStatus.Granted);
            }
コード例 #14
0
        private void initDataSourcesWithPrivileges()
        {
            PrivacyPrivilegeManager.ResponseContext context = null;
            if (PrivacyPrivilegeManager.GetResponseContext(Tools.HEALTHINFO_PRIVILEGE).TryGetTarget(out context))
            {
                context.ResponseFetched += (s, e) =>
                {
                    if (e.result != RequestResult.AllowForever)
                    {
                        Toast.DisplayText("Please provide the necessary privileges for the application to run!");
                        Environment.Exit(1);
                    }
                    else
                    {
                        initCampaignDataSources();
                    }
                }
            }
            ;
            else
            {
                Toast.DisplayText("Please provide the necessary privileges for the application to run!");
                Environment.Exit(1);
            }

            switch (PrivacyPrivilegeManager.CheckPermission(Tools.HEALTHINFO_PRIVILEGE))
            {
            case CheckResult.Allow:
                initCampaignDataSources();
                break;

            case CheckResult.Deny:
                Toast.DisplayText("Please provide the necessary privileges for the application to run!");
                Environment.Exit(1);
                break;

            case CheckResult.Ask:
                PrivacyPrivilegeManager.RequestPermission(Tools.HEALTHINFO_PRIVILEGE);
                break;

            default:
                break;
            }
        }
コード例 #15
0
ファイル: Platform.cs プロジェクト: sreenivasarajiv/shiny
        static async Task <AccessState> RequestPermission(string priv)
        {
            var result  = AccessState.Unknown;
            var tcs     = new TaskCompletionSource <AccessState>();
            var handler = new EventHandler <RequestResponseEventArgs>((sender, args) =>
            {
                switch (args.result)
                {
                case RequestResult.AllowForever:
                    tcs.TrySetResult(AccessState.Available);
                    break;

                default:
                    tcs.TrySetResult(AccessState.Denied);
                    break;
                }
            });

            PrivacyPrivilegeManager.ResponseContext context = null;

            try
            {
                PrivacyPrivilegeManager.GetResponseContext(priv).TryGetTarget(out context);

                if (context == null)
                {
                    tcs.SetResult(AccessState.NotSupported);
                }
                else
                {
                    context.ResponseFetched += handler;
                    PrivacyPrivilegeManager.RequestPermission(priv);
                }
                result = await tcs.Task.ConfigureAwait(false);
            }
            finally
            {
                if (context != null)
                {
                    context.ResponseFetched -= handler;
                }
            }
            return(result);
        }
コード例 #16
0
        /// <summary>
        /// Check privacy privilege and if need to ask for user, send request for PPM.
        /// </summary>
        public async Task <bool> CheckPrivilege()
        {
            // Make array list for requesting privacy privilege
            // Contacts need 2 privilege, contact read and account write.
            var privileges = new List <string>
            {
                "http://tizen.org/privilege/contact.read",
                "http://tizen.org/privilege/contact.write"
            };

            // Check and request privacy privilege if app is needed
            foreach (string privilege in privileges)
            {
                CheckResult result = PrivacyPrivilegeManager.CheckPermission(privilege);
                switch (result)
                {
                case CheckResult.Allow:
                    break;

                case CheckResult.Deny:
                    return(false);

                case CheckResult.Ask:
                    /// Request permission to user
                    if (PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out PrivacyPrivilegeManager.ResponseContext context))
                    {
                        TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
                        context.ResponseFetched += (s, e) =>
                        {
                            tcs.SetResult(e.result == RequestResult.AllowForever);
                        };
                        PrivacyPrivilegeManager.RequestPermission(privilege);
                        if (!(await tcs.Task))
                        {
                            return(false);
                        }
                    }
                    break;
                }
            }

            return(true);
        }
コード例 #17
0
        static Task <bool> RequestPermission(string privilege)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            var response = PrivacyPrivilegeManager.GetResponseContext(privilege);

            PrivacyPrivilegeManager.ResponseContext target;
            response.TryGetTarget(out target);
            target.ResponseFetched += (s, e) =>
            {
                if (e.cause == CallCause.Error)
                {
                    /// Handle errors
                    Tizen.Log.Error(Constants.logTag, "An error occurred while requesting an user permission");
                    tcs.SetResult(false);
                    return;
                }

                // Now, we can check if the permission is granted or not
                switch (e.result)
                {
                case RequestResult.AllowForever:
                    // Permission is granted.
                    // We can do this permission-related task we want to do.
                    Tizen.Log.Info(Constants.logTag, "Response : RequestResult.AllowForever");
                    tcs.SetResult(true);
                    break;

                case RequestResult.DenyForever:
                case RequestResult.DenyOnce:
                    // Functionality that depends on this permission will not be available
                    Tizen.Log.Info(Constants.logTag, "Response: RequestResult." + e.result.ToString());
                    tcs.SetResult(false);
                    break;
                }
            };

            PrivacyPrivilegeManager.RequestPermission(privilege);

            return(tcs.Task);
        }
コード例 #18
0
        /// <summary>
        /// Request user permission for privacy privileges
        /// </summary>
        /// <param name="service">privacy privilege</param>
        /// <returns>true if user consent is gained</returns>
        public async Task <bool> GetPermission(string service)
        {
            try
            {
                // Gets the status of a privacy privilege permission.
                CheckResult result = PrivacyPrivilegeManager.CheckPermission(service);
                switch (result)
                {
                case CheckResult.Allow:
                    // user consent for privacy privilege is already gained
                    return(true);

                case CheckResult.Deny:
                case CheckResult.Ask:
                    // User permission request should be required
                    tcs = new TaskCompletionSource <bool>();
                    // Gets the response context for a given privilege.
                    var reponseContext = PrivacyPrivilegeManager.GetResponseContext(service);
                    PrivacyPrivilegeManager.ResponseContext context = null;
                    if (reponseContext.TryGetTarget(out context))
                    {
                        if (context != null)
                        {
                            context.ResponseFetched += Context_ResponseFetched;
                        }
                    }
                    // Try to get the permission for service from a user.
                    PrivacyPrivilegeManager.RequestPermission(service);

                    return(await tcs.Task);

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[UserPermission] (" + service + ") Error :" + e.Message);
                return(false);
            }
        }
コード例 #19
0
        /// <summary>
        /// Check privacy privilege and if need to ask for user,
        /// send request for Privacy Privilege Manager.
        /// </summary>
        public void CheckAccountPrivilege()
        {
            // Make array list for requesting privacy privilege
            // Account sample need 2 privilege, account read and account write
            ArrayList privilegeList = new ArrayList
            {
                "http://tizen.org/privilege/account.read",

                // Account.read and account.write is same ppm. So if we use both, appear same pop up 2 times. So security team guide use just one.
                // "http://tizen.org/privilege/account.write"
            };

            // Check and request privacy privilege if app is needed
            foreach (string list in privilegeList)
            {
                PrivacyPrivilegeManager.GetResponseContext(list).TryGetTarget(out context);
                if (context != null)
                {
                    ++callbackCount;
                    context.ResponseFetched += PpmRequestResponse;
                }

                CheckResult result = PrivacyPrivilegeManager.CheckPermission(list);
                switch (result)
                {
                case CheckResult.Allow:
                    // Privilege can be used
                    break;

                case CheckResult.Deny:
                    // Privilege can't be used
                    break;

                case CheckResult.Ask:
                    // User permission request required
                    PrivacyPrivilegeManager.RequestPermission(list);
                    break;
                }
            }
        }
コード例 #20
0
        protected static void Check(string permission, Action <bool> callback)
        {
            var initPermission = PrivacyPrivilegeManager.CheckPermission(permission);

            if (initPermission == CheckResult.Allow)
            {
                callback(true);
                return;
            }
            if (initPermission == CheckResult.Deny)
            {
                callback(false);
                return;
            }
            if (!PrivacyPrivilegeManager.GetResponseContext(permission).TryGetTarget(out var context))
            {
                callback(false);
                return;
            }
            context.ResponseFetched += (sender, e) => callback(e.result == RequestResult.AllowForever);
            PrivacyPrivilegeManager.RequestPermission(permission);
        }
コード例 #21
0
        public async Task <bool> CheckAndRequestPermission(string privilege)
        {
            try
            {
                CheckResult result = PrivacyPrivilegeManager.CheckPermission(privilege);
                Log.Debug(Utility.LOG_TAG, "State: " + result.ToString());
                switch (result)
                {
                case CheckResult.Allow:
                    return(true);

                case CheckResult.Deny:
                    Log.Debug(Utility.LOG_TAG, "In this case, health data is not available until the user changes the permission state from the Settings application.");
                    return(false);

                case CheckResult.Ask:
                    // Request permission to User
                    PrivacyPrivilegeManager.ResponseContext context;
                    PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out context);
                    if (context == null)
                    {
                        return(false);
                    }
                    tcs = new TaskCompletionSource <bool>();
                    context.ResponseFetched += PPMResponseHandler;
                    PrivacyPrivilegeManager.RequestPermission(privilege);
                    return(await tcs.Task);

                default:
                    return(false);
                }
            }
            catch (Exception e)
            {
                // Handle exception
                Log.Error(Utility.LOG_TAG, "An error occurred. : " + e.Message);
                return(false);
            }
        }
コード例 #22
0
        /// <summary>
        /// Used to set privilege event listener and request the privilege.
        /// </summary>
        private static void PPM_RequestResponse(object sender, RequestResponseEventArgs e)
        {
            if (e.cause == CallCause.Answer)
            {
                switch (e.result)
                {
                // Allow clicked case
                case RequestResult.AllowForever:
                    if (e.privilege == "http://tizen.org/privilege/calendar.read")
                    {
                        pEvent.AllowCalendarReadPrivilege();
                    }
                    else if (e.privilege == "http://tizen.org/privilege/contact.read")
                    {
                        pEvent.AllowContactReadPrivilege();
                    }
                    break;

                case RequestResult.DenyForever:
                    break;

                case RequestResult.DenyOnce:
                    break;
                }
                ;

                // Unset event listener for privilege
                PrivacyPrivilegeManager.GetResponseContext(e.privilege).TryGetTarget(out context);
                if (context != null)
                {
                    context.ResponseFetched -= PPM_RequestResponse;
                }
            }
            else
            {
                Console.WriteLine("Error occurs during requesting permission for {0}", e.privilege);
            }
        }
コード例 #23
0
ファイル: Permissions.tizen.cs プロジェクト: ycfft/Essentials
        internal static async Task <PermissionStatus> CheckPrivacyPermission(PermissionType permission, bool askUser)
        {
            EnsureDeclared(permission);
            var tizenPrivileges = permission.ToTizenPrivileges(onlyRuntimePermissions: true);

            foreach (var priv in tizenPrivileges)
            {
                if (PrivacyPrivilegeManager.CheckPermission(priv) == CheckResult.Ask)
                {
                    if (askUser)
                    {
                        var tcs = new TaskCompletionSource <bool>();
                        PrivacyPrivilegeManager.ResponseContext context = null;
                        PrivacyPrivilegeManager.GetResponseContext(priv).TryGetTarget(out context);
                        void OnResponseFetched(object sender, RequestResponseEventArgs e)
                        {
                            tcs.TrySetResult(e.result == RequestResult.AllowForever);
                        }

                        context.ResponseFetched += OnResponseFetched;
                        PrivacyPrivilegeManager.RequestPermission(priv);
                        var result = await tcs.Task;
                        context.ResponseFetched -= OnResponseFetched;
                        if (result)
                        {
                            continue;
                        }
                    }
                    return(PermissionStatus.Denied);
                }
                else if (PrivacyPrivilegeManager.CheckPermission(priv) == CheckResult.Deny)
                {
                    return(PermissionStatus.Denied);
                }
            }
            return(PermissionStatus.Granted);
        }
コード例 #24
0
        /// <summary>
        /// Asks the user to grant the permission at runtime
        /// </summary>
        /// <param name="privilege">The privilege name to check for</param>
        /// <returns>A status that indicates whether the permission has been granted or not</returns>
        public static async Task <PrivacyPermissionStatus> RequestAsync(string privilege)
        {
            switch (PrivacyPrivilegeManager.CheckPermission(privilege))
            {
            case CheckResult.Allow:
                return(PrivacyPermissionStatus.Granted);

            case CheckResult.Deny:
            case CheckResult.Ask:
                var tcs      = new TaskCompletionSource <PrivacyPermissionStatus>();
                var response = PrivacyPrivilegeManager.GetResponseContext(privilege);
                PrivacyPrivilegeManager.ResponseContext context = null;

                if (response.TryGetTarget(out context))
                {
                    context.ResponseFetched += (s, e) =>
                    {
                        PrivacyPermissionStatus result = PrivacyPermissionStatus.Denied;

                        if (e.result == RequestResult.AllowForever)
                        {
                            result = PrivacyPermissionStatus.Granted;
                        }

                        tcs.SetResult(result);
                    };
                }

                PrivacyPrivilegeManager.RequestPermission(privilege);

                return(await tcs.Task);

            default:
                return(PrivacyPermissionStatus.Denied);
            }
        }