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) of request permission at runtime. APIs are listed below to let developers check the status and grant permission when needed.

  • bool isInitialized()

Report true/false of native permission service.

  • bool isPermissionGranted(string permission)

Report true/false to indicate if this permission has already been granted.

  • bool shouldGrantPermission(string permission)

Report true to notify 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()

Report true if dialog box will show on the scene.

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

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

Developers, you can start to update your controllers from this point.

  • void requestCompleteCallback(List<RequestResult> results)

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

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

Resources

Sample scene PermissionMgr_Test is located in Assets/Samples/PermissionMgr_Test/Scenes

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

How to Use

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

Sample Code

See in the 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++;
            }
        }
    }
}