From 4499278583e21dbaee25f8faf9c2f554b99d2d70 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Mon, 8 Oct 2018 08:19:49 +0200 Subject: [PATCH] Add XMPP real time mode. --- .../mytrackingdog/MainActivity.java | 32 ++++++- .../xmpp/commands/RealTimeModeCommand.java | 86 +++++++++++++++++++ .../services/ServiceTrackingDog.java | 17 ++++ .../mytrackingdog/services/ServiceXmpp.java | 48 ++++++++++- app/src/main/res/menu/drawer_view.xml | 4 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/RealTimeModeCommand.java diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index e8ea1b0..a2669c7 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -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_reader).setEnabled(false); _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()); + 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); updateSendTraceMenu(); - return false; - //return super.onCreateOptionsMenu(menu); + return super.onCreateOptionsMenu(menu); } private void updateSendTraceMenu() { @@ -381,6 +383,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu } else if (id == R.id.action_active_xmpp) { activeXmpp(item); 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) { startActivityQrCodeGenerator(); 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_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_active_xmpp_real_time_mode).setEnabled(newStat); item.setChecked(newStat); 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() { Intent intent = new Intent(this, QRCodeGeneratorActivity.class); 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(); } }); + } 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()); + } + }); } } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/RealTimeModeCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/RealTimeModeCommand.java new file mode 100644 index 0000000..63745b9 --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/RealTimeModeCommand.java @@ -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 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 { + + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java index 3516da0..d663f67 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java @@ -377,6 +377,23 @@ public class ServiceTrackingDog implements Observer { 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; + } // public void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { if (serviceXmpp != null) { diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java index ad0f871..30b6860 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java @@ -43,6 +43,7 @@ import fr.chteufleur.mytrackingdog.QRCodeGeneratorActivity; import fr.chteufleur.mytrackingdog.models.Notification; import fr.chteufleur.mytrackingdog.models.beans.MyLocation; 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.StopTrailGeolocCommand; 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_COMPLETTED = NOTIF_RECEIVING_FILE+".completed"; 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_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_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_OBJECT_GEOLOC = "object_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_REAL_TIME_MODE = "real_time_mode"; private static final String XMPP_IP_SERVER = "51.254.205.203"; 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 boolean isEnable = false; + private boolean isRealTimeMode = false; public ServiceXmpp(Resources resources) throws UnknownHostException, XmppStringprepException { this.resources = resources; @@ -170,6 +175,14 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi return isEnable; } + public void setRealTimeMode(boolean b) { + this.isRealTimeMode = b; + } + + public boolean isRealTimeMode() { + return this.isRealTimeMode; + } + // public void sendPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException { if (otherJid != null) { @@ -270,6 +283,12 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi 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() { @@ -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)); } + 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 { - if (isOtherJidSet()) { + if (isOtherJidSet() && isRealTimeMode) { Log.i(TAG, "Send command start"); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_START_TRAIL_GEOLOC); 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 { - if (isOtherJidSet()) { + if (isOtherJidSet() && isRealTimeMode) { Log.i(TAG, "Send command stop"); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_STOP_TRAIL_GEOLOC); 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 { - if (isOtherJidSet()) { + if (isOtherJidSet() && isRealTimeMode) { Log.i(TAG, "Send command object"); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_OBJECT_GEOLOC); 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 { - if (isOtherJidSet()) { + if (isOtherJidSet() && isRealTimeMode) { Log.i(TAG, "Send command location"); RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_TRAIL_GEOLOC); command.execute(); @@ -345,6 +371,20 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi 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); + } + } // diff --git a/app/src/main/res/menu/drawer_view.xml b/app/src/main/res/menu/drawer_view.xml index e3fb7be..66940da 100644 --- a/app/src/main/res/menu/drawer_view.xml +++ b/app/src/main/res/menu/drawer_view.xml @@ -28,6 +28,10 @@ android:id="@+id/action_active_xmpp" android:title="@string/action_active_xmpp" android:icon="@drawable/ic_check_box_unchecked" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1073bd2..2e53482 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,5 +17,6 @@ Lecture identifiant XMPP Active XMPP + Active mode temps réel Envoyer trace du traceur