From 25504418d37fd0b06c55af7ce74dfd2652f8bd50 Mon Sep 17 00:00:00 2001 From: chteufleur Date: Fri, 5 Oct 2018 12:12:25 +0200 Subject: [PATCH] Integration of ServiceXmpp into ServiceTrackingDog. --- .../mytrackingdog/MainActivity.java | 58 ++++--------- .../services/ServiceTrackingDog.java | 86 ++++++++++++++++++- 2 files changed, 99 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index ed9568f..c19c83d 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -69,12 +69,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu private static final int ACTIVITY_QR_CODE_GENERATOR = 2; private static final int ACTIVITY_QR_CODE_READER = 3; + private ServiceTrackingDog serviceTrackingDog = null; public static String appName = ""; private MyLocationNewOverlay mLocationOverlay; private IOrientationProvider compass = null; - private ServiceXmpp serviceXmpp = null; - private ServiceTrackingDog serviceTrackingDog = null; private Context ctx = null; private MapView map = null; @@ -170,9 +169,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu changeStatusTrace(); try { if (serviceTrackingDog.isTraceurActivated()) { - serviceXmpp.sendCommandStartTrail(); + serviceTrackingDog.sendCommandStartTrail(); } else { - serviceXmpp.sendCommandStopTrail(); + serviceTrackingDog.sendCommandStopTrail(); } } catch (XmppStringprepException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException ex) { } @@ -196,7 +195,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); addMarker(gp, loc.isFound()); try { - serviceXmpp.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); + serviceTrackingDog.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); } catch (Exception e) { } } @@ -216,22 +215,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu getResources() ); serviceTrackingDog.addObserver(this, ServiceGps.class.getName()); - } - - if (serviceXmpp == null) { - try { - serviceXmpp = new ServiceXmpp(getResources()); - serviceXmpp.addObserver(this); - serviceXmpp.connect(); - } catch (InterruptedException e) { - Toast.makeText(ctx, "Échec du service XMPP.", Toast.LENGTH_LONG).show(); - } catch (XMPPException e) { - Toast.makeText(ctx, "Échec du service XMPP.", Toast.LENGTH_LONG).show(); - } catch (SmackException e) { - Toast.makeText(ctx, "Échec du service XMPP.", Toast.LENGTH_LONG).show(); - } catch (IOException e) { - Toast.makeText(ctx, "Échec du service XMPP.", Toast.LENGTH_LONG).show(); - } + serviceTrackingDog.addObserver(this, ServiceXmpp.class.getName()); } } @@ -317,9 +301,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu if (serviceTrackingDog != null) { serviceTrackingDog.close(); } - if (serviceXmpp != null) { - serviceXmpp.close(); - } zoomed = false; } // @@ -334,7 +315,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu vibrationObjectMenuItem.setChecked(serviceTrackingDog.isVibrationNearObjectEnabled()); MenuItem xmppObjectMenuItem = menu.findItem(R.id.action_active_xmpp); - xmppObjectMenuItem.setChecked(serviceXmpp.isEnabled()); + xmppObjectMenuItem.setChecked(serviceTrackingDog.isXmppEnabled()); return true; } @@ -386,16 +367,16 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu item.setChecked(newStat); if (newStat) { try { - serviceXmpp.enable(); + serviceTrackingDog.enableXmpp(); Toast.makeText(ctx, "Connexion XMPP réussie", Toast.LENGTH_SHORT).show(); } catch (InterruptedException | IOException | SmackException | XMPPException e) { e.printStackTrace(); Toast.makeText(ctx, "Echec de connexion XMPP", Toast.LENGTH_LONG).show(); item.setChecked(false); - serviceXmpp.disable(); + serviceTrackingDog.disableXmpp(); } } else { - serviceXmpp.disable(); + serviceTrackingDog.disableXmpp(); } return true; } @@ -436,15 +417,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu if (data.hasExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT)) { String text = data.getStringExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT); try { - serviceXmpp.setOtherJid(text); - serviceXmpp.sendPresenceAvailable(); + serviceTrackingDog.setOtherJid(text); Log.i(TAG, "TEXT: "+text); - } catch (SmackException.NotConnectedException e) { - Log.e(TAG, "Fail send presence", e); - } catch (InterruptedException e) { - Log.e(TAG, "Fail send presence", e); - } catch (XmppStringprepException e) { + } catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) { Log.e(TAG, "Fail send presence", e); + Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show();; } } break; @@ -661,7 +638,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu public void update(Observable observable, Object o) { if (observable.getClass().getName().equals(ServiceGps.class.getName())) { updateServiceGps(o); - } else if (observable == serviceXmpp) { + } else if (observable.getClass().getName().equals(ServiceXmpp.class.getName())) { updateServiceXmpp(o); } } @@ -670,10 +647,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) { MyLocation loc = serviceTrackingDog.getCurrentLocation(); onNewLocation(loc); - try { - serviceXmpp.sendCommandLocationTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); - } catch (Exception ex) { - } } } @@ -684,7 +657,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu @Override public void run() { if (serviceTrackingDog.isTraceurActivated()) { - MyLocation loc = serviceXmpp.getCurrentLocation(); + MyLocation loc = serviceTrackingDog.getXmppCurrentLocation(); onNewLocation(loc); } } @@ -694,7 +667,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu @Override public void run() { if (serviceTrackingDog.isTraceurActivated()) { - MyLocation locObj = serviceXmpp.getLastObjectXmppLocation(); + MyLocation locObj = serviceTrackingDog.getXmppLastObjectLocation(); WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj); GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); addMarker(gp, loc.isFound()); @@ -721,7 +694,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu private void onNewLocation(MyLocation loc) { if (loc != null) { - //serviceGps.addPoint(loc); GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); if (mLocationOverlay.isFollowLocationEnabled()) { map.getController().setCenter(currentPoint); 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 bd3f63a..81a575d 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java @@ -7,9 +7,12 @@ import android.os.Build; import android.os.VibrationEffect; import android.os.Vibrator; +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPException; import org.jxmpp.stringprep.XmppStringprepException; import java.io.File; +import java.io.IOException; import java.net.UnknownHostException; import java.util.List; import java.util.Observable; @@ -44,6 +47,7 @@ public class ServiceTrackingDog implements Observer { ServiceXmpp sx = null; try { sx = new ServiceXmpp(resources); + sx.addObserver(this); } catch (UnknownHostException | XmppStringprepException ex) { sx = null; } @@ -84,18 +88,34 @@ public class ServiceTrackingDog implements Observer { if (o != null && o instanceof String) { String action = (String) o; if (action.equals(ServiceGps.NOTIF_NEW_LOCATION)) { - onNewLocation(); + MyLocation loc = onNewLocation(); shouldVibrate(); + if (serviceXmpp != null && loc != null) { + try { + serviceXmpp.sendCommandLocationTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); + } catch (Exception ex) { + } + } } } } public void updateXmpp(Object o) { + if (o != null && o instanceof String) { + String action = (String) o; + if (o.equals(ServiceXmpp.NOTIF_NEW_LOCATION)) { + onNewLocation(); +// } else if (o.equals(ServiceXmpp.NOTIF_NEW_OBJECT)) { +// } else if (o.equals(ServiceXmpp.NOTIF_START_TRAIL)) { +// } else if (o.equals(ServiceXmpp.NOTIF_STOP_TRAIL)) { + } + } } - public void onNewLocation() { + public MyLocation onNewLocation() { MyLocation loc = serviceGps.getCurrentLocation(); serviceGps.addPoint(loc); + return loc; } // @@ -224,4 +244,66 @@ public class ServiceTrackingDog implements Observer { } // // + + // + public void enableXmpp() throws InterruptedException, XMPPException, SmackException, IOException { + if (serviceXmpp != null) { + serviceXmpp.enable(); + } + } + public void disableXmpp() { + if (serviceXmpp != null) { + serviceXmpp.disable(); + } + } + public boolean isXmppEnabled() { + boolean ret = false; + if (serviceXmpp != null) { + ret = serviceXmpp.isEnabled(); + } + return ret; + } + public void setOtherJid(String otherJid) throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException { + if (serviceXmpp != null && otherJid != null) { + serviceXmpp.setOtherJid(otherJid); + serviceXmpp.sendPresenceAvailable(); + } + } + public MyLocation getXmppCurrentLocation() { + MyLocation ret = null; + if (serviceXmpp != null) { + ret = serviceXmpp.getCurrentLocation(); + } + return ret; + } + public MyLocation getXmppLastObjectLocation() { + MyLocation ret = null; + if (serviceXmpp != null) { + ret = serviceXmpp.getLastObjectXmppLocation(); + } + return ret; + } + // + public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + if (serviceXmpp != null) { + serviceXmpp.sendCommandStartTrail(); + } + } + public void sendCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + if (serviceXmpp != null) { + serviceXmpp.sendCommandStopTrail(); + } + } + public void sendCommandObjectTrail(double lat, double lon, long time) throws Exception { + if (serviceXmpp != null) { + serviceXmpp.sendCommandObjectTrail(lat, lon, time); + } + } + public void sendCommandLocationTrail(double lat, double lon, long time) throws Exception { + if (serviceXmpp != null) { + serviceXmpp.sendCommandLocationTrail(lat, lon, time); + } + } + // + // }