org.jivesoftware.smackx.filetransfer.FileTransfer Java Examples

The following examples show how to use org.jivesoftware.smackx.filetransfer.FileTransfer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileUpload.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
/**
	 * 发送文件
	 * 
	 * @param connection
	 * @param user
	 * @param toUserName
	 * @param file
	 */
	public static void sendFile(final Context context,
			final Connection connection, final String toUser, final Uri uri,
			final String filePath, final MsgType msgType) {
		new Thread() {
			public void run() {
				XMPPConnection.DEBUG_ENABLED = true;
				// AccountManager accountManager;
				try {
					// accountManager = connection.getAccountManager();
					Presence pre = connection.getRoster().getPresence(toUser);
					if (pre.getType() != Presence.Type.unavailable) {
						if (connection.isConnected()) {
							Log.d(TAG, "connection con");
						}
						// 创建文件传输管理器
//						ServiceDiscoveryManager sdm = ServiceDiscoveryManager
//								.getInstanceFor(connection);
//						if (sdm == null)
//							sdm = new ServiceDiscoveryManager(connection);
						
						FileTransferManager manager = new FileTransferManager(
								connection);
						// 创建输出的文件传输
						OutgoingFileTransfer transfer = manager
								.createOutgoingFileTransfer(pre.getFrom());
						// 发送文件
						transfer.sendFile(new File(filePath),
								msgType.toString());
						while (!transfer.isDone()) {
							if (transfer.getStatus() == FileTransfer.Status.in_progress) {
								// 可以调用transfer.getProgress();获得传输的进度 
								// Log.d(TAG,
								// "send status:" + transfer.getStatus());
								// Log.d(TAG,
								// "send progress:"
								// + transfer.getProgress());
								if (mFileUploadListener != null) {
									mFileUploadListener.transProgress(context,
											uri, filePath,
											transfer.getProgress());
								}
							}
						}
						// YiLog.getInstance().i("send file error: %s",
						// transfer.);
						Log.d(TAG, "send status 1 " + transfer.getStatus());
						if (transfer.isDone()) {
							if (mFileUploadListener != null) {
								mFileUploadListener.transDone(context, toUser,
										uri, msgType, filePath,
										transfer.getStatus());
							}
						}
					}
				} catch (Exception e) {
					Log.d(TAG, "send exception");
					if (mFileUploadListener != null) {
						mFileUploadListener.transDone(context, toUser, uri,
								msgType, filePath, Status.error);
					}
				}
			}
		}.start();
	}
 
Example #2
Source File: XMPPFileTransfer.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
XMPPFileTransfer(FileTransfer transfer) {
  this.transfer = transfer;
}
 
Example #3
Source File: SendFileTransfer.java    From Spark with Apache License 2.0 4 votes vote down vote up
private void updateBar(final OutgoingFileTransfer transfer, String nickname, String kBperSecond) {
    FileTransfer.Status status = transfer.getStatus();
    if (status == Status.negotiating_stream) {
        titleLabel.setText(Res.getString("message.negotiation.file.transfer", nickname));
    }
    else if (status == Status.error) {
        if (transfer.getException() != null) {
            Log.error("Error occured during file transfer.", transfer.getException());
        }
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.unable.to.send.file", nickname));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }
    else if (status == Status.in_progress) {
        titleLabel.setText(Res.getString("message.sending.file.to", nickname));
        showAlert(false);
        if (!progressBar.isVisible()) {
            progressBar.setVisible(true);
            progressLabel.setVisible(true);
        }
        
        try {
        	SwingUtilities.invokeAndWait( () -> {
                // 100 % = Filesize
            // x %   = Currentsize
                long p = (transfer.getBytesSent() * 100 / transfer.getFileSize() );
                progressBar.setValue(Math.round(p));
            } );
        }
        catch (Exception e) {
            Log.error(e);
        }

        ByteFormat format = new ByteFormat();
        String bytesSent = format.format(transfer.getBytesSent());
        String est = TransferUtils.calculateEstimate(transfer.getBytesSent(), transfer.getFileSize(), _starttime, System.currentTimeMillis());
       
        progressLabel.setText(Res.getString("message.transfer.progressbar.text.sent", bytesSent, kBperSecond, est));
    }
    else if (status == Status.complete) {
        progressBar.setVisible(false);
        
        String fin = TransferUtils.convertSecondstoHHMMSS(Math.round(System.currentTimeMillis()-_starttime)/1000);
        progressLabel.setText(Res.getString("label.time", fin));
        titleLabel.setText(Res.getString("message.you.have.sent", nickname));
        cancelButton.setVisible(false);
        showAlert(true);
    }
    else if (status == Status.cancelled) {
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.file.transfer.canceled"));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }
    else if (status == Status.refused) {
        progressBar.setVisible(false);
        progressLabel.setVisible(false);
        titleLabel.setText(Res.getString("message.file.transfer.rejected", nickname));
        cancelButton.setVisible(false);
        retryButton.setVisible(true);
        showAlert(true);
    }

}