1/*
2 * Copyright (C) 2013 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.ui;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.RectF;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.FrameLayout;
25
26/**
27 * ProgressOverlay is a view that sits under the PreviewOverlay.
28 * It does not respond to touch events, and only serves to show a
29 * centered progress bar.
30 */
31public class ProgressOverlay extends View {
32    private final ProgressRenderer mProgressRenderer;
33    private int mCenterX;
34    private int mCenterY;
35
36    /**
37     * Intialize a new ProgressOverlay with a ProgressRenderer.
38     */
39    public ProgressOverlay(Context context, AttributeSet attrs) {
40        super(context, attrs);
41        mProgressRenderer = new ProgressRenderer(context, this);
42    }
43
44    @Override
45    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
46        super.onLayout(changed, left, top, right, bottom);
47        if (changed) {
48            mCenterX = (right - left) / 2;
49            mCenterY = (bottom - top) / 2;
50        }
51    }
52
53    /**
54     * Reposition the view within a given set of bounds, defined by a
55     * {@link android.graphics.RectF}.
56     */
57    public void setBounds(RectF area) {
58        if (area.width() > 0 && area.height() > 0) {
59            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
60            params.width = (int) area.width();
61            params.height= (int) area.height();
62            params.setMargins((int) area.left, (int) area.top, 0, 0);
63            setLayoutParams(params);
64        }
65    }
66
67    @Override
68    public void onDraw(Canvas canvas) {
69        mProgressRenderer.onDraw(canvas, mCenterX, mCenterY);
70    }
71
72    /**
73     * Set the progress state as a percent from 0-100.
74     */
75    public void setProgress(int percent) {
76        mProgressRenderer.setProgress(percent);
77    }
78}