Search in sources :

Example 1 with JSONObject

use of im.actor.runtime.json.JSONObject in project actor-platform by actorapp.

the class AbsContent method convertData.

protected static AbsContent convertData(AbsContentContainer container) {
    if (container instanceof ContentLocalContainer) {
        ContentLocalContainer localContainer = (ContentLocalContainer) container;
        AbsLocalContent content = ((ContentLocalContainer) container).getContent();
        if (content instanceof LocalPhoto) {
            return new PhotoContent(localContainer);
        } else if (content instanceof LocalVideo) {
            return new VideoContent(localContainer);
        } else if (content instanceof LocalVoice) {
            return new VoiceContent(localContainer);
        } else if (content instanceof LocalAnimation) {
            return new AnimationContent(localContainer);
        } else if (content instanceof LocalDocument) {
            return new DocumentContent(localContainer);
        } else {
            throw new RuntimeException("Unknown type");
        }
    } else if (container instanceof ContentRemoteContainer) {
        ContentRemoteContainer remoteContainer = (ContentRemoteContainer) container;
        ApiMessage content = ((ContentRemoteContainer) container).getMessage();
        try {
            if (content instanceof ApiDocumentMessage) {
                ApiDocumentMessage d = (ApiDocumentMessage) content;
                if (d.getExt() instanceof ApiDocumentExPhoto) {
                    return new PhotoContent(remoteContainer);
                } else if (d.getExt() instanceof ApiDocumentExVideo) {
                    return new VideoContent(remoteContainer);
                } else if (d.getExt() instanceof ApiDocumentExVoice) {
                    return new VoiceContent(remoteContainer);
                } else if (d.getExt() instanceof ApiDocumentExAnimation) {
                    return new AnimationContent(remoteContainer);
                } else {
                    return new DocumentContent(remoteContainer);
                }
            } else if (content instanceof ApiTextMessage) {
                return new TextContent(remoteContainer);
            } else if (content instanceof ApiServiceMessage) {
                ApiServiceEx ext = ((ApiServiceMessage) content).getExt();
                if (ext instanceof ApiServiceExContactRegistered) {
                    return new ServiceUserRegistered(remoteContainer);
                } else if (ext instanceof ApiServiceExChangedTitle) {
                    return new ServiceGroupTitleChanged(remoteContainer);
                } else if (ext instanceof ApiServiceExChangedTopic) {
                    return new ServiceGroupTopicChanged(remoteContainer);
                } else if (ext instanceof ApiServiceExChangedAbout) {
                    return new ServiceGroupAboutChanged(remoteContainer);
                } else if (ext instanceof ApiServiceExChangedAvatar) {
                    return new ServiceGroupAvatarChanged(remoteContainer);
                } else if (ext instanceof ApiServiceExGroupCreated) {
                    return new ServiceGroupCreated(remoteContainer);
                } else if (ext instanceof ApiServiceExUserInvited) {
                    return new ServiceGroupUserInvited(remoteContainer);
                } else if (ext instanceof ApiServiceExUserKicked) {
                    return new ServiceGroupUserKicked(remoteContainer);
                } else if (ext instanceof ApiServiceExUserLeft) {
                    return new ServiceGroupUserLeave(remoteContainer);
                } else if (ext instanceof ApiServiceExUserJoined) {
                    return new ServiceGroupUserJoined(remoteContainer);
                } else if (ext instanceof ApiServiceExPhoneCall) {
                    return new ServiceCallEnded(remoteContainer);
                } else if (ext instanceof ApiServiceExPhoneMissed) {
                    return new ServiceCallMissed(remoteContainer);
                } else {
                    return new ServiceContent(remoteContainer);
                }
            } else if (content instanceof ApiJsonMessage) {
                ApiJsonMessage json = (ApiJsonMessage) content;
                JSONObject object = new JSONObject(json.getRawJson());
                if (object.getString("dataType").equals("contact")) {
                    return new ContactContent(remoteContainer);
                } else if (object.getString("dataType").equals("location")) {
                    return new LocationContent(remoteContainer);
                } else {
                    return new JsonContent(remoteContainer);
                }
            } else if (content instanceof ApiStickerMessage) {
                return new StickerContent(remoteContainer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Fallback
        return new UnsupportedContent(remoteContainer);
    } else {
        throw new RuntimeException("Unknown type");
    }
}
Also used : ApiTextMessage(im.actor.core.api.ApiTextMessage) ApiStickerMessage(im.actor.core.api.ApiStickerMessage) ApiServiceExUserLeft(im.actor.core.api.ApiServiceExUserLeft) ApiServiceExChangedAvatar(im.actor.core.api.ApiServiceExChangedAvatar) LocalPhoto(im.actor.core.entity.content.internal.LocalPhoto) ApiServiceExChangedTopic(im.actor.core.api.ApiServiceExChangedTopic) ApiServiceExUserJoined(im.actor.core.api.ApiServiceExUserJoined) ApiServiceMessage(im.actor.core.api.ApiServiceMessage) ApiJsonMessage(im.actor.core.api.ApiJsonMessage) ApiServiceExChangedAbout(im.actor.core.api.ApiServiceExChangedAbout) ContentRemoteContainer(im.actor.core.entity.content.internal.ContentRemoteContainer) ApiDocumentExAnimation(im.actor.core.api.ApiDocumentExAnimation) ContentLocalContainer(im.actor.core.entity.content.internal.ContentLocalContainer) ApiDocumentExVideo(im.actor.core.api.ApiDocumentExVideo) LocalVoice(im.actor.core.entity.content.internal.LocalVoice) AbsLocalContent(im.actor.core.entity.content.internal.AbsLocalContent) ApiServiceExUserInvited(im.actor.core.api.ApiServiceExUserInvited) JSONObject(im.actor.runtime.json.JSONObject) ApiMessage(im.actor.core.api.ApiMessage) LocalDocument(im.actor.core.entity.content.internal.LocalDocument) ApiDocumentExVoice(im.actor.core.api.ApiDocumentExVoice) ApiServiceEx(im.actor.core.api.ApiServiceEx) ApiServiceExChangedTitle(im.actor.core.api.ApiServiceExChangedTitle) ApiServiceExPhoneCall(im.actor.core.api.ApiServiceExPhoneCall) ApiServiceExPhoneMissed(im.actor.core.api.ApiServiceExPhoneMissed) LocalAnimation(im.actor.core.entity.content.internal.LocalAnimation) ApiServiceExGroupCreated(im.actor.core.api.ApiServiceExGroupCreated) IOException(java.io.IOException) LocalVideo(im.actor.core.entity.content.internal.LocalVideo) ApiDocumentMessage(im.actor.core.api.ApiDocumentMessage) ApiDocumentExPhoto(im.actor.core.api.ApiDocumentExPhoto) ApiServiceExUserKicked(im.actor.core.api.ApiServiceExUserKicked) ApiServiceExContactRegistered(im.actor.core.api.ApiServiceExContactRegistered)

Example 2 with JSONObject

use of im.actor.runtime.json.JSONObject in project actor-platform by actorapp.

the class ContactContent method create.

@NotNull
public static ContactContent create(@NotNull String name, @NotNull ArrayList<String> phones, @NotNull ArrayList<String> emails, @Nullable String base64photo) {
    try {
        JSONObject obj = new JSONObject();
        obj.put("dataType", "contact");
        JSONObject contact = new JSONObject();
        contact.put("name", name);
        if (base64photo != null) {
            contact.put("photo", base64photo);
        }
        JSONArray phoneArray = new JSONArray();
        for (String phone : phones) {
            phoneArray.put(phone);
        }
        JSONArray emailArray = new JSONArray();
        for (String phone : emails) {
            emailArray.put(phone);
        }
        contact.put("emails", emailArray);
        contact.put("phones", phoneArray);
        JSONObject data = new JSONObject();
        data.put("contact", contact);
        obj.put("data", data);
        return new ContactContent(new ContentRemoteContainer(new ApiJsonMessage(obj.toString())));
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(im.actor.runtime.json.JSONObject) JSONArray(im.actor.runtime.json.JSONArray) ContentRemoteContainer(im.actor.core.entity.content.internal.ContentRemoteContainer) JSONException(im.actor.runtime.json.JSONException) ApiJsonMessage(im.actor.core.api.ApiJsonMessage) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JSONObject

use of im.actor.runtime.json.JSONObject in project actor-platform by actorapp.

the class InviteHandler method handleIntent.

public static void handleIntent(BaseActivity activity, Intent intent) {
    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW) && intent.getData() != null) {
        String joinGroupUrl = intent.getData().toString();
        if (joinGroupUrl != null && (joinGroupUrl.contains("join") || joinGroupUrl.contains("token"))) {
            String[] urlSplit = null;
            if (joinGroupUrl.contains("join")) {
                urlSplit = joinGroupUrl.split("/join/");
            } else if (joinGroupUrl.contains("token")) {
                urlSplit = joinGroupUrl.split("token=");
            }
            if (urlSplit != null) {
                joinGroupUrl = urlSplit[urlSplit.length - 1];
                final String token = joinGroupUrl;
                HTTP.getMethod(ActorSDK.sharedActor().getInviteDataUrl() + joinGroupUrl, 0, 0, 0).then(new Consumer<HTTPResponse>() {

                    @Override
                    public void apply(HTTPResponse httpResponse) {
                        try {
                            JSONObject data = new JSONObject(new String(httpResponse.getContent(), "UTF-8"));
                            JSONObject group = data.getJSONObject("group");
                            String title = group.getString("title");
                            if (group.has("id") && group.has("isPublic")) {
                                int gid = group.getInt("id");
                                boolean isPublic = group.getBoolean("isPublic");
                                // Check if we have this group
                                try {
                                    GroupVM groupVM = groups().get(gid);
                                    if (groupVM.isMember().get() || isPublic) {
                                        // Have this group, is member or group is public, just open it
                                        activity.startActivity(Intents.openDialog(Peer.group(gid), false, activity));
                                    } else {
                                        // Have this group, but not member, join it
                                        joinViaToken(token, title, activity);
                                    }
                                } catch (Exception e) {
                                    // Do not have this group, join it
                                    if (isPublic) {
                                        messenger().findPublicGroupById(gid).then(peer -> activity.startActivity(Intents.openDialog(peer, false, activity)));
                                    } else {
                                        joinViaToken(token, title, activity);
                                    }
                                }
                            } else {
                                joinViaToken(token, title, activity);
                            }
                        } catch (JSONException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}
Also used : GroupVM(im.actor.core.viewmodel.GroupVM) JSONObject(im.actor.runtime.json.JSONObject) HTTPResponse(im.actor.runtime.http.HTTPResponse) JSONException(im.actor.runtime.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with JSONObject

use of im.actor.runtime.json.JSONObject in project actor-platform by actorapp.

the class SDKPushReceiver method onPushReceived.

@Override
public void onPushReceived(String payload) {
    try {
        JSONObject object = new JSONObject(payload);
        if (object.has("data")) {
            JSONObject data = object.getJSONObject("data");
            ActorSDK.sharedActor().waitForReady();
            if (data.has("seq")) {
                int seq = data.getInt("seq");
                int authId = data.optInt("authId");
                Log.d("SDKPushReceiver", "Seq Received: " + seq);
                ActorSDK.sharedActor().getMessenger().onPushReceived(seq, authId);
            } else if (data.has("callId")) {
                Long callId = Long.parseLong(data.getString("callId"));
                int attempt = 0;
                if (data.has("attemptIndex")) {
                    attempt = data.getInt("attemptIndex");
                }
                Log.d("SDKPushReceiver", "Received Call #" + callId + " (" + attempt + ")");
                ActorSDK.sharedActor().getMessenger().checkCall(callId, attempt);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(im.actor.runtime.json.JSONObject)

Example 5 with JSONObject

use of im.actor.runtime.json.JSONObject in project actor-platform by actorapp.

the class JsonContent method getContentDescription.

public String getContentDescription() {
    String res;
    try {
        JSONObject data = new JSONObject(getRawJson());
        res = data.getJSONObject("data").getString("text");
    } catch (JSONException e) {
        res = "";
    }
    return res;
}
Also used : JSONObject(im.actor.runtime.json.JSONObject) JSONException(im.actor.runtime.json.JSONException)

Aggregations

JSONObject (im.actor.runtime.json.JSONObject)6 JSONException (im.actor.runtime.json.JSONException)4 ApiJsonMessage (im.actor.core.api.ApiJsonMessage)3 ContentRemoteContainer (im.actor.core.entity.content.internal.ContentRemoteContainer)3 NotNull (org.jetbrains.annotations.NotNull)2 ApiDocumentExAnimation (im.actor.core.api.ApiDocumentExAnimation)1 ApiDocumentExPhoto (im.actor.core.api.ApiDocumentExPhoto)1 ApiDocumentExVideo (im.actor.core.api.ApiDocumentExVideo)1 ApiDocumentExVoice (im.actor.core.api.ApiDocumentExVoice)1 ApiDocumentMessage (im.actor.core.api.ApiDocumentMessage)1 ApiMessage (im.actor.core.api.ApiMessage)1 ApiServiceEx (im.actor.core.api.ApiServiceEx)1 ApiServiceExChangedAbout (im.actor.core.api.ApiServiceExChangedAbout)1 ApiServiceExChangedAvatar (im.actor.core.api.ApiServiceExChangedAvatar)1 ApiServiceExChangedTitle (im.actor.core.api.ApiServiceExChangedTitle)1 ApiServiceExChangedTopic (im.actor.core.api.ApiServiceExChangedTopic)1 ApiServiceExContactRegistered (im.actor.core.api.ApiServiceExContactRegistered)1 ApiServiceExGroupCreated (im.actor.core.api.ApiServiceExGroupCreated)1 ApiServiceExPhoneCall (im.actor.core.api.ApiServiceExPhoneCall)1 ApiServiceExPhoneMissed (im.actor.core.api.ApiServiceExPhoneMissed)1