Add send trail over XMPP (Should work with q serveur proxy).

This commit is contained in:
chteufleur 2018-10-06 11:29:04 +02:00
parent 371f5aa1cb
commit 137f024117
5 changed files with 141 additions and 1 deletions

View File

@ -356,6 +356,19 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
}
return true;
} else if (id == R.id.action_send_gpx_trail_by_xmpp) {
File trailFile = serviceTrackingDog.getLastExportedTrailFile();
if (trailFile != null) {
try {
serviceTrackingDog.sendXmppFile(trailFile);
} catch (XmppStringprepException | SmackException e) {
Toast.makeText(ctx, "Echec de l'envoye de la trace.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(ctx, "Aucune trace enregistré.", Toast.LENGTH_LONG).show();
}
return true;
} else if (id == R.id.action_active_vibration_object) {
boolean checked = item.isChecked();
item.setChecked(!checked);
@ -669,6 +682,27 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
changeStatusTrace();
}
});
} else if (notification.isAction(ServiceXmpp.NOTIF_RECEIVING_FILE)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, "Réception d'un fichier en cours", Toast.LENGTH_SHORT).show();
}
});
} else if (notification.isAction(ServiceXmpp.NOTIF_RECEIVING_FILE_COMPLETTED)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, "Le fichier a était réceptionné", Toast.LENGTH_SHORT).show();
}
});
} else if (notification.isAction(ServiceXmpp.NOTIF_RECEIVING_FILE_FAIL)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, "Echec dans la reception du fichier", Toast.LENGTH_LONG).show();
}
});
}
}

View File

@ -79,6 +79,9 @@ public class ServiceTrackingDog implements Observer {
public void setAppName(String appName) {
this.appName = appName;
if (serviceXmpp != null) {
serviceXmpp.setAppName(appName);
}
}
public void addObserver(Observer observer, String name) {
@ -360,6 +363,11 @@ public class ServiceTrackingDog implements Observer {
serviceXmpp.sendPresenceAvailable();
}
}
public void sendXmppFile(File file) throws XmppStringprepException, SmackException {
if (serviceXmpp != null) {
serviceXmpp.sendFile(file);
}
}
//<editor-fold defaultstate="collapsed" desc="Commands">
public void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (serviceXmpp != null) {

View File

@ -1,6 +1,7 @@
package fr.chteufleur.mytrackingdog.services;
import android.content.res.Resources;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
@ -19,6 +20,12 @@ import org.jivesoftware.smackx.commands.AdHocCommandManager;
import org.jivesoftware.smackx.commands.LocalCommand;
import org.jivesoftware.smackx.commands.LocalCommandFactory;
import org.jivesoftware.smackx.commands.RemoteCommand;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.xdata.Form;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.FullJid;
@ -26,6 +33,7 @@ import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
@ -39,7 +47,7 @@ import fr.chteufleur.mytrackingdog.models.xmpp.commands.StartTrailGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.StopTrailGeolocCommand;
import fr.chteufleur.mytrackingdog.models.xmpp.commands.TrailGeolocCommand;
public class ServiceXmpp extends Observable implements PresenceEventListener {
public class ServiceXmpp extends Observable implements PresenceEventListener, FileTransferListener {
public static final String TAG = ServiceXmpp.class.getName();
@ -48,6 +56,10 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
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_NEW_PRESENCE_RECEIVED = ServiceXmpp.class.getName()+".newpresencereceived";
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_FAIL = NOTIF_RECEIVING_FILE+".receivingfile";
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_PRESENCE_RECEIVED_VALUE_JID = NOTIF_NEW_PRESENCE_RECEIVED+".value.jid";
@ -61,9 +73,12 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
private static final String XMPP_DOMAIN_SERVER = "anon.xmpp.kingpenguin.tk";
private static final int XMPP_PORT = 5222;
private String appName = "";
private final Resources resources;
private final XMPPTCPConnectionConfiguration configuration;
private AdHocCommandManager commandManager;
private FileTransferManager fileManager;
private AbstractXMPPConnection connection;
private String jid;
private String otherJid;
@ -85,6 +100,10 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
this.thsi = this;
}
public void setAppName(String appName) {
this.appName = appName;
}
public boolean connect() throws InterruptedException, XMPPException, SmackException, IOException {
if (!isEnable) {
return false;
@ -95,10 +114,12 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
boolean isConnected;
connection = new XMPPTCPConnection(configuration);
commandManager = AdHocCommandManager.getAddHocCommandsManager(connection);
fileManager = FileTransferManager.getInstanceFor(connection);
connection.connect();
connection.login();
registerCommands();
fileManager.addFileTransferListener(this);
Roster roster = Roster.getInstanceFor(connection);
roster.addPresenceEventListener(this);
@ -106,6 +127,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
if (isConnected) {
jid = connection.getUser().asFullJidIfPossible().toString();
Log.i(TAG, "JID: "+jid);
setPresenceAvailable();
}
Runnable r = new Runnable() {
@Override
@ -171,6 +193,14 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
}
}
private void setPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException {
if (connection != null) {
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("");
connection.sendStanza(presence);
}
}
@Override
public void presenceAvailable(FullJid address, Presence presence) {
String fullJid = address.asFullJidIfPossible().toString();
@ -317,6 +347,68 @@ public class ServiceXmpp extends Observable implements PresenceEventListener {
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="File transfert">
// Example: https://github.com/igniterealtime/Smack/blob/master/documentation/extensions/filetransfer.md
public void sendFile(File file) throws XmppStringprepException, SmackException {
if (isOtherJidSet()) {
// Create the outgoing file transfer
OutgoingFileTransfer transfer = fileManager.createOutgoingFileTransfer(JidCreate.entityFullFrom(otherJid));
// Send the file
transfer.sendFile(file, file.getName());
}
}
@Override
public void fileTransferRequest(FileTransferRequest request) {
// Check to see if the request should be accepted
String fullJid = request.getRequestor().asFullJidIfPossible().toString();
if (otherJid != null && fullJid.equals(otherJid)) {
// Accept it
IncomingFileTransfer transfer = request.accept();
File destFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + appName + "/" + request.getFileName());
try {
transfer.recieveFile(destFile);
setChanged();
notifyObservers(new Notification(NOTIF_RECEIVING_FILE));
while(!transfer.isDone()) {
if (transfer.getStatus().equals(FileTransfer.Status.error)) {
Log.i(TAG, "File transfert error");
setChanged();
notifyObservers(new Notification(NOTIF_RECEIVING_FILE_FAIL));
} else {
Log.i(TAG, "File transfert ("+transfer.getStatus()+"): "+(transfer.getProgress() * 100) + " %");
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (transfer.getStatus().equals(FileTransfer.Status.error)) {
Log.e(TAG, "File transfert error");
destFile.delete();
} else {
Log.i(TAG, "File transfert done (" + transfer.getStatus() + ")");
setChanged();
notifyObservers(new Notification(NOTIF_RECEIVING_FILE_COMPLETTED));
}
} catch (SmackException | IOException e) {
e.printStackTrace();
setChanged();
notifyObservers(new Notification(NOTIF_RECEIVING_FILE_FAIL));
}
} else {
// Reject it
try {
request.reject();
} catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
}
}
}
//</editor-fold>
public void setOtherJid(String otherJid) {
this.otherJid = otherJid;
}

View File

@ -12,6 +12,11 @@
android:orderInCategory="100"
android:title="@string/action_send_gpx_trail"
app:showAsAction="never" />
<item
android:id="@+id/action_send_gpx_trail_by_xmpp"
android:orderInCategory="100"
android:title="@string/action_send_gpx_trail_by_xmpp"
app:showAsAction="never" />
<item
android:id="@+id/action_active_vibration_object"
android:checkable="true"

View File

@ -9,6 +9,7 @@
<string name="action_import_gpx">Import GPX</string>
<string name="action_send_gpx_trail">Envoyer trace du traceur</string>
<string name="action_send_gpx_trail_by_xmpp">Envoyer trace du traceur par XMPP</string>
<string name="action_send_to">Envoyer par</string>
<string name="action_active_vibration_object">Active vibration objets</string>
<string name="action_qr_code_generator">Affiche identifiant</string>