Skip to content

Commit

Permalink
fcm: read main db instead of contacts, fix for crash in VCard
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Feb 6, 2019
1 parent 2dd230e commit 40aeb2c
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 51 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/co/tinode/tindroid/db/SqlStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Persistence for Tinode.
*/
class SqlStore implements Storage {
public class SqlStore implements Storage {

private static final String TAG = "SqlStore";

Expand Down Expand Up @@ -70,7 +70,7 @@ public void logout() {
}

@Override
public Topic[] topicGetAll(Tinode tinode) {
public Topic[] topicGetAll(final Tinode tinode) {
Cursor c = TopicDb.query(mDbh.getReadableDatabase());
if (c != null && c.moveToFirst()) {
Topic[] list = new Topic[c.getCount()];
Expand All @@ -83,6 +83,11 @@ public Topic[] topicGetAll(Tinode tinode) {
return null;
}

@Override
public Topic topicGet(final Tinode tinode, final String name) {
return TopicDb.readOne(mDbh.getReadableDatabase(), tinode, name);
}

@Override
public long topicAdd(Topic topic) {
StoredTopic st = (StoredTopic) topic.getLocal();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/db/StoredTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public StoredTopic() {
}

@SuppressWarnings("unchecked")
protected static void deserialize(Topic topic, Cursor c) {
static void deserialize(Topic topic, Cursor c) {
StoredTopic st = new StoredTopic();

st.id = c.getLong(TopicDb.COLUMN_IDX_ID);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/co/tinode/tindroid/db/StoredUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class StoredUser implements LocalData.Payload {
public long id;

@SuppressWarnings("unchecked")
protected static <Pu> void deserialize(User<Pu> user, Cursor c) {
static <Pu> void deserialize(User<Pu> user, Cursor c) {
StoredUser su = new StoredUser();

su.id = c.getLong(UserDb.COLUMN_IDX_ID);

user.uid = c.getString(UserDb.COLUMN_IDX_UID);
user.updated = new Date(c.getLong(UserDb.COLUMN_IDX_UPDATED));
user.pub = (Pu) BaseDb.deserialize(c.getString(UserDb.COLUMN_IDX_PUBLIC));
user.pub = BaseDb.deserialize(c.getString(UserDb.COLUMN_IDX_PUBLIC));

user.setLocal(su);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/db/TopicDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public static Cursor query(SQLiteDatabase db) {
@SuppressWarnings("unchecked, WeakerAccess")
protected static Topic readOne(Tinode tinode, Cursor c) {
// Instantiate topic of an appropriate class ('me' or 'fnd' or group)
Topic topic = tinode.newTopic(c.getString(COLUMN_IDX_TOPIC), null);
Topic topic = Tinode.newTopic(tinode, c.getString(COLUMN_IDX_TOPIC), null);
StoredTopic.deserialize(topic, c);
return topic;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/db/UserDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static long getId(SQLiteDatabase db, String uid) {
return id;
}

static <Pu> User<Pu> readOne(SQLiteDatabase db, String uid) {
public static <Pu> User<Pu> readOne(SQLiteDatabase db, String uid) {
// Instantiate topic of an appropriate class ('me' or group)
User<Pu> user = null;
String sql =
Expand Down
58 changes: 27 additions & 31 deletions app/src/main/java/co/tinode/tindroid/fcm/FBaseMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import co.tinode.tindroid.MessageActivity;
import co.tinode.tindroid.R;
import co.tinode.tindroid.UiUtils;
import co.tinode.tindroid.account.ContactsManager;
import co.tinode.tindroid.db.BaseDb;
import co.tinode.tindroid.media.VxCard;
import co.tinode.tindroid.widgets.RoundImageDrawable;
import co.tinode.tinodesdk.Storage;
import co.tinode.tinodesdk.Topic;
import co.tinode.tinodesdk.model.Subscription;
import co.tinode.tinodesdk.User;

/**
* Receive and handle (e.g. show) a push notification message.
Expand All @@ -48,6 +49,7 @@ private static Bitmap makeLargeIcon(Bitmap bmp) {
}

@Override
@SuppressWarnings("unchecked")
public void onMessageReceived(RemoteMessage remoteMessage) {
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
Expand Down Expand Up @@ -84,42 +86,36 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
}

// Fetch locally stored contacts
Subscription<VxCard,?> sender = ContactsManager.getStoredSubscription(getContentResolver(),
data.get("xfrom"));
Subscription<VxCard,?> topic = ContactsManager.getStoredSubscription(getContentResolver(),
topicName);
if (topic == null || sender == null) {
Log.w(TAG, "Unknown sender or topic in a push notification");
return;
}

Storage store = BaseDb.getInstance().getStore();
User<VxCard> sender = (User<VxCard>) store.userGet(data.get("xfrom"));
String senderName = (sender == null || sender.pub == null) ?
getResources().getString(R.string.sender_unknown) : sender.pub.fn;
Bitmap senderIcon = (sender == null || sender.pub == null) ?
null : makeLargeIcon(sender.pub.getBitmap());
Topic.TopicType tp = Topic.getTopicTypeByName(topicName);

if (tp == Topic.TopicType.P2P) {
// P2P message
if (sender != null && sender.pub != null) {
title = sender.pub.fn;
body = data.get("content");
avatar = makeLargeIcon(sender.pub.getBitmap());
}
title = senderName;
body = data.get("content");
avatar = senderIcon;

} else if (tp == Topic.TopicType.GRP) {
// Group message
if (topic != null && topic.pub != null && sender != null && sender.pub != null) {
title = topic.pub.fn;
body = sender.pub.fn + ": " + data.get("content");
avatar = makeLargeIcon(sender.pub.getBitmap());

Topic<VxCard,?,?,?> topic = (Topic<VxCard,?,?,?>) store.topicGet(null, topicName);
if (topic == null) {
Log.w(TAG, "Unknown topic: " + topicName);
return;
}
} else if (tp == Topic.TopicType.ME) {
// TODO(gene): implement invite notification
// Invite or a request to approve an application to join
title = "User Name";
body = "Invite: " + data.get("content");
} else if (tp == Topic.TopicType.FND) {
// TODO(gene): implement contact found notification
// Someone joined Tinode
title = "User Name has joined";
body = data.get("content");

if (topic.getPub() != null) {
title = topic.getPub().fn;
body = senderName + ": " + data.get("content");
avatar = senderIcon;
}
} else {
Log.w(TAG, "Unexpected topic type=" + tp);
return;
}
} else if (remoteMessage.getNotification() != null) {
RemoteMessage.Notification data = remoteMessage.getNotification();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,5 @@
<string name="tinode_invite_body">Попробуйте Tinode Messenger. Check out Tindroid Messenger. Скачайте его из https://github.com/tinode/</string>
<string name="user_not_found">Не найден</string>
<string name="topic_tags">Тэги, писок через запятую</string>
<string name="sender_unknown">Неизвестный</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,5 @@
<string name="password_reset_message_sent">Message with instructions sent</string>
<string name="user_not_found">Not found</string>
<string name="topic_tags">Tags, comma separated list</string>
<string name="sender_unknown">Unknown</string>
</resources>
2 changes: 2 additions & 0 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface Storage {

// Fetch all topics
Topic[] topicGetAll(Tinode tinode);
// Fetch one topic by name
Topic topicGet(Tinode tinode, String name);
// Add new topic
long topicAdd(Topic topic);
/** Incoming change to topic description: the already mutated topic in memory is synchronized to DB */
Expand Down
23 changes: 19 additions & 4 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1334,21 +1334,36 @@ protected void send(String message) {
mConnection.send(message);
}


/**
* Instantiate topic of an appropriate class given the name.
*
* @param tinode instance of core Tinode to attach topic to
* @param name name of the topic to create
* @param l event listener; could be null
* @return topic of an appropriate class
*/
@SuppressWarnings("unchecked")
public Topic newTopic(String name, Topic.Listener l) {
public static Topic newTopic(final Tinode tinode, final String name, final Topic.Listener l) {
if (TOPIC_ME.equals(name)) {
return new MeTopic(this, l);
return new MeTopic(tinode, l);
} else if (TOPIC_FND.equals(name)) {
return new FndTopic(this, l);
return new FndTopic(tinode, l);
}
return new ComTopic(this, name, l);
return new ComTopic(tinode, name, l);
}


/**
* Instantiate topic of an appropriate class given the name.
*
* @param name name of the topic to create
* @param l event listener; could be null
* @return topic of an appropriate class
*/
@SuppressWarnings("unchecked")
public Topic newTopic(final String name, final Topic.Listener l) {
return Tinode.newTopic(this, name, l);
}

@SuppressWarnings("unchecked")
Expand Down
9 changes: 0 additions & 9 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ protected enum NoteType {READ, RECV}
protected int mMaxDel = 0;

protected Topic(Tinode tinode, Subscription<SP,SR> sub) {
if (tinode == null) {
throw new IllegalArgumentException("Tinode cannot be null");
}
mTinode = tinode;

setName(sub.topic);
Expand All @@ -119,9 +116,6 @@ protected Topic(Tinode tinode, Subscription<SP,SR> sub) {
}

protected Topic(Tinode tinode, String name, Description<DP,DR> desc) {
if (tinode == null) {
throw new IllegalArgumentException("Tinode cannot be null");
}
mTinode = tinode;

setName(name);
Expand All @@ -140,9 +134,6 @@ protected Topic(Tinode tinode, String name, Description<DP,DR> desc) {
* @throws IllegalArgumentException if 'tinode' argument is null
*/
protected Topic(Tinode tinode, String name, Listener<DP,DR,SP,SR> l) {
if (tinode == null) {
throw new IllegalArgumentException("Tinode cannot be null");
}
mTinode = tinode;

setName(name);
Expand Down
8 changes: 8 additions & 0 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/model/VCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public VCard(String fullName, byte[] avatar) {
}

private static String typeToString(ContactType tp) {
if (tp == null) {
return null;
}

switch (tp) {
case HOME:
return TYPE_HOME;
Expand All @@ -53,6 +57,10 @@ private static String typeToString(ContactType tp) {
}

private static ContactType stringToType(String str) {
if (str == null) {
return null;
}

switch (str) {
case TYPE_HOME:
return ContactType.HOME;
Expand Down

0 comments on commit 40aeb2c

Please sign in to comment.