Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 1 | /* |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 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 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 17 | package com.android.camera; |
| 18 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 19 | import android.app.Dialog; |
| 20 | import android.content.DialogInterface; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 22 | import android.graphics.Matrix; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 23 | import android.graphics.RectF; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 24 | import android.graphics.SurfaceTexture; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 25 | import android.hardware.Camera.Face; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 27 | import android.view.GestureDetector; |
| 28 | import android.view.MotionEvent; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 29 | import android.view.View; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 30 | import android.view.ViewGroup; |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
| 32 | import android.widget.ImageView; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 33 | |
Andy Huibers | 62b4c3c | 2014-08-25 15:01:25 -0700 | [diff] [blame] | 34 | import com.android.camera.debug.DebugPropertyHelper; |
Angus Kong | 5596b4c | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 35 | import com.android.camera.debug.Log; |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 36 | import com.android.camera.ui.CountDownView; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 37 | import com.android.camera.ui.FaceView; |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 38 | import com.android.camera.ui.PreviewOverlay; |
Doris Liu | 06db742 | 2013-12-09 19:36:25 -0800 | [diff] [blame] | 39 | import com.android.camera.ui.PreviewStatusListener; |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 40 | import com.android.camera.ui.focus.FocusRing; |
Alan Newberger | 2631a15 | 2014-09-24 14:23:30 -0700 | [diff] [blame] | 41 | import com.android.camera.util.ApiHelper; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 42 | import com.android.camera.util.CameraUtil; |
Alan Newberger | 2631a15 | 2014-09-24 14:23:30 -0700 | [diff] [blame] | 43 | import com.android.camera.util.GservicesHelper; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 44 | import com.android.camera.widget.AspectRatioDialogLayout; |
| 45 | import com.android.camera.widget.AspectRatioSelector; |
| 46 | import com.android.camera.widget.LocationDialogLayout; |
Sascha Haeberling | 8e963a5 | 2013-08-06 11:43:02 -0700 | [diff] [blame] | 47 | import com.android.camera2.R; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 48 | import com.android.ex.camera2.portability.CameraAgent; |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 49 | import com.android.ex.camera2.portability.CameraCapabilities; |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 50 | import com.android.ex.camera2.portability.CameraSettings; |
Angus Kong | b50b5cb | 2013-08-09 14:55:20 -0700 | [diff] [blame] | 51 | |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 52 | public class PhotoUI implements PreviewStatusListener, |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 53 | CameraAgent.CameraFaceDetectionCallback, PreviewStatusListener.PreviewAreaChangedListener { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 54 | |
Angus Kong | 5596b4c | 2014-03-11 16:27:30 -0700 | [diff] [blame] | 55 | private static final Log.Tag TAG = new Log.Tag("PhotoUI"); |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 56 | private static final int DOWN_SAMPLE_FACTOR = 4; |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 57 | private static final float UNSET = 0f; |
Spike Sprague | 0f3c4b4 | 2013-12-10 19:50:17 -0800 | [diff] [blame] | 58 | |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 59 | private final PreviewOverlay mPreviewOverlay; |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 60 | private final FocusRing mFocusRing; |
Sascha Haeberling | e2914fd | 2014-01-17 19:03:52 -0800 | [diff] [blame] | 61 | private final CameraActivity mActivity; |
| 62 | private final PhotoController mController; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 63 | |
Sascha Haeberling | e2914fd | 2014-01-17 19:03:52 -0800 | [diff] [blame] | 64 | private final View mRootView; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 65 | private Dialog mDialog = null; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 66 | |
Doris Liu | d27ea7b | 2014-02-25 11:46:11 -0800 | [diff] [blame] | 67 | // TODO: Remove face view logic if UX does not bring it back within a month. |
Andy Huibers | 62b4c3c | 2014-08-25 15:01:25 -0700 | [diff] [blame] | 68 | private final FaceView mFaceView; |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 69 | private DecodeImageForReview mDecodeTaskForReview = null; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 70 | |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 71 | private float mZoomMax; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 72 | |
| 73 | private int mPreviewWidth = 0; |
| 74 | private int mPreviewHeight = 0; |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 75 | private float mAspectRatio = UNSET; |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 76 | |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 77 | private ImageView mIntentReviewImageView; |
| 78 | |
Doris Liu | 06db742 | 2013-12-09 19:36:25 -0800 | [diff] [blame] | 79 | private final GestureDetector.OnGestureListener mPreviewGestureListener |
| 80 | = new GestureDetector.SimpleOnGestureListener() { |
| 81 | @Override |
| 82 | public boolean onSingleTapUp(MotionEvent ev) { |
| 83 | mController.onSingleTapUp(null, (int) ev.getX(), (int) ev.getY()); |
| 84 | return true; |
| 85 | } |
| 86 | }; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 87 | private final DialogInterface.OnDismissListener mOnDismissListener |
| 88 | = new DialogInterface.OnDismissListener() { |
| 89 | @Override |
| 90 | public void onDismiss(DialogInterface dialog) { |
| 91 | mDialog = null; |
| 92 | } |
| 93 | }; |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 94 | private Runnable mRunnableForNextFrame = null; |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 95 | private final CountDownView mCountdownView; |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 96 | |
Doris Liu | 06db742 | 2013-12-09 19:36:25 -0800 | [diff] [blame] | 97 | @Override |
| 98 | public GestureDetector.OnGestureListener getGestureListener() { |
| 99 | return mPreviewGestureListener; |
| 100 | } |
| 101 | |
Doris Liu | 4a010db | 2013-12-16 18:44:49 -0800 | [diff] [blame] | 102 | @Override |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 103 | public View.OnTouchListener getTouchListener() { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
Doris Liu | 4a010db | 2013-12-16 18:44:49 -0800 | [diff] [blame] | 108 | public void onPreviewLayoutChanged(View v, int left, int top, int right, |
| 109 | int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 110 | int width = right - left; |
| 111 | int height = bottom - top; |
| 112 | if (mPreviewWidth != width || mPreviewHeight != height) { |
| 113 | mPreviewWidth = width; |
| 114 | mPreviewHeight = height; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 115 | } |
Doris Liu | 4a010db | 2013-12-16 18:44:49 -0800 | [diff] [blame] | 116 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 117 | |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public boolean shouldAutoAdjustTransformMatrixOnLayout() { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean shouldAutoAdjustBottomBar() { |
| 125 | return true; |
| 126 | } |
| 127 | |
Doris Liu | 11ddd73 | 2014-01-07 10:52:11 -0800 | [diff] [blame] | 128 | @Override |
| 129 | public void onPreviewFlipped() { |
| 130 | mController.updateCameraOrientation(); |
| 131 | } |
| 132 | |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Sets the runnable to run when the next frame comes in. |
| 135 | */ |
| 136 | public void setRunnableForNextFrame(Runnable runnable) { |
| 137 | mRunnableForNextFrame = runnable; |
| 138 | } |
| 139 | |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Starts the countdown timer. |
| 142 | * |
| 143 | * @param sec seconds to countdown |
| 144 | */ |
| 145 | public void startCountdown(int sec) { |
| 146 | mCountdownView.startCountDown(sec); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets a listener that gets notified when the countdown is finished. |
| 151 | */ |
| 152 | public void setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener) { |
| 153 | mCountdownView.setCountDownStatusListener(listener); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns whether the countdown is on-going. |
| 158 | */ |
| 159 | public boolean isCountingDown() { |
| 160 | return mCountdownView.isCountingDown(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Cancels the on-going countdown, if any. |
| 165 | */ |
| 166 | public void cancelCountDown() { |
| 167 | mCountdownView.cancelCountDown(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onPreviewAreaChanged(RectF previewArea) { |
| 172 | if (mFaceView != null) { |
| 173 | mFaceView.onPreviewAreaChanged(previewArea); |
| 174 | } |
| 175 | mCountdownView.onPreviewAreaChanged(previewArea); |
| 176 | } |
| 177 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 178 | private class DecodeTask extends AsyncTask<Void, Void, Bitmap> { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 179 | private final byte [] mData; |
Sascha Haeberling | e2914fd | 2014-01-17 19:03:52 -0800 | [diff] [blame] | 180 | private final int mOrientation; |
| 181 | private final boolean mMirror; |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 182 | |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 183 | public DecodeTask(byte[] data, int orientation, boolean mirror) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 184 | mData = data; |
Doris Liu | ce2acbc | 2013-08-21 18:45:29 -0700 | [diff] [blame] | 185 | mOrientation = orientation; |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 186 | mMirror = mirror; |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 190 | protected Bitmap doInBackground(Void... params) { |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 191 | // Decode image in background. |
Doris Liu | ce2acbc | 2013-08-21 18:45:29 -0700 | [diff] [blame] | 192 | Bitmap bitmap = CameraUtil.downSample(mData, DOWN_SAMPLE_FACTOR); |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 193 | if (mOrientation != 0 || mMirror) { |
Doris Liu | ce2acbc | 2013-08-21 18:45:29 -0700 | [diff] [blame] | 194 | Matrix m = new Matrix(); |
Doris Liu | 29da2db | 2013-08-28 14:28:45 -0700 | [diff] [blame] | 195 | if (mMirror) { |
| 196 | // Flip horizontally |
| 197 | m.setScale(-1f, 1f); |
| 198 | } |
Andy Huibers | cec75e4 | 2013-10-29 16:03:58 -0700 | [diff] [blame] | 199 | m.preRotate(mOrientation); |
Doris Liu | ce2acbc | 2013-08-21 18:45:29 -0700 | [diff] [blame] | 200 | return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, |
| 201 | false); |
| 202 | } |
| 203 | return bitmap; |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 204 | } |
Sascha Haeberling | 37f3611 | 2013-08-06 14:31:52 -0700 | [diff] [blame] | 205 | } |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 206 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 207 | private class DecodeImageForReview extends DecodeTask { |
| 208 | public DecodeImageForReview(byte[] data, int orientation, boolean mirror) { |
| 209 | super(data, orientation, mirror); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected void onPostExecute(Bitmap bitmap) { |
| 214 | if (isCancelled()) { |
| 215 | return; |
| 216 | } |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 217 | |
| 218 | mIntentReviewImageView.setImageBitmap(bitmap); |
| 219 | showIntentReviewImageView(); |
| 220 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 221 | mDecodeTaskForReview = null; |
| 222 | } |
| 223 | } |
| 224 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 225 | public PhotoUI(CameraActivity activity, PhotoController controller, View parent) { |
| 226 | mActivity = activity; |
| 227 | mController = controller; |
| 228 | mRootView = parent; |
| 229 | |
Doris Liu | f55f3c4 | 2013-11-20 00:24:46 -0800 | [diff] [blame] | 230 | ViewGroup moduleRoot = (ViewGroup) mRootView.findViewById(R.id.module_layout); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 231 | mActivity.getLayoutInflater().inflate(R.layout.photo_module, |
Sascha Haeberling | e2914fd | 2014-01-17 19:03:52 -0800 | [diff] [blame] | 232 | moduleRoot, true); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 233 | initIndicators(); |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 234 | mFocusRing = (FocusRing) mRootView.findViewById(R.id.focus_ring); |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 235 | mPreviewOverlay = (PreviewOverlay) mRootView.findViewById(R.id.preview_overlay); |
Doris Liu | 6c75164 | 2014-05-05 18:43:26 -0700 | [diff] [blame] | 236 | mCountdownView = (CountDownView) mRootView.findViewById(R.id.count_down_view); |
Andy Huibers | 62b4c3c | 2014-08-25 15:01:25 -0700 | [diff] [blame] | 237 | // Show faces if we are in debug mode. |
| 238 | if (DebugPropertyHelper.showCaptureDebugUI()) { |
| 239 | mFaceView = (FaceView) mRootView.findViewById(R.id.face_view); |
| 240 | } else { |
| 241 | mFaceView = null; |
| 242 | } |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 243 | |
| 244 | if (mController.isImageCaptureIntent()) { |
Spike Sprague | 02b9b9a | 2014-07-02 11:26:28 -0700 | [diff] [blame] | 245 | initIntentReviewImageView(); |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 246 | } |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 247 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 248 | |
Spike Sprague | 02b9b9a | 2014-07-02 11:26:28 -0700 | [diff] [blame] | 249 | private void initIntentReviewImageView() { |
| 250 | mIntentReviewImageView = (ImageView) mRootView.findViewById(R.id.intent_review_imageview); |
Spike Sprague | b4a2222 | 2014-05-22 14:40:53 -0700 | [diff] [blame] | 251 | mActivity.getCameraAppUI().addPreviewAreaChangedListener( |
| 252 | new PreviewStatusListener.PreviewAreaChangedListener() { |
| 253 | @Override |
| 254 | public void onPreviewAreaChanged(RectF previewArea) { |
| 255 | FrameLayout.LayoutParams params = |
| 256 | (FrameLayout.LayoutParams) mIntentReviewImageView.getLayoutParams(); |
| 257 | params.width = (int) previewArea.width(); |
| 258 | params.height = (int) previewArea.height(); |
| 259 | params.setMargins((int) previewArea.left, (int) previewArea.top, 0, 0); |
| 260 | mIntentReviewImageView.setLayoutParams(params); |
| 261 | } |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Show the image review over the live preview for intent captures. |
| 267 | */ |
| 268 | public void showIntentReviewImageView() { |
| 269 | if (mIntentReviewImageView != null) { |
| 270 | mIntentReviewImageView.setVisibility(View.VISIBLE); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Hide the image review over the live preview for intent captures. |
| 276 | */ |
| 277 | public void hideIntentReviewImageView() { |
| 278 | if (mIntentReviewImageView != null) { |
| 279 | mIntentReviewImageView.setVisibility(View.INVISIBLE); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 284 | public FocusRing getFocusRing() { |
| 285 | return mFocusRing; |
Doris Liu | 482de02 | 2013-12-18 19:18:16 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Doris Liu | d053a5b | 2013-10-25 20:20:43 -0700 | [diff] [blame] | 288 | public void updatePreviewAspectRatio(float aspectRatio) { |
| 289 | if (aspectRatio <= 0) { |
| 290 | Log.e(TAG, "Invalid aspect ratio: " + aspectRatio); |
| 291 | return; |
| 292 | } |
| 293 | if (aspectRatio < 1f) { |
| 294 | aspectRatio = 1f / aspectRatio; |
| 295 | } |
| 296 | |
| 297 | if (mAspectRatio != aspectRatio) { |
| 298 | mAspectRatio = aspectRatio; |
| 299 | // Update transform matrix with the new aspect ratio. |
Doris Liu | 70da918 | 2013-12-17 18:41:15 -0800 | [diff] [blame] | 300 | mController.updatePreviewAspectRatio(mAspectRatio); |
Doris Liu | d053a5b | 2013-10-25 20:20:43 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 304 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 305 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 306 | mController.onPreviewUIReady(); |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 309 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 310 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 311 | // Ignored, Camera does all the work for us |
| 312 | } |
| 313 | |
Angus Kong | dcccc51 | 2013-08-08 17:06:03 -0700 | [diff] [blame] | 314 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 315 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
Erin Dahlgren | d8de077 | 2014-02-03 10:12:27 -0800 | [diff] [blame] | 316 | mController.onPreviewUIDestroyed(); |
| 317 | return true; |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
ztenghui | 7b265a6 | 2013-09-09 14:58:44 -0700 | [diff] [blame] | 320 | @Override |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 321 | public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 322 | if (mRunnableForNextFrame != null) { |
| 323 | mRootView.post(mRunnableForNextFrame); |
| 324 | mRunnableForNextFrame = null; |
| 325 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | public View getRootView() { |
| 329 | return mRootView; |
| 330 | } |
| 331 | |
| 332 | private void initIndicators() { |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 333 | // TODO init toggle buttons on bottom bar here |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 336 | public void onCameraOpened(CameraCapabilities capabilities, CameraSettings settings) { |
| 337 | initializeZoom(capabilities, settings); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Erin Dahlgren | 0a6a8d8 | 2014-01-09 22:17:38 -0800 | [diff] [blame] | 340 | public void animateCapture(final byte[] jpegData, int orientation, boolean mirror) { |
| 341 | // Decode jpeg byte array and then animate the jpeg |
| 342 | DecodeTask task = new DecodeTask(jpegData, orientation, mirror); |
| 343 | task.execute(); |
Spike Sprague | eb3134f | 2013-12-12 11:14:27 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 346 | // called from onResume but only the first time |
Kevin Gabayan | ffbc43c | 2013-12-09 11:41:50 -0800 | [diff] [blame] | 347 | public void initializeFirstTime() { |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 348 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // called from onResume every other time |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 352 | public void initializeSecondTime(CameraCapabilities capabilities, CameraSettings settings) { |
| 353 | initializeZoom(capabilities, settings); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 354 | if (mController.isImageCaptureIntent()) { |
| 355 | hidePostCaptureAlert(); |
| 356 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 359 | public void showLocationAndAspectRatioDialog( |
| 360 | final PhotoModule.LocationDialogCallback locationCallback, |
| 361 | final PhotoModule.AspectRatioDialogCallback aspectRatioDialogCallback) { |
| 362 | setDialog(new Dialog(mActivity, |
| 363 | android.R.style.Theme_Black_NoTitleBar_Fullscreen)); |
| 364 | final LocationDialogLayout locationDialogLayout = (LocationDialogLayout) mActivity |
| 365 | .getLayoutInflater().inflate(R.layout.location_dialog_layout, null); |
| 366 | locationDialogLayout.setLocationTaggingSelectionListener( |
| 367 | new LocationDialogLayout.LocationTaggingSelectionListener() { |
| 368 | @Override |
| 369 | public void onLocationTaggingSelected(boolean selected) { |
| 370 | // Update setting. |
| 371 | locationCallback.onLocationTaggingSelected(selected); |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 372 | |
| 373 | if (showAspectRatioDialogOnThisDevice()) { |
| 374 | // Go to next page. |
| 375 | showAspectRatioDialog(aspectRatioDialogCallback, mDialog); |
| 376 | } else { |
| 377 | // If we don't want to show the aspect ratio dialog, |
| 378 | // dismiss the dialog right after the user chose the |
| 379 | // location setting. |
| 380 | if (mDialog != null) { |
| 381 | mDialog.dismiss(); |
| 382 | } |
| 383 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 384 | } |
| 385 | }); |
| 386 | mDialog.setContentView(locationDialogLayout, new ViewGroup.LayoutParams( |
| 387 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); |
| 388 | mDialog.show(); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Dismisses previous dialog if any, sets current dialog to the given dialog, |
| 393 | * and set the on dismiss listener for the given dialog. |
| 394 | * @param dialog dialog to show |
| 395 | */ |
| 396 | private void setDialog(Dialog dialog) { |
| 397 | if (mDialog != null) { |
| 398 | mDialog.setOnDismissListener(null); |
| 399 | mDialog.dismiss(); |
| 400 | } |
| 401 | mDialog = dialog; |
| 402 | if (mDialog != null) { |
| 403 | mDialog.setOnDismissListener(mOnDismissListener); |
| 404 | } |
| 405 | } |
| 406 | |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 407 | /** |
| 408 | * @return Whether the dialog was shown. |
| 409 | */ |
| 410 | public boolean showAspectRatioDialog(final PhotoModule.AspectRatioDialogCallback callback) { |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 411 | if (showAspectRatioDialogOnThisDevice()) { |
| 412 | setDialog(new Dialog(mActivity, android.R.style.Theme_Black_NoTitleBar_Fullscreen)); |
| 413 | showAspectRatioDialog(callback, mDialog); |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 414 | return true; |
| 415 | } else { |
| 416 | return false; |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 417 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 420 | private boolean showAspectRatioDialog(final PhotoModule.AspectRatioDialogCallback callback, |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 421 | final Dialog aspectRatioDialog) { |
| 422 | if (aspectRatioDialog == null) { |
| 423 | Log.e(TAG, "Dialog for aspect ratio is null."); |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 424 | return false; |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 425 | } |
| 426 | final AspectRatioDialogLayout aspectRatioDialogLayout = |
| 427 | (AspectRatioDialogLayout) mActivity |
| 428 | .getLayoutInflater().inflate(R.layout.aspect_ratio_dialog_layout, null); |
| 429 | aspectRatioDialogLayout.initialize( |
| 430 | new AspectRatioDialogLayout.AspectRatioChangedListener() { |
| 431 | @Override |
| 432 | public void onAspectRatioChanged(AspectRatioSelector.AspectRatio aspectRatio) { |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 433 | // callback to set picture size. |
Doris Liu | 08b0cdd | 2014-05-13 19:19:55 -0700 | [diff] [blame] | 434 | callback.onAspectRatioSelected(aspectRatio, new Runnable() { |
| 435 | @Override |
| 436 | public void run() { |
| 437 | if (mDialog != null) { |
| 438 | mDialog.dismiss(); |
| 439 | } |
| 440 | } |
| 441 | }); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 442 | } |
Sascha Haeberling | e811b29 | 2014-05-19 14:47:09 -0700 | [diff] [blame] | 443 | }, callback.getCurrentAspectRatio()); |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 444 | aspectRatioDialog.setContentView(aspectRatioDialogLayout, new ViewGroup.LayoutParams( |
| 445 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); |
| 446 | aspectRatioDialog.show(); |
Sascha Haeberling | 4c1bffe | 2014-08-21 10:01:00 -0700 | [diff] [blame] | 447 | return true; |
Doris Liu | 6a83d52 | 2013-07-02 12:03:32 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 450 | /** |
| 451 | * @return Whether this is a device that we should show the aspect ratio |
| 452 | * intro dialog on. |
| 453 | */ |
| 454 | private boolean showAspectRatioDialogOnThisDevice() { |
Alan Newberger | 2631a15 | 2014-09-24 14:23:30 -0700 | [diff] [blame] | 455 | // We only want to show that dialog on N4/N5/N6 |
| 456 | // Don't show if using API2 portability, b/17462976 |
| 457 | return !GservicesHelper.useCamera2ApiThroughPortabilityLayer(mActivity) && |
| 458 | (ApiHelper.IS_NEXUS_4 || ApiHelper.IS_NEXUS_5 || ApiHelper.IS_NEXUS_6); |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 461 | public void initializeZoom(CameraCapabilities capabilities, CameraSettings settings) { |
| 462 | if ((capabilities == null) || settings == null || |
| 463 | !capabilities.supports(CameraCapabilities.Feature.ZOOM)) { |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 464 | return; |
| 465 | } |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 466 | mZoomMax = capabilities.getMaxZoomRatio(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 467 | // Currently we use immediate zoom for fast zooming to get better UX and |
| 468 | // there is no plan to take advantage of the smooth zoom. |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 469 | // TODO: Need to setup a path to AppUI to do this |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 470 | mPreviewOverlay.setupZoom(mZoomMax, settings.getCurrentZoomRatio(), |
Angus Kong | 6607dae | 2014-06-10 16:07:45 -0700 | [diff] [blame] | 471 | new ZoomChangeListener()); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 474 | public void animateFlash() { |
Doris Liu | 1dfe782 | 2013-12-12 00:02:08 -0800 | [diff] [blame] | 475 | mController.startPreCaptureAnimation(); |
Doris Liu | c2e9abd | 2013-06-19 14:20:51 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 478 | public boolean onBackPressed() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 479 | // In image capture mode, back button should: |
| 480 | // 1) if there is any popup, dismiss them, 2) otherwise, get out of |
| 481 | // image capture |
| 482 | if (mController.isImageCaptureIntent()) { |
Doris Liu | c3679c0 | 2013-08-08 18:08:43 -0700 | [diff] [blame] | 483 | mController.onCaptureCancelled(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 484 | return true; |
| 485 | } else if (!mController.isCameraIdle()) { |
| 486 | // ignore backs while we're taking a picture |
| 487 | return true; |
| 488 | } else { |
Doris Liu | c3679c0 | 2013-08-08 18:08:43 -0700 | [diff] [blame] | 489 | return false; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 493 | protected void showCapturedImageForReview(byte[] jpegData, int orientation, boolean mirror) { |
| 494 | mDecodeTaskForReview = new DecodeImageForReview(jpegData, orientation, mirror); |
| 495 | mDecodeTaskForReview.execute(); |
Erin Dahlgren | 4efa8b5 | 2013-12-17 18:31:35 -0800 | [diff] [blame] | 496 | |
Erin Dahlgren | 4efa8b5 | 2013-12-17 18:31:35 -0800 | [diff] [blame] | 497 | mActivity.getCameraAppUI().transitionToIntentReviewLayout(); |
Doris Liu | d31cdfe | 2013-05-14 11:31:19 -0700 | [diff] [blame] | 498 | pauseFaceDetection(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | protected void hidePostCaptureAlert() { |
Doris Liu | 36e56fb | 2013-09-11 17:38:08 -0700 | [diff] [blame] | 502 | if (mDecodeTaskForReview != null) { |
| 503 | mDecodeTaskForReview.cancel(true); |
| 504 | } |
Doris Liu | d31cdfe | 2013-05-14 11:31:19 -0700 | [diff] [blame] | 505 | resumeFaceDetection(); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | public void setDisplayOrientation(int orientation) { |
| 509 | if (mFaceView != null) { |
| 510 | mFaceView.setDisplayOrientation(orientation); |
| 511 | } |
| 512 | } |
| 513 | |
Doris Liu | f9e4f8f | 2013-12-04 18:04:22 -0800 | [diff] [blame] | 514 | private class ZoomChangeListener implements PreviewOverlay.OnZoomChangedListener { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 515 | @Override |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 516 | public void onZoomValueChanged(float ratio) { |
| 517 | mController.onZoomChanged(ratio); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void onZoomStart() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public void onZoomEnd() { |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Doris Liu | 6432cd6 | 2013-06-13 17:20:31 -0700 | [diff] [blame] | 529 | public void setSwipingEnabled(boolean enable) { |
| 530 | mActivity.setSwipingEnabled(enable); |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 533 | public void onPause() { |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 534 | if (mFaceView != null) { |
| 535 | mFaceView.clear(); |
| 536 | } |
Doris Liu | db8f975 | 2014-05-12 15:25:13 -0700 | [diff] [blame] | 537 | if (mDialog != null) { |
| 538 | mDialog.dismiss(); |
| 539 | } |
Alan Newberger | 7351415 | 2014-09-10 15:03:27 -0700 | [diff] [blame] | 540 | // recalculate aspect ratio when restarting. |
| 541 | mAspectRatio = 0.0f; |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 544 | public void clearFaces() { |
Marco Nelissen | 0744e4a | 2013-11-22 01:47:37 +0000 | [diff] [blame] | 545 | if (mFaceView != null) { |
| 546 | mFaceView.clear(); |
| 547 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 550 | public void pauseFaceDetection() { |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 551 | if (mFaceView != null) { |
| 552 | mFaceView.pause(); |
| 553 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 556 | public void resumeFaceDetection() { |
Sascha Haeberling | 1d94364 | 2014-05-19 11:56:53 -0700 | [diff] [blame] | 557 | if (mFaceView != null) { |
| 558 | mFaceView.resume(); |
| 559 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | public void onStartFaceDetection(int orientation, boolean mirror) { |
Doris Liu | d27ea7b | 2014-02-25 11:46:11 -0800 | [diff] [blame] | 563 | if (mFaceView != null) { |
| 564 | mFaceView.clear(); |
| 565 | mFaceView.setVisibility(View.VISIBLE); |
| 566 | mFaceView.setDisplayOrientation(orientation); |
| 567 | mFaceView.setMirror(mirror); |
| 568 | mFaceView.resume(); |
| 569 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | @Override |
Sol Boucher | 5a34496 | 2014-06-17 14:05:08 -0700 | [diff] [blame] | 573 | public void onFaceDetection(Face[] faces, CameraAgent.CameraProxy camera) { |
Doris Liu | d27ea7b | 2014-02-25 11:46:11 -0800 | [diff] [blame] | 574 | if (mFaceView != null) { |
| 575 | mFaceView.setFaces(faces); |
| 576 | } |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Michael Kolb | d6954f3 | 2013-03-08 20:43:01 -0800 | [diff] [blame] | 579 | } |