Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd064ced authored by Mark Harman's avatar Mark Harman
Browse files

Set faces_detected to null on ui thread if camera is closed; move...

Set faces_detected to null on ui thread if camera is closed; move accessibility code to new reportFaces().
parent 18868416
Loading
Loading
Loading
Loading
+26 −16
Original line number Diff line number Diff line
@@ -1872,8 +1872,13 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
						if( MyDebug.LOG )
							Log.d(TAG, "onFaceDetection: " + faces);
						if( camera_controller == null ) {
							// can get a crash in some cases when switching camera when face detection is on
							// can get a crash in some cases when switching camera when face detection is on (at least for Camera2)
							Activity activity = (Activity)Preview.this.getContext();
							activity.runOnUiThread(new Runnable() {
								public void run() {
									faces_detected = null;
								}
							});
							return;
						}
						// take a local copy first before assigning to faces_detected, as the latter has to be done on the UI thread
@@ -1886,9 +1891,25 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
							face_rect.round(face.rect);
						}

						if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
							// accessibility: report number of faces for talkback etc
						reportFaces(local_faces);

						// We don't synchronize on faces_detected, as the array may be passed to other
						// classes via getFacesDetected(). Although that function could copy instead,
						// that would mean an allocation in every frame in DrawPreview.
						// Easier to just do the assignment on the UI thread.
						Activity activity = (Activity)Preview.this.getContext();
						activity.runOnUiThread(new Runnable() {
							public void run() {
								faces_detected = local_faces;
							}
						});
				    }

					/** Accessibility: report number of faces for talkback etc.
					 *  Note, at least for Camera2 API, reportFaces() isn't called on UI thread.
					 */
				    private void reportFaces(CameraController.Face[] local_faces) {
						if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
							int n_faces = local_faces.length;
							FaceLocation face_location = FaceLocation.FACELOCATION_UNKNOWN;
							if( n_faces > 0 ) {
@@ -2001,17 +2022,6 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
								last_face_location = face_location;
							}
						}

						// We don't synchronize on faces_detected, as the array may be passed to other
						// classes via getFacesDetected(). Although that function could copy instead,
						// that would mean an allocation in every frame in DrawPreview.
						// Easier to just do the assignment on the UI thread.
						Activity activity = (Activity)Preview.this.getContext();
						activity.runOnUiThread(new Runnable() {
							public void run() {
								faces_detected = local_faces;
							}
						});
					}
				}
				camera_controller.setFaceDetectionListener(new MyFaceDetectionListener());