improve variable naming and some magic constants

This commit is contained in:
Vincent Breitmoser 2016-06-09 17:58:45 +02:00
parent 1bae68169f
commit 84c0e4c730
4 changed files with 115 additions and 107 deletions

View file

@ -27,18 +27,22 @@ import com.fsck.k9.view.MessageCryptoDisplayStatus;
public class CryptoInfoDialog extends DialogFragment {
public static final String ARG_DISPLAY_STATUS = "display_status";
public static final int ICON_ANIM_DELAY = 400;
public static final int ICON_ANIM_DURATION = 350;
private View dialogView;
private View icon1frame;
private View icon2frame;
private TextView text1;
private TextView text2;
private ImageView icon_1_1;
private ImageView icon_1_2;
private ImageView icon_1_3;
private ImageView icon_2_1;
private ImageView icon_2_2;
private View topIconFrame;
private ImageView topIcon_1;
private ImageView topIcon_2;
private ImageView topIcon_3;
private TextView topText;
private View bottomIconFrame;
private ImageView bottomIcon_1;
private ImageView bottomIcon_2;
private TextView bottomText;
public static CryptoInfoDialog newInstance(MessageCryptoDisplayStatus displayStatus) {
@ -58,16 +62,16 @@ public class CryptoInfoDialog extends DialogFragment {
dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.message_crypto_info_dialog, null);
icon1frame = dialogView.findViewById(R.id.crypto_info_frame_1);
icon_1_1 = (ImageView) icon1frame.findViewById(R.id.crypto_info_icon_1_1);
icon_1_2 = (ImageView) icon1frame.findViewById(R.id.crypto_info_icon_1_2);
icon_1_3 = (ImageView) icon1frame.findViewById(R.id.crypto_info_icon_1_3);
text1 = (TextView) dialogView.findViewById(R.id.crypto_info_text_1);
topIconFrame = dialogView.findViewById(R.id.crypto_info_top_frame);
topIcon_1 = (ImageView) topIconFrame.findViewById(R.id.crypto_info_top_icon_1);
topIcon_2 = (ImageView) topIconFrame.findViewById(R.id.crypto_info_top_icon_2);
topIcon_3 = (ImageView) topIconFrame.findViewById(R.id.crypto_info_top_icon_3);
topText = (TextView) dialogView.findViewById(R.id.crypto_info_top_text);
icon2frame = dialogView.findViewById(R.id.crypto_info_frame_2);
icon_2_1 = (ImageView) icon2frame.findViewById(R.id.crypto_info_icon_2_1);
icon_2_2 = (ImageView) icon2frame.findViewById(R.id.crypto_info_icon_2_2);
text2 = (TextView) dialogView.findViewById(R.id.crypto_info_text_2);
bottomIconFrame = dialogView.findViewById(R.id.crypto_info_bottom_frame);
bottomIcon_1 = (ImageView) bottomIconFrame.findViewById(R.id.crypto_info_bottom_icon_1);
bottomIcon_2 = (ImageView) bottomIconFrame.findViewById(R.id.crypto_info_bottom_icon_2);
bottomText = (TextView) dialogView.findViewById(R.id.crypto_info_bottom_text);
MessageCryptoDisplayStatus displayStatus =
MessageCryptoDisplayStatus.valueOf(getArguments().getString(ARG_DISPLAY_STATUS));
@ -97,83 +101,86 @@ public class CryptoInfoDialog extends DialogFragment {
}
private void setMessageForDisplayStatus(MessageCryptoDisplayStatus displayStatus) {
if (displayStatus.textResFirst == null) {
if (displayStatus.textResTop == null) {
throw new AssertionError("Crypto info dialog can only be displayed for items with text!");
}
if (displayStatus.textResSecond == null) {
setMessageSingleLine(displayStatus.color, displayStatus.textResFirst, displayStatus.iconResFirst, displayStatus.iconResSecond);
if (displayStatus.textResBottom == null) {
setMessageSingleLine(displayStatus.colorRes,
displayStatus.textResTop, displayStatus.statusIconRes,
displayStatus.statusDotsRes);
} else {
if (displayStatus.iconResSecond == null) {
if (displayStatus.statusDotsRes == null) {
throw new AssertionError("second icon must be non-null if second text is non-null!");
}
setMessageWithAnimation(displayStatus.color,
displayStatus.textResFirst, displayStatus.iconResFirst,
displayStatus.textResSecond, displayStatus.iconResSecond);
setMessageWithAnimation(displayStatus.colorRes,
displayStatus.textResTop, displayStatus.statusIconRes,
displayStatus.textResBottom, displayStatus.statusDotsRes);
}
}
private void setMessageSingleLine(@ColorRes int colorres,
@StringRes int text1res, @DrawableRes int icon1res,
@DrawableRes Integer icon2res) {
@ColorInt int color = getResources().getColor(colorres);
private void setMessageSingleLine(@ColorRes int colorRes,
@StringRes int topTextRes, @DrawableRes int statusIconRes,
@DrawableRes Integer statusDotsRes) {
@ColorInt int color = getResources().getColor(colorRes);
icon_1_1.setImageResource(icon1res);
icon_1_1.setColorFilter(color);
text1.setText(text1res);
topIcon_1.setImageResource(statusIconRes);
topIcon_1.setColorFilter(color);
topText.setText(topTextRes);
if (icon2res != null) {
icon_1_3.setImageResource(icon2res);
icon_1_3.setColorFilter(color);
icon_1_3.setVisibility(View.VISIBLE);
if (statusDotsRes != null) {
topIcon_3.setImageResource(statusDotsRes);
topIcon_3.setColorFilter(color);
topIcon_3.setVisibility(View.VISIBLE);
} else {
icon_1_3.setVisibility(View.GONE);
topIcon_3.setVisibility(View.GONE);
}
text2.setVisibility(View.GONE);
icon2frame.setVisibility(View.GONE);
bottomText.setVisibility(View.GONE);
bottomIconFrame.setVisibility(View.GONE);
}
private void setMessageWithAnimation(@ColorRes int colorres,
@StringRes int text1res, @DrawableRes int icon1res, @StringRes int text2res, @DrawableRes int icon2res) {
icon_1_1.setImageResource(icon1res);
icon_1_2.setImageResource(icon2res);
icon_1_3.setVisibility(View.GONE);
text1.setText(text1res);
private void setMessageWithAnimation(@ColorRes int colorRes,
@StringRes int topTextRes, @DrawableRes int statusIconRes,
@StringRes int bottomTextRes, @DrawableRes int statusDotsRes) {
topIcon_1.setImageResource(statusIconRes);
topIcon_2.setImageResource(statusDotsRes);
topIcon_3.setVisibility(View.GONE);
topText.setText(topTextRes);
icon_2_1.setImageResource(icon1res);
icon_2_2.setImageResource(icon2res);
text2.setText(text2res);
bottomIcon_1.setImageResource(statusIconRes);
bottomIcon_2.setImageResource(statusDotsRes);
bottomText.setText(bottomTextRes);
icon_1_1.setColorFilter(getResources().getColor(colorres));
icon_2_2.setColorFilter(getResources().getColor(colorres));
topIcon_1.setColorFilter(getResources().getColor(colorRes));
bottomIcon_2.setColorFilter(getResources().getColor(colorRes));
prepareIconAnimation();
}
private void prepareIconAnimation() {
text1.setAlpha(0.0f);
text2.setAlpha(0.0f);
topText.setAlpha(0.0f);
bottomText.setAlpha(0.0f);
dialogView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
float halfVerticalPixelDifference = (icon2frame.getY() - icon1frame.getY()) / 2.0f;
icon1frame.setTranslationY(halfVerticalPixelDifference);
icon2frame.setTranslationY(-halfVerticalPixelDifference);
float halfVerticalPixelDifference = (bottomIconFrame.getY() - topIconFrame.getY()) / 2.0f;
topIconFrame.setTranslationY(halfVerticalPixelDifference);
bottomIconFrame.setTranslationY(-halfVerticalPixelDifference);
icon1frame.animate().translationY(0)
.setStartDelay(400)
.setDuration(350)
topIconFrame.animate().translationY(0)
.setStartDelay(ICON_ANIM_DELAY)
.setDuration(ICON_ANIM_DURATION)
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
icon2frame.animate().translationY(0)
.setStartDelay(400)
.setDuration(350)
bottomIconFrame.animate().translationY(0)
.setStartDelay(ICON_ANIM_DELAY)
.setDuration(ICON_ANIM_DURATION)
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
text1.animate().alpha(1.0f).setStartDelay(750).start();
text2.animate().alpha(1.0f).setStartDelay(750).start();
topText.animate().alpha(1.0f).setStartDelay(ICON_ANIM_DELAY + ICON_ANIM_DURATION).start();
bottomText.animate().alpha(1.0f).setStartDelay(ICON_ANIM_DELAY + ICON_ANIM_DURATION).start();
view.removeOnLayoutChangeListener(this);
}

View file

@ -148,39 +148,40 @@ public enum MessageCryptoDisplayStatus {
),
;
@ColorRes public final int color;
@DrawableRes public final int iconResFirst;
@DrawableRes public final Integer iconResSecond;
@ColorRes public final int colorRes;
@StringRes public final Integer textResFirst;
@StringRes public final Integer textResSecond;
@DrawableRes public final int statusIconRes;
@DrawableRes public final Integer statusDotsRes;
MessageCryptoDisplayStatus(@ColorRes int color, @DrawableRes int iconResFirst, @DrawableRes Integer iconResSecond,
@StringRes int textResFirst, @StringRes Integer textResSecond) {
this.color = color;
this.iconResFirst = iconResFirst;
this.iconResSecond = iconResSecond;
@StringRes public final Integer textResTop;
@StringRes public final Integer textResBottom;
this.textResFirst = textResFirst;
this.textResSecond = textResSecond;
MessageCryptoDisplayStatus(@ColorRes int colorRes, @DrawableRes int statusIconRes, @DrawableRes Integer statusDotsRes,
@StringRes int textResTop, @StringRes Integer textResBottom) {
this.colorRes = colorRes;
this.statusIconRes = statusIconRes;
this.statusDotsRes = statusDotsRes;
this.textResTop = textResTop;
this.textResBottom = textResBottom;
}
MessageCryptoDisplayStatus(@ColorRes int color, @DrawableRes int iconResFirst, @StringRes int textResFirst) {
this.color = color;
this.iconResFirst = iconResFirst;
this.iconResSecond = null;
MessageCryptoDisplayStatus(@ColorRes int colorRes, @DrawableRes int statusIconRes, @StringRes int textResTop) {
this.colorRes = colorRes;
this.statusIconRes = statusIconRes;
this.statusDotsRes = null;
this.textResFirst = textResFirst;
this.textResSecond = null;
this.textResTop = textResTop;
this.textResBottom = null;
}
MessageCryptoDisplayStatus(@ColorRes int color, @DrawableRes int iconResFirst) {
this.color = color;
this.iconResFirst = iconResFirst;
this.iconResSecond = null;
MessageCryptoDisplayStatus(@ColorRes int colorRes, @DrawableRes int statusIconRes) {
this.colorRes = colorRes;
this.statusIconRes = statusIconRes;
this.statusDotsRes = null;
this.textResFirst = null;
this.textResSecond = null;
this.textResTop = null;
this.textResBottom = null;
}
@NonNull

View file

@ -40,17 +40,17 @@ public class MessageCryptoStatusView extends FrameLayout {
}
public void setCryptoDisplayStatus(MessageCryptoDisplayStatus displayStatus) {
@ColorInt int color = getResources().getColor(displayStatus.color);
@ColorInt int color = getResources().getColor(displayStatus.colorRes);
if (displayStatus.iconResSecond != null) {
if (displayStatus.statusDotsRes != null) {
iconCombinedFirst.setVisibility(View.VISIBLE);
iconCombinedSecond.setVisibility(View.VISIBLE);
iconDotsBackground.setVisibility(View.VISIBLE);
iconSingle.setVisibility(View.GONE);
iconCombinedFirst.setImageResource(displayStatus.iconResFirst);
iconCombinedFirst.setImageResource(displayStatus.statusIconRes);
iconCombinedFirst.setColorFilter(color);
iconCombinedSecond.setImageResource(displayStatus.iconResSecond);
iconCombinedSecond.setImageResource(displayStatus.statusDotsRes);
iconCombinedSecond.setColorFilter(color);
} else {
iconCombinedFirst.setVisibility(View.GONE);
@ -58,7 +58,7 @@ public class MessageCryptoStatusView extends FrameLayout {
iconDotsBackground.setVisibility(View.GONE);
iconSingle.setVisibility(View.VISIBLE);
iconSingle.setImageResource(displayStatus.iconResFirst);
iconSingle.setImageResource(displayStatus.statusIconRes);
iconSingle.setColorFilter(color);
}
}

View file

@ -11,15 +11,15 @@
android:layout_width="36dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/crypto_info_text_1"
android:id="@+id/crypto_info_frame_1"
android:layout_alignBottom="@+id/crypto_info_top_text"
android:id="@+id/crypto_info_top_frame"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/crypto_info_icon_1_1"
android:id="@+id/crypto_info_top_icon_1"
tools:src="@drawable/status_signature_verified_cutout"
tools:tint="@color/openpgp_blue"
/>
@ -28,7 +28,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/crypto_info_icon_1_2"
android:id="@+id/crypto_info_top_icon_2"
android:alpha="0.05"
tools:src="@drawable/status_none_dots_3"
/>
@ -37,19 +37,19 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/crypto_info_icon_1_3"
android:id="@+id/crypto_info_top_icon_3"
/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/crypto_info_frame_1"
android:layout_toRightOf="@+id/crypto_info_top_frame"
android:layout_marginBottom="12dp"
android:layout_marginLeft="20dp"
android:minLines="2"
android:gravity="center_vertical"
android:id="@+id/crypto_info_text_1"
android:id="@+id/crypto_info_top_text"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Signed, but not encrypted."
/>
@ -57,16 +57,16 @@
<FrameLayout
android:layout_width="36dp"
android:layout_height="wrap_content"
android:layout_below="@+id/crypto_info_text_1"
android:layout_alignBottom="@+id/crypto_info_text_2"
android:id="@+id/crypto_info_frame_2"
android:layout_below="@+id/crypto_info_top_text"
android:layout_alignBottom="@+id/crypto_info_bottom_text"
android:id="@+id/crypto_info_bottom_frame"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/crypto_info_icon_2_1"
android:id="@+id/crypto_info_bottom_icon_1"
android:alpha="0.05"
tools:src="@drawable/status_signature_verified_cutout"
/>
@ -75,7 +75,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:id="@+id/crypto_info_icon_2_2"
android:id="@+id/crypto_info_bottom_icon_2"
tools:src="@drawable/status_none_dots_3"
tools:tint="@color/openpgp_blue"
/>
@ -85,10 +85,10 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/crypto_info_text_1"
android:layout_toRightOf="@+id/crypto_info_frame_2"
android:layout_below="@+id/crypto_info_top_text"
android:layout_toRightOf="@+id/crypto_info_bottom_frame"
android:layout_marginLeft="20dp"
android:id="@+id/crypto_info_text_2"
android:id="@+id/crypto_info_bottom_text"
android:minLines="2"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"