Skip to content

Commit 5fd5b1e

Browse files
mcgintymoxie0
authored andcommitted
Fix "twitchy fingers" bug in CameraView
better diagnostic information, too. Fixes signalapp#4422 Closes signalapp#4427 // FREEBIE
1 parent 56a3c99 commit 5fd5b1e

2 files changed

Lines changed: 35 additions & 38 deletions

File tree

src/org/thoughtcrime/securesms/components/camera/CameraUtils.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.thoughtcrime.securesms.components.camera;
22

3-
import android.annotation.TargetApi;
43
import android.hardware.Camera;
5-
import android.hardware.Camera.Parameters;
64
import android.hardware.Camera.Size;
7-
import android.os.Build.VERSION;
85
import android.support.annotation.NonNull;
96
import android.support.annotation.Nullable;
107

@@ -14,24 +11,13 @@
1411

1512
@SuppressWarnings("deprecation")
1613
public class CameraUtils {
17-
@TargetApi(11)
18-
public static @Nullable Size getPreferredPreviewSize(int orientation, int width, int height, @NonNull Camera camera) {
19-
final Parameters parameters = camera.getParameters();
20-
final Size preferredSize = VERSION.SDK_INT > 11
21-
? parameters.getPreferredPreviewSizeForVideo()
22-
: null;
23-
24-
return preferredSize == null ? getBestAspectPreviewSize(orientation, width, height, parameters)
25-
: preferredSize;
26-
}
27-
2814
/*
2915
* modified from: https://github.com/commonsguy/cwac-camera/blob/master/camera/src/com/commonsware/cwac/camera/CameraUtils.java
3016
*/
31-
public static @Nullable Size getBestAspectPreviewSize(int displayOrientation,
32-
int width,
33-
int height,
34-
Parameters parameters) {
17+
public static @Nullable Size getPreferredPreviewSize(int displayOrientation,
18+
int width,
19+
int height,
20+
@NonNull Camera camera) {
3521
double targetRatio = (double)width / height;
3622
Size optimalSize = null;
3723
double minDiff = Double.MAX_VALUE;
@@ -40,7 +26,7 @@ public class CameraUtils {
4026
targetRatio = (double)height / width;
4127
}
4228

43-
List<Size> sizes = parameters.getSupportedPreviewSizes();
29+
List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
4430

4531
Collections.sort(sizes, Collections.reverseOrder(new SizeComparator()));
4632

src/org/thoughtcrime/securesms/components/camera/CameraView.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,17 @@ public void onResume() {
9090
@Override
9191
protected @Nullable Camera onRunBackground() {
9292
try {
93-
if (cameraId >= 0) {
94-
return Camera.open(cameraId);
95-
} else {
96-
return null;
97-
}
93+
return Camera.open(cameraId);
9894
} catch (Exception e) {
95+
Log.w(TAG, e);
9996
return null;
10097
}
10198
}
10299

103100
@Override
104101
protected void onPostMain(@Nullable Camera camera) {
105102
if (camera == null) {
103+
Log.w(TAG, "tried to open camera but got null");
106104
if (listener != null) listener.onCameraFail();
107105
return;
108106
}
@@ -132,17 +130,23 @@ public void onPause() {
132130
if (!started) return;
133131
started = false;
134132
Log.w(TAG, "onPause() queued");
135-
final Optional<Camera> cameraToDestroy = camera;
136133

137134
enqueueTask(new SerialAsyncTask<Void>() {
135+
private Optional<Camera> cameraToDestroy;
138136
@Override protected void onPreMain() {
137+
cameraToDestroy = camera;
139138
camera = Optional.absent();
140139
}
141140

142141
@Override protected Void onRunBackground() {
143142
if (cameraToDestroy.isPresent()) {
144-
stopPreview();
145-
cameraToDestroy.get().release();
143+
try {
144+
stopPreview();
145+
cameraToDestroy.get().release();
146+
Log.w(TAG, "released old camera instance");
147+
} catch (Exception e) {
148+
Log.w(TAG, e);
149+
}
146150
}
147151
return null;
148152
}
@@ -171,6 +175,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
171175
camera.get());
172176
final Parameters parameters = camera.get().getParameters();
173177
if (preferredPreviewSize != null && !parameters.getPreviewSize().equals(preferredPreviewSize)) {
178+
Log.w(TAG, "setting preview size to " + preferredPreviewSize.width + "x" + preferredPreviewSize.height);
174179
stopPreview();
175180
parameters.setPreviewSize(preferredPreviewSize.width, preferredPreviewSize.height);
176181
camera.get().setParameters(parameters);
@@ -239,7 +244,6 @@ public void flipCamera() {
239244
}
240245
}
241246

242-
243247
@TargetApi(14)
244248
private void onCameraReady() {
245249
if (!camera.isPresent()) return;
@@ -262,8 +266,8 @@ private void onCameraReady() {
262266
if (camera.isPresent()) {
263267
try {
264268
camera.get().setPreviewDisplay(surface.getHolder());
265-
startPreview();
266-
} catch (IOException e) {
269+
requestLayout();
270+
} catch (Exception e) {
267271
Log.w(TAG, e);
268272
}
269273
}
@@ -273,13 +277,21 @@ private void onCameraReady() {
273277

274278
private void startPreview() {
275279
if (camera.isPresent()) {
276-
camera.get().startPreview();
280+
try {
281+
camera.get().startPreview();
282+
} catch (Exception e) {
283+
Log.w(TAG, e);
284+
}
277285
}
278286
}
279287

280288
private void stopPreview() {
281289
if (camera.isPresent()) {
282-
camera.get().stopPreview();
290+
try {
291+
camera.get().stopPreview();
292+
} catch (Exception e) {
293+
Log.w(TAG, e);
294+
}
283295
}
284296
}
285297

@@ -304,8 +316,7 @@ private void setCameraDisplayOrientation() {
304316
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
305317
displayOrientation = (info.orientation + degrees ) % 360;
306318
displayOrientation = (360 - displayOrientation) % 360;
307-
}
308-
else {
319+
} else {
309320
displayOrientation = (info.orientation - degrees + 360) % 360;
310321
}
311322

@@ -401,7 +412,7 @@ public void onPreviewFrame(byte[] data, final Camera camera) {
401412
Log.w(TAG, "previewFormat: " + camera.getParameters().getPreviewFormat());
402413
Log.w(TAG, "croppingRect: " + croppingRect.toString());
403414
Log.w(TAG, "rotation: " + rotation);
404-
new RotatePreviewAsyncTask(previewSize, rotation, croppingRect).execute(data);
415+
new CaptureTask(previewSize, rotation, croppingRect).execute(data);
405416
}
406417
});
407418
}
@@ -430,7 +441,6 @@ private Rect getCroppedRect(Size cameraPreviewSize, Rect visibleRect, int rotati
430441
return visibleRect;
431442
}
432443

433-
434444
@SuppressWarnings("SuspiciousNameCombination")
435445
private void rotateRect(Rect rect) {
436446
rect.set(rect.top, rect.left, rect.bottom, rect.right);
@@ -495,12 +505,12 @@ private abstract class PostInitializationTask<Result> extends SerialAsyncTask<Re
495505
}
496506
}
497507

498-
private class RotatePreviewAsyncTask extends AsyncTask<byte[], Void, byte[]> {
508+
private class CaptureTask extends AsyncTask<byte[], Void, byte[]> {
499509
private final Size previewSize;
500510
private final int rotation;
501511
private final Rect croppingRect;
502512

503-
public RotatePreviewAsyncTask(Size previewSize, int rotation, Rect croppingRect) {
513+
public CaptureTask(Size previewSize, int rotation, Rect croppingRect) {
504514
this.previewSize = previewSize;
505515
this.rotation = rotation;
506516
this.croppingRect = croppingRect;
@@ -516,6 +526,7 @@ protected byte[] doInBackground(byte[]... params) {
516526
rotation,
517527
croppingRect);
518528
} catch (IOException e) {
529+
Log.w(TAG, e);
519530
return null;
520531
}
521532
}

0 commit comments

Comments
 (0)