WaveVR_PermissionManager

Contents

Introduction

WaveVR_PermissionManager.cs provides an Android request permission in a VR scene. It is based on Android 6.0 (API 23) request permission at runtime. APIs are listed below to let you check the status and grant permission when needed.

  • bool isInitialized()

This is to report true/false of native permission service.

  • bool isPermissionGranted(string permission)

This is to report true/false to indicate if this permission has already been granted.

  • bool shouldGrantPermission(string permission)

This is to report true to notify a user if the corresponding permission request is going to trigger and show a permission dialog box.

Please check these before requestPermissions, and stop your controller updating if returns true.

  • bool showDialogOnScene()

This is to report true if a dialog box will show on the scene.

  • void requestPermissions(string[] permissions, requestCompleteCallback cb)

The major function of the WaveVR_PermissionManager class, if shouldGrantPermission returns true, is that you must call this major function to request permission. This API will check permission status and show a dialog box on the scene when permission is not granted. Before you call this API, it is necessary to implement the requestCompleteCallback delegate and pass the instance to WaveVR_PermissionManager. requestCompleteCallback will be called as soon as a user answers the permission dialog box.

You can start to update your controllers from this point.

  • void requestCompleteCallback(List<RequestResult> results)

The API above shows the callback prototype. You must implement the callback function to receive grant result data from the permission manager.

PermissionManager will send a callback with all fails to the registered app when requestPermission is processing and it will pause the application (the native service will also be stopped.) It is recommended that you check isInitialized/isPermissionGranted/shouldGrantPermission after onResume() to see if you need requestPermissions again.

  • void requestUsbPermission(requestUsbCompleteCallback cb)

This is the major function of requesting permission to access a USB device. This function will detect if there is any USB device connected. If a USB device is detected and the access permission is not granted, a request dialog will pop up to ask for the user’s allowance or denial. Before calling the function, it is necessary to implement the interface of requestUsbCompleteCallback and pass its instance to WaveVR_PermissionManager. The callback function will be called after the user grants or denies the requested USB device access permission. Currently only one USB device is supported. If there is more than one device connected, only the first device’s callback will be called.

  • void requestUsbCompleteCallback(bool result)

The API above shows the callback prototype. You must implement the callback function to receive the grant result for USB permission.

Resources for Getting Permissions

The sample scene PermissionMgr_Test is located in Assets/Samples/PermissionMgr_Test/Scenes

The sample script Permission_Test.cs is located in Assets/Samples/PermissionMgr_Test/Scripts

How to Get Permissions

  1. Add permissions in AndroidManifest.xml For example, <uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />
  2. Get WaveVR_PermissionManager instance.
  3. Prepare a string array of permissions (must be declared in AndroidManifest.xml)
  4. Implement the callback function to get notification from WaveVR_PermissionManager.
  5. Call Instance.requestPermissions with callback function.
  6. Use Gaze or controller trigger to choose Deny or Allow.
  7. Get the grant result from WaveVR_PermissionManager.

Sample Code for Getting Permissions

See Permission_Test.cs

private static string LOG_TAG = "Permission_Test";

private WaveVR_PermissionManager pmInstance = null;
private static bool isDeny = false;
private static int retryCount = 0;
private static int RETRY_LIMIT = 1;
private static bool requested = false;
private static int systemCheckFailCount = 0;

// Implement callback with retry mechanism
public static void requestDoneCallback(List<WaveVR_PermissionManager.RequestResult> results)
{
    Log.d(LOG_TAG, "requestDoneCallback, count = " + results.Count);
    isDeny = false;

    foreach (WaveVR_PermissionManager.RequestResult p in results)
    {
        Log.d(LOG_TAG, "requestDoneCallback " + p.PermissionName + ": " + (p.Granted ? "Granted" : "Denied"));
        if (!p.Granted)
        {
            isDeny = true;
        }
    }

    if (isDeny)
    {
        if (retryCount++ < RETRY_LIMIT)
        {
            Log.d(LOG_TAG, "Permission denied, retry count = " + retryCount);
            requested = false;
        } else
        {
            Log.w(LOG_TAG, "Permission denied, exceed RETRY_LIMIT and skip request");
        }
    }
}

// get instance on start
void Start () {
    Log.d(LOG_TAG, "get instance at start");
    pmInstance = WaveVR_PermissionManager.instance;
}

// Use APIs of WaveVR_PermissionManager
void Update () {
    if (!requested)
    {
        if (systemCheckFailCount <= 10)
        {
            if (pmInstance.isInitialized())
            {
                Log.d(LOG_TAG, "inited");
                Log.d(LOG_TAG, "showDialogOnScene() = " + pmInstance.showDialogOnScene());

                // We will request CAMERA and STORAGE related permissions in sample, please declare those permissions in AndroidManifest.xml
                string[] tmpStr =
                {
                    "android.permission.CAMERA", "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"
                };

                Log.d(LOG_TAG, "isPermissionGranted(android.permission.CAMERA) = " + pmInstance.isPermissionGranted("android.permission.CAMERA"));
                Log.d(LOG_TAG, "isPermissionGranted(android.permission.WRITE_EXTERNAL_STORAGE) = " + pmInstance.isPermissionGranted("android.permission.WRITE_EXTERNAL_STORAGE"));
                Log.d(LOG_TAG, "shouldGrantPermission(android.permission.READ_EXTERNAL_STORAGE) = " + pmInstance.shouldGrantPermission("android.permission.READ_EXTERNAL_STORAGE"));

                // Major function to request permission
                pmInstance.requestPermissions(tmpStr, requestDoneCallback);
                requested = true;
            }
            else
            {
                systemCheckFailCount++;
            }
        }
    }
}

Resources for Getting USB Permission

The sample scene HelloVR is located in Assets/Samples/HelloVR/Scenes

The sample script Go_Event.cs is located in Assets/Samples/HelloVR/Scripts

How to Get the Permissions of Using the USB Port

  1. Add the necessary declaration in AndroidManifest.xml, e.g., <uses-feature android:name=”android.hardware.usb.host” />
  2. Get WaveVR_PermissionManager instance.
  3. Implement the callback function to get the request result from WaveVR_PermissionManager.
  4. Call Instance.requestUsbPermission().
  5. The user can allow or deny the request by using a controller or by gazing.
  6. Get the request result from WaveVR_PermissionManager.

Sample Code for Getting USB Permission

See Go_Event.cs

private WaveVR_PermissionManager pmInstance = null;

void Start()
{
        pmInstance = WaveVR_PermissionManager.instance;
        if (pmInstance.isInitialized())
        {
                pmInstance.requestUsbPermission(requestUsbDoneCallback);
        }
}

public static void requestUsbDoneCallback(bool result)
{
        #if !UINTY_EDITOR
        Log.d(LOG_TAG, "requestUsbDoneCallback, result= " + result);
        #endif
}