Add other user in friend list when connecting each other.

This commit is contained in:
Chteufleur 2019-12-01 22:35:15 +01:00
parent 4c41b3c41f
commit 0941e945f6
5 changed files with 116 additions and 66 deletions

View File

@ -599,7 +599,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (data.hasExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT)) { if (data.hasExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT)) {
final String text = data.getStringExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT); final String text = data.getStringExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT);
try { try {
serviceTrackingDog.setOtherJid(text); serviceTrackingDog.sendCommandConnexion(text);
boolean isInRoster = serviceTrackingDog.isInRoster(text); boolean isInRoster = serviceTrackingDog.isInRoster(text);
if (!isInRoster) { if (!isInRoster) {
@ -639,8 +639,8 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
dialog.show(); dialog.show();
} }
Log.i(TAG, "TEXT: "+text); Log.i(TAG, "TEXT: "+text);
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) { } catch (XmppStringprepException e) {
Log.e(TAG, "Fail send presence", e); Log.e(TAG, "Fail send connexion command", e);
Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show();
} }
} }
@ -656,10 +656,10 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
friendJid = serviceTrackingDog.getFullJidByName(selectedFriend); friendJid = serviceTrackingDog.getFullJidByName(selectedFriend);
} }
try { try {
serviceTrackingDog.setOtherJid(friendJid); serviceTrackingDog.sendCommandConnexion(friendJid);
Log.i(TAG, "Connexion to: "+selectedFriend); Log.i(TAG, "Connexion to: "+selectedFriend);
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) { } catch (XmppStringprepException e) {
Log.e(TAG, "Fail send presence", e); Log.e(TAG, "Fail send connexion command", e);
Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show();
} }
} }
@ -950,14 +950,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setChecked(serviceTrackingDog.isXmppRealTimeMode()); navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setChecked(serviceTrackingDog.isXmppRealTimeMode());
} }
}); });
} else if (notification.isAction(ServiceXmpp.NOTIF_NEW_PRESENCE_RECEIVED)) {
final String _jid = (String) notification.getExtra(ServiceXmpp.NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, "Connexion avec "+_jid, Toast.LENGTH_LONG).show();
}
});
} else if (notification.isAction(ServiceXmpp.NOTIF_NEW_PRESENCE_SUBSCRIPTION)) { } else if (notification.isAction(ServiceXmpp.NOTIF_NEW_PRESENCE_SUBSCRIPTION)) {
final String _jid = (String) notification.getExtra(ServiceXmpp.NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID); final String _jid = (String) notification.getExtra(ServiceXmpp.NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID);
final AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog.Builder builder = new AlertDialog.Builder(this);

View File

@ -0,0 +1,57 @@
package fr.chteufleur.mytrackingdog.models.xmpp.commands;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.commands.AdHocCommandNote;
import org.jivesoftware.smackx.commands.LocalCommand;
import org.jivesoftware.smackx.xdata.Form;
import org.jxmpp.jid.Jid;
import fr.chteufleur.mytrackingdog.QRCodeGeneratorActivity;
import fr.chteufleur.mytrackingdog.services.ServiceXmpp;
public class ConnexionCommand extends LocalCommand {
private final ServiceXmpp serviceXmpp;
private Jid jid = null;
public ConnexionCommand(ServiceXmpp serviceXmpp) {
this.serviceXmpp = serviceXmpp;
}
@Override
public boolean isLastStage() {
return true;
}
@Override
public boolean hasPermission(Jid jid) {
this.jid = jid;
return true;
}
@Override
public void execute() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
if (serviceXmpp != null) {
serviceXmpp.setOtherJid(jid.toString());
}
this.addNote(new AdHocCommandNote(AdHocCommandNote.Type.info, "SUCCESS"));
QRCodeGeneratorActivity.pressBackButton();
}
@Override
public void next(Form response) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
}
@Override
public void complete(Form response) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
}
@Override
public void prev() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
}
@Override
public void cancel() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
}
}

View File

@ -0,0 +1,23 @@
package fr.chteufleur.mytrackingdog.models.xmpp.commands.send;
import android.util.Log;
import org.jivesoftware.smackx.commands.AdHocCommandManager;
import org.jivesoftware.smackx.commands.RemoteCommand;
import org.jxmpp.jid.FullJid;
import fr.chteufleur.mytrackingdog.services.ServiceXmpp;
public class SendConnexionCommand extends SendCommand {
public SendConnexionCommand(AdHocCommandManager commandManager, FullJid to) {
super(commandManager, to);
}
@Override
public void executeCommand() throws Exception {
Log.i(TAG, "Send command connexion");
RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_CONNEXION);
command.execute();
}
}

View File

@ -284,8 +284,6 @@ public class ServiceTrackingDog implements Observer {
currentLocationProvider = LocationProvider.XMPP; currentLocationProvider = LocationProvider.XMPP;
} else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) { } else if (notification.isAction(ServiceXmpp.NOTIF_STOP_TRAIL)) {
currentLocationProvider = LocationProvider.GPS; currentLocationProvider = LocationProvider.GPS;
} else if (notification.isAction(ServiceXmpp.NOTIF_NEW_PRESENCE_RECEIVED)) {
QRCodeGeneratorActivity.pressBackButton();
} }
} }
@ -369,12 +367,14 @@ public class ServiceTrackingDog implements Observer {
} }
return ret; return ret;
} }
/*
public void setOtherJid(String otherJid) throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException { public void setOtherJid(String otherJid) throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException {
if (serviceXmpp != null && otherJid != null) { if (serviceXmpp != null && otherJid != null) {
serviceXmpp.setOtherJid(otherJid); serviceXmpp.setOtherJid(otherJid);
serviceXmpp.sendPresenceAvailable(); serviceXmpp.sendPresenceAvailable(); // TODO send connexion command
} }
} }
*/
public void sendXmppFile(File file) throws XmppStringprepException, SmackException { public void sendXmppFile(File file) throws XmppStringprepException, SmackException {
if (serviceXmpp != null) { if (serviceXmpp != null) {
serviceXmpp.sendFile(file); serviceXmpp.sendFile(file);
@ -456,6 +456,12 @@ public class ServiceTrackingDog implements Observer {
serviceXmpp.sendCommandLocationTrail(lat, lon, time); serviceXmpp.sendCommandLocationTrail(lat, lon, time);
} }
} }
public void sendCommandConnexion(String jid) throws XmppStringprepException {
if (serviceXmpp != null) {
serviceXmpp.sendCommandConnexion(jid);
serviceXmpp.setOtherJid(jid);
}
}
//</editor-fold> //</editor-fold>
//</editor-fold> //</editor-fold>

View File

@ -19,7 +19,6 @@ import org.jivesoftware.smack.filter.PresenceTypeFilter;
import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.PresenceEventListener;
import org.jivesoftware.smack.roster.Roster; import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry; import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection;
@ -34,8 +33,6 @@ import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer; import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer; import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.iqregister.AccountManager; import org.jivesoftware.smackx.iqregister.AccountManager;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Localpart; import org.jxmpp.jid.parts.Localpart;
@ -55,19 +52,21 @@ import fr.chteufleur.mytrackingdog.QRCodeGeneratorActivity;
import fr.chteufleur.mytrackingdog.models.Notification; import fr.chteufleur.mytrackingdog.models.Notification;
import fr.chteufleur.mytrackingdog.models.beans.MyLocation; import fr.chteufleur.mytrackingdog.models.beans.MyLocation;
import fr.chteufleur.mytrackingdog.models.beans.RandomString; import fr.chteufleur.mytrackingdog.models.beans.RandomString;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.ConnexionCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.ObjectGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.ObjectGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.RealTimeModeCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.RealTimeModeCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.StartTrailGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.StartTrailGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.StopTrailGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.StopTrailGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.TrailGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.TrailGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendConnexionCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendObjectLocationCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendObjectLocationCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendRealTimeModeCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendRealTimeModeCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStartTrailCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStartTrailCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStopTrailCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStopTrailCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendTrailLocationCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendTrailLocationCommand;
public class ServiceXmpp extends Observable implements Runnable, ConnectionListener, StanzaListener, PresenceEventListener, FileTransferListener { public class ServiceXmpp extends Observable implements Runnable, ConnectionListener, StanzaListener, FileTransferListener {
public static final String TAG = ServiceXmpp.class.getName(); public static final String TAG = ServiceXmpp.class.getName();
@ -75,7 +74,6 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
public static final String NOTIF_NEW_OBJECT = ServiceXmpp.class.getName()+".newobject"; public static final String NOTIF_NEW_OBJECT = ServiceXmpp.class.getName()+".newobject";
public static final String NOTIF_START_TRAIL = ServiceXmpp.class.getName()+".starttrail"; public static final String NOTIF_START_TRAIL = ServiceXmpp.class.getName()+".starttrail";
public static final String NOTIF_STOP_TRAIL = ServiceXmpp.class.getName()+".stoptrail"; public static final String NOTIF_STOP_TRAIL = ServiceXmpp.class.getName()+".stoptrail";
public static final String NOTIF_NEW_PRESENCE_RECEIVED = ServiceXmpp.class.getName()+".newpresencereceived";
public static final String NOTIF_NEW_PRESENCE_SUBSCRIPTION = ServiceXmpp.class.getName()+".newpresencesubscriptionreceived"; public static final String NOTIF_NEW_PRESENCE_SUBSCRIPTION = ServiceXmpp.class.getName()+".newpresencesubscriptionreceived";
public static final String NOTIF_RECEIVING_FILE = ServiceXmpp.class.getName()+".receivingfile"; public static final String NOTIF_RECEIVING_FILE = ServiceXmpp.class.getName()+".receivingfile";
public static final String NOTIF_RECEIVING_FILE_COMPLETTED = NOTIF_RECEIVING_FILE+".completed"; public static final String NOTIF_RECEIVING_FILE_COMPLETTED = NOTIF_RECEIVING_FILE+".completed";
@ -84,7 +82,7 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
public static final String NOTIF_NEW_OBJECT_VALUE_LOCATION = NOTIF_NEW_OBJECT+".value.location"; public static final String NOTIF_NEW_OBJECT_VALUE_LOCATION = NOTIF_NEW_OBJECT+".value.location";
public static final String NOTIF_NEW_LOCATION_VALUE_LOCATION = NOTIF_NEW_LOCATION+".value.location"; public static final String NOTIF_NEW_LOCATION_VALUE_LOCATION = NOTIF_NEW_LOCATION+".value.location";
public static final String NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID = NOTIF_NEW_PRESENCE_RECEIVED+".value.jid"; public static final String NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID = NOTIF_NEW_PRESENCE_SUBSCRIPTION+".value.jid";
public static final String NOTIF_NEW_REAL_TIME_MODE_VALUE = NOTIF_NEW_REAL_TIME_MODE+".value"; public static final String NOTIF_NEW_REAL_TIME_MODE_VALUE = NOTIF_NEW_REAL_TIME_MODE+".value";
public static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc"; public static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc";
@ -92,6 +90,7 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
public static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc"; public static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc";
public static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc"; public static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc";
public static final String XMPP_NODE_REAL_TIME_MODE = "real_time_mode"; public static final String XMPP_NODE_REAL_TIME_MODE = "real_time_mode";
public static final String XMPP_NODE_CONNEXION = "connexion";
private static final String XMPP_IP_SERVER = "51.254.205.203"; private static final String XMPP_IP_SERVER = "51.254.205.203";
// private static final String XMPP_DOMAIN_SERVER = "anon.xmpp.kingpenguin.tk"; // private static final String XMPP_DOMAIN_SERVER = "anon.xmpp.kingpenguin.tk";
@ -199,6 +198,7 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException ex) { } catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
otherJid = null;
} }
if (connection != null) { if (connection != null) {
connection.disconnect(); connection.disconnect();
@ -278,7 +278,6 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
roster = Roster.getInstanceFor(connection); roster = Roster.getInstanceFor(connection);
roster.setSubscriptionMode(Roster.SubscriptionMode.manual); roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
roster.addPresenceEventListener(this);
} }
@Override @Override
@ -417,48 +416,6 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
} }
} }
@Override
public void presenceAvailable(FullJid address, Presence presence) {
String fullJid = address.asFullJidIfPossible().toString();
if (presence.getType() == Presence.Type.available) {
if (!fullJid.equals(jid) && (otherJid == null || !fullJid.equals(otherJid))) {
Log.i(TAG, "PRESENCE AVAILABLE RECEIVED FROM " + fullJid);
otherJid = fullJid;
try {
sendPresenceAvailable(fullJid);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (XmppStringprepException e) {
e.printStackTrace();
}
setChanged();
notifyObservers(new Notification(NOTIF_NEW_PRESENCE_RECEIVED).addExtra(NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID, otherJid));
}
} else if (presence.getType() == Presence.Type.unavailable) {
if (otherJid != null && otherJid.equals(fullJid)) {
otherJid = null;
}
}
}
@Override
public void presenceUnavailable(FullJid address, Presence presence) {
}
@Override
public void presenceError(Jid address, Presence errorPresence) {
}
@Override
public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
}
@Override
public void presenceUnsubscribed(BareJid address, Presence unsubscribedPresence) {
}
@Override @Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException { public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
if (packet instanceof Presence) { if (packet instanceof Presence) {
@ -515,6 +472,12 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
return new RealTimeModeCommand(thsi); return new RealTimeModeCommand(thsi);
} }
}); });
commandManager.registerCommand(XMPP_NODE_CONNEXION, XMPP_NODE_CONNEXION, new LocalCommandFactory() {
@Override
public LocalCommand getInstance() throws InstantiationException, IllegalAccessException {
return new ConnexionCommand(thsi);
}
});
} }
public void startTrailGeoloc() { public void startTrailGeoloc() {
@ -580,6 +543,10 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
} }
} }
public void sendCommandConnexion(String jid) throws XmppStringprepException {
sendCommand(new SendConnexionCommand(commandManager, JidCreate.fullFrom(jid)));
}
private void sendCommand(SendCommand command) { private void sendCommand(SendCommand command) {
if (command != null) { if (command != null) {
synchronized (listSendCommand) { synchronized (listSendCommand) {
@ -724,6 +691,11 @@ public class ServiceXmpp extends Observable implements Runnable, ConnectionListe
public void setOtherJid(String otherJid) { public void setOtherJid(String otherJid) {
this.otherJid = otherJid; this.otherJid = otherJid;
try {
sendPresenceAvailable(otherJid);
} catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) {
e.printStackTrace();
}
} }
public boolean isOtherJidSet() { public boolean isOtherJidSet() {