8f69232c9d0a0d87c07ac8f491b37fbb68285957.svn-base 3.15 KB
package com.ivt.android.me.utils.xmpp;

import java.io.File;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

import android.util.Log;

public class XmppSendMessage {
	private static String TAG = "XmppMessage";
	private XMPPConnection con = XmppConnectionTool.getInstance()
			.getConnection();

	public static XmppSendMessage xcl = new XmppSendMessage();

	private boolean isChatAuth = false;

	public static XmppSendMessage getInstance() {
		return xcl;
	}

	/**
	 * Description: 发送单人聊天信息
	 * 
	 * @param chat
	 * @param chatContent
	 * @return
	 */
	public boolean sendSignleChat(Chat chat, String chatContent) {
		try {
			chat.sendMessage(chatContent);
			return true;
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// /**
	// * Description: 发送群组聊天信息
	// *
	// * @param chat
	// * @param chatContent
	// * @return
	// */
	// public int sendMultiChat(MultiUserChat muc, MeMessage msg) {
	// String msgAgo = JSON.toJSONString(memAgo);
	// String jsonStr = JSON.toJSONString(msg);
	// if (msgAgo.equals(jsonStr)) {
	// return 0;
	// } else {
	// memAgo = msg;
	// }
	// if (isChatAuth) {
	// if (msg.getMsgType().equals(XmppMessageType.TYPE_MSG)
	// || msg.getMsgType().equals(XmppMessageType.TYPE_BAR)) {
	// return 2;//已被禁言
	// }
	// }
	// if (!"".equals(jsonStr)) {
	// try {
	// muc.sendMessage(jsonStr);
	// return 1;
	// } catch (XMPPException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// return 0;
	// }

	/**
	 * Description: 发送广播
	 * 
	 * @param xmlString
	 * @return
	 */
	public boolean sendBroadcast(String xmlString) {
		if (con.isConnected() && con.getUser() != null) {
			Message message = new Message("all@broadcast.ivt");
			// Message message = new Message(null);
			message.setBody(xmlString);
			con.sendPacket(message);
			return true;
		}
		return false;
	}

	/**
	 * Description: 发送文件
	 * 
	 * @param userId
	 * @param conn
	 * @param file
	 * @throws Exception
	 */
	public void sendFile(String userId, File file) throws Exception {
		FileTransferManager manager = new FileTransferManager(con);
		OutgoingFileTransfer transfer = manager
				.createOutgoingFileTransfer(userId);
		Log.i(TAG, "要发送的文件是否存在:" + file.exists() + "***" + file.getPath());
		if (file.exists()) {
			transfer.sendFile(file, "data backup");
		}

		Log.i(TAG, transfer.getStatus() + "**1");// 开始发送(Initial)

		while (!transfer.isDone()) {
			// Thread.sleep(1000*2);
			Log.i(TAG, "等待对方接收");
			if (transfer.getStatus() == OutgoingFileTransfer.Status.in_progress) {
				// 可以调用transfer.getProgress();获得传输的进度
				Log.i(TAG, "发送文件:" + transfer.getProgress() + "***\n"
						+ transfer.getStatus());// 发送中(In Progress)
			}
		}

		Log.i(TAG, transfer.getStatus() + "**2");// 完成(Complete) 拒绝(Refused)
	}

}