private IContactList getContactList(IImConnection conn) {
    if (conn == null) {
      return null;
    }

    try {
      IContactListManager contactListMgr = conn.getContactListManager();
      String listName = getSelectedListName();
      if (!TextUtils.isEmpty(listName)) {
        return contactListMgr.getContactList(listName);
      } else {
        // Use the default list
        List<IBinder> lists = contactListMgr.getContactLists();
        for (IBinder binder : lists) {
          IContactList list = IContactList.Stub.asInterface(binder);
          if (list.isDefault()) {
            return list;
          }
        }
        // No default list, use the first one as default list
        if (!lists.isEmpty()) {
          return IContactList.Stub.asInterface(lists.get(0));
        }
        return null;
      }
    } catch (RemoteException e) {
      // If the service has died, there is no list for now.
      return null;
    }
  }
 void inviteBuddies() {
   Rfc822Token[] recipients = Rfc822Tokenizer.tokenize(mAddressList.getText());
   try {
     IImConnection conn = mApp.getConnection(mProviderId);
     IContactList list = getContactList(conn);
     if (list == null) {
       Log.e(
           ImApp.LOG_TAG,
           "<AddContactActivity> can't find given contact list:" + getSelectedListName());
       finish();
     } else {
       boolean fail = false;
       for (Rfc822Token recipient : recipients) {
         String username = recipient.getAddress();
         if (mDefaultDomain != null && username.indexOf('@') == -1) {
           username = username + "@" + mDefaultDomain;
         }
         if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) {
           log("addContact:" + username);
         }
         int res = list.addContact(username);
         if (res != ImErrorInfo.NO_ERROR) {
           fail = true;
           mHandler.showAlert(
               R.string.error, ErrorResUtils.getErrorRes(getResources(), res, username));
         }
       }
       // close the screen if there's no error.
       if (!fail) {
         finish();
       }
     }
   } catch (RemoteException ex) {
     Log.e(ImApp.LOG_TAG, "<AddContactActivity> inviteBuddies: caught " + ex);
   }
 }