summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/BaseUI.java
blob: 4dad158d13e15e2c1f7e659050cb1eeec7e63334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.camera;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;

import com.android.camera.ui.CameraControls;
import com.android.camera.ui.CaptureAnimationOverlay;
import com.android.camera.ui.ModuleSwitcher;
import com.android.camera.ui.RecordingTime;
import com.android.camera.util.CameraUtil;

import org.codeaurora.snapcam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * we can start accumulating common code between UI classes here
 * toward an eventual unification - WF
 */
public abstract class BaseUI {

    private static final String TAG = "CAM_" + BaseUI.class.getSimpleName();

    protected final CaptureAnimationOverlay mCaptureOverlay;
    protected final View mPreviewCover;

    protected final CameraActivity mActivity;
    protected final ViewGroup mRootView;

    protected final CameraControls mCameraControls;
    protected final RecordingTime mRecordingTime;

    protected int mTopMargin = 0;
    protected int mBottomMargin = 0;
    protected int mScreenRatio = CameraUtil.RATIO_UNKNOWN;
    protected int mOrientation = 0;

    private boolean mOverlaysDisabled;
    private float mPreviewCoverAlpha;

    private final List<View> mDisabledViews = new ArrayList<>();

    public BaseUI(CameraActivity activity, ViewGroup rootView, int layout) {
        mActivity = activity;
        mRootView = rootView;

        mActivity.getLayoutInflater().inflate(layout, mRootView, true);

        mCameraControls = (CameraControls) mRootView.findViewById(R.id.camera_controls);
        mCaptureOverlay = (CaptureAnimationOverlay) mRootView.findViewById(R.id.capture_overlay);
        mPreviewCover = mRootView.findViewById(R.id.preview_cover);
        mRecordingTime = (RecordingTime) mRootView.findViewById(R.id.recording_time);

        Point size = new Point();
        mActivity.getWindowManager().getDefaultDisplay().getRealSize(size);
        mScreenRatio = CameraUtil.determineRatio(size.x, size.y);

        Pair<Integer, Integer> margins = CameraUtil.calculateMargins(mActivity);
        mTopMargin = margins.first;
        mBottomMargin = margins.second;
    }

    public void onPreviewFocusChanged(boolean previewFocused) {
        if (previewFocused) {
            showUI();
        } else {
            hideUI(true);
        }
    }

    public void showPreviewCover() {
        if (mPreviewCover != null) {
            synchronized (mPreviewCover) {
                if (mPreviewCover.getVisibility() != View.VISIBLE) {
                    mPreviewCover.setAlpha(0.0f);
                    mPreviewCover.setVisibility(View.VISIBLE);
                    disableOverlays();
                }
            }
        }
    }

    public void hidePreviewCover() {
        if (mPreviewCover != null) {
            synchronized (mPreviewCover) {
                if (mPreviewCover.getVisibility() != View.GONE) {
                    mPreviewCover.setAlpha(1.0f);
                    mPreviewCover.setVisibility(View.GONE);
                    enableOverlays();
                }
                if (mCameraControls != null) {
                    mCameraControls.reset();
                }
            }
        }
    }

    public void animateControls(float offset) {
        if (mPreviewCover != null) {
            synchronized (mPreviewCover) {
                setPreviewCoverAlpha(offset, false);
                if (mCameraControls != null) {
                    mCameraControls.setUIOffset(offset, true);
                }
            }
        }
    }

    public boolean isPreviewCoverVisible() {
        if (mPreviewCover == null) {
            return false;
        }
        synchronized (mPreviewCover) {
            return mPreviewCover.getVisibility() == View.VISIBLE;
        }
    }

    public void hideUI() {
        hideUI(false);
    }

    protected void hideUI(boolean toBlack) {
        if (mPreviewCover != null) {
            synchronized (mPreviewCover) {
                if (toBlack) {
                    setPreviewCoverAlpha(1.0f, true);
                } else {
                    setPreviewCoverAlpha(0.4f, true);
                }
                if (mCameraControls != null) {
                    mCameraControls.hideUI(toBlack);
                }
            }
        }
    }

    protected void showUI() {
        if (mPreviewCover != null) {
            synchronized (mPreviewCover) {
                setPreviewCoverAlpha(0.0f, true);
                if (mCameraControls != null) {
                    mCameraControls.showUI();
                }
            }
        }
    }

    public boolean arePreviewControlsVisible() {
        return mCameraControls != null && mCameraControls.arePreviewControlsVisible();
    }

    public void hideSwitcher() {
        if (mCameraControls != null) {
            mCameraControls.hideSwitcher();
        }
    }

    public void showSwitcher() {
        if (mCameraControls != null) {
            mCameraControls.showSwitcher();
        }
    }

    public void removeControlView(View v) {
        if (mCameraControls != null) {
            mCameraControls.removeFromViewList(v);
        }
    }

    public void setSwitcherIndex() {
        int module = ModuleSwitcher.PHOTO_MODULE_INDEX;
        if (this instanceof WideAnglePanoramaUI) {
            module = ModuleSwitcher.WIDE_ANGLE_PANO_MODULE_INDEX;
        } else if (this instanceof VideoUI) {
            module = ModuleSwitcher.VIDEO_MODULE_INDEX;
        } else if (this instanceof CaptureUI) {
            module = ModuleSwitcher.CAPTURE_MODULE_INDEX;
        }
        mCameraControls.setModuleIndex(module);
    }

    public void animateFlash(boolean shortFlash) {
        if (mCaptureOverlay != null) {
            mCaptureOverlay.startFlashAnimation(shortFlash);
        }
        mActivity.updateThumbnail(true);
    }

    public void animateFlash(Bitmap bitmap) {
        if (mCaptureOverlay != null) {
            mCaptureOverlay.startFlashAnimation(false);
        }
        mActivity.updateThumbnail(bitmap);
    }

    protected void onPreviewRectChanged(RectF rect) {
        CameraUtil.dumpRect(rect, "onPreviewRectChanged");

        if (mCaptureOverlay != null) {
            mCaptureOverlay.setPreviewRect(rect);
        }
        if (mCameraControls != null) {
            mCameraControls.setPreviewRect(rect);
        }
    }

    private void enableOverlays() {
        synchronized (mDisabledViews) {
            if (!mOverlaysDisabled) {
                return;
            }

            for (View v : mDisabledViews) {
                v.setVisibility(View.VISIBLE);
            }

            mDisabledViews.clear();
            mOverlaysDisabled = false;
        }
    }

    private void disableOverlays() {
        synchronized (mDisabledViews) {
            if (mOverlaysDisabled) {
                return;
            }

            mOverlaysDisabled = true;

            View focusRingView = mRootView.findViewById(R.id.focus_ring);
            if (focusRingView != null && focusRingView.getVisibility() == View.VISIBLE) {
                focusRingView.setVisibility(View.GONE);
                mDisabledViews.add(focusRingView);
            }

            View faceView = mRootView.findViewById(R.id.face_view);
            if (faceView != null && faceView.getVisibility() == View.VISIBLE) {
                faceView.setVisibility(View.GONE);
                mDisabledViews.add(faceView);
            }
        }
    }

    public void setOrientation(int orientation, boolean animation) {
        mOrientation = orientation;

        if (mCameraControls != null) {
            mCameraControls.setOrientation(orientation, animation);
        }
        if (mRecordingTime != null) {
            mRecordingTime.setOrientation(orientation);
        }
    }

    public void startRecordingTimer(int frameRate, long frameInterval, long durationMs) {
        if (mRecordingTime != null) {
            mRecordingTime.start(frameRate, frameInterval, durationMs);
        }
    }

    public void stopRecordingTimer() {
        if (mRecordingTime != null) {
            mRecordingTime.stop();
        }
    }

    public long getRecordingTime() {
        if (mRecordingTime != null) {
            return mRecordingTime.getTime();
        }
        return -1;
    }

    public void showTimeLapseUI(boolean enable) {
        if (mRecordingTime != null) {
            mRecordingTime.showTimeLapse(enable);
        }
    }

    private void setPreviewCoverAlpha(float alpha, boolean animate) {
        if (mPreviewCover == null) {
            return;
        }
        synchronized (mPreviewCover) {
            if (alpha == mPreviewCoverAlpha || alpha < 0.0f || alpha > 1.0f) {
                return;
            }

            if (alpha == 0.0f) {
                hidePreviewCover();
            } else {
                showPreviewCover();
            }

            if (animate) {
                mPreviewCover.animate().cancel();
                mPreviewCover.animate().alpha(alpha).start();
            } else {
                mPreviewCover.setAlpha(alpha);
            }

            mPreviewCoverAlpha = alpha;
        }
    }
}