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
17package com.android.camera;
18
19import android.graphics.Bitmap;
20import android.graphics.Matrix;
21import android.graphics.RectF;
22import android.view.TextureView;
23import android.view.View;
24import android.view.ViewGroup;
25
26import com.android.camera.debug.Log;
27import com.android.camera.ui.CountDownView;
28import com.android.camera.ui.PreviewOverlay;
29import com.android.camera.ui.PreviewOverlay.OnZoomChangedListener;
30import com.android.camera.ui.PreviewStatusListener;
31import com.android.camera.ui.ProgressOverlay;
32import com.android.camera.ui.focus.FocusRing;
33import com.android.camera2.R;
34
35/**
36 * Contains the UI for the CaptureModule.
37 */
38public class CaptureModuleUI implements PreviewStatusListener.PreviewAreaChangedListener {
39
40    public interface CaptureModuleUIListener {
41        public void onZoomRatioChanged(float zoomRatio);
42    }
43
44    private static final Log.Tag TAG = new Log.Tag("CaptureModuleUI");
45
46    private final CameraActivity mActivity;
47    private final CaptureModuleUIListener mListener;
48    private final View mRootView;
49
50    private final PreviewOverlay mPreviewOverlay;
51    private final ProgressOverlay mProgressOverlay;
52    private final TextureView mPreviewView;
53
54    private final FocusRing mFocusRing;
55    private final CountDownView mCountdownView;
56
57    private int mPreviewAreaWidth;
58    private int mPreviewAreaHeight;
59
60    /** Maximum zoom; intialize to 1.0 (disabled) */
61    private float mMaxZoom = 1f;
62
63    /** Set up listener to receive zoom changes from View and send to module. */
64    private final OnZoomChangedListener mZoomChangedListener = new OnZoomChangedListener() {
65        @Override
66        public void onZoomValueChanged(float ratio) {
67            mListener.onZoomRatioChanged(ratio);
68        }
69
70        @Override
71        public void onZoomStart() {
72        }
73
74        @Override
75        public void onZoomEnd() {
76        }
77    };
78
79    public CaptureModuleUI(CameraActivity activity, View parent, CaptureModuleUIListener listener) {
80        mActivity = activity;
81        mListener = listener;
82        mRootView = parent;
83
84        ViewGroup moduleRoot = (ViewGroup) mRootView.findViewById(R.id.module_layout);
85        mActivity.getLayoutInflater().inflate(R.layout.capture_module, moduleRoot, true);
86
87        mPreviewView = (TextureView) mRootView.findViewById(R.id.preview_content);
88
89        mPreviewOverlay = (PreviewOverlay) mRootView.findViewById(R.id.preview_overlay);
90        mProgressOverlay = (ProgressOverlay) mRootView.findViewById(R.id.progress_overlay);
91
92        mFocusRing = (FocusRing) mRootView.findViewById(R.id.focus_ring);
93        mCountdownView = (CountDownView) mRootView.findViewById(R.id.count_down_view);
94    }
95
96    /**
97     * Getter for the width of the visible area of the preview.
98     */
99    public int getPreviewAreaWidth() {
100        return mPreviewAreaWidth;
101    }
102
103    /**
104     * Getter for the height of the visible area of the preview.
105     */
106    public int getPreviewAreaHeight() {
107        return mPreviewAreaHeight;
108    }
109
110    public Matrix getPreviewTransform(Matrix m) {
111        return mPreviewView.getTransform(m);
112    }
113
114    public FocusRing getFocusRing() {
115        return mFocusRing;
116    }
117
118    public void showDebugMessage(String message) {
119        /* NoOp */
120    }
121
122    /**
123     * Starts the countdown timer.
124     *
125     * @param sec seconds to countdown
126     */
127    public void startCountdown(int sec) {
128        mCountdownView.startCountDown(sec);
129    }
130
131    /**
132     * Sets a listener that gets notified when the countdown is finished.
133     */
134    public void setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener) {
135        mCountdownView.setCountDownStatusListener(listener);
136    }
137
138    /**
139     * Returns whether the countdown is on-going.
140     */
141    public boolean isCountingDown() {
142        return mCountdownView.isCountingDown();
143    }
144
145    /**
146     * Cancels the on-going countdown, if any.
147     */
148    public void cancelCountDown() {
149        mCountdownView.cancelCountDown();
150    }
151
152    /**
153     * Sets the progress of the gcam picture taking.
154     *
155     * @param percent amount of process done in percent 0-100.
156     */
157    public void setPictureTakingProgress(int percent) {
158        mProgressOverlay.setProgress(percent);
159    }
160
161    public Bitmap getBitMapFromPreview() {
162        Matrix m = new Matrix();
163        m = getPreviewTransform(m);
164        Bitmap src = mPreviewView.getBitmap();
165        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
166    }
167
168    /**
169     * Enables zoom UI, setting maximum zoom.
170     * Called from Module when camera is available.
171     *
172     * @param maxZoom maximum zoom value.
173     */
174    public void initializeZoom(float maxZoom) {
175        mMaxZoom = maxZoom;
176        mPreviewOverlay.setupZoom(mMaxZoom, 0, mZoomChangedListener);
177    }
178
179    @Override
180    public void onPreviewAreaChanged(RectF previewArea) {
181        // TODO: mFaceView.onPreviewAreaChanged(previewArea);
182        mCountdownView.onPreviewAreaChanged(previewArea);
183        mProgressOverlay.setBounds(previewArea);
184    }
185}
186