Example #1
0
  @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();
    }
  }
Example #2
0
 /**
  * 注册
  *
  * @param account 注册帐号
  * @param password 注册密码
  * @return 1、注册成功 0、服务器没有返回结果2、这个账号已经存在3、注册失败
  */
 public String regist(String account, String password) {
   if (connection == null) return "0";
   Registration reg = new Registration();
   reg.setType(IQ.Type.SET);
   reg.setTo(connection.getServiceName());
   reg.setUsername(account); // 注意这里createAccount注册时,参数是username,不是jid,是“@”前面的部分。
   reg.setPassword(password);
   reg.addAttribute("android", "geolo_createUser_android"); // 这边addAttribute不能为空,否则出错。
   PacketFilter filter =
       new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
   PacketCollector collector = connection.createPacketCollector(filter);
   connection.sendPacket(reg);
   IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
   // Stop queuing results
   collector.cancel(); // 停止请求results(是否成功的结果)
   if (result == null) {
     Log.e("Regist", "No response from server.");
     return "0";
   } else if (result.getType() == IQ.Type.RESULT) {
     return "1";
   } else { // if (result.getType() == IQ.Type.ERROR)
     if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
       Log.e("Regist", "IQ.Type.ERROR: " + result.getError().toString());
       return "2";
     } else {
       Log.e("Regist", "IQ.Type.ERROR: " + result.getError().toString());
       return "3";
     }
   }
 }
Example #3
0
 /**
  * Gets the account registration info from the server.
  *
  * @throws XMPPException if an error occurs.
  */
 private synchronized void getRegistrationInfo() throws XMPPException {
   Registration reg = new Registration();
   reg.setTo(connection.getServiceName());
   PacketFilter filter =
       new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
   PacketCollector collector = connection.createPacketCollector(filter);
   connection.sendPacket(reg);
   IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
   // Stop queuing results
   collector.cancel();
   if (result == null) {
     throw new XMPPException("No response from server.");
   } else if (result.getType() == IQ.Type.ERROR) {
     throw new XMPPException(result.getError());
   } else {
     info = (Registration) result;
   }
 }
Example #4
0
 /**
  * Returns the instructions for creating a new account, or <tt>null</tt> if there are no
  * instructions. If present, instructions should be displayed to the end-user that will complete
  * the registration process.
  *
  * @return the account creation instructions, or <tt>null</tt> if there are none.
  */
 public String getAccountInstructions() {
   try {
     if (info == null) {
       getRegistrationInfo();
     }
     return info.getInstructions();
   } catch (XMPPException xe) {
     return null;
   }
 }
Example #5
0
 /**
  * Returns the value of a given account attribute or <tt>null</tt> if the account attribute wasn't
  * found.
  *
  * @param name the name of the account attribute to return its value.
  * @return the value of the account attribute or <tt>null</tt> if an account attribute wasn't
  *     found for the requested name.
  */
 public String getAccountAttribute(String name) {
   try {
     if (info == null) {
       getRegistrationInfo();
     }
     return info.getAttributes().get(name);
   } catch (XMPPException xe) {
     xe.printStackTrace();
   }
   return null;
 }
  private static Registration parseRegistration(XmlPullParser parser) throws Exception {
    Registration registration = new Registration();
    Map<String, String> fields = null;
    boolean done = false;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        // Any element that's in the jabber:iq:register namespace,
        // attempt to parse it if it's in the form <name>value</name>.
        if (parser.getNamespace().equals("jabber:iq:register")) {
          String name = parser.getName();
          String value = "";
          if (fields == null) {
            fields = new HashMap<String, String>();
          }

          if (parser.next() == XmlPullParser.TEXT) {
            value = parser.getText();
          }
          // Ignore instructions, but anything else should be added to the map.
          if (!name.equals("instructions")) {
            fields.put(name, value);
          } else {
            registration.setInstructions(value);
          }
        }
        // Otherwise, it must be a packet extension.
        else {
          registration.addExtension(
              PacketParserUtils.parsePacketExtension(
                  parser.getName(), parser.getNamespace(), parser));
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("query")) {
          done = true;
        }
      }
    }
    registration.setAttributes(fields);
    return registration;
  }
Example #7
0
 /**
  * Changes the password of the currently logged-in account. This operation can only be performed
  * after a successful login operation has been completed. Not all servers support changing
  * passwords; an XMPPException will be thrown when that is the case.
  *
  * @throws IllegalStateException if not currently logged-in to the server.
  * @throws XMPPException if an error occurs when changing the password.
  */
 public void changePassword(String newPassword) throws XMPPException {
   Registration reg = new Registration();
   reg.setType(IQ.Type.SET);
   reg.setTo(connection.getServiceName());
   Map<String, String> map = new HashMap<String, String>();
   map.put("username", StringUtils.parseName(connection.getUser()));
   map.put("password", newPassword);
   reg.setAttributes(map);
   PacketFilter filter =
       new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
   PacketCollector collector = connection.createPacketCollector(filter);
   connection.sendPacket(reg);
   IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
   // Stop queuing results
   collector.cancel();
   if (result == null) {
     throw new XMPPException("No response from server.");
   } else if (result.getType() == IQ.Type.ERROR) {
     throw new XMPPException(result.getError());
   }
 }
Example #8
0
 /**
  * Returns an unmodifiable collection of the names of the required account attributes. All
  * attributes must be set when creating new accounts. The standard set of possible attributes are
  * as follows:
  *
  * <ul>
  *   <li>name -- the user's name.
  *   <li>first -- the user's first name.
  *   <li>last -- the user's last name.
  *   <li>email -- the user's email address.
  *   <li>city -- the user's city.
  *   <li>state -- the user's state.
  *   <li>zip -- the user's ZIP code.
  *   <li>phone -- the user's phone number.
  *   <li>url -- the user's website.
  *   <li>date -- the date the registration took place.
  *   <li>misc -- other miscellaneous information to associate with the account.
  *   <li>text -- textual information to associate with the account.
  *   <li>remove -- empty flag to remove account.
  * </ul>
  *
  * <p>Typically, servers require no attributes when creating new accounts, or just the user's
  * email address.
  *
  * @return the required account attributes.
  */
 public Collection<String> getAccountAttributes() {
   try {
     if (info == null) {
       getRegistrationInfo();
     }
     Map<String, String> attributes = info.getAttributes();
     if (attributes != null) {
       return Collections.unmodifiableSet(attributes.keySet());
     }
   } catch (XMPPException xe) {
     xe.printStackTrace();
   }
   return Collections.emptySet();
 }
Example #9
0
 /**
  * Deletes the currently logged-in account from the server. This operation can only be performed
  * after a successful login operation has been completed. Not all servers support deleting
  * accounts; an XMPPException will be thrown when that is the case.
  *
  * @throws IllegalStateException if not currently logged-in to the server.
  * @throws XMPPException if an error occurs when deleting the account.
  */
 public void deleteAccount() throws XMPPException {
   if (!connection.isAuthenticated()) {
     throw new IllegalStateException("Must be logged in to delete a account.");
   }
   Registration reg = new Registration();
   reg.setType(IQ.Type.SET);
   reg.setTo(connection.getServiceName());
   Map<String, String> attributes = new HashMap<String, String>();
   // To delete an account, we add a single attribute, "remove", that is blank.
   attributes.put("remove", "");
   reg.setAttributes(attributes);
   PacketFilter filter =
       new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
   PacketCollector collector = connection.createPacketCollector(filter);
   connection.sendPacket(reg);
   IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
   // Stop queuing results
   collector.cancel();
   if (result == null) {
     throw new XMPPException("No response from server.");
   } else if (result.getType() == IQ.Type.ERROR) {
     throw new XMPPException(result.getError());
   }
 }
Example #10
0
 /**
  * Creates a new account using the specified username, password and account attributes. The
  * attributes Map must contain only String name/value pairs and must also have values for all
  * required attributes.
  *
  * @param username the username.
  * @param password the password.
  * @param attributes the account attributes.
  * @throws XMPPException if an error occurs creating the account.
  * @see #getAccountAttributes()
  */
 public void createAccount(String username, String password, Map<String, String> attributes)
     throws XMPPException {
   if (!supportsAccountCreation()) {
     throw new XMPPException("Server does not support account creation.");
   }
   Registration reg = new Registration();
   reg.setType(IQ.Type.SET);
   reg.setTo(connection.getServiceName());
   attributes.put("username", username);
   attributes.put("password", password);
   reg.setAttributes(attributes);
   PacketFilter filter =
       new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
   PacketCollector collector = connection.createPacketCollector(filter);
   connection.sendPacket(reg);
   IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
   // Stop queuing results
   collector.cancel();
   if (result == null) {
     throw new XMPPException("No response from server.");
   } else if (result.getType() == IQ.Type.ERROR) {
     throw new XMPPException(result.getError());
   }
 }
Example #11
0
 /**
  * Returns true if the server supports creating new accounts. Many servers require that you not be
  * currently authenticated when creating new accounts, so the safest behavior is to only create
  * new accounts before having logged in to a server.
  *
  * @return true if the server support creating new accounts.
  */
 public boolean supportsAccountCreation() {
   // Check if we already know that the server supports creating new accounts
   if (accountCreationSupported) {
     return true;
   }
   // No information is known yet (e.g. no stream feature was received from the server
   // indicating that it supports creating new accounts) so send an IQ packet as a way
   // to discover if this feature is supported
   try {
     if (info == null) {
       getRegistrationInfo();
       accountCreationSupported = info.getType() != IQ.Type.ERROR;
     }
     return accountCreationSupported;
   } catch (XMPPException xe) {
     return false;
   }
 }