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