Ejemplo n.º 1
0
 protected org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder
     getIQChildElementBuilder(
         org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder
             iqchildelementxmlstringbuilder) {
   return dataPacketExtension.getIQChildElementBuilder(iqchildelementxmlstringbuilder);
 }
  /*
   * Handle a single smack packet, discarding anything but Message.
   * @param packet The smack packet.
   * (non-Javadoc)
   * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
   */
  @Override
  public void processPacket(Packet packet) {

    Log.i("processPacket", packet.toXML());
    if (packet instanceof Presence) {

      double inLat = 0, inLong = 0;
      boolean isMUC = false, isGEO = false;

      Presence presence = (Presence) packet;

      // In a MUC, the "FROM" is the Resource
      String presenceFrom = StringUtils.parseResource(presence.getFrom());
      String presenceTo = StringUtils.parseName(presence.getTo());

      // if presence packet is from yourself, just bail
      if (presenceFrom.equals(presenceTo)) return;

      for (PacketExtension extension : presence.getExtensions()) {
        if (extension instanceof GeoLoc) {
          GeoLoc loc = (GeoLoc) extension;

          inLat = loc.getLat();
          inLong = loc.getLon();

          isGEO = true;

          Log.d("CNL", "ERIK: GEOLOC EXTENSION FOUND, LAT: " + inLat);
        }
        if (extension instanceof MUCUser) {
          // MUCUser muc = (MUCUser) extension;   no need to create this object
          isMUC = true;

          Log.d("CNL", "ERIK: MUC EXTENSION FOUND, presence type=" + presence.getType());
        }
      }

      //  If a MUC available presence packet comes in, add/update database
      if (isMUC == true && presence.getType().toString().equals("available") && isGEO == true) {

        updateDatabase(presenceFrom, inLat, inLong, null);
      }

      // if a MUC Unavailable presence packet comes in, remove user from database
      else if (isMUC == true && presence.getType().toString().equals("unavailable")) {

        if (this.database.delete("user_info", "name='" + presenceFrom + "'", null) > 0) {
          Log.d("CNL", "ERIK: DATABASE UPDATED, USER " + presenceFrom + " DELETED");
        } else Log.d("CNL", "ERIK: DATABASE SEARCHED, USER " + presenceFrom + " NOT FOUND");
      }
    }

    if (packet instanceof IQ) {
      IQ iq = (IQ) packet;
      Log.d("CNL", "ERIK: IQ PACKET RECEIVED: " + iq.getExtensions());
    }

    if (packet instanceof Message) {
      Message msg = (Message) packet;
      String text = msg.getBody();

      if (text == null || text.trim().length() == 0) {
        Log.d("CNL", "ERIK: MESSAGE PACKET LACKS A MESSAGE!!!");
        return;
      }

      // Extract name
      String messageFrom = StringUtils.parseResource(msg.getFrom());

      // Extract lat and lon from message
      double inLat = getLat(msg);
      double inLon = getLon(msg);
      Boolean isEmergency = null;
      if (text.startsWith(this.context.getString(R.string.emergency_message))) {
        isEmergency = Boolean.TRUE;
      } else if (text.startsWith(this.context.getString(R.string.cancel_message))) {
        isEmergency = Boolean.FALSE;
      }

      Log.i("CNL", "recovered name=[" + messageFrom + "], lat/lon=" + inLat + "," + inLon);

      updateDatabase(messageFrom, inLat, inLon, isEmergency);

      String bareFrom = XMPPUtils.getBareJid(msg.getFrom());
      String msgFrom = StringUtils.parseResource(msg.getFrom());
      String bareTo = XMPPUtils.getBareJid(msg.getTo());
      String msgTo = StringUtils.parseName(msg.getTo());

      if (msg.getType().toString().equals("groupchat")) {
        Log.d(
            "CNL",
            "ERIK: MUC MESSAGE PACKET RECEIVED, CONTAINS EXTENSIONS: " + msg.getExtensions());
        if (msgFrom.equals(msgTo)) return;

        // Picture receiving code here.............................!!!!!!!!!!

        for (PacketExtension extension : msg.getExtensions()) {
          if (extension instanceof DataPacketExtension) {
            DataPacketExtension data = (DataPacketExtension) extension;

            byte[] imageBytes = data.getDecodedData();
            String imagePath = Environment.getExternalStorageDirectory() + "/mmmc/";

            // String imageName = text;
            File f = new File(imagePath, text);
            OutputStream out = null;

            try {
              out = new BufferedOutputStream(new FileOutputStream(f));
              out.write(imageBytes);
            } catch (IOException ioe) {
              ioe.printStackTrace();
            } finally {
              if (out != null) {
                try {
                  out.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }

            // Log.d("CNL", "ERIK: MUC EXTENSION FOUND");
            ContentValues values = new ContentValues();
            values.put("ts", System.currentTimeMillis());
            values.put("jid", bareFrom);

            // don't put who it's from, put the resource it came from (user in a MUC)
            values.put("src", msgFrom);

            values.put("dst", msg.getTo());
            values.put("via", bareTo);
            values.put("msg", "Picture received: " + text.trim());
            this.database.insert("msg", "_id", values);

            Log.d("CNL", "ERIK: DATA EXTENSION FOUND, IMAGE SAVED");
            return;
          }
        }

        // Log.d("CNL", "ERIK: MUC EXTENSION FOUND");

        // Insert new message into database
        ContentValues values = new ContentValues();
        values.put("ts", System.currentTimeMillis());
        values.put("jid", bareFrom);

        // don't put who it's from, put the resource it came from (user in a MUC)
        values.put("src", msgFrom);

        values.put("dst", msg.getTo());
        values.put("via", bareTo);
        values.put("msg", text.trim());
        this.database.insert("msg", "_id", values);
      } else Log.d("CNL", "ERIK: NON-MUC MESSAGE PACKET RECEIVED: " + bareFrom);

      Builder builder = new Uri.Builder();
      builder.scheme("content");
      builder.authority("jabber-chat-db");
      builder.appendPath(bareTo);
      builder.appendPath(bareFrom);
      this.context.getContentResolver().notifyChange(builder.build(), null);
      setNotification(bareFrom, bareTo);
    }
  }