Пример #1
0
  @Override
  public void sendMessage(final FriendMessage message) {

    HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
    Friend friend = friends.get(message.getTo());
    Screenname buddy = new Screenname(friend.getUserName());
    Conversation c = (Conversation) friend.getUserInfo();
    if (c == null) {
      c = connection.getIcbmService().getImConversation(buddy);
      friend.setUserInfo(c);
    }
    Message oscarMessage =
        new Message() {

          public boolean isAutoResponse() {
            return false;
          }

          public String getMessageBody() {
            return message.getMessage();
          }
        };
    c.sendMessage(oscarMessage);
    Friend recipient = friends.get(message.getTo());
    accountListener.didReceiveMessageForFriend(message, recipient);
  }
Пример #2
0
  @Override
  public boolean connect(AccountLoginListener loginListener) {
    Screenname name = new Screenname(this.getUserName());
    AimConnectionProperties props = new AimConnectionProperties(name, this.getPassword());
    try {
      File dir = Util.getInstance().activity.getDir("aimconfig", Context.MODE_PRIVATE);
      DAppSession sess = new DAppSession(new File(dir.getAbsolutePath(), ".dolca"));
      sess.setSavePrefsOnExit(true);
      aimSession = (DAimAppSession) sess.openAimSession(name);
      connection = aimSession.openConnection(props);

      connection.addStateListener(connStateListener);
      //			connection.addOpenedServiceListener(new OpenedServiceListener() {
      //
      //				public void openedServices(AimConnection conn,
      //						Collection<? extends Service> services) {
      //					// TODO Auto-generated method stub
      //
      //				}
      //
      //				public void closedServices(AimConnection conn,
      //						Collection<? extends Service> services) {
      //					// TODO Auto-generated method stub
      //
      //				}
      //			})

      connection.connect();
      this.loginListener = loginListener;

    } catch (Exception e) {
      String errorMessage = e.getLocalizedMessage();
      loginListener.loginDidFailedWithError(errorMessage != null ? errorMessage : e.toString());
      return false;
    }
    return true;
  }
Пример #3
0
    public void handleStateChange(StateEvent event) {
      AimConnection conn = event.getAimConnection();
      if (aimSession == null || conn != aimSession.getConnection()) {
        return;
      }

      State state = event.getNewState();

      if (state == State.FAILED) {
        if (AIMAccount.this.loginListener != null) {
          AIMAccount.this.loginListener.loginDidFailedWithError(
              "Failed to connect to '" + AIMAccount.this.getUserName() + "' account");
          AIMAccount.this.loginListener = null;
        }
      }

      if (state == State.ONLINE) {
        if (AIMAccount.this.loginListener != null) {
          AIMAccount.this.loginListener.loginDidSucceeded();
          AIMAccount.this.loginListener = null;
        }

        isOnline = true;
        IcbmService icbmservice = conn.getIcbmService();
        icbmservice.addIcbmListener(
            new IcbmListener() {

              public void buddyInfoUpdated(
                  IcbmService service, Screenname buddy, IcbmBuddyInfo info) {}

              public void newConversation(IcbmService service, Conversation conv) {
                conv.addConversationListener(
                    new ConversationListener() {

                      public void sentOtherEvent(
                          Conversation conversation, ConversationEventInfo event) {
                        // TODO Auto-generated method stub

                      }

                      public void sentMessage(Conversation c, MessageInfo minfo) {
                        // TODO Auto-generated method stub

                      }

                      public void gotOtherEvent(
                          Conversation conversation, ConversationEventInfo event) {
                        // TODO Auto-generated method stub

                      }

                      public void gotMessage(Conversation c, MessageInfo minfo) {
                        Friend friend = getFriendFromBuddy(minfo.getFrom(), false);
                        HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                        Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                        existingFriend.setUserInfo(c);
                        Spanned messageText = Html.fromHtml(minfo.getMessage().getMessageBody());
                        FriendMessage friendMessage =
                            new FriendMessage(
                                existingFriend.getUniqueDescriptior(),
                                AIMAccount.this.getUserName(),
                                messageText.toString(),
                                false);
                        accountListener.didReceiveMessageForFriend(friendMessage, existingFriend);
                      }

                      public void conversationOpened(Conversation c) {
                        // TODO Auto-generated method stub

                      }

                      public void conversationClosed(Conversation c) {
                        Friend friend = getFriendFromBuddy(c.getBuddy(), false);
                        HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                        Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                        existingFriend.setUserInfo(null);
                      }

                      public void canSendMessageChanged(Conversation c, boolean canSend) {
                        // TODO Auto-generated method stub

                      }
                    });
              }

              public void sendAutomaticallyFailed(
                  IcbmService service, Message message, Set<Conversation> triedConversations) {
                // TODO Auto-generated method stub

              }
            });

        connection
            .getBuddyInfoManager()
            .addGlobalBuddyInfoListener(
                new GlobalBuddyInfoListener() {

                  @Override
                  public void receivedStatusUpdate(
                      BuddyInfoManager manager, Screenname buddy, BuddyInfo info) {}

                  @Override
                  public void newBuddyInfo(
                      BuddyInfoManager manager, Screenname buddy, BuddyInfo info) {
                    Friend friend = getFriendFromBuddy(buddy, false);
                    HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                    Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                    if (existingFriend != null) {
                      ByteBlock iconData = info.getIconData();
                      if (iconData != null) {
                        Bitmap bitmap =
                            BitmapFactory.decodeByteArray(
                                iconData.toByteArray(), 0, iconData.getLength());
                        if (bitmap != null) {
                          existingFriend.setAvatar(bitmap);
                        }
                      }
                      applyInfoToFriendFromBuddyInfo(existingFriend, info);
                      accountListener.friendStatusDidChange(existingFriend);
                    }
                  }

                  @Override
                  public void buddyInfoChanged(
                      BuddyInfoManager manager,
                      Screenname buddy,
                      BuddyInfo info,
                      PropertyChangeEvent event) {
                    Friend friend = getFriendFromBuddy(buddy, false);
                    HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                    Friend existingFriend = friends.get(friend.getUniqueDescriptior());

                    if (existingFriend != null) {
                      ByteBlock iconData = info.getIconData();
                      if (iconData != null) {
                        Bitmap bitmap =
                            BitmapFactory.decodeByteArray(
                                iconData.toByteArray(), 0, iconData.getLength());
                        if (bitmap != null) {
                          existingFriend.setAvatar(bitmap);
                        }
                      }
                      applyInfoToFriendFromBuddyInfo(existingFriend, info);
                      accountListener.friendStatusDidChange(existingFriend);
                    }
                  }
                });

        connection
            .getInfoService()
            .addInfoListener(
                new InfoServiceListener() {

                  @Override
                  public void handleUserProfile(
                      InfoService service, Screenname buddy, String userInfo) {
                    Friend friend = getFriendFromBuddy(buddy, false);
                    HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                    Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                    existingFriend.setName(userInfo);
                  }

                  @Override
                  public void handleInvalidCertificates(
                      InfoService service,
                      Screenname buddy,
                      CertificateInfo origCertInfo,
                      Throwable ex) {}

                  @Override
                  public void handleDirectoryInfo(
                      InfoService service, Screenname buddy, DirInfo dirInfo) {
                    Friend friend = getFriendFromBuddy(buddy, false);
                    HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                    Friend existingFriend = friends.get(friend.getUniqueDescriptior());

                    if (dirInfo != null) {
                      String firstName = dirInfo.getFirstname();
                      String nick = dirInfo.getNickname();
                      String lastName = dirInfo.getLastname();
                      String fullName =
                          firstName != null ? firstName : "" + lastName != null ? lastName : "";
                      if (fullName != null && fullName.length() > 0) {
                        existingFriend.setName(fullName);
                      } else if (nick != null && nick.length() > 0) {
                        existingFriend.setName(nick);
                      }
                    }
                  }

                  @Override
                  public void handleCertificateInfo(
                      InfoService service, Screenname buddy, BuddyCertificateInfo certInfo) {}

                  @Override
                  public void handleAwayMessage(
                      InfoService service, Screenname buddy, String awayMessage) {}
                });

        conn.getChatRoomManager()
            .addListener(
                new ChatRoomManagerListener() {

                  public void handleInvitation(
                      ChatRoomManager chatRoomManager, ChatInvitation ourInvitation) {}
                });

        BuddyService buddyService = conn.getBuddyService();
        buddyService.addBuddyListener(
            new BuddyServiceListener() {

              public void buddyOffline(BuddyService service, Screenname buddy) {
                synchronized (AIMAccount.this) {
                  Friend friend = getFriendFromBuddy(buddy, false);
                  friend.setIsAvailable(false);
                  HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                  Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                  if (existingFriend == null) {
                    existingFriend = friend;
                    accountListener.didAddFriend(existingFriend);
                  } else {
                    accountListener.friendStatusDidChange(existingFriend);
                  }
                }
              }

              public void gotBuddyStatus(
                  BuddyService service, Screenname buddy, FullUserInfo info) {
                synchronized (AIMAccount.this) {
                  Friend friend = getFriendFromBuddy(buddy, false);
                  HashMap<String, Friend> friends = AccountManager.getInstance().getFriends();
                  Friend existingFriend = friends.get(friend.getUniqueDescriptior());
                  if (existingFriend == null) {
                    existingFriend = friend;
                    accountListener.didAddFriend(existingFriend);
                  }
                  applyInfoToFriend(existingFriend, info);
                  accountListener.friendStatusDidChange(existingFriend);
                }
              }
            });
      } else {
        disconnect();
      }
    }