From 9502ff25dd6c32c92d356baaaa1640fe26314595 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 18:16:14 +0200 Subject: Add compression support --- res/layout/edit_account_dialog.xml | 8 +- src/eu/siacs/conversations/entities/Account.java | 1 + src/eu/siacs/conversations/ui/EditAccount.java | 13 ++-- .../conversations/utils/zlib/ZLibInputStream.java | 52 +++++++++++++ .../conversations/utils/zlib/ZLibOutputStream.java | 89 ++++++++++++++++++++++ src/eu/siacs/conversations/xml/TagWriter.java | 8 +- src/eu/siacs/conversations/xml/XmlReader.java | 6 +- .../siacs/conversations/xmpp/XmppConnection.java | 60 ++++++++++++++- 8 files changed, 227 insertions(+), 10 deletions(-) create mode 100644 src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java create mode 100644 src/eu/siacs/conversations/utils/zlib/ZLibOutputStream.java diff --git a/res/layout/edit_account_dialog.xml b/res/layout/edit_account_dialog.xml index 2b1f95ab..cc29b6f9 100644 --- a/res/layout/edit_account_dialog.xml +++ b/res/layout/edit_account_dialog.xml @@ -50,7 +50,13 @@ android:layout_height="wrap_content" android:text="Use Transport Layer Security (TLS)" android:checked="true"/> - + + Android 2.2 includes Java7 FLUSH_SYNC option, which will be used by this + * Implementation, preferable via reflection. The @hide was remove in API level + * 19. This class might thus go away in the future.

+ *

Please use {@link ZLibOutputStream#SUPPORTED} to check for flush + * compatibility.

+ */ +public class ZLibOutputStream extends DeflaterOutputStream { + + /** + * The reflection based flush method. + */ + + private final static Method method; + /** + * SUPPORTED is true if a flush compatible method exists. + */ + public final static boolean SUPPORTED; + + /** + * Static block to initialize {@link #SUPPORTED} and {@link #method}. + */ + static { + Method m = null; + try { + m = Deflater.class.getMethod("deflate", byte[].class, int.class, int.class, int.class); + } catch (SecurityException e) { + } catch (NoSuchMethodException e) { + } + method = m; + SUPPORTED = (method != null); + } + + /** + * Create a new ZLib compatible output stream wrapping the given low level + * stream. ZLib compatiblity means we will send a zlib header. + * @param os OutputStream The underlying stream. + * @throws IOException In case of a lowlevel transfer problem. + * @throws NoSuchAlgorithmException In case of a {@link Deflater} error. + */ + public ZLibOutputStream(OutputStream os) throws IOException, + NoSuchAlgorithmException { + super(os, new Deflater(Deflater.BEST_COMPRESSION)); + } + + /** + * Flush the given stream, preferring Java7 FLUSH_SYNC if available. + * @throws IOException In case of a lowlevel exception. + */ + @Override + public void flush() throws IOException { + if (!SUPPORTED) { + super.flush(); + return; + } + int count = 0; + if (!def.needsInput()) { + do { + count = def.deflate(buf, 0, buf.length); + out.write(buf, 0, count); + } while (count > 0); + out.flush(); + } + try { + do { + count = (Integer) method.invoke(def, buf, 0, buf.length, 2); + out.write(buf, 0, count); + } while (count > 0); + } catch (IllegalArgumentException e) { + throw new IOException("Can't flush"); + } catch (IllegalAccessException e) { + throw new IOException("Can't flush"); + } catch (InvocationTargetException e) { + throw new IOException("Can't flush"); + } + super.flush(); + } + +} diff --git a/src/eu/siacs/conversations/xml/TagWriter.java b/src/eu/siacs/conversations/xml/TagWriter.java index f06664fd..d945b47b 100644 --- a/src/eu/siacs/conversations/xml/TagWriter.java +++ b/src/eu/siacs/conversations/xml/TagWriter.java @@ -9,6 +9,7 @@ import eu.siacs.conversations.xmpp.stanzas.AbstractStanza; public class TagWriter { + private OutputStream plainOutputStream; private OutputStreamWriter outputStream; private boolean finshed = false; private LinkedBlockingQueue writeQueue = new LinkedBlockingQueue(); @@ -37,9 +38,14 @@ public class TagWriter { } public void setOutputStream(OutputStream out) { + this.plainOutputStream = out; this.outputStream = new OutputStreamWriter(out); } - + + public OutputStream getOutputStream() { + return this.plainOutputStream; + } + public TagWriter beginDocument() throws IOException { outputStream.write(""); outputStream.flush(); diff --git a/src/eu/siacs/conversations/xml/XmlReader.java b/src/eu/siacs/conversations/xml/XmlReader.java index 7ae9a7b3..71e86cf9 100644 --- a/src/eu/siacs/conversations/xml/XmlReader.java +++ b/src/eu/siacs/conversations/xml/XmlReader.java @@ -36,7 +36,11 @@ public class XmlReader { Log.d(LOGTAG,"error setting input stream"); } } - + + public InputStream getInputStream() { + return is; + } + public void reset() { try { parser.setInput(new InputStreamReader(this.is)); diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index 1fd42bca..3702e66b 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -38,6 +38,8 @@ import android.util.Log; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.DNSHelper; +import eu.siacs.conversations.utils.zlib.ZLibOutputStream; +import eu.siacs.conversations.utils.zlib.ZLibInputStream; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Tag; import eu.siacs.conversations.xml.TagWriter; @@ -177,6 +179,13 @@ public class XmppConnection implements Runnable { wakeLock.release(); } return; + } catch (NoSuchAlgorithmException e) { + this.changeStatus(Account.STATUS_OFFLINE); + Log.d(LOGTAG, "compression exception " + e.getMessage()); + if (wakeLock.isHeld()) { + wakeLock.release(); + } + return; } catch (XmlPullParserException e) { this.changeStatus(Account.STATUS_OFFLINE); Log.d(LOGTAG, "xml exception " + e.getMessage()); @@ -194,7 +203,7 @@ public class XmppConnection implements Runnable { } private void processStream(Tag currentTag) throws XmlPullParserException, - IOException { + IOException, NoSuchAlgorithmException { Tag nextTag = tagReader.readTag(); while ((nextTag != null) && (!nextTag.isEnd("stream"))) { if (nextTag.isStart("error")) { @@ -208,6 +217,8 @@ public class XmppConnection implements Runnable { } } else if (nextTag.isStart("proceed")) { switchOverToTls(nextTag); + } else if (nextTag.isStart("compressed")) { + switchOverToZLib(nextTag); } else if (nextTag.isStart("success")) { Log.d(LOGTAG, account.getJid() + ": logged in"); @@ -375,6 +386,33 @@ public class XmppConnection implements Runnable { } } + private void sendCompressionZlib() throws IOException { + tagWriter.writeElement(new Element("compress") { + public String toString() { + return + "" + + "zlib" + + ""; + } + }); + } + + private void switchOverToZLib(Tag currentTag) throws XmlPullParserException, + IOException, NoSuchAlgorithmException { + + Log.d(LOGTAG,account.getJid()+": Starting zlib compressed stream"); + + tagReader.readTag(); // read tag close + + tagWriter.setOutputStream(new ZLibOutputStream(tagWriter.getOutputStream())); + tagReader.setInputStream(new ZLibInputStream(tagReader.getInputStream())); + + sendStartStream(); + processStream(tagReader.readTag()); + + Log.d(LOGTAG,account.getJid()+": zlib compressed stream established"); + } + private void sendStartTLS() throws IOException { Tag startTLS = Tag.empty("starttls"); startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls"); @@ -486,6 +524,8 @@ public class XmppConnection implements Runnable { if (this.streamFeatures.hasChild("starttls") && account.isOptionSet(Account.OPTION_USETLS)) { sendStartTLS(); + } else if (compressionAvailable()) { + sendCompressionZlib(); } else if (this.streamFeatures.hasChild("register")&&(account.isOptionSet(Account.OPTION_REGISTER))) { sendRegistryRequest(); } else if (!this.streamFeatures.hasChild("register")&&(account.isOptionSet(Account.OPTION_REGISTER))) { @@ -514,6 +554,24 @@ public class XmppConnection implements Runnable { } } + private boolean compressionAvailable() { + if (!this.streamFeatures.hasChild("compression", "http://jabber.org/features/compress")) return false; + if (!ZLibOutputStream.SUPPORTED) return false; + if (!account.isOptionSet(Account.OPTION_USECOMPRESSION)) return false; + + Element compression = this.streamFeatures.findChild("compression", "http://jabber.org/features/compress"); + for (Element child : compression.getChildren()) { + if (!"method".equals(child.getName())) continue; + + if ("zlib".equalsIgnoreCase(child.getContent())) { + Log.d(LOGTAG, account.getJid() + ": compression available"); + return true; + } + } + + return false; + } + private List extractMechanisms(Element stream) { ArrayList mechanisms = new ArrayList(stream.getChildren().size()); for(Element child : stream.getChildren()) { -- cgit v1.2.3 From 7db21136a3221beccdddf6c94ecf1303a6be986a Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 18:37:48 +0200 Subject: Add support for users with empty name --- src/eu/siacs/conversations/utils/UIHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eu/siacs/conversations/utils/UIHelper.java b/src/eu/siacs/conversations/utils/UIHelper.java index a05d590d..f17e738c 100644 --- a/src/eu/siacs/conversations/utils/UIHelper.java +++ b/src/eu/siacs/conversations/utils/UIHelper.java @@ -66,7 +66,7 @@ public class UIHelper { } private static Bitmap getUnknownContactPicture(String name, int size) { - String firstLetter = name.substring(0, 1).toUpperCase(Locale.US); + String firstLetter = (name.length() > 0) ? name.substring(0, 1).toUpperCase(Locale.US) : " "; int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713, 0xFFe92727 }; -- cgit v1.2.3 From e6797e1db4282459d86f78698ed84caf0ce6aba3 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 20:54:50 +0200 Subject: Remove option for compression (enabled by default / hidden setting) --- res/layout/edit_account_dialog.xml | 7 ------- src/eu/siacs/conversations/ui/EditAccount.java | 6 +----- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/res/layout/edit_account_dialog.xml b/res/layout/edit_account_dialog.xml index cc29b6f9..453b15d9 100644 --- a/res/layout/edit_account_dialog.xml +++ b/res/layout/edit_account_dialog.xml @@ -51,13 +51,6 @@ android:text="Use Transport Layer Security (TLS)" android:checked="true"/> - - Date: Thu, 3 Apr 2014 21:22:17 +0200 Subject: Remove option to enable/disable TLS (now enabled by default) --- res/layout/edit_account_dialog.xml | 9 --------- src/eu/siacs/conversations/ui/EditAccount.java | 5 +---- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/res/layout/edit_account_dialog.xml b/res/layout/edit_account_dialog.xml index 453b15d9..0a86039d 100644 --- a/res/layout/edit_account_dialog.xml +++ b/res/layout/edit_account_dialog.xml @@ -42,15 +42,6 @@ android:hint="Password" android:fontFamily="sans-serif" /> - - - - Date: Thu, 3 Apr 2014 21:25:03 +0200 Subject: Only apply defaults if the account is new --- src/eu/siacs/conversations/ui/EditAccount.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/eu/siacs/conversations/ui/EditAccount.java b/src/eu/siacs/conversations/ui/EditAccount.java index 46f4ecfa..8b1c0fa6 100644 --- a/src/eu/siacs/conversations/ui/EditAccount.java +++ b/src/eu/siacs/conversations/ui/EditAccount.java @@ -132,9 +132,9 @@ public class EditAccount extends DialogFragment { account.setServer(server); } else { account = new Account(username, server, password); + account.setOption(Account.OPTION_USETLS, true); + account.setOption(Account.OPTION_USECOMPRESSION, true); } - account.setOption(Account.OPTION_USETLS, true); - account.setOption(Account.OPTION_USECOMPRESSION, true); account.setOption(Account.OPTION_REGISTER, register.isChecked()); if (listener != null) { listener.onAccountEdited(account); -- cgit v1.2.3 From a0fc1c6c77aea1d99078706cf2bee1c5efd246a4 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 22:28:37 +0200 Subject: Migrate all accounts to use compression --- src/eu/siacs/conversations/persistance/DatabaseBackend.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/eu/siacs/conversations/persistance/DatabaseBackend.java index 434d6779..4c0d6216 100644 --- a/src/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -23,7 +23,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static DatabaseBackend instance = null; private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 1; + private static final int DATABASE_VERSION = 2; public DatabaseBackend(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); @@ -66,9 +66,12 @@ public class DatabaseBackend extends SQLiteOpenHelper { } @Override - public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { - // TODO Auto-generated method stub - + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion < 2 && newVersion >= 2) { + // enable compression by default. + db.execSQL("update " + Account.TABLENAME + + " set " + Account.OPTIONS + " = " + Account.OPTIONS + " | 8"); + } } public static synchronized DatabaseBackend getInstance(Context context) { -- cgit v1.2.3