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));
}
}
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();
}
}
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);
}
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;
}
}
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;
}
}
Aggregations