@Override public void newConnection(XMPPConnection connection) { mConnection = connection; mRoster = Roster.getInstanceFor(connection); mRoster.addRosterListener(this); mRoster.addSubscribeListener(new SubscribeListener() { @Override public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) { Set<EntityBareJid> masterJids = mSettings.getMasterJids(); for (EntityBareJid masterJid : masterJids) { if (masterJid.equals(from)) { return SubscribeAnswer.Approve; } } return SubscribeAnswer.Deny; } }); }
private XmppIotDataControl(Context context) { mContext = context.getApplicationContext(); mXmppManager = XmppManager.getInstance(mContext); mSettings = Settings.getInstance(mContext); mXmppManager.addXmppConnectionStatusListener((ma) -> { Roster roster = Roster.getInstanceFor(ma.getConnection()); roster.addSubscribeListener((from, subRequest) -> { if (!mSettings.isIdentityModeApp()) return null; if (!mSettings.isMutualSubscriptionModeEnabled()) return null; if (from.equals(mSettings.getThingJid())) { return SubscribeListener.SubscribeAnswer.Approve; } return null; }); ma.addListener(new AbstractManagedXmppConnectionListener() { @Override public void authenticated(XMPPConnection connection, boolean resumed) { withMainActivity((ma) -> setGuiElements(ma, true)); if (resumed) return; if (!mSettings.isIdentityModeApp()) return; final EntityBareJid thingJid = mSettings.getThingJid(); XmppManager.emptyRoster(connection, thingJid); if (thingJid == null) return; IoTProvisioningManager provisioningManager = IoTProvisioningManager.getInstanceFor(connection); if (!provisioningManager.iAmFriendOf(thingJid)) { withMainActivity((ma) -> Toast.makeText(ma, "Trying to befriend " + thingJid, Toast.LENGTH_SHORT).show()); try { provisioningManager.sendFriendshipRequest(thingJid); } catch (SmackException.NotConnectedException | InterruptedException e) { LOGGER.log(Level.WARNING, "Could not befriend thing", e); } } else { LOGGER.info("We are already a friend of " + thingJid + ". Not sending friendship request."); } provisioningManager.addBecameFriendListener((friend, presence) -> { if (!mSettings.isIdentityModeApp()) return; withMainActivity((c) -> Toast.makeText(c, "We are now a friend of " + friend, Toast.LENGTH_SHORT).show()); }); provisioningManager.addWasUnfriendedListener((friend, presence) -> { if (!mSettings.isIdentityModeApp()) return; withMainActivity((c) -> Toast.makeText(c, "We are no longer a friend of " + friend, Toast.LENGTH_SHORT).show()); }); } @Override public void terminated() { withMainActivity((ma) -> setGuiElements(ma, false)); } }); }); }