The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.widget; |
| 18 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| 20 | import android.graphics.PorterDuff; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 21 | |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 22 | import com.android.internal.R; |
| 23 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 24 | import android.annotation.InterpolatorRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 26 | import android.content.res.ColorStateList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.BitmapShader; |
| 30 | import android.graphics.Canvas; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.graphics.Rect; |
Steve Howard | b25ffff | 2010-08-20 17:39:26 -0700 | [diff] [blame] | 32 | import android.graphics.Shader; |
| 33 | import android.graphics.drawable.Animatable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.graphics.drawable.AnimationDrawable; |
| 35 | import android.graphics.drawable.BitmapDrawable; |
| 36 | import android.graphics.drawable.ClipDrawable; |
| 37 | import android.graphics.drawable.Drawable; |
| 38 | import android.graphics.drawable.LayerDrawable; |
| 39 | import android.graphics.drawable.ShapeDrawable; |
| 40 | import android.graphics.drawable.StateListDrawable; |
| 41 | import android.graphics.drawable.shapes.RoundRectShape; |
| 42 | import android.graphics.drawable.shapes.Shape; |
Steve Howard | b25ffff | 2010-08-20 17:39:26 -0700 | [diff] [blame] | 43 | import android.os.Parcel; |
| 44 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.AttributeSet; |
Alan Viverette | 12a4491 | 2015-04-16 13:06:00 -0700 | [diff] [blame] | 46 | import android.util.MathUtils; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 47 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.view.Gravity; |
Steve Howard | b25ffff | 2010-08-20 17:39:26 -0700 | [diff] [blame] | 49 | import android.view.RemotableViewMethod; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.view.View; |
Steve Zeigler | 7a36788 | 2010-02-23 16:39:08 -0800 | [diff] [blame] | 51 | import android.view.ViewDebug; |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 52 | import android.view.accessibility.AccessibilityEvent; |
| 53 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.view.animation.AlphaAnimation; |
| 55 | import android.view.animation.Animation; |
| 56 | import android.view.animation.AnimationUtils; |
| 57 | import android.view.animation.Interpolator; |
| 58 | import android.view.animation.LinearInterpolator; |
| 59 | import android.view.animation.Transformation; |
| 60 | import android.widget.RemoteViews.RemoteView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 62 | import java.util.ArrayList; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
| 65 | * <p> |
| 66 | * Visual indicator of progress in some operation. Displays a bar to the user |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 67 | * representing how far the operation has progressed; the application can |
| 68 | * change the amount of progress (modifying the length of the bar) as it moves |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * forward. There is also a secondary progress displayable on a progress bar |
| 70 | * which is useful for displaying intermediate progress, such as the buffer |
| 71 | * level during a streaming playback progress bar. |
| 72 | * </p> |
| 73 | * |
| 74 | * <p> |
| 75 | * A progress bar can also be made indeterminate. In indeterminate mode, the |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 76 | * progress bar shows a cyclic animation without an indication of progress. This mode is used by |
| 77 | * applications when the length of the task is unknown. The indeterminate progress bar can be either |
| 78 | * a spinning wheel or a horizontal bar. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * </p> |
| 80 | * |
| 81 | * <p>The following code example shows how a progress bar can be used from |
| 82 | * a worker thread to update the user interface to notify the user of progress: |
| 83 | * </p> |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 84 | * |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 85 | * <pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | * public class MyActivity extends Activity { |
| 87 | * private static final int PROGRESS = 0x1; |
| 88 | * |
| 89 | * private ProgressBar mProgress; |
| 90 | * private int mProgressStatus = 0; |
| 91 | * |
| 92 | * private Handler mHandler = new Handler(); |
| 93 | * |
| 94 | * protected void onCreate(Bundle icicle) { |
| 95 | * super.onCreate(icicle); |
| 96 | * |
| 97 | * setContentView(R.layout.progressbar_activity); |
| 98 | * |
| 99 | * mProgress = (ProgressBar) findViewById(R.id.progress_bar); |
| 100 | * |
| 101 | * // Start lengthy operation in a background thread |
| 102 | * new Thread(new Runnable() { |
| 103 | * public void run() { |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 104 | * while (mProgressStatus < 100) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | * mProgressStatus = doWork(); |
| 106 | * |
| 107 | * // Update the progress bar |
| 108 | * mHandler.post(new Runnable() { |
| 109 | * public void run() { |
| 110 | * mProgress.setProgress(mProgressStatus); |
| 111 | * } |
| 112 | * }); |
| 113 | * } |
| 114 | * } |
| 115 | * }).start(); |
| 116 | * } |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 117 | * }</pre> |
| 118 | * |
| 119 | * <p>To add a progress bar to a layout file, you can use the {@code <ProgressBar>} element. |
| 120 | * By default, the progress bar is a spinning wheel (an indeterminate indicator). To change to a |
| 121 | * horizontal progress bar, apply the {@link android.R.style#Widget_ProgressBar_Horizontal |
| 122 | * Widget.ProgressBar.Horizontal} style, like so:</p> |
| 123 | * |
| 124 | * <pre> |
| 125 | * <ProgressBar |
| 126 | * style="@android:style/Widget.ProgressBar.Horizontal" |
| 127 | * ... /></pre> |
| 128 | * |
| 129 | * <p>If you will use the progress bar to show real progress, you must use the horizontal bar. You |
| 130 | * can then increment the progress with {@link #incrementProgressBy incrementProgressBy()} or |
| 131 | * {@link #setProgress setProgress()}. By default, the progress bar is full when it reaches 100. If |
| 132 | * necessary, you can adjust the maximum value (the value for a full bar) using the {@link |
| 133 | * android.R.styleable#ProgressBar_max android:max} attribute. Other attributes available are listed |
| 134 | * below.</p> |
| 135 | * |
| 136 | * <p>Another common style to apply to the progress bar is {@link |
| 137 | * android.R.style#Widget_ProgressBar_Small Widget.ProgressBar.Small}, which shows a smaller |
| 138 | * version of the spinning wheel—useful when waiting for content to load. |
| 139 | * For example, you can insert this kind of progress bar into your default layout for |
| 140 | * a view that will be populated by some content fetched from the Internet—the spinning wheel |
| 141 | * appears immediately and when your application receives the content, it replaces the progress bar |
| 142 | * with the loaded content. For example:</p> |
| 143 | * |
| 144 | * <pre> |
| 145 | * <LinearLayout |
| 146 | * android:orientation="horizontal" |
| 147 | * ... > |
| 148 | * <ProgressBar |
| 149 | * android:layout_width="wrap_content" |
| 150 | * android:layout_height="wrap_content" |
| 151 | * style="@android:style/Widget.ProgressBar.Small" |
| 152 | * android:layout_marginRight="5dp" /> |
| 153 | * <TextView |
| 154 | * android:layout_width="wrap_content" |
| 155 | * android:layout_height="wrap_content" |
| 156 | * android:text="@string/loading" /> |
| 157 | * </LinearLayout></pre> |
| 158 | * |
| 159 | * <p>Other progress bar styles provided by the system include:</p> |
| 160 | * <ul> |
| 161 | * <li>{@link android.R.style#Widget_ProgressBar_Horizontal Widget.ProgressBar.Horizontal}</li> |
| 162 | * <li>{@link android.R.style#Widget_ProgressBar_Small Widget.ProgressBar.Small}</li> |
| 163 | * <li>{@link android.R.style#Widget_ProgressBar_Large Widget.ProgressBar.Large}</li> |
| 164 | * <li>{@link android.R.style#Widget_ProgressBar_Inverse Widget.ProgressBar.Inverse}</li> |
| 165 | * <li>{@link android.R.style#Widget_ProgressBar_Small_Inverse |
| 166 | * Widget.ProgressBar.Small.Inverse}</li> |
| 167 | * <li>{@link android.R.style#Widget_ProgressBar_Large_Inverse |
| 168 | * Widget.ProgressBar.Large.Inverse}</li> |
| 169 | * </ul> |
| 170 | * <p>The "inverse" styles provide an inverse color scheme for the spinner, which may be necessary |
| 171 | * if your application uses a light colored theme (a white background).</p> |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 172 | * |
| 173 | * <p><strong>XML attributes</b></strong> |
| 174 | * <p> |
| 175 | * See {@link android.R.styleable#ProgressBar ProgressBar Attributes}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | * {@link android.R.styleable#View View Attributes} |
| 177 | * </p> |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 178 | * |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 179 | * @attr ref android.R.styleable#ProgressBar_animationResolution |
| 180 | * @attr ref android.R.styleable#ProgressBar_indeterminate |
| 181 | * @attr ref android.R.styleable#ProgressBar_indeterminateBehavior |
| 182 | * @attr ref android.R.styleable#ProgressBar_indeterminateDrawable |
| 183 | * @attr ref android.R.styleable#ProgressBar_indeterminateDuration |
| 184 | * @attr ref android.R.styleable#ProgressBar_indeterminateOnly |
| 185 | * @attr ref android.R.styleable#ProgressBar_interpolator |
| 186 | * @attr ref android.R.styleable#ProgressBar_max |
| 187 | * @attr ref android.R.styleable#ProgressBar_maxHeight |
| 188 | * @attr ref android.R.styleable#ProgressBar_maxWidth |
| 189 | * @attr ref android.R.styleable#ProgressBar_minHeight |
| 190 | * @attr ref android.R.styleable#ProgressBar_minWidth |
Scott Main | b40c1fd | 2013-04-23 13:30:06 -0700 | [diff] [blame] | 191 | * @attr ref android.R.styleable#ProgressBar_mirrorForRtl |
Scott Main | 42f139c | 2011-04-29 10:31:10 -0700 | [diff] [blame] | 192 | * @attr ref android.R.styleable#ProgressBar_progress |
| 193 | * @attr ref android.R.styleable#ProgressBar_progressDrawable |
| 194 | * @attr ref android.R.styleable#ProgressBar_secondaryProgress |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | */ |
| 196 | @RemoteView |
| 197 | public class ProgressBar extends View { |
| 198 | private static final int MAX_LEVEL = 10000; |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 199 | private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
| 201 | int mMinWidth; |
| 202 | int mMaxWidth; |
| 203 | int mMinHeight; |
| 204 | int mMaxHeight; |
| 205 | |
| 206 | private int mProgress; |
| 207 | private int mSecondaryProgress; |
| 208 | private int mMax; |
| 209 | |
| 210 | private int mBehavior; |
| 211 | private int mDuration; |
| 212 | private boolean mIndeterminate; |
| 213 | private boolean mOnlyIndeterminate; |
| 214 | private Transformation mTransformation; |
| 215 | private AlphaAnimation mAnimation; |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 216 | private boolean mHasAnimation; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | private Drawable mIndeterminateDrawable; |
| 219 | private Drawable mProgressDrawable; |
| 220 | private Drawable mCurrentDrawable; |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 221 | private ProgressTintInfo mProgressTintInfo; |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | Bitmap mSampleTile; |
| 224 | private boolean mNoInvalidate; |
| 225 | private Interpolator mInterpolator; |
| 226 | private RefreshProgressRunnable mRefreshProgressRunnable; |
| 227 | private long mUiThreadId; |
| 228 | private boolean mShouldStartAnimationDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
| 230 | private boolean mInDrawing; |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 231 | private boolean mAttached; |
| 232 | private boolean mRefreshIsPosted; |
| 233 | |
Fabrice Di Meglio | 2b378cd | 2013-01-30 16:39:33 -0800 | [diff] [blame] | 234 | boolean mMirrorForRtl = false; |
| 235 | |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 236 | private final ArrayList<RefreshData> mRefreshData = new ArrayList<RefreshData>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 238 | private AccessibilityEventSender mAccessibilityEventSender; |
| 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Create a new progress bar with range 0...100 and initial progress of 0. |
| 242 | * @param context the application environment |
| 243 | */ |
| 244 | public ProgressBar(Context context) { |
| 245 | this(context, null); |
| 246 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | public ProgressBar(Context context, AttributeSet attrs) { |
| 249 | this(context, attrs, com.android.internal.R.attr.progressBarStyle); |
| 250 | } |
| 251 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 252 | public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { |
| 253 | this(context, attrs, defStyleAttr, 0); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 256 | public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 257 | super(context, attrs, defStyleAttr, defStyleRes); |
| 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | mUiThreadId = Thread.currentThread().getId(); |
| 260 | initProgressBar(); |
| 261 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 262 | final TypedArray a = context.obtainStyledAttributes( |
| 263 | attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | mNoInvalidate = true; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 266 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 267 | final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable); |
| 268 | if (progressDrawable != null) { |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 269 | // Calling setProgressDrawable can set mMaxHeight, so make sure the |
| 270 | // corresponding XML attribute for mMaxHeight is read after calling |
| 271 | // this method. |
| 272 | if (needsTileify(progressDrawable)) { |
| 273 | setProgressDrawableTiled(progressDrawable); |
| 274 | } else { |
| 275 | setProgressDrawable(progressDrawable); |
| 276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | mDuration = a.getInt(R.styleable.ProgressBar_indeterminateDuration, mDuration); |
| 281 | |
| 282 | mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_minWidth, mMinWidth); |
| 283 | mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_maxWidth, mMaxWidth); |
| 284 | mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_minHeight, mMinHeight); |
| 285 | mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_maxHeight, mMaxHeight); |
| 286 | |
| 287 | mBehavior = a.getInt(R.styleable.ProgressBar_indeterminateBehavior, mBehavior); |
| 288 | |
Jean-Baptiste Queru | 72b1f37 | 2009-08-31 09:17:57 -0700 | [diff] [blame] | 289 | final int resID = a.getResourceId( |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 290 | com.android.internal.R.styleable.ProgressBar_interpolator, |
Jean-Baptiste Queru | 72b1f37 | 2009-08-31 09:17:57 -0700 | [diff] [blame] | 291 | android.R.anim.linear_interpolator); // default to linear interpolator |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | if (resID > 0) { |
| 293 | setInterpolator(context, resID); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | |
| 296 | setMax(a.getInt(R.styleable.ProgressBar_max, mMax)); |
| 297 | |
| 298 | setProgress(a.getInt(R.styleable.ProgressBar_progress, mProgress)); |
| 299 | |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 300 | setSecondaryProgress(a.getInt( |
| 301 | R.styleable.ProgressBar_secondaryProgress, mSecondaryProgress)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 303 | final Drawable indeterminateDrawable = a.getDrawable( |
| 304 | R.styleable.ProgressBar_indeterminateDrawable); |
| 305 | if (indeterminateDrawable != null) { |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 306 | if (needsTileify(indeterminateDrawable)) { |
| 307 | setIndeterminateDrawableTiled(indeterminateDrawable); |
| 308 | } else { |
| 309 | setIndeterminateDrawable(indeterminateDrawable); |
| 310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | mOnlyIndeterminate = a.getBoolean( |
| 314 | R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate); |
| 315 | |
| 316 | mNoInvalidate = false; |
| 317 | |
| 318 | setIndeterminate(mOnlyIndeterminate || a.getBoolean( |
| 319 | R.styleable.ProgressBar_indeterminate, mIndeterminate)); |
| 320 | |
Fabrice Di Meglio | 2b378cd | 2013-01-30 16:39:33 -0800 | [diff] [blame] | 321 | mMirrorForRtl = a.getBoolean(R.styleable.ProgressBar_mirrorForRtl, mMirrorForRtl); |
| 322 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 323 | if (a.hasValue(R.styleable.ProgressBar_progressTintMode)) { |
| 324 | if (mProgressTintInfo == null) { |
| 325 | mProgressTintInfo = new ProgressTintInfo(); |
| 326 | } |
| 327 | mProgressTintInfo.mProgressTintMode = Drawable.parseTintMode(a.getInt( |
Alan Viverette | dbc4018 | 2015-04-06 15:01:23 -0700 | [diff] [blame] | 328 | R.styleable.ProgressBar_progressTintMode, -1), null); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 329 | mProgressTintInfo.mHasProgressTintMode = true; |
| 330 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 331 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 332 | if (a.hasValue(R.styleable.ProgressBar_progressTint)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 333 | if (mProgressTintInfo == null) { |
| 334 | mProgressTintInfo = new ProgressTintInfo(); |
| 335 | } |
| 336 | mProgressTintInfo.mProgressTintList = a.getColorStateList( |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 337 | R.styleable.ProgressBar_progressTint); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 338 | mProgressTintInfo.mHasProgressTint = true; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 341 | if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTintMode)) { |
| 342 | if (mProgressTintInfo == null) { |
| 343 | mProgressTintInfo = new ProgressTintInfo(); |
| 344 | } |
| 345 | mProgressTintInfo.mProgressBackgroundTintMode = Drawable.parseTintMode(a.getInt( |
Alan Viverette | dbc4018 | 2015-04-06 15:01:23 -0700 | [diff] [blame] | 346 | R.styleable.ProgressBar_progressBackgroundTintMode, -1), null); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 347 | mProgressTintInfo.mHasProgressBackgroundTintMode = true; |
| 348 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 349 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 350 | if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTint)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 351 | if (mProgressTintInfo == null) { |
| 352 | mProgressTintInfo = new ProgressTintInfo(); |
| 353 | } |
| 354 | mProgressTintInfo.mProgressBackgroundTintList = a.getColorStateList( |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 355 | R.styleable.ProgressBar_progressBackgroundTint); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 356 | mProgressTintInfo.mHasProgressBackgroundTint = true; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 359 | if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTintMode)) { |
| 360 | if (mProgressTintInfo == null) { |
| 361 | mProgressTintInfo = new ProgressTintInfo(); |
| 362 | } |
| 363 | mProgressTintInfo.mSecondaryProgressTintMode = Drawable.parseTintMode( |
| 364 | a.getInt(R.styleable.ProgressBar_secondaryProgressTintMode, -1), null); |
| 365 | mProgressTintInfo.mHasSecondaryProgressTintMode = true; |
| 366 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 367 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 368 | if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTint)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 369 | if (mProgressTintInfo == null) { |
| 370 | mProgressTintInfo = new ProgressTintInfo(); |
| 371 | } |
| 372 | mProgressTintInfo.mSecondaryProgressTintList = a.getColorStateList( |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 373 | R.styleable.ProgressBar_secondaryProgressTint); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 374 | mProgressTintInfo.mHasSecondaryProgressTint = true; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Alan Viverette | dbc4018 | 2015-04-06 15:01:23 -0700 | [diff] [blame] | 377 | if (a.hasValue(R.styleable.ProgressBar_indeterminateTintMode)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 378 | if (mProgressTintInfo == null) { |
| 379 | mProgressTintInfo = new ProgressTintInfo(); |
| 380 | } |
| 381 | mProgressTintInfo.mIndeterminateTintMode = Drawable.parseTintMode(a.getInt( |
| 382 | R.styleable.ProgressBar_indeterminateTintMode, -1), null); |
| 383 | mProgressTintInfo.mHasIndeterminateTintMode = true; |
| 384 | } |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 385 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 386 | if (a.hasValue(R.styleable.ProgressBar_indeterminateTint)) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 387 | if (mProgressTintInfo == null) { |
| 388 | mProgressTintInfo = new ProgressTintInfo(); |
| 389 | } |
| 390 | mProgressTintInfo.mIndeterminateTintList = a.getColorStateList( |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 391 | R.styleable.ProgressBar_indeterminateTint); |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 392 | mProgressTintInfo.mHasIndeterminateTint = true; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 393 | } |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | a.recycle(); |
Svetoslav | 7face75 | 2014-01-13 15:25:58 -0800 | [diff] [blame] | 396 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 397 | applyProgressTints(); |
| 398 | applyIndeterminateTint(); |
| 399 | |
Svetoslav | 7face75 | 2014-01-13 15:25:58 -0800 | [diff] [blame] | 400 | // If not explicitly specified this view is important for accessibility. |
| 401 | if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 402 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 407 | * Returns {@code true} if the target drawable needs to be tileified. |
| 408 | * |
| 409 | * @param dr the drawable to check |
| 410 | * @return {@code true} if the target drawable needs to be tileified, |
| 411 | * {@code false} otherwise |
| 412 | */ |
| 413 | private static boolean needsTileify(Drawable dr) { |
| 414 | if (dr instanceof LayerDrawable) { |
| 415 | final LayerDrawable orig = (LayerDrawable) dr; |
| 416 | final int N = orig.getNumberOfLayers(); |
| 417 | for (int i = 0; i < N; i++) { |
| 418 | if (needsTileify(orig.getDrawable(i))) { |
| 419 | return true; |
| 420 | } |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | if (dr instanceof StateListDrawable) { |
| 426 | final StateListDrawable in = (StateListDrawable) dr; |
| 427 | final int N = in.getStateCount(); |
| 428 | for (int i = 0; i < N; i++) { |
| 429 | if (needsTileify(in.getStateDrawable(i))) { |
| 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // If there's a bitmap that's not wrapped with a ClipDrawable or |
| 437 | // ScaleDrawable, we'll need to wrap it and apply tiling. |
| 438 | if (dr instanceof BitmapDrawable) { |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | * Converts a drawable to a tiled version of itself. It will recursively |
| 447 | * traverse layer and state list drawables. |
| 448 | */ |
| 449 | private Drawable tileify(Drawable drawable, boolean clip) { |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 450 | // TODO: This is a terrible idea that potentially destroys any drawable |
| 451 | // that extends any of these classes. We *really* need to remove this. |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | if (drawable instanceof LayerDrawable) { |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 454 | final LayerDrawable orig = (LayerDrawable) drawable; |
| 455 | final int N = orig.getNumberOfLayers(); |
| 456 | final Drawable[] outDrawables = new Drawable[N]; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | for (int i = 0; i < N; i++) { |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 459 | final int id = orig.getId(i); |
| 460 | outDrawables[i] = tileify(orig.getDrawable(i), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | (id == R.id.progress || id == R.id.secondaryProgress)); |
| 462 | } |
| 463 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 464 | final LayerDrawable clone = new LayerDrawable(outDrawables); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | for (int i = 0; i < N; i++) { |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 466 | clone.setId(i, orig.getId(i)); |
| 467 | clone.setLayerGravity(i, orig.getLayerGravity(i)); |
| 468 | clone.setLayerWidth(i, orig.getLayerWidth(i)); |
| 469 | clone.setLayerHeight(i, orig.getLayerHeight(i)); |
| 470 | clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i)); |
| 471 | clone.setLayerInsetRight(i, orig.getLayerInsetRight(i)); |
| 472 | clone.setLayerInsetTop(i, orig.getLayerInsetTop(i)); |
| 473 | clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i)); |
| 474 | clone.setLayerInsetStart(i, orig.getLayerInsetStart(i)); |
| 475 | clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 477 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 478 | return clone; |
| 479 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 480 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 481 | if (drawable instanceof StateListDrawable) { |
| 482 | final StateListDrawable in = (StateListDrawable) drawable; |
| 483 | final StateListDrawable out = new StateListDrawable(); |
| 484 | final int N = in.getStateCount(); |
| 485 | for (int i = 0; i < N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip)); |
| 487 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 488 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 489 | return out; |
| 490 | } |
| 491 | |
| 492 | if (drawable instanceof BitmapDrawable) { |
Alan Viverette | 813d85b | 2014-02-26 15:38:51 -0800 | [diff] [blame] | 493 | final BitmapDrawable bitmap = (BitmapDrawable) drawable; |
| 494 | final Bitmap tileBitmap = bitmap.getBitmap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | if (mSampleTile == null) { |
| 496 | mSampleTile = tileBitmap; |
| 497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 499 | final BitmapDrawable clone = (BitmapDrawable) bitmap.getConstantState().newDrawable(); |
| 500 | clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | |
Alan Viverette | ff9f7b96 | 2015-04-08 10:49:26 -0700 | [diff] [blame] | 502 | if (clip) { |
| 503 | return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL); |
| 504 | } else { |
| 505 | return clone; |
| 506 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 508 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | return drawable; |
| 510 | } |
| 511 | |
| 512 | Shape getDrawableShape() { |
| 513 | final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }; |
| 514 | return new RoundRectShape(roundedCorners, null, null); |
| 515 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | /** |
| 518 | * Convert a AnimationDrawable for use as a barberpole animation. |
| 519 | * Each frame of the animation is wrapped in a ClipDrawable and |
| 520 | * given a tiling BitmapShader. |
| 521 | */ |
| 522 | private Drawable tileifyIndeterminate(Drawable drawable) { |
| 523 | if (drawable instanceof AnimationDrawable) { |
| 524 | AnimationDrawable background = (AnimationDrawable) drawable; |
| 525 | final int N = background.getNumberOfFrames(); |
| 526 | AnimationDrawable newBg = new AnimationDrawable(); |
| 527 | newBg.setOneShot(background.isOneShot()); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | for (int i = 0; i < N; i++) { |
| 530 | Drawable frame = tileify(background.getFrame(i), true); |
| 531 | frame.setLevel(10000); |
| 532 | newBg.addFrame(frame, background.getDuration(i)); |
| 533 | } |
| 534 | newBg.setLevel(10000); |
| 535 | drawable = newBg; |
| 536 | } |
| 537 | return drawable; |
| 538 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | /** |
| 541 | * <p> |
| 542 | * Initialize the progress bar's default values: |
| 543 | * </p> |
| 544 | * <ul> |
| 545 | * <li>progress = 0</li> |
| 546 | * <li>max = 100</li> |
| 547 | * <li>animation duration = 4000 ms</li> |
| 548 | * <li>indeterminate = false</li> |
| 549 | * <li>behavior = repeat</li> |
| 550 | * </ul> |
| 551 | */ |
| 552 | private void initProgressBar() { |
| 553 | mMax = 100; |
| 554 | mProgress = 0; |
| 555 | mSecondaryProgress = 0; |
| 556 | mIndeterminate = false; |
| 557 | mOnlyIndeterminate = false; |
| 558 | mDuration = 4000; |
| 559 | mBehavior = AlphaAnimation.RESTART; |
| 560 | mMinWidth = 24; |
| 561 | mMaxWidth = 48; |
| 562 | mMinHeight = 24; |
| 563 | mMaxHeight = 48; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * <p>Indicate whether this progress bar is in indeterminate mode.</p> |
| 568 | * |
| 569 | * @return true if the progress bar is in indeterminate mode |
| 570 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 571 | @ViewDebug.ExportedProperty(category = "progress") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | public synchronized boolean isIndeterminate() { |
| 573 | return mIndeterminate; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * <p>Change the indeterminate mode for this progress bar. In indeterminate |
| 578 | * mode, the progress is ignored and the progress bar shows an infinite |
| 579 | * animation instead.</p> |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 580 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | * If this progress bar's style only supports indeterminate mode (such as the circular |
| 582 | * progress bars), then this will be ignored. |
| 583 | * |
| 584 | * @param indeterminate true to enable the indeterminate mode |
| 585 | */ |
| 586 | @android.view.RemotableViewMethod |
| 587 | public synchronized void setIndeterminate(boolean indeterminate) { |
| 588 | if ((!mOnlyIndeterminate || !mIndeterminate) && indeterminate != mIndeterminate) { |
| 589 | mIndeterminate = indeterminate; |
| 590 | |
| 591 | if (indeterminate) { |
| 592 | // swap between indeterminate and regular backgrounds |
| 593 | mCurrentDrawable = mIndeterminateDrawable; |
| 594 | startAnimation(); |
| 595 | } else { |
| 596 | mCurrentDrawable = mProgressDrawable; |
| 597 | stopAnimation(); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * <p>Get the drawable used to draw the progress bar in |
| 604 | * indeterminate mode.</p> |
| 605 | * |
| 606 | * @return a {@link android.graphics.drawable.Drawable} instance |
| 607 | * |
| 608 | * @see #setIndeterminateDrawable(android.graphics.drawable.Drawable) |
| 609 | * @see #setIndeterminate(boolean) |
| 610 | */ |
| 611 | public Drawable getIndeterminateDrawable() { |
| 612 | return mIndeterminateDrawable; |
| 613 | } |
| 614 | |
| 615 | /** |
Alan Viverette | e785d02 | 2013-09-26 15:21:10 -0700 | [diff] [blame] | 616 | * Define the drawable used to draw the progress bar in indeterminate mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | * |
| 618 | * @param d the new drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | * @see #getIndeterminateDrawable() |
| 620 | * @see #setIndeterminate(boolean) |
| 621 | */ |
| 622 | public void setIndeterminateDrawable(Drawable d) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 623 | if (mIndeterminateDrawable != d) { |
| 624 | if (mIndeterminateDrawable != null) { |
| 625 | mIndeterminateDrawable.setCallback(null); |
| 626 | unscheduleDrawable(mIndeterminateDrawable); |
| 627 | } |
| 628 | |
| 629 | mIndeterminateDrawable = d; |
| 630 | |
| 631 | if (d != null) { |
| 632 | d.setCallback(this); |
| 633 | d.setLayoutDirection(getLayoutDirection()); |
| 634 | if (d.isStateful()) { |
| 635 | d.setState(getDrawableState()); |
| 636 | } |
| 637 | applyIndeterminateTint(); |
| 638 | } |
| 639 | |
| 640 | if (mIndeterminate) { |
| 641 | mCurrentDrawable = d; |
| 642 | postInvalidate(); |
| 643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 648 | * Applies a tint to the indeterminate drawable. Does not modify the |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 649 | * current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 650 | * <p> |
| 651 | * Subsequent calls to {@link #setIndeterminateDrawable(Drawable)} will |
| 652 | * automatically mutate the drawable and apply the specified tint and |
| 653 | * tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 654 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 655 | * |
| 656 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 657 | * |
| 658 | * @attr ref android.R.styleable#ProgressBar_indeterminateTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 659 | * @see #getIndeterminateTintList() |
| 660 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 661 | */ |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 662 | @RemotableViewMethod |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 663 | public void setIndeterminateTintList(@Nullable ColorStateList tint) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 664 | if (mProgressTintInfo == null) { |
| 665 | mProgressTintInfo = new ProgressTintInfo(); |
| 666 | } |
| 667 | mProgressTintInfo.mIndeterminateTintList = tint; |
| 668 | mProgressTintInfo.mHasIndeterminateTint = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 669 | |
| 670 | applyIndeterminateTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /** |
| 674 | * @return the tint applied to the indeterminate drawable |
| 675 | * @attr ref android.R.styleable#ProgressBar_indeterminateTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 676 | * @see #setIndeterminateTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 677 | */ |
| 678 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 679 | public ColorStateList getIndeterminateTintList() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 680 | return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateTintList : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 685 | * {@link #setIndeterminateTintList(ColorStateList)} to the indeterminate |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 686 | * drawable. The default mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 687 | * |
| 688 | * @param tintMode the blending mode used to apply the tint, may be |
| 689 | * {@code null} to clear tint |
| 690 | * @attr ref android.R.styleable#ProgressBar_indeterminateTintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 691 | * @see #setIndeterminateTintList(ColorStateList) |
| 692 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 693 | */ |
| 694 | public void setIndeterminateTintMode(@Nullable PorterDuff.Mode tintMode) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 695 | if (mProgressTintInfo == null) { |
| 696 | mProgressTintInfo = new ProgressTintInfo(); |
| 697 | } |
| 698 | mProgressTintInfo.mIndeterminateTintMode = tintMode; |
| 699 | mProgressTintInfo.mHasIndeterminateTintMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 700 | |
| 701 | applyIndeterminateTint(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 705 | * Returns the blending mode used to apply the tint to the indeterminate |
| 706 | * drawable, if specified. |
| 707 | * |
| 708 | * @return the blending mode used to apply the tint to the indeterminate |
| 709 | * drawable |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 710 | * @attr ref android.R.styleable#ProgressBar_indeterminateTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 711 | * @see #setIndeterminateTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 712 | */ |
| 713 | @Nullable |
| 714 | public PorterDuff.Mode getIndeterminateTintMode() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 715 | return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateTintMode : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | private void applyIndeterminateTint() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 719 | if (mIndeterminateDrawable != null && mProgressTintInfo != null) { |
| 720 | final ProgressTintInfo tintInfo = mProgressTintInfo; |
| 721 | if (tintInfo.mHasIndeterminateTint || tintInfo.mHasIndeterminateTintMode) { |
| 722 | mIndeterminateDrawable = mIndeterminateDrawable.mutate(); |
| 723 | |
| 724 | if (tintInfo.mHasIndeterminateTint) { |
| 725 | mIndeterminateDrawable.setTintList(tintInfo.mIndeterminateTintList); |
| 726 | } |
| 727 | |
| 728 | if (tintInfo.mHasIndeterminateTintMode) { |
| 729 | mIndeterminateDrawable.setTintMode(tintInfo.mIndeterminateTintMode); |
| 730 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 731 | |
| 732 | // The drawable (or one of its children) may not have been |
| 733 | // stateful before applying the tint, so let's try again. |
| 734 | if (mIndeterminateDrawable.isStateful()) { |
| 735 | mIndeterminateDrawable.setState(getDrawableState()); |
| 736 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | } |
Alan Viverette | e785d02 | 2013-09-26 15:21:10 -0700 | [diff] [blame] | 740 | |
| 741 | /** |
| 742 | * Define the tileable drawable used to draw the progress bar in |
| 743 | * indeterminate mode. |
| 744 | * <p> |
| 745 | * If the drawable is a BitmapDrawable or contains BitmapDrawables, a |
| 746 | * tiled copy will be generated for display as a progress bar. |
| 747 | * |
| 748 | * @param d the new drawable |
| 749 | * @see #getIndeterminateDrawable() |
| 750 | * @see #setIndeterminate(boolean) |
| 751 | */ |
| 752 | public void setIndeterminateDrawableTiled(Drawable d) { |
| 753 | if (d != null) { |
| 754 | d = tileifyIndeterminate(d); |
| 755 | } |
| 756 | |
| 757 | setIndeterminateDrawable(d); |
| 758 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | /** |
| 761 | * <p>Get the drawable used to draw the progress bar in |
| 762 | * progress mode.</p> |
| 763 | * |
| 764 | * @return a {@link android.graphics.drawable.Drawable} instance |
| 765 | * |
| 766 | * @see #setProgressDrawable(android.graphics.drawable.Drawable) |
| 767 | * @see #setIndeterminate(boolean) |
| 768 | */ |
| 769 | public Drawable getProgressDrawable() { |
| 770 | return mProgressDrawable; |
| 771 | } |
| 772 | |
| 773 | /** |
Alan Viverette | e785d02 | 2013-09-26 15:21:10 -0700 | [diff] [blame] | 774 | * Define the drawable used to draw the progress bar in progress mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | * |
| 776 | * @param d the new drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | * @see #getProgressDrawable() |
| 778 | * @see #setIndeterminate(boolean) |
| 779 | */ |
| 780 | public void setProgressDrawable(Drawable d) { |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 781 | if (mProgressDrawable != d) { |
| 782 | if (mProgressDrawable != null) { |
| 783 | mProgressDrawable.setCallback(null); |
| 784 | unscheduleDrawable(mProgressDrawable); |
| 785 | } |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 786 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 787 | mProgressDrawable = d; |
| 788 | |
| 789 | if (d != null) { |
| 790 | d.setCallback(this); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 791 | d.setLayoutDirection(getLayoutDirection()); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 792 | if (d.isStateful()) { |
| 793 | d.setState(getDrawableState()); |
| 794 | } |
| 795 | |
| 796 | // Make sure the ProgressBar is always tall enough |
| 797 | int drawableHeight = d.getMinimumHeight(); |
| 798 | if (mMaxHeight < drawableHeight) { |
| 799 | mMaxHeight = drawableHeight; |
| 800 | requestLayout(); |
| 801 | } |
| 802 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 803 | applyProgressTints(); |
Fabrice Di Meglio | 0af4b8b | 2012-06-11 18:30:05 -0700 | [diff] [blame] | 804 | } |
NoraBora | a7f7e2a | 2009-12-25 19:51:34 -0500 | [diff] [blame] | 805 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 806 | if (!mIndeterminate) { |
| 807 | mCurrentDrawable = d; |
| 808 | postInvalidate(); |
NoraBora | a7f7e2a | 2009-12-25 19:51:34 -0500 | [diff] [blame] | 809 | } |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 810 | |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 811 | updateDrawableBounds(getWidth(), getHeight()); |
| 812 | updateDrawableState(); |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 813 | |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 814 | doRefreshProgress(R.id.progress, mProgress, false, false); |
| 815 | doRefreshProgress(R.id.secondaryProgress, mSecondaryProgress, false, false); |
| 816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
Alan Viverette | e785d02 | 2013-09-26 15:21:10 -0700 | [diff] [blame] | 818 | |
| 819 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 820 | * Applies the progress tints in order of increasing specificity. |
| 821 | */ |
| 822 | private void applyProgressTints() { |
| 823 | if (mProgressDrawable != null && mProgressTintInfo != null) { |
| 824 | applyPrimaryProgressTint(); |
| 825 | applyProgressBackgroundTint(); |
| 826 | applySecondaryProgressTint(); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Should only be called if we've already verified that mProgressDrawable |
| 832 | * and mProgressTintInfo are non-null. |
| 833 | */ |
| 834 | private void applyPrimaryProgressTint() { |
| 835 | if (mProgressTintInfo.mHasProgressTint |
| 836 | || mProgressTintInfo.mHasProgressTintMode) { |
| 837 | final Drawable target = getTintTarget(R.id.progress, true); |
| 838 | if (target != null) { |
| 839 | if (mProgressTintInfo.mHasProgressTint) { |
| 840 | target.setTintList(mProgressTintInfo.mProgressTintList); |
| 841 | } |
| 842 | if (mProgressTintInfo.mHasProgressTintMode) { |
| 843 | target.setTintMode(mProgressTintInfo.mProgressTintMode); |
| 844 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 845 | |
| 846 | // The drawable (or one of its children) may not have been |
| 847 | // stateful before applying the tint, so let's try again. |
| 848 | if (target.isStateful()) { |
| 849 | target.setState(getDrawableState()); |
| 850 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Should only be called if we've already verified that mProgressDrawable |
| 857 | * and mProgressTintInfo are non-null. |
| 858 | */ |
| 859 | private void applyProgressBackgroundTint() { |
| 860 | if (mProgressTintInfo.mHasProgressBackgroundTint |
| 861 | || mProgressTintInfo.mHasProgressBackgroundTintMode) { |
| 862 | final Drawable target = getTintTarget(R.id.background, false); |
| 863 | if (target != null) { |
| 864 | if (mProgressTintInfo.mHasProgressBackgroundTint) { |
| 865 | target.setTintList(mProgressTintInfo.mProgressBackgroundTintList); |
| 866 | } |
| 867 | if (mProgressTintInfo.mHasProgressBackgroundTintMode) { |
| 868 | target.setTintMode(mProgressTintInfo.mProgressBackgroundTintMode); |
| 869 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 870 | |
| 871 | // The drawable (or one of its children) may not have been |
| 872 | // stateful before applying the tint, so let's try again. |
| 873 | if (target.isStateful()) { |
| 874 | target.setState(getDrawableState()); |
| 875 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * Should only be called if we've already verified that mProgressDrawable |
| 882 | * and mProgressTintInfo are non-null. |
| 883 | */ |
| 884 | private void applySecondaryProgressTint() { |
| 885 | if (mProgressTintInfo.mHasSecondaryProgressTint |
| 886 | || mProgressTintInfo.mHasSecondaryProgressTintMode) { |
| 887 | final Drawable target = getTintTarget(R.id.secondaryProgress, false); |
| 888 | if (target != null) { |
| 889 | if (mProgressTintInfo.mHasSecondaryProgressTint) { |
| 890 | target.setTintList(mProgressTintInfo.mSecondaryProgressTintList); |
| 891 | } |
| 892 | if (mProgressTintInfo.mHasSecondaryProgressTintMode) { |
| 893 | target.setTintMode(mProgressTintInfo.mSecondaryProgressTintMode); |
| 894 | } |
Alan Viverette | d513379 | 2014-10-28 14:41:36 -0700 | [diff] [blame] | 895 | |
| 896 | // The drawable (or one of its children) may not have been |
| 897 | // stateful before applying the tint, so let's try again. |
| 898 | if (target.isStateful()) { |
| 899 | target.setState(getDrawableState()); |
| 900 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 906 | * Applies a tint to the progress indicator, if one exists, or to the |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 907 | * entire progress drawable otherwise. Does not modify the current tint |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 908 | * mode, which is {@link PorterDuff.Mode#SRC_IN} by default. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 909 | * <p> |
| 910 | * The progress indicator should be specified as a layer with |
| 911 | * id {@link android.R.id#progress} in a {@link LayerDrawable} |
| 912 | * used as the progress drawable. |
| 913 | * <p> |
| 914 | * Subsequent calls to {@link #setProgressDrawable(Drawable)} will |
| 915 | * automatically mutate the drawable and apply the specified tint and |
| 916 | * tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 917 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 918 | * |
| 919 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 920 | * |
| 921 | * @attr ref android.R.styleable#ProgressBar_progressTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 922 | * @see #getProgressTintList() |
| 923 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 924 | */ |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 925 | @RemotableViewMethod |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 926 | public void setProgressTintList(@Nullable ColorStateList tint) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 927 | if (mProgressTintInfo == null) { |
| 928 | mProgressTintInfo = new ProgressTintInfo(); |
| 929 | } |
| 930 | mProgressTintInfo.mProgressTintList = tint; |
| 931 | mProgressTintInfo.mHasProgressTint = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 932 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 933 | if (mProgressDrawable != null) { |
| 934 | applyPrimaryProgressTint(); |
| 935 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 939 | * Returns the tint applied to the progress drawable, if specified. |
| 940 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 941 | * @return the tint applied to the progress drawable |
| 942 | * @attr ref android.R.styleable#ProgressBar_progressTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 943 | * @see #setProgressTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 944 | */ |
| 945 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 946 | public ColorStateList getProgressTintList() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 947 | return mProgressTintInfo != null ? mProgressTintInfo.mProgressTintList : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 952 | * {@link #setProgressTintList(ColorStateList)}} to the progress |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 953 | * indicator. The default mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 954 | * |
| 955 | * @param tintMode the blending mode used to apply the tint, may be |
| 956 | * {@code null} to clear tint |
| 957 | * @attr ref android.R.styleable#ProgressBar_progressTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 958 | * @see #getProgressTintMode() |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 959 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 960 | */ |
| 961 | public void setProgressTintMode(@Nullable PorterDuff.Mode tintMode) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 962 | if (mProgressTintInfo == null) { |
| 963 | mProgressTintInfo = new ProgressTintInfo(); |
| 964 | } |
| 965 | mProgressTintInfo.mProgressTintMode = tintMode; |
| 966 | mProgressTintInfo.mHasProgressTintMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 967 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 968 | if (mProgressDrawable != null) { |
| 969 | applyPrimaryProgressTint(); |
| 970 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 974 | * Returns the blending mode used to apply the tint to the progress |
| 975 | * drawable, if specified. |
| 976 | * |
| 977 | * @return the blending mode used to apply the tint to the progress |
| 978 | * drawable |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 979 | * @attr ref android.R.styleable#ProgressBar_progressTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 980 | * @see #setProgressTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 981 | */ |
| 982 | @Nullable |
| 983 | public PorterDuff.Mode getProgressTintMode() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 984 | return mProgressTintInfo != null ? mProgressTintInfo.mProgressTintMode : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /** |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 988 | * Applies a tint to the progress background, if one exists. Does not |
| 989 | * modify the current tint mode, which is |
| 990 | * {@link PorterDuff.Mode#SRC_ATOP} by default. |
| 991 | * <p> |
| 992 | * The progress background must be specified as a layer with |
| 993 | * id {@link android.R.id#background} in a {@link LayerDrawable} |
| 994 | * used as the progress drawable. |
| 995 | * <p> |
| 996 | * Subsequent calls to {@link #setProgressDrawable(Drawable)} where the |
| 997 | * drawable contains a progress background will automatically mutate the |
| 998 | * drawable and apply the specified tint and tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 999 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1000 | * |
| 1001 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 1002 | * |
| 1003 | * @attr ref android.R.styleable#ProgressBar_progressBackgroundTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1004 | * @see #getProgressBackgroundTintList() |
| 1005 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1006 | */ |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1007 | @RemotableViewMethod |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1008 | public void setProgressBackgroundTintList(@Nullable ColorStateList tint) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1009 | if (mProgressTintInfo == null) { |
| 1010 | mProgressTintInfo = new ProgressTintInfo(); |
| 1011 | } |
| 1012 | mProgressTintInfo.mProgressBackgroundTintList = tint; |
| 1013 | mProgressTintInfo.mHasProgressBackgroundTint = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1014 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1015 | if (mProgressDrawable != null) { |
| 1016 | applyProgressBackgroundTint(); |
| 1017 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1021 | * Returns the tint applied to the progress background, if specified. |
| 1022 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1023 | * @return the tint applied to the progress background |
| 1024 | * @attr ref android.R.styleable#ProgressBar_progressBackgroundTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1025 | * @see #setProgressBackgroundTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1026 | */ |
| 1027 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1028 | public ColorStateList getProgressBackgroundTintList() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1029 | return mProgressTintInfo != null ? mProgressTintInfo.mProgressBackgroundTintList : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1034 | * {@link #setProgressBackgroundTintList(ColorStateList)}} to the progress |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1035 | * background. The default mode is {@link PorterDuff.Mode#SRC_IN}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1036 | * |
| 1037 | * @param tintMode the blending mode used to apply the tint, may be |
| 1038 | * {@code null} to clear tint |
| 1039 | * @attr ref android.R.styleable#ProgressBar_progressBackgroundTintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1040 | * @see #setProgressBackgroundTintList(ColorStateList) |
| 1041 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1042 | */ |
| 1043 | public void setProgressBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1044 | if (mProgressTintInfo == null) { |
| 1045 | mProgressTintInfo = new ProgressTintInfo(); |
| 1046 | } |
| 1047 | mProgressTintInfo.mProgressBackgroundTintMode = tintMode; |
| 1048 | mProgressTintInfo.mHasProgressBackgroundTintMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1049 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1050 | if (mProgressDrawable != null) { |
| 1051 | applyProgressBackgroundTint(); |
| 1052 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * @return the blending mode used to apply the tint to the progress |
| 1057 | * background |
| 1058 | * @attr ref android.R.styleable#ProgressBar_progressBackgroundTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1059 | * @see #setProgressBackgroundTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1060 | */ |
| 1061 | @Nullable |
| 1062 | public PorterDuff.Mode getProgressBackgroundTintMode() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1063 | return mProgressTintInfo != null ? mProgressTintInfo.mProgressBackgroundTintMode : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * Applies a tint to the secondary progress indicator, if one exists. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1068 | * Does not modify the current tint mode, which is |
| 1069 | * {@link PorterDuff.Mode#SRC_ATOP} by default. |
| 1070 | * <p> |
| 1071 | * The secondary progress indicator must be specified as a layer with |
| 1072 | * id {@link android.R.id#secondaryProgress} in a {@link LayerDrawable} |
| 1073 | * used as the progress drawable. |
| 1074 | * <p> |
| 1075 | * Subsequent calls to {@link #setProgressDrawable(Drawable)} where the |
| 1076 | * drawable contains a secondary progress indicator will automatically |
| 1077 | * mutate the drawable and apply the specified tint and tint mode using |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1078 | * {@link Drawable#setTintList(ColorStateList)}. |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1079 | * |
| 1080 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 1081 | * |
| 1082 | * @attr ref android.R.styleable#ProgressBar_secondaryProgressTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1083 | * @see #getSecondaryProgressTintList() |
| 1084 | * @see Drawable#setTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1085 | */ |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1086 | public void setSecondaryProgressTintList(@Nullable ColorStateList tint) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1087 | if (mProgressTintInfo == null) { |
| 1088 | mProgressTintInfo = new ProgressTintInfo(); |
| 1089 | } |
| 1090 | mProgressTintInfo.mSecondaryProgressTintList = tint; |
| 1091 | mProgressTintInfo.mHasSecondaryProgressTint = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1092 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1093 | if (mProgressDrawable != null) { |
| 1094 | applySecondaryProgressTint(); |
| 1095 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1099 | * Returns the tint applied to the secondary progress drawable, if |
| 1100 | * specified. |
| 1101 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1102 | * @return the tint applied to the secondary progress drawable |
| 1103 | * @attr ref android.R.styleable#ProgressBar_secondaryProgressTint |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1104 | * @see #setSecondaryProgressTintList(ColorStateList) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1105 | */ |
| 1106 | @Nullable |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1107 | public ColorStateList getSecondaryProgressTintList() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1108 | return mProgressTintInfo != null ? mProgressTintInfo.mSecondaryProgressTintList : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * Specifies the blending mode used to apply the tint specified by |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1113 | * {@link #setSecondaryProgressTintList(ColorStateList)}} to the secondary |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1114 | * progress indicator. The default mode is |
| 1115 | * {@link PorterDuff.Mode#SRC_ATOP}. |
| 1116 | * |
| 1117 | * @param tintMode the blending mode used to apply the tint, may be |
| 1118 | * {@code null} to clear tint |
| 1119 | * @attr ref android.R.styleable#ProgressBar_secondaryProgressTintMode |
Alan Viverette | a426445 | 2014-07-28 16:02:55 -0700 | [diff] [blame] | 1120 | * @see #setSecondaryProgressTintList(ColorStateList) |
| 1121 | * @see Drawable#setTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1122 | */ |
| 1123 | public void setSecondaryProgressTintMode(@Nullable PorterDuff.Mode tintMode) { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1124 | if (mProgressTintInfo == null) { |
| 1125 | mProgressTintInfo = new ProgressTintInfo(); |
| 1126 | } |
| 1127 | mProgressTintInfo.mSecondaryProgressTintMode = tintMode; |
| 1128 | mProgressTintInfo.mHasSecondaryProgressTintMode = true; |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1129 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1130 | if (mProgressDrawable != null) { |
| 1131 | applySecondaryProgressTint(); |
| 1132 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /** |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1136 | * Returns the blending mode used to apply the tint to the secondary |
| 1137 | * progress drawable, if specified. |
| 1138 | * |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1139 | * @return the blending mode used to apply the tint to the secondary |
| 1140 | * progress drawable |
| 1141 | * @attr ref android.R.styleable#ProgressBar_secondaryProgressTintMode |
Alan Viverette | 4f64c04 | 2014-07-21 17:49:13 -0700 | [diff] [blame] | 1142 | * @see #setSecondaryProgressTintMode(PorterDuff.Mode) |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1143 | */ |
| 1144 | @Nullable |
| 1145 | public PorterDuff.Mode getSecondaryProgressTintMode() { |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1146 | return mProgressTintInfo != null ? mProgressTintInfo.mSecondaryProgressTintMode : null; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1149 | /** |
| 1150 | * Returns the drawable to which a tint or tint mode should be applied. |
| 1151 | * |
| 1152 | * @param layerId id of the layer to modify |
| 1153 | * @param shouldFallback whether the base drawable should be returned |
| 1154 | * if the id does not exist |
| 1155 | * @return the drawable to modify |
| 1156 | */ |
| 1157 | @Nullable |
| 1158 | private Drawable getTintTarget(int layerId, boolean shouldFallback) { |
| 1159 | Drawable layer = null; |
| 1160 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1161 | final Drawable d = mProgressDrawable; |
| 1162 | if (d != null) { |
| 1163 | mProgressDrawable = d.mutate(); |
| 1164 | |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1165 | if (d instanceof LayerDrawable) { |
| 1166 | layer = ((LayerDrawable) d).findDrawableByLayerId(layerId); |
| 1167 | } |
| 1168 | |
| 1169 | if (shouldFallback && layer == null) { |
| 1170 | layer = d; |
| 1171 | } |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1172 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1173 | |
| 1174 | return layer; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /** |
Alan Viverette | e785d02 | 2013-09-26 15:21:10 -0700 | [diff] [blame] | 1178 | * Define the tileable drawable used to draw the progress bar in |
| 1179 | * progress mode. |
| 1180 | * <p> |
| 1181 | * If the drawable is a BitmapDrawable or contains BitmapDrawables, a |
| 1182 | * tiled copy will be generated for display as a progress bar. |
| 1183 | * |
| 1184 | * @param d the new drawable |
| 1185 | * @see #getProgressDrawable() |
| 1186 | * @see #setIndeterminate(boolean) |
| 1187 | */ |
| 1188 | public void setProgressDrawableTiled(Drawable d) { |
| 1189 | if (d != null) { |
| 1190 | d = tileify(d, false); |
| 1191 | } |
| 1192 | |
| 1193 | setProgressDrawable(d); |
| 1194 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | /** |
| 1197 | * @return The drawable currently used to draw the progress bar |
| 1198 | */ |
| 1199 | Drawable getCurrentDrawable() { |
| 1200 | return mCurrentDrawable; |
| 1201 | } |
| 1202 | |
| 1203 | @Override |
| 1204 | protected boolean verifyDrawable(Drawable who) { |
| 1205 | return who == mProgressDrawable || who == mIndeterminateDrawable |
| 1206 | || super.verifyDrawable(who); |
| 1207 | } |
| 1208 | |
| 1209 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 1210 | public void jumpDrawablesToCurrentState() { |
| 1211 | super.jumpDrawablesToCurrentState(); |
| 1212 | if (mProgressDrawable != null) mProgressDrawable.jumpToCurrentState(); |
| 1213 | if (mIndeterminateDrawable != null) mIndeterminateDrawable.jumpToCurrentState(); |
| 1214 | } |
| 1215 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 1216 | /** |
| 1217 | * @hide |
| 1218 | */ |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 1219 | @Override |
Fabrice Di Meglio | 0af4b8b | 2012-06-11 18:30:05 -0700 | [diff] [blame] | 1220 | public void onResolveDrawables(int layoutDirection) { |
| 1221 | final Drawable d = mCurrentDrawable; |
| 1222 | if (d != null) { |
| 1223 | d.setLayoutDirection(layoutDirection); |
| 1224 | } |
| 1225 | if (mIndeterminateDrawable != null) { |
| 1226 | mIndeterminateDrawable.setLayoutDirection(layoutDirection); |
| 1227 | } |
| 1228 | if (mProgressDrawable != null) { |
| 1229 | mProgressDrawable.setLayoutDirection(layoutDirection); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | public void postInvalidate() { |
| 1235 | if (!mNoInvalidate) { |
| 1236 | super.postInvalidate(); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | private class RefreshProgressRunnable implements Runnable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | public void run() { |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1242 | synchronized (ProgressBar.this) { |
| 1243 | final int count = mRefreshData.size(); |
| 1244 | for (int i = 0; i < count; i++) { |
| 1245 | final RefreshData rd = mRefreshData.get(i); |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1246 | doRefreshProgress(rd.id, rd.progress, rd.fromUser, true); |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1247 | rd.recycle(); |
| 1248 | } |
| 1249 | mRefreshData.clear(); |
| 1250 | mRefreshIsPosted = false; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1255 | private static class RefreshData { |
| 1256 | private static final int POOL_MAX = 24; |
| 1257 | private static final SynchronizedPool<RefreshData> sPool = |
| 1258 | new SynchronizedPool<RefreshData>(POOL_MAX); |
| 1259 | |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1260 | public int id; |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1261 | public int progress; |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1262 | public boolean fromUser; |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1263 | |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1264 | public static RefreshData obtain(int id, int progress, boolean fromUser) { |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1265 | RefreshData rd = sPool.acquire(); |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1266 | if (rd == null) { |
| 1267 | rd = new RefreshData(); |
| 1268 | } |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1269 | rd.id = id; |
| 1270 | rd.progress = progress; |
| 1271 | rd.fromUser = fromUser; |
| 1272 | return rd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1274 | |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1275 | public void recycle() { |
| 1276 | sPool.release(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | } |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1279 | |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1280 | private synchronized void doRefreshProgress(int id, int progress, boolean fromUser, |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 1281 | boolean callBackToApp) { |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1282 | float scale = mMax > 0 ? (float) progress / (float) mMax : 0; |
Alan Viverette | bac8e12 | 2015-02-04 17:15:01 -0800 | [diff] [blame] | 1283 | final Drawable d = mCurrentDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | if (d != null) { |
| 1285 | Drawable progressDrawable = null; |
| 1286 | |
| 1287 | if (d instanceof LayerDrawable) { |
| 1288 | progressDrawable = ((LayerDrawable) d).findDrawableByLayerId(id); |
Fabrice Di Meglio | 0af4b8b | 2012-06-11 18:30:05 -0700 | [diff] [blame] | 1289 | if (progressDrawable != null && canResolveLayoutDirection()) { |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 1290 | progressDrawable.setLayoutDirection(getLayoutDirection()); |
Fabrice Di Meglio | 0af4b8b | 2012-06-11 18:30:05 -0700 | [diff] [blame] | 1291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | final int level = (int) (scale * MAX_LEVEL); |
| 1295 | (progressDrawable != null ? progressDrawable : d).setLevel(level); |
| 1296 | } else { |
| 1297 | invalidate(); |
| 1298 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1299 | |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1300 | if (callBackToApp && id == R.id.progress) { |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1301 | onProgressRefresh(scale, fromUser, progress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | } |
| 1303 | } |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1304 | |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1305 | void onProgressRefresh(float scale, boolean fromUser, int progress) { |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1306 | if (AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 1307 | scheduleAccessibilityEventSender(); |
| 1308 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1311 | private synchronized void refreshProgress(int id, int progress, boolean fromUser) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | if (mUiThreadId == Thread.currentThread().getId()) { |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1313 | doRefreshProgress(id, progress, fromUser, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | } else { |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1315 | if (mRefreshProgressRunnable == null) { |
| 1316 | mRefreshProgressRunnable = new RefreshProgressRunnable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | } |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1318 | |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1319 | final RefreshData rd = RefreshData.obtain(id, progress, fromUser); |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1320 | mRefreshData.add(rd); |
| 1321 | if (mAttached && !mRefreshIsPosted) { |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1322 | post(mRefreshProgressRunnable); |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1323 | mRefreshIsPosted = true; |
| 1324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | /** |
| 1329 | * <p>Set the current progress to the specified value. Does not do anything |
| 1330 | * if the progress bar is in indeterminate mode.</p> |
| 1331 | * |
| 1332 | * @param progress the new progress, between 0 and {@link #getMax()} |
| 1333 | * |
| 1334 | * @see #setIndeterminate(boolean) |
| 1335 | * @see #isIndeterminate() |
| 1336 | * @see #getProgress() |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1337 | * @see #incrementProgressBy(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | */ |
| 1339 | @android.view.RemotableViewMethod |
| 1340 | public synchronized void setProgress(int progress) { |
| 1341 | setProgress(progress, false); |
| 1342 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | @android.view.RemotableViewMethod |
Alan Viverette | 12a4491 | 2015-04-16 13:06:00 -0700 | [diff] [blame] | 1345 | synchronized boolean setProgress(int progress, boolean fromUser) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | if (mIndeterminate) { |
Alan Viverette | 12a4491 | 2015-04-16 13:06:00 -0700 | [diff] [blame] | 1347 | // Not applicable. |
| 1348 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Alan Viverette | 12a4491 | 2015-04-16 13:06:00 -0700 | [diff] [blame] | 1351 | progress = MathUtils.constrain(progress, 0, mMax); |
| 1352 | |
| 1353 | if (progress == mProgress) { |
| 1354 | // No change from current. |
| 1355 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Alan Viverette | 12a4491 | 2015-04-16 13:06:00 -0700 | [diff] [blame] | 1358 | mProgress = progress; |
| 1359 | refreshProgress(R.id.progress, mProgress, fromUser); |
| 1360 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /** |
| 1364 | * <p> |
| 1365 | * Set the current secondary progress to the specified value. Does not do |
| 1366 | * anything if the progress bar is in indeterminate mode. |
| 1367 | * </p> |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1368 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | * @param secondaryProgress the new secondary progress, between 0 and {@link #getMax()} |
| 1370 | * @see #setIndeterminate(boolean) |
| 1371 | * @see #isIndeterminate() |
| 1372 | * @see #getSecondaryProgress() |
| 1373 | * @see #incrementSecondaryProgressBy(int) |
| 1374 | */ |
| 1375 | @android.view.RemotableViewMethod |
| 1376 | public synchronized void setSecondaryProgress(int secondaryProgress) { |
| 1377 | if (mIndeterminate) { |
| 1378 | return; |
| 1379 | } |
| 1380 | |
| 1381 | if (secondaryProgress < 0) { |
| 1382 | secondaryProgress = 0; |
| 1383 | } |
| 1384 | |
| 1385 | if (secondaryProgress > mMax) { |
| 1386 | secondaryProgress = mMax; |
| 1387 | } |
| 1388 | |
| 1389 | if (secondaryProgress != mSecondaryProgress) { |
| 1390 | mSecondaryProgress = secondaryProgress; |
| 1391 | refreshProgress(R.id.secondaryProgress, mSecondaryProgress, false); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * <p>Get the progress bar's current level of progress. Return 0 when the |
| 1397 | * progress bar is in indeterminate mode.</p> |
| 1398 | * |
| 1399 | * @return the current progress, between 0 and {@link #getMax()} |
| 1400 | * |
| 1401 | * @see #setIndeterminate(boolean) |
| 1402 | * @see #isIndeterminate() |
| 1403 | * @see #setProgress(int) |
| 1404 | * @see #setMax(int) |
| 1405 | * @see #getMax() |
| 1406 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1407 | @ViewDebug.ExportedProperty(category = "progress") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | public synchronized int getProgress() { |
| 1409 | return mIndeterminate ? 0 : mProgress; |
| 1410 | } |
| 1411 | |
| 1412 | /** |
| 1413 | * <p>Get the progress bar's current level of secondary progress. Return 0 when the |
| 1414 | * progress bar is in indeterminate mode.</p> |
| 1415 | * |
| 1416 | * @return the current secondary progress, between 0 and {@link #getMax()} |
| 1417 | * |
| 1418 | * @see #setIndeterminate(boolean) |
| 1419 | * @see #isIndeterminate() |
| 1420 | * @see #setSecondaryProgress(int) |
| 1421 | * @see #setMax(int) |
| 1422 | * @see #getMax() |
| 1423 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1424 | @ViewDebug.ExportedProperty(category = "progress") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | public synchronized int getSecondaryProgress() { |
| 1426 | return mIndeterminate ? 0 : mSecondaryProgress; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * <p>Return the upper limit of this progress bar's range.</p> |
| 1431 | * |
| 1432 | * @return a positive integer |
| 1433 | * |
| 1434 | * @see #setMax(int) |
| 1435 | * @see #getProgress() |
| 1436 | * @see #getSecondaryProgress() |
| 1437 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1438 | @ViewDebug.ExportedProperty(category = "progress") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | public synchronized int getMax() { |
| 1440 | return mMax; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * <p>Set the range of the progress bar to 0...<tt>max</tt>.</p> |
| 1445 | * |
| 1446 | * @param max the upper range of this progress bar |
| 1447 | * |
| 1448 | * @see #getMax() |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1449 | * @see #setProgress(int) |
| 1450 | * @see #setSecondaryProgress(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | */ |
| 1452 | @android.view.RemotableViewMethod |
| 1453 | public synchronized void setMax(int max) { |
| 1454 | if (max < 0) { |
| 1455 | max = 0; |
| 1456 | } |
| 1457 | if (max != mMax) { |
| 1458 | mMax = max; |
| 1459 | postInvalidate(); |
| 1460 | |
| 1461 | if (mProgress > max) { |
| 1462 | mProgress = max; |
| 1463 | } |
Michael Krehan | 58e3822 | 2011-02-17 20:04:27 -0800 | [diff] [blame] | 1464 | refreshProgress(R.id.progress, mProgress, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | } |
| 1466 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1467 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | /** |
| 1469 | * <p>Increase the progress bar's progress by the specified amount.</p> |
| 1470 | * |
| 1471 | * @param diff the amount by which the progress must be increased |
| 1472 | * |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1473 | * @see #setProgress(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | */ |
| 1475 | public synchronized final void incrementProgressBy(int diff) { |
| 1476 | setProgress(mProgress + diff); |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * <p>Increase the progress bar's secondary progress by the specified amount.</p> |
| 1481 | * |
| 1482 | * @param diff the amount by which the secondary progress must be increased |
| 1483 | * |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1484 | * @see #setSecondaryProgress(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | */ |
| 1486 | public synchronized final void incrementSecondaryProgressBy(int diff) { |
| 1487 | setSecondaryProgress(mSecondaryProgress + diff); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * <p>Start the indeterminate progress animation.</p> |
| 1492 | */ |
| 1493 | void startAnimation() { |
Romain Guy | a05e8a5 | 2010-02-25 14:32:39 -0800 | [diff] [blame] | 1494 | if (getVisibility() != VISIBLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 1498 | if (mIndeterminateDrawable instanceof Animatable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | mShouldStartAnimationDrawable = true; |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1500 | mHasAnimation = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | } else { |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1502 | mHasAnimation = true; |
| 1503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | if (mInterpolator == null) { |
| 1505 | mInterpolator = new LinearInterpolator(); |
| 1506 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1507 | |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1508 | if (mTransformation == null) { |
| 1509 | mTransformation = new Transformation(); |
| 1510 | } else { |
| 1511 | mTransformation.clear(); |
| 1512 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1513 | |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1514 | if (mAnimation == null) { |
| 1515 | mAnimation = new AlphaAnimation(0.0f, 1.0f); |
| 1516 | } else { |
| 1517 | mAnimation.reset(); |
| 1518 | } |
| 1519 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | mAnimation.setRepeatMode(mBehavior); |
| 1521 | mAnimation.setRepeatCount(Animation.INFINITE); |
| 1522 | mAnimation.setDuration(mDuration); |
| 1523 | mAnimation.setInterpolator(mInterpolator); |
| 1524 | mAnimation.setStartTime(Animation.START_ON_FIRST_FRAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
Evan Charlton | 08e1473 | 2010-06-07 10:38:53 -0700 | [diff] [blame] | 1526 | postInvalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * <p>Stop the indeterminate progress animation.</p> |
| 1531 | */ |
| 1532 | void stopAnimation() { |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1533 | mHasAnimation = false; |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 1534 | if (mIndeterminateDrawable instanceof Animatable) { |
| 1535 | ((Animatable) mIndeterminateDrawable).stop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | mShouldStartAnimationDrawable = false; |
| 1537 | } |
Evan Charlton | 08e1473 | 2010-06-07 10:38:53 -0700 | [diff] [blame] | 1538 | postInvalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /** |
| 1542 | * Sets the acceleration curve for the indeterminate animation. |
| 1543 | * The interpolator is loaded as a resource from the specified context. |
| 1544 | * |
| 1545 | * @param context The application environment |
| 1546 | * @param resID The resource identifier of the interpolator to load |
| 1547 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 1548 | public void setInterpolator(Context context, @InterpolatorRes int resID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | setInterpolator(AnimationUtils.loadInterpolator(context, resID)); |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * Sets the acceleration curve for the indeterminate animation. |
| 1554 | * Defaults to a linear interpolation. |
| 1555 | * |
| 1556 | * @param interpolator The interpolator which defines the acceleration curve |
| 1557 | */ |
| 1558 | public void setInterpolator(Interpolator interpolator) { |
| 1559 | mInterpolator = interpolator; |
| 1560 | } |
| 1561 | |
| 1562 | /** |
| 1563 | * Gets the acceleration curve type for the indeterminate animation. |
| 1564 | * |
| 1565 | * @return the {@link Interpolator} associated to this animation |
| 1566 | */ |
| 1567 | public Interpolator getInterpolator() { |
| 1568 | return mInterpolator; |
| 1569 | } |
| 1570 | |
| 1571 | @Override |
Steve Howard | b25ffff | 2010-08-20 17:39:26 -0700 | [diff] [blame] | 1572 | @RemotableViewMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | public void setVisibility(int v) { |
| 1574 | if (getVisibility() != v) { |
| 1575 | super.setVisibility(v); |
| 1576 | |
| 1577 | if (mIndeterminate) { |
| 1578 | // let's be nice with the UI thread |
| 1579 | if (v == GONE || v == INVISIBLE) { |
| 1580 | stopAnimation(); |
Romain Guy | a05e8a5 | 2010-02-25 14:32:39 -0800 | [diff] [blame] | 1581 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | startAnimation(); |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | @Override |
Romain Guy | a05e8a5 | 2010-02-25 14:32:39 -0800 | [diff] [blame] | 1589 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 1590 | super.onVisibilityChanged(changedView, visibility); |
| 1591 | |
| 1592 | if (mIndeterminate) { |
| 1593 | // let's be nice with the UI thread |
| 1594 | if (visibility == GONE || visibility == INVISIBLE) { |
| 1595 | stopAnimation(); |
| 1596 | } else { |
| 1597 | startAnimation(); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | public void invalidateDrawable(Drawable dr) { |
| 1604 | if (!mInDrawing) { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 1605 | if (verifyDrawable(dr)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | final Rect dirty = dr.getBounds(); |
Chris Craik | 7546a4b | 2013-10-30 19:49:37 -0700 | [diff] [blame] | 1607 | final int scrollX = mScrollX + mPaddingLeft; |
| 1608 | final int scrollY = mScrollY + mPaddingTop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | |
Chris Craik | 7546a4b | 2013-10-30 19:49:37 -0700 | [diff] [blame] | 1610 | invalidate(dirty.left + scrollX, dirty.top + scrollY, |
| 1611 | dirty.right + scrollX, dirty.bottom + scrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | } else { |
| 1613 | super.invalidateDrawable(dr); |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | @Override |
| 1619 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 1620 | updateDrawableBounds(w, h); |
| 1621 | } |
| 1622 | |
| 1623 | private void updateDrawableBounds(int w, int h) { |
Adam Powell | 6322af5 | 2012-08-08 15:59:12 -0700 | [diff] [blame] | 1624 | // onDraw will translate the canvas so we draw starting at 0,0. |
| 1625 | // Subtract out padding for the purposes of the calculations below. |
| 1626 | w -= mPaddingRight + mPaddingLeft; |
| 1627 | h -= mPaddingTop + mPaddingBottom; |
| 1628 | |
| 1629 | int right = w; |
| 1630 | int bottom = h; |
Adam Powell | a1b92c5 | 2011-09-02 15:05:15 -0700 | [diff] [blame] | 1631 | int top = 0; |
| 1632 | int left = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | |
| 1634 | if (mIndeterminateDrawable != null) { |
Chet Haase | a79803c | 2011-09-28 17:51:53 -0700 | [diff] [blame] | 1635 | // Aspect ratio logic does not apply to AnimationDrawables |
| 1636 | if (mOnlyIndeterminate && !(mIndeterminateDrawable instanceof AnimationDrawable)) { |
Adam Powell | a1b92c5 | 2011-09-02 15:05:15 -0700 | [diff] [blame] | 1637 | // Maintain aspect ratio. Certain kinds of animated drawables |
| 1638 | // get very confused otherwise. |
| 1639 | final int intrinsicWidth = mIndeterminateDrawable.getIntrinsicWidth(); |
| 1640 | final int intrinsicHeight = mIndeterminateDrawable.getIntrinsicHeight(); |
| 1641 | final float intrinsicAspect = (float) intrinsicWidth / intrinsicHeight; |
| 1642 | final float boundAspect = (float) w / h; |
| 1643 | if (intrinsicAspect != boundAspect) { |
| 1644 | if (boundAspect > intrinsicAspect) { |
| 1645 | // New width is larger. Make it smaller to match height. |
| 1646 | final int width = (int) (h * intrinsicAspect); |
| 1647 | left = (w - width) / 2; |
| 1648 | right = left + width; |
| 1649 | } else { |
| 1650 | // New height is larger. Make it smaller to match width. |
| 1651 | final int height = (int) (w * (1 / intrinsicAspect)); |
| 1652 | top = (h - height) / 2; |
| 1653 | bottom = top + height; |
| 1654 | } |
| 1655 | } |
| 1656 | } |
Fabrice Di Meglio | 2b378cd | 2013-01-30 16:39:33 -0800 | [diff] [blame] | 1657 | if (isLayoutRtl() && mMirrorForRtl) { |
Fabrice Di Meglio | 7fb98b3 | 2012-09-12 20:04:04 -0700 | [diff] [blame] | 1658 | int tempLeft = left; |
| 1659 | left = w - right; |
| 1660 | right = w - tempLeft; |
| 1661 | } |
Adam Powell | a1b92c5 | 2011-09-02 15:05:15 -0700 | [diff] [blame] | 1662 | mIndeterminateDrawable.setBounds(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1664 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | if (mProgressDrawable != null) { |
| 1666 | mProgressDrawable.setBounds(0, 0, right, bottom); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | @Override |
| 1671 | protected synchronized void onDraw(Canvas canvas) { |
| 1672 | super.onDraw(canvas); |
| 1673 | |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1674 | drawTrack(canvas); |
| 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * Draws the progress bar track. |
| 1679 | */ |
| 1680 | void drawTrack(Canvas canvas) { |
| 1681 | final Drawable d = mCurrentDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | if (d != null) { |
| 1683 | // Translate canvas so a indeterminate circular progress bar with padding |
| 1684 | // rotates properly in its animation |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1685 | final int saveCount = canvas.save(); |
| 1686 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1687 | if (isLayoutRtl() && mMirrorForRtl) { |
Fabrice Di Meglio | 7fb98b3 | 2012-09-12 20:04:04 -0700 | [diff] [blame] | 1688 | canvas.translate(getWidth() - mPaddingRight, mPaddingTop); |
| 1689 | canvas.scale(-1.0f, 1.0f); |
| 1690 | } else { |
| 1691 | canvas.translate(mPaddingLeft, mPaddingTop); |
| 1692 | } |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1693 | |
| 1694 | final long time = getDrawingTime(); |
Romain Guy | ab4c4f4f | 2012-05-06 13:11:24 -0700 | [diff] [blame] | 1695 | if (mHasAnimation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | mAnimation.getTransformation(time, mTransformation); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1697 | final float scale = mTransformation.getAlpha(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | try { |
| 1699 | mInDrawing = true; |
| 1700 | d.setLevel((int) (scale * MAX_LEVEL)); |
| 1701 | } finally { |
| 1702 | mInDrawing = false; |
| 1703 | } |
Jeff Brown | 6cb7b46 | 2012-03-05 13:21:17 -0800 | [diff] [blame] | 1704 | postInvalidateOnAnimation(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | } |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | d.draw(canvas); |
Alan Viverette | 661e636 | 2014-05-12 10:55:37 -0700 | [diff] [blame] | 1708 | canvas.restoreToCount(saveCount); |
| 1709 | |
Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 1710 | if (mShouldStartAnimationDrawable && d instanceof Animatable) { |
| 1711 | ((Animatable) d).start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | mShouldStartAnimationDrawable = false; |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | @Override |
| 1718 | protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | int dw = 0; |
| 1720 | int dh = 0; |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1721 | |
| 1722 | final Drawable d = mCurrentDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | if (d != null) { |
| 1724 | dw = Math.max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth())); |
| 1725 | dh = Math.max(mMinHeight, Math.min(mMaxHeight, d.getIntrinsicHeight())); |
| 1726 | } |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1727 | |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 1728 | updateDrawableState(); |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1729 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | dw += mPaddingLeft + mPaddingRight; |
| 1731 | dh += mPaddingTop + mPaddingBottom; |
| 1732 | |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1733 | final int measuredWidth = resolveSizeAndState(dw, widthMeasureSpec, 0); |
| 1734 | final int measuredHeight = resolveSizeAndState(dh, heightMeasureSpec, 0); |
| 1735 | setMeasuredDimension(measuredWidth, measuredHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1737 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | @Override |
| 1739 | protected void drawableStateChanged() { |
| 1740 | super.drawableStateChanged(); |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 1741 | updateDrawableState(); |
| 1742 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1743 | |
Joe Onorato | aa07263 | 2010-12-08 15:31:28 -0800 | [diff] [blame] | 1744 | private void updateDrawableState() { |
Alan Viverette | 6a8253f | 2015-02-23 12:49:47 -0800 | [diff] [blame] | 1745 | final int[] state = getDrawableState(); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | if (mProgressDrawable != null && mProgressDrawable.isStateful()) { |
| 1748 | mProgressDrawable.setState(state); |
| 1749 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | if (mIndeterminateDrawable != null && mIndeterminateDrawable.isStateful()) { |
| 1752 | mIndeterminateDrawable.setState(state); |
| 1753 | } |
| 1754 | } |
| 1755 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1756 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 1757 | public void drawableHotspotChanged(float x, float y) { |
| 1758 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 1759 | |
| 1760 | if (mProgressDrawable != null) { |
| 1761 | mProgressDrawable.setHotspot(x, y); |
| 1762 | } |
| 1763 | |
| 1764 | if (mIndeterminateDrawable != null) { |
| 1765 | mIndeterminateDrawable.setHotspot(x, y); |
| 1766 | } |
| 1767 | } |
| 1768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1769 | static class SavedState extends BaseSavedState { |
| 1770 | int progress; |
| 1771 | int secondaryProgress; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1772 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | /** |
| 1774 | * Constructor called from {@link ProgressBar#onSaveInstanceState()} |
| 1775 | */ |
| 1776 | SavedState(Parcelable superState) { |
| 1777 | super(superState); |
| 1778 | } |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1779 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | /** |
| 1781 | * Constructor called from {@link #CREATOR} |
| 1782 | */ |
| 1783 | private SavedState(Parcel in) { |
| 1784 | super(in); |
| 1785 | progress = in.readInt(); |
| 1786 | secondaryProgress = in.readInt(); |
| 1787 | } |
| 1788 | |
| 1789 | @Override |
| 1790 | public void writeToParcel(Parcel out, int flags) { |
| 1791 | super.writeToParcel(out, flags); |
| 1792 | out.writeInt(progress); |
| 1793 | out.writeInt(secondaryProgress); |
| 1794 | } |
| 1795 | |
| 1796 | public static final Parcelable.Creator<SavedState> CREATOR |
| 1797 | = new Parcelable.Creator<SavedState>() { |
| 1798 | public SavedState createFromParcel(Parcel in) { |
| 1799 | return new SavedState(in); |
| 1800 | } |
| 1801 | |
| 1802 | public SavedState[] newArray(int size) { |
| 1803 | return new SavedState[size]; |
| 1804 | } |
| 1805 | }; |
| 1806 | } |
| 1807 | |
| 1808 | @Override |
| 1809 | public Parcelable onSaveInstanceState() { |
| 1810 | // Force our ancestor class to save its state |
| 1811 | Parcelable superState = super.onSaveInstanceState(); |
| 1812 | SavedState ss = new SavedState(superState); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1813 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | ss.progress = mProgress; |
| 1815 | ss.secondaryProgress = mSecondaryProgress; |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | return ss; |
| 1818 | } |
| 1819 | |
| 1820 | @Override |
| 1821 | public void onRestoreInstanceState(Parcelable state) { |
| 1822 | SavedState ss = (SavedState) state; |
| 1823 | super.onRestoreInstanceState(ss.getSuperState()); |
RoboErik | 5b07143 | 2015-02-11 13:52:05 -0800 | [diff] [blame] | 1824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | setProgress(ss.progress); |
| 1826 | setSecondaryProgress(ss.secondaryProgress); |
| 1827 | } |
David Sobreira Marques | 52a3543 | 2010-05-15 16:10:18 -0300 | [diff] [blame] | 1828 | |
| 1829 | @Override |
| 1830 | protected void onAttachedToWindow() { |
| 1831 | super.onAttachedToWindow(); |
| 1832 | if (mIndeterminate) { |
| 1833 | startAnimation(); |
| 1834 | } |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1835 | if (mRefreshData != null) { |
| 1836 | synchronized (this) { |
| 1837 | final int count = mRefreshData.size(); |
| 1838 | for (int i = 0; i < count; i++) { |
| 1839 | final RefreshData rd = mRefreshData.get(i); |
Alan Viverette | 5ce0ec0 | 2014-11-25 09:40:54 -0800 | [diff] [blame] | 1840 | doRefreshProgress(rd.id, rd.progress, rd.fromUser, true); |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1841 | rd.recycle(); |
| 1842 | } |
| 1843 | mRefreshData.clear(); |
| 1844 | } |
| 1845 | } |
| 1846 | mAttached = true; |
David Sobreira Marques | 52a3543 | 2010-05-15 16:10:18 -0300 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | @Override |
| 1850 | protected void onDetachedFromWindow() { |
David Sobreira Marques | 52a3543 | 2010-05-15 16:10:18 -0300 | [diff] [blame] | 1851 | if (mIndeterminate) { |
| 1852 | stopAnimation(); |
| 1853 | } |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1854 | if (mRefreshProgressRunnable != null) { |
| 1855 | removeCallbacks(mRefreshProgressRunnable); |
Bing Deng | 24a2bc7 | 2012-11-08 13:35:15 +0800 | [diff] [blame] | 1856 | mRefreshIsPosted = false; |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1857 | } |
| 1858 | if (mAccessibilityEventSender != null) { |
| 1859 | removeCallbacks(mAccessibilityEventSender); |
| 1860 | } |
Patrick Dubroy | ec84c3a | 2011-01-13 17:55:37 -0800 | [diff] [blame] | 1861 | // This should come after stopAnimation(), otherwise an invalidate message remains in the |
| 1862 | // queue, which can prevent the entire view hierarchy from being GC'ed during a rotation |
| 1863 | super.onDetachedFromWindow(); |
Adam Powell | a050663 | 2012-04-10 17:19:20 -0700 | [diff] [blame] | 1864 | mAttached = false; |
David Sobreira Marques | 52a3543 | 2010-05-15 16:10:18 -0300 | [diff] [blame] | 1865 | } |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1866 | |
| 1867 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1868 | public CharSequence getAccessibilityClassName() { |
| 1869 | return ProgressBar.class.getName(); |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1872 | /** @hide */ |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1873 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1874 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 1875 | super.onInitializeAccessibilityEventInternal(event); |
| 1876 | event.setItemCount(mMax); |
| 1877 | event.setCurrentItemIndex(mProgress); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
Svetoslav Ganov | 6518ad7 | 2011-03-18 16:19:55 -0700 | [diff] [blame] | 1880 | /** |
| 1881 | * Schedule a command for sending an accessibility event. |
| 1882 | * </br> |
| 1883 | * Note: A command is used to ensure that accessibility events |
| 1884 | * are sent at most one in a given time frame to save |
| 1885 | * system resources while the progress changes quickly. |
| 1886 | */ |
| 1887 | private void scheduleAccessibilityEventSender() { |
| 1888 | if (mAccessibilityEventSender == null) { |
| 1889 | mAccessibilityEventSender = new AccessibilityEventSender(); |
| 1890 | } else { |
| 1891 | removeCallbacks(mAccessibilityEventSender); |
| 1892 | } |
| 1893 | postDelayed(mAccessibilityEventSender, TIMEOUT_SEND_ACCESSIBILITY_EVENT); |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * Command for sending an accessibility event. |
| 1898 | */ |
| 1899 | private class AccessibilityEventSender implements Runnable { |
| 1900 | public void run() { |
| 1901 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 1902 | } |
| 1903 | } |
Alan Viverette | b56f5d2 | 2014-09-14 15:48:50 -0700 | [diff] [blame] | 1904 | |
| 1905 | private static class ProgressTintInfo { |
| 1906 | ColorStateList mIndeterminateTintList; |
| 1907 | PorterDuff.Mode mIndeterminateTintMode; |
| 1908 | boolean mHasIndeterminateTint; |
| 1909 | boolean mHasIndeterminateTintMode; |
| 1910 | |
| 1911 | ColorStateList mProgressTintList; |
| 1912 | PorterDuff.Mode mProgressTintMode; |
| 1913 | boolean mHasProgressTint; |
| 1914 | boolean mHasProgressTintMode; |
| 1915 | |
| 1916 | ColorStateList mProgressBackgroundTintList; |
| 1917 | PorterDuff.Mode mProgressBackgroundTintMode; |
| 1918 | boolean mHasProgressBackgroundTint; |
| 1919 | boolean mHasProgressBackgroundTintMode; |
| 1920 | |
| 1921 | ColorStateList mSecondaryProgressTintList; |
| 1922 | PorterDuff.Mode mSecondaryProgressTintMode; |
| 1923 | boolean mHasSecondaryProgressTint; |
| 1924 | boolean mHasSecondaryProgressTintMode; |
| 1925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | } |