Skip to content

Commit

Permalink
Merge pull request #880 from redsolution/develop
Browse files Browse the repository at this point in the history
Merge v2.6.3 (602)
  • Loading branch information
Str4tocaster authored Apr 29, 2019
2 parents 713d53b + 038e9bb commit 952b489
Show file tree
Hide file tree
Showing 71 changed files with 1,470 additions and 428 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "io.realm:realm-gradle-plugin:3.1.1"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.frogermcs.androiddevmetrics:androiddevmetrics-plugin:0.4'
Expand Down
4 changes: 2 additions & 2 deletions xabber/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 591
versionName '2.6.2(591)'
versionCode 602
versionName '2.6.3(602)'
}

lintOptions {
Expand Down
7 changes: 2 additions & 5 deletions xabber/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />

<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
<data android:mimeType="video/*" />
<data android:mimeType="text/*" />
<data android:mimeType="image/*" />
<data android:mimeType="application/*" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.Toast;

import com.xabber.android.R;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.connection.CertificateManager;
import com.xabber.android.data.log.LogManager;
import com.xabber.android.ui.activity.AboutActivity;
Expand Down Expand Up @@ -219,6 +220,7 @@ public void onError(final int resourceId) {
application.addUIListener(OnErrorListener.class, onErrorListener);

CertificateManager.getInstance().registerActivity(activity);
AccountManager.getInstance().stopGracePeriod();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions xabber/src/main/java/com/xabber/android/data/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.xabber.android.data.extension.blocking.BlockingManager;
import com.xabber.android.data.extension.capability.CapabilitiesManager;
import com.xabber.android.data.extension.carbons.CarbonManager;
import com.xabber.android.data.extension.chat_markers.ChatMarkerManager;
import com.xabber.android.data.extension.cs.ChatStateManager;
import com.xabber.android.data.extension.httpfileupload.HttpFileUploadManager;
import com.xabber.android.data.extension.mam.MamManager;
Expand Down Expand Up @@ -376,6 +377,7 @@ private void addManagers() {
addManager(NetworkManager.getInstance());
addManager(ReconnectionManager.getInstance());
addManager(ReceiptManager.getInstance());
addManager(ChatMarkerManager.getInstance());
addManager(SSNManager.getInstance());
addManager(AttentionManager.getInstance());
addManager(CarbonManager.getInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public class AccountItem extends ConnectionItem implements Comparable<AccountItem> {

public static final String UNDEFINED_PASSWORD = "";
private static final long GRACE_PERIOD = 150000;

/**
* Id in database.
Expand Down Expand Up @@ -113,6 +114,8 @@ public class AccountItem extends ConnectionItem implements Comparable<AccountIte
*/
private volatile boolean successfulConnectionHappened;

private long gracePeriodEndTime = 0L;

public AccountItem(boolean custom, String host,
int port, DomainBareJid serverName, Localpart userName, Resourcepart resource,
boolean storePassword, String password, String token, int colorIndex, int order,
Expand Down Expand Up @@ -454,4 +457,16 @@ void setSuccessfulConnectionHappened(boolean successfulConnectionHappened) {
public int compareTo(@NonNull AccountItem accountItem) {
return order - accountItem.order;
}

public void startGracePeriod() {
gracePeriodEndTime = System.currentTimeMillis() + GRACE_PERIOD;
}

public void stopGracePeriod() {
gracePeriodEndTime = 0L;
}

public boolean inGracePeriod() {
return gracePeriodEndTime > System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,20 @@ public void setAllAccountAutoLoginToXabber(boolean autoLogin) {
}
}

public void startGracePeriod(AccountJid accountJid) {
AccountItem accountItem = getAccount(accountJid);
if (accountItem != null) accountItem.startGracePeriod();
}

public void stopGracePeriod(AccountJid accountJid) {
AccountItem accountItem = getAccount(accountJid);
if (accountItem != null) accountItem.stopGracePeriod();
}

public void stopGracePeriod() {
for (AccountJid accountJid : AccountManager.getInstance().getEnabledAccounts()) {
stopGracePeriod(accountJid);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import android.content.IntentFilter;

import com.xabber.android.data.Application;
import com.xabber.android.data.log.LogManager;
import com.xabber.android.data.OnCloseListener;
import com.xabber.android.data.OnInitializedListener;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.extension.csi.ClientStateManager;
import com.xabber.android.data.log.LogManager;
import com.xabber.android.receiver.GoAwayReceiver;
import com.xabber.android.receiver.GoXaReceiver;
import com.xabber.android.receiver.ScreenReceiver;
Expand Down Expand Up @@ -106,6 +106,7 @@ public void onScreen(Intent intent) {
alarmManager.cancel(goAwayPendingIntent);
alarmManager.cancel(goXaPendingIntent);
AccountManager.getInstance().wakeUp();
AccountManager.getInstance().stopGracePeriod();

// notify server(s) that client is now active
ClientStateManager.setActive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.util.Date;

import io.realm.DynamicRealm;
import io.realm.DynamicRealmObject;
import io.realm.FieldAttribute;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmObjectSchema;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.RealmSchema;
Expand All @@ -31,7 +33,7 @@

public class MessageDatabaseManager {
private static final String REALM_MESSAGE_DATABASE_NAME = "xabber.realm";
static final int REALM_MESSAGE_DATABASE_VERSION = 17;
static final int REALM_MESSAGE_DATABASE_VERSION = 19;
private final RealmConfiguration realmConfiguration;

private static MessageDatabaseManager instance;
Expand Down Expand Up @@ -300,6 +302,24 @@ public void migrate(DynamicRealm realm1, long oldVersion, long newVersion) {
oldVersion++;
}

if (oldVersion == 17) {
schema.get(MessageItem.class.getSimpleName())
.addField(MessageItem.Fields.DISPLAYED, boolean.class);

oldVersion++;
}

if (oldVersion == 18) {
schema.get(MessageItem.class.getSimpleName())
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setBoolean(MessageItem.Fields.READ, true);
}
});
oldVersion++;
}

}
})
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class RealmManager {
private static final String REALM_DATABASE_NAME = "realm_database.realm";
private static final int REALM_DATABASE_VERSION = 20;
private static final int REALM_DATABASE_VERSION = 21;
private static final String LOG_TAG = RealmManager.class.getSimpleName();
private final RealmConfiguration realmConfiguration;

Expand Down Expand Up @@ -302,10 +302,12 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}

if (oldVersion == 20) {
schema.create(UploadServer.class.getSimpleName())
.addField(UploadServer.Fields.ID, String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
.addField(UploadServer.Fields.ACCOUNT, String.class)
.addField(UploadServer.Fields.SERVER, String.class);
if (!schema.contains(UploadServer.class.getSimpleName())) {
schema.create(UploadServer.class.getSimpleName())
.addField(UploadServer.Fields.ID, String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
.addField(UploadServer.Fields.ACCOUNT, String.class)
.addField(UploadServer.Fields.SERVER, String.class);
}

oldVersion++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class Fields {
public static final String ERROR = "error";
public static final String ERROR_DESCR = "errorDescription";
public static final String DELIVERED = "delivered";
public static final String DISPLAYED = "displayed";
public static final String SENT = "sent";
public static final String READ = "read";
public static final String STANZA_ID = "stanzaId";
Expand Down Expand Up @@ -131,6 +132,10 @@ public static class Fields {
* Receipt was received for sent message.
*/
private boolean delivered;
/**
* Chat marker was received for sent message.
*/
private boolean displayed;
/**
* Message was sent.
*/
Expand Down Expand Up @@ -336,6 +341,14 @@ public void setDelivered(boolean delivered) {
this.delivered = delivered;
}

public boolean isDisplayed() {
return displayed;
}

public void setDisplayed(boolean displayed) {
this.displayed = displayed;
}

public boolean isSent() {
return sent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import com.xabber.android.data.Application;
import com.xabber.android.data.entity.AccountJid;
import com.xabber.android.data.log.LogManager;
import com.xabber.android.data.extension.chat_markers.ChatMarkerManager;
import com.xabber.android.data.extension.cs.ChatStateManager;
import com.xabber.android.data.message.MessageManager;

import org.jivesoftware.smack.packet.Message;
Expand All @@ -24,11 +25,12 @@ class CarbonCopyListener implements CarbonCopyReceivedListener {
@Override
public void onCarbonCopyReceived(final CarbonExtension.Direction direction,
final Message carbonCopy, Message wrappingMessage) {
LogManager.i(LOG_TAG, "onCarbonCopyReceived " + direction + " " + carbonCopy.getBody());
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
MessageManager.getInstance().processCarbonsMessage(account, carbonCopy, direction);
ChatMarkerManager.getInstance().processCarbonsMessage(account, carbonCopy, direction);
ChatStateManager.getInstance().processCarbonsMessage(account, carbonCopy, direction);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.xabber.android.data.extension.chat_markers;

import com.xabber.android.data.database.MessageDatabaseManager;
import com.xabber.android.data.database.messagerealm.MessageItem;
import com.xabber.android.data.log.LogManager;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.realm.Realm;
import io.realm.RealmResults;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subjects.PublishSubject;


/** Groups messages to send displayed. To avoid
* too often sending displayed chat markers.
* */
public class BackpressureMessageReader {

private static BackpressureMessageReader instance;
private Map<AbstractContact, PublishSubject<MessageHolder>> queries = new HashMap<>();

public static BackpressureMessageReader getInstance() {
if (instance == null) {
instance = new BackpressureMessageReader();
}
return instance;
}

public void markAsRead(MessageItem messageItem, boolean trySendDisplayed) {
AbstractContact contact = RosterManager.getInstance().getAbstractContact(messageItem.getAccount(), messageItem.getUser());
PublishSubject<MessageHolder> subject = queries.get(contact);
if (subject == null) subject = createSubject(contact);
subject.onNext(new MessageHolder(messageItem, trySendDisplayed));
}

private PublishSubject<MessageHolder> createSubject(final AbstractContact contact) {
PublishSubject<MessageHolder> subject = PublishSubject.create();
subject.debounce(2000, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<MessageHolder>() {
@Override
public void call(MessageHolder holder) {
MessageItem message = holder.messageItem;
if (holder.trySendDisplayed)
ChatMarkerManager.getInstance().sendDisplayed(message);

Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
RealmResults<MessageItem> messages = getPreviousUnreadMessages(realm, message);
realm.beginTransaction();
List<String> ids = new ArrayList<>();
for (MessageItem mes : messages) {
mes.setRead(true);
ids.add(mes.getUniqueId());
}
realm.commitTransaction();

AbstractChat chat = MessageManager.getInstance().getOrCreateChat(message.getAccount(), message.getUser());
if (chat != null) chat.approveRead(ids);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
LogManager.exception(this, throwable);
LogManager.d(this, "Exception is thrown. Subject was deleted.");
queries.remove(contact);
}
});
queries.put(contact, subject);
return subject;
}

private RealmResults<MessageItem> getPreviousUnreadMessages(Realm realm, MessageItem messageItem) {
return realm.where(MessageItem.class)
.equalTo(MessageItem.Fields.ACCOUNT, messageItem.getAccount().toString())
.equalTo(MessageItem.Fields.USER, messageItem.getUser().toString())
.equalTo(MessageItem.Fields.READ, false)
.lessThanOrEqualTo(MessageItem.Fields.TIMESTAMP, messageItem.getTimestamp())
.findAll();
}

private class MessageHolder {
final MessageItem messageItem;
final boolean trySendDisplayed;

public MessageHolder(MessageItem messageItem, boolean trySendDisplayed) {
this.messageItem = messageItem;
this.trySendDisplayed = trySendDisplayed;
}
}

}
Loading

0 comments on commit 952b489

Please sign in to comment.