mobile/android/base/toolbar/ToolbarProgressView.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2010 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 package org.mozilla.gecko.toolbar;
michael@0 18
michael@0 19 import android.content.Context;
michael@0 20 import android.graphics.Canvas;
michael@0 21 import android.graphics.Rect;
michael@0 22 import android.graphics.drawable.Drawable;
michael@0 23 import android.os.Build;
michael@0 24 import android.os.Handler;
michael@0 25 import android.os.Message;
michael@0 26 import android.util.AttributeSet;
michael@0 27 import android.widget.ImageView;
michael@0 28 import android.view.View;
michael@0 29 import android.view.animation.Animation;
michael@0 30
michael@0 31 /**
michael@0 32 * Progress view used for page loads.
michael@0 33 *
michael@0 34 * Because we're given limited information about the page load progress, the
michael@0 35 * bar also includes incremental animation between each step to improve
michael@0 36 * perceived performance.
michael@0 37 */
michael@0 38 public class ToolbarProgressView extends ImageView {
michael@0 39 private static final int MAX_PROGRESS = 10000;
michael@0 40 private static final int MSG_UPDATE = 0;
michael@0 41 private static final int MSG_HIDE = 1;
michael@0 42 private static final int STEPS = 10;
michael@0 43 private static final int DELAY = 40;
michael@0 44
michael@0 45 private static final boolean PRE_HONEYCOMB = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
michael@0 46
michael@0 47 private int mTargetProgress;
michael@0 48 private int mIncrement;
michael@0 49 private Rect mBounds;
michael@0 50 private Handler mHandler;
michael@0 51 private int mCurrentProgress;
michael@0 52
michael@0 53 public ToolbarProgressView(Context context, AttributeSet attrs, int defStyle) {
michael@0 54 super(context, attrs, defStyle);
michael@0 55 init(context);
michael@0 56 }
michael@0 57
michael@0 58 public ToolbarProgressView(Context context, AttributeSet attrs) {
michael@0 59 super(context, attrs);
michael@0 60 init(context);
michael@0 61 }
michael@0 62
michael@0 63 public ToolbarProgressView(Context context) {
michael@0 64 super(context);
michael@0 65 init(context);
michael@0 66 }
michael@0 67
michael@0 68 private void init(Context ctx) {
michael@0 69 mBounds = new Rect(0,0,0,0);
michael@0 70 mTargetProgress = 0;
michael@0 71
michael@0 72 mHandler = new Handler() {
michael@0 73 @Override
michael@0 74 public void handleMessage(Message msg) {
michael@0 75 switch (msg.what) {
michael@0 76 case MSG_UPDATE:
michael@0 77 mCurrentProgress = Math.min(mTargetProgress, mCurrentProgress + mIncrement);
michael@0 78
michael@0 79 updateBounds();
michael@0 80
michael@0 81 if (mCurrentProgress < mTargetProgress) {
michael@0 82 final int delay = (mTargetProgress < MAX_PROGRESS) ? DELAY : DELAY / 4;
michael@0 83 sendMessageDelayed(mHandler.obtainMessage(msg.what), delay);
michael@0 84 } else if (mCurrentProgress == MAX_PROGRESS) {
michael@0 85 sendMessageDelayed(mHandler.obtainMessage(MSG_HIDE), DELAY);
michael@0 86 }
michael@0 87 break;
michael@0 88
michael@0 89 case MSG_HIDE:
michael@0 90 setVisibility(View.GONE);
michael@0 91 break;
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 };
michael@0 96 }
michael@0 97
michael@0 98 @Override
michael@0 99 public void setVisibility(int visibility) {
michael@0 100 // On GB/Froyo, setting the visibility to GONE/HIDDEN alone does not
michael@0 101 // work with translations. Calling clearAnimation acts as a workaround.
michael@0 102 if (PRE_HONEYCOMB && visibility != VISIBLE) {
michael@0 103 clearAnimation();
michael@0 104 }
michael@0 105
michael@0 106 super.setVisibility(visibility);
michael@0 107 }
michael@0 108
michael@0 109 @Override
michael@0 110 public void setAnimation(Animation animation) {
michael@0 111 // On GB/Froyo, setting the animation after hiding the view causes it
michael@0 112 // to reappear. As a workaround, disallow setAnimation from being
michael@0 113 // called if the view is not shown.
michael@0 114 if (PRE_HONEYCOMB && isShown()) {
michael@0 115 super.setAnimation(animation);
michael@0 116 }
michael@0 117 }
michael@0 118
michael@0 119 @Override
michael@0 120 public void onLayout(boolean f, int l, int t, int r, int b) {
michael@0 121 mBounds.left = 0;
michael@0 122 mBounds.right = (r - l) * mCurrentProgress / MAX_PROGRESS;
michael@0 123 mBounds.top = 0;
michael@0 124 mBounds.bottom = b - t;
michael@0 125 }
michael@0 126
michael@0 127 @Override
michael@0 128 public void onDraw(Canvas canvas) {
michael@0 129 final Drawable d = getDrawable();
michael@0 130 d.setBounds(mBounds);
michael@0 131 d.draw(canvas);
michael@0 132 }
michael@0 133
michael@0 134 /**
michael@0 135 * Immediately sets the progress bar to the given progress percentage.
michael@0 136 *
michael@0 137 * @param progress Percentage (0-100) to which progress bar should be set
michael@0 138 */
michael@0 139 void setProgress(int progressPercentage) {
michael@0 140 mCurrentProgress = mTargetProgress = getAbsoluteProgress(progressPercentage);
michael@0 141 updateBounds();
michael@0 142
michael@0 143 clearMessages();
michael@0 144 }
michael@0 145
michael@0 146 /**
michael@0 147 * Animates the progress bar from the current progress value to the given
michael@0 148 * progress percentage.
michael@0 149 *
michael@0 150 * @param progress Percentage (0-100) to which progress bar should be animated
michael@0 151 */
michael@0 152 void animateProgress(int progressPercentage) {
michael@0 153 final int absoluteProgress = getAbsoluteProgress(progressPercentage);
michael@0 154 if (absoluteProgress <= mTargetProgress) {
michael@0 155 // After we manually click stop, we can still receive page load
michael@0 156 // events (e.g., DOMContentLoaded). Updating for other updates
michael@0 157 // after a STOP event can freeze the progress bar, so guard against
michael@0 158 // that here.
michael@0 159 return;
michael@0 160 }
michael@0 161
michael@0 162 mTargetProgress = absoluteProgress;
michael@0 163 mIncrement = (mTargetProgress - mCurrentProgress) / STEPS;
michael@0 164
michael@0 165 clearMessages();
michael@0 166 mHandler.sendEmptyMessage(MSG_UPDATE);
michael@0 167 }
michael@0 168
michael@0 169 private void clearMessages() {
michael@0 170 mHandler.removeMessages(MSG_UPDATE);
michael@0 171 mHandler.removeMessages(MSG_HIDE);
michael@0 172 }
michael@0 173
michael@0 174 private int getAbsoluteProgress(int progressPercentage) {
michael@0 175 if (progressPercentage < 0) {
michael@0 176 return 0;
michael@0 177 }
michael@0 178
michael@0 179 if (progressPercentage > 100) {
michael@0 180 return 100;
michael@0 181 }
michael@0 182
michael@0 183 return progressPercentage * MAX_PROGRESS / 100;
michael@0 184 }
michael@0 185
michael@0 186 private void updateBounds() {
michael@0 187 mBounds.right = getWidth() * mCurrentProgress / MAX_PROGRESS;
michael@0 188 invalidate();
michael@0 189 }
michael@0 190 }

mercurial