Esempio n. 1
0
  private void registerPongListener() {
    pingID = null;

    if (pongListener != null) extXMPPConnection.removePacketListener(pongListener);

    pongListener =
        new PacketListener() {

          @Override
          public void processPacket(Packet packet) {
            if (packet == null) return;

            gotServerPong(packet.getPacketID());
          }
        };

    extXMPPConnection.addPacketListener(pongListener, new PacketTypeFilter(IQ.class));
    pingAlarmPendIntent =
        PendingIntent.getBroadcast(
            service.getApplicationContext(), 0, intentPingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    timeoutAlarmPendIntent =
        PendingIntent.getBroadcast(
            service.getApplicationContext(),
            0,
            intentTimeoutAlarm,
            PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setInexactRepeating(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
        AlarmManager.INTERVAL_FIFTEEN_MINUTES,
        pingAlarmPendIntent);
  }
Esempio n. 2
0
  public void unRegisterCallback() {
    try {
      extXMPPConnection.getRoster().removeRosterListener(rosterListener);
      extXMPPConnection.removePacketListener(packetListener);
      extXMPPConnection.removePacketListener(presenceListener);

      extXMPPConnection.removePacketListener(pongListener);
      unregisterPongListener();
    } catch (Exception e) {
      e.printStackTrace();
    }
    requestConnectionState(ConnectionState.OFFLINE);
    setStatusOffline();
    service.unregisterReceiver(receiverPingAlarm);
    service.unregisterReceiver(receiverTimeoutAlarm);
    this.serviceCallback = null;
  }
Esempio n. 3
0
  private void registerRosterListener() {

    roster = extXMPPConnection.getRoster();
    roster.setSubscriptionMode(Roster.SubscriptionMode.manual);

    if (rosterListener != null) roster.removeRosterListener(rosterListener);

    rosterListener =
        new RosterListener() {
          private boolean first_roster = true;

          public void entriesAdded(Collection<String> entries) {
            ContentValues[] cvs = new ContentValues[entries.size()];
            int i = 0;
            for (String entry : entries) {
              RosterEntry rosterEntry = roster.getEntry(entry);
              cvs[i++] = getContentValuesForRosterEntry(rosterEntry);
            }
            contentResolver.bulkInsert(RosterProvider.CONTENT_URI, cvs);
            if (first_roster) {
              removeOldRosterEntries();
              first_roster = false;
              serviceCallback.rosterChanged();
            }
          }

          public void entriesDeleted(Collection<String> entries) {
            for (String entry : entries) {
              deleteRosterEntryFromDB(entry);
            }
            serviceCallback.rosterChanged();
          }

          public void entriesUpdated(Collection<String> entries) {

            for (String item : entries) {
              RosterEntry rosterEntry = roster.getEntry(item);
              updateRosterEntryInDB(rosterEntry);
            }
            serviceCallback.rosterChanged();
          }

          public void presenceChanged(Presence presence) {

            String ID = getBareJID(presence.getFrom());
            RosterEntry rosterEntry = roster.getEntry(ID);
            if (rosterEntry != null) {
              updateRosterEntryInDB(rosterEntry);
              serviceCallback.rosterChanged();
            }
          }
        };
    roster.addRosterListener(rosterListener);
  }
Esempio n. 4
0
  public void sendServerPing() {
    if (extXMPPConnection == null || !extXMPPConnection.isAuthenticated()) {
      requestConnectionState(ConnectionState.ONLINE, false);
      return;
    }
    if (pingID != null) {
      return;
    }

    if (streamHandler.isIfEnabled()) {
      pingID = "" + streamHandler.requestAck();
    } else {
      Ping ping = new Ping();
      ping.setType(Type.GET);
      ping.setTo(configuration.server);
      pingID = ping.getPacketID();
      extXMPPConnection.sendPacket(ping);
    }

    registerPongTimeout(PACKET_TIMEOUT + 3000, pingID);
  }
Esempio n. 5
0
 protected void sendUserWatching() {
   IQ toggle_google_queue =
       new IQ() {
         public String getChildElementXML() {
           return "<query xmlns='google:queue'><"
               + (isUserWatching ? "disable" : "enable")
               + "/></query>";
         }
       };
   toggle_google_queue.setType(IQ.Type.SET);
   extXMPPConnection.sendPacket(toggle_google_queue);
 }
Esempio n. 6
0
  public void setStatusFromConfig() {
    CarbonManager.getInstanceFor(extXMPPConnection)
        .sendCarbonsEnabled(configuration.messageCarbons);

    Presence presence = new Presence(Presence.Type.available);
    Mode mode = Mode.valueOf(configuration.statusMode);
    presence.setMode(mode);
    presence.setStatus(configuration.statusMessage);
    presence.setPriority(configuration.priority);
    extXMPPConnection.sendPacket(presence);
    configuration.presence_required = false;
  }
Esempio n. 7
0
  private void registerPresenceListener() {

    if (presenceListener != null) extXMPPConnection.removePacketListener(presenceListener);

    presenceListener =
        new PacketListener() {
          public void processPacket(Packet packet) {
            try {
              Presence p = (Presence) packet;
              switch (p.getType()) {
                case subscribe:
                  handleIncomingSubscribe(p);
                  break;
                case unsubscribe:
                  break;
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        };

    extXMPPConnection.addPacketListener(presenceListener, new PacketTypeFilter(Presence.class));
  }
Esempio n. 8
0
  private void tryToRemoveRosterEntry(String user) throws ClientException {
    try {
      RosterEntry rosterEntry = roster.getEntry(user);

      if (rosterEntry != null) {

        Presence unsub = new Presence(Presence.Type.unsubscribed);
        unsub.setTo(rosterEntry.getUser());
        extXMPPConnection.sendPacket(unsub);
        roster.removeEntry(rosterEntry);
      }
    } catch (XMPPException e) {
      throw new ClientException("tryToRemoveRosterEntry", e);
    }
  }
Esempio n. 9
0
  private void tryToConnect(boolean create_account) throws ClientException {
    try {
      if (extXMPPConnection.isConnected()) {
        try {
          streamHandler.quickShutdown();
        } catch (Exception e) {
          Log.d(TAG, "conn.shutdown() failed: " + e);
        }
      }
      registerRosterListener();
      boolean need_bind = !streamHandler.isResumePossible();

      if (connectionListener != null)
        extXMPPConnection.removeConnectionListener(connectionListener);
      connectionListener =
          new ConnectionListener() {
            public void connectionClosedOnError(Exception e) {
              onDisconnected(e);
            }

            public void connectionClosed() {

              updateConnectionState(ConnectionState.OFFLINE);
            }

            public void reconnectingIn(int seconds) {}

            public void reconnectionFailed(Exception e) {}

            public void reconnectionSuccessful() {}
          };
      extXMPPConnection.addConnectionListener(connectionListener);

      extXMPPConnection.connect(need_bind);
      if (!extXMPPConnection.isAuthenticated()) {
        if (create_account) {
          AccountManager am = new AccountManager(extXMPPConnection);
          am.createAccount(configuration.userName, configuration.password);
        }
        extXMPPConnection.login(
            configuration.userName, configuration.password, configuration.ressource);
      }
      if (need_bind) {
        streamHandler.notifyInitialLogin();
        setStatusFromConfig();
      }

    } catch (Exception e) {

      throw new ClientException("tryToConnect failed", e);
    }
  }
Esempio n. 10
0
  public void sendOfflineMessages() {
    Cursor cursor =
        contentResolver.query(
            ChatProvider.CONTENT_URI, SEND_OFFLINE_PROJECTION, SEND_OFFLINE_SELECTION, null, null);
    final int ID = cursor.getColumnIndexOrThrow(ChatConstants._ID);
    final int JABBER_ID = cursor.getColumnIndexOrThrow(ChatConstants.JID);
    final int MESSAGE = cursor.getColumnIndexOrThrow(ChatConstants.MESSAGE);
    final int DATE = cursor.getColumnIndexOrThrow(ChatConstants.DATE);
    final int PACKET_ID = cursor.getColumnIndexOrThrow(ChatConstants.PACKET_ID);

    ContentValues mark_sent = new ContentValues();
    mark_sent.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_SENT_OR_READ);
    while (cursor.moveToNext()) {
      int id = cursor.getInt(ID);
      String toJID = cursor.getString(JABBER_ID);
      String message = cursor.getString(MESSAGE);
      String packetID = cursor.getString(PACKET_ID);
      long dateTs = cursor.getLong(DATE);
      final Message newMsg = new Message(toJID, Message.Type.chat);
      newMsg.setBody(message);
      DelayInformation delay = new DelayInformation(new Date(dateTs));
      newMsg.addExtension(delay);
      newMsg.addExtension(new DelayInfo(delay));
      newMsg.addExtension(new DeliveryReceiptRequest());
      if ((packetID != null) && (packetID.length() > 0)) {
        newMsg.setPacketID(packetID);
      } else {
        packetID = newMsg.getPacketID();
        mark_sent.put(ChatConstants.PACKET_ID, packetID);
      }
      Uri rowuri =
          Uri.parse(
              Constant.CONTENT_PATH
                  + ChatProvider.AUTHORITY
                  + "/"
                  + ChatProvider.TABLE_NAME
                  + "/"
                  + id);
      contentResolver.update(rowuri, mark_sent, null, null);
      extXMPPConnection.sendPacket(newMsg);
    }
    cursor.close();
  }
Esempio n. 11
0
 public void sendMessage(String toJID, String message) {
   final Message newMessage = new Message(toJID, Message.Type.chat);
   newMessage.setBody(message);
   newMessage.addExtension(new DeliveryReceiptRequest());
   if (isAuthenticated()) {
     addChatMessageToDB(
         ChatConstants.OUTGOING,
         toJID,
         message,
         ChatConstants.DS_SENT_OR_READ,
         System.currentTimeMillis(),
         newMessage.getPacketID());
     extXMPPConnection.sendPacket(newMessage);
   } else {
     addChatMessageToDB(
         ChatConstants.OUTGOING,
         toJID,
         message,
         ChatConstants.DS_NEW,
         System.currentTimeMillis(),
         newMessage.getPacketID());
   }
 }
Esempio n. 12
0
  private void registerMessageListener() {

    if (packetListener != null) extXMPPConnection.removePacketListener(packetListener);

    PacketTypeFilter filter = new PacketTypeFilter(Message.class);

    packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) {
            try {
              if (packet instanceof Message) {
                Message message = (Message) packet;

                String fromJID = getBareJID(message.getFrom());
                int direction = ChatConstants.INCOMING;
                Carbon carbon = CarbonManager.getCarbon(message);

                // extract timestamp
                long ts;
                DelayInfo timestamp =
                    (DelayInfo) message.getExtension(Constant.DELAY, Constant.URN_XMPP_DELAY);
                if (timestamp == null)
                  timestamp = (DelayInfo) message.getExtension(Constant.X, Constant.JABBER_X_DELAY);
                if (carbon != null) timestamp = carbon.getForwarded().getDelayInfo();
                if (timestamp != null) ts = timestamp.getStamp().getTime();
                else ts = System.currentTimeMillis();
                if (carbon != null) {

                  message = (Message) carbon.getForwarded().getForwardedPacket();

                  if (carbon.getDirection() == Carbon.Direction.sent) {
                    fromJID = getBareJID(message.getTo());
                    direction = ChatConstants.OUTGOING;
                  } else {
                    fromJID = getBareJID(message.getFrom());
                    DeliveryReceipt dr =
                        (DeliveryReceipt)
                            message.getExtension(
                                DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);
                    if (dr != null) {

                      changeMessageDeliveryStatus(dr.getId(), ChatConstants.DS_ACKED);
                    }
                  }
                }

                String chatMessage = message.getBody();
                if (message.getType() == Message.Type.error) {
                  if (changeMessageDeliveryStatus(message.getPacketID(), ChatConstants.DS_FAILED))
                    serviceCallback.messageError(
                        fromJID, message.getError().toString(), (carbon != null));
                  return;
                }

                if (chatMessage == null) {
                  return;
                }

                int is_new =
                    (carbon == null) ? ChatConstants.DS_NEW : ChatConstants.DS_SENT_OR_READ;
                if (message.getType() == Message.Type.error) is_new = ChatConstants.DS_FAILED;

                addChatMessageToDB(
                    direction, fromJID, chatMessage, is_new, ts, message.getPacketID());
                if (direction == ChatConstants.INCOMING)
                  serviceCallback.newMessage(fromJID, chatMessage, (carbon != null));
              }
            } catch (Exception e) {

              e.printStackTrace();
            }
          }
        };

    extXMPPConnection.addPacketListener(packetListener, filter);
  }
Esempio n. 13
0
 public void setUserWatching(boolean user_watching) {
   if (isUserWatching == user_watching) return;
   isUserWatching = user_watching;
   if (extXMPPConnection != null && extXMPPConnection.isAuthenticated()) sendUserWatching();
 }
Esempio n. 14
0
 public boolean isAuthenticated() {
   if (extXMPPConnection != null) {
     return (extXMPPConnection.isConnected() && extXMPPConnection.isAuthenticated());
   }
   return false;
 }
Esempio n. 15
0
 public void sendReceipt(String toJID, String id) {
   final Message ack = new Message(toJID, Message.Type.normal);
   ack.addExtension(new DeliveryReceipt(id));
   extXMPPConnection.sendPacket(ack);
 }
Esempio n. 16
0
 public void sendPresenceRequest(String user, String type) {
   if ("unsubscribed".equals(type)) deleteRosterEntryFromDB(user);
   Presence response = new Presence(Presence.Type.valueOf(type));
   response.setTo(user);
   extXMPPConnection.sendPacket(response);
 }