Integration of ServiceXmpp into ServiceTrackingDog.

This commit is contained in:
chteufleur 2018-10-05 12:12:25 +02:00
parent 83342b5be9
commit 25504418d3
2 changed files with 99 additions and 45 deletions

View File

@ -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_GENERATOR = 2;
private static final int ACTIVITY_QR_CODE_READER = 3; private static final int ACTIVITY_QR_CODE_READER = 3;
private ServiceTrackingDog serviceTrackingDog = null;
public static String appName = ""; public static String appName = "";
private MyLocationNewOverlay mLocationOverlay; private MyLocationNewOverlay mLocationOverlay;
private IOrientationProvider compass = null; private IOrientationProvider compass = null;
private ServiceXmpp serviceXmpp = null;
private ServiceTrackingDog serviceTrackingDog = null;
private Context ctx = null; private Context ctx = null;
private MapView map = null; private MapView map = null;
@ -170,9 +169,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
changeStatusTrace(); changeStatusTrace();
try { try {
if (serviceTrackingDog.isTraceurActivated()) { if (serviceTrackingDog.isTraceurActivated()) {
serviceXmpp.sendCommandStartTrail(); serviceTrackingDog.sendCommandStartTrail();
} else { } else {
serviceXmpp.sendCommandStopTrail(); serviceTrackingDog.sendCommandStopTrail();
} }
} catch (XmppStringprepException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException ex) { } 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()); GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
addMarker(gp, loc.isFound()); addMarker(gp, loc.isFound());
try { try {
serviceXmpp.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime()); serviceTrackingDog.sendCommandObjectTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime());
} catch (Exception e) { } catch (Exception e) {
} }
} }
@ -216,22 +215,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
getResources() getResources()
); );
serviceTrackingDog.addObserver(this, ServiceGps.class.getName()); serviceTrackingDog.addObserver(this, ServiceGps.class.getName());
} serviceTrackingDog.addObserver(this, ServiceXmpp.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();
}
} }
} }
@ -317,9 +301,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (serviceTrackingDog != null) { if (serviceTrackingDog != null) {
serviceTrackingDog.close(); serviceTrackingDog.close();
} }
if (serviceXmpp != null) {
serviceXmpp.close();
}
zoomed = false; zoomed = false;
} }
//</editor-fold> //</editor-fold>
@ -334,7 +315,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
vibrationObjectMenuItem.setChecked(serviceTrackingDog.isVibrationNearObjectEnabled()); vibrationObjectMenuItem.setChecked(serviceTrackingDog.isVibrationNearObjectEnabled());
MenuItem xmppObjectMenuItem = menu.findItem(R.id.action_active_xmpp); MenuItem xmppObjectMenuItem = menu.findItem(R.id.action_active_xmpp);
xmppObjectMenuItem.setChecked(serviceXmpp.isEnabled()); xmppObjectMenuItem.setChecked(serviceTrackingDog.isXmppEnabled());
return true; return true;
} }
@ -386,16 +367,16 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
item.setChecked(newStat); item.setChecked(newStat);
if (newStat) { if (newStat) {
try { try {
serviceXmpp.enable(); serviceTrackingDog.enableXmpp();
Toast.makeText(ctx, "Connexion XMPP réussie", Toast.LENGTH_SHORT).show(); Toast.makeText(ctx, "Connexion XMPP réussie", Toast.LENGTH_SHORT).show();
} catch (InterruptedException | IOException | SmackException | XMPPException e) { } catch (InterruptedException | IOException | SmackException | XMPPException e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(ctx, "Echec de connexion XMPP", Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Echec de connexion XMPP", Toast.LENGTH_LONG).show();
item.setChecked(false); item.setChecked(false);
serviceXmpp.disable(); serviceTrackingDog.disableXmpp();
} }
} else { } else {
serviceXmpp.disable(); serviceTrackingDog.disableXmpp();
} }
return true; return true;
} }
@ -436,15 +417,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (data.hasExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT)) { if (data.hasExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT)) {
String text = data.getStringExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT); String text = data.getStringExtra(QRCodeReaderActivity.EXTRA_SCANNED_TEXT);
try { try {
serviceXmpp.setOtherJid(text); serviceTrackingDog.setOtherJid(text);
serviceXmpp.sendPresenceAvailable();
Log.i(TAG, "TEXT: "+text); Log.i(TAG, "TEXT: "+text);
} catch (SmackException.NotConnectedException e) { } catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException e) {
Log.e(TAG, "Fail send presence", e);
} catch (InterruptedException e) {
Log.e(TAG, "Fail send presence", e);
} catch (XmppStringprepException e) {
Log.e(TAG, "Fail send presence", e); Log.e(TAG, "Fail send presence", e);
Toast.makeText(ctx, "Echec connexion avec le matériel", Toast.LENGTH_LONG).show();;
} }
} }
break; break;
@ -661,7 +638,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
public void update(Observable observable, Object o) { public void update(Observable observable, Object o) {
if (observable.getClass().getName().equals(ServiceGps.class.getName())) { if (observable.getClass().getName().equals(ServiceGps.class.getName())) {
updateServiceGps(o); updateServiceGps(o);
} else if (observable == serviceXmpp) { } else if (observable.getClass().getName().equals(ServiceXmpp.class.getName())) {
updateServiceXmpp(o); updateServiceXmpp(o);
} }
} }
@ -670,10 +647,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) { if (o instanceof String && o.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
MyLocation loc = serviceTrackingDog.getCurrentLocation(); MyLocation loc = serviceTrackingDog.getCurrentLocation();
onNewLocation(loc); 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 @Override
public void run() { public void run() {
if (serviceTrackingDog.isTraceurActivated()) { if (serviceTrackingDog.isTraceurActivated()) {
MyLocation loc = serviceXmpp.getCurrentLocation(); MyLocation loc = serviceTrackingDog.getXmppCurrentLocation();
onNewLocation(loc); onNewLocation(loc);
} }
} }
@ -694,7 +667,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
@Override @Override
public void run() { public void run() {
if (serviceTrackingDog.isTraceurActivated()) { if (serviceTrackingDog.isTraceurActivated()) {
MyLocation locObj = serviceXmpp.getLastObjectXmppLocation(); MyLocation locObj = serviceTrackingDog.getXmppLastObjectLocation();
WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj); WayPointLocation loc = serviceTrackingDog.addPointObjectTrail(locObj);
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
addMarker(gp, loc.isFound()); addMarker(gp, loc.isFound());
@ -721,7 +694,6 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
private void onNewLocation(MyLocation loc) { private void onNewLocation(MyLocation loc) {
if (loc != null) { if (loc != null) {
//serviceGps.addPoint(loc);
GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()); GeoPoint currentPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude());
if (mLocationOverlay.isFollowLocationEnabled()) { if (mLocationOverlay.isFollowLocationEnabled()) {
map.getController().setCenter(currentPoint); map.getController().setCenter(currentPoint);

View File

@ -7,9 +7,12 @@ import android.os.Build;
import android.os.VibrationEffect; import android.os.VibrationEffect;
import android.os.Vibrator; import android.os.Vibrator;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List; import java.util.List;
import java.util.Observable; import java.util.Observable;
@ -44,6 +47,7 @@ public class ServiceTrackingDog implements Observer {
ServiceXmpp sx = null; ServiceXmpp sx = null;
try { try {
sx = new ServiceXmpp(resources); sx = new ServiceXmpp(resources);
sx.addObserver(this);
} catch (UnknownHostException | XmppStringprepException ex) { } catch (UnknownHostException | XmppStringprepException ex) {
sx = null; sx = null;
} }
@ -84,18 +88,34 @@ public class ServiceTrackingDog implements Observer {
if (o != null && o instanceof String) { if (o != null && o instanceof String) {
String action = (String) o; String action = (String) o;
if (action.equals(ServiceGps.NOTIF_NEW_LOCATION)) { if (action.equals(ServiceGps.NOTIF_NEW_LOCATION)) {
onNewLocation(); MyLocation loc = onNewLocation();
shouldVibrate(); shouldVibrate();
if (serviceXmpp != null && loc != null) {
try {
serviceXmpp.sendCommandLocationTrail(loc.getLatitude(), loc.getLongitude(), loc.getTime());
} catch (Exception ex) {
}
}
} }
} }
} }
public void updateXmpp(Object o) { 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(); MyLocation loc = serviceGps.getCurrentLocation();
serviceGps.addPoint(loc); serviceGps.addPoint(loc);
return loc;
} }
//</editor-fold> //</editor-fold>
@ -224,4 +244,66 @@ public class ServiceTrackingDog implements Observer {
} }
//</editor-fold> //</editor-fold>
//</editor-fold> //</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Service XMPP delegation">
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;
}
//<editor-fold defaultstate="collapsed" desc="Commands">
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);
}
}
//</editor-fold>
//</editor-fold>
} }