Search in sources :

Example 16 with UserJid

use of com.xabber.android.data.entity.UserJid 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)

Example 17 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class AccountActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    account = getAccount(intent);
    if (account == null) {
        LogManager.i(LOG_TAG, "Account is null, finishing!");
        finish();
        return;
    }
    accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        Application.getInstance().onError(R.string.NO_SUCH_ACCOUNT);
        finish();
        return;
    }
    if (ACTION_CONNECTION_SETTINGS.equals(intent.getAction())) {
        isConnectionSettingsAction = true;
        startAccountSettingsActivity();
        setIntent(null);
    }
    setContentView(R.layout.activity_account);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_default);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(AccountActivity.this);
        }
    });
    toolbar.setTitle(R.string.contact_account);
    toolbar.inflateMenu(R.menu.toolbar_account);
    MenuItem item = toolbar.getMenu().findItem(R.id.action_account_switch);
    switchCompat = (SwitchCompat) item.getActionView().findViewById(R.id.account_switch_view);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            AccountManager.getInstance().setEnabled(accountItem.getAccount(), isChecked);
        }
    });
    barPainter = new BarPainter(this, toolbar);
    UserJid fakeAccountUser;
    try {
        fakeAccountUser = UserJid.from(account.getFullJid().asBareJid());
    } catch (UserJid.UserJidCreateException e) {
        throw new IllegalStateException();
    }
    bestContact = RosterManager.getInstance().getBestContact(account, fakeAccountUser);
    contactTitleView = findViewById(R.id.contact_title_expanded);
    statusIcon = findViewById(R.id.ivStatus);
    statusText = (TextView) findViewById(R.id.status_text);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.account_options_recycler_view);
    accountOptionsAdapter = new AccountOptionsAdapter(AccountOption.getValues(), this, accountItem);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(accountOptionsAdapter);
    recyclerView.setNestedScrollingEnabled(false);
    Fragment fragmentById = getFragmentManager().findFragmentById(R.id.account_fragment_container);
    if (fragmentById == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.account_fragment_container, ContactVcardViewerFragment.newInstance(account)).commit();
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) Intent(android.content.Intent) MenuItem(android.view.MenuItem) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) ContactVcardViewerFragment(com.xabber.android.ui.fragment.ContactVcardViewerFragment) Fragment(android.app.Fragment) BarPainter(com.xabber.android.ui.color.BarPainter) AccountOptionsAdapter(com.xabber.android.ui.adapter.accountoptions.AccountOptionsAdapter) RecyclerView(androidx.recyclerview.widget.RecyclerView) CompoundButton(android.widget.CompoundButton) Toolbar(androidx.appcompat.widget.Toolbar)

Example 18 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class ChatActivity method handleOtrIntent.

private void handleOtrIntent(Intent intent) {
    String account = intent.getStringExtra(KEY_ACCOUNT);
    String user = intent.getStringExtra(KEY_USER);
    String question = intent.getStringExtra(KEY_QUESTION);
    if (account != null && user != null) {
        try {
            AccountJid accountJid = AccountJid.from(account);
            UserJid userJid = UserJid.from(user);
            AbstractChat chat = MessageManager.getInstance().getOrCreateChat(accountJid, userJid);
            if (chat != null && chat instanceof RegularChat) {
                if (intent.getBooleanExtra(EXTRA_OTR_PROGRESS, false)) {
                    ((RegularChat) chat).setIntent(QuestionActivity.createCancelIntent(Application.getInstance(), accountJid, userJid));
                } else {
                    ((RegularChat) chat).setIntent(QuestionActivity.createIntent(Application.getInstance(), accountJid, userJid, question != null, true, question));
                }
            }
        } catch (UserJid.UserJidCreateException | XmppStringprepException e) {
            e.printStackTrace();
        }
    }
    getIntent().removeExtra(EXTRA_OTR_REQUEST);
    getIntent().removeExtra(EXTRA_OTR_PROGRESS);
}
Also used : AbstractChat(com.xabber.android.data.message.AbstractChat) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) RegularChat(com.xabber.android.data.message.RegularChat)

Example 19 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class ChatActivity method getUser.

@Nullable
private static UserJid getUser(Intent intent) {
    UserJid value = EntityIntentBuilder.getUser(intent);
    if (value != null)
        return value;
    // Backward compatibility.
    String stringExtra = intent.getStringExtra("com.xabber.android.data.user");
    if (stringExtra == null) {
        return null;
    }
    try {
        return UserJid.from(stringExtra);
    } catch (UserJid.UserJidCreateException e) {
        LogManager.exception(LOG_TAG, e);
        return null;
    }
}
Also used : UserJid(com.xabber.android.data.entity.UserJid) Nullable(androidx.annotation.Nullable)

Example 20 with UserJid

use of com.xabber.android.data.entity.UserJid in project xabber-android by redsolution.

the class XMPPAuthManager method addContactToRoster.

private void addContactToRoster(String apiJid, String clientJid) {
    UserJid user;
    AccountJid account;
    try {
        user = UserJid.from(apiJid);
        account = AccountJid.from(clientJid);
        RosterManager.getInstance().createContact(account, user, "xabber", Collections.EMPTY_LIST);
        PresenceManager.getInstance().requestSubscription(account, user, false);
    } catch (UserJid.UserJidCreateException | XmppStringprepException | InterruptedException | SmackException | NetworkException | XMPPException.XMPPErrorException e) {
        LogManager.exception(this, e);
        return;
    }
}
Also used : AccountJid(com.xabber.android.data.entity.AccountJid) SmackException(org.jivesoftware.smack.SmackException) UserJid(com.xabber.android.data.entity.UserJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

UserJid (com.xabber.android.data.entity.UserJid)67 AccountJid (com.xabber.android.data.entity.AccountJid)43 AbstractChat (com.xabber.android.data.message.AbstractChat)15 ArrayList (java.util.ArrayList)15 Intent (android.content.Intent)9 Jid (org.jxmpp.jid.Jid)9 Presence (org.jivesoftware.smack.packet.Presence)8 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)8 AccountItem (com.xabber.android.data.account.AccountItem)7 AbstractContact (com.xabber.android.data.roster.AbstractContact)6 Message (org.jivesoftware.smack.packet.Message)6 Resourcepart (org.jxmpp.jid.parts.Resourcepart)6 NetworkException (com.xabber.android.data.NetworkException)5 IFlexible (eu.davidea.flexibleadapter.items.IFlexible)5 SmackException (org.jivesoftware.smack.SmackException)5 Uri (android.net.Uri)4 ContactVO (com.xabber.android.presentation.ui.contactlist.viewobjects.ContactVO)4 DomainBareJid (org.jxmpp.jid.DomainBareJid)4 TextView (android.widget.TextView)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)3