Search in sources :

Example 1 with XMPPUser

use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.

the class FirebaseService method onTokenRefresh.

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    // Save the Token in Preferences
    Context context = getApplicationContext();
    PreferencesManager prefManager = PreferencesManager.getInstance(context);
    prefManager.setFirebaseDeviceID(refreshedToken);
    XMPPUser xmppUser = prefManager.getXMPPUser();
    if (xmppUser != null && xmppUser.getUsername() != null) {
        String authToken = prefManager.getAuthorizationToken();
        if (authToken != null) {
            NotificationUtil.sendDeviceTokenToServer(refreshedToken, xmppUser.getUsername(), authToken, context);
        }
    }
}
Also used : Context(android.content.Context) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser) PreferencesManager(com.yellowmessenger.sdk.utils.PreferencesManager)

Example 2 with XMPPUser

use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.

the class XMPPService method createConnection.

private void createConnection() throws XmppStringprepException, NoSuchAlgorithmException {
    if (mConnection == null) {
        AndroidSmackInitializer androidSmackInitializer = new AndroidSmackInitializer();
        androidSmackInitializer.initialize();
        ExtensionsInitializer extensionsInitializer = new ExtensionsInitializer();
        extensionsInitializer.initialize();
        XMPPUser xmppUser = PreferencesManager.getInstance(XMPPService.this.getApplicationContext()).getXMPPUser();
        XMPPTCPConnectionConfiguration connConfig = XMPPTCPConnectionConfiguration.builder().setXmppDomain(JidCreate.domainBareFrom(DOMAIN)).setHost(HOST).setPort(PORT).setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible).setCustomSSLContext(SSLContext.getInstance("TLS")).setSocketFactory(SSLSocketFactory.getDefault()).setUsernameAndPassword(xmppUser.getUsername(), xmppUser.getPassword()).build();
        SmackConfiguration.setDefaultPacketReplyTimeout(5000);
        XMPPTCPConnection.setUseStreamManagementDefault(true);
        XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
        mConnection = new XMPPTCPConnection(connConfig);
        mConnection.setPacketReplyTimeout(5000);
        mConnection.setPreferredResumptionTime(10);
        mConnection.setUseStreamManagement(true);
        mConnection.setUseStreamManagementResumption(true);
        mConnection.addAsyncStanzaListener(packetListener, packetFilter);
        mConnection.addAsyncStanzaListener(pingPacketListener, pingPacketFilter);
        mConnection.addStanzaAcknowledgedListener(new StanzaListener() {

            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
                // TODO Acknowledgement
                ChatMessage chatMessage = ChatMessageDAO.getChatMessageByStanzaId(packet.getStanzaId());
                if (chatMessage != null) {
                    chatMessage.setAcknowledged(true);
                    chatMessage.save();
                    EventBus.getDefault().post(new MessageAcknowledgementEvent(chatMessage, chatMessage.getAcknowledged()));
                }
            }
        });
        SASLAuthentication.unregisterSASLMechanism("org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism");
        SASLAuthentication.registerSASLMechanism(new CustomSCRAMSHA1Mechanism());
        mConnection.addConnectionListener(connectionListener);
        ServerPingWithAlarmManager.getInstanceFor(mConnection).setEnabled(true);
    // ReconnectionManager.getInstanceFor(mConnection).enableAutomaticReconnection();
    }
    try {
        if (!mConnection.isConnected() && !mConnection.isAuthenticated()) {
            mConnection.connect();
        } else if (mConnection.isConnected() && !mConnection.isAuthenticated()) {
            mConnection.login();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) CustomSCRAMSHA1Mechanism(com.yellowmessenger.sdk.xmpp.CustomSCRAMSHA1Mechanism) MessageAcknowledgementEvent(com.yellowmessenger.sdk.events.MessageAcknowledgementEvent) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AndroidSmackInitializer(org.jivesoftware.smack.android.AndroidSmackInitializer) ExtensionsInitializer(org.jivesoftware.smack.extensions.ExtensionsInitializer) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser)

Example 3 with XMPPUser

use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.

the class XMPPService method createUser.

private void createUser(final String username) {
    if (creatingUser) {
        return;
    }
    try {
        creatingUser = true;
        String salt = "a04aa6a74e76bf8f57b0e2e715138171";
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update((username + salt).getBytes());
        byte[] byteData = md.digest();
        // convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        String hash = sb.toString();
        String url = "https://sso.botplatform.io/createUser";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        jsonObject.put("hash", hash);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getBoolean("success") && PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
                        JSONObject data = response.getJSONObject("data");
                        PreferencesManager.getInstance(getBaseContext()).setXMPPUser(new XMPPUser(data.getString("username"), data.getString("password")));
                        creatingUser = false;
                        XMPPService.this.login();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    creatingUser = false;
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                creatingUser = false;
            }
        });
        jsonObjectRequest.setRetryPolicy(retryPolicy);
        if (PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
            queue.add(jsonObjectRequest);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : VolleyError(com.android.volley.VolleyError) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) MessageDigest(java.security.MessageDigest)

Example 4 with XMPPUser

use of com.yellowmessenger.sdk.models.XMPPUser in project yellowmessenger-sdk by yellowmessenger.

the class PreferencesManager method getXMPPUser.

public XMPPUser getXMPPUser() {
    String username = pref.getString("xmpp-token", null);
    String password = pref.getString("xmpp-key", null);
    return username != null ? new XMPPUser(username, password) : null;
}
Also used : XMPPUser(com.yellowmessenger.sdk.models.XMPPUser)

Aggregations

XMPPUser (com.yellowmessenger.sdk.models.XMPPUser)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SmackException (org.jivesoftware.smack.SmackException)2 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)2 Context (android.content.Context)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 MessageAcknowledgementEvent (com.yellowmessenger.sdk.events.MessageAcknowledgementEvent)1 ChatResponse (com.yellowmessenger.sdk.models.ChatResponse)1 ChatMessage (com.yellowmessenger.sdk.models.db.ChatMessage)1 PreferencesManager (com.yellowmessenger.sdk.utils.PreferencesManager)1 CustomSCRAMSHA1Mechanism (com.yellowmessenger.sdk.xmpp.CustomSCRAMSHA1Mechanism)1 MessageDigest (java.security.MessageDigest)1 StanzaListener (org.jivesoftware.smack.StanzaListener)1 AndroidSmackInitializer (org.jivesoftware.smack.android.AndroidSmackInitializer)1 ExtensionsInitializer (org.jivesoftware.smack.extensions.ExtensionsInitializer)1 Stanza (org.jivesoftware.smack.packet.Stanza)1 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)1 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)1