diff --git a/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java b/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java index 7749f35eded..36f2a4fe516 100644 --- a/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java +++ b/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java @@ -1,5 +1,5 @@ /** - * TiDev Titanium Mobile + * Titanium SDK * Copyright TiDev, Inc. 04/07/2022-Present. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. @@ -93,7 +93,8 @@ public class MediaModule extends KrollModule implements Handler.Callback public static final int NO_CAMERA = 2; @Kroll.constant public static final int NO_VIDEO = 3; - + @Kroll.constant + public static final int NO_FOCUS = 4; @Kroll.constant public static final int IMAGE_SCALING_AUTO = -1; @Kroll.constant diff --git a/android/modules/media/src/java/ti/modules/titanium/media/TiCameraActivity.java b/android/modules/media/src/java/ti/modules/titanium/media/TiCameraActivity.java index ec7709b3465..8089c00d04c 100644 --- a/android/modules/media/src/java/ti/modules/titanium/media/TiCameraActivity.java +++ b/android/modules/media/src/java/ti/modules/titanium/media/TiCameraActivity.java @@ -1,5 +1,5 @@ /** - * TiDev Titanium Mobile + * Titanium SDK * Copyright TiDev, Inc. 04/07/2022-Present. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. @@ -20,6 +20,7 @@ import org.appcelerator.titanium.TiC; import org.appcelerator.titanium.io.TiContentFile; import org.appcelerator.titanium.proxy.TiViewProxy; +import org.appcelerator.titanium.view.TiUIView; import android.Manifest; import android.app.Activity; @@ -246,14 +247,23 @@ protected void onResume() cameraActivity = this; previewLayout.addView(preview, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - View overlayView = localOverlayProxy.getOrCreateView().getNativeView(); - ViewGroup parent = (ViewGroup) overlayView.getParent(); - // Detach from the parent if applicable - if (parent != null) { - parent.removeView(overlayView); - } - cameraLayout.addView(overlayView, - new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + if (localOverlayProxy != null) { + TiUIView localView = localOverlayProxy.getOrCreateView(); + if (localView != null) { + View overlayView = localView.getNativeView(); + ViewGroup parent = (ViewGroup) overlayView.getParent(); + // Detach from the parent if applicable + if (parent != null) { + parent.removeView(overlayView); + } + cameraLayout.addView(overlayView, + new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + } else { + Log.e(TAG, "Overlay view is null"); + } + } else { + Log.e(TAG, "Overlay is null"); + } } public static void setFlashMode(int cameraFlashMode) @@ -729,6 +739,18 @@ public void onAutoFocus(boolean success, Camera camera) } } else { Log.w(TAG, "Unable to focus."); + if (errorCallback != null) { + KrollDict response = new KrollDict(); + response.putCodeAndMessage(MediaModule.NO_FOCUS, "Couldn't focus"); + errorCallback.callAsync(callbackContext, response); + } + try { + camera.cancelAutoFocus(); + camera.autoFocus(null); + } catch (Exception e) { + Log.w(TAG, "Failed to cancel auto focus: " + e.toString()); + } + takingPicture = false; } } } diff --git a/apidoc/Titanium/Media/Media.yml b/apidoc/Titanium/Media/Media.yml index 188ded1d2c3..8f093d29dac 100644 --- a/apidoc/Titanium/Media/Media.yml +++ b/apidoc/Titanium/Media/Media.yml @@ -1228,6 +1228,13 @@ properties: type: Number permission: read-only + - name: NO_FOCUS + summary: Constant for camera didn't focus when taking a trying to take a picture. + type: Number + permission: read-only + since: + android: "12.2.1" + - name: VIDEO_CONTROL_DEFAULT deprecated: since: "7.0.0" @@ -2370,7 +2377,7 @@ properties: - name: code summary: Error code, if applicable. type: Number - constants: [Titanium.Media.DEVICE_BUSY, Titanium.Media.NO_CAMERA, Titanium.Media.UNKNOWN_ERROR] + constants: [Titanium.Media.DEVICE_BUSY, Titanium.Media.NO_CAMERA, Titanium.Media.UNKNOWN_ERROR, Titanium.Media.NO_FOCUS] accessors: false - name: success