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