From 1361473eee5745c8ca5418fb657d28299dc22dbc Mon Sep 17 00:00:00 2001 From: chteufleur Date: Thu, 4 Oct 2018 18:05:05 +0200 Subject: [PATCH] Add menu to connect on XMPP. --- .../mytrackingdog/MainActivity.java | 22 ++++++++++++++++++ .../mytrackingdog/services/ServiceXmpp.java | 23 ++++++++++++++++--- app/src/main/res/menu/menu_main.xml | 6 +++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index dad2634..849045f 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -335,6 +335,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu MenuItem vibrationObjectMenuItem = menu.findItem(R.id.action_active_vibration_object); vibrationObjectMenuItem.setChecked(serviceGps.isVibrationNearObjectEnabled()); + + MenuItem xmppObjectMenuItem = menu.findItem(R.id.action_active_xmpp); + xmppObjectMenuItem.setChecked(serviceXmpp.isEnabled()); return true; } @@ -379,6 +382,25 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu item.setChecked(!checked); serviceGps.setVibrationNearObjectEnabled(!checked); return true; + + } else if (id == R.id.action_active_xmpp) { + boolean checked = item.isChecked(); + boolean newStat = !checked; + item.setChecked(newStat); + if (newStat) { + try { + serviceXmpp.enable(); + 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(); + } + } else { + serviceXmpp.disable(); + } + return true; } return super.onOptionsItemSelected(item); 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 1e8104b..5524627 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java @@ -65,6 +65,8 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { private Roster roster; private final ServiceXmpp thsi; + private boolean isEnable = false; + public ServiceXmpp(Resources resources) throws UnknownHostException, XmppStringprepException { this.resources = resources; this.configuration = XMPPTCPConnectionConfiguration.builder() @@ -80,6 +82,9 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { } public boolean connect() throws InterruptedException, XMPPException, SmackException, IOException { + if (!isEnable) { + return false; + } StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); @@ -116,9 +121,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { if (otherJid != null) { try { sendPresenceUnavailable(otherJid); - } catch (SmackException.NotConnectedException ex) { - } catch (InterruptedException ex) { - } catch (XmppStringprepException ex) { + } catch (SmackException.NotConnectedException | InterruptedException | XmppStringprepException ex) { } } if (connection != null) { @@ -126,6 +129,20 @@ public class ServiceXmpp extends Observable implements PresenceEventListener { } } + public void enable() throws InterruptedException, IOException, SmackException, XMPPException { + this.isEnable = true; + connect(); + } + + public void disable() { + this.isEnable = false; + close(); + } + + public boolean isEnabled() { + return isEnable; + } + // public void sendPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException { if (otherJid != null) { diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index b689f1f..c047005 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -28,4 +28,10 @@ android:orderInCategory="100" android:title="@string/action_qr_code_reader" app:showAsAction="never" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14a58d0..b63b015 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,4 +13,5 @@ Active vibration objets Affiche identifiant Lecture identifiant + Active XMPP