src/com/beem/project/beem/service/Contact.java
changeset 224 d8e2cb1eb895
parent 212 bbc0b169cdf0
parent 223 bb656974bab1
child 267 ab5493f08c57
child 269 d78115a6b45b
equal deleted inserted replaced
215:5db64229be69 224:d8e2cb1eb895
     7 import java.util.Collection;
     7 import java.util.Collection;
     8 import java.util.List;
     8 import java.util.List;
     9 
     9 
    10 import org.jivesoftware.smack.RosterGroup;
    10 import org.jivesoftware.smack.RosterGroup;
    11 import org.jivesoftware.smack.packet.Presence;
    11 import org.jivesoftware.smack.packet.Presence;
    12 
    12 import org.jivesoftware.smack.util.StringUtils;
       
    13 
       
    14 import android.net.Uri;
    13 import android.os.Parcel;
    15 import android.os.Parcel;
    14 import android.os.Parcelable;
    16 import android.os.Parcelable;
    15 
    17 
    16 import com.beem.project.beem.utils.Status;
    18 import com.beem.project.beem.utils.Status;
    17 
    19 
    20  * 
    22  * 
    21  * @author darisk
    23  * @author darisk
    22  */
    24  */
    23 public class Contact implements Parcelable {
    25 public class Contact implements Parcelable {
    24 
    26 
    25     @SuppressWarnings("unused")
       
    26     private static final String                     TAG     = "Contact";
       
    27 
       
    28     private int                                     mID;
       
    29     private int                                     mStatus;
       
    30     private String                                  mJID;
       
    31     private String                                  mName;
       
    32     private String                                  mMsgState;
       
    33     private List<String>                            mRes;
       
    34     private List<String>                            mGroups;
       
    35 
       
    36     /**
    27     /**
    37      * Parcelable.Creator needs by Android.
    28      * Parcelable.Creator needs by Android.
    38      */
    29      */
    39     public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {
    30     public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() {
    40 
    31 
    47 	                                                        public Contact[] newArray(int size) {
    38 	                                                        public Contact[] newArray(int size) {
    48 		                                                    return new Contact[size];
    39 		                                                    return new Contact[size];
    49 	                                                        }
    40 	                                                        }
    50 	                                                    };
    41 	                                                    };
    51 
    42 
       
    43     private int                                     mID;
       
    44     private int                                     mStatus;
       
    45     private String                                  mJID;
       
    46     private String                                  mMsgState;
       
    47     private List<String>                            mRes;
       
    48     private List<String>                            mGroups;
       
    49     private String                                  mName;
       
    50 
    52     /**
    51     /**
    53      * Constructor.
    52      * Constructor.
    54      */
    53      */
    55     public Contact() {
    54     public Contact() {
    56 	// TODO Auto-generated constructor stub
       
    57     }
    55     }
    58 
    56 
    59     /**
    57     /**
    60      * Construct a contact from a parcel.
    58      * Construct a contact from a parcel.
    61      * 
    59      * 
    87 	mRes = new ArrayList<String>();
    85 	mRes = new ArrayList<String>();
    88 	mRes.add("none");
    86 	mRes.add("none");
    89 	mGroups = new ArrayList<String>();
    87 	mGroups = new ArrayList<String>();
    90     }
    88     }
    91 
    89 
       
    90     /**
       
    91      * Create a contact from a Uri.
       
    92      * 
       
    93      * @param uri
       
    94      *            an uri for the contact
       
    95      * @throws IllegalArgumentException
       
    96      *             if it is not a xmpp uri
       
    97      */
       
    98     public Contact(Uri uri) {
       
    99 	if (!uri.getScheme().equals("xmpp"))
       
   100 	    throw new IllegalArgumentException();
       
   101 	mJID = uri.getSchemeSpecificPart();
       
   102     }
       
   103 
       
   104     /**
       
   105      * Add a group for the contact.
       
   106      * 
       
   107      * @param group
       
   108      *            the group
       
   109      */
    92     public void addGroup(String group) {
   110     public void addGroup(String group) {
    93 	mGroups.add(group);
   111 	mGroups.add(group);
    94     }
   112     }
    95 
   113 
    96     /**
   114     /**
       
   115      * Add a resource for this contact.
       
   116      * 
    97      * @param res
   117      * @param res
       
   118      *            the resource to add
    98      */
   119      */
    99     public void addRes(String res) {
   120     public void addRes(String res) {
   100 	if (!mRes.contains(res))
   121 	if (!mRes.contains(res))
   101 	    mRes.add(res);
   122 	    mRes.add(res);
   102     }
   123     }
   103 
   124 
   104     /**
   125     /**
       
   126      * Delete a resource for this contact.
       
   127      * 
   105      * @param res
   128      * @param res
       
   129      *            the resource de delete
   106      */
   130      */
   107     public void delRes(String res) {
   131     public void delRes(String res) {
   108 	mRes.remove(res);
   132 	mRes.remove(res);
   109     }
   133     }
   110 
   134 
   116 	// TODO Auto-generated method stub
   140 	// TODO Auto-generated method stub
   117 	return 0;
   141 	return 0;
   118     }
   142     }
   119 
   143 
   120     /**
   144     /**
       
   145      * Get the groups the contact is in.
       
   146      * 
   121      * @return the mGroups
   147      * @return the mGroups
   122      */
   148      */
   123     public List<String> getGroups() {
   149     public List<String> getGroups() {
   124 	return mGroups;
   150 	return mGroups;
   125     }
   151     }
   141     public String getJID() {
   167     public String getJID() {
   142 	return mJID;
   168 	return mJID;
   143     }
   169     }
   144 
   170 
   145     /**
   171     /**
       
   172      * Get the list of resource for the contact.
       
   173      * 
   146      * @return the mRes
   174      * @return the mRes
   147      */
   175      */
   148     public List<String> getMRes() {
   176     public List<String> getMRes() {
   149 	return mRes;
   177 	return mRes;
   150     }
   178     }
   172      */
   200      */
   173     public int getStatus() {
   201     public int getStatus() {
   174 	return mStatus;
   202 	return mStatus;
   175     }
   203     }
   176 
   204 
       
   205     /**
       
   206      * Set the groups the contact is in.
       
   207      * 
       
   208      * @param groups
       
   209      *            list of groups
       
   210      */
   177     public void setGroups(Collection<RosterGroup> groups) {
   211     public void setGroups(Collection<RosterGroup> groups) {
       
   212 	this.mGroups.clear();
   178 	for (RosterGroup rosterGroup : groups) {
   213 	for (RosterGroup rosterGroup : groups) {
   179 	    mGroups.add(rosterGroup.getName());
   214 	    mGroups.add(rosterGroup.getName());
   180 	}
   215 	}
   181     }
   216     }
   182 
   217 
   183     /**
   218     /**
       
   219      * Set the groups the contact is in.
       
   220      * 
   184      * @param mGroups
   221      * @param mGroups
   185      *            the mGroups to set
   222      *            the mGroups to set
   186      */
   223      */
   187     public void setGroups(List<String> mGroups) {
   224     public void setGroups(List<String> mGroups) {
   188 	this.mGroups = mGroups;
   225 	this.mGroups = mGroups;
   199     }
   236     }
   200 
   237 
   201     /**
   238     /**
   202      * Set the Jabber ID of the contact.
   239      * Set the Jabber ID of the contact.
   203      * 
   240      * 
   204      * @param mjid
   241      * @param jid
   205      *            the jabber ID to set
   242      *            the jabber ID to set
   206      */
   243      */
   207     public void setJID(String mjid) {
   244     public void setJID(String jid) {
   208 	mJID = mjid;
   245 	mJID = jid;
   209     }
   246     }
   210 
   247 
   211     /**
   248     /**
       
   249      * Set a list of resource for the contact.
       
   250      * 
   212      * @param mRes
   251      * @param mRes
   213      *            the mRes to set
   252      *            the mRes to set
   214      */
   253      */
   215     public void setMRes(List<String> mRes) {
   254     public void setMRes(List<String> mRes) {
   216 	this.mRes = mRes;
   255 	this.mRes = mRes;
   254     public void setStatus(Presence presence) {
   293     public void setStatus(Presence presence) {
   255 	mStatus = Status.getStatusFromPresence(presence);
   294 	mStatus = Status.getStatusFromPresence(presence);
   256 	mMsgState = presence.getStatus();
   295 	mMsgState = presence.getStatus();
   257     }
   296     }
   258 
   297 
       
   298     /**
       
   299      * Set status for the contact.
       
   300      * 
       
   301      * @param presence
       
   302      *            The presence packet which contains the status
       
   303      */
   259     public void setStatus(PresenceAdapter presence) {
   304     public void setStatus(PresenceAdapter presence) {
   260 	mStatus = presence.getStatus();
   305 	mStatus = presence.getStatus();
   261 	mMsgState = presence.getStatusText();
   306 	mMsgState = presence.getStatusText();
   262 
   307 
   263     }
   308     }
   264 
   309 
       
   310     /**
       
   311      * {@inheritDoc}
       
   312      */
   265     @Override
   313     @Override
   266     public String toString() {
   314     public String toString() {
   267 	if (mJID != null)
   315 	if (mJID != null)
   268 	    return mJID;
   316 	    return mJID;
   269 	return super.toString();
   317 	return super.toString();
       
   318     }
       
   319 
       
   320     /**
       
   321      * Get a URI to access the contact.
       
   322      * 
       
   323      * @return the URI
       
   324      */
       
   325     public Uri toUri() {
       
   326 	StringBuilder build = new StringBuilder("xmpp:");
       
   327 	build.append(StringUtils.parseName(mJID)).append('@').append(StringUtils.parseServer(mJID));
       
   328 	Uri u = Uri.parse(build.toString());
       
   329 	return u;
   270     }
   330     }
   271 
   331 
   272     /**
   332     /**
   273      * {@inheritDoc}
   333      * {@inheritDoc}
   274      */
   334      */
   280 	dest.writeString(mName);
   340 	dest.writeString(mName);
   281 	dest.writeString(mMsgState);
   341 	dest.writeString(mMsgState);
   282 	dest.writeStringList(getMRes());
   342 	dest.writeStringList(getMRes());
   283 	dest.writeStringList(getGroups());
   343 	dest.writeStringList(getGroups());
   284     }
   344     }
       
   345 
   285 }
   346 }