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