|
| 1 | +package com.beardedhen.androidbootstrap; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Rect; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.os.Build; |
| 8 | +import android.text.Html; |
| 9 | +import android.util.AttributeSet; |
| 10 | +import android.view.Gravity; |
| 11 | +import android.view.TouchDelegate; |
| 12 | +import android.view.View; |
| 13 | +import android.view.animation.AccelerateInterpolator; |
| 14 | +import android.view.animation.AlphaAnimation; |
| 15 | +import android.view.animation.Animation; |
| 16 | +import android.widget.ImageView; |
| 17 | +import android.widget.RelativeLayout; |
| 18 | +import android.widget.TextView; |
| 19 | + |
| 20 | +import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand; |
| 21 | +import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand; |
| 22 | +import com.beardedhen.androidbootstrap.utils.DimenUtils; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | + |
| 26 | +public class BootstrapAlert extends RelativeLayout { |
| 27 | + |
| 28 | + private ImageView closeButton; |
| 29 | + private TextView alertText; |
| 30 | + |
| 31 | + private BootstrapBrand bootstrapBrand; |
| 32 | + |
| 33 | + private String strongText; |
| 34 | + private String messageText; |
| 35 | + |
| 36 | + private float baselineFontSize; |
| 37 | + private float baselinePadding; |
| 38 | + |
| 39 | + private AlphaAnimation fadeInAnimation; |
| 40 | + private AlphaAnimation fadeOutAnimation; |
| 41 | + |
| 42 | + private boolean dismissible; |
| 43 | + private boolean hidden; |
| 44 | + |
| 45 | + private OnDismissListener onDismissListener; |
| 46 | + |
| 47 | + private static final AtomicInteger nextGeneratedId = new AtomicInteger(1); |
| 48 | + |
| 49 | + public BootstrapAlert(Context context) { |
| 50 | + super(context); |
| 51 | + initialise(null); |
| 52 | + } |
| 53 | + |
| 54 | + public BootstrapAlert(Context context, AttributeSet attrs) { |
| 55 | + super(context, attrs); |
| 56 | + initialise(attrs); |
| 57 | + } |
| 58 | + |
| 59 | + public BootstrapAlert(Context context, AttributeSet attrs, int defStyleAttr) { |
| 60 | + super(context, attrs, defStyleAttr); |
| 61 | + initialise(attrs); |
| 62 | + } |
| 63 | + |
| 64 | + private void initialise(AttributeSet attrs) { |
| 65 | + TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BootstrapAlert); |
| 66 | + |
| 67 | + try { |
| 68 | + int typeOrdinal = a.getInt(R.styleable.BootstrapAlert_bootstrapBrand, -1); |
| 69 | + |
| 70 | + this.bootstrapBrand = DefaultBootstrapBrand.fromAttributeValue(typeOrdinal); |
| 71 | + |
| 72 | + strongText = a.getString(R.styleable.BootstrapAlert_strongText); |
| 73 | + messageText = a.getString(R.styleable.BootstrapAlert_messageText); |
| 74 | + dismissible = a.getBoolean(R.styleable.BootstrapAlert_dismissible, false); |
| 75 | + if (strongText == null) { |
| 76 | + strongText = ""; |
| 77 | + } |
| 78 | + if (messageText == null) { |
| 79 | + messageText = ""; |
| 80 | + } |
| 81 | + } finally { |
| 82 | + a.recycle(); |
| 83 | + } |
| 84 | + |
| 85 | + baselineFontSize = DimenUtils.pixelsFromSpResource(getContext(), R.dimen.bootstrap_button_default_font_size); |
| 86 | + baselinePadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_alert_paddings); |
| 87 | + |
| 88 | + updateBootstrapState(); |
| 89 | + } |
| 90 | + |
| 91 | + private void updateBootstrapState() { |
| 92 | + alertText = new TextView(getContext()); |
| 93 | + closeButton = new ImageView(getContext()); |
| 94 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 95 | + alertText.setId(generateViewUniqueId()); |
| 96 | + closeButton.setId(generateViewUniqueId()); |
| 97 | + } else { |
| 98 | + alertText.setId(View.generateViewId()); |
| 99 | + closeButton.setId(View.generateViewId()); |
| 100 | + } |
| 101 | + fadeInAnimation = new AlphaAnimation(0.0f, 1.0f); |
| 102 | + fadeInAnimation.setDuration(300); |
| 103 | + fadeInAnimation.setInterpolator(new AccelerateInterpolator()); |
| 104 | + fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f); |
| 105 | + fadeOutAnimation.setDuration(300); |
| 106 | + fadeOutAnimation.setInterpolator(new AccelerateInterpolator()); |
| 107 | + |
| 108 | + fadeInAnimation.setAnimationListener(new Animation.AnimationListener() { |
| 109 | + @Override public void onAnimationStart(Animation animation) {setVisibility(VISIBLE);} |
| 110 | + @Override public void onAnimationEnd(Animation animation) {} |
| 111 | + @Override public void onAnimationRepeat(Animation animation) {} |
| 112 | + }); |
| 113 | + |
| 114 | + fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() { |
| 115 | + @Override public void onAnimationStart(Animation animation) {} |
| 116 | + @Override public void onAnimationEnd(Animation animation) { |
| 117 | + setVisibility(GONE); |
| 118 | + if (onDismissListener != null) onDismissListener.onDismiss(); |
| 119 | + } |
| 120 | + @Override public void onAnimationRepeat(Animation animation) {} |
| 121 | + }); |
| 122 | + |
| 123 | + LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 124 | + LayoutParams closeParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 125 | + textParams.addRule(RelativeLayout.LEFT_OF, closeButton.getId()); |
| 126 | + closeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); |
| 127 | + |
| 128 | + alertText.setLayoutParams(textParams); |
| 129 | + alertText.setTextSize(baselineFontSize); |
| 130 | + alertText.setGravity(Gravity.LEFT); |
| 131 | + alertText.setTextColor(BootstrapDrawableFactory.bootstrapButtonText( |
| 132 | + getContext(), |
| 133 | + true, |
| 134 | + bootstrapBrand)); |
| 135 | + alertText.setText(Html.fromHtml(String.format("<b>%s</b>%s", strongText, (strongText.length() > 0 ? " " + messageText : messageText)))); |
| 136 | + |
| 137 | + closeButton.setLayoutParams(closeParams); |
| 138 | + closeButton.setBackgroundDrawable(BootstrapDrawableFactory.bootstrapAlertCloseIcon( |
| 139 | + getContext(), |
| 140 | + (int) baselineFontSize, |
| 141 | + (int) baselineFontSize, |
| 142 | + DimenUtils.dpToPixels(6))); |
| 143 | + |
| 144 | + Drawable bg = BootstrapDrawableFactory.bootstrapAlert( |
| 145 | + getContext(), |
| 146 | + bootstrapBrand); |
| 147 | + |
| 148 | + if (Build.VERSION.SDK_INT >= 16) { |
| 149 | + setBackground(bg); |
| 150 | + } else { |
| 151 | + setBackgroundDrawable(bg); |
| 152 | + } |
| 153 | + addView(alertText); |
| 154 | + if (dismissible) { |
| 155 | + addView(closeButton); |
| 156 | + ((View) closeButton.getParent()).post(new Runnable() { |
| 157 | + @Override |
| 158 | + public void run() { |
| 159 | + Rect bounds = new Rect(); |
| 160 | + closeButton.getHitRect(bounds); |
| 161 | + bounds.top -= DimenUtils.dpToPixels(6); |
| 162 | + bounds.bottom += DimenUtils.dpToPixels(6); |
| 163 | + bounds.left -= DimenUtils.dpToPixels(6); |
| 164 | + bounds.right += DimenUtils.dpToPixels(6); |
| 165 | + TouchDelegate touchDelegate = new TouchDelegate(bounds, closeButton); |
| 166 | + if (View.class.isInstance(closeButton.getParent())) { |
| 167 | + ((View) closeButton.getParent()).setTouchDelegate(touchDelegate); |
| 168 | + } |
| 169 | + } |
| 170 | + }); |
| 171 | + closeButton.setOnClickListener( |
| 172 | + new OnClickListener() { |
| 173 | + @Override public void onClick(View v) { |
| 174 | + hide(); |
| 175 | + } |
| 176 | + } |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + int vert = (int) (baselinePadding * 1.5); |
| 181 | + int hori = (int) (baselinePadding * 1.5); |
| 182 | + setPadding(hori, vert, hori, vert); |
| 183 | + } |
| 184 | + |
| 185 | + public void hide() { |
| 186 | + hidden = true; |
| 187 | + startAnimation(fadeOutAnimation); |
| 188 | + } |
| 189 | + |
| 190 | + public void show() { |
| 191 | + hidden = false; |
| 192 | + startAnimation(fadeInAnimation); |
| 193 | + } |
| 194 | + |
| 195 | + public boolean isHidden() { |
| 196 | + return hidden; |
| 197 | + } |
| 198 | + |
| 199 | + public void setOnDismissListener(OnDismissListener onDismissListener) { |
| 200 | + this.onDismissListener = onDismissListener; |
| 201 | + } |
| 202 | + |
| 203 | + private int generateViewUniqueId() { |
| 204 | + for (;;) { |
| 205 | + final int result = nextGeneratedId.get(); |
| 206 | + // aapt-generated IDs have the high byte nonzero; clamp to the range under that. |
| 207 | + int newValue = result + 1; |
| 208 | + if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. |
| 209 | + if (nextGeneratedId.compareAndSet(result, newValue)) { |
| 210 | + return result; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public interface OnDismissListener { |
| 216 | + void onDismiss(); |
| 217 | + } |
| 218 | +} |
0 commit comments