Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.graphics.Bitmap; |
| 20 | import android.graphics.Matrix; |
| 21 | import android.graphics.RectF; |
| 22 | import android.graphics.SurfaceTexture; |
| 23 | import android.view.GestureDetector; |
| 24 | import android.view.MotionEvent; |
| 25 | import android.view.TextureView; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 28 | |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 29 | import com.android.camera.debug.Log; |
Andy Huibers | a31162c | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 30 | import com.android.camera.ui.CountDownView; |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 31 | import com.android.camera.ui.PreviewOverlay; |
| 32 | import com.android.camera.ui.PreviewOverlay.OnZoomChangedListener; |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 33 | import com.android.camera.ui.PreviewStatusListener; |
| 34 | import com.android.camera.ui.ProgressOverlay; |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 35 | import com.android.camera.ui.focus.FocusRing; |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 36 | import com.android.camera2.R; |
| 37 | |
| 38 | /** |
| 39 | * Contains the UI for the CaptureModule. |
| 40 | */ |
| 41 | public class CaptureModuleUI implements |
| 42 | PreviewStatusListener { |
| 43 | |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 44 | private static final Log.Tag TAG = new Log.Tag("CaptureModuleUI"); |
| 45 | |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 46 | private final CameraActivity mActivity; |
| 47 | private final CaptureModule mModule; |
| 48 | private final View mRootView; |
| 49 | |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 50 | private final PreviewOverlay mPreviewOverlay; |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 51 | private final ProgressOverlay mProgressOverlay; |
| 52 | private final View.OnLayoutChangeListener mLayoutListener; |
| 53 | private final TextureView mPreviewView; |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 54 | |
| 55 | private final GestureDetector.OnGestureListener mPreviewGestureListener = new GestureDetector.SimpleOnGestureListener() { |
| 56 | @Override |
| 57 | public boolean onSingleTapUp(MotionEvent ev) { |
| 58 | mModule.onSingleTapUp(null, (int) ev.getX(), (int) ev.getY()); |
| 59 | return true; |
| 60 | } |
| 61 | }; |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 62 | private final FocusRing mFocusRing; |
Andy Huibers | a31162c | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 63 | private final CountDownView mCountdownView; |
| 64 | |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 65 | private int mPreviewAreaWidth; |
| 66 | private int mPreviewAreaHeight; |
| 67 | |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 68 | /** Maximum zoom; intialize to 1.0 (disabled) */ |
| 69 | private float mMaxZoom = 1f; |
| 70 | |
| 71 | /** Set up listener to receive zoom changes from View and send to module. */ |
| 72 | private final OnZoomChangedListener mZoomChancedListener = new OnZoomChangedListener() { |
| 73 | @Override |
Sol Boucher | 2192fba | 2014-08-19 17:24:07 -0700 | [diff] [blame] | 74 | public void onZoomValueChanged(float ratio) { |
| 75 | mModule.setZoom(ratio); |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onZoomStart() { |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onZoomEnd() { |
| 84 | } |
| 85 | }; |
| 86 | |
Andy Huibers | a31162c | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 87 | public void onPreviewAreaChanged(RectF previewArea) { |
| 88 | // TODO: mFaceView.onPreviewAreaChanged(previewArea); |
| 89 | mCountdownView.onPreviewAreaChanged(previewArea); |
| 90 | } |
| 91 | |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public void onPreviewLayoutChanged(View v, int left, int top, int right, |
| 94 | int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 95 | if (mLayoutListener != null) { |
| 96 | mLayoutListener.onLayoutChange(v, left, top, right, bottom, oldLeft, oldTop, oldRight, |
| 97 | oldBottom); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean shouldAutoAdjustTransformMatrixOnLayout() { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean shouldAutoAdjustBottomBar() { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onPreviewFlipped() { |
| 113 | // Do nothing because when preview is flipped, TextureView will lay |
| 114 | // itself out again, which will then trigger a transform matrix update. |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public GestureDetector.OnGestureListener getGestureListener() { |
| 119 | return mPreviewGestureListener; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public View.OnTouchListener getTouchListener() { |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | public CaptureModuleUI(CameraActivity activity, CaptureModule module, View parent, |
| 128 | View.OnLayoutChangeListener layoutListener) { |
| 129 | mActivity = activity; |
| 130 | mModule = module; |
| 131 | mRootView = parent; |
| 132 | mLayoutListener = layoutListener; |
| 133 | |
| 134 | ViewGroup moduleRoot = (ViewGroup) mRootView.findViewById(R.id.module_layout); |
| 135 | mActivity.getLayoutInflater().inflate(R.layout.capture_module, |
| 136 | moduleRoot, true); |
| 137 | |
| 138 | mPreviewView = (TextureView) mRootView.findViewById(R.id.preview_content); |
| 139 | |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 140 | mPreviewOverlay = (PreviewOverlay) mRootView.findViewById(R.id.preview_overlay); |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 141 | mProgressOverlay = (ProgressOverlay) mRootView.findViewById(R.id.progress_overlay); |
| 142 | |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 143 | mFocusRing = (FocusRing) mRootView.findViewById(R.id.focus_ring); |
Andy Huibers | a31162c | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 144 | mCountdownView = (CountDownView) mRootView.findViewById(R.id.count_down_view); |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 149 | mModule.onSurfaceTextureAvailable(surface, width, height); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
| 154 | return mModule.onSurfaceTextureDestroyed(surface); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 159 | mModule.onSurfaceTextureSizeChanged(surface, width, height); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 164 | mModule.onSurfaceTextureUpdated(surface); |
| 165 | } |
| 166 | |
| 167 | public void positionProgressOverlay(RectF area) { |
| 168 | mProgressOverlay.setBounds(area); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Getter for the width of the visible area of the preview. |
| 173 | */ |
| 174 | public int getPreviewAreaWidth() { |
| 175 | return mPreviewAreaWidth; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Getter for the height of the visible area of the preview. |
| 180 | */ |
| 181 | public int getPreviewAreaHeight() { |
| 182 | return mPreviewAreaHeight; |
| 183 | } |
| 184 | |
| 185 | public Matrix getPreviewTransform(Matrix m) { |
| 186 | return mPreviewView.getTransform(m); |
| 187 | } |
| 188 | |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 189 | public FocusRing getFocusRing() { |
| 190 | return mFocusRing; |
Andy Huibers | ca3c564 | 2014-09-06 20:44:25 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | public void showDebugMessage(String message) { |
Paul Rohde | 987ee64 | 2014-12-05 12:17:15 -0800 | [diff] [blame] | 194 | /* NoOp */ |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 195 | } |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 196 | |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 197 | /** |
Andy Huibers | a31162c | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 198 | * Starts the countdown timer. |
| 199 | * |
| 200 | * @param sec seconds to countdown |
| 201 | */ |
| 202 | public void startCountdown(int sec) { |
| 203 | mCountdownView.startCountDown(sec); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Sets a listener that gets notified when the countdown is finished. |
| 208 | */ |
| 209 | public void setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener) { |
| 210 | mCountdownView.setCountDownStatusListener(listener); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns whether the countdown is on-going. |
| 215 | */ |
| 216 | public boolean isCountingDown() { |
| 217 | return mCountdownView.isCountingDown(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Cancels the on-going countdown, if any. |
| 222 | */ |
| 223 | public void cancelCountDown() { |
| 224 | mCountdownView.cancelCountDown(); |
| 225 | } |
| 226 | |
| 227 | /** |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 228 | * Sets the progress of the gcam picture taking. |
| 229 | * |
| 230 | * @param percent amount of process done in percent 0-100. |
| 231 | */ |
| 232 | public void setPictureTakingProgress(int percent) { |
| 233 | mProgressOverlay.setProgress(percent); |
| 234 | } |
| 235 | |
| 236 | public Bitmap getBitMapFromPreview() { |
| 237 | Matrix m = new Matrix(); |
| 238 | m = getPreviewTransform(m); |
| 239 | Bitmap src = mPreviewView.getBitmap(); |
| 240 | return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true); |
| 241 | } |
Andy Huibers | 26b3334 | 2014-08-20 00:02:19 -0700 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Enables zoom UI, setting maximum zoom. |
| 245 | * Called from Module when camera is available. |
| 246 | * |
| 247 | * @param maxZoom maximum zoom value. |
| 248 | */ |
| 249 | public void initializeZoom(float maxZoom) { |
| 250 | mMaxZoom = maxZoom; |
| 251 | mPreviewOverlay.setupZoom(mMaxZoom, 0, mZoomChancedListener); |
| 252 | } |
Sascha Haeberling | 08b3c94 | 2014-07-30 17:48:52 -0700 | [diff] [blame] | 253 | } |