Search in sources :

Example 1 with JSONException

use of im.actor.runtime.json.JSONException 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 2 with JSONException

use of im.actor.runtime.json.JSONException 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 3 with JSONException

use of im.actor.runtime.json.JSONException 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

JSONException (im.actor.runtime.json.JSONException)3 JSONObject (im.actor.runtime.json.JSONObject)3 ApiJsonMessage (im.actor.core.api.ApiJsonMessage)1 ContentRemoteContainer (im.actor.core.entity.content.internal.ContentRemoteContainer)1 GroupVM (im.actor.core.viewmodel.GroupVM)1 HTTPResponse (im.actor.runtime.http.HTTPResponse)1 JSONArray (im.actor.runtime.json.JSONArray)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NotNull (org.jetbrains.annotations.NotNull)1