Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xabber-android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
xabber-android
Commits
2cc0fb51
Commit
2cc0fb51
authored
May 12, 2015
by
Grigory Fedorov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CarbonManager: code format (indent)
parent
797bf743
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
183 additions
and
181 deletions
+183
-181
CarbonManager.java
app/src/main/java/com/xabber/xmpp/carbon/CarbonManager.java
+183
-181
No files found.
app/src/main/java/com/xabber/xmpp/carbon/CarbonManager.java
View file @
2cc0fb51
package
com
.
xabber
.
xmpp
.
carbon
;
import
org.jivesoftware.smack.Connection
;
import
org.jivesoftware.smack.ConnectionCreationListener
;
import
org.jivesoftware.smack.PacketListener
;
import
org.jivesoftware.smack.XMPPConnection
;
import
org.jivesoftware.smack.filter.PacketIDFilter
;
import
org.jivesoftware.smack.packet.IQ
;
import
org.jivesoftware.smack.packet.Message
;
import
org.jivesoftware.smack.packet.Packet
;
import
org.jivesoftware.smack.packet.PacketExtension
;
import
org.jivesoftware.smackx.ServiceDiscoveryManager
;
import
com.xabber.android.data.Application
;
import
com.xabber.android.data.LogManager
;
import
com.xabber.android.data.SettingsManager
;
...
...
@@ -26,125 +15,146 @@ import com.xabber.android.data.message.AbstractChat;
import
com.xabber.android.data.message.MessageItem
;
import
com.xabber.android.data.message.MessageManager
;
import
org.jivesoftware.smack.Connection
;
import
org.jivesoftware.smack.ConnectionCreationListener
;
import
org.jivesoftware.smack.PacketListener
;
import
org.jivesoftware.smack.XMPPConnection
;
import
org.jivesoftware.smack.filter.PacketIDFilter
;
import
org.jivesoftware.smack.packet.IQ
;
import
org.jivesoftware.smack.packet.Message
;
import
org.jivesoftware.smack.packet.Packet
;
import
org.jivesoftware.smack.packet.PacketExtension
;
import
org.jivesoftware.smackx.ServiceDiscoveryManager
;
/**
* Packet extension for XEP-0280: Message Carbons. This class implements
* the manager for registering {@link Carbon} support, enabling and disabling
* message carbons.
*
*
<p/>
* You should call enableCarbons() before sending your first undirected
* presence.
*
* @author Georg Lukas, Semyon Baranov
*/
public
class
CarbonManager
implements
OnServerInfoReceivedListener
,
OnPacketListener
{
private
final
static
CarbonManager
instance
;
private
Connection
connection
;
private
volatile
boolean
enabled_state
=
false
;
public
class
CarbonManager
implements
OnServerInfoReceivedListener
,
OnPacketListener
{
public
static
final
String
NAMESPACE
=
"urn:xmpp:carbons:2"
;
private
final
static
CarbonManager
instance
;
static
{
instance
=
new
CarbonManager
();
Application
.
getInstance
().
addManager
(
instance
);
Connection
.
addConnectionCreationListener
(
new
ConnectionCreationListener
()
{
@Override
public
void
connectionCreated
(
final
Connection
connection
)
{
if
(!(
connection
instanceof
XMPPConnection
))
{
return
;
}
ServiceDiscoveryManager
sdm
=
ServiceDiscoveryManager
.
getInstanceFor
(
connection
);
if
(
sdm
!=
null
)
{
sdm
.
addFeature
(
NAMESPACE
);
sdm
.
addFeature
(
Forwarded
.
NAMESPACE
);
}
instance
.
connection
=
connection
;
}
});
}
public
static
CarbonManager
getInstance
()
{
return
instance
;
}
instance
=
new
CarbonManager
();
Application
.
getInstance
().
addManager
(
instance
);
Connection
.
addConnectionCreationListener
(
new
ConnectionCreationListener
()
{
@Override
public
void
connectionCreated
(
final
Connection
connection
)
{
if
(!(
connection
instanceof
XMPPConnection
))
{
return
;
}
ServiceDiscoveryManager
sdm
=
ServiceDiscoveryManager
.
getInstanceFor
(
connection
);
if
(
sdm
!=
null
)
{
sdm
.
addFeature
(
NAMESPACE
);
sdm
.
addFeature
(
Forwarded
.
NAMESPACE
);
}
instance
.
connection
=
connection
;
}
});
}
private
Connection
connection
;
private
volatile
boolean
enabled_state
=
false
;
private
CarbonManager
()
{
}
public
static
CarbonManager
getInstance
()
{
return
instance
;
}
/**
* Mark a message as "private", so it will not be carbon-copied.
*
* @param msg Message object to mark private
*/
public
static
void
disableCarbons
(
Message
msg
)
{
msg
.
addExtension
(
new
Private
());
}
private
IQ
carbonsEnabledIQ
(
final
boolean
new_state
)
{
if
(!
checkConnected
())
{
return
null
;
}
if
(!
checkConnected
())
{
return
null
;
}
IQ
setIQ
=
new
IQ
()
{
public
String
getChildElementXML
()
{
return
String
.
format
(
"<%s xmlns='%s'/>"
,
new_state
?
"enable"
:
"disable"
,
NAMESPACE
);
}
};
setIQ
.
setType
(
IQ
.
Type
.
SET
);
setIQ
.
setType
(
IQ
.
Type
.
SET
);
setIQ
.
setFrom
(
connection
.
getUser
());
return
setIQ
;
}
private
boolean
checkConnected
()
{
if
(
connection
==
null
)
{
LogManager
.
exception
(
this
,
new
Exception
(
"connection is null"
));
if
(
connection
==
null
)
{
LogManager
.
exception
(
this
,
new
Exception
(
"connection is null"
));
return
false
;
}
if
(!
connection
.
isConnected
())
{
LogManager
.
exception
(
this
,
new
Exception
(
"not connected"
));
if
(!
connection
.
isConnected
())
{
LogManager
.
exception
(
this
,
new
Exception
(
"not connected"
));
return
false
;
}
return
true
;
}
return
true
;
}
/**
/**
* Returns true if XMPP Carbons are supported by the server.
*
*
* @return true if supported
*/
public
boolean
isSupportedByServer
()
{
if
(!
checkConnected
())
{
return
false
;
}
boolean
isCarbonSupported
=
ServerInfoManager
.
getInstance
().
isProtocolSupported
(
connection
.
getUser
(),
NAMESPACE
);
return
isCarbonSupported
;
if
(!
checkConnected
())
{
return
false
;
}
boolean
isCarbonSupported
=
ServerInfoManager
.
getInstance
().
isProtocolSupported
(
connection
.
getUser
(),
NAMESPACE
);
return
isCarbonSupported
;
}
/**
* Notify server to change the carbons state. This method returns
* immediately and changes the variable when the reply arrives.
*
*
<p/>
* You should first check for support using isSupportedByServer().
*
* @param new_state whether carbons should be enabled or disabled
*/
public
void
sendCarbonsEnabled
(
final
boolean
new_state
)
{
LogManager
.
d
(
this
,
"sendCarbonsEnabled "
+
String
.
valueOf
(
new_state
));
LogManager
.
d
(
this
,
"sendCarbonsEnabled "
+
String
.
valueOf
(
new_state
));
if
(!
checkConnected
())
{
return
;
}
return
;
}
IQ
setIQ
=
carbonsEnabledIQ
(
new_state
);
connection
.
addPacketListener
(
new
PacketListener
()
{
public
void
processPacket
(
Packet
packet
)
{
IQ
result
=
(
IQ
)
packet
;
IQ
result
=
(
IQ
)
packet
;
if
(
result
.
getType
()
==
IQ
.
Type
.
RESULT
)
{
enabled_state
=
new_state
;
}
connection
.
removePacketListener
(
this
);
}
},
new
PacketIDFilter
(
setIQ
.
getPacketID
()));
connection
.
sendPacket
(
setIQ
);
}
/**
* Helper method to enable carbons.
*/
...
...
@@ -152,125 +162,117 @@ public class CarbonManager implements OnServerInfoReceivedListener, OnPacketList
sendCarbonsEnabled
(
true
);
}
/**
* Helper method to disable carbons.
*/
public
void
disableCarbons
()
{
sendCarbonsEnabled
(
false
);
sendCarbonsEnabled
(
false
);
}
/**
* Check if carbons are enabled on this connection.
*/
public
boolean
getCarbonsEnabled
()
{
return
enabled_state
;
}
/**
* Mark a message as "private", so it will not be carbon-copied.
*
* @param msg Message object to mark private
*/
public
static
void
disableCarbons
(
Message
msg
)
{
msg
.
addExtension
(
new
Private
());
}
@Override
public
void
onPacket
(
ConnectionItem
connection
,
String
bareAddress
,
Packet
packet
)
{
if
(!(
connection
instanceof
AccountItem
))
{
return
;
}
if
(!
SettingsManager
.
connectionUseCarbons
())
{
return
;
}
final
String
user
=
packet
.
getFrom
();
if
(
user
==
null
)
return
;
if
(!(
packet
instanceof
Message
))
return
;
final
Message
message
=
(
Message
)
packet
;
if
(!
getCarbonsEnabled
())
{
return
;
}
PacketExtension
carbonExtension
=
null
;
Direction
dir
=
null
;
for
(
PacketExtension
packetExtension
:
message
.
getExtensions
())
{
if
(
packetExtension
instanceof
Received
)
{
carbonExtension
=
packetExtension
;
dir
=
Direction
.
received
;
}
else
if
(
packetExtension
instanceof
Sent
)
{
carbonExtension
=
packetExtension
;
dir
=
Direction
.
sent
;
}
else
{
continue
;
}
}
if
(
carbonExtension
==
null
)
{
@Override
public
void
onPacket
(
ConnectionItem
connection
,
String
bareAddress
,
Packet
packet
)
{
if
(!(
connection
instanceof
AccountItem
))
{
return
;
}
}
if
(!
SettingsManager
.
connectionUseCarbons
())
{
return
;
}
final
String
user
=
packet
.
getFrom
();
if
(
user
==
null
)
return
;
if
(!(
packet
instanceof
Message
))
return
;
final
Message
message
=
(
Message
)
packet
;
if
(!
getCarbonsEnabled
())
{
return
;
}
PacketExtension
carbonExtension
=
null
;
Direction
dir
=
null
;
for
(
PacketExtension
packetExtension
:
message
.
getExtensions
())
{
if
(
packetExtension
instanceof
Received
)
{
carbonExtension
=
packetExtension
;
dir
=
Direction
.
received
;
}
else
if
(
packetExtension
instanceof
Sent
)
{
carbonExtension
=
packetExtension
;
dir
=
Direction
.
sent
;
}
else
{
continue
;
}
}
if
(
carbonExtension
==
null
)
{
return
;
}
Forwarded
forwarded
=
null
;
if
(
dir
==
Direction
.
sent
)
{
Sent
carbon
=
(
Sent
)
carbonExtension
;
forwarded
=
carbon
.
getForwarded
();
}
else
{
Received
carbon
=
(
Received
)
carbonExtension
;
forwarded
=
carbon
.
getForwarded
();
}
if
(
forwarded
==
null
)
{
return
;
}
if
(
dir
==
Direction
.
sent
)
{
Sent
carbon
=
(
Sent
)
carbonExtension
;
forwarded
=
carbon
.
getForwarded
();
}
else
{
Received
carbon
=
(
Received
)
carbonExtension
;
forwarded
=
carbon
.
getForwarded
();
}
if
(
forwarded
==
null
)
{
return
;
}
Message
forwardedMsg
=
(
Message
)
forwarded
.
getForwardedPacket
();
MessageManager
.
getInstance
().
displayForwardedMessage
(
connection
,
forwardedMsg
,
dir
);
}
}
@Override
public
void
onServerInfoReceived
(
ConnectionItem
connection
)
{
if
(
isSupportedByServer
())
{
onUseCarbonsSettingsChanged
();
public
void
onServerInfoReceived
(
ConnectionItem
connection
)
{
if
(
isSupportedByServer
())
{
onUseCarbonsSettingsChanged
();
}
}
/**
* Sends the new state of message carbons to the server
* when this setting has been changed
*/
public
void
onUseCarbonsSettingsChanged
()
{
sendCarbonsEnabled
(
SettingsManager
.
connectionUseCarbons
());
}
/**
* Update outgoing message before sending.
* Marks the message as non-carbon-copied in the following cases:
* - Message Carbons is enabled and OTR mode is enabled.
* - Message Carbons is enabled and OTR security level != plain.
*
* @param abstractChat
* @param message the <tt>Message</tt> to be sent
* @param messageItem
*/
public
void
updateOutgoingMessage
(
AbstractChat
abstractChat
,
Message
message
,
MessageItem
messageItem
)
{
if
(!
SettingsManager
.
connectionUseCarbons
())
{
return
;
}
}
/**
* Sends the new state of message carbons to the server
* when this setting has been changed
*/
public
void
onUseCarbonsSettingsChanged
()
{
sendCarbonsEnabled
(
SettingsManager
.
connectionUseCarbons
());
}
/**
* Update outgoing message before sending.
* Marks the message as non-carbon-copied in the following cases:
* - Message Carbons is enabled and OTR mode is enabled.
* - Message Carbons is enabled and OTR security level != plain.
* @param abstractChat
* @param message the <tt>Message</tt> to be sent
* @param messageItem
*/
public
void
updateOutgoingMessage
(
AbstractChat
abstractChat
,
Message
message
,
MessageItem
messageItem
)
{
if
(!
SettingsManager
.
connectionUseCarbons
())
{
return
;
}
if
(
SettingsManager
.
securityOtrMode
()
==
SecurityOtrMode
.
disabled
)
{
return
;
}
if
(
OTRManager
.
getInstance
().
getSecurityLevel
(
abstractChat
.
getAccount
(),
abstractChat
.
getUser
())
!=
SecurityLevel
.
plain
)
{
return
;
}
message
.
addExtension
(
new
Private
());
}
public
enum
Direction
{
sent
,
received
}
if
(
SettingsManager
.
securityOtrMode
()
==
SecurityOtrMode
.
disabled
)
{
return
;
}
if
(
OTRManager
.
getInstance
().
getSecurityLevel
(
abstractChat
.
getAccount
(),
abstractChat
.
getUser
())
!=
SecurityLevel
.
plain
)
{
return
;
}
message
.
addExtension
(
new
Private
());
}
public
enum
Direction
{
sent
,
received
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment