Android Open Source - Android-VKontakte-SDK Notification






From Project

Back to project page Android-VKontakte-SDK.

License

The source code is released under:

MIT License

If you think the Android project Android-VKontakte-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.perm.kate.api;
//from   w w  w  . jav  a2  s  . c o  m
import java.io.Serializable;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Notification implements Serializable {
    
    private static final long serialVersionUID = 1L;
    public static final String FOLLOW = "follow";
    public static final String FRIEND_ACCEPTED = "friend_accepted";
    public static final String MENTION = "mention";
    public static final String MENTION_COMMENTS = "mention_comments";
    public static final String WALL = "wall";
    public static final String COMMENT_POST = "comment_post";
    public static final String COMMENT_PHOTO = "comment_photo";
    public static final String COMMENT_VIDEO = "comment_video";
    public static final String REPLY_COMMENT = "reply_comment";//wall
    public static final String REPLY_COMMENT_PHOTO = "reply_comment_photo";
    public static final String REPLY_COMMENT_VIDEO = "reply_comment_video";
    public static final String REPLY_TOPIC = "reply_topic";
    public static final String LIKE_POST = "like_post";
    public static final String LIKE_COMMENT = "like_comment";
    public static final String LIKE_COMMENT_PHOTO = "like_comment_photo";
    public static final String LIKE_COMMENT_VIDEO = "like_comment_video";
    public static final String LIKE_COMMENT_TOPIC = "like_comment_topic";
    public static final String LIKE_PHOTO = "like_photo";
    public static final String LIKE_VIDEO = "like_video";
    public static final String COPY_POST = "copy_post";
    public static final String COPY_PHOTO = "copy_photo";
    public static final String COPY_VIDEO = "copy_video";
    
    public String type;
    public Long date;
    public Object parent;
    public Object feedback;
    public Object reply;
    public Photo photo;//for type reply_comment_photo
    public Video video;//for type reply_comment_video
    
    public static Notification parse(JSONObject o) {
        Notification n = null;
        try {
            n = new Notification();
            n.type = o.getString("type");
            n.date = o.optLong("date");
            if (n.type.equals(FOLLOW)) {
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                n.parent = null;//empty
                if (jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(FRIEND_ACCEPTED)) {
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                n.parent = null;//empty
                if (jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(MENTION)) {
                JSONObject jfeedback = o.optJSONObject("feedback");//post
                n.parent = null;//empty
                if (jfeedback != null)
                    n.feedback = WallMessage.parseForNotifications(jfeedback);
            } else if (n.type.equals(MENTION_COMMENTS)) {
                JSONObject jparent = o.optJSONObject("parent"); //post
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = WallMessage.parse(jparent);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(WALL)) {
                JSONObject jfeedback = o.optJSONObject("feedback");//post
                if (jfeedback != null)
                    n.feedback = WallMessage.parseForNotifications(jfeedback);
            } else if (n.type.equals(COMMENT_POST)) {
                JSONObject jparent = o.optJSONObject("parent"); //post
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = WallMessage.parse(jparent);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(COMMENT_PHOTO)) {
                JSONObject jparent = o.optJSONObject("parent"); //photo
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = Photo.parse(jparent);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(COMMENT_VIDEO)) {
                JSONObject jparent = o.optJSONObject("parent"); //video
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = Video.parse(jparent);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(REPLY_COMMENT)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = Comment.parseNotificationComment(jparent, true);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(REPLY_COMMENT_PHOTO)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null) {
                    n.parent = Comment.parseNotificationComment(jparent, false);
                    if(jparent.has("photo"))
                        n.photo=Photo.parse(jparent.optJSONObject("photo"));
                }
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(REPLY_COMMENT_VIDEO)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null) {
                    n.parent = Comment.parseNotificationComment(jparent, false);
                    if(jparent.has("video"))
                        n.video=Video.parse(jparent.optJSONObject("video"));
                }
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(REPLY_TOPIC)) {
                JSONObject jparent = o.optJSONObject("parent"); //topic
                JSONObject jfeedback = o.optJSONObject("feedback");//comment
                if (jparent != null)
                    n.parent = GroupTopic.parseForNotifications(jparent);
                if(jfeedback != null)
                    n.feedback = Comment.parseNotificationComment(jfeedback, false);
            } else if (n.type.equals(LIKE_POST)) {
                JSONObject jparent = o.optJSONObject("parent"); //post
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null)
                    n.parent = WallMessage.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_COMMENT)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null)
                    n.parent = Comment.parseNotificationComment(jparent, true);
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_COMMENT_PHOTO)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null) {
                    n.parent = Comment.parseNotificationComment(jparent, false);
                    if(jparent.has("photo"))
                        n.photo=Photo.parse(jparent.optJSONObject("photo"));
                }
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_COMMENT_VIDEO)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null) {
                    n.parent = Comment.parseNotificationComment(jparent, false);
                    if(jparent.has("video"))
                        n.video=Video.parse(jparent.optJSONObject("video"));
                }
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_COMMENT_TOPIC)) {
                JSONObject jparent = o.optJSONObject("parent"); //comment
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null) {
                    n.parent = Comment.parseNotificationComment(jparent, false);
                    //TODO
                    //if(jparent.has("topic"))
                    //    n.xxx=Xxx.parse(jparent.optJSONObject("topic"));
                }
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_PHOTO)) {
                JSONObject jparent = o.optJSONObject("parent"); //photo
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null)
                    n.parent = Photo.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(LIKE_VIDEO)) {
                JSONObject jparent = o.optJSONObject("parent"); //video
                JSONObject jfeedback = o.optJSONObject("feedback");//profiles
                if (jparent != null)
                    n.parent = Video.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getProfiles(jfeedback);
            } else if (n.type.equals(COPY_POST)) {
                JSONObject jparent = o.optJSONObject("parent"); //wall
                JSONObject jfeedback = o.optJSONObject("feedback");//copy
                if (jparent != null)
                    n.parent = WallMessage.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getCopies(jfeedback);
            } else if (n.type.equals(COPY_PHOTO)) {
                JSONObject jparent = o.optJSONObject("parent"); //photo
                JSONObject jfeedback = o.optJSONObject("feedback");//copy
                if (jparent != null)
                    n.parent = Photo.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getCopies(jfeedback);
            } else if (n.type.equals(COPY_VIDEO)) {
                JSONObject jparent = o.optJSONObject("parent"); //video
                JSONObject jfeedback = o.optJSONObject("feedback");//copy
                if (jparent != null)
                    n.parent = Video.parse(jparent);
                if(jfeedback != null)
                    n.feedback = getCopies(jfeedback);
            }        
            JSONObject jreply = o.optJSONObject("reply");
            if (jreply != null)
                n.reply = Reply.parse(jreply);
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }
        return n;
    }

    public static ArrayList<Notification> parseNotifications(JSONArray jnotifications) throws JSONException {
        ArrayList<Notification> notifications = new ArrayList<Notification>();
        for(int i = 0; i < jnotifications.length(); i++) {
            if(!(jnotifications.get(i) instanceof JSONObject))
                continue;
            JSONObject jgroup = (JSONObject)jnotifications.get(i);
            Notification n = Notification.parse(jgroup);
            if (n != null)
                notifications.add(n);
        }
        return notifications;
    }
    
    public static ArrayList<Object> getProfiles(JSONObject jfeedback) throws JSONException {
        ArrayList<Object> ids = new ArrayList<Object>();
        JSONArray items=jfeedback.optJSONArray("items");
        if(items==null)
            return ids;
        for (int i = 0; i < items.length(); i++) {
            JSONObject j_id = items.optJSONObject(i);
            if(j_id!=null)
                ids.add(j_id.optLong("from_id"));
        }
        return ids;
    }
    
    public static ArrayList<Object> getCopies(JSONObject jfeedback) throws JSONException {
        ArrayList<Object> ids = new ArrayList<Object>();
        JSONArray items=jfeedback.optJSONArray("items");
        if(items==null)
            return ids;
        for (int i = 0; i < items.length(); i++) {
            JSONObject j_id = items.optJSONObject(i);
            if(j_id==null)
                continue;
            Long id = j_id.optLong("id");
            Long owner_id = j_id.optLong("from_id");
            if (id != null && owner_id != null) {
                IdsPair c = new IdsPair();
                c.id = id;
                c.owner_id = owner_id;
                ids.add(c);
            }
        }
        return ids;
    }
    
}




Java Source Code List

com.perm.kate.api.Album.java
com.perm.kate.api.Api.java
com.perm.kate.api.Attachment.java
com.perm.kate.api.AudioAlbum.java
com.perm.kate.api.Audio.java
com.perm.kate.api.Auth.java
com.perm.kate.api.BanInfo.java
com.perm.kate.api.BannArg.java
com.perm.kate.api.City.java
com.perm.kate.api.CommentList.java
com.perm.kate.api.Comment.java
com.perm.kate.api.Constants.java
com.perm.kate.api.Contact.java
com.perm.kate.api.Counters.java
com.perm.kate.api.Country.java
com.perm.kate.api.Document.java
com.perm.kate.api.FriendsList.java
com.perm.kate.api.Geo.java
com.perm.kate.api.Gift.java
com.perm.kate.api.Graffiti.java
com.perm.kate.api.GroupBanItem.java
com.perm.kate.api.GroupTopic.java
com.perm.kate.api.Group.java
com.perm.kate.api.IdsPair.java
com.perm.kate.api.KException.java
com.perm.kate.api.LastActivity.java
com.perm.kate.api.Link.java
com.perm.kate.api.Media.java
com.perm.kate.api.Message.java
com.perm.kate.api.NameCases.java
com.perm.kate.api.NewsItem.java
com.perm.kate.api.NewsJTags.java
com.perm.kate.api.NewsTypes.java
com.perm.kate.api.Newsfeed.java
com.perm.kate.api.Note.java
com.perm.kate.api.Notification.java
com.perm.kate.api.Notifications.java
com.perm.kate.api.Page.java
com.perm.kate.api.Params.java
com.perm.kate.api.PhotoComment.java
com.perm.kate.api.PhotoTag.java
com.perm.kate.api.Photo.java
com.perm.kate.api.Place.java
com.perm.kate.api.Reply.java
com.perm.kate.api.SearchDialogItem.java
com.perm.kate.api.User.java
com.perm.kate.api.Video.java
com.perm.kate.api.VkApp.java
com.perm.kate.api.VkPollAnswer.java
com.perm.kate.api.VkPoll.java
com.perm.kate.api.VkStatus.java
com.perm.kate.api.WallMessage.java
com.perm.kate.api.sample.Account.java
com.perm.kate.api.sample.Constants.java
com.perm.kate.api.sample.LoginActivity.java
com.perm.kate.api.sample.MainActivity.java
com.perm.utils.Utils.java
com.perm.utils.WrongResponseCodeException.java