Пример #1
0
public class ConnectTask extends BaseTask {

  private static final String LOGTAG = LogUtil.makeLogTag(ConnectTask.class);

  public ConnectTask(XmppManager xmppManager) {
    super(xmppManager);
  }

  @Override
  public void run() {
    Log.i(LOGTAG, "ConnectTask.run()...");
    XmppManager xmppManager = getXmppManager();
    if (!xmppManager.isConnected()) {
      ConnectionConfiguration connCfg =
          new ConnectionConfiguration(xmppManager.getXmppHost(), xmppManager.getXmppPort());
      connCfg.setSecurityMode(SecurityMode.required);
      connCfg.setSASLAuthenticationEnabled(false);
      connCfg.setCompressionEnabled(false);
      XMPPConnection connection = new XMPPConnection(connCfg);
      xmppManager.setConnection(connection);
      try {
        connection.connect();
        ProviderManager.getInstance()
            .addIQProvider(
                "notification", Constants.NOTIFICATION_NAMESPACE, new NotificationIQProvider());
        Log.i(LOGTAG, "XMPP connected successfully");
        xmppManager.getContext().sendBroadcast(new Intent(Constants.ACTION_CONNECT_SUCCESS));
      } catch (XMPPException e) {
        Log.e(LOGTAG, "XMPP connection failed", e);
        xmppManager.getContext().sendBroadcast(new Intent(Constants.ACTION_CONNECT_FAIL));
      }
      xmppManager.runTask();
    } else {
      Log.i(LOGTAG, "XMPP connected already");
      xmppManager.runTask();
    }
  }
}
Пример #2
0
public class RegisterTask extends BaseTask {

  private static final String LOGTAG = LogUtil.makeLogTag(RegisterTask.class);

  public RegisterTask(XmppManager xmppManager) {
    super(xmppManager);
  }

  @Override
  public void run() {
    Log.i(LOGTAG, "RegisterTask.run()...");
    final XmppManager xmppManager = getXmppManager();
    if (!xmppManager.isRegistered()) {
      String uuid = Util.getDeviceUUID(xmppManager.getContext());
      final String newUsername = uuid;
      final String newPassword = uuid;
      Registration registration = new Registration();
      PacketFilter packetFilter =
          new AndFilter(
              new PacketIDFilter(registration.getPacketID()), new PacketTypeFilter(IQ.class));
      PacketListener packetListener =
          new PacketListener() {
            public void processPacket(Packet packet) {
              Log.d("RegisterTask.PacketListener", "processPacket().....");
              Log.d("RegisterTask.PacketListener", "packet=" + packet.toXML());

              if (packet instanceof IQ) {
                IQ response = (IQ) packet;
                if (response.getType() == IQ.Type.ERROR) {
                  if (!response.getError().toString().contains("409")) {
                    Log.e(
                        LOGTAG,
                        "Unknown error while registering XMPP account! "
                            + response.getError().getCondition());
                  }
                } else if (response.getType() == IQ.Type.RESULT) {
                  xmppManager.setUsername(newUsername);
                  xmppManager.setPassword(newPassword);
                  Log.d(LOGTAG, "username="******"password="******"Account registered successfully");
                  xmppManager.runTask();
                }
              }
            }
          };
      XMPPConnection connection = xmppManager.getConnection();
      connection.addPacketListener(packetListener, packetFilter);
      registration.setType(IQ.Type.SET);
      registration.addAttribute("username", newUsername);
      registration.addAttribute("password", newPassword);
      connection.sendPacket(registration);
    } else {
      Log.i(LOGTAG, "Account registered already");
      xmppManager.runTask();
    }
  }
}