Commit 427067e8 authored by Matthew Jones's avatar Matthew Jones Committed by Commit Bot

Simplify and add tests for the progress bar

This change refactors much of the progress bar code in an effort to
simplify the logic that has grown out of control. The significant
changes are as follows:

- The smooth progress animator runs until progress is finished instead
  of ending when progress reaches the current complete percentage.
- 'finish(...)' is blocked until the progress animators are complete
  unless it is called with 'fadeOut' set to false (see todo in code).
- 'updateVisibleProgress' has been removed as its function had become
  unclear.
- More complete tests to exercise the different functionality.

BUG=

Change-Id: I95ff4c5918bccaac5bc1ef025254caea13a862c2
Reviewed-on: https://chromium-review.googlesource.com/769772
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522161}
parent cb7807f3
......@@ -15,6 +15,7 @@ import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
/**
......@@ -32,6 +33,23 @@ public class ClipDrawableProgressBar extends ImageView {
public int progressBarBackgroundColor;
}
/**
* An observer for visible progress updates.
*/
@VisibleForTesting
interface ProgressBarObserver {
/**
* A notification that the visible progress has been updated. This may not coincide with
* updates from the web page due to animations for the progress bar running.
*/
void onVisibleProgressUpdated();
/**
* A notification that the visibility of the progress bar has changed.
*/
void onVisibilityChanged();
}
// ClipDrawable's max is a fixed constant 10000.
// http://developer.android.com/reference/android/graphics/drawable/ClipDrawable.html
private static final int CLIP_DRAWABLE_MAX = 10000;
......@@ -41,6 +59,9 @@ public class ClipDrawableProgressBar extends ImageView {
private float mProgress;
private int mDesiredVisibility;
/** An observer of updates to the progress bar. */
private ProgressBarObserver mProgressBarObserver;
/**
* Create the progress bar with a custom height.
* @param context An Android context.
......@@ -64,6 +85,15 @@ public class ClipDrawableProgressBar extends ImageView {
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height));
}
/**
* @param observer An update observer for the progress bar.
*/
@VisibleForTesting
void setProgressBarObserver(ProgressBarObserver observer) {
assert mProgressBarObserver == null;
mProgressBarObserver = observer;
}
/**
* Get the progress bar's current level of progress.
*
......@@ -84,6 +114,7 @@ public class ClipDrawableProgressBar extends ImageView {
mProgress = progress;
getDrawable().setLevel(Math.round(progress * CLIP_DRAWABLE_MAX));
if (mProgressBarObserver != null) mProgressBarObserver.onVisibleProgressUpdated();
}
/**
......@@ -132,7 +163,10 @@ public class ClipDrawableProgressBar extends ImageView {
int oldVisibility = getVisibility();
int newVisibility = mDesiredVisibility;
if (getAlpha() == 0 && mDesiredVisibility == VISIBLE) newVisibility = INVISIBLE;
if (oldVisibility != newVisibility) super.setVisibility(newVisibility);
if (oldVisibility != newVisibility) {
super.setVisibility(newVisibility);
if (mProgressBarObserver != null) mProgressBarObserver.onVisibilityChanged();
}
}
private int applyAlpha(int color, float alpha) {
......
......@@ -107,6 +107,7 @@ public class ToolbarProgressBarAnimatingView extends ImageView {
public ToolbarProgressBarAnimatingView(Context context, LayoutParams layoutParams) {
super(context);
setLayoutParams(layoutParams);
mIsCanceled = true;
mIsRtl = LocalizationUtils.isLayoutRtl();
mDpToPx = getResources().getDisplayMetrics().density;
......@@ -241,7 +242,7 @@ public class ToolbarProgressBarAnimatingView extends ImageView {
* @return True if the animation is running.
*/
public boolean isRunning() {
return mAnimatorSet.isStarted();
return !mIsCanceled;
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment