package com.yalantis.ucrop.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.TextView;
import com.yalantis.ucrop.R;
public class SweetAlertDialog extends Dialog {
private View mDialogView;
private AnimationSet mModalInAnim;
private AnimationSet mModalOutAnim;
private Animation mOverlayOutAnim;
private TextView mTitleTextView;
private String mTitleText;
private boolean mCloseFromCancel;
public Context context;
public SweetAlertDialog(Context context) {
super(context, R.style.alert_dialog);
this.context = context;
setCancelable(true);
setCanceledOnTouchOutside(false);
mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mDialogView.setVisibility(View.INVISIBLE);
mDialogView.post(new Runnable() {
@Override
public void run() {
if (mCloseFromCancel) {
SweetAlertDialog.super.cancel();
} else {
SweetAlertDialog.super.dismiss();
}
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
// dialog overlay fade out
mOverlayOutAnim = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
WindowManager.LayoutParams wlp = getWindow().getAttributes();
wlp.alpha = 1 - interpolatedTime;
getWindow().setAttributes(wlp);
}
};
mOverlayOutAnim.setDuration(120);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_alert_dialog);
mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
mTitleTextView = (TextView) findViewById(R.id.title_text);
setTitleText(mTitleText);
}
public String getTitleText() {
return mTitleText;
}
public SweetAlertDialog setTitleText(String text) {
mTitleText = text;
if (mTitleTextView != null && mTitleText != null) {
mTitleTextView.setText(mTitleText);
}
return this;
}
protected void onStart() {
mDialogView.startAnimation(mModalInAnim);
}
/**
* The real Dialog.cancel() will be invoked async-ly after the animation finishes.
*/
@Override
public void cancel() {
dismissWithAnimation(true);
}
/**
* The real Dialog.dismiss() will be invoked async-ly after the animation finishes.
*/
public void dismissWithAnimation() {
dismissWithAnimation(false);
}
private void dismissWithAnimation(boolean fromCancel) {
mCloseFromCancel = fromCancel;
mDialogView.startAnimation(mModalOutAnim);
dismiss();
}
}