diff --git a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnection.java b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnection.java index b0b07b8..66a1a03 100644 --- a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnection.java +++ b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnection.java @@ -32,6 +32,8 @@ import org.jivesoftware.smackx.receipts.DeliveryReceiptRequest; import org.jivesoftware.smackx.xdata.form.FillableForm; import org.jivesoftware.smackx.xdata.form.Form; +import org.jivesoftware.smackx.iqregister.AccountManager; +import org.jxmpp.jid.parts.Localpart; import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.impl.JidCreate; @@ -68,12 +70,16 @@ public class FlutterXmppConnection implements ConnectionListener { private static String mServiceName = ""; private static XMPPTCPConnection mConnection; private static MultiUserChatManager multiUserChatManager; - private static boolean mRequireSSLConnection, mAutoDeliveryReceipt, mAutomaticReconnection = true, mUseStreamManagement = true; + private static boolean mRequireSSLConnection, mAutoDeliveryReceipt, mAutomaticReconnection = true, + mUseStreamManagement = true, mRegisterUser = false; private static Context mApplicationContext; - private BroadcastReceiver uiThreadMessageReceiver;//Receives messages from the ui thread. + private BroadcastReceiver uiThreadMessageReceiver;// Receives messages from the ui thread. + private FlutterXmppConnectionService mService; - public FlutterXmppConnection(Context context, String jid_user, String password, String host, Integer port, boolean requireSSLConnection, - boolean autoDeliveryReceipt, boolean useStreamManagement, boolean automaticReconnection) { + public FlutterXmppConnection(Context context, String jid_user, String password, String host, Integer port, + boolean requireSSLConnection, + boolean autoDeliveryReceipt, boolean useStreamManagement, boolean automaticReconnection, + boolean registerUser, FlutterXmppConnectionService service) { Utils.printLog(" Connection Constructor called: "); @@ -85,6 +91,8 @@ public FlutterXmppConnection(Context context, String jid_user, String password, mAutoDeliveryReceipt = autoDeliveryReceipt; mUseStreamManagement = useStreamManagement; mAutomaticReconnection = automaticReconnection; + mRegisterUser = registerUser; + mService = service; if (jid_user != null && jid_user.contains(Constants.SYMBOL_COMPARE_JID)) { String[] jid_list = jid_user.split(Constants.SYMBOL_COMPARE_JID); mUsername = jid_list[0]; @@ -107,7 +115,8 @@ public static XMPPTCPConnection getConnection() { return mConnection == null ? new XMPPTCPConnection(null) : mConnection; } - public static void sendCustomMessage(String body, String toJid, String msgId, String customText, boolean isDm, String time) { + public static void sendCustomMessage(String body, String toJid, String msgId, String customText, boolean isDm, + String time) { try { @@ -121,11 +130,13 @@ public static void sendCustomMessage(String body, String toJid, String msgId, St DeliveryReceiptRequest.addTo(xmppMessage); } - StandardExtensionElement timeElement = StandardExtensionElement.builder(Constants.TIME, Constants.URN_XMPP_TIME) + StandardExtensionElement timeElement = StandardExtensionElement + .builder(Constants.TIME, Constants.URN_XMPP_TIME) .addElement(Constants.TS, time).build(); xmppMessage.addExtension(timeElement); - StandardExtensionElement element = StandardExtensionElement.builder(Constants.CUSTOM, Constants.URN_XMPP_CUSTOM) + StandardExtensionElement element = StandardExtensionElement + .builder(Constants.CUSTOM, Constants.URN_XMPP_CUSTOM) .addElement(Constants.custom, customText).build(); xmppMessage.addExtension(element); @@ -170,7 +181,8 @@ public static void send_delivery_receipt(String toJid, String msgId, String rece mConnection.sendStanza(deliveryMessage); - Utils.addLogInStorage("Action: sentDeliveryReceiptToServer, Content: " + deliveryMessage.toXML().toString()); + Utils.addLogInStorage( + "Action: sentDeliveryReceiptToServer, Content: " + deliveryMessage.toXML().toString()); } catch (Exception e) { e.printStackTrace(); @@ -182,7 +194,8 @@ public static void manageAddMembersInGroup(GroupRole groupRole, String groupName try { List jidList = new ArrayList<>(); - MultiUserChat muc = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat muc = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); for (String memberJid : membersJid) { if (!memberJid.contains(mHost)) { @@ -221,7 +234,8 @@ public static void manageRemoveFromGroup(GroupRole groupRole, String groupName, jidList.add(jid); } - MultiUserChat muc = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat muc = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); if (groupRole == GroupRole.ADMIN) { for (Jid jid : jidList) { @@ -242,7 +256,8 @@ public static List getMembersOrAdminsOrOwners(GroupRole groupRole, Strin try { List affiliates; - MultiUserChat muc = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat muc = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); if (groupRole == GroupRole.ADMIN) { affiliates = muc.getAdmins(); } else if (groupRole == GroupRole.MEMBER) { @@ -268,7 +283,8 @@ public static List getMembersOrAdminsOrOwners(GroupRole groupRole, Strin public static int getOnlineMemberCount(String groupName) { try { - MultiUserChat muc = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat muc = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); return muc.getOccupants().size(); } catch (Exception e) { @@ -281,7 +297,8 @@ public static long getLastSeen(String userJid) { long userLastActivity = Constants.RESULT_DEFAULT; try { LastActivityManager lastActivityManager = LastActivityManager.getInstanceFor(mConnection); - LastActivity lastActivity = lastActivityManager.getLastActivity(JidCreate.from(Utils.getJidWithDomainName(userJid, mHost))); + LastActivity lastActivity = lastActivityManager + .getLastActivity(JidCreate.from(Utils.getJidWithDomainName(userJid, mHost))); userLastActivity = lastActivity.lastActivity; } catch (Exception e) { e.printStackTrace(); @@ -289,7 +306,6 @@ public static long getLastSeen(String userJid) { return userLastActivity; } - public static List getMyRosters() { List muRosterList = new ArrayList<>(); try { @@ -305,8 +321,10 @@ public static List getMyRosters() { public static void createRosterEntry(String userJid) { try { -// rosterConnection.createEntry(JidCreate.bareFrom(Utils.getJidWithDomainName(userJid, mHost)), userJid, null); - rosterConnection.createItemAndRequestSubscription(JidCreate.bareFrom(Utils.getJidWithDomainName(userJid, mHost)), userJid, null); + // rosterConnection.createEntry(JidCreate.bareFrom(Utils.getJidWithDomainName(userJid, + // mHost)), userJid, null); + rosterConnection.createItemAndRequestSubscription( + JidCreate.bareFrom(Utils.getJidWithDomainName(userJid, mHost)), userJid, null); } catch (Exception e) { e.printStackTrace(); @@ -318,16 +336,17 @@ public static boolean createMUC(String groupName, String persistent) { boolean isGroupCreatedSuccessfully = false; try { - MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat multiUserChat = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); multiUserChat.create(Resourcepart.from(mUsername)); -// if (persistent.equals(Constants.TRUE)) { -// Form form = multiUserChat.getConfigurationForm(); -// Form answerForm = form.createAnswerForm(); -// answerForm.setAnswer(Constants.MUC_PERSISTENT_ROOM, true); -// answerForm.setAnswer(Constants.MUC_MEMBER_ONLY, true); -// multiUserChat.sendConfigurationForm(answerForm); -// } + // if (persistent.equals(Constants.TRUE)) { + // Form form = multiUserChat.getConfigurationForm(); + // Form answerForm = form.createAnswerForm(); + // answerForm.setAnswer(Constants.MUC_PERSISTENT_ROOM, true); + // answerForm.setAnswer(Constants.MUC_MEMBER_ONLY, true); + // multiUserChat.sendConfigurationForm(answerForm); + // } if (persistent.equals(Constants.TRUE)) { Form form = multiUserChat.getConfigurationForm(); FillableForm answerForm = form.getFillableForm(); @@ -342,7 +361,8 @@ public static boolean createMUC(String groupName, String persistent) { e.printStackTrace(); String groupCreateError = e.getLocalizedMessage(); Utils.printLog(" createMUC : exception: " + groupCreateError); - Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_CREATION_FAILED, groupCreateError, groupName); + Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_CREATION_FAILED, + groupCreateError, groupName); } return isGroupCreatedSuccessfully; @@ -362,11 +382,12 @@ public static String joinAllGroups(ArrayList allGroupsIds) { lastMsgTime = groupData[1]; } - MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat( + (EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); Resourcepart resourcepart = Resourcepart.from(mUsername); long currentTime = new Date().getTime(); -// long lastMessageTime = Long.valueOf(lastMsgTime); + // long lastMessageTime = Long.valueOf(lastMsgTime); long lastMessageTime = Long.parseLong(lastMsgTime); long diff = currentTime - lastMessageTime; @@ -381,7 +402,8 @@ public static String joinAllGroups(ArrayList allGroupsIds) { e.printStackTrace(); String allGroupJoinError = e.getLocalizedMessage(); Utils.printLog(" joinAllGroup : exception: " + allGroupJoinError); - Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_JOINED_FAILED, allGroupJoinError, groupId); + Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_JOINED_FAILED, + allGroupJoinError, groupId); } } @@ -403,11 +425,12 @@ public static boolean joinGroupWithResponse(String groupId) { lastMsgTime = groupData[1]; } - MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); + MultiUserChat multiUserChat = multiUserChatManager + .getMultiUserChat((EntityBareJid) JidCreate.from(Utils.getRoomIdWithDomainName(groupName, mHost))); Resourcepart resourcepart = Resourcepart.from(mUsername); long currentTime = new Date().getTime(); -// long lastMessageTime = Long.valueOf(lastMsgTime); + // long lastMessageTime = Long.valueOf(lastMsgTime); long lastMessageTime = Long.parseLong(lastMsgTime); long diff = currentTime - lastMessageTime; @@ -423,7 +446,8 @@ public static boolean joinGroupWithResponse(String groupId) { } catch (Exception e) { String groupJoinError = e.getLocalizedMessage(); Utils.printLog(" joinGroup : exception: " + groupJoinError); - Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_JOINED_FAILED, groupJoinError, groupId); + Utils.broadcastErrorMessageToFlutter(mApplicationContext, ErrorState.GROUP_JOINED_FAILED, groupJoinError, + groupId); e.printStackTrace(); } @@ -489,17 +513,15 @@ public void connect() throws IOException, XMPPException, SmackException { conf.setPort(Constants.PORT_NUMBER); } - conf.setUsernameAndPassword(mUsername, mPassword); conf.setResource(mResource); conf.setCompressionEnabled(true); conf.enableDefaultDebugger(); - if (mRequireSSLConnection) { SSLContext context = null; try { context = SSLContext.getInstance(Constants.TLS); - context.init(null, new TrustManager[]{new TLSUtils.AcceptAllTrustManager()}, new SecureRandom()); + context.init(null, new TrustManager[] { new TLSUtils.AcceptAllTrustManager() }, new SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { @@ -512,19 +534,45 @@ public void connect() throws IOException, XMPPException, SmackException { conf.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); } - Utils.printLog(" connect 1 mServiceName: " + mServiceName + " mHost: " + mHost + " mPort: " + Constants.PORT + " mUsername: " + mUsername + " mPassword: " + mPassword + " mResource:" + mResource); - //Set up the ui thread broadcast message receiver. - + Utils.printLog(" connect 1 mServiceName: " + mServiceName + " mHost: " + mHost + " mPort: " + Constants.PORT + + " mUsername: " + mUsername + " mPassword: " + mPassword + " mResource:" + mResource + + " registerUser: " + + mRegisterUser); + // Set up the ui thread broadcast message receiver. try { mConnection = new XMPPTCPConnection(conf.build()); mConnection.addConnectionListener(this); - - Utils.printLog(" Calling connect(): "); mConnection.connect(); + if (mRegisterUser) { + try { + // Registering the user + AccountManager accountManager = AccountManager.getInstance(mConnection); + if (!mRequireSSLConnection) { + accountManager.sensitiveOperationOverInsecureConnection(true); + } + accountManager.createAccount(Localpart.from(mUsername), mPassword); + FlutterXmppConnectionService.sConnectionState = ConnectionState.DISCONNECTED; + Utils.broadcastConnectionMessageToFlutter(mApplicationContext, ConnectionState.DISCONNECTED, ""); + mConnection.disconnect(); + mService.stop(); + Utils.printLog(" User registered successfully."); + return; + } catch (Exception e) { + e.printStackTrace(); + Utils.broadcastConnectionMessageToFlutter(mApplicationContext, ConnectionState.FAILED, + e.getLocalizedMessage()); + Utils.printLog(" User registration failed."); + mConnection.disconnect(); + mService.stop(); + FlutterXmppConnectionService.sConnectionState = ConnectionState.FAILED; + return; + } + + } rosterConnection = Roster.getInstanceFor(mConnection); rosterConnection.setSubscriptionMode(Roster.SubscriptionMode.accept_all); @@ -534,11 +582,12 @@ public void connect() throws IOException, XMPPException, SmackException { mConnection.setUseStreamManagementResumption(true); } - mConnection.login(); + mConnection.login(mUsername, mPassword); setupUiThreadBroadCastMessageReceiver(); - mConnection.addSyncStanzaListener(new PresenceListenerAndFilter(mApplicationContext), StanzaTypeFilter.PRESENCE); + mConnection.addSyncStanzaListener(new PresenceListenerAndFilter(mApplicationContext), + StanzaTypeFilter.PRESENCE); mConnection.addStanzaAcknowledgedListener(new StanzaAckListener(mApplicationContext)); @@ -550,7 +599,6 @@ public void connect() throws IOException, XMPPException, SmackException { reconnectionManager.enableAutomaticReconnection(); } - } catch (InterruptedException e) { FlutterXmppConnectionService.sConnectionState = ConnectionState.FAILED; e.printStackTrace(); @@ -564,12 +612,12 @@ private void setupUiThreadBroadCastMessageReceiver() { @Override public void onReceive(Context context, Intent intent) { - //Check if the Intents purpose is to send the message. + // Check if the Intents purpose is to send the message. String action = intent.getAction(); Utils.printLog(" action: " + action); if (action.equals(Constants.X_SEND_MESSAGE) || action.equals(Constants.GROUP_SEND_MESSAGE)) { - //Send the message. + // Send the message. sendMessage(intent.getStringExtra(Constants.BUNDLE_MESSAGE_BODY), intent.getStringExtra(Constants.BUNDLE_TO), intent.getStringExtra(Constants.BUNDLE_MESSAGE_PARAMS), @@ -598,7 +646,8 @@ private void sendMessage(String body, String toJid, String msgId, boolean isDm, xmppMessage.setBody(body); xmppMessage.setType(isDm ? Message.Type.chat : Message.Type.groupchat); - StandardExtensionElement timeElement = StandardExtensionElement.builder(Constants.TIME, Constants.URN_XMPP_TIME) + StandardExtensionElement timeElement = StandardExtensionElement + .builder(Constants.TIME, Constants.URN_XMPP_TIME) .addElement(Constants.TS, time).build(); xmppMessage.addExtension(timeElement); @@ -606,16 +655,17 @@ private void sendMessage(String body, String toJid, String msgId, boolean isDm, DeliveryReceiptRequest.addTo(xmppMessage); } -// if (isDm) { -// xmppMessage.setTo(JidCreate.from(toJid)); -// mConnection.sendStanza(xmppMessage); -// } else { -// EntityBareJid jid = JidCreate.entityBareFrom(toJid); -// xmppMessage.setTo(jid); -// EntityBareJid mucJid = (EntityBareJid) JidCreate.bareFrom(Utils.getRoomIdWithDomainName(toJid, mHost)); -// MultiUserChat muc = multiUserChatManager.getMultiUserChat(mucJid); -// muc.sendMessage(xmppMessage); -// } + // if (isDm) { + // xmppMessage.setTo(JidCreate.from(toJid)); + // mConnection.sendStanza(xmppMessage); + // } else { + // EntityBareJid jid = JidCreate.entityBareFrom(toJid); + // xmppMessage.setTo(jid); + // EntityBareJid mucJid = (EntityBareJid) + // JidCreate.bareFrom(Utils.getRoomIdWithDomainName(toJid, mHost)); + // MultiUserChat muc = multiUserChatManager.getMultiUserChat(mucJid); + // muc.sendMessage(xmppMessage); + // } if (isDm) { xmppMessage.setTo(JidCreate.from(toJid)); mConnection.sendStanza(xmppMessage); @@ -664,7 +714,7 @@ public void connected(XMPPConnection connection) { Utils.broadcastConnectionMessageToFlutter(mApplicationContext, ConnectionState.CONNECTED, ""); - //Bundle up the intent and send the broadcast. + // Bundle up the intent and send the broadcast. Intent intent = new Intent(Constants.RECEIVE_MESSAGE); intent.setPackage(mApplicationContext.getPackageName()); intent.putExtra(Constants.BUNDLE_FROM_JID, mUsername); @@ -681,11 +731,11 @@ public void authenticated(XMPPConnection connection, boolean resumed) { multiUserChatManager = MultiUserChatManager.getInstanceFor(connection); FlutterXmppConnectionService.sConnectionState = ConnectionState.AUTHENTICATED; -// showContactListActivityWhenAuthenticated(); + // showContactListActivityWhenAuthenticated(); Utils.broadcastConnectionMessageToFlutter(mApplicationContext, ConnectionState.AUTHENTICATED, ""); - //Bundle up the intent and send the broadcast. + // Bundle up the intent and send the broadcast. Intent intent = new Intent(Constants.RECEIVE_MESSAGE); intent.setPackage(mApplicationContext.getPackageName()); intent.putExtra(Constants.BUNDLE_FROM_JID, mUsername); @@ -700,7 +750,7 @@ public void authenticated(XMPPConnection connection, boolean resumed) { public void connectionClosed() { Utils.printLog(" ConnectionClosed(): "); - if(FlutterXmppConnectionService.sConnectionState == ConnectionState.FAILED) { + if (FlutterXmppConnectionService.sConnectionState == ConnectionState.FAILED) { connectionCloseMessageToFlutter(ConnectionState.FAILED, Constants.FAILED); } else { FlutterXmppConnectionService.sConnectionState = ConnectionState.DISCONNECTED; @@ -709,11 +759,11 @@ public void connectionClosed() { } void connectionCloseMessageToFlutter(ConnectionState connectionState, String connection) { - if(connectionState != ConnectionState.FAILED) { + if (connectionState != ConnectionState.FAILED) { Utils.broadcastConnectionMessageToFlutter(mApplicationContext, connectionState, ""); } - //Bundle up the intent and send the broadcast. + // Bundle up the intent and send the broadcast. Intent intent = new Intent(Constants.RECEIVE_MESSAGE); intent.setPackage(mApplicationContext.getPackageName()); intent.putExtra(Constants.BUNDLE_FROM_JID, connection); @@ -732,7 +782,7 @@ public void connectionClosedOnError(Exception e) { Utils.broadcastConnectionMessageToFlutter(mApplicationContext, ConnectionState.FAILED, e.getLocalizedMessage()); - //Bundle up the intent and send the broadcast. + // Bundle up the intent and send the broadcast. Intent intent = new Intent(Constants.RECEIVE_MESSAGE); intent.setPackage(mApplicationContext.getPackageName()); intent.putExtra(Constants.BUNDLE_FROM_JID, Constants.DISCONNECTED); diff --git a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnectionService.java b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnectionService.java index 04a7198..4c8fce8 100644 --- a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnectionService.java +++ b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Connection/FlutterXmppConnectionService.java @@ -27,7 +27,8 @@ public class FlutterXmppConnectionService extends Service { private Handler mTHandler; private String jid_user = ""; private String password = ""; - private boolean requireSSLConnection = false, autoDeliveryReceipt = false, useStreamManagement = true, automaticReconnection = true; + private boolean requireSSLConnection = false, autoDeliveryReceipt = false, useStreamManagement = true, + automaticReconnection = true, registerUser = false; private FlutterXmppConnection mConnection; public FlutterXmppConnectionService() { @@ -66,15 +67,19 @@ private void initConnection() { Utils.printLog(" initConnection(): "); if (mConnection == null) { - mConnection = new FlutterXmppConnection(this, this.jid_user, this.password, this.host, this.port, requireSSLConnection, autoDeliveryReceipt, useStreamManagement, automaticReconnection); + mConnection = new FlutterXmppConnection(this, this.jid_user, this.password, this.host, this.port, + requireSSLConnection, autoDeliveryReceipt, useStreamManagement, automaticReconnection, + registerUser, this); } mConnection.connect(); } catch (IOException | SmackException | XMPPException e) { FlutterXmppConnectionService.sConnectionState = ConnectionState.FAILED; - Utils.broadcastConnectionMessageToFlutter(this, ConnectionState.FAILED, "Something went wrong while connecting ,make sure the credentials are right and try again."); - Utils.printLog(" Something went wrong while connecting ,make sure the credentials are right and try again: "); + Utils.broadcastConnectionMessageToFlutter(this, ConnectionState.FAILED, + "Something went wrong while connecting ,make sure the credentials are right and try again."); + Utils.printLog( + " Something went wrong while connecting ,make sure the credentials are right and try again: "); e.printStackTrace(); stopSelf(); } @@ -82,7 +87,7 @@ private void initConnection() { public void start() { - Utils.printLog(" Service Start() function called: "); + Utils.printLog(" Service Start() function called: " + mActive); if (!mActive) { mActive = true; @@ -104,15 +109,20 @@ public void stop() { Utils.printLog(" stop() :"); mActive = false; + if (mThread.isAlive() && mThread != null) { + mThread.interrupt(); + mThread = null; + } if (mTHandler != null) { mTHandler.post(() -> { if (mConnection != null) { mConnection.disconnect(); + mConnection = null; } }); } - } + } @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -134,6 +144,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { this.autoDeliveryReceipt = extras.getBoolean(Constants.AUTO_DELIVERY_RECEIPT, false); this.useStreamManagement = extras.getBoolean(Constants.USER_STREAM_MANAGEMENT, true); this.automaticReconnection = extras.getBoolean(Constants.AUTOMATIC_RECONNECTION, true); + this.registerUser = extras.getBoolean(Constants.REGISTER_USER, false); } start(); @@ -148,4 +159,3 @@ public void onDestroy() { stop(); } } - diff --git a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/FlutterXmppPlugin.java b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/FlutterXmppPlugin.java index 39248bc..ffa4e9a 100644 --- a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/FlutterXmppPlugin.java +++ b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/FlutterXmppPlugin.java @@ -55,7 +55,7 @@ public class FlutterXmppPlugin implements MethodCallHandler, FlutterPlugin, Acti private BroadcastReceiver successBroadcastReceiver = null; private BroadcastReceiver errorBroadcastReceiver = null; private BroadcastReceiver connectionBroadcastReceiver = null; - private boolean requireSSLConnection = false, autoDeliveryReceipt = false, automaticReconnection = true, useStreamManagement = true; + private boolean requireSSLConnection = false, autoDeliveryReceipt = false, automaticReconnection = true, useStreamManagement = true, registerUser = false; // public static void registerWith(Registrar registrar) { // @@ -463,6 +463,7 @@ public void onMethodCall(MethodCall call, Result result) { jid_user = call.argument(Constants.USER_JID).toString(); password = call.argument(Constants.PASSWORD).toString(); + Utils.printLog("jid_user: " + jid_user + " password: " + password); host = call.argument(Constants.HOST).toString(); if (call.hasArgument(Constants.PORT)) { Constants.PORT_NUMBER = Integer.parseInt(call.argument(Constants.PORT).toString()); @@ -488,6 +489,10 @@ public void onMethodCall(MethodCall call, Result result) { useStreamManagement = call.argument(Constants.USER_STREAM_MANAGEMENT); } + if (call.hasArgument(Constants.REGISTER_USER)) { + registerUser = call.argument(Constants.REGISTER_USER); + } + // Start authentication. doLogin(); @@ -786,7 +791,9 @@ public void onMethodCall(MethodCall call, Result result) { // login private void doLogin() { // Check if the user is already connected or not ? if not then start login process. - if (FlutterXmppConnectionService.getState().equals(ConnectionState.DISCONNECTED)) { + Log.d("TAG", "doLogin: " + FlutterXmppConnectionService.getState()); + if (FlutterXmppConnectionService.getState().equals(ConnectionState.DISCONNECTED) || + FlutterXmppConnectionService.getState().equals(ConnectionState.FAILED)) { Intent i = new Intent(activity, FlutterXmppConnectionService.class); i.putExtra(Constants.JID_USER, jid_user); i.putExtra(Constants.PASSWORD, password); @@ -796,6 +803,7 @@ private void doLogin() { i.putExtra(Constants.REQUIRE_SSL_CONNECTION, requireSSLConnection); i.putExtra(Constants.USER_STREAM_MANAGEMENT, useStreamManagement); i.putExtra(Constants.AUTOMATIC_RECONNECTION, automaticReconnection); + i.putExtra(Constants.REGISTER_USER, registerUser); activity.startService(i); } } diff --git a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Constants.java b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Constants.java index 7bd2393..51787be 100644 --- a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Constants.java +++ b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Constants.java @@ -122,6 +122,7 @@ public class Constants { public static final String PASSWORD = "password"; public static final String SEND_MESSAGE = "send_message"; public static final String SEND_GROUP_MESSAGE = "send_group_message"; + public static final String REGISTER_USER = "register_user"; public static final String LOGIN = "login"; public static final String TO_JID = "to_jid"; public static final String TO_JID_1 = "toJid"; diff --git a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Utils.java b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Utils.java index 120e1dc..00efb27 100644 --- a/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Utils.java +++ b/android/src/main/java/org/xrstudio/xmpp/flutter_xmpp/Utils/Utils.java @@ -70,6 +70,7 @@ public static long getLongDate() { } public static String getJidWithDomainName(String jid, String host) { + Log.d(Constants.TAG, "jid: " + jid + ", host: " + host); return jid.contains(host) ? jid : jid + Constants.SYMBOL_COMPARE_JID + host; } diff --git a/example/lib/main.dart b/example/lib/main.dart index b2f269b..9789222 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -85,6 +85,7 @@ class _MyAppState extends State "autoDeliveryReceipt": false, "useStreamManagement": false, "automaticReconnection": true, + "register": true }; flutterXmpp = XmppConnection(auth); diff --git a/example/pubspec.lock b/example/pubspec.lock index 25feecd..227161e 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.18.0" cross_file: dependency: transitive description: @@ -101,10 +101,10 @@ packages: dependency: transitive description: name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" url: "https://pub.dev" source: hosted - version: "6.1.4" + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -151,30 +151,54 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.7" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" matcher: dependency: transitive description: name: matcher - sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.15" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.11.0" mime: dependency: transitive description: @@ -187,10 +211,10 @@ packages: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" path_provider: dependency: "direct main" description: @@ -283,10 +307,10 @@ packages: dependency: transitive description: name: platform - sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76" + sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec" url: "https://pub.dev" source: hosted - version: "3.1.0" + version: "3.1.4" plugin_platform_interface: dependency: transitive description: @@ -299,10 +323,10 @@ packages: dependency: transitive description: name: process - sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09" + sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32" url: "https://pub.dev" source: hosted - version: "4.2.4" + version: "5.0.2" share_plus: dependency: "direct main" description: @@ -347,10 +371,10 @@ packages: dependency: transitive description: name: shared_preferences_linux - sha256: "71d6806d1449b0a9d4e85e0c7a917771e672a3d5dc61149cc9fac871115018e1" + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" url: "https://pub.dev" source: hosted - version: "2.3.0" + version: "2.3.2" shared_preferences_platform_interface: dependency: transitive description: @@ -371,10 +395,10 @@ packages: dependency: transitive description: name: shared_preferences_windows - sha256: f95e6a43162bce43c9c3405f3eb6f39e5b5d11f65fab19196cf8225e2777624d + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" url: "https://pub.dev" source: hosted - version: "2.3.0" + version: "2.3.2" sky_engine: dependency: transitive description: flutter @@ -384,26 +408,26 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -432,10 +456,10 @@ packages: dependency: transitive description: name: test_api - sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "0.6.1" typed_data: dependency: transitive description: @@ -496,18 +520,18 @@ packages: dependency: transitive description: name: vm_service - sha256: f6deed8ed625c52864792459709183da231ebf66ff0cf09e69b573227c377efe + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "11.3.0" + version: "13.0.0" webdriver: dependency: transitive description: name: webdriver - sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49" + sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e" url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "3.0.3" win32: dependency: transitive description: @@ -538,7 +562,7 @@ packages: path: ".." relative: true source: path - version: "2.2.10" + version: "2.2.12" sdks: - dart: ">=3.0.0 <4.0.0" - flutter: ">=3.3.0" + dart: ">=3.2.0-0 <4.0.0" + flutter: ">=3.7.0" diff --git a/lib/ennums/typing_status.dart b/lib/ennums/typing_status.dart index 9abcce0..c9ee9e2 100644 --- a/lib/ennums/typing_status.dart +++ b/lib/ennums/typing_status.dart @@ -1,7 +1,7 @@ enum TypingStatus { - COMPOSING, - PAUSED, - ACTIVE, - GONE, - INACTIVE; + composing, + paused, + active, + gone, + inactive, } diff --git a/lib/models/chat_state_model.dart b/lib/models/chat_state_model.dart index 229a4c0..5092b78 100644 --- a/lib/models/chat_state_model.dart +++ b/lib/models/chat_state_model.dart @@ -1,4 +1,5 @@ import '../ennums/typing_status.dart'; +import 'package:collection/collection.dart'; class ChatState { String? from; @@ -35,7 +36,8 @@ class ChatState { id: eventData['id'] ?? '', type: eventData['type'] ?? '', msgtype: eventData['msgtype'] ?? '', - chatStateType: eventData['chatStateType'] ?? '', + chatStateType: TypingStatus.values.firstWhereOrNull( + (e) => e.name == (eventData['chatStateType'] ?? '')) ); } } diff --git a/pubspec.lock b/pubspec.lock index 6103d34..4a6f2f1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.18.0" fake_async: dependency: transitive description: @@ -59,38 +59,30 @@ packages: description: flutter source: sdk version: "0.0.0" - js: - dependency: transitive - description: - name: js - sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 - url: "https://pub.dev" - source: hosted - version: "0.6.7" matcher: dependency: transitive description: name: matcher - sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" url: "https://pub.dev" source: hosted - version: "0.12.15" + version: "0.12.16" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 + sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.5.0" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" path: dependency: transitive description: @@ -108,26 +100,26 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -148,10 +140,10 @@ packages: dependency: transitive description: name: test_api - sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "0.6.1" vector_math: dependency: transitive description: @@ -160,6 +152,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + web: + dependency: transitive + description: + name: web + sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + url: "https://pub.dev" + source: hosted + version: "0.3.0" sdks: - dart: ">=3.0.0-0 <4.0.0" + dart: ">=3.2.0-194.0.dev <4.0.0" flutter: ">=2.5.0"