• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.internal.widget;
18 
19 import android.content.Context;
20 import android.os.SystemClock;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.Chronometer;
27 import android.widget.Chronometer.OnChronometerTickListener;
28 import android.widget.ProgressBar;
29 import android.widget.RelativeLayout;
30 import 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
48 public 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 
TextProgressBar(Context context, AttributeSet attrs, int defStyle)63     public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {
64         super(context, attrs, defStyle);
65     }
66 
TextProgressBar(Context context, AttributeSet attrs)67     public TextProgressBar(Context context, AttributeSet attrs) {
68         super(context, attrs);
69     }
70 
TextProgressBar(Context context)71     public TextProgressBar(Context context) {
72         super(context);
73     }
74 
75     /**
76      * Catch any interesting children when they are added.
77      */
78     @Override
addView(View child, int index, ViewGroup.LayoutParams params)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     @android.view.RemotableViewMethod
setDurationBase(long durationBase)108     public void setDurationBase(long durationBase) {
109         mDurationBase = durationBase;
110 
111         if (mProgressBar == null || mChronometer == null) {
112             throw new RuntimeException("Expecting child ProgressBar with id " +
113                     "'android.R.id.progress' and Chronometer id 'android.R.id.text1'");
114         }
115 
116         // Update the ProgressBar maximum relative to Chronometer base
117         mDuration = (int) (durationBase - mChronometer.getBase());
118         if (mDuration <= 0) {
119             mDuration = 1;
120         }
121         mProgressBar.setMax(mDuration);
122     }
123 
124     /**
125      * Callback when {@link Chronometer} changes, indicating that we should
126      * update the {@link ProgressBar} and change the layout if necessary.
127      */
onChronometerTick(Chronometer chronometer)128     public void onChronometerTick(Chronometer chronometer) {
129         if (mProgressBar == null) {
130             throw new RuntimeException(
131                 "Expecting child ProgressBar with id 'android.R.id.progress'");
132         }
133 
134         // Stop Chronometer if we're past duration
135         long now = SystemClock.elapsedRealtime();
136         if (now >= mDurationBase) {
137             mChronometer.stop();
138         }
139 
140         // Update the ProgressBar status
141         int remaining = (int) (mDurationBase - now);
142         mProgressBar.setProgress(mDuration - remaining);
143 
144         // Move the Chronometer if gravity is set correctly
145         if (mChronometerFollow) {
146             RelativeLayout.LayoutParams params;
147 
148             // Calculate estimate of ProgressBar leading edge position
149             params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams();
150             int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin);
151             int leadingEdge = ((contentWidth * mProgressBar.getProgress()) /
152                     mProgressBar.getMax()) + params.leftMargin;
153 
154             // Calculate any adjustment based on gravity
155             int adjustLeft = 0;
156             int textWidth = mChronometer.getWidth();
157             if (mChronometerGravity == Gravity.RIGHT) {
158                 adjustLeft = -textWidth;
159             } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) {
160                 adjustLeft = -(textWidth / 2);
161             }
162 
163             // Limit margin to keep text inside ProgressBar bounds
164             leadingEdge += adjustLeft;
165             int rightLimit = contentWidth - params.rightMargin - textWidth;
166             if (leadingEdge < params.leftMargin) {
167                 leadingEdge = params.leftMargin;
168             } else if (leadingEdge > rightLimit) {
169                 leadingEdge = rightLimit;
170             }
171 
172             params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams();
173             params.leftMargin = leadingEdge;
174 
175             // Request layout to move Chronometer
176             mChronometer.requestLayout();
177 
178         }
179     }
180 }
181