Search in sources :

Example 71 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class HttpFileUploadManager method uploadFile.

public void uploadFile(final AccountJid account, final UserJid user, final String filePath) {
    final Jid uploadServerUrl = uploadServers.get(account);
    if (uploadServerUrl == null) {
        return;
    }
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return;
    }
    final File file = new File(filePath);
    final com.xabber.xmpp.httpfileupload.Request httpFileUpload = new com.xabber.xmpp.httpfileupload.Request();
    httpFileUpload.setFilename(file.getName());
    httpFileUpload.setSize(String.valueOf(file.length()));
    httpFileUpload.setTo(uploadServerUrl);
    try {
        accountItem.getConnection().sendIqWithResponseCallback(httpFileUpload, new StanzaListener() {

            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
                if (!(packet instanceof Slot)) {
                    return;
                }
                uploadFileToSlot(account, (Slot) packet);
            }

            private void uploadFileToSlot(final AccountJid account, final Slot slot) {
                SSLSocketFactory sslSocketFactory = null;
                MemorizingTrustManager mtm = CertificateManager.getInstance().getNewFileUploadManager(account);
                final SSLContext sslContext;
                try {
                    sslContext = SSLContext.getInstance("SSL");
                    sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
                    sslSocketFactory = sslContext.getSocketFactory();
                } catch (NoSuchAlgorithmException | KeyManagementException e) {
                    return;
                }
                OkHttpClient client = new OkHttpClient().newBuilder().sslSocketFactory(sslSocketFactory).hostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier())).writeTimeout(5, TimeUnit.MINUTES).connectTimeout(5, TimeUnit.MINUTES).readTimeout(5, TimeUnit.MINUTES).build();
                Request request = new Request.Builder().url(slot.getPutUrl()).put(RequestBody.create(CONTENT_TYPE, file)).build();
                final String fileMessageId;
                fileMessageId = MessageManager.getInstance().createFileMessage(account, user, file);
                LogManager.i(HttpFileUploadManager.this, "starting upload file to " + slot.getPutUrl() + " size " + file.length());
                client.newCall(request).enqueue(new Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        LogManager.i(HttpFileUploadManager.this, "onFailure " + e.getMessage());
                        MessageManager.getInstance().updateMessageWithError(fileMessageId, e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        LogManager.i(HttpFileUploadManager.this, "onResponse " + response.isSuccessful() + " " + response.body().string());
                        if (response.isSuccessful()) {
                            MessageManager.getInstance().updateFileMessage(account, user, fileMessageId, slot.getGetUrl());
                        } else {
                            MessageManager.getInstance().updateMessageWithError(fileMessageId, response.message());
                        }
                    }
                });
            }
        }, new ExceptionCallback() {

            @Override
            public void processException(Exception exception) {
                LogManager.i(this, "On HTTP file upload slot error");
                LogManager.exception(this, exception);
                Application.getInstance().onError(R.string.http_file_upload_slot_error);
            }
        });
    } catch (SmackException.NotConnectedException | InterruptedException e) {
        LogManager.exception(this, e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AccountItem(com.xabber.android.data.account.AccountItem) StanzaListener(org.jivesoftware.smack.StanzaListener) AccountJid(com.xabber.android.data.entity.AccountJid) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Call(okhttp3.Call) UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) Jid(org.jxmpp.jid.Jid) Stanza(org.jivesoftware.smack.packet.Stanza) Request(okhttp3.Request) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) MemorizingTrustManager(de.duenndns.ssl.MemorizingTrustManager) Response(okhttp3.Response) Callback(okhttp3.Callback) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) X509TrustManager(javax.net.ssl.X509TrustManager) Slot(com.xabber.xmpp.httpfileupload.Slot) File(java.io.File)

Example 72 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class MamManager method requestPreviousHistory.

public void requestPreviousHistory(final AbstractChat chat) {
    if (chat == null || chat.isRemotePreviousHistoryCompletelyLoaded()) {
        return;
    }
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null || !accountItem.getFactualStatusMode().isOnline()) {
        return;
    }
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            if (!checkSupport(accountItem)) {
                return;
            }
            String firstMamMessageMamId;
            boolean remoteHistoryCompletelyLoaded;
            {
                Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
                SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
                firstMamMessageMamId = syncInfo.getFirstMamMessageMamId();
                remoteHistoryCompletelyLoaded = syncInfo.isRemoteHistoryCompletelyLoaded();
                realm.close();
            }
            if (remoteHistoryCompletelyLoaded) {
                chat.setRemotePreviousHistoryCompletelyLoaded(true);
            }
            if (firstMamMessageMamId == null || remoteHistoryCompletelyLoaded) {
                return;
            }
            org.jivesoftware.smackx.mam.MamManager mamManager = org.jivesoftware.smackx.mam.MamManager.getInstanceFor(accountItem.getConnection());
            final org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult;
            try {
                EventBus.getDefault().post(new PreviousHistoryLoadStartedEvent(chat));
                LogManager.i("MAM", "Loading previous history");
                mamQueryResult = mamManager.pageBefore(chat.getUser().getJid(), firstMamMessageMamId, PAGE_SIZE);
            } catch (SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
                LogManager.exception(this, e);
                EventBus.getDefault().post(new PreviousHistoryLoadFinishedEvent(chat));
                return;
            }
            EventBus.getDefault().post(new PreviousHistoryLoadFinishedEvent(chat));
            LogManager.i("MAM", "queryArchive finished. fin count expected: " + mamQueryResult.mamFin.getRSMSet().getCount() + " real: " + mamQueryResult.forwardedMessages.size());
            Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
            List<MessageItem> messageItems = getMessageItems(mamQueryResult, chat);
            syncMessages(realm, chat, messageItems);
            updatePreviousHistorySyncInfo(realm, chat, mamQueryResult, messageItems);
            realm.close();
        }
    });
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) SmackException(org.jivesoftware.smack.SmackException) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) ArrayList(java.util.ArrayList) List(java.util.List) XMPPException(org.jivesoftware.smack.XMPPException) Realm(io.realm.Realm)

Example 73 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class BlockingManager method getBlockingCommandManager.

@SuppressWarnings("WeakerAccess")
@Nullable
BlockingCommandManager getBlockingCommandManager(AccountJid account) {
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return null;
    }
    XMPPTCPConnection connection = accountItem.getConnection();
    // "Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once"
    if (connection.getUser() == null) {
        return null;
    }
    return BlockingCommandManager.getInstanceFor(connection);
}
Also used : XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) AccountItem(com.xabber.android.data.account.AccountItem) Nullable(androidx.annotation.Nullable)

Example 74 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class ClientStateManager method sendClientState.

protected static void sendClientState(Nonza nonza) {
    AccountManager accountManager = AccountManager.getInstance();
    for (AccountJid accountName : accountManager.getEnabledAccounts()) {
        AccountItem accountItem = accountManager.getAccount(accountName);
        if (accountItem == null) {
            continue;
        }
        AbstractXMPPConnection xmppConnection = accountItem.getConnection();
        if (!xmppConnection.isAuthenticated()) {
            continue;
        }
        if (xmppConnection.hasFeature("csi", ClientStateIndication.NAMESPACE))
            try {
                xmppConnection.sendNonza(nonza);
            } catch (SmackException.NotConnectedException | InterruptedException e) {
                LogManager.exception(LOG_TAG, e);
            }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) SmackException(org.jivesoftware.smack.SmackException) AccountManager(com.xabber.android.data.account.AccountManager) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 75 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class BookmarksManager method getUrlFromBookmarks.

public List<BookmarkedURL> getUrlFromBookmarks(AccountJid accountJid) {
    AccountItem accountItem = AccountManager.getInstance().getAccount(accountJid);
    List<BookmarkedURL> urls = Collections.emptyList();
    if (accountItem != null) {
        BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(accountItem.getConnection());
        try {
            urls = bookmarkManager.getBookmarkedURLs();
        } catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
            LogManager.exception(this, e);
        }
    }
    return urls;
}
Also used : BookmarkedURL(org.jivesoftware.smackx.bookmarks.BookmarkedURL) AccountItem(com.xabber.android.data.account.AccountItem) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager)

Aggregations

AccountItem (com.xabber.android.data.account.AccountItem)96 AccountJid (com.xabber.android.data.entity.AccountJid)24 UserJid (com.xabber.android.data.entity.UserJid)14 View (android.view.View)12 NetworkException (com.xabber.android.data.NetworkException)11 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Message (org.jivesoftware.smack.packet.Message)11 Presence (org.jivesoftware.smack.packet.Presence)10 TextView (android.widget.TextView)9 XMPPException (org.jivesoftware.smack.XMPPException)9 Jid (org.jxmpp.jid.Jid)9 ImageView (android.widget.ImageView)8 StatusMode (com.xabber.android.data.account.StatusMode)6 ConnectionState (com.xabber.android.data.connection.ConnectionState)5 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Realm (io.realm.Realm)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 AccountManager (com.xabber.android.data.account.AccountManager)4