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 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() & 90 Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK); 91 92 } else if (childId == PROGRESSBAR_ID && child instanceof ProgressBar) { 93 mProgressBar = (ProgressBar) child; 94 } 95 } 96 97 /** 98 * Set the expected termination time of the running {@link Chronometer}. 99 * This value is used to adjust the {@link ProgressBar} against the elapsed 100 * time. 101 * <p> 102 * Call this <b>after</b> adjusting the {@link Chronometer} base, if 103 * necessary. 104 * 105 * @param durationBase Use the {@link SystemClock#elapsedRealtime} time 106 * base. 107 */ 108 @android.view.RemotableViewMethod 109 public void setDurationBase(long durationBase) { 110 mDurationBase = durationBase; 111 112 if (mProgressBar == null || mChronometer == null) { 113 throw new RuntimeException("Expecting child ProgressBar with id " + 114 "'android.R.id.progress' and Chronometer id 'android.R.id.text1'"); 115 } 116 117 // Update the ProgressBar maximum relative to Chronometer base 118 mDuration = (int) (durationBase - mChronometer.getBase()); 119 if (mDuration <= 0) { 120 mDuration = 1; 121 } 122 mProgressBar.setMax(mDuration); 123 } 124 125 /** 126 * Callback when {@link Chronometer} changes, indicating that we should 127 * update the {@link ProgressBar} and change the layout if necessary. 128 */ 129 public void onChronometerTick(Chronometer chronometer) { 130 if (mProgressBar == null) { 131 throw new RuntimeException( 132 "Expecting child ProgressBar with id 'android.R.id.progress'"); 133 } 134 135 // Stop Chronometer if we're past duration 136 long now = SystemClock.elapsedRealtime(); 137 if (now >= mDurationBase) { 138 mChronometer.stop(); 139 } 140 141 // Update the ProgressBar status 142 int remaining = (int) (mDurationBase - now); 143 mProgressBar.setProgress(mDuration - remaining); 144 145 // Move the Chronometer if gravity is set correctly 146 if (mChronometerFollow) { 147 RelativeLayout.LayoutParams params; 148 149 // Calculate estimate of ProgressBar leading edge position 150 params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams(); 151 int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin); 152 int leadingEdge = ((contentWidth * mProgressBar.getProgress()) / 153 mProgressBar.getMax()) + params.leftMargin; 154 155 // Calculate any adjustment based on gravity 156 int adjustLeft = 0; 157 int textWidth = mChronometer.getWidth(); 158 if (mChronometerGravity == Gravity.RIGHT) { 159 adjustLeft = -textWidth; 160 } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) { 161 adjustLeft = -(textWidth / 2); 162 } 163 164 // Limit margin to keep text inside ProgressBar bounds 165 leadingEdge += adjustLeft; 166 int rightLimit = contentWidth - params.rightMargin - textWidth; 167 if (leadingEdge < params.leftMargin) { 168 leadingEdge = params.leftMargin; 169 } else if (leadingEdge > rightLimit) { 170 leadingEdge = rightLimit; 171 } 172 173 params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams(); 174 params.leftMargin = leadingEdge; 175 176 // Request layout to move Chronometer 177 mChronometer.requestLayout(); 178 179 } 180 } 181 } 182