mobile/android/base/home/HomeBanner.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.home;
michael@0 7
michael@0 8 import org.json.JSONException;
michael@0 9 import org.json.JSONObject;
michael@0 10 import org.mozilla.gecko.GeckoAppShell;
michael@0 11 import org.mozilla.gecko.GeckoEvent;
michael@0 12 import org.mozilla.gecko.R;
michael@0 13 import org.mozilla.gecko.animation.PropertyAnimator;
michael@0 14 import org.mozilla.gecko.animation.PropertyAnimator.Property;
michael@0 15 import org.mozilla.gecko.animation.ViewHelper;
michael@0 16 import org.mozilla.gecko.gfx.BitmapUtils;
michael@0 17 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 18 import org.mozilla.gecko.util.ThreadUtils;
michael@0 19 import org.mozilla.gecko.widget.EllipsisTextView;
michael@0 20
michael@0 21 import android.content.Context;
michael@0 22 import android.graphics.drawable.Drawable;
michael@0 23 import android.os.Build;
michael@0 24 import android.text.Html;
michael@0 25 import android.text.Spanned;
michael@0 26 import android.text.TextUtils;
michael@0 27 import android.util.AttributeSet;
michael@0 28 import android.util.Log;
michael@0 29 import android.view.LayoutInflater;
michael@0 30 import android.view.MotionEvent;
michael@0 31 import android.view.View;
michael@0 32 import android.widget.ImageButton;
michael@0 33 import android.widget.ImageView;
michael@0 34 import android.widget.LinearLayout;
michael@0 35 import android.widget.TextView;
michael@0 36
michael@0 37 public class HomeBanner extends LinearLayout
michael@0 38 implements GeckoEventListener {
michael@0 39 private static final String LOGTAG = "GeckoHomeBanner";
michael@0 40
michael@0 41 // Used for tracking scroll length
michael@0 42 private float mTouchY = -1;
michael@0 43
michael@0 44 // Used to detect for upwards scroll to push banner all the way up
michael@0 45 private boolean mSnapBannerToTop;
michael@0 46
michael@0 47 // Tracks whether or not the banner should be shown on the current panel.
michael@0 48 private boolean mActive = false;
michael@0 49
michael@0 50 // The user is currently swiping between HomePager pages
michael@0 51 private boolean mScrollingPages = false;
michael@0 52
michael@0 53 // Tracks whether the user swiped the banner down, preventing us from autoshowing when the user
michael@0 54 // switches back to the default page.
michael@0 55 private boolean mUserSwipedDown = false;
michael@0 56
michael@0 57 // We must use this custom TextView to address an issue on 2.3 and lower where ellipsized text
michael@0 58 // will not wrap more than 2 lines.
michael@0 59 private final EllipsisTextView mTextView;
michael@0 60 private final ImageView mIconView;
michael@0 61
michael@0 62 // The height of the banner view.
michael@0 63 private final float mHeight;
michael@0 64
michael@0 65 // Listener that gets called when the banner is dismissed from the close button.
michael@0 66 private OnDismissListener mOnDismissListener;
michael@0 67
michael@0 68 public interface OnDismissListener {
michael@0 69 public void onDismiss();
michael@0 70 }
michael@0 71
michael@0 72 public HomeBanner(Context context) {
michael@0 73 this(context, null);
michael@0 74 }
michael@0 75
michael@0 76 public HomeBanner(Context context, AttributeSet attrs) {
michael@0 77 super(context, attrs);
michael@0 78
michael@0 79 LayoutInflater.from(context).inflate(R.layout.home_banner_content, this);
michael@0 80
michael@0 81 mTextView = (EllipsisTextView) findViewById(R.id.text);
michael@0 82 mIconView = (ImageView) findViewById(R.id.icon);
michael@0 83
michael@0 84 mHeight = getResources().getDimensionPixelSize(R.dimen.home_banner_height);
michael@0 85
michael@0 86 // Disable the banner until a message is set.
michael@0 87 setEnabled(false);
michael@0 88 }
michael@0 89
michael@0 90 @Override
michael@0 91 public void onAttachedToWindow() {
michael@0 92 super.onAttachedToWindow();
michael@0 93
michael@0 94 // Tapping on the close button will ensure that the banner is never
michael@0 95 // showed again on this session.
michael@0 96 final ImageButton closeButton = (ImageButton) findViewById(R.id.close);
michael@0 97
michael@0 98 // The drawable should have 50% opacity.
michael@0 99 closeButton.getDrawable().setAlpha(127);
michael@0 100
michael@0 101 closeButton.setOnClickListener(new View.OnClickListener() {
michael@0 102 @Override
michael@0 103 public void onClick(View view) {
michael@0 104 HomeBanner.this.dismiss();
michael@0 105
michael@0 106 // Send the current message id back to JS.
michael@0 107 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Dismiss", (String) getTag()));
michael@0 108 }
michael@0 109 });
michael@0 110
michael@0 111 setOnClickListener(new View.OnClickListener() {
michael@0 112 @Override
michael@0 113 public void onClick(View v) {
michael@0 114 HomeBanner.this.dismiss();
michael@0 115
michael@0 116 // Send the current message id back to JS.
michael@0 117 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Click", (String) getTag()));
michael@0 118 }
michael@0 119 });
michael@0 120
michael@0 121 GeckoAppShell.getEventDispatcher().registerEventListener("HomeBanner:Data", this);
michael@0 122 }
michael@0 123
michael@0 124 @Override
michael@0 125 public void onDetachedFromWindow() {
michael@0 126 super.onDetachedFromWindow();
michael@0 127
michael@0 128 GeckoAppShell.getEventDispatcher().unregisterEventListener("HomeBanner:Data", this);
michael@0 129 }
michael@0 130
michael@0 131 @Override
michael@0 132 public void setVisibility(int visibility) {
michael@0 133 // On pre-Honeycomb devices, setting the visibility to GONE won't actually
michael@0 134 // hide the view unless we clear animations first.
michael@0 135 if (Build.VERSION.SDK_INT < 11 && visibility == View.GONE) {
michael@0 136 clearAnimation();
michael@0 137 }
michael@0 138
michael@0 139 super.setVisibility(visibility);
michael@0 140 }
michael@0 141
michael@0 142 public void setScrollingPages(boolean scrollingPages) {
michael@0 143 mScrollingPages = scrollingPages;
michael@0 144 }
michael@0 145
michael@0 146 public void setOnDismissListener(OnDismissListener listener) {
michael@0 147 mOnDismissListener = listener;
michael@0 148 }
michael@0 149
michael@0 150 /**
michael@0 151 * Hides and disables the banner.
michael@0 152 */
michael@0 153 private void dismiss() {
michael@0 154 setVisibility(View.GONE);
michael@0 155 setEnabled(false);
michael@0 156
michael@0 157 if (mOnDismissListener != null) {
michael@0 158 mOnDismissListener.onDismiss();
michael@0 159 }
michael@0 160 }
michael@0 161
michael@0 162 /**
michael@0 163 * Sends a message to gecko to request a new banner message. UI is updated in handleMessage.
michael@0 164 */
michael@0 165 public void update() {
michael@0 166 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Get", null));
michael@0 167 }
michael@0 168
michael@0 169 @Override
michael@0 170 public void handleMessage(String event, JSONObject message) {
michael@0 171 final String id = message.optString("id");
michael@0 172 final String text = message.optString("text");
michael@0 173 final String iconURI = message.optString("iconURI");
michael@0 174
michael@0 175 // Don't update the banner if the message doesn't have valid id and text.
michael@0 176 if (TextUtils.isEmpty(id) || TextUtils.isEmpty(text)) {
michael@0 177 return;
michael@0 178 }
michael@0 179
michael@0 180 // Update the banner message on the UI thread.
michael@0 181 ThreadUtils.postToUiThread(new Runnable() {
michael@0 182 @Override
michael@0 183 public void run() {
michael@0 184 // Store the current message id to pass back to JS in the view's OnClickListener.
michael@0 185 setTag(id);
michael@0 186 mTextView.setOriginalText(Html.fromHtml(text));
michael@0 187
michael@0 188 BitmapUtils.getDrawable(getContext(), iconURI, new BitmapUtils.BitmapLoader() {
michael@0 189 @Override
michael@0 190 public void onBitmapFound(final Drawable d) {
michael@0 191 // Hide the image view if we don't have an icon to show.
michael@0 192 if (d == null) {
michael@0 193 mIconView.setVisibility(View.GONE);
michael@0 194 } else {
michael@0 195 mIconView.setImageDrawable(d);
michael@0 196 }
michael@0 197 }
michael@0 198 });
michael@0 199
michael@0 200 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Shown", id));
michael@0 201
michael@0 202 // Enable the banner after a message is set.
michael@0 203 setEnabled(true);
michael@0 204
michael@0 205 // Animate the banner if it is currently active.
michael@0 206 if (mActive) {
michael@0 207 animateUp();
michael@0 208 }
michael@0 209 }
michael@0 210 });
michael@0 211 }
michael@0 212
michael@0 213 public void setActive(boolean active) {
michael@0 214 // No need to animate if not changing
michael@0 215 if (mActive == active) {
michael@0 216 return;
michael@0 217 }
michael@0 218
michael@0 219 mActive = active;
michael@0 220
michael@0 221 // Don't animate if the banner isn't enabled.
michael@0 222 if (!isEnabled()) {
michael@0 223 return;
michael@0 224 }
michael@0 225
michael@0 226 if (active) {
michael@0 227 animateUp();
michael@0 228 } else {
michael@0 229 animateDown();
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 private void ensureVisible() {
michael@0 234 // The banner visibility is set to GONE after it is animated off screen,
michael@0 235 // so we need to make it visible again.
michael@0 236 if (getVisibility() == View.GONE) {
michael@0 237 // Translate the banner off screen before setting it to VISIBLE.
michael@0 238 ViewHelper.setTranslationY(this, mHeight);
michael@0 239 setVisibility(View.VISIBLE);
michael@0 240 }
michael@0 241 }
michael@0 242
michael@0 243 private void animateUp() {
michael@0 244 // Don't try to animate if the user swiped the banner down previously to hide it.
michael@0 245 if (mUserSwipedDown) {
michael@0 246 return;
michael@0 247 }
michael@0 248
michael@0 249 ensureVisible();
michael@0 250
michael@0 251 final PropertyAnimator animator = new PropertyAnimator(100);
michael@0 252 animator.attach(this, Property.TRANSLATION_Y, 0);
michael@0 253 animator.start();
michael@0 254 }
michael@0 255
michael@0 256 private void animateDown() {
michael@0 257 if (ViewHelper.getTranslationY(this) == mHeight) {
michael@0 258 // Hide the banner to avoid intercepting clicks on pre-honeycomb devices.
michael@0 259 setVisibility(View.GONE);
michael@0 260 return;
michael@0 261 }
michael@0 262
michael@0 263 final PropertyAnimator animator = new PropertyAnimator(100);
michael@0 264 animator.attach(this, Property.TRANSLATION_Y, mHeight);
michael@0 265 animator.addPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
michael@0 266 @Override
michael@0 267 public void onPropertyAnimationStart() {
michael@0 268 }
michael@0 269
michael@0 270 @Override
michael@0 271 public void onPropertyAnimationEnd() {
michael@0 272 // Hide the banner to avoid intercepting clicks on pre-honeycomb devices.
michael@0 273 setVisibility(View.GONE);
michael@0 274 }
michael@0 275 });
michael@0 276 animator.start();
michael@0 277 }
michael@0 278
michael@0 279 public void handleHomeTouch(MotionEvent event) {
michael@0 280 if (!mActive || !isEnabled() || mScrollingPages) {
michael@0 281 return;
michael@0 282 }
michael@0 283
michael@0 284 ensureVisible();
michael@0 285
michael@0 286 switch (event.getActionMasked()) {
michael@0 287 case MotionEvent.ACTION_DOWN: {
michael@0 288 // Track the beginning of the touch
michael@0 289 mTouchY = event.getRawY();
michael@0 290 break;
michael@0 291 }
michael@0 292
michael@0 293 case MotionEvent.ACTION_MOVE: {
michael@0 294 final float curY = event.getRawY();
michael@0 295 final float delta = mTouchY - curY;
michael@0 296 mSnapBannerToTop = delta <= 0.0f;
michael@0 297
michael@0 298 float newTranslationY = ViewHelper.getTranslationY(this) + delta;
michael@0 299
michael@0 300 // Clamp the values to be between 0 and height.
michael@0 301 if (newTranslationY < 0.0f) {
michael@0 302 newTranslationY = 0.0f;
michael@0 303 } else if (newTranslationY > mHeight) {
michael@0 304 newTranslationY = mHeight;
michael@0 305 }
michael@0 306
michael@0 307 // Don't change this value if it wasn't a significant movement
michael@0 308 if (delta >= 10 || delta <= -10) {
michael@0 309 mUserSwipedDown = (newTranslationY == mHeight);
michael@0 310 }
michael@0 311
michael@0 312 ViewHelper.setTranslationY(this, newTranslationY);
michael@0 313 mTouchY = curY;
michael@0 314 break;
michael@0 315 }
michael@0 316
michael@0 317 case MotionEvent.ACTION_UP:
michael@0 318 case MotionEvent.ACTION_CANCEL: {
michael@0 319 mTouchY = -1;
michael@0 320 if (mSnapBannerToTop) {
michael@0 321 animateUp();
michael@0 322 } else {
michael@0 323 animateDown();
michael@0 324 mUserSwipedDown = true;
michael@0 325 }
michael@0 326 break;
michael@0 327 }
michael@0 328 }
michael@0 329 }
michael@0 330 }

mercurial