1/*
2 * Copyright (C) 2010 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.videoeditor.widgets;
18
19import com.android.videoeditor.R;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.graphics.Canvas;
26import android.graphics.Rect;
27
28/**
29 * Draw a progress bar for media items, transitions and the audio track
30 */
31class ProgressBar {
32    // The one
33    private static ProgressBar mProgressBar;
34
35    // Instance variables
36    private final Bitmap mProgressLeftBitmap;
37    private final Bitmap mProgressRightBitmap;
38    private final Rect mProgressSrcRect;
39
40    /**
41     * Get the progress bar singleton
42     *
43     * @param context The context
44     *
45     * @return The progress bar
46     */
47    public static ProgressBar getProgressBar(Context context) {
48        if (mProgressBar == null) {
49            mProgressBar = new ProgressBar(context);
50        }
51
52        return mProgressBar;
53    }
54
55    /**
56     * Constructor
57     *
58     * @param context The context
59     */
60    private ProgressBar(Context context) {
61        final Resources resources = context.getResources();
62
63        // Prepare the progress bitmap
64        mProgressLeftBitmap = BitmapFactory.decodeResource(resources,
65                R.drawable.item_progress_left);
66        mProgressRightBitmap = BitmapFactory.decodeResource(resources,
67                R.drawable.item_progress_right);
68
69        mProgressSrcRect = new Rect(0, 0, mProgressLeftBitmap.getWidth(),
70                mProgressLeftBitmap.getHeight());
71    }
72
73    /**
74     * @return The height
75     */
76    public int getHeight() {
77        return mProgressLeftBitmap.getHeight();
78    }
79
80    /**
81     * Draw the progress bar
82     *
83     * @param canvas The canvas
84     * @param progress The progress (between 0 and 100)
85     * @param dest The destination rectangle
86     * @param left The left offset
87     * @param width The width
88     */
89    public void draw(Canvas canvas, int progress, Rect dest, int left, int width) {
90        switch (progress) {
91            case 0: {
92                dest.left = left;
93                dest.right = width;
94                canvas.drawBitmap(mProgressRightBitmap, mProgressSrcRect, dest, null);
95                break;
96            }
97
98            case 100: {
99                dest.left = left;
100                dest.right = width;
101                canvas.drawBitmap(mProgressLeftBitmap, mProgressSrcRect, dest, null);
102                break;
103            }
104
105            default: {
106                dest.right = ((width - left) * progress) / 100;
107
108                // Draw progress
109                if (progress > 0) {
110                    dest.left = left;
111                    canvas.drawBitmap(mProgressLeftBitmap, mProgressSrcRect, dest, null);
112                }
113
114                if (progress < 100) {
115                    dest.left = dest.right;
116                    dest.right = width;
117                    canvas.drawBitmap(mProgressRightBitmap, mProgressSrcRect, dest, null);
118                }
119
120                break;
121            }
122        }
123    }
124}
125