Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.camera; |
| 18 | |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 22 | import android.annotation.TargetApi; |
| 23 | import android.graphics.Matrix; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.RectF; |
| 26 | import android.hardware.Camera.Area; |
| 27 | import android.hardware.Camera.Parameters; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
| 31 | import android.util.Log; |
| 32 | |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 33 | import com.android.camera.util.ApiHelper; |
| 34 | import com.android.camera.util.CameraUtil; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 35 | |
| 36 | /* A class that handles everything about focus in still picture mode. |
| 37 | * This also handles the metering area because it is the same as focus area. |
| 38 | * |
| 39 | * The test cases: |
| 40 | * (1) The camera has continuous autofocus. Move the camera. Take a picture when |
| 41 | * CAF is not in progress. |
| 42 | * (2) The camera has continuous autofocus. Move the camera. Take a picture when |
| 43 | * CAF is in progress. |
| 44 | * (3) The camera has face detection. Point the camera at some faces. Hold the |
| 45 | * shutter. Release to take a picture. |
| 46 | * (4) The camera has face detection. Point the camera at some faces. Single tap |
| 47 | * the shutter to take a picture. |
| 48 | * (5) The camera has autofocus. Single tap the shutter to take a picture. |
| 49 | * (6) The camera has autofocus. Hold the shutter. Release to take a picture. |
| 50 | * (7) The camera has no autofocus. Single tap the shutter and take a picture. |
| 51 | * (8) The camera has autofocus and supports focus area. Touch the screen to |
| 52 | * trigger autofocus. Take a picture. |
| 53 | * (9) The camera has autofocus and supports focus area. Touch the screen to |
| 54 | * trigger autofocus. Wait until it times out. |
| 55 | * (10) The camera has no autofocus and supports metering area. Touch the screen |
| 56 | * to change metering area. |
| 57 | */ |
| 58 | public class FocusOverlayManager { |
| 59 | private static final String TAG = "CAM_FocusManager"; |
| 60 | |
| 61 | private static final int RESET_TOUCH_FOCUS = 0; |
| 62 | private static final int RESET_TOUCH_FOCUS_DELAY = 3000; |
| 63 | |
| 64 | private int mState = STATE_IDLE; |
| 65 | private static final int STATE_IDLE = 0; // Focus is not active. |
| 66 | private static final int STATE_FOCUSING = 1; // Focus is in progress. |
| 67 | // Focus is in progress and the camera should take a picture after focus finishes. |
| 68 | private static final int STATE_FOCUSING_SNAP_ON_FINISH = 2; |
| 69 | private static final int STATE_SUCCESS = 3; // Focus finishes and succeeds. |
| 70 | private static final int STATE_FAIL = 4; // Focus finishes and fails. |
| 71 | |
| 72 | private boolean mInitialized; |
| 73 | private boolean mFocusAreaSupported; |
| 74 | private boolean mMeteringAreaSupported; |
| 75 | private boolean mLockAeAwbNeeded; |
| 76 | private boolean mAeAwbLock; |
| 77 | private Matrix mMatrix; |
| 78 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 79 | private int mPreviewWidth; // The width of the preview frame layout. |
| 80 | private int mPreviewHeight; // The height of the preview frame layout. |
| 81 | private boolean mMirror; // true if the camera is front-facing. |
| 82 | private int mDisplayOrientation; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 83 | private List<Object> mFocusArea; // focus area in driver format |
| 84 | private List<Object> mMeteringArea; // metering area in driver format |
| 85 | private String mFocusMode; |
| 86 | private String[] mDefaultFocusModes; |
| 87 | private String mOverrideFocusMode; |
| 88 | private Parameters mParameters; |
| 89 | private ComboPreferences mPreferences; |
| 90 | private Handler mHandler; |
| 91 | Listener mListener; |
Michael Kolb | e3de722 | 2013-02-18 15:16:44 -0800 | [diff] [blame] | 92 | private boolean mPreviousMoving; |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 93 | private boolean mFocusDefault; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 94 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 95 | private FocusUI mUI; |
| 96 | |
| 97 | public interface FocusUI { |
| 98 | public boolean hasFaces(); |
| 99 | public void clearFocus(); |
| 100 | public void setFocusPosition(int x, int y); |
| 101 | public void onFocusStarted(); |
| 102 | public void onFocusSucceeded(boolean timeOut); |
| 103 | public void onFocusFailed(boolean timeOut); |
| 104 | public void pauseFaceDetection(); |
| 105 | public void resumeFaceDetection(); |
| 106 | } |
| 107 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 108 | public interface Listener { |
| 109 | public void autoFocus(); |
| 110 | public void cancelAutoFocus(); |
| 111 | public boolean capture(); |
| 112 | public void startFaceDetection(); |
| 113 | public void stopFaceDetection(); |
| 114 | public void setFocusParameters(); |
| 115 | } |
| 116 | |
| 117 | private class MainHandler extends Handler { |
| 118 | public MainHandler(Looper looper) { |
| 119 | super(looper); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void handleMessage(Message msg) { |
| 124 | switch (msg.what) { |
| 125 | case RESET_TOUCH_FOCUS: { |
| 126 | cancelAutoFocus(); |
| 127 | mListener.startFaceDetection(); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public FocusOverlayManager(ComboPreferences preferences, String[] defaultFocusModes, |
| 135 | Parameters parameters, Listener listener, |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 136 | boolean mirror, Looper looper, FocusUI ui) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 137 | mHandler = new MainHandler(looper); |
| 138 | mMatrix = new Matrix(); |
| 139 | mPreferences = preferences; |
| 140 | mDefaultFocusModes = defaultFocusModes; |
| 141 | setParameters(parameters); |
| 142 | mListener = listener; |
| 143 | setMirror(mirror); |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 144 | mFocusDefault = true; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 145 | mUI = ui; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | public void setParameters(Parameters parameters) { |
| 149 | // parameters can only be null when onConfigurationChanged is called |
| 150 | // before camera is open. We will just return in this case, because |
| 151 | // parameters will be set again later with the right parameters after |
| 152 | // camera is open. |
| 153 | if (parameters == null) return; |
| 154 | mParameters = parameters; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 155 | mFocusAreaSupported = CameraUtil.isFocusAreaSupported(parameters); |
| 156 | mMeteringAreaSupported = CameraUtil.isMeteringAreaSupported(parameters); |
| 157 | mLockAeAwbNeeded = (CameraUtil.isAutoExposureLockSupported(mParameters) || |
| 158 | CameraUtil.isAutoWhiteBalanceLockSupported(mParameters)); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public void setPreviewSize(int previewWidth, int previewHeight) { |
| 162 | if (mPreviewWidth != previewWidth || mPreviewHeight != previewHeight) { |
| 163 | mPreviewWidth = previewWidth; |
| 164 | mPreviewHeight = previewHeight; |
| 165 | setMatrix(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public void setMirror(boolean mirror) { |
| 170 | mMirror = mirror; |
| 171 | setMatrix(); |
| 172 | } |
| 173 | |
| 174 | public void setDisplayOrientation(int displayOrientation) { |
| 175 | mDisplayOrientation = displayOrientation; |
| 176 | setMatrix(); |
| 177 | } |
| 178 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 179 | private void setMatrix() { |
| 180 | if (mPreviewWidth != 0 && mPreviewHeight != 0) { |
| 181 | Matrix matrix = new Matrix(); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 182 | CameraUtil.prepareMatrix(matrix, mMirror, mDisplayOrientation, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 183 | mPreviewWidth, mPreviewHeight); |
| 184 | // In face detection, the matrix converts the driver coordinates to UI |
| 185 | // coordinates. In tap focus, the inverted matrix converts the UI |
| 186 | // coordinates to driver coordinates. |
| 187 | matrix.invert(mMatrix); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 188 | mInitialized = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | private void lockAeAwbIfNeeded() { |
| 193 | if (mLockAeAwbNeeded && !mAeAwbLock) { |
| 194 | mAeAwbLock = true; |
| 195 | mListener.setFocusParameters(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private void unlockAeAwbIfNeeded() { |
| 200 | if (mLockAeAwbNeeded && mAeAwbLock && (mState != STATE_FOCUSING_SNAP_ON_FINISH)) { |
| 201 | mAeAwbLock = false; |
| 202 | mListener.setFocusParameters(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | public void onShutterDown() { |
| 207 | if (!mInitialized) return; |
| 208 | |
| 209 | boolean autoFocusCalled = false; |
| 210 | if (needAutoFocusCall()) { |
| 211 | // Do not focus if touch focus has been triggered. |
| 212 | if (mState != STATE_SUCCESS && mState != STATE_FAIL) { |
| 213 | autoFocus(); |
| 214 | autoFocusCalled = true; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (!autoFocusCalled) lockAeAwbIfNeeded(); |
| 219 | } |
| 220 | |
| 221 | public void onShutterUp() { |
| 222 | if (!mInitialized) return; |
| 223 | |
| 224 | if (needAutoFocusCall()) { |
| 225 | // User releases half-pressed focus key. |
| 226 | if (mState == STATE_FOCUSING || mState == STATE_SUCCESS |
| 227 | || mState == STATE_FAIL) { |
| 228 | cancelAutoFocus(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Unlock AE and AWB after cancelAutoFocus. Camera API does not |
| 233 | // guarantee setParameters can be called during autofocus. |
| 234 | unlockAeAwbIfNeeded(); |
| 235 | } |
| 236 | |
| 237 | public void doSnap() { |
| 238 | if (!mInitialized) return; |
| 239 | |
| 240 | // If the user has half-pressed the shutter and focus is completed, we |
| 241 | // can take the photo right away. If the focus mode is infinity, we can |
| 242 | // also take the photo. |
| 243 | if (!needAutoFocusCall() || (mState == STATE_SUCCESS || mState == STATE_FAIL)) { |
| 244 | capture(); |
| 245 | } else if (mState == STATE_FOCUSING) { |
| 246 | // Half pressing the shutter (i.e. the focus button event) will |
| 247 | // already have requested AF for us, so just request capture on |
| 248 | // focus here. |
| 249 | mState = STATE_FOCUSING_SNAP_ON_FINISH; |
| 250 | } else if (mState == STATE_IDLE) { |
| 251 | // We didn't do focus. This can happen if the user press focus key |
| 252 | // while the snapshot is still in progress. The user probably wants |
| 253 | // the next snapshot as soon as possible, so we just do a snapshot |
| 254 | // without focusing again. |
| 255 | capture(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public void onAutoFocus(boolean focused, boolean shutterButtonPressed) { |
| 260 | if (mState == STATE_FOCUSING_SNAP_ON_FINISH) { |
| 261 | // Take the picture no matter focus succeeds or fails. No need |
| 262 | // to play the AF sound if we're about to play the shutter |
| 263 | // sound. |
| 264 | if (focused) { |
| 265 | mState = STATE_SUCCESS; |
| 266 | } else { |
| 267 | mState = STATE_FAIL; |
| 268 | } |
| 269 | updateFocusUI(); |
| 270 | capture(); |
| 271 | } else if (mState == STATE_FOCUSING) { |
| 272 | // This happens when (1) user is half-pressing the focus key or |
| 273 | // (2) touch focus is triggered. Play the focus tone. Do not |
| 274 | // take the picture now. |
| 275 | if (focused) { |
| 276 | mState = STATE_SUCCESS; |
| 277 | } else { |
| 278 | mState = STATE_FAIL; |
| 279 | } |
| 280 | updateFocusUI(); |
| 281 | // If this is triggered by touch focus, cancel focus after a |
| 282 | // while. |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 283 | if (!mFocusDefault) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 284 | mHandler.sendEmptyMessageDelayed(RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY); |
| 285 | } |
| 286 | if (shutterButtonPressed) { |
| 287 | // Lock AE & AWB so users can half-press shutter and recompose. |
| 288 | lockAeAwbIfNeeded(); |
| 289 | } |
| 290 | } else if (mState == STATE_IDLE) { |
| 291 | // User has released the focus key before focus completes. |
| 292 | // Do nothing. |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | public void onAutoFocusMoving(boolean moving) { |
| 297 | if (!mInitialized) return; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 298 | |
| 299 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 300 | // Ignore if the camera has detected some faces. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 301 | if (mUI.hasFaces()) { |
| 302 | mUI.clearFocus(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Ignore if we have requested autofocus. This method only handles |
| 307 | // continuous autofocus. |
| 308 | if (mState != STATE_IDLE) return; |
| 309 | |
Michael Kolb | e3de722 | 2013-02-18 15:16:44 -0800 | [diff] [blame] | 310 | // animate on false->true trasition only b/8219520 |
| 311 | if (moving && !mPreviousMoving) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 312 | mUI.onFocusStarted(); |
Michael Kolb | e3de722 | 2013-02-18 15:16:44 -0800 | [diff] [blame] | 313 | } else if (!moving) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 314 | mUI.onFocusSucceeded(true); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 315 | } |
Michael Kolb | e3de722 | 2013-02-18 15:16:44 -0800 | [diff] [blame] | 316 | mPreviousMoving = moving; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH) |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 320 | private void initializeFocusAreas(int x, int y) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 321 | if (mFocusArea == null) { |
| 322 | mFocusArea = new ArrayList<Object>(); |
| 323 | mFocusArea.add(new Area(new Rect(), 1)); |
| 324 | } |
| 325 | |
| 326 | // Convert the coordinates to driver format. |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 327 | calculateTapArea(x, y, 1f, ((Area) mFocusArea.get(0)).rect); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH) |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 331 | private void initializeMeteringAreas(int x, int y) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 332 | if (mMeteringArea == null) { |
| 333 | mMeteringArea = new ArrayList<Object>(); |
| 334 | mMeteringArea.add(new Area(new Rect(), 1)); |
| 335 | } |
| 336 | |
| 337 | // Convert the coordinates to driver format. |
| 338 | // AE area is bigger because exposure is sensitive and |
| 339 | // easy to over- or underexposure if area is too small. |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 340 | calculateTapArea(x, y, 1.5f, ((Area) mMeteringArea.get(0)).rect); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | public void onSingleTapUp(int x, int y) { |
| 344 | if (!mInitialized || mState == STATE_FOCUSING_SNAP_ON_FINISH) return; |
| 345 | |
| 346 | // Let users be able to cancel previous touch focus. |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 347 | if ((!mFocusDefault) && (mState == STATE_FOCUSING || |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 348 | mState == STATE_SUCCESS || mState == STATE_FAIL)) { |
| 349 | cancelAutoFocus(); |
| 350 | } |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 351 | if (mPreviewWidth == 0 || mPreviewHeight == 0) return; |
| 352 | mFocusDefault = false; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 353 | // Initialize mFocusArea. |
| 354 | if (mFocusAreaSupported) { |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 355 | initializeFocusAreas(x, y); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 356 | } |
| 357 | // Initialize mMeteringArea. |
| 358 | if (mMeteringAreaSupported) { |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 359 | initializeMeteringAreas(x, y); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Use margin to set the focus indicator to the touched area. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 363 | mUI.setFocusPosition(x, y); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 364 | |
| 365 | // Stop face detection because we want to specify focus and metering area. |
| 366 | mListener.stopFaceDetection(); |
| 367 | |
| 368 | // Set the focus area and metering area. |
| 369 | mListener.setFocusParameters(); |
| 370 | if (mFocusAreaSupported) { |
| 371 | autoFocus(); |
| 372 | } else { // Just show the indicator in all other cases. |
| 373 | updateFocusUI(); |
| 374 | // Reset the metering area in 3 seconds. |
| 375 | mHandler.removeMessages(RESET_TOUCH_FOCUS); |
| 376 | mHandler.sendEmptyMessageDelayed(RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | public void onPreviewStarted() { |
| 381 | mState = STATE_IDLE; |
| 382 | } |
| 383 | |
| 384 | public void onPreviewStopped() { |
| 385 | // If auto focus was in progress, it would have been stopped. |
| 386 | mState = STATE_IDLE; |
| 387 | resetTouchFocus(); |
| 388 | updateFocusUI(); |
| 389 | } |
| 390 | |
| 391 | public void onCameraReleased() { |
| 392 | onPreviewStopped(); |
| 393 | } |
| 394 | |
| 395 | private void autoFocus() { |
| 396 | Log.v(TAG, "Start autofocus."); |
| 397 | mListener.autoFocus(); |
| 398 | mState = STATE_FOCUSING; |
| 399 | // Pause the face view because the driver will keep sending face |
| 400 | // callbacks after the focus completes. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 401 | mUI.pauseFaceDetection(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 402 | updateFocusUI(); |
| 403 | mHandler.removeMessages(RESET_TOUCH_FOCUS); |
| 404 | } |
| 405 | |
| 406 | private void cancelAutoFocus() { |
| 407 | Log.v(TAG, "Cancel autofocus."); |
| 408 | |
| 409 | // Reset the tap area before calling mListener.cancelAutofocus. |
| 410 | // Otherwise, focus mode stays at auto and the tap area passed to the |
| 411 | // driver is not reset. |
| 412 | resetTouchFocus(); |
| 413 | mListener.cancelAutoFocus(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 414 | mUI.resumeFaceDetection(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 415 | mState = STATE_IDLE; |
| 416 | updateFocusUI(); |
| 417 | mHandler.removeMessages(RESET_TOUCH_FOCUS); |
| 418 | } |
| 419 | |
| 420 | private void capture() { |
| 421 | if (mListener.capture()) { |
| 422 | mState = STATE_IDLE; |
| 423 | mHandler.removeMessages(RESET_TOUCH_FOCUS); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | public String getFocusMode() { |
| 428 | if (mOverrideFocusMode != null) return mOverrideFocusMode; |
Michael Kolb | 4a40e12 | 2013-02-14 08:30:59 -0800 | [diff] [blame] | 429 | if (mParameters == null) return Parameters.FOCUS_MODE_AUTO; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 430 | List<String> supportedFocusModes = mParameters.getSupportedFocusModes(); |
| 431 | |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 432 | if (mFocusAreaSupported && !mFocusDefault) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 433 | // Always use autofocus in tap-to-focus. |
| 434 | mFocusMode = Parameters.FOCUS_MODE_AUTO; |
| 435 | } else { |
| 436 | // The default is continuous autofocus. |
| 437 | mFocusMode = mPreferences.getString( |
| 438 | CameraSettings.KEY_FOCUS_MODE, null); |
| 439 | |
| 440 | // Try to find a supported focus mode from the default list. |
| 441 | if (mFocusMode == null) { |
| 442 | for (int i = 0; i < mDefaultFocusModes.length; i++) { |
| 443 | String mode = mDefaultFocusModes[i]; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 444 | if (CameraUtil.isSupported(mode, supportedFocusModes)) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 445 | mFocusMode = mode; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 451 | if (!CameraUtil.isSupported(mFocusMode, supportedFocusModes)) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 452 | // For some reasons, the driver does not support the current |
| 453 | // focus mode. Fall back to auto. |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 454 | if (CameraUtil.isSupported(Parameters.FOCUS_MODE_AUTO, |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 455 | mParameters.getSupportedFocusModes())) { |
| 456 | mFocusMode = Parameters.FOCUS_MODE_AUTO; |
| 457 | } else { |
| 458 | mFocusMode = mParameters.getFocusMode(); |
| 459 | } |
| 460 | } |
| 461 | return mFocusMode; |
| 462 | } |
| 463 | |
| 464 | public List getFocusAreas() { |
| 465 | return mFocusArea; |
| 466 | } |
| 467 | |
| 468 | public List getMeteringAreas() { |
| 469 | return mMeteringArea; |
| 470 | } |
| 471 | |
| 472 | public void updateFocusUI() { |
| 473 | if (!mInitialized) return; |
| 474 | // Show only focus indicator or face indicator. |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 475 | |
| 476 | if (mState == STATE_IDLE) { |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 477 | if (mFocusDefault) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 478 | mUI.clearFocus(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 479 | } else { |
| 480 | // Users touch on the preview and the indicator represents the |
| 481 | // metering area. Either focus area is not supported or |
| 482 | // autoFocus call is not required. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 483 | mUI.onFocusStarted(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 484 | } |
| 485 | } else if (mState == STATE_FOCUSING || mState == STATE_FOCUSING_SNAP_ON_FINISH) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 486 | mUI.onFocusStarted(); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 487 | } else { |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 488 | if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusMode)) { |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 489 | // TODO: check HAL behavior and decide if this can be removed. |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 490 | mUI.onFocusSucceeded(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 491 | } else if (mState == STATE_SUCCESS) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 492 | mUI.onFocusSucceeded(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 493 | } else if (mState == STATE_FAIL) { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 494 | mUI.onFocusFailed(false); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | public void resetTouchFocus() { |
| 500 | if (!mInitialized) return; |
| 501 | |
| 502 | // Put focus indicator to the center. clear reset position |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 503 | mUI.clearFocus(); |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 504 | // Initialize mFocusArea. |
| 505 | if (mFocusAreaSupported) { |
| 506 | initializeFocusAreas(mPreviewWidth / 2, mPreviewHeight / 2); |
| 507 | } |
| 508 | // Initialize mMeteringArea. |
| 509 | if (mMeteringAreaSupported) { |
| 510 | initializeMeteringAreas(mPreviewWidth / 2, mPreviewHeight / 2); |
| 511 | } |
| 512 | mFocusDefault = true; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 515 | private void calculateTapArea(int x, int y, float areaMultiple, Rect rect) { |
| 516 | int areaSize = (int) (Math.min(mPreviewWidth, mPreviewHeight) * areaMultiple / 20); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 517 | int left = CameraUtil.clamp(x - areaSize, 0, mPreviewWidth - 2 * areaSize); |
| 518 | int top = CameraUtil.clamp(y - areaSize, 0, mPreviewHeight - 2 * areaSize); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 519 | |
Michael Kolb | 0718d48 | 2013-02-16 13:13:47 -0800 | [diff] [blame] | 520 | RectF rectF = new RectF(left, top, left + 2 * areaSize, top + 2 * areaSize); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 521 | mMatrix.mapRect(rectF); |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame^] | 522 | CameraUtil.rectFToRect(rectF, rect); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* package */ int getFocusState() { |
| 526 | return mState; |
| 527 | } |
| 528 | |
| 529 | public boolean isFocusCompleted() { |
| 530 | return mState == STATE_SUCCESS || mState == STATE_FAIL; |
| 531 | } |
| 532 | |
| 533 | public boolean isFocusingSnapOnFinish() { |
| 534 | return mState == STATE_FOCUSING_SNAP_ON_FINISH; |
| 535 | } |
| 536 | |
| 537 | public void removeMessages() { |
| 538 | mHandler.removeMessages(RESET_TOUCH_FOCUS); |
| 539 | } |
| 540 | |
| 541 | public void overrideFocusMode(String focusMode) { |
| 542 | mOverrideFocusMode = focusMode; |
| 543 | } |
| 544 | |
| 545 | public void setAeAwbLock(boolean lock) { |
| 546 | mAeAwbLock = lock; |
| 547 | } |
| 548 | |
| 549 | public boolean getAeAwbLock() { |
| 550 | return mAeAwbLock; |
| 551 | } |
| 552 | |
| 553 | private boolean needAutoFocusCall() { |
| 554 | String focusMode = getFocusMode(); |
| 555 | return !(focusMode.equals(Parameters.FOCUS_MODE_INFINITY) |
| 556 | || focusMode.equals(Parameters.FOCUS_MODE_FIXED) |
| 557 | || focusMode.equals(Parameters.FOCUS_MODE_EDOF)); |
| 558 | } |
| 559 | } |