TextProgressBar.java revision 3001a035439d8134a7d70d796376d1dfbff3cdcd
1/*
2 * Copyright (C) 2008 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.internal.widget;
18
19import android.content.Context;
20import android.os.SystemClock;
21import android.util.AttributeSet;
22import android.util.Log;
23import android.view.Gravity;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.Chronometer;
27import android.widget.Chronometer.OnChronometerTickListener;
28import android.widget.ProgressBar;
29import android.widget.RelativeLayout;
30import android.widget.RemoteViews.RemoteView;
31
32/**
33 * Container that links together a {@link ProgressBar} and {@link Chronometer}
34 * as children. It subscribes to {@link Chronometer#OnChronometerTickListener}
35 * and updates the {@link ProgressBar} based on a preset finishing time.
36 * <p>
37 * This widget expects to contain two children with specific ids
38 * {@link android.R.id.progress} and {@link android.R.id.text1}.
39 * <p>
40 * If the {@link Chronometer} {@link android.R.attr#layout_width} is
41 * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, then the
42 * {@link android.R.attr#gravity} will be used to automatically move it with
43 * respect to the {@link ProgressBar} position. For example, if
44 * {@link android.view.Gravity#LEFT} then the {@link Chronometer} will be placed
45 * just ahead of the leading edge of the {@link ProgressBar} position.
46 */
47@RemoteView
48public class TextProgressBar extends RelativeLayout implements OnChronometerTickListener {
49    public static final String TAG = "TextProgressBar";
50
51    static final int CHRONOMETER_ID = android.R.id.text1;
52    static final int PROGRESSBAR_ID = android.R.id.progress;
53
54    Chronometer mChronometer = null;
55    ProgressBar mProgressBar = null;
56
57    long mDurationBase = -1;
58    int mDuration = -1;
59
60    boolean mChronometerFollow = false;
61    int mChronometerGravity = Gravity.NO_GRAVITY;
62
63    public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
64        super(context, attrs, defStyle);
65    }
66
67    public TextProgressBar(Context context, AttributeSet attrs) {
68        super(context, attrs);
69    }
70
71    public TextProgressBar(Context context) {
72        super(context);
73    }
74
75    /**
76     * Catch any interesting children when they are added.
77     */
78    @Override
79    public void addView(View child, int index, ViewGroup.LayoutParams params) {
80        super.addView(child, index, params);
81
82        int childId = child.getId();
83        if (childId == CHRONOMETER_ID && child instanceof Chronometer) {
84            mChronometer = (Chronometer) child;
85            mChronometer.setOnChronometerTickListener(this);
86
87            // Check if Chronometer should move with with ProgressBar
88            mChronometerFollow = (params.width == ViewGroup.LayoutParams.WRAP_CONTENT);
89            mChronometerGravity = (mChronometer.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK);
90
91        } else if (childId == PROGRESSBAR_ID && child instanceof ProgressBar) {
92            mProgressBar = (ProgressBar) child;
93        }
94    }
95
96    /**
97     * Set the expected termination time of the running {@link Chronometer}.
98     * This value is used to adjust the {@link ProgressBar} against the elapsed
99     * time.
100     * <p>
101     * Call this <b>after</b> adjusting the {@link Chronometer} base, if
102     * necessary.
103     *
104     * @param durationBase Use the {@link SystemClock#elapsedRealtime} time
105     *            base.
106     */
107    public void setDurationBase(long durationBase) {
108        mDurationBase = durationBase;
109
110        if (mProgressBar == null || mChronometer == null) {
111            throw new RuntimeException("Expecting child ProgressBar with id " +
112                    "'android.R.id.progress' and Chronometer id 'android.R.id.text1'");
113        }
114
115        // Update the ProgressBar maximum relative to Chronometer base
116        mDuration = (int) (durationBase - mChronometer.getBase());
117        mProgressBar.setMax(mDuration);
118
119    }
120
121    /**
122     * Callback when {@link Chronometer} changes, indicating that we should
123     * update the {@link ProgressBar} and change the layout if necessary.
124     */
125    public void onChronometerTick(Chronometer chronometer) {
126        if (mProgressBar == null) {
127            throw new RuntimeException(
128                "Expecting child ProgressBar with id 'android.R.id.progress'");
129        }
130
131        // Stop Chronometer if we're past duration
132        long now = SystemClock.elapsedRealtime();
133        if (now >= mDurationBase) {
134            mChronometer.stop();
135        }
136
137        // Update the ProgressBar status
138        int remaining = (int) (mDurationBase - now);
139        mProgressBar.setProgress(mDuration - remaining);
140
141        // Move the Chronometer if gravity is set correctly
142        if (mChronometerFollow) {
143            RelativeLayout.LayoutParams params;
144
145            // Calculate estimate of ProgressBar leading edge position
146            params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams();
147            int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin);
148            int leadingEdge = ((contentWidth * mProgressBar.getProgress()) /
149                    mProgressBar.getMax()) + params.leftMargin;
150
151            // Calculate any adjustment based on gravity
152            int adjustLeft = 0;
153            int textWidth = mChronometer.getWidth();
154            if (mChronometerGravity == Gravity.RIGHT) {
155                adjustLeft = -textWidth;
156            } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) {
157                adjustLeft = -(textWidth / 2);
158            }
159
160            // Limit margin to keep text inside ProgressBar bounds
161            leadingEdge += adjustLeft;
162            int rightLimit = contentWidth - params.rightMargin - textWidth;
163            if (leadingEdge < params.leftMargin) {
164                leadingEdge = params.leftMargin;
165            } else if (leadingEdge > rightLimit) {
166                leadingEdge = rightLimit;
167            }
168
169            params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams();
170            params.leftMargin = leadingEdge;
171
172            // Request layout to move Chronometer
173            mChronometer.requestLayout();
174
175        }
176    }
177}
178