From 1d458e8ab3db775f74404cc9e58d4ec22cc07010 Mon Sep 17 00:00:00 2001 From: Christian S Date: Sun, 27 Mar 2016 20:02:36 +0200 Subject: Update ShortcutBadger to version 1.1.4 --- build.gradle | 2 +- .../java/eu/siacs/conversations/services/XmppConnectionService.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index d190dd09..662cb12a 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { compile 'com.google.zxing:android-integration:3.2.1' compile 'de.measite.minidns:minidns:0.1.7' compile 'de.timroes.android:EnhancedListView:0.3.4' - compile 'me.leolin:ShortcutBadger:1.1.3@aar' + compile 'me.leolin:ShortcutBadger:1.1.4@aar' compile 'com.kyleduo.switchbutton:library:1.2.8' compile 'org.whispersystems:axolotl-android:1.3.4' compile 'com.makeramen:roundedimageview:2.2.0' diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 2159e92e..bf498bc6 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -2809,9 +2809,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (unreadCount != count) { Log.d(Config.LOGTAG, "update unread count to " + count); if (count > 0) { - ShortcutBadger.with(getApplicationContext()).count(count); + ShortcutBadger.applyCount(getApplicationContext(), count); } else { - ShortcutBadger.with(getApplicationContext()).remove(); + ShortcutBadger.removeCount(getApplicationContext()); } unreadCount = count; } -- cgit v1.2.3 From 343a6b4e6b75d9ca0faef0ae317200ef695a624c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 13:55:25 +0200 Subject: made setting aes keys in DownloadableFile more readable --- .../conversations/entities/DownloadableFile.java | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java b/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java index d35a4b01..4e63a66a 100644 --- a/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java +++ b/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java @@ -52,20 +52,16 @@ public class DownloadableFile extends File { public void setKeyAndIv(byte[] keyIvCombo) { if (keyIvCombo.length == 48) { - byte[] secretKey = new byte[32]; - byte[] iv = new byte[16]; - System.arraycopy(keyIvCombo, 0, iv, 0, 16); - System.arraycopy(keyIvCombo, 16, secretKey, 0, 32); - this.aeskey = secretKey; - this.iv = iv; + this.aeskey = new byte[32]; + this.iv = new byte[16]; + System.arraycopy(keyIvCombo, 0, this.iv, 0, 16); + System.arraycopy(keyIvCombo, 16, this.aeskey, 0, 32); } else if (keyIvCombo.length >= 32) { - byte[] secretKey = new byte[32]; - System.arraycopy(keyIvCombo, 0, secretKey, 0, 32); - this.aeskey = secretKey; + this.aeskey = new byte[32]; + System.arraycopy(keyIvCombo, 0, aeskey, 0, 32); } else if (keyIvCombo.length >= 16) { - byte[] secretKey = new byte[16]; - System.arraycopy(keyIvCombo, 0, secretKey, 0, 16); - this.aeskey = secretKey; + this.aeskey = new byte[16]; + System.arraycopy(keyIvCombo, 0, this.aeskey, 0, 16); } } -- cgit v1.2.3 From 8d98c52803b128463b96706044d551ed7268dac5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 13:55:46 +0200 Subject: closed some cursors under error conditions --- .../siacs/conversations/persistance/DatabaseBackend.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 2ce73796..04c53238 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -524,8 +524,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { Cursor cursor = db.query(Conversation.TABLENAME, null, Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null); - if (cursor.getCount() == 0) + if (cursor.getCount() == 0) { + cursor.close(); return null; + } cursor.moveToFirst(); Conversation conversation = Conversation.fromCursor(cursor); cursor.close(); @@ -575,12 +577,15 @@ public class DatabaseBackend extends SQLiteOpenHelper { try { cursor.moveToFirst(); int count = cursor.getInt(0); - cursor.close(); return (count > 0); } catch (SQLiteCantOpenDatabaseException e) { return true; // better safe than sorry } catch (RuntimeException e) { return true; // better safe than sorry + } finally { + if (cursor != null) { + cursor.close(); + } } } @@ -642,11 +647,12 @@ public class DatabaseBackend extends SQLiteOpenHelper { } public Pair getLastMessageReceived(Account account) { + Cursor cursor = null; try { SQLiteDatabase db = this.getReadableDatabase(); String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1"; String[] args = {account.getUuid()}; - Cursor cursor = db.rawQuery(sql, args); + cursor = db.rawQuery(sql, args); if (cursor.getCount() == 0) { return null; } else { @@ -655,6 +661,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { } } catch (Exception e) { return null; + } finally { + if (cursor != null) { + cursor.close(); + } } } -- cgit v1.2.3 From 867d0ef191faf5af2573e7721d2fb14390eca0ee Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 14:21:56 +0200 Subject: include form fields into caps hash calculation --- .../entities/ServiceDiscoveryResult.java | 19 +++++++++++++++++-- .../java/eu/siacs/conversations/xmpp/forms/Data.java | 12 ++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 42f2d840..31570d2d 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -17,6 +17,7 @@ import org.json.JSONObject; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.forms.Data; +import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.stanzas.IqPacket; public class ServiceDiscoveryResult { @@ -219,8 +220,22 @@ public class ServiceDiscoveryResult { }); for(Data form : forms) { - s.append(form.getFormType()+"<"); - //TODO append fields and values + s.append(form.getFormType() + "<"); + List fields = form.getFields(); + Collections.sort(fields, new Comparator() { + @Override + public int compare(Field lhs, Field rhs) { + return lhs.getFieldName().compareTo(rhs.getFieldName()); + } + }); + for(Field field : fields) { + s.append(field.getFieldName()+"<"); + List values = field.getValues(); + Collections.sort(values); + for(String value : values) { + s.append(value+"<"); + } + } } MessageDigest md; diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java index 380f0280..8dabcb5b 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java @@ -9,6 +9,8 @@ import eu.siacs.conversations.xml.Element; public class Data extends Element { + private static final String FORM_TYPE = "FORM_TYPE"; + public Data() { super("x"); this.setAttribute("xmlns","jabber:x:data"); @@ -17,7 +19,8 @@ public class Data extends Element { public List getFields() { ArrayList fields = new ArrayList(); for(Element child : getChildren()) { - if (child.getName().equals("field")) { + if (child.getName().equals("field") + && !FORM_TYPE.equals(child.getAttribute("var"))) { fields.add(Field.parse(child)); } } @@ -26,7 +29,8 @@ public class Data extends Element { public Field getFieldByName(String needle) { for(Element child : getChildren()) { - if (child.getName().equals("field") && needle.equals(child.getAttribute("var"))) { + if (child.getName().equals("field") + && needle.equals(child.getAttribute("var"))) { return Field.parse(child); } } @@ -76,11 +80,11 @@ public class Data extends Element { } public void setFormType(String formType) { - this.put("FORM_TYPE", formType); + this.put(FORM_TYPE, formType); } public String getFormType() { - String type = getValue("FORM_TYPE"); + String type = getValue(FORM_TYPE); return type == null ? "" : type; } -- cgit v1.2.3 From e074104004fa146611ce39cd32751ab3cd718dd1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 21:15:49 +0200 Subject: save otr fingerprint in message --- src/main/java/eu/siacs/conversations/entities/Message.java | 4 ++-- .../java/eu/siacs/conversations/parser/MessageParser.java | 11 +++++++---- .../siacs/conversations/services/XmppConnectionService.java | 4 ++-- .../java/eu/siacs/conversations/ui/ConversationFragment.java | 4 ++-- .../eu/siacs/conversations/ui/adapter/MessageAdapter.java | 2 +- .../eu/siacs/conversations/xmpp/jingle/JingleConnection.java | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index be9d3d92..f6a45533 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -773,11 +773,11 @@ public class Message extends AbstractEntity { public int height = 0; } - public void setAxolotlFingerprint(String fingerprint) { + public void setFingerprint(String fingerprint) { this.axolotlFingerprint = fingerprint; } - public String getAxolotlFingerprint() { + public String getFingerprint() { return axolotlFingerprint; } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 9470dab8..21bdbdf6 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -6,12 +6,12 @@ import android.util.Pair; import net.java.otr4j.session.Session; import net.java.otr4j.session.SessionStatus; -import java.net.URL; import java.util.ArrayList; import java.util.Set; import java.util.UUID; import eu.siacs.conversations.Config; +import eu.siacs.conversations.crypto.OtrService; import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage; import eu.siacs.conversations.entities.Account; @@ -95,8 +95,11 @@ public class MessageParser extends AbstractParser implements conversation.setSymmetricKey(CryptoHelper.hexToBytes(key)); return null; } + final OtrService otrService = conversation.getAccount().getOtrService(); Message finishedMessage = new Message(conversation, body, Message.ENCRYPTION_OTR, Message.STATUS_RECEIVED); + finishedMessage.setFingerprint(otrService.getFingerprint(otrSession.getRemotePublicKey())); conversation.setLastReceivedOtrMessageId(null); + return finishedMessage; } catch (Exception e) { conversation.resetOtrSession(); @@ -111,7 +114,7 @@ public class MessageParser extends AbstractParser implements XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = service.processReceivingPayloadMessage(xmppAxolotlMessage); if(plaintextMessage != null) { finishedMessage = new Message(conversation, plaintextMessage.getPlaintext(), Message.ENCRYPTION_AXOLOTL, status); - finishedMessage.setAxolotlFingerprint(plaintextMessage.getFingerprint()); + finishedMessage.setFingerprint(plaintextMessage.getFingerprint()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(finishedMessage.getConversation().getAccount())+" Received Message with session fingerprint: "+plaintextMessage.getFingerprint()); } @@ -408,8 +411,8 @@ public class MessageParser extends AbstractParser implements message.getStatus() == Message.STATUS_RECEIVED, message.isCarbon()); if (replacedMessage != null) { - final boolean fingerprintsMatch = replacedMessage.getAxolotlFingerprint() == null - || replacedMessage.getAxolotlFingerprint().equals(message.getAxolotlFingerprint()); + final boolean fingerprintsMatch = replacedMessage.getFingerprint() == null + || replacedMessage.getFingerprint().equals(message.getFingerprint()); final boolean trueCountersMatch = replacedMessage.getTrueCounterpart() != null && replacedMessage.getTrueCounterpart().equals(message.getTrueCounterpart()); if (fingerprintsMatch && (trueCountersMatch || conversation.getMode() == Conversation.MODE_SINGLE)) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index bf498bc6..cc0a8f70 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -931,7 +931,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } break; case Message.ENCRYPTION_AXOLOTL: - message.setAxolotlFingerprint(account.getAxolotlService().getOwnFingerprint()); + message.setFingerprint(account.getAxolotlService().getOwnFingerprint()); if (message.needsUploading()) { if (account.httpUploadAvailable() || message.fixCounterpart()) { this.sendFileMessage(message, delay); @@ -977,7 +977,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } break; case Message.ENCRYPTION_AXOLOTL: - message.setAxolotlFingerprint(account.getAxolotlService().getOwnFingerprint()); + message.setFingerprint(account.getAxolotlService().getOwnFingerprint()); break; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 679e2a03..1a834ae5 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -468,13 +468,13 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa highlightInConference(user); } } else { - activity.switchToContactDetails(message.getContact(), message.getAxolotlFingerprint()); + activity.switchToContactDetails(message.getContact(), message.getFingerprint()); } } else { Account account = message.getConversation().getAccount(); Intent intent = new Intent(activity, EditAccountActivity.class); intent.putExtra("jid", account.getJid().toBareJid().toString()); - intent.putExtra("fingerprint", message.getAxolotlFingerprint()); + intent.putExtra("fingerprint", message.getFingerprint()); startActivity(intent); } } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index ab9f74a3..0268097f 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -202,7 +202,7 @@ public class MessageAdapter extends ArrayAdapter { if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { XmppAxolotlSession.Trust trust = message.getConversation() .getAccount().getAxolotlService().getFingerprintTrust( - message.getAxolotlFingerprint()); + message.getFingerprint()); if(trust == null || (!trust.trusted() && !trust.trustedInactive())) { viewHolder.indicator.setColorFilter(activity.getWarningTextColor()); diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index e3eec647..beed92fa 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -378,7 +378,7 @@ public class JingleConnection implements Transferable { message.setEncryption(Message.ENCRYPTION_AXOLOTL); this.file.setKey(transportMessage.getKey()); this.file.setIv(transportMessage.getIv()); - message.setAxolotlFingerprint(transportMessage.getFingerprint()); + message.setFingerprint(transportMessage.getFingerprint()); } else { Log.d(Config.LOGTAG,"could not process KeyTransportMessage"); } -- cgit v1.2.3 From 74c496fe3ef544d45c94365799c061ddfe898330 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 21:56:59 +0200 Subject: add methods to check max file size for http upload --- .../eu/siacs/conversations/entities/Account.java | 6 ++++- .../entities/ServiceDiscoveryResult.java | 13 ++++++++++ .../services/XmppConnectionService.java | 3 +-- .../conversations/ui/EditAccountActivity.java | 2 +- .../siacs/conversations/xmpp/XmppConnection.java | 29 ++++++++++++++++------ 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 356b34e5..2356ffb9 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -52,8 +52,12 @@ public class Account extends AbstractEntity { public static final int OPTION_USECOMPRESSION = 3; public final HashSet> inProgressDiscoFetches = new HashSet<>(); + public boolean httpUploadAvailable(long filesize) { + return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize); + } + public boolean httpUploadAvailable() { - return xmppConnection != null && xmppConnection.getFeatures().httpUpload(); + return httpUploadAvailable(0); } public void setDisplayName(String displayName) { diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 31570d2d..c4fc30ab 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -190,6 +190,19 @@ public class ServiceDiscoveryResult { return false; } + public String getExtendedDiscoInformation(String formType, String name) { + for(Data form : this.forms) { + if (formType.equals(form.getFormType())) { + for(Field field: form.getFields()) { + if (name.equals(field.getFieldName())) { + return field.getValue(); + } + } + } + } + return null; + } + protected byte[] mkCapHash() { StringBuilder s = new StringBuilder(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index cc0a8f70..1d7a4600 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -847,8 +847,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private void sendFileMessage(final Message message, final boolean delay) { Log.d(Config.LOGTAG, "send file message"); final Account account = message.getConversation().getAccount(); - final XmppConnection connection = account.getXmppConnection(); - if (connection != null && connection.getFeatures().httpUpload()) { + if (account.httpUploadAvailable()) { mHttpConnectionManager.createNewUploadConnection(message, delay); } else { mJingleConnectionManager.createNewConnection(message); diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index ed80a509..9d73290a 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -688,7 +688,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } else { this.mServerInfoPep.setText(R.string.server_info_unavailable); } - if (features.httpUpload()) { + if (features.httpUpload(0)) { this.mServerInfoHttpUpload.setText(R.string.server_info_available); } else { this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable); diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index fd0e355d..1552fe1a 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1346,12 +1346,12 @@ public class XmppConnection implements Runnable { this.streamId = null; } - public List findDiscoItemsByFeature(final String feature) { + private List> findDiscoItemsByFeature(final String feature) { synchronized (this.disco) { - final List items = new ArrayList<>(); + final List> items = new ArrayList<>(); for (final Entry cursor : this.disco.entrySet()) { if (cursor.getValue().getFeatures().contains(feature)) { - items.add(cursor.getKey()); + items.add(cursor); } } return items; @@ -1359,9 +1359,9 @@ public class XmppConnection implements Runnable { } public Jid findDiscoItemByFeature(final String feature) { - final List items = findDiscoItemsByFeature(feature); + final List> items = findDiscoItemsByFeature(feature); if (items.size() >= 1) { - return items.get(0); + return items.get(0).getKey(); } return null; } @@ -1505,7 +1505,6 @@ public class XmppConnection implements Runnable { public boolean pep() { synchronized (XmppConnection.this.disco) { - final Pair needle = new Pair<>("pubsub", "pep"); ServiceDiscoveryResult info = disco.get(account.getServer()); if (info != null && info.hasIdentity("pubsub", "pep")) { return true; @@ -1534,8 +1533,22 @@ public class XmppConnection implements Runnable { this.blockListRequested = value; } - public boolean httpUpload() { - return !Config.DISABLE_HTTP_UPLOAD && findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0; + public boolean httpUpload(long filesize) { + if (Config.DISABLE_HTTP_UPLOAD) { + return false; + } else { + List> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD); + if (items.size() > 0) { + try { + long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); + return maxsize <= filesize; + } catch (Exception e) { + return filesize <= 0; + } + } else { + return false; + } + } } } -- cgit v1.2.3 From 2549ce89b0fac2a41c4de61c42d76e521875717f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 1 Apr 2016 00:03:14 +0200 Subject: check max http file size when attaching files --- .../conversations/persistance/FileBackend.java | 22 ++++++++++++++++ .../services/XmppConnectionService.java | 11 +++++--- .../conversations/ui/ConversationActivity.java | 30 ++++++++++++++++++---- .../siacs/conversations/ui/ShareWithActivity.java | 11 +++++++- .../eu/siacs/conversations/ui/XmppActivity.java | 4 +++ .../siacs/conversations/xmpp/XmppConnection.java | 17 ++++++++++-- 6 files changed, 83 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 18c195f1..6e118ec2 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -1,6 +1,8 @@ package eu.siacs.conversations.persistance; +import android.content.Context; import android.content.Intent; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -8,6 +10,7 @@ import android.graphics.Matrix; import android.graphics.RectF; import android.net.Uri; import android.os.Environment; +import android.provider.OpenableColumns; import android.util.Base64; import android.util.Base64OutputStream; import android.util.Log; @@ -28,6 +31,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Locale; import eu.siacs.conversations.Config; @@ -110,6 +114,24 @@ public class FileBackend { } } + public static long getFileSize(Context context, Uri uri) { + Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); + } else { + return -1; + } + } + + public static boolean allFilesUnderSize(Context context, List uris, long max) { + for(Uri uri : uris) { + if (FileBackend.getFileSize(context, uri) > max) { + return false; + } + } + return true; + } + public static String getConversationsFileDirectory() { return Environment.getExternalStorageDirectory().getAbsolutePath()+"/Conversations/"; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 1d7a4600..28ac4d19 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -847,7 +847,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private void sendFileMessage(final Message message, final boolean delay) { Log.d(Config.LOGTAG, "send file message"); final Account account = message.getConversation().getAccount(); - if (account.httpUploadAvailable()) { + if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize())) { mHttpConnectionManager.createNewUploadConnection(message, delay); } else { mJingleConnectionManager.createNewConnection(message); @@ -884,7 +884,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa switch (message.getEncryption()) { case Message.ENCRYPTION_NONE: if (message.needsUploading()) { - if (account.httpUploadAvailable() || message.fixCounterpart()) { + if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize()) + || message.fixCounterpart()) { this.sendFileMessage(message, delay); } else { break; @@ -896,7 +897,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa case Message.ENCRYPTION_PGP: case Message.ENCRYPTION_DECRYPTED: if (message.needsUploading()) { - if (account.httpUploadAvailable() || message.fixCounterpart()) { + if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize()) + || message.fixCounterpart()) { this.sendFileMessage(message, delay); } else { break; @@ -932,7 +934,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa case Message.ENCRYPTION_AXOLOTL: message.setFingerprint(account.getAxolotlService().getOwnFingerprint()); if (message.needsUploading()) { - if (account.httpUploadAvailable() || message.fixCounterpart()) { + if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize()) + || message.fixCounterpart()) { this.sendFileMessage(message, delay); } else { break; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index bb85439a..42252ede 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -54,6 +54,7 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Transferable; +import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate; @@ -1298,12 +1299,31 @@ public class ConversationActivity extends XmppActivity } } } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) { - mPendingFileUris.clear(); - mPendingFileUris.addAll(extractUriFromIntent(data)); - if (xmppConnectionServiceBound) { - for (Iterator i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) { - attachFileToConversation(getSelectedConversation(), i.next()); + final List uris = extractUriFromIntent(data); + final Conversation c = getSelectedConversation(); + final long max = c.getAccount() + .getXmppConnection() + .getFeatures() + .getMaxHttpUploadSize(); + final OnPresenceSelected callback = new OnPresenceSelected() { + @Override + public void onPresenceSelected() { + mPendingFileUris.clear(); + mPendingFileUris.addAll(uris); + if (xmppConnectionServiceBound) { + for (Iterator i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) { + attachFileToConversation(c, i.next()); + } + } } + }; + if (max <= 0 + || c.getMode() == Conversation.MODE_MULTI + || FileBackend.allFilesUnderSize(this, uris, max) + || c.getNextEncryption() == Message.ENCRYPTION_OTR) { + callback.onPresenceSelected(); + } else { + selectPresence(c, callback); } } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) { if (mPendingImageUris.size() == 1) { diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index ae722081..90333cfe 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -24,6 +24,7 @@ import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.ui.adapter.ConversationAdapter; import eu.siacs.conversations.xmpp.jid.InvalidJidException; @@ -259,6 +260,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } private void share(final Conversation conversation) { + final Account account = conversation.getAccount(); mListView.setEnabled(false); if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) { if (share.uuid == null) { @@ -270,6 +272,9 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer return; } if (share.uris.size() != 0) { + final long max = account.getXmppConnection() + .getFeatures() + .getMaxHttpUploadSize(); OnPresenceSelected callback = new OnPresenceSelected() { @Override public void onPresenceSelected() { @@ -290,7 +295,11 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } } }; - if (conversation.getAccount().httpUploadAvailable()) { + if (account.httpUploadAvailable() + && ( + (share.image && !neverCompressPictures()) + || conversation.getMode() == Conversation.MODE_MULTI + || FileBackend.allFilesUnderSize(this, share.uris, max))) { callback.onPresenceSelected(); } else { selectPresence(conversation, callback); diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index fe3a3031..7d70b20f 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -1002,6 +1002,10 @@ public abstract class XmppActivity extends Activity { } } + protected boolean neverCompressPictures() { + return getPreferences().getString("picture_compression", "auto").equals("never"); + } + protected void unregisterNdefPushMessageCallback() { NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter != null && nfcAdapter.isEnabled()) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 1552fe1a..6371f115 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1541,15 +1541,28 @@ public class XmppConnection implements Runnable { if (items.size() > 0) { try { long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); - return maxsize <= filesize; + return filesize <= maxsize; } catch (Exception e) { - return filesize <= 0; + return true; } } else { return false; } } } + + public long getMaxHttpUploadSize() { + List> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD); + if (items.size() > 0) { + try { + return Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); + } catch (Exception e) { + return -1; + } + } else { + return -1; + } + } } private IqGenerator getIqGenerator() { -- cgit v1.2.3 From 701742f5500ade311741c8af59fcd73181a6e6f6 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 2 Apr 2016 18:07:38 +0200 Subject: don't ask for resource when server uses http upload v0.1 --- src/main/java/eu/siacs/conversations/persistance/FileBackend.java | 5 ++++- src/main/java/eu/siacs/conversations/ui/ConversationActivity.java | 3 +-- src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 6e118ec2..21ba4509 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -114,7 +114,7 @@ public class FileBackend { } } - public static long getFileSize(Context context, Uri uri) { + private static long getFileSize(Context context, Uri uri) { Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); @@ -124,6 +124,9 @@ public class FileBackend { } public static boolean allFilesUnderSize(Context context, List uris, long max) { + if (max <= 0) { + return true; //exception to be compatible with HTTP Upload < v0.2 + } for(Uri uri : uris) { if (FileBackend.getFileSize(context, uri) > max) { return false; diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 42252ede..184b5dc6 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -1317,8 +1317,7 @@ public class ConversationActivity extends XmppActivity } } }; - if (max <= 0 - || c.getMode() == Conversation.MODE_MULTI + if (c.getMode() == Conversation.MODE_MULTI || FileBackend.allFilesUnderSize(this, uris, max) || c.getNextEncryption() == Message.ENCRYPTION_OTR) { callback.onPresenceSelected(); diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index 90333cfe..8cd017bf 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -296,10 +296,10 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } }; if (account.httpUploadAvailable() - && ( - (share.image && !neverCompressPictures()) + && ((share.image && !neverCompressPictures()) || conversation.getMode() == Conversation.MODE_MULTI - || FileBackend.allFilesUnderSize(this, share.uris, max))) { + || FileBackend.allFilesUnderSize(this, share.uris, max)) + && conversation.getNextEncryption() != Message.ENCRYPTION_OTR) { callback.onPresenceSelected(); } else { selectPresence(conversation, callback); -- cgit v1.2.3 From 5eedce91f9d92e5139f13fddb57314570f42b9ad Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 2 Apr 2016 18:09:07 +0200 Subject: version bump to 1.11.3 and changelog --- CHANGELOG.md | 4 ++++ build.gradle | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9b3881c..85e7c5ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ###Changelog +####Version 1.11.3 +* check maximum file size when using HTTP Upload +* properly calculate caps hash + ####Version 1.11.2 * only add image files to media scanner * allow to delete files diff --git a/build.gradle b/build.gradle index 662cb12a..bcdff444 100644 --- a/build.gradle +++ b/build.gradle @@ -58,8 +58,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 23 - versionCode 134 - versionName "1.11.2" + versionCode 135 + versionName "1.11.3" archivesBaseName += "-$versionName" applicationId "eu.siacs.conversations" } -- cgit v1.2.3