|
|
@@ -9,8 +9,8 @@ import android.os.Handler;
|
|
|
import android.os.Message;
|
|
|
import android.util.Log;
|
|
|
|
|
|
-import com.common.system.SystemUtil;
|
|
|
import com.core.app.MyApplication;
|
|
|
+import com.core.utils.CommonUtil;
|
|
|
|
|
|
import org.apache.harmony.javax.security.sasl.SaslException;
|
|
|
import org.jivesoftware.smack.AbstractConnectionListener;
|
|
|
@@ -19,18 +19,15 @@ import org.jivesoftware.smack.SmackException;
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
-import org.jivesoftware.smack.XMPPTCPConnection;
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
+import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
import org.jivesoftware.smackx.ping.PingManager;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Random;
|
|
|
|
|
|
-/**
|
|
|
- * @desc:
|
|
|
- * @author:Arison on 2017/8/22
|
|
|
- */
|
|
|
+
|
|
|
public class XmppConnectionManager {
|
|
|
private static final String TAG = XmppConnectionManager.class.getSimpleName();
|
|
|
private Context mContext;
|
|
|
@@ -110,7 +107,7 @@ public class XmppConnectionManager {
|
|
|
// NetworkInfo wifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
// boolean isConnectedGprs = gprs != null && gprs.isConnected();
|
|
|
// boolean isConnectedWifi = wifi != null && wifi.isConnected();
|
|
|
- return SystemUtil.isNetWorkConnected(MyApplication.getInstance());
|
|
|
+ return CommonUtil.isNetWorkConnected(MyApplication.getInstance());
|
|
|
}
|
|
|
|
|
|
private BroadcastReceiver mNetWorkChangeReceiver = new BroadcastReceiver() {
|
|
|
@@ -121,7 +118,7 @@ public class XmppConnectionManager {
|
|
|
}
|
|
|
final boolean isConnected = isGprsOrWifiConnected();
|
|
|
if (mIsNetWorkActive != isConnected) {// 和之前的状态不同
|
|
|
- Log.d("roamer", "网络状态改变了 isLoginAllowed():"+isLoginAllowed());
|
|
|
+ Log.d("roamer", "网络状态改变了doLogining:"+doLogining);
|
|
|
mIsNetWorkActive = isConnected;
|
|
|
// 网络状态改变了
|
|
|
if (!mIsNetWorkActive) {// 由有网变为没网
|
|
|
@@ -129,6 +126,7 @@ public class XmppConnectionManager {
|
|
|
mLoginThread.interrupt();
|
|
|
}
|
|
|
} else {
|
|
|
+ doLogining=true;
|
|
|
if (isLoginAllowed()) {
|
|
|
login(mLoginUserId, mLoginPassword);
|
|
|
}
|
|
|
@@ -200,6 +198,12 @@ public class XmppConnectionManager {
|
|
|
private boolean doLogining = false;
|
|
|
|
|
|
private boolean isLoginAllowed() {
|
|
|
+// LogUtil.d("Xmpp","---------isLoginAllowed()------------");
|
|
|
+// LogUtil.d("Xmpp","doLogining:"+doLogining);
|
|
|
+// LogUtil.d("Xmpp","mIsNetWorkActive:"+mIsNetWorkActive);
|
|
|
+// LogUtil.d("Xmpp","!mConnection.isConnected():"+!mConnection.isConnected());
|
|
|
+// LogUtil.d("Xmpp","!mConnection.isAuthenticated():"+!mConnection.isAuthenticated());
|
|
|
+// LogUtil.d("Xmpp","---------isLoginAllowed()------------");
|
|
|
return doLogining && mIsNetWorkActive && (!mConnection.isConnected() || !mConnection.isAuthenticated());
|
|
|
}
|
|
|
|
|
|
@@ -222,11 +226,19 @@ public class XmppConnectionManager {
|
|
|
presenceOffline();
|
|
|
}
|
|
|
if (mConnection.isConnected()) {
|
|
|
- mConnection.disconnect();
|
|
|
+ disconnect();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private void disconnect() {
|
|
|
+ try {
|
|
|
+ mConnection.disconnect();
|
|
|
+ } catch (NotConnectedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public synchronized void login(final String userId, final String password) {
|
|
|
Log.d("roamer", "login start");
|
|
|
if (mConnection.isAuthenticated()) {// 如果已经登陆
|
|
|
@@ -234,7 +246,7 @@ public class XmppConnectionManager {
|
|
|
if (StringUtils.parseName(mConnection.getUser()).equals(userId)) {// 如果登陆的用户和需要在登陆的是同一个用户,赋予可能改变的用户名和密码,返回
|
|
|
return;
|
|
|
} else {
|
|
|
- mConnection.disconnect();
|
|
|
+ disconnect();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -281,7 +293,7 @@ public class XmppConnectionManager {
|
|
|
}
|
|
|
mReconnectionManager.release();
|
|
|
if (mConnection != null && mConnection.isConnected()) {
|
|
|
- mConnection.disconnect();
|
|
|
+ disconnect();
|
|
|
}
|
|
|
presenceOffline();
|
|
|
}
|
|
|
@@ -309,7 +321,7 @@ public class XmppConnectionManager {
|
|
|
|
|
|
/**
|
|
|
* Returns the number of seconds until the next reconnection attempt.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return the number of seconds until the next reconnection attempt.
|
|
|
*/
|
|
|
private int timeDelay() {
|
|
|
@@ -329,7 +341,8 @@ public class XmppConnectionManager {
|
|
|
Log.d("roamer", "login try");
|
|
|
try {
|
|
|
if (!mConnection.isConnected()) {
|
|
|
- ((XMPPTCPConnection) mConnection).connectWithoutLogin();
|
|
|
+ //((XMPPTCPConnection) mConnection).connectWithoutLogin();
|
|
|
+ mConnection.connect();
|
|
|
}
|
|
|
if (mConnection.isConnected()) {
|
|
|
PingManager.getInstanceFor(mConnection).setPingInterval(60);
|
|
|
@@ -350,7 +363,7 @@ public class XmppConnectionManager {
|
|
|
|
|
|
if (mConnection.isAuthenticated()) {
|
|
|
if (!StringUtils.parseName(mConnection.getUser()).equals(loginUserId)) {
|
|
|
- mConnection.disconnect();
|
|
|
+ disconnect();
|
|
|
} else {
|
|
|
doLogining = false;
|
|
|
mAbstractConnectionListener.authenticated(mConnection);
|
|
|
@@ -376,7 +389,7 @@ public class XmppConnectionManager {
|
|
|
|
|
|
|
|
|
public void handOfflineMessage() {
|
|
|
- Log.d("roamer","这里是去获取离线消息handofflineMessage");
|
|
|
+ Log.d("roamer","这里是去获取离线消息handofflineMessage");
|
|
|
new Thread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
@@ -386,7 +399,7 @@ public class XmppConnectionManager {
|
|
|
// OfflineMessageManager manager = new OfflineMessageManager(mConnection);
|
|
|
// manager.getMessages();
|
|
|
// manager.deleteMessages();
|
|
|
- presenceOnline();// 取得离线消息后,发送在线消息状态
|
|
|
+ presenceOnline();// 取得离线消息后,发送在线消息状态
|
|
|
// } catch (NoResponseException e) {
|
|
|
// Log.d("roamer", "NoResponseException");
|
|
|
// e.printStackTrace();
|