Skip to content

Commit

Permalink
feat(Lamp): Support setting Brightness on Android 13 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 3, 2023
1 parent 2af7e1a commit dd04f75
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Uno.UWP/Devices/Lights/Lamp.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public float BrightnessLevel
get => _brightness;
set
{
_brightness = value > 0 ? 1 : 0;
_brightness = value;
UpdateLampState();
}
}
Expand Down Expand Up @@ -113,15 +113,37 @@ private void UpdateLampState()
var isOn = _isEnabled && _brightness > 0;
lock (_lock)
{
if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.M)
if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.Tiramisu)
{
_cameraManager.SetTorchMode(_defaultCameraId, isOn);
if (!isOn)
{
return;
}

var characteristics = _cameraManager.GetCameraCharacteristics(_defaultCameraId);
const int minLevel = 1;
var maxLevel = (Java.Lang.Integer)characteristics.Get(CameraCharacteristics.FlashInfoStrengthMaximumLevel);
if (maxLevel is null)
{
// https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#FLASH_INFO_STRENGTH_MAXIMUM_LEVEL
// The value for this key will be null for devices with no flash unit.
return;
}

// Android ranges from 1 (minLevel) to maxLevel
// _brightness ranges from 0 to 1
var nativeLevel = minLevel + _brightness * ((int)maxLevel - minLevel);
_cameraManager.TurnOnTorchWithStrengthLevel(_defaultCameraId, (int)Math.Round(nativeLevel));
}
else if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.M)
{
_cameraManager.SetTorchMode(_defaultCameraId, isOn);
}
else
{
#pragma warning disable CS0618
#pragma warning disable CA1422 // Validate platform compatibility
// using deprecated API for older Android versions
#pragma warning disable CS0618 // Type or member is obsolete
var param = _camera.GetParameters();
param.FlashMode = _isEnabled ?
Android.Hardware.Camera.Parameters.FlashModeTorch :
Expand All @@ -136,8 +158,7 @@ private void UpdateLampState()
{
_camera.StopPreview();
}
#pragma warning restore CS0618
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
Expand Down

0 comments on commit dd04f75

Please sign in to comment.