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.Color;
22import android.graphics.Paint;
23import android.graphics.RectF;
24
25import com.android.camera2.R;
26
27/**
28 * Renders a circular progress bar on the screen.
29 */
30public class ProgressRenderer {
31
32    public static interface VisibilityListener {
33        public void onHidden();
34    }
35
36    private final int mProgressRadius;
37    private final Paint mProgressBasePaint;
38    private final Paint mProgressPaint;
39
40    private RectF mArcBounds = new RectF(0, 0, 1, 1);
41    private int mProgressAngleDegrees = 270;
42    private boolean mVisible = false;
43    private VisibilityListener mVisibilityListener;
44
45    /**
46     * After we reach 100%, keep on painting the progress for another x milliseconds
47     * before hiding it.
48     */
49    private static final int SHOW_PROGRESS_X_ADDITIONAL_MS = 100;
50
51    /** When to hide the progress indicator. */
52    private long mTimeToHide = 0;
53
54    public ProgressRenderer(Context context) {
55        mProgressRadius = context.getResources().getDimensionPixelSize(R.dimen.pie_progress_radius);
56        int pieProgressWidth = context.getResources().getDimensionPixelSize(
57                R.dimen.pie_progress_width);
58        mProgressBasePaint = createProgressPaint(pieProgressWidth, 0.2f);
59        mProgressPaint = createProgressPaint(pieProgressWidth, 1.0f);
60    }
61
62    /**
63     * Sets or replaces a visiblity listener.
64     */
65    public void setVisibilityListener(VisibilityListener listener) {
66        mVisibilityListener = listener;
67    }
68
69    /**
70     * Shows a progress indicator. If the progress is '100', the progress
71     * indicator will be hidden.
72     *
73     * @param percent the progress in percent (0-100).
74     */
75    public void setProgress(int percent) {
76        // Clamp the value.
77        percent = Math.min(100, Math.max(percent, 0));
78        mProgressAngleDegrees = (int) ((360f / 100) * percent);
79
80        // We hide the progress once we drew the 100% state once.
81        if (percent < 100) {
82            mVisible = true;
83            mTimeToHide = System.currentTimeMillis() + SHOW_PROGRESS_X_ADDITIONAL_MS;
84        }
85    }
86
87    /**
88     * Draw the current progress (if < 100%) centered at the given location.
89     */
90    public void onDraw(Canvas canvas, int centerX, int centerY) {
91        if (!mVisible) {
92            return;
93        }
94        mArcBounds = new RectF(centerX - mProgressRadius, centerY - mProgressRadius, centerX
95                + mProgressRadius,
96                centerY + mProgressRadius);
97
98        canvas.drawCircle(centerX, centerY, mProgressRadius, mProgressBasePaint);
99        canvas.drawArc(mArcBounds, -90, mProgressAngleDegrees, false, mProgressPaint);
100
101        // After we reached 100%, we paint the progress renderer for another x
102        // milliseconds until we hide it.
103        if (mProgressAngleDegrees == 360 && System.currentTimeMillis() > mTimeToHide) {
104            mVisible = false;
105            if (mVisibilityListener != null) {
106                mVisibilityListener.onHidden();
107            }
108        }
109    }
110
111    /**
112     * @return Whether the progress renderer is visible.
113     */
114    public boolean isVisible() {
115        return mVisible;
116    }
117
118    private static Paint createProgressPaint(int width, float alpha) {
119        Paint paint = new Paint();
120        paint.setAntiAlias(true);
121        // 20% alpha.
122        paint.setColor(Color.argb((int) (alpha * 255), 255, 255, 255));
123        paint.setStrokeWidth(width);
124        paint.setStyle(Paint.Style.STROKE);
125        return paint;
126    }
127}
128