/// <summary> /// Requests the specified privileges. This may possibly solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> public static MLResult RequestPrivilege(MLPrivileges.Id privilegeId) { try { if (MLPrivileges.IsValidInstance()) { MLResult.Code requestPrivilegeResult = NativeBindings.MLPrivilegesRequestPrivilege(privilegeId); MLResult result = MLResult.Create(requestPrivilegeResult); if (result.Result != MLResult.Code.PrivilegeGranted) { MLPluginLog.ErrorFormat("MLPrivileges.RequestPrivilege failed to request {0}. Reason: {1}", privilegeId, result); } return(result); } else { MLPluginLog.ErrorFormat("MLPrivileges.RequestPrivilege failed. Reason: No Instance for MLPrivileges."); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.RequestPrivilege failed. Reason: No Instance for MLPrivileges.")); } } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLPrivileges.RequestPrivilege failed. Reason: API symbols not found"); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.RequestPrivilege failed. Reason: API symbols not found.")); } }
/// <summary> /// Initializes a new instance of the <see cref="RequestPrivilegeQuery"/> class. /// </summary> /// <param name="callback">The callback that should receive the notification.</param> /// <param name="request">A pointer to the request.</param> /// <param name="privilege">The privilege Id.</param> public RequestPrivilegeQuery(CallbackDelegate callback, IntPtr request, MLPrivileges.Id privilege) { this.Callback = callback; this.Result = MLResult.Create(MLResult.Code.Pending); this.Request = request; this.PrivilegeId = privilege; }
private MLResult RequestPrivilegeAsyncInternal(MLPrivileges.Id privilegeId, CallbackDelegate callback) { if (callback == null) { return(MLResult.Create(MLResult.Code.InvalidParam, "MLPrivileges.RequestPrivilegeAsync failed. Reason: Must send a valid callback.")); } if (!this.currentRequests.ContainsKey(privilegeId)) { IntPtr newRequest = IntPtr.Zero; MLResult.Code resultCode = NativeBindings.MLPrivilegesRequestPrivilegeAsync(privilegeId, ref newRequest); if (resultCode == MLResult.Code.Ok) { RequestPrivilegeQuery newQuery = new RequestPrivilegeQuery(callback, newRequest, privilegeId); this.currentRequests.Add(privilegeId, newQuery); } return(MLResult.Create(resultCode)); } else { return(MLResult.Create(MLResult.Code.Ok)); } }
private static void CheckAndRequestPrivilege(MLPrivileges.Id privilegeId) { if (MLPrivileges.CheckPrivilege(privilegeId) == MLResult.Code.PrivilegeNotGranted) { Debug.LogError(MLResult.Code.PrivilegeNotGranted + ": " + privilegeId); MLPrivileges.RequestPrivilege(privilegeId); } }
/// <summary> /// Request privileges asynchronously. /// </summary> ////<param name="callback">The method to call back once a result is known. It will be called when all privileges are granted or if requesting all privileges fails.</param> /// <param name="privileges">An array of privileges to request.</param> public static MLResult RequestPrivilegesAsync(Action <MLResult> callback, params MLPrivileges.Id[] privileges) { #if PLATFORM_LUMIN int numPrivilegesToRequest = privileges.Length; for (int i = 0; i < privileges.Length; i++) { MLPrivileges.Id privilege = privileges[i]; _result = CheckPrivilege(privilege); if (_result.Result == MLResult.Code.PrivilegeGranted) { numPrivilegesToRequest--; if (numPrivilegesToRequest == 0) { callback?.Invoke(_result); } continue; } _result = MLPrivileges.RequestPrivilegeAsync(privilege, (MLResult result, MLPrivileges.Id priv) => { numPrivilegesToRequest--; if (result.Result == MLResult.Code.PrivilegeGranted) { if (numPrivilegesToRequest == 0) { callback?.Invoke(result); } } // Privilege was not granted else { numPrivilegesToRequest = 0; if (numPrivilegesToRequest == 0) { callback?.Invoke(result); } } }); if (!_result.IsOk) { return(_result); } } // Override result in case privilege was already granted. if (_result.Result == MLResult.Code.PrivilegeGranted) { _result = MLResult.Create(MLResult.Code.Ok); } #endif return(_result); }
/// <summary> /// Used to check if your privilege has already been granted. /// </summary> /// <param name="privilege">The privilege to check for.</param> public static MLResult CheckPrivilege(MLPrivileges.Id privilege) { #if PLATFORM_LUMIN _result = MLPrivileges.CheckPrivilege(privilege); if (_result.Result != MLResult.Code.PrivilegeGranted && _result.Result != MLResult.Code.PrivilegeNotGranted) { Debug.LogErrorFormat("Error: MLPrivilegesStarterKit.CheckPrivilege failed for the privilege {0}. Reason: {1}", privilege, _result); } #endif return(_result); }
/// <summary> /// Handles the result that is received from the query to the Privilege API. /// If one of the required privileges are denied, set the Privilege state to Denied. /// <param name="result">The resulting status of the query</param> /// <param name="privilegeId">The privilege being queried</param> /// </summary> private void HandlePrivilegeAsyncRequest(MLResult result, MLPrivileges.Id privilegeId) { #if PLATFORM_LUMIN if (result.Result == MLResult.Code.PrivilegeGranted) { _privilegesGranted.Add(privilegeId); Debug.LogFormat("{0} Privilege Granted", privilegeId); } else { Debug.LogErrorFormat("{0} Privilege Error: {1}.", privilegeId, result); _state = PrivilegeState.Failed; } #endif }
/// <summary> /// Request the specified privileges. This may solicit consent from the end-user. /// Note: The asynchronous callback occurs within the main thread. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <param name="callback">Callback to be executed when the privilege request has completed.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.Ok</c> if the privilege request is in progress. /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if the callback is null. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// Callback MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// Callback MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// </returns> public static MLResult RequestPrivilegeAsync(MLPrivileges.Id privilegeId, CallbackDelegate callback) { try { if (MLPrivileges.IsValidInstance()) { if (callback == null) { return(MLResult.Create(MLResult.Code.InvalidParam, "MLPrivileges.RequestPrivilegeAsync failed. Reason: Must send a valid callback.")); } if (!_instance.currentRequests.ContainsKey(privilegeId)) { IntPtr newRequest = IntPtr.Zero; MLResult.Code resultCode = NativeBindings.MLPrivilegesRequestPrivilegeAsync(privilegeId, ref newRequest); if (resultCode == MLResult.Code.Ok) { RequestPrivilegeQuery newQuery = new RequestPrivilegeQuery(callback, newRequest, privilegeId); _instance.currentRequests.Add(privilegeId, newQuery); } return(MLResult.Create(resultCode)); } else { return(MLResult.Create(MLResult.Code.Ok)); } } else { MLPluginLog.ErrorFormat("MLPrivileges.RequestPrivilegeAsync failed. Reason: No Instance for MLPrivileges."); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.RequestPrivilegeAsync failed. Reason: No Instance for MLPrivileges.")); } } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLPrivileges.RequestPrivilegeAsync failed. Reason: API symbols not found"); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.RequestPrivilegeAsync failed. Reason: API symbols not found.")); } }
/// <summary> /// Checks whether the application has the specified privileges. /// This does not solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to check for access.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> public static MLResult CheckPrivilege(MLPrivileges.Id privilegeId) { try { if (MLPrivileges.IsValidInstance()) { MLResult.Code checkPrivilegeResult = NativeBindings.MLPrivilegesCheckPrivilege(privilegeId); return((checkPrivilegeResult == MLResult.Code.PrivilegeNotGranted) ? MLResult.Create(checkPrivilegeResult, "Privilege Denied or Not Yet Requested.") : MLResult.Create(checkPrivilegeResult)); } else { MLPluginLog.ErrorFormat("MLPrivileges.CheckPrivilege failed. Reason: No Instance for MLPrivileges."); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.CheckPrivilege failed. Reason: No Instance for MLPrivileges.")); } } catch (System.EntryPointNotFoundException) { MLPluginLog.Error("MLPrivileges.CheckPrivilege failed. Reason: API symbols not found"); return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges.CheckPrivilege failed. Reason: API symbols not found.")); } }
/// <summary> /// Used to check if your privilege has already been granted. /// </summary> /// <param name="privilege">The privilege to check for.</param> public static MLResult CheckPrivilege(MLPrivileges.Id privilege) { #if PLATFORM_LUMIN if (MLPrivileges.IsStarted) { _result = MLPrivileges.CheckPrivilege(privilege); if (_result.Result != MLResult.Code.PrivilegeGranted && _result.Result != MLResult.Code.PrivilegeNotGranted) { Debug.LogErrorFormat("Error: MLPrivilegesStarterKit.CheckPrivilege failed for the privilege {0}. Reason: {1}", privilege, _result); } } else { Debug.LogError("Error: MLPrivilegesStarterKit.CheckPrivilege failed because MLPrivileges was not started."); _result = MLResult.Create(MLResult.Code.UnspecifiedFailure, "MLPrivileges was not started"); } #endif return(_result); }
/// <summary> /// Request the specified privilege asynchronously. This may solicit consent from the end-user. /// This async override uses Tasks instead of a callback. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> private async Task <MLResult> RequestPrivilegeAsyncInternal(MLPrivileges.Id privilegeId) { var taskCompletionSource = new TaskCompletionSource <MLResult>(); if (this.currentRequests.ContainsKey(privilegeId)) { return(MLResult.Create(MLResult.Code.Ok)); } IntPtr newRequest = IntPtr.Zero; MLResult.Code resultCode = NativeBindings.MLPrivilegesRequestPrivilegeAsync(privilegeId, ref newRequest); if (!MLResult.IsOK(resultCode)) { return(MLResult.Create(resultCode)); } RequestPrivilegeQuery newQuery = new RequestPrivilegeQuery((result, id) => taskCompletionSource.SetResult(result), newRequest, privilegeId); this.currentRequests.Add(privilegeId, newQuery); return(await taskCompletionSource.Task); }
public static extern MLResult.Code MLPrivilegesRequestPrivilege(MLPrivileges.Id privilegeId);
/// <summary> /// Requests the specified privileges. This may possibly solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> private MLResult.Code RequestPrivilegeInternal(MLPrivileges.Id privilegeId) { return(NativeBindings.MLPrivilegesRequestPrivilege(privilegeId)); }
/// <summary> /// Checks whether the application has the specified privileges. /// This does not solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to check for access.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> private MLResult.Code CheckPrivilegeInternal(MLPrivileges.Id privilegeId) { return(NativeBindings.MLPrivilegesCheckPrivilege(privilegeId)); }
/// <summary> /// Request the specified privilege asynchronously. This may solicit consent from the end-user. /// This async override uses Tasks instead of a callback. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> public static async Task <MLResult> RequestPrivilegeAsync(MLPrivileges.Id privilegeId) { return(await Instance.RequestPrivilegeAsyncInternal(privilegeId)); }
/// <summary> /// Requests the specified privilege. This may possibly solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to request.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> public static MLResult RequestPrivilege(MLPrivileges.Id privilegeId) { return(MLResult.Create(Instance.RequestPrivilegeInternal(privilegeId))); }
/// <summary> /// Checks whether the application has the specified privileges. /// This does not solicit consent from the end-user. /// </summary> /// <param name="privilegeId">The privilege to check for access.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.PrivilegeGranted</c> if the privilege is granted. /// MLResult.Result will be <c>MLResult.Code.PrivilegeNotGranted</c> if the privilege is denied. /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if the privilege system was not started. /// </returns> public static MLResult CheckPrivilege(MLPrivileges.Id privilegeId) { return(MLResult.Create(Instance.CheckPrivilegeInternal(privilegeId))); }
public static extern MLResult.Code MLPrivilegesCheckPrivilege(MLPrivileges.Id privilegeId);
public static extern MLResult.Code MLPrivilegesRequestPrivilegeAsync(MLPrivileges.Id privilegeId, ref IntPtr request);
public static MLResult RequestPrivilegeAsync(MLPrivileges.Id privilegeId, CallbackDelegate callback) { return(Instance.RequestPrivilegeAsyncInternal(privilegeId, callback)); }