From a09cc126ea89b25aaa0f9a657936496b29465946 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 16 Feb 2023 12:58:52 +0100 Subject: [PATCH] use logging framework in more places --- .../database/entity/PresenceEntity.java | 7 +- .../conversations/android/xml/XmlReader.java | 16 +++-- .../android/xmpp/ConnectionPool.java | 24 ++++--- .../android/xmpp/ConnectionState.java | 68 ------------------- .../android/xmpp/model/muc/Affiliation.java | 9 +++ .../android/xmpp/model/muc/Role.java | 8 +++ .../android/xmpp/sasl/ChannelBinding.java | 3 - .../android/xmpp/sasl/HashedToken.java | 13 ++-- .../android/xmpp/sasl/SaslMechanism.java | 10 +-- 9 files changed, 53 insertions(+), 105 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/model/muc/Affiliation.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/muc/Role.java diff --git a/src/main/java/im/conversations/android/database/entity/PresenceEntity.java b/src/main/java/im/conversations/android/database/entity/PresenceEntity.java index 760d9667f..2ec42f31b 100644 --- a/src/main/java/im/conversations/android/database/entity/PresenceEntity.java +++ b/src/main/java/im/conversations/android/database/entity/PresenceEntity.java @@ -6,9 +6,10 @@ import androidx.room.Entity; import androidx.room.ForeignKey; import androidx.room.Index; import androidx.room.PrimaryKey; -import eu.siacs.conversations.entities.MucOptions; import im.conversations.android.database.model.PresenceShow; import im.conversations.android.database.model.PresenceType; +import im.conversations.android.xmpp.model.muc.Affiliation; +import im.conversations.android.xmpp.model.muc.Role; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.parts.Resourcepart; @@ -54,9 +55,9 @@ public class PresenceEntity { @Nullable public String occupantId; - @Nullable public MucOptions.Affiliation mucUserAffiliation; + @Nullable public Affiliation mucUserAffiliation; - @Nullable public MucOptions.Role mucUserRole; + @Nullable public Role mucUserRole; @Nullable public Jid mucUserJid; diff --git a/src/main/java/im/conversations/android/xml/XmlReader.java b/src/main/java/im/conversations/android/xml/XmlReader.java index 52a34bc32..df507010d 100644 --- a/src/main/java/im/conversations/android/xml/XmlReader.java +++ b/src/main/java/im/conversations/android/xml/XmlReader.java @@ -1,18 +1,20 @@ package im.conversations.android.xml; -import android.util.Log; import android.util.Xml; -import eu.siacs.conversations.Config; import im.conversations.android.xmpp.ExtensionFactory; import im.conversations.android.xmpp.model.Extension; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; public class XmlReader implements Closeable { + + private static final Logger LOGGER = LoggerFactory.getLogger(XmlReader.class); private final XmlPullParser parser; private InputStream is; @@ -20,19 +22,19 @@ public class XmlReader implements Closeable { this.parser = Xml.newPullParser(); try { this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); - } catch (XmlPullParserException e) { - Log.d(Config.LOGTAG, "error setting namespace feature on parser"); + } catch (final XmlPullParserException e) { + LOGGER.error("error setting namespace feature on parser", e); } } - public void setInputStream(InputStream inputStream) throws IOException { + public void setInputStream(final InputStream inputStream) throws IOException { if (inputStream == null) { throw new IOException(); } this.is = inputStream; try { - parser.setInput(new InputStreamReader(this.is)); - } catch (XmlPullParserException e) { + this.parser.setInput(new InputStreamReader(this.is)); + } catch (final XmlPullParserException e) { throw new IOException("error resetting parser"); } } diff --git a/src/main/java/im/conversations/android/xmpp/ConnectionPool.java b/src/main/java/im/conversations/android/xmpp/ConnectionPool.java index fdb6342aa..a57b5e6ab 100644 --- a/src/main/java/im/conversations/android/xmpp/ConnectionPool.java +++ b/src/main/java/im/conversations/android/xmpp/ConnectionPool.java @@ -11,8 +11,6 @@ import com.google.common.collect.Sets; import com.google.common.primitives.Ints; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import eu.siacs.conversations.Config; -import eu.siacs.conversations.utils.PhoneHelper; import im.conversations.android.Conversations; import im.conversations.android.database.ConversationsDatabase; import im.conversations.android.database.model.Account; @@ -33,6 +31,12 @@ public class ConnectionPool { private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionPool.class); + public static final int CONNECT_TIMEOUT = 90; + public static final int PING_MAX_INTERVAL = 300; + public static final int PING_MIN_INTERVAL = 30; + public static final int LOW_PING_TIMEOUT = 1; // used after push received + public static final int PING_TIMEOUT = 15; + private static volatile ConnectionPool INSTANCE; private final Context context; @@ -148,7 +152,7 @@ public class ConnectionPool { if (connection.supportsClientStateIndication()) { // TODO send correct CSI state (connection.sendActive or connection.sendInactive) } - scheduleWakeUpCall(Config.PING_MAX_INTERVAL); + scheduleWakeUpCall(PING_MAX_INTERVAL); } else if (connection.getStatus() == ConnectionState.OFFLINE) { // TODO previously we would call resetSendingToWaiting. The new architecture likely @@ -227,9 +231,7 @@ public class ConnectionPool { // WakeLockHelper.acquire(wakeLock); int pingNow = 0; final HashSet pingCandidates = new HashSet<>(); - final String androidId = PhoneHelper.getAndroidId(context); for (final XmppConnection xmppConnection : this.connections) { - final Account account = xmppConnection.getAccount(); // TODO fix me if we bring back FCM push support final boolean pushWasMeantForThisAccount = false; if (processAccountState(xmppConnection, pushWasMeantForThisAccount, pingCandidates)) { @@ -242,7 +244,7 @@ public class ConnectionPool { final boolean lowTimeout = isInLowPingTimeoutMode(account); xmppConnection.sendPing(); LOGGER.debug("{}: send ping (lowTimeout={})", account.address, lowTimeout); - scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT); + scheduleWakeUpCall(lowTimeout ? LOW_PING_TIMEOUT : PING_TIMEOUT); } } // WakeLockHelper.release(wakeLock); @@ -260,12 +262,12 @@ public class ConnectionPool { final long lastReceived = connection.getLastPacketReceived(); final long lastSent = connection.getLastPingSent(); final long msToNextPing = - (Math.max(lastReceived, lastSent) + Config.PING_MAX_INTERVAL * 1000) + (Math.max(lastReceived, lastSent) + PING_MAX_INTERVAL * 1000) - SystemClock.elapsedRealtime(); final int pingTimeout = lowPingTimeoutMode.contains(account.address) - ? Config.LOW_PING_TIMEOUT * 1000 - : Config.PING_TIMEOUT * 1000; + ? LOW_PING_TIMEOUT * 1000 + : PING_TIMEOUT * 1000; final long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime(); if (lastSent > lastReceived) { @@ -297,7 +299,7 @@ public class ConnectionPool { } else if (connection.getStatus() == ConnectionState.CONNECTING) { long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - connection.getLastConnect()) / 1000; - long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect; + long timeout = CONNECT_TIMEOUT - secondsSinceLastConnect; if (timeout < 0) { LOGGER.debug( "{}: time out during connect reconnecting (secondsSinceLast={})", @@ -322,7 +324,7 @@ public class ConnectionPool { connection.prepareNewConnection(); connection.interrupt(); thread.start(); - scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT); + scheduleWakeUpCall(PING_MAX_INTERVAL); } else { disconnect(connection, true); connection.resetEverything(); diff --git a/src/main/java/im/conversations/android/xmpp/ConnectionState.java b/src/main/java/im/conversations/android/xmpp/ConnectionState.java index 6fd71b13d..5e12967b5 100644 --- a/src/main/java/im/conversations/android/xmpp/ConnectionState.java +++ b/src/main/java/im/conversations/android/xmpp/ConnectionState.java @@ -1,8 +1,5 @@ package im.conversations.android.xmpp; -import androidx.annotation.StringRes; -import eu.siacs.conversations.R; - public enum ConnectionState { OFFLINE(false), CONNECTING(false), @@ -56,69 +53,4 @@ public enum ConnectionState { public boolean isAttemptReconnect() { return this.attemptReconnect; } - - // TODO refactor into DataBinder (we can print the enum directly in the UI) - @StringRes - public int getReadableId() { - switch (this) { - case ONLINE: - return R.string.account_status_online; - case CONNECTING: - return R.string.account_status_connecting; - case OFFLINE: - return R.string.account_status_offline; - case UNAUTHORIZED: - return R.string.account_status_unauthorized; - case SERVER_NOT_FOUND: - return R.string.account_status_not_found; - case REGISTRATION_FAILED: - return R.string.account_status_regis_fail; - case REGISTRATION_WEB: - return R.string.account_status_regis_web; - case REGISTRATION_CONFLICT: - return R.string.account_status_regis_conflict; - case REGISTRATION_SUCCESSFUL: - return R.string.account_status_regis_success; - case REGISTRATION_NOT_SUPPORTED: - return R.string.account_status_regis_not_sup; - case REGISTRATION_INVALID_TOKEN: - return R.string.account_status_regis_invalid_token; - case TLS_ERROR: - return R.string.account_status_tls_error; - case TLS_ERROR_DOMAIN: - return R.string.account_status_tls_error_domain; - case INCOMPATIBLE_SERVER: - return R.string.account_status_incompatible_server; - case INCOMPATIBLE_CLIENT: - return R.string.account_status_incompatible_client; - case TOR_NOT_AVAILABLE: - return R.string.account_status_tor_unavailable; - case BIND_FAILURE: - return R.string.account_status_bind_failure; - case SESSION_FAILURE: - return R.string.session_failure; - case DOWNGRADE_ATTACK: - return R.string.sasl_downgrade; - case HOST_UNKNOWN: - return R.string.account_status_host_unknown; - case POLICY_VIOLATION: - return R.string.account_status_policy_violation; - case REGISTRATION_PLEASE_WAIT: - return R.string.registration_please_wait; - case REGISTRATION_PASSWORD_TOO_WEAK: - return R.string.registration_password_too_weak; - case STREAM_ERROR: - return R.string.account_status_stream_error; - case STREAM_OPENING_ERROR: - return R.string.account_status_stream_opening_error; - case PAYMENT_REQUIRED: - return R.string.payment_required; - case MISSING_INTERNET_PERMISSION: - return R.string.missing_internet_permission; - case TEMPORARY_AUTH_FAILURE: - return R.string.account_status_temporary_auth_failure; - default: - return R.string.account_status_unknown; - } - } } diff --git a/src/main/java/im/conversations/android/xmpp/model/muc/Affiliation.java b/src/main/java/im/conversations/android/xmpp/model/muc/Affiliation.java new file mode 100644 index 000000000..6502a16e7 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/muc/Affiliation.java @@ -0,0 +1,9 @@ +package im.conversations.android.xmpp.model.muc; + +public enum Affiliation { + OWNER, + ADMIN, + MEMBER, + OUTCAST, + NONE; +} diff --git a/src/main/java/im/conversations/android/xmpp/model/muc/Role.java b/src/main/java/im/conversations/android/xmpp/model/muc/Role.java new file mode 100644 index 000000000..9e9d3d165 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/muc/Role.java @@ -0,0 +1,8 @@ +package im.conversations.android.xmpp.model.muc; + +public enum Role { + MODERATOR, + VISITOR, + PARTICIPANT, + NONE +} diff --git a/src/main/java/im/conversations/android/xmpp/sasl/ChannelBinding.java b/src/main/java/im/conversations/android/xmpp/sasl/ChannelBinding.java index b03b02a47..1363c85ea 100644 --- a/src/main/java/im/conversations/android/xmpp/sasl/ChannelBinding.java +++ b/src/main/java/im/conversations/android/xmpp/sasl/ChannelBinding.java @@ -1,6 +1,5 @@ package im.conversations.android.xmpp.sasl; -import android.util.Log; import com.google.common.base.CaseFormat; import com.google.common.base.Preconditions; import com.google.common.base.Predicates; @@ -8,7 +7,6 @@ import com.google.common.base.Strings; import com.google.common.collect.BiMap; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableBiMap; -import eu.siacs.conversations.Config; import im.conversations.android.tls.SSLSockets; import im.conversations.android.xml.Element; import im.conversations.android.xml.Namespace; @@ -57,7 +55,6 @@ public enum ChannelBinding { return valueOf( CaseFormat.LOWER_HYPHEN.converterTo(CaseFormat.UPPER_UNDERSCORE).convert(type)); } catch (final IllegalArgumentException e) { - Log.d(Config.LOGTAG, type + " is not a known channel binding"); return null; } } diff --git a/src/main/java/im/conversations/android/xmpp/sasl/HashedToken.java b/src/main/java/im/conversations/android/xmpp/sasl/HashedToken.java index 9dde11840..4c910b299 100644 --- a/src/main/java/im/conversations/android/xmpp/sasl/HashedToken.java +++ b/src/main/java/im/conversations/android/xmpp/sasl/HashedToken.java @@ -1,14 +1,12 @@ package im.conversations.android.xmpp.sasl; import android.util.Base64; -import android.util.Log; import com.google.common.base.MoreObjects; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; import com.google.common.hash.HashFunction; import com.google.common.primitives.Bytes; -import eu.siacs.conversations.Config; import im.conversations.android.database.model.Account; import im.conversations.android.database.model.Credential; import im.conversations.android.tls.SSLSockets; @@ -18,9 +16,13 @@ import java.util.Collection; import java.util.List; import javax.net.ssl.SSLSocket; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class HashedToken extends SaslMechanism implements ChannelBindingMechanism { + private static final Logger LOGGER = LoggerFactory.getLogger(HashedToken.class); + private static final String PREFIX = "HT"; private static final List HASH_FUNCTIONS = Arrays.asList("SHA-512", "SHA-256"); @@ -65,12 +67,7 @@ public abstract class HashedToken extends SaslMechanism implements ChannelBindin try { return ChannelBindingMechanism.getChannelBindingData(sslSocket, this.channelBinding); } catch (final AuthenticationException e) { - Log.e( - Config.LOGTAG, - account.address - + ": unable to retrieve channel binding data for " - + getMechanism(), - e); + LOGGER.error("Could not retrieve channel binding data for {}", getMechanism(), e); return new byte[0]; } } diff --git a/src/main/java/im/conversations/android/xmpp/sasl/SaslMechanism.java b/src/main/java/im/conversations/android/xmpp/sasl/SaslMechanism.java index 40be22327..32c035889 100644 --- a/src/main/java/im/conversations/android/xmpp/sasl/SaslMechanism.java +++ b/src/main/java/im/conversations/android/xmpp/sasl/SaslMechanism.java @@ -1,10 +1,8 @@ package im.conversations.android.xmpp.sasl; -import android.util.Log; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Collections2; -import eu.siacs.conversations.Config; import im.conversations.android.database.model.Account; import im.conversations.android.database.model.Credential; import im.conversations.android.tls.SSLSockets; @@ -13,9 +11,13 @@ import im.conversations.android.xml.Namespace; import java.util.Collection; import java.util.Collections; import javax.net.ssl.SSLSocket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class SaslMechanism { + private static final Logger LOGGER = LoggerFactory.getLogger(SaslMechanism.class); + protected final Account account; protected final Credential credential; @@ -216,9 +218,7 @@ public abstract class SaslMechanism { if (ChannelBinding.isAvailable(cb, sslVersion)) { return mechanism; } else { - Log.d( - Config.LOGTAG, - "pinned channel binding method " + cb + " no longer available"); + LOGGER.warn("Pinned channel binding method {} no longer available", cb); return null; } } else {