From 1d5d94080a6bffaf5fa7c358af76a3cfae0a8ade Mon Sep 17 00:00:00 2001 From: steckbrief Date: Tue, 29 Mar 2016 22:28:19 +0200 Subject: Introduction of a TextViewUtil to easily set attributes for TextViews --- .../ui/ChangePasswordActivity.java | 9 +-- .../conversationsplus/ui/EditAccountActivity.java | 22 ++----- .../ui/PublishProfilePictureActivity.java | 24 ++----- .../conversationsplus/ui/TrustKeysActivity.java | 29 ++------- .../conversationsplus/ui/VerifyOTRActivity.java | 9 +-- .../conversationsplus/utils/ui/TextViewUtil.java | 75 ++++++++++++++++++++++ 6 files changed, 99 insertions(+), 69 deletions(-) create mode 100644 src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java (limited to 'src') diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ChangePasswordActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/ChangePasswordActivity.java index 65b3c49b..2e063757 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ChangePasswordActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ChangePasswordActivity.java @@ -10,6 +10,7 @@ import de.thedevstack.conversationsplus.ConversationsPlusColors; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.services.XmppConnectionService; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; public class ChangePasswordActivity extends XmppActivity implements XmppConnectionService.OnAccountPasswordChanged { @@ -38,9 +39,7 @@ public class ChangePasswordActivity extends XmppActivity implements XmppConnecti mNewPassword.setError(null); mNewPasswordConfirm.setError(null); xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this); - mChangePasswordButton.setEnabled(false); - mChangePasswordButton.setTextColor(ConversationsPlusColors.secondaryText()); - mChangePasswordButton.setText(R.string.updating); + TextViewUtil.disable(mChangePasswordButton, ConversationsPlusColors.secondaryText(), R.string.updating); } } } @@ -91,9 +90,7 @@ public class ChangePasswordActivity extends XmppActivity implements XmppConnecti @Override public void run() { mNewPassword.setError(getString(R.string.could_not_change_password)); - mChangePasswordButton.setEnabled(true); - mChangePasswordButton.setTextColor(ConversationsPlusColors.primaryText()); - mChangePasswordButton.setText(R.string.change_password); + TextViewUtil.enable(mChangePasswordButton, ConversationsPlusColors.primaryText(), R.string.change_password); } }); diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java index 9a616414..33b73a32 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java @@ -51,6 +51,7 @@ import de.thedevstack.conversationsplus.services.XmppConnectionService.OnCaptcha import de.thedevstack.conversationsplus.ui.adapter.KnownHostsAdapter; import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.UIHelper; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.OnKeyStatusUpdated; import de.thedevstack.conversationsplus.xmpp.XmppConnection; @@ -335,26 +336,18 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate protected void updateSaveButton() { if (accountInfoEdited() && !mInitMode) { - this.mSaveButton.setText(R.string.save); - this.mSaveButton.setEnabled(true); - this.mSaveButton.setTextColor(ConversationsPlusColors.primaryText()); + TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText(), R.string.save); } else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) { - this.mSaveButton.setEnabled(false); - this.mSaveButton.setTextColor(ConversationsPlusColors.secondaryText()); - this.mSaveButton.setText(R.string.account_status_connecting); + TextViewUtil.disable(mSaveButton, ConversationsPlusColors.secondaryText(), R.string.account_status_connecting); } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) { - this.mSaveButton.setEnabled(true); - this.mSaveButton.setTextColor(ConversationsPlusColors.primaryText()); - this.mSaveButton.setText(R.string.enable); + TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText(), R.string.enable); } else { - this.mSaveButton.setEnabled(true); - this.mSaveButton.setTextColor(ConversationsPlusColors.primaryText()); + TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText()); if (!mInitMode) { if (mAccount != null && mAccount.isOnlineAndConnected()) { this.mSaveButton.setText(R.string.save); if (!accountInfoEdited()) { - this.mSaveButton.setEnabled(false); - this.mSaveButton.setTextColor(ConversationsPlusColors.secondaryText()); + TextViewUtil.disable(mSaveButton, ConversationsPlusColors.secondaryText()); } } else { this.mSaveButton.setText(R.string.connect); @@ -552,8 +545,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate getActionBar().setDisplayShowHomeEnabled(false); getActionBar().setHomeButtonEnabled(false); } - this.mCancelButton.setEnabled(false); - this.mCancelButton.setTextColor(ConversationsPlusColors.secondaryText()); + TextViewUtil.disable(mCancelButton, ConversationsPlusColors.secondaryText()); } if (Config.DOMAIN_LOCK == null) { final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java index 1501c78a..6793d882 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java @@ -31,6 +31,7 @@ import de.thedevstack.conversationsplus.services.AvatarService; import de.thedevstack.conversationsplus.utils.ExifHelper; import de.thedevstack.conversationsplus.utils.FileUtils; import de.thedevstack.conversationsplus.utils.PhoneHelper; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; import de.thedevstack.conversationsplus.xmpp.pep.Avatar; public class PublishProfilePictureActivity extends XmppActivity { @@ -87,8 +88,7 @@ public class PublishProfilePictureActivity extends XmppActivity { public void run() { hintOrWarning.setText(errorCode); hintOrWarning.setTextColor(ConversationsPlusColors.warning()); - publishButton.setText(R.string.publish); - enablePublishButton(); + TextViewUtil.enable(publishButton, ConversationsPlusColors.primaryText(), R.string.publish); } }); @@ -114,8 +114,7 @@ public class PublishProfilePictureActivity extends XmppActivity { @Override public void onClick(View v) { if (avatarUri != null) { - publishButton.setText(R.string.publishing); - disablePublishButton(); + TextViewUtil.disable(publishButton, ConversationsPlusColors.secondaryText(), R.string.publishing); AvatarService.getInstance().publishAvatar(account, avatarUri, avatarPublication); } @@ -288,7 +287,7 @@ public class PublishProfilePictureActivity extends XmppActivity { } if (bm == null) { - disablePublishButton(); + TextViewUtil.disable(this.publishButton, ConversationsPlusColors.secondaryText()); this.hintOrWarning.setTextColor(ConversationsPlusColors.warning()); this.hintOrWarning .setText(R.string.error_publish_avatar_converting); @@ -296,12 +295,11 @@ public class PublishProfilePictureActivity extends XmppActivity { } this.avatar.setImageBitmap(bm); if (support) { - enablePublishButton(); - this.publishButton.setText(R.string.publish); + TextViewUtil.enable(this.publishButton, ConversationsPlusColors.primaryText(), R.string.publish); this.hintOrWarning.setText(R.string.publish_avatar_explanation); this.hintOrWarning.setTextColor(ConversationsPlusColors.primaryText()); } else { - disablePublishButton(); + TextViewUtil.disable(this.publishButton, ConversationsPlusColors.secondaryText()); this.hintOrWarning.setTextColor(ConversationsPlusColors.warning()); this.hintOrWarning .setText(R.string.error_publish_avatar_no_server_support); @@ -315,16 +313,6 @@ public class PublishProfilePictureActivity extends XmppActivity { } } - protected void enablePublishButton() { - this.publishButton.setEnabled(true); - this.publishButton.setTextColor(ConversationsPlusColors.primaryText()); - } - - protected void disablePublishButton() { - this.publishButton.setEnabled(false); - this.publishButton.setTextColor(ConversationsPlusColors.secondaryText()); - } - public void refreshUiReal() { //nothing to do. This Activity doesn't implement any listeners } diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/TrustKeysActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/TrustKeysActivity.java index 7772adc4..9ecf7f62 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/TrustKeysActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/TrustKeysActivity.java @@ -25,6 +25,7 @@ import de.thedevstack.conversationsplus.crypto.axolotl.XmppAxolotlSession; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Contact; import de.thedevstack.conversationsplus.entities.Conversation; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; import de.thedevstack.conversationsplus.xmpp.OnKeyStatusUpdated; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; import de.thedevstack.conversationsplus.xmpp.jid.Jid; @@ -161,8 +162,7 @@ public class TrustKeysActivity extends XmppActivity implements OnKeyStatusUpdate ownKeysCard.setVisibility(hasOwnKeys ? View.VISIBLE : View.GONE); foreignKeys.setVisibility(hasForeignKeys ? View.VISIBLE : View.GONE); if(hasPendingKeyFetches()) { - setFetching(); - lock(); + TextViewUtil.disable(this.mSaveButton, ConversationsPlusColors.secondaryText(), R.string.fetching_keys); } else { if (!hasForeignKeys && hasNoOtherTrustedKeys()) { keyErrorMessageCard.setVisibility(View.VISIBLE); @@ -178,7 +178,7 @@ public class TrustKeysActivity extends XmppActivity implements OnKeyStatusUpdate foreignKeys.setVisibility(View.GONE); } lockOrUnlockAsNeeded(); - setDone(); + mSaveButton.setText(R.string.done); } } @@ -305,35 +305,16 @@ public class TrustKeysActivity extends XmppActivity implements OnKeyStatusUpdate } } - private void unlock() { - mSaveButton.setEnabled(true); - mSaveButton.setTextColor(ConversationsPlusColors.primaryText()); - } - - private void lock() { - mSaveButton.setEnabled(false); - mSaveButton.setTextColor(ConversationsPlusColors.secondaryText()); - } - private void lockOrUnlockAsNeeded() { synchronized (this.foreignKeysToTrust) { for (Jid jid : contactJids) { Map fingerprints = foreignKeysToTrust.get(jid); if (hasNoOtherTrustedKeys(jid) && (fingerprints == null || !fingerprints.values().contains(true))) { - lock(); + TextViewUtil.disable(this.mSaveButton, ConversationsPlusColors.secondaryText()); return; } } } - unlock(); - - } - - private void setDone() { - mSaveButton.setText(getString(R.string.done)); - } - - private void setFetching() { - mSaveButton.setText(getString(R.string.fetching_keys)); + TextViewUtil.enable(this.mSaveButton, ConversationsPlusColors.primaryText()); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/VerifyOTRActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/VerifyOTRActivity.java index 3ecdc179..4f1c9fc7 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/VerifyOTRActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/VerifyOTRActivity.java @@ -27,6 +27,7 @@ import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.services.XmppConnectionService; import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.XmppUri; +import de.thedevstack.conversationsplus.utils.ui.TextViewUtil; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; import de.thedevstack.conversationsplus.xmpp.jid.Jid; @@ -380,16 +381,12 @@ public class VerifyOTRActivity extends XmppActivity implements XmppConnectionSer } protected void activateButton(Button button, int text, View.OnClickListener listener) { - button.setEnabled(true); - button.setTextColor(ConversationsPlusColors.primaryText()); - button.setText(text); + TextViewUtil.enable(button, ConversationsPlusColors.primaryText(), text); button.setOnClickListener(listener); } protected void deactivateButton(Button button, int text) { - button.setEnabled(false); - button.setTextColor(ConversationsPlusColors.secondaryText()); - button.setText(text); + TextViewUtil.disable(button, ConversationsPlusColors.secondaryText(), text); button.setOnClickListener(null); } diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java new file mode 100644 index 00000000..bb08014b --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java @@ -0,0 +1,75 @@ +package de.thedevstack.conversationsplus.utils.ui; + +import android.support.annotation.StringRes; +import android.widget.TextView; + +/** + * Created by steckbrief on 29.03.2016. + */ +public final class TextViewUtil { + public static void enable(TextView tv) { + setColorEnabledAndTextResId(tv, null, true, null); + } + + public static void enable(TextView tv, String text) { + setColorEnabledAndText(tv, null, true, text); + } + + public static void enable(TextView tv, Integer color) { + setColorEnabledAndTextResId(tv, color, true, null); + } + + public static void enable(TextView tv, Integer color, @StringRes Integer resid) { + setColorEnabledAndTextResId(tv, color, true, resid); + } + + public static void disable(TextView tv) { + setColorEnabledAndTextResId(tv, null, false, null); + } + + public static void disable(TextView tv, String text) { + setColorEnabledAndText(tv, null, false, text); + } + + public static void disable(TextView tv, Integer color) { + setColorEnabledAndTextResId(tv, color, false, null); + } + + public static void disable(TextView tv, Integer color, @StringRes Integer resid) { + setColorEnabledAndTextResId(tv, color, false, resid); + } + + public static void setColor(TextView tv, Integer color) { + setColorEnabledAndTextResId(tv, color, null, null); + } + + public static void setColorEnabledAndTextResId(TextView tv, Integer color, Boolean enabled, @StringRes Integer resid) { + if (null != color) { + tv.setTextColor(color); + } + + if (enabled != null) { + tv.setEnabled(enabled); + } + if (resid != null) { + tv.setText(resid); + } + } + + public static void setColorEnabledAndText(TextView tv, Integer color, Boolean enabled, String text) { + if (null != color) { + tv.setTextColor(color); + } + + if (enabled != null) { + tv.setEnabled(enabled); + } + if (text != null) { + tv.setText(text); + } + } + + private TextViewUtil() { + // avoid instantiation - helper class + } +} -- cgit v1.2.3 From bcf0d929dda50a3c8c94e5d384840bedc51d4e63 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Tue, 29 Mar 2016 22:30:13 +0200 Subject: Avoid unnecessary text change on activate account button --- .../de/thedevstack/conversationsplus/ui/EditAccountActivity.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java index 33b73a32..abb43c4e 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/EditAccountActivity.java @@ -118,6 +118,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) { mAccount.setOption(Account.OPTION_DISABLED, false); + mAccount.setStatus(Account.State.CONNECTING); xmppConnectionService.updateAccount(mAccount); return; } @@ -544,8 +545,8 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setDisplayShowHomeEnabled(false); getActionBar().setHomeButtonEnabled(false); - } - TextViewUtil.disable(mCancelButton, ConversationsPlusColors.secondaryText()); + } + TextViewUtil.disable(mCancelButton, ConversationsPlusColors.secondaryText()); } if (Config.DOMAIN_LOCK == null) { final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, -- cgit v1.2.3