-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development'
# Conflicts: # settings.gradle
- Loading branch information
Showing
61 changed files
with
1,592 additions
and
1,333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#Contains API Secret used to validate your application. Commit to internal source control; avoid making secret public. | ||
#Wed Mar 25 11:57:04 GMT+02:00 2015 | ||
apiSecret=343e58f13448a59778de3a8f630a0bd4a03803d1ae397818fbe2e76c66d09398 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
sample-chat/src/main/java/com/quickblox/sample/chat/managers/DialogsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package com.quickblox.sample.chat.managers; | ||
|
||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.quickblox.chat.QBChatService; | ||
import com.quickblox.chat.QBSystemMessagesManager; | ||
import com.quickblox.chat.model.QBChatDialog; | ||
import com.quickblox.chat.model.QBChatMessage; | ||
import com.quickblox.chat.model.QBDialogType; | ||
import com.quickblox.sample.chat.utils.chat.ChatHelper; | ||
import com.quickblox.sample.chat.utils.qb.QbDialogHolder; | ||
import com.quickblox.sample.chat.utils.qb.QbDialogUtils; | ||
import com.quickblox.sample.chat.utils.qb.callback.QbEntityCallbackImpl; | ||
import com.quickblox.users.model.QBUser; | ||
|
||
import org.jivesoftware.smack.SmackException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
|
||
public class DialogsManager { | ||
|
||
public static final String PROPERTY_OCCUPANTS_IDS = "occupants_ids"; | ||
public static final String PROPERTY_DIALOG_TYPE = "dialog_type"; | ||
public static final String PROPERTY_DIALOG_NAME = "dialog_name"; | ||
public static final String PROPERTY_NOTIFICATION_TYPE = "notification_type"; | ||
public static final String CREATING_DIALOG = "creating_dialog"; | ||
|
||
private Set<ManagingDialogsCallbacks> managingDialogsCallbackListener = new CopyOnWriteArraySet<>(); | ||
|
||
private boolean isMessageCreatingDialog(QBChatMessage systemMessage){ | ||
return CREATING_DIALOG.equals(systemMessage.getProperty(PROPERTY_NOTIFICATION_TYPE)); | ||
} | ||
|
||
private QBChatMessage buildSystemMessageAboutCreatingGroupDialog(QBChatDialog dialog){ | ||
QBChatMessage qbChatMessage = new QBChatMessage(); | ||
qbChatMessage.setDialogId(dialog.getDialogId()); | ||
qbChatMessage.setProperty(PROPERTY_OCCUPANTS_IDS, QbDialogUtils.getOccupantsIdsStringFromList(dialog.getOccupants())); | ||
qbChatMessage.setProperty(PROPERTY_DIALOG_TYPE, String.valueOf(dialog.getType().getCode())); | ||
qbChatMessage.setProperty(PROPERTY_DIALOG_NAME, String.valueOf(dialog.getName())); | ||
qbChatMessage.setProperty(PROPERTY_NOTIFICATION_TYPE, CREATING_DIALOG); | ||
|
||
return qbChatMessage; | ||
} | ||
|
||
private QBChatDialog buildChatDialogFromSystemMessage(QBChatMessage qbChatMessage){ | ||
QBChatDialog chatDialog = new QBChatDialog(); | ||
chatDialog.setDialogId(qbChatMessage.getDialogId()); | ||
chatDialog.setOccupantsIds(QbDialogUtils.getOccupantsIdsListFromString((String) qbChatMessage.getProperty(PROPERTY_OCCUPANTS_IDS))); | ||
chatDialog.setType(QBDialogType.parseByCode(Integer.parseInt(qbChatMessage.getProperty(PROPERTY_DIALOG_TYPE).toString()))); | ||
chatDialog.setName(qbChatMessage.getProperty(PROPERTY_DIALOG_NAME).toString()); | ||
chatDialog.setUnreadMessageCount(0); | ||
|
||
return chatDialog; | ||
} | ||
|
||
public void sendSystemMessageAboutCreatingDialog(QBSystemMessagesManager systemMessagesManager, QBChatDialog dialog) { | ||
QBChatMessage systemMessageCreatingDialog = buildSystemMessageAboutCreatingGroupDialog(dialog); | ||
|
||
try { | ||
for (Integer recipientId : dialog.getOccupants()) { | ||
if (!recipientId.equals(QBChatService.getInstance().getUser().getId())) { | ||
systemMessageCreatingDialog.setRecipientId(recipientId); | ||
systemMessagesManager.sendSystemMessage(systemMessageCreatingDialog); | ||
} | ||
} | ||
} catch (SmackException.NotConnectedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void loadUsersFromDialog(QBChatDialog chatDialog){ | ||
ChatHelper.getInstance().getUsersFromDialog(chatDialog, new QbEntityCallbackImpl<ArrayList<QBUser>>()); | ||
} | ||
|
||
public void onGlobalMessageReceived(String dialogId, QBChatMessage chatMessage){ | ||
if (chatMessage.getBody() != null && chatMessage.isMarkable()) { //for excluding status messages until will be released v.3.1 | ||
if (QbDialogHolder.getInstance().hasDialogWithId(dialogId)) { | ||
QbDialogHolder.getInstance().updateDialog(dialogId, chatMessage); | ||
notifyListenersDialogUpdated(dialogId); | ||
} else { | ||
ChatHelper.getInstance().getDialogById(dialogId, new QbEntityCallbackImpl<QBChatDialog>() { | ||
@Override | ||
public void onSuccess(QBChatDialog chatDialog, Bundle bundle) { | ||
loadUsersFromDialog(chatDialog); | ||
QbDialogHolder.getInstance().addDialog(chatDialog); | ||
notifyListenersNewDialogLoaded(chatDialog); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public void onSystemMessageReceived(QBChatMessage systemMessage){ | ||
if (isMessageCreatingDialog(systemMessage)) { | ||
QBChatDialog chatDialog = buildChatDialogFromSystemMessage(systemMessage); | ||
chatDialog.initForChat(QBChatService.getInstance()); | ||
QbDialogHolder.getInstance().addDialog(chatDialog); | ||
notifyListenersDialogCreated(chatDialog); | ||
} | ||
} | ||
|
||
private void notifyListenersDialogCreated(final QBChatDialog chatDialog){ | ||
new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ | ||
listener.onDialogCreated(chatDialog); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void notifyListenersDialogUpdated(final String dialogId){ | ||
new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ | ||
listener.onDialogUpdated(dialogId); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void notifyListenersNewDialogLoaded(final QBChatDialog chatDialog){ | ||
new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ | ||
listener.onNewDialogLoaded(chatDialog); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void addManagingDialogsCallbackListener(ManagingDialogsCallbacks listener){ | ||
if (listener != null){ | ||
managingDialogsCallbackListener.add(listener); | ||
} | ||
} | ||
|
||
public void removeManagingDialogsCallbackListener(ManagingDialogsCallbacks listener) { | ||
managingDialogsCallbackListener.remove(listener); | ||
} | ||
|
||
public Collection<ManagingDialogsCallbacks> getManagingDialogsCallbackListeners() { | ||
return Collections.unmodifiableCollection(managingDialogsCallbackListener); | ||
} | ||
|
||
public interface ManagingDialogsCallbacks{ | ||
|
||
void onDialogCreated(QBChatDialog chatDialog); | ||
|
||
void onDialogUpdated(String chatDialog); | ||
|
||
void onNewDialogLoaded(QBChatDialog chatDialog); | ||
} | ||
} |
Oops, something went wrong.