Skip to content

Upstream/master #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions AndroidBootstrap/build.gradle
Original file line number Diff line number Diff line change
@@ -3,17 +3,17 @@ apply from: 'push.gradle'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 11
minSdkVersion 14
targetSdkVersion 23
versionCode = Integer.parseInt(VERSION_CODE)
versionName = VERSION_NAME
}
}

dependencies {
compile 'com.android.support:support-annotations:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:support-annotations:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.beardedhen.androidbootstrap;

/**
* Any class marked with this annotation is subject to Major API changes which may cause client code
* to break between releases. Use with caution!!!
*/
public @interface BetaApi {}
Original file line number Diff line number Diff line change
@@ -20,13 +20,47 @@
import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.androidbootstrap.utils.DimenUtils;
import com.beardedhen.androidbootstrap.utils.ViewUtils;

import java.util.concurrent.atomic.AtomicInteger;

public class BootstrapAlert extends RelativeLayout {
/**
* See <a href="http://getbootstrap.com/components/#badges>http://getbootstrap.com/components/#alerts</a>
*/
@BetaApi
public class BootstrapAlert extends RelativeLayout implements Animation.AnimationListener {

/**
* Provides methods which receive callbacks in response to changes in the view visibility.
*/
public interface VisibilityChangeListener{

/**
* Called when the alert is set to be dismissed with an animation.
* @param alert the alert
*/
void onAlertDismissStarted(BootstrapAlert alert);

/**
* Called when the alert is no longer visible.
* @param alert the alert
*/
void onAlertDismissCompletion(BootstrapAlert alert);

/**
* Called when the alert set to appear with an animation
* @param alert the alert
*/
void onAlertAppearStarted(BootstrapAlert alert);

/**
* Called when the alert is now visible.
* @param alert the alert
*/
void onAlertAppearCompletion(BootstrapAlert alert);
}

private ImageView closeButton;
private TextView alertText;

private BootstrapBrand bootstrapBrand;

@@ -39,10 +73,9 @@ public class BootstrapAlert extends RelativeLayout {
private AlphaAnimation fadeInAnimation;
private AlphaAnimation fadeOutAnimation;

private boolean dismissible;
private boolean hidden;
private boolean userDismissible;

private OnDismissListener onDismissListener;
private VisibilityChangeListener visibilityChangeListener;

private static final AtomicInteger nextGeneratedId = new AtomicInteger(1);

@@ -71,148 +104,221 @@ private void initialise(AttributeSet attrs) {

strongText = a.getString(R.styleable.BootstrapAlert_strongText);
messageText = a.getString(R.styleable.BootstrapAlert_messageText);
dismissible = a.getBoolean(R.styleable.BootstrapAlert_dismissible, false);
userDismissible = a.getBoolean(R.styleable.BootstrapAlert_dismissible, false);

if (strongText == null) {
strongText = "";
}
if (messageText == null) {
messageText = "";
}
} finally {
}
finally {
a.recycle();
}

baselineFontSize = DimenUtils.pixelsFromSpResource(getContext(), R.dimen.bootstrap_button_default_font_size);
baselinePadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_alert_paddings);

baselineFontSize = DimenUtils.pixelsFromSpResource(getContext(),
R.dimen.bootstrap_button_default_font_size);
baselinePadding = DimenUtils.pixelsFromDpResource(getContext(),
R.dimen.bootstrap_alert_paddings);
setupAnimations();
updateBootstrapState();
}

private void updateBootstrapState() {
alertText = new TextView(getContext());
TextView alertText = new TextView(getContext());
closeButton = new ImageView(getContext());

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
alertText.setId(generateViewUniqueId());
closeButton.setId(generateViewUniqueId());
} else {
}
else {
alertText.setId(View.generateViewId());
closeButton.setId(View.generateViewId());
}
fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
fadeInAnimation.setDuration(300);
fadeInAnimation.setInterpolator(new AccelerateInterpolator());
fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
fadeOutAnimation.setDuration(300);
fadeOutAnimation.setInterpolator(new AccelerateInterpolator());

fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) {setVisibility(VISIBLE);}
@Override public void onAnimationEnd(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
});

fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationEnd(Animation animation) {
setVisibility(GONE);
if (onDismissListener != null) onDismissListener.onDismiss();
}
@Override public void onAnimationRepeat(Animation animation) {}
});

LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams closeParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
LayoutParams closeParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.LEFT_OF, closeButton.getId());
closeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

alertText.setLayoutParams(textParams);
alertText.setTextSize(baselineFontSize);
alertText.setGravity(Gravity.LEFT);
alertText.setTextColor(BootstrapDrawableFactory.bootstrapButtonText(
getContext(),
true,
bootstrapBrand));
alertText.setText(Html.fromHtml(String.format("<b>%s</b>%s", strongText, (strongText.length() > 0 ? "&nbsp;" + messageText : messageText))));
alertText.setGravity(Gravity.START);
alertText.setTextColor(
BootstrapDrawableFactory.bootstrapButtonText(getContext(), true, bootstrapBrand));
alertText.setText(Html.fromHtml(String.format("<b>%s</b>%s", strongText,
(strongText.length() > 0 ?
"&nbsp;" + messageText :
messageText))));

closeButton.setLayoutParams(closeParams);
closeButton.setBackgroundDrawable(BootstrapDrawableFactory.bootstrapAlertCloseIcon(
getContext(),
(int) baselineFontSize,
(int) baselineFontSize,
DimenUtils.dpToPixels(6)));

Drawable bg = BootstrapDrawableFactory.bootstrapAlert(
getContext(),
bootstrapBrand);

if (Build.VERSION.SDK_INT >= 16) {
setBackground(bg);
} else {
setBackgroundDrawable(bg);
}
Drawable buttonBg = BootstrapDrawableFactory.bootstrapAlertCloseIcon(
getContext(), (int) baselineFontSize, (int) baselineFontSize,
DimenUtils.dpToPixels(6));

ViewUtils.setBackgroundDrawable(closeButton, buttonBg);

Drawable bg = BootstrapDrawableFactory.bootstrapAlert(getContext(), bootstrapBrand);
ViewUtils.setBackgroundDrawable(this, bg);

addView(alertText);
if (dismissible) {

if (userDismissible) {
addView(closeButton);
((View) closeButton.getParent()).post(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
closeButton.getHitRect(bounds);
bounds.top -= DimenUtils.dpToPixels(6);
bounds.top -= DimenUtils.dpToPixels(6);
bounds.bottom += DimenUtils.dpToPixels(6);
bounds.left -= DimenUtils.dpToPixels(6);
bounds.right += DimenUtils.dpToPixels(6);
bounds.left -= DimenUtils.dpToPixels(6);
bounds.right += DimenUtils.dpToPixels(6);
TouchDelegate touchDelegate = new TouchDelegate(bounds, closeButton);
if (View.class.isInstance(closeButton.getParent())) {
((View) closeButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
closeButton.setOnClickListener(
new OnClickListener() {
@Override public void onClick(View v) {
hide();
}
}
);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss(true);
}
});
}

int vert = (int) (baselinePadding * 1.5);
int hori = (int) (baselinePadding * 1.5);
setPadding(hori, vert, hori, vert);
}

public void hide() {
hidden = true;
startAnimation(fadeOutAnimation);
private void setupAnimations() {
fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
fadeInAnimation.setDuration(300);
fadeInAnimation.setInterpolator(new AccelerateInterpolator());
fadeInAnimation.setAnimationListener(this);

fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
fadeOutAnimation.setDuration(300);
fadeOutAnimation.setInterpolator(new AccelerateInterpolator());
fadeOutAnimation.setAnimationListener(this);
}

/**
* Hides the alert with an animation, setting its visibility to {@link View#GONE}
* @param animated whether the dismissal should be animated or not
*/
public void dismiss(boolean animated) {
if (animated) {
if (visibilityChangeListener != null) {
visibilityChangeListener.onAlertDismissStarted(this);
}
startAnimation(fadeOutAnimation);
}
else {
setVisibility(GONE);
}
}

public void show() {
hidden = false;
startAnimation(fadeInAnimation);
/**
* Shows the alert with an animation, setting its visibility to {@link View#VISIBLE}
* @param animated whether the appearance should be animated or not
*/
public void show(boolean animated) {
if (animated) {
if (visibilityChangeListener != null) {
visibilityChangeListener.onAlertAppearStarted(this);
}
startAnimation(fadeInAnimation);
}
else {
setVisibility(VISIBLE);
}
}

public boolean isHidden() {
return hidden;
/**
* Retrieves whether the user can dismiss the dialog or not
* @return true if dismissable
*/
public boolean isUserDismissible() {
return userDismissible;
}

public void setOnDismissListener(OnDismissListener onDismissListener) {
this.onDismissListener = onDismissListener;
/**
* Sets whether the user can dismiss the dialog or not.
* @param userDismissible true if dismissable
*/
public void setUserDismissible(boolean userDismissible) {
this.userDismissible = userDismissible;
updateBootstrapState();
}

/**
* Sets a {@link VisibilityChangeListener} that will be notified on changes
*
* @param visibilityChangeListener the listener
*/
public void setVisibilityChangeListener(VisibilityChangeListener visibilityChangeListener) {
this.visibilityChangeListener = visibilityChangeListener;
}

private int generateViewUniqueId() {
for (;;) {
for (; ; ) {
final int result = nextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (newValue > 0x00FFFFFF) {
newValue = 1; // Roll over to 1, not 0.
}
if (nextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}

public interface OnDismissListener {
void onDismiss();
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);

if (visibilityChangeListener != null) {
if (GONE == visibility) {
visibilityChangeListener.onAlertDismissCompletion(this);
}
else if (VISIBLE == visibility) {
visibilityChangeListener.onAlertAppearCompletion(this);
}
}
}

// Animation change listener

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

if (animation == fadeInAnimation) {
setVisibility(VISIBLE);
}
else if (animation == fadeOutAnimation) {
setVisibility(GONE);
}
else {
throw new IllegalStateException("Unsupported animation attempted to use this listener");
}
}

@Override
public void onAnimationRepeat(Animation animation) {

}
}
Loading