diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus')
8 files changed, 233 insertions, 28 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java index f60efb56..038369ca 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteRemoteFileService.java @@ -29,12 +29,16 @@ public class DeleteRemoteFileService implements SimpleUserDecisionCallback { path = this.message.getFileParams().getUrl(); } - DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, this.message); - Account account = this.message.getConversation().getAccount(); - Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); - IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); - MessageUtil.setAndSaveFileStatus(this.message, FileStatus.DELETING); - XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); + if (null != path) { + DeleteRemoteFile remoteFile = new DeleteRemoteFile(path, this.message); + Account account = this.message.getConversation().getAccount(); + Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); + if (null != host) { + IqPacket request = FileTransferHttpDeleteSlotRequestPacketGenerator.generate(host, path); + MessageUtil.setAndSaveFileStatus(this.message, FileStatus.DELETING); + XmppSendUtil.sendIqPacket(account, request, new DeleteTokenReceived(remoteFile)); + } + } } } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java index 3151ca30..ef0032e3 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/delete/DeleteTokenReceived.java @@ -1,17 +1,23 @@ package de.thedevstack.conversationsplus.services.filetransfer.http.delete; +import android.widget.Toast; + import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import de.thedevstack.android.logcat.Logging; + import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.http.HttpClient; import de.thedevstack.conversationsplus.utils.MessageUtil; +import de.thedevstack.conversationsplus.utils.ui.ConversationsPlusToast; +import de.thedevstack.conversationsplus.xmpp.exceptions.ServiceUnavailableException; import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.delete.DeleteSlotPacketParser; +import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.OnIqPacketReceived; @@ -83,6 +89,11 @@ public class DeleteTokenReceived implements OnIqPacketReceived { } catch (XmppException e) { Logging.e("filetransfer.http.delete", "Error while trying to get the delete token: " + e.getMessage()); + int messageResId = R.string.cplus_remote_file_delete_failed; + if (e instanceof ServiceUnavailableException) { + messageResId = R.string.cplus_remote_file_delete_service_unavailable; + } + ConversationsPlusToast.makeErrorToast(messageResId, Toast.LENGTH_LONG); MessageUtil.setAndSaveFileStatus(remoteFile.getMessage(), FileStatus.DELETE_FAILED); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java index 7fe12dc6..0ab6a8ec 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/filetransfer/http/upload/HttpUploadFileTransferService.java @@ -9,8 +9,10 @@ import de.thedevstack.android.logcat.Logging; import eu.siacs.conversations.services.AbstractConnectionManager; import de.thedevstack.conversationsplus.services.FileTransferService; import de.thedevstack.conversationsplus.services.filetransfer.AbstractFileTransferService; +import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUpload; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload.HttpUploadRequestSlotPacketGenerator; @@ -62,19 +64,29 @@ public class HttpUploadFileTransferService extends AbstractFileTransferService i file.setExpectedSize(inputStreamAndExpectedSize.second); Logging.d("httpupload", "Requesting upload slot for file upload"); - Jid host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); - IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); - XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); - MessageUtil.markMessage(message, Message.STATUS_UNSEND); + Jid host = this.getHost(account); + if (null != host) { + IqPacket request = HttpUploadRequestSlotPacketGenerator.generate(host, file.getName(), file.getSize(), file.getMimeType()); + XmppSendUtil.sendIqPacket(account, request, new HttpUploadSlotRequestReceived(entity)); + MessageUtil.markMessage(message, Message.STATUS_UNSEND); - Logging.d("httpupload", "Upload slot for file upload requested"); - started = true; + Logging.d("httpupload", "Upload slot for file upload requested"); + started = true; + } } catch (FileNotFoundException e) { Logging.e("httpupload", "Could not find file, exception message: " + e.getMessage()); } return started; } + private Jid getHost(Account account) { + Jid host = account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE); + if (null == host) { + host = account.getXmppConnection().findDiscoItemByFeature(HttpUpload.NAMESPACE); + } + return host; + } + /** * Checks whether a message can be sent using this service or not. * @@ -87,6 +99,6 @@ public class HttpUploadFileTransferService extends AbstractFileTransferService i && null != message.getConversation() && null != message.getConversation().getAccount() && null != message.getFileParams() - && message.getConversation().getAccount().httpUploadAvailable(message.getFileParams().getSize()); + && AccountUtil.isHttpUploadAvailable(message.getConversation().getAccount(), message.getFileParams().getSize()); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java new file mode 100644 index 00000000..06c6b6ab --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/AccountUtil.java @@ -0,0 +1,34 @@ +package de.thedevstack.conversationsplus.utils; + +import eu.siacs.conversations.entities.Account; + +/** + * Utility class to work with accounts. + */ +public final class AccountUtil { + + public static boolean isHttpUploadAvailable(Account account, long filesize) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().httpUpload(filesize); + } + + public static boolean isHttpUploadAvailable(Account account) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().httpUpload(0); + } + + public static boolean isFileTransferHttpAvailable(Account account) { + return null != account + && null != account.getXmppConnection() + && null != account.getXmppConnection().getFeatures() + && account.getXmppConnection().getFeatures().hasFeatureFileTransferHttp(0); + } + + private AccountUtil() { + // avoid instantiation of utility class + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java new file mode 100644 index 00000000..3ebb8602 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ConversationsPlusToast.java @@ -0,0 +1,102 @@ +package de.thedevstack.conversationsplus.utils.ui; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.StringRes; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ImageView; +import android.widget.Toast; + +import de.thedevstack.conversationsplus.ConversationsPlusApplication; + +import eu.siacs.conversations.R; + +/** + * Wrapper for custom styled Toasts for Conversations+. + */ +public final class ConversationsPlusToast { + + /** + * Creates an error toast with text and duration + * @param text + * @param duration + */ + public static void makeErrorToast(final CharSequence text, final int duration) { + Handler toastHandler = new Handler(Looper.getMainLooper()); + toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration, true)); + } + + /** + * Creates an error toast with text from resource and duration + * @param resId + * @param duration + */ + public static void makeErrorToast(@StringRes int resId, int duration) { + makeErrorToast(ConversationsPlusApplication.getAppContext().getString(resId), duration); + } + + /** + * Creates an toast with text and duration + * @param text + * @param duration + */ + public static void makeToast(final CharSequence text, final int duration) { + Handler toastHandler = new Handler(Looper.getMainLooper()); + toastHandler.post(new ConversationsPlusToast.ToastRunnable(text, duration)); + } + + /** + * Creates an toast with text from resource and duration + * @param resId + * @param duration + */ + public static void makeToast(@StringRes int resId, int duration) { + makeToast(ConversationsPlusApplication.getAppContext().getString(resId), duration); + } + + private ConversationsPlusToast() { + // avoid instantiation - helper class + } + + /** + * Runnable to show the toast in an UI thread. + */ + static class ToastRunnable implements Runnable { + private boolean showErrorToast = false; + private CharSequence text; + private int duration; + + public ToastRunnable(CharSequence text, int duration, boolean isError) { + this.showErrorToast = isError; + this.text = text; + this.duration = duration; + } + + public ToastRunnable(CharSequence text, int duration) { + this.text = text; + this.duration = duration; + } + + @Override + public void run() { + Context applicationContext = ConversationsPlusApplication.getAppContext(); + LayoutInflater inflater = LayoutInflater.from(applicationContext); + View layout = inflater.inflate(R.layout.cplus_toast_container, null); + + TextViewUtil.setText(layout, R.id.cplus_toast_txt, text); + + if (this.showErrorToast) { + // Set image view to error icon + ImageView iv = (ImageView) layout.findViewById(R.id.cplus_toast_icon); + iv.setImageResource(R.drawable.ic_toast_error); + } + + Toast toast = new Toast(applicationContext); + toast.setDuration(duration); + toast.setView(layout); + toast.show(); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java index a775dad6..27a269f2 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java @@ -5,9 +5,9 @@ import android.view.View; import android.widget.TextView; /** - * Created by steckbrief on 29.03.2016. + * */ -public final class TextViewUtil { +public final class TextViewUtil extends ViewUtil { public static void setText(View parentView, int textViewId, CharSequence text) { TextView tv = (TextView) parentView.findViewById(textViewId); diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/ViewUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ViewUtil.java new file mode 100644 index 00000000..77422587 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/ViewUtil.java @@ -0,0 +1,39 @@ +package de.thedevstack.conversationsplus.utils.ui; + +import android.support.annotation.IdRes; +import android.view.View; + +/** + * Created by steckbrief on 11.01.2017. + */ + +public class ViewUtil { + + public static <T extends View> T visible(View parentView, @IdRes int textViewId) { + T tv = (T) parentView.findViewById(textViewId); + if (null != tv) { + tv.setVisibility(View.VISIBLE); + } + + return tv; + } + + public static <T extends View> T invisible(View parentView, @IdRes int textViewId) { + T tv = (T) parentView.findViewById(textViewId); + if (null != tv) { + tv.setVisibility(View.INVISIBLE); + } + + return tv; + } + + public static <T extends View> T gone(View parentView, @IdRes int textViewId) { + T tv = (T) parentView.findViewById(textViewId); + if (null != tv) { + tv.setVisibility(View.GONE); + } + + return tv; + } + +} diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java index 15771248..93e525a8 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java @@ -15,20 +15,23 @@ import eu.siacs.conversations.xml.Element; public final class ErrorIqPacketExceptionHelper { private final static String ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; - public static void throwIqErrorException(Element packet) throws IqPacketErrorException { - if (hasErrorElement(packet, "bad-request")) { - throw new BadRequestIqErrorException(packet, getErrorText(packet)); + public static void throwIqErrorException(Element errorIqPacket) throws IqPacketErrorException { + Element packet = IqPacketParser.findChild(errorIqPacket, "error", "jabber:client"); + if (null != packet) { + if (hasErrorElement(packet, "bad-request")) { + throw new BadRequestIqErrorException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "service-unavailable")) { + throw new ServiceUnavailableException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "internal-server-error")) { + throw new InternalServerErrorException(errorIqPacket, getErrorText(packet)); + } + if (hasErrorElement(packet, "undefined-condition")) { + throw new UndefinedConditionException(errorIqPacket, getErrorText(packet)); + } } - if (hasErrorElement(packet, "service-unavailable")) { - throw new ServiceUnavailableException(packet, getErrorText(packet)); - } - if (hasErrorElement(packet, "internal-server-error")) { - throw new InternalServerErrorException(packet, getErrorText(packet)); - } - if (hasErrorElement(packet, "undefined-condition")) { - throw new UndefinedConditionException(packet, getErrorText(packet)); - } - throw new IqPacketErrorException(packet, "Unknown error packet."); + throw new IqPacketErrorException(errorIqPacket, "Unknown error packet."); } private static boolean hasErrorElement(Element packet, String elementName) { |