Skip to content

Commit da868c2

Browse files
committed
fix: typo issue on java
1 parent 24448c5 commit da868c2

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

android/src/main/java/ee/forgr/plugin/capacitor_flash/CapacitorFlashPlugin.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,21 @@ private void getAvailibility(PluginCall call) {
6868
@RequiresApi(api = Build.VERSION_CODES.M)
6969
@PluginMethod
7070
public void switchOn(PluginCall call) {
71-
String value = call.getString("instensity"); // cannot be use in android
71+
Float intensity = call.getFloat("intensity", 1.0f);
7272
JSObject ret = new JSObject();
7373
if (cameraManager == null) {
7474
ret.put("value", false);
7575
call.resolve(ret);
7676
return;
7777
}
7878
try {
79-
cameraManager.setTorchMode(cameraId, true);
79+
// Android 13+ (API 33) supports torch brightness control
80+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
81+
switchOnWithIntensity(intensity);
82+
} else {
83+
// For older Android versions, only on/off is supported
84+
cameraManager.setTorchMode(cameraId, true);
85+
}
8086
isFlashStateOn = true;
8187
ret.put("value", true);
8288
} catch (Exception e) {
@@ -86,6 +92,33 @@ public void switchOn(PluginCall call) {
8692
call.resolve(ret);
8793
}
8894

95+
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
96+
private void switchOnWithIntensity(Float intensity) throws CameraAccessException {
97+
// Clamp intensity between 0.0 and 1.0
98+
float clampedIntensity = Math.max(0.0f, Math.min(1.0f, intensity));
99+
100+
// Get max torch strength level using reflection for API 33+ compatibility
101+
try {
102+
// CameraCharacteristics.FLASH_INFO_STRENGTH_MAX_LEVEL is an API 33+ constant
103+
java.lang.reflect.Field field = CameraCharacteristics.class.getField("FLASH_INFO_STRENGTH_MAX_LEVEL");
104+
CameraCharacteristics.Key<Integer> key = (CameraCharacteristics.Key<Integer>) field.get(null);
105+
Integer maxLevel = cameraManager.getCameraCharacteristics(cameraId).get(key);
106+
107+
if (maxLevel != null && maxLevel > 1) {
108+
// Convert 0.0-1.0 range to device's torch level range (1 to maxLevel)
109+
// Level 1 is minimum brightness, maxLevel is maximum
110+
int torchLevel = Math.max(1, Math.round(clampedIntensity * maxLevel));
111+
cameraManager.turnOnTorchWithStrengthLevel(cameraId, torchLevel);
112+
} else {
113+
// Fallback if max level is not available
114+
cameraManager.setTorchMode(cameraId, true);
115+
}
116+
} catch (Exception e) {
117+
// If reflection fails, fall back to simple on mode
118+
cameraManager.setTorchMode(cameraId, true);
119+
}
120+
}
121+
89122
@RequiresApi(api = Build.VERSION_CODES.M)
90123
@PluginMethod
91124
public void switchOff(PluginCall call) {

0 commit comments

Comments
 (0)