TextProgressBar.java revision 15ab3eae2ec3d73b3e8aa60b33ae41445bf83f4b
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 @android.view.RemotableViewMethod 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 mProgressBar.setMax(mDuration); 119 120 } 121 122 /** 123 * Callback when {@link Chronometer} changes, indicating that we should 124 * update the {@link ProgressBar} and change the layout if necessary. 125 */ 126 public void onChronometerTick(Chronometer chronometer) { 127 if (mProgressBar == null) { 128 throw new RuntimeException( 129 "Expecting child ProgressBar with id 'android.R.id.progress'"); 130 } 131 132 // Stop Chronometer if we're past duration 133 long now = SystemClock.elapsedRealtime(); 134 if (now >= mDurationBase) { 135 mChronometer.stop(); 136 } 137 138 // Update the ProgressBar status 139 int remaining = (int) (mDurationBase - now); 140 mProgressBar.setProgress(mDuration - remaining); 141 142 // Move the Chronometer if gravity is set correctly 143 if (mChronometerFollow) { 144 RelativeLayout.LayoutParams params; 145 146 // Calculate estimate of ProgressBar leading edge position 147 params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams(); 148 int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin); 149 int leadingEdge = ((contentWidth * mProgressBar.getProgress()) / 150 mProgressBar.getMax()) + params.leftMargin; 151 152 // Calculate any adjustment based on gravity 153 int adjustLeft = 0; 154 int textWidth = mChronometer.getWidth(); 155 if (mChronometerGravity == Gravity.RIGHT) { 156 adjustLeft = -textWidth; 157 } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) { 158 adjustLeft = -(textWidth / 2); 159 } 160 161 // Limit margin to keep text inside ProgressBar bounds 162 leadingEdge += adjustLeft; 163 int rightLimit = contentWidth - params.rightMargin - textWidth; 164 if (leadingEdge < params.leftMargin) { 165 leadingEdge = params.leftMargin; 166 } else if (leadingEdge > rightLimit) { 167 leadingEdge = rightLimit; 168 } 169 170 params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams(); 171 params.leftMargin = leadingEdge; 172 173 // Request layout to move Chronometer 174 mChronometer.requestLayout(); 175 176 } 177 } 178} 179