holokit / holokit-1-sdk

HoloKit Unity SDK for HoloKit 1 (Cardboard version). This repo is deprecated. Please refer to https://github.com/holokit/holokit-unity-sdk for latest SDK for HoloKit X.
https://holokit.io
Apache License 2.0
153 stars 58 forks source link

HoloKitScreenBrightnessManager throwing an exception on Android #10

Open Raptor2000 opened 6 years ago

Raptor2000 commented 6 years ago

There's an issue when getting and setting the screen brightness on Android:

First: android.provider.Settings.System should be android.provider.Settings$System (System is a nested class of Settings) and system.CallStatic("putInt", contentResolver, system.CallStatic<string>("SCREEN_BRIGHTNESS_MODE"), 0); should be system.CallStatic<bool>("putInt", contentResolver, system.GetStatic<string>("SCREEN_BRIGHTNESS_MODE"), 0);

Second: You cannot get the write settings permission by only adding it to the manifest. Here's a quick workaround :

// We are using ARCore so Build.VERSION.SDK_INT will always be greater than VERSION_CODES.M
private bool HasWriteSettingsPermission()
{
    var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    var system = new AndroidJavaClass("android.provider.Settings$System");
    if (!system.CallStatic<bool>("canWrite", activity))
    {
        var settings = new AndroidJavaClass("android.provider.Settings");
        var uri = new AndroidJavaClass("android.net.Uri");

         var intent = new AndroidJavaObject("android.content.Intent", settings.GetStatic<string>("ACTION_MANAGE_WRITE_SETTINGS"));
        intent.Call<AndroidJavaObject>("setData", uri.CallStatic<AndroidJavaObject>("parse", "package:" + activity.Call<string>("getPackageName")));

        activity.Call("startActivityForResult", intent, RESULT_CODE);

        return false;
    }
    return true;
}

It will open the settings dialog of the app and it's up to you how you want to handle onActivityResult. I might be wrong but at one point you'll need to have a custom activity in native android code and handle the result from there.

PS: The AndroidManifest.xml under HoloKitSDK/Plugins/HoloKitScreenBrightnessManager/Android doesn't seem to be merged. I had to copy the default Unity Android manifest and add the WRITE_SETTINGS permission to Plugins/Android/AndroidManifest.xml