mobile/android/base/widget/ButtonToast.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright 2012 Roman Nurik
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.widget;
michael@0 18
michael@0 19 import java.util.LinkedList;
michael@0 20
michael@0 21 import org.mozilla.gecko.R;
michael@0 22 import org.mozilla.gecko.animation.PropertyAnimator;
michael@0 23
michael@0 24 import android.graphics.drawable.Drawable;
michael@0 25 import android.os.Bundle;
michael@0 26 import android.os.Handler;
michael@0 27 import android.view.View;
michael@0 28 import android.widget.Button;
michael@0 29 import android.widget.TextView;
michael@0 30
michael@0 31 public class ButtonToast {
michael@0 32 private final static String LOGTAG = "GeckoButtonToast";
michael@0 33 private final static int TOAST_DURATION = 5000;
michael@0 34
michael@0 35 private final View mView;
michael@0 36 private final TextView mMessageView;
michael@0 37 private final Button mButton;
michael@0 38 private final Handler mHideHandler = new Handler();
michael@0 39
michael@0 40 private final ToastListener mListener;
michael@0 41 private final LinkedList<Toast> mQueue = new LinkedList<Toast>();
michael@0 42 private Toast mCurrentToast;
michael@0 43
michael@0 44 public enum ReasonHidden {
michael@0 45 CLICKED,
michael@0 46 TIMEOUT,
michael@0 47 STARTUP
michael@0 48 }
michael@0 49
michael@0 50 // State objects
michael@0 51 private static class Toast {
michael@0 52 public final CharSequence buttonMessage;
michael@0 53 public Drawable buttonDrawable;
michael@0 54 public final CharSequence message;
michael@0 55 public ToastListener listener;
michael@0 56
michael@0 57 public Toast(CharSequence aMessage, CharSequence aButtonMessage,
michael@0 58 Drawable aDrawable, ToastListener aListener) {
michael@0 59 message = aMessage;
michael@0 60 buttonMessage = aButtonMessage;
michael@0 61 buttonDrawable = aDrawable;
michael@0 62 listener = aListener;
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 public interface ToastListener {
michael@0 67 void onButtonClicked();
michael@0 68 void onToastHidden(ReasonHidden reason);
michael@0 69 }
michael@0 70
michael@0 71 public ButtonToast(View view) {
michael@0 72 mView = view;
michael@0 73 mListener = null;
michael@0 74 mMessageView = (TextView) mView.findViewById(R.id.toast_message);
michael@0 75 mButton = (Button) mView.findViewById(R.id.toast_button);
michael@0 76 mButton.setOnClickListener(new View.OnClickListener() {
michael@0 77 @Override
michael@0 78 public void onClick(View view) {
michael@0 79 Toast t = mCurrentToast;
michael@0 80 if (t == null)
michael@0 81 return;
michael@0 82
michael@0 83 hide(false, ReasonHidden.CLICKED);
michael@0 84 if (t.listener != null) {
michael@0 85 t.listener.onButtonClicked();
michael@0 86 }
michael@0 87 }
michael@0 88 });
michael@0 89
michael@0 90 hide(true, ReasonHidden.STARTUP);
michael@0 91 }
michael@0 92
michael@0 93 public void show(boolean immediate, CharSequence message,
michael@0 94 CharSequence buttonMessage, Drawable buttonDrawable,
michael@0 95 ToastListener listener) {
michael@0 96 show(new Toast(message, buttonMessage, buttonDrawable, listener), immediate);
michael@0 97 }
michael@0 98
michael@0 99 private void show(Toast t, boolean immediate) {
michael@0 100 // If we're already showing a toast, add this one to the queue to show later
michael@0 101 if (mView.getVisibility() == View.VISIBLE) {
michael@0 102 mQueue.offer(t);
michael@0 103 return;
michael@0 104 }
michael@0 105
michael@0 106 mCurrentToast = t;
michael@0 107 mButton.setEnabled(true);
michael@0 108
michael@0 109 mMessageView.setText(t.message);
michael@0 110 mButton.setText(t.buttonMessage);
michael@0 111 mButton.setCompoundDrawablePadding(mView.getContext().getResources().getDimensionPixelSize(R.dimen.toast_button_padding));
michael@0 112 mButton.setCompoundDrawablesWithIntrinsicBounds(t.buttonDrawable, null, null, null);
michael@0 113
michael@0 114 mHideHandler.removeCallbacks(mHideRunnable);
michael@0 115 mHideHandler.postDelayed(mHideRunnable, TOAST_DURATION);
michael@0 116
michael@0 117 mView.setVisibility(View.VISIBLE);
michael@0 118 int duration = immediate ? 0 : mView.getResources().getInteger(android.R.integer.config_longAnimTime);
michael@0 119
michael@0 120 PropertyAnimator animator = new PropertyAnimator(duration);
michael@0 121 animator.attach(mView, PropertyAnimator.Property.ALPHA, 1.0f);
michael@0 122 animator.start();
michael@0 123 }
michael@0 124
michael@0 125 public void hide(boolean immediate, ReasonHidden reason) {
michael@0 126 if (mButton.isPressed() && reason != ReasonHidden.CLICKED) {
michael@0 127 mHideHandler.postDelayed(mHideRunnable, TOAST_DURATION);
michael@0 128 return;
michael@0 129 }
michael@0 130
michael@0 131 if (mCurrentToast != null && mCurrentToast.listener != null) {
michael@0 132 mCurrentToast.listener.onToastHidden(reason);
michael@0 133 }
michael@0 134 mCurrentToast = null;
michael@0 135 mButton.setEnabled(false);
michael@0 136 mHideHandler.removeCallbacks(mHideRunnable);
michael@0 137 int duration = immediate ? 0 : mView.getResources().getInteger(android.R.integer.config_longAnimTime);
michael@0 138
michael@0 139 mView.clearAnimation();
michael@0 140 if (immediate) {
michael@0 141 mView.setVisibility(View.GONE);
michael@0 142 showNextInQueue();
michael@0 143 } else {
michael@0 144 // Using Android's animation frameworks will not correctly turn off clicking.
michael@0 145 // See bug 885717.
michael@0 146 PropertyAnimator animator = new PropertyAnimator(duration);
michael@0 147 animator.attach(mView, PropertyAnimator.Property.ALPHA, 0.0f);
michael@0 148 animator.addPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener () {
michael@0 149 // If we are showing a toast and go in the background
michael@0 150 // onAnimationEnd will be called when the app is restored
michael@0 151 public void onPropertyAnimationEnd() {
michael@0 152 mView.setVisibility(View.GONE);
michael@0 153 showNextInQueue();
michael@0 154 }
michael@0 155 public void onPropertyAnimationStart() { }
michael@0 156 });
michael@0 157 animator.start();
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 public void onSaveInstanceState(Bundle outState) {
michael@0 162 // Add whatever toast we're currently showing to the front of the queue
michael@0 163 if (mCurrentToast != null) {
michael@0 164 mQueue.add(0, mCurrentToast);
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 private void showNextInQueue() {
michael@0 169 Toast t = mQueue.poll();
michael@0 170 if (t != null) {
michael@0 171 show(t, false);
michael@0 172 }
michael@0 173 }
michael@0 174
michael@0 175 private Runnable mHideRunnable = new Runnable() {
michael@0 176 @Override
michael@0 177 public void run() {
michael@0 178 hide(false, ReasonHidden.TIMEOUT);
michael@0 179 }
michael@0 180 };
michael@0 181 }

mercurial