Search in sources :

Example 41 with AccountItem

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

the class XabberAccountManager method createSettingsList.

/**
 * @return list of settings from local account for patch to server
 */
public List<XMPPAccountSettings> createSettingsList() {
    List<XMPPAccountSettings> resultList = new ArrayList<>();
    Collection<AccountItem> localAccounts = AccountManager.getInstance().getAllAccountItems();
    for (AccountItem localAccount : localAccounts) {
        String localJid = localAccount.getAccount().getFullJid().asBareJid().toString();
        if (SettingsManager.isSyncAllAccounts() || isAccountSynchronize(localJid)) {
            XMPPAccountSettings item = new XMPPAccountSettings(localJid, true, localAccount.getTimestamp());
            item.setOrder(localAccount.getOrder());
            item.setColor(ColorManager.getInstance().convertIndexToColorName(localAccount.getColorIndex()));
            item.setTimestamp(localAccount.getTimestamp());
            resultList.add(item);
        }
    }
    return resultList;
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ArrayList(java.util.ArrayList)

Example 42 with AccountItem

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

the class NotificationManager method updatePersistentNotification.

private void updatePersistentNotification() {
    if (XabberService.getInstance() == null)
        return;
    // we do not want to show persistent notification if there are no enabled accounts
    XabberService.getInstance().changeForeground();
    if (!XabberService.getInstance().needForeground())
        return;
    Collection<AccountJid> accountList = AccountManager.getInstance().getEnabledAccounts();
    if (accountList.isEmpty()) {
        return;
    }
    int waiting = 0;
    int connecting = 0;
    int connected = 0;
    for (AccountJid account : accountList) {
        AccountItem accountItem = AccountManager.getInstance().getAccount(account);
        if (accountItem == null) {
            continue;
        }
        ConnectionState state = accountItem.getState();
        LogManager.i(this, "updatePersistentNotification account " + account + " state " + state);
        switch(state) {
            case offline:
                break;
            case waiting:
                waiting++;
                break;
            case connecting:
            case registration:
            case authentication:
                connecting++;
                break;
            case connected:
                connected++;
                break;
        }
    }
    final Intent persistentIntent;
    persistentIntent = ContactListActivity.createPersistentIntent(application);
    if (SyncManager.getInstance().isSyncMode()) {
        persistentNotificationBuilder.setColor(NotificationCompat.COLOR_DEFAULT);
        persistentNotificationBuilder.setSmallIcon(R.drawable.ic_sync);
        persistentNotificationBuilder.setContentText(application.getString(R.string.connection_state_sync));
    } else {
        if (connected > 0) {
            persistentNotificationBuilder.setColor(persistentNotificationColor);
            persistentNotificationBuilder.setSmallIcon(R.drawable.ic_stat_online);
        } else {
            persistentNotificationBuilder.setColor(NotificationCompat.COLOR_DEFAULT);
            persistentNotificationBuilder.setSmallIcon(R.drawable.ic_stat_offline);
        }
        persistentNotificationBuilder.setContentText(getConnectionState(waiting, connecting, connected, accountList.size()));
    }
    persistentNotificationBuilder.setContentIntent(PendingIntent.getActivity(application, 0, persistentIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    notify(PERSISTENT_NOTIFICATION_ID, persistentNotificationBuilder.build());
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ConnectionState(com.xabber.android.data.connection.ConnectionState)

Example 43 with AccountItem

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

the class AccountRosterListener method onRosterLoaded.

@Override
public void onRosterLoaded(Roster roster) {
    LogManager.i(getLogTag(), "onRosterLoaded");
    final AccountItem accountItem = AccountManager.getInstance().getAccount(AccountRosterListener.this.account);
    if (accountItem != null) {
        for (OnRosterReceivedListener listener : Application.getInstance().getManagers(OnRosterReceivedListener.class)) {
            listener.onRosterReceived(accountItem);
        }
    }
    AccountManager.getInstance().onAccountChanged(AccountRosterListener.this.account);
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem)

Example 44 with AccountItem

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

the class PushManager method onEndpointRegistered.

public void onEndpointRegistered(String jid, String pushServiceJid, String node) {
    LogManager.d(LOG_TAG, "Received endpoint-registered push. Send push enable iq.");
    AccountJid accountJid = null;
    Collection<AccountJid> accounts = AccountManager.getInstance().getEnabledAccounts();
    for (AccountJid account : accounts) {
        if ((account.getFullJid().asBareJid() + getAndroidId()).equals(jid)) {
            accountJid = account;
            break;
        }
    }
    if (accountJid != null) {
        AccountItem account = AccountManager.getInstance().getAccount(accountJid);
        if (account != null) {
            // save node to account
            AccountManager.getInstance().setPushNode(account, node, pushServiceJid);
            // update push nodes
            updateEnabledPushNodes();
            // enable push on XMPP-server
            sendEnablePushIQ(account, pushServiceJid, node);
        }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid)

Example 45 with AccountItem

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

the class UploadService method startWork.

private void startWork(AccountJid account, UserJid user, List<String> filePaths, CharSequence uploadServerUrl, String existMessageId) {
    // get account item
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        publishError(null, "Account not found");
        return;
    }
    // get upload jid
    Jid uploadJid;
    try {
        uploadJid = JidCreate.bareFrom(uploadServerUrl);
    } catch (XmppStringprepException e) {
        publishError(null, "Wrong upload jid");
        return;
    }
    final String fileMessageId;
    if (existMessageId == null) {
        // create fileMessage with files
        List<File> files = new ArrayList<>();
        for (String filePath : filePaths) {
            files.add(new File(filePath));
        }
        fileMessageId = MessageManager.getInstance().createFileMessage(account, user, files);
    } else
        // use existing fileMessage
        fileMessageId = existMessageId;
    HashMap<String, String> uploadedFilesUrls = new HashMap<>();
    List<String> notUploadedFilesPaths = new ArrayList<>();
    List<File> notUploadedFiles = new ArrayList<>();
    List<String> errors = new ArrayList<>();
    for (String filePath : filePaths) {
        if (needStop) {
            stopWork(fileMessageId);
            return;
        }
        try {
            File uncompressedFile = new File(filePath);
            final File file;
            // compress file if image
            if (FileManager.fileIsImage(uncompressedFile) && SettingsManager.connectionCompressImage()) {
                file = ImageCompressor.compressImage(uncompressedFile, getCompressedDirPath());
                if (file == null)
                    throw new Exception("Compress image failed");
            } else
                file = uncompressedFile;
            // request slot
            Stanza slot = requestSlot(accountItem, file, uploadJid);
            if (!(slot instanceof Slot))
                throw new Exception("Could not request upload slot");
            // upload file
            Response response = uploadFileToSlot(account, (Slot) slot, file);
            if (response.isSuccessful())
                uploadedFilesUrls.put(filePath, ((Slot) slot).getGetUrl());
            else
                throw new Exception("Upload failed: " + response.message());
        } catch (Exception e) {
            notUploadedFilesPaths.add(filePath);
            notUploadedFiles.add(new File(filePath));
            errors.add(e.toString());
        }
        publishProgress(fileMessageId, uploadedFilesUrls.size(), filePaths.size());
    }
    removeTempDirectory();
    // check that files are uploaded
    if (uploadedFilesUrls.size() == 0) {
        setErrorForMessage(fileMessageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
        publishError(fileMessageId, "Could not upload any files");
        return;
    }
    // save results to Realm and send message
    MessageManager.getInstance().updateFileMessage(account, user, fileMessageId, uploadedFilesUrls, notUploadedFilesPaths);
    publishCompleted(fileMessageId);
    // if some files have errors move its to separate message
    if (notUploadedFilesPaths.size() > 0) {
        String messageId = MessageManager.getInstance().createFileMessage(account, user, notUploadedFiles);
        setErrorForMessage(messageId, generateErrorDescriptionForFiles(notUploadedFilesPaths, errors));
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) Jid(org.jxmpp.jid.Jid) AccountItem(com.xabber.android.data.account.AccountItem) HashMap(java.util.HashMap) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) Response(okhttp3.Response) Slot(com.xabber.xmpp.httpfileupload.Slot) File(java.io.File)

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