From edd58f19b46340f3c9e2182441c9f749754cfa5c Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Fri, 16 Jan 2015 00:36:11 -0500 Subject: [PATCH] Add download button on notification if applicable --- .../conversations/entities/Conversation.java | 12 ++-- .../services/NotificationService.java | 67 +++++++++++++----- .../ui/ConversationActivity.java | 20 ++++-- .../res/drawable-hdpi/ic_action_download.png | Bin 0 -> 363 bytes .../res/drawable-mdpi/ic_action_download.png | Bin 0 -> 313 bytes .../res/drawable-xhdpi/ic_action_download.png | Bin 0 -> 470 bytes .../drawable-xxhdpi/ic_action_download.png | Bin 0 -> 574 bytes src/main/res/values/strings.xml | 1 + 8 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 src/main/res/drawable-hdpi/ic_action_download.png create mode 100644 src/main/res/drawable-mdpi/ic_action_download.png create mode 100644 src/main/res/drawable-xhdpi/ic_action_download.png create mode 100644 src/main/res/drawable-xxhdpi/ic_action_download.png diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 470bd2902..036acf635 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -108,9 +108,9 @@ public class Conversation extends AbstractEntity implements Blockable { } } - public void findMessagesWithFiles(OnMessageFound onMessageFound) { + public void findMessagesWithFiles(final OnMessageFound onMessageFound) { synchronized (this.messages) { - for (Message message : this.messages) { + for (final Message message : this.messages) { if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) && message.getEncryption() != Message.ENCRYPTION_PGP) { onMessageFound.onMessageFound(message); @@ -119,14 +119,14 @@ public class Conversation extends AbstractEntity implements Blockable { } } - public Message findMessageWithFileAndUuid(String uuid) { + public Message findMessageWithFileAndUuid(final String uuid) { synchronized (this.messages) { - for (Message message : this.messages) { - if (message.getType() == Message.TYPE_IMAGE + for (final Message message : this.messages) { + if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) && message.getEncryption() != Message.ENCRYPTION_PGP && message.getUuid().equals(uuid)) { return message; - } + } } } return null; diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index a32bb2797..efdd2b608 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -44,7 +44,7 @@ import eu.siacs.conversations.utils.UIHelper; public class NotificationService { - private XmppConnectionService mXmppConnectionService; + private final XmppConnectionService mXmppConnectionService; private final LinkedHashMap> notifications = new LinkedHashMap<>(); @@ -56,7 +56,7 @@ public class NotificationService { private boolean mIsInForeground; private long mLastNotification; - public NotificationService(XmppConnectionService service) { + public NotificationService(final XmppConnectionService service) { this.mXmppConnectionService = service; } @@ -214,17 +214,17 @@ public class NotificationService { private Builder buildMultipleConversation() { final Builder mBuilder = new NotificationCompat.Builder( mXmppConnectionService); - NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); + final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); style.setBigContentTitle(notifications.size() + " " + mXmppConnectionService .getString(R.string.unread_conversations)); final StringBuilder names = new StringBuilder(); Conversation conversation = null; - for (ArrayList messages : notifications.values()) { + for (final ArrayList messages : notifications.values()) { if (messages.size() > 0) { conversation = messages.get(0).getConversation(); - String name = conversation.getName(); + final String name = conversation.getName(); style.addLine(Html.fromHtml("" + name + " " + UIHelper.getMessagePreview(mXmppConnectionService,messages.get(0)).first)); names.append(name); @@ -241,8 +241,7 @@ public class NotificationService { mBuilder.setContentText(names.toString()); mBuilder.setStyle(style); if (conversation != null) { - mBuilder.setContentIntent(createContentIntent(conversation - .getUuid())); + mBuilder.setContentIntent(createContentIntent(conversation)); } return mBuilder; } @@ -256,14 +255,24 @@ public class NotificationService { mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService() .get(conversation, getPixel(64))); mBuilder.setContentTitle(conversation.getName()); - final Message message; + Message message; if ((message = getImage(messages)) != null) { modifyForImage(mBuilder, message, messages, notify); } else { modifyForTextOnly(mBuilder, messages, notify); } - mBuilder.setContentIntent(createContentIntent(conversation - .getUuid())); + if ((message = getFirstDownloadableMessage(messages)) != null) { + mBuilder.addAction( + R.drawable.ic_action_download, + mXmppConnectionService.getResources().getString( + message.getType() == Message.TYPE_IMAGE ? + R.string.download_image : + R.string.download_file + ), + createDownloadIntent(message) + ); + } + mBuilder.setContentIntent(createContentIntent(conversation)); } return mBuilder; } @@ -303,7 +312,7 @@ public class NotificationService { } } - private Message getImage(final ArrayList messages) { + private Message getImage(final Iterable messages) { for (final Message message : messages) { if (message.getType() == Message.TYPE_IMAGE && message.getDownloadable() == null @@ -314,7 +323,17 @@ public class NotificationService { return null; } - private String getMergedBodies(final ArrayList messages) { + private Message getFirstDownloadableMessage(final Iterable messages) { + for (final Message message : messages) { + if ((message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) && + message.getDownloadable() != null) { + return message; + } + } + return null; + } + + private CharSequence getMergedBodies(final ArrayList messages) { final StringBuilder text = new StringBuilder(); for (int i = 0; i < messages.size(); ++i) { text.append(UIHelper.getMessagePreview(mXmppConnectionService,messages.get(i)).first); @@ -325,25 +344,39 @@ public class NotificationService { return text.toString(); } - private PendingIntent createContentIntent(final String conversationUuid) { + private PendingIntent createContentIntent(final String conversationUuid, final String downloadMessageUuid) { final TaskStackBuilder stackBuilder = TaskStackBuilder .create(mXmppConnectionService); stackBuilder.addParentStack(ConversationActivity.class); final Intent viewConversationIntent = new Intent(mXmppConnectionService, ConversationActivity.class); - viewConversationIntent.setAction(Intent.ACTION_VIEW); + if (downloadMessageUuid != null) { + viewConversationIntent.setAction(ConversationActivity.ACTION_DOWNLOAD); + } else { + viewConversationIntent.setAction(Intent.ACTION_VIEW); + } if (conversationUuid != null) { - viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, - conversationUuid); + viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, conversationUuid); viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); } + if (downloadMessageUuid != null) { + viewConversationIntent.putExtra(ConversationActivity.MESSAGE, downloadMessageUuid); + } stackBuilder.addNextIntent(viewConversationIntent); return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); } + private PendingIntent createDownloadIntent(final Message message) { + return createContentIntent(message.getConversationUuid(), message.getUuid()); + } + + private PendingIntent createContentIntent(final Conversation conversation) { + return createContentIntent(conversation.getUuid(), null); + } + private PendingIntent createDeleteIntent() { final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class); @@ -445,7 +478,7 @@ public class NotificationService { mBuilder.setOngoing(true); //mBuilder.setLights(0xffffffff, 2000, 4000); mBuilder.setSmallIcon(R.drawable.ic_stat_alert_warning); - TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService); + final TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService); stackBuilder.addParentStack(ConversationActivity.class); final Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 4ba6c7738..1356e6161 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -15,7 +15,6 @@ import android.os.SystemClock; import android.provider.MediaStore; import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -33,7 +32,6 @@ import net.java.otr4j.session.SessionStatus; import java.util.ArrayList; import java.util.List; -import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Blockable; @@ -50,8 +48,11 @@ import eu.siacs.conversations.xmpp.OnUpdateBlocklist; public class ConversationActivity extends XmppActivity implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist { + public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD"; + public static final String VIEW_CONVERSATION = "viewConversation"; public static final String CONVERSATION = "conversationUuid"; + public static final String MESSAGE = "messageUuid"; public static final String TEXT = "text"; public static final String NICK = "nick"; @@ -823,10 +824,11 @@ public class ConversationActivity extends XmppActivity setIntent(new Intent()); } - private void handleViewConversationIntent(Intent intent) { - String uuid = (String) intent.getExtras().get(CONVERSATION); - String text = intent.getExtras().getString(TEXT, ""); - String nick = intent.getExtras().getString(NICK,null); + private void handleViewConversationIntent(final Intent intent) { + final String uuid = (String) intent.getExtras().get(CONVERSATION); + final String downloadUuid = (String) intent.getExtras().get(MESSAGE); + final String text = intent.getExtras().getString(TEXT, ""); + final String nick = intent.getExtras().getString(NICK, null); if (selectConversationByUuid(uuid)) { this.mConversationFragment.reInit(getSelectedConversation()); if (nick != null) { @@ -839,6 +841,12 @@ public class ConversationActivity extends XmppActivity if (mContentView instanceof SlidingPaneLayout) { updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet } + if (downloadUuid != null) { + final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid); + if (message != null) { + mConversationFragment.messageListAdapter.startDownloadable(message); + } + } } } diff --git a/src/main/res/drawable-hdpi/ic_action_download.png b/src/main/res/drawable-hdpi/ic_action_download.png new file mode 100644 index 0000000000000000000000000000000000000000..9fc6455ae11e3f84af8fa9287fbfbd840556ecbe GIT binary patch literal 363 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTC$r9IylHmNblJdl&R0hYC{G?O` z&)mfH)S%SFl*+=BsWuD@jJ%#Mjv*Dd-b}yC+hicpy7J>y#uNte2F5uD7-bq*3mC)` z7$hCUKE$#&u*Lv|82l#NDQYrJzwqkkq}gh`OP_yvzs=EsL$PJT(|}9%YhGJ96~27q zvod1W{;BVc9hgnP=zng4?u63CVWk;A?Ov|Xo3QYHcZ{k+M)RYt8-0I{@;*A>eXlhk zDcy(XX{&{wWbH&-21b98n;pwuhI~w#E#br^apROsFkcQ07M z=BgXhr^74esZOv^yi~;SMN&i7P~fwx8_@j%pFEWhFer<8TW?joum~7>44$rjF6*2U FngF7Si<$rc literal 0 HcmV?d00001 diff --git a/src/main/res/drawable-mdpi/ic_action_download.png b/src/main/res/drawable-mdpi/ic_action_download.png new file mode 100644 index 0000000000000000000000000000000000000000..84b43d34c3b365925e4c46230e8b791e4db9abbd GIT binary patch literal 313 zcmV-90mlA`P)mKcmTnG!T0Rp^%!C|+mfd1EB*b6~c7 zM(u3KizVwJ1DM=rAdaSb6hI4_SwtvpB-ZyNrvaeBAP3+p@s=xbgFfi*a1LB4OD@1^nOF^q<`nCtq(gSr% zLTHILlSYA-j^1z?@VbDJnC2E00jR9_%rKb1XtR9O0R!#;fB*vkkjyrTKyhbt00000 LNkvXXu0mjfg57w% literal 0 HcmV?d00001 diff --git a/src/main/res/drawable-xhdpi/ic_action_download.png b/src/main/res/drawable-xhdpi/ic_action_download.png new file mode 100644 index 0000000000000000000000000000000000000000..702ad476c581de01b5b27caf175da3a9214a4a71 GIT binary patch literal 470 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=k|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+7%Mzo978H@y_s>=_ppP2TkaHXRtH9t32Yh+TpX+um=-V; zvA^5Vu!CX8WF`-0JqGa!OlQ(F`))*9-|Kx_>o1kNC)dqZ7wYX9>h*8QK~#!OhY!qnN`$|gwT zNBd0`r?RGW*>8VOzcF&mF5exK`o65;oRMhVHuF12ruXOMn=oC8OggSxT?vzFPsc4@dsOt#+NtLm3gm*0@2#5u<^6{_+{>4o z+NxSM)#A%%x3*yR&TSQMm~^akmgn?EgM)}c!RsH31jAAJICtB&r7J*Up00i_>zopr E076E-F8}}l literal 0 HcmV?d00001 diff --git a/src/main/res/drawable-xxhdpi/ic_action_download.png b/src/main/res/drawable-xxhdpi/ic_action_download.png new file mode 100644 index 0000000000000000000000000000000000000000..6bcd83134a01ad5b656d02facc08024500e96849 GIT binary patch literal 574 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD1|%QND7OGok|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+7$11LIEGZ*dUNNZZ?l60dx60|s|LOYOnMI(cD2bakab{R z@xF}V_d3}Fj3teH3G4+7e>!GJOxDuXmHG1BTW`;lpndk`?Cpl9f%bub#Hu~~_M0@i zUf+nxElH}hdv$oH?Dr>5mc3<$N8}1p1OyrwaG@LX{o+rFyB1EqT7A8GTJ2u(dq<+r zp7;G^^WWkMgJ05Tk+(}0&o}6Obe_MAW7WDnp6>1SFL(X$Ji{2+QnVstw?OKLa&~+6 zKT>^i$;J0BJ#-8G@UE%OukGs8o%x$Xy5@&41Wx%b*4Db+;@p|y1HS~~k~T~H+A;rE z^9_?*Mf~@x!@n9aUfE%~_wL6UgY{L@mv+ojoMM|0p8VBlU;eCDK`xuJ9j`hEU0(fd zw|OeljiNh!+p;$Nue~~(>uNq53&pCc;uz%JZ zDc^lR`y#jA;=OLw0ln{>B;2ioIT;r0W#9OTPbK^P`--6LU!%9H{ok-s=U?kB`Dc?i sfPyt+-AR=_A$D6KQNfU~*PoG%;p5-r&?B3bKna4u)78&qol`;+0Do)tcmMzZ literal 0 HcmV?d00001 diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index f35b59ca1..e6776a606 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -85,6 +85,7 @@ Send OpenPGP encrypted message Your nickname has been changed Download Image + Download File Image file offered for download Send unencrypted Decryption failed. Maybe you don’t have the proper private key.