aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/xmpp/JingleConnection.java
blob: 34587a93414950d6cefeeb5aea10db561f49d993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package eu.siacs.conversations.xmpp;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.JingleConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.jingle.Content;
import eu.siacs.conversations.xmpp.stanzas.jingle.JinglePacket;

public class JingleConnection {

	private JingleConnectionManager mJingleConnectionManager;
	private XmppConnectionService mXmppConnectionService;
	
	private String sessionId;
	private Account account;
	private String counterpart;
	private List<Element> canditates = new ArrayList<Element>();
	
	public JingleConnection(JingleConnectionManager mJingleConnectionManager, Account account, String counterpart) {
		this.mJingleConnectionManager = mJingleConnectionManager;
		this.mXmppConnectionService = mJingleConnectionManager.getXmppConnectionService();
		this.account = account;
		this.counterpart = counterpart;
		SecureRandom random = new SecureRandom();
		sessionId = new BigInteger(100, random).toString(32);
		this.canditates.add(this.mJingleConnectionManager.getPrimaryCanditate(account.getJid()));
	}
	
	public String getSessionId() {
		return this.sessionId;
	}
	
	public void init(Message message) {
		JinglePacket packet = this.bootstrapPacket();
		packet.setAction("session-initiate");
		packet.setInitiator(this.account.getFullJid());
		Content content = new Content();
		if (message.getType() == Message.TYPE_IMAGE) {
			//creator='initiator' name='a-file-offer'
			content.setAttribute("creator", "initiator");
			content.setAttribute("name", "a-file-offer");
			content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message));
			content.setCanditates(this.canditates);
			packet.setContent(content);
			Log.d("xmppService",packet.toString());
			account.getXmppConnection().sendIqPacket(packet, null);
		}
	}
	
	private JinglePacket bootstrapPacket() {
		JinglePacket packet = new JinglePacket();
		packet.setFrom(account.getFullJid());
		packet.setTo(this.counterpart+"/Gajim");
		packet.setSessionId(this.sessionId);
		return packet;
	}

}