diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..83915d7 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/OptAnimationLoader.java b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/OptAnimationLoader.java index f3c3767..bcef69c 100644 --- a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/OptAnimationLoader.java +++ b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/OptAnimationLoader.java @@ -22,22 +22,13 @@ public class OptAnimationLoader { public static Animation loadAnimation(Context context, int id) throws Resources.NotFoundException { - XmlResourceParser parser = null; - try { - parser = context.getResources().getAnimation(id); + try (XmlResourceParser parser = context.getResources().getAnimation(id)) { return createAnimationFromXml(context, parser); - } catch (XmlPullParserException ex) { + } catch (XmlPullParserException | IOException ex) { Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id)); rnf.initCause(ex); throw rnf; - } catch (IOException ex) { - Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + - Integer.toHexString(id)); - rnf.initCause(ex); - throw rnf; - } finally { - if (parser != null) parser.close(); } } @@ -65,23 +56,30 @@ private static Animation createAnimationFromXml(Context c, XmlPullParser parser, String name = parser.getName(); - if (name.equals("set")) { - anim = new AnimationSet(c, attrs); - createAnimationFromXml(c, parser, (AnimationSet)anim, attrs); - } else if (name.equals("alpha")) { - anim = new AlphaAnimation(c, attrs); - } else if (name.equals("scale")) { - anim = new ScaleAnimation(c, attrs); - } else if (name.equals("rotate")) { - anim = new RotateAnimation(c, attrs); - } else if (name.equals("translate")) { - anim = new TranslateAnimation(c, attrs); - } else { - try { - anim = (Animation) Class.forName(name).getConstructor(Context.class, AttributeSet.class).newInstance(c, attrs); - } catch (Exception te) { - throw new RuntimeException("Unknown animation name: " + parser.getName() + " error:" + te.getMessage()); - } + switch (name) { + case "set": + anim = new AnimationSet(c, attrs); + createAnimationFromXml(c, parser, (AnimationSet) anim, attrs); + break; + case "alpha": + anim = new AlphaAnimation(c, attrs); + break; + case "scale": + anim = new ScaleAnimation(c, attrs); + break; + case "rotate": + anim = new RotateAnimation(c, attrs); + break; + case "translate": + anim = new TranslateAnimation(c, attrs); + break; + default: + try { + anim = (Animation) Class.forName(name).getConstructor(Context.class, AttributeSet.class).newInstance(c, attrs); + } catch (Exception te) { + throw new RuntimeException("Unknown animation name: " + parser.getName() + " error:" + te.getMessage()); + } + break; } if (parent != null) { diff --git a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/Rotate3dAnimation.java b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/Rotate3dAnimation.java index c0766fa..9a860f3 100644 --- a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/Rotate3dAnimation.java +++ b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/Rotate3dAnimation.java @@ -15,12 +15,12 @@ public class Rotate3dAnimation extends Animation { private float mPivotXValue = 0.0f; private float mPivotYValue = 0.0f; - private float mFromDegrees; - private float mToDegrees; + private final float mFromDegrees; + private final float mToDegrees; private float mPivotX; private float mPivotY; private Camera mCamera; - private int mRollType; + private final int mRollType; public static final int ROLL_BY_X = 0; public static final int ROLL_BY_Y = 1; diff --git a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SuccessTickView.java b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SuccessTickView.java index ffe1bc4..31fd9c4 100644 --- a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SuccessTickView.java +++ b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SuccessTickView.java @@ -9,6 +9,8 @@ import android.view.animation.Animation; import android.view.animation.Transformation; +import androidx.annotation.NonNull; + public class SuccessTickView extends View { private float mDensity = -1; private Paint mPaint; @@ -43,29 +45,25 @@ private void init () { } @Override - public void draw(Canvas canvas) { + public void draw(@NonNull Canvas canvas) { super.draw(canvas); int totalW = getWidth(); int totalH = getHeight(); // rotate canvas first - canvas.rotate(45, totalW / 2, totalH / 2); + canvas.rotate(45, (float) totalW / 2, (float) totalH / 2); - totalW /= 1.2; - totalH /= 1.4; mMaxLeftRectWidth = (totalW + CONST_LEFT_RECT_W) / 2 + CONST_RECT_WEIGHT - 1; RectF leftRect = new RectF(); if (mLeftRectGrowMode) { leftRect.left = 0; leftRect.right = leftRect.left + mLeftRectWidth; - leftRect.top = (totalH + CONST_RIGHT_RECT_W) / 2; - leftRect.bottom = leftRect.top + CONST_RECT_WEIGHT; } else { leftRect.right = (totalW + CONST_LEFT_RECT_W) / 2 + CONST_RECT_WEIGHT - 1; leftRect.left = leftRect.right - mLeftRectWidth; - leftRect.top = (totalH + CONST_RIGHT_RECT_W) / 2; - leftRect.bottom = leftRect.top + CONST_RECT_WEIGHT; } + leftRect.top = (totalH + CONST_RIGHT_RECT_W) / 2; + leftRect.bottom = leftRect.top + CONST_RECT_WEIGHT; canvas.drawRoundRect(leftRect, CONST_RADIUS, CONST_RADIUS, mPaint); @@ -103,7 +101,7 @@ protected void applyTransformation(float interpolatedTime, Transformation t) { } else if (0.7 < interpolatedTime && 0.84 >= interpolatedTime) { // shorten left rect from right, still grow right rect mLeftRectGrowMode = false; mLeftRectWidth = mMaxLeftRectWidth * (1 - ((interpolatedTime - 0.7f) / 0.14f)); - mLeftRectWidth = mLeftRectWidth < MIN_LEFT_RECT_W ? MIN_LEFT_RECT_W : mLeftRectWidth; + mLeftRectWidth = Math.max(mLeftRectWidth, MIN_LEFT_RECT_W); mRightRectWidth = MAX_RIGHT_RECT_W * ((interpolatedTime - 0.65f) / 0.19f); invalidate(); } else if (0.84 < interpolatedTime && 1 >= interpolatedTime) { // restore left rect width, shorten right rect to const diff --git a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SweetAlertDialog.java b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SweetAlertDialog.java index 0da0db4..68d8c2c 100644 --- a/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SweetAlertDialog.java +++ b/SweetAlert/src/main/java/cn/samnyandoro/sweetalert/SweetAlertDialog.java @@ -3,11 +3,9 @@ import android.app.Dialog; import android.content.Context; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.WindowManager; -import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.Transformation; @@ -15,19 +13,16 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; -import com.materialprogress.mylibrary.ProgressWheel; - -import java.util.List; public class SweetAlertDialog extends Dialog implements View.OnClickListener { private View mDialogView; - private AnimationSet mModalInAnim; - private AnimationSet mModalOutAnim; - private Animation mOverlayOutAnim; - private Animation mErrorInAnim; - private AnimationSet mErrorXInAnim; - private AnimationSet mSuccessLayoutAnimSet; - private Animation mSuccessBowAnim; + private final AnimationSet mModalInAnim; + private final AnimationSet mModalOutAnim; + private final Animation mOverlayOutAnim; + private final Animation mErrorInAnim; + private final AnimationSet mErrorXInAnim; + private final AnimationSet mSuccessLayoutAnimSet; + private final Animation mSuccessBowAnim; private TextView mTitleTextView; private TextView mContentTextView; private String mTitleText; @@ -48,7 +43,7 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener { private ImageView mCustomImage; private Button mConfirmButton; private Button mCancelButton; - private ProgressHelper mProgressHelper; + private final ProgressHelper mProgressHelper; private FrameLayout mWarningFrame; private OnSweetClickListener mCancelClickListener; private OnSweetClickListener mConfirmClickListener; @@ -79,18 +74,6 @@ public SweetAlertDialog(Context context, int alertType) { mErrorXInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(getContext(), R.anim.error_x_in); // 2.3.x system don't support alpha-animation on layer-list drawable // remove it from animation set - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { - List childAnims = mErrorXInAnim.getAnimations(); - int idx = 0; - for (;idx < childAnims.size();idx++) { - if (childAnims.get(idx) instanceof AlphaAnimation) { - break; - } - } - if (idx < childAnims.size()) { - childAnims.remove(idx); - } - } mSuccessBowAnim = OptAnimationLoader.loadAnimation(getContext(), R.anim.success_bow_roate); mSuccessLayoutAnimSet = (AnimationSet)OptAnimationLoader.loadAnimation(getContext(), R.anim.success_mask_layout); mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in); @@ -104,14 +87,11 @@ public void onAnimationStart(Animation animation) { @Override public void onAnimationEnd(Animation animation) { mDialogView.setVisibility(View.GONE); - mDialogView.post(new Runnable() { - @Override - public void run() { - if (mCloseFromCancel) { - SweetAlertDialog.super.cancel(); - } else { - SweetAlertDialog.super.dismiss(); - } + mDialogView.post(() -> { + if (mCloseFromCancel) { + SweetAlertDialog.super.cancel(); + } else { + SweetAlertDialog.super.dismiss(); } }); } @@ -138,20 +118,20 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.alert_dialog); mDialogView = getWindow().getDecorView().findViewById(android.R.id.content); - mTitleTextView = (TextView)findViewById(R.id.title_text); - mContentTextView = (TextView)findViewById(R.id.content_text); - mErrorFrame = (FrameLayout)findViewById(R.id.error_frame); - mErrorX = (ImageView)mErrorFrame.findViewById(R.id.error_x); - mSuccessFrame = (FrameLayout)findViewById(R.id.success_frame); - mProgressFrame = (FrameLayout)findViewById(R.id.progress_dialog); - mSuccessTick = (SuccessTickView)mSuccessFrame.findViewById(R.id.success_tick); + mTitleTextView = findViewById(R.id.title_text); + mContentTextView = findViewById(R.id.content_text); + mErrorFrame = findViewById(R.id.error_frame); + mErrorX = mErrorFrame.findViewById(R.id.error_x); + mSuccessFrame = findViewById(R.id.success_frame); + mProgressFrame = findViewById(R.id.progress_dialog); + mSuccessTick = mSuccessFrame.findViewById(R.id.success_tick); mSuccessLeftMask = mSuccessFrame.findViewById(R.id.mask_left); mSuccessRightMask = mSuccessFrame.findViewById(R.id.mask_right); - mCustomImage = (ImageView)findViewById(R.id.custom_image); - mWarningFrame = (FrameLayout)findViewById(R.id.warning_frame); - mConfirmButton = (Button)findViewById(R.id.confirm_button); - mCancelButton = (Button)findViewById(R.id.cancel_button); - mProgressHelper.setProgressWheel((ProgressWheel)findViewById(R.id.progressWheel)); + mCustomImage = findViewById(R.id.custom_image); + mWarningFrame = findViewById(R.id.warning_frame); + mConfirmButton = findViewById(R.id.confirm_button); + mCancelButton = findViewById(R.id.cancel_button); + mProgressHelper.setProgressWheel(findViewById(R.id.progressWheel)); mConfirmButton.setOnClickListener(this); mCancelButton.setOnClickListener(this); @@ -225,19 +205,11 @@ private void changeAlertType(int alertType, boolean fromCreate) { } } - public int getAlerType () { - return mAlertType; - } - public void changeAlertType(int alertType) { changeAlertType(alertType, false); } - public String getTitleText () { - return mTitleText; - } - public SweetAlertDialog setTitleText (String text) { mTitleText = text; if (mTitleTextView != null && mTitleText != null) { @@ -259,10 +231,6 @@ public SweetAlertDialog setCustomImage (int resourceId) { return setCustomImage(getContext().getResources().getDrawable(resourceId)); } - public String getContentText () { - return mContentText; - } - public SweetAlertDialog setContentText (String text) { mContentText = text; if (mContentTextView != null && mContentText != null) { @@ -272,10 +240,6 @@ public SweetAlertDialog setContentText (String text) { return this; } - public boolean isShowCancelButton () { - return mShowCancel; - } - public SweetAlertDialog showCancelButton (boolean isShow) { mShowCancel = isShow; if (mCancelButton != null) { @@ -284,20 +248,11 @@ public SweetAlertDialog showCancelButton (boolean isShow) { return this; } - public boolean isShowContentText () { - return mShowContent; - } - - public SweetAlertDialog showContentText (boolean isShow) { + public void showContentText (boolean isShow) { mShowContent = isShow; if (mContentTextView != null) { mContentTextView.setVisibility(mShowContent ? View.VISIBLE : View.GONE); } - return this; - } - - public String getCancelText () { - return mCancelText; } public SweetAlertDialog setCancelText (String text) { @@ -309,10 +264,6 @@ public SweetAlertDialog setCancelText (String text) { return this; } - public String getConfirmText () { - return mConfirmText; - } - public SweetAlertDialog setConfirmText (String text) { mConfirmText = text; if (mConfirmButton != null && mConfirmText != null) { diff --git a/app/src/main/java/cn/samnyandoro/sweetalertdialog/MainActivity.java b/app/src/main/java/cn/samnyandoro/sweetalertdialog/MainActivity.java index f9f2aa6..abec282 100644 --- a/app/src/main/java/cn/samnyandoro/sweetalertdialog/MainActivity.java +++ b/app/src/main/java/cn/samnyandoro/sweetalertdialog/MainActivity.java @@ -27,129 +27,118 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onClick(View v) { - switch (v.getId()) { - case R.id.basic_test: - // default title "Here's a message!" - SweetAlertDialog sd = new SweetAlertDialog(this); - sd.setCancelable(true); - sd.setCanceledOnTouchOutside(true); - sd.show(); - break; - case R.id.under_text_test: - new SweetAlertDialog(this) - .setContentText("It's pretty, isn't it?") - .show(); - break; - case R.id.error_text_test: - new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE) - .setTitleText("Oops...") - .setContentText("Something went wrong!") - .show(); - break; - case R.id.success_text_test: - new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE) - .setTitleText("Good job!") - .setContentText("You clicked the button!") - .show(); - break; - case R.id.warning_confirm_test: - new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) - .setTitleText("Are you sure?") - .setContentText("Won't be able to recover this file!") - .setConfirmText("Yes,delete it!") - .setConfirmClickListener(sDialog -> { - // reuse previous dialog instance - sDialog.setTitleText("Deleted!") - .setContentText("Your imaginary file has been deleted!") - .setConfirmText("OK") - .setConfirmClickListener(null) - .changeAlertType(SweetAlertDialog.SUCCESS_TYPE); - }) - .show(); - break; - case R.id.warning_cancel_test: - new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) - .setTitleText("Are you sure?") - .setContentText("Won't be able to recover this file!") - .setCancelText("No,cancel plx!") - .setConfirmText("Yes,delete it!") - .showCancelButton(true) - .setCancelClickListener(sDialog -> { - // reuse previous dialog instance, keep widget user state, reset them if you need - sDialog.setTitleText("Cancelled!") - .setContentText("Your imaginary file is safe :)") - .setConfirmText("OK") - .showCancelButton(false) - .setCancelClickListener(null) - .setConfirmClickListener(null) - .changeAlertType(SweetAlertDialog.ERROR_TYPE); + int id = v.getId(); + if (id == R.id.basic_test) {// default title "Here's a message!" + SweetAlertDialog sd = new SweetAlertDialog(this); + sd.setCancelable(true); + sd.setCanceledOnTouchOutside(true); + sd.show(); + } else if (id == R.id.under_text_test) { + new SweetAlertDialog(this) + .setContentText("It's pretty, isn't it?") + .show(); + } else if (id == R.id.error_text_test) { + new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE) + .setTitleText("Oops...") + .setContentText("Something went wrong!") + .show(); + } else if (id == R.id.success_text_test) { + new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE) + .setTitleText("Good job!") + .setContentText("You clicked the button!") + .show(); + } else if (id == R.id.warning_confirm_test) { + new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) + .setTitleText("Are you sure?") + .setContentText("Won't be able to recover this file!") + .setConfirmText("Yes,delete it!") + .setConfirmClickListener(sDialog -> { + // reuse previous dialog instance + sDialog.setTitleText("Deleted!") + .setContentText("Your imaginary file has been deleted!") + .setConfirmText("OK") + .setConfirmClickListener(null) + .changeAlertType(SweetAlertDialog.SUCCESS_TYPE); + }) + .show(); + } else if (id == R.id.warning_cancel_test) { + new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) + .setTitleText("Are you sure?") + .setContentText("Won't be able to recover this file!") + .setCancelText("No,cancel plx!") + .setConfirmText("Yes,delete it!") + .showCancelButton(true) + .setCancelClickListener(sDialog -> { + // reuse previous dialog instance, keep widget user state, reset them if you need + sDialog.setTitleText("Cancelled!") + .setContentText("Your imaginary file is safe :)") + .setConfirmText("OK") + .showCancelButton(false) + .setCancelClickListener(null) + .setConfirmClickListener(null) + .changeAlertType(SweetAlertDialog.ERROR_TYPE); - // or you can new a SweetAlertDialog to show + // or you can new a SweetAlertDialog to show /* sDialog.dismiss(); new SweetAlertDialog(SampleActivity.this, SweetAlertDialog.ERROR_TYPE) .setTitleText("Cancelled!") .setContentText("Your imaginary file is safe :)") .setConfirmText("OK") .show();*/ - }) - .setConfirmClickListener(sDialog -> sDialog.setTitleText("Deleted!") - .setContentText("Your imaginary file has been deleted!") - .setConfirmText("OK") - .showCancelButton(false) - .setCancelClickListener(null) - .setConfirmClickListener(null) - .changeAlertType(SweetAlertDialog.SUCCESS_TYPE)) - .show(); - break; - case R.id.custom_img_test: - new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE) - .setTitleText("Sweet!") - .setContentText("Here's a custom image.") - .setCustomImage(R.drawable.custom_img) - .show(); - break; - case R.id.progress_dialog: - final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE) - .setTitleText("Loading"); - pDialog.show(); - pDialog.setCancelable(false); - new CountDownTimer(800 * 7, 800) { - public void onTick(long millisUntilFinished) { - // you can change the progress bar color by ProgressHelper every 800 millis - i++; - switch (i){ - case 0: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color)); - break; - case 1: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50)); - break; - case 2: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color)); - break; - case 3: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20)); - break; - case 4: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80)); - break; - case 5: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color)); - break; - case 6: - pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color)); - break; - } + }) + .setConfirmClickListener(sDialog -> sDialog.setTitleText("Deleted!") + .setContentText("Your imaginary file has been deleted!") + .setConfirmText("OK") + .showCancelButton(false) + .setCancelClickListener(null) + .setConfirmClickListener(null) + .changeAlertType(SweetAlertDialog.SUCCESS_TYPE)) + .show(); + } else if (id == R.id.custom_img_test) { + new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE) + .setTitleText("Sweet!") + .setContentText("Here's a custom image.") + .setCustomImage(R.drawable.custom_img) + .show(); + } else if (id == R.id.progress_dialog) { + final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE) + .setTitleText("Loading"); + pDialog.show(); + pDialog.setCancelable(false); + new CountDownTimer(800 * 7, 800) { + public void onTick(long millisUntilFinished) { + // you can change the progress bar color by ProgressHelper every 800 millis + i++; + switch (i) { + case 0: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color)); + break; + case 1: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50)); + break; + case 2: + case 6: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color)); + break; + case 3: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20)); + break; + case 4: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80)); + break; + case 5: + pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color)); + break; } + } - public void onFinish() { - i = -1; - pDialog.setTitleText("Success!") - .setConfirmText("OK") - .changeAlertType(SweetAlertDialog.SUCCESS_TYPE); - } - }.start(); - break; + public void onFinish() { + i = -1; + pDialog.setTitleText("Success!") + .setConfirmText("OK") + .changeAlertType(SweetAlertDialog.SUCCESS_TYPE); + } + }.start(); } } } \ No newline at end of file