Add menu to connect on XMPP.

This commit is contained in:
chteufleur 2018-10-04 18:05:05 +02:00
parent 5246ab3595
commit 1361473eee
4 changed files with 49 additions and 3 deletions

View File

@ -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);

View File

@ -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;
}
//<editor-fold defaultstate="collapsed" desc="Presence">
public void sendPresenceAvailable() throws SmackException.NotConnectedException, InterruptedException, XmppStringprepException {
if (otherJid != null) {

View File

@ -28,4 +28,10 @@
android:orderInCategory="100"
android:title="@string/action_qr_code_reader"
app:showAsAction="never" />
<item
android:id="@+id/action_active_xmpp"
android:checkable="true"
android:orderInCategory="100"
android:title="@string/action_active_xmpp"
app:showAsAction="never" />
</menu>

View File

@ -13,4 +13,5 @@
<string name="action_active_vibration_object">Active vibration objets</string>
<string name="action_qr_code_generator">Affiche identifiant</string>
<string name="action_qr_code_reader">Lecture identifiant</string>
<string name="action_active_xmpp">Active XMPP</string>
</resources>