1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/HomeBanner.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,330 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.json.JSONException; 1.12 +import org.json.JSONObject; 1.13 +import org.mozilla.gecko.GeckoAppShell; 1.14 +import org.mozilla.gecko.GeckoEvent; 1.15 +import org.mozilla.gecko.R; 1.16 +import org.mozilla.gecko.animation.PropertyAnimator; 1.17 +import org.mozilla.gecko.animation.PropertyAnimator.Property; 1.18 +import org.mozilla.gecko.animation.ViewHelper; 1.19 +import org.mozilla.gecko.gfx.BitmapUtils; 1.20 +import org.mozilla.gecko.util.GeckoEventListener; 1.21 +import org.mozilla.gecko.util.ThreadUtils; 1.22 +import org.mozilla.gecko.widget.EllipsisTextView; 1.23 + 1.24 +import android.content.Context; 1.25 +import android.graphics.drawable.Drawable; 1.26 +import android.os.Build; 1.27 +import android.text.Html; 1.28 +import android.text.Spanned; 1.29 +import android.text.TextUtils; 1.30 +import android.util.AttributeSet; 1.31 +import android.util.Log; 1.32 +import android.view.LayoutInflater; 1.33 +import android.view.MotionEvent; 1.34 +import android.view.View; 1.35 +import android.widget.ImageButton; 1.36 +import android.widget.ImageView; 1.37 +import android.widget.LinearLayout; 1.38 +import android.widget.TextView; 1.39 + 1.40 +public class HomeBanner extends LinearLayout 1.41 + implements GeckoEventListener { 1.42 + private static final String LOGTAG = "GeckoHomeBanner"; 1.43 + 1.44 + // Used for tracking scroll length 1.45 + private float mTouchY = -1; 1.46 + 1.47 + // Used to detect for upwards scroll to push banner all the way up 1.48 + private boolean mSnapBannerToTop; 1.49 + 1.50 + // Tracks whether or not the banner should be shown on the current panel. 1.51 + private boolean mActive = false; 1.52 + 1.53 + // The user is currently swiping between HomePager pages 1.54 + private boolean mScrollingPages = false; 1.55 + 1.56 + // Tracks whether the user swiped the banner down, preventing us from autoshowing when the user 1.57 + // switches back to the default page. 1.58 + private boolean mUserSwipedDown = false; 1.59 + 1.60 + // We must use this custom TextView to address an issue on 2.3 and lower where ellipsized text 1.61 + // will not wrap more than 2 lines. 1.62 + private final EllipsisTextView mTextView; 1.63 + private final ImageView mIconView; 1.64 + 1.65 + // The height of the banner view. 1.66 + private final float mHeight; 1.67 + 1.68 + // Listener that gets called when the banner is dismissed from the close button. 1.69 + private OnDismissListener mOnDismissListener; 1.70 + 1.71 + public interface OnDismissListener { 1.72 + public void onDismiss(); 1.73 + } 1.74 + 1.75 + public HomeBanner(Context context) { 1.76 + this(context, null); 1.77 + } 1.78 + 1.79 + public HomeBanner(Context context, AttributeSet attrs) { 1.80 + super(context, attrs); 1.81 + 1.82 + LayoutInflater.from(context).inflate(R.layout.home_banner_content, this); 1.83 + 1.84 + mTextView = (EllipsisTextView) findViewById(R.id.text); 1.85 + mIconView = (ImageView) findViewById(R.id.icon); 1.86 + 1.87 + mHeight = getResources().getDimensionPixelSize(R.dimen.home_banner_height); 1.88 + 1.89 + // Disable the banner until a message is set. 1.90 + setEnabled(false); 1.91 + } 1.92 + 1.93 + @Override 1.94 + public void onAttachedToWindow() { 1.95 + super.onAttachedToWindow(); 1.96 + 1.97 + // Tapping on the close button will ensure that the banner is never 1.98 + // showed again on this session. 1.99 + final ImageButton closeButton = (ImageButton) findViewById(R.id.close); 1.100 + 1.101 + // The drawable should have 50% opacity. 1.102 + closeButton.getDrawable().setAlpha(127); 1.103 + 1.104 + closeButton.setOnClickListener(new View.OnClickListener() { 1.105 + @Override 1.106 + public void onClick(View view) { 1.107 + HomeBanner.this.dismiss(); 1.108 + 1.109 + // Send the current message id back to JS. 1.110 + GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Dismiss", (String) getTag())); 1.111 + } 1.112 + }); 1.113 + 1.114 + setOnClickListener(new View.OnClickListener() { 1.115 + @Override 1.116 + public void onClick(View v) { 1.117 + HomeBanner.this.dismiss(); 1.118 + 1.119 + // Send the current message id back to JS. 1.120 + GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Click", (String) getTag())); 1.121 + } 1.122 + }); 1.123 + 1.124 + GeckoAppShell.getEventDispatcher().registerEventListener("HomeBanner:Data", this); 1.125 + } 1.126 + 1.127 + @Override 1.128 + public void onDetachedFromWindow() { 1.129 + super.onDetachedFromWindow(); 1.130 + 1.131 + GeckoAppShell.getEventDispatcher().unregisterEventListener("HomeBanner:Data", this); 1.132 + } 1.133 + 1.134 + @Override 1.135 + public void setVisibility(int visibility) { 1.136 + // On pre-Honeycomb devices, setting the visibility to GONE won't actually 1.137 + // hide the view unless we clear animations first. 1.138 + if (Build.VERSION.SDK_INT < 11 && visibility == View.GONE) { 1.139 + clearAnimation(); 1.140 + } 1.141 + 1.142 + super.setVisibility(visibility); 1.143 + } 1.144 + 1.145 + public void setScrollingPages(boolean scrollingPages) { 1.146 + mScrollingPages = scrollingPages; 1.147 + } 1.148 + 1.149 + public void setOnDismissListener(OnDismissListener listener) { 1.150 + mOnDismissListener = listener; 1.151 + } 1.152 + 1.153 + /** 1.154 + * Hides and disables the banner. 1.155 + */ 1.156 + private void dismiss() { 1.157 + setVisibility(View.GONE); 1.158 + setEnabled(false); 1.159 + 1.160 + if (mOnDismissListener != null) { 1.161 + mOnDismissListener.onDismiss(); 1.162 + } 1.163 + } 1.164 + 1.165 + /** 1.166 + * Sends a message to gecko to request a new banner message. UI is updated in handleMessage. 1.167 + */ 1.168 + public void update() { 1.169 + GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Get", null)); 1.170 + } 1.171 + 1.172 + @Override 1.173 + public void handleMessage(String event, JSONObject message) { 1.174 + final String id = message.optString("id"); 1.175 + final String text = message.optString("text"); 1.176 + final String iconURI = message.optString("iconURI"); 1.177 + 1.178 + // Don't update the banner if the message doesn't have valid id and text. 1.179 + if (TextUtils.isEmpty(id) || TextUtils.isEmpty(text)) { 1.180 + return; 1.181 + } 1.182 + 1.183 + // Update the banner message on the UI thread. 1.184 + ThreadUtils.postToUiThread(new Runnable() { 1.185 + @Override 1.186 + public void run() { 1.187 + // Store the current message id to pass back to JS in the view's OnClickListener. 1.188 + setTag(id); 1.189 + mTextView.setOriginalText(Html.fromHtml(text)); 1.190 + 1.191 + BitmapUtils.getDrawable(getContext(), iconURI, new BitmapUtils.BitmapLoader() { 1.192 + @Override 1.193 + public void onBitmapFound(final Drawable d) { 1.194 + // Hide the image view if we don't have an icon to show. 1.195 + if (d == null) { 1.196 + mIconView.setVisibility(View.GONE); 1.197 + } else { 1.198 + mIconView.setImageDrawable(d); 1.199 + } 1.200 + } 1.201 + }); 1.202 + 1.203 + GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("HomeBanner:Shown", id)); 1.204 + 1.205 + // Enable the banner after a message is set. 1.206 + setEnabled(true); 1.207 + 1.208 + // Animate the banner if it is currently active. 1.209 + if (mActive) { 1.210 + animateUp(); 1.211 + } 1.212 + } 1.213 + }); 1.214 + } 1.215 + 1.216 + public void setActive(boolean active) { 1.217 + // No need to animate if not changing 1.218 + if (mActive == active) { 1.219 + return; 1.220 + } 1.221 + 1.222 + mActive = active; 1.223 + 1.224 + // Don't animate if the banner isn't enabled. 1.225 + if (!isEnabled()) { 1.226 + return; 1.227 + } 1.228 + 1.229 + if (active) { 1.230 + animateUp(); 1.231 + } else { 1.232 + animateDown(); 1.233 + } 1.234 + } 1.235 + 1.236 + private void ensureVisible() { 1.237 + // The banner visibility is set to GONE after it is animated off screen, 1.238 + // so we need to make it visible again. 1.239 + if (getVisibility() == View.GONE) { 1.240 + // Translate the banner off screen before setting it to VISIBLE. 1.241 + ViewHelper.setTranslationY(this, mHeight); 1.242 + setVisibility(View.VISIBLE); 1.243 + } 1.244 + } 1.245 + 1.246 + private void animateUp() { 1.247 + // Don't try to animate if the user swiped the banner down previously to hide it. 1.248 + if (mUserSwipedDown) { 1.249 + return; 1.250 + } 1.251 + 1.252 + ensureVisible(); 1.253 + 1.254 + final PropertyAnimator animator = new PropertyAnimator(100); 1.255 + animator.attach(this, Property.TRANSLATION_Y, 0); 1.256 + animator.start(); 1.257 + } 1.258 + 1.259 + private void animateDown() { 1.260 + if (ViewHelper.getTranslationY(this) == mHeight) { 1.261 + // Hide the banner to avoid intercepting clicks on pre-honeycomb devices. 1.262 + setVisibility(View.GONE); 1.263 + return; 1.264 + } 1.265 + 1.266 + final PropertyAnimator animator = new PropertyAnimator(100); 1.267 + animator.attach(this, Property.TRANSLATION_Y, mHeight); 1.268 + animator.addPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() { 1.269 + @Override 1.270 + public void onPropertyAnimationStart() { 1.271 + } 1.272 + 1.273 + @Override 1.274 + public void onPropertyAnimationEnd() { 1.275 + // Hide the banner to avoid intercepting clicks on pre-honeycomb devices. 1.276 + setVisibility(View.GONE); 1.277 + } 1.278 + }); 1.279 + animator.start(); 1.280 + } 1.281 + 1.282 + public void handleHomeTouch(MotionEvent event) { 1.283 + if (!mActive || !isEnabled() || mScrollingPages) { 1.284 + return; 1.285 + } 1.286 + 1.287 + ensureVisible(); 1.288 + 1.289 + switch (event.getActionMasked()) { 1.290 + case MotionEvent.ACTION_DOWN: { 1.291 + // Track the beginning of the touch 1.292 + mTouchY = event.getRawY(); 1.293 + break; 1.294 + } 1.295 + 1.296 + case MotionEvent.ACTION_MOVE: { 1.297 + final float curY = event.getRawY(); 1.298 + final float delta = mTouchY - curY; 1.299 + mSnapBannerToTop = delta <= 0.0f; 1.300 + 1.301 + float newTranslationY = ViewHelper.getTranslationY(this) + delta; 1.302 + 1.303 + // Clamp the values to be between 0 and height. 1.304 + if (newTranslationY < 0.0f) { 1.305 + newTranslationY = 0.0f; 1.306 + } else if (newTranslationY > mHeight) { 1.307 + newTranslationY = mHeight; 1.308 + } 1.309 + 1.310 + // Don't change this value if it wasn't a significant movement 1.311 + if (delta >= 10 || delta <= -10) { 1.312 + mUserSwipedDown = (newTranslationY == mHeight); 1.313 + } 1.314 + 1.315 + ViewHelper.setTranslationY(this, newTranslationY); 1.316 + mTouchY = curY; 1.317 + break; 1.318 + } 1.319 + 1.320 + case MotionEvent.ACTION_UP: 1.321 + case MotionEvent.ACTION_CANCEL: { 1.322 + mTouchY = -1; 1.323 + if (mSnapBannerToTop) { 1.324 + animateUp(); 1.325 + } else { 1.326 + animateDown(); 1.327 + mUserSwipedDown = true; 1.328 + } 1.329 + break; 1.330 + } 1.331 + } 1.332 + } 1.333 +}