Add XMPP real time mode.

This commit is contained in:
Chteufleur 2018-10-08 08:19:49 +02:00
parent 372896ac34
commit 4499278583
6 changed files with 181 additions and 7 deletions

View File

@ -328,12 +328,14 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
_menu.findItem(R.id.action_qr_code_generator).setEnabled(false); _menu.findItem(R.id.action_qr_code_generator).setEnabled(false);
_menu.findItem(R.id.action_qr_code_reader).setEnabled(false); _menu.findItem(R.id.action_qr_code_reader).setEnabled(false);
_menu.findItem(R.id.action_active_vibration_object).setIcon(getResources().getDrawable( _menu.findItem(R.id.action_active_vibration_object).setIcon(getResources().getDrawable(
serviceTrackingDog.isVibrationNearObjectEnabled() ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked)); serviceTrackingDog.isVibrationNearObjectEnabled() ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked)
);
_menu.findItem(R.id.action_active_vibration_object).setChecked(serviceTrackingDog.isVibrationNearObjectEnabled()); _menu.findItem(R.id.action_active_vibration_object).setChecked(serviceTrackingDog.isVibrationNearObjectEnabled());
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setIcon(getResources().getDrawable(R.drawable.ic_check_box_unchecked));
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setEnabled(false);
navigationView.getMenu().findItem(R.id.action_clear_dog).setEnabled(false); navigationView.getMenu().findItem(R.id.action_clear_dog).setEnabled(false);
updateSendTraceMenu(); updateSendTraceMenu();
return false; return super.onCreateOptionsMenu(menu);
//return super.onCreateOptionsMenu(menu);
} }
private void updateSendTraceMenu() { private void updateSendTraceMenu() {
@ -381,6 +383,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
} else if (id == R.id.action_active_xmpp) { } else if (id == R.id.action_active_xmpp) {
activeXmpp(item); activeXmpp(item);
return true; return true;
} else if (id == R.id.action_active_xmpp_real_time_mode) {
activeXmppRealTimeMode(item);
return true;
} else if (id == R.id.action_qr_code_generator) { } else if (id == R.id.action_qr_code_generator) {
startActivityQrCodeGenerator(); startActivityQrCodeGenerator();
mDrawerLayout.closeDrawers(); mDrawerLayout.closeDrawers();
@ -471,10 +476,21 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
navigationView.getMenu().findItem(R.id.action_qr_code_generator).setEnabled(newStat); navigationView.getMenu().findItem(R.id.action_qr_code_generator).setEnabled(newStat);
navigationView.getMenu().findItem(R.id.action_qr_code_reader).setEnabled(newStat); navigationView.getMenu().findItem(R.id.action_qr_code_reader).setEnabled(newStat);
navigationView.getMenu().findItem(R.id.action_send_gpx_trail_by_xmpp).setEnabled(newStat && serviceTrackingDog.getLastExportedTrailFile() != null); navigationView.getMenu().findItem(R.id.action_send_gpx_trail_by_xmpp).setEnabled(newStat && serviceTrackingDog.getLastExportedTrailFile() != null);
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setEnabled(newStat);
item.setChecked(newStat); item.setChecked(newStat);
item.setIcon(getResources().getDrawable(newStat ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked)); item.setIcon(getResources().getDrawable(newStat ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked));
} }
private void activeXmppRealTimeMode(MenuItem item) {
boolean checked = item.isChecked();
boolean newStat = !checked;
item.setChecked(newStat);
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setIcon(
getResources().getDrawable(newStat ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked)
);
serviceTrackingDog.setXmppRealTimeMode(newStat);
}
private void startActivityQrCodeGenerator() { private void startActivityQrCodeGenerator() {
Intent intent = new Intent(this, QRCodeGeneratorActivity.class); Intent intent = new Intent(this, QRCodeGeneratorActivity.class);
startActivityForResult(intent, ACTIVITY_QR_CODE_GENERATOR); startActivityForResult(intent, ACTIVITY_QR_CODE_GENERATOR);
@ -822,6 +838,16 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
Toast.makeText(ctx, "Echec dans la reception du fichier", Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Echec dans la reception du fichier", Toast.LENGTH_LONG).show();
} }
}); });
} else if (notification.isAction(ServiceXmpp.NOTIF_NEW_REAL_TIME_MODE)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setIcon(
getResources().getDrawable(serviceTrackingDog.isXmppRealTimeMode() ? R.drawable.ic_check_box_checked : R.drawable.ic_check_box_unchecked)
);
navigationView.getMenu().findItem(R.id.action_active_xmpp_real_time_mode).setChecked(serviceTrackingDog.isXmppRealTimeMode());
}
});
} }
} }

View File

@ -0,0 +1,86 @@
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.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jxmpp.jid.Jid;
import java.util.List;
import fr.chteufleur.mytrackingdog.services.ServiceXmpp;
public class RealTimeModeCommand extends LocalCommand {
public static final String TAG = ObjectGeolocCommand.class.getName();
public static final String FIELD_PARAM_IS_ACTIVE = "is_active";
private final ServiceXmpp serviceXmpp;
public RealTimeModeCommand(ServiceXmpp serviceXmpp) {
this.serviceXmpp = serviceXmpp;
}
@Override
public boolean isLastStage() {
return getCurrentStage() == 1;
}
@Override
public boolean hasPermission(Jid jid) {
return true;
}
@Override
public void execute() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {Form result = new Form(DataForm.Type.form);
setExecuteAction(Action.next);
FormField resultFieldIsActive = new FormField(FIELD_PARAM_IS_ACTIVE);
resultFieldIsActive.setLabel(FIELD_PARAM_IS_ACTIVE);
resultFieldIsActive.setType(FormField.Type.bool);
result.addField(resultFieldIsActive);
this.addActionAvailable(Action.next);
setForm(result);
}
@Override
public void next(Form response) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
FormField formIsValide = response.getField(FIELD_PARAM_IS_ACTIVE);
if (formIsValide != null) {
List<String> isValueStrs = formIsValide.getValues();
boolean isValue = false;
for (String isValueStr : isValueStrs) {
isValue = isValueStr.equalsIgnoreCase("1") || isValueStr.equalsIgnoreCase("true") || isValueStr.equalsIgnoreCase("yes");
}
if (serviceXmpp != null) {
serviceXmpp.addIsRealTimeMode(isValue);
}
this.addNote(new AdHocCommandNote(AdHocCommandNote.Type.info, "SUCCESS"));
} else {
this.addNote((new AdHocCommandNote(AdHocCommandNote.Type.error, "FAIL")));
}
}
@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

@ -377,6 +377,23 @@ public class ServiceTrackingDog implements Observer {
serviceXmpp.sendFile(file); serviceXmpp.sendFile(file);
} }
} }
public void setXmppRealTimeMode(boolean b) {
if (serviceXmpp != null) {
serviceXmpp.setRealTimeMode(b);
try {
serviceXmpp.sendCommandeRealTimeMode();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean isXmppRealTimeMode() {
boolean ret = false;
if (serviceXmpp != null) {
ret = serviceXmpp.isRealTimeMode();
}
return ret;
}
//<editor-fold defaultstate="collapsed" desc="Commands"> //<editor-fold defaultstate="collapsed" desc="Commands">
public void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { public void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (serviceXmpp != null) { if (serviceXmpp != null) {

View File

@ -43,6 +43,7 @@ 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.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.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;
@ -59,15 +60,18 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
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";
public static final String NOTIF_RECEIVING_FILE_FAIL = NOTIF_RECEIVING_FILE+".receivingfile"; public static final String NOTIF_RECEIVING_FILE_FAIL = NOTIF_RECEIVING_FILE+".receivingfile";
public static final String NOTIF_NEW_REAL_TIME_MODE = ServiceXmpp.class.getName()+"realtimemode";
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_RECEIVED+".value.jid";
public static final String NOTIF_NEW_REAL_TIME_MODE_VALUE = NOTIF_NEW_REAL_TIME_MODE+".value";
private static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc"; private static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc";
private static final String XMPP_NODE_OBJECT_GEOLOC = "object_geoloc"; private static final String XMPP_NODE_OBJECT_GEOLOC = "object_geoloc";
private static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc"; private static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc";
private static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc"; private static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc";
private static final String XMPP_NODE_REAL_TIME_MODE = "real_time_mode";
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";
@ -85,6 +89,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
private final ServiceXmpp thsi; private final ServiceXmpp thsi;
private boolean isEnable = false; private boolean isEnable = false;
private boolean isRealTimeMode = false;
public ServiceXmpp(Resources resources) throws UnknownHostException, XmppStringprepException { public ServiceXmpp(Resources resources) throws UnknownHostException, XmppStringprepException {
this.resources = resources; this.resources = resources;
@ -170,6 +175,14 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
return isEnable; return isEnable;
} }
public void setRealTimeMode(boolean b) {
this.isRealTimeMode = b;
}
public boolean isRealTimeMode() {
return this.isRealTimeMode;
}
//<editor-fold defaultstate="collapsed" desc="Presence"> //<editor-fold defaultstate="collapsed" desc="Presence">
public void sendPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException { public void sendPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException {
if (otherJid != null) { if (otherJid != null) {
@ -270,6 +283,12 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
return new StopTrailGeolocCommand(thsi); return new StopTrailGeolocCommand(thsi);
} }
}); });
commandManager.registerCommand(XMPP_NODE_REAL_TIME_MODE, XMPP_NODE_REAL_TIME_MODE, new LocalCommandFactory() {
@Override
public LocalCommand getInstance() {
return new RealTimeModeCommand(thsi);
}
});
} }
public void startTrailGeoloc() { public void startTrailGeoloc() {
@ -298,8 +317,15 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
notifyObservers(new Notification(NOTIF_NEW_OBJECT).addExtra(NOTIF_NEW_OBJECT_VALUE_LOCATION, objectLocation)); notifyObservers(new Notification(NOTIF_NEW_OBJECT).addExtra(NOTIF_NEW_OBJECT_VALUE_LOCATION, objectLocation));
} }
public void addIsRealTimeMode(boolean isRealTimeMode) {
Log.i(TAG, "Add real time mode");
this.isRealTimeMode = isRealTimeMode;
setChanged();
notifyObservers(new Notification(NOTIF_NEW_REAL_TIME_MODE).addExtra(NOTIF_NEW_REAL_TIME_MODE_VALUE, isRealTimeMode));
}
public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (isOtherJidSet()) { if (isOtherJidSet() && isRealTimeMode) {
Log.i(TAG, "Send command start"); Log.i(TAG, "Send command start");
RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_START_TRAIL_GEOLOC); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_START_TRAIL_GEOLOC);
command.execute(); command.execute();
@ -307,7 +333,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
} }
public void sendCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { public void sendCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
if (isOtherJidSet()) { if (isOtherJidSet() && isRealTimeMode) {
Log.i(TAG, "Send command stop"); Log.i(TAG, "Send command stop");
RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_STOP_TRAIL_GEOLOC); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_STOP_TRAIL_GEOLOC);
command.execute(); command.execute();
@ -315,7 +341,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
} }
public void sendCommandObjectTrail(double lat, double lon, long time) throws Exception { public void sendCommandObjectTrail(double lat, double lon, long time) throws Exception {
if (isOtherJidSet()) { if (isOtherJidSet() && isRealTimeMode) {
Log.i(TAG, "Send command object"); Log.i(TAG, "Send command object");
RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_OBJECT_GEOLOC); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_OBJECT_GEOLOC);
command.execute(); command.execute();
@ -331,7 +357,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
} }
public void sendCommandLocationTrail(double lat, double lon, long time) throws Exception { public void sendCommandLocationTrail(double lat, double lon, long time) throws Exception {
if (isOtherJidSet()) { if (isOtherJidSet() && isRealTimeMode) {
Log.i(TAG, "Send command location"); Log.i(TAG, "Send command location");
RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_TRAIL_GEOLOC); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_TRAIL_GEOLOC);
command.execute(); command.execute();
@ -345,6 +371,20 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi
command.next(form); command.next(form);
} }
} }
public void sendCommandeRealTimeMode() throws Exception {
if (isOtherJidSet()) {
Log.i(TAG, "Send commande real time mode");
RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_REAL_TIME_MODE);
command.execute();
if (command.getForm() == null) {
throw new Exception("Didn't get form back");
}
Form form = command.getForm().createAnswerForm();
form.setAnswer(RealTimeModeCommand.FIELD_PARAM_IS_ACTIVE, isRealTimeMode);
command.next(form);
}
}
//</editor-fold> //</editor-fold>

View File

@ -28,6 +28,10 @@
android:id="@+id/action_active_xmpp" android:id="@+id/action_active_xmpp"
android:title="@string/action_active_xmpp" android:title="@string/action_active_xmpp"
android:icon="@drawable/ic_check_box_unchecked" /> android:icon="@drawable/ic_check_box_unchecked" />
<item
android:id="@+id/action_active_xmpp_real_time_mode"
android:title="@string/action_active_xmpp_real_time_mode"
android:icon="@drawable/ic_check_box_unchecked" />
<item <item
android:id="@+id/action_qr_code_generator" android:id="@+id/action_qr_code_generator"
android:title="@string/action_qr_code_generator" /> android:title="@string/action_qr_code_generator" />

View File

@ -17,5 +17,6 @@
<string name="action_qr_code_reader">Lecture identifiant</string> <string name="action_qr_code_reader">Lecture identifiant</string>
<string name="menu_title_xmpp">XMPP</string> <string name="menu_title_xmpp">XMPP</string>
<string name="action_active_xmpp">Active XMPP</string> <string name="action_active_xmpp">Active XMPP</string>
<string name="action_active_xmpp_real_time_mode">Active mode temps réel</string>
<string name="action_send_gpx_trail_by_xmpp">Envoyer trace du traceur</string> <string name="action_send_gpx_trail_by_xmpp">Envoyer trace du traceur</string>
</resources> </resources>