Skip to content

Commit df2b1b6

Browse files
committed
refactor: extract getParams/getZoomParams helpers in Camera1Provider
Eliminate repeated CameraInstance.getInstance().getParams() calls (5 sites) and the duplicated zoom-support guard (params == null || !isZoomSupported) by introducing two private helpers: - getParams(): shorthand wrapper, no behavioral change - getZoomParams(): returns params only when camera is open and zoom is supported; used by isZoomSupported, getMaxZoomRatio, setZoomRatio Pure structural cleanup, zero logic change.
1 parent 3a0c4a3 commit df2b1b6

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

library/src/main/java/org/wysaid/camera/Camera1Provider.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void focusAtPoint(float x, float y, float radius, AutoFocusCallback callb
105105

106106
@Override
107107
public boolean setFlashMode(FlashMode mode) {
108-
Camera.Parameters params = CameraInstance.getInstance().getParams();
108+
Camera.Parameters params = getParams();
109109
if (params == null) return false;
110110

111111
String camera1Mode = ICameraProvider.flashModeToCamera1(mode);
@@ -126,7 +126,7 @@ public boolean setFlashMode(FlashMode mode) {
126126

127127
@Override
128128
public FlashMode getFlashMode() {
129-
Camera.Parameters params = CameraInstance.getInstance().getParams();
129+
Camera.Parameters params = getParams();
130130
if (params == null) return null;
131131
return ICameraProvider.camera1ToFlashMode(params.getFlashMode());
132132
}
@@ -141,12 +141,30 @@ public void setFocusMode(String focusMode) {
141141
CameraInstance.getInstance().setFocusMode(focusMode);
142142
}
143143

144+
// ========== Private helpers ==========
145+
146+
/**
147+
* Shorthand for {@link CameraInstance#getParams()}.
148+
* Returns {@code null} when the camera is closed.
149+
*/
150+
private Camera.Parameters getParams() {
151+
return CameraInstance.getInstance().getParams();
152+
}
153+
154+
/**
155+
* Returns camera parameters only when the camera is open <em>and</em> zoom is supported;
156+
* returns {@code null} otherwise. Use this as the single guard in all zoom methods.
157+
*/
158+
private Camera.Parameters getZoomParams() {
159+
Camera.Parameters params = getParams();
160+
return (params != null && params.isZoomSupported()) ? params : null;
161+
}
162+
144163
// ========== Zoom ==========
145164

146165
@Override
147166
public boolean isZoomSupported() {
148-
Camera.Parameters params = CameraInstance.getInstance().getParams();
149-
return params != null && params.isZoomSupported();
167+
return getZoomParams() != null;
150168
}
151169

152170
@Override
@@ -156,8 +174,8 @@ public float getMinZoomRatio() {
156174

157175
@Override
158176
public float getMaxZoomRatio() {
159-
Camera.Parameters params = CameraInstance.getInstance().getParams();
160-
if (params == null || !params.isZoomSupported()) return 1.0f;
177+
Camera.Parameters params = getZoomParams();
178+
if (params == null) return 1.0f;
161179
List<Integer> ratios = params.getZoomRatios();
162180
if (ratios == null || ratios.isEmpty()) return 1.0f;
163181
int maxIndex = Math.min(params.getMaxZoom(), ratios.size() - 1);
@@ -166,8 +184,8 @@ public float getMaxZoomRatio() {
166184

167185
@Override
168186
public void setZoomRatio(float ratio) {
169-
Camera.Parameters params = CameraInstance.getInstance().getParams();
170-
if (params == null || !params.isZoomSupported()) return;
187+
Camera.Parameters params = getZoomParams();
188+
if (params == null) return;
171189

172190
if (!Float.isFinite(ratio) || ratio <= 0f) {
173191
Log.e(LOG_TAG, "Camera1: invalid zoom ratio: " + ratio);
@@ -248,7 +266,7 @@ public void takePicture(ShutterCallback shutterCallback, PictureDataCallback pic
248266
int jpegRotation = computeJpegRotation(camera1Facing);
249267

250268
// Set rotation
251-
Camera.Parameters params = CameraInstance.getInstance().getParams();
269+
Camera.Parameters params = getParams();
252270
if (params != null) {
253271
try {
254272
params.setRotation(jpegRotation);

0 commit comments

Comments
 (0)