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