001/** 002 * 003 * Copyright 2017 Florian Schmaus. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.chat2; 018 019import org.jivesoftware.smack.Manager; 020import org.jivesoftware.smack.SmackException.NotConnectedException; 021import org.jivesoftware.smack.XMPPConnection; 022import org.jivesoftware.smack.packet.Message; 023import org.jivesoftware.smack.packet.Presence; 024import org.jxmpp.jid.EntityBareJid; 025import org.jxmpp.jid.EntityFullJid; 026import org.jxmpp.jid.Jid; 027 028public final class Chat extends Manager { 029 030 private final EntityBareJid jid; 031 032 volatile EntityFullJid lockedResource; 033 034 Presence lastPresenceOfLockedResource; 035 036 Chat(final XMPPConnection connection, EntityBareJid jid) { 037 super(connection); 038 this.jid = jid; 039 } 040 041 public void send(CharSequence message) throws NotConnectedException, InterruptedException { 042 Message stanza = new Message(); 043 stanza.setBody(message); 044 send(stanza); 045 } 046 047 public void send(Message message) throws NotConnectedException, InterruptedException { 048 switch (message.getType()) { 049 case normal: 050 case chat: 051 break; 052 default: 053 throw new IllegalArgumentException("Message must be of type 'normal' or 'chat'"); 054 } 055 056 Jid to = lockedResource; 057 if (to == null) { 058 to = jid; 059 } 060 message.setTo(to); 061 062 connection().sendStanza(message); 063 } 064 065 public EntityBareJid getXmppAddressOfChatPartner() { 066 return jid; 067 } 068 069 void unlockResource() { 070 lockedResource = null; 071 lastPresenceOfLockedResource = null; 072 } 073}